Skip to content

Commit 76cfea2

Browse files
committed
Created a new cluster object for the basic infromation culster to allow for a mutabel device location.
1 parent b67437b commit 76cfea2

6 files changed

+101
-28
lines changed

src/app/chip_data_model.gni

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ template("chip_data_model") {
182182

183183
sources += [
184184
"${_app_root}/clusters/barrier-control-server/barrier-control-server.h",
185+
"${_app_root}/clusters/basic-information/basic-information-cluster-objects.h",
185186
"${_app_root}/clusters/basic-information/basic-information.h",
186187
"${_app_root}/clusters/color-control-server/color-control-server.h",
187188
"${_app_root}/clusters/door-lock-server/door-lock-server.h",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#pragma once
2+
3+
#include <app-common/zap-generated/cluster-objects.h>
4+
#include <app-common/zap-generated/cluster-enums.h>
5+
6+
7+
namespace chip {
8+
namespace app {
9+
namespace Clusters {
10+
namespace BasicInformation {
11+
12+
const uint32_t kMaxDeviceLocationNameLength = 128;
13+
using DeviceLocatioType = Attributes::DeviceLocation::TypeInfo::Type;
14+
15+
16+
struct MutableDeviceLocation : public DeviceLocatioType
17+
{
18+
MutableDeviceLocation() {
19+
MutableDeviceLocation(CharSpan(mLocationNameBuffer, 0), DataModel::NullNullable, DataModel::NullNullable);
20+
}
21+
22+
MutableDeviceLocation(CharSpan aLocationName, DataModel::Nullable<int16_t> floorNumber, DataModel::Nullable<AreaTypeTag> aAreaTypeTag) {
23+
Set(aLocationName, floorNumber, aAreaTypeTag);
24+
}
25+
26+
MutableDeviceLocation & operator = (const MutableDeviceLocation & other) {
27+
Set(other->locationName, other->floorNumber, other->areaType);
28+
return *this;
29+
}
30+
31+
MutableDeviceLocation(const MutableDeviceLocation & other) : Nullable(other) {
32+
*this = other;
33+
}
34+
35+
// todo is this needed?
36+
MutableDeviceLocation & operator = (const DeviceLocatioType & other) {
37+
if (other.IsNull())
38+
{
39+
this->SetNull();
40+
return *this;
41+
}
42+
43+
Set(other->locationName, other->floorNumber, other->areaType);
44+
return *this;
45+
}
46+
47+
/**
48+
* @brief Set the location name, floor number, and area type tag. The location name is truncated to kMaxDeviceLocationNameLength. The location name is deep copied.
49+
*/
50+
void Set(CharSpan aLocationName, DataModel::Nullable<int16_t> aFloorNumber, DataModel::Nullable<AreaTypeTag> aAreaTypeTag) {
51+
52+
auto nameLength = aLocationName.size();
53+
54+
if (nameLength > kMaxDeviceLocationNameLength)
55+
{
56+
nameLength = kMaxDeviceLocationNameLength;
57+
}
58+
59+
memcpy(mLocationNameBuffer, aLocationName.data(), nameLength);
60+
61+
this->SetNonNull();
62+
this->Value().locationName = CharSpan(mLocationNameBuffer, nameLength);
63+
this->Value().floorNumber = aFloorNumber;
64+
this->Value().areaType = aAreaTypeTag;
65+
}
66+
67+
// todo add setters and getters for all the attributes
68+
69+
private:
70+
char mLocationNameBuffer[kMaxDeviceLocationNameLength] = { 0 };
71+
};
72+
73+
74+
} // namespace BasicInformation
75+
} // namespace Clusters
76+
} // namespace app
77+
} // namespace chip

src/app/clusters/basic-information/basic-information.cpp

+3-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*
1717
*/
1818

19+
#include "basic-information-cluster-objects.h"
1920
#include "basic-information.h"
2021

2122
#include <app-common/zap-generated/attributes/Accessors.h>
@@ -423,12 +424,7 @@ CHIP_ERROR BasicAttrAccess::ReadMaxPathsPerInvoke(AttributeValueEncoder & aEncod
423424

424425
CHIP_ERROR BasicAttrAccess::ReadDeviceLocation(AttributeValueEncoder & aEncoder)
425426
{
426-
427-
char locationNameBuffer[DeviceLayer::ConfigurationManager::kMaxDeviceLocationNameLength] = { 0 };
428-
429-
DeviceLocatioType deviceLocation({
430-
.locationName = MutableCharSpan(locationNameBuffer),
431-
});
427+
MutableDeviceLocation deviceLocation;
432428

433429
ReturnErrorOnFailure(ConfigurationMgr().GetDeviceLocation(deviceLocation));
434430

@@ -437,11 +433,7 @@ CHIP_ERROR BasicAttrAccess::ReadDeviceLocation(AttributeValueEncoder & aEncoder)
437433

438434
CHIP_ERROR BasicAttrAccess::WriteDeviceLocation(AttributeValueDecoder & aDecoder)
439435
{
440-
char locationNameBuffer[DeviceLayer::ConfigurationManager::kMaxDeviceLocationNameLength] = { 0 };
441-
442-
DeviceLocatioType deviceLocation({
443-
.locationName = MutableCharSpan(locationNameBuffer),
444-
});
436+
DeviceLocatioType deviceLocation;
445437

446438
ReturnErrorOnFailure(aDecoder.Decode(deviceLocation));
447439

src/include/platform/ConfigurationManager.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
#endif
3434

3535
#include <app-common/zap-generated/cluster-objects.h>
36+
#include <app/clusters/basic-information/basic-information-cluster-objects.h>
3637
#include <app/data-model/Nullable.h>
3738
#include <lib/core/ClusterEnums.h>
3839
#include <lib/support/Span.h>
3940
#include <platform/PersistedStorage.h>
4041
#include <platform/internal/CHIPDeviceLayerInternal.h>
4142

4243
using DeviceLocatioType = chip::app::Clusters::BasicInformation::Attributes::DeviceLocation::TypeInfo::Type;
44+
using MutableDeviceLocation = chip::app::Clusters::BasicInformation::MutableDeviceLocation;
4345

4446
namespace chip {
4547
namespace Ble {
@@ -80,7 +82,6 @@ class ConfigurationManager
8082
kMaxProductLabelLength = 64,
8183
kMaxSerialNumberLength = 32,
8284
kMaxUniqueIDLength = 32,
83-
kMaxDeviceLocationNameLength = 128,
8485
#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
8586
kMinRotatingDeviceIDUniqueIDLength = 16,
8687
kRotatingDeviceIDUniqueIDLength = CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH,
@@ -136,7 +137,7 @@ class ConfigurationManager
136137
virtual CHIP_ERROR GenerateUniqueId(char * buf, size_t bufSize) = 0;
137138
virtual CHIP_ERROR GetFailSafeArmed(bool & val) = 0;
138139
virtual CHIP_ERROR SetFailSafeArmed(bool val) = 0;
139-
virtual CHIP_ERROR GetDeviceLocation(DeviceLocatioType & location) = 0;
140+
virtual CHIP_ERROR GetDeviceLocation(MutableDeviceLocation & location) = 0;
140141
virtual CHIP_ERROR SetDeviceLocation(DeviceLocatioType location) = 0;
141142

142143
virtual CHIP_ERROR GetBLEDeviceIdentificationInfo(Ble::ChipBLEDeviceIdentificationInfo & deviceIdInfo) = 0;

src/include/platform/internal/GenericConfigurationManagerImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class GenericConfigurationManagerImpl : public ConfigurationManager
8686
*
8787
* Expects that location is initialised with a locationName CharSpan pointing to a buffer of size kMaxDeviceLocationNameLength.
8888
*/
89-
CHIP_ERROR GetDeviceLocation(DeviceLocatioType & location) override;
89+
CHIP_ERROR GetDeviceLocation(MutableDeviceLocation & location) override;
9090
CHIP_ERROR SetDeviceLocation(DeviceLocatioType location) override;
9191
CHIP_ERROR GetBLEDeviceIdentificationInfo(Ble::ChipBLEDeviceIdentificationInfo & deviceIdInfo) override;
9292
bool IsCommissionableDeviceTypeEnabled() override;

src/include/platform/internal/GenericConfigurationManagerImpl.ipp

+16-14
Original file line numberDiff line numberDiff line change
@@ -587,43 +587,45 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::SetFailSafeArmed(bool v
587587
}
588588

589589
template <class ConfigClass>
590-
CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetDeviceLocation(DeviceLocatioType & location)
590+
CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetDeviceLocation(MutableDeviceLocation & location)
591591
{
592-
uint8_t locationData[kMaxDeviceLocationNameLength + sizeof(DeviceLocatioType)];
592+
uint8_t locationData[app::Clusters::BasicInformation::kMaxDeviceLocationNameLength + sizeof(DeviceLocatioType)];
593593
MutableByteSpan locationSpan(locationData);
594594

595595
size_t outLen = 0;
596-
ReturnErrorOnFailure(
597-
ReadConfigValueBin(ConfigClass::kConfigKey_DeviceLocation, locationSpan.data(), locationSpan.size(), outLen));
596+
auto err = ReadConfigValueBin(ConfigClass::kConfigKey_DeviceLocation, locationSpan.data(), locationSpan.size(), outLen);
597+
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
598+
{
599+
location.SetNull();
600+
return CHIP_NO_ERROR;
601+
}
602+
ReturnErrorOnFailure(err);
598603

599604
TLV::TLVReader tlvReader;
600605
tlvReader.Init(locationSpan);
601606

602-
char locationNameBuffer[DeviceLayer::ConfigurationManager::kMaxDeviceLocationNameLength] = { 0 };
607+
char locationNameBuffer[1] = { 0 };
603608
DeviceLocatioType loc({
604-
.locationName = MutableCharSpan(locationNameBuffer),
609+
.locationName = CharSpan(locationNameBuffer, 0),
605610
});
606611

607612
ReturnErrorOnFailure(tlvReader.Next(TLV::AnonymousTag()));
608613
ReturnErrorOnFailure(loc->Decode(tlvReader));
609614

610-
if (loc.IsNull())
615+
// This would not be needed if the Decode methed proprly decodes a null value.
616+
if (loc.Value().locationName.empty() && loc.Value().floorNumber.IsNull() && loc.Value().areaType.IsNull())
611617
{
612-
location.SetNull();
613-
return CHIP_NO_ERROR;
618+
loc.SetNull();
614619
}
615620

616-
memcpy((void *) location.Value().locationName.data(), loc.Value().locationName.data(), loc.Value().locationName.size());
617-
location.Value().locationName.reduce_size(loc.Value().locationName.size());
618-
location.Value().floorNumber = loc.Value().floorNumber;
619-
location.Value().areaType = loc.Value().areaType;
621+
location = loc;
620622

621623
return CHIP_NO_ERROR;
622624
}
623625
template <class ConfigClass>
624626
CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::SetDeviceLocation(DeviceLocatioType location)
625627
{
626-
uint8_t locationData[kMaxDeviceLocationNameLength + sizeof(DeviceLocatioType)];
628+
uint8_t locationData[app::Clusters::BasicInformation::kMaxDeviceLocationNameLength + sizeof(DeviceLocatioType)];
627629
MutableByteSpan locationSpan(locationData);
628630

629631
TLV::TLVWriter tlvWriter;

0 commit comments

Comments
 (0)