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 Oct 18 02:21:27 2012 +0000
Revision:
16:aea56cce3bf5
Parent:
15:5febfc399099
Child:
17:6828b084e74b
fix

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 "dbg.h"
gsfan 0:2f6062c6d018 14 #include "mbed.h"
gsfan 0:2f6062c6d018 15 #include "GSwifi.h"
gsfan 15:5febfc399099 16 #include <ctype.h>
gsfan 0:2f6062c6d018 17
gsfan 1:b127c6c5241d 18 #ifdef GS_UART_DIRECT
gsfan 1:b127c6c5241d 19 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
gsfan 1:b127c6c5241d 20 #define _gs_getc() LPC_UART1->RBR
gsfan 1:b127c6c5241d 21 #define _gs_putc(c) while(!(LPC_UART1->LSR & (1<<5))); LPC_UART1->THR = c
gsfan 1:b127c6c5241d 22 #elif defined(TARGET_LPC11U24)
gsfan 11:983f4e832a3e 23 #define _gs_getc() LPC_USART->RBR
gsfan 11:983f4e832a3e 24 #define _gs_putc(c) while(!(LPC_USART->LSR & (1<<5))); LPC_USART->THR = c
gsfan 1:b127c6c5241d 25 #endif
gsfan 1:b127c6c5241d 26 #else
gsfan 1:b127c6c5241d 27 #define _gs_getc() _gs.getc()
gsfan 1:b127c6c5241d 28 #define _gs_putc(c) _gs.putc(c)
gsfan 1:b127c6c5241d 29 #endif
gsfan 0:2f6062c6d018 30
gsfan 10:698c5e96b5b1 31 GSwifi::GSwifi (PinName p_tx, PinName p_rx, int baud) : _gs(p_tx, p_rx), _buf_cmd(GS_CMD_SIZE) {
gsfan 0:2f6062c6d018 32 _connect = false;
gsfan 0:2f6062c6d018 33 _status = GSSTAT_READY;
gsfan 0:2f6062c6d018 34 _escape = 0;
gsfan 0:2f6062c6d018 35 _gs_mode = GSMODE_COMMAND;
gsfan 0:2f6062c6d018 36
gsfan 10:698c5e96b5b1 37 _gs.baud(baud);
gsfan 0:2f6062c6d018 38 _gs.attach(this, &GSwifi::isr_recv, Serial::RxIrq);
gsfan 0:2f6062c6d018 39 _rts = false;
gsfan 0:2f6062c6d018 40 }
gsfan 0:2f6062c6d018 41
gsfan 10:698c5e96b5b1 42 GSwifi::GSwifi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts, int baud) : _gs(p_tx, p_rx), _buf_cmd(GS_CMD_SIZE) {
gsfan 0:2f6062c6d018 43 _connect = false;
gsfan 0:2f6062c6d018 44 _status = GSSTAT_READY;
gsfan 0:2f6062c6d018 45 _escape = 0;
gsfan 0:2f6062c6d018 46 _gs_mode = GSMODE_COMMAND;
gsfan 0:2f6062c6d018 47
gsfan 10:698c5e96b5b1 48 _gs.baud(baud);
gsfan 0:2f6062c6d018 49 _gs.attach(this, &GSwifi::isr_recv, Serial::RxIrq);
gsfan 0:2f6062c6d018 50
gsfan 0:2f6062c6d018 51 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
gsfan 0:2f6062c6d018 52 if (p_cts == p12) { // CTS input (P0_17)
gsfan 0:2f6062c6d018 53 LPC_UART1->MCR |= (1<<7); // CTSEN
gsfan 0:2f6062c6d018 54 LPC_PINCON->PINSEL1 &= ~(3 << 2);
gsfan 0:2f6062c6d018 55 LPC_PINCON->PINSEL1 |= (1 << 2); // UART CTS
gsfan 0:2f6062c6d018 56 }
gsfan 0:2f6062c6d018 57 if (p_rts == P0_22) { // RTS output (P0_22)
gsfan 0:2f6062c6d018 58 LPC_UART1->MCR |= (1<<6); // RTSEN
gsfan 0:2f6062c6d018 59 LPC_PINCON->PINSEL1 &= ~(3 << 12);
gsfan 0:2f6062c6d018 60 LPC_PINCON->PINSEL1 |= (1 << 12); // UART RTS
gsfan 0:2f6062c6d018 61 _rts = true;
gsfan 0:2f6062c6d018 62 } else {
gsfan 0:2f6062c6d018 63 _rts = false;
gsfan 0:2f6062c6d018 64 }
gsfan 0:2f6062c6d018 65 #elif defined(TARGET_LPC11U24)
gsfan 0:2f6062c6d018 66 if (p_cts == p21) { // CTS input (P0_7)
gsfan 16:aea56cce3bf5 67 LPC_USART->MCR |= (1<<7); // CTSEN
gsfan 0:2f6062c6d018 68 LPC_IOCON->PIO0_7 &= ~0x07;
gsfan 0:2f6062c6d018 69 LPC_IOCON->PIO0_7 |= 0x01; // UART CTS
gsfan 0:2f6062c6d018 70 }
gsfan 0:2f6062c6d018 71 if (p_rts == p22) { // RTS output (P0_17)
gsfan 16:aea56cce3bf5 72 LPC_USART->MCR |= (1<<6); // RTSEN
gsfan 0:2f6062c6d018 73 LPC_IOCON->PIO0_17 &= ~0x07;
gsfan 0:2f6062c6d018 74 LPC_IOCON->PIO0_17 |= 0x01; // UART RTS
gsfan 0:2f6062c6d018 75 _rts = true;
gsfan 0:2f6062c6d018 76 } else {
gsfan 0:2f6062c6d018 77 _rts = false;
gsfan 0:2f6062c6d018 78 }
gsfan 0:2f6062c6d018 79 #endif
gsfan 0:2f6062c6d018 80 }
gsfan 0:2f6062c6d018 81
gsfan 0:2f6062c6d018 82 // uart interrupt
gsfan 0:2f6062c6d018 83 void GSwifi::isr_recv () {
gsfan 0:2f6062c6d018 84 static int len, mode;
gsfan 0:2f6062c6d018 85 static char tmp[20];
gsfan 0:2f6062c6d018 86 char flg, dat;
gsfan 0:2f6062c6d018 87
gsfan 0:2f6062c6d018 88 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
gsfan 0:2f6062c6d018 89 flg = LPC_UART1->LSR;
gsfan 0:2f6062c6d018 90 #elif defined(TARGET_LPC11U24)
gsfan 0:2f6062c6d018 91 flg = LPC_USART->LSR;
gsfan 0:2f6062c6d018 92 #endif
gsfan 1:b127c6c5241d 93 dat = _gs_getc();
gsfan 0:2f6062c6d018 94
gsfan 0:2f6062c6d018 95 if (flg & ((1 << 7)|(1 << 3)|(1 << 4))) return;
gsfan 0:2f6062c6d018 96 // DBG("%02x ", dat);
gsfan 0:2f6062c6d018 97
gsfan 0:2f6062c6d018 98 switch (_gs_mode) {
gsfan 0:2f6062c6d018 99 case GSMODE_COMMAND: // command responce
gsfan 0:2f6062c6d018 100 if (_escape) {
gsfan 0:2f6062c6d018 101 // esc
gsfan 0:2f6062c6d018 102 switch (dat) {
gsfan 0:2f6062c6d018 103 case 'O':
gsfan 0:2f6062c6d018 104 DBG("ok\r\n");
gsfan 0:2f6062c6d018 105 _gs_ok = 1;
gsfan 0:2f6062c6d018 106 break;
gsfan 0:2f6062c6d018 107 case 'F':
gsfan 0:2f6062c6d018 108 DBG("failure\r\n");
gsfan 0:2f6062c6d018 109 _gs_failure = 1;
gsfan 0:2f6062c6d018 110 break;
gsfan 0:2f6062c6d018 111 case 'S':
gsfan 0:2f6062c6d018 112 DBG("GSMODE_DATA_RX\r\n");
gsfan 0:2f6062c6d018 113 _gs_mode = GSMODE_DATA_RX;
gsfan 0:2f6062c6d018 114 mode = 0;
gsfan 0:2f6062c6d018 115 break;
gsfan 0:2f6062c6d018 116 case 'u':
gsfan 0:2f6062c6d018 117 DBG("GSMODE_DATA_RXUDP\r\n");
gsfan 0:2f6062c6d018 118 _gs_mode = GSMODE_DATA_RXUDP;
gsfan 0:2f6062c6d018 119 mode = 0;
gsfan 0:2f6062c6d018 120 break;
gsfan 0:2f6062c6d018 121 case 'Z':
gsfan 0:2f6062c6d018 122 case 'H':
gsfan 0:2f6062c6d018 123 DBG("GSMODE_DATA_RX_BULK\r\n");
gsfan 0:2f6062c6d018 124 _gs_mode = GSMODE_DATA_RX_BULK;
gsfan 0:2f6062c6d018 125 mode = 0;
gsfan 0:2f6062c6d018 126 break;
gsfan 0:2f6062c6d018 127 case 'y':
gsfan 0:2f6062c6d018 128 DBG("GSMODE_DATA_RXUDP_BULK\r\n");
gsfan 0:2f6062c6d018 129 _gs_mode = GSMODE_DATA_RXUDP_BULK;
gsfan 0:2f6062c6d018 130 mode = 0;
gsfan 0:2f6062c6d018 131 break;
gsfan 0:2f6062c6d018 132 default:
gsfan 0:2f6062c6d018 133 DBG("unknown [ESC] %02x\r\n", dat);
gsfan 0:2f6062c6d018 134 break;
gsfan 0:2f6062c6d018 135 }
gsfan 0:2f6062c6d018 136 _escape = 0;
gsfan 0:2f6062c6d018 137 } else {
gsfan 0:2f6062c6d018 138 if (dat == 0x1b) {
gsfan 0:2f6062c6d018 139 _escape = 1;
gsfan 0:2f6062c6d018 140 } else
gsfan 0:2f6062c6d018 141 if (dat != '\r') {
gsfan 0:2f6062c6d018 142 // command
gsfan 0:2f6062c6d018 143 _buf_cmd.put(dat);
gsfan 0:2f6062c6d018 144 if (dat == '\n') _gs_enter ++;
gsfan 0:2f6062c6d018 145 }
gsfan 0:2f6062c6d018 146 }
gsfan 0:2f6062c6d018 147 break;
gsfan 0:2f6062c6d018 148
gsfan 0:2f6062c6d018 149 case GSMODE_DATA_RX:
gsfan 0:2f6062c6d018 150 case GSMODE_DATA_RXUDP:
gsfan 0:2f6062c6d018 151 if (mode == 0) {
gsfan 0:2f6062c6d018 152 // cid
gsfan 0:2f6062c6d018 153 _cid = x2i(dat);
gsfan 0:2f6062c6d018 154 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 155 mode ++;
gsfan 0:2f6062c6d018 156 if (_gs_mode == GSMODE_DATA_RX) {
gsfan 0:2f6062c6d018 157 mode = 3;
gsfan 0:2f6062c6d018 158 }
gsfan 0:2f6062c6d018 159 len = 0;
gsfan 0:2f6062c6d018 160 } else
gsfan 0:2f6062c6d018 161 if (mode == 1) {
gsfan 0:2f6062c6d018 162 // ip
gsfan 0:2f6062c6d018 163 if ((dat < '0' || dat > '9') && dat != '.') {
gsfan 0:2f6062c6d018 164 int ip1, ip2, ip3, ip4;
gsfan 0:2f6062c6d018 165 tmp[len] = 0;
gsfan 0:2f6062c6d018 166 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 167 _from.setIp(IpAddr(ip1, ip2, ip3, ip4));
gsfan 0:2f6062c6d018 168 mode ++;
gsfan 0:2f6062c6d018 169 len = 0;
gsfan 0:2f6062c6d018 170 break;
gsfan 0:2f6062c6d018 171 }
gsfan 0:2f6062c6d018 172 tmp[len] = dat;
gsfan 0:2f6062c6d018 173 len ++;
gsfan 0:2f6062c6d018 174 } else
gsfan 0:2f6062c6d018 175 if (mode == 2) {
gsfan 0:2f6062c6d018 176 // port
gsfan 0:2f6062c6d018 177 if (dat < '0' || dat > '9') {
gsfan 0:2f6062c6d018 178 tmp[len] = 0;
gsfan 0:2f6062c6d018 179 _from.setPort(atoi(tmp));
gsfan 0:2f6062c6d018 180 mode ++;
gsfan 0:2f6062c6d018 181 len = 0;
gsfan 0:2f6062c6d018 182 break;
gsfan 0:2f6062c6d018 183 }
gsfan 0:2f6062c6d018 184 tmp[len] = dat;
gsfan 0:2f6062c6d018 185 len ++;
gsfan 0:2f6062c6d018 186 } else
gsfan 0:2f6062c6d018 187 if (_escape) {
gsfan 0:2f6062c6d018 188 // esc
gsfan 0:2f6062c6d018 189 switch (dat) {
gsfan 0:2f6062c6d018 190 case 'E':
gsfan 0:2f6062c6d018 191 DBG("recv ascii %d\r\n", _cid);
gsfan 0:2f6062c6d018 192 _gs_sock[_cid].received = 1;
gsfan 0:2f6062c6d018 193 _gs_mode = GSMODE_COMMAND;
gsfan 0:2f6062c6d018 194 // recv interrupt
gsfan 0:2f6062c6d018 195 if (_gs_sock[_cid].protocol == GSPROT_HTTPGET && _gs_sock[_cid].onGsReceive != NULL) {
gsfan 1:b127c6c5241d 196 _gs_sock[_cid].onGsReceive(_cid, _gs_sock[_cid].data->use());
gsfan 0:2f6062c6d018 197 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 198 }
gsfan 0:2f6062c6d018 199 break;
gsfan 0:2f6062c6d018 200 default:
gsfan 0:2f6062c6d018 201 DBG("unknown <ESC> %02x\r\n", dat);
gsfan 0:2f6062c6d018 202 break;
gsfan 0:2f6062c6d018 203 }
gsfan 0:2f6062c6d018 204 _escape = 0;
gsfan 0:2f6062c6d018 205 } else {
gsfan 0:2f6062c6d018 206 if (dat == 0x1b) {
gsfan 0:2f6062c6d018 207 _escape = 1;
gsfan 0:2f6062c6d018 208 } else {
gsfan 0:2f6062c6d018 209 // data
gsfan 0:2f6062c6d018 210 _gs_sock[_cid].data->put(dat);
gsfan 0:2f6062c6d018 211 len ++;
gsfan 12:63e714550791 212 if (len < GS_DATA_SIZE && _gs_sock[_cid].data->available() == 0) {
gsfan 14:9e89b922ace1 213 // buffer full
gsfan 14:9e89b922ace1 214 if (_gs_sock[_cid].onGsReceive != NULL) {
gsfan 14:9e89b922ace1 215 _gs_sock[_cid].onGsReceive(_cid, _gs_sock[_cid].data->use());
gsfan 12:63e714550791 216 }
gsfan 12:63e714550791 217 }
gsfan 0:2f6062c6d018 218 }
gsfan 0:2f6062c6d018 219 }
gsfan 0:2f6062c6d018 220 break;
gsfan 0:2f6062c6d018 221
gsfan 0:2f6062c6d018 222 case GSMODE_DATA_RX_BULK:
gsfan 0:2f6062c6d018 223 case GSMODE_DATA_RXUDP_BULK:
gsfan 5:6def1d0df519 224 // DBG("%c", dat);
gsfan 0:2f6062c6d018 225 if (mode == 0) {
gsfan 0:2f6062c6d018 226 // cid
gsfan 0:2f6062c6d018 227 _cid = x2i(dat);
gsfan 0:2f6062c6d018 228 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 229 mode ++;
gsfan 0:2f6062c6d018 230 if (_gs_mode == GSMODE_DATA_RX_BULK) {
gsfan 0:2f6062c6d018 231 mode = 3;
gsfan 0:2f6062c6d018 232 }
gsfan 0:2f6062c6d018 233 len = 0;
gsfan 0:2f6062c6d018 234 } else
gsfan 0:2f6062c6d018 235 if (mode == 1) {
gsfan 0:2f6062c6d018 236 // ip
gsfan 0:2f6062c6d018 237 if ((dat < '0' || dat > '9') && dat != '.') {
gsfan 0:2f6062c6d018 238 int ip1, ip2, ip3, ip4;
gsfan 0:2f6062c6d018 239 tmp[len] = 0;
gsfan 0:2f6062c6d018 240 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 241 _from.setIp(IpAddr(ip1, ip2, ip3, ip4));
gsfan 0:2f6062c6d018 242 mode ++;
gsfan 0:2f6062c6d018 243 len = 0;
gsfan 0:2f6062c6d018 244 break;
gsfan 0:2f6062c6d018 245 }
gsfan 0:2f6062c6d018 246 tmp[len] = dat;
gsfan 0:2f6062c6d018 247 len ++;
gsfan 0:2f6062c6d018 248 } else
gsfan 0:2f6062c6d018 249 if (mode == 2) {
gsfan 0:2f6062c6d018 250 // port
gsfan 0:2f6062c6d018 251 if (dat < '0' || dat > '9') {
gsfan 0:2f6062c6d018 252 tmp[len] = 0;
gsfan 0:2f6062c6d018 253 _from.setPort(atoi(tmp));
gsfan 0:2f6062c6d018 254 mode ++;
gsfan 0:2f6062c6d018 255 len = 0;
gsfan 0:2f6062c6d018 256 break;
gsfan 0:2f6062c6d018 257 }
gsfan 0:2f6062c6d018 258 tmp[len] = dat;
gsfan 0:2f6062c6d018 259 len ++;
gsfan 0:2f6062c6d018 260 } else
gsfan 0:2f6062c6d018 261 if (mode == 3) {
gsfan 0:2f6062c6d018 262 // length
gsfan 0:2f6062c6d018 263 tmp[len] = dat;
gsfan 0:2f6062c6d018 264 len ++;
gsfan 0:2f6062c6d018 265 if (len >= 4) {
gsfan 0:2f6062c6d018 266 tmp[len] = 0;
gsfan 0:2f6062c6d018 267 len = atoi(tmp);
gsfan 0:2f6062c6d018 268 mode ++;
gsfan 0:2f6062c6d018 269 break;
gsfan 0:2f6062c6d018 270 }
gsfan 0:2f6062c6d018 271 } else
gsfan 0:2f6062c6d018 272 if (mode == 4) {
gsfan 0:2f6062c6d018 273 // data
gsfan 5:6def1d0df519 274 if (_gs_sock[_cid].data != NULL) {
gsfan 5:6def1d0df519 275 _gs_sock[_cid].data->put(dat);
gsfan 5:6def1d0df519 276 }
gsfan 0:2f6062c6d018 277 len --;
gsfan 12:63e714550791 278 if (len && _gs_sock[_cid].data->available() == 0) {
gsfan 14:9e89b922ace1 279 // buffer full
gsfan 14:9e89b922ace1 280 if (_gs_sock[_cid].onGsReceive != NULL) {
gsfan 12:63e714550791 281 _gs_sock[_cid].onGsReceive(_cid, _gs_sock[_cid].data->use());
gsfan 12:63e714550791 282 }
gsfan 12:63e714550791 283 }
gsfan 0:2f6062c6d018 284 if (len == 0) {
gsfan 0:2f6062c6d018 285 DBG("recv binary %d\r\n", _cid);
gsfan 0:2f6062c6d018 286 _gs_sock[_cid].received = 1;
gsfan 0:2f6062c6d018 287 _escape = 0;
gsfan 0:2f6062c6d018 288 _gs_mode = GSMODE_COMMAND;
gsfan 0:2f6062c6d018 289 // recv interrupt
gsfan 0:2f6062c6d018 290 if (_gs_sock[_cid].protocol == GSPROT_HTTPGET && _gs_sock[_cid].onGsReceive != NULL) {
gsfan 1:b127c6c5241d 291 _gs_sock[_cid].onGsReceive(_cid, _gs_sock[_cid].data->use());
gsfan 0:2f6062c6d018 292 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 293 }
gsfan 0:2f6062c6d018 294 }
gsfan 0:2f6062c6d018 295 }
gsfan 0:2f6062c6d018 296 break;
gsfan 0:2f6062c6d018 297
gsfan 0:2f6062c6d018 298 }
gsfan 0:2f6062c6d018 299 }
gsfan 0:2f6062c6d018 300
gsfan 12:63e714550791 301 int GSwifi::command (const char *cmd, GSRESPONCE res, int timeout) {
gsfan 0:2f6062c6d018 302 int i;
gsfan 0:2f6062c6d018 303
gsfan 0:2f6062c6d018 304 if (! cmd) {
gsfan 0:2f6062c6d018 305 // dummy CR+LF
gsfan 0:2f6062c6d018 306 _gs.printf("\r\n");
gsfan 12:63e714550791 307 for (i = 0; i < 10; i ++) {
gsfan 14:9e89b922ace1 308 wait_ms(10);
gsfan 14:9e89b922ace1 309 poll();
gsfan 14:9e89b922ace1 310 _buf_cmd.clear();
gsfan 2:c6e0e97901b3 311 }
gsfan 0:2f6062c6d018 312 return 0;
gsfan 0:2f6062c6d018 313 }
gsfan 0:2f6062c6d018 314
gsfan 0:2f6062c6d018 315 _buf_cmd.clear();
gsfan 0:2f6062c6d018 316 _gs_ok = 0;
gsfan 0:2f6062c6d018 317 _gs_failure = 0;
gsfan 0:2f6062c6d018 318 _gs_enter = 0;
gsfan 0:2f6062c6d018 319 for (i = 0; i < strlen(cmd); i ++) {
gsfan 1:b127c6c5241d 320 _gs_putc(cmd[i]);
gsfan 0:2f6062c6d018 321 }
gsfan 1:b127c6c5241d 322 _gs_putc('\r');
gsfan 1:b127c6c5241d 323 _gs_putc('\n');
gsfan 0:2f6062c6d018 324 DBG("command: %s\r\n", cmd);
gsfan 0:2f6062c6d018 325 if (strlen(cmd) == 0) return 0;
gsfan 0:2f6062c6d018 326 wait_ms(50);
gsfan 0:2f6062c6d018 327 if (timeout) {
gsfan 0:2f6062c6d018 328 return cmdResponse(res, timeout);
gsfan 0:2f6062c6d018 329 } else {
gsfan 0:2f6062c6d018 330 return 0;
gsfan 0:2f6062c6d018 331 }
gsfan 0:2f6062c6d018 332 }
gsfan 0:2f6062c6d018 333
gsfan 0:2f6062c6d018 334 int GSwifi::cmdResponse (GSRESPONCE res, int ms) {
gsfan 0:2f6062c6d018 335 int i, n = 0, flg = 0;
gsfan 0:2f6062c6d018 336 char buf[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 337 Timer timeout;
gsfan 0:2f6062c6d018 338
gsfan 0:2f6062c6d018 339 timeout.start();
gsfan 0:2f6062c6d018 340 for (;;) {
gsfan 0:2f6062c6d018 341 // recv response
gsfan 0:2f6062c6d018 342 i = 0;
gsfan 0:2f6062c6d018 343 while (i < sizeof(buf)) {
gsfan 0:2f6062c6d018 344 if (_buf_cmd.use()) {
gsfan 0:2f6062c6d018 345 _buf_cmd.get(&buf[i]);
gsfan 0:2f6062c6d018 346 if (buf[i] == '\n') {
gsfan 0:2f6062c6d018 347 break;
gsfan 0:2f6062c6d018 348 }
gsfan 0:2f6062c6d018 349 i ++;
gsfan 0:2f6062c6d018 350 }
gsfan 0:2f6062c6d018 351 if (timeout.read_ms() > ms) {
gsfan 0:2f6062c6d018 352 timeout.stop();
gsfan 0:2f6062c6d018 353 DBG("timeout\r\n");
gsfan 0:2f6062c6d018 354 return -1;
gsfan 0:2f6062c6d018 355 }
gsfan 0:2f6062c6d018 356 }
gsfan 0:2f6062c6d018 357 _gs_enter = 0;
gsfan 0:2f6062c6d018 358 if (i == 0) continue;
gsfan 0:2f6062c6d018 359 buf[i] = 0;
gsfan 0:2f6062c6d018 360 DBG("response: %s\r\n", buf);
gsfan 0:2f6062c6d018 361 timeout.stop();
gsfan 0:2f6062c6d018 362
gsfan 0:2f6062c6d018 363 if (strcmp(buf, "OK") == 0) {
gsfan 0:2f6062c6d018 364 _gs_ok = 1;
gsfan 0:2f6062c6d018 365 } else
gsfan 0:2f6062c6d018 366 if (strncmp(buf, "ERROR", 5) == 0) {
gsfan 0:2f6062c6d018 367 _gs_failure = 1;
gsfan 0:2f6062c6d018 368 }
gsfan 0:2f6062c6d018 369
gsfan 0:2f6062c6d018 370 switch(res) {
gsfan 0:2f6062c6d018 371 case GSRES_NORMAL:
gsfan 0:2f6062c6d018 372 flg = 1;
gsfan 0:2f6062c6d018 373 break;
gsfan 0:2f6062c6d018 374 case GSRES_WPS:
gsfan 0:2f6062c6d018 375 if (n == 0 && strncmp(buf, "SSID", 4) == 0) {
gsfan 0:2f6062c6d018 376 n ++;
gsfan 0:2f6062c6d018 377 } else
gsfan 0:2f6062c6d018 378 if (n == 1 && strncmp(buf, "CHANNEL", 7) == 0) {
gsfan 0:2f6062c6d018 379 n ++;
gsfan 0:2f6062c6d018 380 } else
gsfan 0:2f6062c6d018 381 if (n == 2 && strncmp(buf, "PASSPHRASE", 10) == 0) {
gsfan 0:2f6062c6d018 382 n ++;
gsfan 0:2f6062c6d018 383 flg = 1;
gsfan 0:2f6062c6d018 384 }
gsfan 0:2f6062c6d018 385 break;
gsfan 0:2f6062c6d018 386 case GSRES_CONNECT:
gsfan 0:2f6062c6d018 387 if (strncmp(buf, "CONNECT", 7) == 0) {
gsfan 0:2f6062c6d018 388 _cid = x2i(buf[8]);
gsfan 0:2f6062c6d018 389 flg = 1;
gsfan 0:2f6062c6d018 390 }
gsfan 0:2f6062c6d018 391 break;
gsfan 0:2f6062c6d018 392 case GSRES_DHCP:
gsfan 0:2f6062c6d018 393 if (n == 0 && strstr(buf, "SubNet") && strstr(buf, "Gateway")) {
gsfan 0:2f6062c6d018 394 n ++;
gsfan 0:2f6062c6d018 395 } else
gsfan 0:2f6062c6d018 396 if (n == 1) {
gsfan 0:2f6062c6d018 397 int ip1, ip2, ip3, ip4;
gsfan 0:2f6062c6d018 398 char *tmp = buf + 1;
gsfan 0:2f6062c6d018 399 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 400 _ipaddr = IpAddr(ip1, ip2, ip3, ip4);
gsfan 0:2f6062c6d018 401 tmp = strstr(tmp, ":") + 2;
gsfan 0:2f6062c6d018 402 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 403 _netmask = IpAddr(ip1, ip2, ip3, ip4);
gsfan 0:2f6062c6d018 404 tmp = strstr(tmp, ":") + 2;
gsfan 0:2f6062c6d018 405 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 406 _gateway = IpAddr(ip1, ip2, ip3, ip4);
gsfan 0:2f6062c6d018 407 n ++;
gsfan 0:2f6062c6d018 408 flg = 1;
gsfan 0:2f6062c6d018 409 }
gsfan 0:2f6062c6d018 410 break;
gsfan 0:2f6062c6d018 411 case GSRES_MACADDRESS:
gsfan 9:3b819ba34c6c 412 if (buf[2] == ':' && buf[5] == ':') {
gsfan 0:2f6062c6d018 413 int mac1, mac2, mac3, mac4, mac5, mac6;
gsfan 0:2f6062c6d018 414 sscanf(buf, "%x:%x:%x:%x:%x:%x", &mac1, &mac2, &mac3, &mac4, &mac5, &mac6);
gsfan 4:a8d38857f3fd 415 _mac[0] = mac1;
gsfan 4:a8d38857f3fd 416 _mac[1] = mac2;
gsfan 4:a8d38857f3fd 417 _mac[2] = mac3;
gsfan 4:a8d38857f3fd 418 _mac[3] = mac4;
gsfan 4:a8d38857f3fd 419 _mac[4] = mac5;
gsfan 4:a8d38857f3fd 420 _mac[5] = mac6;
gsfan 0:2f6062c6d018 421 flg = 1;
gsfan 0:2f6062c6d018 422 }
gsfan 0:2f6062c6d018 423 break;
gsfan 0:2f6062c6d018 424 case GSRES_DNSLOOKUP:
gsfan 0:2f6062c6d018 425 if (strncmp(buf, "IP:", 3) == 0) {
gsfan 0:2f6062c6d018 426 int ip1, ip2, ip3, ip4;
gsfan 0:2f6062c6d018 427 sscanf(&buf[3], "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 428 _resolv = IpAddr(ip1, ip2, ip3, ip4);
gsfan 0:2f6062c6d018 429 flg = 1;
gsfan 0:2f6062c6d018 430 }
gsfan 0:2f6062c6d018 431 break;
gsfan 0:2f6062c6d018 432 case GSRES_HTTP:
gsfan 4:a8d38857f3fd 433 if (buf[0] >= '0' && buf[0] <= 'F') {
gsfan 4:a8d38857f3fd 434 _cid = x2i(buf[0]);
gsfan 0:2f6062c6d018 435 flg = 1;
gsfan 0:2f6062c6d018 436 }
gsfan 0:2f6062c6d018 437 break;
gsfan 1:b127c6c5241d 438 case GSRES_RSSI:
gsfan 1:b127c6c5241d 439 if (buf[0] == '-' || (buf[0] >= '0' && buf[0] <= '9')) {
gsfan 1:b127c6c5241d 440 _rssi = atoi(buf);
gsfan 1:b127c6c5241d 441 flg = 1;
gsfan 1:b127c6c5241d 442 }
gsfan 1:b127c6c5241d 443 break;
gsfan 5:6def1d0df519 444 case GSRES_TIME:
gsfan 5:6def1d0df519 445 if (buf[0] >= '0' && buf[0] <= '9') {
gsfan 5:6def1d0df519 446 int year, month, day, hour, min, sec;
gsfan 5:6def1d0df519 447 struct tm t;
gsfan 11:983f4e832a3e 448 sscanf(buf, "%d/%d/%d,%d:%d:%d", &day, &month, &year, &hour, &min, &sec);
gsfan 5:6def1d0df519 449 t.tm_sec = sec;
gsfan 5:6def1d0df519 450 t.tm_min = min;
gsfan 5:6def1d0df519 451 t.tm_hour = hour;
gsfan 5:6def1d0df519 452 t.tm_mday = day;
gsfan 5:6def1d0df519 453 t.tm_mon = month - 1;
gsfan 5:6def1d0df519 454 t.tm_year = year - 1900;
gsfan 5:6def1d0df519 455 _time = mktime(&t);
gsfan 5:6def1d0df519 456 flg = 1;
gsfan 5:6def1d0df519 457 }
gsfan 5:6def1d0df519 458 break;
gsfan 0:2f6062c6d018 459 }
gsfan 0:2f6062c6d018 460
gsfan 0:2f6062c6d018 461 if ((flg && _gs_ok) || _gs_failure) break;
gsfan 0:2f6062c6d018 462 timeout.reset();
gsfan 0:2f6062c6d018 463 timeout.start();
gsfan 0:2f6062c6d018 464 }
gsfan 0:2f6062c6d018 465
gsfan 0:2f6062c6d018 466 if (_gs_failure || flg == 0) {
gsfan 0:2f6062c6d018 467 return -1;
gsfan 0:2f6062c6d018 468 } else {
gsfan 0:2f6062c6d018 469 return 0;
gsfan 0:2f6062c6d018 470 }
gsfan 0:2f6062c6d018 471 }
gsfan 0:2f6062c6d018 472
gsfan 0:2f6062c6d018 473 int GSwifi::connect (GSSECURITY sec, char *ssid, char *pass, int dhcp) {
gsfan 0:2f6062c6d018 474 int r;
gsfan 0:2f6062c6d018 475 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 476
gsfan 0:2f6062c6d018 477 if (_connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 478
gsfan 0:2f6062c6d018 479 command(NULL, GSRES_NORMAL);
gsfan 0:2f6062c6d018 480 if (command("ATE0", GSRES_NORMAL)) return -1;
gsfan 0:2f6062c6d018 481 if (_rts) {
gsfan 0:2f6062c6d018 482 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 483 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 484 }
gsfan 0:2f6062c6d018 485 command("AT+NMAC=?", GSRES_MACADDRESS);
gsfan 0:2f6062c6d018 486 #ifdef GS_BULK
gsfan 0:2f6062c6d018 487 command("AT+BDATA=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 488 #endif
gsfan 0:2f6062c6d018 489
gsfan 0:2f6062c6d018 490 disconnect();
gsfan 0:2f6062c6d018 491 command("AT+WM=0", GSRES_NORMAL); // infrastructure
gsfan 0:2f6062c6d018 492 wait_ms(100);
gsfan 0:2f6062c6d018 493 if (dhcp) {
gsfan 0:2f6062c6d018 494 command("AT+NDHCP=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 495 } else {
gsfan 0:2f6062c6d018 496 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 497 }
gsfan 0:2f6062c6d018 498
gsfan 0:2f6062c6d018 499 switch (sec) {
gsfan 0:2f6062c6d018 500 case GSSEC_NONE:
gsfan 0:2f6062c6d018 501 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 502 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 503 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 504 break;
gsfan 0:2f6062c6d018 505 case GSSEC_OPEN:
gsfan 0:2f6062c6d018 506 case GSSEC_WEP:
gsfan 0:2f6062c6d018 507 sprintf(cmd, "AT+WAUTH=%d", sec);
gsfan 0:2f6062c6d018 508 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 509 sprintf(cmd, "AT+WWEP1=%s", pass);
gsfan 0:2f6062c6d018 510 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 511 wait_ms(100);
gsfan 0:2f6062c6d018 512 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 1:b127c6c5241d 513 r = command(cmd, GSRES_DHCP, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 514 break;
gsfan 0:2f6062c6d018 515 case GSSEC_WPA_PSK:
gsfan 0:2f6062c6d018 516 case GSSEC_WPA2_PSK:
gsfan 0:2f6062c6d018 517 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 518 // sprintf(cmd, "AT+WWPA=%s", pass);
gsfan 0:2f6062c6d018 519 // command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 520 sprintf(cmd, "AT+WPAPSK=%s,%s", ssid, pass);
gsfan 0:2f6062c6d018 521 command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 522 wait_ms(100);
gsfan 0:2f6062c6d018 523 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 1:b127c6c5241d 524 r = command(cmd, GSRES_DHCP, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 525 break;
gsfan 0:2f6062c6d018 526 case GSSEC_WPS_BUTTON:
gsfan 0:2f6062c6d018 527 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 528 r = command("AT+WWPS=1", GSRES_WPS, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 529 if (r) break;
gsfan 0:2f6062c6d018 530 if (dhcp) {
gsfan 0:2f6062c6d018 531 r = command("AT+NDHCP=1", GSRES_DHCP, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 532 }
gsfan 0:2f6062c6d018 533 break;
gsfan 6:a423f0d197de 534 default:
gsfan 6:a423f0d197de 535 DBG("Can't use security\r\n");
gsfan 6:a423f0d197de 536 r = -1;
gsfan 6:a423f0d197de 537 break;
gsfan 0:2f6062c6d018 538 }
gsfan 0:2f6062c6d018 539
gsfan 0:2f6062c6d018 540 if (r == 0) _connect = true;
gsfan 0:2f6062c6d018 541 return r;
gsfan 0:2f6062c6d018 542 }
gsfan 0:2f6062c6d018 543
gsfan 0:2f6062c6d018 544 int GSwifi::adhock (GSSECURITY sec, char *ssid, char *pass, IpAddr ipaddr, IpAddr netmask) {
gsfan 0:2f6062c6d018 545 int r;
gsfan 0:2f6062c6d018 546 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 547
gsfan 0:2f6062c6d018 548 if (_connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 549
gsfan 0:2f6062c6d018 550 command(NULL, GSRES_NORMAL);
gsfan 0:2f6062c6d018 551 if (command("ATE0", GSRES_NORMAL)) return -1;
gsfan 0:2f6062c6d018 552 if (_rts) {
gsfan 0:2f6062c6d018 553 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 554 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 555 }
gsfan 0:2f6062c6d018 556 disconnect();
gsfan 0:2f6062c6d018 557 command("AT+NMAC=?", GSRES_MACADDRESS);
gsfan 0:2f6062c6d018 558 #ifdef GS_BULK
gsfan 0:2f6062c6d018 559 command("AT+BDATA=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 560 #endif
gsfan 0:2f6062c6d018 561
gsfan 0:2f6062c6d018 562 command("AT+WM=1", GSRES_NORMAL); // adhock
gsfan 0:2f6062c6d018 563 wait_ms(100);
gsfan 0:2f6062c6d018 564 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 565 setAddress(ipaddr, netmask, ipaddr, ipaddr);
gsfan 0:2f6062c6d018 566
gsfan 0:2f6062c6d018 567 switch (sec) {
gsfan 0:2f6062c6d018 568 case GSSEC_NONE:
gsfan 0:2f6062c6d018 569 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 570 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 571 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 572 break;
gsfan 0:2f6062c6d018 573 case GSSEC_OPEN:
gsfan 0:2f6062c6d018 574 case GSSEC_WEP:
gsfan 0:2f6062c6d018 575 sprintf(cmd, "AT+WAUTH=%d", sec);
gsfan 0:2f6062c6d018 576 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 577 sprintf(cmd, "AT+WWEP1=%s", pass);
gsfan 0:2f6062c6d018 578 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 579 wait_ms(100);
gsfan 0:2f6062c6d018 580 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 581 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 582 break;
gsfan 0:2f6062c6d018 583 default:
gsfan 0:2f6062c6d018 584 DBG("Can't use security\r\n");
gsfan 0:2f6062c6d018 585 r = -1;
gsfan 0:2f6062c6d018 586 break;
gsfan 0:2f6062c6d018 587 }
gsfan 0:2f6062c6d018 588
gsfan 0:2f6062c6d018 589 if (r == 0) _connect = true;
gsfan 0:2f6062c6d018 590 return r;
gsfan 0:2f6062c6d018 591 }
gsfan 0:2f6062c6d018 592
gsfan 0:2f6062c6d018 593 int GSwifi::limitedap (GSSECURITY sec, char *ssid, char *pass, IpAddr ipaddr, IpAddr netmask) {
gsfan 0:2f6062c6d018 594 int r;
gsfan 0:2f6062c6d018 595 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 596
gsfan 0:2f6062c6d018 597 if (_connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 598
gsfan 0:2f6062c6d018 599 command(NULL, GSRES_NORMAL);
gsfan 0:2f6062c6d018 600 if (command("ATE0", GSRES_NORMAL)) return -1;
gsfan 0:2f6062c6d018 601 if (_rts) {
gsfan 0:2f6062c6d018 602 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 603 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 604 }
gsfan 0:2f6062c6d018 605 disconnect();
gsfan 0:2f6062c6d018 606 command("AT+NMAC=?", GSRES_MACADDRESS);
gsfan 0:2f6062c6d018 607 #ifdef GS_BULK
gsfan 0:2f6062c6d018 608 command("AT+BDATA=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 609 #endif
gsfan 0:2f6062c6d018 610
gsfan 0:2f6062c6d018 611 command("AT+WM=2", GSRES_NORMAL); // limited ap
gsfan 0:2f6062c6d018 612 wait_ms(1000);
gsfan 0:2f6062c6d018 613 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 614 setAddress(ipaddr, netmask, ipaddr, ipaddr);
gsfan 8:bce9e7e51a0d 615 if (command("AT+DHCPSRVR=1", GSRES_NORMAL)) return -1;
gsfan 8:bce9e7e51a0d 616 if (command("AT+DNS=1,setup", GSRES_NORMAL)) return -1;
gsfan 0:2f6062c6d018 617
gsfan 0:2f6062c6d018 618 switch (sec) {
gsfan 0:2f6062c6d018 619 case GSSEC_NONE:
gsfan 0:2f6062c6d018 620 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 621 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 622 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 623 break;
gsfan 0:2f6062c6d018 624 case GSSEC_OPEN:
gsfan 0:2f6062c6d018 625 case GSSEC_WEP:
gsfan 0:2f6062c6d018 626 sprintf(cmd, "AT+WAUTH=%d", sec);
gsfan 0:2f6062c6d018 627 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 628 sprintf(cmd, "AT+WWEP1=%s", pass);
gsfan 0:2f6062c6d018 629 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 630 wait_ms(100);
gsfan 0:2f6062c6d018 631 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 632 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 633 break;
gsfan 0:2f6062c6d018 634 default:
gsfan 0:2f6062c6d018 635 DBG("Can't use security\r\n");
gsfan 0:2f6062c6d018 636 r = -1;
gsfan 0:2f6062c6d018 637 break;
gsfan 0:2f6062c6d018 638 }
gsfan 0:2f6062c6d018 639
gsfan 0:2f6062c6d018 640 if (r == 0) _connect = true;
gsfan 0:2f6062c6d018 641 return r;
gsfan 0:2f6062c6d018 642 }
gsfan 0:2f6062c6d018 643
gsfan 0:2f6062c6d018 644 int GSwifi::disconnect () {
gsfan 0:2f6062c6d018 645 int i;
gsfan 0:2f6062c6d018 646
gsfan 0:2f6062c6d018 647 _connect = false;
gsfan 0:2f6062c6d018 648 for (i = 0; i < 16; i ++) {
gsfan 0:2f6062c6d018 649 _gs_sock[i].connect = false;
gsfan 0:2f6062c6d018 650 }
gsfan 0:2f6062c6d018 651 command("AT+NCLOSEALL", GSRES_NORMAL);
gsfan 0:2f6062c6d018 652 command("AT+WD", GSRES_NORMAL);
gsfan 0:2f6062c6d018 653 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 654 wait_ms(100);
gsfan 0:2f6062c6d018 655 return 0;
gsfan 0:2f6062c6d018 656 }
gsfan 0:2f6062c6d018 657
gsfan 0:2f6062c6d018 658 int GSwifi::setAddress () {
gsfan 0:2f6062c6d018 659
gsfan 0:2f6062c6d018 660 if (command("AT+NDHCP=1", GSRES_DHCP), GS_TIMEOUT2) return -1;
gsfan 0:2f6062c6d018 661 if (_ipaddr.isNull()) return -1;
gsfan 0:2f6062c6d018 662 return 0;
gsfan 0:2f6062c6d018 663 }
gsfan 0:2f6062c6d018 664
gsfan 0:2f6062c6d018 665 int GSwifi::setAddress (IpAddr ipaddr, IpAddr netmask, IpAddr gateway, IpAddr nameserver) {
gsfan 0:2f6062c6d018 666 int r;
gsfan 0:2f6062c6d018 667 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 668
gsfan 0:2f6062c6d018 669 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 670 wait_ms(100);
gsfan 0:2f6062c6d018 671
gsfan 0:2f6062c6d018 672 sprintf(cmd, "AT+NSET=%d.%d.%d.%d,%d.%d.%d.%d,%d.%d.%d.%d",
gsfan 0:2f6062c6d018 673 ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3],
gsfan 0:2f6062c6d018 674 netmask[0], netmask[1], netmask[2], netmask[3],
gsfan 0:2f6062c6d018 675 gateway[0], gateway[1], gateway[2], gateway[3]);
gsfan 0:2f6062c6d018 676 r = command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 677 if (r) return -1;
gsfan 0:2f6062c6d018 678 _ipaddr = ipaddr;
gsfan 0:2f6062c6d018 679 _netmask = netmask;
gsfan 0:2f6062c6d018 680 _gateway = gateway;
gsfan 0:2f6062c6d018 681
gsfan 0:2f6062c6d018 682 if (ipaddr != nameserver) {
gsfan 0:2f6062c6d018 683 sprintf(cmd, "AT+DNSSET=%d.%d.%d.%d",
gsfan 0:2f6062c6d018 684 nameserver[0], nameserver[1], nameserver[2], nameserver[3]);
gsfan 0:2f6062c6d018 685 r = command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 686 }
gsfan 0:2f6062c6d018 687 return r;
gsfan 0:2f6062c6d018 688 }
gsfan 0:2f6062c6d018 689
gsfan 1:b127c6c5241d 690 int GSwifi::getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver) {
gsfan 1:b127c6c5241d 691 ipaddr = _ipaddr;
gsfan 1:b127c6c5241d 692 netmask = _netmask;
gsfan 1:b127c6c5241d 693 gateway = _gateway;
gsfan 1:b127c6c5241d 694 nameserver = _nameserver;
gsfan 2:c6e0e97901b3 695 return 0;
gsfan 1:b127c6c5241d 696 }
gsfan 1:b127c6c5241d 697
gsfan 0:2f6062c6d018 698 int GSwifi::getHostByName (const char* name, IpAddr &addr) {
gsfan 0:2f6062c6d018 699 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 700
gsfan 0:2f6062c6d018 701 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 702
gsfan 0:2f6062c6d018 703 sprintf(cmd, "AT+DNSLOOKUP=%s", name);
gsfan 0:2f6062c6d018 704 if (command(cmd, GSRES_DNSLOOKUP)) return -1;
gsfan 0:2f6062c6d018 705
gsfan 0:2f6062c6d018 706 addr = _resolv;
gsfan 0:2f6062c6d018 707 return 0;
gsfan 0:2f6062c6d018 708 }
gsfan 0:2f6062c6d018 709
gsfan 0:2f6062c6d018 710 int GSwifi::getHostByName (Host &host) {
gsfan 0:2f6062c6d018 711 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 712
gsfan 0:2f6062c6d018 713 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 714
gsfan 0:2f6062c6d018 715 sprintf(cmd, "AT+DNSLOOKUP=%s", host.getName());
gsfan 0:2f6062c6d018 716 if (command(cmd, GSRES_DNSLOOKUP)) return -1;
gsfan 0:2f6062c6d018 717
gsfan 0:2f6062c6d018 718 host.setIp(_resolv);
gsfan 0:2f6062c6d018 719 return 0;
gsfan 0:2f6062c6d018 720 }
gsfan 0:2f6062c6d018 721
gsfan 0:2f6062c6d018 722 int GSwifi::setRFPower (int power) {
gsfan 0:2f6062c6d018 723 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 724
gsfan 0:2f6062c6d018 725 if (power < 0 || power > 7) return -1;
gsfan 0:2f6062c6d018 726
gsfan 0:2f6062c6d018 727 sprintf(cmd, "AT+WP=%d", power);
gsfan 0:2f6062c6d018 728 return command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 729 }
gsfan 0:2f6062c6d018 730
gsfan 2:c6e0e97901b3 731 int GSwifi::powerSave (int active, int save) {
gsfan 0:2f6062c6d018 732 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 733
gsfan 0:2f6062c6d018 734 if (_status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 735
gsfan 2:c6e0e97901b3 736 sprintf(cmd, "AT+WRXACTIVE=%d", active);
gsfan 0:2f6062c6d018 737 command(cmd, GSRES_NORMAL);
gsfan 3:1345daf4ec1a 738 sprintf(cmd, "AT+WRXPS=%d", save);
gsfan 0:2f6062c6d018 739 return command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 740 }
gsfan 0:2f6062c6d018 741
gsfan 0:2f6062c6d018 742 int GSwifi::standby (int msec) {
gsfan 0:2f6062c6d018 743 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 744
gsfan 0:2f6062c6d018 745 if (_status != GSSTAT_READY && _status != GSSTAT_WAKEUP) return -1;
gsfan 0:2f6062c6d018 746
gsfan 0:2f6062c6d018 747 if (_status != GSSTAT_WAKEUP) {
gsfan 0:2f6062c6d018 748 command("AT+WRXACTIVE=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 749 command("AT+STORENWCONN", GSRES_NORMAL);
gsfan 0:2f6062c6d018 750 } else {
gsfan 0:2f6062c6d018 751 command("ATE0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 752 if (_rts) {
gsfan 0:2f6062c6d018 753 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 754 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 755 }
gsfan 0:2f6062c6d018 756 }
gsfan 0:2f6062c6d018 757 _status = GSSTAT_STANDBY;
gsfan 0:2f6062c6d018 758 sprintf(cmd, "AT+PSSTBY=%d,0,0,0", msec); // go standby
gsfan 0:2f6062c6d018 759 return command(cmd, GSRES_NORMAL, 0);
gsfan 0:2f6062c6d018 760 }
gsfan 0:2f6062c6d018 761
gsfan 0:2f6062c6d018 762 int GSwifi::wakeup () {
gsfan 0:2f6062c6d018 763
gsfan 0:2f6062c6d018 764 if (_status == GSSTAT_WAKEUP) {
gsfan 0:2f6062c6d018 765 _status = GSSTAT_READY;
gsfan 0:2f6062c6d018 766 command("ATE0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 767 if (_rts) {
gsfan 0:2f6062c6d018 768 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 769 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 770 }
gsfan 0:2f6062c6d018 771 #ifdef GS_BULK
gsfan 0:2f6062c6d018 772 command("AT+BDATA=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 773 #endif
gsfan 0:2f6062c6d018 774 command("AT+RESTORENWCONN", GSRES_NORMAL);
gsfan 0:2f6062c6d018 775 wait_ms(100);
gsfan 0:2f6062c6d018 776 return command("AT+WRXACTIVE=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 777 } else
gsfan 0:2f6062c6d018 778 if (_status == GSSTAT_DEEPSLEEP) {
gsfan 0:2f6062c6d018 779 _status = GSSTAT_READY;
gsfan 0:2f6062c6d018 780 return command("AT", GSRES_NORMAL);
gsfan 0:2f6062c6d018 781 }
gsfan 0:2f6062c6d018 782 return -1;
gsfan 0:2f6062c6d018 783 }
gsfan 0:2f6062c6d018 784
gsfan 0:2f6062c6d018 785 int GSwifi::deepSleep () {
gsfan 0:2f6062c6d018 786
gsfan 0:2f6062c6d018 787 if (_status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 788
gsfan 0:2f6062c6d018 789 _status = GSSTAT_DEEPSLEEP;
gsfan 0:2f6062c6d018 790 return command("AT+PSDPSLEEP", GSRES_NORMAL, 0); // go deep sleep
gsfan 0:2f6062c6d018 791 }
gsfan 0:2f6062c6d018 792
gsfan 0:2f6062c6d018 793 bool GSwifi::isConnected () {
gsfan 0:2f6062c6d018 794 return _connect;
gsfan 0:2f6062c6d018 795 }
gsfan 0:2f6062c6d018 796
gsfan 0:2f6062c6d018 797 GSSTATUS GSwifi::getStatus () {
gsfan 0:2f6062c6d018 798 return _status;
gsfan 0:2f6062c6d018 799 }
gsfan 0:2f6062c6d018 800
gsfan 1:b127c6c5241d 801 int GSwifi::getRssi () {
gsfan 1:b127c6c5241d 802 if (command("AT+WRSSI=?", GSRES_RSSI)) {
gsfan 1:b127c6c5241d 803 return 0;
gsfan 1:b127c6c5241d 804 }
gsfan 1:b127c6c5241d 805 return _rssi;
gsfan 1:b127c6c5241d 806 }
gsfan 1:b127c6c5241d 807
gsfan 5:6def1d0df519 808 int GSwifi::ntpdate (Host host, int sec) {
gsfan 5:6def1d0df519 809 char cmd[GS_CMD_SIZE];
gsfan 5:6def1d0df519 810
gsfan 5:6def1d0df519 811 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 5:6def1d0df519 812
gsfan 5:6def1d0df519 813 if (host.getIp().isNull()) {
gsfan 5:6def1d0df519 814 if (getHostByName(host)) {
gsfan 5:6def1d0df519 815 if (getHostByName(host)) return -1;
gsfan 5:6def1d0df519 816 }
gsfan 5:6def1d0df519 817 }
gsfan 5:6def1d0df519 818
gsfan 5:6def1d0df519 819 if (sec) {
gsfan 5:6def1d0df519 820 sprintf(cmd, "AT+NTIMESYNC=1,%d.%d.%d.%d,%d,1,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3],
gsfan 5:6def1d0df519 821 GS_TIMEOUT / 1000, sec);
gsfan 5:6def1d0df519 822 } else {
gsfan 5:6def1d0df519 823 sprintf(cmd, "AT+NTIMESYNC=1,%d.%d.%d.%d,%d,0", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3],
gsfan 5:6def1d0df519 824 GS_TIMEOUT / 1000);
gsfan 5:6def1d0df519 825 }
gsfan 5:6def1d0df519 826 return command(cmd, GSRES_NORMAL);
gsfan 5:6def1d0df519 827 }
gsfan 5:6def1d0df519 828
gsfan 5:6def1d0df519 829 int GSwifi::setTime (time_t time) {
gsfan 5:6def1d0df519 830 char cmd[GS_CMD_SIZE];
gsfan 5:6def1d0df519 831 struct tm *t;
gsfan 5:6def1d0df519 832
gsfan 5:6def1d0df519 833 if (_status != GSSTAT_READY) return -1;
gsfan 5:6def1d0df519 834
gsfan 5:6def1d0df519 835 t = localtime(&time);
gsfan 5:6def1d0df519 836 sprintf(cmd, "AT+SETTIME=%d/%d/%d,%d:%d:%d", t->tm_mday, t->tm_mon + 1, t->tm_year + 1900, t->tm_hour, t->tm_min, t->tm_sec);
gsfan 5:6def1d0df519 837 return command(cmd, GSRES_NORMAL);
gsfan 5:6def1d0df519 838 }
gsfan 5:6def1d0df519 839
gsfan 5:6def1d0df519 840 time_t GSwifi::getTime () {
gsfan 5:6def1d0df519 841
gsfan 5:6def1d0df519 842 if (command("AT+GETTIME=?", GSRES_TIME)) {
gsfan 5:6def1d0df519 843 return 0;
gsfan 5:6def1d0df519 844 }
gsfan 5:6def1d0df519 845 return _time;
gsfan 5:6def1d0df519 846 }
gsfan 5:6def1d0df519 847
gsfan 6:a423f0d197de 848 int GSwifi::gpioOut (int port, int out) {
gsfan 6:a423f0d197de 849 char cmd[GS_CMD_SIZE];
gsfan 6:a423f0d197de 850
gsfan 6:a423f0d197de 851 if (_status != GSSTAT_READY) return -1;
gsfan 6:a423f0d197de 852
gsfan 6:a423f0d197de 853 sprintf(cmd, "AT+DGPIO=%d,%d", port, out);
gsfan 6:a423f0d197de 854 return command(cmd, GSRES_NORMAL);
gsfan 6:a423f0d197de 855 }
gsfan 6:a423f0d197de 856
gsfan 0:2f6062c6d018 857 void GSwifi::poll() {
gsfan 13:03b420e152b7 858 int i, j;
gsfan 0:2f6062c6d018 859
gsfan 16:aea56cce3bf5 860 while (_gs_enter) {
gsfan 0:2f6062c6d018 861 // received "\n"
gsfan 0:2f6062c6d018 862 char buf[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 863
gsfan 0:2f6062c6d018 864 wait_ms(10);
gsfan 0:2f6062c6d018 865 _gs_enter --;
gsfan 0:2f6062c6d018 866 i = 0;
gsfan 0:2f6062c6d018 867 while (_buf_cmd.use() && i < sizeof(buf)) {
gsfan 0:2f6062c6d018 868 _buf_cmd.get(&buf[i]);
gsfan 0:2f6062c6d018 869 if (buf[i] == '\n') {
gsfan 0:2f6062c6d018 870 break;
gsfan 0:2f6062c6d018 871 }
gsfan 0:2f6062c6d018 872 i ++;
gsfan 0:2f6062c6d018 873 }
gsfan 0:2f6062c6d018 874 buf[i] = 0;
gsfan 0:2f6062c6d018 875 DBG("poll: %s\r\n", buf);
gsfan 0:2f6062c6d018 876
gsfan 14:9e89b922ace1 877 if (i == 0) {
gsfan 14:9e89b922ace1 878 } else
gsfan 1:b127c6c5241d 879 if (strncmp(buf, "CONNECT", 7) == 0 && buf[8] >= '0' && buf[8] <= 'F') {
gsfan 0:2f6062c6d018 880 i = x2i(buf[8]);
gsfan 1:b127c6c5241d 881 if (_gs_sock[i].type == GSTYPE_SERVER) {
gsfan 1:b127c6c5241d 882 int acid, ip1, ip2, ip3, ip4;
gsfan 1:b127c6c5241d 883 char *tmp = buf + 12;
gsfan 1:b127c6c5241d 884
gsfan 1:b127c6c5241d 885 acid = x2i(buf[10]);
gsfan 1:b127c6c5241d 886 newSock(acid, _gs_sock[i].type, _gs_sock[i].protocol, _gs_sock[i].onGsReceive);
gsfan 1:b127c6c5241d 887 _gs_sock[acid].lcid = i;
gsfan 1:b127c6c5241d 888 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 1:b127c6c5241d 889 _gs_sock[acid].host.setIp(IpAddr(ip1, ip2, ip3, ip4));
gsfan 1:b127c6c5241d 890 tmp = strstr(tmp, " ") + 1;
gsfan 1:b127c6c5241d 891 _gs_sock[acid].host.setPort(atoi(tmp));
gsfan 1:b127c6c5241d 892 }
gsfan 0:2f6062c6d018 893 } else
gsfan 0:2f6062c6d018 894 if (strncmp(buf, "DISCONNECT", 10) == 0) {
gsfan 0:2f6062c6d018 895 _gs_sock[x2i(buf[11])].connect = false;
gsfan 0:2f6062c6d018 896 } else
gsfan 0:2f6062c6d018 897 if (strncmp(buf, "DISASSOCIATED", 13) == 0 ||
gsfan 0:2f6062c6d018 898 strncmp(buf, "Disassociated", 13) == 0 ||
gsfan 16:aea56cce3bf5 899 strncmp(buf, "Disassociation Event", 20) == 0 ||
gsfan 16:aea56cce3bf5 900 strncmp(buf, "UnExpected Warm Boot", 20) == 0) {
gsfan 0:2f6062c6d018 901 _connect = false;
gsfan 0:2f6062c6d018 902 for (i = 0; i < 16; i ++) {
gsfan 0:2f6062c6d018 903 _gs_sock[i].connect = false;
gsfan 0:2f6062c6d018 904 }
gsfan 0:2f6062c6d018 905 } else
gsfan 0:2f6062c6d018 906 if (strncmp(buf, "Out of StandBy-Timer", 20) == 0 ||
gsfan 0:2f6062c6d018 907 strncmp(buf, "Out of StandBy-Alarm", 20) == 0) {
gsfan 16:aea56cce3bf5 908 // if (_status == GSSTAT_STANDBY) {
gsfan 0:2f6062c6d018 909 _status = GSSTAT_WAKEUP;
gsfan 16:aea56cce3bf5 910 // }
gsfan 0:2f6062c6d018 911 } else
gsfan 0:2f6062c6d018 912 if (strncmp(buf, "Out of Deep Sleep", 17) == 0 ) {
gsfan 16:aea56cce3bf5 913 // if (_status == GSSTAT_DEEPSLEEP) {
gsfan 0:2f6062c6d018 914 _status = GSSTAT_READY;
gsfan 16:aea56cce3bf5 915 // }
gsfan 0:2f6062c6d018 916 } else
gsfan 0:2f6062c6d018 917 if (strncmp(buf, "Out of", 6) == 0) {
gsfan 0:2f6062c6d018 918 }
gsfan 0:2f6062c6d018 919 DBG("status: %d\r\n", _status);
gsfan 0:2f6062c6d018 920 }
gsfan 0:2f6062c6d018 921
gsfan 0:2f6062c6d018 922 for (i = 0; i < 16; i ++) {
gsfan 0:2f6062c6d018 923 // if (_gs_sock[i].connect && _gs_sock[i].received) {
gsfan 0:2f6062c6d018 924 if (_gs_sock[i].received && _gs_sock[i].data->use()) {
gsfan 0:2f6062c6d018 925 // recv interrupt
gsfan 0:2f6062c6d018 926 _gs_sock[i].received = 0;
gsfan 12:63e714550791 927 if (_gs_sock[i].onGsReceive != NULL) {
gsfan 12:63e714550791 928 for (j = 0; j < 1500 / GS_DATA_SIZE + 1; j ++) {
gsfan 12:63e714550791 929 if (! _gs_sock[i].data->use()) break;
gsfan 12:63e714550791 930 _gs_sock[i].onGsReceive(i, _gs_sock[i].data->use());
gsfan 12:63e714550791 931 }
gsfan 12:63e714550791 932 }
gsfan 0:2f6062c6d018 933 }
gsfan 0:2f6062c6d018 934 }
gsfan 0:2f6062c6d018 935 }
gsfan 0:2f6062c6d018 936
gsfan 2:c6e0e97901b3 937 void GSwifi::newSock (int cid, GSTYPE type, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) {
gsfan 1:b127c6c5241d 938 _gs_sock[cid].type = type;
gsfan 1:b127c6c5241d 939 _gs_sock[cid].protocol = pro;
gsfan 1:b127c6c5241d 940 _gs_sock[cid].connect = true;
gsfan 1:b127c6c5241d 941 if (_gs_sock[cid].data == NULL) {
gsfan 1:b127c6c5241d 942 _gs_sock[cid].data = new RingBuffer(GS_DATA_SIZE);
gsfan 1:b127c6c5241d 943 } else {
gsfan 1:b127c6c5241d 944 _gs_sock[cid].data->clear();
gsfan 1:b127c6c5241d 945 }
gsfan 1:b127c6c5241d 946 _gs_sock[cid].lcid = 0;
gsfan 1:b127c6c5241d 947 _gs_sock[cid].received = 0;
gsfan 1:b127c6c5241d 948 _gs_sock[cid].onGsReceive = ponGsReceive;
gsfan 1:b127c6c5241d 949 }
gsfan 1:b127c6c5241d 950
gsfan 0:2f6062c6d018 951 int GSwifi::open (Host &host, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) {
gsfan 0:2f6062c6d018 952 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 953
gsfan 0:2f6062c6d018 954 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 955 if (host.getIp().isNull() || host.getPort() == 0) {
gsfan 0:2f6062c6d018 956 return -1;
gsfan 0:2f6062c6d018 957 }
gsfan 0:2f6062c6d018 958
gsfan 0:2f6062c6d018 959 if (pro == GSPROT_UDP) {
gsfan 0:2f6062c6d018 960 sprintf(cmd, "AT+NCUDP=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 0:2f6062c6d018 961 } else {
gsfan 0:2f6062c6d018 962 sprintf(cmd, "AT+NCTCP=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 0:2f6062c6d018 963 }
gsfan 0:2f6062c6d018 964 if (command(cmd, GSRES_CONNECT)) return -1;
gsfan 0:2f6062c6d018 965
gsfan 1:b127c6c5241d 966 newSock(_cid, GSTYPE_CLIENT, pro, ponGsReceive);
gsfan 0:2f6062c6d018 967 return _cid;
gsfan 0:2f6062c6d018 968 }
gsfan 0:2f6062c6d018 969
gsfan 0:2f6062c6d018 970 int GSwifi::listen (int port, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) {
gsfan 0:2f6062c6d018 971 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 972
gsfan 0:2f6062c6d018 973 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 974 if (port == 0) {
gsfan 0:2f6062c6d018 975 return -1;
gsfan 0:2f6062c6d018 976 }
gsfan 0:2f6062c6d018 977
gsfan 0:2f6062c6d018 978 if (pro == GSPROT_UDP) {
gsfan 0:2f6062c6d018 979 sprintf(cmd, "AT+NSUDP=%d", port);
gsfan 0:2f6062c6d018 980 } else {
gsfan 0:2f6062c6d018 981 sprintf(cmd, "AT+NSTCP=%d", port);
gsfan 0:2f6062c6d018 982 }
gsfan 0:2f6062c6d018 983 if (command(cmd, GSRES_CONNECT)) return -1;
gsfan 0:2f6062c6d018 984
gsfan 1:b127c6c5241d 985 newSock(_cid, GSTYPE_SERVER, pro, ponGsReceive);
gsfan 0:2f6062c6d018 986 return _cid;
gsfan 0:2f6062c6d018 987 }
gsfan 0:2f6062c6d018 988
gsfan 0:2f6062c6d018 989 int GSwifi::close (int cid) {
gsfan 0:2f6062c6d018 990 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 991
gsfan 0:2f6062c6d018 992 if (! _gs_sock[cid].connect) return -1;
gsfan 0:2f6062c6d018 993
gsfan 0:2f6062c6d018 994 _gs_sock[cid].connect = false;
gsfan 0:2f6062c6d018 995 delete _gs_sock[cid].data;
gsfan 0:2f6062c6d018 996 _gs_sock[cid].data = NULL;
gsfan 0:2f6062c6d018 997 sprintf(cmd, "AT+NCLOSE=%X", cid);
gsfan 0:2f6062c6d018 998 return command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 999 }
gsfan 0:2f6062c6d018 1000
gsfan 0:2f6062c6d018 1001 int GSwifi::send (int cid, char *buf, int len) {
gsfan 0:2f6062c6d018 1002 int i;
gsfan 6:a423f0d197de 1003 Timer timeout;
gsfan 0:2f6062c6d018 1004
gsfan 0:2f6062c6d018 1005 if (! _gs_sock[cid].connect) return -1;
gsfan 0:2f6062c6d018 1006
gsfan 0:2f6062c6d018 1007 if ((_gs_sock[cid].protocol == GSPROT_TCP) ||
gsfan 0:2f6062c6d018 1008 (_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_CLIENT)) {
gsfan 0:2f6062c6d018 1009 // TCP Client, TCP Server, UDP Client
gsfan 0:2f6062c6d018 1010 _gs_ok = 0;
gsfan 0:2f6062c6d018 1011 _gs_failure = 0;
gsfan 0:2f6062c6d018 1012 #ifdef GS_BULK
gsfan 0:2f6062c6d018 1013 _gs.printf("\x1bZ%X%04d", cid, len);
gsfan 0:2f6062c6d018 1014 for (i = 0; i < len; i ++) {
gsfan 1:b127c6c5241d 1015 _gs_putc(buf[i]);
gsfan 0:2f6062c6d018 1016 DBG("%c", buf[i]);
gsfan 0:2f6062c6d018 1017 }
gsfan 0:2f6062c6d018 1018 #else
gsfan 0:2f6062c6d018 1019 _gs.printf("\x1bS%X", cid);
gsfan 0:2f6062c6d018 1020 for (i = 0; i < len; i ++) {
gsfan 0:2f6062c6d018 1021 if (buf[i] >= 0x20 && buf[i] < 0x7f) {
gsfan 1:b127c6c5241d 1022 _gs_putc(buf[i]);
gsfan 0:2f6062c6d018 1023 DBG("%c", buf[i]);
gsfan 0:2f6062c6d018 1024 }
gsfan 0:2f6062c6d018 1025 }
gsfan 1:b127c6c5241d 1026 _gs_putc(0x1b);
gsfan 1:b127c6c5241d 1027 _gs_putc('E');
gsfan 0:2f6062c6d018 1028 #endif
gsfan 0:2f6062c6d018 1029 } else {
gsfan 0:2f6062c6d018 1030 return -1;
gsfan 0:2f6062c6d018 1031 }
gsfan 6:a423f0d197de 1032 timeout.start();
gsfan 6:a423f0d197de 1033 while (!_gs_ok && !_gs_failure && timeout.read_ms() < GS_TIMEOUT);
gsfan 0:2f6062c6d018 1034 return _gs_ok == 1 ? 0 : -1;
gsfan 0:2f6062c6d018 1035 }
gsfan 0:2f6062c6d018 1036
gsfan 0:2f6062c6d018 1037 int GSwifi::send (int cid, char *buf, int len, Host &host) {
gsfan 0:2f6062c6d018 1038 int i;
gsfan 6:a423f0d197de 1039 Timer timeout;
gsfan 0:2f6062c6d018 1040
gsfan 0:2f6062c6d018 1041 if (! _gs_sock[cid].connect) return -1;
gsfan 0:2f6062c6d018 1042
gsfan 0:2f6062c6d018 1043 if ((_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_SERVER)) {
gsfan 0:2f6062c6d018 1044 // UDP Server
gsfan 0:2f6062c6d018 1045 _gs_ok = 0;
gsfan 0:2f6062c6d018 1046 _gs_failure = 0;
gsfan 0:2f6062c6d018 1047 #ifdef GS_BULK
gsfan 0:2f6062c6d018 1048 _gs.printf("\x1bY%X", cid);
gsfan 7:b75b7fc144ff 1049 _gs.printf("%d.%d.%d.%d:%d:", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 0:2f6062c6d018 1050 _gs.printf("%04d", len);
gsfan 0:2f6062c6d018 1051 for (i = 0; i < len; i ++) {
gsfan 1:b127c6c5241d 1052 _gs_putc(buf[i]);
gsfan 0:2f6062c6d018 1053 DBG("%c", buf[i]);
gsfan 0:2f6062c6d018 1054 }
gsfan 0:2f6062c6d018 1055 #else
gsfan 0:2f6062c6d018 1056 _gs.printf("\x1bU%X", cid);
gsfan 7:b75b7fc144ff 1057 _gs.printf("%d.%d.%d.%d:%d:", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 0:2f6062c6d018 1058 for (i = 0; i < len; i ++) {
gsfan 0:2f6062c6d018 1059 if (buf[i] >= 0x20 && buf[i] < 0x7f) {
gsfan 1:b127c6c5241d 1060 _gs_putc(buf[i]);
gsfan 0:2f6062c6d018 1061 DBG("%c", buf[i]);
gsfan 0:2f6062c6d018 1062 }
gsfan 0:2f6062c6d018 1063 }
gsfan 1:b127c6c5241d 1064 _gs_putc(0x1b);
gsfan 1:b127c6c5241d 1065 _gs_putc('E');
gsfan 0:2f6062c6d018 1066 #endif
gsfan 0:2f6062c6d018 1067 } else {
gsfan 0:2f6062c6d018 1068 return -1;
gsfan 0:2f6062c6d018 1069 }
gsfan 6:a423f0d197de 1070 timeout.start();
gsfan 6:a423f0d197de 1071 while (!_gs_ok && !_gs_failure && timeout.read_ms() < GS_TIMEOUT);
gsfan 0:2f6062c6d018 1072 return _gs_ok == 1 ? 0 : -1;
gsfan 0:2f6062c6d018 1073 }
gsfan 0:2f6062c6d018 1074
gsfan 0:2f6062c6d018 1075 int GSwifi::recv (int cid, char *buf, int len) {
gsfan 0:2f6062c6d018 1076 int r;
gsfan 0:2f6062c6d018 1077 Timer timeout;
gsfan 0:2f6062c6d018 1078
gsfan 0:2f6062c6d018 1079 if (_gs_sock[cid].data == NULL) return 0;
gsfan 0:2f6062c6d018 1080
gsfan 0:2f6062c6d018 1081 timeout.start();
gsfan 0:2f6062c6d018 1082 while (_gs_sock[cid].data->use() == 0) {
gsfan 0:2f6062c6d018 1083 if (timeout.read_ms() > GS_TIMEOUT) return 0;
gsfan 0:2f6062c6d018 1084 }
gsfan 0:2f6062c6d018 1085 timeout.stop();
gsfan 0:2f6062c6d018 1086
gsfan 0:2f6062c6d018 1087 r = _gs_sock[cid].data->get(buf, len);
gsfan 0:2f6062c6d018 1088 return r;
gsfan 0:2f6062c6d018 1089 }
gsfan 0:2f6062c6d018 1090
gsfan 0:2f6062c6d018 1091 int GSwifi::recv (int cid, char *buf, int len, Host &host) {
gsfan 0:2f6062c6d018 1092 int r;
gsfan 0:2f6062c6d018 1093 Timer timeout;
gsfan 0:2f6062c6d018 1094
gsfan 0:2f6062c6d018 1095 if (_gs_sock[cid].data == NULL) return 0;
gsfan 0:2f6062c6d018 1096
gsfan 0:2f6062c6d018 1097 timeout.start();
gsfan 0:2f6062c6d018 1098 while (_gs_sock[cid].data->use() == 0) {
gsfan 0:2f6062c6d018 1099 if (timeout.read_ms() > GS_TIMEOUT) return 0;
gsfan 0:2f6062c6d018 1100 }
gsfan 0:2f6062c6d018 1101 timeout.stop();
gsfan 0:2f6062c6d018 1102
gsfan 0:2f6062c6d018 1103 r = _gs_sock[cid].data->get(buf, len);
gsfan 0:2f6062c6d018 1104 host = _from;
gsfan 0:2f6062c6d018 1105 return r;
gsfan 0:2f6062c6d018 1106 }
gsfan 0:2f6062c6d018 1107
gsfan 0:2f6062c6d018 1108 bool GSwifi::isConnected (int cid) {
gsfan 0:2f6062c6d018 1109 return _gs_sock[cid].connect;
gsfan 0:2f6062c6d018 1110 }
gsfan 0:2f6062c6d018 1111
gsfan 15:5febfc399099 1112 int GSwifi::httpGet (Host &host, char *uri, int ssl, char *user, char *pwd, onGsReceiveFunc ponGsReceive) {
gsfan 0:2f6062c6d018 1113 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 1114
gsfan 0:2f6062c6d018 1115 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 1116
gsfan 0:2f6062c6d018 1117 if (host.getIp().isNull()) {
gsfan 0:2f6062c6d018 1118 if (getHostByName(host)) {
gsfan 0:2f6062c6d018 1119 if (getHostByName(host)) return -1;
gsfan 0:2f6062c6d018 1120 }
gsfan 0:2f6062c6d018 1121 }
gsfan 0:2f6062c6d018 1122 if (host.getPort() == 0) {
gsfan 0:2f6062c6d018 1123 if (ssl) {
gsfan 0:2f6062c6d018 1124 host.setPort(443);
gsfan 0:2f6062c6d018 1125 } else {
gsfan 0:2f6062c6d018 1126 host.setPort(80);
gsfan 0:2f6062c6d018 1127 }
gsfan 0:2f6062c6d018 1128 }
gsfan 0:2f6062c6d018 1129
gsfan 0:2f6062c6d018 1130 command("AT+HTTPCONF=3,close", GSRES_NORMAL); // Connection:
gsfan 0:2f6062c6d018 1131 sprintf(cmd, "AT+HTTPCONF=11,%s", host.getName()); // Host:
gsfan 0:2f6062c6d018 1132 command(cmd, GSRES_NORMAL);
gsfan 15:5febfc399099 1133 if (user && pwd) {
gsfan 15:5febfc399099 1134 char tmp[GS_CMD_SIZE], tmp2[GS_CMD_SIZE];
gsfan 15:5febfc399099 1135 snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
gsfan 15:5febfc399099 1136 base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2));
gsfan 15:5febfc399099 1137 sprintf(cmd, "AT+HTTPCONF=2,Basic %s", tmp2); // Authorization:
gsfan 15:5febfc399099 1138 command(cmd, GSRES_NORMAL);
gsfan 15:5febfc399099 1139 } else {
gsfan 15:5febfc399099 1140 command("AT+HTTPCONFDEL=2", GSRES_NORMAL);
gsfan 15:5febfc399099 1141 }
gsfan 15:5febfc399099 1142 command("AT+HTTPCONFDEL=5", GSRES_NORMAL);
gsfan 15:5febfc399099 1143 command("AT+HTTPCONFDEL=7", GSRES_NORMAL);
gsfan 15:5febfc399099 1144
gsfan 0:2f6062c6d018 1145 sprintf(cmd, "AT+HTTPOPEN=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 0:2f6062c6d018 1146 if (ssl) {
gsfan 0:2f6062c6d018 1147 strcat(cmd, ",1");
gsfan 0:2f6062c6d018 1148 }
gsfan 0:2f6062c6d018 1149 if (command(cmd, GSRES_HTTP)) return -1;
gsfan 1:b127c6c5241d 1150 newSock(_cid, GSTYPE_CLIENT, GSPROT_HTTPGET, ponGsReceive);
gsfan 0:2f6062c6d018 1151
gsfan 15:5febfc399099 1152 sprintf(cmd, "AT+HTTPSEND=%d,1,%d,%s", _cid, GS_TIMEOUT / 1000, uri); // GET
gsfan 0:2f6062c6d018 1153 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 1154
gsfan 0:2f6062c6d018 1155 return _cid;
gsfan 0:2f6062c6d018 1156 }
gsfan 0:2f6062c6d018 1157
gsfan 15:5febfc399099 1158 int GSwifi::httpGet (Host &host, char *uri, int ssl, onGsReceiveFunc ponGsReceive) {
gsfan 15:5febfc399099 1159 return httpGet (host, uri, ssl, NULL, NULL, ponGsReceive);
gsfan 15:5febfc399099 1160 }
gsfan 15:5febfc399099 1161
gsfan 15:5febfc399099 1162 int GSwifi::httpPost (Host &host, char *uri, char *body, int ssl, char *user, char *pwd, onGsReceiveFunc ponGsReceive) {
gsfan 15:5febfc399099 1163 char cmd[GS_CMD_SIZE];
gsfan 15:5febfc399099 1164 int i, len;
gsfan 15:5febfc399099 1165
gsfan 15:5febfc399099 1166 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 15:5febfc399099 1167
gsfan 15:5febfc399099 1168 if (host.getIp().isNull()) {
gsfan 15:5febfc399099 1169 if (getHostByName(host)) {
gsfan 15:5febfc399099 1170 if (getHostByName(host)) return -1;
gsfan 15:5febfc399099 1171 }
gsfan 15:5febfc399099 1172 }
gsfan 15:5febfc399099 1173 if (host.getPort() == 0) {
gsfan 15:5febfc399099 1174 if (ssl) {
gsfan 15:5febfc399099 1175 host.setPort(443);
gsfan 15:5febfc399099 1176 } else {
gsfan 15:5febfc399099 1177 host.setPort(80);
gsfan 15:5febfc399099 1178 }
gsfan 15:5febfc399099 1179 }
gsfan 15:5febfc399099 1180 len = strlen(body);
gsfan 15:5febfc399099 1181
gsfan 15:5febfc399099 1182 command("AT+HTTPCONF=3,close", GSRES_NORMAL); // Connection:
gsfan 15:5febfc399099 1183 sprintf(cmd, "AT+HTTPCONF=11,%s", host.getName()); // Host:
gsfan 15:5febfc399099 1184 command(cmd, GSRES_NORMAL);
gsfan 15:5febfc399099 1185 sprintf(cmd, "AT+HTTPCONF=5,%d", len); // Content-Length:
gsfan 15:5febfc399099 1186 command(cmd, GSRES_NORMAL);
gsfan 15:5febfc399099 1187 command("AT+HTTPCONF=7,application/x-www-form-urlencoded", GSRES_NORMAL); // Content-type:
gsfan 15:5febfc399099 1188 if (user && pwd) {
gsfan 15:5febfc399099 1189 char tmp[GS_CMD_SIZE], tmp2[GS_CMD_SIZE];
gsfan 15:5febfc399099 1190 snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
gsfan 15:5febfc399099 1191 base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2));
gsfan 15:5febfc399099 1192 sprintf(cmd, "AT+HTTPCONF=2,Basic %s", tmp2); // Authorization:
gsfan 15:5febfc399099 1193 command(cmd, GSRES_NORMAL);
gsfan 15:5febfc399099 1194 } else {
gsfan 15:5febfc399099 1195 command("AT+HTTPCONFDEL=2", GSRES_NORMAL);
gsfan 15:5febfc399099 1196 }
gsfan 15:5febfc399099 1197
gsfan 15:5febfc399099 1198 sprintf(cmd, "AT+HTTPOPEN=%d.%d.%d.%d,%d", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 15:5febfc399099 1199 if (ssl) {
gsfan 15:5febfc399099 1200 strcat(cmd, ",1");
gsfan 15:5febfc399099 1201 }
gsfan 15:5febfc399099 1202 if (command(cmd, GSRES_HTTP)) return -1;
gsfan 15:5febfc399099 1203 newSock(_cid, GSTYPE_CLIENT, GSPROT_HTTPPOST, ponGsReceive);
gsfan 15:5febfc399099 1204
gsfan 15:5febfc399099 1205 sprintf(cmd, "AT+HTTPSEND=%d,3,%d,%s,%d", _cid, GS_TIMEOUT / 1000, uri, len); // POST
gsfan 15:5febfc399099 1206 command(cmd, GSRES_NORMAL);
gsfan 15:5febfc399099 1207
gsfan 15:5febfc399099 1208 _gs.printf("\x1bH%X", _cid);
gsfan 15:5febfc399099 1209 for (i = 0; i < len; i ++) {
gsfan 15:5febfc399099 1210 _gs_putc(body[i]);
gsfan 15:5febfc399099 1211 DBG("%c", body[i]);
gsfan 15:5febfc399099 1212 }
gsfan 15:5febfc399099 1213
gsfan 15:5febfc399099 1214 return _cid;
gsfan 15:5febfc399099 1215 }
gsfan 15:5febfc399099 1216
gsfan 15:5febfc399099 1217 int GSwifi::httpPost (Host &host, char *uri, char *body, int ssl, onGsReceiveFunc ponGsReceive) {
gsfan 15:5febfc399099 1218 return httpPost (host, uri, body, ssl, NULL, NULL, ponGsReceive);
gsfan 15:5febfc399099 1219 }
gsfan 15:5febfc399099 1220
gsfan 0:2f6062c6d018 1221 int GSwifi::certAdd (char *name, char *cert, int len) {
gsfan 0:2f6062c6d018 1222 int i;
gsfan 0:2f6062c6d018 1223 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 1224
gsfan 0:2f6062c6d018 1225 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 1226
gsfan 0:2f6062c6d018 1227 sprintf(cmd, "AT+TCERTADD=%s,1,%d,1", name, len); // Hex, ram
gsfan 0:2f6062c6d018 1228 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 1229
gsfan 1:b127c6c5241d 1230 _gs_putc(0x1b);
gsfan 1:b127c6c5241d 1231 _gs_putc('W');
gsfan 0:2f6062c6d018 1232 for (i = 0; i < len; i ++) {
gsfan 1:b127c6c5241d 1233 _gs_putc(cert[i]);
gsfan 0:2f6062c6d018 1234 }
gsfan 0:2f6062c6d018 1235 return cmdResponse(GSRES_NORMAL, GS_TIMEOUT);
gsfan 0:2f6062c6d018 1236 }
gsfan 0:2f6062c6d018 1237
gsfan 15:5febfc399099 1238 int GSwifi::base64encode (const char *input, int length, char *output, int len) {
gsfan 15:5febfc399099 1239 // code from
gsfan 15:5febfc399099 1240 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
gsfan 15:5febfc399099 1241 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
gsfan 15:5febfc399099 1242 unsigned int c, c1, c2, c3;
gsfan 15:5febfc399099 1243
gsfan 15:5febfc399099 1244 if (len < ((((length-1)/3)+1)<<2)) return -1;
gsfan 15:5febfc399099 1245 for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
gsfan 15:5febfc399099 1246 c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
gsfan 15:5febfc399099 1247 c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
gsfan 15:5febfc399099 1248 c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
gsfan 15:5febfc399099 1249
gsfan 15:5febfc399099 1250 c = ((c1 & 0xFC) >> 2);
gsfan 15:5febfc399099 1251 output[j+0] = base64[c];
gsfan 15:5febfc399099 1252 c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
gsfan 15:5febfc399099 1253 output[j+1] = base64[c];
gsfan 15:5febfc399099 1254 c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
gsfan 15:5febfc399099 1255 output[j+2] = (length>i+1)?base64[c]:'=';
gsfan 15:5febfc399099 1256 c = (c3 & 0x3F);
gsfan 15:5febfc399099 1257 output[j+3] = (length>i+2)?base64[c]:'=';
gsfan 15:5febfc399099 1258 }
gsfan 15:5febfc399099 1259 output[(((length-1)/3)+1)<<2] = '\0';
gsfan 15:5febfc399099 1260 return 0;
gsfan 15:5febfc399099 1261 }
gsfan 15:5febfc399099 1262
gsfan 15:5febfc399099 1263 int GSwifi::urlencode (char *str, char *buf, int len) {
gsfan 15:5febfc399099 1264 // code from
gsfan 15:5febfc399099 1265 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
gsfan 15:5febfc399099 1266 static const char to_hex[] = "0123456789ABCDEF";
gsfan 15:5febfc399099 1267 // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
gsfan 15:5febfc399099 1268 char *pstr = str, *pbuf = buf;
gsfan 15:5febfc399099 1269
gsfan 15:5febfc399099 1270 if (len < (strlen(str) * 3 + 1)) return -1;
gsfan 15:5febfc399099 1271 while (*pstr) {
gsfan 15:5febfc399099 1272 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
gsfan 15:5febfc399099 1273 *pbuf++ = *pstr;
gsfan 15:5febfc399099 1274 } else if (*pstr == ' ') {
gsfan 15:5febfc399099 1275 *pbuf++ = '+';
gsfan 15:5febfc399099 1276 } else {
gsfan 15:5febfc399099 1277 *pbuf++ = '%';
gsfan 15:5febfc399099 1278 *pbuf++ = to_hex[(*pstr >> 4) & 0x0f];
gsfan 15:5febfc399099 1279 *pbuf++ = to_hex[*pstr & 0x0f];
gsfan 15:5febfc399099 1280 }
gsfan 15:5febfc399099 1281 pstr++;
gsfan 15:5febfc399099 1282 }
gsfan 15:5febfc399099 1283 *pbuf = '\0';
gsfan 15:5febfc399099 1284 return 0;
gsfan 15:5febfc399099 1285 }
gsfan 15:5febfc399099 1286
gsfan 0:2f6062c6d018 1287 int GSwifi::x2i (char c) {
gsfan 0:2f6062c6d018 1288 if (c >= '0' && c <= '9') {
gsfan 0:2f6062c6d018 1289 return c - '0';
gsfan 0:2f6062c6d018 1290 } else
gsfan 0:2f6062c6d018 1291 if (c >= 'A' && c <= 'F') {
gsfan 0:2f6062c6d018 1292 return c - 'A' + 10;
gsfan 0:2f6062c6d018 1293 } else
gsfan 0:2f6062c6d018 1294 if (c >= 'a' && c <= 'f') {
gsfan 0:2f6062c6d018 1295 return c - 'a' + 10;
gsfan 0:2f6062c6d018 1296 }
gsfan 0:2f6062c6d018 1297 return 0;
gsfan 0:2f6062c6d018 1298 }
gsfan 0:2f6062c6d018 1299
gsfan 0:2f6062c6d018 1300 char GSwifi::i2x (int i) {
gsfan 0:2f6062c6d018 1301 if (i >= 0 && i <= 9) {
gsfan 0:2f6062c6d018 1302 return i + '0';
gsfan 0:2f6062c6d018 1303 } else
gsfan 0:2f6062c6d018 1304 if (i >= 10 && i <= 15) {
gsfan 0:2f6062c6d018 1305 return i - 10 + 'A';
gsfan 0:2f6062c6d018 1306 }
gsfan 0:2f6062c6d018 1307 return 0;
gsfan 0:2f6062c6d018 1308 }
gsfan 0:2f6062c6d018 1309
gsfan 0:2f6062c6d018 1310
gsfan 0:2f6062c6d018 1311 // for test
gsfan 0:2f6062c6d018 1312
gsfan 0:2f6062c6d018 1313 void GSwifi::test () {
gsfan 2:c6e0e97901b3 1314 /*
gsfan 0:2f6062c6d018 1315 command(NULL, GSRES_NORMAL);
gsfan 0:2f6062c6d018 1316 wait_ms(100);
gsfan 0:2f6062c6d018 1317 command("AT+NCLOSEALL", GSRES_NORMAL);
gsfan 0:2f6062c6d018 1318 _connect = true;
gsfan 2:c6e0e97901b3 1319 */
gsfan 2:c6e0e97901b3 1320 command("AT+WRXACTIVE=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 1321 }
gsfan 0:2f6062c6d018 1322
gsfan 0:2f6062c6d018 1323 int GSwifi::getc() {
gsfan 0:2f6062c6d018 1324 char c;
gsfan 0:2f6062c6d018 1325 if (_buf_cmd.use()) {
gsfan 0:2f6062c6d018 1326 _buf_cmd.get(&c);
gsfan 0:2f6062c6d018 1327 }
gsfan 0:2f6062c6d018 1328 /*
gsfan 0:2f6062c6d018 1329 } else
gsfan 0:2f6062c6d018 1330 if (_gs_sock[0].data != NULL) {
gsfan 0:2f6062c6d018 1331 _gs_sock[0].data->get(&c);
gsfan 0:2f6062c6d018 1332 }
gsfan 0:2f6062c6d018 1333 */
gsfan 0:2f6062c6d018 1334 return c;
gsfan 0:2f6062c6d018 1335 }
gsfan 0:2f6062c6d018 1336
gsfan 0:2f6062c6d018 1337 void GSwifi::putc(char c) {
gsfan 1:b127c6c5241d 1338 _gs_putc(c);
gsfan 0:2f6062c6d018 1339 }
gsfan 0:2f6062c6d018 1340
gsfan 0:2f6062c6d018 1341 int GSwifi::readable() {
gsfan 0:2f6062c6d018 1342 return _buf_cmd.use();
gsfan 0:2f6062c6d018 1343 // return _buf_cmd.use() || (_gs_sock[0].data != NULL && _gs_sock[0].data->use());
gsfan 0:2f6062c6d018 1344 }