WebSocket client library

Committer:
samux
Date:
Wed May 09 11:45:54 2012 +0000
Revision:
25:2214f1e5d4a3
Parent:
24:15112fd8ad45

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:21fe3b05249f 1 #include "Websocket.h"
samux 6:01a1eb7c0145 2 #include <string>
samux 0:21fe3b05249f 3
samux 24:15112fd8ad45 4 //#define DEBUG
samux 20:bb9d7ba48e6e 5
samux 6:01a1eb7c0145 6 Websocket::Websocket(char * url, Wifly * wifi) {
samux 9:9fa055ed54b4 7 this->wifi = wifi;
samux 16:d4518b50f653 8 netif = WIF;
samux 16:d4518b50f653 9 fillFields(url);
samux 6:01a1eb7c0145 10 }
samux 6:01a1eb7c0145 11
samux 13:3b058372cad9 12
samux 22:f4aac491ea26 13 #ifdef TARGET_LPC1768
samux 13:3b058372cad9 14 Websocket::Websocket(char * url) {
samux 13:3b058372cad9 15 server_ip = NULL;
samux 16:d4518b50f653 16 netif = ETH;
samux 13:3b058372cad9 17 eth_writeable = false;
samux 13:3b058372cad9 18 eth_readable = false;
samux 13:3b058372cad9 19 eth_connected = false;
samux 13:3b058372cad9 20 response_server_eth = false;
samux 16:d4518b50f653 21 new_msg = false;
samux 16:d4518b50f653 22 fillFields(url);
samux 14:c5ac3e26998f 23
samux 16:d4518b50f653 24 eth = new EthernetNetIf();
samux 16:d4518b50f653 25 sock = new TCPSocket();
samux 14:c5ac3e26998f 26
samux 16:d4518b50f653 27 EthernetErr ethErr = eth->setup();
samux 16:d4518b50f653 28 #ifdef DEBUG
samux 14:c5ac3e26998f 29 if (ethErr) {
samux 14:c5ac3e26998f 30 printf("\r\nERROR %d in setup.\r\n", ethErr);
samux 14:c5ac3e26998f 31 }
samux 16:d4518b50f653 32 #endif
samux 14:c5ac3e26998f 33
samux 14:c5ac3e26998f 34 //we must use dnsresolver to find the ip address
samux 14:c5ac3e26998f 35 if (server_ip == NULL) {
samux 14:c5ac3e26998f 36 DNSResolver dr;
samux 16:d4518b50f653 37 server_ip = new IpAddr();
samux 17:1e339933c97a 38 *server_ip = dr.resolveName(ip_domain.c_str());
samux 16:d4518b50f653 39 #ifdef DEBUG
samux 22:f4aac491ea26 40 printf("\r\nserver with dns=%d.%d.%d.%d\r\n", (*server_ip)[0], (*server_ip)[1], (*server_ip)[2], (*server_ip)[3]);
samux 16:d4518b50f653 41 #endif
samux 16:d4518b50f653 42
samux 14:c5ac3e26998f 43 }
samux 15:79bfbc0ad6bc 44
samux 16:d4518b50f653 45 IpAddr ipt = eth->getIp();
samux 16:d4518b50f653 46 #ifdef DEBUG
samux 14:c5ac3e26998f 47 printf("\r\nmbed IP Address is %d.%d.%d.%d\r\n", ipt[0], ipt[1], ipt[2], ipt[3]);
samux 16:d4518b50f653 48 #endif
samux 15:79bfbc0ad6bc 49
samux 16:d4518b50f653 50 sock->setOnEvent(this, &Websocket::onTCPSocketEvent);
samux 13:3b058372cad9 51 }
samux 22:f4aac491ea26 52 #endif //target
samux 13:3b058372cad9 53
samux 13:3b058372cad9 54
samux 16:d4518b50f653 55 void Websocket::fillFields(char * url) {
samux 0:21fe3b05249f 56 char *res = NULL;
samux 6:01a1eb7c0145 57 char *res1 = NULL;
samux 6:01a1eb7c0145 58
samux 16:d4518b50f653 59 char buf[50];
samux 0:21fe3b05249f 60 strcpy(buf, url);
samux 6:01a1eb7c0145 61
samux 0:21fe3b05249f 62 res = strtok(buf, ":");
samux 9:9fa055ed54b4 63 if (strcmp(res, "ws")) {
samux 16:d4518b50f653 64 #ifdef DEBUG
samux 6:01a1eb7c0145 65 printf("\r\nFormat error: please use: \"ws://ip-or-domain[:port]/path\"\r\n\r\n");
samux 16:d4518b50f653 66 #endif
samux 9:9fa055ed54b4 67 } else {
samux 6:01a1eb7c0145 68 //ip_domain and port
samux 6:01a1eb7c0145 69 res = strtok(NULL, "/");
samux 6:01a1eb7c0145 70
samux 6:01a1eb7c0145 71 //path
samux 6:01a1eb7c0145 72 res1 = strtok(NULL, " ");
samux 6:01a1eb7c0145 73 if (res1 != NULL) {
samux 17:1e339933c97a 74 path = res1;
samux 6:01a1eb7c0145 75 }
samux 6:01a1eb7c0145 76
samux 6:01a1eb7c0145 77 //ip_domain
samux 6:01a1eb7c0145 78 res = strtok(res, ":");
samux 9:9fa055ed54b4 79
samux 7:b15978708360 80 //port
samux 7:b15978708360 81 res1 = strtok(NULL, " ");
samux 7:b15978708360 82 //port
samux 7:b15978708360 83 if (res1 != NULL) {
samux 17:1e339933c97a 84 port = res1;
samux 7:b15978708360 85 } else {
samux 17:1e339933c97a 86 port = "80";
samux 7:b15978708360 87 }
samux 9:9fa055ed54b4 88
samux 6:01a1eb7c0145 89 if (res != NULL) {
samux 17:1e339933c97a 90 ip_domain = res;
samux 16:d4518b50f653 91
samux 6:01a1eb7c0145 92 //if we use ethernet, we must decode ip address or use dnsresolver
samux 22:f4aac491ea26 93 #ifdef TARGET_LPC1768
samux 16:d4518b50f653 94 if (netif == ETH) {
samux 6:01a1eb7c0145 95 strcpy(buf, res);
samux 14:c5ac3e26998f 96
samux 6:01a1eb7c0145 97 //we try to decode the ip address
samux 6:01a1eb7c0145 98 if (buf[0] >= '0' && buf[0] <= '9') {
samux 6:01a1eb7c0145 99 res = strtok(buf, ".");
samux 6:01a1eb7c0145 100 int i = 0;
samux 6:01a1eb7c0145 101 int ip[4];
samux 6:01a1eb7c0145 102 while (res != NULL) {
samux 6:01a1eb7c0145 103 ip[i] = atoi(res);
samux 6:01a1eb7c0145 104 res = strtok(NULL, ".");
samux 6:01a1eb7c0145 105 i++;
samux 6:01a1eb7c0145 106 }
samux 16:d4518b50f653 107 server_ip = new IpAddr(ip[0], ip[1], ip[2], ip[3]);
samux 16:d4518b50f653 108 #ifdef DEBUG
samux 16:d4518b50f653 109 printf("server without dns=%i.%i.%i.%i\n",(*server_ip)[0],(*server_ip)[1],(*server_ip)[2],(*server_ip)[3]);
samux 16:d4518b50f653 110 #endif
samux 6:01a1eb7c0145 111 }
samux 6:01a1eb7c0145 112 }
samux 22:f4aac491ea26 113 #endif //target
samux 0:21fe3b05249f 114 }
samux 0:21fe3b05249f 115 }
samux 0:21fe3b05249f 116 }
samux 0:21fe3b05249f 117
samux 6:01a1eb7c0145 118
samux 6:01a1eb7c0145 119 bool Websocket::connect() {
samux 0:21fe3b05249f 120 char cmd[50];
samux 16:d4518b50f653 121 if (netif == WIF) {
samux 6:01a1eb7c0145 122 //enter in cmd mode
samux 23:80bffaf28bef 123 wifi->exit();
samux 22:f4aac491ea26 124 while (!wifi->cmdMode()) {
samux 16:d4518b50f653 125 #ifdef DEBUG
samux 6:01a1eb7c0145 126 printf("cannot enter in CMD mode\r\n");
samux 16:d4518b50f653 127 #endif
samux 22:f4aac491ea26 128 wifi->send("a\r\n");
samux 22:f4aac491ea26 129 wait(0.2);
samux 6:01a1eb7c0145 130 wifi->exit();
samux 22:f4aac491ea26 131 if (!wifi->cmdMode())
samux 22:f4aac491ea26 132 return false;
samux 22:f4aac491ea26 133 }
samux 22:f4aac491ea26 134
samux 22:f4aac491ea26 135 if (!wifi->send("set comm remote 0\r\n", "AOK")) {
samux 22:f4aac491ea26 136 #ifdef DEBUG
samux 22:f4aac491ea26 137 printf("Websocket::connect(): cannot set empty remote string\r\n");
samux 22:f4aac491ea26 138 #endif
samux 22:f4aac491ea26 139 return false;
samux 6:01a1eb7c0145 140 }
samux 6:01a1eb7c0145 141
samux 6:01a1eb7c0145 142 //open the connection
samux 17:1e339933c97a 143 sprintf(cmd, "open %s %s\r\n", ip_domain.c_str(), port.c_str());
samux 25:2214f1e5d4a3 144 if (!wifi->send(cmd, "OPEN")) {
samux 25:2214f1e5d4a3 145 if (wifi->send(cmd, "nected")) {
samux 22:f4aac491ea26 146 #ifdef DEBUG
samux 25:2214f1e5d4a3 147 printf("will try to close the conn\r\n");
samux 22:f4aac491ea26 148 #endif
samux 25:2214f1e5d4a3 149 if (!wifi->send("close\r", "CLOS")) {
samux 25:2214f1e5d4a3 150 return false;
samux 25:2214f1e5d4a3 151 }
samux 25:2214f1e5d4a3 152 if (!wifi->send(cmd, "OPEN"))
samux 25:2214f1e5d4a3 153 return false;
samux 25:2214f1e5d4a3 154 } else {
samux 22:f4aac491ea26 155 return false;
samux 22:f4aac491ea26 156 }
samux 22:f4aac491ea26 157 }
samux 6:01a1eb7c0145 158
samux 6:01a1eb7c0145 159 //send websocket HTTP header
samux 17:1e339933c97a 160 sprintf(cmd, "GET /%s HTTP/1.1\r\n", path.c_str());
samux 22:f4aac491ea26 161 wifi->send(cmd);
samux 22:f4aac491ea26 162 wifi->send("Upgrade: websocket\r\n");
samux 22:f4aac491ea26 163 wifi->send("Connection: Upgrade\r\n");
samux 17:1e339933c97a 164 sprintf(cmd, "Host: %s:%s\r\n", ip_domain.c_str(), port.c_str());
samux 22:f4aac491ea26 165 wifi->send(cmd);
samux 22:f4aac491ea26 166 wifi->send("Origin: null\r\n");
samux 22:f4aac491ea26 167 wifi->send("Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n");
samux 22:f4aac491ea26 168 if (!wifi->send("Sec-WebSocket-Version: 13\r\n\r\n", "DdLWT/1JcX+nQFHebYP+rqEx5xI="))
samux 22:f4aac491ea26 169 return false;
samux 6:01a1eb7c0145 170
samux 22:f4aac491ea26 171 wifi->flush();
samux 22:f4aac491ea26 172
samux 22:f4aac491ea26 173 //printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain.c_str(), this->path.c_str(), this->port.c_str());
samux 6:01a1eb7c0145 174 return true;
samux 22:f4aac491ea26 175 }
samux 22:f4aac491ea26 176 #ifdef TARGET_LPC1768
samux 22:f4aac491ea26 177 else if (netif == ETH) {
samux 17:1e339933c97a 178 Host server (*server_ip, atoi(port.c_str()));
samux 16:d4518b50f653 179 sock->close();
samux 16:d4518b50f653 180 TCPSocketErr bindErr = sock->connect(server);
samux 6:01a1eb7c0145 181 if (bindErr) {
samux 16:d4518b50f653 182 #ifdef DEBUG
samux 14:c5ac3e26998f 183 printf("\r\nERROR binderr: %d\r\n", bindErr);
samux 16:d4518b50f653 184 #endif
samux 6:01a1eb7c0145 185 return false;
samux 6:01a1eb7c0145 186 }
samux 6:01a1eb7c0145 187
samux 6:01a1eb7c0145 188 Timer tmr;
samux 6:01a1eb7c0145 189 tmr.start();
samux 15:79bfbc0ad6bc 190
samux 14:c5ac3e26998f 191 Timer stop;
samux 14:c5ac3e26998f 192 stop.start();
samux 0:21fe3b05249f 193
samux 14:c5ac3e26998f 194 int i = 0;
samux 6:01a1eb7c0145 195 while (true) {
samux 6:01a1eb7c0145 196 Net::poll();
samux 15:79bfbc0ad6bc 197 if (stop.read() > 3)
samux 14:c5ac3e26998f 198 return false;
samux 22:f4aac491ea26 199 if (tmr.read() > 0.05) {
samux 6:01a1eb7c0145 200 tmr.reset();
samux 6:01a1eb7c0145 201 if (eth_connected) {
samux 6:01a1eb7c0145 202 switch (i) {
samux 6:01a1eb7c0145 203 case 0:
samux 17:1e339933c97a 204 sprintf(cmd, "GET /%s HTTP/1.1\r\n", path.c_str());
samux 16:d4518b50f653 205 sock->send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 206 i++;
samux 6:01a1eb7c0145 207 break;
samux 6:01a1eb7c0145 208 case 1:
samux 17:1e339933c97a 209 sprintf(cmd, "Host: %s:%s\r\n", ip_domain.c_str(), port.c_str());
samux 16:d4518b50f653 210 sock->send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 211 i++;
samux 6:01a1eb7c0145 212 break;
samux 6:01a1eb7c0145 213 case 2:
samux 6:01a1eb7c0145 214 sprintf(cmd, "Upgrade: WebSocket\r\n");
samux 16:d4518b50f653 215 sock->send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 216 i++;
samux 6:01a1eb7c0145 217 break;
samux 6:01a1eb7c0145 218 case 3:
samux 22:f4aac491ea26 219 sprintf(cmd, "Origin: null\r\n");
samux 16:d4518b50f653 220 sock->send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 221 i++;
samux 6:01a1eb7c0145 222 break;
samux 6:01a1eb7c0145 223 case 4:
samux 6:01a1eb7c0145 224 sprintf(cmd, "Connection: Upgrade\r\n");
samux 16:d4518b50f653 225 sock->send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 226 i++;
samux 6:01a1eb7c0145 227 break;
samux 6:01a1eb7c0145 228 case 5:
samux 22:f4aac491ea26 229 sprintf(cmd, "Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n");
samux 16:d4518b50f653 230 sock->send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 231 i++;
samux 6:01a1eb7c0145 232 break;
samux 6:01a1eb7c0145 233 case 6:
samux 22:f4aac491ea26 234 sprintf(cmd, "Sec-WebSocket-Version: 13\r\n\r\n");
samux 16:d4518b50f653 235 sock->send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 236 i++;
samux 6:01a1eb7c0145 237 break;
samux 6:01a1eb7c0145 238 case 7:
samux 9:9fa055ed54b4 239 if (response_server_eth)
samux 9:9fa055ed54b4 240 i++;
samux 9:9fa055ed54b4 241 else
samux 9:9fa055ed54b4 242 break;
samux 9:9fa055ed54b4 243
samux 6:01a1eb7c0145 244 default:
samux 6:01a1eb7c0145 245 break;
samux 6:01a1eb7c0145 246 }
samux 6:01a1eb7c0145 247 }
samux 22:f4aac491ea26 248 if (i==8) {
samux 22:f4aac491ea26 249 #ifdef DEBUG
samux 17:1e339933c97a 250 printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain.c_str(), this->path.c_str(), this->port.c_str());
samux 22:f4aac491ea26 251 #endif
samux 6:01a1eb7c0145 252 return true;
samux 6:01a1eb7c0145 253 }
samux 6:01a1eb7c0145 254 }
samux 6:01a1eb7c0145 255 }
samux 0:21fe3b05249f 256 }
samux 22:f4aac491ea26 257 #endif //target
samux 16:d4518b50f653 258 //the program shouldn't be here
samux 16:d4518b50f653 259 return false;
samux 0:21fe3b05249f 260 }
samux 0:21fe3b05249f 261
samux 22:f4aac491ea26 262 void Websocket::sendLength(uint32_t len) {
samux 22:f4aac491ea26 263 if (len < 126) {
samux 22:f4aac491ea26 264 sendChar(len | (1<<7));
samux 22:f4aac491ea26 265 } else if (len < 65535) {
samux 22:f4aac491ea26 266 sendChar(126 | (1<<7));
samux 23:80bffaf28bef 267 sendChar((len >> 8) & 0xff);
samux 22:f4aac491ea26 268 sendChar(len & 0xff);
samux 22:f4aac491ea26 269 } else {
samux 22:f4aac491ea26 270 sendChar(127 | (1<<7));
samux 22:f4aac491ea26 271 for (int i = 0; i < 8; i++) {
samux 22:f4aac491ea26 272 sendChar((len >> i*8) & 0xff);
samux 22:f4aac491ea26 273 }
samux 6:01a1eb7c0145 274 }
samux 0:21fe3b05249f 275 }
samux 0:21fe3b05249f 276
samux 22:f4aac491ea26 277 void Websocket::sendChar(uint8_t c) {
samux 22:f4aac491ea26 278 if (netif == WIF) {
samux 22:f4aac491ea26 279 wifi->putc(c);
samux 22:f4aac491ea26 280 }
samux 22:f4aac491ea26 281 #ifdef TARGET_LPC1768
samux 22:f4aac491ea26 282 else if (netif == ETH) {
samux 22:f4aac491ea26 283 Net::poll();
samux 22:f4aac491ea26 284 sock->send((const char *)&c, 1);
samux 22:f4aac491ea26 285 }
samux 22:f4aac491ea26 286 #endif
samux 22:f4aac491ea26 287 }
samux 22:f4aac491ea26 288
samux 22:f4aac491ea26 289 void Websocket::sendOpcode(uint8_t opcode) {
samux 22:f4aac491ea26 290 sendChar(0x80 | (opcode & 0x0f));
samux 22:f4aac491ea26 291 }
samux 22:f4aac491ea26 292
samux 22:f4aac491ea26 293 void Websocket::sendMask() {
samux 22:f4aac491ea26 294 for (int i = 0; i < 4; i++) {
samux 22:f4aac491ea26 295 sendChar(0);
samux 22:f4aac491ea26 296 }
samux 22:f4aac491ea26 297 }
samux 22:f4aac491ea26 298
samux 22:f4aac491ea26 299 void Websocket::send(char * str) {
samux 22:f4aac491ea26 300 sendOpcode(0x01);
samux 22:f4aac491ea26 301 sendLength(strlen(str));
samux 22:f4aac491ea26 302 sendMask();
samux 22:f4aac491ea26 303 if (netif == WIF) {
samux 25:2214f1e5d4a3 304 wifi->send(str, NULL);
samux 22:f4aac491ea26 305 }
samux 22:f4aac491ea26 306 #ifdef TARGET_LPC1768
samux 22:f4aac491ea26 307 else if (netif == ETH) {
samux 22:f4aac491ea26 308 Net::poll();
samux 22:f4aac491ea26 309 sock->send(str, strlen(str));
samux 22:f4aac491ea26 310 }
samux 22:f4aac491ea26 311 #endif //target
samux 22:f4aac491ea26 312 }
samux 22:f4aac491ea26 313
samux 22:f4aac491ea26 314
samux 22:f4aac491ea26 315
samux 6:01a1eb7c0145 316 bool Websocket::read(char * message) {
samux 0:21fe3b05249f 317 int i = 0;
samux 22:f4aac491ea26 318 int length_buffer = 0;
samux 23:80bffaf28bef 319 uint64_t len_msg;
samux 22:f4aac491ea26 320 char opcode = 0;
samux 22:f4aac491ea26 321 char mask[4] = {0, 0, 0, 0};
samux 22:f4aac491ea26 322 Timer tmr;
samux 6:01a1eb7c0145 323
samux 16:d4518b50f653 324 if (netif == WIF) {
samux 22:f4aac491ea26 325 length_buffer = wifi->readable();
samux 22:f4aac491ea26 326
samux 22:f4aac491ea26 327 if (length_buffer > 1) {
samux 22:f4aac491ea26 328 // read the opcode
samux 22:f4aac491ea26 329 tmr.start();
samux 22:f4aac491ea26 330 while (true) {
samux 22:f4aac491ea26 331 if (tmr.read() > 3) {
samux 22:f4aac491ea26 332 return false;
samux 22:f4aac491ea26 333 }
samux 22:f4aac491ea26 334 if (wifi->readable()) {
samux 22:f4aac491ea26 335 opcode = wifi->getc();
samux 22:f4aac491ea26 336 }
samux 22:f4aac491ea26 337 if (opcode == 0x81) {
samux 22:f4aac491ea26 338 break;
samux 22:f4aac491ea26 339 }
samux 22:f4aac491ea26 340 }
samux 22:f4aac491ea26 341 #ifdef DEBUG
samux 22:f4aac491ea26 342 printf("opcode: 0x%X\r\n", opcode);
samux 22:f4aac491ea26 343 #endif
samux 22:f4aac491ea26 344 len_msg = wifi->getc() & 0x7f;
samux 22:f4aac491ea26 345 if (len_msg == 126) {
samux 23:80bffaf28bef 346 len_msg = (wifi->getc() << 8);
samux 23:80bffaf28bef 347 len_msg += wifi->getc();
samux 22:f4aac491ea26 348 } else if (len_msg == 127) {
samux 22:f4aac491ea26 349 len_msg = 0;
samux 22:f4aac491ea26 350 for (int i = 0; i < 8; i++) {
samux 23:80bffaf28bef 351 len_msg += (wifi->getc() << (7-i)*8);
samux 22:f4aac491ea26 352 }
samux 22:f4aac491ea26 353 }
samux 23:80bffaf28bef 354 if (len_msg == 0) {
samux 22:f4aac491ea26 355 return false;
samux 22:f4aac491ea26 356 }
samux 22:f4aac491ea26 357 #ifdef DEBUG
samux 23:80bffaf28bef 358 printf("length: %lld\r\n", len_msg);
samux 22:f4aac491ea26 359 #endif
samux 22:f4aac491ea26 360 if ((len_msg & 0x80)) {
samux 23:80bffaf28bef 361 #ifdef DEBUG
samux 23:80bffaf28bef 362 printf("will read mask\r\n");
samux 23:80bffaf28bef 363 #endif
samux 23:80bffaf28bef 364 for (int i = 0; i < 4; i++) {
samux 22:f4aac491ea26 365 mask[i] = wifi->getc();
samux 23:80bffaf28bef 366 #ifdef DEBUG
samux 23:80bffaf28bef 367 printf("mask[%d]: %d\r\n", i, mask[i]);
samux 23:80bffaf28bef 368 #endif
samux 23:80bffaf28bef 369 }
samux 23:80bffaf28bef 370 } else {
samux 23:80bffaf28bef 371 #ifdef DEBUG
samux 23:80bffaf28bef 372 printf("len not 0x80\r\n");
samux 23:80bffaf28bef 373 #endif
samux 22:f4aac491ea26 374 }
samux 23:80bffaf28bef 375 } else {
samux 16:d4518b50f653 376 return false;
samux 23:80bffaf28bef 377 }
samux 15:79bfbc0ad6bc 378
samux 22:f4aac491ea26 379
samux 22:f4aac491ea26 380 for (i = 0; i < len_msg; i++) {
samux 22:f4aac491ea26 381 message[i] = wifi->getc() ^ mask[i % 4];
samux 15:79bfbc0ad6bc 382 }
samux 16:d4518b50f653 383
samux 22:f4aac491ea26 384 message[len_msg] = 0;
samux 22:f4aac491ea26 385 return true;
samux 22:f4aac491ea26 386 }
samux 22:f4aac491ea26 387 #ifdef TARGET_LPC1768
samux 22:f4aac491ea26 388 else if (netif == ETH) {
samux 16:d4518b50f653 389
samux 22:f4aac491ea26 390 uint32_t index = 0;
samux 15:79bfbc0ad6bc 391 Net::poll();
samux 9:9fa055ed54b4 392
samux 16:d4518b50f653 393 if (new_msg) {
samux 22:f4aac491ea26 394 tmr.start();
samux 22:f4aac491ea26 395 // read the opcode
samux 22:f4aac491ea26 396 while (true) {
samux 22:f4aac491ea26 397 if (tmr.read() > 3) {
samux 22:f4aac491ea26 398 return false;
samux 22:f4aac491ea26 399 }
samux 22:f4aac491ea26 400 opcode = eth_rx[index++];
samux 22:f4aac491ea26 401 if (opcode == 0x81) {
samux 22:f4aac491ea26 402 break;
samux 22:f4aac491ea26 403 }
samux 22:f4aac491ea26 404 }
samux 22:f4aac491ea26 405 #ifdef DEBUG
samux 22:f4aac491ea26 406 printf("opcode: 0x%X\r\n", opcode);
samux 22:f4aac491ea26 407 #endif
samux 22:f4aac491ea26 408
samux 22:f4aac491ea26 409 len_msg = eth_rx[index++] & 0x7f;
samux 22:f4aac491ea26 410 if (len_msg == 126) {
samux 23:80bffaf28bef 411 len_msg = (eth_rx[index++] << 8);
samux 23:80bffaf28bef 412 len_msg += eth_rx[index++];
samux 22:f4aac491ea26 413 } else if (len_msg == 127) {
samux 22:f4aac491ea26 414 len_msg = 0;
samux 22:f4aac491ea26 415 for (int i = 0; i < 8; i++) {
samux 23:80bffaf28bef 416 len_msg += eth_rx[index++] << (7-i)*8;
samux 22:f4aac491ea26 417 }
samux 22:f4aac491ea26 418 }
samux 23:80bffaf28bef 419 if (len_msg == 0) {
samux 16:d4518b50f653 420 return false;
samux 16:d4518b50f653 421 }
samux 22:f4aac491ea26 422 #ifdef DEBUG
samux 23:80bffaf28bef 423 printf("length: %lld\r\n", len_msg);
samux 22:f4aac491ea26 424 #endif
samux 22:f4aac491ea26 425 if ((len_msg & 0x80)) {
samux 22:f4aac491ea26 426 for (int i = 0; i < 4; i++)
samux 22:f4aac491ea26 427 mask[i] = eth_rx[index++];
samux 16:d4518b50f653 428 }
samux 22:f4aac491ea26 429
samux 22:f4aac491ea26 430 for (i = 0; i < len_msg; i++) {
samux 22:f4aac491ea26 431 message[i] = eth_rx[index++] ^ mask[i % 4];
samux 22:f4aac491ea26 432 }
samux 22:f4aac491ea26 433
samux 22:f4aac491ea26 434 message[len_msg] = 0;
samux 16:d4518b50f653 435 new_msg = false;
samux 16:d4518b50f653 436 return true;
samux 9:9fa055ed54b4 437 }
samux 16:d4518b50f653 438 return false;
samux 0:21fe3b05249f 439 }
samux 22:f4aac491ea26 440 #endif //target
samux 22:f4aac491ea26 441 //the program shouldn't be here
samux 16:d4518b50f653 442 return false;
samux 0:21fe3b05249f 443 }
samux 0:21fe3b05249f 444
samux 6:01a1eb7c0145 445 bool Websocket::close() {
samux 23:80bffaf28bef 446 sendOpcode(0x08);
samux 23:80bffaf28bef 447 sendLength(0);
samux 23:80bffaf28bef 448 sendMask();
samux 16:d4518b50f653 449 if (netif == WIF) {
samux 25:2214f1e5d4a3 450
samux 23:80bffaf28bef 451 wait(0.25);
samux 19:ca8c5ad59850 452 if (!wifi->cmdMode()) {
samux 16:d4518b50f653 453 #ifdef DEBUG
samux 9:9fa055ed54b4 454 printf("Websocket::close: cannot enter in cmd mode\r\n");
samux 16:d4518b50f653 455 #endif
samux 9:9fa055ed54b4 456 return false;
samux 9:9fa055ed54b4 457 }
samux 23:80bffaf28bef 458 wait(0.25);
samux 6:01a1eb7c0145 459
samux 25:2214f1e5d4a3 460 wifi->send("close\r", NULL);
samux 6:01a1eb7c0145 461
samux 9:9fa055ed54b4 462 if (!wifi->exit())
samux 9:9fa055ed54b4 463 return false;
samux 22:f4aac491ea26 464 }
samux 22:f4aac491ea26 465 #ifdef TARGET_LPC1768
samux 22:f4aac491ea26 466 else if (netif == ETH) {
samux 23:80bffaf28bef 467 #ifdef TARGET_LPC1768
samux 23:80bffaf28bef 468 Net::poll();
samux 23:80bffaf28bef 469 #endif //target
samux 16:d4518b50f653 470
samux 16:d4518b50f653 471 if (sock->close())
samux 9:9fa055ed54b4 472 return false;
samux 16:d4518b50f653 473 return true;
samux 9:9fa055ed54b4 474 }
samux 22:f4aac491ea26 475 #endif //target
samux 16:d4518b50f653 476 //the program shouldn't be here
samux 16:d4518b50f653 477 return false;
samux 0:21fe3b05249f 478 }
samux 0:21fe3b05249f 479
samux 0:21fe3b05249f 480
samux 0:21fe3b05249f 481
samux 6:01a1eb7c0145 482 bool Websocket::connected() {
samux 16:d4518b50f653 483 if (netif == WIF) {
samux 6:01a1eb7c0145 484 char str[10];
samux 6:01a1eb7c0145 485
samux 22:f4aac491ea26 486 // we have to wait at least 0.25s to enter in cmd mode whan we was sending tcp packets
samux 6:01a1eb7c0145 487 wait(0.25);
samux 19:ca8c5ad59850 488 if (!wifi->cmdMode()) {
samux 16:d4518b50f653 489 #ifdef DEBUG
samux 6:01a1eb7c0145 490 printf("Websocket::connected: cannot enter in cmd mode\r\n");
samux 16:d4518b50f653 491 #endif
samux 0:21fe3b05249f 492 return false;
samux 3:9b00db719afa 493 }
samux 6:01a1eb7c0145 494 wait(0.25);
samux 6:01a1eb7c0145 495
samux 25:2214f1e5d4a3 496 wifi->send("show c\r\n", NULL, str);
samux 22:f4aac491ea26 497 #ifdef DEBUG
samux 22:f4aac491ea26 498 printf("Websocket::connected: str: %s\r\n", str);
samux 22:f4aac491ea26 499 #endif
samux 6:01a1eb7c0145 500
samux 6:01a1eb7c0145 501 if (str[3] == '1') {
samux 6:01a1eb7c0145 502 if (!wifi->exit()) {
samux 16:d4518b50f653 503 #ifdef DEBUG
samux 6:01a1eb7c0145 504 printf("Websocket::connected: cannot exit\r\n");
samux 16:d4518b50f653 505 #endif
samux 6:01a1eb7c0145 506 return false;
samux 6:01a1eb7c0145 507 }
samux 6:01a1eb7c0145 508 return true;
samux 6:01a1eb7c0145 509 }
samux 16:d4518b50f653 510 if (!wifi->exit()) {
samux 16:d4518b50f653 511 #ifdef DEBUG
samux 6:01a1eb7c0145 512 printf("Websocket::connected: cannot exit\r\n");
samux 16:d4518b50f653 513 #endif
samux 16:d4518b50f653 514 }
samux 6:01a1eb7c0145 515 return false;
samux 22:f4aac491ea26 516 }
samux 22:f4aac491ea26 517 #ifdef TARGET_LPC1768
samux 22:f4aac491ea26 518 else if (netif == ETH) {
samux 6:01a1eb7c0145 519 return eth_connected;
samux 16:d4518b50f653 520 }
samux 22:f4aac491ea26 521 #endif //target
samux 16:d4518b50f653 522 //the program shouldn't be here
samux 16:d4518b50f653 523 return false;
samux 6:01a1eb7c0145 524 }
samux 6:01a1eb7c0145 525
samux 23:80bffaf28bef 526
samux 22:f4aac491ea26 527 std::string Websocket::getPath() {
samux 17:1e339933c97a 528 return path;
samux 17:1e339933c97a 529 }
samux 17:1e339933c97a 530
samux 6:01a1eb7c0145 531
samux 6:01a1eb7c0145 532
samux 6:01a1eb7c0145 533
samux 22:f4aac491ea26 534 #ifdef TARGET_LPC1768
samux 6:01a1eb7c0145 535 void Websocket::onTCPSocketEvent(TCPSocketEvent e) {
samux 9:9fa055ed54b4 536 if (e == TCPSOCKET_CONNECTED) {
samux 9:9fa055ed54b4 537 eth_connected = true;
samux 16:d4518b50f653 538 #ifdef DEBUG
samux 9:9fa055ed54b4 539 printf("TCP Socket Connected\r\n");
samux 16:d4518b50f653 540 #endif
samux 12:1f6b9451a608 541 } else if (e == TCPSOCKET_WRITEABLE) {
samux 12:1f6b9451a608 542 } else if (e == TCPSOCKET_READABLE) {
samux 16:d4518b50f653 543 int len = sock->recv(eth_rx, 512);
samux 9:9fa055ed54b4 544 eth_rx[len] = 0;
samux 16:d4518b50f653 545 new_msg = true;
samux 9:9fa055ed54b4 546 if (!response_server_eth) {
samux 9:9fa055ed54b4 547 string checking;
samux 9:9fa055ed54b4 548 size_t found = string::npos;
samux 9:9fa055ed54b4 549 checking = eth_rx;
samux 22:f4aac491ea26 550 found = checking.find("DdLWT/1JcX+nQFHebYP+rqEx5xI=");
samux 9:9fa055ed54b4 551 if (found != string::npos)
samux 9:9fa055ed54b4 552 response_server_eth = true;
samux 9:9fa055ed54b4 553 }
samux 12:1f6b9451a608 554 } else {
samux 16:d4518b50f653 555 #ifdef DEBUG
samux 9:9fa055ed54b4 556 printf("TCP Socket Fail\r\n");
samux 16:d4518b50f653 557 #endif
samux 9:9fa055ed54b4 558 eth_connected = false;
samux 0:21fe3b05249f 559 }
samux 6:01a1eb7c0145 560 }
samux 22:f4aac491ea26 561 #endif //target
samux 6:01a1eb7c0145 562
samux 6:01a1eb7c0145 563