Changeset 129

Show
Ignore:
Timestamp:
01/09/08 07:25:41 (11 months ago)
Author:
follower
Message:

Copy current (old) version of demo to serve as base for serial/net proxy demo.

Location:
branches/follower/wiz810mj/src/demo/WizDemo6
Files:
2 copied

Legend:

Unmodified
Added
Removed
  • branches/follower/wiz810mj/src/demo/WizDemo6/WizDemo5.pde

    r128 r129  
    223223 
    224224  public: 
    225     NetworkConnection(uint16_t port); // TODO: Add UDP, TCP choice? 
     225    // TODO: Split into client/server connections? Subclass? 
     226    NetworkConnection(uint16_t port); // TODO: Add UDP, TCP choice? // For servers 
     227    NetworkConnection(); // For clients--is using the default constructor hide misuse? TODO: As above. 
    226228 
    227229    int listen(); 
     230    int connect(uint8 * addr, uint16 port); 
    228231    int isConnected(); 
    229232    int available(); 
    230233    int read(); 
    231234    void print(uint8_t); 
     235    void print(const char * text); 
    232236    void close(); 
    233237 
     
    249253} 
    250254 
     255NetworkConnection::NetworkConnection() { 
     256  /* 
     257 
     258   */ 
     259   NetworkConnection(0); 
     260} 
     261 
     262 
    251263int NetworkConnection::listen() { // TODO: Make private or protected? 
    252264  /* 
     
    255267  return !!::listen(_socket); // TODO: Use C++ namespaces for the driver functions? 
    256268} 
     269 
     270int NetworkConnection::connect(uint8 * addr, uint16 port) { // TODO: Make private or protected? 
     271  /* 
     272 
     273   */   
     274    
     275  // TODO: Accept bytes here for addr? 
     276  int result = 0; 
     277   
     278  result = !!::connect(_socket, addr, port); // TODO: Use C++ namespaces for the driver functions? 
     279  Serial.println(IINCHIP_READ(Sn_DPORT0(_socket)), HEX); 
     280  Serial.println(IINCHIP_READ(Sn_DPORT0(_socket) + 1), HEX); 
     281  return result; 
     282} 
     283 
    257284 
    258285int NetworkConnection::available() { 
     
    314341 // TODO: If we want the 'Network*' classes to be generic we 
    315342 //       would need to handle this differently: 
     343 //Serial.println(getSn_SR(_socket), HEX); 
    316344 return (getSn_SR(_socket) == SOCK_ESTABLISHED); 
    317345 
     
    328356} 
    329357 
     358void NetworkConnection::print(const char * text) { 
     359  /* 
     360   */ 
     361  for (int idx = 0; idx < strlen(text); idx++) { 
     362    print(text[idx]); 
     363    Serial.print(text[idx]); 
     364  }  
     365} 
     366 
    330367void NetworkConnection::close() { 
    331368  /* 
     
    351388     
    352389    NetworkConnection listen(uint16_t port); 
     390    NetworkConnection connect(byte b0, byte b1, byte b2, byte b3, uint16 port); 
    353391 
    354392    Wiz810MjDevice& device; // TODO: Make this a generic "network device" interface 
     
    408446} 
    409447 
     448NetworkConnection NetworkInterface::connect(byte b0, byte b1, byte b2, byte b3, uint16 port) { 
     449   /* 
     450    
     451    */ 
     452  NetworkConnection connection = NetworkConnection(4000); 
     453     
     454  byte _scratchBuffer[4];   // TODO: Move this? 
     455 
     456  // TODO: Better? 
     457  _scratchBuffer[0] = b0; 
     458  _scratchBuffer[1] = b1; 
     459  _scratchBuffer[2] = b2; 
     460  _scratchBuffer[3] = b3; 
     461   
     462    if (!connection.connect(_scratchBuffer, port)) { 
     463      HANDLE_BAD_ERROR();  
     464    } 
     465   
     466  return connection; 
     467} 
     468 
    410469// NetworkInterface Network = NetworkInterface(...); // TODO: Make this a global 
    411470 
     
    489548#define PIN_LED 2 
    490549 
     550//------ 
     551// Note: Probably not efficient or anything... 
     552class StreamConnection { 
     553   
     554  public: 
     555    StreamConnection(NetworkConnection& connection); 
     556    int peekByte(); 
     557    int readByte(); 
     558    int skipSegment(const char * separators); // TODO: Return if separators found or not? 
     559    int readSegmentByte(const char * separators); 
     560    int debug; 
     561    int gobbleMatch(const char * separators, const char * target); 
     562     
     563  private: 
     564    NetworkConnection& _connection;  
     565    int _byteSeen; 
     566}; 
     567 
     568StreamConnection::StreamConnection(NetworkConnection& connection) : _connection (connection) { 
     569  /* 
     570   
     571   */   
     572  _byteSeen = -1; 
     573  debug = 0; 
     574} 
     575 
     576int StreamConnection::peekByte() { 
     577  /* 
     578  */ 
     579  if (_byteSeen < 0) { 
     580    if (_connection.available()) { 
     581      _byteSeen = _connection.read(); 
     582    } 
     583  } 
     584   
     585  return _byteSeen; 
     586} 
     587 
     588int StreamConnection::readByte() { 
     589  /* 
     590  */ 
     591  int newByte = -1; 
     592   
     593  if (_byteSeen < 0) { 
     594    if (_connection.available()) { 
     595      newByte = _connection.read(); 
     596    } 
     597  } else { 
     598    newByte = _byteSeen; 
     599    _byteSeen = -1; 
     600  } 
     601 
     602  if (debug == 1) { 
     603    Serial.print(newByte, BYTE); 
     604  } 
     605   
     606  return newByte; 
     607} 
     608 
     609int StreamConnection::readSegmentByte(const char * separators) { 
     610  /* 
     611  */ 
     612  // Blocks 
     613  while (peekByte() < 0) { // TODO: Time out? 
     614    delay(500); 
     615  } 
     616  if (!strchr(separators, peekByte())) { 
     617    return readByte(); 
     618  }  
     619  return -1; 
     620 
     621 
     622int StreamConnection::skipSegment(const char * separators) { 
     623  /* 
     624  */ 
     625 
     626  if (debug == 1) { 
     627     Serial.println("Start skipping non-separator");  
     628  } 
     629 
     630 
     631  do { 
     632    if (peekByte() < 0) { 
     633      return -1; 
     634    } 
     635    // Skip everything not a separator 
     636  } while (!strchr(separators, peekByte()) && readByte()); 
     637 
     638  if (debug == 1) { 
     639     Serial.println("Finished skipping non-separator");  
     640  } 
     641 
     642  do { 
     643    /* // We can't return here or we get stuck above. 
     644    if (peekByte() < 0) { 
     645      return -1; 
     646    }*/ 
     647      while (peekByte() < 0) { 
     648        delay(500); // TODO: Time out? 
     649      } 
     650    // Skip everything that *is* a separator 
     651  } while (strchr(separators, peekByte()) && readByte()); 
     652 
     653  if (debug == 1) { 
     654     Serial.println("Finished skipping separator");  
     655  } 
     656 
     657  return 1;   
     658} 
     659 
     660int StreamConnection::gobbleMatch(const char * separators, const char * target) { 
     661  /* 
     662  */ 
     663 
     664  int idx = 0; 
     665  int byteRead = -1; 
     666 
     667  while ((byteRead = readSegmentByte(separators)) >=0) { 
     668    if (idx < strlen(target)) { 
     669      if (byteRead != target[idx]) { 
     670        break; 
     671      } 
     672      idx++; 
     673    } else { 
     674      break; 
     675    } 
     676  } 
     677                     
     678  if ((byteRead == -1) && (idx == strlen(target))) { // Matched 
     679    while (skipSegment(separators) < 0) { 
     680      // Ummm, we ran outta data--this screws things up... 
     681      // TODO: Have a time out? 
     682      delay(500);  
     683    } 
     684    return 1; 
     685  } 
     686  return -1; 
     687} 
     688//------ 
     689 
     690 
     691 
    491692void setup () { 
    492693  Serial.begin(9600); 
     
    503704  Network.device.setIp(192,168,2,105); 
    504705  Network.device.setMask(255,255,255,0); 
     706 
     707  Network.device.setGateway(192,168,2,101); 
    505708    
    506709  Serial.println("End W5100 configuration..."); 
     
    511714  Serial.println("Setup exit..."); 
    512715 
     716  // Serial.println(((analogRead(2)*70)/630)/10+'0', BYTE); 
     717 
    513718/**/ 
    514719  Serial.println("Test sockets..."); 
    515720 
    516 #if  
     721#if 1 
     722#if 1 //b 
     723 
     724 
     725 
     726  NetworkConnection conn = Network.connect(209,177,146,34, 6667);  // TODO: Better? 
     727   
     728  Serial.println("Waiting to connect..."); 
     729 
     730  while (!conn.isConnected()) { 
     731    //Serial.print(!conn.isConnected()); 
     732    delay(500);  
     733  } 
     734   
     735  Serial.println("Connected..."); 
     736 
     737  int byteRead = -1; 
     738  int dataWait = 0; 
     739 
     740  StreamConnection stream = StreamConnection(conn); 
     741 
     742  conn.print("NICK Arduino\n"); 
     743  conn.print("USER Arduino 0 0 Arduino\n"); 
     744 
     745  byte buf[4] = {0, 0, 0, 0}; 
     746 
     747  int state = 0; 
     748 
     749  while (conn.isConnected()) { 
     750#if 1 //c 
     751 
     752  if (stream.peekByte() >= 0) { 
     753     
     754    // New line 
     755    //Serial.println("Process new line..."); 
     756    if (stream.peekByte() != ':') { 
     757      // Skip local responses/messages entirely. TODO: Handle PINGs? 
     758      while (stream.skipSegment("\x0A\x0D") < 0) { 
     759        // Ummm, we ran outta data--this screws things up... 
     760        // TODO: Have a time out? 
     761        delay(500);  
     762      } 
     763    } else { 
     764      //Serial.println(state, HEX); 
     765      if (state == 0) { 
     766      // Skip sending servername // TODO: Check it's the main one? 
     767      while (stream.skipSegment(" ") < 0) { 
     768        // Ummm, we ran outta data--this screws things up... 
     769        // TODO: Have a time out? 
     770        delay(500);  
     771      } 
     772       
     773      for (int idx=0; idx < 3; idx++) { 
     774        while (stream.peekByte() < 0) { // TODO: Time out? 
     775          delay(500); 
     776        } 
     777        buf[idx] = stream.readByte(); 
     778        //Serial.print(buf[idx], BYTE); 
     779      } 
     780      //Serial.print("//"); 
     781      Serial.println((const char *) buf);   
     782 
     783      if (strcmp((const char *) buf, "376") == 0) { // End MOTD 
     784        state=1; 
     785        conn.print("JOIN #arduino\n");         
     786        //stream.debug = 1; 
     787      } 
     788      } else if (state == 1) { 
     789        //stream.debug = 1; 
     790        //Serial.println("Skip servername"); 
     791          // Skip sending servername // TODO: Check it's the main one? 
     792          while (stream.skipSegment(" ") < 0) { 
     793            // Ummm, we ran outta data--this screws things up... 
     794            // TODO: Have a time out? 
     795            delay(500);  
     796          } 
     797        //stream.debug = 0; 
     798         
     799        /* 
     800          int idx = 0; 
     801          const char * PRIVMSG = "PRIVMSG"; 
     802          //Serial.println("Checking for PRIVMSG."); 
     803           
     804          //stream.debug = 1; 
     805 
     806          while ((byteRead = stream.readSegmentByte(" ")) >=0) { 
     807            if (idx < strlen(PRIVMSG)) { 
     808              if (byteRead != PRIVMSG[idx]) { 
     809                break; 
     810              } 
     811              idx++; 
     812            } else { 
     813              break; 
     814            } 
     815          } 
     816           
     817         // stream.debug = 0; 
     818 
     819           
     820          //Serial.println("Finished check PRIVMSG, determining result."); 
     821           
     822          if ((byteRead == -1) && (idx == strlen(PRIVMSG))) { // Matched 
     823            //Serial.println("Matched.");           
     824            while (stream.skipSegment(" ") < 0) { 
     825              // Ummm, we ran outta data--this screws things up... 
     826              // TODO: Have a time out? 
     827              delay(500);  
     828            } 
     829          */ 
     830           
     831          if (stream.gobbleMatch(" ", "PRIVMSG") > 0) { 
     832            /* ** 
     833            while ((byteRead = stream.readSegmentByte("\x0A\x0D")) >=0) { 
     834              Serial.print(byteRead, BYTE); 
     835            } 
     836            Serial.println(""); 
     837            */ 
     838            Serial.println("Matched PRIV MSG"); 
     839            if (stream.gobbleMatch(" :", "#arduino") > 0) { // We treat the ":" as a separator too--does this break? 
     840              Serial.println("Matched #arduino"); 
     841              //Serial.println(stream.peekByte(), HEX); 
     842              //if (stream.gobbleMatch("\x0A\x0D", "arduino:") > 0) { 
     843              //if (stream.gobbleMatch(":", "Arduino") > 0) {   
     844              //Serial.println("Matched Arduino");                 
     845              if ((stream.peekByte() == 'A') && (stream.gobbleMatch(":", "Arduino") > 0)) { 
     846                Serial.println("Matched something"); 
     847                //conn.print("PRIVMSG #arduino :Huh, what? Was someone talking to me?\n"); 
     848                conn.print("PRIVMSG #arduino :My current pot status is "); 
     849                conn.print(((analogRead(2)*70)/630)/10+'0'); 
     850                conn.print("3%, man!\n"); 
     851              } 
     852            }  
     853             
     854          } else { 
     855            //Serial.println("No match."); 
     856          } 
     857           
     858           
     859           
     860      } else { 
     861        while ((byteRead = stream.readSegmentByte("\x0A\x0D")) >=0) { 
     862          Serial.print(byteRead, BYTE); 
     863        } 
     864        Serial.println(""); 
     865      } 
     866      //Serial.println("Skipping this:"); 
     867      //stream.debug = 1; 
     868      while (stream.skipSegment("\x0A\x0D") < 0) { 
     869        // Ummm, we ran outta data--this screws things up... 
     870        // TODO: Have a time out? 
     871        delay(500);  
     872      } 
     873      //stream.debug = 0; 
     874      //Serial.println("\nFinish skipping."); 
     875 
     876    } 
     877    
     878  }  
     879 
     880#else //c 
     881     if (conn.available()) { 
     882       byteRead = conn.read(); 
     883       Serial.print(byteRead, BYTE); 
     884       if (byteRead == '\n') { 
     885         dataWait++; 
     886         Serial.println(dataWait, DEC); 
     887       } 
     888       if (dataWait==4) { 
     889          //conn.print("NICK Arduino\n"); 
     890          //conn.print("USER Arduino Arduino Arduino Arduino\n"); 
     891          dataWait++; 
     892       } 
     893       if (dataWait==67) { 
     894         conn.print("JOIN #arduino\n"); 
     895         //conn.print("PRIVMSG #arduino :Hello, world!\n"); 
     896         //delay(10000); 
     897         conn.print("QUIT\n"); 
     898         dataWait++; 
     899       } 
     900       //conn.print(conn.read()); 
     901     } else { 
     902        //dataWait++; 
     903        //Serial.print(dataWait, DEC); 
     904     } 
     905#endif //c 
     906  } 
     907   
     908  Serial.println(""); 
     909 
     910  conn.close(); 
     911 
     912 
     913#else //b 
    517914  NetworkConnection conn = Network.listen(7); 
    518915 
     
    536933 
    537934  conn.close(); 
     935#endif  //b 
    538936#else 
    539937