Skip to content

Commit 993382a

Browse files
committed
add memory endpoint
1 parent a27cc2f commit 993382a

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

lib/MicroOcpp

src/api.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "mongoose.h"
77

88
#include <MicroOcpp/Debug.h>
9+
#include <MicroOcpp/Core/Memory.h>
910
#include <MicroOcpp/Model/ConnectorBase/EvseId.h>
1011
#include <MicroOcpp/Model/Authorization/IdToken.h>
1112

@@ -189,6 +190,9 @@ int mocpp_api2_call(const char *uri_raw, size_t uri_raw_len, MicroOcpp::Method m
189190
}
190191

191192
if (mg_match(uri, mg_str("/plugin"), NULL)) {
193+
if (method != MicroOcpp::Method::POST) {
194+
return 405;
195+
}
192196
if (evse_id < 0) {
193197
snprintf(resp_body, resp_body_size, "no action taken");
194198
return 200;
@@ -200,6 +204,9 @@ int mocpp_api2_call(const char *uri_raw, size_t uri_raw_len, MicroOcpp::Method m
200204
return 200;
201205
}
202206
} else if (mg_match(uri, mg_str("/plugout"), NULL)) {
207+
if (method != MicroOcpp::Method::POST) {
208+
return 405;
209+
}
203210
if (evse_id < 0) {
204211
snprintf(resp_body, resp_body_size, "no action taken");
205212
return 200;
@@ -211,6 +218,9 @@ int mocpp_api2_call(const char *uri_raw, size_t uri_raw_len, MicroOcpp::Method m
211218
return 200;
212219
}
213220
} else if (mg_match(uri, mg_str("/end"), NULL)) {
221+
if (method != MicroOcpp::Method::POST) {
222+
return 405;
223+
}
214224
bool trackEvReady = false;
215225
for (size_t i = 0; i < connectors.size(); i++) {
216226
trackEvReady |= connectors[i].getEvReady();
@@ -219,6 +229,9 @@ int mocpp_api2_call(const char *uri_raw, size_t uri_raw_len, MicroOcpp::Method m
219229
snprintf(resp_body, resp_body_size, "%s", trackEvReady ? "suspended EV" : "EV already suspended");
220230
return 200;
221231
} else if (mg_match(uri, mg_str("/state"), NULL)) {
232+
if (method != MicroOcpp::Method::POST) {
233+
return 405;
234+
}
222235
struct mg_str ready_str = mg_http_var(query, mg_str("ready"));
223236
bool ready = true;
224237
if (ready_str.buf) {
@@ -243,6 +256,9 @@ int mocpp_api2_call(const char *uri_raw, size_t uri_raw_len, MicroOcpp::Method m
243256
snprintf(resp_body, resp_body_size, "no action taken - EV not plugged");
244257
return 200;
245258
} else if (mg_match(uri, mg_str("/authorize"), NULL)) {
259+
if (method != MicroOcpp::Method::POST) {
260+
return 405;
261+
}
246262
struct mg_str id = mg_http_var(query, mg_str("id"));
247263
if (!id.buf) {
248264
snprintf(resp_body, resp_body_size, "missing id");
@@ -289,6 +305,43 @@ int mocpp_api2_call(const char *uri_raw, size_t uri_raw_len, MicroOcpp::Method m
289305
"no action taken (EVSE not authorized)");
290306

291307
return 200;
308+
} else if (mg_match(uri, mg_str("/memory/info"), NULL)) {
309+
#if MO_OVERRIDE_ALLOCATION && MO_ENABLE_HEAP_PROFILER
310+
{
311+
if (method != MicroOcpp::Method::GET) {
312+
return 405;
313+
}
314+
315+
int ret = mo_mem_write_stats_json(resp_body, resp_body_size);
316+
if (ret < 0 || ret >= resp_body_size) {
317+
snprintf(resp_body, resp_body_size, "internal error");
318+
return 500;
319+
}
320+
321+
return 200;
322+
}
323+
#else
324+
{
325+
snprintf(resp_body, resp_body_size, "memory profiler disabled");
326+
return 404;
327+
}
328+
#endif
329+
} else if (mg_match(uri, mg_str("/memory/reset"), NULL)) {
330+
#if MO_OVERRIDE_ALLOCATION && MO_ENABLE_HEAP_PROFILER
331+
{
332+
if (method != MicroOcpp::Method::POST) {
333+
return 405;
334+
}
335+
336+
MO_MEM_RESET();
337+
}
338+
#else
339+
{
340+
snprintf(resp_body, resp_body_size, "memory profiler disabled");
341+
return 404;
342+
}
343+
#endif
344+
292345
}
293346

294347
return 404;

src/net_mongoose.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void http_serve(struct mg_connection *c, int ev, void *ev_data) {
120120
mg_http_reply(c, 200, final_headers, serialized.c_str());
121121
return;
122122
} else if (strncmp(message_data->uri.buf, "/api", strlen("api")) == 0) {
123-
#define RESP_BUF_SIZE 1024
123+
#define RESP_BUF_SIZE 8192
124124
char resp_buf [RESP_BUF_SIZE];
125125

126126
//replace endpoint-body separator by null

0 commit comments

Comments
 (0)