forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermostat-delegate-impl.cpp
256 lines (230 loc) · 9.44 KB
/
thermostat-delegate-impl.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
/*
*
* 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 <thermostat-delegate-impl.h>
#include <thermostat-manager.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <lib/support/Span.h>
#include <platform/internal/CHIPDeviceLayerInternal.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters::Thermostat;
using namespace chip::app::Clusters::Thermostat::Structs;
ThermostatDelegate ThermostatDelegate::sInstance;
ThermostatDelegate::ThermostatDelegate()
{
mNumberOfPresets = kMaxNumberOfPresetTypes * kMaxNumberOfPresetsOfEachType;
mNextFreeIndexInPresetsList = 0;
mNextFreeIndexInPendingPresetsList = 0;
InitializePresetTypes();
InitializePresets();
memset(mActivePresetHandleData, 0, sizeof(mActivePresetHandleData));
mActivePresetHandleDataSize = 0;
}
void ThermostatDelegate::InitializePresetTypes()
{
PresetScenarioEnum presetScenarioEnumArray[kMaxNumberOfPresetTypes] = {
PresetScenarioEnum::kOccupied, PresetScenarioEnum::kUnoccupied, PresetScenarioEnum::kSleep,
PresetScenarioEnum::kWake, PresetScenarioEnum::kVacation, PresetScenarioEnum::kGoingToSleep
};
static_assert(ArraySize(presetScenarioEnumArray) <= ArraySize(mPresetTypes));
uint8_t index = 0;
for (PresetScenarioEnum presetScenario : presetScenarioEnumArray)
{
mPresetTypes[index].presetScenario = presetScenario;
mPresetTypes[index].numberOfPresets = kMaxNumberOfPresetsOfEachType;
mPresetTypes[index].presetTypeFeatures =
(presetScenario == PresetScenarioEnum::kOccupied || presetScenario == PresetScenarioEnum::kUnoccupied)
? PresetTypeFeaturesBitmap::kAutomatic
: PresetTypeFeaturesBitmap::kSupportsNames;
index++;
}
}
void ThermostatDelegate::InitializePresets()
{
// Initialize the presets with 2 built in presets - occupied and unoccupied.
PresetScenarioEnum presetScenarioEnumArray[2] = { PresetScenarioEnum::kOccupied, PresetScenarioEnum::kUnoccupied };
static_assert(ArraySize(presetScenarioEnumArray) <= ArraySize(mPresets));
uint8_t index = 0;
for (PresetScenarioEnum presetScenario : presetScenarioEnumArray)
{
mPresets[index].SetPresetScenario(presetScenario);
// Set the preset handle to the preset scenario value as a unique id.
const uint8_t handle[] = { static_cast<uint8_t>(presetScenario) };
mPresets[index].SetPresetHandle(DataModel::MakeNullable(ByteSpan(handle)));
mPresets[index].SetName(NullOptional);
int16_t coolingSetpointValue = static_cast<int16_t>(2500 + (index * 100));
mPresets[index].SetCoolingSetpoint(MakeOptional(coolingSetpointValue));
int16_t heatingSetpointValue = static_cast<int16_t>(2100 - (index * 100));
mPresets[index].SetHeatingSetpoint(MakeOptional(heatingSetpointValue));
mPresets[index].SetBuiltIn(DataModel::MakeNullable(true));
index++;
}
// Set the value of the next free index in the presets list.
mNextFreeIndexInPresetsList = index;
}
CHIP_ERROR ThermostatDelegate::GetPresetTypeAtIndex(size_t index, PresetTypeStruct::Type & presetType)
{
if (index < ArraySize(mPresetTypes))
{
presetType = mPresetTypes[index];
return CHIP_NO_ERROR;
}
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
}
uint8_t ThermostatDelegate::GetNumberOfPresets()
{
return mNumberOfPresets;
}
CHIP_ERROR ThermostatDelegate::GetPresetAtIndex(size_t index, PresetStructWithOwnedMembers & preset)
{
if (index < mNextFreeIndexInPresetsList)
{
preset = mPresets[index];
return CHIP_NO_ERROR;
}
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
}
CHIP_ERROR ThermostatDelegate::GetActivePresetHandle(DataModel::Nullable<MutableByteSpan> & activePresetHandle)
{
if (mActivePresetHandleDataSize != 0)
{
ReturnErrorOnFailure(
CopySpanToMutableSpan(ByteSpan(mActivePresetHandleData, mActivePresetHandleDataSize), activePresetHandle.Value()));
activePresetHandle.Value().reduce_size(mActivePresetHandleDataSize);
}
else
{
activePresetHandle.SetNull();
}
return CHIP_NO_ERROR;
}
CHIP_ERROR ThermostatDelegate::SetActivePresetHandle(const DataModel::Nullable<ByteSpan> & newActivePresetHandle)
{
if (!newActivePresetHandle.IsNull())
{
size_t newActivePresetHandleSize = newActivePresetHandle.Value().size();
if (newActivePresetHandleSize > sizeof(mActivePresetHandleData))
{
ChipLogError(NotSpecified,
"Failed to set ActivePresetHandle. newActivePresetHandle size %u is larger than preset handle size %u",
static_cast<uint8_t>(newActivePresetHandleSize), static_cast<uint8_t>(kPresetHandleSize));
return CHIP_ERROR_NO_MEMORY;
}
memcpy(mActivePresetHandleData, newActivePresetHandle.Value().data(), newActivePresetHandleSize);
mActivePresetHandleDataSize = newActivePresetHandleSize;
ChipLogDetail(NotSpecified, "Set ActivePresetHandle to ");
ChipLogByteSpan(NotSpecified, newActivePresetHandle.Value());
}
else
{
memset(mActivePresetHandleData, 0, sizeof(mActivePresetHandleData));
mActivePresetHandleDataSize = 0;
ChipLogDetail(NotSpecified, "Clear ActivePresetHandle");
}
return CHIP_NO_ERROR;
}
std::optional<System::Clock::Milliseconds16>
ThermostatDelegate::GetAtomicWriteTimeout(DataModel::DecodableList<AttributeId> attributeRequests,
System::Clock::Milliseconds16 timeoutRequest)
{
auto attributeIdsIter = attributeRequests.begin();
bool requestedPresets = false, requestedSchedules = false;
while (attributeIdsIter.Next())
{
auto & attributeId = attributeIdsIter.GetValue();
switch (attributeId)
{
case Attributes::Presets::Id:
requestedPresets = true;
break;
case Attributes::Schedules::Id:
requestedSchedules = true;
break;
default:
return System::Clock::Milliseconds16(0);
}
}
if (attributeIdsIter.GetStatus() != CHIP_NO_ERROR)
{
return System::Clock::Milliseconds16(0);
}
auto timeout = System::Clock::Milliseconds16(0);
if (requestedPresets)
{
// If the client expects to edit the presets, then we'll give it 3 seconds to do so
timeout += std::chrono::milliseconds(3000);
}
if (requestedSchedules)
{
// If the client expects to edit the schedules, then we'll give it 9 seconds to do so
timeout += std::chrono::milliseconds(9000);
}
// If the client requested an even smaller timeout, then use that one
return std::min(timeoutRequest, timeout);
}
void ThermostatDelegate::InitializePendingPresets()
{
mNextFreeIndexInPendingPresetsList = 0;
for (uint8_t indexInPresets = 0; indexInPresets < mNextFreeIndexInPresetsList; indexInPresets++)
{
mPendingPresets[mNextFreeIndexInPendingPresetsList] = mPresets[indexInPresets];
mNextFreeIndexInPendingPresetsList++;
}
}
CHIP_ERROR ThermostatDelegate::AppendToPendingPresetList(const PresetStruct::Type & preset)
{
if (mNextFreeIndexInPendingPresetsList < ArraySize(mPendingPresets))
{
mPendingPresets[mNextFreeIndexInPendingPresetsList] = preset;
if (preset.presetHandle.IsNull())
{
// TODO: #34556 Since we support only one preset of each type, using the octet string containing the preset scenario
// suffices as the unique preset handle. Need to fix this to actually provide unique handles once multiple presets of
// each type are supported.
const uint8_t handle[] = { static_cast<uint8_t>(preset.presetScenario) };
mPendingPresets[mNextFreeIndexInPendingPresetsList].SetPresetHandle(DataModel::MakeNullable(ByteSpan(handle)));
}
mNextFreeIndexInPendingPresetsList++;
return CHIP_NO_ERROR;
}
return CHIP_ERROR_WRITE_FAILED;
}
CHIP_ERROR ThermostatDelegate::GetPendingPresetAtIndex(size_t index, PresetStructWithOwnedMembers & preset)
{
if (index < mNextFreeIndexInPendingPresetsList)
{
preset = mPendingPresets[index];
return CHIP_NO_ERROR;
}
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
}
CHIP_ERROR ThermostatDelegate::ApplyPendingPresets()
{
mNextFreeIndexInPresetsList = 0;
for (uint8_t indexInPendingPresets = 0; indexInPendingPresets < mNextFreeIndexInPendingPresetsList; indexInPendingPresets++)
{
const PresetStructWithOwnedMembers & pendingPreset = mPendingPresets[indexInPendingPresets];
mPresets[mNextFreeIndexInPresetsList] = pendingPreset;
mNextFreeIndexInPresetsList++;
}
return CHIP_NO_ERROR;
}
void ThermostatDelegate::ClearPendingPresetList()
{
mNextFreeIndexInPendingPresetsList = 0;
}