55
66#include " MongooseOcppSocketClient.h"
77#include " net_manager.h"
8+ #include " debug.h"
89
910/* ** To define a custom CA for OCPP please define the flag OCPP_CUSTOM_CA and set define const char *ocpp_ca with the certificate
1011 *
@@ -34,23 +35,29 @@ const char *ocpp_ca = "-----BEGIN CERTIFICATE-----\r\n"
3435
3536 ***/
3637
38+ #define WS_UNRESPONSIVE_THRESHOLD_MS 4000UL
39+
40+ #define DEBUG_MSG_INTERVAL 5000UL
41+
42+ #define RECONNECT_AFTER 5000UL // pause interval between two reconnection attempts
43+
3744// see ArduinoMongoose/src/mongoose.c
3845#ifndef MG_WEBSOCKET_PING_INTERVAL_SECONDS
3946#define MG_WEBSOCKET_PING_INTERVAL_SECONDS 5
4047#endif
4148
42- #define MG_WEBSOCKET_PING_INTERVAL_MS (MG_WEBSOCKET_PING_INTERVAL_SECONDS * 1000 )
49+ #define MG_WEBSOCKET_PING_INTERVAL_MS (MG_WEBSOCKET_PING_INTERVAL_SECONDS * 1000UL )
4350
4451bool ocppIsConnected = false ;
4552
46- MongooseOcppSocketClient::MongooseOcppSocketClient (String &ws_url) {
53+ MongooseOcppSocketClient::MongooseOcppSocketClient (const String &ws_url) {
4754 this ->ws_url = String (ws_url);
4855
4956}
5057
5158MongooseOcppSocketClient::~MongooseOcppSocketClient () {
52- if (DEBUG_OUT) Serial. print (F (" [MongooseOcppSocketClient] Close and destroy connection to " ));
53- if (DEBUG_OUT) Serial. println (this ->ws_url );
59+ DBUG (F (" [MongooseOcppSocketClient] Close and destroy connection to " ));
60+ DBUGLN (this ->ws_url );
5461 if (nc) {
5562 connection_established = false ;
5663 ocppIsConnected = connection_established; // need it static for Wi-Fi dashboard
@@ -70,40 +77,32 @@ void MongooseOcppSocketClient::mg_event_callback(struct mg_connection *nc, int e
7077 return ;
7178 }
7279
73- if (DEBUG_OUT && ev != 0 ) {
74- Serial.print (F (" [MongooseOcppSocketClient] Opcode: " ));
75- Serial.print (ev);
76- Serial.print (F (" , flags " ));
77- Serial.print (nc->flags );
78- Serial.println ();
79- }
80-
8180 if (ev != MG_EV_POLL && ev != MG_EV_SEND) {
8281 instance->last_recv = millis ();
8382 }
8483
8584 switch (ev) {
8685 case MG_EV_CONNECT: {
8786 int status = *((int *) ev_data);
88- if (DEBUG_OUT && status != 0 ) {
89- Serial. print (F (" [MongooseOcppSocketClient] Connection to " ));
90- instance->printUrl ( );
91- Serial. print (F (" -- Error: " ));
92- Serial. println (status);
87+ if (status != 0 ) {
88+ DBUG (F (" [MongooseOcppSocketClient] Connection to " ));
89+ DBUG ( instance->ws_url );
90+ DBUG (F (" -- Error: " ));
91+ DBUGLN (status);
9392 }
9493 break ;
9594 }
9695 case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
9796 struct http_message *hm = (struct http_message *) ev_data;
98- if (DEBUG_OUT) Serial. print (F (" [MongooseOcppSocketClient] Connection to " ));
99- if (DEBUG_OUT) instance->printUrl ( );
97+ DBUG (F (" [MongooseOcppSocketClient] Connection to " ));
98+ DBUG ( instance->ws_url );
10099 if (hm->resp_code == 101 ) {
101- if (DEBUG_OUT) Serial. print ( F (" -- Connected\n " ));
100+ DBUGLN ( F (" -- Connected" ));
102101 instance->connection_established = true ;
103102 ocppIsConnected = instance->connection_established ; // need it static for Wi-Fi dashboard
104103 } else {
105- if (DEBUG_OUT) Serial. print (F (" -- Connection failed! HTTP code " ));
106- if (DEBUG_OUT) Serial. println (hm->resp_code );
104+ DBUG (F (" -- Connection failed! HTTP code " ));
105+ DBUGLN (hm->resp_code );
107106 /* Connection will be closed after this. */
108107 }
109108 break ;
@@ -115,27 +114,18 @@ void MongooseOcppSocketClient::mg_event_callback(struct mg_connection *nc, int e
115114 case MG_EV_WEBSOCKET_FRAME: {
116115 struct websocket_message *wm = (struct websocket_message *) ev_data;
117116
118- if (DEBUG_OUT) {
119- Serial.print (F (" [MongooseOcppSocketClient] WS get text: " ));
120- for (int i = 0 ; i < wm->size ; i++)
121- Serial.print ((char ) wm->data [i]);
122- Serial.println ();
123- }
124-
125117 if (!instance->receiveTXT ((const char *) wm->data , wm->size )) { // forward message to OcppEngine
126- if (DEBUG_OUT) Serial. print ( F (" [MongooseOcppSocketClient] Processing WebSocket input event failed!\n " ));
118+ DBUGLN ( F (" [MongooseOcppSocketClient] Processing WebSocket input event failed!" ));
127119 }
128120 break ;
129121 }
130122 case MG_EV_CLOSE: {
131123 instance->connection_established = false ;
132124 ocppIsConnected = instance->connection_established ; // need it static for Wi-Fi dashboard
133125 instance->nc = NULL ; // resources will be free'd by Mongoose
134- if (DEBUG_OUT) {
135- Serial.print (F (" [MongooseOcppSocketClient] Connection to " ));
136- instance->printUrl ();
137- Serial.print (F (" -- Closed\n " ));
138- }
126+ DBUG (F (" [MongooseOcppSocketClient] Connection to " ));
127+ DBUG (instance->ws_url );
128+ DBUGLN (F (" -- Closed" ));
139129 break ;
140130 }
141131 }
@@ -147,16 +137,15 @@ void MongooseOcppSocketClient::loop() {
147137
148138void MongooseOcppSocketClient::maintainWsConn () {
149139
150- const ulong DEBUG_MSG_INTERVAL = 5000 ;
151- if (DEBUG_OUT && millis () - last_debug_message >= DEBUG_MSG_INTERVAL) {
152- last_debug_message = millis ();
140+ if (millis () - last_status_dbg_msg >= DEBUG_MSG_INTERVAL) {
141+ last_status_dbg_msg = millis ();
153142
154143 // WS successfully connected?
155144 if (!connection_established) {
156- if (DEBUG_OUT) Serial. print ( F (" [MongooseOcppSocketClient] WS unconnected\n " ));
145+ DBUGLN ( F (" [MongooseOcppSocketClient] WS unconnected" ));
157146 } else if (millis () - last_recv >= MG_WEBSOCKET_PING_INTERVAL_MS + WS_UNRESPONSIVE_THRESHOLD_MS) {
158147 // WS connected but unresponsive
159- if (DEBUG_OUT) Serial. print ( F (" [MongooseOcppSocketClient] WS unresponsive\n " ));
148+ DBUGLN ( F (" [MongooseOcppSocketClient] WS unresponsive" ));
160149 }
161150 }
162151
@@ -172,13 +161,12 @@ void MongooseOcppSocketClient::maintainWsConn() {
172161 return ;
173162 }
174163
175- const ulong RECONNECT_AFTER = 5000 ; // in ms
176164 if (millis () - last_reconnection_attempt < RECONNECT_AFTER) {
177165 return ;
178166 }
179167
180- if (DEBUG_OUT) Serial. print (F (" [MongooseOcppSocketClient] (re-)connect to " ));
181- if (DEBUG_OUT) Serial. println (this ->ws_url );
168+ DBUG (F (" [MongooseOcppSocketClient] (re-)connect to " ));
169+ DBUGLN (this ->ws_url );
182170
183171 struct mg_connect_opts opts;
184172 Mongoose.getDefaultOpts (&opts);
@@ -192,16 +180,16 @@ void MongooseOcppSocketClient::maintainWsConn() {
192180 nc = mg_connect_ws_opt (Mongoose.getMgr (), mg_event_callback, this , opts, this ->ws_url .c_str (), " ocpp1.6" , NULL );
193181
194182 if (!nc) {
195- if (DEBUG_OUT) Serial. print (F (" [MongooseOcppSocketClient] Failed to connect to URL: " ));
196- if (DEBUG_OUT) Serial. println (this ->ws_url );
183+ DBUG (F (" [MongooseOcppSocketClient] Failed to connect to URL: " ));
184+ DBUGLN (this ->ws_url );
197185 }
198186
199187 nc->flags |= MG_F_IS_MongooseOcppSocketClient;
200188
201189 last_reconnection_attempt = millis ();
202190}
203191
204- void MongooseOcppSocketClient::reconnect (String &ws_url) {
192+ void MongooseOcppSocketClient::reconnect (const String &ws_url) {
205193 this ->ws_url = String (ws_url);
206194
207195 connection_established = false ;
@@ -249,11 +237,22 @@ bool MongooseOcppSocketClient::ocppConnected() {
249237}
250238
251239bool MongooseOcppSocketClient::isValidUrl (const char *url) {
240+ // URL must start with ws: or wss:
241+ if (url[0 ] != ' W' && url[0 ] != ' w' )
242+ return false ;
243+ if (url[1 ] != ' S' && url[1 ] != ' s' )
244+ return false ;
245+
246+ if (url[2 ] == ' S' && url[2 ] == ' s' ) {
247+ if (url[3 ] != ' :' )
248+ return false ;
249+ // else: passed
250+ } else if (url[2 ] != ' :' ) {
251+ return false ;
252+ }
253+ // passed
254+
252255 unsigned int port_i = 0 ;
253256 struct mg_str scheme, query, fragment;
254257 return mg_parse_uri (mg_mk_str (url), &scheme, NULL , NULL , &port_i, NULL , &query, &fragment) == 0 ; // mg_parse_uri returns 0 on success, false otherwise
255258}
256-
257- void MongooseOcppSocketClient::printUrl () {
258- Serial.print (ws_url);
259- }
0 commit comments