blob: cfe371e48aa97b1ea980cdf191a17bd34ae220eb [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>
26#include <platform/AttributeList.h>
27
28namespace chip {
29namespace DeviceLayer {
30
Yufeng Wang7d415be2022-03-31 22:52:05 -070031static constexpr size_t kMaxUserLabelListLength = 10;
32static constexpr size_t kMaxLabelNameLength = 16;
33static constexpr size_t kMaxLabelValueLength = 16;
Yufeng Wang2947ac02022-04-04 09:17:01 -070034static constexpr size_t kMaxActiveLocaleLength = 35;
Yufeng Wang6a551942022-03-30 23:37:40 -070035
36class DeviceInfoProvider
37{
38public:
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 Wang7d415be2022-03-31 22:52:05 -070068 using UserLabelType = app::Clusters::UserLabel::Structs::LabelStruct::Type;
Yufeng Wang6a551942022-03-30 23:37:40 -070069
Yufeng Wang2947ac02022-04-04 09:17:01 -070070 using FixedLabelIterator = Iterator<FixedLabelType>;
71 using UserLabelIterator = Iterator<UserLabelType>;
72 using SupportedLocalesIterator = Iterator<CharSpan>;
Yufeng Wang6a551942022-03-30 23:37:40 -070073
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 Wang7d415be2022-03-31 22:52:05 -070082 CHIP_ERROR SetUserLabelList(EndpointId endpoint, const AttributeList<UserLabelType, kMaxUserLabelListLength> & labelList);
83 CHIP_ERROR AppendUserLabel(EndpointId endpoint, const UserLabelType & label);
84
Yufeng Wang6a551942022-03-30 23:37:40 -070085 // Iterators
86 /**
Yufeng Wang7d415be2022-03-31 22:52:05 -070087 * 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 -070088 * 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 -070089 * Modifying the label during the iteration is currently not supported, and may yield unexpected behaviour.
Yufeng Wang6a551942022-03-30 23:37:40 -070090 * @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 Wang7d415be2022-03-31 22:52:05 -070094 virtual UserLabelIterator * IterateUserLabel(EndpointId endpoint) = 0;
95
Yufeng Wang2947ac02022-04-04 09:17:01 -070096 /**
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 Wang7d415be2022-03-31 22:52:05 -0700104protected:
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 Wang6a551942022-03-30 23:37:40 -0700133};
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 */
142DeviceInfoProvider * 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 */
153void SetDeviceInfoProvider(DeviceInfoProvider * provider);
154
155} // namespace DeviceLayer
156} // namespace chip