Index: rtos_arduino/trunk/arduino_lib/libraries/ESP8266_Arudino_AT/ESP8266.h
===================================================================
--- rtos_arduino/trunk/arduino_lib/libraries/ESP8266_Arudino_AT/ESP8266.h	(revision 144)
+++ rtos_arduino/trunk/arduino_lib/libraries/ESP8266_Arudino_AT/ESP8266.h	(revision 177)
@@ -26,12 +26,59 @@
 #include "Arduino.h"
 
-
 //#define ESP8266_USE_SOFTWARE_SERIAL
 
+#define ESP8266_SUPPORT_VERSION "AT version:0.25"
+
+#define ESP8266_NUM_CONNECTION 5
 
 #ifdef ESP8266_USE_SOFTWARE_SERIAL
 #include "SoftwareSerial.h"
-#endif
-
+#endif /* ESP8266_USE_SOFTWARE_SERIAL */
+
+#define ESP8266_CONNECTION_OPEN     0x01
+#define ESP8266_CONNECTION_CLOSE    0x02
+
+#define ESP8266_CONNECTION_TCP      0x01
+#define ESP8266_CONNECTION_UDP      0x02
+
+#define ESP8266_CONNECTION_CLIENT   0x01
+#define ESP8266_CONNECTION_SERVER   0x02
+
+#define ESP8266_STATUS_GOTIP        0x02
+#define ESP8266_STATUS_CONNECTED    0x03
+#define ESP8266_STATUS_DISCONNECTED 0x04
+
+#define ESP8266_WMODE_STATION       0x01
+#define ESP8266_WMODE_SOFTAP        0x02
+#define ESP8266_WMODE_AP_STATION    0x03
+
+struct esp8266_connection_status
+{
+    uint8_t connection_type;
+    uint8_t protocol_type;
+};
+
+#define ESP8266_RINGBUFFER_SIZE  512
+
+class ESP8266_RingBuffer
+{
+  public:
+    ESP8266_RingBuffer(void);
+    void init(void);
+    void free(void) {len = head = tail = 0;};
+    bool write(uint8_t c);
+    uint8_t read(void);
+    uint32_t copy(uint8_t *pdata, uint32_t size);
+    uint8_t peek(void);
+    bool available(void) {return (len > 0);};
+    bool isFull(void) {return (len == ESP8266_RINGBUFFER_SIZE);};
+    uint32_t length(void) {return len;};
+    
+  private:
+    uint8_t *buffer;
+    uint32_t head;
+    uint32_t tail;
+    uint32_t len;
+};
 
 /**
@@ -41,5 +88,4 @@
  public:
 
-#ifdef ESP8266_USE_SOFTWARE_SERIAL
     /*
      * Begin.
@@ -48,19 +94,15 @@
      * @param baud - the buad rate to communicate with ESP8266(default:115200). 
      *
-     * @warning parameter baud depends on the AT firmware. 115200 is an common value. 
-     */
-    void begin(SoftwareSerial &uart, uint32_t baud = 115200);
+     * @retval 0 : Success
+     * @retval 1 : Can not communicate ESP8266
+     * @retval 2 : Firmware Version mismatch.
+     *      
+     * @warning parameter baud depends on the AT firmware. 115200 is an common value.
+     */
+#ifdef ESP8266_USE_SOFTWARE_SERIAL    
+    int begin(SoftwareSerial &uart, uint32_t baud = 115200);
 #else /* HardwareSerial */
-    /*
-     * Begin.
-     *
-     * @param uart - an reference of HardwareSerial object. 
-     * @param baud - the buad rate to communicate with ESP8266(default:115200). 
-     *
-     * @warning parameter baud depends on the AT firmware. 115200 is an common value. 
-     */
-    void begin(HardwareSerial &uart, uint32_t baud = 115200);
-#endif
-    
+    int begin(HardwareSerial &uart, uint32_t baud = 115200);
+#endif /* ESP8266_USE_SOFTWARE_SERIAL */
     
     /** 
@@ -176,5 +218,5 @@
      */
     String getLocalIP(void);
-    
+
     /**
      * Enable IP MUX(multiple connection mode). 
@@ -390,5 +432,5 @@
      */
     uint32_t recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
-    
+
     /**
      * Receive data from all of TCP or UDP builded already in multiple mode. 
@@ -404,12 +446,51 @@
      */
     uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
