Skip to content

Commit 793cdfd

Browse files
fuxingguo16jamesluo11
authored andcommitted
Implement DeviceInfoProvider on beken platform.
Change-Id: I62961c1ecd72bf0f12eda67b2fdf61d37997f0da
1 parent 22c4a98 commit 793cdfd

File tree

4 files changed

+476
-0
lines changed

4 files changed

+476
-0
lines changed

src/platform/Beken/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ static_library("Beken") {
3232
"ConfigurationManagerImpl.h",
3333
"ConnectivityManagerImpl.cpp",
3434
"ConnectivityManagerImpl.h",
35+
"DeviceInfoProviderImpl.cpp",
36+
"DeviceInfoProviderImpl.h",
3537
"DiagnosticDataProviderImpl.cpp",
3638
"DiagnosticDataProviderImpl.h",
3739
"KeyValueStoreManagerImpl.cpp",
Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,364 @@
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+
18+
#include <lib/core/CHIPTLV.h>
19+
#include <lib/support/CHIPMemString.h>
20+
#include <lib/support/CodeUtils.h>
21+
#include <lib/support/DefaultStorageKeyAllocator.h>
22+
#include <platform/Beken/DeviceInfoProviderImpl.h>
23+
#include <platform/internal/CHIPDeviceLayerInternal.h>
24+
25+
#include <stdlib.h>
26+
#include <string.h>
27+
28+
namespace chip {
29+
namespace DeviceLayer {
30+
31+
namespace {
32+
constexpr TLV::Tag kLabelNameTag = TLV::ContextTag(0);
33+
constexpr TLV::Tag kLabelValueTag = TLV::ContextTag(1);
34+
} // anonymous namespace
35+
36+
DeviceInfoProviderImpl & DeviceInfoProviderImpl::GetDefaultInstance()
37+
{
38+
static DeviceInfoProviderImpl sInstance;
39+
return sInstance;
40+
}
41+
42+
DeviceInfoProvider::FixedLabelIterator * DeviceInfoProviderImpl::IterateFixedLabel(EndpointId endpoint)
43+
{
44+
return new FixedLabelIteratorImpl(endpoint);
45+
}
46+
47+
DeviceInfoProviderImpl::FixedLabelIteratorImpl::FixedLabelIteratorImpl(EndpointId endpoint) : mEndpoint(endpoint)
48+
{
49+
mIndex = 0;
50+
}
51+
52+
size_t DeviceInfoProviderImpl::FixedLabelIteratorImpl::Count()
53+
{
54+
// In Beken Simulation, return the size of the hardcoded labelList on all endpoints.
55+
return 4;
56+
}
57+
58+
bool DeviceInfoProviderImpl::FixedLabelIteratorImpl::Next(FixedLabelType & output)
59+
{
60+
bool retval = true;
61+
62+
// In Beken Simulation, use the following hardcoded labelList on all endpoints.
63+
CHIP_ERROR err = CHIP_NO_ERROR;
64+
65+
const char * labelPtr = nullptr;
66+
const char * valuePtr = nullptr;
67+
68+
VerifyOrReturnError(mIndex < 4, false);
69+
70+
ChipLogProgress(DeviceLayer, "Get the fixed label with index:%d at endpoint:%d", mIndex, mEndpoint);
71+
72+
switch (mIndex)
73+
{
74+
case 0:
75+
labelPtr = "room";
76+
valuePtr = "bedroom 2";
77+
break;
78+
case 1:
79+
labelPtr = "orientation";
80+
valuePtr = "North";
81+
break;
82+
case 2:
83+
labelPtr = "floor";
84+
valuePtr = "2";
85+
break;
86+
case 3:
87+
labelPtr = "direction";
88+
valuePtr = "up";
89+
break;
90+
default:
91+
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
92+
break;
93+
}
94+
95+
if (err == CHIP_NO_ERROR)
96+
{
97+
VerifyOrReturnError(std::strlen(labelPtr) <= kMaxLabelNameLength, false);
98+
VerifyOrReturnError(std::strlen(valuePtr) <= kMaxLabelValueLength, false);
99+
100+
Platform::CopyString(mFixedLabelNameBuf, kMaxLabelNameLength + 1, labelPtr);
101+
Platform::CopyString(mFixedLabelValueBuf, kMaxLabelValueLength + 1, valuePtr);
102+
103+
output.label = CharSpan::fromCharString(mFixedLabelNameBuf);
104+
output.value = CharSpan::fromCharString(mFixedLabelValueBuf);
105+
106+
mIndex++;
107+
108+
retval = true;
109+
}
110+
else
111+
{
112+
retval = false;
113+
}
114+
115+
return retval;
116+
}
117+
118+
CHIP_ERROR DeviceInfoProviderImpl::SetUserLabelLength(EndpointId endpoint, size_t val)
119+
{
120+
DefaultStorageKeyAllocator keyAlloc;
121+
122+
return mStorage->SyncSetKeyValue(keyAlloc.UserLabelLengthKey(endpoint), &val, static_cast<uint16_t>(sizeof(val)));
123+
}
124+
125+
CHIP_ERROR DeviceInfoProviderImpl::GetUserLabelLength(EndpointId endpoint, size_t & val)
126+
{
127+
DefaultStorageKeyAllocator keyAlloc;
128+
uint16_t len = static_cast<uint16_t>(sizeof(val));
129+
130+
return mStorage->SyncGetKeyValue(keyAlloc.UserLabelLengthKey(endpoint), &val, len);
131+
}
132+
133+
CHIP_ERROR DeviceInfoProviderImpl::SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel)
134+
{
135+
DefaultStorageKeyAllocator keyAlloc;
136+
uint8_t buf[UserLabelTLVMaxSize()];
137+
TLV::TLVWriter writer;
138+
writer.Init(buf);
139+
140+
TLV::TLVType outerType;
141+
ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, outerType));
142+
ReturnErrorOnFailure(writer.PutString(kLabelNameTag, userLabel.label));
143+
ReturnErrorOnFailure(writer.PutString(kLabelValueTag, userLabel.value));
144+
ReturnErrorOnFailure(writer.EndContainer(outerType));
145+
146+
return mStorage->SyncSetKeyValue(keyAlloc.UserLabelIndexKey(endpoint, index), buf,
147+
static_cast<uint16_t>(writer.GetLengthWritten()));
148+
}
149+
150+
DeviceInfoProvider::UserLabelIterator * DeviceInfoProviderImpl::IterateUserLabel(EndpointId endpoint)
151+
{
152+
return new UserLabelIteratorImpl(*this, endpoint);
153+
}
154+
155+
DeviceInfoProviderImpl::UserLabelIteratorImpl::UserLabelIteratorImpl(DeviceInfoProviderImpl & provider, EndpointId endpoint) :
156+
mProvider(provider), mEndpoint(endpoint)
157+
{
158+
size_t total = 0;
159+
160+
ReturnOnFailure(mProvider.GetUserLabelLength(mEndpoint, total));
161+
mTotal = total;
162+
mIndex = 0;
163+
}
164+
165+
bool DeviceInfoProviderImpl::UserLabelIteratorImpl::Next(UserLabelType & output)
166+
{
167+
CHIP_ERROR err = CHIP_NO_ERROR;
168+
169+
VerifyOrReturnError(mIndex < mTotal, false);
170+
171+
DefaultStorageKeyAllocator keyAlloc;
172+
uint8_t buf[UserLabelTLVMaxSize()];
173+
uint16_t len = static_cast<uint16_t>(UserLabelTLVMaxSize());
174+
175+
err = mProvider.mStorage->SyncGetKeyValue(keyAlloc.UserLabelIndexKey(mEndpoint, mIndex), buf, len);
176+
VerifyOrReturnError(err == CHIP_NO_ERROR, false);
177+
178+
TLV::ContiguousBufferTLVReader reader;
179+
reader.Init(buf);
180+
err = reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag());
181+
VerifyOrReturnError(err == CHIP_NO_ERROR, false);
182+
183+
TLV::TLVType containerType;
184+
VerifyOrReturnError(reader.EnterContainer(containerType) == CHIP_NO_ERROR, false);
185+
186+
chip::CharSpan label;
187+
chip::CharSpan value;
188+
189+
VerifyOrReturnError(reader.Next(kLabelNameTag) == CHIP_NO_ERROR, false);
190+
VerifyOrReturnError(reader.Get(label) == CHIP_NO_ERROR, false);
191+
192+
VerifyOrReturnError(reader.Next(kLabelValueTag) == CHIP_NO_ERROR, false);
193+
VerifyOrReturnError(reader.Get(value) == CHIP_NO_ERROR, false);
194+
195+
VerifyOrReturnError(reader.VerifyEndOfContainer() == CHIP_NO_ERROR, false);
196+
VerifyOrReturnError(reader.ExitContainer(containerType) == CHIP_NO_ERROR, false);
197+
198+
Platform::CopyString(mUserLabelNameBuf, label);
199+
Platform::CopyString(mUserLabelValueBuf, value);
200+
201+
output.label = CharSpan::fromCharString(mUserLabelNameBuf);
202+
output.value = CharSpan::fromCharString(mUserLabelValueBuf);
203+
204+
mIndex++;
205+
206+
return true;
207+
}
208+
209+
DeviceInfoProvider::SupportedLocalesIterator * DeviceInfoProviderImpl::IterateSupportedLocales()
210+
{
211+
return new SupportedLocalesIteratorImpl();
212+
}
213+
214+
size_t DeviceInfoProviderImpl::SupportedLocalesIteratorImpl::Count()
215+
{
216+
// In Beken Simulation, return the size of the hardcoded list of Strings that are valid values for the ActiveLocale.
217+
// {("en-US"), ("de-DE"), ("fr-FR"), ("en-GB"), ("es-ES"), ("zh-CN"), ("it-IT"), ("ja-JP")}
218+
219+
return 8;
220+
}
221+
222+
bool DeviceInfoProviderImpl::SupportedLocalesIteratorImpl::Next(CharSpan & output)
223+
{
224+
bool retval = true;
225+
226+
// In Beken simulation, return following hardcoded list of Strings that are valid values for the ActiveLocale.
227+
CHIP_ERROR err = CHIP_NO_ERROR;
228+
229+
const char * activeLocalePtr = nullptr;
230+
231+
VerifyOrReturnError(mIndex < 8, false);
232+
233+
switch (mIndex)
234+
{
235+
case 0:
236+
activeLocalePtr = "en-US";
237+
break;
238+
case 1:
239+
activeLocalePtr = "de-DE";
240+
break;
241+
case 2:
242+
activeLocalePtr = "fr-FR";
243+
break;
244+
case 3:
245+
activeLocalePtr = "en-GB";
246+
break;
247+
case 4:
248+
activeLocalePtr = "es-ES";
249+
break;
250+
case 5:
251+
activeLocalePtr = "zh-CN";
252+
break;
253+
case 6:
254+
activeLocalePtr = "it-IT";
255+
break;
256+
case 7:
257+
activeLocalePtr = "ja-JP";
258+
break;
259+
default:
260+
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
261+
break;
262+
}
263+
264+
if (err == CHIP_NO_ERROR)
265+
{
266+
VerifyOrReturnError(std::strlen(activeLocalePtr) <= kMaxActiveLocaleLength, false);
267+
268+
Platform::CopyString(mActiveLocaleBuf, kMaxActiveLocaleLength + 1, activeLocalePtr);
269+
270+
output = CharSpan::fromCharString(mActiveLocaleBuf);
271+
272+
mIndex++;
273+
274+
retval = true;
275+
}
276+
else
277+
{
278+
retval = false;
279+
}
280+
281+
return retval;
282+
}
283+
284+
DeviceInfoProvider::SupportedCalendarTypesIterator * DeviceInfoProviderImpl::IterateSupportedCalendarTypes()
285+
{
286+
return new SupportedCalendarTypesIteratorImpl();
287+
}
288+
289+
size_t DeviceInfoProviderImpl::SupportedCalendarTypesIteratorImpl::Count()
290+
{
291+
// In Beken Simulation, return the size of the hardcoded list of Strings that are valid values for the Calendar Types.
292+
// {("kBuddhist"), ("kChinese"), ("kCoptic"), ("kEthiopian"), ("kGregorian"), ("kHebrew"), ("kIndian"), ("kJapanese"),
293+
// ("kKorean"), ("kPersian"), ("kTaiwanese"), ("kIslamic")}
294+
295+
return 12;
296+
}
297+
298+
bool DeviceInfoProviderImpl::SupportedCalendarTypesIteratorImpl::Next(CalendarType & output)
299+
{
300+
bool retval = true;
301+
302+
// In Beken Simulation, return following hardcoded list of Strings that are valid values for the Calendar Types.
303+
CHIP_ERROR err = CHIP_NO_ERROR;
304+
305+
VerifyOrReturnError(mIndex < 12, false);
306+
307+
switch (mIndex)
308+
{
309+
case 0:
310+
output = app::Clusters::TimeFormatLocalization::CalendarType::kBuddhist;
311+
break;
312+
case 1:
313+
output = app::Clusters::TimeFormatLocalization::CalendarType::kChinese;
314+
break;
315+
case 2:
316+
output = app::Clusters::TimeFormatLocalization::CalendarType::kCoptic;
317+
break;
318+
case 3:
319+
output = app::Clusters::TimeFormatLocalization::CalendarType::kEthiopian;
320+
break;
321+
case 4:
322+
output = app::Clusters::TimeFormatLocalization::CalendarType::kGregorian;
323+
break;
324+
case 5:
325+
output = app::Clusters::TimeFormatLocalization::CalendarType::kHebrew;
326+
break;
327+
case 6:
328+
output = app::Clusters::TimeFormatLocalization::CalendarType::kIndian;
329+
break;
330+
case 7:
331+
output = app::Clusters::TimeFormatLocalization::CalendarType::kJapanese;
332+
break;
333+
case 8:
334+
output = app::Clusters::TimeFormatLocalization::CalendarType::kKorean;
335+
break;
336+
case 9:
337+
output = app::Clusters::TimeFormatLocalization::CalendarType::kPersian;
338+
break;
339+
case 10:
340+
output = app::Clusters::TimeFormatLocalization::CalendarType::kTaiwanese;
341+
break;
342+
case 11:
343+
output = app::Clusters::TimeFormatLocalization::CalendarType::kIslamic;
344+
break;
345+
default:
346+
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
347+
break;
348+
}
349+
350+
if (err == CHIP_NO_ERROR)
351+
{
352+
mIndex++;
353+
retval = true;
354+
}
355+
else
356+
{
357+
retval = false;
358+
}
359+
360+
return retval;
361+
}
362+
363+
} // namespace DeviceLayer
364+
} // namespace chip

0 commit comments

Comments
 (0)