|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +#include "DeviceSynchronization.h" |
| 20 | +#include "rpc/RpcClient.h" |
| 21 | + |
| 22 | +#include <app/InteractionModelEngine.h> |
| 23 | +#include <app/server/Server.h> |
| 24 | + |
| 25 | +#include <app-common/zap-generated/ids/Attributes.h> |
| 26 | +#include <app-common/zap-generated/ids/Clusters.h> |
| 27 | + |
| 28 | +using namespace ::chip; |
| 29 | +using namespace ::chip::app; |
| 30 | +using chip::app::ReadClient; |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +void OnDeviceConnectedWrapper(void * context, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle) |
| 35 | +{ |
| 36 | + reinterpret_cast<DeviceSynchronizer *>(context)->OnDeviceConnected(exchangeMgr, sessionHandle); |
| 37 | +} |
| 38 | + |
| 39 | +void OnDeviceConnectionFailureWrapper(void * context, const ScopedNodeId & peerId, CHIP_ERROR error) |
| 40 | +{ |
| 41 | + reinterpret_cast<DeviceSynchronizer *>(context)->OnDeviceConnectionFailure(peerId, error); |
| 42 | +} |
| 43 | + |
| 44 | +bool SuccessOrLog(CHIP_ERROR err, const char * name) |
| 45 | +{ |
| 46 | + if (err == CHIP_NO_ERROR) |
| 47 | + { |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + ChipLogError(NotSpecified, "Failed to read %s: %" CHIP_ERROR_FORMAT, name, err.Format()); |
| 52 | + |
| 53 | + return false; |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
| 58 | +DeviceSynchronizer & DeviceSynchronizer::Instance() |
| 59 | +{ |
| 60 | + static DeviceSynchronizer instance; |
| 61 | + return instance; |
| 62 | +} |
| 63 | + |
| 64 | +DeviceSynchronizer::DeviceSynchronizer() : |
| 65 | + mOnDeviceConnectedCallback(OnDeviceConnectedWrapper, this), |
| 66 | + mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureWrapper, this) |
| 67 | +{} |
| 68 | + |
| 69 | +void DeviceSynchronizer::OnAttributeData(const ConcreteDataAttributePath & path, TLV::TLVReader * data, const StatusIB & status) |
| 70 | +{ |
| 71 | + VerifyOrDie(path.mEndpointId == kRootEndpointId); |
| 72 | + VerifyOrDie(path.mClusterId == Clusters::BasicInformation::Id); |
| 73 | + |
| 74 | + switch (path.mAttributeId) |
| 75 | + { |
| 76 | + case Clusters::BasicInformation::Attributes::UniqueID::Id: |
| 77 | + mCurrentDeviceData.has_unique_id = |
| 78 | + SuccessOrLog(data->GetString(mCurrentDeviceData.unique_id, sizeof(mCurrentDeviceData.unique_id)), "UniqueId"); |
| 79 | + break; |
| 80 | + case Clusters::BasicInformation::Attributes::VendorName::Id: |
| 81 | + mCurrentDeviceData.has_vendor_name = |
| 82 | + SuccessOrLog(data->GetString(mCurrentDeviceData.vendor_name, sizeof(mCurrentDeviceData.vendor_name)), "VendorName"); |
| 83 | + break; |
| 84 | + case Clusters::BasicInformation::Attributes::VendorID::Id: |
| 85 | + mCurrentDeviceData.has_vendor_id = SuccessOrLog(data->Get(mCurrentDeviceData.vendor_id), "VendorID"); |
| 86 | + break; |
| 87 | + case Clusters::BasicInformation::Attributes::ProductName::Id: |
| 88 | + mCurrentDeviceData.has_product_name = |
| 89 | + SuccessOrLog(data->GetString(mCurrentDeviceData.product_name, sizeof(mCurrentDeviceData.product_name)), "ProductName"); |
| 90 | + break; |
| 91 | + case Clusters::BasicInformation::Attributes::ProductID::Id: |
| 92 | + mCurrentDeviceData.has_product_id = SuccessOrLog(data->Get(mCurrentDeviceData.product_id), "ProductID"); |
| 93 | + break; |
| 94 | + case Clusters::BasicInformation::Attributes::NodeLabel::Id: |
| 95 | + mCurrentDeviceData.has_node_label = |
| 96 | + SuccessOrLog(data->GetString(mCurrentDeviceData.node_label, sizeof(mCurrentDeviceData.node_label)), "NodeLabel"); |
| 97 | + break; |
| 98 | + case Clusters::BasicInformation::Attributes::HardwareVersion::Id: |
| 99 | + mCurrentDeviceData.has_hardware_version = SuccessOrLog(data->Get(mCurrentDeviceData.hardware_version), "HardwareVersion"); |
| 100 | + break; |
| 101 | + case Clusters::BasicInformation::Attributes::HardwareVersionString::Id: |
| 102 | + mCurrentDeviceData.has_hardware_version_string = SuccessOrLog( |
| 103 | + data->GetString(mCurrentDeviceData.hardware_version_string, sizeof(mCurrentDeviceData.hardware_version_string)), |
| 104 | + "HardwareVersionString"); |
| 105 | + break; |
| 106 | + case Clusters::BasicInformation::Attributes::SoftwareVersion::Id: |
| 107 | + mCurrentDeviceData.has_software_version = SuccessOrLog(data->Get(mCurrentDeviceData.software_version), "HardwareVersion"); |
| 108 | + break; |
| 109 | + case Clusters::BasicInformation::Attributes::SoftwareVersionString::Id: |
| 110 | + mCurrentDeviceData.has_software_version_string = SuccessOrLog( |
| 111 | + data->GetString(mCurrentDeviceData.software_version_string, sizeof(mCurrentDeviceData.software_version_string)), |
| 112 | + "SoftwareVersionString"); |
| 113 | + break; |
| 114 | + default: |
| 115 | + break; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void DeviceSynchronizer::OnReportEnd() |
| 120 | +{ |
| 121 | + // Report end is at the end of all attributes (success) |
| 122 | +#if defined(PW_RPC_ENABLED) |
| 123 | + AddSynchronizedDevice(mCurrentDeviceData); |
| 124 | +#else |
| 125 | + ChipLogError(NotSpecified, "Cannot synchronize device with fabric bridge: RPC not enabled"); |
| 126 | +#endif |
| 127 | +} |
| 128 | + |
| 129 | +void DeviceSynchronizer::OnDone(chip::app::ReadClient * apReadClient) |
| 130 | +{ |
| 131 | + // Nothing to do: error reported on OnError or report ended called. |
| 132 | + mDeviceSyncInProcess = false; |
| 133 | +} |
| 134 | + |
| 135 | +void DeviceSynchronizer::OnError(CHIP_ERROR error) |
| 136 | +{ |
| 137 | + ChipLogProgress(NotSpecified, "Error fetching device data: %" CHIP_ERROR_FORMAT, error.Format()); |
| 138 | +} |
| 139 | + |
| 140 | +void DeviceSynchronizer::OnDeviceConnected(chip::Messaging::ExchangeManager & exchangeMgr, |
| 141 | + const chip::SessionHandle & sessionHandle) |
| 142 | +{ |
| 143 | + mClient = std::make_unique<ReadClient>(app::InteractionModelEngine::GetInstance(), &exchangeMgr /* echangeMgr */, |
| 144 | + *this /* callback */, ReadClient::InteractionType::Read); |
| 145 | + VerifyOrDie(mClient); |
| 146 | + |
| 147 | + AttributePathParams readPaths[1]; |
| 148 | + readPaths[0] = AttributePathParams(kRootEndpointId, Clusters::BasicInformation::Id); |
| 149 | + |
| 150 | + ReadPrepareParams readParams(sessionHandle); |
| 151 | + |
| 152 | + readParams.mpAttributePathParamsList = readPaths; |
| 153 | + readParams.mAttributePathParamsListSize = 1; |
| 154 | + |
| 155 | + CHIP_ERROR err = mClient->SendRequest(readParams); |
| 156 | + |
| 157 | + if (err != CHIP_NO_ERROR) |
| 158 | + { |
| 159 | + ChipLogError(NotSpecified, "Failed to issue read for BasicInformation data"); |
| 160 | + mDeviceSyncInProcess = false; |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +void DeviceSynchronizer::OnDeviceConnectionFailure(const chip::ScopedNodeId & peerId, CHIP_ERROR error) |
| 165 | +{ |
| 166 | + ChipLogError(NotSpecified, "Device Sync failed to connect to " ChipLogFormatX64, ChipLogValueX64(peerId.GetNodeId())); |
| 167 | + mDeviceSyncInProcess = false; |
| 168 | +} |
| 169 | + |
| 170 | +void DeviceSynchronizer::StartDeviceSynchronization(chip::Controller::DeviceController & controller, chip::NodeId nodeId, |
| 171 | + bool deviceIsIcd) |
| 172 | +{ |
| 173 | + if (mDeviceSyncInProcess) |
| 174 | + { |
| 175 | + ChipLogError(NotSpecified, "Device Sync NOT POSSIBLE: another sync is in progress"); |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + mCurrentDeviceData = chip_rpc_SynchronizedDevice_init_default; |
| 180 | + mCurrentDeviceData.node_id = nodeId; |
| 181 | + mCurrentDeviceData.has_is_icd = true; |
| 182 | + mCurrentDeviceData.is_icd = deviceIsIcd; |
| 183 | + |
| 184 | + mDeviceSyncInProcess = true; |
| 185 | + |
| 186 | + controller.GetConnectedDevice(nodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); |
| 187 | +} |
0 commit comments