-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
WriteClient.cpp
400 lines (329 loc) · 12.8 KB
/
WriteClient.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* 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, software
* 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 defines the initiator side of a CHIP Write Interaction.
*
*/
#include <app/InteractionModelEngine.h>
#include <app/WriteClient.h>
namespace chip {
namespace app {
CHIP_ERROR WriteClient::Init(Messaging::ExchangeManager * apExchangeMgr, InteractionModelDelegate * apDelegate, intptr_t aAppIdentifier)
{
CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrReturnError(apExchangeMgr != nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mpExchangeMgr == nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mpExchangeCtx == nullptr, CHIP_ERROR_INCORRECT_STATE);
AttributeDataList::Builder attributeDataListBuilder;
System::PacketBufferHandle packet = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
VerifyOrReturnError(!packet.IsNull(), err = CHIP_ERROR_NO_MEMORY);
mMessageWriter.Init(std::move(packet));
ReturnErrorOnFailure(mWriteRequestBuilder.Init(&mMessageWriter));
attributeDataListBuilder = mWriteRequestBuilder.CreateAttributeDataListBuilder();
ReturnErrorOnFailure(attributeDataListBuilder.GetError());
ClearExistingExchangeContext();
mpExchangeMgr = apExchangeMgr;
mpDelegate = apDelegate;
mAppIdentifier = aAppIdentifier;
mAttributeStatusIndex = 0;
MoveToState(State::Initialized);
return CHIP_NO_ERROR;
}
void WriteClient::Shutdown()
{
VerifyOrReturn(mState != State::Uninitialized);
mMessageWriter.Reset();
ClearExistingExchangeContext();
mpExchangeMgr = nullptr;
mpDelegate = nullptr;
mAttributeStatusIndex = 0;
ClearState();
}
CHIP_ERROR WriteClient::ClearExistingExchangeContext()
{
// Discard any existing exchange context. Effectively we can only have one Echo exchange with
// a single node at any one time.
if (mpExchangeCtx != nullptr)
{
mpExchangeCtx->Abort();
mpExchangeCtx = nullptr;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR WriteClient::ProcessWriteResponseMessage(System::PacketBufferHandle && payload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::System::PacketBufferTLVReader reader;
chip::TLV::TLVReader attributeStatusListReader;
WriteResponse::Parser writeResponse;
AttributeStatusList::Parser attributeStatusListParser;
reader.Init(std::move(payload));
err = reader.Next();
SuccessOrExit(err);
err = writeResponse.Init(reader);
SuccessOrExit(err);
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
err = writeResponse.CheckSchemaValidity();
SuccessOrExit(err);
#endif
err = writeResponse.GetAttributeStatusList(&attributeStatusListParser);
SuccessOrExit(err);
attributeStatusListParser.GetReader(&attributeStatusListReader);
while (CHIP_NO_ERROR == (err = attributeStatusListReader.Next()))
{
VerifyOrExit(chip::TLV::AnonymousTag == attributeStatusListReader.GetTag(), err = CHIP_ERROR_INVALID_TLV_TAG);
VerifyOrExit(chip::TLV::kTLVType_Structure == attributeStatusListReader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
AttributeStatusElement::Parser element;
err = element.Init(attributeStatusListReader);
SuccessOrExit(err);
err = ProcessAttributeStatusElement(element);
SuccessOrExit(err);
}
// if we have exhausted this container
if (CHIP_END_OF_TLV == err)
{
err = CHIP_NO_ERROR;
}
exit:
ChipLogFunctError(err);
return err;
}
CHIP_ERROR WriteClient::PrepareAttribute(const AttributePathParams & attributePathParams)
{
CHIP_ERROR err = CHIP_NO_ERROR;
AttributeDataElement::Builder attributeDataElement =
mWriteRequestBuilder.GetAttributeDataListBuilder().CreateAttributeDataElementBuilder();
err = attributeDataElement.GetError();
SuccessOrExit(err);
err = ConstructAttributePath(attributePathParams, attributeDataElement);
exit:
ChipLogFunctError(err);
return err;
}
CHIP_ERROR WriteClient::FinishAttribute()
{
CHIP_ERROR err = CHIP_NO_ERROR;
AttributeDataElement::Builder attributeDataElement = mWriteRequestBuilder.GetAttributeDataListBuilder().GetAttributeDataElementBuilder();
attributeDataElement.EndOfAttributeDataElement();
err = attributeDataElement.GetError();
SuccessOrExit(err);
MoveToState(State::AddAttribute);
exit:
ChipLogFunctError(err);
return err;
}
TLV::TLVWriter * WriteClient::GetAttributeDataElementTLVWriter()
{
return mWriteRequestBuilder.GetAttributeDataListBuilder().GetAttributeDataElementBuilder().GetWriter();
}
CHIP_ERROR WriteClient::ConstructAttributePath(const AttributePathParams & aAttributePathParams,
AttributeDataElement::Builder aAttributeDataElement)
{
AttributePath::Builder attributePath = aAttributeDataElement.CreateAttributePathBuilder();
if (aAttributePathParams.mFlags.Has(AttributePathParams::Flags::kFieldIdValid))
{
attributePath.FieldId(aAttributePathParams.mFieldId);
}
if (aAttributePathParams.mFlags.Has(AttributePathParams::Flags::kListIndexValid))
{
attributePath.ListIndex(aAttributePathParams.mListIndex);
}
attributePath.NodeId(aAttributePathParams.mNodeId).ClusterId(aAttributePathParams.mClusterId).EndpointId(aAttributePathParams.mEndpointId).EndOfAttributePath();
return attributePath.GetError();
}
CHIP_ERROR WriteClient::FinalizeMessage(System::PacketBufferHandle & aPacket)
{
CHIP_ERROR err = CHIP_NO_ERROR;
AttributeDataList::Builder attributeDataListBuilder;
VerifyOrExit(mState == State::AddAttribute, err = CHIP_ERROR_INCORRECT_STATE);
attributeDataListBuilder = mWriteRequestBuilder.GetAttributeDataListBuilder().EndOfAttributeDataList();
err = attributeDataListBuilder.GetError();
SuccessOrExit(err);
mWriteRequestBuilder.EndOfWriteRequest();
err = mWriteRequestBuilder.GetError();
SuccessOrExit(err);
err = mMessageWriter.Finalize(&aPacket);
SuccessOrExit(err);
exit:
ChipLogFunctError(err);
return err;
}
const char * WriteClient::GetStateStr() const
{
#if CHIP_DETAIL_LOGGING
switch (mState)
{
case State::Uninitialized:
return "Uninitialized";
case State::Initialized:
return "Initialized";
case State::AddAttribute:
return "AddAttribute";
case State::AwaitingResponse:
return "AwaitingResponse";
}
#endif // CHIP_DETAIL_LOGGING
return "N/A";
}
void WriteClient::MoveToState(const State aTargetState)
{
mState = aTargetState;
ChipLogDetail(DataManagement, "WriteClient moving to [%10.10s]", GetStateStr());
}
void WriteClient::ClearState()
{
MoveToState(State::Uninitialized);
}
CHIP_ERROR WriteClient::SendWriteRequest(NodeId aNodeId, Transport::AdminId aAdminId, SecureSessionHandle * apSecureSession)
{
CHIP_ERROR err = CHIP_NO_ERROR;
System::PacketBufferHandle packet;
VerifyOrExit(mState == State::AddAttribute, err = CHIP_ERROR_INCORRECT_STATE);
err = FinalizeMessage(packet);
SuccessOrExit(err);
// Discard any existing exchange context. Effectively we can only have one exchange per CommandSender
// at any one time.
ClearExistingExchangeContext();
// Create a new exchange context.
// TODO: temprary create a SecureSessionHandle from node id, will be fix in PR 3602
// TODO: Hard code keyID to 0 to unblock IM end-to-end test. Complete solution is tracked in issue:4451
if (apSecureSession == nullptr)
{
mpExchangeCtx = mpExchangeMgr->NewContext({ aNodeId, 0, aAdminId }, this);
}
else
{
mpExchangeCtx = mpExchangeMgr->NewContext(*apSecureSession, this);
}
VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);
mpExchangeCtx->SetResponseTimeout(kImMessageTimeoutMsec);
err = mpExchangeCtx->SendMessage(
Protocols::InteractionModel::MsgType::InvokeCommandRequest, std::move(packet),
Messaging::SendFlags(Messaging::SendMessageFlags::kExpectResponse).Set(Messaging::SendMessageFlags::kNoAutoRequestAck));
SuccessOrExit(err);
MoveToState(State::AwaitingResponse);
exit:
if (err != CHIP_NO_ERROR)
{
ClearExistingExchangeContext();
}
ChipLogFunctError(err);
return err;
}
void WriteClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
// Assert that the exchange context matches the client's current context.
// This should never fail because even if SendCommandRequest is called
// back-to-back, the second call will call Close() on the first exchange,
// which clears the OnMessageReceived callback.
VerifyOrDie(apExchangeContext == mpExchangeCtx);
// Verify that the message is an Invoke Command Response.
// If not, close the exchange and free the payload.
if (!aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::InvokeCommandResponse))
{
apExchangeContext->Close();
mpExchangeCtx = nullptr;
ExitNow();
}
// Close the current exchange after receiving the response since the response message marks the
// end of conversation represented by the exchange. We should create an new exchange for a new
// conversation defined in Interaction Model protocol.
ClearExistingExchangeContext();
err = ProcessWriteResponseMessage(std::move(aPayload));
exit:
if (mpDelegate != nullptr)
{
if (err != CHIP_NO_ERROR)
{
mpDelegate->WriteResponseError(this, err);
}
else
{
mpDelegate->WriteResponseProcessed(this);
}
}
Shutdown();
}
void WriteClient::OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext)
{
ChipLogProgress(DataManagement, "Time out! failed to receive write response from Exchange: %d",
apExchangeContext->GetExchangeId());
if (mpDelegate != nullptr)
{
mpDelegate->WriteResponseError(this, CHIP_ERROR_TIMEOUT);
}
Shutdown();
}
CHIP_ERROR WriteClient::ProcessAttributeStatusElement(AttributeStatusElement::Parser & aAttributeStatusElement)
{
CHIP_ERROR err = CHIP_NO_ERROR;
AttributePath::Parser attributePath;
Protocols::SecureChannel::GeneralStatusCode generalCode = Protocols::SecureChannel::GeneralStatusCode::kSuccess;
uint32_t protocolId = 0;
uint16_t protocolCode = 0;
StatusElement::Parser statusElementParser;
AttributePathParams attributePathParams;
mAttributeStatusIndex++;
err = aAttributeStatusElement.GetAttributePath(&attributePath);
SuccessOrExit(err);
err = attributePath.GetNodeId(&(attributePathParams.mNodeId));
SuccessOrExit(err);
err = attributePath.GetClusterId(&(attributePathParams.mClusterId));
SuccessOrExit(err);
err = attributePath.GetEndpointId(&(attributePathParams.mEndpointId));
SuccessOrExit(err);
err = attributePath.GetFieldId(&(attributePathParams.mFieldId));
if (CHIP_NO_ERROR == err)
{
attributePathParams.mFlags.Set(AttributePathParams::Flags::kFieldIdValid);
}
else if (CHIP_END_OF_TLV == err)
{
err = CHIP_NO_ERROR;
}
SuccessOrExit(err);
err = attributePath.GetListIndex(&(attributePathParams.mListIndex));
if (CHIP_NO_ERROR == err)
{
VerifyOrExit(attributePathParams.mFlags.Has(AttributePathParams::Flags::kFieldIdValid), err = CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_PATH);
attributePathParams.mFlags.Set(AttributePathParams::Flags::kListIndexValid);
}
err = aAttributeStatusElement.GetStatusElement(&(statusElementParser));
if (CHIP_NO_ERROR == err)
{
err = statusElementParser.DecodeStatusElement(&generalCode, &protocolId, &protocolCode);
SuccessOrExit(err);
if (mpDelegate != nullptr)
{
mpDelegate->WriteResponseStatus(this, generalCode, protocolId, protocolCode, attributePathParams,
mAttributeStatusIndex);
}
}
exit:
ChipLogFunctError(err);
if (err != CHIP_NO_ERROR && mpDelegate != nullptr)
{
mpDelegate->WriteResponseProtocolError(this, mAttributeStatusIndex);
}
return err;
}
};
};