forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootService.cpp
More file actions
265 lines (214 loc) · 8.96 KB
/
Copy pathBootService.cpp
File metadata and controls
265 lines (214 loc) · 8.96 KB
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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <limits>
#include <MicroOcpp/Model/Boot/BootService.h>
#include <MicroOcpp/Core/Context.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Core/Configuration.h>
#include <MicroOcpp/Core/Request.h>
#include <MicroOcpp/Core/FilesystemUtils.h>
#include <MicroOcpp/Operations/BootNotification.h>
#include <MicroOcpp/Platform.h>
#include <MicroOcpp/Debug.h>
using namespace MicroOcpp;
unsigned int PreBootQueue::getFrontRequestOpNr() {
if (!activatedPostBootCommunication) {
return 0;
}
return VolatileRequestQueue::getFrontRequestOpNr();
}
void PreBootQueue::activatePostBootCommunication() {
activatedPostBootCommunication = true;
}
RegistrationStatus MicroOcpp::deserializeRegistrationStatus(const char *serialized) {
if (!strcmp(serialized, "Accepted")) {
return RegistrationStatus::Accepted;
} else if (!strcmp(serialized, "Pending")) {
return RegistrationStatus::Pending;
} else if (!strcmp(serialized, "Rejected")) {
return RegistrationStatus::Rejected;
} else {
MO_DBG_ERR("deserialization error");
return RegistrationStatus::UNDEFINED;
}
}
BootService::BootService(Context& context, std::shared_ptr<FilesystemAdapter> filesystem) : MemoryManaged("v16.Boot.BootService"), context(context), filesystem(filesystem), cpCredentials{makeString(getMemoryTag())} {
context.getRequestQueue().setPreBootSendQueue(&preBootQueue); //register PreBootQueue in RequestQueue module
//if transactions can start before the BootNotification succeeds
preBootTransactionsBool = declareConfiguration<bool>(MO_CONFIG_EXT_PREFIX "PreBootTransactions", false);
if (!preBootTransactionsBool) {
MO_DBG_ERR("initialization error");
}
//Register message handler for TriggerMessage operation
context.getOperationRegistry().registerOperation("BootNotification", [this] () {
return new Ocpp16::BootNotification(this->context.getModel(), getChargePointCredentials());});
}
void BootService::loop() {
if (!executedFirstTime) {
executedFirstTime = true;
firstExecutionTimestamp = mocpp_tick_ms();
}
if (!executedLongTime && mocpp_tick_ms() - firstExecutionTimestamp >= MO_BOOTSTATS_LONGTIME_MS) {
executedLongTime = true;
MO_DBG_DEBUG("boot success timer reached");
configuration_clean_unused();
BootStats bootstats;
loadBootStats(filesystem, bootstats);
bootstats.lastBootSuccess = bootstats.bootNr;
storeBootStats(filesystem, bootstats);
}
preBootQueue.loop();
if (!activatedPostBootCommunication && status == RegistrationStatus::Accepted) {
preBootQueue.activatePostBootCommunication();
activatedPostBootCommunication = true;
}
if (!activatedModel && (status == RegistrationStatus::Accepted || preBootTransactionsBool->getBool())) {
context.getModel().activateTasks();
activatedModel = true;
}
if (status == RegistrationStatus::Accepted) {
return;
}
if (mocpp_tick_ms() - lastBootNotification < (interval_s * 1000UL)) {
return;
}
/*
* Create BootNotification. The BootNotifaction object will fetch its paremeters from
* this class and notify this class about the response
*/
auto bootNotification = makeRequest(new Ocpp16::BootNotification(context.getModel(), getChargePointCredentials()));
bootNotification->setTimeout(interval_s * 1000UL);
context.getRequestQueue().sendRequestPreBoot(std::move(bootNotification));
lastBootNotification = mocpp_tick_ms();
}
void BootService::setChargePointCredentials(JsonObject credentials) {
auto written = serializeJson(credentials, cpCredentials);
if (written < 2) {
MO_DBG_ERR("serialization error");
cpCredentials = "{}";
}
}
void BootService::setChargePointCredentials(const char *credentials) {
cpCredentials = credentials;
if (cpCredentials.size() < 2) {
cpCredentials = "{}";
}
}
std::unique_ptr<JsonDoc> BootService::getChargePointCredentials() {
if (cpCredentials.size() <= 2) {
return createEmptyDocument();
}
std::unique_ptr<JsonDoc> doc;
size_t capacity = JSON_OBJECT_SIZE(9) + cpCredentials.size();
DeserializationError err = DeserializationError::NoMemory;
while (err == DeserializationError::NoMemory && capacity <= MO_MAX_JSON_CAPACITY) {
doc = makeJsonDoc(getMemoryTag(), capacity);
err = deserializeJson(*doc, cpCredentials);
capacity *= 2;
}
if (!err) {
return doc;
} else {
MO_DBG_ERR("could not parse stored credentials: %s", err.c_str());
return nullptr;
}
}
void BootService::notifyRegistrationStatus(RegistrationStatus status) {
this->status = status;
lastBootNotification = mocpp_tick_ms();
}
void BootService::setRetryInterval(unsigned long interval_s) {
if (interval_s == 0) {
this->interval_s = MO_BOOT_INTERVAL_DEFAULT;
} else {
this->interval_s = interval_s;
}
lastBootNotification = mocpp_tick_ms();
}
bool BootService::loadBootStats(std::shared_ptr<FilesystemAdapter> filesystem, BootStats& bstats) {
if (!filesystem) {
return false;
}
size_t msize = 0;
if (filesystem->stat(MO_FILENAME_PREFIX "bootstats.jsn", &msize) == 0) {
bool success = true;
auto json = FilesystemUtils::loadJson(filesystem, MO_FILENAME_PREFIX "bootstats.jsn", "v16.Boot.BootService");
if (json) {
int bootNrIn = (*json)["bootNr"] | -1;
if (bootNrIn >= 0 && bootNrIn <= std::numeric_limits<uint16_t>::max()) {
bstats.bootNr = (uint16_t) bootNrIn;
} else {
success = false;
}
int lastSuccessIn = (*json)["lastSuccess"] | -1;
if (lastSuccessIn >= 0 && lastSuccessIn <= std::numeric_limits<uint16_t>::max()) {
bstats.lastBootSuccess = (uint16_t) lastSuccessIn;
} else {
success = false;
}
const char *microOcppVersionIn = (*json)["MicroOcppVersion"] | (const char*)nullptr;
if (microOcppVersionIn) {
auto ret = snprintf(bstats.microOcppVersion, sizeof(bstats.microOcppVersion), "%s", microOcppVersionIn);
if (ret < 0 || (size_t)ret >= sizeof(bstats.microOcppVersion)) {
success = false;
}
} //else: version specifier can be missing after upgrade from pre 1.2.0 version
} else {
success = false;
}
if (!success) {
MO_DBG_ERR("bootstats corrupted");
filesystem->remove(MO_FILENAME_PREFIX "bootstats.jsn");
bstats = BootStats();
}
return success;
} else {
return false;
}
}
bool BootService::storeBootStats(std::shared_ptr<FilesystemAdapter> filesystem, BootStats& bstats) {
if (!filesystem) {
return false;
}
auto json = initJsonDoc("v16.Boot.BootService", JSON_OBJECT_SIZE(3));
json["bootNr"] = bstats.bootNr;
json["lastSuccess"] = bstats.lastBootSuccess;
json["MicroOcppVersion"] = (const char*)bstats.microOcppVersion;
return FilesystemUtils::storeJson(filesystem, MO_FILENAME_PREFIX "bootstats.jsn", json);
}
bool BootService::recover(std::shared_ptr<FilesystemAdapter> filesystem, BootStats& bstats) {
if (!filesystem) {
return false;
}
bool success = FilesystemUtils::remove_if(filesystem, [] (const char *fname) -> bool {
return !strncmp(fname, "sd", strlen("sd")) ||
!strncmp(fname, "tx", strlen("tx")) ||
!strncmp(fname, "sc-", strlen("sc-")) ||
!strncmp(fname, "reservation", strlen("reservation")) ||
!strncmp(fname, "client-state", strlen("client-state"));
});
MO_DBG_ERR("clear local state files (recovery): %s", success ? "success" : "not completed");
return success;
}
bool BootService::migrate(std::shared_ptr<FilesystemAdapter> filesystem, BootStats& bstats) {
if (!filesystem) {
return false;
}
bool success = true;
if (strcmp(bstats.microOcppVersion, MO_VERSION)) {
MO_DBG_INFO("migrate persistent storage to MO v" MO_VERSION);
success = FilesystemUtils::remove_if(filesystem, [] (const char *fname) -> bool {
return !strncmp(fname, "sd", strlen("sd")) ||
!strncmp(fname, "tx", strlen("tx")) ||
!strncmp(fname, "op", strlen("op")) ||
!strncmp(fname, "sc-", strlen("sc-")) ||
!strcmp(fname, "client-state.cnf") ||
!strcmp(fname, "arduino-ocpp.cnf") ||
!strcmp(fname, "ocpp-creds.jsn");
});
snprintf(bstats.microOcppVersion, sizeof(bstats.microOcppVersion), "%s", MO_VERSION);
MO_DBG_DEBUG("clear local state files (migration): %s", success ? "success" : "not completed");
}
return success;
}