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> |
Yufeng Wang | bc7f2c3 | 2022-04-12 14:13:15 -0700 | [diff] [blame] | 26 | #include <lib/core/CHIPPersistentStorageDelegate.h> |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 27 | #include <platform/AttributeList.h> |
| 28 | |
| 29 | namespace chip { |
| 30 | namespace DeviceLayer { |
| 31 | |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 32 | static constexpr size_t kMaxUserLabelListLength = 10; |
| 33 | static constexpr size_t kMaxLabelNameLength = 16; |
| 34 | static constexpr size_t kMaxLabelValueLength = 16; |
Yufeng Wang | 2947ac0 | 2022-04-04 09:17:01 -0700 | [diff] [blame] | 35 | static constexpr size_t kMaxActiveLocaleLength = 35; |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 36 | |
| 37 | class DeviceInfoProvider |
| 38 | { |
| 39 | public: |
| 40 | /** |
| 41 | * Template used to iterate the stored group data |
| 42 | */ |
| 43 | template <typename T> |
| 44 | class Iterator |
| 45 | { |
| 46 | public: |
| 47 | virtual ~Iterator() = default; |
| 48 | /** |
| 49 | * @retval The number of entries in total that will be iterated. |
| 50 | */ |
| 51 | virtual size_t Count() = 0; |
| 52 | /** |
| 53 | * @param[out] item Value associated with the next element in the iteration. |
| 54 | * @retval true if the next entry is successfully retrieved. |
| 55 | * @retval false if no more entries can be found. |
| 56 | */ |
| 57 | virtual bool Next(T & item) = 0; |
| 58 | /** |
| 59 | * Release the memory allocated by this iterator. |
| 60 | * Must be called before the pointer goes out of scope. |
| 61 | */ |
| 62 | virtual void Release() = 0; |
| 63 | |
| 64 | protected: |
| 65 | Iterator() = default; |
| 66 | }; |
| 67 | |
| 68 | using FixedLabelType = app::Clusters::FixedLabel::Structs::LabelStruct::Type; |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 69 | using UserLabelType = app::Clusters::UserLabel::Structs::LabelStruct::Type; |
Yufeng Wang | a278bfc | 2022-04-05 13:59:59 -0700 | [diff] [blame] | 70 | using CalendarType = app::Clusters::TimeFormatLocalization::CalendarType; |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 71 | |
Yufeng Wang | a278bfc | 2022-04-05 13:59:59 -0700 | [diff] [blame] | 72 | using FixedLabelIterator = Iterator<FixedLabelType>; |
| 73 | using UserLabelIterator = Iterator<UserLabelType>; |
| 74 | using SupportedLocalesIterator = Iterator<CharSpan>; |
| 75 | using SupportedCalendarTypesIterator = Iterator<CalendarType>; |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 76 | |
| 77 | DeviceInfoProvider() = default; |
| 78 | |
| 79 | virtual ~DeviceInfoProvider() = default; |
| 80 | |
| 81 | // Not copyable |
| 82 | DeviceInfoProvider(const DeviceInfoProvider &) = delete; |
| 83 | DeviceInfoProvider & operator=(const DeviceInfoProvider &) = delete; |
| 84 | |
Yufeng Wang | bc7f2c3 | 2022-04-12 14:13:15 -0700 | [diff] [blame] | 85 | /** |
| 86 | * @brief Set the storage implementation used for non-volatile storage of device information data. |
| 87 | * |
| 88 | * @param storage Pointer to storage instance to set. Cannot be nullptr, will assert. |
| 89 | */ |
| 90 | void SetStorageDelegate(PersistentStorageDelegate * storage); |
| 91 | |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 92 | CHIP_ERROR SetUserLabelList(EndpointId endpoint, const AttributeList<UserLabelType, kMaxUserLabelListLength> & labelList); |
Yufeng Wang | 5533f4d | 2022-07-29 10:45:17 -0700 | [diff] [blame] | 93 | CHIP_ERROR ClearUserLabelList(EndpointId endpoint); |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 94 | CHIP_ERROR AppendUserLabel(EndpointId endpoint, const UserLabelType & label); |
| 95 | |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 96 | // Iterators |
| 97 | /** |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 98 | * 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] | 99 | * 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] | 100 | * 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] | 101 | * @retval An instance of EndpointIterator on success |
| 102 | * @retval nullptr if no iterator instances are available. |
| 103 | */ |
| 104 | virtual FixedLabelIterator * IterateFixedLabel(EndpointId endpoint) = 0; |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 105 | virtual UserLabelIterator * IterateUserLabel(EndpointId endpoint) = 0; |
| 106 | |
Yufeng Wang | 2947ac0 | 2022-04-04 09:17:01 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Creates an iterator that may be used to obtain the list of supported locales of the device. |
| 109 | * In order to release the allocated memory, the Release() method must be called after the iteration is finished. |
| 110 | * @retval An instance of EndpointIterator on success |
| 111 | * @retval nullptr if no iterator instances are available. |
| 112 | */ |
| 113 | virtual SupportedLocalesIterator * IterateSupportedLocales() = 0; |
| 114 | |
Yufeng Wang | a278bfc | 2022-04-05 13:59:59 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Creates an iterator that may be used to obtain the list of supported calendar types of the device. |
| 117 | * In order to release the allocated memory, the Release() method must be called after the iteration is finished. |
| 118 | * @retval An instance of EndpointIterator on success |
| 119 | * @retval nullptr if no iterator instances are available. |
| 120 | */ |
| 121 | virtual SupportedCalendarTypesIterator * IterateSupportedCalendarTypes() = 0; |
| 122 | |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 123 | protected: |
Yufeng Wang | bc7f2c3 | 2022-04-12 14:13:15 -0700 | [diff] [blame] | 124 | PersistentStorageDelegate * mStorage = nullptr; |
| 125 | |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 126 | /** |
| 127 | * @brief Set the UserLabel at the specified index of the UserLabelList on a given endpoint |
| 128 | * |
| 129 | * @param endpoint - id to UserLabelList on which to set the UserLabel. |
| 130 | * @param index - index within the UserLabelList for which to set the UserLabel. |
| 131 | * @param userLabel - user label to set. |
| 132 | * @return CHIP_NO_ERROR on success, CHIP_ERROR_INVALID_KEY_ID if index exceed the range (Total length - 1), |
| 133 | * or other CHIP_ERROR values from implementation on other errors. |
| 134 | */ |
| 135 | virtual CHIP_ERROR SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel) = 0; |
| 136 | |
| 137 | /** |
Yufeng Wang | 5533f4d | 2022-07-29 10:45:17 -0700 | [diff] [blame] | 138 | * @brief Delete the UserLabel at the specified index of the UserLabelList on a given endpoint |
| 139 | * |
| 140 | * @param endpoint - id to UserLabelList on which to delete the UserLabel. |
| 141 | * @param index - index within the UserLabelList for which to remove the UserLabel. |
| 142 | * @return CHIP_NO_ERROR on success, CHIP_ERROR_INVALID_KEY_ID if index exceed the range (Total length - 1), |
| 143 | * or other CHIP_ERROR values from implementation on other errors. |
| 144 | */ |
| 145 | virtual CHIP_ERROR DeleteUserLabelAt(EndpointId endpoint, size_t index) = 0; |
| 146 | |
| 147 | /** |
Yufeng Wang | 7d415be | 2022-03-31 22:52:05 -0700 | [diff] [blame] | 148 | * @brief Set the total length of the UserLabelList on a given endpoint |
| 149 | * |
| 150 | * @param endpoint - id of the UserLabelList. |
| 151 | * @param val - total count of the UserLabelList. |
| 152 | * @return CHIP_NO_ERROR on success, other CHIP_ERROR values from implementation on other errors. |
| 153 | */ |
| 154 | virtual CHIP_ERROR SetUserLabelLength(EndpointId endpoint, size_t val) = 0; |
| 155 | |
| 156 | /** |
| 157 | * @brief Get the total length of the UserLabelList on a given endpoint |
| 158 | * |
| 159 | * @param endpoint - id of the UserLabelList. |
| 160 | * @param val - output of the total count of the UserLabelList. |
| 161 | * @return CHIP_NO_ERROR on success, other CHIP_ERROR values from implementation on other errors. |
| 162 | */ |
| 163 | virtual CHIP_ERROR GetUserLabelLength(EndpointId endpoint, size_t & val) = 0; |
Yufeng Wang | 6a55194 | 2022-03-30 23:37:40 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /** |
| 167 | * Instance getter for the global DeviceInfoProvider. |
| 168 | * |
| 169 | * Callers have to externally synchronize usage of this function. |
| 170 | * |
| 171 | * @return The global Device Info Provider. Assume never null. |
| 172 | */ |
| 173 | DeviceInfoProvider * GetDeviceInfoProvider(); |
| 174 | |
| 175 | /** |
| 176 | * Instance setter for the global DeviceInfoProvider. |
| 177 | * |
| 178 | * Callers have to externally synchronize usage of this function. |
| 179 | * |
| 180 | * If the `provider` is nullptr, no change is done. |
| 181 | * |
| 182 | * @param[in] provider the Device Info Provider |
| 183 | */ |
| 184 | void SetDeviceInfoProvider(DeviceInfoProvider * provider); |
| 185 | |
| 186 | } // namespace DeviceLayer |
| 187 | } // namespace chip |