-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathDeviceInfoProvider.h
225 lines (199 loc) · 8.93 KB
/
DeviceInfoProvider.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <algorithm>
#include <stdint.h>
#include <sys/types.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app/util/basic-types.h>
#include <lib/core/CHIPError.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <platform/AttributeList.h>
namespace chip {
namespace DeviceLayer {
static constexpr size_t kMaxUserLabelListLength = 10;
static constexpr size_t kMaxLabelNameLength = 16;
static constexpr size_t kMaxLabelValueLength = 16;
static constexpr size_t kMaxActiveLocaleLength = 35;
class DeviceInfoProvider
{
public:
/**
* Template used to iterate the stored group data
*/
template <typename T>
class Iterator
{
public:
virtual ~Iterator() = default;
/**
* @retval The number of entries in total that will be iterated.
*/
virtual size_t Count() = 0;
/**
* @param[out] item Value associated with the next element in the iteration.
* @retval true if the next entry is successfully retrieved.
* @retval false if no more entries can be found.
*/
virtual bool Next(T & item) = 0;
/**
* Release the memory allocated by this iterator.
* Must be called before the pointer goes out of scope.
*/
virtual void Release() = 0;
protected:
Iterator() = default;
};
using FixedLabelType = app::Clusters::FixedLabel::Structs::LabelStruct::Type;
using UserLabelType = app::Clusters::UserLabel::Structs::LabelStruct::Type;
using CalendarType = app::Clusters::TimeFormatLocalization::CalendarTypeEnum;
using FixedLabelIterator = Iterator<FixedLabelType>;
using UserLabelIterator = Iterator<UserLabelType>;
using SupportedLocalesIterator = Iterator<CharSpan>;
using SupportedCalendarTypesIterator = Iterator<CalendarType>;
DeviceInfoProvider() = default;
virtual ~DeviceInfoProvider() = default;
// Not copyable
DeviceInfoProvider(const DeviceInfoProvider &) = delete;
DeviceInfoProvider & operator=(const DeviceInfoProvider &) = delete;
/**
* @brief Set the storage implementation used for non-volatile storage of device information data.
*
* @param storage Pointer to storage instance to set. Cannot be nullptr, will assert.
*/
void SetStorageDelegate(PersistentStorageDelegate * storage);
/**
* @brief Sets the user label list for a specified endpoint.
*
* Replaces the current user label list with a new list. If the new list is smaller
* than the existing one, excess labels are deleted to free up space.
*
* @param[in] endpoint The endpoint ID associated with the user label list.
* @param[in] labelList The new list of user labels to store.
*
* @return CHIP_NO_ERROR on success.
* @return CHIP_ERROR if an error occurs.
*/
CHIP_ERROR SetUserLabelList(EndpointId endpoint, const AttributeList<UserLabelType, kMaxUserLabelListLength> & labelList);
/**
* @brief Clears the user label list for a specified endpoint.
*
* Deletes all user labels associated with the given endpoint, resetting the list length to zero.
* If no previous list exists, this function has no effect.
*
* @param[in] endpoint The endpoint ID whose user label list will be cleared.
*
* @return CHIP_NO_ERROR on success or if no previous value exists.
* @return CHIP_ERROR if an error occurs during deletion.
*/
CHIP_ERROR ClearUserLabelList(EndpointId endpoint);
/**
* @brief Appends a user label to the user label list for a specified endpoint.
*
* Adds a new label to the end of the existing user label list. The list size must not
* exceed `kMaxUserLabelListLength`. If the list is full, the function returns an error.
*
* @param[in] endpoint The endpoint ID to which the user label will be added.
* @param[in] label The user label to append to the list.
*
* @return CHIP_NO_ERROR on success.
* @return CHIP_ERROR_NO_MEMORY if the list is already at its maximum size.
* @return CHIP_ERROR if an error occurs during storage.
*/
CHIP_ERROR AppendUserLabel(EndpointId endpoint, const UserLabelType & label);
// Iterators
/**
* Creates an iterator that may be used to obtain the list of labels associated with the given endpoint.
* In order to release the allocated memory, the Release() method must be called after the iteration is finished.
* Modifying the label during the iteration is currently not supported, and may yield unexpected behaviour.
* @retval An instance of EndpointIterator on success
* @retval nullptr if no iterator instances are available.
*/
virtual FixedLabelIterator * IterateFixedLabel(EndpointId endpoint) = 0;
virtual UserLabelIterator * IterateUserLabel(EndpointId endpoint) = 0;
/**
* Creates an iterator that may be used to obtain the list of supported locales of the device.
* In order to release the allocated memory, the Release() method must be called after the iteration is finished.
* @retval An instance of EndpointIterator on success
* @retval nullptr if no iterator instances are available.
*/
virtual SupportedLocalesIterator * IterateSupportedLocales() = 0;
/**
* Creates an iterator that may be used to obtain the list of supported calendar types of the device.
* In order to release the allocated memory, the Release() method must be called after the iteration is finished.
* @retval An instance of EndpointIterator on success
* @retval nullptr if no iterator instances are available.
*/
virtual SupportedCalendarTypesIterator * IterateSupportedCalendarTypes() = 0;
protected:
PersistentStorageDelegate * mStorage = nullptr;
/**
* @brief Set the UserLabel at the specified index of the UserLabelList on a given endpoint
*
* @param endpoint - id to UserLabelList on which to set the UserLabel.
* @param index - index within the UserLabelList for which to set the UserLabel.
* @param userLabel - user label to set.
* @return CHIP_NO_ERROR on success, CHIP_ERROR_INVALID_KEY_ID if index exceed the range (Total length - 1),
* or other CHIP_ERROR values from implementation on other errors.
*/
virtual CHIP_ERROR SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel) = 0;
/**
* @brief Delete the UserLabel at the specified index of the UserLabelList on a given endpoint
*
* @param endpoint - id to UserLabelList on which to delete the UserLabel.
* @param index - index within the UserLabelList for which to remove the UserLabel.
* @return CHIP_NO_ERROR on success, CHIP_ERROR_INVALID_KEY_ID if index exceed the range (Total length - 1),
* or other CHIP_ERROR values from implementation on other errors.
*/
virtual CHIP_ERROR DeleteUserLabelAt(EndpointId endpoint, size_t index) = 0;
/**
* @brief Set the total length of the UserLabelList on a given endpoint
*
* @param endpoint - id of the UserLabelList.
* @param val - total count of the UserLabelList.
* @return CHIP_NO_ERROR on success, other CHIP_ERROR values from implementation on other errors.
*/
virtual CHIP_ERROR SetUserLabelLength(EndpointId endpoint, size_t val) = 0;
/**
* @brief Get the total length of the UserLabelList on a given endpoint
*
* @param endpoint - id of the UserLabelList.
* @param val - output of the total count of the UserLabelList.
* @return CHIP_NO_ERROR on success, other CHIP_ERROR values from implementation on other errors.
*/
virtual CHIP_ERROR GetUserLabelLength(EndpointId endpoint, size_t & val) = 0;
};
/**
* Instance getter for the global DeviceInfoProvider.
*
* Callers have to externally synchronize usage of this function.
*
* @return The global Device Info Provider. Assume never null.
*/
DeviceInfoProvider * GetDeviceInfoProvider();
/**
* Instance setter for the global DeviceInfoProvider.
*
* Callers have to externally synchronize usage of this function.
*
* If the `provider` is nullptr, no change is done.
*
* @param[in] provider the Device Info Provider
*/
void SetDeviceInfoProvider(DeviceInfoProvider * provider);
} // namespace DeviceLayer
} // namespace chip