WebSocket client library

Committer:
samux
Date:
Fri Aug 26 09:17:30 2011 +0000
Revision:
12:1f6b9451a608
Parent:
10:4f02275c34ee
Child:
13:3b058372cad9

        

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 6:01a1eb7c0145 4 Websocket::Websocket(char * url, Wifly * wifi) {
samux 9:9fa055ed54b4 5 this->wifi = wifi;
samux 9:9fa055ed54b4 6 wifi_use = true;
samux 9:9fa055ed54b4 7 eth_use = false;
samux 9:9fa055ed54b4 8 response_server_eth = false;
samux 9:9fa055ed54b4 9 fillFields(wifi_use, url);
samux 6:01a1eb7c0145 10 }
samux 6:01a1eb7c0145 11
samux 6:01a1eb7c0145 12 void Websocket::fillFields(bool wifi, char * url) {
samux 0:21fe3b05249f 13 char *res = NULL;
samux 6:01a1eb7c0145 14 char *res1 = NULL;
samux 6:01a1eb7c0145 15
samux 0:21fe3b05249f 16 char buf[30];
samux 0:21fe3b05249f 17 strcpy(buf, url);
samux 6:01a1eb7c0145 18
samux 0:21fe3b05249f 19 res = strtok(buf, ":");
samux 9:9fa055ed54b4 20 if (strcmp(res, "ws")) {
samux 0:21fe3b05249f 21 this->ip_domain = NULL;
samux 0:21fe3b05249f 22 this->path = NULL;
samux 6:01a1eb7c0145 23 printf("\r\nFormat error: please use: \"ws://ip-or-domain[:port]/path\"\r\n\r\n");
samux 9:9fa055ed54b4 24 } else {
samux 6:01a1eb7c0145 25 //ip_domain and port
samux 6:01a1eb7c0145 26 res = strtok(NULL, "/");
samux 6:01a1eb7c0145 27
samux 6:01a1eb7c0145 28 //path
samux 6:01a1eb7c0145 29 res1 = strtok(NULL, " ");
samux 6:01a1eb7c0145 30 if (res1 != NULL) {
samux 6:01a1eb7c0145 31 path = (char *) malloc (sizeof(char) * strlen(res1));
samux 6:01a1eb7c0145 32 strcpy(this->path, res1);
samux 6:01a1eb7c0145 33 }
samux 6:01a1eb7c0145 34
samux 6:01a1eb7c0145 35 //ip_domain
samux 6:01a1eb7c0145 36 res = strtok(res, ":");
samux 9:9fa055ed54b4 37
samux 7:b15978708360 38 //port
samux 7:b15978708360 39 res1 = strtok(NULL, " ");
samux 7:b15978708360 40 //port
samux 7:b15978708360 41 if (res1 != NULL) {
samux 7:b15978708360 42 port = (char *) malloc (sizeof(char) * strlen(res1));
samux 7:b15978708360 43 strcpy(this->port, res1);
samux 7:b15978708360 44 } else {
samux 7:b15978708360 45 port = (char *) malloc (sizeof(char) * 3);
samux 7:b15978708360 46 strcpy(this->port, "80");
samux 7:b15978708360 47 }
samux 9:9fa055ed54b4 48
samux 6:01a1eb7c0145 49 if (res != NULL) {
samux 0:21fe3b05249f 50 ip_domain = (char *) malloc (sizeof(char) * strlen(res));
samux 0:21fe3b05249f 51 strcpy(this->ip_domain, res);
samux 6:01a1eb7c0145 52 //if we use ethernet, we must decode ip address or use dnsresolver
samux 6:01a1eb7c0145 53 if (!wifi) {
samux 6:01a1eb7c0145 54 strcpy(buf, res);
samux 12:1f6b9451a608 55
samux 6:01a1eb7c0145 56 //we try to decode the ip address
samux 6:01a1eb7c0145 57 if (buf[0] >= '0' && buf[0] <= '9') {
samux 6:01a1eb7c0145 58 res = strtok(buf, ".");
samux 6:01a1eb7c0145 59 int i = 0;
samux 6:01a1eb7c0145 60 int ip[4];
samux 6:01a1eb7c0145 61 while (res != NULL) {
samux 6:01a1eb7c0145 62 ip[i] = atoi(res);
samux 6:01a1eb7c0145 63 res = strtok(NULL, ".");
samux 6:01a1eb7c0145 64 i++;
samux 6:01a1eb7c0145 65 }
samux 6:01a1eb7c0145 66 server_ip = IpAddr(ip[0], ip[1], ip[2], ip[3]);
samux 12:1f6b9451a608 67 printf("server without dns=%i.%i.%i.%i\n",server_ip[0],server_ip[1],server_ip[2],server_ip[3]);
samux 6:01a1eb7c0145 68 }
samux 6:01a1eb7c0145 69 }
samux 0:21fe3b05249f 70 }
samux 0:21fe3b05249f 71 }
samux 0:21fe3b05249f 72 }
samux 0:21fe3b05249f 73
samux 6:01a1eb7c0145 74
samux 12:1f6b9451a608 75 Websocket::Websocket(char * url) {
samux 12:1f6b9451a608 76 wifi_use = false;
samux 12:1f6b9451a608 77 server_ip = NULL;
samux 12:1f6b9451a608 78 eth_use = true;
samux 12:1f6b9451a608 79 eth_writeable = false;
samux 12:1f6b9451a608 80 eth_readable = false;
samux 12:1f6b9451a608 81 eth_connected = false;
samux 12:1f6b9451a608 82 response_server_eth = false;
samux 12:1f6b9451a608 83 fillFields(wifi_use, url);
samux 12:1f6b9451a608 84 }
samux 12:1f6b9451a608 85
samux 12:1f6b9451a608 86
samux 6:01a1eb7c0145 87
samux 6:01a1eb7c0145 88
samux 6:01a1eb7c0145 89 bool Websocket::connect() {
samux 0:21fe3b05249f 90 char cmd[50];
samux 6:01a1eb7c0145 91 if ( wifi_use ) {
samux 6:01a1eb7c0145 92 wifi->Send("exit\r", "NO");
samux 6:01a1eb7c0145 93 //enter in cmd mode
samux 6:01a1eb7c0145 94 while (!wifi->Send("$$$", "CMD")) {
samux 6:01a1eb7c0145 95 printf("cannot enter in CMD mode\r\n");
samux 6:01a1eb7c0145 96 wifi->exit();
samux 6:01a1eb7c0145 97 }
samux 6:01a1eb7c0145 98
samux 6:01a1eb7c0145 99
samux 6:01a1eb7c0145 100 //open the connection
samux 6:01a1eb7c0145 101 sprintf(cmd, "open %s %s\r\n", ip_domain, port);
samux 6:01a1eb7c0145 102 if (!wifi->Send(cmd, "OPEN")) {
samux 6:01a1eb7c0145 103 printf("Websocket::connect cannot open\r\n");
samux 6:01a1eb7c0145 104 return false;
samux 6:01a1eb7c0145 105 }
samux 6:01a1eb7c0145 106
samux 6:01a1eb7c0145 107
samux 6:01a1eb7c0145 108 //send websocket HTTP header
samux 6:01a1eb7c0145 109 sprintf(cmd, "GET /%s HTTP/1.1\r\n", path);
samux 6:01a1eb7c0145 110 wifi->Send(cmd, "NO");
samux 6:01a1eb7c0145 111
samux 6:01a1eb7c0145 112 sprintf(cmd, "Host: %s:%s\r\n", ip_domain, port);
samux 6:01a1eb7c0145 113 wifi->Send(cmd, "NO");
samux 6:01a1eb7c0145 114
samux 6:01a1eb7c0145 115 wifi->Send("Upgrade: WebSocket\r\n", "NO");
samux 6:01a1eb7c0145 116
samux 6:01a1eb7c0145 117 sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain, port);
samux 6:01a1eb7c0145 118 wifi->Send(cmd, "NO");
samux 6:01a1eb7c0145 119
samux 6:01a1eb7c0145 120
samux 6:01a1eb7c0145 121 wifi->Send("Connection: Upgrade\r\n", "NO");
samux 6:01a1eb7c0145 122 wifi->Send("Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n", "NO");
samux 6:01a1eb7c0145 123 wifi->Send("Sec-WebSocket-key2: 12998 5 Y3 1 .P00\r\n\r\n", "NO");
samux 6:01a1eb7c0145 124 if (!wifi->Send("^n:ds[4U", "8jKS'y:G*Co,Wxa-"))
samux 6:01a1eb7c0145 125 return false;
samux 6:01a1eb7c0145 126
samux 6:01a1eb7c0145 127 printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain, this->path, this->port);
samux 6:01a1eb7c0145 128 return true;
samux 6:01a1eb7c0145 129 } else if ( eth_use ) {
samux 6:01a1eb7c0145 130 int i = 0;
samux 6:01a1eb7c0145 131
samux 6:01a1eb7c0145 132 EthernetErr ethErr = eth.setup();
samux 6:01a1eb7c0145 133 if (ethErr) {
samux 6:01a1eb7c0145 134 printf("Error %d in setup.\r\n", ethErr);
samux 6:01a1eb7c0145 135 return false;
samux 6:01a1eb7c0145 136 }
samux 12:1f6b9451a608 137
samux 12:1f6b9451a608 138 //we must use dnsresolver to find the ip address
samux 12:1f6b9451a608 139 if (server_ip == NULL) {
samux 12:1f6b9451a608 140 DNSResolver dr;
samux 12:1f6b9451a608 141 server_ip = dr.resolveName(ip_domain);
samux 12:1f6b9451a608 142 printf("server with dns=%i.%i.%i.%i\r\n",server_ip[0],server_ip[1],server_ip[2],server_ip[3]);
samux 12:1f6b9451a608 143 }
samux 6:01a1eb7c0145 144
samux 6:01a1eb7c0145 145 IpAddr ipt = eth.getIp();
samux 6:01a1eb7c0145 146 printf("mbed IP Address is %d.%d.%d.%d\r\n", ipt[0], ipt[1], ipt[2], ipt[3]);
samux 6:01a1eb7c0145 147
samux 6:01a1eb7c0145 148 sock.setOnEvent(this, &Websocket::onTCPSocketEvent);
samux 6:01a1eb7c0145 149 Host server (server_ip, atoi(port));
samux 6:01a1eb7c0145 150 TCPSocketErr bindErr = sock.connect(server);
samux 6:01a1eb7c0145 151 if (bindErr) {
samux 12:1f6b9451a608 152 printf("binderr: %d\r\n", bindErr);
samux 6:01a1eb7c0145 153 return false;
samux 6:01a1eb7c0145 154 }
samux 6:01a1eb7c0145 155
samux 6:01a1eb7c0145 156
samux 6:01a1eb7c0145 157 Timer tmr;
samux 6:01a1eb7c0145 158 tmr.start();
samux 0:21fe3b05249f 159
samux 6:01a1eb7c0145 160 while (true) {
samux 6:01a1eb7c0145 161 Net::poll();
samux 12:1f6b9451a608 162 if (tmr.read() > 0.01) {
samux 6:01a1eb7c0145 163 tmr.reset();
samux 6:01a1eb7c0145 164 if (eth_connected) {
samux 6:01a1eb7c0145 165 switch (i) {
samux 6:01a1eb7c0145 166 case 0:
samux 6:01a1eb7c0145 167 sprintf(cmd, "GET /%s HTTP/1.1\r\n", path);
samux 6:01a1eb7c0145 168 sock.send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 169 i++;
samux 6:01a1eb7c0145 170 break;
samux 6:01a1eb7c0145 171 case 1:
samux 6:01a1eb7c0145 172 sprintf(cmd, "Host: %s:%s\r\n", ip_domain, port);
samux 6:01a1eb7c0145 173 sock.send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 174 i++;
samux 6:01a1eb7c0145 175 break;
samux 6:01a1eb7c0145 176 case 2:
samux 6:01a1eb7c0145 177 sprintf(cmd, "Upgrade: WebSocket\r\n");
samux 6:01a1eb7c0145 178 sock.send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 179 i++;
samux 6:01a1eb7c0145 180 break;
samux 6:01a1eb7c0145 181 case 3:
samux 6:01a1eb7c0145 182 sprintf(cmd, "Origin: http:%s:%s\r\n", ip_domain, port);
samux 6:01a1eb7c0145 183 sock.send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 184 i++;
samux 6:01a1eb7c0145 185 break;
samux 6:01a1eb7c0145 186 case 4:
samux 6:01a1eb7c0145 187 sprintf(cmd, "Connection: Upgrade\r\n");
samux 6:01a1eb7c0145 188 sock.send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 189 i++;
samux 6:01a1eb7c0145 190 break;
samux 6:01a1eb7c0145 191 case 5:
samux 6:01a1eb7c0145 192 sprintf(cmd, "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n");
samux 6:01a1eb7c0145 193 sock.send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 194 i++;
samux 6:01a1eb7c0145 195 break;
samux 6:01a1eb7c0145 196 case 6:
samux 6:01a1eb7c0145 197 sprintf(cmd, "Sec-WebSocket-key2: 12998 5 Y3 1 .P00\r\n\r\n");
samux 6:01a1eb7c0145 198 sock.send(cmd, strlen(cmd));
samux 6:01a1eb7c0145 199 i++;
samux 6:01a1eb7c0145 200 break;
samux 6:01a1eb7c0145 201 case 7:
samux 6:01a1eb7c0145 202 sock.send("^n:ds[4U", 8);
samux 6:01a1eb7c0145 203 i++;
samux 6:01a1eb7c0145 204 break;
samux 6:01a1eb7c0145 205 case 8:
samux 9:9fa055ed54b4 206 if (response_server_eth)
samux 9:9fa055ed54b4 207 i++;
samux 9:9fa055ed54b4 208 else
samux 9:9fa055ed54b4 209 break;
samux 9:9fa055ed54b4 210
samux 6:01a1eb7c0145 211 default:
samux 6:01a1eb7c0145 212 break;
samux 6:01a1eb7c0145 213 }
samux 6:01a1eb7c0145 214 }
samux 9:9fa055ed54b4 215 if (i==9) {
samux 6:01a1eb7c0145 216 printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %s\r\n\r\n",this->ip_domain, this->path, this->port);
samux 6:01a1eb7c0145 217 return true;
samux 6:01a1eb7c0145 218 }
samux 6:01a1eb7c0145 219 }
samux 6:01a1eb7c0145 220 }
samux 6:01a1eb7c0145 221
samux 0:21fe3b05249f 222 }
samux 0:21fe3b05249f 223 return true;
samux 0:21fe3b05249f 224 }
samux 0:21fe3b05249f 225
samux 6:01a1eb7c0145 226 void Websocket::Send(char * str) {
samux 0:21fe3b05249f 227 char cmd[100];
samux 6:01a1eb7c0145 228 if (wifi_use) {
samux 6:01a1eb7c0145 229 wifi->putc('\x00');
samux 6:01a1eb7c0145 230 sprintf(cmd, "%s%c", str, '\xff');
samux 6:01a1eb7c0145 231 wifi->Send(cmd, "NO");
samux 6:01a1eb7c0145 232 } else if (eth_use) {
samux 6:01a1eb7c0145 233 sprintf(cmd, "%c%s%c", 0x00, str, 0xff);
samux 6:01a1eb7c0145 234 Net::poll();
samux 6:01a1eb7c0145 235 sock.send(cmd, strlen(cmd + 1) + 1);
samux 6:01a1eb7c0145 236 }
samux 0:21fe3b05249f 237 }
samux 0:21fe3b05249f 238
samux 6:01a1eb7c0145 239 bool Websocket::read(char * message) {
samux 0:21fe3b05249f 240 int i = 0;
samux 6:01a1eb7c0145 241
samux 9:9fa055ed54b4 242 if (wifi_use) {
samux 9:9fa055ed54b4 243 char char_read;
samux 9:9fa055ed54b4 244 if (!wifi->readable()) {
samux 9:9fa055ed54b4 245 message = NULL;
samux 9:9fa055ed54b4 246 return false;
samux 9:9fa055ed54b4 247 }
samux 9:9fa055ed54b4 248
samux 9:9fa055ed54b4 249 if (wifi->getc() != 0x00) {
samux 9:9fa055ed54b4 250 message = NULL;
samux 9:9fa055ed54b4 251 return false;
samux 9:9fa055ed54b4 252 }
samux 6:01a1eb7c0145 253
samux 9:9fa055ed54b4 254 while ( (char_read = wifi->getc()) != 0xff)
samux 9:9fa055ed54b4 255 message[i++] = char_read;
samux 9:9fa055ed54b4 256 message[i] = 0;
samux 9:9fa055ed54b4 257 } else if (eth_use) {
samux 9:9fa055ed54b4 258
samux 9:9fa055ed54b4 259 if (eth_rx[0] != 0x00) {
samux 9:9fa055ed54b4 260 message = NULL;
samux 9:9fa055ed54b4 261 return false;
samux 9:9fa055ed54b4 262 }
samux 9:9fa055ed54b4 263 while (eth_rx[i + 1] != 0xff) {
samux 9:9fa055ed54b4 264 message[i] = eth_rx[i + 1];
samux 9:9fa055ed54b4 265 i++;
samux 9:9fa055ed54b4 266 }
samux 9:9fa055ed54b4 267 message[i] = 0;
samux 0:21fe3b05249f 268 }
samux 0:21fe3b05249f 269 return true;
samux 0:21fe3b05249f 270 }
samux 0:21fe3b05249f 271
samux 6:01a1eb7c0145 272 bool Websocket::close() {
samux 9:9fa055ed54b4 273 if (wifi_use) {
samux 9:9fa055ed54b4 274 if (!wifi->CmdMode()) {
samux 9:9fa055ed54b4 275 printf("Websocket::close: cannot enter in cmd mode\r\n");
samux 9:9fa055ed54b4 276 return false;
samux 9:9fa055ed54b4 277 }
samux 6:01a1eb7c0145 278
samux 9:9fa055ed54b4 279 wifi->Send("close\r", "NO");
samux 6:01a1eb7c0145 280
samux 9:9fa055ed54b4 281 if (!wifi->exit())
samux 9:9fa055ed54b4 282 return false;
samux 9:9fa055ed54b4 283 } else if (eth_use) {
samux 9:9fa055ed54b4 284 if (sock.close())
samux 9:9fa055ed54b4 285 return false;
samux 9:9fa055ed54b4 286 }
samux 0:21fe3b05249f 287 return true;
samux 0:21fe3b05249f 288 }
samux 0:21fe3b05249f 289
samux 0:21fe3b05249f 290
samux 0:21fe3b05249f 291
samux 6:01a1eb7c0145 292 bool Websocket::connected() {
samux 6:01a1eb7c0145 293 if (wifi_use) {
samux 6:01a1eb7c0145 294 char str[10];
samux 6:01a1eb7c0145 295
samux 6:01a1eb7c0145 296 wait(0.25);
samux 6:01a1eb7c0145 297 if (!wifi->CmdMode()) {
samux 6:01a1eb7c0145 298 printf("Websocket::connected: cannot enter in cmd mode\r\n");
samux 0:21fe3b05249f 299 return false;
samux 3:9b00db719afa 300 }
samux 6:01a1eb7c0145 301 wait(0.25);
samux 6:01a1eb7c0145 302
samux 6:01a1eb7c0145 303 wifi->Send("show c\r\n", "NO");
samux 6:01a1eb7c0145 304 wifi->read(str);
samux 6:01a1eb7c0145 305
samux 6:01a1eb7c0145 306 if (str[3] == '1') {
samux 6:01a1eb7c0145 307 if (!wifi->exit()) {
samux 6:01a1eb7c0145 308 printf("Websocket::connected: cannot exit\r\n");
samux 6:01a1eb7c0145 309 return false;
samux 6:01a1eb7c0145 310 }
samux 6:01a1eb7c0145 311 return true;
samux 6:01a1eb7c0145 312 }
samux 6:01a1eb7c0145 313 if (!wifi->exit())
samux 6:01a1eb7c0145 314 printf("Websocket::connected: cannot exit\r\n");
samux 6:01a1eb7c0145 315 return false;
samux 9:9fa055ed54b4 316 } else if (eth_use)
samux 6:01a1eb7c0145 317 return eth_connected;
samux 6:01a1eb7c0145 318
samux 6:01a1eb7c0145 319 return true;
samux 6:01a1eb7c0145 320 }
samux 6:01a1eb7c0145 321
samux 6:01a1eb7c0145 322
samux 6:01a1eb7c0145 323
samux 6:01a1eb7c0145 324
samux 6:01a1eb7c0145 325
samux 6:01a1eb7c0145 326 void Websocket::onTCPSocketEvent(TCPSocketEvent e) {
samux 9:9fa055ed54b4 327 if (e == TCPSOCKET_CONNECTED) {
samux 9:9fa055ed54b4 328 eth_connected = true;
samux 9:9fa055ed54b4 329 printf("TCP Socket Connected\r\n");
samux 12:1f6b9451a608 330 } else if (e == TCPSOCKET_WRITEABLE) {
samux 12:1f6b9451a608 331 } else if (e == TCPSOCKET_READABLE) {
samux 9:9fa055ed54b4 332 int len = sock.recv(eth_rx, 512);
samux 9:9fa055ed54b4 333 eth_rx[len] = 0;
samux 9:9fa055ed54b4 334 if (!response_server_eth) {
samux 9:9fa055ed54b4 335 string checking;
samux 9:9fa055ed54b4 336 size_t found = string::npos;
samux 9:9fa055ed54b4 337 checking = eth_rx;
samux 9:9fa055ed54b4 338 found = checking.find("HTTP");
samux 9:9fa055ed54b4 339 if (found != string::npos)
samux 9:9fa055ed54b4 340 response_server_eth = true;
samux 9:9fa055ed54b4 341 }
samux 12:1f6b9451a608 342 } else {
samux 9:9fa055ed54b4 343 printf("TCP Socket Fail\r\n");
samux 9:9fa055ed54b4 344 eth_connected = false;
samux 0:21fe3b05249f 345 }
samux 6:01a1eb7c0145 346 }
samux 6:01a1eb7c0145 347
samux 6:01a1eb7c0145 348