RemoteDebugWS.cpp
6.15 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Libraries Arduino
* *****************
* Library : Remote debug - debug over telnet - for Esp8266 (NodeMCU) or ESP32
* Author : Joao Lopes
* File : RemoteDebugWS - web socket server to RemoteDebugAapp
* Comments: Web socket server uses the arduinWebSockets library (https://github.com/Links2004/arduinoWebSockets)
* The author uses Eclise IDE (sloeber) to made this source
* License : See RemoteDebugWS.h
*
* Versions:
* ------ ---------- -----------------
* 0.2.0 2019-03-19 All public configurations (#defines) have moved to RemoteDebugCfg.h, to facilitate changes for anybody.
* Several adjustments.
* 0.1.0 2019-03-11 Fist version
*
*/
/* TODO:
*/
///// RemoteDebug configuration
#include "RemoteDebugCfg.h"
// Debug enabled ?
#ifndef DEBUG_DISABLED
/////// Includes
#include "RemoteDebugWS.h"
// Only if web socket (RemoteDebugApp) is enabled
#ifndef WEBSOCKET_DISABLED
#include "Arduino.h"
#ifdef USE_LIB_WEBSOCKET // This library already installed
#include <WebSockets.h> // https://github.com/Links2004/arduinoWebSockets
#include <WebSocketsClient.h>
#include <WebSocketsServer.h>
#else // local copy
#include "utility/WebSockets.h" // https://github.com/Links2004/arduinoWebSockets
#include "utility/WebSocketsClient.h"
#include "utility/WebSocketsServer.h"
#endif
#include "RemoteDebug.h"
/////// Defines
// Version
#define REMOTEDEBUGWS_VERSION "0.1.1"
// Internal debug macro - recommended stay disable
#define D(fmt, ...) // Without this
//#define D(fmt, ...) Serial.printf("rdws: " fmt "\n", ##__VA_ARGS__) // Serial debug
/////// Variables
// Arduino websocket server (to comunicate with RemoteDebugApp)
static WebSocketsServer WebSocketServer(WEBSOCKET_PORT); // Websocket server on port 81
// Variables used by webSocketEvent
static int8_t _webSocketConnected = WS_NOT_CONNECTED; // Client is connected ?
static RemoteDebugWSCallbacks* _callbacks; // callbacks to RemoteDebug
////// Methods
// Initialize the socket server
void RemoteDebugWS::begin(RemoteDebugWSCallbacks* callbacks) {
// Initialize web socket (RemoteDebugApp)
WebSocketServer.begin(); // start the websocket server
WebSocketServer.onEvent(webSocketEvent); // if there's an incomming websocket message, go to function 'webSocketEvent'
// Set callbacks
_callbacks = callbacks;
// Debug
D("socket server started");
}
// Finalize the socket server
void RemoteDebugWS::stop() {
// Finalize web socket (RemoteDebugApp)
WebSocketServer.close();
_webSocketConnected = WS_NOT_CONNECTED;
}
void RemoteDebugWS::disconnectAllClients() {
// Disconnect all clients
WebSocketServer.disconnect();
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
}
void RemoteDebugWS::disconnect() {
// Disconnect actual clients
if (_webSocketConnected != WS_NOT_CONNECTED) {
WebSocketServer.disconnect(_webSocketConnected);
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
}
}
// Handle
void RemoteDebugWS::handle() {
WebSocketServer.loop();
// // Test
//
// if (_webSocketConnected) {
// WebSocketServer.sendTXT(_webSocketConnected, "AAAAAAAAA\n");
// }
}
// Is connected ?
boolean RemoteDebugWS::isConnected() {
return (_webSocketConnected != WS_NOT_CONNECTED);
}
// Print
size_t RemoteDebugWS::write(const uint8_t *buffer, size_t size) {
// Process buffer
for(size_t i=0; i<size; i++) {
write((uint8_t) buffer[i]);
}
return size;
}
size_t RemoteDebugWS::write(uint8_t character) {
static String buffer = "";
size_t ret = 0;
// Process
if (character == '\n') {
if (_webSocketConnected != WS_NOT_CONNECTED) {
D("write send buf[%u]: %s", buffer.length(), buffer.c_str());
WebSocketServer.sendTXT(_webSocketConnected, buffer.c_str(), buffer.length());
}
// Empty the buffer
ret = buffer.length();
buffer = "";
} else if (character != '\r' && isPrintable(character)) {
// Append
buffer.concat((char)character);
}
// Return
return ret;
}
// Destructor
RemoteDebugWS::~RemoteDebugWS() {
// Stop
stop();
}
/////// WebSocket to comunicate with RemoteDebugApp
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t payloadlength) { // When a WebSocket message is received
int blk_count = 0;
char ipaddr[26];
IPAddress localip;
switch (type) {
case WStype_DISCONNECTED: // if the websocket is disconnected
D("[%u] Disconnected!", num);
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
break;
case WStype_CONNECTED: // if a new websocket connection is established
{
#ifdef D // serial debug
IPAddress ip = WebSocketServer.remoteIP(num);
D("[%u] Connected from %d.%d.%d.%d url: %s", num, ip[0], ip[1], ip[2], ip[3],
payload);
#endif
// Have in another connection
// One connection to reduce overheads
if (num != _webSocketConnected) {
WebSocketServer.sendTXT(_webSocketConnected, "* Closing client connection ...");
WebSocketServer.disconnect(_webSocketConnected);
}
// Set as connected
_webSocketConnected = num;
// Send initial message
WebSocketServer.sendTXT(_webSocketConnected, "$app:I");
// Callback
if (_callbacks) {
_callbacks->onConnect();
}
}
break;
case WStype_TEXT: // if new text data is received
{
// if (payloadlength == 0) {
// return;
// }
D("[%u] get Text: %s", num, payload);
// Callback
if (_callbacks) {
_callbacks->onReceive((const char*)payload);
}
}
break;
case WStype_ERROR: // if new text data is received
D("Error");
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
break;
default:
// Disconnected
_webSocketConnected = WS_NOT_CONNECTED;
// Callback
if (_callbacks) {
_callbacks->onDisconnect();
}
D("WStype %x not handled ", type);
}
}
#endif // WEBSOCKET_DISABLED
#endif // DEBUG_DISABLED
/// End