|
3 | 3 | // GPL-3.0 License |
4 | 4 |
|
5 | 5 | #include "api.h" |
| 6 | +#include "mongoose.h" |
6 | 7 |
|
7 | 8 | #include <MicroOcpp/Debug.h> |
| 9 | +#include <MicroOcpp/Core/Memory.h> |
| 10 | +#include <MicroOcpp/Model/ConnectorBase/EvseId.h> |
| 11 | +#include <MicroOcpp/Model/Authorization/IdToken.h> |
8 | 12 |
|
9 | 13 | #include "evse.h" |
10 | 14 |
|
11 | 15 | //simple matching function; takes * as a wildcard |
12 | 16 | bool str_match(const char *query, const char *pattern) { |
13 | 17 | size_t qi = 0, pi = 0; |
14 | 18 |
|
15 | | - while (query[qi] && pattern[pi]) { |
16 | | - if (query[qi] == pattern[pi]) { |
| 19 | + while (pattern[pi]) { |
| 20 | + if (query[qi] && query[qi] == pattern[pi]) { |
17 | 21 | qi++; |
18 | 22 | pi++; |
19 | 23 | } else if (pattern[pi] == '*') { |
@@ -155,3 +159,191 @@ int mocpp_api_call(const char *endpoint, MicroOcpp::Method method, const char *b |
155 | 159 |
|
156 | 160 | return status; |
157 | 161 | } |
| 162 | + |
| 163 | +int mocpp_api2_call(const char *uri_raw, size_t uri_raw_len, MicroOcpp::Method method, const char *query_raw, size_t query_raw_len, char *resp_body, size_t resp_body_size) { |
| 164 | + |
| 165 | + snprintf(resp_body, resp_body_size, "%s", ""); |
| 166 | + |
| 167 | + struct mg_str uri = mg_str_n(uri_raw, uri_raw_len); |
| 168 | + struct mg_str query = mg_str_n(query_raw, query_raw_len); |
| 169 | + |
| 170 | + int evse_id = -1; |
| 171 | + int connector_id = -1; |
| 172 | + |
| 173 | + unsigned int num; |
| 174 | + struct mg_str evse_id_str = mg_http_var(query, mg_str("evse_id")); |
| 175 | + if (evse_id_str.buf) { |
| 176 | + if (!mg_str_to_num(evse_id_str, 10, &num, sizeof(num)) || num < 1 || num >= MO_NUM_EVSEID) { |
| 177 | + snprintf(resp_body, resp_body_size, "invalid connector_id"); |
| 178 | + return 400; |
| 179 | + } |
| 180 | + evse_id = (int)num; |
| 181 | + } |
| 182 | + |
| 183 | + struct mg_str connector_id_str = mg_http_var(query, mg_str("connector_id")); |
| 184 | + if (connector_id_str.buf) { |
| 185 | + if (!mg_str_to_num(connector_id_str, 10, &num, sizeof(num)) || num != 1) { |
| 186 | + snprintf(resp_body, resp_body_size, "invalid connector_id"); |
| 187 | + return 400; |
| 188 | + } |
| 189 | + connector_id = (int)num; |
| 190 | + } |
| 191 | + |
| 192 | + if (mg_match(uri, mg_str("/plugin"), NULL)) { |
| 193 | + if (method != MicroOcpp::Method::POST) { |
| 194 | + return 405; |
| 195 | + } |
| 196 | + if (evse_id < 0) { |
| 197 | + snprintf(resp_body, resp_body_size, "no action taken"); |
| 198 | + return 200; |
| 199 | + } else { |
| 200 | + snprintf(resp_body, resp_body_size, "%s", connectors[evse_id-1].getEvPlugged() ? "EV already plugged" : "plugged in EV"); |
| 201 | + connectors[evse_id-1].setEvPlugged(true); |
| 202 | + connectors[evse_id-1].setEvReady(true); |
| 203 | + connectors[evse_id-1].setEvseReady(true); |
| 204 | + return 200; |
| 205 | + } |
| 206 | + } else if (mg_match(uri, mg_str("/plugout"), NULL)) { |
| 207 | + if (method != MicroOcpp::Method::POST) { |
| 208 | + return 405; |
| 209 | + } |
| 210 | + if (evse_id < 0) { |
| 211 | + snprintf(resp_body, resp_body_size, "no action taken"); |
| 212 | + return 200; |
| 213 | + } else { |
| 214 | + snprintf(resp_body, resp_body_size, "%s", connectors[evse_id-1].getEvPlugged() ? "EV already unplugged" : "unplug EV"); |
| 215 | + connectors[evse_id-1].setEvPlugged(false); |
| 216 | + connectors[evse_id-1].setEvReady(false); |
| 217 | + connectors[evse_id-1].setEvseReady(false); |
| 218 | + return 200; |
| 219 | + } |
| 220 | + } else if (mg_match(uri, mg_str("/end"), NULL)) { |
| 221 | + if (method != MicroOcpp::Method::POST) { |
| 222 | + return 405; |
| 223 | + } |
| 224 | + bool trackEvReady = false; |
| 225 | + for (size_t i = 0; i < connectors.size(); i++) { |
| 226 | + trackEvReady |= connectors[i].getEvReady(); |
| 227 | + connectors[i].setEvReady(false); |
| 228 | + } |
| 229 | + snprintf(resp_body, resp_body_size, "%s", trackEvReady ? "suspended EV" : "EV already suspended"); |
| 230 | + return 200; |
| 231 | + } else if (mg_match(uri, mg_str("/state"), NULL)) { |
| 232 | + if (method != MicroOcpp::Method::POST) { |
| 233 | + return 405; |
| 234 | + } |
| 235 | + struct mg_str ready_str = mg_http_var(query, mg_str("ready")); |
| 236 | + bool ready = true; |
| 237 | + if (ready_str.buf) { |
| 238 | + if (mg_match(ready_str, mg_str("true"), NULL)) { |
| 239 | + ready = true; |
| 240 | + } else if (mg_match(ready_str, mg_str("false"), NULL)) { |
| 241 | + ready = false; |
| 242 | + } else { |
| 243 | + snprintf(resp_body, resp_body_size, "invalid ready"); |
| 244 | + return 400; |
| 245 | + } |
| 246 | + } |
| 247 | + bool trackEvReady = false; |
| 248 | + for (size_t i = 0; i < connectors.size(); i++) { |
| 249 | + if (connectors[i].getEvPlugged()) { |
| 250 | + bool trackEvReady = connectors[i].getEvReady(); |
| 251 | + connectors[i].setEvReady(ready); |
| 252 | + snprintf(resp_body, resp_body_size, "%s, %s", ready ? "EV suspended" : "EV not suspended", trackEvReady ? "suspended before" : "not suspended before"); |
| 253 | + return 200; |
| 254 | + } |
| 255 | + } |
| 256 | + snprintf(resp_body, resp_body_size, "no action taken - EV not plugged"); |
| 257 | + return 200; |
| 258 | + } else if (mg_match(uri, mg_str("/authorize"), NULL)) { |
| 259 | + if (method != MicroOcpp::Method::POST) { |
| 260 | + return 405; |
| 261 | + } |
| 262 | + struct mg_str id = mg_http_var(query, mg_str("id")); |
| 263 | + if (!id.buf) { |
| 264 | + snprintf(resp_body, resp_body_size, "missing id"); |
| 265 | + return 400; |
| 266 | + } |
| 267 | + struct mg_str type = mg_http_var(query, mg_str("type")); |
| 268 | + if (!id.buf) { |
| 269 | + snprintf(resp_body, resp_body_size, "missing type"); |
| 270 | + return 400; |
| 271 | + } |
| 272 | + |
| 273 | + int ret; |
| 274 | + char id_buf [MO_IDTOKEN_LEN_MAX + 1]; |
| 275 | + ret = snprintf(id_buf, sizeof(id_buf), "%.*s", (int)id.len, id.buf); |
| 276 | + if (ret < 0 || ret >= sizeof(id_buf)) { |
| 277 | + snprintf(resp_body, resp_body_size, "invalid id"); |
| 278 | + return 400; |
| 279 | + } |
| 280 | + char type_buf [128]; |
| 281 | + ret = snprintf(type_buf, sizeof(type_buf), "%.*s", (int)type.len, type.buf); |
| 282 | + if (ret < 0 || ret >= sizeof(type_buf)) { |
| 283 | + snprintf(resp_body, resp_body_size, "invalid type"); |
| 284 | + return 400; |
| 285 | + } |
| 286 | + |
| 287 | + if (evse_id <= 0) { |
| 288 | + snprintf(resp_body, resp_body_size, "invalid evse_id"); |
| 289 | + return 400; |
| 290 | + } |
| 291 | + |
| 292 | + bool trackAuthActive = connectors[evse_id-1].getSessionIdTag(); |
| 293 | + |
| 294 | + if (!connectors[evse_id-1].presentNfcTag(id_buf, type_buf)) { |
| 295 | + snprintf(resp_body, resp_body_size, "invalid id and / or type"); |
| 296 | + return 400; |
| 297 | + } |
| 298 | + |
| 299 | + bool authActive = connectors[evse_id-1].getSessionIdTag(); |
| 300 | + |
| 301 | + snprintf(resp_body, resp_body_size, "%s", |
| 302 | + !trackAuthActive && authActive ? "authorize in progress" : |
| 303 | + trackAuthActive && !authActive ? "unauthorize in progress" : |
| 304 | + trackAuthActive && authActive ? "no action taken (EVSE still authorized)" : |
| 305 | + "no action taken (EVSE not authorized)"); |
| 306 | + |
| 307 | + 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 | + return 200; |
| 338 | + } |
| 339 | + #else |
| 340 | + { |
| 341 | + snprintf(resp_body, resp_body_size, "memory profiler disabled"); |
| 342 | + return 404; |
| 343 | + } |
| 344 | + #endif |
| 345 | + |
| 346 | + } |
| 347 | + |
| 348 | + return 404; |
| 349 | +} |
0 commit comments