|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "DeviceManager.h" |
| 20 | + |
| 21 | +#include <app-common/zap-generated/ids/Attributes.h> |
| 22 | +#include <app-common/zap-generated/ids/Clusters.h> |
| 23 | +#include <app/AttributeAccessInterfaceRegistry.h> |
| 24 | +#include <app/ConcreteAttributePath.h> |
| 25 | +#include <app/EventLogging.h> |
| 26 | +#include <app/reporting/reporting.h> |
| 27 | +#include <app/server/Server.h> |
| 28 | +#include <app/util/af-types.h> |
| 29 | +#include <app/util/attribute-storage.h> |
| 30 | +#include <app/util/endpoint-config-api.h> |
| 31 | +#include <app/util/util.h> |
| 32 | +#include <lib/support/CHIPMem.h> |
| 33 | +#include <lib/support/ZclString.h> |
| 34 | + |
| 35 | +#include <cstdio> |
| 36 | +#include <string> |
| 37 | + |
| 38 | +using namespace chip; |
| 39 | +using namespace chip::app; |
| 40 | +using namespace chip::Credentials; |
| 41 | +using namespace chip::Inet; |
| 42 | +using namespace chip::Transport; |
| 43 | +using namespace chip::DeviceLayer; |
| 44 | +using namespace chip::app::Clusters; |
| 45 | + |
| 46 | +namespace { |
| 47 | +constexpr uint8_t kMaxRetries = 10; |
| 48 | +} // namespace |
| 49 | + |
| 50 | +DeviceManager::DeviceManager() |
| 51 | +{ |
| 52 | + memset(mDevices, 0, sizeof(mDevices)); |
| 53 | + mFirstDynamicEndpointId = static_cast<chip::EndpointId>( |
| 54 | + static_cast<int>(emberAfEndpointFromIndex(static_cast<uint16_t>(emberAfFixedEndpointCount() - 1))) + 1); |
| 55 | + mCurrentEndpointId = mFirstDynamicEndpointId; |
| 56 | +} |
| 57 | + |
| 58 | +int DeviceManager::AddDeviceEndpoint(Device * dev, EmberAfEndpointType * ep, |
| 59 | + const chip::Span<const EmberAfDeviceType> & deviceTypeList, |
| 60 | + const chip::Span<chip::DataVersion> & dataVersionStorage, chip::EndpointId parentEndpointId) |
| 61 | +{ |
| 62 | + uint8_t index = 0; |
| 63 | + while (index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT) |
| 64 | + { |
| 65 | + if (nullptr == mDevices[index]) |
| 66 | + { |
| 67 | + mDevices[index] = dev; |
| 68 | + CHIP_ERROR err; |
| 69 | + int retryCount = 0; |
| 70 | + while (retryCount < kMaxRetries) |
| 71 | + { |
| 72 | + DeviceLayer::StackLock lock; |
| 73 | + dev->SetEndpointId(mCurrentEndpointId); |
| 74 | + dev->SetParentEndpointId(parentEndpointId); |
| 75 | + err = |
| 76 | + emberAfSetDynamicEndpoint(index, mCurrentEndpointId, ep, dataVersionStorage, deviceTypeList, parentEndpointId); |
| 77 | + if (err == CHIP_NO_ERROR) |
| 78 | + { |
| 79 | + ChipLogProgress(NotSpecified, "Added device %s to dynamic endpoint %d (index=%d)", dev->GetName(), |
| 80 | + mCurrentEndpointId, index); |
| 81 | + return index; |
| 82 | + } |
| 83 | + if (err != CHIP_ERROR_ENDPOINT_EXISTS) |
| 84 | + { |
| 85 | + return -1; // Return error as endpoint addition failed due to an error other than endpoint already exists |
| 86 | + } |
| 87 | + // Increment the endpoint ID and handle wrap condition |
| 88 | + if (++mCurrentEndpointId < mFirstDynamicEndpointId) |
| 89 | + { |
| 90 | + mCurrentEndpointId = mFirstDynamicEndpointId; |
| 91 | + } |
| 92 | + retryCount++; |
| 93 | + } |
| 94 | + ChipLogError(NotSpecified, "Failed to add dynamic endpoint after %d retries", kMaxRetries); |
| 95 | + return -1; // Return error as all retries are exhausted |
| 96 | + } |
| 97 | + index++; |
| 98 | + } |
| 99 | + ChipLogProgress(NotSpecified, "Failed to add dynamic endpoint: No endpoints available!"); |
| 100 | + return -1; |
| 101 | +} |
| 102 | + |
| 103 | +int DeviceManager::RemoveDeviceEndpoint(Device * dev) |
| 104 | +{ |
| 105 | + uint8_t index = 0; |
| 106 | + while (index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT) |
| 107 | + { |
| 108 | + if (mDevices[index] == dev) |
| 109 | + { |
| 110 | + DeviceLayer::StackLock lock; |
| 111 | + // Silence complaints about unused ep when progress logging |
| 112 | + // disabled. |
| 113 | + [[maybe_unused]] EndpointId ep = emberAfClearDynamicEndpoint(index); |
| 114 | + mDevices[index] = nullptr; |
| 115 | + ChipLogProgress(NotSpecified, "Removed device %s from dynamic endpoint %d (index=%d)", dev->GetName(), ep, index); |
| 116 | + return index; |
| 117 | + } |
| 118 | + index++; |
| 119 | + } |
| 120 | + return -1; |
| 121 | +} |
| 122 | + |
| 123 | +Device * DeviceManager::GetDevice(uint16_t index) const |
| 124 | +{ |
| 125 | + if (index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT) |
| 126 | + { |
| 127 | + return mDevices[index]; |
| 128 | + } |
| 129 | + return nullptr; |
| 130 | +} |
0 commit comments