-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
chip-message-send.cpp
125 lines (107 loc) · 4.77 KB
/
chip-message-send.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, softwarEchoe
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* This file provides an implementation of functions for sending messages.
*/
#include <app/util/chip-message-send.h>
#include <assert.h>
#include <inet/InetLayer.h>
#include <messaging/ExchangeContext.h>
#include <messaging/ExchangeMgr.h>
#include <protocols/Protocols.h>
#include <support/logging/CHIPLogging.h>
#include <transport/SecureSessionMgr.h>
#include <transport/raw/MessageHeader.h>
using namespace chip;
// TODO: This is not ideal, but we're still sorting out how secure session
// managers end up working and whether they're singletons. In the long term,
// there will be some sane API that lets us send a message to a given node id.
//
// https://github.com/project-chip/connectedhomeip/issues/2566 tracks that API.
namespace chip {
// TODO: This is a placeholder delegate for exchange context created in Device::SendMessage()
// Delete this class when Device::SendMessage() is obsoleted.
class DeviceExchangeDelegate : public Messaging::ExchangeDelegate
{
void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
System::PacketBufferHandle payload) override
{}
void OnResponseTimeout(Messaging::ExchangeContext * ec) override {}
};
extern SecureSessionMgr & SessionManager();
extern Messaging::ExchangeManager * ExchangeManager();
} // namespace chip
EmberStatus chipSendUnicast(NodeId destination, EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * message)
{
uint16_t frameSize = encodeApsFrame(nullptr, 0, apsFrame);
uint32_t dataLengthUnchecked = uint32_t(frameSize) + uint32_t(messageLength);
if (dataLengthUnchecked > UINT16_MAX)
{
// Definitely too long for a packet!
return EMBER_MESSAGE_TOO_LONG;
}
uint16_t dataLength = static_cast<uint16_t>(dataLengthUnchecked);
if (frameSize == 0)
{
ChipLogError(Zcl, "Error encoding APS frame");
return EMBER_ERR_FATAL;
}
System::PacketBufferHandle buffer = MessagePacketBuffer::New(dataLength);
if (buffer.IsNull())
{
// FIXME: Not quite right... what's the right way to indicate "out of
// heap"?
return EMBER_MESSAGE_TOO_LONG;
}
if (encodeApsFrame(buffer->Start(), dataLength, apsFrame) != frameSize)
{
// Something is very wrong here; our first call lied to us!
ChipLogError(Zcl, "Something wrong happened trying to encode aps frame to respond with");
return EMBER_ERR_FATAL;
}
memcpy(buffer->Start() + frameSize, message, messageLength);
buffer->SetDataLength(dataLength);
// TODO: temporary create a handle from node id, will be fix in PR 3602
Messaging::ExchangeManager * exchangeMgr = ExchangeManager();
if (exchangeMgr == nullptr)
{
return EMBER_DELIVERY_FAILED;
}
Messaging::ExchangeContext * exchange = exchangeMgr->NewContext({ destination, Transport::kAnyKeyId, 0 }, nullptr);
if (exchange == nullptr)
{
return EMBER_DELIVERY_FAILED;
}
// TODO(#5675): This code is temporary, and must be updated to use the IM API. Currently, we use a temporary Protocol
// TempZCL to carry over legacy ZCL messages, use an ephemeral exchange to send message and use its unsolicited message
// handler to receive messages. We need to set flag kFromInitiator to allow receiver to deliver message to corresponding
// unsolicited message handler, and we also need to set flag kNoAutoRequestAck since there is no persistent exchange to
// receive the ack message. This logic needs to be deleted after we convert all legacy ZCL messages to IM messages.
DeviceExchangeDelegate delegate;
exchange->SetDelegate(&delegate);
Messaging::SendFlags sendFlags;
sendFlags.Set(Messaging::SendMessageFlags::kFromInitiator).Set(Messaging::SendMessageFlags::kNoAutoRequestAck);
CHIP_ERROR err = exchange->SendMessage(Protocols::TempZCL::Id, 0, std::move(buffer), sendFlags);
exchange->Close();
if (err != CHIP_NO_ERROR)
{
// FIXME: Figure out better translations between our error types?
return EMBER_DELIVERY_FAILED;
}
return EMBER_SUCCESS;
}