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

Dependents:   GSwifi_httpd GSwifi_websocket GSwifi_tcpclient GSwifi_tcpserver ... more

Fork of GSwifi 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/

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

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

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

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

Committer:
gsfan
Date:
Mon Jul 09 14:36:06 2012 +0000
Revision:
0:2f6062c6d018
Child:
1:b127c6c5241d

        

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 0:2f6062c6d018 16
gsfan 0:2f6062c6d018 17
gsfan 0:2f6062c6d018 18 GSwifi::GSwifi (PinName p_tx, PinName p_rx) : _gs(p_tx, p_rx), _buf_cmd(GS_CMD_SIZE) {
gsfan 0:2f6062c6d018 19 _connect = false;
gsfan 0:2f6062c6d018 20 _status = GSSTAT_READY;
gsfan 0:2f6062c6d018 21 _escape = 0;
gsfan 0:2f6062c6d018 22 _gs_mode = GSMODE_COMMAND;
gsfan 0:2f6062c6d018 23
gsfan 0:2f6062c6d018 24 _gs.baud(GS_BAUD);
gsfan 0:2f6062c6d018 25 _gs.attach(this, &GSwifi::isr_recv, Serial::RxIrq);
gsfan 0:2f6062c6d018 26 _rts = false;
gsfan 0:2f6062c6d018 27 }
gsfan 0:2f6062c6d018 28
gsfan 0:2f6062c6d018 29 GSwifi::GSwifi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts) : _gs(p_tx, p_rx), _buf_cmd(GS_CMD_SIZE) {
gsfan 0:2f6062c6d018 30 _connect = false;
gsfan 0:2f6062c6d018 31 _status = GSSTAT_READY;
gsfan 0:2f6062c6d018 32 _escape = 0;
gsfan 0:2f6062c6d018 33 _gs_mode = GSMODE_COMMAND;
gsfan 0:2f6062c6d018 34
gsfan 0:2f6062c6d018 35 _gs.baud(GS_BAUD);
gsfan 0:2f6062c6d018 36 _gs.attach(this, &GSwifi::isr_recv, Serial::RxIrq);
gsfan 0:2f6062c6d018 37
gsfan 0:2f6062c6d018 38 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
gsfan 0:2f6062c6d018 39 if (p_cts == p12) { // CTS input (P0_17)
gsfan 0:2f6062c6d018 40 LPC_UART1->MCR |= (1<<7); // CTSEN
gsfan 0:2f6062c6d018 41 LPC_PINCON->PINSEL1 &= ~(3 << 2);
gsfan 0:2f6062c6d018 42 LPC_PINCON->PINSEL1 |= (1 << 2); // UART CTS
gsfan 0:2f6062c6d018 43 }
gsfan 0:2f6062c6d018 44 if (p_rts == P0_22) { // RTS output (P0_22)
gsfan 0:2f6062c6d018 45 LPC_UART1->MCR |= (1<<6); // RTSEN
gsfan 0:2f6062c6d018 46 LPC_PINCON->PINSEL1 &= ~(3 << 12);
gsfan 0:2f6062c6d018 47 LPC_PINCON->PINSEL1 |= (1 << 12); // UART RTS
gsfan 0:2f6062c6d018 48 _rts = true;
gsfan 0:2f6062c6d018 49 } else {
gsfan 0:2f6062c6d018 50 _rts = false;
gsfan 0:2f6062c6d018 51 }
gsfan 0:2f6062c6d018 52 #elif defined(TARGET_LPC11U24)
gsfan 0:2f6062c6d018 53 if (p_cts == p21) { // CTS input (P0_7)
gsfan 0:2f6062c6d018 54 LPC_USART->MCR = (1<<7); // CTSEN
gsfan 0:2f6062c6d018 55 LPC_IOCON->PIO0_7 &= ~0x07;
gsfan 0:2f6062c6d018 56 LPC_IOCON->PIO0_7 |= 0x01; // UART CTS
gsfan 0:2f6062c6d018 57 }
gsfan 0:2f6062c6d018 58 if (p_rts == p22) { // RTS output (P0_17)
gsfan 0:2f6062c6d018 59 LPC_USART->MCR = (1<<6); // RTSEN
gsfan 0:2f6062c6d018 60 LPC_IOCON->PIO0_17 &= ~0x07;
gsfan 0:2f6062c6d018 61 LPC_IOCON->PIO0_17 |= 0x01; // UART RTS
gsfan 0:2f6062c6d018 62 _rts = true;
gsfan 0:2f6062c6d018 63 } else {
gsfan 0:2f6062c6d018 64 _rts = false;
gsfan 0:2f6062c6d018 65 }
gsfan 0:2f6062c6d018 66 #endif
gsfan 0:2f6062c6d018 67 }
gsfan 0:2f6062c6d018 68
gsfan 0:2f6062c6d018 69 // uart interrupt
gsfan 0:2f6062c6d018 70 void GSwifi::isr_recv () {
gsfan 0:2f6062c6d018 71 static int len, mode;
gsfan 0:2f6062c6d018 72 static char tmp[20];
gsfan 0:2f6062c6d018 73 char flg, dat;
gsfan 0:2f6062c6d018 74
gsfan 0:2f6062c6d018 75 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
gsfan 0:2f6062c6d018 76 flg = LPC_UART1->LSR;
gsfan 0:2f6062c6d018 77 #elif defined(TARGET_LPC11U24)
gsfan 0:2f6062c6d018 78 flg = LPC_USART->LSR;
gsfan 0:2f6062c6d018 79 #endif
gsfan 0:2f6062c6d018 80 dat = _gs.getc();
gsfan 0:2f6062c6d018 81
gsfan 0:2f6062c6d018 82 if (flg & ((1 << 7)|(1 << 3)|(1 << 4))) return;
gsfan 0:2f6062c6d018 83 // DBG("%02x ", dat);
gsfan 0:2f6062c6d018 84
gsfan 0:2f6062c6d018 85 switch (_gs_mode) {
gsfan 0:2f6062c6d018 86 case GSMODE_COMMAND: // command responce
gsfan 0:2f6062c6d018 87 if (_escape) {
gsfan 0:2f6062c6d018 88 // esc
gsfan 0:2f6062c6d018 89 switch (dat) {
gsfan 0:2f6062c6d018 90 case 'O':
gsfan 0:2f6062c6d018 91 DBG("ok\r\n");
gsfan 0:2f6062c6d018 92 _gs_ok = 1;
gsfan 0:2f6062c6d018 93 break;
gsfan 0:2f6062c6d018 94 case 'F':
gsfan 0:2f6062c6d018 95 DBG("failure\r\n");
gsfan 0:2f6062c6d018 96 _gs_failure = 1;
gsfan 0:2f6062c6d018 97 break;
gsfan 0:2f6062c6d018 98 case 'S':
gsfan 0:2f6062c6d018 99 DBG("GSMODE_DATA_RX\r\n");
gsfan 0:2f6062c6d018 100 _gs_mode = GSMODE_DATA_RX;
gsfan 0:2f6062c6d018 101 mode = 0;
gsfan 0:2f6062c6d018 102 break;
gsfan 0:2f6062c6d018 103 case 'u':
gsfan 0:2f6062c6d018 104 DBG("GSMODE_DATA_RXUDP\r\n");
gsfan 0:2f6062c6d018 105 _gs_mode = GSMODE_DATA_RXUDP;
gsfan 0:2f6062c6d018 106 mode = 0;
gsfan 0:2f6062c6d018 107 break;
gsfan 0:2f6062c6d018 108 case 'Z':
gsfan 0:2f6062c6d018 109 case 'H':
gsfan 0:2f6062c6d018 110 DBG("GSMODE_DATA_RX_BULK\r\n");
gsfan 0:2f6062c6d018 111 _gs_mode = GSMODE_DATA_RX_BULK;
gsfan 0:2f6062c6d018 112 mode = 0;
gsfan 0:2f6062c6d018 113 break;
gsfan 0:2f6062c6d018 114 case 'y':
gsfan 0:2f6062c6d018 115 DBG("GSMODE_DATA_RXUDP_BULK\r\n");
gsfan 0:2f6062c6d018 116 _gs_mode = GSMODE_DATA_RXUDP_BULK;
gsfan 0:2f6062c6d018 117 mode = 0;
gsfan 0:2f6062c6d018 118 break;
gsfan 0:2f6062c6d018 119 default:
gsfan 0:2f6062c6d018 120 DBG("unknown [ESC] %02x\r\n", dat);
gsfan 0:2f6062c6d018 121 break;
gsfan 0:2f6062c6d018 122 }
gsfan 0:2f6062c6d018 123 _escape = 0;
gsfan 0:2f6062c6d018 124 } else {
gsfan 0:2f6062c6d018 125 if (dat == 0x1b) {
gsfan 0:2f6062c6d018 126 _escape = 1;
gsfan 0:2f6062c6d018 127 } else
gsfan 0:2f6062c6d018 128 if (dat != '\r') {
gsfan 0:2f6062c6d018 129 // command
gsfan 0:2f6062c6d018 130 _buf_cmd.put(dat);
gsfan 0:2f6062c6d018 131 if (dat == '\n') _gs_enter ++;
gsfan 0:2f6062c6d018 132 }
gsfan 0:2f6062c6d018 133 }
gsfan 0:2f6062c6d018 134 break;
gsfan 0:2f6062c6d018 135
gsfan 0:2f6062c6d018 136 case GSMODE_DATA_RX:
gsfan 0:2f6062c6d018 137 case GSMODE_DATA_RXUDP:
gsfan 0:2f6062c6d018 138 if (mode == 0) {
gsfan 0:2f6062c6d018 139 // cid
gsfan 0:2f6062c6d018 140 _cid = x2i(dat);
gsfan 0:2f6062c6d018 141 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 142 mode ++;
gsfan 0:2f6062c6d018 143 if (_gs_mode == GSMODE_DATA_RX) {
gsfan 0:2f6062c6d018 144 mode = 3;
gsfan 0:2f6062c6d018 145 }
gsfan 0:2f6062c6d018 146 len = 0;
gsfan 0:2f6062c6d018 147 } else
gsfan 0:2f6062c6d018 148 if (mode == 1) {
gsfan 0:2f6062c6d018 149 // ip
gsfan 0:2f6062c6d018 150 if ((dat < '0' || dat > '9') && dat != '.') {
gsfan 0:2f6062c6d018 151 int ip1, ip2, ip3, ip4;
gsfan 0:2f6062c6d018 152 tmp[len] = 0;
gsfan 0:2f6062c6d018 153 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 154 _from.setIp(IpAddr(ip1, ip2, ip3, ip4));
gsfan 0:2f6062c6d018 155 mode ++;
gsfan 0:2f6062c6d018 156 len = 0;
gsfan 0:2f6062c6d018 157 break;
gsfan 0:2f6062c6d018 158 }
gsfan 0:2f6062c6d018 159 tmp[len] = dat;
gsfan 0:2f6062c6d018 160 len ++;
gsfan 0:2f6062c6d018 161 } else
gsfan 0:2f6062c6d018 162 if (mode == 2) {
gsfan 0:2f6062c6d018 163 // port
gsfan 0:2f6062c6d018 164 if (dat < '0' || dat > '9') {
gsfan 0:2f6062c6d018 165 tmp[len] = 0;
gsfan 0:2f6062c6d018 166 _from.setPort(atoi(tmp));
gsfan 0:2f6062c6d018 167 mode ++;
gsfan 0:2f6062c6d018 168 len = 0;
gsfan 0:2f6062c6d018 169 break;
gsfan 0:2f6062c6d018 170 }
gsfan 0:2f6062c6d018 171 tmp[len] = dat;
gsfan 0:2f6062c6d018 172 len ++;
gsfan 0:2f6062c6d018 173 } else
gsfan 0:2f6062c6d018 174 if (_escape) {
gsfan 0:2f6062c6d018 175 // esc
gsfan 0:2f6062c6d018 176 switch (dat) {
gsfan 0:2f6062c6d018 177 case 'E':
gsfan 0:2f6062c6d018 178 DBG("recv ascii %d\r\n", _cid);
gsfan 0:2f6062c6d018 179 _gs_sock[_cid].received = 1;
gsfan 0:2f6062c6d018 180 _gs_mode = GSMODE_COMMAND;
gsfan 0:2f6062c6d018 181 // recv interrupt
gsfan 0:2f6062c6d018 182 if (_gs_sock[_cid].protocol == GSPROT_HTTPGET && _gs_sock[_cid].onGsReceive != NULL) {
gsfan 0:2f6062c6d018 183 _gs_sock[_cid].onGsReceive(_cid, 0, _gs_sock[_cid].data->use());
gsfan 0:2f6062c6d018 184 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 185 }
gsfan 0:2f6062c6d018 186 break;
gsfan 0:2f6062c6d018 187 default:
gsfan 0:2f6062c6d018 188 DBG("unknown <ESC> %02x\r\n", dat);
gsfan 0:2f6062c6d018 189 break;
gsfan 0:2f6062c6d018 190 }
gsfan 0:2f6062c6d018 191 _escape = 0;
gsfan 0:2f6062c6d018 192 } else {
gsfan 0:2f6062c6d018 193 if (dat == 0x1b) {
gsfan 0:2f6062c6d018 194 _escape = 1;
gsfan 0:2f6062c6d018 195 } else {
gsfan 0:2f6062c6d018 196 // data
gsfan 0:2f6062c6d018 197 _gs_sock[_cid].data->put(dat);
gsfan 0:2f6062c6d018 198 len ++;
gsfan 0:2f6062c6d018 199 }
gsfan 0:2f6062c6d018 200 }
gsfan 0:2f6062c6d018 201 break;
gsfan 0:2f6062c6d018 202
gsfan 0:2f6062c6d018 203 case GSMODE_DATA_RX_BULK:
gsfan 0:2f6062c6d018 204 case GSMODE_DATA_RXUDP_BULK:
gsfan 0:2f6062c6d018 205 if (mode == 0) {
gsfan 0:2f6062c6d018 206 // cid
gsfan 0:2f6062c6d018 207 _cid = x2i(dat);
gsfan 0:2f6062c6d018 208 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 209 mode ++;
gsfan 0:2f6062c6d018 210 if (_gs_mode == GSMODE_DATA_RX_BULK) {
gsfan 0:2f6062c6d018 211 mode = 3;
gsfan 0:2f6062c6d018 212 }
gsfan 0:2f6062c6d018 213 len = 0;
gsfan 0:2f6062c6d018 214 } else
gsfan 0:2f6062c6d018 215 if (mode == 1) {
gsfan 0:2f6062c6d018 216 // ip
gsfan 0:2f6062c6d018 217 if ((dat < '0' || dat > '9') && dat != '.') {
gsfan 0:2f6062c6d018 218 int ip1, ip2, ip3, ip4;
gsfan 0:2f6062c6d018 219 tmp[len] = 0;
gsfan 0:2f6062c6d018 220 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 221 _from.setIp(IpAddr(ip1, ip2, ip3, ip4));
gsfan 0:2f6062c6d018 222 mode ++;
gsfan 0:2f6062c6d018 223 len = 0;
gsfan 0:2f6062c6d018 224 break;
gsfan 0:2f6062c6d018 225 }
gsfan 0:2f6062c6d018 226 tmp[len] = dat;
gsfan 0:2f6062c6d018 227 len ++;
gsfan 0:2f6062c6d018 228 } else
gsfan 0:2f6062c6d018 229 if (mode == 2) {
gsfan 0:2f6062c6d018 230 // port
gsfan 0:2f6062c6d018 231 if (dat < '0' || dat > '9') {
gsfan 0:2f6062c6d018 232 tmp[len] = 0;
gsfan 0:2f6062c6d018 233 _from.setPort(atoi(tmp));
gsfan 0:2f6062c6d018 234 mode ++;
gsfan 0:2f6062c6d018 235 len = 0;
gsfan 0:2f6062c6d018 236 break;
gsfan 0:2f6062c6d018 237 }
gsfan 0:2f6062c6d018 238 tmp[len] = dat;
gsfan 0:2f6062c6d018 239 len ++;
gsfan 0:2f6062c6d018 240 } else
gsfan 0:2f6062c6d018 241 if (mode == 3) {
gsfan 0:2f6062c6d018 242 // length
gsfan 0:2f6062c6d018 243 tmp[len] = dat;
gsfan 0:2f6062c6d018 244 len ++;
gsfan 0:2f6062c6d018 245 if (len >= 4) {
gsfan 0:2f6062c6d018 246 tmp[len] = 0;
gsfan 0:2f6062c6d018 247 len = atoi(tmp);
gsfan 0:2f6062c6d018 248 mode ++;
gsfan 0:2f6062c6d018 249 break;
gsfan 0:2f6062c6d018 250 }
gsfan 0:2f6062c6d018 251 } else
gsfan 0:2f6062c6d018 252 if (mode == 4) {
gsfan 0:2f6062c6d018 253 // data
gsfan 0:2f6062c6d018 254 _gs_sock[_cid].data->put(dat);
gsfan 0:2f6062c6d018 255 len --;
gsfan 0:2f6062c6d018 256 if (len == 0) {
gsfan 0:2f6062c6d018 257 DBG("recv binary %d\r\n", _cid);
gsfan 0:2f6062c6d018 258 _gs_sock[_cid].received = 1;
gsfan 0:2f6062c6d018 259 _escape = 0;
gsfan 0:2f6062c6d018 260 _gs_mode = GSMODE_COMMAND;
gsfan 0:2f6062c6d018 261 // recv interrupt
gsfan 0:2f6062c6d018 262 if (_gs_sock[_cid].protocol == GSPROT_HTTPGET && _gs_sock[_cid].onGsReceive != NULL) {
gsfan 0:2f6062c6d018 263 _gs_sock[_cid].onGsReceive(_cid, 0, _gs_sock[_cid].data->use());
gsfan 0:2f6062c6d018 264 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 265 }
gsfan 0:2f6062c6d018 266 }
gsfan 0:2f6062c6d018 267 }
gsfan 0:2f6062c6d018 268 break;
gsfan 0:2f6062c6d018 269
gsfan 0:2f6062c6d018 270 }
gsfan 0:2f6062c6d018 271 }
gsfan 0:2f6062c6d018 272
gsfan 0:2f6062c6d018 273 int GSwifi::command (char *cmd, GSRESPONCE res, int timeout) {
gsfan 0:2f6062c6d018 274 int i;
gsfan 0:2f6062c6d018 275
gsfan 0:2f6062c6d018 276 if (! cmd) {
gsfan 0:2f6062c6d018 277 // dummy CR+LF
gsfan 0:2f6062c6d018 278 _gs.printf("\r\n");
gsfan 0:2f6062c6d018 279 wait_ms(100);
gsfan 0:2f6062c6d018 280 while (_gs.readable()) _gs.getc(); // dummy read
gsfan 0:2f6062c6d018 281 return 0;
gsfan 0:2f6062c6d018 282 }
gsfan 0:2f6062c6d018 283
gsfan 0:2f6062c6d018 284 _buf_cmd.clear();
gsfan 0:2f6062c6d018 285 _gs_ok = 0;
gsfan 0:2f6062c6d018 286 _gs_failure = 0;
gsfan 0:2f6062c6d018 287 _gs_enter = 0;
gsfan 0:2f6062c6d018 288 for (i = 0; i < strlen(cmd); i ++) {
gsfan 0:2f6062c6d018 289 _gs.putc(cmd[i]);
gsfan 0:2f6062c6d018 290 }
gsfan 0:2f6062c6d018 291 _gs.putc('\r');
gsfan 0:2f6062c6d018 292 _gs.putc('\n');
gsfan 0:2f6062c6d018 293 DBG("command: %s\r\n", cmd);
gsfan 0:2f6062c6d018 294 if (strlen(cmd) == 0) return 0;
gsfan 0:2f6062c6d018 295 wait_ms(50);
gsfan 0:2f6062c6d018 296 if (timeout) {
gsfan 0:2f6062c6d018 297 return cmdResponse(res, timeout);
gsfan 0:2f6062c6d018 298 } else {
gsfan 0:2f6062c6d018 299 return 0;
gsfan 0:2f6062c6d018 300 }
gsfan 0:2f6062c6d018 301 }
gsfan 0:2f6062c6d018 302
gsfan 0:2f6062c6d018 303 int GSwifi::cmdResponse (GSRESPONCE res, int ms) {
gsfan 0:2f6062c6d018 304 int i, n = 0, flg = 0;
gsfan 0:2f6062c6d018 305 char buf[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 306 Timer timeout;
gsfan 0:2f6062c6d018 307
gsfan 0:2f6062c6d018 308 timeout.start();
gsfan 0:2f6062c6d018 309 for (;;) {
gsfan 0:2f6062c6d018 310 // recv response
gsfan 0:2f6062c6d018 311 i = 0;
gsfan 0:2f6062c6d018 312 while (i < sizeof(buf)) {
gsfan 0:2f6062c6d018 313 if (_buf_cmd.use()) {
gsfan 0:2f6062c6d018 314 _buf_cmd.get(&buf[i]);
gsfan 0:2f6062c6d018 315 if (buf[i] == '\n') {
gsfan 0:2f6062c6d018 316 break;
gsfan 0:2f6062c6d018 317 }
gsfan 0:2f6062c6d018 318 i ++;
gsfan 0:2f6062c6d018 319 }
gsfan 0:2f6062c6d018 320 if (timeout.read_ms() > ms) {
gsfan 0:2f6062c6d018 321 timeout.stop();
gsfan 0:2f6062c6d018 322 DBG("timeout\r\n");
gsfan 0:2f6062c6d018 323 return -1;
gsfan 0:2f6062c6d018 324 }
gsfan 0:2f6062c6d018 325 }
gsfan 0:2f6062c6d018 326 _gs_enter = 0;
gsfan 0:2f6062c6d018 327 if (i == 0) continue;
gsfan 0:2f6062c6d018 328 buf[i] = 0;
gsfan 0:2f6062c6d018 329 DBG("response: %s\r\n", buf);
gsfan 0:2f6062c6d018 330 timeout.stop();
gsfan 0:2f6062c6d018 331
gsfan 0:2f6062c6d018 332 if (strcmp(buf, "OK") == 0) {
gsfan 0:2f6062c6d018 333 _gs_ok = 1;
gsfan 0:2f6062c6d018 334 } else
gsfan 0:2f6062c6d018 335 if (strncmp(buf, "ERROR", 5) == 0) {
gsfan 0:2f6062c6d018 336 _gs_failure = 1;
gsfan 0:2f6062c6d018 337 }
gsfan 0:2f6062c6d018 338
gsfan 0:2f6062c6d018 339 switch(res) {
gsfan 0:2f6062c6d018 340 case GSRES_NORMAL:
gsfan 0:2f6062c6d018 341 flg = 1;
gsfan 0:2f6062c6d018 342 break;
gsfan 0:2f6062c6d018 343 case GSRES_WPS:
gsfan 0:2f6062c6d018 344 if (n == 0 && strncmp(buf, "SSID", 4) == 0) {
gsfan 0:2f6062c6d018 345 n ++;
gsfan 0:2f6062c6d018 346 } else
gsfan 0:2f6062c6d018 347 if (n == 1 && strncmp(buf, "CHANNEL", 7) == 0) {
gsfan 0:2f6062c6d018 348 n ++;
gsfan 0:2f6062c6d018 349 } else
gsfan 0:2f6062c6d018 350 if (n == 2 && strncmp(buf, "PASSPHRASE", 10) == 0) {
gsfan 0:2f6062c6d018 351 n ++;
gsfan 0:2f6062c6d018 352 flg = 1;
gsfan 0:2f6062c6d018 353 }
gsfan 0:2f6062c6d018 354 break;
gsfan 0:2f6062c6d018 355 case GSRES_CONNECT:
gsfan 0:2f6062c6d018 356 if (strncmp(buf, "CONNECT", 7) == 0) {
gsfan 0:2f6062c6d018 357 _cid = x2i(buf[8]);
gsfan 0:2f6062c6d018 358 flg = 1;
gsfan 0:2f6062c6d018 359 }
gsfan 0:2f6062c6d018 360 break;
gsfan 0:2f6062c6d018 361 case GSRES_DHCP:
gsfan 0:2f6062c6d018 362 if (n == 0 && strstr(buf, "SubNet") && strstr(buf, "Gateway")) {
gsfan 0:2f6062c6d018 363 n ++;
gsfan 0:2f6062c6d018 364 } else
gsfan 0:2f6062c6d018 365 if (n == 1) {
gsfan 0:2f6062c6d018 366 int ip1, ip2, ip3, ip4;
gsfan 0:2f6062c6d018 367 char *tmp = buf + 1;
gsfan 0:2f6062c6d018 368 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 369 _ipaddr = IpAddr(ip1, ip2, ip3, ip4);
gsfan 0:2f6062c6d018 370 tmp = strstr(tmp, ":") + 2;
gsfan 0:2f6062c6d018 371 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 372 _netmask = IpAddr(ip1, ip2, ip3, ip4);
gsfan 0:2f6062c6d018 373 tmp = strstr(tmp, ":") + 2;
gsfan 0:2f6062c6d018 374 sscanf(tmp, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 375 _gateway = IpAddr(ip1, ip2, ip3, ip4);
gsfan 0:2f6062c6d018 376 n ++;
gsfan 0:2f6062c6d018 377 flg = 1;
gsfan 0:2f6062c6d018 378 }
gsfan 0:2f6062c6d018 379 break;
gsfan 0:2f6062c6d018 380 case GSRES_MACADDRESS:
gsfan 0:2f6062c6d018 381 if (strncmp(buf, "00:", 3) == 0) {
gsfan 0:2f6062c6d018 382 int mac1, mac2, mac3, mac4, mac5, mac6;
gsfan 0:2f6062c6d018 383 sscanf(buf, "%x:%x:%x:%x:%x:%x", &mac1, &mac2, &mac3, &mac4, &mac5, &mac6);
gsfan 0:2f6062c6d018 384 mac[0] = mac1;
gsfan 0:2f6062c6d018 385 mac[1] = mac2;
gsfan 0:2f6062c6d018 386 mac[2] = mac3;
gsfan 0:2f6062c6d018 387 mac[3] = mac4;
gsfan 0:2f6062c6d018 388 mac[4] = mac5;
gsfan 0:2f6062c6d018 389 mac[5] = mac6;
gsfan 0:2f6062c6d018 390 flg = 1;
gsfan 0:2f6062c6d018 391 }
gsfan 0:2f6062c6d018 392 break;
gsfan 0:2f6062c6d018 393 case GSRES_DNSLOOKUP:
gsfan 0:2f6062c6d018 394 if (strncmp(buf, "IP:", 3) == 0) {
gsfan 0:2f6062c6d018 395 int ip1, ip2, ip3, ip4;
gsfan 0:2f6062c6d018 396 sscanf(&buf[3], "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
gsfan 0:2f6062c6d018 397 _resolv = IpAddr(ip1, ip2, ip3, ip4);
gsfan 0:2f6062c6d018 398 flg = 1;
gsfan 0:2f6062c6d018 399 }
gsfan 0:2f6062c6d018 400 break;
gsfan 0:2f6062c6d018 401 case GSRES_HTTP:
gsfan 0:2f6062c6d018 402 if (i == 1 && buf[0] >= '0' && buf[0] <= 'F') {
gsfan 0:2f6062c6d018 403 _cid = x2i(buf[8]);
gsfan 0:2f6062c6d018 404 flg = 1;
gsfan 0:2f6062c6d018 405 }
gsfan 0:2f6062c6d018 406 break;
gsfan 0:2f6062c6d018 407 }
gsfan 0:2f6062c6d018 408
gsfan 0:2f6062c6d018 409 if ((flg && _gs_ok) || _gs_failure) break;
gsfan 0:2f6062c6d018 410 timeout.reset();
gsfan 0:2f6062c6d018 411 timeout.start();
gsfan 0:2f6062c6d018 412 }
gsfan 0:2f6062c6d018 413
gsfan 0:2f6062c6d018 414 if (_gs_failure || flg == 0) {
gsfan 0:2f6062c6d018 415 return -1;
gsfan 0:2f6062c6d018 416 } else {
gsfan 0:2f6062c6d018 417 return 0;
gsfan 0:2f6062c6d018 418 }
gsfan 0:2f6062c6d018 419 }
gsfan 0:2f6062c6d018 420
gsfan 0:2f6062c6d018 421 int GSwifi::connect (GSSECURITY sec, char *ssid, char *pass, int dhcp) {
gsfan 0:2f6062c6d018 422 int r;
gsfan 0:2f6062c6d018 423 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 424
gsfan 0:2f6062c6d018 425 if (_connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 426
gsfan 0:2f6062c6d018 427 command(NULL, GSRES_NORMAL);
gsfan 0:2f6062c6d018 428 if (command("ATE0", GSRES_NORMAL)) return -1;
gsfan 0:2f6062c6d018 429 if (_rts) {
gsfan 0:2f6062c6d018 430 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 431 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 432 }
gsfan 0:2f6062c6d018 433 command("AT+NMAC=?", GSRES_MACADDRESS);
gsfan 0:2f6062c6d018 434 #ifdef GS_BULK
gsfan 0:2f6062c6d018 435 command("AT+BDATA=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 436 #endif
gsfan 0:2f6062c6d018 437
gsfan 0:2f6062c6d018 438 disconnect();
gsfan 0:2f6062c6d018 439 command("AT+WM=0", GSRES_NORMAL); // infrastructure
gsfan 0:2f6062c6d018 440 wait_ms(100);
gsfan 0:2f6062c6d018 441 if (dhcp) {
gsfan 0:2f6062c6d018 442 command("AT+NDHCP=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 443 } else {
gsfan 0:2f6062c6d018 444 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 445 }
gsfan 0:2f6062c6d018 446
gsfan 0:2f6062c6d018 447 switch (sec) {
gsfan 0:2f6062c6d018 448 case GSSEC_NONE:
gsfan 0:2f6062c6d018 449 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 450 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 451 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 452 break;
gsfan 0:2f6062c6d018 453 case GSSEC_OPEN:
gsfan 0:2f6062c6d018 454 case GSSEC_WEP:
gsfan 0:2f6062c6d018 455 sprintf(cmd, "AT+WAUTH=%d", sec);
gsfan 0:2f6062c6d018 456 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 457 sprintf(cmd, "AT+WWEP1=%s", pass);
gsfan 0:2f6062c6d018 458 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 459 wait_ms(100);
gsfan 0:2f6062c6d018 460 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 461 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 462 break;
gsfan 0:2f6062c6d018 463 case GSSEC_WPA_PSK:
gsfan 0:2f6062c6d018 464 case GSSEC_WPA2_PSK:
gsfan 0:2f6062c6d018 465 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 466 // sprintf(cmd, "AT+WWPA=%s", pass);
gsfan 0:2f6062c6d018 467 // command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 468 sprintf(cmd, "AT+WPAPSK=%s,%s", ssid, pass);
gsfan 0:2f6062c6d018 469 command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 470 wait_ms(100);
gsfan 0:2f6062c6d018 471 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 472 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 473 break;
gsfan 0:2f6062c6d018 474 case GSSEC_WPS_BUTTON:
gsfan 0:2f6062c6d018 475 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 476 r = command("AT+WWPS=1", GSRES_WPS, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 477 if (r) break;
gsfan 0:2f6062c6d018 478 if (dhcp) {
gsfan 0:2f6062c6d018 479 r = command("AT+NDHCP=1", GSRES_DHCP, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 480 }
gsfan 0:2f6062c6d018 481 break;
gsfan 0:2f6062c6d018 482 }
gsfan 0:2f6062c6d018 483
gsfan 0:2f6062c6d018 484 if (r == 0) _connect = true;
gsfan 0:2f6062c6d018 485 return r;
gsfan 0:2f6062c6d018 486 }
gsfan 0:2f6062c6d018 487
gsfan 0:2f6062c6d018 488 int GSwifi::adhock (GSSECURITY sec, char *ssid, char *pass, IpAddr ipaddr, IpAddr netmask) {
gsfan 0:2f6062c6d018 489 int r;
gsfan 0:2f6062c6d018 490 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 491
gsfan 0:2f6062c6d018 492 if (_connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 493
gsfan 0:2f6062c6d018 494 command(NULL, GSRES_NORMAL);
gsfan 0:2f6062c6d018 495 if (command("ATE0", GSRES_NORMAL)) return -1;
gsfan 0:2f6062c6d018 496 if (_rts) {
gsfan 0:2f6062c6d018 497 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 498 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 499 }
gsfan 0:2f6062c6d018 500 disconnect();
gsfan 0:2f6062c6d018 501 command("AT+NMAC=?", GSRES_MACADDRESS);
gsfan 0:2f6062c6d018 502 #ifdef GS_BULK
gsfan 0:2f6062c6d018 503 command("AT+BDATA=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 504 #endif
gsfan 0:2f6062c6d018 505
gsfan 0:2f6062c6d018 506 command("AT+WM=1", GSRES_NORMAL); // adhock
gsfan 0:2f6062c6d018 507 wait_ms(100);
gsfan 0:2f6062c6d018 508 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 509 setAddress(ipaddr, netmask, ipaddr, ipaddr);
gsfan 0:2f6062c6d018 510
gsfan 0:2f6062c6d018 511 switch (sec) {
gsfan 0:2f6062c6d018 512 case GSSEC_NONE:
gsfan 0:2f6062c6d018 513 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 514 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 515 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 516 break;
gsfan 0:2f6062c6d018 517 case GSSEC_OPEN:
gsfan 0:2f6062c6d018 518 case GSSEC_WEP:
gsfan 0:2f6062c6d018 519 sprintf(cmd, "AT+WAUTH=%d", sec);
gsfan 0:2f6062c6d018 520 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 521 sprintf(cmd, "AT+WWEP1=%s", pass);
gsfan 0:2f6062c6d018 522 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 523 wait_ms(100);
gsfan 0:2f6062c6d018 524 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 525 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 526 break;
gsfan 0:2f6062c6d018 527 default:
gsfan 0:2f6062c6d018 528 DBG("Can't use security\r\n");
gsfan 0:2f6062c6d018 529 r = -1;
gsfan 0:2f6062c6d018 530 break;
gsfan 0:2f6062c6d018 531 }
gsfan 0:2f6062c6d018 532
gsfan 0:2f6062c6d018 533 if (r == 0) _connect = true;
gsfan 0:2f6062c6d018 534 return r;
gsfan 0:2f6062c6d018 535 }
gsfan 0:2f6062c6d018 536
gsfan 0:2f6062c6d018 537 int GSwifi::limitedap (GSSECURITY sec, char *ssid, char *pass, IpAddr ipaddr, IpAddr netmask) {
gsfan 0:2f6062c6d018 538 int r;
gsfan 0:2f6062c6d018 539 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 540
gsfan 0:2f6062c6d018 541 if (_connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 542
gsfan 0:2f6062c6d018 543 command(NULL, GSRES_NORMAL);
gsfan 0:2f6062c6d018 544 if (command("ATE0", GSRES_NORMAL)) return -1;
gsfan 0:2f6062c6d018 545 if (_rts) {
gsfan 0:2f6062c6d018 546 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 547 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 548 }
gsfan 0:2f6062c6d018 549 disconnect();
gsfan 0:2f6062c6d018 550 command("AT+NMAC=?", GSRES_MACADDRESS);
gsfan 0:2f6062c6d018 551 #ifdef GS_BULK
gsfan 0:2f6062c6d018 552 command("AT+BDATA=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 553 #endif
gsfan 0:2f6062c6d018 554
gsfan 0:2f6062c6d018 555 command("AT+WM=2", GSRES_NORMAL); // limited ap
gsfan 0:2f6062c6d018 556 wait_ms(1000);
gsfan 0:2f6062c6d018 557 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 558 setAddress(ipaddr, netmask, ipaddr, ipaddr);
gsfan 0:2f6062c6d018 559 command("AT+DHCPSRVR=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 560 command("AT+DNS=1,gainspan", GSRES_NORMAL);
gsfan 0:2f6062c6d018 561
gsfan 0:2f6062c6d018 562 switch (sec) {
gsfan 0:2f6062c6d018 563 case GSSEC_NONE:
gsfan 0:2f6062c6d018 564 command("AT+WAUTH=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 565 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 566 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 567 break;
gsfan 0:2f6062c6d018 568 case GSSEC_OPEN:
gsfan 0:2f6062c6d018 569 case GSSEC_WEP:
gsfan 0:2f6062c6d018 570 sprintf(cmd, "AT+WAUTH=%d", sec);
gsfan 0:2f6062c6d018 571 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 572 sprintf(cmd, "AT+WWEP1=%s", pass);
gsfan 0:2f6062c6d018 573 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 574 wait_ms(100);
gsfan 0:2f6062c6d018 575 sprintf(cmd, "AT+WA=%s", ssid);
gsfan 0:2f6062c6d018 576 r = command(cmd, GSRES_NORMAL, GS_TIMEOUT2);
gsfan 0:2f6062c6d018 577 break;
gsfan 0:2f6062c6d018 578 default:
gsfan 0:2f6062c6d018 579 DBG("Can't use security\r\n");
gsfan 0:2f6062c6d018 580 r = -1;
gsfan 0:2f6062c6d018 581 break;
gsfan 0:2f6062c6d018 582 }
gsfan 0:2f6062c6d018 583
gsfan 0:2f6062c6d018 584 if (r == 0) _connect = true;
gsfan 0:2f6062c6d018 585 return r;
gsfan 0:2f6062c6d018 586 }
gsfan 0:2f6062c6d018 587
gsfan 0:2f6062c6d018 588 int GSwifi::disconnect () {
gsfan 0:2f6062c6d018 589 int i;
gsfan 0:2f6062c6d018 590
gsfan 0:2f6062c6d018 591 _connect = false;
gsfan 0:2f6062c6d018 592 for (i = 0; i < 16; i ++) {
gsfan 0:2f6062c6d018 593 _gs_sock[i].connect = false;
gsfan 0:2f6062c6d018 594 }
gsfan 0:2f6062c6d018 595 command("AT+NCLOSEALL", GSRES_NORMAL);
gsfan 0:2f6062c6d018 596 command("AT+WD", GSRES_NORMAL);
gsfan 0:2f6062c6d018 597 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 598 wait_ms(100);
gsfan 0:2f6062c6d018 599 return 0;
gsfan 0:2f6062c6d018 600 }
gsfan 0:2f6062c6d018 601
gsfan 0:2f6062c6d018 602 int GSwifi::setAddress () {
gsfan 0:2f6062c6d018 603
gsfan 0:2f6062c6d018 604 if (command("AT+NDHCP=1", GSRES_DHCP), GS_TIMEOUT2) return -1;
gsfan 0:2f6062c6d018 605 if (_ipaddr.isNull()) return -1;
gsfan 0:2f6062c6d018 606 return 0;
gsfan 0:2f6062c6d018 607 }
gsfan 0:2f6062c6d018 608
gsfan 0:2f6062c6d018 609 int GSwifi::setAddress (IpAddr ipaddr, IpAddr netmask, IpAddr gateway, IpAddr nameserver) {
gsfan 0:2f6062c6d018 610 int r;
gsfan 0:2f6062c6d018 611 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 612
gsfan 0:2f6062c6d018 613 command("AT+NDHCP=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 614 wait_ms(100);
gsfan 0:2f6062c6d018 615
gsfan 0:2f6062c6d018 616 sprintf(cmd, "AT+NSET=%d.%d.%d.%d,%d.%d.%d.%d,%d.%d.%d.%d",
gsfan 0:2f6062c6d018 617 ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3],
gsfan 0:2f6062c6d018 618 netmask[0], netmask[1], netmask[2], netmask[3],
gsfan 0:2f6062c6d018 619 gateway[0], gateway[1], gateway[2], gateway[3]);
gsfan 0:2f6062c6d018 620 r = command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 621 if (r) return -1;
gsfan 0:2f6062c6d018 622 _ipaddr = ipaddr;
gsfan 0:2f6062c6d018 623 _netmask = netmask;
gsfan 0:2f6062c6d018 624 _gateway = gateway;
gsfan 0:2f6062c6d018 625
gsfan 0:2f6062c6d018 626 if (ipaddr != nameserver) {
gsfan 0:2f6062c6d018 627 sprintf(cmd, "AT+DNSSET=%d.%d.%d.%d",
gsfan 0:2f6062c6d018 628 nameserver[0], nameserver[1], nameserver[2], nameserver[3]);
gsfan 0:2f6062c6d018 629 r = command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 630 }
gsfan 0:2f6062c6d018 631 return r;
gsfan 0:2f6062c6d018 632 }
gsfan 0:2f6062c6d018 633
gsfan 0:2f6062c6d018 634 int GSwifi::getHostByName (const char* name, IpAddr &addr) {
gsfan 0:2f6062c6d018 635 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 636
gsfan 0:2f6062c6d018 637 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 638
gsfan 0:2f6062c6d018 639 sprintf(cmd, "AT+DNSLOOKUP=%s", name);
gsfan 0:2f6062c6d018 640 if (command(cmd, GSRES_DNSLOOKUP)) return -1;
gsfan 0:2f6062c6d018 641
gsfan 0:2f6062c6d018 642 addr = _resolv;
gsfan 0:2f6062c6d018 643 return 0;
gsfan 0:2f6062c6d018 644 }
gsfan 0:2f6062c6d018 645
gsfan 0:2f6062c6d018 646 int GSwifi::getHostByName (Host &host) {
gsfan 0:2f6062c6d018 647 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 648
gsfan 0:2f6062c6d018 649 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 650
gsfan 0:2f6062c6d018 651 sprintf(cmd, "AT+DNSLOOKUP=%s", host.getName());
gsfan 0:2f6062c6d018 652 if (command(cmd, GSRES_DNSLOOKUP)) return -1;
gsfan 0:2f6062c6d018 653
gsfan 0:2f6062c6d018 654 host.setIp(_resolv);
gsfan 0:2f6062c6d018 655 return 0;
gsfan 0:2f6062c6d018 656 }
gsfan 0:2f6062c6d018 657
gsfan 0:2f6062c6d018 658 int GSwifi::setRFPower (int power) {
gsfan 0:2f6062c6d018 659 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 660
gsfan 0:2f6062c6d018 661 if (power < 0 || power > 7) return -1;
gsfan 0:2f6062c6d018 662
gsfan 0:2f6062c6d018 663 sprintf(cmd, "AT+WP=%d", power);
gsfan 0:2f6062c6d018 664 return command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 665 }
gsfan 0:2f6062c6d018 666
gsfan 0:2f6062c6d018 667 int GSwifi::powerSave (int beacon, int association) {
gsfan 0:2f6062c6d018 668 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 669
gsfan 0:2f6062c6d018 670 if (_status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 671
gsfan 0:2f6062c6d018 672 sprintf(cmd, "AT+WAPSM=%d", association);
gsfan 0:2f6062c6d018 673 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 674 if (beacon) {
gsfan 0:2f6062c6d018 675 sprintf(cmd, "AT+WRXPS=1,%d", beacon);
gsfan 0:2f6062c6d018 676 } else {
gsfan 0:2f6062c6d018 677 strcpy(cmd, "AT+WRXPS=0");
gsfan 0:2f6062c6d018 678 }
gsfan 0:2f6062c6d018 679 return command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 680 }
gsfan 0:2f6062c6d018 681
gsfan 0:2f6062c6d018 682 int GSwifi::standby (int msec) {
gsfan 0:2f6062c6d018 683 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 684
gsfan 0:2f6062c6d018 685 if (_status != GSSTAT_READY && _status != GSSTAT_WAKEUP) return -1;
gsfan 0:2f6062c6d018 686
gsfan 0:2f6062c6d018 687 if (_status != GSSTAT_WAKEUP) {
gsfan 0:2f6062c6d018 688 command("AT+WRXACTIVE=0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 689 command("AT+STORENWCONN", GSRES_NORMAL);
gsfan 0:2f6062c6d018 690 } else {
gsfan 0:2f6062c6d018 691 command("ATE0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 692 if (_rts) {
gsfan 0:2f6062c6d018 693 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 694 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 695 }
gsfan 0:2f6062c6d018 696 }
gsfan 0:2f6062c6d018 697 _status = GSSTAT_STANDBY;
gsfan 0:2f6062c6d018 698 sprintf(cmd, "AT+PSSTBY=%d,0,0,0", msec); // go standby
gsfan 0:2f6062c6d018 699 return command(cmd, GSRES_NORMAL, 0);
gsfan 0:2f6062c6d018 700 }
gsfan 0:2f6062c6d018 701
gsfan 0:2f6062c6d018 702 int GSwifi::wakeup () {
gsfan 0:2f6062c6d018 703
gsfan 0:2f6062c6d018 704 if (_status == GSSTAT_WAKEUP) {
gsfan 0:2f6062c6d018 705 _status = GSSTAT_READY;
gsfan 0:2f6062c6d018 706 command("ATE0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 707 if (_rts) {
gsfan 0:2f6062c6d018 708 command("AT&K0", GSRES_NORMAL);
gsfan 0:2f6062c6d018 709 command("AT&R1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 710 }
gsfan 0:2f6062c6d018 711 #ifdef GS_BULK
gsfan 0:2f6062c6d018 712 command("AT+BDATA=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 713 #endif
gsfan 0:2f6062c6d018 714 command("AT+RESTORENWCONN", GSRES_NORMAL);
gsfan 0:2f6062c6d018 715 wait_ms(100);
gsfan 0:2f6062c6d018 716 return command("AT+WRXACTIVE=1", GSRES_NORMAL);
gsfan 0:2f6062c6d018 717 } else
gsfan 0:2f6062c6d018 718 if (_status == GSSTAT_DEEPSLEEP) {
gsfan 0:2f6062c6d018 719 _status = GSSTAT_READY;
gsfan 0:2f6062c6d018 720 return command("AT", GSRES_NORMAL);
gsfan 0:2f6062c6d018 721 }
gsfan 0:2f6062c6d018 722 return -1;
gsfan 0:2f6062c6d018 723 }
gsfan 0:2f6062c6d018 724
gsfan 0:2f6062c6d018 725 int GSwifi::deepSleep () {
gsfan 0:2f6062c6d018 726
gsfan 0:2f6062c6d018 727 if (_status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 728
gsfan 0:2f6062c6d018 729 _status = GSSTAT_DEEPSLEEP;
gsfan 0:2f6062c6d018 730 return command("AT+PSDPSLEEP", GSRES_NORMAL, 0); // go deep sleep
gsfan 0:2f6062c6d018 731 }
gsfan 0:2f6062c6d018 732
gsfan 0:2f6062c6d018 733 bool GSwifi::isConnected () {
gsfan 0:2f6062c6d018 734 return _connect;
gsfan 0:2f6062c6d018 735 }
gsfan 0:2f6062c6d018 736
gsfan 0:2f6062c6d018 737 GSSTATUS GSwifi::getStatus () {
gsfan 0:2f6062c6d018 738 return _status;
gsfan 0:2f6062c6d018 739 }
gsfan 0:2f6062c6d018 740
gsfan 0:2f6062c6d018 741 void GSwifi::poll() {
gsfan 0:2f6062c6d018 742 int i;
gsfan 0:2f6062c6d018 743
gsfan 0:2f6062c6d018 744 if (_gs_enter) {
gsfan 0:2f6062c6d018 745 // received "\n"
gsfan 0:2f6062c6d018 746 int i;
gsfan 0:2f6062c6d018 747 char buf[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 748
gsfan 0:2f6062c6d018 749 wait_ms(10);
gsfan 0:2f6062c6d018 750 _gs_enter --;
gsfan 0:2f6062c6d018 751 i = 0;
gsfan 0:2f6062c6d018 752 while (_buf_cmd.use() && i < sizeof(buf)) {
gsfan 0:2f6062c6d018 753 _buf_cmd.get(&buf[i]);
gsfan 0:2f6062c6d018 754 if (buf[i] == '\n') {
gsfan 0:2f6062c6d018 755 if (i == 0) continue;
gsfan 0:2f6062c6d018 756 break;
gsfan 0:2f6062c6d018 757 }
gsfan 0:2f6062c6d018 758 i ++;
gsfan 0:2f6062c6d018 759 }
gsfan 0:2f6062c6d018 760 buf[i] = 0;
gsfan 0:2f6062c6d018 761 DBG("poll: %s\r\n", buf);
gsfan 0:2f6062c6d018 762
gsfan 0:2f6062c6d018 763 if (strncmp(buf, "CONNECT", 7) == 0 && buf[9] >= '0' && buf[9] <= 'F' &&
gsfan 0:2f6062c6d018 764 _gs_sock[x2i(buf[8])].type == GSTYPE_SERVER) {
gsfan 0:2f6062c6d018 765 i = x2i(buf[8]);
gsfan 0:2f6062c6d018 766 _gs_sock[i].acid = x2i(buf[9]);
gsfan 0:2f6062c6d018 767 // ip, port
gsfan 0:2f6062c6d018 768 } else
gsfan 0:2f6062c6d018 769 if (strncmp(buf, "DISCONNECT", 10) == 0) {
gsfan 0:2f6062c6d018 770 _gs_sock[x2i(buf[11])].connect = false;
gsfan 0:2f6062c6d018 771 } else
gsfan 0:2f6062c6d018 772 if (strncmp(buf, "DISASSOCIATED", 13) == 0 ||
gsfan 0:2f6062c6d018 773 strncmp(buf, "Disassociated", 13) == 0 ||
gsfan 0:2f6062c6d018 774 strncmp(buf, "UnExpected", 10) == 0) {
gsfan 0:2f6062c6d018 775 _connect = false;
gsfan 0:2f6062c6d018 776 for (i = 0; i < 16; i ++) {
gsfan 0:2f6062c6d018 777 _gs_sock[i].connect = false;
gsfan 0:2f6062c6d018 778 }
gsfan 0:2f6062c6d018 779 } else
gsfan 0:2f6062c6d018 780 if (strncmp(buf, "Out of StandBy-Timer", 20) == 0 ||
gsfan 0:2f6062c6d018 781 strncmp(buf, "Out of StandBy-Alarm", 20) == 0) {
gsfan 0:2f6062c6d018 782 if (_status == GSSTAT_STANDBY) {
gsfan 0:2f6062c6d018 783 _status = GSSTAT_WAKEUP;
gsfan 0:2f6062c6d018 784 }
gsfan 0:2f6062c6d018 785 } else
gsfan 0:2f6062c6d018 786 if (strncmp(buf, "Out of Deep Sleep", 17) == 0 ) {
gsfan 0:2f6062c6d018 787 if (_status == GSSTAT_DEEPSLEEP) {
gsfan 0:2f6062c6d018 788 _status = GSSTAT_READY;
gsfan 0:2f6062c6d018 789 }
gsfan 0:2f6062c6d018 790 } else
gsfan 0:2f6062c6d018 791 if (strncmp(buf, "Out of", 6) == 0) {
gsfan 0:2f6062c6d018 792 }
gsfan 0:2f6062c6d018 793 DBG("status: %d\r\n", _status);
gsfan 0:2f6062c6d018 794 }
gsfan 0:2f6062c6d018 795
gsfan 0:2f6062c6d018 796 for (i = 0; i < 16; i ++) {
gsfan 0:2f6062c6d018 797 // if (_gs_sock[i].connect && _gs_sock[i].received) {
gsfan 0:2f6062c6d018 798 if (_gs_sock[i].received && _gs_sock[i].data->use()) {
gsfan 0:2f6062c6d018 799 // recv interrupt
gsfan 0:2f6062c6d018 800 _gs_sock[i].received = 0;
gsfan 0:2f6062c6d018 801 if (_gs_sock[i].onGsReceive != NULL)
gsfan 0:2f6062c6d018 802 _gs_sock[i].onGsReceive(i, _gs_sock[i].acid, _gs_sock[i].data->use());
gsfan 0:2f6062c6d018 803 }
gsfan 0:2f6062c6d018 804 }
gsfan 0:2f6062c6d018 805 }
gsfan 0:2f6062c6d018 806
gsfan 0:2f6062c6d018 807 int GSwifi::open (Host &host, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) {
gsfan 0:2f6062c6d018 808 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 809
gsfan 0:2f6062c6d018 810 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 811 if (host.getIp().isNull() || host.getPort() == 0) {
gsfan 0:2f6062c6d018 812 return -1;
gsfan 0:2f6062c6d018 813 }
gsfan 0:2f6062c6d018 814
gsfan 0:2f6062c6d018 815 if (pro == GSPROT_UDP) {
gsfan 0:2f6062c6d018 816 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 817 } else {
gsfan 0:2f6062c6d018 818 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 819 }
gsfan 0:2f6062c6d018 820 if (command(cmd, GSRES_CONNECT)) return -1;
gsfan 0:2f6062c6d018 821
gsfan 0:2f6062c6d018 822 _gs_sock[_cid].type = GSTYPE_CLIENT;
gsfan 0:2f6062c6d018 823 _gs_sock[_cid].protocol = pro;
gsfan 0:2f6062c6d018 824 _gs_sock[_cid].connect = true;
gsfan 0:2f6062c6d018 825 if (_gs_sock[_cid].data == NULL) {
gsfan 0:2f6062c6d018 826 _gs_sock[_cid].data = new RingBuffer(GS_DATA_SIZE);
gsfan 0:2f6062c6d018 827 } else {
gsfan 0:2f6062c6d018 828 _gs_sock[_cid].data->clear();
gsfan 0:2f6062c6d018 829 }
gsfan 0:2f6062c6d018 830 _gs_sock[_cid].acid = 0;
gsfan 0:2f6062c6d018 831 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 832 _gs_sock[_cid].onGsReceive = ponGsReceive;
gsfan 0:2f6062c6d018 833 return _cid;
gsfan 0:2f6062c6d018 834 }
gsfan 0:2f6062c6d018 835
gsfan 0:2f6062c6d018 836 int GSwifi::listen (int port, GSPROTOCOL pro, onGsReceiveFunc ponGsReceive) {
gsfan 0:2f6062c6d018 837 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 838
gsfan 0:2f6062c6d018 839 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 840 if (port == 0) {
gsfan 0:2f6062c6d018 841 return -1;
gsfan 0:2f6062c6d018 842 }
gsfan 0:2f6062c6d018 843
gsfan 0:2f6062c6d018 844 if (pro == GSPROT_UDP) {
gsfan 0:2f6062c6d018 845 sprintf(cmd, "AT+NSUDP=%d", port);
gsfan 0:2f6062c6d018 846 } else {
gsfan 0:2f6062c6d018 847 sprintf(cmd, "AT+NSTCP=%d", port);
gsfan 0:2f6062c6d018 848 }
gsfan 0:2f6062c6d018 849 if (command(cmd, GSRES_CONNECT)) return -1;
gsfan 0:2f6062c6d018 850
gsfan 0:2f6062c6d018 851 _gs_sock[_cid].type = GSTYPE_SERVER;
gsfan 0:2f6062c6d018 852 _gs_sock[_cid].protocol = pro;
gsfan 0:2f6062c6d018 853 _gs_sock[_cid].connect = true;
gsfan 0:2f6062c6d018 854 if (_gs_sock[_cid].data == NULL) {
gsfan 0:2f6062c6d018 855 _gs_sock[_cid].data = new RingBuffer(GS_DATA_SIZE);
gsfan 0:2f6062c6d018 856 } else {
gsfan 0:2f6062c6d018 857 _gs_sock[_cid].data->clear();
gsfan 0:2f6062c6d018 858 }
gsfan 0:2f6062c6d018 859 _gs_sock[_cid].acid = 0;
gsfan 0:2f6062c6d018 860 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 861 _gs_sock[_cid].onGsReceive = ponGsReceive;
gsfan 0:2f6062c6d018 862 return _cid;
gsfan 0:2f6062c6d018 863 }
gsfan 0:2f6062c6d018 864
gsfan 0:2f6062c6d018 865 int GSwifi::close (int cid) {
gsfan 0:2f6062c6d018 866 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 867
gsfan 0:2f6062c6d018 868 if (! _gs_sock[cid].connect) return -1;
gsfan 0:2f6062c6d018 869
gsfan 0:2f6062c6d018 870 _gs_sock[cid].connect = false;
gsfan 0:2f6062c6d018 871 delete _gs_sock[cid].data;
gsfan 0:2f6062c6d018 872 _gs_sock[cid].data = NULL;
gsfan 0:2f6062c6d018 873 sprintf(cmd, "AT+NCLOSE=%X", cid);
gsfan 0:2f6062c6d018 874 return command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 875 }
gsfan 0:2f6062c6d018 876
gsfan 0:2f6062c6d018 877 int GSwifi::send (int cid, char *buf, int len) {
gsfan 0:2f6062c6d018 878 int i;
gsfan 0:2f6062c6d018 879
gsfan 0:2f6062c6d018 880 if (! _gs_sock[cid].connect) return -1;
gsfan 0:2f6062c6d018 881
gsfan 0:2f6062c6d018 882 if ((_gs_sock[cid].protocol == GSPROT_TCP) ||
gsfan 0:2f6062c6d018 883 (_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_CLIENT)) {
gsfan 0:2f6062c6d018 884 // TCP Client, TCP Server, UDP Client
gsfan 0:2f6062c6d018 885 _gs_ok = 0;
gsfan 0:2f6062c6d018 886 _gs_failure = 0;
gsfan 0:2f6062c6d018 887 #ifdef GS_BULK
gsfan 0:2f6062c6d018 888 _gs.printf("\x1bZ%X%04d", cid, len);
gsfan 0:2f6062c6d018 889 for (i = 0; i < len; i ++) {
gsfan 0:2f6062c6d018 890 _gs.putc(buf[i]);
gsfan 0:2f6062c6d018 891 DBG("%c", buf[i]);
gsfan 0:2f6062c6d018 892 }
gsfan 0:2f6062c6d018 893 #else
gsfan 0:2f6062c6d018 894 _gs.printf("\x1bS%X", cid);
gsfan 0:2f6062c6d018 895 for (i = 0; i < len; i ++) {
gsfan 0:2f6062c6d018 896 if (buf[i] >= 0x20 && buf[i] < 0x7f) {
gsfan 0:2f6062c6d018 897 _gs.putc(buf[i]);
gsfan 0:2f6062c6d018 898 DBG("%c", buf[i]);
gsfan 0:2f6062c6d018 899 }
gsfan 0:2f6062c6d018 900 }
gsfan 0:2f6062c6d018 901 _gs.putc(0x1b);
gsfan 0:2f6062c6d018 902 _gs.putc('E');
gsfan 0:2f6062c6d018 903 #endif
gsfan 0:2f6062c6d018 904 } else {
gsfan 0:2f6062c6d018 905 return -1;
gsfan 0:2f6062c6d018 906 }
gsfan 0:2f6062c6d018 907 while (!_gs_ok && !_gs_failure);
gsfan 0:2f6062c6d018 908 return _gs_ok == 1 ? 0 : -1;
gsfan 0:2f6062c6d018 909 }
gsfan 0:2f6062c6d018 910
gsfan 0:2f6062c6d018 911 int GSwifi::send (int cid, char *buf, int len, Host &host) {
gsfan 0:2f6062c6d018 912 int i;
gsfan 0:2f6062c6d018 913
gsfan 0:2f6062c6d018 914 if (! _gs_sock[cid].connect) return -1;
gsfan 0:2f6062c6d018 915
gsfan 0:2f6062c6d018 916 if ((_gs_sock[cid].protocol == GSPROT_UDP && _gs_sock[cid].type == GSTYPE_SERVER)) {
gsfan 0:2f6062c6d018 917 // UDP Server
gsfan 0:2f6062c6d018 918 _gs_ok = 0;
gsfan 0:2f6062c6d018 919 _gs_failure = 0;
gsfan 0:2f6062c6d018 920 _gs.putc(0x1b);
gsfan 0:2f6062c6d018 921 #ifdef GS_BULK
gsfan 0:2f6062c6d018 922 _gs.printf("\x1bY%X", cid);
gsfan 0:2f6062c6d018 923 _gs.printf("%d.%d.%d.%d %d:", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 0:2f6062c6d018 924 _gs.printf("%04d", len);
gsfan 0:2f6062c6d018 925 for (i = 0; i < len; i ++) {
gsfan 0:2f6062c6d018 926 _gs.putc(buf[i]);
gsfan 0:2f6062c6d018 927 DBG("%c", buf[i]);
gsfan 0:2f6062c6d018 928 }
gsfan 0:2f6062c6d018 929 #else
gsfan 0:2f6062c6d018 930 _gs.printf("\x1bU%X", cid);
gsfan 0:2f6062c6d018 931 _gs.printf("%d.%d.%d.%d %d:", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort());
gsfan 0:2f6062c6d018 932 for (i = 0; i < len; i ++) {
gsfan 0:2f6062c6d018 933 if (buf[i] >= 0x20 && buf[i] < 0x7f) {
gsfan 0:2f6062c6d018 934 _gs.putc(buf[i]);
gsfan 0:2f6062c6d018 935 DBG("%c", buf[i]);
gsfan 0:2f6062c6d018 936 }
gsfan 0:2f6062c6d018 937 }
gsfan 0:2f6062c6d018 938 _gs.putc(0x1b);
gsfan 0:2f6062c6d018 939 _gs.putc('E');
gsfan 0:2f6062c6d018 940 #endif
gsfan 0:2f6062c6d018 941 } else {
gsfan 0:2f6062c6d018 942 return -1;
gsfan 0:2f6062c6d018 943 }
gsfan 0:2f6062c6d018 944 while (!_gs_ok && !_gs_failure);
gsfan 0:2f6062c6d018 945 return _gs_ok == 1 ? 0 : -1;
gsfan 0:2f6062c6d018 946 }
gsfan 0:2f6062c6d018 947
gsfan 0:2f6062c6d018 948 int GSwifi::recv (int cid, char *buf, int len) {
gsfan 0:2f6062c6d018 949 int r;
gsfan 0:2f6062c6d018 950 Timer timeout;
gsfan 0:2f6062c6d018 951
gsfan 0:2f6062c6d018 952 if (_gs_sock[cid].data == NULL) return 0;
gsfan 0:2f6062c6d018 953
gsfan 0:2f6062c6d018 954 timeout.start();
gsfan 0:2f6062c6d018 955 while (_gs_sock[cid].data->use() == 0) {
gsfan 0:2f6062c6d018 956 if (timeout.read_ms() > GS_TIMEOUT) return 0;
gsfan 0:2f6062c6d018 957 }
gsfan 0:2f6062c6d018 958 timeout.stop();
gsfan 0:2f6062c6d018 959
gsfan 0:2f6062c6d018 960 r = _gs_sock[cid].data->get(buf, len);
gsfan 0:2f6062c6d018 961 return r;
gsfan 0:2f6062c6d018 962 }
gsfan 0:2f6062c6d018 963
gsfan 0:2f6062c6d018 964 int GSwifi::recv (int cid, char *buf, int len, Host &host) {
gsfan 0:2f6062c6d018 965 int r;
gsfan 0:2f6062c6d018 966 Timer timeout;
gsfan 0:2f6062c6d018 967
gsfan 0:2f6062c6d018 968 if (_gs_sock[cid].data == NULL) return 0;
gsfan 0:2f6062c6d018 969
gsfan 0:2f6062c6d018 970 timeout.start();
gsfan 0:2f6062c6d018 971 while (_gs_sock[cid].data->use() == 0) {
gsfan 0:2f6062c6d018 972 if (timeout.read_ms() > GS_TIMEOUT) return 0;
gsfan 0:2f6062c6d018 973 }
gsfan 0:2f6062c6d018 974 timeout.stop();
gsfan 0:2f6062c6d018 975
gsfan 0:2f6062c6d018 976 r = _gs_sock[cid].data->get(buf, len);
gsfan 0:2f6062c6d018 977 host = _from;
gsfan 0:2f6062c6d018 978 return r;
gsfan 0:2f6062c6d018 979 }
gsfan 0:2f6062c6d018 980
gsfan 0:2f6062c6d018 981 bool GSwifi::isConnected (int cid) {
gsfan 0:2f6062c6d018 982 return _gs_sock[cid].connect;
gsfan 0:2f6062c6d018 983 }
gsfan 0:2f6062c6d018 984
gsfan 0:2f6062c6d018 985 int GSwifi::httpGet (Host &host, char *uri, int ssl, onGsReceiveFunc ponGsReceive) {
gsfan 0:2f6062c6d018 986 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 987
gsfan 0:2f6062c6d018 988 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 989
gsfan 0:2f6062c6d018 990 if (host.getIp().isNull()) {
gsfan 0:2f6062c6d018 991 if (getHostByName(host)) {
gsfan 0:2f6062c6d018 992 if (getHostByName(host)) return -1;
gsfan 0:2f6062c6d018 993 }
gsfan 0:2f6062c6d018 994 }
gsfan 0:2f6062c6d018 995 if (host.getPort() == 0) {
gsfan 0:2f6062c6d018 996 if (ssl) {
gsfan 0:2f6062c6d018 997 host.setPort(443);
gsfan 0:2f6062c6d018 998 } else {
gsfan 0:2f6062c6d018 999 host.setPort(80);
gsfan 0:2f6062c6d018 1000 }
gsfan 0:2f6062c6d018 1001 }
gsfan 0:2f6062c6d018 1002
gsfan 0:2f6062c6d018 1003 command("AT+HTTPCONF=3,close", GSRES_NORMAL); // Connection:
gsfan 0:2f6062c6d018 1004 sprintf(cmd, "AT+HTTPCONF=11,%s", host.getName()); // Host:
gsfan 0:2f6062c6d018 1005 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 1006 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 1007 if (ssl) {
gsfan 0:2f6062c6d018 1008 strcat(cmd, ",1");
gsfan 0:2f6062c6d018 1009 }
gsfan 0:2f6062c6d018 1010 if (command(cmd, GSRES_HTTP)) return -1;
gsfan 0:2f6062c6d018 1011
gsfan 0:2f6062c6d018 1012 _gs_sock[_cid].type = GSTYPE_CLIENT;
gsfan 0:2f6062c6d018 1013 _gs_sock[_cid].protocol = GSPROT_HTTPGET;
gsfan 0:2f6062c6d018 1014 _gs_sock[_cid].connect = true;
gsfan 0:2f6062c6d018 1015 if (_gs_sock[_cid].data == NULL) {
gsfan 0:2f6062c6d018 1016 _gs_sock[_cid].data = new RingBuffer(GS_DATA_SIZE);
gsfan 0:2f6062c6d018 1017 } else {
gsfan 0:2f6062c6d018 1018 _gs_sock[_cid].data->clear();
gsfan 0:2f6062c6d018 1019 }
gsfan 0:2f6062c6d018 1020 _gs_sock[_cid].acid = 0;
gsfan 0:2f6062c6d018 1021 _gs_sock[_cid].received = 0;
gsfan 0:2f6062c6d018 1022 _gs_sock[_cid].onGsReceive = ponGsReceive;
gsfan 0:2f6062c6d018 1023
gsfan 0:2f6062c6d018 1024 sprintf(cmd, "AT+HTTPSEND=%d,1,%d,%s", _cid, GS_TIMEOUT / 1000, uri); // Get:
gsfan 0:2f6062c6d018 1025 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 1026
gsfan 0:2f6062c6d018 1027 return _cid;
gsfan 0:2f6062c6d018 1028 }
gsfan 0:2f6062c6d018 1029
gsfan 0:2f6062c6d018 1030 int GSwifi::certAdd (char *name, char *cert, int len) {
gsfan 0:2f6062c6d018 1031 int i;
gsfan 0:2f6062c6d018 1032 char cmd[GS_CMD_SIZE];
gsfan 0:2f6062c6d018 1033
gsfan 0:2f6062c6d018 1034 if (! _connect || _status != GSSTAT_READY) return -1;
gsfan 0:2f6062c6d018 1035
gsfan 0:2f6062c6d018 1036 sprintf(cmd, "AT+TCERTADD=%s,1,%d,1", name, len); // Hex, ram
gsfan 0:2f6062c6d018 1037 command(cmd, GSRES_NORMAL);
gsfan 0:2f6062c6d018 1038
gsfan 0:2f6062c6d018 1039 _gs.putc(0x1b);
gsfan 0:2f6062c6d018 1040 _gs.putc('W');
gsfan 0:2f6062c6d018 1041 for (i = 0; i < len; i ++) {
gsfan 0:2f6062c6d018 1042 _gs.putc(cert[i]);
gsfan 0:2f6062c6d018 1043 }
gsfan 0:2f6062c6d018 1044 return cmdResponse(GSRES_NORMAL, GS_TIMEOUT);
gsfan 0:2f6062c6d018 1045 }
gsfan 0:2f6062c6d018 1046
gsfan 0:2f6062c6d018 1047 int GSwifi::x2i (char c) {
gsfan 0:2f6062c6d018 1048 if (c >= '0' && c <= '9') {
gsfan 0:2f6062c6d018 1049 return c - '0';
gsfan 0:2f6062c6d018 1050 } else
gsfan 0:2f6062c6d018 1051 if (c >= 'A' && c <= 'F') {
gsfan 0:2f6062c6d018 1052 return c - 'A' + 10;
gsfan 0:2f6062c6d018 1053 } else
gsfan 0:2f6062c6d018 1054 if (c >= 'a' && c <= 'f') {
gsfan 0:2f6062c6d018 1055 return c - 'a' + 10;
gsfan 0:2f6062c6d018 1056 }
gsfan 0:2f6062c6d018 1057 return 0;
gsfan 0:2f6062c6d018 1058 }
gsfan 0:2f6062c6d018 1059
gsfan 0:2f6062c6d018 1060 char GSwifi::i2x (int i) {
gsfan 0:2f6062c6d018 1061 if (i >= 0 && i <= 9) {
gsfan 0:2f6062c6d018 1062 return i + '0';
gsfan 0:2f6062c6d018 1063 } else
gsfan 0:2f6062c6d018 1064 if (i >= 10 && i <= 15) {
gsfan 0:2f6062c6d018 1065 return i - 10 + 'A';
gsfan 0:2f6062c6d018 1066 }
gsfan 0:2f6062c6d018 1067 return 0;
gsfan 0:2f6062c6d018 1068 }
gsfan 0:2f6062c6d018 1069
gsfan 0:2f6062c6d018 1070
gsfan 0:2f6062c6d018 1071 // for test
gsfan 0:2f6062c6d018 1072
gsfan 0:2f6062c6d018 1073 void GSwifi::test () {
gsfan 0:2f6062c6d018 1074 command(NULL, GSRES_NORMAL);
gsfan 0:2f6062c6d018 1075 wait_ms(100);
gsfan 0:2f6062c6d018 1076 command("AT+NCLOSEALL", GSRES_NORMAL);
gsfan 0:2f6062c6d018 1077 _connect = true;
gsfan 0:2f6062c6d018 1078 }
gsfan 0:2f6062c6d018 1079
gsfan 0:2f6062c6d018 1080 int GSwifi::getc() {
gsfan 0:2f6062c6d018 1081 char c;
gsfan 0:2f6062c6d018 1082 if (_buf_cmd.use()) {
gsfan 0:2f6062c6d018 1083 _buf_cmd.get(&c);
gsfan 0:2f6062c6d018 1084 }
gsfan 0:2f6062c6d018 1085 /*
gsfan 0:2f6062c6d018 1086 } else
gsfan 0:2f6062c6d018 1087 if (_gs_sock[0].data != NULL) {
gsfan 0:2f6062c6d018 1088 _gs_sock[0].data->get(&c);
gsfan 0:2f6062c6d018 1089 }
gsfan 0:2f6062c6d018 1090 */
gsfan 0:2f6062c6d018 1091 return c;
gsfan 0:2f6062c6d018 1092 }
gsfan 0:2f6062c6d018 1093
gsfan 0:2f6062c6d018 1094 void GSwifi::putc(char c) {
gsfan 0:2f6062c6d018 1095 _gs.putc(c);
gsfan 0:2f6062c6d018 1096 }
gsfan 0:2f6062c6d018 1097
gsfan 0:2f6062c6d018 1098 int GSwifi::readable() {
gsfan 0:2f6062c6d018 1099 return _buf_cmd.use();
gsfan 0:2f6062c6d018 1100 // return _buf_cmd.use() || (_gs_sock[0].data != NULL && _gs_sock[0].data->use());
gsfan 0:2f6062c6d018 1101 }