S K UCI / Mbed 2 deprecated AutonomousDAQ

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Websocket.cpp Source File

Websocket.cpp

00001 #ifdef IGNORE_THIS_FILE
00002 #include "Websocket.h"
00003 #include <string>
00004 
00005 #include "SnBase64.h"
00006 
00007 //#define DEBUG
00008 
00009 const bool Websocket::kUseB64 = true;
00010 
00011 #ifdef TARGET_LPC1768
00012 Websocket::Websocket(char * url) {
00013     server_ip = NULL;
00014     netif = ETH;
00015     eth_writeable = false;
00016     eth_readable = false;
00017     eth_connected = false;
00018     response_server_eth = false;
00019     new_msg = false;
00020     fillFields(url);
00021     
00022     memset(eth_rx, 0, RX_BUF_SIZE);
00023     
00024     eth = new EthernetNetIf(
00025         IpAddr(128,195,204,148), //IP Address
00026         IpAddr(255,255,255,0), //Network Mask
00027         IpAddr(128,195,204,1), //Gateway
00028         IpAddr(128,200,1,201)  //DNS   
00029     );
00030     sock = new TCPSocket();
00031 
00032     EthernetErr ethErr = eth->setup();
00033 #ifdef DEBUG
00034     if (ethErr) {
00035         printf("\r\nERROR %d in setup.\r\n", ethErr);
00036     }
00037 #endif
00038 
00039     //we must use dnsresolver to find the ip address
00040     if (server_ip == NULL) {
00041         DNSResolver dr;
00042         server_ip = new IpAddr();
00043         *server_ip = dr.resolveName(ip_domain.c_str());
00044 #ifdef DEBUG
00045         printf("\r\nserver with dns=%d.%d.%d.%d\r\n", (*server_ip)[0], (*server_ip)[1], (*server_ip)[2], (*server_ip)[3]);
00046 #endif
00047 
00048     }
00049 
00050     IpAddr ipt = eth->getIp();
00051 #ifdef DEBUG
00052     printf("\r\nmbed IP Address is %d.%d.%d.%d\r\n", ipt[0], ipt[1], ipt[2], ipt[3]);
00053 #endif
00054 
00055     sock->setOnEvent(this, &Websocket::onTCPSocketEvent);
00056 }
00057 #endif //target
00058 
00059 
00060 void Websocket::fillFields(char * url) 
00061 {
00062 #ifdef DEBUG
00063     printf("FILLFIELDS\r\n");
00064 #endif
00065     char *res = NULL;
00066     char *res1 = NULL;
00067 
00068     char buf[50];
00069     strcpy(buf, url);
00070     printf("\r\nBuf is:");
00071     for(int i=0;i<50;i++)
00072     {
00073         printf("%d", buf[i]);
00074     }
00075     printf("\r\n");
00076     res = strtok(buf, ":");
00077     if (strcmp(res, "ws")) 
00078     {
00079 #ifdef DEBUG
00080         printf("\r\nFormat error: please use: \"ws://ip-or-domain[:port]/path\"\r\n\r\n");
00081 #endif
00082     } 
00083     else 
00084     {
00085         //ip_domain and port
00086         res = strtok(NULL, "/");
00087 
00088         //path
00089         res1 = strtok(NULL, " ");
00090         if (res1 != NULL) 
00091         {
00092             path = res1;
00093         }
00094 
00095         //ip_domain
00096         res = strtok(res, ":");
00097 
00098         //port
00099         res1 = strtok(NULL, " ");
00100         //port
00101         if (res1 != NULL) 
00102         {
00103             port = res1;
00104         } else 
00105         {
00106             port = "80";
00107         }
00108 
00109         if (res != NULL) 
00110         {
00111             ip_domain = res;
00112 
00113             //if we use ethernet, we must decode ip address or use dnsresolver
00114 #ifdef TARGET_LPC1768
00115             if (netif == ETH) {
00116                 strcpy(buf, res);
00117                 //we try to decode the ip address
00118                 if (buf[0] >= '0' && buf[0] <= '9') {
00119                     res = strtok(buf, ".");
00120                     int i = 0;
00121                     int ip[4];
00122                     while (res != NULL) {
00123                         ip[i] = atoi(res);
00124                         res = strtok(NULL, ".");
00125                         i++;
00126                     }
00127                     server_ip = new IpAddr(ip[0], ip[1], ip[2], ip[3]);
00128 #ifdef DEBUG
00129                     printf("server without dns=%i.%i.%i.%i\n",(*server_ip)[0],(*server_ip)[1],(*server_ip)[2],(*server_ip)[3]);
00130 #endif
00131                 }
00132             }
00133 #endif //target
00134         }
00135     }
00136 }
00137 
00138 
00139 bool Websocket::connect(const uint32_t timeout) 
00140 {
00141 #ifdef DEBUG
00142     printf("CONNECT() Function\r\n");
00143 #endif
00144     char cmd[50];
00145 #ifdef TARGET_LPC1768
00146    //M: else if (netif == ETH) 
00147     if (netif == ETH) 
00148     {
00149         Host server (*server_ip, atoi(port.c_str()));
00150         sock->close();
00151         TCPSocketErr bindErr = sock->connect(server);
00152         if (bindErr) 
00153         {
00154 #ifdef DEBUG
00155             printf("\r\nERROR binderr: %d\r\n", bindErr);
00156 #endif
00157             return false;
00158         }
00159         Timer tmr;
00160         tmr.start();
00161         int i = 0;
00162         while (true) {
00163             Net::poll();
00164             if (time(0) > timeout) {
00165                 
00166                 printf("connect timeout %u > %u\r\n",
00167                     time(0), timeout);
00168                 
00169                 return false;
00170             }
00171             if (tmr.read() > 0.05) {
00172                 tmr.reset();
00173                 if (eth_connected) {
00174                     //printf("connect msg %d\r\n",i);
00175                     switch (i) {
00176                         case 0:
00177                             sprintf(cmd, "GET /%s HTTP/1.1\r\n", path.c_str());
00178                             sock->send(cmd, strlen(cmd));
00179                             i++;
00180                             break;
00181                         case 1:
00182                             sprintf(cmd, "Host: %s:%s\r\n", ip_domain.c_str(), port.c_str());
00183                             sock->send(cmd, strlen(cmd));
00184                             i++;
00185                             break;
00186                         case 2:
00187                             sprintf(cmd, "Upgrade: WebSocket\r\n");
00188                             sock->send(cmd, strlen(cmd));
00189                             i++;
00190                             break;
00191                         case 3:
00192                             sprintf(cmd, "Origin: null\r\n");
00193                             sock->send(cmd, strlen(cmd));
00194                             i++;
00195                             break;
00196                         case 4:
00197                             sprintf(cmd, "Connection: Upgrade\r\n");
00198                             sock->send(cmd, strlen(cmd));
00199                             i++;
00200                             break;
00201                         case 5:
00202                             sprintf(cmd, "Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n");
00203                             sock->send(cmd, strlen(cmd));
00204                             i++;
00205                             break;
00206                         case 6:
00207                             sprintf(cmd, "Sec-WebSocket-Version: 13\r\n\r\n");
00208                             sock->send(cmd, strlen(cmd));
00209                             i++;
00210                             break;
00211                         case 7:
00212                             if (response_server_eth)
00213                                 i++;
00214                             else
00215                                 break;
00216 
00217                         default:
00218                             break;
00219                     }
00220                 }
00221                 if (i==8) {
00222 #ifdef DEBUG
00223                     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());
00224 #endif
00225                     return true;
00226                 }
00227             }
00228         }
00229     }
00230 #endif //target
00231     //the program shouldn't be here
00232     return false;
00233 }    
00234 
00235 void Websocket::sendLength(uint32_t len) 
00236 {
00237     //printf("SENDLENGTH(), Send Length: %d\r\n", len);
00238     if (len < 126) 
00239     {
00240         sendChar(len | (1<<7));
00241     } 
00242     else if (len < 65535) 
00243     { // use 2 bytes 
00244         sendChar(126 | (1<<7));
00245        //M: reverce these two lines 
00246         sendChar((len >> 8) & 0xff); //2       
00247         sendChar(len & 0xff); //1
00248   //      sendChar((len >> 8) & 0xff); //2         
00249     } 
00250     else
00251     {
00252         sendChar(127 | (1<<7));
00253         for (int i = 0; i < 8; i++) 
00254         {
00255             sendChar((len >> i*8) & 0xff);
00256         }
00257     }
00258 }
00259 
00260 int Websocket::sendChar(uint8_t c) 
00261 {
00262 #ifdef TARGET_LPC1768
00263     if (netif == ETH) {
00264         Net::poll();
00265         return sock->send((const char *)&c, 1);
00266   //      printf("SENDCHAR(), Sendchar %d \r\n", c);
00267     }
00268 #endif
00269     return 0;
00270 }
00271 
00272 void Websocket::sendOpcode(uint8_t opcode) 
00273 {
00274 //    printf("SENDOPCODE()\r\n");
00275     sendChar(0x80 | (opcode & 0x0f));
00276 //    printf("SendOpcode: 0x%X\r\n", opcode);
00277 }
00278 
00279 /*Masking-key:  0 or 4 bytes
00280       All frames sent from the client to the server are masked by a 32-
00281       bit value that is contained within the frame.  This field is
00282       present if the mask bit is set to 1, and is absent if the mask bit
00283       is set to 0.
00284 */
00285 void Websocket::sendMask() 
00286 {
00287 //    printf("SENDMASK()\r\n");
00288     for (int i = 0; i < 4; i++) {
00289         sendChar(0); //no frame masking 
00290     }
00291 }
00292 
00293 void Websocket::send(const char * str) 
00294 {
00295 #ifdef DEBUG
00296     printf("SEND(%s)\r\n",str);
00297 #endif
00298 /* Opcode:  4 bits
00299       The opcode denotes the frame type of the WebSocket frame
00300       Defines the interpretation of the payload data.  If an unknown
00301       opcode is received, the receiving endpoint MUST _Fail the
00302       WebSocket Connection_.  The following values are defined.
00303       *  %x0 denotes a continuation frame
00304       *  %x1 denotes a text frame
00305       *  %x2 denotes a binary frame
00306       *  %x3-7 are reserved for further non-control frames
00307       *  %x8 denotes a connection close
00308       *  %x9 denotes a pingg
00309       *  %xA denotes a pong
00310       *  %xB-F are reserved for further control frames
00311       
00312       */
00313     sendOpcode(0x01);
00314     sendLength(strlen(str));
00315     sendMask();
00316 
00317 #ifdef TARGET_LPC1768
00318    //M: else if (netif == ETH) {
00319     if (netif == ETH) 
00320     {
00321         Net::poll();
00322         sock->send(str, strlen(str));
00323     }
00324 #endif //target
00325 }
00326 
00327 bool Websocket::sendBinary(const char* hbuf, const uint32_t hlen, 
00328                            FILE* f, const uint32_t nbytes,
00329                            char* const bbuf) {
00330     if (kUseB64) {
00331         return sendBinaryB64txt(hbuf, hlen, f, nbytes, bbuf);
00332     } else {
00333         return sendBinaryDirect(hbuf, hlen, f, nbytes);
00334     }
00335 }
00336 
00337 bool Websocket::sendBinaryB64txt(const char* hbuf, const uint32_t hlen,
00338                                  FILE* f, const uint32_t nbytes,
00339                                  char* const bbuf) {
00340 #ifdef DEBUG
00341     printf("SENDbinaryB64txt(%s)\r\n",str);
00342 #endif
00343 /* Opcode:  4 bits
00344       The opcode denotes the frame type of the WebSocket frame
00345       Defines the interpretation of the payload data.  If an unknown
00346       opcode is received, the receiving endpoint MUST _Fail the
00347       WebSocket Connection_.  The following values are defined.
00348       *  %x0 denotes a continuation frame
00349       *  %x1 denotes a text frame
00350       *  %x2 denotes a binary frame
00351       *  %x3-7 are reserved for further non-control frames
00352       *  %x8 denotes a connection close
00353       *  %x9 denotes a pingg
00354       *  %xA denotes a pong
00355       *  %xB-F are reserved for further control frames
00356       
00357       */
00358     sendOpcode(0x01);
00359     sendLength(BASE64ENC_LEN(nbytes));
00360     sendMask();
00361 
00362 #ifdef TARGET_LPC1768
00363    //M: else if (netif == ETH) {
00364     if (netif == ETH) 
00365     {
00366         Net::poll();
00367         //sock->send(str, strlen(str));
00368         // read 3 bytes at a time
00369         static const uint8_t cbytes = 3; // must be multiple of 3
00370         static const uint32_t clen = BASE64ENC_LEN(cbytes)+1;
00371         char chunk[clen];
00372         char fbuf[cbytes];
00373         const uint32_t nchunks = nbytes / cbytes;
00374         for (uint32_t i=0; i<nchunks; i++) {
00375             fread(fbuf, cbytes, 1, f);
00376             B64::modp_b64_encode(fbuf, cbytes, chunk);
00377             sock->send(chunk, clen-1);
00378         }
00379         // now the remainder
00380         const uint32_t rem = nbytes-(nchunks*cbytes);
00381         if (rem>0) {
00382             fread(fbuf, rem, 1, f);
00383             B64::modp_b64_encode(fbuf, rem, chunk);
00384             sock->send(chunk, strlen(chunk));
00385         }
00386     }
00387 #endif //target
00388     return true;
00389 }
00390 
00391 bool Websocket::sendBinary(const char* str, const uint32_t len,
00392                            char* const bbuf) {
00393     if (kUseB64) {
00394         return sendBinaryB64txt(str, len, bbuf);
00395     } else {
00396         return sendBinaryDirect(str, len);
00397     }
00398 }
00399 
00400 bool Websocket::sendBinaryB64txt(const char* str, const uint32_t len,
00401                                  char* const bbuf) {
00402     B64::modp_b64_encode(str, len, bbuf);
00403     send(bbuf);
00404     return true;
00405 }
00406 
00407 bool Websocket::sendBinaryDirect(const char* str, const uint32_t len) 
00408 {
00409     // CJR: add function for sending binary
00410     // force header info to be sent on each call
00411 #ifdef DEBUG
00412     printf("SENDBINARY()\r\n");
00413 #endif
00414 /* Opcode:  4 bits
00415       The opcode denotes the frame type of the WebSocket frame
00416       Defines the interpretation of the payload data.  If an unknown
00417       opcode is received, the receiving endpoint MUST _Fail the
00418       WebSocket Connection_.  The following values are defined.
00419       *  %x0 denotes a continuation frame
00420       *  %x1 denotes a text frame
00421       *  %x2 denotes a binary frame
00422       *  %x3-7 are reserved for further non-control frames
00423       *  %x8 denotes a connection close
00424       *  %x9 denotes a pingg
00425       *  %xA denotes a pong
00426       *  %xB-F are reserved for further control frames
00427       
00428       */
00429     printf("send binary: 0x02 %u 0\r\n", len);
00430     sendOpcode(0x02);
00431     sendLength(len);
00432     sendMask();
00433 
00434 #ifdef TARGET_LPC1768
00435    //M: else if (netif == ETH) {
00436     if (netif == ETH) 
00437     {
00438         Net::poll();
00439         /*
00440         // first send the header
00441         const char* hh = hbuf;
00442         for (uint32_t j=0; j<hlen; j++, hh++) {
00443             sendChar(*hh);
00444             printf("%02x ",*hh);
00445         }
00446         */
00447         // now send the message
00448         uint32_t nbytes=0;
00449         const char* ss = str;
00450         for (uint32_t i=0; i<len; i++, ss++) {
00451             nbytes += sendChar(*ss);
00452             printf("%02x ",*ss);
00453         }
00454         printf("\r\n");
00455         //const int32_t nbytes = sock->send(str, len);
00456         return nbytes==len;
00457     }
00458 #endif //target
00459     return false;
00460 }
00461 
00462 bool Websocket::sendBinaryDirect(const char* hbuf, const uint32_t hlen,
00463                                  FILE* f, const uint32_t nbytes) 
00464 {
00465 #ifdef DEBUG
00466     printf("SENDBINARY(FILE*)\r\n");
00467 #endif
00468     // CJR: add function for sending binary
00469     // force header info to be sent on each call
00470     // return false if EOF or file error before nbytes sent
00471     
00472     //printf("SEND()\r\n");
00473 /* Opcode:  4 bits
00474       The opcode denotes the frame type of the WebSocket frame
00475       Defines the interpretation of the payload data.  If an unknown
00476       opcode is received, the receiving endpoint MUST _Fail the
00477       WebSocket Connection_.  The following values are defined.
00478       *  %x0 denotes a continuation frame
00479       *  %x1 denotes a text frame
00480       *  %x2 denotes a binary frame
00481       *  %x3-7 are reserved for further non-control frames
00482       *  %x8 denotes a connection close
00483       *  %x9 denotes a pingg
00484       *  %xA denotes a pong
00485       *  %xB-F are reserved for further control frames
00486       
00487       */
00488     sendOpcode(0x02);
00489     sendLength(nbytes);
00490     sendMask();
00491 
00492 #ifdef TARGET_LPC1768
00493    //M: else if (netif == ETH) {
00494     if (netif == ETH) 
00495     {
00496         Net::poll();
00497         // first send the header
00498         const char* hh = hbuf;
00499         for (uint32_t j=0; j<hlen; j++, hh++) {
00500             sendChar(*hh);
00501         }
00502         // conserve mbed memory by reading byte-by-byte
00503         uint8_t c;
00504         for (uint32_t i=0; i<nbytes; i++) {
00505             fread(&c, 1, 1, f);
00506             if ((feof(f)==0) && (ferror(f)==0)) {
00507                 sendChar(c);
00508             } else {
00509                 return false;
00510             }
00511         }
00512             
00513     }
00514 #endif //target
00515     return true;
00516 }
00517 
00518 bool Websocket::read(char * message, uint32_t& len_msg, const uint32_t maxlen,
00519                      const uint32_t timeout,
00520                      char* const bbuf, const uint32_t bbsize) 
00521 {
00522 #ifdef DEBUG
00523     printf("READ()\r\n");
00524 #endif
00525     //uint32_t len_msg; //32 bit size
00526     char opcode = 0; //continuation frame
00527     char mask[4] = {0, 0, 0, 0}; //no mask
00528 #ifdef TARGET_LPC1768
00529     if (netif == ETH) 
00530     {
00531 #ifdef DEBUG
00532         printf("Current opcode in read() 0x%X\r\n", opcode);
00533 #endif
00534         uint32_t index = 0;
00535         Net::poll();
00536         if (new_msg) //from webserver
00537         {
00538 #ifdef DEBUG
00539             printf("There is new message from webserver\r\n");
00540 #endif
00541              // read the opcode
00542             while (true) 
00543             {
00544                 if (time(0) > timeout) 
00545                 {
00546                     printf("read: timing out %d > %u \r\n",
00547                         time(0), timeout);
00548                     wait(2);
00549                     return false;
00550                 } else if (index>=RX_BUF_SIZE) {
00551                     index=0;
00552                     continue;
00553                 }
00554                 opcode = eth_rx[index++];
00555 #ifdef DEBUG
00556                 printf("new opcode in read() func: 0x%X\r\n", opcode); //0x81
00557 #endif
00558                 if (opcode == 0x81 || opcode==0x82) 
00559                 {
00560                     break;
00561                 }
00562             }// end of while(true)
00563             
00564             // redo search in case opcode is actually inside a byte message
00565             // and a race-condition occurred
00566             int32_t ix=0;
00567             const char* erx = eth_rx;
00568             for (ix=0; ix<RX_BUF_SIZE; ix++, erx++) { // 1024 or 512? (since sock->read is called with 512)
00569                 if ( (*erx==0x81) || (*erx==0x82) ) {
00570                     index = ix+1;
00571                     opcode = *erx;
00572                     break;
00573                 }
00574             }
00575             if (ix==RX_BUF_SIZE) {
00576                 // some new non-websocket message came in during the redoing of the search?
00577                 // anyway opcode not found
00578                 printf("opcode re-search failed\r\n");
00579                 return false;
00580             }
00581 //#ifdef DEBUG
00582             printf("opcode: 0x%X @ index=%u\r\n", opcode, index-1);
00583             for (int i=0; i<RX_BUF_SIZE; i++) {
00584                 printf("%02x ",eth_rx[i]);
00585             }
00586             printf("\r\n");
00587 //#endif                 
00588             printf("eth_rx[i-1]=%x, eth_rx[i]=%x , eth_rx[i+1]=%x\r\n",
00589                 index>0?eth_rx[index-1]:eth_rx[index],eth_rx[index],eth_rx[index+1]);
00590             len_msg = eth_rx[index++] & 0x7f;
00591             printf("length_message2 : %d\r\n", len_msg);            //
00592             if (len_msg == 126)
00593             {
00594                 len_msg += eth_rx[index++] << 8;                        
00595                 len_msg = eth_rx[index++];                                  
00596                 printf("len message is greater than 126 %d\r\n", len_msg);     
00597             } 
00598             else if (len_msg == 127) 
00599             {              
00600                 len_msg = 0;
00601                 for (int i = 0; i < 8; i++) 
00602                 {
00603                     len_msg += eth_rx[index++] << i*8;
00604                 }            
00605             } 
00606             if(len_msg == 0) 
00607             {  
00608                 return false;
00609             } else if (len_msg>=maxlen) {
00610                 return false;
00611             }
00612 //#ifdef DEBUG
00613             printf("length: %d\r\n", len_msg);
00614 //#endif
00615             if ((len_msg & 0x80)) 
00616             {
00617                 printf("print mask bit %d\r\n", len_msg & 0x80);
00618                 for (int j = 0; j < 4; j++){
00619                     mask[j] = eth_rx[index++];
00620                     printf(" mask index %i is %i, ", j, mask[j]); 
00621                 }                   
00622             }
00623             printf("\r\n");
00624             
00625             // read the actual message
00626             for (int i = 0; i < len_msg; i++) 
00627             {             
00628                 if (len_msg < 126) {
00629                     message[i] = eth_rx[i+2];                      
00630                 } else {
00631                     message[i] = eth_rx[i+4];
00632                 }
00633             }            
00634             message[len_msg] = 0;
00635             
00636             if ( kUseB64 && ((opcode & 0x01)!=0) ) {
00637                 // decode the message
00638                 printf("need to b64decode\r\n");
00639                 const uint8_t pads = len_msg%4;
00640                 if ((len_msg+pads+1)<maxlen) {
00641                     char* mm = message+len_msg;
00642                     for (uint8_t k=0; k<pads; k++, mm++) {
00643                         *mm = CHARPAD;
00644                     }
00645                     *mm=0;
00646                     if (BASE64ENC_LEN(len_msg+pads)<bbsize) {
00647                         len_msg = B64::modp_b64_decode(message, len_msg+pads, bbuf);
00648                         if (len_msg<maxlen) {
00649                             memcpy(message, bbuf, len_msg);
00650                         } else {
00651                             return false;
00652                         }
00653                     } else {
00654                         return false;
00655                     }
00656                 } else {
00657                     return false;
00658                 }
00659             }
00660             
00661             for (int i=0; i<len_msg; i++) {
00662                 printf("%02x ",message[i]);
00663             }
00664             printf("\r\n");
00665             
00666             new_msg = false;                
00667             return true;
00668         }//if new message
00669         //printf("no new message!\r\n");
00670         return false;
00671     }//end of if(eth)
00672 #endif //target
00673 //the program shouldn't be here
00674     return false;
00675 } // end of read func
00676 
00677 bool Websocket::close() 
00678 {
00679 #ifdef DEBUG
00680    printf("CLOSE()\r\n");
00681 #endif
00682 #ifdef TARGET_LPC1768
00683     if (netif == ETH) 
00684     {
00685         if (sock->close())
00686             return false;
00687         return true;
00688     }
00689 #endif //target
00690     //the program shouldn't be here
00691     return false;
00692 }
00693 
00694 bool Websocket::connected() {
00695  //   printf("CONNECTED()\r\n");
00696 
00697 #ifdef TARGET_LPC1768
00698 
00699     if (netif == ETH)
00700     {    
00701         return eth_connected;
00702     }
00703 #endif //target
00704     //the program shouldn't be here
00705     return false;
00706 }
00707 
00708 std::string Websocket::getPath()
00709 {
00710     return path;
00711 }
00712 
00713 #ifdef TARGET_LPC1768
00714 void Websocket::onTCPSocketEvent(TCPSocketEvent e) 
00715 {
00716 #ifdef DEBUG
00717     printf("TCPSocketEvent is ");
00718 #endif
00719     if (e == TCPSOCKET_CONNECTED) 
00720     {
00721         eth_connected = true;
00722 #ifdef DEBUG
00723         printf("TCP Socket Connected\r\n");
00724 #endif
00725     } 
00726     else if (e == TCPSOCKET_WRITEABLE) {
00727 #ifdef DEBUG
00728         printf("TCPSOCKET_WRITEABLE\r\n");
00729 #endif
00730     } 
00731     else if (e == TCPSOCKET_READABLE) 
00732     {
00733 #ifdef DEBUG
00734         printf("TCPSOCKET_READABLE\r\n");
00735 #endif
00736         const int len = sock->recv(eth_rx, RX_RECV_BYTES);
00737         eth_rx[len] = 0;
00738         new_msg = true;
00739         printf("sock recv len %d\r\n",len);
00740         printf("eth_rx:");
00741         for (int i=0; i<len; i++) {
00742             printf("%02x ",eth_rx[i]);
00743         }
00744         printf("\r\n");
00745         if (!response_server_eth) 
00746         {
00747             string checking;
00748             size_t found = string::npos;
00749             checking = eth_rx;
00750             found = checking.find("DdLWT/1JcX+nQFHebYP+rqEx5xI=");
00751             if (found != string::npos) {
00752                 printf("response_server_eth true. found=%u\r\n",found);
00753                 response_server_eth = true;
00754             }
00755         }
00756     } 
00757     else 
00758     {
00759 #ifdef DEBUG
00760         printf("TCP Socket Fail (%d)\r\n",(int)e);
00761 #endif
00762         eth_connected = false;
00763     }
00764 }
00765 #endif //target
00766 
00767 #endif