|
| 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 | +#include "BridgedAdministratorCommissioning.h" |
| 19 | + |
| 20 | +#include "BridgedDevice.h" |
| 21 | +#include "BridgedDeviceManager.h" |
| 22 | +#include <app/AttributeAccessInterfaceRegistry.h> |
| 23 | + |
| 24 | +using namespace chip; |
| 25 | +using namespace chip::app; |
| 26 | +using namespace chip::app::Clusters; |
| 27 | +using namespace chip::app::Clusters::AdministratorCommissioning; |
| 28 | + |
| 29 | +CHIP_ERROR BridgedAdministratorCommissioning::Init() |
| 30 | +{ |
| 31 | + // We expect initialization after all embr plugin clusters initialization. This allows us to unregister |
| 32 | + // the existing AccessAttributeInterface for AdministratorCommissioning and register ourselves, ensuring |
| 33 | + // we get the callback for reading attribute. If the read is not intended for a bridged device we will |
| 34 | + // forward it to the original attribute interface that we are unregistering. |
| 35 | + mOriginalAttributeInterface = AttributeAccessInterfaceRegistry::Instance().Get(kRootEndpointId, AdministratorCommissioning::Id); |
| 36 | + VerifyOrReturnError(mOriginalAttributeInterface, CHIP_ERROR_INTERNAL); |
| 37 | + AttributeAccessInterfaceRegistry::Instance().Unregister(mOriginalAttributeInterface); |
| 38 | + VerifyOrDie(AttributeAccessInterfaceRegistry::Instance().Register(this)); |
| 39 | + return CHIP_NO_ERROR; |
| 40 | +} |
| 41 | + |
| 42 | +CHIP_ERROR BridgedAdministratorCommissioning::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) |
| 43 | +{ |
| 44 | + VerifyOrDie(aPath.mClusterId == Clusters::AdministratorCommissioning::Id); |
| 45 | + EndpointId endpointId = aPath.mEndpointId; |
| 46 | + BridgedDevice * device = BridgeDeviceMgr().GetDevice(endpointId); |
| 47 | + |
| 48 | + if (!device) |
| 49 | + { |
| 50 | + VerifyOrDie(mOriginalAttributeInterface); |
| 51 | + return mOriginalAttributeInterface->Read(aPath, aEncoder); |
| 52 | + } |
| 53 | + auto attr = device->GetAdminCommissioningAttributes(); |
| 54 | + |
| 55 | + switch (aPath.mAttributeId) |
| 56 | + { |
| 57 | + case Attributes::WindowStatus::Id: { |
| 58 | + return aEncoder.Encode(attr.commissioningWindowStatus); |
| 59 | + } |
| 60 | + case Attributes::AdminFabricIndex::Id: { |
| 61 | + DataModel::Nullable<FabricIndex> encodeableFabricIndex = DataModel::NullNullable; |
| 62 | + if (attr.openerFabricIndex.has_value()) |
| 63 | + { |
| 64 | + encodeableFabricIndex.SetNonNull(attr.openerFabricIndex.value()); |
| 65 | + } |
| 66 | + return aEncoder.Encode(encodeableFabricIndex); |
| 67 | + } |
| 68 | + case Attributes::AdminVendorId::Id: { |
| 69 | + DataModel::Nullable<VendorId> encodeableVendorId = DataModel::NullNullable; |
| 70 | + if (attr.openerVendorId.has_value()) |
| 71 | + { |
| 72 | + encodeableVendorId.SetNonNull(attr.openerVendorId.value()); |
| 73 | + } |
| 74 | + return aEncoder.Encode(encodeableVendorId); |
| 75 | + } |
| 76 | + default: |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + return CHIP_NO_ERROR; |
| 81 | +} |
0 commit comments