Skip to content

Commit 5bda642

Browse files
committedDec 14, 2023
Sync'd changes from example energy management app, and commits from project-chip#30857 & project-chip#30727
1 parent c92a4e2 commit 5bda642

File tree

5 files changed

+169
-22
lines changed

5 files changed

+169
-22
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
*
3+
* Copyright (c) 2023 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
using namespace chip::app::Clusters::EnergyEvse;
22+
23+
/* This callbacks mechanism is intended to allow different delegates to callback
24+
* and inform the manufacturer that something has changed.
25+
*
26+
* This is not specific to the EnergyEVSE cluster, but includes DeviceEnergyManagement
27+
* and potential future clusters.
28+
*/
29+
enum EVSECallbackType
30+
{
31+
/*
32+
* The State has changed (e.g. from Disabled to Charging, or vice-versa)
33+
*/
34+
StateChanged,
35+
/*
36+
* ChargeCurrent has changed
37+
*/
38+
ChargeCurrentChanged,
39+
/*
40+
* Charging Preferences have changed
41+
*/
42+
ChargingPreferencesChanged,
43+
/*
44+
* DeviceEnergyManagement has changed
45+
*/
46+
DeviceEnergyManagementChanged,
47+
};
48+
49+
struct EVSECbInfo
50+
{
51+
EVSECallbackType type;
52+
53+
union
54+
{
55+
struct
56+
{
57+
StateEnum state;
58+
SupplyStateEnum supplyState;
59+
} StateChange;
60+
61+
struct
62+
{
63+
int64_t maximumChargeCurrent;
64+
} ChargingCurrent;
65+
};
66+
};
67+
68+
typedef void (*EVSECallbackFunc)(const EVSECbInfo * cb, intptr_t arg);
69+
70+
struct EVSECallbackWrapper
71+
{
72+
EVSECallbackFunc handler;
73+
intptr_t arg;
74+
};

‎examples/all-clusters-app/all-clusters-common/include/EnergyEvseDelegateImpl.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#pragma once
2020

2121
#include "app/clusters/energy-evse-server/energy-evse-server.h"
22+
#include <EVSECallbacks.h>
2223

2324
#include <app/util/af.h>
2425
#include <app/util/config.h>
@@ -70,7 +71,7 @@ class EnergyEvseDelegate : public EnergyEvse::Delegate
7071

7172
// -----------------------------------------------------------------
7273
// Internal API to allow an EVSE to change its internal state etc
73-
// TODO Status HwRegisterEvseHardwareCallback(Callback);
74+
Status HwRegisterEvseCallbackHandler(EVSECallbackFunc handler, intptr_t arg);
7475
Status HwSetMaxHardwareCurrentLimit(int64_t currentmA);
7576
Status HwSetCircuitCapacity(int64_t currentmA);
7677
Status HwSetCableAssemblyLimit(int64_t currentmA);
@@ -150,6 +151,11 @@ class EnergyEvseDelegate : public EnergyEvse::Delegate
150151
int64_t mActualChargingCurrentLimit = 0;
151152
StateEnum mHwState = StateEnum::kNotPluggedIn; /* Hardware state */
152153

154+
/* Callback related */
155+
EVSECallbackWrapper mCallbacks = { .handler = nullptr }; /* Wrapper to allow callbacks to be registered */
156+
Status NotifyApplicationCurrentLimitChange(int64_t maximumChargeCurrent);
157+
Status NotifyApplicationStateChange(void);
158+
153159
/**
154160
* @brief Helper function to work out the charge limit based on conditions and settings
155161
*/

‎examples/all-clusters-app/all-clusters-common/src/EnergyEvseDelegateImpl.cpp

+75-21
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Status EnergyEvseDelegate::Disable()
8383
/* update MaximumDischargeCurrent to 0 */
8484
SetMaximumDischargeCurrent(0);
8585

86+
NotifyApplicationStateChange();
8687
// TODO: Generate events
8788

