Provide an easy-to-use way to manipulate ESP8266.

Dependencies:   ArduinoAPI

Dependents:   WeeESP8266_TCPClientMultiple WeeESP8266_TCPClientSingle WeeESP8266_TCPServer WeeESP8266_UDPClientMultiple ... more

Revision:
3:9f745022331a
Parent:
2:fb209f93f3f1
Child:
4:c8e2381d34d2
diff -r fb209f93f3f1 -r 9f745022331a ESP8266.h
--- a/ESP8266.h	Fri Feb 06 00:55:22 2015 +0000
+++ b/ESP8266.h	Fri Feb 06 04:01:50 2015 +0000
@@ -29,11 +29,15 @@
 class ESP8266 {
  public:
  
-    /* Constructors */
-    ESP8266(PinName tx, PinName rx, int32_t baud_rate = 9600);
+    /**
+     * Constuctor. 
+     *
+     * @param uart - an reference of ArduinoSerial object with baud 9600. 
+     */
+    ESP8266(ArduinoSerial &uart); /* baud rate is 9600 */
     
     /** 
-     * Verify ESP8266 live or not. 
+     * Verify ESP8266 whether live or not. 
      *
      * Actually, this method will send command "AT" to ESP8266 and waiting for "OK". 
      * 
@@ -129,11 +133,224 @@
      * @return the list of IP.
      * @note This method should not be called when station mode. 
      */
-    String getDeviceIP(void);
+    String getJoinedDeviceIP(void);
+    
+    /**
+     * Get the current status of connection(UDP and TCP). 
+     * 
+     * @return the status. 
+     */
+    String getIPStatus(void);
+    
+    /**
+     * Get the IP address of ESP8266. 
+     *
+     * @return the IP list. 
+     */
+    String getLocalIP(void);
+    
+    /**
+     * Enable IP MUX(multiple connection mode). 
+     *
+     * In multiple connection mode, a couple of TCP and UDP communication can be builded. 
+     * They can be distinguished by the identifier of TCP or UDP named mux_id. 
+     * 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool enableMUX(void);
+    
+    /**
+     * Disable IP MUX(single connection mode). 
+     *
+     * In single connection mode, only one TCP or UDP communication can be builded. 
+     * 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool disableMUX(void);
+    
+    /*
+     * Single connection mode methods
+     */
+    
+    /**
+     * Create TCP connection in single mode. 
+     * 
+     * @param addr - the IP or domain name of the target host. 
+     * @param port - the port number of the target host. 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool createTCP(String addr, uint32_t port);
+    
+    /**
+     * Release TCP connection in single mode. 
+     * 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool releaseTCP(void);
+    
+    /**
+     * Register UDP port number in single mode.
+     * 
+     * @param addr - the IP or domain name of the target host. 
+     * @param port - the port number of the target host. 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool registerUDP(String addr, uint32_t port);
+    
+    /**
+     * Unregister UDP port number in single mode. 
+     * 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool unregisterUDP(void);
+    
+    /**
+     * Send data based on TCP or UDP builded already in single mode. 
+     * 
+     * @param buffer - the buffer of data to send. 
+     * @param len - the length of data to send. 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool send(const uint8_t *buffer, uint32_t len);
+    
+    /**
+     * Receive data from TCP or UDP builded already in single mode. 
+     *
+     * @param buffer - the buffer for storing data. 
+     * @param len - the length of the buffer. 
+     * @param timeout - the time waiting data. 
+     * @return the length of data received actually. 
+     */
+    uint32_t recv(uint8_t *buffer, uint32_t len, uint32_t timeout = 1000);
+    
+    /*
+     * Multiple connection mode methods
+     */
+     
+    /**
+     * Create TCP connection in multiple mode. 
+     * 
+     * @param mux_id - the identifier of this TCP(available value: 0 - 4). 
+     * @param addr - the IP or domain name of the target host. 
+     * @param port - the port number of the target host. 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool createTCP(uint8_t mux_id, String addr, uint32_t port);
+    
+    /**
+     * Release TCP connection in multiple mode. 
+     * 
+     * @param mux_id - the identifier of this TCP(available value: 0 - 4). 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool releaseTCP(uint8_t mux_id);
+    
+    /**
+     * Register UDP port number in multiple mode.
+     * 
+     * @param mux_id - the identifier of this TCP(available value: 0 - 4). 
+     * @param addr - the IP or domain name of the target host. 
+     * @param port - the port number of the target host. 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool registerUDP(uint8_t mux_id, String addr, uint32_t port);
+    
+    /**
+     * Unregister UDP port number in multiple mode. 
+     * 
+     * @param mux_id - the identifier of this TCP(available value: 0 - 4). 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool unregisterUDP(uint8_t mux_id);
+    
+    /**
+     * Send data based on one of TCP or UDP builded already in multiple mode. 
+     * 
+     * @param mux_id - the identifier of this TCP(available value: 0 - 4). 
+     * @param buffer - the buffer of data to send. 
+     * @param len - the length of data to send. 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool send(uint8_t mux_id, const char *buffer, uint32_t len);
+    
+    /**
+     * Receive data from one of TCP or UDP builded already in multiple mode. 
+     *
+     * @param mux_id - the identifier of this TCP(available value: 0 - 4). 
+     * @param buffer - the buffer for storing data. 
+     * @param len - the length of the buffer. 
+     * @param timeout - the time waiting data. 
+     * @return the length of data received actually. 
+     */
+    uint32_t recv(uint8_t mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout = 1000);
+    
+    /**
+     * Receive data from all of TCP or UDP builded already in multiple mode. 
+     *
+     * After return, coming_mux_id store the id of TCP or UDP from which data coming. 
+     * User should read the value of coming_mux_id and decide what next to do. 
+     * 
+     * @param coming_mux_id - the identifier of TCP or UDP. 
+     * @param buffer - the buffer for storing data. 
+     * @param len - the length of the buffer. 
+     * @param timeout - the time waiting data. 
+     * @return the length of data received actually. 
+     */
+    uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout = 1000);
+    
+    /*
+     * Server 
+     */
+     
+    /**
+     * Set the timeout of TCP Server. 
+     * 
+     * @param timeout - the duration for timeout by second(0 ~ 28800, default:180). 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool setTCPServerTimeout(uint32_t timeout = 180);
+    
+    /**
+     * Start TCP Server(Only in multiple mode). 
+     * 
+     * After started, user should call method: getIPStatus to know the status of TCP connections. 
+     * The methods of receiving data can be called for user's any purpose. After communication, 
+     * release the TCP connection is needed by calling method: releaseTCP with mux_id. 
+     *
+     * @param port - the port number to listen(default: 333).
+     * @retval true - success.
+     * @retval false - failure.
+     *
+     * @see String getIPStatus(void);
+     * @see uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout);
+     * @see bool releaseTCP(uint8_t mux_id);
+     */
+    bool startTCPServer(uint32_t port = 333);
     
     
+    /**
+     * Stop TCP Server(Only in multiple mode). 
+     * 
+     * @retval true - success.
+     * @retval false - failure.
+     */
+    bool stopTCPServer(void);
+    
  private:
-    ArduinoSerial m_uart; /* The UART to communicate with ESP8266 */
+    ArduinoSerial *m_puart; /* The UART to communicate with ESP8266 */
  
 };