forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-format-localization-server.cpp
235 lines (194 loc) · 8.02 KB
/
time-format-localization-server.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
/*
*
* Copyright (c) 2021 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.
*/
/****************************************************************************
* @file
* @brief Implementation for the Time Format Localization Server Cluster
***************************************************************************/
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-enums-check.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/AttributeAccessInterface.h>
#include <app/util/attribute-storage.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/DeviceInfoProvider.h>
#include <platform/PlatformManager.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::TimeFormatLocalization;
using namespace chip::app::Clusters::TimeFormatLocalization::Attributes;
using namespace chip::DeviceLayer;
using chip::Protocols::InteractionModel::Status;
namespace {
class TimeFormatLocalizationAttrAccess : public AttributeAccessInterface
{
public:
// Register for the Time Format Localization cluster on all endpoints.
TimeFormatLocalizationAttrAccess() : AttributeAccessInterface(Optional<EndpointId>::Missing(), TimeFormatLocalization::Id) {}
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
private:
CHIP_ERROR ReadSupportedCalendarTypes(AttributeValueEncoder & aEncoder);
};
class AutoReleaseIterator
{
public:
using Iterator = DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator;
AutoReleaseIterator(Iterator * value) : mIterator(value) {}
~AutoReleaseIterator()
{
if (mIterator != nullptr)
{
mIterator->Release();
}
}
bool IsValid() const { return mIterator != nullptr; }
bool Next(CalendarTypeEnum & value) { return (mIterator == nullptr) ? false : mIterator->Next(value); }
private:
Iterator * mIterator;
};
TimeFormatLocalizationAttrAccess gAttrAccess;
bool HasFeature(EndpointId endpoint, Feature feature)
{
uint32_t featureMap;
return FeatureMap::Get(endpoint, &featureMap) == Status::Success ? (featureMap & to_underlying(feature)) : false;
}
CHIP_ERROR TimeFormatLocalizationAttrAccess::ReadSupportedCalendarTypes(AttributeValueEncoder & aEncoder)
{
DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider();
VerifyOrReturnValue(provider != nullptr, aEncoder.EncodeEmptyList());
AutoReleaseIterator it(provider->IterateSupportedCalendarTypes());
VerifyOrReturnValue(it.IsValid(), aEncoder.EncodeEmptyList());
return aEncoder.EncodeList([&it](const auto & encoder) -> CHIP_ERROR {
CalendarTypeEnum type;
while (it.Next(type))
{
ReturnErrorOnFailure(encoder.Encode(type));
}
return CHIP_NO_ERROR;
});
}
CHIP_ERROR TimeFormatLocalizationAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
VerifyOrDie(aPath.mClusterId == TimeFormatLocalization::Id);
switch (aPath.mAttributeId)
{
case SupportedCalendarTypes::Id:
return ReadSupportedCalendarTypes(aEncoder);
default:
break;
}
return CHIP_NO_ERROR;
}
// Returns whether newType is a valid calendar type. If it's not, validType is set to a valid calendar type,
// if there are any, and to kBuddhist if there are not.
bool IsSupportedCalendarType(CalendarTypeEnum newType, CalendarTypeEnum & validType)
{
// Reset valid type if no supported calendar types found.
validType = CalendarTypeEnum::kBuddhist;
DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider();
VerifyOrReturnValue(provider != nullptr, false);
AutoReleaseIterator it(provider->IterateSupportedCalendarTypes());
VerifyOrReturnValue(it.IsValid(), false);
CalendarTypeEnum type;
while (it.Next(type))
{
validType = type;
if (validType == newType)
{
return true;
}
}
return false;
}
template <typename E>
Optional<E> SafeCast(uint8_t value)
{
E val = static_cast<E>(value);
if (EnsureKnownEnumValue(val) == E::kUnknownEnumValue)
{
return NullOptional;
}
return MakeOptional(val);
}
} // anonymous namespace
// =============================================================================
// Pre-change callbacks for cluster attributes
// =============================================================================
static Protocols::InteractionModel::Status emberAfPluginTimeFormatLocalizationOnCalendarTypeChange(EndpointId EndpointId,
CalendarTypeEnum newType)
{
Protocols::InteractionModel::Status res;
CalendarTypeEnum validType = CalendarTypeEnum::kUseActiveLocale;
if (IsSupportedCalendarType(newType, validType))
{
res = Protocols::InteractionModel::Status::Success;
}
else
{
res = Protocols::InteractionModel::Status::InvalidValue;
ChipLogError(Zcl, "Trying to write invalid Calendar Type");
}
return res;
}
Protocols::InteractionModel::Status MatterTimeFormatLocalizationClusterServerPreAttributeChangedCallback(
const ConcreteAttributePath & attributePath, EmberAfAttributeType attributeType, uint16_t size, uint8_t * value)
{
switch (attributePath.mAttributeId)
{
case ActiveCalendarType::Id: {
VerifyOrReturnValue(sizeof(uint8_t) == size, Protocols::InteractionModel::Status::InvalidValue);
auto calendarType = SafeCast<CalendarTypeEnum>(*value);
VerifyOrReturnValue(calendarType.HasValue(), Protocols::InteractionModel::Status::ConstraintError);
return emberAfPluginTimeFormatLocalizationOnCalendarTypeChange(attributePath.mEndpointId, calendarType.Value());
}
case HourFormat::Id: {
VerifyOrReturnValue(sizeof(uint8_t) == size, Protocols::InteractionModel::Status::InvalidValue);
auto hourFormat = SafeCast<HourFormatEnum>(*value);
VerifyOrReturnValue(hourFormat.HasValue(), Protocols::InteractionModel::Status::ConstraintError);
return Protocols::InteractionModel::Status::Success;
}
default:
return Protocols::InteractionModel::Status::Success;
}
}
void emberAfTimeFormatLocalizationClusterServerInitCallback(EndpointId endpoint)
{
if (!HasFeature(endpoint, Feature::kCalendarFormat))
{
return;
}
CalendarTypeEnum calendarType;
CalendarTypeEnum validType;
Status status = ActiveCalendarType::Get(endpoint, &calendarType);
VerifyOrReturn(Status::Success == status,
ChipLogError(Zcl, "Failed to read calendar type with error: 0x%02x", to_underlying(status)));
// We could have an invalid calendar type value if an OTA update removed support for the value we were using.
// If initial value is not one of the allowed values, pick one valid value and write it.
if (!IsSupportedCalendarType(calendarType, validType))
{
status = ActiveCalendarType::Set(endpoint, validType);
VerifyOrReturn(Status::Success == status,
ChipLogError(Zcl, "Failed to write calendar type with error: 0x%02x", to_underlying(status)));
}
}
void MatterTimeFormatLocalizationPluginServerInitCallback()
{
registerAttributeAccessOverride(&gAttrAccess);
}