Skip to content

Commit 5d530ea

Browse files
committed
Start addressing comments from Andrei
1 parent 0a83d17 commit 5d530ea

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

examples/energy-management-app/energy-management-common/include/ChargingTargetsMemMgr.h

+15-15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
* limitations under the License.
1717
*/
1818

19+
#pragma once
20+
21+
#include <app/clusters/energy-evse-server/energy-evse-server.h>
22+
#include <lib/core/CHIPError.h>
23+
24+
#include <app-common/zap-generated/cluster-objects.h>
25+
26+
namespace chip {
27+
namespace app {
28+
namespace Clusters {
29+
namespace EnergyEvse {
30+
1931
/*
2032
* The full Target data structure defined as:
2133
*
@@ -67,28 +79,16 @@
6779
*
6880
*/
6981

70-
#pragma once
71-
72-
#include <app/clusters/energy-evse-server/energy-evse-server.h>
73-
#include <lib/core/CHIPError.h>
74-
75-
#include <app-common/zap-generated/cluster-objects.h>
76-
77-
namespace chip {
78-
namespace app {
79-
namespace Clusters {
80-
namespace EnergyEvse {
81-
8282
class ChargingTargetsMemMgr
8383
{
8484
public:
8585
ChargingTargetsMemMgr();
8686
~ChargingTargetsMemMgr();
8787

8888
void Reset(uint16_t chargingTargetSchedulesIdx);
89-
void AddChargingTarget(EnergyEvse::Structs::ChargingTargetStruct::Type & chargingTarget);
90-
void AllocAndCopy();
91-
void AllocAndCopy(const DataModel::List<const Structs::ChargingTargetStruct::Type> & chargingTargets);
89+
void AddChargingTarget(const EnergyEvse::Structs::ChargingTargetStruct::Type & chargingTarget);
90+
CHIP_ERROR AllocAndCopy();
91+
CHIP_ERROR AllocAndCopy(const DataModel::List<const Structs::ChargingTargetStruct::Type> & chargingTargets);
9292
CHIP_ERROR AllocAndCopy(const DataModel::DecodableList<Structs::ChargingTargetStruct::DecodableType> & chargingTargets);
9393

9494
EnergyEvse::Structs::ChargingTargetStruct::Type * GetChargingTargets() const;

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CHIP_ERROR GetEpochTS(uint32_t & chipEpoch);
3939
*
4040
* @param reference to hold the day of week as a bitmap
4141
*
42-
* Sunday = 0x01, Monday = 0x01 ... Saturday = 0x40 (1<<6)
42+
* Sunday = 0x00, Monday = 0x01 ... Saturday = 0x40 (1<<6)
4343
*/
4444
CHIP_ERROR GetDayOfWeekNow(uint8_t & dayOfWeekMap);
4545

@@ -57,6 +57,12 @@ CHIP_ERROR GetDayOfWeekNow(uint8_t & dayOfWeekMap);
5757
*/
5858
uint8_t GetDayOfWeekUnixEpoch(time_t unixEpoch);
5959

60+
/**
61+
* @brief Helper function to get current timestamp and work out the current number of minutes
62+
* past midnight based on localtime
63+
*
64+
* @param reference to hold the number of minutes past midnight
65+
*/
6066
CHIP_ERROR GetMinutesPastMidnight(uint16_t & minutesPastMidnight);
6167

6268
namespace chip {
@@ -309,11 +315,11 @@ class EnergyEvseDelegate : public EnergyEvse::Delegate
309315

310316
private:
311317
/* Constants */
312-
static constexpr int kDefaultMinChargeCurrent = 6000; /* 6A */
313-
static constexpr int kDefaultUserMaximumChargeCurrent = 80000; /* 80A */
314-
static constexpr int kDefaultRandomizationDelayWindow = 600; /* 600s */
318+
static constexpr int kDefaultMinChargeCurrent_mA = 6000; /* 6A */
319+
static constexpr int kDefaultUserMaximumChargeCurrent_mA = 80000; /* 80A */
320+
static constexpr int kDefaultRandomizationDelayWindow_sec = 600; /* 600s */
315321
static constexpr int kMaxVehicleIDBufSize = 32;
316-
static constexpr int kPeriodicCheckIntervalRealTimeClockNotSynced = 30;
322+
static constexpr int kPeriodicCheckIntervalRealTimeClockNotSynced_sec = 30;
317323

318324
/* private variables for controlling the hardware - these are not attributes */
319325
int64_t mMaxHardwareCurrentLimit = 0; /* Hardware current limit in mA */
@@ -366,11 +372,11 @@ class EnergyEvseDelegate : public EnergyEvse::Delegate
366372
DataModel::Nullable<uint32_t> mChargingEnabledUntil; // TODO Default to 0 to indicate disabled
367373
DataModel::Nullable<uint32_t> mDischargingEnabledUntil; // TODO Default to 0 to indicate disabled
368374
int64_t mCircuitCapacity = 0;
369-
int64_t mMinimumChargeCurrent = kDefaultMinChargeCurrent;
375+
int64_t mMinimumChargeCurrent = kDefaultMinChargeCurrent_mA;
370376
int64_t mMaximumChargeCurrent = 0;
371377
int64_t mMaximumDischargeCurrent = 0;
372-
int64_t mUserMaximumChargeCurrent = kDefaultUserMaximumChargeCurrent; // TODO update spec
373-
uint32_t mRandomizationDelayWindow = kDefaultRandomizationDelayWindow;
378+
int64_t mUserMaximumChargeCurrent = kDefaultUserMaximumChargeCurrent_mA; // TODO update spec
379+
uint32_t mRandomizationDelayWindow = kDefaultRandomizationDelayWindow_sec;
374380
/* PREF attributes */
375381
DataModel::Nullable<uint32_t> mNextChargeStartTime;
376382
DataModel::Nullable<uint32_t> mNextChargeTargetTime;

examples/energy-management-app/energy-management-common/src/ChargingTargetsMemMgr.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void ChargingTargetsMemMgr::Reset(uint16_t chargingTargetSchedulesIdx)
5757
}
5858
}
5959

60-
void ChargingTargetsMemMgr::AddChargingTarget(EnergyEvse::Structs::ChargingTargetStruct::Type & chargingTarget)
60+
void ChargingTargetsMemMgr::AddChargingTarget(const EnergyEvse::Structs::ChargingTargetStruct::Type & chargingTarget)
6161
{
6262
if (mNumDailyChargingTargets < kEvseTargetsMaxTargetsPerDay)
6363
{
@@ -75,14 +75,19 @@ uint16_t ChargingTargetsMemMgr::GetNumDailyChargingTargets() const
7575
return mNumDailyChargingTargets;
7676
}
7777

78-
void ChargingTargetsMemMgr::AllocAndCopy()
78+
CHIP_ERROR ChargingTargetsMemMgr::AllocAndCopy()
7979
{
8080
if (mNumDailyChargingTargets > 0)
8181
{
8282
// Allocate the memory first and then use placement new to initialise the memory of each element in the array
8383
mpListOfDays[mChargingTargetSchedulesIdx] = static_cast<EnergyEvse::Structs::ChargingTargetStruct::Type *>(
8484
chip::Platform::MemoryAlloc(sizeof(EnergyEvse::Structs::ChargingTargetStruct::Type) * mNumDailyChargingTargets));
8585

86+
if (mpListOfDays[mChargingTargetSchedulesIdx] == nullptr)
87+
{
88+
return CHIP_ERROR_NO_MEMORY;
89+
}
90+
8691
for (uint16_t idx = 0; idx < mNumDailyChargingTargets; idx++)
8792
{
8893
// This will cause the ChargingTargetStruct constructor to be called and this element in the array
@@ -92,9 +97,12 @@ void ChargingTargetsMemMgr::AllocAndCopy()
9297
mpListOfDays[mChargingTargetSchedulesIdx][idx] = mDailyChargingTargets[idx];
9398
}
9499
}
100+
101+
return CHIP_NO_ERROR;
95102
}
96103

97-
void ChargingTargetsMemMgr::AllocAndCopy(const DataModel::List<const Structs::ChargingTargetStruct::Type> & chargingTargets)
104+
CHIP_ERROR
105+
ChargingTargetsMemMgr::AllocAndCopy(const DataModel::List<const Structs::ChargingTargetStruct::Type> & chargingTargets)
98106
{
99107
mNumDailyChargingTargets = static_cast<uint16_t>(chargingTargets.size());
100108

@@ -104,6 +112,11 @@ void ChargingTargetsMemMgr::AllocAndCopy(const DataModel::List<const Structs::Ch
104112
mpListOfDays[mChargingTargetSchedulesIdx] = static_cast<EnergyEvse::Structs::ChargingTargetStruct::Type *>(
105113
chip::Platform::MemoryAlloc(sizeof(EnergyEvse::Structs::ChargingTargetStruct::Type) * chargingTargets.size()));
106114

115+
if (mpListOfDays[mChargingTargetSchedulesIdx] == nullptr)
116+
{
117+
return CHIP_ERROR_NO_MEMORY;
118+
}
119+
107120
uint16_t idx = 0;
108121
for (auto & chargingTarget : chargingTargets)
109122
{
@@ -116,6 +129,8 @@ void ChargingTargetsMemMgr::AllocAndCopy(const DataModel::List<const Structs::Ch
116129
idx++;
117130
}
118131
}
132+
133+
return CHIP_NO_ERROR;
119134
}
120135

121136
CHIP_ERROR

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Status EnergyEvseDelegate::ScheduleCheckOnEnabledTimeout()
200200
else if (err == CHIP_ERROR_REAL_TIME_NOT_SYNCED)
201201
{
202202
/* Real time isn't sync'd -lets check again in 30 seconds - otherwise keep the charger enabled */
203-
DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(kPeriodicCheckIntervalRealTimeClockNotSynced),
203+
DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(kPeriodicCheckIntervalRealTimeClockNotSynced_sec),
204204
EvseCheckTimerExpiry, this);
205205
}
206206
return Status::Success;

examples/energy-management-app/energy-management-common/src/EnergyEvseTargetsStore.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ CHIP_ERROR EvseTargetsDelegate::LoadTargets()
176176

177177
// Allocate an array for the chargingTargets loaded for this schedule and copy the chargingTargets into that array.
178178
// The allocated array will be pointed to in the List below.
179-
mChargingTargets.AllocAndCopy();
179+
err = mChargingTargets.AllocAndCopy();
180+
if (err != CHIP_NO_ERROR)
181+
{
182+
ChipLogError(AppServer, "SetTargets: Failed to allocate memory during LoadTargets %s", chip::ErrorStr(err));
183+
return err;
184+
}
180185

181186
// Construct the List<ChargingTargetStruct>. mChargingTargetSchedulesArray will be pointed to in the
182187
// List<ChargingTargetScheduleStruct> mChargingTargetSchedulesList below
@@ -304,7 +309,12 @@ CHIP_ERROR EvseTargetsDelegate::SetTargets(
304309
updatedBitmask = BitMask<TargetDayOfWeekBitmap>(bitmaskB);
305310

306311
// Copy the existing chargingTargets
307-
updatedChargingTargets.AllocAndCopy(currentChargingTargetSchedule.chargingTargets);
312+
CHIP_ERROR err = updatedChargingTargets.AllocAndCopy(currentChargingTargetSchedule.chargingTargets);
313+
if (err != CHIP_NO_ERROR)
314+
{
315+
ChipLogError(AppServer, "SetTargets: Failed to copy the new chargingTargets %s", chip::ErrorStr(err));
316+
return err;
317+
}
308318
}
309319

310320
// Update the new schedule with the dayOfWeekForSequence and list of chargingTargets

0 commit comments

Comments
 (0)