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