forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrvc-service-area-delegate.cpp
413 lines (341 loc) · 13.2 KB
/
rvc-service-area-delegate.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* 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.
*/
#include <app-common/zap-generated/attributes/Accessors.h>
#include <rvc-service-area-delegate.h>
#include <vector>
using namespace chip;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::ServiceArea;
CHIP_ERROR RvcServiceAreaDelegate::Init()
{
// hardcoded fill of SUPPORTED MAPS for prototyping
uint32_t supportedMapId_XX = 3;
uint32_t supportedMapId_YY = 245;
GetInstance()->AddSupportedMap(supportedMapId_XX, "My Map XX"_span);
GetInstance()->AddSupportedMap(supportedMapId_YY, "My Map YY"_span);
// hardcoded fill of SUPPORTED AREAS for prototyping
uint32_t supportedAreaID_A = 7;
uint32_t supportedAreaID_B = 1234567;
uint32_t supportedAreaID_C = 10050;
uint32_t supportedAreaID_D = 0x88888888;
// Area A has name, floor number, uses map XX
auto areaA =
AreaStructureWrapper{}
.SetAreaId(supportedAreaID_A)
.SetMapId(supportedMapId_XX)
.SetLocationInfo("My Location A"_span, DataModel::Nullable<int16_t>(4), DataModel::Nullable<Globals::AreaTypeTag>());
// Area B has name, uses map XX
auto areaB = AreaStructureWrapper{}
.SetMapId(supportedAreaID_B)
.SetMapId(supportedMapId_XX)
.SetLocationInfo("My Location B"_span, DataModel::NullNullable, DataModel::NullNullable);
// Area C has full SemData, no name, Map YY
auto areaC = AreaStructureWrapper{}
.SetAreaId(supportedAreaID_C)
.SetMapId(supportedMapId_YY)
.SetLocationInfo(""_span, -1, Globals::AreaTypeTag::kPlayRoom)
.SetLandmarkInfo(Globals::LandmarkTag::kBackDoor, Globals::RelativePositionTag::kNextTo);
// Area D has null values for all landmark fields, Map YY
auto areaD = AreaStructureWrapper{}
.SetAreaId(supportedAreaID_D)
.SetMapId(supportedMapId_YY)
.SetLocationInfo("My Location D"_span, DataModel::NullNullable, DataModel::NullNullable)
.SetLandmarkInfo(Globals::LandmarkTag::kCouch, Globals::RelativePositionTag::kNextTo);
GetInstance()->AddSupportedArea(areaA);
GetInstance()->AddSupportedArea(areaB);
GetInstance()->AddSupportedArea(areaC);
GetInstance()->AddSupportedArea(areaD);
GetInstance()->SetCurrentArea(supportedAreaID_C);
return CHIP_NO_ERROR;
}
//*************************************************************************
// command support
bool RvcServiceAreaDelegate::IsSetSelectedAreasAllowed(MutableCharSpan & statusText)
{
// TODO IMPLEMENT
return true;
};
bool RvcServiceAreaDelegate::IsValidSelectAreasSet(const Commands::SelectAreas::DecodableType & req, SelectAreasStatus & areaStatus,
MutableCharSpan & statusText)
{
// TODO IMPLEMENT
return true;
};
bool RvcServiceAreaDelegate::HandleSkipCurrentArea(uint32_t skippedArea, MutableCharSpan & skipStatusText)
{
// TODO IMPLEMENT
return true;
};
//*************************************************************************
// Supported Areas accessors
bool RvcServiceAreaDelegate::IsSupportedAreasChangeAllowed()
{
// TODO IMPLEMENT
return true;
}
uint32_t RvcServiceAreaDelegate::GetNumberOfSupportedAreas()
{
return static_cast<uint32_t>(mSupportedAreas.size());
}
bool RvcServiceAreaDelegate::GetSupportedAreaByIndex(uint32_t listIndex, AreaStructureWrapper & supportedArea)
{
if (listIndex < mSupportedAreas.size())
{
supportedArea = mSupportedAreas[listIndex];
return true;
}
return false;
};
bool RvcServiceAreaDelegate::GetSupportedAreaById(uint32_t aAreaID, uint32_t & listIndex, AreaStructureWrapper & supportedArea)
{
// We do not need to reimplement this method as it's already done by the SDK.
// We are reimplementing this method, still using linear search, but with some optimization on the SDK implementation
// since we have direct access to the list.
listIndex = 0;
while (listIndex < mSupportedAreas.size())
{
if (mSupportedAreas[listIndex].areaID == aAreaID)
{
supportedArea = mSupportedAreas[listIndex];
return true;
}
++listIndex;
}
return false;
};
bool RvcServiceAreaDelegate::AddSupportedArea(const AreaStructureWrapper & newArea, uint32_t & listIndex)
{
// The server instance (caller) is responsible for ensuring that there are no duplicate area IDs, list size not exceeded,
// etc.
// Double-check list size to ensure there no memory issues.
if (mSupportedAreas.size() < kMaxNumSupportedAreas)
{
// not sorting list, number of areas normally expected to be small, max 255
mSupportedAreas.push_back(newArea);
listIndex = static_cast<uint32_t>(mSupportedMaps.size()) - 1; // new element is last in list
return true;
}
ChipLogError(Zcl, "AddSupportedArea %u - supported areas list is already at maximum size %u", newArea.areaID,
static_cast<uint32_t>(kMaxNumSupportedAreas));
return false;
}
bool RvcServiceAreaDelegate::ModifySupportedArea(uint32_t listIndex, const AreaStructureWrapper & modifiedArea)
{
// The server instance (caller) is responsible for ensuring that there are no duplicate area IDs, list size not exceeded,
// etc.
// Double-check that areaID's match.
if (modifiedArea.areaID != mSupportedAreas[listIndex].areaID)
{
ChipLogError(Zcl, "ModifySupportedArea - areaID's do not match, new areaID %u, existing areaID %u", modifiedArea.areaID,
mSupportedAreas[listIndex].areaID);
return false;
}
// checks passed, update the attribute
mSupportedAreas[listIndex] = modifiedArea;
return true;
}
bool RvcServiceAreaDelegate::ClearSupportedAreas()
{
if (!mSupportedAreas.empty())
{
mSupportedAreas.clear();
return true;
}
return false;
}
//*************************************************************************
// Supported Maps accessors
bool RvcServiceAreaDelegate::IsSupportedMapChangeAllowed()
{
// TODO IMPLEMENT
return true;
}
uint32_t RvcServiceAreaDelegate::GetNumberOfSupportedMaps()
{
return static_cast<uint32_t>(mSupportedMaps.size());
}
bool RvcServiceAreaDelegate::GetSupportedMapByIndex(uint32_t listIndex, MapStructureWrapper & aSupportedMap)
{
if (listIndex < mSupportedMaps.size())
{
aSupportedMap = mSupportedMaps[listIndex];
return true;
}
return false;
};
bool RvcServiceAreaDelegate::GetSupportedMapById(uint32_t aMapId, uint32_t & listIndex, MapStructureWrapper & aSupportedMap)
{
// We do not need to reimplement this method as it's already done by the SDK.
// We are reimplementing this method, still using linear search, but with some optimization on the SDK implementation
// since we have direct access to the list.
listIndex = 0;
while (listIndex < mSupportedMaps.size())
{
if (mSupportedMaps[listIndex].mapID == aMapId)
{
aSupportedMap = mSupportedMaps[listIndex];
return true;
}
++listIndex;
}
return false;
};
bool RvcServiceAreaDelegate::AddSupportedMap(const MapStructureWrapper & newMap, uint32_t & listIndex)
{
// The server instance (caller) is responsible for ensuring that there are no duplicate area IDs, list size not exceeded,
// etc.
// Double-check list size to ensure there no memory issues.
if (mSupportedMaps.size() < kMaxNumSupportedMaps)
{
// not sorting list, number of areas normally expected to be small, max 255
mSupportedMaps.push_back(newMap);
listIndex = static_cast<uint32_t>(mSupportedMaps.size()) - 1; // new element is last in list
return true;
}
ChipLogError(Zcl, "AddSupportedMap %u - supported maps list is already at maximum size %u", newMap.mapID,
static_cast<uint32_t>(kMaxNumSupportedMaps));
return false;
}
bool RvcServiceAreaDelegate::ModifySupportedMap(uint32_t listIndex, const MapStructureWrapper & modifiedMap)
{
// The server instance (caller) is responsible for ensuring that there are no duplicate area IDs, list size not exceeded,
// etc.
// Double-check that mapID's match.
if (modifiedMap.mapID != mSupportedMaps[listIndex].mapID)
{
ChipLogError(Zcl, "ModifySupportedMap - mapID's do not match, new mapID %u, existing mapID %u", modifiedMap.mapID,
mSupportedMaps[listIndex].mapID);
return false;
}
// save modified map
mSupportedMaps[listIndex] = modifiedMap;
return true;
}
bool RvcServiceAreaDelegate::ClearSupportedMaps()
{
if (!mSupportedMaps.empty())
{
mSupportedMaps.clear();
return true;
}
return false;
}
//*************************************************************************
// Selected areas accessors
uint32_t RvcServiceAreaDelegate::GetNumberOfSelectedAreas()
{
return static_cast<uint32_t>(mSelectedAreas.size());
}
bool RvcServiceAreaDelegate::GetSelectedAreaByIndex(uint32_t listIndex, uint32_t & selectedArea)
{
if (listIndex < mSelectedAreas.size())
{
selectedArea = mSelectedAreas[listIndex];
return true;
}
return false;
};
bool RvcServiceAreaDelegate::AddSelectedArea(uint32_t aAreaID, uint32_t & listIndex)
{
// The server instance (caller) is responsible for ensuring that there are no duplicate area IDs, list size not exceeded,
// etc.
// Double-check list size to ensure there no memory issues.
if (mSelectedAreas.size() < kMaxNumSelectedAreas)
{
// not sorting list, number of areas normally expected to be small, max 255
mSelectedAreas.push_back(aAreaID);
listIndex = static_cast<uint32_t>(mSelectedAreas.size()) - 1; // new element is last in list
return true;
}
ChipLogError(Zcl, "AddSelectedArea %u - selected areas list is already at maximum size %u", aAreaID,
static_cast<uint32_t>(kMaxNumSelectedAreas));
return false;
}
bool RvcServiceAreaDelegate::ClearSelectedAreas()
{
if (!mSelectedAreas.empty())
{
mSelectedAreas.clear();
return true;
}
return false;
}
//*************************************************************************
// Progress List accessors
uint32_t RvcServiceAreaDelegate::GetNumberOfProgressElements()
{
return static_cast<uint32_t>(mProgressList.size());
}
bool RvcServiceAreaDelegate::GetProgressElementByIndex(uint32_t listIndex, Structs::ProgressStruct::Type & aProgressElement)
{
if (listIndex < mProgressList.size())
{
aProgressElement = mProgressList[listIndex];
return true;
}
return false;
};
bool RvcServiceAreaDelegate::GetProgressElementById(uint32_t aAreaID, uint32_t & listIndex,
Structs::ProgressStruct::Type & aProgressElement)
{
// We do not need to reimplement this method as it's already done by the SDK.
// We are reimplementing this method, still using linear search, but with some optimization on the SDK implementation
// since we have direct access to the list.
listIndex = 0;
while (listIndex < mProgressList.size())
{
if (mProgressList[listIndex].areaID == aAreaID)
{
aProgressElement = mProgressList[listIndex];
return true;
}
++listIndex;
}
return false;
};
bool RvcServiceAreaDelegate::AddProgressElement(const Structs::ProgressStruct::Type & newProgressElement, uint32_t & listIndex)
{
// The server instance (caller) is responsible for ensuring that there are no duplicate area IDs, list size not exceeded,
// etc.
// Double-check list size to ensure there no memory issues.
if (mProgressList.size() < kMaxNumProgressElements)
{
// not sorting list, number of areas normally expected to be small, max 255
mProgressList.push_back(newProgressElement);
listIndex = static_cast<uint32_t>(mProgressList.size()) - 1; // new element is last in list
return true;
}
ChipLogError(Zcl, "AddProgressElement %u -progress list is already at maximum size %u", newProgressElement.areaID,
static_cast<uint32_t>(kMaxNumProgressElements));
return false;
}
bool RvcServiceAreaDelegate::ModifyProgressElement(uint32_t listIndex,
const Structs::ProgressStruct::Type & modifiedProgressElement)
{
// TODO IMPLEMENT
return false;
}
bool RvcServiceAreaDelegate::ClearProgress()
{
if (!mProgressList.empty())
{
mProgressList.clear();
return true;
}
return false;
}