forked from matth-x/MicroOcppSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
256 lines (193 loc) · 7.68 KB
/
main.cpp
File metadata and controls
256 lines (193 loc) · 7.68 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
// matth-x/MicroOcppSimulator
// Copyright Matthias Akstaller 2022 - 2024
// GPL-3.0 License
//
// ============================================================================
// Fenexity CSMS Platform - Environment Variable Configuration Support
// ============================================================================
#include <iostream>
#include <signal.h>
#include <cstdlib>
#include <mbedtls/platform.h>
#include <MicroOcpp.h>
#include <MicroOcpp/Core/Context.h>
#include <MicroOcpp/Core/FilesystemUtils.h>
#include "evse.h"
#include "api.h"
#include <MicroOcpp/Core/Memory.h>
// ============================================================================
// Fenexity Environment Variable Helper Functions
// ============================================================================
const char* getEnvOrDefault(const char* name, const char* defaultValue) {
const char* value = getenv(name);
return value ? value : defaultValue;
}
#if MO_NUMCONNECTORS == 3
std::array<Evse, MO_NUMCONNECTORS - 1> connectors {{1,2}};
#else
std::array<Evse, MO_NUMCONNECTORS - 1> connectors {{1}};
#endif
bool g_isOcpp201 = false;
bool g_runSimulator = true;
bool g_isUpAndRunning = false; //if the initial BootNotification and StatusNotifications got through + 1s delay
unsigned int g_bootNotificationTime = 0;
#define MO_NETLIB_MONGOOSE 1
#define MO_NETLIB_WASM 2
#if MO_NETLIB == MO_NETLIB_MONGOOSE
#include "mongoose.h"
#include <MicroOcppMongooseClient.h>
#include "net_mongoose.h"
struct mg_mgr mgr;
MicroOcpp::MOcppMongooseClient *osock;
#elif MO_NETLIB == MO_NETLIB_WASM
#include <emscripten.h>
#include <MicroOcpp/Core/Connection.h>
#include "net_wasm.h"
MicroOcpp::Connection *conn = nullptr;
#else
#error Please ensure that build flag MO_NETLIB is set as MO_NETLIB_MONGOOSE or MO_NETLIB_WASM
#endif
#if MBEDTLS_PLATFORM_MEMORY //configure MbedTLS with allocation hook functions
void *mo_mem_mbedtls_calloc( size_t n, size_t count ) {
size_t size = n * count;
auto ptr = MO_MALLOC("MbedTLS", size);
if (ptr) {
memset(ptr, 0, size);
}
return ptr;
}
void mo_mem_mbedtls_free( void *ptr ) {
MO_FREE(ptr);
}
#endif //MBEDTLS_PLATFORM_MEMORY
void mo_sim_sig_handler(int s){
if (!g_runSimulator) { //already tried to shut down, now force stop
exit(EXIT_FAILURE);
}
g_runSimulator = false; //shut down simulator gracefully
}
/*
* Setup MicroOcpp and API
*/
void load_ocpp_version(std::shared_ptr<MicroOcpp::FilesystemAdapter> filesystem) {
MicroOcpp::configuration_init(filesystem);
#if MO_ENABLE_V201
{
auto protocolVersion_stored = MicroOcpp::declareConfiguration<const char*>("OcppVersion", "1.6", SIMULATOR_FN, false, false, false);
MicroOcpp::configuration_load(SIMULATOR_FN);
if (!strcmp(protocolVersion_stored->getString(), "2.0.1")) {
//select OCPP 2.0.1
g_isOcpp201 = true;
return;
}
}
#endif //MO_ENABLE_V201
g_isOcpp201 = false;
}
void app_setup(MicroOcpp::Connection& connection, std::shared_ptr<MicroOcpp::FilesystemAdapter> filesystem) {
mocpp_initialize(connection,
g_isOcpp201 ?
ChargerCredentials::v201("MicroOcpp Simulator", "MicroOcpp") :
ChargerCredentials("MicroOcpp Simulator", "MicroOcpp"),
filesystem,
false,
g_isOcpp201 ?
MicroOcpp::ProtocolVersion{2,0,1} :
MicroOcpp::ProtocolVersion{1,6}
);
for (unsigned int i = 0; i < connectors.size(); i++) {
connectors[i].setup();
}
}
/*
* Execute one loop iteration
*/
void app_loop() {
mocpp_loop();
for (unsigned int i = 0; i < connectors.size(); i++) {
connectors[i].loop();
}
}
#if MO_NETLIB == MO_NETLIB_MONGOOSE
#ifndef MO_SIM_ENDPOINT_URL
#define MO_SIM_ENDPOINT_URL "http://0.0.0.0:8000" //URL to forward to mg_http_listen(). Will be ignored if the URL field exists in api.jsn
#endif
int main() {
#if MBEDTLS_PLATFORM_MEMORY
mbedtls_platform_set_calloc_free(mo_mem_mbedtls_calloc, mo_mem_mbedtls_free);
#endif //MBEDTLS_PLATFORM_MEMORY
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = mo_sim_sig_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
mg_log_set(MG_LL_INFO);
mg_mgr_init(&mgr);
auto filesystem = MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Use_Mount_FormatOnFail);
load_ocpp_version(filesystem);
struct mg_str api_cert = mg_file_read(&mg_fs_posix, MO_FILENAME_PREFIX "api_cert.pem");
struct mg_str api_key = mg_file_read(&mg_fs_posix, MO_FILENAME_PREFIX "api_key.pem");
auto api_settings_doc = MicroOcpp::FilesystemUtils::loadJson(filesystem, MO_FILENAME_PREFIX "api.jsn", "Simulator");
if (!api_settings_doc) {
api_settings_doc = MicroOcpp::makeJsonDoc("Simulator", 0);
}
JsonObject api_settings = api_settings_doc->as<JsonObject>();
const char *api_url = api_settings["url"] | MO_SIM_ENDPOINT_URL;
mg_http_listen(&mgr, api_url, http_serve, (void*)api_url); // Create listening connection
// ========================================================================
// Fenexity: Environment Variable Configuration
// ========================================================================
const char* websocketUrl = getEnvOrDefault("CENTRAL_SYSTEM_URL", "ws://echo.websocket.events");
const char* chargerId = getEnvOrDefault("CHARGER_ID", "charger-01");
printf("[FENEXITY_CONFIG] WebSocket URL: %s\n", websocketUrl);
printf("[FENEXITY_CONFIG] Charger ID: %s\n", chargerId);
printf("[FENEXITY_CONFIG] OCPP Version: %s\n", g_isOcpp201 ? "2.0.1" : "1.6");
osock = new MicroOcpp::MOcppMongooseClient(&mgr,
websocketUrl, // Use environment variable
chargerId, // Use environment variable
"",
"",
filesystem,
g_isOcpp201 ?
MicroOcpp::ProtocolVersion{2,0,1} :
MicroOcpp::ProtocolVersion{1,6}
);
server_initialize(osock, api_cert.buf ? api_cert.buf : "", api_key.buf ? api_key.buf : "", api_settings["user"] | "", api_settings["pass"] | "");
app_setup(*osock, filesystem);
setOnResetExecute([] (bool isHard) {
g_runSimulator = false;
});
while (g_runSimulator) { //Run Simulator until OCPP Reset is executed or user presses Ctrl+C
mg_mgr_poll(&mgr, 100);
app_loop();
if (!g_bootNotificationTime && getOcppContext()->getModel().getClock().now() >= MicroOcpp::MIN_TIME) {
//time has been set, BootNotification succeeded
g_bootNotificationTime = mocpp_tick_ms();
}
if (!g_isUpAndRunning && g_bootNotificationTime && mocpp_tick_ms() - g_bootNotificationTime >= 1000) {
printf("[Sim] Resetting maximum heap usage after boot success\n");
g_isUpAndRunning = true;
MO_MEM_RESET();
}
}
printf("[Sim] Shutting down Simulator\n");
MO_MEM_PRINT_STATS();
mocpp_deinitialize();
delete osock;
mg_mgr_free(&mgr);
free(api_cert.buf);
free(api_key.buf);
return 0;
}
#elif MO_NETLIB == MO_NETLIB_WASM
int main() {
printf("[WASM] start\n");
auto filesystem = MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Deactivate);
conn = wasm_ocpp_connection_init(nullptr, nullptr, nullptr);
app_setup(*conn, filesystem);
const int LOOP_FREQ = 10; //called 10 times per second
const int BLOCK_INFINITELY = 0; //0 for non-blocking execution, 1 for blocking infinitely
emscripten_set_main_loop(app_loop, LOOP_FREQ, BLOCK_INFINITELY);
printf("[WASM] setup complete\n");
}
#endif