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 Mar 31 12:43:30 2016 +0000
Revision:
12:0979caf96fa6
Parent:
11:85bff70bab45
Child:
13:152b70450615
- Added debug messages

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