Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2022 Project CHIP Authors |
| 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 | #pragma once |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <app-common/zap-generated/cluster-objects.h> |
| 24 | #include <app/util/basic-types.h> |
| 25 | #include <lib/core/CHIPError.h> |
| 26 | #include <platform/AttributeList.h> |
| 27 | |
| 28 | namespace chip { |
| 29 | namespace DeviceLayer { |
| 30 | |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 31 | static constexpr size_t kMaxUserLabelListLength = 10; |
| 32 | static constexpr size_t kMaxLabelNameLength = 16; |
| 33 | static constexpr size_t kMaxLabelValueLength = 16; |
Yufeng Wang | 2947ac0 | 2022-04-04 09:17:01 -0700 | [diff] [blame^] | 34 | static constexpr size_t kMaxActiveLocaleLength = 35; |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 35 | |
| 36 | class DeviceInfoProvider |
| 37 | { |
| 38 | public: |
| 39 | /** |
| 40 | * Template used to iterate the stored group data |
| 41 | */ |
| 42 | template <typename T> |
| 43 | class Iterator |
| 44 | { |
| 45 | public: |
| 46 | virtual ~Iterator() = default; |
| 47 | /** |
| 48 | * @retval The number of entries in total that will be iterated. |
| 49 | */ |
| 50 | virtual size_t Count() = 0; |
| 51 | /** |
| 52 | * @param[out] item Value associated with the next element in the iteration. |
| 53 | * @retval true if the next entry is successfully retrieved. |
| 54 | * @retval false if no more entries can be found. |
| 55 | */ |
| 56 | virtual bool Next(T & item) = 0; |
| 57 | /** |
| 58 | * Release the memory allocated by this iterator. |
| 59 | * Must be called before the pointer goes out of scope. |
| 60 | */ |
| 61 | virtual void Release() = 0; |
| 62 | |
| 63 | protected: |
| 64 | Iterator() = default; |
| 65 | }; |
| 66 | |
| 67 | using FixedLabelType = app::Clusters::FixedLabel::Structs::LabelStruct::Type; |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 68 | using UserLabelType = app::Clusters::UserLabel::Structs::LabelStruct::Type; |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 69 | |
Yufeng Wang | 2947ac0 | 2022-04-04 09:17:01 -0700 | [diff] [blame^] | 70 | using FixedLabelIterator = Iterator<FixedLabelType>; |
| 71 | using UserLabelIterator = Iterator<UserLabelType>; |
| 72 | using SupportedLocalesIterator = Iterator<CharSpan>; |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 73 | |
| 74 | DeviceInfoProvider() = default; |
| 75 | |
| 76 | virtual ~DeviceInfoProvider() = default; |
| 77 | |
| 78 | // Not copyable |
| 79 | DeviceInfoProvider(const DeviceInfoProvider &) = delete; |
| 80 | DeviceInfoProvider & operator=(const DeviceInfoProvider &) = delete; |
| 81 | |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 82 | CHIP_ERROR SetUserLabelList(EndpointId endpoint, const AttributeList<UserLabelType, kMaxUserLabelListLength> & labelList); |
| 83 | CHIP_ERROR AppendUserLabel(EndpointId endpoint, const UserLabelType & label); |
| 84 | |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 85 | // Iterators |
| 86 | /** |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 87 | * Creates an iterator that may be used to obtain the list of labels associated with the given endpoint. |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 88 | * In order to release the allocated memory, the Release() method must be called after the iteration is finished. |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 89 | * Modifying the label during the iteration is currently not supported, and may yield unexpected behaviour. |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 90 | * @retval An instance of EndpointIterator on success |
| 91 | * @retval nullptr if no iterator instances are available. |
| 92 | */ |
| 93 | virtual FixedLabelIterator * IterateFixedLabel(EndpointId endpoint) = 0; |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 94 | virtual UserLabelIterator * IterateUserLabel(EndpointId endpoint) = 0; |
| 95 | |
Yufeng Wang | 2947ac0 | 2022-04-04 09:17:01 -0700 | [diff] [blame^] | 96 | /** |
| 97 | * Creates an iterator that may be used to obtain the list of supported locales of the device. |
| 98 | * In order to release the allocated memory, the Release() method must be called after the iteration is finished. |
| 99 | * @retval An instance of EndpointIterator on success |
| 100 | * @retval nullptr if no iterator instances are available. |
| 101 | */ |
| 102 | virtual SupportedLocalesIterator * IterateSupportedLocales() = 0; |
| 103 | |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 104 | protected: |
| 105 | /** |
| 106 | * @brief Set the UserLabel at the specified index of the UserLabelList on a given endpoint |
| 107 | * |
| 108 | * @param endpoint - id to UserLabelList on which to set the UserLabel. |
| 109 | * @param index - index within the UserLabelList for which to set the UserLabel. |
| 110 | * @param userLabel - user label to set. |
| 111 | * @return CHIP_NO_ERROR on success, CHIP_ERROR_INVALID_KEY_ID if index exceed the range (Total length - 1), |
| 112 | * or other CHIP_ERROR values from implementation on other errors. |
| 113 | */ |
| 114 | virtual CHIP_ERROR SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel) = 0; |
| 115 | |
| 116 | /** |
| 117 | * @brief Set the total length of the UserLabelList on a given endpoint |
| 118 | * |
| 119 | * @param endpoint - id of the UserLabelList. |
| 120 | * @param val - total count of the UserLabelList. |
| 121 | * @return CHIP_NO_ERROR on success, other CHIP_ERROR values from implementation on other errors. |
| 122 | */ |
| 123 | virtual CHIP_ERROR SetUserLabelLength(EndpointId endpoint, size_t val) = 0; |
| 124 | |
| 125 | /** |
| 126 | * @brief Get the total length of the UserLabelList on a given endpoint |
| 127 | * |
| 128 | * @param endpoint - id of the UserLabelList. |
| 129 | * @param val - output of the total count of the UserLabelList. |
| 130 | * @return CHIP_NO_ERROR on success, other CHIP_ERROR values from implementation on other errors. |
| 131 | */ |
| 132 | virtual CHIP_ERROR GetUserLabelLength(EndpointId endpoint, size_t & val) = 0; |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /** |
| 136 | * Instance getter for the global DeviceInfoProvider. |
| 137 | * |
| 138 | * Callers have to externally synchronize usage of this function. |
| 139 | * |
| 140 | * @return The global Device Info Provider. Assume never null. |
| 141 | */ |
| 142 | DeviceInfoProvider * GetDeviceInfoProvider(); |
| 143 | |
| 144 | /** |
| 145 | * Instance setter for the global DeviceInfoProvider. |
| 146 | * |
| 147 | * Callers have to externally synchronize usage of this function. |
| 148 | * |
| 149 | * If the `provider` is nullptr, no change is done. |
| 150 | * |
| 151 | * @param[in] provider the Device Info Provider |
| 152 | */ |
| 153 | void SetDeviceInfoProvider(DeviceInfoProvider * provider); |
| 154 | |
| 155 | } // namespace DeviceLayer |
| 156 | } // namespace chip |