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