blob: b16ab39c91b36e142111a1a02c40de95140437b6 [file] [log] [blame]
Yufeng Wang6a551942022-03-30 23:37:40 -07001/*
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 Wangbc7f2c32022-04-12 14:13:15 -070026#include <lib/core/CHIPPersistentStorageDelegate.h>
Yufeng Wang6a551942022-03-30 23:37:40 -070027#include <platform/AttributeList.h>
28
29namespace chip {
30namespace DeviceLayer {
31
Yufeng Wang7d415be2022-03-31 22:52:05 -070032static constexpr size_t kMaxUserLabelListLength = 10;
33static constexpr size_t kMaxLabelNameLength = 16;
34static constexpr size_t kMaxLabelValueLength = 16;
Yufeng Wang2947ac02022-04-04 09:17:01 -070035static constexpr size_t kMaxActiveLocaleLength = 35;
Yufeng Wang6a551942022-03-30 23:37:40 -070036
37class DeviceInfoProvider
38{
39public:
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 Wang7d415be2022-03-31 22:52:05 -070069 using UserLabelType = app::Clusters::UserLabel::Structs::LabelStruct::Type;
Yufeng Wanga278bfc2022-04-05 13:59:59 -070070 using CalendarType = app::Clusters::TimeFormatLocalization::CalendarType;
Yufeng Wang6a551942022-03-30 23:37:40 -070071
Yufeng Wanga278bfc2022-04-05 13:59:59 -070072 using FixedLabelIterator = Iterator<FixedLabelType>;
73 using UserLabelIterator = Iterator<UserLabelType>;
74 using SupportedLocalesIterator = Iterator<CharSpan>;
75 using SupportedCalendarTypesIterator = Iterator<CalendarType>;
Yufeng Wang6a551942022-03-30 23:37:40 -070076
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 Wangbc7f2c32022-04-12 14:13:15 -070085 /**
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 Wang7d415be2022-03-31 22:52:05 -070092 CHIP_ERROR SetUserLabelList(EndpointId endpoint, const AttributeList<UserLabelType, kMaxUserLabelListLength> & labelList);
Yufeng Wang5533f4d2022-07-29 10:45:17 -070093 CHIP_ERROR ClearUserLabelList(EndpointId endpoint);
Yufeng Wang7d415be2022-03-31 22:52:05 -070094 CHIP_ERROR AppendUserLabel(EndpointId endpoint, const UserLabelType & label);
95
Yufeng Wang6a551942022-03-30 23:37:40 -070096 // Iterators
97 /**
Yufeng Wang7d415be2022-03-31 22:52:05 -070098 * Creates an iterator that may be used to obtain the list of labels associated with the given endpoint.
Yufeng Wang6a551942022-03-30 23:37:40 -070099 * In order to release the allocated memory, the Release() method must be called after the iteration is finished.
Yufeng Wang7d415be2022-03-31 22:52:05 -0700100 * Modifying the label during the iteration is currently not supported, and may yield unexpected behaviour.
Yufeng Wang6a551942022-03-30 23:37:40 -0700101 * @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 Wang7d415be2022-03-31 22:52:05 -0700105 virtual UserLabelIterator * IterateUserLabel(EndpointId endpoint) = 0;
106
Yufeng Wang2947ac02022-04-04 09:17:01 -0700107 /**
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 Wanga278bfc2022-04-05 13:59:59 -0700115 /**
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 Wang7d415be2022-03-31 22:52:05 -0700123protected:
Yufeng Wangbc7f2c32022-04-12 14:13:15 -0700124 PersistentStorageDelegate * mStorage = nullptr;
125
Yufeng Wang7d415be2022-03-31 22:52:05 -0700126 /**
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 Wang5533f4d2022-07-29 10:45:17 -0700138 * @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 Wang7d415be2022-03-31 22:52:05 -0700148 * @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 Wang6a551942022-03-30 23:37:40 -0700164};
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 */
173DeviceInfoProvider * 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 */
184void SetDeviceInfoProvider(DeviceInfoProvider * provider);
185
186} // namespace DeviceLayer
187} // namespace chip