-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathDeviceSynchronization.cpp
321 lines (273 loc) · 11.5 KB
/
DeviceSynchronization.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
/*
* Copyright (c) 2024 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.
*
*/
#include "DeviceSynchronization.h"
#include "DeviceSubscriptionManager.h"
#if defined(PW_RPC_ENABLED)
#include "rpc/RpcClient.h"
#endif
#include <app/InteractionModelEngine.h>
#include <app/server/Server.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <device_manager/DeviceManager.h>
using namespace ::chip;
using namespace ::chip::app;
using chip::app::ReadClient;
namespace {
void OnDeviceConnectedWrapper(void * context, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle)
{
reinterpret_cast<DeviceSynchronizer *>(context)->OnDeviceConnected(exchangeMgr, sessionHandle);
}
void OnDeviceConnectionFailureWrapper(void * context, const ScopedNodeId & peerId, CHIP_ERROR error)
{
reinterpret_cast<DeviceSynchronizer *>(context)->OnDeviceConnectionFailure(peerId, error);
}
#if defined(PW_RPC_ENABLED)
bool SuccessOrLog(CHIP_ERROR err, const char * name)
{
if (err == CHIP_NO_ERROR)
{
return true;
}
ChipLogError(NotSpecified, "Failed to read %s: %" CHIP_ERROR_FORMAT, name, err.Format());
return false;
}
#endif
} // namespace
DeviceSynchronizer & DeviceSynchronizer::Instance()
{
static DeviceSynchronizer instance;
return instance;
}
DeviceSynchronizer::DeviceSynchronizer() :
mOnDeviceConnectedCallback(OnDeviceConnectedWrapper, this),
mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureWrapper, this)
{}
void DeviceSynchronizer::OnAttributeData(const ConcreteDataAttributePath & path, TLV::TLVReader * data, const StatusIB & status)
{
VerifyOrDie(path.mEndpointId == kRootEndpointId);
VerifyOrDie(path.mClusterId == Clusters::BasicInformation::Id);
if (!status.IsSuccess())
{
ChipLogError(NotSpecified, "Response Failure: %" CHIP_ERROR_FORMAT, status.ToChipError().Format());
return;
}
switch (path.mAttributeId)
{
#if defined(PW_RPC_ENABLED)
case Clusters::BasicInformation::Attributes::UniqueID::Id:
mCurrentDeviceData.has_unique_id =
SuccessOrLog(data->GetString(mCurrentDeviceData.unique_id, sizeof(mCurrentDeviceData.unique_id)), "UniqueId");
break;
case Clusters::BasicInformation::Attributes::VendorName::Id:
mCurrentDeviceData.has_vendor_name =
SuccessOrLog(data->GetString(mCurrentDeviceData.vendor_name, sizeof(mCurrentDeviceData.vendor_name)), "VendorName");
break;
case Clusters::BasicInformation::Attributes::VendorID::Id:
mCurrentDeviceData.has_vendor_id = SuccessOrLog(data->Get(mCurrentDeviceData.vendor_id), "VendorID");
break;
case Clusters::BasicInformation::Attributes::ProductName::Id:
mCurrentDeviceData.has_product_name =
SuccessOrLog(data->GetString(mCurrentDeviceData.product_name, sizeof(mCurrentDeviceData.product_name)), "ProductName");
break;
case Clusters::BasicInformation::Attributes::ProductID::Id:
mCurrentDeviceData.has_product_id = SuccessOrLog(data->Get(mCurrentDeviceData.product_id), "ProductID");
break;
case Clusters::BasicInformation::Attributes::NodeLabel::Id:
mCurrentDeviceData.has_node_label =
SuccessOrLog(data->GetString(mCurrentDeviceData.node_label, sizeof(mCurrentDeviceData.node_label)), "NodeLabel");
break;
case Clusters::BasicInformation::Attributes::HardwareVersion::Id:
mCurrentDeviceData.has_hardware_version = SuccessOrLog(data->Get(mCurrentDeviceData.hardware_version), "HardwareVersion");
break;
case Clusters::BasicInformation::Attributes::HardwareVersionString::Id:
mCurrentDeviceData.has_hardware_version_string = SuccessOrLog(
data->GetString(mCurrentDeviceData.hardware_version_string, sizeof(mCurrentDeviceData.hardware_version_string)),
"HardwareVersionString");
break;
case Clusters::BasicInformation::Attributes::SoftwareVersion::Id:
mCurrentDeviceData.has_software_version = SuccessOrLog(data->Get(mCurrentDeviceData.software_version), "HardwareVersion");
break;
case Clusters::BasicInformation::Attributes::SoftwareVersionString::Id:
mCurrentDeviceData.has_software_version_string = SuccessOrLog(
data->GetString(mCurrentDeviceData.software_version_string, sizeof(mCurrentDeviceData.software_version_string)),
"SoftwareVersionString");
break;
#endif // #if defined(PW_RPC_ENABLED)
default:
break;
}
}
void DeviceSynchronizer::OnReportEnd()
{
// Report end is at the end of all attributes (success)
MoveToState(State::ReceivedResponse);
}
void DeviceSynchronizer::OnDone(chip::app::ReadClient * apReadClient)
{
#if defined(PW_RPC_ENABLED)
if (mState == State::ReceivedResponse && !DeviceMgr().IsCurrentBridgeDevice(mNodeId))
{
GetUniqueId();
if (mState == State::GettingUid)
{
// GetUniqueId was successful and we rely on callback to call SynchronizationCompleteAddDevice.
return;
}
SynchronizationCompleteAddDevice();
}
#else
ChipLogError(NotSpecified, "Cannot synchronize device with fabric bridge: RPC not enabled");
#endif
MoveToState(State::Idle);
}
void DeviceSynchronizer::OnError(CHIP_ERROR error)
{
MoveToState(State::ReceivedError);
ChipLogProgress(NotSpecified, "Error fetching device data: %" CHIP_ERROR_FORMAT, error.Format());
}
void DeviceSynchronizer::OnDeviceConnected(chip::Messaging::ExchangeManager & exchangeMgr,
const chip::SessionHandle & sessionHandle)
{
mClient = std::make_unique<ReadClient>(app::InteractionModelEngine::GetInstance(), &exchangeMgr /* echangeMgr */,
*this /* callback */, ReadClient::InteractionType::Read);
VerifyOrDie(mClient);
AttributePathParams readPaths[1];
readPaths[0] = AttributePathParams(kRootEndpointId, Clusters::BasicInformation::Id);
ReadPrepareParams readParams(sessionHandle);
readParams.mpAttributePathParamsList = readPaths;
readParams.mAttributePathParamsListSize = 1;
CHIP_ERROR err = mClient->SendRequest(readParams);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to issue read for BasicInformation data");
MoveToState(State::Idle);
}
MoveToState(State::AwaitingResponse);
}
void DeviceSynchronizer::OnDeviceConnectionFailure(const chip::ScopedNodeId & peerId, CHIP_ERROR error)
{
ChipLogError(NotSpecified, "Device Sync failed to connect to " ChipLogFormatX64, ChipLogValueX64(peerId.GetNodeId()));
MoveToState(State::Idle);
}
void DeviceSynchronizer::StartDeviceSynchronization(chip::Controller::DeviceController * controller, chip::NodeId nodeId,
bool deviceIsIcd)
{
VerifyOrDie(controller);
if (mState != State::Idle)
{
ChipLogError(NotSpecified, "Device Sync NOT POSSIBLE: another sync is in progress");
return;
}
mNodeId = nodeId;
#if defined(PW_RPC_ENABLED)
mCurrentDeviceData = chip_rpc_SynchronizedDevice_init_default;
mCurrentDeviceData.node_id = nodeId;
mCurrentDeviceData.has_is_icd = true;
mCurrentDeviceData.is_icd = deviceIsIcd;
#endif
ReturnOnFailure(controller->GetConnectedDevice(nodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback));
mController = controller;
MoveToState(State::Connecting);
}
void DeviceSynchronizer::GetUniqueId()
{
VerifyOrDie(mState == State::ReceivedResponse);
VerifyOrDie(mController);
#if defined(PW_RPC_ENABLED)
// If we have a UniqueId we can return leaving state in ReceivedResponse.
VerifyOrReturn(!mCurrentDeviceData.has_unique_id, ChipLogDetail(NotSpecified, "We already have UniqueId"));
#endif
auto * device = DeviceMgr().FindDeviceByNode(mNodeId);
// If there is no associated remote Fabric Sync Aggregator there is no other place for us to try
// getting the UniqueId from and can return leaving the state in ReceivedResponse.
VerifyOrReturn(device, ChipLogDetail(NotSpecified, "No remote Fabric Sync Aggregator to get UniqueId from"));
// Because device is not-null we expect IsFabricSyncReady to be true. IsFabricSyncReady indicates we have a
// connection to the remote Fabric Sync Aggregator.
VerifyOrDie(DeviceMgr().IsFabricSyncReady());
auto remoteBridgeNodeId = DeviceMgr().GetRemoteBridgeNodeId();
EndpointId remoteEndpointIdOfInterest = device->GetEndpointId();
ChipLogDetail(NotSpecified, "Attempting to get UniqueId from remote Fabric Sync Aggregator") CHIP_ERROR err =
mUniqueIdGetter.GetUniqueId(
[this](std::optional<CharSpan> aUniqueId) {
if (aUniqueId.has_value())
{
#if defined(PW_RPC_ENABLED)
this->mCurrentDeviceData.has_unique_id = true;
memcpy(this->mCurrentDeviceData.unique_id, aUniqueId.value().data(), aUniqueId.value().size());
#endif
}
else
{
ChipLogError(NotSpecified, "We expected to get UniqueId from remote Fabric Sync Aggregator, but failed");
}
this->SynchronizationCompleteAddDevice();
},
*mController, remoteBridgeNodeId, remoteEndpointIdOfInterest);
if (err == CHIP_NO_ERROR)
{
MoveToState(State::GettingUid);
}
else
{
ChipLogDetail(NotSpecified, "Failed to get UniqueId from remote Fabric Sync Aggregator")
}
}
void DeviceSynchronizer::SynchronizationCompleteAddDevice()
{
VerifyOrDie(mState == State::ReceivedResponse || mState == State::GettingUid);
#if defined(PW_RPC_ENABLED)
AddSynchronizedDevice(mCurrentDeviceData);
// TODO(#35077) Figure out how we should reflect CADMIN values of ICD.
if (!mCurrentDeviceData.is_icd)
{
VerifyOrDie(mController);
// TODO(#35333) Figure out how we should recover in this circumstance.
CHIP_ERROR err = DeviceSubscriptionManager::Instance().StartSubscription(*mController, mNodeId);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed start subscription to NodeId:" ChipLogFormatX64, ChipLogValueX64(mNodeId));
}
}
#endif
MoveToState(State::Idle);
}
void DeviceSynchronizer::MoveToState(const State targetState)
{
mState = targetState;
ChipLogDetail(NotSpecified, "DeviceSynchronizer moving to [%10.10s]", GetStateStr());
}
const char * DeviceSynchronizer::GetStateStr() const
{
switch (mState)
{
case State::Idle:
return "Idle";
case State::Connecting:
return "Connecting";
case State::AwaitingResponse:
return "AwaitingResponse";
case State::ReceivedResponse:
return "ReceivedResponse";
case State::ReceivedError:
return "ReceivedError";
case State::GettingUid:
return "GettingUid";
}
return "N/A";
}