for TwitterMbed

Fork of GSwifi_old by gs fan

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