|
| 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 "BridgeSubscription.h" |
| 20 | +#include "DeviceManager.h" |
| 21 | + |
| 22 | +using namespace ::chip; |
| 23 | +using namespace ::chip::app; |
| 24 | +using chip::app::ReadClient; |
| 25 | + |
| 26 | +namespace admin { |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +constexpr uint16_t kSubscribeMinInterval = 0; |
| 31 | +constexpr uint16_t kSubscribeMaxInterval = 60; |
| 32 | + |
| 33 | +void OnDeviceConnectedWrapper(void * context, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle) |
| 34 | +{ |
| 35 | + reinterpret_cast<BridgeSubscription *>(context)->OnDeviceConnected(exchangeMgr, sessionHandle); |
| 36 | +} |
| 37 | + |
| 38 | +void OnDeviceConnectionFailureWrapper(void * context, const ScopedNodeId & peerId, CHIP_ERROR error) |
| 39 | +{ |
| 40 | + reinterpret_cast<BridgeSubscription *>(context)->OnDeviceConnectionFailure(peerId, error); |
| 41 | +} |
| 42 | + |
| 43 | +} // namespace |
| 44 | + |
| 45 | +BridgeSubscription::BridgeSubscription() : |
| 46 | + mOnDeviceConnectedCallback(OnDeviceConnectedWrapper, this), |
| 47 | + mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureWrapper, this) |
| 48 | +{} |
| 49 | + |
| 50 | +CHIP_ERROR BridgeSubscription::StartSubscription(Controller::DeviceController & controller, NodeId nodeId, EndpointId endpointId) |
| 51 | +{ |
| 52 | + assertChipStackLockedByCurrentThread(); |
| 53 | + |
| 54 | + VerifyOrDie(!subscriptionStarted); // Ensure it's not called multiple times. |
| 55 | + |
| 56 | + // Mark as started |
| 57 | + subscriptionStarted = true; |
| 58 | + |
| 59 | + mEndpointId = endpointId; |
| 60 | + |
| 61 | + CHIP_ERROR err = controller.GetConnectedDevice(nodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); |
| 62 | + if (err != CHIP_NO_ERROR) |
| 63 | + { |
| 64 | + ChipLogError(NotSpecified, "Failed to connect to remote fabric sync bridge %" CHIP_ERROR_FORMAT, err.Format()); |
| 65 | + } |
| 66 | + return err; |
| 67 | +} |
| 68 | + |
| 69 | +void BridgeSubscription::OnAttributeData(const ConcreteDataAttributePath & path, TLV::TLVReader * data, const StatusIB & status) |
| 70 | +{ |
| 71 | + if (!status.IsSuccess()) |
| 72 | + { |
| 73 | + ChipLogError(NotSpecified, "Response Failure: %" CHIP_ERROR_FORMAT, status.ToChipError().Format()); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (data == nullptr) |
| 78 | + { |
| 79 | + ChipLogError(NotSpecified, "Response Failure: No Data"); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + DeviceMgr().HandleAttributeData(path, *data); |
| 84 | +} |
| 85 | + |
| 86 | +void BridgeSubscription::OnEventData(const app::EventHeader & eventHeader, TLV::TLVReader * data, const app::StatusIB * status) |
| 87 | +{ |
| 88 | + if (status != nullptr) |
| 89 | + { |
| 90 | + CHIP_ERROR error = status->ToChipError(); |
| 91 | + if (CHIP_NO_ERROR != error) |
| 92 | + { |
| 93 | + ChipLogError(NotSpecified, "Response Failure: %" CHIP_ERROR_FORMAT, error.Format()); |
| 94 | + return; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (data == nullptr) |
| 99 | + { |
| 100 | + ChipLogError(NotSpecified, "Response Failure: No Data"); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + DeviceMgr().HandleEventData(eventHeader, *data); |
| 105 | +} |
| 106 | + |
| 107 | +void BridgeSubscription::OnError(CHIP_ERROR error) |
| 108 | +{ |
| 109 | + ChipLogProgress(NotSpecified, "Error on remote fabric sync bridge subscription: %" CHIP_ERROR_FORMAT, error.Format()); |
| 110 | +} |
| 111 | + |
| 112 | +void BridgeSubscription::OnDone(ReadClient * apReadClient) |
| 113 | +{ |
| 114 | + mClient.reset(); |
| 115 | + ChipLogProgress(NotSpecified, "The remote fabric sync bridge subscription is terminated"); |
| 116 | + |
| 117 | + // Reset the subscription state to allow retry |
| 118 | + subscriptionStarted = false; |
| 119 | + |
| 120 | + // TODO:(#36092) Fabric-Admin should attempt to re-subscribe when the subscription to the remote bridge is terminated. |
| 121 | +} |
| 122 | + |
| 123 | +void BridgeSubscription::OnDeviceConnected(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle) |
| 124 | +{ |
| 125 | + mClient = std::make_unique<ReadClient>(app::InteractionModelEngine::GetInstance(), &exchangeMgr /* echangeMgr */, |
| 126 | + *this /* callback */, ReadClient::InteractionType::Subscribe); |
| 127 | + VerifyOrDie(mClient); |
| 128 | + |
| 129 | + AttributePathParams readPaths[1]; |
| 130 | + readPaths[0] = AttributePathParams(mEndpointId, Clusters::Descriptor::Id, Clusters::Descriptor::Attributes::PartsList::Id); |
| 131 | + |
| 132 | + EventPathParams eventPaths[1]; |
| 133 | + eventPaths[0] = EventPathParams(mEndpointId, Clusters::CommissionerControl::Id, |
| 134 | + Clusters::CommissionerControl::Events::CommissioningRequestResult::Id); |
| 135 | + eventPaths[0].mIsUrgentEvent = true; |
| 136 | + |
| 137 | + ReadPrepareParams readParams(sessionHandle); |
| 138 | + |
| 139 | + readParams.mpAttributePathParamsList = readPaths; |
| 140 | + readParams.mAttributePathParamsListSize = 1; |
| 141 | + readParams.mpEventPathParamsList = eventPaths; |
| 142 | + readParams.mEventPathParamsListSize = 1; |
| 143 | + readParams.mMinIntervalFloorSeconds = kSubscribeMinInterval; |
| 144 | + readParams.mMaxIntervalCeilingSeconds = kSubscribeMaxInterval; |
| 145 | + readParams.mKeepSubscriptions = true; |
| 146 | + |
| 147 | + CHIP_ERROR err = mClient->SendRequest(readParams); |
| 148 | + |
| 149 | + if (err != CHIP_NO_ERROR) |
| 150 | + { |
| 151 | + ChipLogError(NotSpecified, "Failed to issue subscription to the Descriptor Cluster of the remote bridged device."); |
| 152 | + OnDone(nullptr); |
| 153 | + return; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +void BridgeSubscription::OnDeviceConnectionFailure(const ScopedNodeId & peerId, CHIP_ERROR error) |
| 158 | +{ |
| 159 | + ChipLogError(NotSpecified, "BridgeSubscription failed to connect to " ChipLogFormatX64, ChipLogValueX64(peerId.GetNodeId())); |
| 160 | + OnDone(nullptr); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace admin |
0 commit comments