Simple websocket client based on the original with a few added features such as: - setBaud() - set the baud rate for the communication - Initialize() - mimics the constructor - chaged read() to readmsg() to avoid confusion with other functions

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WebSocketClient by Samuel Mokrani

Committer:
defrost
Date:
Thu Apr 07 17:02:56 2016 +0000
Revision:
17:c4f0e141bdbd
Parent:
16:a8c6ee37356f
Child:
18:49cfd9b4e060
- Debug disabled

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 3:9589afa4712e 1 #include "Websocket.h"
samux 3:9589afa4712e 2
samux 7:4567996414a5 3 #define MAX_TRY_WRITE 20
samux 7:4567996414a5 4 #define MAX_TRY_READ 10
samux 3:9589afa4712e 5
defrost 17:c4f0e141bdbd 6 //#define DEBUG
defrost 15:9df4ffc6ce48 7 #define INFOMESSAGES
samux 3:9589afa4712e 8 //Debug is disabled by default
defrost 15:9df4ffc6ce48 9 #ifdef DEBUG
defrost 15:9df4ffc6ce48 10 #define DBG(x, ...) pc.printf("[WebSocket : DBG] "x"\r\n", ##__VA_ARGS__);
defrost 15:9df4ffc6ce48 11 #define WARN(x, ...) pc.printf("[WebSocket : WARN] "x"\r\n", ##__VA_ARGS__);
defrost 15:9df4ffc6ce48 12 #define ERR(x, ...) pc.printf("[WebSocket : ERR] "x"\r\n", ##__VA_ARGS__);
samux 3:9589afa4712e 13 #else
samux 3:9589afa4712e 14 #define DBG(x, ...)
samux 3:9589afa4712e 15 #define WARN(x, ...)
samux 3:9589afa4712e 16 #define ERR(x, ...)
samux 3:9589afa4712e 17 #endif
samux 3:9589afa4712e 18
defrost 15:9df4ffc6ce48 19 #ifdef INFOMESSAGES
defrost 15:9df4ffc6ce48 20 #define INFO(x, ...) pc.printf("[WebSocket : INFO] "x"\r\n", ##__VA_ARGS__);
defrost 15:9df4ffc6ce48 21 #else
defrost 15:9df4ffc6ce48 22 #define INFO(x, ...)
defrost 15:9df4ffc6ce48 23 #endif
samux 3:9589afa4712e 24
samux 3:9589afa4712e 25 Websocket::Websocket(char * url) {
samux 3:9589afa4712e 26 fillFields(url);
samux 3:9589afa4712e 27 socket.set_blocking(false, 400);
samux 3:9589afa4712e 28 }
samux 3:9589afa4712e 29
defrost 11:85bff70bab45 30 Websocket::Websocket() {
defrost 11:85bff70bab45 31
defrost 11:85bff70bab45 32 }
defrost 11:85bff70bab45 33
defrost 11:85bff70bab45 34 void Websocket::Initialize(char * url){
defrost 11:85bff70bab45 35 fillFields(url);
defrost 11:85bff70bab45 36 socket.set_blocking(false, 400);
defrost 11:85bff70bab45 37 }
defrost 11:85bff70bab45 38
defrost 15:9df4ffc6ce48 39 void Websocket::setBaud(int baudrate){
defrost 15:9df4ffc6ce48 40 socket.setBaud(baudrate);
defrost 15:9df4ffc6ce48 41 return;
defrost 15:9df4ffc6ce48 42 }
defrost 15:9df4ffc6ce48 43
samux 3:9589afa4712e 44 void Websocket::fillFields(char * url) {
donatien 6:86e89a0369b9 45 int ret = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path));
donatien 6:86e89a0369b9 46 if(ret)
donatien 6:86e89a0369b9 47 {
donatien 6:86e89a0369b9 48 ERR("URL parsing failed; please use: \"ws://ip-or-domain[:port]/path\"");
donatien 6:86e89a0369b9 49 return;
donatien 6:86e89a0369b9 50 }
samux 3:9589afa4712e 51
donatien 6:86e89a0369b9 52 if(port == 0) //TODO do handle WSS->443
donatien 6:86e89a0369b9 53 {
donatien 6:86e89a0369b9 54 port = 80;
donatien 6:86e89a0369b9 55 }
donatien 6:86e89a0369b9 56
donatien 6:86e89a0369b9 57 if(strcmp(scheme, "ws"))
donatien 6:86e89a0369b9 58 {
donatien 6:86e89a0369b9 59 ERR("Wrong scheme, please use \"ws\" instead");
donatien 6:86e89a0369b9 60 }
donatien 6:86e89a0369b9 61 }
samux 3:9589afa4712e 62
donatien 6:86e89a0369b9 63 int Websocket::parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen) //Parse URL
donatien 6:86e89a0369b9 64 {
donatien 6:86e89a0369b9 65 char* schemePtr = (char*) url;
donatien 6:86e89a0369b9 66 char* hostPtr = (char*) strstr(url, "://");
donatien 6:86e89a0369b9 67 if(hostPtr == NULL)
donatien 6:86e89a0369b9 68 {
donatien 6:86e89a0369b9 69 WARN("Could not find host");
donatien 6:86e89a0369b9 70 return -1; //URL is invalid
donatien 6:86e89a0369b9 71 }
donatien 6:86e89a0369b9 72
donatien 6:86e89a0369b9 73 if( maxSchemeLen < hostPtr - schemePtr + 1 ) //including NULL-terminating char
donatien 6:86e89a0369b9 74 {
donatien 6:86e89a0369b9 75 WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1);
donatien 6:86e89a0369b9 76 return -1;
donatien 6:86e89a0369b9 77 }
donatien 6:86e89a0369b9 78 memcpy(scheme, schemePtr, hostPtr - schemePtr);
donatien 6:86e89a0369b9 79 scheme[hostPtr - schemePtr] = '\0';
donatien 6:86e89a0369b9 80
donatien 6:86e89a0369b9 81 hostPtr+=3;
donatien 6:86e89a0369b9 82
donatien 6:86e89a0369b9 83 size_t hostLen = 0;
samux 3:9589afa4712e 84
donatien 6:86e89a0369b9 85 char* portPtr = strchr(hostPtr, ':');
donatien 6:86e89a0369b9 86 if( portPtr != NULL )
donatien 6:86e89a0369b9 87 {
donatien 6:86e89a0369b9 88 hostLen = portPtr - hostPtr;
donatien 6:86e89a0369b9 89 portPtr++;
donatien 6:86e89a0369b9 90 if( sscanf(portPtr, "%hu", port) != 1)
donatien 6:86e89a0369b9 91 {
donatien 6:86e89a0369b9 92 WARN("Could not find port");
donatien 6:86e89a0369b9 93 return -1;
donatien 6:86e89a0369b9 94 }
donatien 6:86e89a0369b9 95 }
donatien 6:86e89a0369b9 96 else
donatien 6:86e89a0369b9 97 {
donatien 6:86e89a0369b9 98 *port=0;
donatien 6:86e89a0369b9 99 }
donatien 6:86e89a0369b9 100 char* pathPtr = strchr(hostPtr, '/');
donatien 6:86e89a0369b9 101 if( hostLen == 0 )
donatien 6:86e89a0369b9 102 {
donatien 6:86e89a0369b9 103 hostLen = pathPtr - hostPtr;
donatien 6:86e89a0369b9 104 }
samux 3:9589afa4712e 105
donatien 6:86e89a0369b9 106 if( maxHostLen < hostLen + 1 ) //including NULL-terminating char
donatien 6:86e89a0369b9 107 {
donatien 6:86e89a0369b9 108 WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1);
donatien 6:86e89a0369b9 109 return -1;
donatien 6:86e89a0369b9 110 }
donatien 6:86e89a0369b9 111 memcpy(host, hostPtr, hostLen);
donatien 6:86e89a0369b9 112 host[hostLen] = '\0';
samux 3:9589afa4712e 113
donatien 6:86e89a0369b9 114 size_t pathLen;
donatien 6:86e89a0369b9 115 char* fragmentPtr = strchr(hostPtr, '#');
donatien 6:86e89a0369b9 116 if(fragmentPtr != NULL)
donatien 6:86e89a0369b9 117 {
donatien 6:86e89a0369b9 118 pathLen = fragmentPtr - pathPtr;
donatien 6:86e89a0369b9 119 }
donatien 6:86e89a0369b9 120 else
donatien 6:86e89a0369b9 121 {
donatien 6:86e89a0369b9 122 pathLen = strlen(pathPtr);
donatien 6:86e89a0369b9 123 }
donatien 6:86e89a0369b9 124
donatien 6:86e89a0369b9 125 if( maxPathLen < pathLen + 1 ) //including NULL-terminating char
donatien 6:86e89a0369b9 126 {
donatien 6:86e89a0369b9 127 WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1);
donatien 6:86e89a0369b9 128 return -1;
donatien 6:86e89a0369b9 129 }
donatien 6:86e89a0369b9 130 memcpy(path, pathPtr, pathLen);
donatien 6:86e89a0369b9 131 path[pathLen] = '\0';
donatien 6:86e89a0369b9 132
donatien 6:86e89a0369b9 133 return 0;
samux 3:9589afa4712e 134 }
samux 3:9589afa4712e 135
samux 3:9589afa4712e 136
samux 3:9589afa4712e 137 bool Websocket::connect() {
samux 3:9589afa4712e 138 char cmd[200];
samux 3:9589afa4712e 139
donatien 6:86e89a0369b9 140 while (socket.connect(host, port) < 0) {
donatien 6:86e89a0369b9 141 ERR("Unable to connect to (%s) on port (%d)", host, port);
samux 3:9589afa4712e 142 wait(0.2);
donatien 6:86e89a0369b9 143 return false;
samux 3:9589afa4712e 144 }
defrost 12:0979caf96fa6 145 INFO("TCP/IP Connection established, upgrading protocol...");
samux 3:9589afa4712e 146 // sent http header to upgrade to the ws protocol
donatien 6:86e89a0369b9 147 sprintf(cmd, "GET %s HTTP/1.1\r\n", path);
samux 3:9589afa4712e 148 write(cmd, strlen(cmd));
donatien 6:86e89a0369b9 149
donatien 6:86e89a0369b9 150 sprintf(cmd, "Host: %s:%d\r\n", host, port);
samux 3:9589afa4712e 151 write(cmd, strlen(cmd));
samux 3:9589afa4712e 152
samux 3:9589afa4712e 153 sprintf(cmd, "Upgrade: WebSocket\r\n");
samux 3:9589afa4712e 154 write(cmd, strlen(cmd));
samux 3:9589afa4712e 155
samux 3:9589afa4712e 156 sprintf(cmd, "Connection: Upgrade\r\n");
samux 3:9589afa4712e 157 write(cmd, strlen(cmd));
samux 3:9589afa4712e 158
samux 3:9589afa4712e 159 sprintf(cmd, "Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n");
samux 3:9589afa4712e 160 write(cmd, strlen(cmd));
samux 3:9589afa4712e 161
samux 3:9589afa4712e 162 sprintf(cmd, "Sec-WebSocket-Version: 13\r\n\r\n");
samux 3:9589afa4712e 163 int ret = write(cmd, strlen(cmd));
samux 3:9589afa4712e 164 if (ret != strlen(cmd)) {
samux 3:9589afa4712e 165 close();
samux 3:9589afa4712e 166 ERR("Could not send request");
samux 3:9589afa4712e 167 return false;
samux 3:9589afa4712e 168 }
samux 3:9589afa4712e 169
samux 3:9589afa4712e 170 ret = read(cmd, 200, 100);
samux 3:9589afa4712e 171 if (ret < 0) {
samux 3:9589afa4712e 172 close();
samux 3:9589afa4712e 173 ERR("Could not receive answer\r\n");
samux 3:9589afa4712e 174 return false;
samux 3:9589afa4712e 175 }
samux 3:9589afa4712e 176
samux 3:9589afa4712e 177 cmd[ret] = '\0';
samux 3:9589afa4712e 178 DBG("recv: %s\r\n", cmd);
samux 3:9589afa4712e 179
samux 3:9589afa4712e 180 if ( strstr(cmd, "DdLWT/1JcX+nQFHebYP+rqEx5xI=") == NULL ) {
samux 3:9589afa4712e 181 ERR("Wrong answer from server, got \"%s\" instead\r\n", cmd);
samux 3:9589afa4712e 182 do {
samux 3:9589afa4712e 183 ret = read(cmd, 200, 100);
samux 3:9589afa4712e 184 if (ret < 0) {
samux 3:9589afa4712e 185 ERR("Could not receive answer\r\n");
samux 3:9589afa4712e 186 return false;
samux 3:9589afa4712e 187 }
samux 3:9589afa4712e 188 cmd[ret] = '\0';
defrost 12:0979caf96fa6 189 DBG("%s",cmd);
samux 3:9589afa4712e 190 } while (ret > 0);
samux 3:9589afa4712e 191 close();
samux 3:9589afa4712e 192 return false;
samux 3:9589afa4712e 193 }
samux 3:9589afa4712e 194
defrost 12:0979caf96fa6 195 INFO("\r\n\thost: %s\r\n\tpath: %s\r\n\tport: %d\r\n\r\n", host, path, port);
samux 3:9589afa4712e 196 return true;
samux 3:9589afa4712e 197 }
samux 3:9589afa4712e 198
samux 4:466f90b7849a 199 int Websocket::sendLength(uint32_t len, char * msg) {
samux 3:9589afa4712e 200
samux 3:9589afa4712e 201 if (len < 126) {
samux 4:466f90b7849a 202 msg[0] = len | (1<<7);
samux 3:9589afa4712e 203 return 1;
samux 3:9589afa4712e 204 } else if (len < 65535) {
samux 4:466f90b7849a 205 msg[0] = 126 | (1<<7);
samux 4:466f90b7849a 206 msg[1] = (len >> 8) & 0xff;
samux 4:466f90b7849a 207 msg[2] = len & 0xff;
samux 3:9589afa4712e 208 return 3;
samux 3:9589afa4712e 209 } else {
samux 4:466f90b7849a 210 msg[0] = 127 | (1<<7);
samux 3:9589afa4712e 211 for (int i = 0; i < 8; i++) {
samux 4:466f90b7849a 212 msg[i+1] = (len >> i*8) & 0xff;
samux 3:9589afa4712e 213 }
samux 3:9589afa4712e 214 return 9;
samux 3:9589afa4712e 215 }
samux 3:9589afa4712e 216 }
samux 3:9589afa4712e 217
samux 3:9589afa4712e 218 int Websocket::readChar(char * pC, bool block) {
samux 3:9589afa4712e 219 return read(pC, 1, 1);
samux 3:9589afa4712e 220 }
samux 3:9589afa4712e 221
samux 4:466f90b7849a 222 int Websocket::sendOpcode(uint8_t opcode, char * msg) {
samux 4:466f90b7849a 223 msg[0] = 0x80 | (opcode & 0x0f);
samux 4:466f90b7849a 224 return 1;
samux 3:9589afa4712e 225 }
samux 3:9589afa4712e 226
samux 4:466f90b7849a 227 int Websocket::sendMask(char * msg) {
samux 3:9589afa4712e 228 for (int i = 0; i < 4; i++) {
samux 4:466f90b7849a 229 msg[i] = 0;
samux 3:9589afa4712e 230 }
samux 3:9589afa4712e 231 return 4;
samux 3:9589afa4712e 232 }
samux 3:9589afa4712e 233
samux 3:9589afa4712e 234 int Websocket::send(char * str) {
samux 4:466f90b7849a 235 char msg[strlen(str) + 15];
samux 4:466f90b7849a 236 int idx = 0;
samux 4:466f90b7849a 237 idx = sendOpcode(0x01, msg);
samux 4:466f90b7849a 238 idx += sendLength(strlen(str), msg + idx);
samux 4:466f90b7849a 239 idx += sendMask(msg + idx);
samux 4:466f90b7849a 240 memcpy(msg+idx, str, strlen(str));
samux 4:466f90b7849a 241 int res = write(msg, idx + strlen(str));
samux 3:9589afa4712e 242 return res;
samux 3:9589afa4712e 243 }
samux 3:9589afa4712e 244
samux 3:9589afa4712e 245
defrost 13:152b70450615 246 bool Websocket::readmsg(char * message) {
samux 3:9589afa4712e 247 int i = 0;
samux 3:9589afa4712e 248 uint32_t len_msg;
samux 3:9589afa4712e 249 char opcode = 0;
samux 3:9589afa4712e 250 char c;
samux 3:9589afa4712e 251 char mask[4] = {0, 0, 0, 0};
samux 3:9589afa4712e 252 bool is_masked = false;
samux 3:9589afa4712e 253 Timer tmr;
samux 3:9589afa4712e 254
samux 3:9589afa4712e 255 // read the opcode
samux 3:9589afa4712e 256 tmr.start();
samux 3:9589afa4712e 257 while (true) {
samux 3:9589afa4712e 258 if (tmr.read() > 3) {
samux 3:9589afa4712e 259 DBG("timeout ws\r\n");
samux 3:9589afa4712e 260 return false;
samux 3:9589afa4712e 261 }
donatien 5:bb09d7a6c92f 262
donatien 5:bb09d7a6c92f 263 if(!socket.is_connected())
donatien 5:bb09d7a6c92f 264 {
donatien 5:bb09d7a6c92f 265 WARN("Connection was closed by server");
donatien 5:bb09d7a6c92f 266 return false;
donatien 5:bb09d7a6c92f 267 }
samux 3:9589afa4712e 268
samux 3:9589afa4712e 269 socket.set_blocking(false, 1);
samux 3:9589afa4712e 270 if (socket.receive(&opcode, 1) != 1) {
samux 3:9589afa4712e 271 socket.set_blocking(false, 2000);
samux 3:9589afa4712e 272 return false;
samux 3:9589afa4712e 273 }
samux 3:9589afa4712e 274
samux 3:9589afa4712e 275 socket.set_blocking(false, 2000);
samux 3:9589afa4712e 276
samux 3:9589afa4712e 277 if (opcode == 0x81)
samux 3:9589afa4712e 278 break;
samux 3:9589afa4712e 279 }
defrost 12:0979caf96fa6 280 DBG("opcode: 0x%X", opcode);
samux 3:9589afa4712e 281
samux 3:9589afa4712e 282 readChar(&c);
samux 3:9589afa4712e 283 len_msg = c & 0x7f;
samux 3:9589afa4712e 284 is_masked = c & 0x80;
samux 3:9589afa4712e 285 if (len_msg == 126) {
samux 3:9589afa4712e 286 readChar(&c);
samux 3:9589afa4712e 287 len_msg = c << 8;
samux 3:9589afa4712e 288 readChar(&c);
samux 3:9589afa4712e 289 len_msg += c;
samux 3:9589afa4712e 290 } else if (len_msg == 127) {
samux 3:9589afa4712e 291 len_msg = 0;
samux 3:9589afa4712e 292 for (int i = 0; i < 8; i++) {
samux 3:9589afa4712e 293 readChar(&c);
samux 3:9589afa4712e 294 len_msg += (c << (7-i)*8);
samux 3:9589afa4712e 295 }
samux 3:9589afa4712e 296 }
samux 3:9589afa4712e 297
samux 3:9589afa4712e 298 if (len_msg == 0) {
samux 3:9589afa4712e 299 return false;
samux 3:9589afa4712e 300 }
defrost 12:0979caf96fa6 301 DBG("length: %d", len_msg);
samux 3:9589afa4712e 302
samux 3:9589afa4712e 303 if (is_masked) {
samux 3:9589afa4712e 304 for (i = 0; i < 4; i++)
samux 3:9589afa4712e 305 readChar(&c);
samux 3:9589afa4712e 306 mask[i] = c;
samux 3:9589afa4712e 307 }
defrost 12:0979caf96fa6 308
defrost 12:0979caf96fa6 309 DBG("Done readChar.");
samux 3:9589afa4712e 310
samux 3:9589afa4712e 311 int nb = read(message, len_msg, len_msg);
defrost 12:0979caf96fa6 312 DBG("Done nb:%d = read(message:%s, len_msg:%d, len_msg:%d)", nb, message, len_msg, len_msg);
samux 3:9589afa4712e 313 if (nb != len_msg)
samux 3:9589afa4712e 314 return false;
samux 3:9589afa4712e 315
samux 3:9589afa4712e 316 for (i = 0; i < len_msg; i++) {
samux 3:9589afa4712e 317 message[i] = message[i] ^ mask[i % 4];
samux 3:9589afa4712e 318 }
samux 3:9589afa4712e 319
samux 3:9589afa4712e 320 message[len_msg] = '\0';
defrost 12:0979caf96fa6 321 DBG("Websocket::read() returning true, message:%s", message);
samux 3:9589afa4712e 322 return true;
samux 3:9589afa4712e 323 }
samux 3:9589afa4712e 324
samux 3:9589afa4712e 325 bool Websocket::close() {
samux 3:9589afa4712e 326 if (!is_connected())
samux 3:9589afa4712e 327 return false;
samux 3:9589afa4712e 328
samux 3:9589afa4712e 329 int ret = socket.close();
samux 3:9589afa4712e 330 if (ret < 0) {
samux 3:9589afa4712e 331 ERR("Could not disconnect");
samux 3:9589afa4712e 332 return false;
samux 3:9589afa4712e 333 }
samux 3:9589afa4712e 334 return true;
samux 3:9589afa4712e 335 }
samux 3:9589afa4712e 336
samux 3:9589afa4712e 337 bool Websocket::is_connected() {
samux 3:9589afa4712e 338 return socket.is_connected();
samux 3:9589afa4712e 339 }
samux 3:9589afa4712e 340
donatien 6:86e89a0369b9 341 char* Websocket::getPath() {
samux 3:9589afa4712e 342 return path;
samux 3:9589afa4712e 343 }
samux 3:9589afa4712e 344
samux 3:9589afa4712e 345 int Websocket::write(char * str, int len) {
samux 3:9589afa4712e 346 int res = 0, idx = 0;
samux 3:9589afa4712e 347
samux 3:9589afa4712e 348 for (int j = 0; j < MAX_TRY_WRITE; j++) {
donatien 5:bb09d7a6c92f 349
donatien 5:bb09d7a6c92f 350 if(!socket.is_connected())
donatien 5:bb09d7a6c92f 351 {
donatien 5:bb09d7a6c92f 352 WARN("Connection was closed by server");
donatien 5:bb09d7a6c92f 353 break;
donatien 5:bb09d7a6c92f 354 }
samux 3:9589afa4712e 355
samux 3:9589afa4712e 356 if ((res = socket.send_all(str + idx, len - idx)) == -1)
samux 3:9589afa4712e 357 continue;
samux 3:9589afa4712e 358
samux 3:9589afa4712e 359 idx += res;
samux 3:9589afa4712e 360
samux 3:9589afa4712e 361 if (idx == len)
samux 3:9589afa4712e 362 return len;
samux 3:9589afa4712e 363 }
defrost 10:fc40c56e8a3c 364 WARN("Websocket::write.idx on exit: %d", idx);
samux 3:9589afa4712e 365 return (idx == 0) ? -1 : idx;
samux 3:9589afa4712e 366 }
samux 3:9589afa4712e 367
samux 3:9589afa4712e 368 int Websocket::read(char * str, int len, int min_len) {
samux 3:9589afa4712e 369 int res = 0, idx = 0;
samux 3:9589afa4712e 370
samux 3:9589afa4712e 371 for (int j = 0; j < MAX_TRY_WRITE; j++) {
samux 3:9589afa4712e 372
samux 3:9589afa4712e 373 if ((res = socket.receive_all(str + idx, len - idx)) == -1)
samux 3:9589afa4712e 374 continue;
samux 3:9589afa4712e 375 idx += res;
defrost 12:0979caf96fa6 376 DBG("In read(* str, len, min_len), res = %d, idx = %d", res, idx);
samux 3:9589afa4712e 377 if (idx == len || (min_len != -1 && idx > min_len))
samux 3:9589afa4712e 378 return idx;
samux 3:9589afa4712e 379 }
samux 3:9589afa4712e 380
samux 3:9589afa4712e 381 return (idx == 0) ? -1 : idx;
samux 3:9589afa4712e 382 }