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:
Wed Mar 30 09:05:44 2016 +0000
Revision:
10:fc40c56e8a3c
Parent:
9:da7c2b9480c5
Child:
11:85bff70bab45
- Added debug

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