Skip to content

Commit baec7ed

Browse files
committed
integrate new MO features
1 parent 9d119f1 commit baec7ed

File tree

5 files changed

+83
-50
lines changed

5 files changed

+83
-50
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 230 files

src/evse.cpp

Lines changed: 12 additions & 47 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);
@@ -131,12 +117,11 @@ void Evse::setup() {
131117
}
132118

133119
void Evse::loop() {
134-
if (auto connector = getConnector(connectorId)) {
135-
auto curStatus = connector->getStatus();
136120

137-
if (status.compare(MicroOcpp::cstrFromOcppEveState(curStatus))) {
138-
status = MicroOcpp::cstrFromOcppEveState(curStatus);
139-
}
121+
auto curStatus = getChargePointStatus(connectorId);
122+
123+
if (status.compare(MicroOcpp::cstrFromOcppEveState(curStatus))) {
124+
status = MicroOcpp::cstrFromOcppEveState(curStatus);
140125
}
141126

142127
bool simulate_isCharging = ocppPermitsCharge(connectorId) && trackEvPluggedBool->getBool() && trackEvReadyBool->getBool() && trackEvseReadyBool->getBool();
@@ -164,11 +149,6 @@ void Evse::presentNfcTag(const char *uid_cstr) {
164149
return;
165150
}
166151
std::string uid = uid_cstr;
167-
auto connector = getConnector(connectorId);
168-
if (!connector) {
169-
MO_DBG_ERR("invalid state");
170-
return;
171-
}
172152

173153
#if MO_ENABLE_V201
174154
if (auto context = getOcppContext()) {
@@ -178,7 +158,7 @@ void Evse::presentNfcTag(const char *uid_cstr) {
178158
if (evse->getTransaction() && evse->getTransaction()->isAuthorized) {
179159
evse->endAuthorization(uid_cstr);
180160
} else {
181-
evse->beginAuthorization(uid_cstr);
161+
evse->beginAuthorization(MicroOcpp::IdToken(uid_cstr, MicroOcpp::IdToken::Type::KeyCode));
182162
}
183163
return;
184164
}
@@ -187,14 +167,14 @@ void Evse::presentNfcTag(const char *uid_cstr) {
187167
}
188168
#endif
189169

190-
if (connector->getTransaction() && connector->getTransaction()->isActive()) {
191-
if (!uid.compare(connector->getTransaction()->getIdTag())) {
192-
connector->endTransaction(uid.c_str());
170+
if (isTransactionActive(connectorId)) {
171+
if (!uid.compare(getTransactionIdTag(connectorId))) {
172+
endTransaction(uid.c_str(), "Local", connectorId);
193173
} else {
194174
MO_DBG_INFO("RFID card denied");
195175
}
196176
} else {
197-
connector->beginTransaction(uid.c_str());
177+
beginTransaction(uid.c_str(), connectorId);
198178
}
199179
}
200180

@@ -232,30 +212,15 @@ bool Evse::getEvseReady() {
232212
}
233213

234214
const char *Evse::getSessionIdTag() {
235-
auto connector = getConnector(connectorId);
236-
if (!connector) {
237-
MO_DBG_ERR("invalid state");
238-
return nullptr;
239-
}
240-
return connector->getTransaction() ? connector->getTransaction()->getIdTag() : nullptr;
215+
return getTransactionIdTag(connectorId) ? getTransactionIdTag(connectorId) : "";
241216
}
242217

243218
int Evse::getTransactionId() {
244-
auto connector = getConnector(connectorId);
245-
if (!connector) {
246-
MO_DBG_ERR("invalid state");
247-
return -1;
248-
}
249-
return connector->getTransaction() ? connector->getTransaction()->getTransactionId() : -1;
219+
return getTransaction(connectorId) ? getTransaction(connectorId)->getTransactionId() : -1;
250220
}
251221

252222
bool Evse::chargingPermitted() {
253-
auto connector = getConnector(connectorId);
254-
if (!connector) {
255-
MO_DBG_ERR("invalid state");
256-
return false;
257-
}
258-
return connector->ocppPermitsCharge();
223+
return ocppPermitsCharge(connectorId);
259224
}
260225

261226
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)