Skip to content

Commit a1ec85f

Browse files
committed
Committing work before merge
1 parent 81f10e0 commit a1ec85f

File tree

4 files changed

+87
-26
lines changed

4 files changed

+87
-26
lines changed

examples/thermostat/thermostat-common/src/thermostat-schedules-manager.cpp

+76-17
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,33 @@ using namespace chip::app::Clusters::Thermostat;
88
using namespace chip::app::Clusters::Thermostat::Structs;
99

1010
// built in presets will be prefixed with B
11-
constexpr const char * kBuiltInOneHandle = "B1";
11+
constexpr const char * kBuiltInPOneHandle = "BP1";
1212
constexpr const char * kOccupiedName = "Occupied Default";
1313

14-
constexpr const char * kBuiltinTwoHandle = "B2";
14+
constexpr const char * kBuiltinPTwoHandle = "BP2";
1515
constexpr const char * kUnoccupiedName = "Unoccupied Default";
1616

1717
constexpr const int kMaxPresets = 10;
1818

19-
// user presets will be prefixed with U
20-
static const char * userPresetPrefix = "U";
21-
static uint32_t nextNewHandle = 0;
19+
// user presets will be prefixed with UP
20+
static const char * userPresetPrefix = "UP";
21+
static uint32_t nextNewPresetHandle = 0;
22+
23+
// user schedules will be prefixed with US
24+
static const char * userSchedulePrefix = "US";
25+
static uint32_t nextNewScheduleHandle = 0;
2226

