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