WebSockets.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /**
  2. * @file WebSockets.h
  3. * @date 20.05.2015
  4. * @author Markus Sattler
  5. *
  6. * Copyright (c) 2015 Markus Sattler. All rights reserved.
  7. * This file is part of the WebSockets for Arduino.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #ifndef WEBSOCKETS_H_
  25. #define WEBSOCKETS_H_
  26. #ifdef STM32_DEVICE
  27. #include <application.h>
  28. #define bit(b) (1UL << (b)) // Taken directly from Arduino.h
  29. #else
  30. #include <Arduino.h>
  31. #include <IPAddress.h>
  32. #endif
  33. #ifdef ARDUINO_ARCH_AVR
  34. #error Version 2.x.x currently does not support Arduino with AVR since there is no support for std namespace of c++.
  35. #error Use Version 1.x.x. (ATmega branch)
  36. #else
  37. #include <functional>
  38. #endif
  39. #include "WebSocketsVersion.h"
  40. #ifndef NODEBUG_WEBSOCKETS
  41. #ifdef DEBUG_ESP_PORT
  42. #define DEBUG_WEBSOCKETS(...) \
  43. { \
  44. DEBUG_ESP_PORT.printf(__VA_ARGS__); \
  45. DEBUG_ESP_PORT.flush(); \
  46. }
  47. #else
  48. //#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
  49. #endif
  50. #endif
  51. #ifndef DEBUG_WEBSOCKETS
  52. #define DEBUG_WEBSOCKETS(...)
  53. #ifndef NODEBUG_WEBSOCKETS
  54. #define NODEBUG_WEBSOCKETS
  55. #endif
  56. #endif
  57. #if defined(ESP8266) || defined(ESP32)
  58. #define WEBSOCKETS_MAX_DATA_SIZE (15 * 1024)
  59. #define WEBSOCKETS_USE_BIG_MEM
  60. #define GET_FREE_HEAP ESP.getFreeHeap()
  61. // moves all Header strings to Flash (~300 Byte)
  62. //#define WEBSOCKETS_SAVE_RAM
  63. #if defined(ESP8266)
  64. #define WEBSOCKETS_YIELD() delay(0)
  65. #define WEBSOCKETS_YIELD_MORE() delay(1)
  66. #elif defined(ESP32)
  67. #define WEBSOCKETS_YIELD() yield()
  68. #define WEBSOCKETS_YIELD_MORE() delay(1)
  69. #endif
  70. #elif defined(STM32_DEVICE)
  71. #define WEBSOCKETS_MAX_DATA_SIZE (15 * 1024)
  72. #define WEBSOCKETS_USE_BIG_MEM
  73. #define GET_FREE_HEAP System.freeMemory()
  74. #define WEBSOCKETS_YIELD()
  75. #define WEBSOCKETS_YIELD_MORE()
  76. #else
  77. //atmega328p has only 2KB ram!
  78. #define WEBSOCKETS_MAX_DATA_SIZE (1024)
  79. // moves all Header strings to Flash
  80. #define WEBSOCKETS_SAVE_RAM
  81. #define WEBSOCKETS_YIELD()
  82. #define WEBSOCKETS_YIELD_MORE()
  83. #endif
  84. #define WEBSOCKETS_TCP_TIMEOUT (5000)
  85. #define NETWORK_ESP8266_ASYNC (0)
  86. #define NETWORK_ESP8266 (1)
  87. #define NETWORK_W5100 (2)
  88. #define NETWORK_ENC28J60 (3)
  89. #define NETWORK_ESP32 (4)
  90. #define NETWORK_ESP32_ETH (5)
  91. // max size of the WS Message Header
  92. #define WEBSOCKETS_MAX_HEADER_SIZE (14)
  93. #if !defined(WEBSOCKETS_NETWORK_TYPE)
  94. // select Network type based
  95. #if defined(ESP8266) || defined(ESP31B)
  96. #define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266
  97. //#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266_ASYNC
  98. //#define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
  99. #elif defined(ESP32)
  100. #define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP32
  101. //#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP32_ETH
  102. #else
  103. #define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
  104. #endif
  105. #endif
  106. // Includes and defined based on Network Type
  107. #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
  108. // Note:
  109. // No SSL/WSS support for client in Async mode
  110. // TLS lib need a sync interface!
  111. #if defined(ESP8266)
  112. #include <ESP8266WiFi.h>
  113. #elif defined(ESP32)
  114. #include <WiFi.h>
  115. #include <WiFiClientSecure.h>
  116. #define SSL_AXTLS
  117. #elif defined(ESP31B)
  118. #include <ESP31BWiFi.h>
  119. #else
  120. #error "network type ESP8266 ASYNC only possible on the ESP mcu!"
  121. #endif
  122. #include <ESPAsyncTCP.h>
  123. #include <ESPAsyncTCPbuffer.h>
  124. #define WEBSOCKETS_NETWORK_CLASS AsyncTCPbuffer
  125. #define WEBSOCKETS_NETWORK_SERVER_CLASS AsyncServer
  126. #elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
  127. #if !defined(ESP8266) && !defined(ESP31B)
  128. #error "network type ESP8266 only possible on the ESP mcu!"
  129. #endif
  130. #ifdef ESP8266
  131. #include <ESP8266WiFi.h>
  132. #if defined(wificlientbearssl_h) && !defined(USING_AXTLS) && !defined(wificlientsecure_h)
  133. #define SSL_BARESSL
  134. #else
  135. #define SSL_AXTLS
  136. #endif
  137. #else
  138. #include <ESP31BWiFi.h>
  139. #endif
  140. #define WEBSOCKETS_NETWORK_CLASS WiFiClient
  141. #define WEBSOCKETS_NETWORK_SSL_CLASS WiFiClientSecure
  142. #define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
  143. #elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100)
  144. #ifdef STM32_DEVICE
  145. #define WEBSOCKETS_NETWORK_CLASS TCPClient
  146. #define WEBSOCKETS_NETWORK_SERVER_CLASS TCPServer
  147. #else
  148. #include <Ethernet.h>
  149. #include <SPI.h>
  150. #define WEBSOCKETS_NETWORK_CLASS EthernetClient
  151. #define WEBSOCKETS_NETWORK_SERVER_CLASS EthernetServer
  152. #endif
  153. #elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_ENC28J60)
  154. #include <UIPEthernet.h>
  155. #define WEBSOCKETS_NETWORK_CLASS UIPClient
  156. #define WEBSOCKETS_NETWORK_SERVER_CLASS UIPServer
  157. #elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
  158. #include <WiFi.h>
  159. #include <WiFiClientSecure.h>
  160. #define SSL_AXTLS
  161. #define WEBSOCKETS_NETWORK_CLASS WiFiClient
  162. #define WEBSOCKETS_NETWORK_SSL_CLASS WiFiClientSecure
  163. #define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
  164. #elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32_ETH)
  165. #include <ETH.h>
  166. #define WEBSOCKETS_NETWORK_CLASS WiFiClient
  167. #define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
  168. #else
  169. #error "no network type selected!"
  170. #endif
  171. #ifdef WEBSOCKETS_NETWORK_SSL_CLASS
  172. #define HAS_SSL
  173. #endif
  174. // moves all Header strings to Flash (~300 Byte)
  175. #ifdef WEBSOCKETS_SAVE_RAM
  176. #define WEBSOCKETS_STRING(var) F(var)
  177. #else
  178. #define WEBSOCKETS_STRING(var) var
  179. #endif
  180. typedef enum {
  181. WSC_NOT_CONNECTED,
  182. WSC_HEADER,
  183. WSC_BODY,
  184. WSC_CONNECTED
  185. } WSclientsStatus_t;
  186. typedef enum {
  187. WStype_ERROR,
  188. WStype_DISCONNECTED,
  189. WStype_CONNECTED,
  190. WStype_TEXT,
  191. WStype_BIN,
  192. WStype_FRAGMENT_TEXT_START,
  193. WStype_FRAGMENT_BIN_START,
  194. WStype_FRAGMENT,
  195. WStype_FRAGMENT_FIN,
  196. WStype_PING,
  197. WStype_PONG,
  198. } WStype_t;
  199. typedef enum {
  200. WSop_continuation = 0x00, ///< %x0 denotes a continuation frame
  201. WSop_text = 0x01, ///< %x1 denotes a text frame
  202. WSop_binary = 0x02, ///< %x2 denotes a binary frame
  203. ///< %x3-7 are reserved for further non-control frames
  204. WSop_close = 0x08, ///< %x8 denotes a connection close
  205. WSop_ping = 0x09, ///< %x9 denotes a ping
  206. WSop_pong = 0x0A ///< %xA denotes a pong
  207. ///< %xB-F are reserved for further control frames
  208. } WSopcode_t;
  209. typedef struct {
  210. bool fin;
  211. bool rsv1;
  212. bool rsv2;
  213. bool rsv3;
  214. WSopcode_t opCode;
  215. bool mask;
  216. size_t payloadLen;
  217. uint8_t * maskKey;
  218. } WSMessageHeader_t;
  219. typedef struct {
  220. void init(uint8_t num,
  221. uint32_t pingInterval,
  222. uint32_t pongTimeout,
  223. uint8_t disconnectTimeoutCount) {
  224. this->num = num;
  225. this->pingInterval = pingInterval;
  226. this->pongTimeout = pongTimeout;
  227. this->disconnectTimeoutCount = disconnectTimeoutCount;
  228. }
  229. uint8_t num = 0; ///< connection number
  230. WSclientsStatus_t status = WSC_NOT_CONNECTED;
  231. WEBSOCKETS_NETWORK_CLASS * tcp = nullptr;
  232. bool isSocketIO = false; ///< client for socket.io server
  233. #if defined(HAS_SSL)
  234. bool isSSL = false; ///< run in ssl mode
  235. WEBSOCKETS_NETWORK_SSL_CLASS * ssl;
  236. #endif
  237. String cUrl; ///< http url
  238. uint16_t cCode = 0; ///< http code
  239. bool cIsClient = false; ///< will be used for masking
  240. bool cIsUpgrade = false; ///< Connection == Upgrade
  241. bool cIsWebsocket = false; ///< Upgrade == websocket
  242. String cSessionId; ///< client Set-Cookie (session id)
  243. String cKey; ///< client Sec-WebSocket-Key
  244. String cAccept; ///< client Sec-WebSocket-Accept
  245. String cProtocol; ///< client Sec-WebSocket-Protocol
  246. String cExtensions; ///< client Sec-WebSocket-Extensions
  247. uint16_t cVersion = 0; ///< client Sec-WebSocket-Version
  248. uint8_t cWsRXsize = 0; ///< State of the RX
  249. uint8_t cWsHeader[WEBSOCKETS_MAX_HEADER_SIZE]; ///< RX WS Message buffer
  250. WSMessageHeader_t cWsHeaderDecode;
  251. String base64Authorization; ///< Base64 encoded Auth request
  252. String plainAuthorization; ///< Base64 encoded Auth request
  253. String extraHeaders;
  254. bool cHttpHeadersValid = false; ///< non-websocket http header validity indicator
  255. size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count
  256. bool pongReceived = false;
  257. uint32_t pingInterval = 0; // how often ping will be sent, 0 means "heartbeat is not active"
  258. uint32_t lastPing = 0; // millis when last pong has been received
  259. uint32_t pongTimeout = 0; // interval in millis after which pong is considered to timeout
  260. uint8_t disconnectTimeoutCount = 0; // after how many subsequent pong timeouts discconnect will happen, 0 means "do not disconnect"
  261. uint8_t pongTimeoutCount = 0; // current pong timeout count
  262. #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
  263. String cHttpLine; ///< HTTP header lines
  264. #endif
  265. } WSclient_t;
  266. class WebSockets {
  267. protected:
  268. #ifdef __AVR__
  269. typedef void (*WSreadWaitCb)(WSclient_t * client, bool ok);
  270. #else
  271. typedef std::function<void(WSclient_t * client, bool ok)> WSreadWaitCb;
  272. #endif
  273. virtual void clientDisconnect(WSclient_t * client) = 0;
  274. virtual bool clientIsConnected(WSclient_t * client) = 0;
  275. void clientDisconnect(WSclient_t * client, uint16_t code, char * reason = NULL, size_t reasonLen = 0);
  276. virtual void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin) = 0;
  277. uint8_t createHeader(uint8_t * buf, WSopcode_t opcode, size_t length, bool mask, uint8_t maskKey[4], bool fin);
  278. bool sendFrameHeader(WSclient_t * client, WSopcode_t opcode, size_t length = 0, bool fin = true);
  279. bool sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t length = 0, bool fin = true, bool headerToPayload = false);
  280. void headerDone(WSclient_t * client);
  281. void handleWebsocket(WSclient_t * client);
  282. bool handleWebsocketWaitFor(WSclient_t * client, size_t size);
  283. void handleWebsocketCb(WSclient_t * client);
  284. void handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t * payload);
  285. String acceptKey(String & clientKey);
  286. String base64_encode(uint8_t * data, size_t length);
  287. bool readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWaitCb cb);
  288. virtual size_t write(WSclient_t * client, uint8_t * out, size_t n);
  289. size_t write(WSclient_t * client, const char * out);
  290. void enableHeartbeat(WSclient_t * client, uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount);
  291. void handleHBTimeout(WSclient_t * client);
  292. };
  293. #ifndef UNUSED
  294. #define UNUSED(var) (void)(var)
  295. #endif
  296. #endif /* WEBSOCKETS_H_ */