GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Fork of GSwifi_old by gs fan

GainSpan Wi-Fi library

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

see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Information

Please change the baud rate in advance.

  • ATB=115200
  • AT&W0

It may be better and sometimes faster.
GSwifi gs(p13, p14, baud);

Heavily modified new library: http://mbed.org/users/gsfan/code/GSwifi

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

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

解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Information

モジュールはあらかじめ次のコマンドでボーレートを変更しておく。

  • ATB=115200
  • AT&W0

場合によってはもっと高速の方がいいかもしれない。クラス宣言時にレート設定をする。
GSwifi gs(p13, p14, baud);

大幅に更新された新しいライブラリ: http://mbed.org/users/gsfan/code/GSwifi

Committer:
gsfan
Date:
Thu Jul 12 08:19:50 2012 +0000
Revision:
1:b127c6c5241d
Parent:
0:2f6062c6d018
Child:
2:c6e0e97901b3
bugfix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsfan 0:2f6062c6d018 1 /**
gsfan 0:2f6062c6d018 2 * Gainspan wi-fi module library for mbed
gsfan 0:2f6062c6d018 3 * Copyright (c) 2012 gsfan
gsfan 0:2f6062c6d018 4 * Released under the MIT License: http://mbed.org/license/mit
gsfan 0:2f6062c6d018 5 */
gsfan 0:2f6062c6d018 6
gsfan 0:2f6062c6d018 7 /** @file
gsfan 0:2f6062c6d018 8 * @brief Gainspan wi-fi module library for mbed
gsfan 0:2f6062c6d018 9 * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc.
gsfan 0:2f6062c6d018 10 * module configuration: ATB=115200
gsfan 0:2f6062c6d018 11 */
gsfan 0:2f6062c6d018 12
gsfan 0:2f6062c6d018 13 #include "mbed.h"
gsfan 0:2f6062c6d018 14 #include "RingBuffer.h"
gsfan 0:2f6062c6d018 15 #include "host.h"
gsfan 0:2f6062c6d018 16 #include "ipaddr.h"
gsfan 0:2f6062c6d018 17
gsfan 0:2f6062c6d018 18 #define GS_BAUD 115200
gsfan 1:b127c6c5241d 19 #define GS_UART_DIRECT
gsfan 0:2f6062c6d018 20
gsfan 0:2f6062c6d018 21 #define GS_BULK
gsfan 0:2f6062c6d018 22
gsfan 0:2f6062c6d018 23 #define GS_TIMEOUT 10000 // ms
gsfan 0:2f6062c6d018 24 #define GS_TIMEOUT2 30000 // ms
gsfan 0:2f6062c6d018 25 #define GS_CMD_SIZE 100
gsfan 0:2f6062c6d018 26 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
gsfan 0:2f6062c6d018 27 #define GS_DATA_SIZE 1500
gsfan 0:2f6062c6d018 28 #elif defined(TARGET_LPC11U24)
gsfan 0:2f6062c6d018 29 #define GS_DATA_SIZE 800
gsfan 0:2f6062c6d018 30 #endif
gsfan 0:2f6062c6d018 31
gsfan 0:2f6062c6d018 32 /**
gsfan 0:2f6062c6d018 33 * Wi-Fi security
gsfan 0:2f6062c6d018 34 */
gsfan 0:2f6062c6d018 35 enum GSSECURITY {
gsfan 0:2f6062c6d018 36 GSSEC_AUTO = 0,
gsfan 0:2f6062c6d018 37 GSSEC_NONE = 0,
gsfan 0:2f6062c6d018 38 GSSEC_OPEN = 1,
gsfan 0:2f6062c6d018 39 GSSEC_WEP = 2,
gsfan 0:2f6062c6d018 40 GSSEC_WPA_PSK = 4,
gsfan 0:2f6062c6d018 41 GSSEC_WPA2_PSK = 8,
gsfan 0:2f6062c6d018 42 GSSEC_WPA_ENT = 16,
gsfan 0:2f6062c6d018 43 GSSEC_WPA2_ENT = 32,
gsfan 0:2f6062c6d018 44 GSSEC_WPS_BUTTON = 64,
gsfan 0:2f6062c6d018 45 };
gsfan 0:2f6062c6d018 46
gsfan 0:2f6062c6d018 47 /**
gsfan 0:2f6062c6d018 48 * TCP/IP protocol
gsfan 0:2f6062c6d018 49 */
gsfan 0:2f6062c6d018 50 enum GSPROTOCOL {
gsfan 0:2f6062c6d018 51 GSPROT_UDP = 0,
gsfan 0:2f6062c6d018 52 GSPROT_TCP = 1,
gsfan 0:2f6062c6d018 53 GSPROT_HTTPGET,
gsfan 0:2f6062c6d018 54 GSPROT_HTTPPOST,
gsfan 0:2f6062c6d018 55 };
gsfan 0:2f6062c6d018 56
gsfan 0:2f6062c6d018 57 /**
gsfan 0:2f6062c6d018 58 * Client/Server
gsfan 0:2f6062c6d018 59 */
gsfan 0:2f6062c6d018 60 enum GSTYPE {
gsfan 0:2f6062c6d018 61 GSTYPE_CLIENT = 0,
gsfan 0:2f6062c6d018 62 GSTYPE_SERVER = 1,
gsfan 0:2f6062c6d018 63 };
gsfan 0:2f6062c6d018 64
gsfan 0:2f6062c6d018 65 enum GSRESPONCE {
gsfan 0:2f6062c6d018 66 GSRES_NORMAL,
gsfan 0:2f6062c6d018 67 GSRES_CONNECT,
gsfan 0:2f6062c6d018 68 GSRES_WPS,
gsfan 0:2f6062c6d018 69 GSRES_MACADDRESS,
gsfan 0:2f6062c6d018 70 GSRES_DHCP,
gsfan 0:2f6062c6d018 71 GSRES_DNSLOOKUP,
gsfan 0:2f6062c6d018 72 GSRES_HTTP,
gsfan 1:b127c6c5241d 73 GSRES_RSSI,
gsfan 0:2f6062c6d018 74 };
gsfan 0:2f6062c6d018 75
gsfan 0:2f6062c6d018 76 enum GSMODE {
gsfan 0:2f6062c6d018 77 GSMODE_COMMAND,
gsfan 0:2f6062c6d018 78 GSMODE_DATA_RX,
gsfan 0:2f6062c6d018 79 GSMODE_DATA_RXUDP,
gsfan 0:2f6062c6d018 80 GSMODE_DATA_RX_BULK,
gsfan 0:2f6062c6d018 81 GSMODE_DATA_RXUDP_BULK,
gsfan 0:2f6062c6d018 82 GSMODE_DATA_RXHTTP,
gsfan 0:2f6062c6d018 83 };
gsfan 0:2f6062c6d018 84
gsfan 0:2f6062c6d018 85 enum GSSTATUS {
gsfan 0:2f6062c6d018 86 GSSTAT_READY,
gsfan 0:2f6062c6d018 87 GSSTAT_STANDBY,
gsfan 0:2f6062c6d018 88 GSSTAT_WAKEUP,
gsfan 0:2f6062c6d018 89 GSSTAT_DEEPSLEEP,
gsfan 0:2f6062c6d018 90 };
gsfan 0:2f6062c6d018 91
gsfan 0:2f6062c6d018 92 /**
gsfan 0:2f6062c6d018 93 * data receive callback function
gsfan 0:2f6062c6d018 94 */
gsfan 1:b127c6c5241d 95 typedef void (*onGsReceiveFunc)(int cid, int len);
gsfan 0:2f6062c6d018 96
gsfan 0:2f6062c6d018 97 struct GS_Socket {
gsfan 0:2f6062c6d018 98 GSTYPE type;
gsfan 0:2f6062c6d018 99 GSPROTOCOL protocol;
gsfan 0:2f6062c6d018 100 bool connect;
gsfan 0:2f6062c6d018 101 Host host;
gsfan 0:2f6062c6d018 102 RingBuffer *data;
gsfan 1:b127c6c5241d 103 int lcid;
gsfan 0:2f6062c6d018 104 int received;
gsfan 0:2f6062c6d018 105 onGsReceiveFunc onGsReceive;
gsfan 0:2f6062c6d018 106 };
gsfan 0:2f6062c6d018 107
gsfan 0:2f6062c6d018 108 /**
gsfan 0:2f6062c6d018 109 * GSwifi class
gsfan 0:2f6062c6d018 110 */
gsfan 0:2f6062c6d018 111 class GSwifi {
gsfan 0:2f6062c6d018 112 public:
gsfan 0:2f6062c6d018 113 /**
gsfan 0:2f6062c6d018 114 * default constructor
gsfan 0:2f6062c6d018 115 */
gsfan 0:2f6062c6d018 116 GSwifi (PinName p_tx, PinName p_rx);
gsfan 0:2f6062c6d018 117 /**
gsfan 0:2f6062c6d018 118 * Default constructor (with hardware fllow controll)
gsfan 0:2f6062c6d018 119 */
gsfan 0:2f6062c6d018 120 GSwifi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts);
gsfan 0:2f6062c6d018 121 /**
gsfan 0:2f6062c6d018 122 * send command
gsfan 0:2f6062c6d018 123 */
gsfan 0:2f6062c6d018 124
gsfan 0:2f6062c6d018 125 int command (char *cmd, GSRESPONCE res, int timeout = GS_TIMEOUT);
gsfan 0:2f6062c6d018 126 /**
gsfan 0:2f6062c6d018 127 * recv responce
gsfan 0:2f6062c6d018 128 */
gsfan 0:2f6062c6d018 129 int cmdResponse (GSRESPONCE res, int ms);
gsfan 0:2f6062c6d018 130 /**
gsfan 0:2f6062c6d018 131 * associate infrastructure
gsfan 0:2f6062c6d018 132 * @param sec GSSEC_OPEN, GSSEC_WEP, GSSEC_WPA_PSK, GSSEC_WPA2_PSK, GSSEC_WPS_BUTTON
gsfan 0:2f6062c6d018 133 * @param ssid
gsfan 0:2f6062c6d018 134 * @param pass
gsfan 0:2f6062c6d018 135 */
gsfan 0:2f6062c6d018 136
gsfan 0:2f6062c6d018 137 int connect (GSSECURITY sec, char *ssid, char *pass, int dhcp = 1);
gsfan 0:2f6062c6d018 138 /**
gsfan 0:2f6062c6d018 139 * adhock
gsfan 0:2f6062c6d018 140 * @param sec GSSEC_OPEN or GSSEC_WEP
gsfan 0:2f6062c6d018 141 * @param ssid
gsfan 0:2f6062c6d018 142 * @param pass
gsfan 0:2f6062c6d018 143 */
gsfan 0:2f6062c6d018 144 int adhock (GSSECURITY sec, char *ssid, char *pass, IpAddr ipaddr, IpAddr netmask);
gsfan 0:2f6062c6d018 145 /**
gsfan 0:2f6062c6d018 146 * limited AP
gsfan 0:2f6062c6d018 147 * @param sec GSSEC_OPEN or GSSEC_WEP
gsfan 0:2f6062c6d018 148 * @param ssid
gsfan 0:2f6062c6d018 149 * @param pass 10 or 26 hexadecimal digits
gsfan 0:2f6062c6d018 150 * firmware: s2w-secureweb, s2w-web, s2w-wpsweb
gsfan 0:2f6062c6d018 151 */
gsfan 0:2f6062c6d018 152 int limitedap (GSSECURITY sec, char *ssid, char *pass, IpAddr ipaddr, IpAddr netmask);
gsfan 0:2f6062c6d018 153 /**
gsfan 0:2f6062c6d018 154 * unassociate
gsfan 0:2f6062c6d018 155 */
gsfan 0:2f6062c6d018 156 int disconnect ();
gsfan 0:2f6062c6d018 157
gsfan 0:2f6062c6d018 158 /**
gsfan 0:2f6062c6d018 159 * use DHCP
gsfan 0:2f6062c6d018 160 */
gsfan 0:2f6062c6d018 161 int setAddress ();
gsfan 0:2f6062c6d018 162 /**
gsfan 0:2f6062c6d018 163 * use static ip address
gsfan 0:2f6062c6d018 164 */
gsfan 0:2f6062c6d018 165 int setAddress (IpAddr ipaddr, IpAddr netmask, IpAddr gateway, IpAddr nameserver);
gsfan 0:2f6062c6d018 166 /**
gsfan 1:b127c6c5241d 167 * get ip address
gsfan 1:b127c6c5241d 168 */
gsfan 1:b127c6c5241d 169 int getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver);
gsfan 1:b127c6c5241d 170 /**
gsfan 0:2f6062c6d018 171 * resolv hostname
gsfan 0:2f6062c6d018 172 */
gsfan 0:2f6062c6d018 173 int getHostByName (const char* name, IpAddr &addr);
gsfan 0:2f6062c6d018 174 /**
gsfan 0:2f6062c6d018 175 * resolv hostname
gsfan 0:2f6062c6d018 176 */
gsfan 0:2f6062c6d018 177 int getHostByName (Host &host);
gsfan 0:2f6062c6d018 178 /**
gsfan 0:2f6062c6d018 179 * RF power
gsfan 0:2f6062c6d018 180 * @param power 0(high)-7(low)
gsfan 0:2f6062c6d018 181 */
gsfan 0:2f6062c6d018 182 int setRFPower (int power);
gsfan 0:2f6062c6d018 183 /**
gsfan 0:2f6062c6d018 184 * power save mode
gsfan 0:2f6062c6d018 185 */
gsfan 0:2f6062c6d018 186 int powerSave (int beacon = 10, int association = 1);
gsfan 0:2f6062c6d018 187 /**
gsfan 0:2f6062c6d018 188 * standby mode
gsfan 0:2f6062c6d018 189 * @param msec wakeup after
gsfan 0:2f6062c6d018 190 * wakeup after msec or alarm1/2
gsfan 0:2f6062c6d018 191 * core off, save to RTC ram
gsfan 0:2f6062c6d018 192 */
gsfan 0:2f6062c6d018 193 int standby (int msec);
gsfan 0:2f6062c6d018 194 /**
gsfan 0:2f6062c6d018 195 * restore standby
gsfan 0:2f6062c6d018 196 */
gsfan 0:2f6062c6d018 197 int wakeup ();
gsfan 0:2f6062c6d018 198 /**
gsfan 0:2f6062c6d018 199 * deep sleep mode
gsfan 0:2f6062c6d018 200 */
gsfan 0:2f6062c6d018 201 int deepSleep ();
gsfan 0:2f6062c6d018 202 /**
gsfan 0:2f6062c6d018 203 * wifi connected
gsfan 0:2f6062c6d018 204 */
gsfan 0:2f6062c6d018 205 bool isConnected ();
gsfan 0:2f6062c6d018 206 /**
gsfan 0:2f6062c6d018 207 * status
gsfan 0:2f6062c6d018 208 */
gsfan 0:2f6062c6d018 209 GSSTATUS getStatus ();
gsfan 1:b127c6c5241d 210 /**
gsfan 1:b127c6c5241d 211 * RSSI dBm
gsfan 1:b127c6c5241d 212 */
gsfan 1:b127c6c5241d 213 int getRssi ();
gsfan 0:2f6062c6d018 214
gsfan 0:2f6062c6d018 215 /**
gsfan 0:2f6062c6d018 216 * polling
gsfan 0:2f6062c6d018 217 */
gsfan 0:2f6062c6d018 218 void poll();
gsfan 0:2f6062c6d018 219
gsfan 0:2f6062c6d018 220 /**
gsfan 0:2f6062c6d018 221 * tcp/udp client
gsfan 0:2f6062c6d018 222 */
gsfan 0:2f6062c6d018 223 int open (Host &host, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive = NULL);
gsfan 0:2f6062c6d018 224 /**
gsfan 0:2f6062c6d018 225 * tcp/udp server
gsfan 0:2f6062c6d018 226 */
gsfan 0:2f6062c6d018 227 int listen (int port, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive = NULL);
gsfan 0:2f6062c6d018 228 /**
gsfan 0:2f6062c6d018 229 * close client/server
gsfan 0:2f6062c6d018 230 */
gsfan 0:2f6062c6d018 231 int close (int cid);
gsfan 0:2f6062c6d018 232 /**
gsfan 0:2f6062c6d018 233 * send data tcp(s/c), udp(c)
gsfan 0:2f6062c6d018 234 */
gsfan 0:2f6062c6d018 235 int send (int cid, char *buf, int len);
gsfan 0:2f6062c6d018 236 /**
gsfan 0:2f6062c6d018 237 * send data udp(s)
gsfan 0:2f6062c6d018 238 */
gsfan 0:2f6062c6d018 239 int send (int cid, char *buf, int len, Host &host);
gsfan 0:2f6062c6d018 240 /**
gsfan 0:2f6062c6d018 241 * recv data tcp(s/c), udp(c)
gsfan 0:2f6062c6d018 242 */
gsfan 0:2f6062c6d018 243 int recv (int cid, char *buf, int len);
gsfan 0:2f6062c6d018 244 /**
gsfan 0:2f6062c6d018 245 * recv data udp(s)
gsfan 0:2f6062c6d018 246 */
gsfan 0:2f6062c6d018 247 int recv (int cid, char *buf, int len, Host &host);
gsfan 0:2f6062c6d018 248 /**
gsfan 0:2f6062c6d018 249 * tcp/udp connected
gsfan 0:2f6062c6d018 250 */
gsfan 0:2f6062c6d018 251 bool isConnected (int cid);
gsfan 0:2f6062c6d018 252
gsfan 0:2f6062c6d018 253 /**
gsfan 0:2f6062c6d018 254 * http request
gsfan 0:2f6062c6d018 255 */
gsfan 0:2f6062c6d018 256 int httpGet (Host &host, char *uri, int ssl = 0, onGsReceiveFunc ponGsReceive = NULL);
gsfan 0:2f6062c6d018 257 /**
gsfan 0:2f6062c6d018 258 * certificate
gsfan 0:2f6062c6d018 259 */
gsfan 0:2f6062c6d018 260 int certAdd (char *name, char *cert, int len);
gsfan 0:2f6062c6d018 261
gsfan 0:2f6062c6d018 262 void test ();
gsfan 0:2f6062c6d018 263 int getc();
gsfan 0:2f6062c6d018 264 void putc(char c);
gsfan 0:2f6062c6d018 265 int readable();
gsfan 0:2f6062c6d018 266
gsfan 0:2f6062c6d018 267 protected:
gsfan 0:2f6062c6d018 268 int x2i (char c);
gsfan 0:2f6062c6d018 269 char i2x (int i);
gsfan 0:2f6062c6d018 270 void isr_recv ();
gsfan 1:b127c6c5241d 271 int newSock (int cid, GSTYPE type, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive);
gsfan 0:2f6062c6d018 272
gsfan 0:2f6062c6d018 273 private:
gsfan 0:2f6062c6d018 274 Serial _gs;
gsfan 0:2f6062c6d018 275 bool _rts;
gsfan 0:2f6062c6d018 276 volatile bool _connect;
gsfan 0:2f6062c6d018 277 volatile GSSTATUS _status;
gsfan 0:2f6062c6d018 278 volatile int _gs_ok, _gs_failure, _gs_enter;
gsfan 0:2f6062c6d018 279 GSMODE _gs_mode;
gsfan 0:2f6062c6d018 280 int _escape;
gsfan 1:b127c6c5241d 281 int _cid, _rssi;
gsfan 0:2f6062c6d018 282 IpAddr _ipaddr, _netmask, _gateway, _nameserver, _resolv;
gsfan 0:2f6062c6d018 283 Host _from, _to;
gsfan 0:2f6062c6d018 284 char mac[6];
gsfan 0:2f6062c6d018 285 RingBuffer _buf_cmd;
gsfan 0:2f6062c6d018 286 struct GS_Socket _gs_sock[16];
gsfan 0:2f6062c6d018 287 };