Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Websocket.cpp@0:664899e0b988, 2012-06-30 (annotated)
- Committer:
- uci1
- Date:
- Sat Jun 30 02:03:51 2012 +0000
- Revision:
- 0:664899e0b988
- Child:
- 1:e392595b4b76
first version. SD card writing and data readout works. communications not tested.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 0:664899e0b988 | 1 | #include "Websocket.h" |
uci1 | 0:664899e0b988 | 2 | #include <string> |
uci1 | 0:664899e0b988 | 3 | |
uci1 | 0:664899e0b988 | 4 | //#define DEBUG |
uci1 | 0:664899e0b988 | 5 | |
uci1 | 0:664899e0b988 | 6 | |
uci1 | 0:664899e0b988 | 7 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 8 | Websocket::Websocket(char * url) { |
uci1 | 0:664899e0b988 | 9 | server_ip = NULL; |
uci1 | 0:664899e0b988 | 10 | netif = ETH; |
uci1 | 0:664899e0b988 | 11 | eth_writeable = false; |
uci1 | 0:664899e0b988 | 12 | eth_readable = false; |
uci1 | 0:664899e0b988 | 13 | eth_connected = false; |
uci1 | 0:664899e0b988 | 14 | response_server_eth = false; |
uci1 | 0:664899e0b988 | 15 | new_msg = false; |
uci1 | 0:664899e0b988 | 16 | fillFields(url); |
uci1 | 0:664899e0b988 | 17 | |
uci1 | 0:664899e0b988 | 18 | eth = new EthernetNetIf( |
uci1 | 0:664899e0b988 | 19 | IpAddr(128,195,204,148), //IP Address |
uci1 | 0:664899e0b988 | 20 | IpAddr(255,255,255,0), //Network Mask |
uci1 | 0:664899e0b988 | 21 | IpAddr(128,195,204,1), //Gateway |
uci1 | 0:664899e0b988 | 22 | IpAddr(128,200,1,201) //DNS |
uci1 | 0:664899e0b988 | 23 | ); |
uci1 | 0:664899e0b988 | 24 | sock = new TCPSocket(); |
uci1 | 0:664899e0b988 | 25 | |
uci1 | 0:664899e0b988 | 26 | EthernetErr ethErr = eth->setup(); |
uci1 | 0:664899e0b988 | 27 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 28 | if (ethErr) { |
uci1 | 0:664899e0b988 | 29 | printf("\r\nERROR %d in setup.\r\n", ethErr); |
uci1 | 0:664899e0b988 | 30 | } |
uci1 | 0:664899e0b988 | 31 | #endif |
uci1 | 0:664899e0b988 | 32 | |
uci1 | 0:664899e0b988 | 33 | //we must use dnsresolver to find the ip address |
uci1 | 0:664899e0b988 | 34 | if (server_ip == NULL) { |
uci1 | 0:664899e0b988 | 35 | DNSResolver dr; |
uci1 | 0:664899e0b988 | 36 | server_ip = new IpAddr(); |
uci1 | 0:664899e0b988 | 37 | *server_ip = dr.resolveName(ip_domain.c_str()); |
uci1 | 0:664899e0b988 | 38 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 39 | printf("\r\nserver with dns=%d.%d.%d.%d\r\n", (*server_ip)[0], (*server_ip)[1], (*server_ip)[2], (*server_ip)[3]); |
uci1 | 0:664899e0b988 | 40 | #endif |
uci1 | 0:664899e0b988 | 41 | |
uci1 | 0:664899e0b988 | 42 | } |
uci1 | 0:664899e0b988 | 43 | |
uci1 | 0:664899e0b988 | 44 | IpAddr ipt = eth->getIp(); |
uci1 | 0:664899e0b988 | 45 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 46 | printf("\r\nmbed IP Address is %d.%d.%d.%d\r\n", ipt[0], ipt[1], ipt[2], ipt[3]); |
uci1 | 0:664899e0b988 | 47 | #endif |
uci1 | 0:664899e0b988 | 48 | |
uci1 | 0:664899e0b988 | 49 | sock->setOnEvent(this, &Websocket::onTCPSocketEvent); |
uci1 | 0:664899e0b988 | 50 | } |
uci1 | 0:664899e0b988 | 51 | #endif //target |
uci1 | 0:664899e0b988 | 52 | |
uci1 | 0:664899e0b988 | 53 | |
uci1 | 0:664899e0b988 | 54 | void Websocket::fillFields(char * url) |
uci1 | 0:664899e0b988 | 55 | { |
uci1 | 0:664899e0b988 | 56 | printf("FILLFIELDS\r\n"); |
uci1 | 0:664899e0b988 | 57 | char *res = NULL; |
uci1 | 0:664899e0b988 | 58 | char *res1 = NULL; |
uci1 | 0:664899e0b988 | 59 | |
uci1 | 0:664899e0b988 | 60 | char buf[50]; |
uci1 | 0:664899e0b988 | 61 | strcpy(buf, url); |
uci1 | 0:664899e0b988 | 62 | printf("\r\nBuf is:"); |
uci1 | 0:664899e0b988 | 63 | for(int i=0;i<50;i++) |
uci1 | 0:664899e0b988 | 64 | { |
uci1 | 0:664899e0b988 | 65 | printf("%d", buf[i]); |
uci1 | 0:664899e0b988 | 66 | } |
uci1 | 0:664899e0b988 | 67 | printf("\r\n"); |
uci1 | 0:664899e0b988 | 68 | res = strtok(buf, ":"); |
uci1 | 0:664899e0b988 | 69 | if (strcmp(res, "ws")) |
uci1 | 0:664899e0b988 | 70 | { |
uci1 | 0:664899e0b988 | 71 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 72 | printf("\r\nFormat error: please use: \"ws://ip-or-domain[:port]/path\"\r\n\r\n"); |
uci1 | 0:664899e0b988 | 73 | #endif |
uci1 | 0:664899e0b988 | 74 | } |
uci1 | 0:664899e0b988 | 75 | else |
uci1 | 0:664899e0b988 | 76 | { |
uci1 | 0:664899e0b988 | 77 | //ip_domain and port |
uci1 | 0:664899e0b988 | 78 | res = strtok(NULL, "/"); |
uci1 | 0:664899e0b988 | 79 | |
uci1 | 0:664899e0b988 | 80 | //path |
uci1 | 0:664899e0b988 | 81 | res1 = strtok(NULL, " "); |
uci1 | 0:664899e0b988 | 82 | if (res1 != NULL) |
uci1 | 0:664899e0b988 | 83 | { |
uci1 | 0:664899e0b988 | 84 | path = res1; |
uci1 | 0:664899e0b988 | 85 | } |
uci1 | 0:664899e0b988 | 86 | |
uci1 | 0:664899e0b988 | 87 | //ip_domain |
uci1 | 0:664899e0b988 | 88 | res = strtok(res, ":"); |
uci1 | 0:664899e0b988 | 89 | |
uci1 | 0:664899e0b988 | 90 | //port |
uci1 | 0:664899e0b988 | 91 | res1 = strtok(NULL, " "); |
uci1 | 0:664899e0b988 | 92 | //port |
uci1 | 0:664899e0b988 | 93 | if (res1 != NULL) |
uci1 | 0:664899e0b988 | 94 | { |
uci1 | 0:664899e0b988 | 95 | port = res1; |
uci1 | 0:664899e0b988 | 96 | } else |
uci1 | 0:664899e0b988 | 97 | { |
uci1 | 0:664899e0b988 | 98 | port = "80"; |
uci1 | 0:664899e0b988 | 99 | } |
uci1 | 0:664899e0b988 | 100 | |
uci1 | 0:664899e0b988 | 101 | if (res != NULL) |
uci1 | 0:664899e0b988 | 102 | { |
uci1 | 0:664899e0b988 | 103 | ip_domain = res; |
uci1 | 0:664899e0b988 | 104 | |
uci1 | 0:664899e0b988 | 105 | //if we use ethernet, we must decode ip address or use dnsresolver |
uci1 | 0:664899e0b988 | 106 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 107 | if (netif == ETH) { |
uci1 | 0:664899e0b988 | 108 | strcpy(buf, res); |
uci1 | 0:664899e0b988 | 109 | //we try to decode the ip address |
uci1 | 0:664899e0b988 | 110 | if (buf[0] >= '0' && buf[0] <= '9') { |
uci1 | 0:664899e0b988 | 111 | res = strtok(buf, "."); |
uci1 | 0:664899e0b988 | 112 | int i = 0; |
uci1 | 0:664899e0b988 | 113 | int ip[4]; |
uci1 | 0:664899e0b988 | 114 | while (res != NULL) { |
uci1 | 0:664899e0b988 | 115 | ip[i] = atoi(res); |
uci1 | 0:664899e0b988 | 116 | res = strtok(NULL, "."); |
uci1 | 0:664899e0b988 | 117 | i++; |
uci1 | 0:664899e0b988 | 118 | } |
uci1 | 0:664899e0b988 | 119 | server_ip = new IpAddr(ip[0], ip[1], ip[2], ip[3]); |
uci1 | 0:664899e0b988 | 120 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 121 | printf("server without dns=%i.%i.%i.%i\n",(*server_ip)[0],(*server_ip)[1],(*server_ip)[2],(*server_ip)[3]); |
uci1 | 0:664899e0b988 | 122 | #endif |
uci1 | 0:664899e0b988 | 123 | } |
uci1 | 0:664899e0b988 | 124 | } |
uci1 | 0:664899e0b988 | 125 | #endif //target |
uci1 | 0:664899e0b988 | 126 | } |
uci1 | 0:664899e0b988 | 127 | } |
uci1 | 0:664899e0b988 | 128 | } |
uci1 | 0:664899e0b988 | 129 | |
uci1 | 0:664899e0b988 | 130 | |
uci1 | 0:664899e0b988 | 131 | bool Websocket::connect(Timer* stop, const uint32_t timeout) |
uci1 | 0:664899e0b988 | 132 | { |
uci1 | 0:664899e0b988 | 133 | // printf("CONNECT() Function\r\n"); |
uci1 | 0:664899e0b988 | 134 | char cmd[50]; |
uci1 | 0:664899e0b988 | 135 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 136 | //M: else if (netif == ETH) |
uci1 | 0:664899e0b988 | 137 | if (netif == ETH) |
uci1 | 0:664899e0b988 | 138 | { |
uci1 | 0:664899e0b988 | 139 | Host server (*server_ip, atoi(port.c_str())); |
uci1 | 0:664899e0b988 | 140 | sock->close(); |
uci1 | 0:664899e0b988 | 141 | TCPSocketErr bindErr = sock->connect(server); |
uci1 | 0:664899e0b988 | 142 | if (bindErr) |
uci1 | 0:664899e0b988 | 143 | { |
uci1 | 0:664899e0b988 | 144 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 145 | printf("\r\nERROR binderr: %d\r\n", bindErr); |
uci1 | 0:664899e0b988 | 146 | #endif |
uci1 | 0:664899e0b988 | 147 | return false; |
uci1 | 0:664899e0b988 | 148 | } |
uci1 | 0:664899e0b988 | 149 | Timer tmr; |
uci1 | 0:664899e0b988 | 150 | tmr.start(); |
uci1 | 0:664899e0b988 | 151 | Timer myStop; |
uci1 | 0:664899e0b988 | 152 | if (stop==0) { |
uci1 | 0:664899e0b988 | 153 | stop = &myStop; |
uci1 | 0:664899e0b988 | 154 | } |
uci1 | 0:664899e0b988 | 155 | int i = 0; |
uci1 | 0:664899e0b988 | 156 | printf("I: %d\r\n", i); |
uci1 | 0:664899e0b988 | 157 | while (true) { |
uci1 | 0:664899e0b988 | 158 | Net::poll(); |
uci1 | 0:664899e0b988 | 159 | if (stop->read() > timeout) |
uci1 | 0:664899e0b988 | 160 | return false; |
uci1 | 0:664899e0b988 | 161 | if (tmr.read() > 0.05) { |
uci1 | 0:664899e0b988 | 162 | tmr.reset(); |
uci1 | 0:664899e0b988 | 163 | if (eth_connected) { |
uci1 | 0:664899e0b988 | 164 | switch (i) { |
uci1 | 0:664899e0b988 | 165 | case 0: |
uci1 | 0:664899e0b988 | 166 | sprintf(cmd, "GET /%s HTTP/1.1\r\n", path.c_str()); |
uci1 | 0:664899e0b988 | 167 | sock->send(cmd, strlen(cmd)); |
uci1 | 0:664899e0b988 | 168 | i++; |
uci1 | 0:664899e0b988 | 169 | break; |
uci1 | 0:664899e0b988 | 170 | case 1: |
uci1 | 0:664899e0b988 | 171 | sprintf(cmd, "Host: %s:%s\r\n", ip_domain.c_str(), port.c_str()); |
uci1 | 0:664899e0b988 | 172 | sock->send(cmd, strlen(cmd)); |
uci1 | 0:664899e0b988 | 173 | i++; |
uci1 | 0:664899e0b988 | 174 | break; |
uci1 | 0:664899e0b988 | 175 | case 2: |
uci1 | 0:664899e0b988 | 176 | sprintf(cmd, "Upgrade: WebSocket\r\n"); |
uci1 | 0:664899e0b988 | 177 | sock->send(cmd, strlen(cmd)); |
uci1 | 0:664899e0b988 | 178 | i++; |
uci1 | 0:664899e0b988 | 179 | break; |
uci1 | 0:664899e0b988 | 180 | case 3: |
uci1 | 0:664899e0b988 | 181 | sprintf(cmd, "Origin: null\r\n"); |
uci1 | 0:664899e0b988 | 182 | sock->send(cmd, strlen(cmd)); |
uci1 | 0:664899e0b988 | 183 | i++; |
uci1 | 0:664899e0b988 | 184 | break; |
uci1 | 0:664899e0b988 | 185 | case 4: |
uci1 | 0:664899e0b988 | 186 | sprintf(cmd, "Connection: Upgrade\r\n"); |
uci1 | 0:664899e0b988 | 187 | sock->send(cmd, strlen(cmd)); |
uci1 | 0:664899e0b988 | 188 | i++; |
uci1 | 0:664899e0b988 | 189 | break; |
uci1 | 0:664899e0b988 | 190 | case 5: |
uci1 | 0:664899e0b988 | 191 | sprintf(cmd, "Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n"); |
uci1 | 0:664899e0b988 | 192 | sock->send(cmd, strlen(cmd)); |
uci1 | 0:664899e0b988 | 193 | i++; |
uci1 | 0:664899e0b988 | 194 | break; |
uci1 | 0:664899e0b988 | 195 | case 6: |
uci1 | 0:664899e0b988 | 196 | sprintf(cmd, "Sec-WebSocket-Version: 13\r\n\r\n"); |
uci1 | 0:664899e0b988 | 197 | sock->send(cmd, strlen(cmd)); |
uci1 | 0:664899e0b988 | 198 | i++; |
uci1 | 0:664899e0b988 | 199 | break; |
uci1 | 0:664899e0b988 | 200 | case 7: |
uci1 | 0:664899e0b988 | 201 | if (response_server_eth) |
uci1 | 0:664899e0b988 | 202 | i++; |
uci1 | 0:664899e0b988 | 203 | else |
uci1 | 0:664899e0b988 | 204 | break; |
uci1 | 0:664899e0b988 | 205 | |
uci1 | 0:664899e0b988 | 206 | default: |
uci1 | 0:664899e0b988 | 207 | break; |
uci1 | 0:664899e0b988 | 208 | } |
uci1 | 0:664899e0b988 | 209 | } |
uci1 | 0:664899e0b988 | 210 | if (i==8) { |
uci1 | 0:664899e0b988 | 211 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 212 | 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()); |
uci1 | 0:664899e0b988 | 213 | #endif |
uci1 | 0:664899e0b988 | 214 | return true; |
uci1 | 0:664899e0b988 | 215 | } |
uci1 | 0:664899e0b988 | 216 | } |
uci1 | 0:664899e0b988 | 217 | } |
uci1 | 0:664899e0b988 | 218 | } |
uci1 | 0:664899e0b988 | 219 | #endif //target |
uci1 | 0:664899e0b988 | 220 | //the program shouldn't be here |
uci1 | 0:664899e0b988 | 221 | return false; |
uci1 | 0:664899e0b988 | 222 | } |
uci1 | 0:664899e0b988 | 223 | |
uci1 | 0:664899e0b988 | 224 | void Websocket::sendLength(uint32_t len) |
uci1 | 0:664899e0b988 | 225 | { |
uci1 | 0:664899e0b988 | 226 | //printf("SENDLENGTH(), Send Length: %d\r\n", len); |
uci1 | 0:664899e0b988 | 227 | if (len < 126) |
uci1 | 0:664899e0b988 | 228 | { |
uci1 | 0:664899e0b988 | 229 | sendChar(len | (1<<7)); |
uci1 | 0:664899e0b988 | 230 | } |
uci1 | 0:664899e0b988 | 231 | else if (len < 65535) |
uci1 | 0:664899e0b988 | 232 | { // use 2 bytes |
uci1 | 0:664899e0b988 | 233 | sendChar(126 | (1<<7)); |
uci1 | 0:664899e0b988 | 234 | //M: reverce these two lines |
uci1 | 0:664899e0b988 | 235 | sendChar((len >> 8) & 0xff); //2 |
uci1 | 0:664899e0b988 | 236 | sendChar(len & 0xff); //1 |
uci1 | 0:664899e0b988 | 237 | // sendChar((len >> 8) & 0xff); //2 |
uci1 | 0:664899e0b988 | 238 | } |
uci1 | 0:664899e0b988 | 239 | else |
uci1 | 0:664899e0b988 | 240 | { |
uci1 | 0:664899e0b988 | 241 | sendChar(127 | (1<<7)); |
uci1 | 0:664899e0b988 | 242 | for (int i = 0; i < 8; i++) |
uci1 | 0:664899e0b988 | 243 | { |
uci1 | 0:664899e0b988 | 244 | sendChar((len >> i*8) & 0xff); |
uci1 | 0:664899e0b988 | 245 | } |
uci1 | 0:664899e0b988 | 246 | } |
uci1 | 0:664899e0b988 | 247 | } |
uci1 | 0:664899e0b988 | 248 | |
uci1 | 0:664899e0b988 | 249 | int Websocket::sendChar(uint8_t c) |
uci1 | 0:664899e0b988 | 250 | { |
uci1 | 0:664899e0b988 | 251 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 252 | if (netif == ETH) { |
uci1 | 0:664899e0b988 | 253 | Net::poll(); |
uci1 | 0:664899e0b988 | 254 | return sock->send((const char *)&c, 1); |
uci1 | 0:664899e0b988 | 255 | // printf("SENDCHAR(), Sendchar %d \r\n", c); |
uci1 | 0:664899e0b988 | 256 | } |
uci1 | 0:664899e0b988 | 257 | #endif |
uci1 | 0:664899e0b988 | 258 | return 0; |
uci1 | 0:664899e0b988 | 259 | } |
uci1 | 0:664899e0b988 | 260 | |
uci1 | 0:664899e0b988 | 261 | void Websocket::sendOpcode(uint8_t opcode) |
uci1 | 0:664899e0b988 | 262 | { |
uci1 | 0:664899e0b988 | 263 | // printf("SENDOPCODE()\r\n"); |
uci1 | 0:664899e0b988 | 264 | sendChar(0x80 | (opcode & 0x0f)); |
uci1 | 0:664899e0b988 | 265 | // printf("SendOpcode: 0x%X\r\n", opcode); |
uci1 | 0:664899e0b988 | 266 | } |
uci1 | 0:664899e0b988 | 267 | |
uci1 | 0:664899e0b988 | 268 | /*Masking-key: 0 or 4 bytes |
uci1 | 0:664899e0b988 | 269 | All frames sent from the client to the server are masked by a 32- |
uci1 | 0:664899e0b988 | 270 | bit value that is contained within the frame. This field is |
uci1 | 0:664899e0b988 | 271 | present if the mask bit is set to 1, and is absent if the mask bit |
uci1 | 0:664899e0b988 | 272 | is set to 0. |
uci1 | 0:664899e0b988 | 273 | */ |
uci1 | 0:664899e0b988 | 274 | void Websocket::sendMask() |
uci1 | 0:664899e0b988 | 275 | { |
uci1 | 0:664899e0b988 | 276 | // printf("SENDMASK()\r\n"); |
uci1 | 0:664899e0b988 | 277 | for (int i = 0; i < 4; i++) { |
uci1 | 0:664899e0b988 | 278 | sendChar(0); //no frame masking |
uci1 | 0:664899e0b988 | 279 | } |
uci1 | 0:664899e0b988 | 280 | } |
uci1 | 0:664899e0b988 | 281 | |
uci1 | 0:664899e0b988 | 282 | void Websocket::send(char * str) |
uci1 | 0:664899e0b988 | 283 | { |
uci1 | 0:664899e0b988 | 284 | //printf("SEND()\r\n"); |
uci1 | 0:664899e0b988 | 285 | /* Opcode: 4 bits |
uci1 | 0:664899e0b988 | 286 | The opcode denotes the frame type of the WebSocket frame |
uci1 | 0:664899e0b988 | 287 | Defines the interpretation of the payload data. If an unknown |
uci1 | 0:664899e0b988 | 288 | opcode is received, the receiving endpoint MUST _Fail the |
uci1 | 0:664899e0b988 | 289 | WebSocket Connection_. The following values are defined. |
uci1 | 0:664899e0b988 | 290 | * %x0 denotes a continuation frame |
uci1 | 0:664899e0b988 | 291 | * %x1 denotes a text frame |
uci1 | 0:664899e0b988 | 292 | * %x2 denotes a binary frame |
uci1 | 0:664899e0b988 | 293 | * %x3-7 are reserved for further non-control frames |
uci1 | 0:664899e0b988 | 294 | * %x8 denotes a connection close |
uci1 | 0:664899e0b988 | 295 | * %x9 denotes a pingg |
uci1 | 0:664899e0b988 | 296 | * %xA denotes a pong |
uci1 | 0:664899e0b988 | 297 | * %xB-F are reserved for further control frames |
uci1 | 0:664899e0b988 | 298 | |
uci1 | 0:664899e0b988 | 299 | */ |
uci1 | 0:664899e0b988 | 300 | sendOpcode(0x01); |
uci1 | 0:664899e0b988 | 301 | sendLength(strlen(str)); |
uci1 | 0:664899e0b988 | 302 | sendMask(); |
uci1 | 0:664899e0b988 | 303 | |
uci1 | 0:664899e0b988 | 304 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 305 | //M: else if (netif == ETH) { |
uci1 | 0:664899e0b988 | 306 | if (netif == ETH) |
uci1 | 0:664899e0b988 | 307 | { |
uci1 | 0:664899e0b988 | 308 | Net::poll(); |
uci1 | 0:664899e0b988 | 309 | sock->send(str, strlen(str)); |
uci1 | 0:664899e0b988 | 310 | } |
uci1 | 0:664899e0b988 | 311 | #endif //target |
uci1 | 0:664899e0b988 | 312 | } |
uci1 | 0:664899e0b988 | 313 | |
uci1 | 0:664899e0b988 | 314 | bool Websocket::sendBinary(char* str, const uint32_t len) |
uci1 | 0:664899e0b988 | 315 | { |
uci1 | 0:664899e0b988 | 316 | // CJR: add function for sending binary |
uci1 | 0:664899e0b988 | 317 | |
uci1 | 0:664899e0b988 | 318 | //printf("SEND()\r\n"); |
uci1 | 0:664899e0b988 | 319 | /* Opcode: 4 bits |
uci1 | 0:664899e0b988 | 320 | The opcode denotes the frame type of the WebSocket frame |
uci1 | 0:664899e0b988 | 321 | Defines the interpretation of the payload data. If an unknown |
uci1 | 0:664899e0b988 | 322 | opcode is received, the receiving endpoint MUST _Fail the |
uci1 | 0:664899e0b988 | 323 | WebSocket Connection_. The following values are defined. |
uci1 | 0:664899e0b988 | 324 | * %x0 denotes a continuation frame |
uci1 | 0:664899e0b988 | 325 | * %x1 denotes a text frame |
uci1 | 0:664899e0b988 | 326 | * %x2 denotes a binary frame |
uci1 | 0:664899e0b988 | 327 | * %x3-7 are reserved for further non-control frames |
uci1 | 0:664899e0b988 | 328 | * %x8 denotes a connection close |
uci1 | 0:664899e0b988 | 329 | * %x9 denotes a pingg |
uci1 | 0:664899e0b988 | 330 | * %xA denotes a pong |
uci1 | 0:664899e0b988 | 331 | * %xB-F are reserved for further control frames |
uci1 | 0:664899e0b988 | 332 | |
uci1 | 0:664899e0b988 | 333 | */ |
uci1 | 0:664899e0b988 | 334 | sendOpcode(0x02); |
uci1 | 0:664899e0b988 | 335 | sendLength(len); |
uci1 | 0:664899e0b988 | 336 | sendMask(); |
uci1 | 0:664899e0b988 | 337 | |
uci1 | 0:664899e0b988 | 338 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 339 | //M: else if (netif == ETH) { |
uci1 | 0:664899e0b988 | 340 | if (netif == ETH) |
uci1 | 0:664899e0b988 | 341 | { |
uci1 | 0:664899e0b988 | 342 | Net::poll(); |
uci1 | 0:664899e0b988 | 343 | uint32_t nbytes=0; |
uci1 | 0:664899e0b988 | 344 | char* s = str; |
uci1 | 0:664899e0b988 | 345 | for (uint32_t i=0; i<len; i++, s++) { |
uci1 | 0:664899e0b988 | 346 | nbytes += sendChar(*s); |
uci1 | 0:664899e0b988 | 347 | } |
uci1 | 0:664899e0b988 | 348 | //const int32_t nbytes = sock->send(str, len); |
uci1 | 0:664899e0b988 | 349 | return nbytes==len; |
uci1 | 0:664899e0b988 | 350 | } |
uci1 | 0:664899e0b988 | 351 | #endif //target |
uci1 | 0:664899e0b988 | 352 | return false; |
uci1 | 0:664899e0b988 | 353 | } |
uci1 | 0:664899e0b988 | 354 | |
uci1 | 0:664899e0b988 | 355 | bool Websocket::sendBinary(FILE* f, const uint32_t nbytes) |
uci1 | 0:664899e0b988 | 356 | { |
uci1 | 0:664899e0b988 | 357 | // CJR: add function for sending binary |
uci1 | 0:664899e0b988 | 358 | // return false if EOF or file error before nbytes sent |
uci1 | 0:664899e0b988 | 359 | |
uci1 | 0:664899e0b988 | 360 | //printf("SEND()\r\n"); |
uci1 | 0:664899e0b988 | 361 | /* Opcode: 4 bits |
uci1 | 0:664899e0b988 | 362 | The opcode denotes the frame type of the WebSocket frame |
uci1 | 0:664899e0b988 | 363 | Defines the interpretation of the payload data. If an unknown |
uci1 | 0:664899e0b988 | 364 | opcode is received, the receiving endpoint MUST _Fail the |
uci1 | 0:664899e0b988 | 365 | WebSocket Connection_. The following values are defined. |
uci1 | 0:664899e0b988 | 366 | * %x0 denotes a continuation frame |
uci1 | 0:664899e0b988 | 367 | * %x1 denotes a text frame |
uci1 | 0:664899e0b988 | 368 | * %x2 denotes a binary frame |
uci1 | 0:664899e0b988 | 369 | * %x3-7 are reserved for further non-control frames |
uci1 | 0:664899e0b988 | 370 | * %x8 denotes a connection close |
uci1 | 0:664899e0b988 | 371 | * %x9 denotes a pingg |
uci1 | 0:664899e0b988 | 372 | * %xA denotes a pong |
uci1 | 0:664899e0b988 | 373 | * %xB-F are reserved for further control frames |
uci1 | 0:664899e0b988 | 374 | |
uci1 | 0:664899e0b988 | 375 | */ |
uci1 | 0:664899e0b988 | 376 | sendOpcode(0x02); |
uci1 | 0:664899e0b988 | 377 | sendLength(nbytes); |
uci1 | 0:664899e0b988 | 378 | sendMask(); |
uci1 | 0:664899e0b988 | 379 | |
uci1 | 0:664899e0b988 | 380 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 381 | //M: else if (netif == ETH) { |
uci1 | 0:664899e0b988 | 382 | if (netif == ETH) |
uci1 | 0:664899e0b988 | 383 | { |
uci1 | 0:664899e0b988 | 384 | Net::poll(); |
uci1 | 0:664899e0b988 | 385 | uint8_t c; |
uci1 | 0:664899e0b988 | 386 | // conserve mbed memory by reading byte-by-byte |
uci1 | 0:664899e0b988 | 387 | for (uint32_t i=0; i<nbytes; i++) { |
uci1 | 0:664899e0b988 | 388 | fread(&c, 1, 1, f); |
uci1 | 0:664899e0b988 | 389 | if ((feof(f)==0) && (ferror(f)==0)) { |
uci1 | 0:664899e0b988 | 390 | sendChar(c); |
uci1 | 0:664899e0b988 | 391 | } else { |
uci1 | 0:664899e0b988 | 392 | return false; |
uci1 | 0:664899e0b988 | 393 | } |
uci1 | 0:664899e0b988 | 394 | } |
uci1 | 0:664899e0b988 | 395 | |
uci1 | 0:664899e0b988 | 396 | } |
uci1 | 0:664899e0b988 | 397 | #endif //target |
uci1 | 0:664899e0b988 | 398 | return true; |
uci1 | 0:664899e0b988 | 399 | } |
uci1 | 0:664899e0b988 | 400 | |
uci1 | 0:664899e0b988 | 401 | bool Websocket::read(char * message, Timer* timer, const uint32_t timeout) |
uci1 | 0:664899e0b988 | 402 | { |
uci1 | 0:664899e0b988 | 403 | //printf("READ()\r\n"); |
uci1 | 0:664899e0b988 | 404 | uint32_t len_msg; //32 bit size |
uci1 | 0:664899e0b988 | 405 | char opcode = 0; //continuation frame |
uci1 | 0:664899e0b988 | 406 | char mask[4] = {0, 0, 0, 0}; //no mask |
uci1 | 0:664899e0b988 | 407 | Timer myTimer; |
uci1 | 0:664899e0b988 | 408 | if (timer==0) { |
uci1 | 0:664899e0b988 | 409 | timer = &myTimer; |
uci1 | 0:664899e0b988 | 410 | } |
uci1 | 0:664899e0b988 | 411 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 412 | if (netif == ETH) |
uci1 | 0:664899e0b988 | 413 | { |
uci1 | 0:664899e0b988 | 414 | // printf("Current opcode in read() 0x%X\r\n", opcode); |
uci1 | 0:664899e0b988 | 415 | uint32_t index = 0; |
uci1 | 0:664899e0b988 | 416 | Net::poll(); |
uci1 | 0:664899e0b988 | 417 | if (new_msg) //from webserver |
uci1 | 0:664899e0b988 | 418 | { |
uci1 | 0:664899e0b988 | 419 | printf("There is new message from webserver\r\n"); |
uci1 | 0:664899e0b988 | 420 | // read the opcode |
uci1 | 0:664899e0b988 | 421 | while (true) |
uci1 | 0:664899e0b988 | 422 | { |
uci1 | 0:664899e0b988 | 423 | if (timer->read() > timeout) |
uci1 | 0:664899e0b988 | 424 | { |
uci1 | 0:664899e0b988 | 425 | return false; |
uci1 | 0:664899e0b988 | 426 | } |
uci1 | 0:664899e0b988 | 427 | opcode = eth_rx[index++]; |
uci1 | 0:664899e0b988 | 428 | printf("new opcode in read() func: 0x%X\r\n", opcode); //0x81 |
uci1 | 0:664899e0b988 | 429 | if (opcode == 0x81) |
uci1 | 0:664899e0b988 | 430 | { |
uci1 | 0:664899e0b988 | 431 | break; |
uci1 | 0:664899e0b988 | 432 | } |
uci1 | 0:664899e0b988 | 433 | }// end of while(true) |
uci1 | 0:664899e0b988 | 434 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 435 | // printf("opcode: 0x%X\r\n", opcode); |
uci1 | 0:664899e0b988 | 436 | #endif |
uci1 | 0:664899e0b988 | 437 | len_msg = eth_rx[index++] & 0x7f; |
uci1 | 0:664899e0b988 | 438 | printf("length_message2 : %d\r\n", len_msg); // |
uci1 | 0:664899e0b988 | 439 | if (len_msg == 126) |
uci1 | 0:664899e0b988 | 440 | { |
uci1 | 0:664899e0b988 | 441 | len_msg += eth_rx[index++] << 8; |
uci1 | 0:664899e0b988 | 442 | len_msg = eth_rx[index++]; |
uci1 | 0:664899e0b988 | 443 | printf("len message is greater than 126 %d\r\n", len_msg); |
uci1 | 0:664899e0b988 | 444 | } |
uci1 | 0:664899e0b988 | 445 | else if (len_msg == 127) |
uci1 | 0:664899e0b988 | 446 | { |
uci1 | 0:664899e0b988 | 447 | len_msg = 0; |
uci1 | 0:664899e0b988 | 448 | for (int i = 0; i < 8; i++) |
uci1 | 0:664899e0b988 | 449 | { |
uci1 | 0:664899e0b988 | 450 | len_msg += eth_rx[index++] << i*8; |
uci1 | 0:664899e0b988 | 451 | } |
uci1 | 0:664899e0b988 | 452 | } |
uci1 | 0:664899e0b988 | 453 | if(len_msg == 0) |
uci1 | 0:664899e0b988 | 454 | { |
uci1 | 0:664899e0b988 | 455 | return false; |
uci1 | 0:664899e0b988 | 456 | } |
uci1 | 0:664899e0b988 | 457 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 458 | // printf("length: %d\r\n", len_msg); |
uci1 | 0:664899e0b988 | 459 | #endif |
uci1 | 0:664899e0b988 | 460 | if ((len_msg & 0x80)) |
uci1 | 0:664899e0b988 | 461 | { |
uci1 | 0:664899e0b988 | 462 | printf("print mask bit %d\r\n", len_msg & 0x80); |
uci1 | 0:664899e0b988 | 463 | for (int j = 0; j < 4; j++){ |
uci1 | 0:664899e0b988 | 464 | mask[j] = eth_rx[index++]; |
uci1 | 0:664899e0b988 | 465 | printf(" mask index %i is %i, ", j, mask[j]); |
uci1 | 0:664899e0b988 | 466 | } |
uci1 | 0:664899e0b988 | 467 | } |
uci1 | 0:664899e0b988 | 468 | // for (int i = 0; i < 4; i++) |
uci1 | 0:664899e0b988 | 469 | //{ |
uci1 | 0:664899e0b988 | 470 | // mask[i] = 0; |
uci1 | 0:664899e0b988 | 471 | //} |
uci1 | 0:664899e0b988 | 472 | /* for (int i = 0; i < 4; i++) |
uci1 | 0:664899e0b988 | 473 | { |
uci1 | 0:664899e0b988 | 474 | printf(" mask index %i is %i, ", i, mask[i]); |
uci1 | 0:664899e0b988 | 475 | }*/ |
uci1 | 0:664899e0b988 | 476 | printf("\r\n"); |
uci1 | 0:664899e0b988 | 477 | for (int i = 0; i < len_msg; i++) |
uci1 | 0:664899e0b988 | 478 | { |
uci1 | 0:664899e0b988 | 479 | // printf("%c", eth_rx[i]); |
uci1 | 0:664899e0b988 | 480 | // message[i-8] = eth_rx[i]; |
uci1 | 0:664899e0b988 | 481 | // message[i] = eth_rx[index++] ^ mask[i % 4]; |
uci1 | 0:664899e0b988 | 482 | if (len_msg < 126) |
uci1 | 0:664899e0b988 | 483 | message[i] = eth_rx[i+2]; |
uci1 | 0:664899e0b988 | 484 | else |
uci1 | 0:664899e0b988 | 485 | message[i] = eth_rx[i+4]; |
uci1 | 0:664899e0b988 | 486 | // printf("message is %s\r\n", message); |
uci1 | 0:664899e0b988 | 487 | } |
uci1 | 0:664899e0b988 | 488 | message[len_msg] = 0; |
uci1 | 0:664899e0b988 | 489 | |
uci1 | 0:664899e0b988 | 490 | //M: add : |
uci1 | 0:664899e0b988 | 491 | // if (new_msg) |
uci1 | 0:664899e0b988 | 492 | // {new_msg = true;} |
uci1 | 0:664899e0b988 | 493 | //else |
uci1 | 0:664899e0b988 | 494 | new_msg = false; |
uci1 | 0:664899e0b988 | 495 | return true; |
uci1 | 0:664899e0b988 | 496 | }//if new message |
uci1 | 0:664899e0b988 | 497 | printf("no new message!\r\n"); |
uci1 | 0:664899e0b988 | 498 | return false; |
uci1 | 0:664899e0b988 | 499 | }//end of if(eth) |
uci1 | 0:664899e0b988 | 500 | #endif //target |
uci1 | 0:664899e0b988 | 501 | //the program shouldn't be here |
uci1 | 0:664899e0b988 | 502 | return false; |
uci1 | 0:664899e0b988 | 503 | } // end of read func |
uci1 | 0:664899e0b988 | 504 | |
uci1 | 0:664899e0b988 | 505 | bool Websocket::close() |
uci1 | 0:664899e0b988 | 506 | { |
uci1 | 0:664899e0b988 | 507 | // printf("COSE()\r\n"); |
uci1 | 0:664899e0b988 | 508 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 509 | if (netif == ETH) |
uci1 | 0:664899e0b988 | 510 | { |
uci1 | 0:664899e0b988 | 511 | if (sock->close()) |
uci1 | 0:664899e0b988 | 512 | return false; |
uci1 | 0:664899e0b988 | 513 | return true; |
uci1 | 0:664899e0b988 | 514 | } |
uci1 | 0:664899e0b988 | 515 | #endif //target |
uci1 | 0:664899e0b988 | 516 | //the program shouldn't be here |
uci1 | 0:664899e0b988 | 517 | return false; |
uci1 | 0:664899e0b988 | 518 | } |
uci1 | 0:664899e0b988 | 519 | |
uci1 | 0:664899e0b988 | 520 | bool Websocket::connected() { |
uci1 | 0:664899e0b988 | 521 | // printf("CONNECTED()\r\n"); |
uci1 | 0:664899e0b988 | 522 | |
uci1 | 0:664899e0b988 | 523 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 524 | |
uci1 | 0:664899e0b988 | 525 | if (netif == ETH) |
uci1 | 0:664899e0b988 | 526 | { |
uci1 | 0:664899e0b988 | 527 | return eth_connected; |
uci1 | 0:664899e0b988 | 528 | } |
uci1 | 0:664899e0b988 | 529 | #endif //target |
uci1 | 0:664899e0b988 | 530 | //the program shouldn't be here |
uci1 | 0:664899e0b988 | 531 | return false; |
uci1 | 0:664899e0b988 | 532 | } |
uci1 | 0:664899e0b988 | 533 | |
uci1 | 0:664899e0b988 | 534 | std::string Websocket::getPath() |
uci1 | 0:664899e0b988 | 535 | { |
uci1 | 0:664899e0b988 | 536 | return path; |
uci1 | 0:664899e0b988 | 537 | } |
uci1 | 0:664899e0b988 | 538 | |
uci1 | 0:664899e0b988 | 539 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 540 | void Websocket::onTCPSocketEvent(TCPSocketEvent e) |
uci1 | 0:664899e0b988 | 541 | { |
uci1 | 0:664899e0b988 | 542 | // printf("TCPSocketEvent is "); |
uci1 | 0:664899e0b988 | 543 | if (e == TCPSOCKET_CONNECTED) |
uci1 | 0:664899e0b988 | 544 | { |
uci1 | 0:664899e0b988 | 545 | eth_connected = true; |
uci1 | 0:664899e0b988 | 546 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 547 | // printf("TCP Socket Connected\r\n"); |
uci1 | 0:664899e0b988 | 548 | #endif |
uci1 | 0:664899e0b988 | 549 | } |
uci1 | 0:664899e0b988 | 550 | else if (e == TCPSOCKET_WRITEABLE) { |
uci1 | 0:664899e0b988 | 551 | // printf("TCPSOCKET_WRITEABLE\r\n"); |
uci1 | 0:664899e0b988 | 552 | } |
uci1 | 0:664899e0b988 | 553 | else if (e == TCPSOCKET_READABLE) |
uci1 | 0:664899e0b988 | 554 | { |
uci1 | 0:664899e0b988 | 555 | // printf("TCPSOCKET_READABLE\r\n"); |
uci1 | 0:664899e0b988 | 556 | int len = sock->recv(eth_rx, 512); |
uci1 | 0:664899e0b988 | 557 | eth_rx[len] = 0; |
uci1 | 0:664899e0b988 | 558 | new_msg = true; |
uci1 | 0:664899e0b988 | 559 | if (!response_server_eth) |
uci1 | 0:664899e0b988 | 560 | { |
uci1 | 0:664899e0b988 | 561 | string checking; |
uci1 | 0:664899e0b988 | 562 | size_t found = string::npos; |
uci1 | 0:664899e0b988 | 563 | checking = eth_rx; |
uci1 | 0:664899e0b988 | 564 | found = checking.find("DdLWT/1JcX+nQFHebYP+rqEx5xI="); |
uci1 | 0:664899e0b988 | 565 | if (found != string::npos) |
uci1 | 0:664899e0b988 | 566 | response_server_eth = true; |
uci1 | 0:664899e0b988 | 567 | } |
uci1 | 0:664899e0b988 | 568 | } |
uci1 | 0:664899e0b988 | 569 | else |
uci1 | 0:664899e0b988 | 570 | { |
uci1 | 0:664899e0b988 | 571 | #ifdef DEBUG |
uci1 | 0:664899e0b988 | 572 | printf("TCP Socket Fail\r\n"); |
uci1 | 0:664899e0b988 | 573 | #endif |
uci1 | 0:664899e0b988 | 574 | eth_connected = false; |
uci1 | 0:664899e0b988 | 575 | } |
uci1 | 0:664899e0b988 | 576 | } |
uci1 | 0:664899e0b988 | 577 | #endif //target |
uci1 | 0:664899e0b988 | 578 |