WebSocket client library
Websocket.cpp@22:f4aac491ea26, 2012-02-01 (annotated)
- Committer:
- samux
- Date:
- Wed Feb 01 17:23:40 2012 +0000
- Revision:
- 22:f4aac491ea26
- Parent:
- 21:2c4462ca74fa
- Child:
- 23:80bffaf28bef
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 | 22:f4aac491ea26 | 4 | //#define DEBUG |
samux | 20:bb9d7ba48e6e | 5 | |
samux | 6:01a1eb7c0145 | 6 | Websocket::Websocket(char * url, Wifly * wifi) { |
samux | 9:9fa055ed54b4 | 7 | this->wifi = wifi; |
samux | 16:d4518b50f653 | 8 | netif = WIF; |
samux | 16:d4518b50f653 | 9 | fillFields(url); |
samux | 6:01a1eb7c0145 | 10 | } |
samux | 6:01a1eb7c0145 | 11 | |
samux | 13:3b058372cad9 | 12 | |
samux | 22:f4aac491ea26 | 13 | #ifdef TARGET_LPC1768 |
samux | 13:3b058372cad9 | 14 | Websocket::Websocket(char * url) { |
samux | 13:3b058372cad9 | 15 | server_ip = NULL; |
samux | 16:d4518b50f653 | 16 | netif = ETH; |
samux | 13:3b058372cad9 | 17 | eth_writeable = false; |
samux | 13:3b058372cad9 | 18 | eth_readable = false; |
samux | 13:3b058372cad9 | 19 | eth_connected = false; |
samux | 13:3b058372cad9 | 20 | response_server_eth = false; |
samux | 16:d4518b50f653 | 21 | new_msg = false; |
samux | 16:d4518b50f653 | 22 | fillFields(url); |
samux | 14:c5ac3e26998f | 23 | |
samux | 16:d4518b50f653 | 24 | eth = new EthernetNetIf(); |
samux | 16:d4518b50f653 | 25 | sock = new TCPSocket(); |
samux | 14:c5ac3e26998f | 26 | |
samux | 16:d4518b50f653 | 27 | EthernetErr ethErr = eth->setup(); |
samux | 16:d4518b50f653 | 28 | #ifdef DEBUG |
samux | 14:c5ac3e26998f | 29 | if (ethErr) { |
samux | 14:c5ac3e26998f | 30 | printf("\r\nERROR %d in setup.\r\n", ethErr); |
samux | 14:c5ac3e26998f | 31 | } |
samux | 16:d4518b50f653 | 32 | #endif |
samux | 14:c5ac3e26998f | 33 | |
samux | 14:c5ac3e26998f | 34 | //we must use dnsresolver to find the ip address |
samux | 14:c5ac3e26998f | 35 | if (server_ip == NULL) { |
samux | 14:c5ac3e26998f | 36 | DNSResolver dr; |
samux | 16:d4518b50f653 | 37 | server_ip = new IpAddr(); |
samux | 17:1e339933c97a | 38 | *server_ip = dr.resolveName(ip_domain.c_str()); |
samux | 16:d4518b50f653 | 39 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 40 | printf("\r\nserver with dns=%d.%d.%d.%d\r\n", (*server_ip)[0], (*server_ip)[1], (*server_ip)[2], (*server_ip)[3]); |
samux | 16:d4518b50f653 | 41 | #endif |
samux | 16:d4518b50f653 | 42 | |
samux | 14:c5ac3e26998f | 43 | } |
samux | 15:79bfbc0ad6bc | 44 | |
samux | 16:d4518b50f653 | 45 | IpAddr ipt = eth->getIp(); |
samux | 16:d4518b50f653 | 46 | #ifdef DEBUG |
samux | 14:c5ac3e26998f | 47 | printf("\r\nmbed IP Address is %d.%d.%d.%d\r\n", ipt[0], ipt[1], ipt[2], ipt[3]); |
samux | 16:d4518b50f653 | 48 | #endif |
samux | 15:79bfbc0ad6bc | 49 | |
samux | 16:d4518b50f653 | 50 | sock->setOnEvent(this, &Websocket::onTCPSocketEvent); |
samux | 13:3b058372cad9 | 51 | } |
samux | 22:f4aac491ea26 | 52 | #endif //target |
samux | 13:3b058372cad9 | 53 | |
samux | 13:3b058372cad9 | 54 | |
samux | 16:d4518b50f653 | 55 | void Websocket::fillFields(char * url) { |
samux | 0:21fe3b05249f | 56 | char *res = NULL; |
samux | 6:01a1eb7c0145 | 57 | char *res1 = NULL; |
samux | 6:01a1eb7c0145 | 58 | |
samux | 16:d4518b50f653 | 59 | char buf[50]; |
samux | 0:21fe3b05249f | 60 | strcpy(buf, url); |
samux | 6:01a1eb7c0145 | 61 | |
samux | 0:21fe3b05249f | 62 | res = strtok(buf, ":"); |
samux | 9:9fa055ed54b4 | 63 | if (strcmp(res, "ws")) { |
samux | 16:d4518b50f653 | 64 | #ifdef DEBUG |
samux | 6:01a1eb7c0145 | 65 | printf("\r\nFormat error: please use: \"ws://ip-or-domain[:port]/path\"\r\n\r\n"); |
samux | 16:d4518b50f653 | 66 | #endif |
samux | 9:9fa055ed54b4 | 67 | } else { |
samux | 6:01a1eb7c0145 | 68 | //ip_domain and port |
samux | 6:01a1eb7c0145 | 69 | res = strtok(NULL, "/"); |
samux | 6:01a1eb7c0145 | 70 | |
samux | 6:01a1eb7c0145 | 71 | //path |
samux | 6:01a1eb7c0145 | 72 | res1 = strtok(NULL, " "); |
samux | 6:01a1eb7c0145 | 73 | if (res1 != NULL) { |
samux | 17:1e339933c97a | 74 | path = res1; |
samux | 6:01a1eb7c0145 | 75 | } |
samux | 6:01a1eb7c0145 | 76 | |
samux | 6:01a1eb7c0145 | 77 | //ip_domain |
samux | 6:01a1eb7c0145 | 78 | res = strtok(res, ":"); |
samux | 9:9fa055ed54b4 | 79 | |
samux | 7:b15978708360 | 80 | //port |
samux | 7:b15978708360 | 81 | res1 = strtok(NULL, " "); |
samux | 7:b15978708360 | 82 | //port |
samux | 7:b15978708360 | 83 | if (res1 != NULL) { |
samux | 17:1e339933c97a | 84 | port = res1; |
samux | 7:b15978708360 | 85 | } else { |
samux | 17:1e339933c97a | 86 | port = "80"; |
samux | 7:b15978708360 | 87 | } |
samux | 9:9fa055ed54b4 | 88 | |
samux | 6:01a1eb7c0145 | 89 | if (res != NULL) { |
samux | 17:1e339933c97a | 90 | ip_domain = res; |
samux | 16:d4518b50f653 | 91 | |
samux | 6:01a1eb7c0145 | 92 | //if we use ethernet, we must decode ip address or use dnsresolver |
samux | 22:f4aac491ea26 | 93 | #ifdef TARGET_LPC1768 |
samux | 16:d4518b50f653 | 94 | if (netif == ETH) { |
samux | 6:01a1eb7c0145 | 95 | strcpy(buf, res); |
samux | 14:c5ac3e26998f | 96 | |
samux | 6:01a1eb7c0145 | 97 | //we try to decode the ip address |
samux | 6:01a1eb7c0145 | 98 | if (buf[0] >= '0' && buf[0] <= '9') { |
samux | 6:01a1eb7c0145 | 99 | res = strtok(buf, "."); |
samux | 6:01a1eb7c0145 | 100 | int i = 0; |
samux | 6:01a1eb7c0145 | 101 | int ip[4]; |
samux | 6:01a1eb7c0145 | 102 | while (res != NULL) { |
samux | 6:01a1eb7c0145 | 103 | ip[i] = atoi(res); |
samux | 6:01a1eb7c0145 | 104 | res = strtok(NULL, "."); |
samux | 6:01a1eb7c0145 | 105 | i++; |
samux | 6:01a1eb7c0145 | 106 | } |
samux | 16:d4518b50f653 | 107 | server_ip = new IpAddr(ip[0], ip[1], ip[2], ip[3]); |
samux | 16:d4518b50f653 | 108 | #ifdef DEBUG |
samux | 16:d4518b50f653 | 109 | printf("server without dns=%i.%i.%i.%i\n",(*server_ip)[0],(*server_ip)[1],(*server_ip)[2],(*server_ip)[3]); |
samux | 16:d4518b50f653 | 110 | #endif |
samux | 6:01a1eb7c0145 | 111 | } |
samux | 6:01a1eb7c0145 | 112 | } |
samux | 22:f4aac491ea26 | 113 | #endif //target |
samux | 0:21fe3b05249f | 114 | } |
samux | 0:21fe3b05249f | 115 | } |
samux | 0:21fe3b05249f | 116 | } |
samux | 0:21fe3b05249f | 117 | |
samux | 6:01a1eb7c0145 | 118 | |
samux | 6:01a1eb7c0145 | 119 | bool Websocket::connect() { |
samux | 0:21fe3b05249f | 120 | char cmd[50]; |
samux | 16:d4518b50f653 | 121 | if (netif == WIF) { |
samux | 6:01a1eb7c0145 | 122 | //enter in cmd mode |
samux | 22:f4aac491ea26 | 123 | wifi->exit(); |
samux | 22:f4aac491ea26 | 124 | while (!wifi->cmdMode()) { |
samux | 16:d4518b50f653 | 125 | #ifdef DEBUG |
samux | 6:01a1eb7c0145 | 126 | printf("cannot enter in CMD mode\r\n"); |
samux | 16:d4518b50f653 | 127 | #endif |
samux | 22:f4aac491ea26 | 128 | wifi->send("a\r\n"); |
samux | 22:f4aac491ea26 | 129 | wait(0.2); |
samux | 6:01a1eb7c0145 | 130 | wifi->exit(); |
samux | 22:f4aac491ea26 | 131 | if (!wifi->cmdMode()) |
samux | 22:f4aac491ea26 | 132 | return false; |
samux | 22:f4aac491ea26 | 133 | } |
samux | 22:f4aac491ea26 | 134 | |
samux | 22:f4aac491ea26 | 135 | if (!wifi->send("set comm remote 0\r\n", "AOK")) { |
samux | 22:f4aac491ea26 | 136 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 137 | printf("Websocket::connect(): cannot set empty remote string\r\n"); |
samux | 22:f4aac491ea26 | 138 | #endif |
samux | 22:f4aac491ea26 | 139 | return false; |
samux | 6:01a1eb7c0145 | 140 | } |
samux | 6:01a1eb7c0145 | 141 | |
samux | 6:01a1eb7c0145 | 142 | |
samux | 6:01a1eb7c0145 | 143 | //open the connection |
samux | 22:f4aac491ea26 | 144 | char recv[30]; |
samux | 22:f4aac491ea26 | 145 | string str; |
samux | 17:1e339933c97a | 146 | sprintf(cmd, "open %s %s\r\n", ip_domain.c_str(), port.c_str()); |
samux | 22:f4aac491ea26 | 147 | wifi->send(cmd, "NO", recv); |
samux | 22:f4aac491ea26 | 148 | str = recv; |
samux | 22:f4aac491ea26 | 149 | |
samux | 22:f4aac491ea26 | 150 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 151 | printf("Websocket::connect recv: %s\r\n", recv); |
samux | 22:f4aac491ea26 | 152 | #endif |
samux | 22:f4aac491ea26 | 153 | |
samux | 22:f4aac491ea26 | 154 | if (str.find("Connected") == string::npos && str.find("OPEN") == string::npos) { |
samux | 16:d4518b50f653 | 155 | #ifdef DEBUG |
samux | 6:01a1eb7c0145 | 156 | printf("Websocket::connect cannot open\r\n"); |
samux | 16:d4518b50f653 | 157 | #endif |
samux | 6:01a1eb7c0145 | 158 | return false; |
samux | 6:01a1eb7c0145 | 159 | } |
samux | 6:01a1eb7c0145 | 160 | |
samux | 22:f4aac491ea26 | 161 | if (str.find("Connected") != string::npos) { |
samux | 22:f4aac491ea26 | 162 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 163 | printf("will try to close the conn\r\n"); |
samux | 22:f4aac491ea26 | 164 | #endif |
samux | 22:f4aac491ea26 | 165 | if (!wifi->send("close\r\n", "CLOS")) { |
samux | 22:f4aac491ea26 | 166 | return false; |
samux | 22:f4aac491ea26 | 167 | } |
samux | 22:f4aac491ea26 | 168 | if (!wifi->send(cmd, "OPEN")) |
samux | 22:f4aac491ea26 | 169 | return false; |
samux | 22:f4aac491ea26 | 170 | } |
samux | 6:01a1eb7c0145 | 171 | |
samux | 6:01a1eb7c0145 | 172 | //send websocket HTTP header |
samux | 17:1e339933c97a | 173 | sprintf(cmd, "GET /%s HTTP/1.1\r\n", path.c_str()); |
samux | 22:f4aac491ea26 | 174 | wifi->send(cmd); |
samux | 22:f4aac491ea26 | 175 | wifi->send("Upgrade: websocket\r\n"); |
samux | 22:f4aac491ea26 | 176 | wifi->send("Connection: Upgrade\r\n"); |
samux | 17:1e339933c97a | 177 | sprintf(cmd, "Host: %s:%s\r\n", ip_domain.c_str(), port.c_str()); |
samux | 22:f4aac491ea26 | 178 | wifi->send(cmd); |
samux | 22:f4aac491ea26 | 179 | wifi->send("Origin: null\r\n"); |
samux | 22:f4aac491ea26 | 180 | wifi->send("Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n"); |
samux | 22:f4aac491ea26 | 181 | if (!wifi->send("Sec-WebSocket-Version: 13\r\n\r\n", "DdLWT/1JcX+nQFHebYP+rqEx5xI=")) |
samux | 22:f4aac491ea26 | 182 | return false; |
samux | 6:01a1eb7c0145 | 183 | |
samux | 22:f4aac491ea26 | 184 | wifi->flush(); |
samux | 22:f4aac491ea26 | 185 | |
samux | 22:f4aac491ea26 | 186 | //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 | 187 | return true; |
samux | 22:f4aac491ea26 | 188 | } |
samux | 22:f4aac491ea26 | 189 | #ifdef TARGET_LPC1768 |
samux | 22:f4aac491ea26 | 190 | else if (netif == ETH) { |
samux | 17:1e339933c97a | 191 | Host server (*server_ip, atoi(port.c_str())); |
samux | 16:d4518b50f653 | 192 | sock->close(); |
samux | 16:d4518b50f653 | 193 | TCPSocketErr bindErr = sock->connect(server); |
samux | 6:01a1eb7c0145 | 194 | if (bindErr) { |
samux | 16:d4518b50f653 | 195 | #ifdef DEBUG |
samux | 14:c5ac3e26998f | 196 | printf("\r\nERROR binderr: %d\r\n", bindErr); |
samux | 16:d4518b50f653 | 197 | #endif |
samux | 6:01a1eb7c0145 | 198 | return false; |
samux | 6:01a1eb7c0145 | 199 | } |
samux | 6:01a1eb7c0145 | 200 | |
samux | 6:01a1eb7c0145 | 201 | Timer tmr; |
samux | 6:01a1eb7c0145 | 202 | tmr.start(); |
samux | 15:79bfbc0ad6bc | 203 | |
samux | 14:c5ac3e26998f | 204 | Timer stop; |
samux | 14:c5ac3e26998f | 205 | stop.start(); |
samux | 0:21fe3b05249f | 206 | |
samux | 14:c5ac3e26998f | 207 | int i = 0; |
samux | 6:01a1eb7c0145 | 208 | while (true) { |
samux | 6:01a1eb7c0145 | 209 | Net::poll(); |
samux | 15:79bfbc0ad6bc | 210 | if (stop.read() > 3) |
samux | 14:c5ac3e26998f | 211 | return false; |
samux | 22:f4aac491ea26 | 212 | if (tmr.read() > 0.05) { |
samux | 6:01a1eb7c0145 | 213 | tmr.reset(); |
samux | 6:01a1eb7c0145 | 214 | if (eth_connected) { |
samux | 6:01a1eb7c0145 | 215 | switch (i) { |
samux | 6:01a1eb7c0145 | 216 | case 0: |
samux | 17:1e339933c97a | 217 | sprintf(cmd, "GET /%s HTTP/1.1\r\n", path.c_str()); |
samux | 16:d4518b50f653 | 218 | sock->send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 219 | i++; |
samux | 6:01a1eb7c0145 | 220 | break; |
samux | 6:01a1eb7c0145 | 221 | case 1: |
samux | 17:1e339933c97a | 222 | sprintf(cmd, "Host: %s:%s\r\n", ip_domain.c_str(), port.c_str()); |
samux | 16:d4518b50f653 | 223 | sock->send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 224 | i++; |
samux | 6:01a1eb7c0145 | 225 | break; |
samux | 6:01a1eb7c0145 | 226 | case 2: |
samux | 6:01a1eb7c0145 | 227 | sprintf(cmd, "Upgrade: WebSocket\r\n"); |
samux | 16:d4518b50f653 | 228 | sock->send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 229 | i++; |
samux | 6:01a1eb7c0145 | 230 | break; |
samux | 6:01a1eb7c0145 | 231 | case 3: |
samux | 22:f4aac491ea26 | 232 | sprintf(cmd, "Origin: null\r\n"); |
samux | 16:d4518b50f653 | 233 | sock->send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 234 | i++; |
samux | 6:01a1eb7c0145 | 235 | break; |
samux | 6:01a1eb7c0145 | 236 | case 4: |
samux | 6:01a1eb7c0145 | 237 | sprintf(cmd, "Connection: Upgrade\r\n"); |
samux | 16:d4518b50f653 | 238 | sock->send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 239 | i++; |
samux | 6:01a1eb7c0145 | 240 | break; |
samux | 6:01a1eb7c0145 | 241 | case 5: |
samux | 22:f4aac491ea26 | 242 | sprintf(cmd, "Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n"); |
samux | 16:d4518b50f653 | 243 | sock->send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 244 | i++; |
samux | 6:01a1eb7c0145 | 245 | break; |
samux | 6:01a1eb7c0145 | 246 | case 6: |
samux | 22:f4aac491ea26 | 247 | sprintf(cmd, "Sec-WebSocket-Version: 13\r\n\r\n"); |
samux | 16:d4518b50f653 | 248 | sock->send(cmd, strlen(cmd)); |
samux | 6:01a1eb7c0145 | 249 | i++; |
samux | 6:01a1eb7c0145 | 250 | break; |
samux | 6:01a1eb7c0145 | 251 | case 7: |
samux | 9:9fa055ed54b4 | 252 | if (response_server_eth) |
samux | 9:9fa055ed54b4 | 253 | i++; |
samux | 9:9fa055ed54b4 | 254 | else |
samux | 9:9fa055ed54b4 | 255 | break; |
samux | 9:9fa055ed54b4 | 256 | |
samux | 6:01a1eb7c0145 | 257 | default: |
samux | 6:01a1eb7c0145 | 258 | break; |
samux | 6:01a1eb7c0145 | 259 | } |
samux | 6:01a1eb7c0145 | 260 | } |
samux | 22:f4aac491ea26 | 261 | if (i==8) { |
samux | 22:f4aac491ea26 | 262 | #ifdef DEBUG |
samux | 17:1e339933c97a | 263 | 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 | 22:f4aac491ea26 | 264 | #endif |
samux | 6:01a1eb7c0145 | 265 | return true; |
samux | 6:01a1eb7c0145 | 266 | } |
samux | 6:01a1eb7c0145 | 267 | } |
samux | 6:01a1eb7c0145 | 268 | } |
samux | 0:21fe3b05249f | 269 | } |
samux | 22:f4aac491ea26 | 270 | #endif //target |
samux | 16:d4518b50f653 | 271 | //the program shouldn't be here |
samux | 16:d4518b50f653 | 272 | return false; |
samux | 0:21fe3b05249f | 273 | } |
samux | 0:21fe3b05249f | 274 | |
samux | 22:f4aac491ea26 | 275 | void Websocket::sendLength(uint32_t len) { |
samux | 22:f4aac491ea26 | 276 | if (len < 126) { |
samux | 22:f4aac491ea26 | 277 | sendChar(len | (1<<7)); |
samux | 22:f4aac491ea26 | 278 | } else if (len < 65535) { |
samux | 22:f4aac491ea26 | 279 | sendChar(126 | (1<<7)); |
samux | 22:f4aac491ea26 | 280 | sendChar(len & 0xff); |
samux | 22:f4aac491ea26 | 281 | sendChar((len >> 8) & 0xff); |
samux | 22:f4aac491ea26 | 282 | } else { |
samux | 22:f4aac491ea26 | 283 | sendChar(127 | (1<<7)); |
samux | 22:f4aac491ea26 | 284 | for (int i = 0; i < 8; i++) { |
samux | 22:f4aac491ea26 | 285 | sendChar((len >> i*8) & 0xff); |
samux | 22:f4aac491ea26 | 286 | } |
samux | 6:01a1eb7c0145 | 287 | } |
samux | 0:21fe3b05249f | 288 | } |
samux | 0:21fe3b05249f | 289 | |
samux | 22:f4aac491ea26 | 290 | void Websocket::sendChar(uint8_t c) { |
samux | 22:f4aac491ea26 | 291 | if (netif == WIF) { |
samux | 22:f4aac491ea26 | 292 | wifi->putc(c); |
samux | 22:f4aac491ea26 | 293 | } |
samux | 22:f4aac491ea26 | 294 | #ifdef TARGET_LPC1768 |
samux | 22:f4aac491ea26 | 295 | else if (netif == ETH) { |
samux | 22:f4aac491ea26 | 296 | Net::poll(); |
samux | 22:f4aac491ea26 | 297 | sock->send((const char *)&c, 1); |
samux | 22:f4aac491ea26 | 298 | } |
samux | 22:f4aac491ea26 | 299 | #endif |
samux | 22:f4aac491ea26 | 300 | } |
samux | 22:f4aac491ea26 | 301 | |
samux | 22:f4aac491ea26 | 302 | void Websocket::sendOpcode(uint8_t opcode) { |
samux | 22:f4aac491ea26 | 303 | sendChar(0x80 | (opcode & 0x0f)); |
samux | 22:f4aac491ea26 | 304 | } |
samux | 22:f4aac491ea26 | 305 | |
samux | 22:f4aac491ea26 | 306 | void Websocket::sendMask() { |
samux | 22:f4aac491ea26 | 307 | for (int i = 0; i < 4; i++) { |
samux | 22:f4aac491ea26 | 308 | sendChar(0); |
samux | 22:f4aac491ea26 | 309 | } |
samux | 22:f4aac491ea26 | 310 | } |
samux | 22:f4aac491ea26 | 311 | |
samux | 22:f4aac491ea26 | 312 | void Websocket::send(char * str) { |
samux | 22:f4aac491ea26 | 313 | sendOpcode(0x01); |
samux | 22:f4aac491ea26 | 314 | sendLength(strlen(str)); |
samux | 22:f4aac491ea26 | 315 | sendMask(); |
samux | 22:f4aac491ea26 | 316 | if (netif == WIF) { |
samux | 22:f4aac491ea26 | 317 | wifi->send(str, "NO"); |
samux | 22:f4aac491ea26 | 318 | } |
samux | 22:f4aac491ea26 | 319 | #ifdef TARGET_LPC1768 |
samux | 22:f4aac491ea26 | 320 | else if (netif == ETH) { |
samux | 22:f4aac491ea26 | 321 | Net::poll(); |
samux | 22:f4aac491ea26 | 322 | sock->send(str, strlen(str)); |
samux | 22:f4aac491ea26 | 323 | } |
samux | 22:f4aac491ea26 | 324 | #endif //target |
samux | 22:f4aac491ea26 | 325 | } |
samux | 22:f4aac491ea26 | 326 | |
samux | 22:f4aac491ea26 | 327 | |
samux | 22:f4aac491ea26 | 328 | |
samux | 6:01a1eb7c0145 | 329 | bool Websocket::read(char * message) { |
samux | 0:21fe3b05249f | 330 | int i = 0; |
samux | 22:f4aac491ea26 | 331 | int length_buffer = 0; |
samux | 22:f4aac491ea26 | 332 | uint32_t len_msg; |
samux | 22:f4aac491ea26 | 333 | char opcode = 0; |
samux | 22:f4aac491ea26 | 334 | char mask[4] = {0, 0, 0, 0}; |
samux | 22:f4aac491ea26 | 335 | Timer tmr; |
samux | 6:01a1eb7c0145 | 336 | |
samux | 16:d4518b50f653 | 337 | if (netif == WIF) { |
samux | 22:f4aac491ea26 | 338 | length_buffer = wifi->readable(); |
samux | 22:f4aac491ea26 | 339 | |
samux | 22:f4aac491ea26 | 340 | if (length_buffer > 1) { |
samux | 22:f4aac491ea26 | 341 | // read the opcode |
samux | 22:f4aac491ea26 | 342 | tmr.start(); |
samux | 22:f4aac491ea26 | 343 | while (true) { |
samux | 22:f4aac491ea26 | 344 | if (tmr.read() > 3) { |
samux | 22:f4aac491ea26 | 345 | return false; |
samux | 22:f4aac491ea26 | 346 | } |
samux | 22:f4aac491ea26 | 347 | if (wifi->readable()) { |
samux | 22:f4aac491ea26 | 348 | opcode = wifi->getc(); |
samux | 22:f4aac491ea26 | 349 | } |
samux | 22:f4aac491ea26 | 350 | if (opcode == 0x81) { |
samux | 22:f4aac491ea26 | 351 | break; |
samux | 22:f4aac491ea26 | 352 | } |
samux | 22:f4aac491ea26 | 353 | } |
samux | 22:f4aac491ea26 | 354 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 355 | printf("opcode: 0x%X\r\n", opcode); |
samux | 22:f4aac491ea26 | 356 | #endif |
samux | 22:f4aac491ea26 | 357 | len_msg = wifi->getc() & 0x7f; |
samux | 22:f4aac491ea26 | 358 | if (len_msg == 126) { |
samux | 22:f4aac491ea26 | 359 | len_msg = wifi->getc(); |
samux | 22:f4aac491ea26 | 360 | len_msg += wifi->getc() << 8; |
samux | 22:f4aac491ea26 | 361 | } else if (len_msg == 127) { |
samux | 22:f4aac491ea26 | 362 | len_msg = 0; |
samux | 22:f4aac491ea26 | 363 | for (int i = 0; i < 8; i++) { |
samux | 22:f4aac491ea26 | 364 | len_msg += wifi->getc() << i*8; |
samux | 22:f4aac491ea26 | 365 | } |
samux | 22:f4aac491ea26 | 366 | } |
samux | 22:f4aac491ea26 | 367 | if(len_msg == 0) { |
samux | 22:f4aac491ea26 | 368 | return false; |
samux | 22:f4aac491ea26 | 369 | } |
samux | 22:f4aac491ea26 | 370 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 371 | printf("length: %d\r\n", len_msg); |
samux | 22:f4aac491ea26 | 372 | #endif |
samux | 22:f4aac491ea26 | 373 | if ((len_msg & 0x80)) { |
samux | 22:f4aac491ea26 | 374 | for (int i = 0; i < 4; i++) |
samux | 22:f4aac491ea26 | 375 | mask[i] = wifi->getc(); |
samux | 22:f4aac491ea26 | 376 | } |
samux | 22:f4aac491ea26 | 377 | } else |
samux | 16:d4518b50f653 | 378 | return false; |
samux | 15:79bfbc0ad6bc | 379 | |
samux | 22:f4aac491ea26 | 380 | |
samux | 22:f4aac491ea26 | 381 | for (i = 0; i < len_msg; i++) { |
samux | 22:f4aac491ea26 | 382 | message[i] = wifi->getc() ^ mask[i % 4]; |
samux | 15:79bfbc0ad6bc | 383 | } |
samux | 16:d4518b50f653 | 384 | |
samux | 22:f4aac491ea26 | 385 | message[len_msg] = 0; |
samux | 22:f4aac491ea26 | 386 | return true; |
samux | 22:f4aac491ea26 | 387 | } |
samux | 22:f4aac491ea26 | 388 | #ifdef TARGET_LPC1768 |
samux | 22:f4aac491ea26 | 389 | else if (netif == ETH) { |
samux | 16:d4518b50f653 | 390 | |
samux | 22:f4aac491ea26 | 391 | uint32_t index = 0; |
samux | 15:79bfbc0ad6bc | 392 | Net::poll(); |
samux | 9:9fa055ed54b4 | 393 | |
samux | 16:d4518b50f653 | 394 | if (new_msg) { |
samux | 22:f4aac491ea26 | 395 | tmr.start(); |
samux | 22:f4aac491ea26 | 396 | // read the opcode |
samux | 22:f4aac491ea26 | 397 | while (true) { |
samux | 22:f4aac491ea26 | 398 | if (tmr.read() > 3) { |
samux | 22:f4aac491ea26 | 399 | return false; |
samux | 22:f4aac491ea26 | 400 | } |
samux | 22:f4aac491ea26 | 401 | opcode = eth_rx[index++]; |
samux | 22:f4aac491ea26 | 402 | if (opcode == 0x81) { |
samux | 22:f4aac491ea26 | 403 | break; |
samux | 22:f4aac491ea26 | 404 | } |
samux | 22:f4aac491ea26 | 405 | } |
samux | 22:f4aac491ea26 | 406 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 407 | printf("opcode: 0x%X\r\n", opcode); |
samux | 22:f4aac491ea26 | 408 | #endif |
samux | 22:f4aac491ea26 | 409 | |
samux | 22:f4aac491ea26 | 410 | len_msg = eth_rx[index++] & 0x7f; |
samux | 22:f4aac491ea26 | 411 | if (len_msg == 126) { |
samux | 22:f4aac491ea26 | 412 | len_msg = eth_rx[index++]; |
samux | 22:f4aac491ea26 | 413 | len_msg += eth_rx[index++] << 8; |
samux | 22:f4aac491ea26 | 414 | } else if (len_msg == 127) { |
samux | 22:f4aac491ea26 | 415 | len_msg = 0; |
samux | 22:f4aac491ea26 | 416 | for (int i = 0; i < 8; i++) { |
samux | 22:f4aac491ea26 | 417 | len_msg += eth_rx[index++] << i*8; |
samux | 22:f4aac491ea26 | 418 | } |
samux | 22:f4aac491ea26 | 419 | } |
samux | 22:f4aac491ea26 | 420 | if(len_msg == 0) { |
samux | 16:d4518b50f653 | 421 | return false; |
samux | 16:d4518b50f653 | 422 | } |
samux | 22:f4aac491ea26 | 423 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 424 | printf("length: %d\r\n", len_msg); |
samux | 22:f4aac491ea26 | 425 | #endif |
samux | 22:f4aac491ea26 | 426 | if ((len_msg & 0x80)) { |
samux | 22:f4aac491ea26 | 427 | for (int i = 0; i < 4; i++) |
samux | 22:f4aac491ea26 | 428 | mask[i] = eth_rx[index++]; |
samux | 16:d4518b50f653 | 429 | } |
samux | 22:f4aac491ea26 | 430 | |
samux | 22:f4aac491ea26 | 431 | for (i = 0; i < len_msg; i++) { |
samux | 22:f4aac491ea26 | 432 | message[i] = eth_rx[index++] ^ mask[i % 4]; |
samux | 22:f4aac491ea26 | 433 | } |
samux | 22:f4aac491ea26 | 434 | |
samux | 22:f4aac491ea26 | 435 | message[len_msg] = 0; |
samux | 16:d4518b50f653 | 436 | new_msg = false; |
samux | 16:d4518b50f653 | 437 | return true; |
samux | 9:9fa055ed54b4 | 438 | } |
samux | 16:d4518b50f653 | 439 | return false; |
samux | 0:21fe3b05249f | 440 | } |
samux | 22:f4aac491ea26 | 441 | #endif //target |
samux | 22:f4aac491ea26 | 442 | //the program shouldn't be here |
samux | 16:d4518b50f653 | 443 | return false; |
samux | 0:21fe3b05249f | 444 | } |
samux | 0:21fe3b05249f | 445 | |
samux | 6:01a1eb7c0145 | 446 | bool Websocket::close() { |
samux | 16:d4518b50f653 | 447 | if (netif == WIF) { |
samux | 19:ca8c5ad59850 | 448 | if (!wifi->cmdMode()) { |
samux | 16:d4518b50f653 | 449 | #ifdef DEBUG |
samux | 9:9fa055ed54b4 | 450 | printf("Websocket::close: cannot enter in cmd mode\r\n"); |
samux | 16:d4518b50f653 | 451 | #endif |
samux | 9:9fa055ed54b4 | 452 | return false; |
samux | 9:9fa055ed54b4 | 453 | } |
samux | 6:01a1eb7c0145 | 454 | |
samux | 19:ca8c5ad59850 | 455 | wifi->send("close\r", "NO"); |
samux | 6:01a1eb7c0145 | 456 | |
samux | 9:9fa055ed54b4 | 457 | if (!wifi->exit()) |
samux | 9:9fa055ed54b4 | 458 | return false; |
samux | 22:f4aac491ea26 | 459 | } |
samux | 22:f4aac491ea26 | 460 | #ifdef TARGET_LPC1768 |
samux | 22:f4aac491ea26 | 461 | else if (netif == ETH) { |
samux | 16:d4518b50f653 | 462 | |
samux | 16:d4518b50f653 | 463 | if (sock->close()) |
samux | 9:9fa055ed54b4 | 464 | return false; |
samux | 16:d4518b50f653 | 465 | return true; |
samux | 9:9fa055ed54b4 | 466 | } |
samux | 22:f4aac491ea26 | 467 | #endif //target |
samux | 16:d4518b50f653 | 468 | //the program shouldn't be here |
samux | 16:d4518b50f653 | 469 | return false; |
samux | 0:21fe3b05249f | 470 | } |
samux | 0:21fe3b05249f | 471 | |
samux | 0:21fe3b05249f | 472 | |
samux | 0:21fe3b05249f | 473 | |
samux | 6:01a1eb7c0145 | 474 | bool Websocket::connected() { |
samux | 16:d4518b50f653 | 475 | if (netif == WIF) { |
samux | 6:01a1eb7c0145 | 476 | char str[10]; |
samux | 6:01a1eb7c0145 | 477 | |
samux | 22:f4aac491ea26 | 478 | // we have to wait at least 0.25s to enter in cmd mode whan we was sending tcp packets |
samux | 6:01a1eb7c0145 | 479 | wait(0.25); |
samux | 19:ca8c5ad59850 | 480 | if (!wifi->cmdMode()) { |
samux | 16:d4518b50f653 | 481 | #ifdef DEBUG |
samux | 6:01a1eb7c0145 | 482 | printf("Websocket::connected: cannot enter in cmd mode\r\n"); |
samux | 16:d4518b50f653 | 483 | #endif |
samux | 0:21fe3b05249f | 484 | return false; |
samux | 3:9b00db719afa | 485 | } |
samux | 6:01a1eb7c0145 | 486 | wait(0.25); |
samux | 6:01a1eb7c0145 | 487 | |
samux | 19:ca8c5ad59850 | 488 | wifi->send("show c\r\n", "NO", str); |
samux | 22:f4aac491ea26 | 489 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 490 | printf("Websocket::connected: str: %s\r\n", str); |
samux | 22:f4aac491ea26 | 491 | #endif |
samux | 6:01a1eb7c0145 | 492 | |
samux | 6:01a1eb7c0145 | 493 | if (str[3] == '1') { |
samux | 6:01a1eb7c0145 | 494 | if (!wifi->exit()) { |
samux | 16:d4518b50f653 | 495 | #ifdef DEBUG |
samux | 6:01a1eb7c0145 | 496 | printf("Websocket::connected: cannot exit\r\n"); |
samux | 16:d4518b50f653 | 497 | #endif |
samux | 6:01a1eb7c0145 | 498 | return false; |
samux | 6:01a1eb7c0145 | 499 | } |
samux | 6:01a1eb7c0145 | 500 | return true; |
samux | 6:01a1eb7c0145 | 501 | } |
samux | 16:d4518b50f653 | 502 | if (!wifi->exit()) { |
samux | 16:d4518b50f653 | 503 | #ifdef DEBUG |
samux | 6:01a1eb7c0145 | 504 | printf("Websocket::connected: cannot exit\r\n"); |
samux | 16:d4518b50f653 | 505 | #endif |
samux | 16:d4518b50f653 | 506 | } |
samux | 6:01a1eb7c0145 | 507 | return false; |
samux | 22:f4aac491ea26 | 508 | } |
samux | 22:f4aac491ea26 | 509 | #ifdef TARGET_LPC1768 |
samux | 22:f4aac491ea26 | 510 | else if (netif == ETH) { |
samux | 6:01a1eb7c0145 | 511 | return eth_connected; |
samux | 16:d4518b50f653 | 512 | } |
samux | 22:f4aac491ea26 | 513 | #endif //target |
samux | 16:d4518b50f653 | 514 | //the program shouldn't be here |
samux | 16:d4518b50f653 | 515 | return false; |
samux | 6:01a1eb7c0145 | 516 | } |
samux | 6:01a1eb7c0145 | 517 | |
samux | 22:f4aac491ea26 | 518 | std::string Websocket::getPath() { |
samux | 17:1e339933c97a | 519 | return path; |
samux | 17:1e339933c97a | 520 | } |
samux | 17:1e339933c97a | 521 | |
samux | 6:01a1eb7c0145 | 522 | |
samux | 6:01a1eb7c0145 | 523 | |
samux | 6:01a1eb7c0145 | 524 | |
samux | 22:f4aac491ea26 | 525 | #ifdef TARGET_LPC1768 |
samux | 6:01a1eb7c0145 | 526 | void Websocket::onTCPSocketEvent(TCPSocketEvent e) { |
samux | 9:9fa055ed54b4 | 527 | if (e == TCPSOCKET_CONNECTED) { |
samux | 9:9fa055ed54b4 | 528 | eth_connected = true; |
samux | 16:d4518b50f653 | 529 | #ifdef DEBUG |
samux | 9:9fa055ed54b4 | 530 | printf("TCP Socket Connected\r\n"); |
samux | 16:d4518b50f653 | 531 | #endif |
samux | 12:1f6b9451a608 | 532 | } else if (e == TCPSOCKET_WRITEABLE) { |
samux | 12:1f6b9451a608 | 533 | } else if (e == TCPSOCKET_READABLE) { |
samux | 16:d4518b50f653 | 534 | int len = sock->recv(eth_rx, 512); |
samux | 9:9fa055ed54b4 | 535 | eth_rx[len] = 0; |
samux | 16:d4518b50f653 | 536 | new_msg = true; |
samux | 9:9fa055ed54b4 | 537 | if (!response_server_eth) { |
samux | 9:9fa055ed54b4 | 538 | string checking; |
samux | 9:9fa055ed54b4 | 539 | size_t found = string::npos; |
samux | 9:9fa055ed54b4 | 540 | checking = eth_rx; |
samux | 22:f4aac491ea26 | 541 | found = checking.find("DdLWT/1JcX+nQFHebYP+rqEx5xI="); |
samux | 9:9fa055ed54b4 | 542 | if (found != string::npos) |
samux | 9:9fa055ed54b4 | 543 | response_server_eth = true; |
samux | 9:9fa055ed54b4 | 544 | } |
samux | 12:1f6b9451a608 | 545 | } else { |
samux | 16:d4518b50f653 | 546 | #ifdef DEBUG |
samux | 9:9fa055ed54b4 | 547 | printf("TCP Socket Fail\r\n"); |
samux | 16:d4518b50f653 | 548 | #endif |
samux | 9:9fa055ed54b4 | 549 | eth_connected = false; |
samux | 0:21fe3b05249f | 550 | } |
samux | 6:01a1eb7c0145 | 551 | } |
samux | 22:f4aac491ea26 | 552 | #endif //target |
samux | 6:01a1eb7c0145 | 553 | |
samux | 6:01a1eb7c0145 | 554 |