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