WebSocket client library
Websocket.cpp@23:80bffaf28bef, 2012-03-19 (annotated)
- Committer:
- samux
- Date:
- Mon Mar 19 13:53:13 2012 +0000
- Revision:
- 23:80bffaf28bef
- Parent:
- 22:f4aac491ea26
- Child:
- 24:15112fd8ad45
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 | 23:80bffaf28bef | 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 | 23:80bffaf28bef | 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 | 23:80bffaf28bef | 280 | sendChar((len >> 8) & 0xff); |
samux | 22:f4aac491ea26 | 281 | sendChar(len & 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 | 23:80bffaf28bef | 332 | uint64_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 | 23:80bffaf28bef | 359 | len_msg = (wifi->getc() << 8); |
samux | 23:80bffaf28bef | 360 | len_msg += wifi->getc(); |
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 | 23:80bffaf28bef | 364 | len_msg += (wifi->getc() << (7-i)*8); |
samux | 22:f4aac491ea26 | 365 | } |
samux | 22:f4aac491ea26 | 366 | } |
samux | 23:80bffaf28bef | 367 | if (len_msg == 0) { |
samux | 22:f4aac491ea26 | 368 | return false; |
samux | 22:f4aac491ea26 | 369 | } |
samux | 22:f4aac491ea26 | 370 | #ifdef DEBUG |
samux | 23:80bffaf28bef | 371 | printf("length: %lld\r\n", len_msg); |
samux | 22:f4aac491ea26 | 372 | #endif |
samux | 22:f4aac491ea26 | 373 | if ((len_msg & 0x80)) { |
samux | 23:80bffaf28bef | 374 | #ifdef DEBUG |
samux | 23:80bffaf28bef | 375 | printf("will read mask\r\n"); |
samux | 23:80bffaf28bef | 376 | #endif |
samux | 23:80bffaf28bef | 377 | for (int i = 0; i < 4; i++) { |
samux | 22:f4aac491ea26 | 378 | mask[i] = wifi->getc(); |
samux | 23:80bffaf28bef | 379 | #ifdef DEBUG |
samux | 23:80bffaf28bef | 380 | printf("mask[%d]: %d\r\n", i, mask[i]); |
samux | 23:80bffaf28bef | 381 | #endif |
samux | 23:80bffaf28bef | 382 | } |
samux | 23:80bffaf28bef | 383 | } else { |
samux | 23:80bffaf28bef | 384 | #ifdef DEBUG |
samux | 23:80bffaf28bef | 385 | printf("len not 0x80\r\n"); |
samux | 23:80bffaf28bef | 386 | #endif |
samux | 22:f4aac491ea26 | 387 | } |
samux | 23:80bffaf28bef | 388 | } else { |
samux | 16:d4518b50f653 | 389 | return false; |
samux | 23:80bffaf28bef | 390 | } |
samux | 15:79bfbc0ad6bc | 391 | |
samux | 22:f4aac491ea26 | 392 | |
samux | 22:f4aac491ea26 | 393 | for (i = 0; i < len_msg; i++) { |
samux | 22:f4aac491ea26 | 394 | message[i] = wifi->getc() ^ mask[i % 4]; |
samux | 15:79bfbc0ad6bc | 395 | } |
samux | 16:d4518b50f653 | 396 | |
samux | 22:f4aac491ea26 | 397 | message[len_msg] = 0; |
samux | 22:f4aac491ea26 | 398 | return true; |
samux | 22:f4aac491ea26 | 399 | } |
samux | 22:f4aac491ea26 | 400 | #ifdef TARGET_LPC1768 |
samux | 22:f4aac491ea26 | 401 | else if (netif == ETH) { |
samux | 16:d4518b50f653 | 402 | |
samux | 22:f4aac491ea26 | 403 | uint32_t index = 0; |
samux | 15:79bfbc0ad6bc | 404 | Net::poll(); |
samux | 9:9fa055ed54b4 | 405 | |
samux | 16:d4518b50f653 | 406 | if (new_msg) { |
samux | 22:f4aac491ea26 | 407 | tmr.start(); |
samux | 22:f4aac491ea26 | 408 | // read the opcode |
samux | 22:f4aac491ea26 | 409 | while (true) { |
samux | 22:f4aac491ea26 | 410 | if (tmr.read() > 3) { |
samux | 22:f4aac491ea26 | 411 | return false; |
samux | 22:f4aac491ea26 | 412 | } |
samux | 22:f4aac491ea26 | 413 | opcode = eth_rx[index++]; |
samux | 22:f4aac491ea26 | 414 | if (opcode == 0x81) { |
samux | 22:f4aac491ea26 | 415 | break; |
samux | 22:f4aac491ea26 | 416 | } |
samux | 22:f4aac491ea26 | 417 | } |
samux | 22:f4aac491ea26 | 418 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 419 | printf("opcode: 0x%X\r\n", opcode); |
samux | 22:f4aac491ea26 | 420 | #endif |
samux | 22:f4aac491ea26 | 421 | |
samux | 22:f4aac491ea26 | 422 | len_msg = eth_rx[index++] & 0x7f; |
samux | 22:f4aac491ea26 | 423 | if (len_msg == 126) { |
samux | 23:80bffaf28bef | 424 | len_msg = (eth_rx[index++] << 8); |
samux | 23:80bffaf28bef | 425 | len_msg += eth_rx[index++]; |
samux | 22:f4aac491ea26 | 426 | } else if (len_msg == 127) { |
samux | 22:f4aac491ea26 | 427 | len_msg = 0; |
samux | 22:f4aac491ea26 | 428 | for (int i = 0; i < 8; i++) { |
samux | 23:80bffaf28bef | 429 | len_msg += eth_rx[index++] << (7-i)*8; |
samux | 22:f4aac491ea26 | 430 | } |
samux | 22:f4aac491ea26 | 431 | } |
samux | 23:80bffaf28bef | 432 | if (len_msg == 0) { |
samux | 16:d4518b50f653 | 433 | return false; |
samux | 16:d4518b50f653 | 434 | } |
samux | 22:f4aac491ea26 | 435 | #ifdef DEBUG |
samux | 23:80bffaf28bef | 436 | printf("length: %lld\r\n", len_msg); |
samux | 22:f4aac491ea26 | 437 | #endif |
samux | 22:f4aac491ea26 | 438 | if ((len_msg & 0x80)) { |
samux | 22:f4aac491ea26 | 439 | for (int i = 0; i < 4; i++) |
samux | 22:f4aac491ea26 | 440 | mask[i] = eth_rx[index++]; |
samux | 16:d4518b50f653 | 441 | } |
samux | 22:f4aac491ea26 | 442 | |
samux | 22:f4aac491ea26 | 443 | for (i = 0; i < len_msg; i++) { |
samux | 22:f4aac491ea26 | 444 | message[i] = eth_rx[index++] ^ mask[i % 4]; |
samux | 22:f4aac491ea26 | 445 | } |
samux | 22:f4aac491ea26 | 446 | |
samux | 22:f4aac491ea26 | 447 | message[len_msg] = 0; |
samux | 16:d4518b50f653 | 448 | new_msg = false; |
samux | 16:d4518b50f653 | 449 | return true; |
samux | 9:9fa055ed54b4 | 450 | } |
samux | 16:d4518b50f653 | 451 | return false; |
samux | 0:21fe3b05249f | 452 | } |
samux | 22:f4aac491ea26 | 453 | #endif //target |
samux | 22:f4aac491ea26 | 454 | //the program shouldn't be here |
samux | 16:d4518b50f653 | 455 | return false; |
samux | 0:21fe3b05249f | 456 | } |
samux | 0:21fe3b05249f | 457 | |
samux | 6:01a1eb7c0145 | 458 | bool Websocket::close() { |
samux | 23:80bffaf28bef | 459 | sendOpcode(0x08); |
samux | 23:80bffaf28bef | 460 | sendLength(0); |
samux | 23:80bffaf28bef | 461 | sendMask(); |
samux | 16:d4518b50f653 | 462 | if (netif == WIF) { |
samux | 23:80bffaf28bef | 463 | |
samux | 23:80bffaf28bef | 464 | wait(0.25); |
samux | 19:ca8c5ad59850 | 465 | if (!wifi->cmdMode()) { |
samux | 16:d4518b50f653 | 466 | #ifdef DEBUG |
samux | 9:9fa055ed54b4 | 467 | printf("Websocket::close: cannot enter in cmd mode\r\n"); |
samux | 16:d4518b50f653 | 468 | #endif |
samux | 9:9fa055ed54b4 | 469 | return false; |
samux | 9:9fa055ed54b4 | 470 | } |
samux | 23:80bffaf28bef | 471 | wait(0.25); |
samux | 6:01a1eb7c0145 | 472 | |
samux | 19:ca8c5ad59850 | 473 | wifi->send("close\r", "NO"); |
samux | 6:01a1eb7c0145 | 474 | |
samux | 9:9fa055ed54b4 | 475 | if (!wifi->exit()) |
samux | 9:9fa055ed54b4 | 476 | return false; |
samux | 22:f4aac491ea26 | 477 | } |
samux | 22:f4aac491ea26 | 478 | #ifdef TARGET_LPC1768 |
samux | 22:f4aac491ea26 | 479 | else if (netif == ETH) { |
samux | 23:80bffaf28bef | 480 | #ifdef TARGET_LPC1768 |
samux | 23:80bffaf28bef | 481 | Net::poll(); |
samux | 23:80bffaf28bef | 482 | #endif //target |
samux | 16:d4518b50f653 | 483 | |
samux | 16:d4518b50f653 | 484 | if (sock->close()) |
samux | 9:9fa055ed54b4 | 485 | return false; |
samux | 16:d4518b50f653 | 486 | return true; |
samux | 9:9fa055ed54b4 | 487 | } |
samux | 22:f4aac491ea26 | 488 | #endif //target |
samux | 16:d4518b50f653 | 489 | //the program shouldn't be here |
samux | 16:d4518b50f653 | 490 | return false; |
samux | 0:21fe3b05249f | 491 | } |
samux | 0:21fe3b05249f | 492 | |
samux | 0:21fe3b05249f | 493 | |
samux | 0:21fe3b05249f | 494 | |
samux | 6:01a1eb7c0145 | 495 | bool Websocket::connected() { |
samux | 16:d4518b50f653 | 496 | if (netif == WIF) { |
samux | 6:01a1eb7c0145 | 497 | char str[10]; |
samux | 6:01a1eb7c0145 | 498 | |
samux | 22:f4aac491ea26 | 499 | // we have to wait at least 0.25s to enter in cmd mode whan we was sending tcp packets |
samux | 6:01a1eb7c0145 | 500 | wait(0.25); |
samux | 19:ca8c5ad59850 | 501 | if (!wifi->cmdMode()) { |
samux | 16:d4518b50f653 | 502 | #ifdef DEBUG |
samux | 6:01a1eb7c0145 | 503 | printf("Websocket::connected: cannot enter in cmd mode\r\n"); |
samux | 16:d4518b50f653 | 504 | #endif |
samux | 0:21fe3b05249f | 505 | return false; |
samux | 3:9b00db719afa | 506 | } |
samux | 6:01a1eb7c0145 | 507 | wait(0.25); |
samux | 6:01a1eb7c0145 | 508 | |
samux | 19:ca8c5ad59850 | 509 | wifi->send("show c\r\n", "NO", str); |
samux | 22:f4aac491ea26 | 510 | #ifdef DEBUG |
samux | 22:f4aac491ea26 | 511 | printf("Websocket::connected: str: %s\r\n", str); |
samux | 22:f4aac491ea26 | 512 | #endif |
samux | 6:01a1eb7c0145 | 513 | |
samux | 6:01a1eb7c0145 | 514 | if (str[3] == '1') { |
samux | 6:01a1eb7c0145 | 515 | if (!wifi->exit()) { |
samux | 16:d4518b50f653 | 516 | #ifdef DEBUG |
samux | 6:01a1eb7c0145 | 517 | printf("Websocket::connected: cannot exit\r\n"); |
samux | 16:d4518b50f653 | 518 | #endif |
samux | 6:01a1eb7c0145 | 519 | return false; |
samux | 6:01a1eb7c0145 | 520 | } |
samux | 6:01a1eb7c0145 | 521 | return true; |
samux | 6:01a1eb7c0145 | 522 | } |
samux | 16:d4518b50f653 | 523 | if (!wifi->exit()) { |
samux | 16:d4518b50f653 | 524 | #ifdef DEBUG |
samux | 6:01a1eb7c0145 | 525 | printf("Websocket::connected: cannot exit\r\n"); |
samux | 16:d4518b50f653 | 526 | #endif |
samux | 16:d4518b50f653 | 527 | } |
samux | 6:01a1eb7c0145 | 528 | return false; |
samux | 22:f4aac491ea26 | 529 | } |
samux | 22:f4aac491ea26 | 530 | #ifdef TARGET_LPC1768 |
samux | 22:f4aac491ea26 | 531 | else if (netif == ETH) { |
samux | 6:01a1eb7c0145 | 532 | return eth_connected; |
samux | 16:d4518b50f653 | 533 | } |
samux | 22:f4aac491ea26 | 534 | #endif //target |
samux | 16:d4518b50f653 | 535 | //the program shouldn't be here |
samux | 16:d4518b50f653 | 536 | return false; |
samux | 6:01a1eb7c0145 | 537 | } |
samux | 6:01a1eb7c0145 | 538 | |
samux | 23:80bffaf28bef | 539 | |
samux | 22:f4aac491ea26 | 540 | std::string Websocket::getPath() { |
samux | 17:1e339933c97a | 541 | return path; |
samux | 17:1e339933c97a | 542 | } |
samux | 17:1e339933c97a | 543 | |
samux | 6:01a1eb7c0145 | 544 | |
samux | 6:01a1eb7c0145 | 545 | |
samux | 6:01a1eb7c0145 | 546 | |
samux | 22:f4aac491ea26 | 547 | #ifdef TARGET_LPC1768 |
samux | 6:01a1eb7c0145 | 548 | void Websocket::onTCPSocketEvent(TCPSocketEvent e) { |
samux | 9:9fa055ed54b4 | 549 | if (e == TCPSOCKET_CONNECTED) { |
samux | 9:9fa055ed54b4 | 550 | eth_connected = true; |
samux | 16:d4518b50f653 | 551 | #ifdef DEBUG |
samux | 9:9fa055ed54b4 | 552 | printf("TCP Socket Connected\r\n"); |
samux | 16:d4518b50f653 | 553 | #endif |
samux | 12:1f6b9451a608 | 554 | } else if (e == TCPSOCKET_WRITEABLE) { |
samux | 12:1f6b9451a608 | 555 | } else if (e == TCPSOCKET_READABLE) { |
samux | 16:d4518b50f653 | 556 | int len = sock->recv(eth_rx, 512); |
samux | 9:9fa055ed54b4 | 557 | eth_rx[len] = 0; |
samux | 16:d4518b50f653 | 558 | new_msg = true; |
samux | 9:9fa055ed54b4 | 559 | if (!response_server_eth) { |
samux | 9:9fa055ed54b4 | 560 | string checking; |
samux | 9:9fa055ed54b4 | 561 | size_t found = string::npos; |
samux | 9:9fa055ed54b4 | 562 | checking = eth_rx; |
samux | 22:f4aac491ea26 | 563 | found = checking.find("DdLWT/1JcX+nQFHebYP+rqEx5xI="); |
samux | 9:9fa055ed54b4 | 564 | if (found != string::npos) |
samux | 9:9fa055ed54b4 | 565 | response_server_eth = true; |
samux | 9:9fa055ed54b4 | 566 | } |
samux | 12:1f6b9451a608 | 567 | } else { |
samux | 16:d4518b50f653 | 568 | #ifdef DEBUG |
samux | 9:9fa055ed54b4 | 569 | printf("TCP Socket Fail\r\n"); |
samux | 16:d4518b50f653 | 570 | #endif |
samux | 9:9fa055ed54b4 | 571 | eth_connected = false; |
samux | 0:21fe3b05249f | 572 | } |
samux | 6:01a1eb7c0145 | 573 | } |
samux | 22:f4aac491ea26 | 574 | #endif //target |
samux | 6:01a1eb7c0145 | 575 | |
samux | 6:01a1eb7c0145 | 576 |