Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Fri Jul 20 19:04:02 2012 +0000
Revision:
1:e392595b4b76
Parent:
0:664899e0b988
Child:
2:e67f7c158087
many features checked and working. afar implemented. sending of data not yet tested. contains many debug prints

Who changed what in which revision?

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