GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/

Dependents:   GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more

Fork of WiflyInterface by mbed official

GainSpan Wi-Fi library

The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.

mbed RTOS supported.

/media/uploads/gsfan/gs_im_002.jpg /media/uploads/gsfan/gs1011m_2.jpg

ゲインスパン Wi-Fi モジュール ライブラリ

ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。

mbed RTOS に対応しています。(mbed2.0)

Committer:
gsfan
Date:
Fri Jan 10 14:23:33 2014 +0000
Revision:
14:1e89a3f0282d
Parent:
13:189e04ac70bd
Child:
15:086d1a33a197
supported LPC800-MAX

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsfan 11:71d67fea5ace 1 /* Copyright (C) 2013 gsfan, MIT License
gsfan 5:78943b3945b5 2 *
gsfan 5:78943b3945b5 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
gsfan 5:78943b3945b5 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
gsfan 5:78943b3945b5 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
gsfan 5:78943b3945b5 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
gsfan 5:78943b3945b5 7 * furnished to do so, subject to the following conditions:
gsfan 5:78943b3945b5 8 *
gsfan 5:78943b3945b5 9 * The above copyright notice and this permission notice shall be included in all copies or
gsfan 5:78943b3945b5 10 * substantial portions of the Software.
gsfan 5:78943b3945b5 11 *
gsfan 5:78943b3945b5 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
gsfan 5:78943b3945b5 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
gsfan 5:78943b3945b5 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
gsfan 5:78943b3945b5 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gsfan 5:78943b3945b5 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
gsfan 5:78943b3945b5 17 *
gsfan 5:78943b3945b5 18 * @section DESCRIPTION
gsfan 5:78943b3945b5 19 *
gsfan 5:78943b3945b5 20 * GainSpan GS1011, Wi-Fi module
gsfan 5:78943b3945b5 21 *
gsfan 5:78943b3945b5 22 * http://www.gainspan.com/modules
gsfan 5:78943b3945b5 23 */
gsfan 8:64184a968e3b 24 /** @file
gsfan 8:64184a968e3b 25 * @brief Gainspan wi-fi module library for mbed
gsfan 8:64184a968e3b 26 * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc.
gsfan 8:64184a968e3b 27 */
gsfan 5:78943b3945b5 28
gsfan 5:78943b3945b5 29 #ifndef GSwifi_H
gsfan 5:78943b3945b5 30 #define GSwifi_H
gsfan 5:78943b3945b5 31
gsfan 11:71d67fea5ace 32 #include "GSwifi_conf.h"
gsfan 11:71d67fea5ace 33
gsfan 5:78943b3945b5 34 #include "mbed.h"
gsfan 8:64184a968e3b 35 #include "RawSerial.h"
gsfan 5:78943b3945b5 36 #include "CBuffer.h"
gsfan 8:64184a968e3b 37 #include <ctype.h>
gsfan 8:64184a968e3b 38 #include <stdlib.h>
gsfan 8:64184a968e3b 39 #include <string.h>
gsfan 8:64184a968e3b 40
gsfan 11:71d67fea5ace 41 #ifdef CFG_ENABLE_RTOS
gsfan 11:71d67fea5ace 42 #include "rtos.h"
gsfan 8:64184a968e3b 43 #endif
gsfan 5:78943b3945b5 44
gsfan 5:78943b3945b5 45 //Debug is disabled by default
gsfan 8:64184a968e3b 46 #if defined(DEBUG) and (!defined(TARGET_LPC11U24))
gsfan 8:64184a968e3b 47 #define DBG(x, ...) std::printf("[DBG]" x "\r\n", ##__VA_ARGS__);
gsfan 8:64184a968e3b 48 #define WARN(x, ...) std::printf("[WARN]" x "\r\n", ##__VA_ARGS__);
gsfan 8:64184a968e3b 49 #define ERR(x, ...) std::printf("[ERR]" x "\r\n", ##__VA_ARGS__);
gsfan 8:64184a968e3b 50 #define INFO(x, ...) std::printf("[INFO]" x "\r\n", ##__VA_ARGS__);
gsfan 5:78943b3945b5 51 #else
gsfan 5:78943b3945b5 52 #define DBG(x, ...)
gsfan 5:78943b3945b5 53 #define WARN(x, ...)
gsfan 5:78943b3945b5 54 #define ERR(x, ...)
gsfan 5:78943b3945b5 55 #define INFO(x, ...)
gsfan 5:78943b3945b5 56 #endif
gsfan 5:78943b3945b5 57
gsfan 5:78943b3945b5 58
gsfan 8:64184a968e3b 59 /** GSwifi class
gsfan 8:64184a968e3b 60 */
gsfan 5:78943b3945b5 61 class GSwifi
gsfan 5:78943b3945b5 62 {
gsfan 5:78943b3945b5 63
gsfan 5:78943b3945b5 64 public:
gsfan 5:78943b3945b5 65
gsfan 8:64184a968e3b 66 /** Wi-Fi mode
gsfan 8:64184a968e3b 67 */
gsfan 8:64184a968e3b 68 enum WiFiMode {
gsfan 8:64184a968e3b 69 WM_INFRASTRUCTURE,
gsfan 8:64184a968e3b 70 WM_ADHOCK,
gsfan 8:64184a968e3b 71 WM_LIMITEDAP,
gsfan 8:64184a968e3b 72 };
gsfan 8:64184a968e3b 73
gsfan 8:64184a968e3b 74 /** Wi-Fi security
gsfan 8:64184a968e3b 75 */
gsfan 5:78943b3945b5 76 enum Security {
gsfan 5:78943b3945b5 77 SEC_AUTO = 0,
gsfan 5:78943b3945b5 78 SEC_NONE = 0,
gsfan 5:78943b3945b5 79 SEC_OPEN = 1,
gsfan 5:78943b3945b5 80 SEC_WEP = 2,
gsfan 5:78943b3945b5 81 SEC_WPA_PSK = 4,
gsfan 5:78943b3945b5 82 SEC_WPA2_PSK = 8,
gsfan 5:78943b3945b5 83 SEC_WPA_ENT = 16,
gsfan 5:78943b3945b5 84 SEC_WPA2_ENT = 32,
gsfan 5:78943b3945b5 85 SEC_WPS_BUTTON = 64,
gsfan 8:64184a968e3b 86 SEC_WPS_PIN,
gsfan 5:78943b3945b5 87 };
gsfan 5:78943b3945b5 88
gsfan 8:64184a968e3b 89 /** TCP/IP protocol
gsfan 8:64184a968e3b 90 */
gsfan 5:78943b3945b5 91 enum Protocol {
gsfan 5:78943b3945b5 92 PROTO_UDP = 0,
gsfan 5:78943b3945b5 93 PROTO_TCP = 1,
gsfan 5:78943b3945b5 94 PROTO_HTTPGET,
gsfan 5:78943b3945b5 95 PROTO_HTTPPOST,
gsfan 5:78943b3945b5 96 PROTO_HTTPD,
gsfan 5:78943b3945b5 97 };
gsfan 5:78943b3945b5 98
gsfan 8:64184a968e3b 99 /** Client/Server
gsfan 8:64184a968e3b 100 */
gsfan 8:64184a968e3b 101 enum Type {
gsfan 8:64184a968e3b 102 TYPE_CLIENT = 0,
gsfan 8:64184a968e3b 103 TYPE_SERVER = 1,
gsfan 8:64184a968e3b 104 };
gsfan 8:64184a968e3b 105
gsfan 5:78943b3945b5 106 enum Response {
gsfan 5:78943b3945b5 107 RES_NULL,
gsfan 5:78943b3945b5 108 RES_CONNECT,
gsfan 13:189e04ac70bd 109 RES_WPAPSK,
gsfan 5:78943b3945b5 110 RES_WPS,
gsfan 5:78943b3945b5 111 RES_MACADDRESS,
gsfan 5:78943b3945b5 112 RES_DHCP,
gsfan 5:78943b3945b5 113 RES_DNSLOOKUP,
gsfan 5:78943b3945b5 114 RES_HTTP,
gsfan 5:78943b3945b5 115 RES_RSSI,
gsfan 5:78943b3945b5 116 RES_TIME,
gsfan 8:64184a968e3b 117 RES_STATUS,
gsfan 5:78943b3945b5 118 };
gsfan 5:78943b3945b5 119
gsfan 5:78943b3945b5 120 enum Mode {
gsfan 5:78943b3945b5 121 MODE_COMMAND,
gsfan 8:64184a968e3b 122 MODE_CMDRESP,
gsfan 8:64184a968e3b 123 MODE_ESCAPE,
gsfan 5:78943b3945b5 124 MODE_DATA_RX,
gsfan 8:64184a968e3b 125 MODE_DATA_RX_BULK,
gsfan 5:78943b3945b5 126 MODE_DATA_RXUDP,
gsfan 8:64184a968e3b 127 MODE_DATA_RXUDP_BULK,
gsfan 5:78943b3945b5 128 MODE_DATA_RXHTTP,
gsfan 8:64184a968e3b 129 MODE_DATA_RAW,
gsfan 5:78943b3945b5 130 };
gsfan 5:78943b3945b5 131
gsfan 5:78943b3945b5 132 enum Status {
gsfan 8:64184a968e3b 133 STAT_NONE,
gsfan 5:78943b3945b5 134 STAT_READY,
gsfan 5:78943b3945b5 135 STAT_STANDBY,
gsfan 5:78943b3945b5 136 STAT_WAKEUP,
gsfan 5:78943b3945b5 137 STAT_DEEPSLEEP,
gsfan 5:78943b3945b5 138 };
gsfan 5:78943b3945b5 139
gsfan 8:64184a968e3b 140 // ----- GSwifi.cpp -----
gsfan 8:64184a968e3b 141 /** Constructor
gsfan 8:64184a968e3b 142 * \param tx mbed pin to use for tx line of Serial interface
gsfan 8:64184a968e3b 143 * \param rx mbed pin to use for rx line of Serial interface
gsfan 8:64184a968e3b 144 * \param cts mbed pin to use for cts line of Serial interface
gsfan 8:64184a968e3b 145 * \param rts mbed pin to use for rts line of Serial interface
gsfan 8:64184a968e3b 146 * \param reset reset pin of the wifi module
gsfan 12:057089026a20 147 * \param alarm alarm pin of the wifi module (default NULL)
gsfan 12:057089026a20 148 * \param baud baud rate of Serial interface (default 9600)
gsfan 8:64184a968e3b 149 */
gsfan 12:057089026a20 150 GSwifi (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 9600);
gsfan 5:78943b3945b5 151
gsfan 8:64184a968e3b 152 /** Connect the wifi module to the ssid contained in the constructor.
gsfan 8:64184a968e3b 153 * @param sec Security type (NONE, WEP_128 or WPA)
gsfan 8:64184a968e3b 154 * @param ssid ssid of the network
gsfan 8:64184a968e3b 155 * @param phrase WEP or WPA key
gsfan 8:64184a968e3b 156 * @return 0 if connected, -1 otherwise
gsfan 8:64184a968e3b 157 */
gsfan 11:71d67fea5ace 158 int join ();
gsfan 5:78943b3945b5 159
gsfan 8:64184a968e3b 160 /** Connect the wifi module to the adhock in the constructor.
gsfan 8:64184a968e3b 161 * @param sec Security type (NONE, WEP_128 or WPA)
gsfan 8:64184a968e3b 162 * @param ssid ssid of the network
gsfan 8:64184a968e3b 163 * @param phrase WEP or WPA key
gsfan 8:64184a968e3b 164 * @return 0 if connected, -1 otherwise
gsfan 8:64184a968e3b 165 */
gsfan 11:71d67fea5ace 166 int adhock ();
gsfan 5:78943b3945b5 167
gsfan 8:64184a968e3b 168 /** Connect the wifi module to the limited AP in the constructor.
gsfan 8:64184a968e3b 169 * @param sec Security type (NONE, WEP_128 or WPA)
gsfan 8:64184a968e3b 170 * @param ssid ssid of the network
gsfan 8:64184a968e3b 171 * @param phrase WEP or WPA key
gsfan 8:64184a968e3b 172 * @return 0 if connected, -1 otherwise
gsfan 8:64184a968e3b 173 */
gsfan 11:71d67fea5ace 174 int limitedap ();
gsfan 5:78943b3945b5 175
gsfan 8:64184a968e3b 176 /** Disconnect the wifi module from the access point
gsfan 8:64184a968e3b 177 * @returns 0 if successful
gsfan 8:64184a968e3b 178 */
gsfan 12:057089026a20 179 int dissociate ();
gsfan 12:057089026a20 180 int disconnect () {
gsfan 12:057089026a20 181 return dissociate();
gsfan 12:057089026a20 182 }
gsfan 5:78943b3945b5 183
gsfan 12:057089026a20 184 /** Check if a Wi-Fi link is active
gsfan 8:64184a968e3b 185 * @returns true if successful
gsfan 8:64184a968e3b 186 */
gsfan 12:057089026a20 187 bool isAssociated();
gsfan 5:78943b3945b5 188
gsfan 12:057089026a20 189 /** polling if not use rtos
gsfan 12:057089026a20 190 */
gsfan 11:71d67fea5ace 191 void poll ();
gsfan 8:64184a968e3b 192
gsfan 12:057089026a20 193 /** get Wi-Fi modue status
gsfan 12:057089026a20 194 * @return Status
gsfan 12:057089026a20 195 */
gsfan 12:057089026a20 196 Status getStatus ();
gsfan 12:057089026a20 197
gsfan 12:057089026a20 198 /** set MAC address
gsfan 12:057089026a20 199 */
gsfan 12:057089026a20 200 int setMacAddress (const char *mac);
gsfan 12:057089026a20 201 /** get MAC address
gsfan 12:057089026a20 202 */
gsfan 12:057089026a20 203 int getMacAddress (char *mac);
gsfan 12:057089026a20 204
gsfan 12:057089026a20 205 /** use DHCP
gsfan 12:057089026a20 206 */
gsfan 11:71d67fea5ace 207 int setAddress (const char *name = NULL);
gsfan 12:057089026a20 208 /** use static IP address
gsfan 12:057089026a20 209 * @param ip my ip address (dhcp start address)
gsfan 12:057089026a20 210 * @param netmask subnet mask
gsfan 12:057089026a20 211 * @param gateway default gateway
gsfan 12:057089026a20 212 * @param dns my host name (default NULL)
gsfan 12:057089026a20 213 */
gsfan 11:71d67fea5ace 214 int setAddress (const char *ip, const char *netmask, const char *gateway, const char *name = NULL);
gsfan 12:057089026a20 215 /** get IP address
gsfan 12:057089026a20 216 */
gsfan 11:71d67fea5ace 217 int getAddress (char *ip, char *netmask, char *gateway);
gsfan 12:057089026a20 218
gsfan 12:057089026a20 219 /** set Wi-Fi security parameter
gsfan 12:057089026a20 220 * @param sec Security
gsfan 12:057089026a20 221 * @param ssid SSID
gsfan 12:057089026a20 222 * @param pass pass phrase
gsfan 12:057089026a20 223 */
gsfan 11:71d67fea5ace 224 int setSsid (Security sec, const char *ssid, const char *phrase);
gsfan 5:78943b3945b5 225
gsfan 5:78943b3945b5 226 static GSwifi * getInstance() {
gsfan 5:78943b3945b5 227 return _inst;
gsfan 5:78943b3945b5 228 };
gsfan 5:78943b3945b5 229
gsfan 8:64184a968e3b 230 // ----- GSwifi_util.cpp -----
gsfan 8:64184a968e3b 231 int setRegion (int reg);
gsfan 8:64184a968e3b 232
gsfan 12:057089026a20 233 /** get RSSI
gsfan 8:64184a968e3b 234 * @return RSSI (dBm)
gsfan 8:64184a968e3b 235 */
gsfan 8:64184a968e3b 236 int getRssi ();
gsfan 8:64184a968e3b 237
gsfan 8:64184a968e3b 238 /** power save mode
gsfan 8:64184a968e3b 239 * @param active rx radio 0:switched off, 1:always on
gsfan 8:64184a968e3b 240 * @param save power save 0:disable, 1:enable
gsfan 8:64184a968e3b 241 */
gsfan 8:64184a968e3b 242 int powerSave (int active, int save);
gsfan 8:64184a968e3b 243
gsfan 8:64184a968e3b 244 /** RF power
gsfan 8:64184a968e3b 245 * @param power 0(high)-7(low)
gsfan 8:64184a968e3b 246 */
gsfan 8:64184a968e3b 247 int setRfPower (int power);
gsfan 8:64184a968e3b 248
gsfan 8:64184a968e3b 249 /** set system time
gsfan 8:64184a968e3b 250 * @param time date time (UTC)
gsfan 8:64184a968e3b 251 */
gsfan 8:64184a968e3b 252 int setTime (time_t time);
gsfan 8:64184a968e3b 253
gsfan 8:64184a968e3b 254 /** get system time
gsfan 8:64184a968e3b 255 * @return date time (UTC)
gsfan 8:64184a968e3b 256 */
gsfan 8:64184a968e3b 257 time_t getTime ();
gsfan 8:64184a968e3b 258
gsfan 8:64184a968e3b 259 /** set NTP server
gsfan 8:64184a968e3b 260 * @param host SNTP server
gsfan 8:64184a968e3b 261 * @param sec time sync interval, 0:one time
gsfan 8:64184a968e3b 262 */
gsfan 8:64184a968e3b 263 int ntpdate (char *host, int sec = 0);
gsfan 8:64184a968e3b 264
gsfan 8:64184a968e3b 265 /** GPIO output
gsfan 8:64184a968e3b 266 * @param port 10,11,30,31
gsfan 8:64184a968e3b 267 * @param out 0:set(high), 1:reset(low)
gsfan 8:64184a968e3b 268 */
gsfan 8:64184a968e3b 269 int setGpio (int port, int out);
gsfan 8:64184a968e3b 270
gsfan 8:64184a968e3b 271 /** Web server
gsfan 8:64184a968e3b 272 */
gsfan 8:64184a968e3b 273 int provisioning (char *user, char *pass);
gsfan 8:64184a968e3b 274
gsfan 8:64184a968e3b 275 /** standby mode
gsfan 8:64184a968e3b 276 * @param msec wakeup after
gsfan 8:64184a968e3b 277 * wakeup after msec or alarm1/2
gsfan 8:64184a968e3b 278 * core off, save to RTC ram
gsfan 8:64184a968e3b 279 */
gsfan 8:64184a968e3b 280 int standby (int msec);
gsfan 8:64184a968e3b 281
gsfan 8:64184a968e3b 282 /** deep sleep mode
gsfan 8:64184a968e3b 283 */
gsfan 8:64184a968e3b 284 int deepSleep ();
gsfan 8:64184a968e3b 285
gsfan 8:64184a968e3b 286 /** restore standby or deep sleep
gsfan 8:64184a968e3b 287 */
gsfan 8:64184a968e3b 288 int wakeup ();
gsfan 8:64184a968e3b 289
gsfan 8:64184a968e3b 290 // ----- GSwifi_sock.cpp -----
gsfan 8:64184a968e3b 291 /** Resolv hostname
gsfan 8:64184a968e3b 292 * @param name hostname
gsfan 8:64184a968e3b 293 * @param ip resolved ip address
gsfan 8:64184a968e3b 294 */
gsfan 8:64184a968e3b 295 int getHostByName(const char * host, char * ip);
gsfan 8:64184a968e3b 296
gsfan 8:64184a968e3b 297 /** TCP/UDP client
gsfan 8:64184a968e3b 298 * @return CID, -1:failure
gsfan 8:64184a968e3b 299 */
gsfan 12:057089026a20 300 int open (Protocol proto, const char *ip, int port, int src = 0, void(*func)(int) = NULL);
gsfan 8:64184a968e3b 301
gsfan 8:64184a968e3b 302 /** TCP/UDP server
gsfan 8:64184a968e3b 303 * @return CID, -1:failure
gsfan 8:64184a968e3b 304 */
gsfan 12:057089026a20 305 int listen (Protocol proto, int port, void(*func)(int) = NULL);
gsfan 8:64184a968e3b 306
gsfan 8:64184a968e3b 307 /** close client/server
gsfan 8:64184a968e3b 308 */
gsfan 8:64184a968e3b 309 int close (int cid);
gsfan 8:64184a968e3b 310
gsfan 8:64184a968e3b 311 /** send data tcp(s/c), udp(c)
gsfan 8:64184a968e3b 312 */
gsfan 8:64184a968e3b 313 int send (int cid, const char *buf, int len);
gsfan 8:64184a968e3b 314
gsfan 12:057089026a20 315 /** send data udp(s)
gsfan 8:64184a968e3b 316 */
gsfan 8:64184a968e3b 317 int sendto (int cid, const char *buf, int len, const char *ip, int port);
gsfan 8:64184a968e3b 318
gsfan 8:64184a968e3b 319 /** recv data tcp(s/c), udp(c)
gsfan 8:64184a968e3b 320 * @return length
gsfan 8:64184a968e3b 321 */
gsfan 8:64184a968e3b 322 int recv (int cid, char *buf, int len);
gsfan 8:64184a968e3b 323
gsfan 8:64184a968e3b 324 /** recv data udp(s)
gsfan 8:64184a968e3b 325 * @return length
gsfan 8:64184a968e3b 326 */
gsfan 8:64184a968e3b 327 int recvfrom (int cid, char *buf, int len, char *ip, int *port);
gsfan 8:64184a968e3b 328
gsfan 8:64184a968e3b 329 /** readable recv data
gsfan 8:64184a968e3b 330 */
gsfan 8:64184a968e3b 331 int readable (int cid);
gsfan 8:64184a968e3b 332
gsfan 8:64184a968e3b 333 /** tcp/udp connected
gsfan 8:64184a968e3b 334 */
gsfan 8:64184a968e3b 335 bool isConnected (int cid);
gsfan 8:64184a968e3b 336
gsfan 8:64184a968e3b 337 int accept (int cid);
gsfan 8:64184a968e3b 338 int getRemote(int cid, char **ip, int *port);
gsfan 8:64184a968e3b 339
gsfan 8:64184a968e3b 340 // ----- GSwifi_http.cpp -----
gsfan 12:057089026a20 341 /** http request (GET method)
gsfan 12:057089026a20 342 */
gsfan 12:057089026a20 343 int httpGet (const char *host, int port, const char *uri, bool ssl = false, const char *user = NULL, const char *pwd = NULL, void(*func)(int) = NULL);
gsfan 12:057089026a20 344 int httpGet (const char *host, int port, const char *uri, void(*func)(int) = NULL) {
gsfan 12:057089026a20 345 return httpGet(host, port, uri, false, NULL, NULL, func);
gsfan 12:057089026a20 346 }
gsfan 12:057089026a20 347 /** http request (POST method)
gsfan 12:057089026a20 348 */
gsfan 12:057089026a20 349 int httpPost (const char *host, int port, const char *uri, const char *body, bool ssl = false, const char *user = NULL, const char *pwd = NULL, void(*func)(int) = NULL);
gsfan 12:057089026a20 350 int httpPost (const char *host, int port, const char *uri, const char *body, void(*func)(int) = NULL) {
gsfan 12:057089026a20 351 return httpPost(host, port, uri, body, false, NULL, NULL, func);
gsfan 12:057089026a20 352 }
gsfan 12:057089026a20 353
gsfan 8:64184a968e3b 354 int base64encode (const char *input, int length, char *output, int len);
gsfan 8:64184a968e3b 355 int urlencode (const char *str, char *buf, int len);
gsfan 8:64184a968e3b 356 int urldecode (const char *str, char *buf, int len);
gsfan 8:64184a968e3b 357
gsfan 12:057089026a20 358 #ifdef CFG_ENABLE_HTTPD
gsfan 12:057089026a20 359 // ----- GSwifi_httpd.cpp -----
gsfan 12:057089026a20 360 /** start http server
gsfan 12:057089026a20 361 * @param port
gsfan 12:057089026a20 362 */
gsfan 12:057089026a20 363 int httpd (int port = 80);
gsfan 12:057089026a20 364
gsfan 12:057089026a20 365 // ----- GSwifi_httpd_util.cpp -----
gsfan 12:057089026a20 366 /** attach uri to dirctory handler
gsfan 12:057089026a20 367 */
gsfan 12:057089026a20 368 void httpdError (int cid, int err);
gsfan 12:057089026a20 369 /** attach uri to dirctory handler
gsfan 12:057089026a20 370 */
gsfan 12:057089026a20 371 int httpdAttach (const char *uri, const char *dir);
gsfan 12:057089026a20 372 /** attach uri to cgi handler
gsfan 12:057089026a20 373 */
gsfan 12:057089026a20 374 int httpdAttach (const char *uri, void (*funcCgi)(int));
gsfan 12:057089026a20 375 #endif
gsfan 12:057089026a20 376
gsfan 12:057089026a20 377 #ifdef CFG_ENABLE_WEBSOCKET
gsfan 12:057089026a20 378 // ----- GSwifi_httpd_ws.cpp -----
gsfan 12:057089026a20 379 /** websocket request (Upgrade method)
gsfan 12:057089026a20 380 * @return CID, -1:failure
gsfan 12:057089026a20 381 */
gsfan 12:057089026a20 382 int wsOpen (const char *host, int port, const char *uri, const char *user = NULL, const char *pwd = NULL);
gsfan 12:057089026a20 383 /** send websocket data
gsfan 12:057089026a20 384 */
gsfan 12:057089026a20 385 int wsSend (int cid, const char *buf, int len, const char *mask = NULL);
gsfan 12:057089026a20 386 #endif
gsfan 12:057089026a20 387
gsfan 12:057089026a20 388 #ifdef CFG_ENABLE_SMTP
gsfan 12:057089026a20 389 // ----- GSwifi_smtp.cpp -----
gsfan 12:057089026a20 390 /** send mail (smtp)
gsfan 12:057089026a20 391 * @param host SMTP server
gsfan 12:057089026a20 392 * @param port SMTP port (25 or 587 or etc.)
gsfan 12:057089026a20 393 * @param to To address
gsfan 12:057089026a20 394 * @param from From address
gsfan 12:057089026a20 395 * @param subject Subject
gsfan 12:057089026a20 396 * @param mesg Message
gsfan 12:057089026a20 397 * @param user username (SMTP Auth)
gsfan 12:057089026a20 398 * @param pwd password (SMTP Auth)
gsfan 12:057089026a20 399 * @retval 0 success
gsfan 12:057089026a20 400 * @retval -1 failure
gsfan 12:057089026a20 401 */
gsfan 12:057089026a20 402 int mail (const char *host, int port, const char *to, const char *from, const char *subject, const char *mesg, const char *user = NULL, const char *pwd = NULL);
gsfan 12:057089026a20 403 #endif
gsfan 12:057089026a20 404
gsfan 8:64184a968e3b 405 // ----- GSwifi_msg.cpp -----
gsfan 8:64184a968e3b 406
gsfan 8:64184a968e3b 407 // ----- GSwifi_at.cpp -----
gsfan 8:64184a968e3b 408 /** Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
gsfan 8:64184a968e3b 409 * @param cmd string to be sent
gsfan 8:64184a968e3b 410 * @param res need response
gsfan 8:64184a968e3b 411 * @param timeout
gsfan 8:64184a968e3b 412 * @returns 0 if successful
gsfan 8:64184a968e3b 413 */
gsfan 8:64184a968e3b 414 int sendCommand(const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
gsfan 8:64184a968e3b 415
gsfan 8:64184a968e3b 416 /** Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
gsfan 8:64184a968e3b 417 * @param data string to be sent
gsfan 8:64184a968e3b 418 * @param res need response
gsfan 8:64184a968e3b 419 * @param timeout
gsfan 8:64184a968e3b 420 * @param cmd
gsfan 8:64184a968e3b 421 * @returns 0 if successful
gsfan 8:64184a968e3b 422 */
gsfan 11:71d67fea5ace 423 int sendData(const char * data, int len, int timeout = CFG_TIMEOUT, const char * cmd = NULL);
gsfan 8:64184a968e3b 424
gsfan 5:78943b3945b5 425
gsfan 5:78943b3945b5 426 protected:
gsfan 5:78943b3945b5 427
gsfan 5:78943b3945b5 428 static GSwifi * _inst;
gsfan 11:71d67fea5ace 429 #ifdef CFG_ENABLE_RTOS
gsfan 8:64184a968e3b 430 Thread *_threadPoll;
gsfan 8:64184a968e3b 431 Mutex _mutexUart;
gsfan 8:64184a968e3b 432 #endif
gsfan 5:78943b3945b5 433
gsfan 8:64184a968e3b 434 // Serial _gs;
gsfan 8:64184a968e3b 435 RawSerial _gs;
gsfan 8:64184a968e3b 436 int _baud;
gsfan 8:64184a968e3b 437 DigitalIn *_cts;
gsfan 8:64184a968e3b 438 DigitalOut *_rts;
gsfan 8:64184a968e3b 439 int _flow;
gsfan 8:64184a968e3b 440 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC4088)
gsfan 8:64184a968e3b 441 LPC_UART1_TypeDef *_uart;
gsfan 8:64184a968e3b 442 #elif defined(TARGET_LPC11U24)
gsfan 8:64184a968e3b 443 LPC_USART_Type *_uart;
gsfan 12:057089026a20 444 #elif defined(TARGET_LPC11XX)
gsfan 12:057089026a20 445 LPC_UART_TypeDef *_uart;
gsfan 14:1e89a3f0282d 446 #elif defined(TARGET_LPC8XX)
gsfan 14:1e89a3f0282d 447 LPC_UART_TypeDef *_uart;
gsfan 8:64184a968e3b 448 #endif
gsfan 8:64184a968e3b 449 DigitalInOut _reset;
gsfan 8:64184a968e3b 450 DigitalInOut *_alarm;
gsfan 5:78943b3945b5 451
gsfan 8:64184a968e3b 452 struct STATE {
gsfan 8:64184a968e3b 453 WiFiMode wm;
gsfan 8:64184a968e3b 454 Security sec;
gsfan 8:64184a968e3b 455 char ssid[35];
gsfan 8:64184a968e3b 456 char pass[66];
gsfan 8:64184a968e3b 457 char ip[16];
gsfan 8:64184a968e3b 458 char netmask[16];
gsfan 8:64184a968e3b 459 char gateway[16];
gsfan 8:64184a968e3b 460 char nameserver[16];
gsfan 8:64184a968e3b 461 char mac[18];
gsfan 8:64184a968e3b 462 char resolv[16];
gsfan 8:64184a968e3b 463 char name[32];
gsfan 8:64184a968e3b 464 int rssi;
gsfan 5:78943b3945b5 465 bool dhcp;
gsfan 8:64184a968e3b 466 time_t time;
gsfan 8:64184a968e3b 467
gsfan 11:71d67fea5ace 468 bool initialized;
gsfan 8:64184a968e3b 469 bool associated;
gsfan 8:64184a968e3b 470 volatile Mode mode;
gsfan 8:64184a968e3b 471 volatile Status status;
gsfan 5:78943b3945b5 472 bool escape;
gsfan 8:64184a968e3b 473 volatile bool ok, failure;
gsfan 10:7d8134e7df01 474 volatile Response res;
gsfan 11:71d67fea5ace 475 int cid;
gsfan 5:78943b3945b5 476 int n;
gsfan 8:64184a968e3b 477 CircBuffer<char> *buf;
gsfan 8:64184a968e3b 478 } _state;
gsfan 5:78943b3945b5 479
gsfan 8:64184a968e3b 480 struct CONNECTION {
gsfan 8:64184a968e3b 481 Protocol protocol;
gsfan 8:64184a968e3b 482 Type type;
gsfan 5:78943b3945b5 483 bool connected;
gsfan 5:78943b3945b5 484 char ip[16];
gsfan 5:78943b3945b5 485 int port;
gsfan 5:78943b3945b5 486 CircBuffer<char> *buf;
gsfan 8:64184a968e3b 487 volatile bool received;
gsfan 8:64184a968e3b 488 volatile int parent;
gsfan 8:64184a968e3b 489 volatile bool accept;
gsfan 12:057089026a20 490 void(*func)(int);
gsfan 8:64184a968e3b 491 } _con[16];
gsfan 8:64184a968e3b 492
gsfan 12:057089026a20 493 #ifdef CFG_ENABLE_HTTPD
gsfan 12:057089026a20 494 enum HttpdMode {
gsfan 12:057089026a20 495 HTTPDMODE_REQUEST,
gsfan 12:057089026a20 496 HTTPDMODE_REQUEST_STR,
gsfan 12:057089026a20 497 HTTPDMODE_HEADER,
gsfan 12:057089026a20 498 HTTPDMODE_BODY,
gsfan 12:057089026a20 499 HTTPDMODE_ENTER,
gsfan 12:057089026a20 500 HTTPDMODE_ERROR,
gsfan 12:057089026a20 501 HTTPDMODE_WEBSOCKET,
gsfan 12:057089026a20 502 HTTPDMODE_WEBSOCKET_MASK,
gsfan 12:057089026a20 503 HTTPDMODE_WEBSOCKET_BODY,
gsfan 12:057089026a20 504 HTTPDMODE_WEBSOCKET_ENTER,
gsfan 12:057089026a20 505 };
gsfan 12:057089026a20 506
gsfan 12:057089026a20 507 enum HttpdReq {
gsfan 12:057089026a20 508 REQ_HTTPGET,
gsfan 12:057089026a20 509 REQ_HTTPPOST,
gsfan 12:057089026a20 510 };
gsfan 12:057089026a20 511
gsfan 12:057089026a20 512 struct HTTPD {
gsfan 12:057089026a20 513 HttpdMode mode;
gsfan 12:057089026a20 514 HttpdReq req;
gsfan 12:057089026a20 515 CircBuffer <char>*buf;
gsfan 12:057089026a20 516 char *uri;
gsfan 12:057089026a20 517 char *filename;
gsfan 12:057089026a20 518 char *querystring;
gsfan 12:057089026a20 519 int enter;
gsfan 12:057089026a20 520 int length;
gsfan 12:057089026a20 521 int n;
gsfan 12:057089026a20 522 int keepalive;
gsfan 12:057089026a20 523 #ifdef CFG_ENABLE_WEBSOCKET
gsfan 12:057089026a20 524 int websocket;
gsfan 12:057089026a20 525 char *websocket_key;
gsfan 12:057089026a20 526 int websocket_opcode;
gsfan 12:057089026a20 527 int websocket_flg;
gsfan 12:057089026a20 528 char websocket_mask[4];
gsfan 12:057089026a20 529 int websocket_payload;
gsfan 12:057089026a20 530 #endif
gsfan 12:057089026a20 531 } _httpd[16];
gsfan 12:057089026a20 532
gsfan 12:057089026a20 533 struct HTTPD_HANDLER {
gsfan 12:057089026a20 534 char *uri;
gsfan 12:057089026a20 535 char *dir;
gsfan 12:057089026a20 536 void (*func)(int);
gsfan 12:057089026a20 537 } _httpd_handler[CFG_HTTPD_HANDLER_NUM];
gsfan 12:057089026a20 538
gsfan 12:057089026a20 539 int _httpd_cid;
gsfan 12:057089026a20 540 int _handler_count;
gsfan 12:057089026a20 541 #endif // CFG_ENABLE_HTTPD
gsfan 12:057089026a20 542
gsfan 8:64184a968e3b 543
gsfan 8:64184a968e3b 544 // ----- GSwifi.cpp -----
gsfan 11:71d67fea5ace 545 #ifdef CFG_ENABLE_RTOS
gsfan 8:64184a968e3b 546 static void threadPoll (void const *args);
gsfan 8:64184a968e3b 547 #endif
gsfan 8:64184a968e3b 548
gsfan 12:057089026a20 549 // ----- GSwifi_sock.cpp -----
gsfan 12:057089026a20 550 void initCon (int cid, bool connected);
gsfan 12:057089026a20 551
gsfan 8:64184a968e3b 552 // ----- GSwifi_util.cpp -----
gsfan 8:64184a968e3b 553 int x2i (char c);
gsfan 8:64184a968e3b 554 char i2x (int i);
gsfan 8:64184a968e3b 555 int from_hex (int ch);
gsfan 8:64184a968e3b 556 int to_hex (int code);
gsfan 8:64184a968e3b 557
gsfan 12:057089026a20 558 // ----- GSwifi_http.cpp -----
gsfan 12:057089026a20 559
gsfan 12:057089026a20 560 #ifdef CFG_ENABLE_HTTPD
gsfan 12:057089026a20 561 // ----- GSwifi_httpd.cpp -----
gsfan 12:057089026a20 562 void httpdRecvData (int cid, char c);
gsfan 12:057089026a20 563 int httpdParseRequest (int cid);
gsfan 12:057089026a20 564 void httpdPoll ();
gsfan 12:057089026a20 565 int httpdParseHeader (int cid);
gsfan 12:057089026a20 566 void reqContentLength (int cid, const char *buf);
gsfan 12:057089026a20 567 void reqConnection (int cid, const char *buf);
gsfan 12:057089026a20 568 void reqUpgrade (int cid, const char *buf);
gsfan 12:057089026a20 569 void reqWebSocketVersion (int cid, const char *buf);
gsfan 12:057089026a20 570 void reqWebSocketKey (int cid, const char *buf);
gsfan 12:057089026a20 571
gsfan 12:057089026a20 572 // ----- GSwifi_httpd_util.cpp -----
gsfan 12:057089026a20 573 int httpdFile (int cid, char *dir);
gsfan 12:057089026a20 574 int httpdGetHandler (const char *uri);
gsfan 12:057089026a20 575 char *mimetype (char *file);
gsfan 12:057089026a20 576 int strnicmp (const char *p1, const char *p2, int n);
gsfan 12:057089026a20 577 #endif
gsfan 12:057089026a20 578
gsfan 12:057089026a20 579 #ifdef CFG_ENABLE_WEBSOCKET
gsfan 12:057089026a20 580 // ----- GSwifi_httpd_ws.cpp -----
gsfan 12:057089026a20 581 int wsWait (int cid, int code);
gsfan 12:057089026a20 582 #ifdef CFG_ENABLE_HTTPD
gsfan 12:057089026a20 583 void wsRecvData (int cid, char c);
gsfan 12:057089026a20 584 int wsParseRequest (int cid);
gsfan 12:057089026a20 585 int wsAccept (int cid);
gsfan 12:057089026a20 586 #endif
gsfan 12:057089026a20 587 #endif
gsfan 12:057089026a20 588
gsfan 12:057089026a20 589 #ifdef CFG_ENABLE_SMTP
gsfan 12:057089026a20 590 // ----- GSwifi_smtp.cpp -----
gsfan 12:057089026a20 591 int smtpWait (int cid, int code);
gsfan 12:057089026a20 592 #endif
gsfan 12:057089026a20 593
gsfan 8:64184a968e3b 594 // ----- GSwifi_msg.cpp -----
gsfan 8:64184a968e3b 595 void recvData (char c);
gsfan 8:64184a968e3b 596 int parseMessage ();
gsfan 8:64184a968e3b 597 void msgOk (const char*);
gsfan 8:64184a968e3b 598 void msgError (const char*);
gsfan 8:64184a968e3b 599 void msgConnect (const char*);
gsfan 8:64184a968e3b 600 void msgDisconnect (const char*);
gsfan 8:64184a968e3b 601 void msgDisassociated (const char*);
gsfan 8:64184a968e3b 602 void msgReset (const char*);
gsfan 8:64184a968e3b 603 void msgOutofStandby (const char*);
gsfan 8:64184a968e3b 604 void msgOutofDeepsleep (const char*);
gsfan 8:64184a968e3b 605 void resNormal (const char*);
gsfan 8:64184a968e3b 606 void resConnect (const char*);
gsfan 13:189e04ac70bd 607 void resWpapsk (const char *buf);
gsfan 8:64184a968e3b 608 void resWps (const char*);
gsfan 8:64184a968e3b 609 void resMacAddress (const char*);
gsfan 8:64184a968e3b 610 void resIp (const char*);
gsfan 8:64184a968e3b 611 void resLookup (const char*);
gsfan 8:64184a968e3b 612 void resRssi (const char*);
gsfan 8:64184a968e3b 613 void resTime (const char*);
gsfan 8:64184a968e3b 614 void resChannel (const char*);
gsfan 8:64184a968e3b 615 void resStatus (const char*);
gsfan 12:057089026a20 616 void resHttp (const char *buf);
gsfan 5:78943b3945b5 617
gsfan 8:64184a968e3b 618 // ----- GSwifi_at.cpp -----
gsfan 8:64184a968e3b 619 void clearFlags ();
gsfan 8:64184a968e3b 620 int cmdAT ();
gsfan 8:64184a968e3b 621 int cmdE (bool n);
gsfan 8:64184a968e3b 622 int cmdR (bool n);
gsfan 8:64184a968e3b 623 int cmdNMAC (const char *s = NULL);
gsfan 8:64184a968e3b 624 int cmdWREGDOMAIN (int n = CFG_WREGDOMAIN);
gsfan 8:64184a968e3b 625 int cmdWS ();
gsfan 8:64184a968e3b 626 int cmdWM (int n);
gsfan 8:64184a968e3b 627 int cmdWA (const char *s);
gsfan 8:64184a968e3b 628 int cmdWD ();
gsfan 8:64184a968e3b 629 int cmdWWPS (bool n, const char *p = NULL);
gsfan 8:64184a968e3b 630 int cmdNSTAT ();
gsfan 8:64184a968e3b 631 int cmdWSTATUS ();
gsfan 8:64184a968e3b 632 int cmdWRSSI ();
gsfan 8:64184a968e3b 633 int cmdWAUTH (int n);
gsfan 8:64184a968e3b 634 int cmdWWEP (int n, const char *s);
gsfan 8:64184a968e3b 635 int cmdWPAPSK (const char *s, const char *p);
gsfan 8:64184a968e3b 636 int cmdWRXACTIVE (bool n);
gsfan 8:64184a968e3b 637 int cmdWRXPS (bool n);
gsfan 8:64184a968e3b 638 int cmdWP (int n);
gsfan 8:64184a968e3b 639 int cmdNDHCP (bool n, const char *s = NULL, int m = CFG_TIMEOUT);
gsfan 8:64184a968e3b 640 int cmdDHCPSRVR (bool n);
gsfan 8:64184a968e3b 641 int cmdNSET (const char *s, const char *t, const char *u);
gsfan 8:64184a968e3b 642 int cmdDNS (bool n, const char *s);
gsfan 8:64184a968e3b 643 int cmdDNSLOOKUP (const char *s);
gsfan 8:64184a968e3b 644 int cmdDNSSET (const char *s);
gsfan 8:64184a968e3b 645 int cmdSTORENWCONN ();
gsfan 8:64184a968e3b 646 int cmdRESTORENWCONN ();
gsfan 8:64184a968e3b 647 int cmdBDATA (bool n);
gsfan 8:64184a968e3b 648 int cmdNCTCP (const char *s, int n);
gsfan 8:64184a968e3b 649 int cmdNCUDP (const char *s, int n, int m = 0);
gsfan 8:64184a968e3b 650 int cmdNSTCP (int n);
gsfan 8:64184a968e3b 651 int cmdNSUDP (int n);
gsfan 8:64184a968e3b 652 int cmdNCLOSE (int n);
gsfan 8:64184a968e3b 653 int cmdNCLOSEALL ();
gsfan 8:64184a968e3b 654 int cmdHTTPCONF (int n, const char *s);
gsfan 8:64184a968e3b 655 int cmdHTTPCONFDEL (int n);
gsfan 8:64184a968e3b 656 int cmdHTTPOPEN (const char *s, int n = 80, bool m = false);
gsfan 8:64184a968e3b 657 int cmdHTTPSEND (int n, bool m, const char *s, int t = 0);
gsfan 8:64184a968e3b 658 int cmdHTTPCLOSE (int n);
gsfan 8:64184a968e3b 659 int cmdPSDPSLEEP (int n = 0);
gsfan 8:64184a968e3b 660 int cmdPSSTBY (int n, int m = 0);
gsfan 8:64184a968e3b 661 int cmdWEBPROV (const char *s, const char *p);
gsfan 8:64184a968e3b 662 int cmdSETTIME (const char *s, const char *t);
gsfan 8:64184a968e3b 663 int cmdGETTIME ();
gsfan 8:64184a968e3b 664 int cmdNTIMESYNC (bool n, const char *s, int m = 0);
gsfan 8:64184a968e3b 665 int cmdDGPIO (int n, int m);
gsfan 8:64184a968e3b 666
gsfan 8:64184a968e3b 667 // ----- GSwifi_hal.cpp -----
gsfan 8:64184a968e3b 668 void setReset (bool flg);
gsfan 8:64184a968e3b 669 void setAlarm (bool flg);
gsfan 8:64184a968e3b 670 void isrUart ();
gsfan 8:64184a968e3b 671 int getUart ();
gsfan 8:64184a968e3b 672 void putUart (char c);
gsfan 8:64184a968e3b 673 void setRts (bool flg);
gsfan 8:64184a968e3b 674 int lockUart (int ms);
gsfan 8:64184a968e3b 675 void unlockUart ();
gsfan 8:64184a968e3b 676 void initUart (PinName cts, PinName rts, PinName alarm, int baud);
gsfan 8:64184a968e3b 677
gsfan 5:78943b3945b5 678 };
gsfan 5:78943b3945b5 679
samux 1:fb4494783863 680 #endif