-
-    /**
-     * Check data is available.
-     *
-     * @return 1 : data is available, 0 : data is not available
-     */
-    int dataAvailable(void);
-
+    
+    /**
+     * Check data is available in specific connection.
+     * 
+     * @param mux_id - the identifier of this TCP(available value: 0 - 4).      
+     * @return true - data is available.
+     * @return false - data is not available.
+     */
+    bool isDataAvailable(uint8_t mux_id);
+
+    /**
+     * Check data is available in any connection.
+     *
+     * @return true - data is available.
+     * @return false - data is not available.
+     */
+    bool isDataAvailable(void);
+
+    /**
+     * Check connection is opned in single mode. 
+     *
+     * @return true  - opend.
+     * @return false - closed.
+     */
+    bool isConnected(void);
+
+    /**
+     * Check specific connection is opned in multiple mode. 
+     *
+     * @return true  - opend.
+     * @return false - closed.
+     */
+    bool isConnected(uint8_t mux_id);
+
+    /**
+     * Get connection link status
+     *
+     * @return true  - success.
+     * @return false - error.
+     */    
+    bool getMuxCStatus(uint8_t *mux_id_ptn);
+
+    /**
+     *  
+     */ 
+    ESP8266_RingBuffer rx_buffer[ESP8266_NUM_CONNECTION];
+    
  private:
 
@@ -418,19 +499,14 @@
      */
     void rx_empty(void);
- 
+
     /* 
-     * Recvive data from uart. Return all received data if target found or timeout. 
-     */
-    String recvString(String target, uint32_t timeout = 1000);
-    
-    /* 
-     * Recvive data from uart. Return all received data if one of target1 and target2 found or timeout. 
-     */
-    String recvString(String target1, String target2, uint32_t timeout = 1000);
-    
+     * Read all data in UART RX.
+     */
+    void rx_update(void);    
+     
     /* 
      * Recvive data from uart. Return all received data if one of target1, target2 and target3 found or timeout. 
      */
-    String recvString(String target1, String target2, String target3, uint32_t timeout = 1000);
+    int recvString(String target1, String target2, String target3, uint32_t timeout = 1000);
     
     /* 
@@ -440,5 +516,5 @@
     
     /* 
-     * Recvive data from uart and search first target and cut out the substring between begin and end(excluding begin and end self). 
+     * Recvive data from uart and search first target and cut out the substring between begin and end(excluding begin and end self). a
      * Return true if target found, false for timeout.
      */
@@ -454,6 +530,5 @@
      * @param coming_mux_id - in single connection mode, should be NULL and not NULL in multiple. 
      */
-    uint32_t recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_len, uint32_t timeout, uint8_t *coming_mux_id);
-    
+    uint32_t recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout, uint8_t mux_id, uint8_t *coming_mux_id);
     
     bool eAT(void);
@@ -461,10 +536,10 @@
     bool eATGMR(String &version);
     
-    bool qATCWMODE(uint8_t *mode);
-    bool sATCWMODE(uint8_t mode);
-    bool sATCWJAP(String ssid, String pwd);
+    bool qATCWMODE_CUR(uint8_t *mode);
+    bool sATCWMODE_CUR(uint8_t mode);
+    bool sATCWJAP_CUR(String ssid, String pwd);
     bool eATCWLAP(String &list);
     bool eATCWQAP(void);
-    bool sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn);
+    bool sATCWSAP_CUR(String ssid, String pwd, uint8_t chl, uint8_t ecn);
     bool eATCWLIF(String &list);
     
@@ -482,9 +557,11 @@
     bool sATCIPSERVER(uint8_t mode, uint32_t port = 333);
     bool sATCIPSTO(uint32_t timeout);
-    
-    /*
-     * +IPD,len:data
-     * +IPD,id,len:data
-     */
+
+    void initialize_status(void);
+    
+    uint32_t recvIPD(uint32_t timeout, uint8_t *buffer = NULL, uint32_t buffer_size = 0,
+                     uint8_t mux_id = 0, uint8_t *coming_mux_id = NULL);
+
+    void recvAsyncdata(uint32_t timeout = 0);
     
 #ifdef ESP8266_USE_SOFTWARE_SERIAL
@@ -494,4 +571,11 @@
 #endif
     uint32_t m_baud;
+
+    uint8_t wifi_status;
+    bool    mux_mode;
+    uint8_t connection_bitmap;
+    esp8266_connection_status connection_status[ESP8266_NUM_CONNECTION];
+
+    String rx_cmd;
 };
 
