-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathenergy-evse-server.cpp
503 lines (442 loc) · 18.4 KB
/
energy-evse-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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* Copyright (c) 2023 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.
*/
#include "energy-evse-server.h"
#include <app/AttributeAccessInterface.h>
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/CommandHandlerInterfaceRegistry.h>
#include <app/ConcreteAttributePath.h>
#include <app/InteractionModelEngine.h>
#include <app/util/attribute-storage.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::DataModel;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::EnergyEvse;
using namespace chip::app::Clusters::EnergyEvse::Attributes;
using chip::Protocols::InteractionModel::Status;
namespace chip {
namespace app {
namespace Clusters {
namespace EnergyEvse {
CHIP_ERROR Instance::Init()
{
ReturnErrorOnFailure(CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(this));
VerifyOrReturnError(AttributeAccessInterfaceRegistry::Instance().Register(this), CHIP_ERROR_INCORRECT_STATE);
return CHIP_NO_ERROR;
}
void Instance::Shutdown()
{
CommandHandlerInterfaceRegistry::Instance().UnregisterCommandHandler(this);
AttributeAccessInterfaceRegistry::Instance().Unregister(this);
}
bool Instance::HasFeature(Feature aFeature) const
{
return mFeature.Has(aFeature);
}
bool Instance::SupportsOptAttr(OptionalAttributes aOptionalAttrs) const
{
return mOptionalAttrs.Has(aOptionalAttrs);
}
bool Instance::SupportsOptCmd(OptionalCommands aOptionalCmds) const
{
return mOptionalCmds.Has(aOptionalCmds);
}
// AttributeAccessInterface
CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
switch (aPath.mAttributeId)
{
case State::Id:
return aEncoder.Encode(mDelegate.GetState());
case SupplyState::Id:
return aEncoder.Encode(mDelegate.GetSupplyState());
case FaultState::Id:
return aEncoder.Encode(mDelegate.GetFaultState());
case ChargingEnabledUntil::Id:
return aEncoder.Encode(mDelegate.GetChargingEnabledUntil());
case DischargingEnabledUntil::Id:
/* V2X */
return aEncoder.Encode(mDelegate.GetDischargingEnabledUntil());
case CircuitCapacity::Id:
return aEncoder.Encode(mDelegate.GetCircuitCapacity());
case MinimumChargeCurrent::Id:
return aEncoder.Encode(mDelegate.GetMinimumChargeCurrent());
case MaximumChargeCurrent::Id:
return aEncoder.Encode(mDelegate.GetMaximumChargeCurrent());
case MaximumDischargeCurrent::Id:
/* V2X */
return aEncoder.Encode(mDelegate.GetMaximumDischargeCurrent());
case UserMaximumChargeCurrent::Id:
return aEncoder.Encode(mDelegate.GetUserMaximumChargeCurrent());
case RandomizationDelayWindow::Id:
/* Optional */
return aEncoder.Encode(mDelegate.GetRandomizationDelayWindow());
/* PREF - ChargingPreferences attributes */
case NextChargeStartTime::Id:
return aEncoder.Encode(mDelegate.GetNextChargeStartTime());
case NextChargeTargetTime::Id:
return aEncoder.Encode(mDelegate.GetNextChargeTargetTime());
case NextChargeRequiredEnergy::Id:
return aEncoder.Encode(mDelegate.GetNextChargeRequiredEnergy());
case NextChargeTargetSoC::Id:
return aEncoder.Encode(mDelegate.GetNextChargeTargetSoC());
case ApproximateEVEfficiency::Id:
return aEncoder.Encode(mDelegate.GetApproximateEVEfficiency());
/* SOC attributes */
case StateOfCharge::Id:
return aEncoder.Encode(mDelegate.GetStateOfCharge());
case BatteryCapacity::Id:
return aEncoder.Encode(mDelegate.GetBatteryCapacity());
/* PNC attributes*/
case VehicleID::Id:
return aEncoder.Encode(mDelegate.GetVehicleID());
/* Session SESS attributes */
case SessionID::Id:
return aEncoder.Encode(mDelegate.GetSessionID());
case SessionDuration::Id:
return aEncoder.Encode(mDelegate.GetSessionDuration());
case SessionEnergyCharged::Id:
return aEncoder.Encode(mDelegate.GetSessionEnergyCharged());
case SessionEnergyDischarged::Id:
return aEncoder.Encode(mDelegate.GetSessionEnergyDischarged());
/* FeatureMap - is held locally */
case FeatureMap::Id:
return aEncoder.Encode(mFeature);
}
/* Allow all other unhandled attributes to fall through to Ember */
return CHIP_NO_ERROR;
}
CHIP_ERROR Instance::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder)
{
switch (aPath.mAttributeId)
{
case UserMaximumChargeCurrent::Id: {
// Optional Attribute
if (!SupportsOptAttr(OptionalAttributes::kSupportsUserMaximumChargingCurrent))
{
return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute);
}
int64_t newValue;
ReturnErrorOnFailure(aDecoder.Decode(newValue));
ReturnErrorOnFailure(mDelegate.SetUserMaximumChargeCurrent(newValue));
return CHIP_NO_ERROR;
}
case RandomizationDelayWindow::Id: {
// Optional Attribute
if (!SupportsOptAttr(OptionalAttributes::kSupportsRandomizationWindow))
{
return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute);
}
uint32_t newValue;
ReturnErrorOnFailure(aDecoder.Decode(newValue));
ReturnErrorOnFailure(mDelegate.SetRandomizationDelayWindow(newValue));
return CHIP_NO_ERROR;
}
case ApproximateEVEfficiency::Id: {
// Optional Attribute if ChargingPreferences is supported
if ((!HasFeature(Feature::kChargingPreferences)) ||
(!SupportsOptAttr(OptionalAttributes::kSupportsApproximateEvEfficiency)))
{
return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute);
}
uint16_t newValue;
ReturnErrorOnFailure(aDecoder.Decode(newValue));
ReturnErrorOnFailure(mDelegate.SetApproximateEVEfficiency(MakeNullable(newValue)));
return CHIP_NO_ERROR;
}
default:
// Unknown attribute; return error. None of the other attributes for
// this cluster are writable, so should not be ending up in this code to
// start with.
return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute);
}
}
// CommandHandlerInterface
CHIP_ERROR Instance::EnumerateAcceptedCommands(const ConcreteClusterPath & cluster,
DataModel::ListBuilder<DataModel::AcceptedCommandEntry> & builder)
{
using namespace Commands;
using QF = DataModel::CommandQualityFlags;
using Priv = chip::Access::Privilege;
ReturnErrorOnFailure(builder.EnsureAppendCapacity(7));
ReturnErrorOnFailure(builder.AppendElements({
{ Disable::Id, QF::kTimed, Priv::kOperate }, //
{ EnableCharging::Id, QF::kTimed, Priv::kOperate }, //
}));
if (HasFeature(Feature::kV2x))
{
ReturnErrorOnFailure(builder.Append({ EnableDischarging::Id, QF::kTimed, Priv::kOperate }));
}
if (HasFeature(Feature::kChargingPreferences))
{
ReturnErrorOnFailure(builder.AppendElements({
{ SetTargets::Id, QF::kTimed, Priv::kOperate }, //
{ GetTargets::Id, QF::kTimed, Priv::kOperate }, //
{ ClearTargets::Id, QF::kTimed, Priv::kOperate }, //
}));
}
if (SupportsOptCmd(OptionalCommands::kSupportsStartDiagnostics))
{
ReturnErrorOnFailure(builder.Append({ StartDiagnostics::Id, QF::kTimed, Priv::kOperate }));
}
return CHIP_NO_ERROR;
}
void Instance::InvokeCommand(HandlerContext & handlerContext)
{
using namespace Commands;
switch (handlerContext.mRequestPath.mCommandId)
{
case Disable::Id:
HandleCommand<Disable::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleDisable(ctx, commandData); });
return;
case EnableCharging::Id:
HandleCommand<EnableCharging::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleEnableCharging(ctx, commandData); });
return;
case EnableDischarging::Id:
if (!HasFeature(Feature::kV2x))
{
handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath, Status::UnsupportedCommand);
}
else
{
HandleCommand<EnableDischarging::DecodableType>(handlerContext, [this](HandlerContext & ctx, const auto & commandData) {
HandleEnableDischarging(ctx, commandData);
});
}
return;
case StartDiagnostics::Id:
if (!SupportsOptCmd(OptionalCommands::kSupportsStartDiagnostics))
{
handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath, Status::UnsupportedCommand);
}
else
{
HandleCommand<StartDiagnostics::DecodableType>(handlerContext, [this](HandlerContext & ctx, const auto & commandData) {
HandleStartDiagnostics(ctx, commandData);
});
}
return;
case SetTargets::Id:
if (!HasFeature(Feature::kChargingPreferences))
{
handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath, Status::UnsupportedCommand);
}
else
{
HandleCommand<SetTargets::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleSetTargets(ctx, commandData); });
}
return;
case GetTargets::Id:
if (!HasFeature(Feature::kChargingPreferences))
{
handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath, Status::UnsupportedCommand);
}
else
{
HandleCommand<GetTargets::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleGetTargets(ctx, commandData); });
}
return;
case ClearTargets::Id:
if (!HasFeature(Feature::kChargingPreferences))
{
handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath, Status::UnsupportedCommand);
}
else
{
HandleCommand<ClearTargets::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleClearTargets(ctx, commandData); });
}
return;
}
}
void Instance::HandleDisable(HandlerContext & ctx, const Commands::Disable::DecodableType & commandData)
{
// No parameters for this command
// Call the delegate
Status status = mDelegate.Disable();
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
void Instance::HandleEnableCharging(HandlerContext & ctx, const Commands::EnableCharging::DecodableType & commandData)
{
auto & chargingEnabledUntil = commandData.chargingEnabledUntil;
auto & minimumChargeCurrent = commandData.minimumChargeCurrent;
auto & maximumChargeCurrent = commandData.maximumChargeCurrent;
if (minimumChargeCurrent < kMinimumChargeCurrent)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::ConstraintError);
return;
}
if (maximumChargeCurrent < kMinimumChargeCurrent)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::ConstraintError);
return;
}
if (minimumChargeCurrent > maximumChargeCurrent)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::ConstraintError);
return;
}
// Call the delegate
Status status = mDelegate.EnableCharging(chargingEnabledUntil, minimumChargeCurrent, maximumChargeCurrent);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
void Instance::HandleEnableDischarging(HandlerContext & ctx, const Commands::EnableDischarging::DecodableType & commandData)
{
auto & dischargingEnabledUntil = commandData.dischargingEnabledUntil;
auto & maximumDischargeCurrent = commandData.maximumDischargeCurrent;
if (maximumDischargeCurrent < kMinimumChargeCurrent)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Status::ConstraintError);
return;
}
// Call the delegate
Status status = mDelegate.EnableDischarging(dischargingEnabledUntil, maximumDischargeCurrent);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
void Instance::HandleStartDiagnostics(HandlerContext & ctx, const Commands::StartDiagnostics::DecodableType & commandData)
{
// No parameters for this command
// Call the delegate
Status status = mDelegate.StartDiagnostics();
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
void Instance::HandleSetTargets(HandlerContext & ctx, const Commands::SetTargets::DecodableType & commandData)
{
// Call the delegate
auto & chargingTargetSchedules = commandData.chargingTargetSchedules;
Status status = ValidateTargets(chargingTargetSchedules);
if (status != Status::Success)
{
ChipLogError(AppServer, "SetTargets contained invalid data - Rejecting");
}
else
{
status = mDelegate.SetTargets(chargingTargetSchedules);
}
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
Status Instance::ValidateTargets(
const DataModel::DecodableList<Structs::ChargingTargetScheduleStruct::DecodableType> & chargingTargetSchedules)
{
/* A) check that the targets are valid
* 1) each target must be within valid range (TargetTimeMinutesPastMidnight < 1440)
* 2) each target must be within valid range (TargetSoC percent 0 - 100)
* If SOC feature not supported then this MUST be 100 or not present
* 3) each target must be within valid range (AddedEnergy >= 0)
* B) Day of Week is only allowed to be included once
*/
uint8_t dayOfWeekBitmap = 0;
auto iter = chargingTargetSchedules.begin();
while (iter.Next())
{
auto & entry = iter.GetValue();
uint8_t bitmask = entry.dayOfWeekForSequence.GetField(static_cast<TargetDayOfWeekBitmap>(0x7F));
ChipLogProgress(AppServer, "DayOfWeekForSequence = 0x%02x", bitmask);
if ((dayOfWeekBitmap & bitmask) != 0)
{
// A bit has already been set - Return ConstraintError
ChipLogError(AppServer, "DayOfWeekForSequence has a bit set which has already been set in another entry.");
return Status::ConstraintError;
}
dayOfWeekBitmap |= bitmask; // add this day Of week to the previously seen days
auto iterInner = entry.chargingTargets.begin();
uint8_t innerIdx = 0;
while (iterInner.Next())
{
auto & targetStruct = iterInner.GetValue();
uint16_t minutesPastMidnight = targetStruct.targetTimeMinutesPastMidnight;
ChipLogProgress(AppServer, "[%d] MinutesPastMidnight : %d", innerIdx,
static_cast<short unsigned int>(minutesPastMidnight));
if (minutesPastMidnight > 1439)
{
ChipLogError(AppServer, "MinutesPastMidnight has invalid value (%d)", static_cast<int>(minutesPastMidnight));
return Status::ConstraintError;
}
// If SocReporting is supported, targetSoc must have a value in the range [0, 100]
if (HasFeature(Feature::kSoCReporting))
{
if (!targetStruct.targetSoC.HasValue())
{
ChipLogError(AppServer, "kSoCReporting is supported but TargetSoC does not have a value");
return Status::Failure;
}
if (targetStruct.targetSoC.Value() > 100)
{
ChipLogError(AppServer, "TargetSoC has invalid value (%d)", static_cast<int>(targetStruct.targetSoC.Value()));
return Status::ConstraintError;
}
}
else if (targetStruct.targetSoC.HasValue() && targetStruct.targetSoC.Value() != 100)
{
// If SocReporting is not supported but targetSoc has a value, it must be 100
ChipLogError(AppServer, "TargetSoC has can only be 100%% if SOC feature is not supported");
return Status::ConstraintError;
}
// One or both of targetSoc and addedEnergy must be specified
if (!(targetStruct.targetSoC.HasValue()) && !(targetStruct.addedEnergy.HasValue()))
{
ChipLogError(AppServer, "Must have one of AddedEnergy or TargetSoC");
return Status::Failure;
}
// Validate the value of addedEnergy, if specified is >= 0
if (targetStruct.addedEnergy.HasValue() && targetStruct.addedEnergy.Value() < 0)
{
ChipLogError(AppServer, "AddedEnergy has invalid value (%ld)",
static_cast<signed long int>(targetStruct.addedEnergy.Value()));
return Status::ConstraintError;
}
innerIdx++;
}
if (iterInner.GetStatus() != CHIP_NO_ERROR)
{
return Status::InvalidCommand;
}
}
if (iter.GetStatus() != CHIP_NO_ERROR)
{
return Status::InvalidCommand;
}
return Status::Success;
}
void Instance::HandleGetTargets(HandlerContext & ctx, const Commands::GetTargets::DecodableType & commandData)
{
Commands::GetTargetsResponse::Type response;
Status status = mDelegate.GetTargets(response.chargingTargetSchedules);
if (status != Status::Success)
{
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
return;
}
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, Protocols::InteractionModel::Status::Success);
}
void Instance::HandleClearTargets(HandlerContext & ctx, const Commands::ClearTargets::DecodableType & commandData)
{
// Call the delegate
Status status = mDelegate.ClearTargets();
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
} // namespace EnergyEvse
} // namespace Clusters
} // namespace app
} // namespace chip
// -----------------------------------------------------------------------------
// Plugin initialization
void MatterEnergyEvsePluginServerInitCallback() {}