Skip to content

Commit 0526495

Browse files
authored
Merge pull request #29 from matth-x/feature/v201-update
Integrate new MO features
2 parents 3a58611 + 449a0bc commit 0526495

File tree

5 files changed

+85
-52
lines changed

5 files changed

+85
-52
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_compile_definitions(
3535
MO_FILENAME_PREFIX="./mo_store/"
3636
MO_ENABLE_V201=1
3737
MO_ENABLE_MBEDTLS=1
38+
MO_ENABLE_TIMESTAMP_MILLISECONDS=1
3839
)
3940

4041
add_executable(mo_simulator ${MO_SIM_SRC} ${MO_SIM_MG_SRC})

lib/MicroOcpp

Submodule MicroOcpp updated 236 files

src/evse.cpp

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ Evse::Evse(unsigned int connectorId) : connectorId{connectorId} {
1919

2020
}
2121

22-
MicroOcpp::Connector *getConnector(unsigned int connectorId) {
23-
if (!getOcppContext()) {
24-
MO_DBG_ERR("unitialized");
25-
return nullptr;
26-
}
27-
return getOcppContext()->getModel().getConnector(connectorId);
28-
}
29-
3022
void Evse::setup() {
3123

3224
#if MO_ENABLE_V201
@@ -43,12 +35,6 @@ void Evse::setup() {
4335
}
4436
#endif
4537

46-
auto connector = getConnector(connectorId);
47-
if (!connector) {
48-
MO_DBG_ERR("invalid state");
49-
return;
50-
}
51-
5238
char key [30] = {'\0'};
5339

5440
snprintf(key, 30, "evPlugged_cId_%u", connectorId);
@@ -134,12 +120,11 @@ void Evse::setup() {
134120
}
135121

136122
void Evse::loop() {
137-
if (auto connector = getConnector(connectorId)) {
138-
auto curStatus = connector->getStatus();
139123

140-
if (status.compare(MicroOcpp::cstrFromOcppEveState(curStatus))) {
141-
status = MicroOcpp::cstrFromOcppEveState(curStatus);
142-
}
124+
auto curStatus = getChargePointStatus(connectorId);
125+
126+
if (status.compare(MicroOcpp::cstrFromOcppEveState(curStatus))) {
127+
status = MicroOcpp::cstrFromOcppEveState(curStatus);
143128
}
144129

145130
bool simulate_isCharging = ocppPermitsCharge(connectorId) && trackEvPluggedBool->getBool() && trackEvsePluggedBool->getBool() && trackEvReadyBool->getBool() && trackEvseReadyBool->getBool();
@@ -167,21 +152,16 @@ void Evse::presentNfcTag(const char *uid_cstr) {
167152
return;
168153
}
169154
std::string uid = uid_cstr;
170-
auto connector = getConnector(connectorId);
171-
if (!connector) {
172-
MO_DBG_ERR("invalid state");
173-
return;
174-
}
175155

176156
#if MO_ENABLE_V201
177157
if (auto context = getOcppContext()) {
178158
if (context->getVersion().major == 2) {
179159
if (auto txService = context->getModel().getTransactionService()) {
180160
if (auto evse = txService->getEvse(connectorId)) {
181-
if (evse->getTransaction() && evse->getTransaction()->isAuthorized) {
182-
evse->endAuthorization(uid_cstr);
161+
if (evse->getTransaction() && evse->getTransaction()->isAuthorizationActive) {
162+
evse->endAuthorization(MicroOcpp::IdToken(uid_cstr, MicroOcpp::IdToken::Type::KeyCode));
183163
} else {
184-
evse->beginAuthorization(uid_cstr);
164+
evse->beginAuthorization(MicroOcpp::IdToken(uid_cstr, MicroOcpp::IdToken::Type::KeyCode));
185165
}
186166
return;
187167
}
@@ -190,14 +170,14 @@ void Evse::presentNfcTag(const char *uid_cstr) {
190170
}
191171
#endif
192172

193-
if (connector->getTransaction() && connector->getTransaction()->isActive()) {
194-
if (!uid.compare(connector->getTransaction()->getIdTag())) {
195-
connector->endTransaction(uid.c_str());
173+
if (isTransactionActive(connectorId)) {
174+
if (!uid.compare(getTransactionIdTag(connectorId))) {
175+
endTransaction(uid.c_str(), "Local", connectorId);
196176
} else {
197177
MO_DBG_INFO("RFID card denied");
198178
}
199179
} else {
200-
connector->beginTransaction(uid.c_str());
180+
beginTransaction(uid.c_str(), connectorId);
201181
}
202182
}
203183

@@ -246,30 +226,15 @@ bool Evse::getEvseReady() {
246226
}
247227

248228
const char *Evse::getSessionIdTag() {
249-
auto connector = getConnector(connectorId);
250-
if (!connector) {
251-
MO_DBG_ERR("invalid state");
252-
return nullptr;
253-
}
254-
return connector->getTransaction() ? connector->getTransaction()->getIdTag() : nullptr;
229+
return getTransactionIdTag(connectorId) ? getTransactionIdTag(connectorId) : "";
255230
}
256231

257232
int Evse::getTransactionId() {
258-
auto connector = getConnector(connectorId);
259-
if (!connector) {
260-
MO_DBG_ERR("invalid state");
261-
return -1;
262-
}
263-
return connector->getTransaction() ? connector->getTransaction()->getTransactionId() : -1;
233+
return getTransaction(connectorId) ? getTransaction(connectorId)->getTransactionId() : -1;
264234
}
265235

266236
bool Evse::chargingPermitted() {
267-
auto connector = getConnector(connectorId);
268-
if (!connector) {
269-
MO_DBG_ERR("invalid state");
270-
return false;
271-
}
272-
return connector->ocppPermitsCharge();
237+
return ocppPermitsCharge(connectorId);
273238
}
274239

275240
int Evse::getPower() {

src/main.cpp

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33
// GPL-3.0 License
44

55
#include <iostream>
6+
#include <signal.h>
7+
8+
#include <mbedtls/platform.h>
69

710
#include <MicroOcpp.h>
11+
#include <MicroOcpp/Core/Context.h>
812
#include "evse.h"
913
#include "api.h"
1014

15+
#include <MicroOcpp/Core/Memory.h>
16+
1117
#if MO_NUMCONNECTORS == 3
1218
std::array<Evse, MO_NUMCONNECTORS - 1> connectors {{1,2}};
1319
#else
1420
std::array<Evse, MO_NUMCONNECTORS - 1> connectors {{1}};
1521
#endif
1622

1723
bool g_isOcpp201 = false;
24+
bool g_runSimulator = true;
25+
26+
bool g_isUpAndRunning = false; //if the initial BootNotification and StatusNotifications got through + 1s delay
27+
unsigned int g_bootNotificationTime = 0;
1828

1929
#define MO_NETLIB_MONGOOSE 1
2030
#define MO_NETLIB_WASM 2
@@ -42,6 +52,31 @@ MicroOcpp::Connection *conn = nullptr;
4252
#error Please ensure that build flag MO_NETLIB is set as MO_NETLIB_MONGOOSE or MO_NETLIB_WASM
4353
#endif
4454

55+
#if MBEDTLS_PLATFORM_MEMORY //configure MbedTLS with allocation hook functions
56+
57+
void *mo_mem_mbedtls_calloc( size_t n, size_t count ) {
58+
size_t size = n * count;
59+
auto ptr = MO_MALLOC("MbedTLS", size);
60+
if (ptr) {
61+
memset(ptr, 0, size);
62+
}
63+
return ptr;
64+
}
65+
void mo_mem_mbedtls_free( void *ptr ) {
66+
MO_FREE(ptr);
67+
}
68+
69+
#endif //MBEDTLS_PLATFORM_MEMORY
70+
71+
void mo_sim_sig_handler(int s){
72+
73+
if (!g_runSimulator) { //already tried to shut down, now force stop
74+
exit(EXIT_FAILURE);
75+
}
76+
77+
g_runSimulator = false; //shut down simulator gracefully
78+
}
79+
4580
/*
4681
* Setup MicroOcpp and API
4782
*/
@@ -94,6 +129,17 @@ void app_loop() {
94129
#if MO_NETLIB == MO_NETLIB_MONGOOSE
95130

96131
int main() {
132+
133+
#if MBEDTLS_PLATFORM_MEMORY
134+
mbedtls_platform_set_calloc_free(mo_mem_mbedtls_calloc, mo_mem_mbedtls_free);
135+
#endif //MBEDTLS_PLATFORM_MEMORY
136+
137+
struct sigaction sigIntHandler;
138+
sigIntHandler.sa_handler = mo_sim_sig_handler;
139+
sigemptyset(&sigIntHandler.sa_mask);
140+
sigIntHandler.sa_flags = 0;
141+
sigaction(SIGINT, &sigIntHandler, NULL);
142+
97143
mg_log_set(MG_LL_INFO);
98144
mg_mgr_init(&mgr);
99145

@@ -117,11 +163,32 @@ int main() {
117163
server_initialize(osock);
118164
app_setup(*osock, filesystem);
119165

120-
for (;;) { // Block forever
166+
setOnResetExecute([] (bool isHard) {
167+
g_runSimulator = false;
168+
});
169+
170+
while (g_runSimulator) { //Run Simulator until OCPP Reset is executed or user presses Ctrl+C
121171
mg_mgr_poll(&mgr, 100);
122172
app_loop();
173+
174+
if (!g_bootNotificationTime && getOcppContext()->getModel().getClock().now() >= MicroOcpp::MIN_TIME) {
175+
//time has been set, BootNotification succeeded
176+
g_bootNotificationTime = mocpp_tick_ms();
177+
}
178+
179+
if (!g_isUpAndRunning && g_bootNotificationTime && mocpp_tick_ms() - g_bootNotificationTime >= 1000) {
180+
printf("[Sim] Resetting maximum heap usage after boot success\n");
181+
g_isUpAndRunning = true;
182+
MO_MEM_RESET();
183+
}
123184
}
124185

186+
printf("[Sim] Shutting down Simulator\n");
187+
188+
MO_MEM_PRINT_STATS();
189+
190+
mocpp_deinitialize();
191+
125192
delete osock;
126193
mg_mgr_free(&mgr);
127194
return 0;

0 commit comments

Comments
 (0)