2327
static PresetStruct::Type BuiltInPresets[] = {
2428
{ .presetHandle =
25-
DataModel::Nullable<chip::ByteSpan>(chip::ByteSpan(Uint8::from_const_char(kBuiltInOneHandle), strlen(kBuiltInOneHandle))),
29+
DataModel::Nullable<chip::ByteSpan>(chip::ByteSpan(Uint8::from_const_char(kBuiltInPOneHandle), strlen(kBuiltInPOneHandle))),
2630
.presetScenario = PresetScenarioEnum::kOccupied,
2731
.name = Optional<DataModel::Nullable<chip::CharSpan>>(
2832
DataModel::Nullable<chip::CharSpan>(chip::CharSpan(kOccupiedName, strlen(kOccupiedName)))),
2933
.coolingSetpoint = Optional<int16_t>(2400),
3034
.heatingSetpoint = Optional<int16_t>(1800),
3135
.builtIn = DataModel::Nullable<bool>(true) },
3236
{ .presetHandle =
33-
DataModel::Nullable<chip::ByteSpan>(chip::ByteSpan(Uint8::from_const_char(kBuiltinTwoHandle), strlen(kBuiltinTwoHandle))),
37+
DataModel::Nullable<chip::ByteSpan>(chip::ByteSpan(Uint8::from_const_char(kBuiltinPTwoHandle), strlen(kBuiltinPTwoHandle))),
3438
.presetScenario = PresetScenarioEnum::kUnoccupied,
3539
.name = Optional<DataModel::Nullable<chip::CharSpan>>(
3640
DataModel::Nullable<chip::CharSpan>(chip::CharSpan(kUnoccupiedName, strlen(kUnoccupiedName)))),
@@ -39,12 +43,36 @@ static PresetStruct::Type BuiltInPresets[] = {
3943
.builtIn = DataModel::Nullable<bool>(true) }
4044
};
4145

46+
// built in schedules will be prefixed with B
47+
constexpr const char * kBuiltInSOneHandle = "BS1";
48+
constexpr const char * kBuildInScheduleOneName = "Schedule1";
49+
constexpr const char * kBuiltinSTwoHandle = "BS2";
50+
constexpr const char * kBuildInScheduleTwoName = "Schedule2";
51+
constexpr const int kMaxSchedules = 10;
52+
53+
static ScheduleStruct::Type BuiltInSchedules[] = {
54+
{
55+
.scheduleHandle = DataModel::Nullable<chip::ByteSpan>(chip::ByteSpan(Uint8::from_const_char(kBuiltInSOneHandle), strlen(kBuiltInSOneHandle))),
56+
.systemMode = SystemModeEnum::kAuto,
57+
.name = Optional<chip::CharSpan>(chip::CharSpan(kBuildInScheduleOneName, strlen(kBuildInScheduleOneName))),
58+
.presetHandle = Optional<chip::ByteSpan>(chip::ByteSpan(Uint8::from_const_char(kBuiltInPOneHandle), strlen(kBuiltInPOneHandle))),
59+
.transitions = DataModel::List<const Structs::ScheduleTransitionStruct::Type>(),
60+
.builtIn = Optional<DataModel::Nullable<bool>>(DataModel::Nullable<bool>(true))
61+
},
62+
};
63+
4264
static unsigned int gsActivePresetsEmptyIndex = 0;
4365
static std::array<PresetStruct::Type, kMaxPresets> gsActivePresets;
4466

4567
static unsigned int gsEditingPresetsEmptyIndex = 0;
4668
static std::array<PresetStruct::Type, kMaxPresets> gsEditingPresets;
4769

70+
static unsigned int gsActiveSchedulesEmptyIndex = 0;
71+
static std::array<ScheduleStruct::Type, kMaxSchedules> gsActiveSchedules;
72+
73+
static unsigned int gsEditingSchedulesEmptyIndex = 0;
74+
static std::array<ScheduleStruct::Type, kMaxSchedules> gsEditingSchedules;
75+
4876
static void onEditStart(ThermostatMatterScheduleManager * mgr)
4977
{
5078
ChipLogProgress(Zcl, "ThermstatScheduleManager - onEditStart");
@@ -54,30 +82,61 @@ static void onEditCancel(ThermostatMatterScheduleManager * mgr)
5482
{
5583
ChipLogProgress(Zcl, "ThermstatScheduleManager - onEditCancel");
5684
gsEditingPresetsEmptyIndex = 0;
85+
gsEditingSchedulesEmptyIndex = 0;
5786
}
5887

5988
static EmberAfStatus onEditCommit(ThermostatMatterScheduleManager * mgr)
6089
{
6190
EmberAfStatus status = EMBER_ZCL_STATUS_SUCCESS;
6291

6392
ChipLogProgress(Zcl, "ThermstatScheduleManager - onEditCommit");
64-
Span<PresetStruct::Type> oldPresets = Span<PresetStruct::Type>(gsActivePresets).SubSpan(0, gsActivePresetsEmptyIndex);
65-
Span<PresetStruct::Type> newPresets = Span<PresetStruct::Type>(gsEditingPresets).SubSpan(0, gsEditingPresetsEmptyIndex);
6693

67-
status = mgr->ThermostatMatterScheduleManager::ValidatePresetsForCommitting(oldPresets, newPresets);
68-
SuccessOrExit(status);
94+
if (gsEditingPresetsEmptyIndex != 0)
95+
{
96+
Span<PresetStruct::Type> oldPresets = Span<PresetStruct::Type>(gsActivePresets).SubSpan(0, gsActivePresetsEmptyIndex);
97+
Span<PresetStruct::Type> newPresets = Span<PresetStruct::Type>(gsEditingPresets).SubSpan(0, gsEditingPresetsEmptyIndex);
98+
99+
status = mgr->ThermostatMatterScheduleManager::ValidatePresetsForCommitting(oldPresets, newPresets);
100+
SuccessOrExit(status);
69101

70-
// New presets look good, lets generate some new ID's for the new presets.
71-
for (unsigned int index = 0; index < gsEditingPresetsEmptyIndex; ++index)
102+
// New presets look good, lets generate some new ID's for the new presets.
103+
for (unsigned int index = 0; index < gsEditingPresetsEmptyIndex; ++index)
104+
{
105+
if (gsEditingPresets[index].presetHandle.IsNull() || gsEditingPresets[index].presetHandle.Value().empty())
106+
{
107+
char handle[16];
108+
snprintf(handle, 16, "%s%d", userPresetPrefix, nextNewPresetHandle++);
109+
gsEditingPresets[index].presetHandle.SetNonNull(ByteSpan((const unsigned char *) handle, strlen(handle)));
110+
}
111+
}
112+
}
113+
114+
if (gsEditingSchedulesEmptyIndex != 0)
72115
{
73-
if (gsEditingPresets[index].presetHandle.IsNull() || gsEditingPresets[index].presetHandle.Value().empty())
116+
Span<ScheduleStruct::Type> oldSchedules = Span<ScheduleStruct::Type>(gsActiveSchedules).SubSpan(0, gsActiveSchedulesEmptyIndex);
117+
Span<ScheduleStruct::Type> newSchedules = Span<ScheduleStruct::Type>(gsEditingSchedules).SubSpan(0, gsEditingSchedulesEmptyIndex);
118+
Span<PresetStruct::Type> presets = gsEditingPresetsEmptyIndex > 0 ? Span<PresetStruct::Type>(gsEditingPresets).SubSpan(0, gsEditingPresetsEmptyIndex) :
119+
Span<PresetStruct::Type>(gsActivePresets).SubSpan(0, gsActivePresetsEmptyIndex);
120+
121+
status = mgr->ThermostatMatterScheduleManager::ValidateSchedulesForCommitting(oldSchedules, newSchedules, presets);
122+
SuccessOrExit(status);
123+
124+
// New schedules look good, lets generate some new ID's for the new schedules.
125+
for (unsigned int index = 0; index < gsEditingSchedulesEmptyIndex; ++index)
74126
{
75-
char handle[16];
76-
snprintf(handle, 16, "%s%d", userPresetPrefix, nextNewHandle++);
77-
gsEditingPresets[index].presetHandle.SetNonNull(ByteSpan((const unsigned char *) handle, strlen(handle)));
127+
if (gsEditingSchedules[index].scheduleHandle.IsNull() || gsEditingSchedules[index].scheduleHandle.Value().empty())
128+
{
129+
char handle[16];
130+
snprintf(handle, 16, "%s%d", userSchedulePrefix, nextNewScheduleHandle++);
131+
gsEditingSchedules[index].scheduleHandle.SetNonNull(ByteSpan((const unsigned char *) handle, strlen(handle)));
132+
}
78133
}
79134
}
80135

136+
137+
// Everything *SHOULD* be validated now, so lets commit them.
138+
139+
81140
// copy the presets to the active list.
82141
gsActivePresets = gsEditingPresets;
83142
gsEditingPresetsEmptyIndex = 0;

src/app/chip_data_model.gni

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ template("chip_data_model") {
342342
"${_app_root}/clusters/${cluster}/${cluster}.h",
343343
"${_app_root}/clusters/${cluster}/thermostat-preset-commit-validation.cpp",
344344
"${_app_root}/clusters/${cluster}/thermostat-schedule-commit-validation.cpp",
345+
]
345346
} else if (cluster == "energy-evse-server") {
346347
sources += [
347348
"${_app_root}/clusters/${cluster}/${cluster}.cpp",

src/app/clusters/thermostat-server/thermostat-schedule-commit-validation.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ using DecodableType = Type;
142142
} // namespace ScheduleTypeStruct
143143
#endif
144144

145-
static EmberAfStatus CheckScheduleTypes(ThermostatMatterScheduleManager & mgr, ScheduleStruct::Type & schedule)
145+
static EmberAfStatus CheckScheduleTypes(ThermostatMatterScheduleManager & mgr, ScheduleStruct::Type & schedule, Span<PresetStruct::Type> & presetList)
146146
{
147147
EmberAfStatus status = EMBER_ZCL_STATUS_CONSTRAINT_ERROR;
148148
size_t index = 0;
@@ -161,12 +161,11 @@ static EmberAfStatus CheckScheduleTypes(ThermostatMatterScheduleManager & mgr, S
161161
VerifyOrReturnError(presetsSupported == true, EMBER_ZCL_STATUS_CONSTRAINT_ERROR);
162162

163163
// make sure the preset exists (check 7)
164-
VerifyOrDie(mgr.mGetPresetAtIndexCb);
165164
{
166165
size_t preset_index = 0;
167166
PresetStruct::Type preset;
168167
bool presetFound = false;
169-
while (mgr.mGetPresetAtIndexCb(&mgr, preset_index, preset) != CHIP_ERROR_NOT_FOUND)
168+
while (presetList[index] != CHIP_ERROR_NOT_FOUND)
170169
{
171170
VerifyOrDie(preset.presetHandle.IsNull() == false);
172171
if (preset.presetHandle.Value().data_equal(schedule.presetHandle.Value()))
@@ -244,7 +243,8 @@ static EmberAfStatus CheckNumberOfTransitions(ThermostatMatterScheduleManager &
244243
}
245244

246245
EmberAfStatus ThermostatMatterScheduleManager::ValidateSchedulesForCommitting(Span<ScheduleStruct::Type> & oldlist,
247-
Span<ScheduleStruct::Type> & newlist)
246+
Span<ScheduleStruct::Type> & newlist,
247+
Span<PresetStruct::Type> & presetlist)
248248
{
249249
EmberAfStatus status = EMBER_ZCL_STATUS_SUCCESS;
250250
ScheduleStruct::Type querySchedule; // manager storage used for queries.
@@ -306,7 +306,7 @@ EmberAfStatus ThermostatMatterScheduleManager::ValidateSchedulesForCommitting(Sp
306306
}
307307

308308
// Check for system mode in Schedule Types
309-
status = CheckScheduleTypes(*this, new_schedule);
309+
status = CheckScheduleTypes(*this, new_schedule, presetlist);
310310
SuccessOrExit(status);
311311

312312
// Make sure the number of transitions does not exceed out limits

src/app/clusters/thermostat-server/thermostat-server.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ struct ThermostatMatterScheduleManager
122122
ThermostatMatterScheduleManager * next() { return this->nextEditor; }
123123
void setNext(ThermostatMatterScheduleManager * inst) { this->nextEditor = inst; }
124124

125-
EmberAfStatus ValidatePresetsForCommitting(chip::Span<chip::app::Clusters::Thermostat::Structs::PresetStruct::Type> & oldlist,
126-
chip::Span<chip::app::Clusters::Thermostat::Structs::PresetStruct::Type> & newlist);
125+
EmberAfStatus ValidatePresetsForCommitting(chip::Span<chip::app::Clusters::Thermostat::Structs::PresetStruct::Type> & oldList,
126+
chip::Span<chip::app::Clusters::Thermostat::Structs::PresetStruct::Type> & newList);
127127
EmberAfStatus
128-
ValidateSchedulesForCommitting(chip::Span<chip::app::Clusters::Thermostat::Structs::ScheduleStruct::Type> & oldlist,
129-
chip::Span<chip::app::Clusters::Thermostat::Structs::ScheduleStruct::Type> & newlist);
128+
ValidateSchedulesForCommitting(chip::Span<chip::app::Clusters::Thermostat::Structs::ScheduleStruct::Type> & oldList,
129+
chip::Span<chip::app::Clusters::Thermostat::Structs::ScheduleStruct::Type> & newList,
130+
chip::Span<chip::app::Clusters::Thermostat::Structs::PresetStruct::Type> & presetList);
130131
};

0 commit comments

Comments
 (0)