forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChargingTargetsMemMgr.h
107 lines (97 loc) · 4.46 KB
/
ChargingTargetsMemMgr.h
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
/*
*
* 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.
*/
/*
* The full Target data structure defined as:
*
* DataModel::List<const Structs::ChargingTargetScheduleStruct::Type> chargingTargetSchedules;
*
* contains a list of ChargingTargetScheduleStructs which in turn contains a list of ChargingTargetStructs.
* This means that somewhere the following memory needs to be allocated in the case where
* the Target is at its maximum size:
*
* ChargingTargetStruct::Type mChargingTargets[kEvseTargetsMaxNumberOfDays][kEvseTargetsMaxTargetsPerDay]
*
* This is 1680B.
*
* However it is likely the number of chargingTargets configured will be considerably less. To avoid
* allocating the maximum possible Target size, each List<ChargingTargetScheduleStructs> is allocated
* separately. This class handles that allocation.
*
* When iterating through the chargingTargetSchedules, an index in this list is kept and the
* ChargingTargetsMemMgr::Reset must be called so this object knows which day schedule it is tracking.
* This will free any previous memory allocated for the day schedule in this object.
*
* There are then three usage cases:
*
* 1. When loading the Target from persistent storage. In this scenario, it is not known upfront
* how many chargingTargets are associated with this day schedule so ChargingTargetsMemMgr::AddChargingTarget()
* needs to be called as each individual chargingTarget is loaded from persistent data.
*
* Once the chargingTargets for the day schedule have been loaded, ChargingTargetsMemMgr::AllocAndCopy() is
* called to allocate the memory to store the chargingTargets and the chargingTargets are copied.
*
* 2. When updating a Target and a day schedule is unaffected, the chargingTargets associated with
* day schedule need copying. The following should be called:
*
* ChargingTargetsMemMgr::AllocAndCopy(const DataModel::List<const Structs::ChargingTargetStruct::Type> & chargingTargets)
*
* 3. When in SetTargets, a new list of chargingTargets needs to be added to a day schedule, the following
* should be called:
*
* ChargingTargetsMemMgr::AllocAndCopy(const DataModel::DecodableList<Structs::ChargingTargetStruct::DecodableType> &
* chargingTargets)
*
* Having allocated and copied the chargingTargets accordingly, they can be added to a
* DataModel::List<const Structs::ChargingTargetStruct::Type as follows:
*
* chargingTargetsList = DataModel::List<Structs::ChargingTargetStruct::Type>(ChargingTargetsMemMgr::GetChargingTargets(),
* ChargingTargetsMemMgr::GetNumChargingTargets());
*
* All memory allocated by this object is released When the ChargingTargetsMemMgr destructor is called.
*
*/
#pragma once
#include <app/clusters/energy-evse-server/energy-evse-server.h>
#include <lib/core/CHIPError.h>
#include <app-common/zap-generated/cluster-objects.h>
namespace chip {
namespace app {
namespace Clusters {
namespace EnergyEvse {
class ChargingTargetsMemMgr
{
public:
ChargingTargetsMemMgr();
~ChargingTargetsMemMgr();
void Reset(uint16_t chargingTargetSchedulesIdx);
void AddChargingTarget(EnergyEvse::Structs::ChargingTargetStruct::Type & chargingTarget);
void AllocAndCopy();
void AllocAndCopy(const DataModel::List<const Structs::ChargingTargetStruct::Type> & chargingTargets);
CHIP_ERROR AllocAndCopy(const DataModel::DecodableList<Structs::ChargingTargetStruct::DecodableType> & chargingTargets);
EnergyEvse::Structs::ChargingTargetStruct::Type * GetChargingTargets() const;
uint16_t GetNumChargingTargets() const;
private:
EnergyEvse::Structs::ChargingTargetStruct::Type * mpChargingTargets[kEvseTargetsMaxNumberOfDays];
EnergyEvse::Structs::ChargingTargetStruct::Type mChargingTargets[kEvseTargetsMaxTargetsPerDay];
uint16_t mChargingTargetSchedulesIdx;
uint16_t mNumChargingTargets;
};
} // namespace EnergyEvse
} // namespace Clusters
} // namespace app
} // namespace chip