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