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