Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

### Added
- Mongoose v7.13 support

## [v1.1.0] - 2024-05-21

### Changed
Expand Down
57 changes: 48 additions & 9 deletions src/MicroOcppMongooseClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@
#define DEBUG_MSG_INTERVAL 5000UL
#define WS_UNRESPONSIVE_THRESHOLD_MS 15000UL

#define MO_MG_V614 614
#define MO_MG_V708 708
#define MO_MG_V713 713
#define MO_MG_V714 714

#ifndef MO_MG_USE_VERSION
#if defined(MO_MG_VERSION_614)
#define MO_MG_USE_VERSION MO_MG_V614
#else
#define MO_MG_USE_VERSION MO_MG_V708
#endif
#endif

#if MO_MG_USE_VERSION == MO_MG_V614
#define MO_MG_F_IS_MOcppMongooseClient MG_F_USER_2
#endif

Expand All @@ -19,7 +32,11 @@ bool validateAuthorizationKeyHex(const char *auth_key_hex);

using namespace MicroOcpp;

#if MO_MG_USE_VERSION <= MO_MG_V708
void ws_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
#else
void ws_cb(struct mg_connection *c, int ev, void *ev_data);
#endif

MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr,
const char *backend_url_factory,
Expand Down Expand Up @@ -75,10 +92,12 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr,

reloadConfigs(); //load WS creds with configs values

#if defined(MO_MG_VERSION_614)
#if MO_MG_USE_VERSION == MO_MG_V614
MO_DBG_DEBUG("use MG version %s (tested with 6.14)", MG_VERSION);
#else
#elif MO_MG_USE_VERSION == MO_MG_V708
MO_DBG_DEBUG("use MG version %s (tested with 7.8)", MG_VERSION);
#elif MO_MG_USE_VERSION == MO_MG_V713
MO_DBG_DEBUG("use MG version %s (tested with 7.13)", MG_VERSION);
#endif

maintainWsConn();
Expand Down Expand Up @@ -106,7 +125,7 @@ MOcppMongooseClient::~MOcppMongooseClient() {
MO_DBG_DEBUG("destruct MOcppMongooseClient");
if (websocket) {
reconnect(); //close WS connection, won't be reopened
#if defined(MO_MG_VERSION_614)
#if MO_MG_USE_VERSION == MO_MG_V614
websocket->flags &= ~MO_MG_F_IS_MOcppMongooseClient;
websocket->user_data = nullptr;
#else
Expand All @@ -124,7 +143,7 @@ bool MOcppMongooseClient::sendTXT(const char *msg, size_t length) {
return false;
}
size_t sent;
#if defined(MO_MG_VERSION_614)
#if MO_MG_USE_VERSION == MO_MG_V614
if (websocket->send_mbuf.len > 0) {
sent = 0;
return false;
Expand Down Expand Up @@ -167,7 +186,7 @@ void MOcppMongooseClient::maintainWsConn() {
if (websocket && isConnectionOpen() &&
ws_ping_interval_int && ws_ping_interval_int->getInt() > 0 && mocpp_tick_ms() - last_hb >= (ws_ping_interval_int->getInt() * 1000UL)) {
last_hb = mocpp_tick_ms();
#if defined(MO_MG_VERSION_614)
#if MO_MG_USE_VERSION == MO_MG_V614
mg_send_websocket_frame(websocket, WEBSOCKET_OP_PING, "", 0);
#else
mg_ws_send(websocket, "", 0, WEBSOCKET_OP_PING);
Expand Down Expand Up @@ -231,7 +250,11 @@ void MOcppMongooseClient::maintainWsConn() {
}

// mg_base64_encode() places a null terminator automatically, because the output is a c-string
#if MO_MG_USE_VERSION <= MO_MG_V708
mg_base64_encode(token, len, base64);
#else
mg_base64_encode(token, len, base64, sizeof(base64));
#endif
delete[] token;

MO_DBG_DEBUG("auth64 len=%u, auth64 Token=%s", base64_length, base64);
Expand All @@ -244,7 +267,7 @@ void MOcppMongooseClient::maintainWsConn() {
(void) 0;
}

#if defined(MO_MG_VERSION_614)
#if MO_MG_USE_VERSION == MO_MG_V614

struct mg_connect_opts opts;
memset(&opts, 0, sizeof(opts));
Expand Down Expand Up @@ -305,7 +328,7 @@ void MOcppMongooseClient::reconnect() {
if (!websocket) {
return;
}
#if defined(MO_MG_VERSION_614)
#if MO_MG_USE_VERSION == MO_MG_V614
if (!connection_closing) {
const char *msg = "socket closed by client";
mg_send_websocket_frame(websocket, WEBSOCKET_OP_CLOSE, msg, strlen(msg));
Expand Down Expand Up @@ -391,8 +414,10 @@ void MOcppMongooseClient::reloadConfigs() {

#if MO_MG_VERSION_614
cs_from_hex((char*)auth_key, auth_key_hex, strlen(auth_key_hex));
#else
#elif MO_MG_USE_VERSION <= MO_MG_V713
mg_unhex(auth_key_hex, strlen(auth_key_hex), auth_key);
#else
mg_str_to_num(mg_str(auth_key_hex), 16, auth_key, MO_AUTHKEY_LEN_MAX);
#endif

auth_key_len = strlen(setting_auth_key_hex_str->getString()) / 2;
Expand Down Expand Up @@ -455,7 +480,7 @@ unsigned long MOcppMongooseClient::getLastConnected() {
return last_connection_established;
}

#if defined(MO_MG_VERSION_614)
#if MO_MG_USE_VERSION == MO_MG_V614

void ws_cb(struct mg_connection *nc, int ev, void *ev_data, void *user_data) {

Expand Down Expand Up @@ -517,7 +542,12 @@ void ws_cb(struct mg_connection *nc, int ev, void *ev_data, void *user_data) {

#else

#if MO_MG_USE_VERSION <= MO_MG_V708
void ws_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
#else
void ws_cb(struct mg_connection *c, int ev, void *ev_data) {
void *fn_data = c->fn_data;
#endif
if (ev != 2) {
MO_DBG_VERBOSE("Cb fn with event: %d\n", ev);
(void)0;
Expand Down Expand Up @@ -548,8 +578,13 @@ void ws_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
}
struct mg_tls_opts opts;
memset(&opts, 0, sizeof(struct mg_tls_opts));
#if MO_MG_USE_VERSION <= MO_MG_V708
opts.ca = ca_string;
opts.srvname = mg_url_host(osock->getUrl());
#else
opts.ca = mg_str(ca_string);
opts.name = mg_url_host(osock->getUrl());
#endif
mg_tls_init(c, &opts);
} else {
MO_DBG_WARN("Insecure connection (WS)");
Expand All @@ -561,7 +596,11 @@ void ws_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
osock->updateRcvTimer();
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
#if MO_MG_USE_VERSION <= MO_MG_V713
if (!osock->getReceiveTXTcallback()((const char*) wm->data.ptr, wm->data.len)) {
#else
if (!osock->getReceiveTXTcallback()((const char*) wm->data.buf, wm->data.len)) {
#endif
MO_DBG_WARN("processing input message failed");
}
osock->updateRcvTimer();
Expand Down