wifly/socket interface for wifly modules

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Revision:
11:b912f91e3212
Parent:
8:9a3cd07ed7e8
--- a/Wifly/Wifly.h	Thu Aug 23 16:03:52 2012 +0000
+++ b/Wifly/Wifly.h	Fri Aug 24 13:07:01 2012 +0000
@@ -38,7 +38,13 @@
     WPA = 3
 };
 
-class Wifly {
+enum Protocol {
+    UDP = (1 << 0),
+    TCP = (1 << 1)
+};
+
+class Wifly
+{
 
 public:
     /*
@@ -55,39 +61,36 @@
     Wifly(  PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec);
 
     /*
-    * Send a string to the wifi module by serial port. This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module.
-    * Useful to send a command to the module and wait a response.
-    *
-    *
-    * @param str string to be sent
-    * @param len string length
-    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
-    * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
-    *
-    * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
-    */
-    int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
-
-    /*
     * Connect the wifi module to the ssid contained in the constructor.
     *
     * @return true if connected, false otherwise
     */
     bool join();
-    
+
     /*
-    * Close a connection with the access point
+    * Disconnect the wifly module from the access point
     *
     * @ returns true if successful
     */
-    bool leave();
+    bool disconnect();
 
     /*
-    * Read a string if available
+    * Open a tcp connection with the specified host on the specified port
     *
-    *@param str pointer where will be stored the string read
+    * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
+    * @param port port
+    * @ returns true if successful
     */
-    bool read(char * str);
+    bool connect(const char * host, int port);
+
+
+    /*
+    * Set the protocol (UDP or TCP)
+    *
+    * @param p protocol
+    * @ returns true if successful
+    */
+    bool setProtocol(Protocol p);
 
     /*
     * Reset the wifi module
@@ -100,14 +103,14 @@
     * @return number of available characters
     */
     int readable();
-    
+
     /*
     * Check if characters are available
     *
     * @return number of available characters
     */
     int writeable();
-    
+
     /*
     * Check if a tcp link is active
     *
@@ -148,14 +151,28 @@
     * @return true if successful, false otherwise
     */
     bool exit();
-    
+
     /*
     * Close a tcp connection
     *
     * @ returns true if successful
     */
     bool close();
-    
+
+    /*
+    * Send a string to the wifi module by serial port. This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module.
+    * Useful to send a command to the module and wait a response.
+    *
+    *
+    * @param str string to be sent
+    * @param len string length
+    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
+    * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
+    *
+    * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
+    */
+    int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
+
     /*
     * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
     *
@@ -166,17 +183,17 @@
     * @returns true if successful
     */
     bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
-    
-    bool dnsLookup(const char * host, char * ip);
-    
-    static Wifly * getInstance() {return inst;};
+
+    bool gethostbyname(const char * host, char * ip);
+
+    static Wifly * getInstance() {
+        return inst;
+    };
 
 protected:
     Serial wifi;
     DigitalOut reset_pin;
     DigitalIn tcp_status;
-    bool wpa;
-    bool dhcp;
     char phrase[30];
     char ssid[30];
     const char * ip;
@@ -184,15 +201,24 @@
     const char * gateway;
     int channel;
     CircBuffer<char> buf_wifly;
-    Security security;
-    char * getStringSecurity();
-    
-    bool cmd_mode;
-    
+
     static Wifly * inst;
 
     void attach_rx(bool null);
     void handler_rx(void);
+
+
+    typedef struct STATE {
+        bool associated;
+        bool tcp;
+        bool dhcp;
+        Security sec;
+        Protocol proto;
+        bool cmd_mode;
+    } State;
+
+    State state;
+    char * getStringSecurity();
 };
 
 #endif
\ No newline at end of file