8889
return Status::Success;
@@ -102,31 +103,31 @@ Status EnergyEvseDelegate::EnableCharging(const DataModel::Nullable<uint32_t> &
102103

103104
if (maximumChargeCurrent < kMinimumChargeCurrent || maximumChargeCurrent > kMaximumChargeCurrent)
104105
{
105-
ChipLogError(NotSpecified, "Maximum Current outside limits");
106+
ChipLogError(AppServer, "Maximum Current outside limits");
106107
return Status::ConstraintError;
107108
}
108109

109110
if (minimumChargeCurrent < kMinimumChargeCurrent || minimumChargeCurrent > kMaximumChargeCurrent)
110111
{
111-
ChipLogError(NotSpecified, "Maximum Current outside limits");
112+
ChipLogError(AppServer, "Maximum Current outside limits");
112113
return Status::ConstraintError;
113114
}
114115

115116
if (minimumChargeCurrent > maximumChargeCurrent)
116117
{
117-
ChipLogError(NotSpecified, "Minium Current > Maximum Current!");
118+
ChipLogError(AppServer, "Minium Current > Maximum Current!");
118119
return Status::ConstraintError;
119120
}
120121

121122
if (chargingEnabledUntil.IsNull())
122123
{
123124
/* Charging enabled indefinitely */
124-
ChipLogError(NotSpecified, "Charging enabled indefinitely");
125+
ChipLogError(AppServer, "Charging enabled indefinitely");
125126
}
126127
else
127128
{
128129
/* check chargingEnabledUntil is in the future */
129-
ChipLogError(NotSpecified, "Charging enabled until: %lu", static_cast<long unsigned int>(chargingEnabledUntil.Value()));
130+
ChipLogError(AppServer, "Charging enabled until: %lu", static_cast<long unsigned int>(chargingEnabledUntil.Value()));
130131
// TODO
131132
// if (checkChargingEnabled)
132133
}
@@ -169,6 +170,8 @@ Status EnergyEvseDelegate::EnableCharging(const DataModel::Nullable<uint32_t> &
169170

170171
// TODO: Generate events
171172

173+
NotifyApplicationStateChange();
174+
172175
return this->ComputeMaxChargeCurrentLimit();
173176
}
174177

@@ -188,6 +191,8 @@ Status EnergyEvseDelegate::EnableDischarging(const DataModel::Nullable<uint32_t>
188191

189192
// TODO: Generate events
190193

194+
NotifyApplicationStateChange();
195+
191196
return Status::Success;
192197
}
193198

@@ -199,25 +204,42 @@ Status EnergyEvseDelegate::StartDiagnostics()
199204
/* For EVSE manufacturers to customize */
200205
ChipLogProgress(AppServer, "EnergyEvseDelegate::StartDiagnostics()");
201206

202-
/* update SupplyState */
207+
/* update SupplyState to indicate in Diagnostics mode */
203208
SetSupplyState(SupplyStateEnum::kDisabledDiagnostics);
204209

205210
// TODO: Generate events
206211

212+
// TODO: Notify Application to implement Diagnostics
213+
214+
NotifyApplicationStateChange();
215+
207216
return Status::Success;
208217
}
209218

210219
/* ---------------------------------------------------------------------------
211-
* FUNCTIONS BELOW:
212-
* - EVSE Hardware interface
220+
* EVSE Hardware interface below
221+
*/
222+
223+
/**
224+
* @brief Called by EVSE Hardware to register a callback handler mechanism
213225
*
214-
* SetMaxHardwareCurrentLimit( currentmA )
215-
* SetCircuitCapacity( currentmA )
216-
* SetCableAssemblyLimit( currentmA )
217-
* SetState( EVSEStateEnum )
218-
* SetFault
226+
* This is normally called at start-up.
219227
*
228+
* @param EVSECallbackFunct - function pointer to call
229+
* @param intptr_t - optional context to provide back to callback handler
220230
*/
231+
Status EnergyEvseDelegate::HwRegisterEvseCallbackHandler(EVSECallbackFunc handler, intptr_t arg)
232+
{
233+
if (mCallbacks.handler != nullptr)
234+
{
235+
ChipLogError(AppServer, "Callback handler already initialized");
236+
return Status::Failure;
237+
}
238+
mCallbacks.handler = handler;
239+
mCallbacks.arg = arg;
240+
241+
return Status::Success;
242+
}
221243

