wifly/socket interface for wifly modules

Dependents:   PROJET_MATTHIEU_2019 PROJET_MATTHIEU_2019

Committer:
samux
Date:
Thu Aug 23 10:32:45 2012 +0000
Revision:
4:74bfdd00362a
Parent:
1:8f04181f9ad8
Child:
5:8e253731f76f
no reboot needed/add timeout param in send function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 4:74bfdd00362a 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 4:74bfdd00362a 2 *
samux 4:74bfdd00362a 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 4:74bfdd00362a 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 4:74bfdd00362a 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 4:74bfdd00362a 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 4:74bfdd00362a 7 * furnished to do so, subject to the following conditions:
samux 4:74bfdd00362a 8 *
samux 4:74bfdd00362a 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 4:74bfdd00362a 10 * substantial portions of the Software.
samux 4:74bfdd00362a 11 *
samux 4:74bfdd00362a 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 4:74bfdd00362a 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 4:74bfdd00362a 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 4:74bfdd00362a 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 4:74bfdd00362a 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 4:74bfdd00362a 17 *
samux 4:74bfdd00362a 18 * @section DESCRIPTION
samux 4:74bfdd00362a 19 *
samux 4:74bfdd00362a 20 * Wifly RN131-C, wifi module
samux 4:74bfdd00362a 21 *
samux 4:74bfdd00362a 22 * Datasheet:
samux 4:74bfdd00362a 23 *
samux 4:74bfdd00362a 24 * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf
samux 4:74bfdd00362a 25 */
samux 4:74bfdd00362a 26
samux 4:74bfdd00362a 27 #ifndef WIFLY_H
samux 4:74bfdd00362a 28 #define WIFLY_H
samux 4:74bfdd00362a 29
samux 4:74bfdd00362a 30 #include "mbed.h"
samux 4:74bfdd00362a 31 #include "CBuffer.h"
samux 4:74bfdd00362a 32
samux 4:74bfdd00362a 33 #define DEFAULT_WAIT_RESP_TIMEOUT 500
samux 4:74bfdd00362a 34
samux 4:74bfdd00362a 35 class Wifly {
samux 4:74bfdd00362a 36
samux 4:74bfdd00362a 37 public:
samux 4:74bfdd00362a 38 /*
samux 4:74bfdd00362a 39 * Constructor for joining open, wep or wpa secured networks
samux 4:74bfdd00362a 40 *
samux 4:74bfdd00362a 41 * @param tx mbed pin to use for tx line of Serial interface
samux 4:74bfdd00362a 42 * @param rx mbed pin to use for rx line of Serial interface
samux 4:74bfdd00362a 43 * @param reset reset pin of the wifi module ()
samux 4:74bfdd00362a 44 * @param tcp_status connection status pin of the wifi module (GPIO 6)
samux 4:74bfdd00362a 45 * @param ssid ssid of the network
samux 4:74bfdd00362a 46 * @param phrase WEP or WPA key
samux 4:74bfdd00362a 47 * @param wpa true if wpa security false otherwise
samux 4:74bfdd00362a 48 */
samux 4:74bfdd00362a 49 Wifly( PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, bool wpa);
samux 4:74bfdd00362a 50
samux 4:74bfdd00362a 51 /*
samux 4:74bfdd00362a 52 * 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.
samux 4:74bfdd00362a 53 * Useful to send a command to the module and wait a response.
samux 4:74bfdd00362a 54 *
samux 4:74bfdd00362a 55 *
samux 4:74bfdd00362a 56 * @param str string to be sent
samux 4:74bfdd00362a 57 * @param len string length
samux 4:74bfdd00362a 58 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
samux 4:74bfdd00362a 59 * @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)
samux 4:74bfdd00362a 60 *
samux 4:74bfdd00362a 61 * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
samux 4:74bfdd00362a 62 */
samux 4:74bfdd00362a 63 int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 4:74bfdd00362a 64
samux 4:74bfdd00362a 65 /*
samux 4:74bfdd00362a 66 * Connect the wifi module to the ssid contained in the constructor.
samux 4:74bfdd00362a 67 *
samux 4:74bfdd00362a 68 * @return true if connected, false otherwise
samux 4:74bfdd00362a 69 */
samux 4:74bfdd00362a 70 bool join();
samux 4:74bfdd00362a 71
samux 4:74bfdd00362a 72 /*
samux 4:74bfdd00362a 73 * Close a connection with the access point
samux 4:74bfdd00362a 74 *
samux 4:74bfdd00362a 75 * @ returns true if successful
samux 4:74bfdd00362a 76 */
samux 4:74bfdd00362a 77 bool leave();
samux 4:74bfdd00362a 78
samux 4:74bfdd00362a 79 /*
samux 4:74bfdd00362a 80 * Read a string if available
samux 4:74bfdd00362a 81 *
samux 4:74bfdd00362a 82 *@param str pointer where will be stored the string read
samux 4:74bfdd00362a 83 */
samux 4:74bfdd00362a 84 bool read(char * str);
samux 4:74bfdd00362a 85
samux 4:74bfdd00362a 86 /*
samux 4:74bfdd00362a 87 * Reset the wifi module
samux 4:74bfdd00362a 88 */
samux 4:74bfdd00362a 89 void reset();
samux 4:74bfdd00362a 90
samux 4:74bfdd00362a 91 /*
samux 4:74bfdd00362a 92 * Check if characters are available
samux 4:74bfdd00362a 93 *
samux 4:74bfdd00362a 94 * @return number of available characters
samux 4:74bfdd00362a 95 */
samux 4:74bfdd00362a 96 int readable();
samux 4:74bfdd00362a 97
samux 4:74bfdd00362a 98 /*
samux 4:74bfdd00362a 99 * Check if characters are available
samux 4:74bfdd00362a 100 *
samux 4:74bfdd00362a 101 * @return number of available characters
samux 4:74bfdd00362a 102 */
samux 4:74bfdd00362a 103 int writeable();
samux 4:74bfdd00362a 104
samux 4:74bfdd00362a 105 /*
samux 4:74bfdd00362a 106 * Check if a tcp link is active
samux 4:74bfdd00362a 107 *
samux 4:74bfdd00362a 108 * @returns true if successful
samux 4:74bfdd00362a 109 */
samux 4:74bfdd00362a 110 bool is_connected();
samux 4:74bfdd00362a 111
samux 4:74bfdd00362a 112 /*
samux 4:74bfdd00362a 113 * Read a character
samux 4:74bfdd00362a 114 *
samux 4:74bfdd00362a 115 * @return the character read
samux 4:74bfdd00362a 116 */
samux 4:74bfdd00362a 117 char getc();
samux 4:74bfdd00362a 118
samux 4:74bfdd00362a 119 /*
samux 4:74bfdd00362a 120 * Flush the buffer
samux 4:74bfdd00362a 121 */
samux 4:74bfdd00362a 122 void flush();
samux 4:74bfdd00362a 123
samux 4:74bfdd00362a 124 /*
samux 4:74bfdd00362a 125 * Write a character
samux 4:74bfdd00362a 126 *
samux 4:74bfdd00362a 127 * @param the character which will be written
samux 4:74bfdd00362a 128 */
samux 4:74bfdd00362a 129 int putc(char c);
samux 4:74bfdd00362a 130
samux 4:74bfdd00362a 131
samux 4:74bfdd00362a 132 /*
samux 4:74bfdd00362a 133 * To enter in command mode (we can configure the module)
samux 4:74bfdd00362a 134 *
samux 4:74bfdd00362a 135 * @return true if successful, false otherwise
samux 4:74bfdd00362a 136 */
samux 4:74bfdd00362a 137 bool cmdMode();
samux 4:74bfdd00362a 138
samux 4:74bfdd00362a 139 /*
samux 4:74bfdd00362a 140 * To exit the command mode
samux 4:74bfdd00362a 141 *
samux 4:74bfdd00362a 142 * @return true if successful, false otherwise
samux 4:74bfdd00362a 143 */
samux 4:74bfdd00362a 144 bool exit();
samux 4:74bfdd00362a 145
samux 4:74bfdd00362a 146 /*
samux 4:74bfdd00362a 147 * Close a tcp connection
samux 4:74bfdd00362a 148 *
samux 4:74bfdd00362a 149 * @ returns true if successful
samux 4:74bfdd00362a 150 */
samux 4:74bfdd00362a 151 bool close();
samux 4:74bfdd00362a 152
samux 4:74bfdd00362a 153 /*
samux 4:74bfdd00362a 154 * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
samux 4:74bfdd00362a 155 *
samux 4:74bfdd00362a 156 * @param str string to be sent
samux 4:74bfdd00362a 157 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
samux 4:74bfdd00362a 158 * @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)
samux 4:74bfdd00362a 159 *
samux 4:74bfdd00362a 160 * @returns true if successful
samux 4:74bfdd00362a 161 */
samux 4:74bfdd00362a 162 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
samux 4:74bfdd00362a 163
samux 4:74bfdd00362a 164 bool dnsLookup(const char * host, char * ip);
samux 4:74bfdd00362a 165
samux 4:74bfdd00362a 166 static Wifly * getInstance() {return inst;};
samux 4:74bfdd00362a 167
samux 4:74bfdd00362a 168 protected:
samux 4:74bfdd00362a 169 Serial wifi;
samux 4:74bfdd00362a 170 DigitalOut reset_pin;
samux 4:74bfdd00362a 171 DigitalIn tcp_status;
samux 4:74bfdd00362a 172 bool wpa;
samux 4:74bfdd00362a 173 bool dhcp;
samux 4:74bfdd00362a 174 const char * phrase;
samux 4:74bfdd00362a 175 const char * ssid;
samux 4:74bfdd00362a 176 const char * ip;
samux 4:74bfdd00362a 177 const char * netmask;
samux 4:74bfdd00362a 178 const char * gateway;
samux 4:74bfdd00362a 179 int channel;
samux 4:74bfdd00362a 180 CircBuffer<char> buf_wifly;
samux 4:74bfdd00362a 181
samux 4:74bfdd00362a 182 bool cmd_mode;
samux 4:74bfdd00362a 183
samux 4:74bfdd00362a 184 static Wifly * inst;
samux 4:74bfdd00362a 185
samux 4:74bfdd00362a 186 void attach_rx(bool null);
samux 4:74bfdd00362a 187 void handler_rx(void);
samux 4:74bfdd00362a 188 };
samux 4:74bfdd00362a 189
samux 0:6ffb0aeb3972 190 #endif