222244
/**
223245
* @brief Called by EVSE Hardware to notify the delegate of the maximum
@@ -420,17 +442,18 @@ Status EnergyEvseDelegate::HwSetVehicleID(const CharSpan & newValue)
420442

421443
/**
422444
* @brief Called to compute the safe charging current limit
445+
*
446+
* mActualChargingCurrentLimit is the minimum of:
447+
* - MaxHardwareCurrentLimit (of the hardware)
448+
* - CircuitCapacity (set by the electrician - less than the hardware)
449+
* - CableAssemblyLimit (detected when the cable is inserted)
450+
* - MaximumChargeCurrent (from charging command)
451+
* - UserMaximumChargeCurrent (could dynamically change)
452+
*
423453
*/
424454
Status EnergyEvseDelegate::ComputeMaxChargeCurrentLimit()
425455
{
426456
int64_t oldValue;
427-
/* mActualChargingCurrentLimit is the minimum of:
428-
* - MaxHardwareCurrentLimit (of the hardware)
429-
* - CircuitCapacity (set by the electrician - less than the hardware)
430-
* - CableAssemblyLimit (detected when the cable is inserted)
431-
* - MaximumChargeCurrent (from charging command)
432-
* - UserMaximumChargeCurrent (could dynamically change)
433-
*/
434457

435458
oldValue = mActualChargingCurrentLimit;
436459
mActualChargingCurrentLimit = mMaxHardwareCurrentLimit;
@@ -448,11 +471,42 @@ Status EnergyEvseDelegate::ComputeMaxChargeCurrentLimit()
448471
MatterReportingAttributeChangeCallback(mEndpointId, EnergyEvse::Id, MaximumChargeCurrent::Id);
449472

450473
/* Call the EV Charger hardware current limit callback */
451-
// TODO
474+
NotifyApplicationCurrentLimitChange(mMaximumChargeCurrent);
452475
}
453476
return Status::Success;
454477
}
455478

479+
Status EnergyEvseDelegate::NotifyApplicationCurrentLimitChange(int64_t maximumChargeCurrent)
480+
{
481+
EVSECbInfo cbInfo;
482+
483+
cbInfo.type = EVSECallbackType::ChargeCurrentChanged;
484+
cbInfo.ChargingCurrent.maximumChargeCurrent = maximumChargeCurrent;
485+
486+
if (mCallbacks.handler != nullptr)
487+
{
488+
mCallbacks.handler(&cbInfo, mCallbacks.arg);
489+
}
490+
491+
return Status::Success;
492+
}
493+
494+
Status EnergyEvseDelegate::NotifyApplicationStateChange(void)
495+
{
496+
EVSECbInfo cbInfo;
497+
498+
cbInfo.type = EVSECallbackType::StateChanged;
499+
cbInfo.StateChange.state = mState;
500+
cbInfo.StateChange.supplyState = mSupplyState;
501+
502+
if (mCallbacks.handler != nullptr)
503+
{
504+
mCallbacks.handler(&cbInfo, mCallbacks.arg);
505+
}
506+
507+
return Status::Success;
508+
}
509+
456510
/**
457511
* Attribute methods
458512
*/

‎examples/energy-management-app/energy-management-common/include/EnergyEvseDelegateImpl.h

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ namespace EnergyEvse {
3838
class EnergyEvseDelegate : public EnergyEvse::Delegate
3939
{
4040
public:
41+
~EnergyEvseDelegate();
42+
4143
/**
4244
* @brief Called when EVSE cluster receives Disable command
4345
*/

‎examples/energy-management-app/energy-management-common/src/EnergyEvseDelegateImpl.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ using namespace chip::app::Clusters::EnergyEvse::Attributes;
3131
using chip::app::LogEvent;
3232
using chip::Protocols::InteractionModel::Status;
3333

34+
EnergyEvseDelegate::~EnergyEvseDelegate()
35+
{
36+
// TODO Fix this as part of issue #30993 refactoring
37+
if (!mVehicleID.IsNull())
38+
{
39+
ChipLogDetail(AppServer, "Freeing VehicleID");
40+
delete[] mVehicleID.Value().data();
41+
}
42+
}
43+
3444
/**
3545
* @brief Called when EVSE cluster receives Disable command
3646
*/
@@ -395,6 +405,7 @@ Status EnergyEvseDelegate::HwSetRFID(ByteSpan uid)
395405
*/
396406
Status EnergyEvseDelegate::HwSetVehicleID(const CharSpan & newValue)
397407
{
408+
// TODO this code to be refactored - See Issue #30993
398409
if (!mVehicleID.IsNull() && newValue.data_equal(mVehicleID.Value()))
399410
{
400411
return Status::Success;

0 commit comments

Comments
 (0)