This is boring
SPWFSA01.cpp@23:0b01aa59bb6f, 2016-11-03 (annotated)
- Committer:
- mansiag
- Date:
- Thu Nov 03 06:53:47 2016 +0000
- Revision:
- 23:0b01aa59bb6f
- Parent:
- 22:2eb9b3c54506
- Child:
- 24:419285201dba
Set Wi-Fi mode and rate to b/g/n
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 0:9e5d98ceea74 | 1 | /* SPWFInterface Example |
mridup | 0:9e5d98ceea74 | 2 | * Copyright (c) 2015 ARM Limited |
mridup | 0:9e5d98ceea74 | 3 | * |
mridup | 0:9e5d98ceea74 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mridup | 0:9e5d98ceea74 | 5 | * you may not use this file except in compliance with the License. |
mridup | 0:9e5d98ceea74 | 6 | * You may obtain a copy of the License at |
mridup | 0:9e5d98ceea74 | 7 | * |
mridup | 0:9e5d98ceea74 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mridup | 0:9e5d98ceea74 | 9 | * |
mridup | 0:9e5d98ceea74 | 10 | * Unless required by applicable law or agreed to in writing, software |
mridup | 0:9e5d98ceea74 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mridup | 0:9e5d98ceea74 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mridup | 0:9e5d98ceea74 | 13 | * See the License for the specific language governing permissions and |
mridup | 0:9e5d98ceea74 | 14 | * limitations under the License. |
mridup | 0:9e5d98ceea74 | 15 | */ |
mridup | 0:9e5d98ceea74 | 16 | |
mridup | 0:9e5d98ceea74 | 17 | #include "SPWFSA01.h" |
mridup | 13:f21e4e73bbb6 | 18 | #include "mbed_debug.h" |
mridup | 0:9e5d98ceea74 | 19 | |
mridup | 20:01031cd39e73 | 20 | #define SPWFSA01_CONNECT_TIMEOUT 15000 |
mridup | 20:01031cd39e73 | 21 | #define SPWFSA01_SEND_TIMEOUT 500 |
mridup | 20:01031cd39e73 | 22 | #define SPWFSA01_RECV_TIMEOUT 1500//some commands like AT&F/W takes some time to get the result back! |
mridup | 20:01031cd39e73 | 23 | #define SPWFSA01_MISC_TIMEOUT 500 |
mridup | 20:01031cd39e73 | 24 | #define SPWFSA01_SOCKQ_TIMEOUT 3000 |
mridup | 0:9e5d98ceea74 | 25 | |
mridup | 0:9e5d98ceea74 | 26 | SPWFSA01::SPWFSA01(PinName tx, PinName rx, bool debug) |
mridup | 0:9e5d98ceea74 | 27 | : _serial(tx, rx, 1024), _parser(_serial), |
mridup | 14:868b0fd4e84a | 28 | _wakeup(D14, PIN_INPUT, PullNone, 0), _reset(D15, PIN_INPUT, PullNone, 1), |
mridup | 14:868b0fd4e84a | 29 | //PC_12->D15, PC_8->D14 (re-wires needed in-case used, currently not used) |
mridup | 13:f21e4e73bbb6 | 30 | dbg_on(debug) |
mridup | 0:9e5d98ceea74 | 31 | //Pin PC_8 is wakeup pin |
mridup | 0:9e5d98ceea74 | 32 | //Pin PA_12 is reset pin |
mridup | 0:9e5d98ceea74 | 33 | { |
mridup | 0:9e5d98ceea74 | 34 | _serial.baud(115200); |
mridup | 0:9e5d98ceea74 | 35 | _reset.output(); |
mridup | 0:9e5d98ceea74 | 36 | _wakeup.output(); |
mridup | 0:9e5d98ceea74 | 37 | _parser.debugOn(debug); |
mridup | 0:9e5d98ceea74 | 38 | } |
mridup | 0:9e5d98ceea74 | 39 | |
mridup | 0:9e5d98ceea74 | 40 | bool SPWFSA01::startup(int mode) |
mridup | 0:9e5d98ceea74 | 41 | { |
mridup | 0:9e5d98ceea74 | 42 | setTimeout(SPWFSA01_RECV_TIMEOUT); |
mridup | 0:9e5d98ceea74 | 43 | |
mridup | 0:9e5d98ceea74 | 44 | /*Test module before reset*/ |
mridup | 0:9e5d98ceea74 | 45 | waitSPWFReady(); |
mridup | 0:9e5d98ceea74 | 46 | /*Reset module*/ |
mridup | 0:9e5d98ceea74 | 47 | reset(); |
mridup | 0:9e5d98ceea74 | 48 | |
mridup | 0:9e5d98ceea74 | 49 | /*set local echo to 0*/ |
mridup | 8:a4425b8dd2e4 | 50 | if(!(_parser.send("AT+S.SCFG=localecho1,%d\r", 0) && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 51 | { |
mridup | 13:f21e4e73bbb6 | 52 | debug_if(dbg_on, "SPWF> error local echo set\r\n"); |
mridup | 0:9e5d98ceea74 | 53 | return false; |
mridup | 0:9e5d98ceea74 | 54 | } |
mridup | 0:9e5d98ceea74 | 55 | /*reset factory settings*/ |
mridup | 0:9e5d98ceea74 | 56 | if(!(_parser.send("AT&F") && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 57 | { |
mridup | 13:f21e4e73bbb6 | 58 | debug_if(dbg_on, "SPWF> error AT&F\r\n"); |
mridup | 0:9e5d98ceea74 | 59 | return false; |
mridup | 0:9e5d98ceea74 | 60 | } |
mansiag | 23:0b01aa59bb6f | 61 | |
mansiag | 23:0b01aa59bb6f | 62 | /*set Wi-Fi mode and rate to b/g/n*/ |
mansiag | 23:0b01aa59bb6f | 63 | if(!(_parser.send("AT+S.SCFG=wifi_ht_mode,%d\r",1) && _parser.recv("OK"))) |
mansiag | 23:0b01aa59bb6f | 64 | { |
mansiag | 23:0b01aa59bb6f | 65 | debug_if(dbg_on, "SPWF> error setting ht_mode\r\n"); |
mansiag | 23:0b01aa59bb6f | 66 | return false; |
mansiag | 23:0b01aa59bb6f | 67 | } |
mansiag | 23:0b01aa59bb6f | 68 | |
mansiag | 23:0b01aa59bb6f | 69 | if(!(_parser.send("AT+S.SCFG=wifi_opr_rate_mask,0x003FFFCF\r") && _parser.recv("OK"))) |
mansiag | 23:0b01aa59bb6f | 70 | { |
mansiag | 23:0b01aa59bb6f | 71 | debug_if(dbg_on, "SPWF> error setting operational rates\r\n"); |
mansiag | 23:0b01aa59bb6f | 72 | return false; |
mansiag | 23:0b01aa59bb6f | 73 | } |
mridup | 0:9e5d98ceea74 | 74 | |
mridup | 0:9e5d98ceea74 | 75 | /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/ |
mridup | 0:9e5d98ceea74 | 76 | if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", mode) && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 77 | { |
mridup | 13:f21e4e73bbb6 | 78 | debug_if(dbg_on, "SPWF> error wifi mode set\r\n"); |
mridup | 0:9e5d98ceea74 | 79 | return false; |
mridup | 0:9e5d98ceea74 | 80 | } |
mridup | 0:9e5d98ceea74 | 81 | |
mridup | 0:9e5d98ceea74 | 82 | /* save current setting in flash */ |
mridup | 0:9e5d98ceea74 | 83 | if(!(_parser.send("AT&W") && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 84 | { |
mridup | 13:f21e4e73bbb6 | 85 | debug_if(dbg_on, "SPWF> error AT&W\r\n"); |
mridup | 0:9e5d98ceea74 | 86 | return false; |
mridup | 0:9e5d98ceea74 | 87 | } |
mridup | 0:9e5d98ceea74 | 88 | |
mridup | 0:9e5d98ceea74 | 89 | /*reset again and send AT command and check for result (AT->OK)*/ |
mridup | 0:9e5d98ceea74 | 90 | reset(); |
mridup | 0:9e5d98ceea74 | 91 | |
mridup | 0:9e5d98ceea74 | 92 | return true; |
mridup | 0:9e5d98ceea74 | 93 | } |
mridup | 0:9e5d98ceea74 | 94 | |
mridup | 0:9e5d98ceea74 | 95 | bool SPWFSA01::hw_reset(void) |
mridup | 0:9e5d98ceea74 | 96 | { |
mridup | 0:9e5d98ceea74 | 97 | /* reset the pin PC12 */ |
mridup | 0:9e5d98ceea74 | 98 | _reset.write(0); |
mridup | 0:9e5d98ceea74 | 99 | wait_ms(200); |
mridup | 0:9e5d98ceea74 | 100 | _reset.write(1); |
mridup | 0:9e5d98ceea74 | 101 | wait_ms(100); |
mridup | 0:9e5d98ceea74 | 102 | return 1; |
mridup | 0:9e5d98ceea74 | 103 | } |
mridup | 0:9e5d98ceea74 | 104 | |
mridup | 0:9e5d98ceea74 | 105 | bool SPWFSA01::reset(void) |
mridup | 0:9e5d98ceea74 | 106 | { |
mridup | 0:9e5d98ceea74 | 107 | if(!_parser.send("AT+CFUN=1")) return false; |
mridup | 0:9e5d98ceea74 | 108 | while(1) { |
mridup | 8:a4425b8dd2e4 | 109 | if (_parser.recv("+WIND:32:WiFi Hardware Started\r")) { |
mridup | 0:9e5d98ceea74 | 110 | return true; |
mridup | 0:9e5d98ceea74 | 111 | } |
mridup | 0:9e5d98ceea74 | 112 | } |
mridup | 0:9e5d98ceea74 | 113 | } |
mridup | 0:9e5d98ceea74 | 114 | |
mridup | 0:9e5d98ceea74 | 115 | void SPWFSA01::waitSPWFReady(void) |
mridup | 0:9e5d98ceea74 | 116 | { |
mridup | 0:9e5d98ceea74 | 117 | //wait_ms(200); |
mridup | 0:9e5d98ceea74 | 118 | while(1) |
mridup | 0:9e5d98ceea74 | 119 | if(_parser.send("AT") && _parser.recv("OK")) |
mridup | 0:9e5d98ceea74 | 120 | //till we get OK from AT command |
mridup | 0:9e5d98ceea74 | 121 | //printf("\r\nwaiting for reset to complete..\n"); |
mridup | 0:9e5d98ceea74 | 122 | return; |
mridup | 0:9e5d98ceea74 | 123 | |
mridup | 0:9e5d98ceea74 | 124 | } |
mridup | 0:9e5d98ceea74 | 125 | |
mridup | 0:9e5d98ceea74 | 126 | /* Security Mode |
mridup | 0:9e5d98ceea74 | 127 | None = 0, |
mridup | 0:9e5d98ceea74 | 128 | WEP = 1, |
mridup | 0:9e5d98ceea74 | 129 | WPA_Personal = 2, |
mridup | 0:9e5d98ceea74 | 130 | */ |
mridup | 0:9e5d98ceea74 | 131 | bool SPWFSA01::connect(const char *ap, const char *passPhrase, int securityMode) |
mridup | 0:9e5d98ceea74 | 132 | { |
mridup | 0:9e5d98ceea74 | 133 | uint32_t n1, n2, n3, n4; |
mridup | 0:9e5d98ceea74 | 134 | |
mridup | 0:9e5d98ceea74 | 135 | //AT+S.SCFG=wifi_wpa_psk_text,%s\r |
mridup | 0:9e5d98ceea74 | 136 | if(!(_parser.send("AT+S.SCFG=wifi_wpa_psk_text,%s", passPhrase) && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 137 | { |
mridup | 13:f21e4e73bbb6 | 138 | debug_if(dbg_on, "SPWF> error pass set\r\n"); |
mridup | 0:9e5d98ceea74 | 139 | return false; |
mridup | 0:9e5d98ceea74 | 140 | } |
mridup | 0:9e5d98ceea74 | 141 | //AT+S.SSIDTXT=%s\r |
mridup | 0:9e5d98ceea74 | 142 | if(!(_parser.send("AT+S.SSIDTXT=%s", ap) && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 143 | { |
mridup | 13:f21e4e73bbb6 | 144 | debug_if(dbg_on, "SPWF> error ssid set\r\n"); |
mridup | 0:9e5d98ceea74 | 145 | return false; |
mridup | 0:9e5d98ceea74 | 146 | } |
mridup | 0:9e5d98ceea74 | 147 | //AT+S.SCFG=wifi_priv_mode,%d\r |
mridup | 0:9e5d98ceea74 | 148 | if(!(_parser.send("AT+S.SCFG=wifi_priv_mode,%d", securityMode) && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 149 | { |
mridup | 13:f21e4e73bbb6 | 150 | debug_if(dbg_on, "SPWF> error security mode set\r\n"); |
mridup | 0:9e5d98ceea74 | 151 | return false; |
mridup | 0:9e5d98ceea74 | 152 | } |
mridup | 0:9e5d98ceea74 | 153 | //"AT+S.SCFG=wifi_mode,%d\r" |
mridup | 0:9e5d98ceea74 | 154 | /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/ |
mridup | 0:9e5d98ceea74 | 155 | if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", 1) && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 156 | { |
mridup | 13:f21e4e73bbb6 | 157 | debug_if(dbg_on, "SPWF> error wifi mode set\r\n"); |
mridup | 0:9e5d98ceea74 | 158 | return false; |
mridup | 0:9e5d98ceea74 | 159 | } |
mridup | 0:9e5d98ceea74 | 160 | //AT&W |
mridup | 0:9e5d98ceea74 | 161 | /* save current setting in flash */ |
mridup | 0:9e5d98ceea74 | 162 | if(!(_parser.send("AT&W") && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 163 | { |
mridup | 13:f21e4e73bbb6 | 164 | debug_if(dbg_on, "SPWF> error AT&W\r\n"); |
mridup | 0:9e5d98ceea74 | 165 | return false; |
mridup | 0:9e5d98ceea74 | 166 | } |
mridup | 0:9e5d98ceea74 | 167 | //reset module |
mridup | 0:9e5d98ceea74 | 168 | reset(); |
mridup | 0:9e5d98ceea74 | 169 | |
mridup | 0:9e5d98ceea74 | 170 | while(1) |
mansiag | 18:fd46c2ca8eb7 | 171 | if((_parser.recv("+WIND:24:WiFi Up:%u.%u.%u.%u",&n1, &n2, &n3, &n4))) |
mridup | 0:9e5d98ceea74 | 172 | { |
mridup | 0:9e5d98ceea74 | 173 | break; |
mridup | 8:a4425b8dd2e4 | 174 | } |
mridup | 0:9e5d98ceea74 | 175 | |
mridup | 0:9e5d98ceea74 | 176 | return true; |
mridup | 0:9e5d98ceea74 | 177 | } |
mridup | 0:9e5d98ceea74 | 178 | |
mridup | 0:9e5d98ceea74 | 179 | bool SPWFSA01::disconnect(void) |
mridup | 0:9e5d98ceea74 | 180 | { |
mridup | 0:9e5d98ceea74 | 181 | //"AT+S.SCFG=wifi_mode,%d\r" |
mridup | 0:9e5d98ceea74 | 182 | /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/ |
mridup | 0:9e5d98ceea74 | 183 | if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", 0) && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 184 | { |
mridup | 13:f21e4e73bbb6 | 185 | debug_if(dbg_on, "SPWF> error wifi mode set\r\n"); |
mridup | 0:9e5d98ceea74 | 186 | return false; |
mridup | 0:9e5d98ceea74 | 187 | } |
mridup | 0:9e5d98ceea74 | 188 | //AT&W |
mridup | 0:9e5d98ceea74 | 189 | /* save current setting in flash */ |
mridup | 0:9e5d98ceea74 | 190 | if(!(_parser.send("AT&W") && _parser.recv("OK"))) |
mridup | 0:9e5d98ceea74 | 191 | { |
mridup | 13:f21e4e73bbb6 | 192 | debug_if(dbg_on, "SPWF> error AT&W\r\n"); |
mridup | 0:9e5d98ceea74 | 193 | return false; |
mridup | 0:9e5d98ceea74 | 194 | } |
mridup | 0:9e5d98ceea74 | 195 | //reset module |
mridup | 0:9e5d98ceea74 | 196 | reset(); |
mridup | 0:9e5d98ceea74 | 197 | return true; |
mridup | 0:9e5d98ceea74 | 198 | } |
mridup | 0:9e5d98ceea74 | 199 | |
mridup | 5:f17d6351e942 | 200 | bool SPWFSA01::dhcp(int mode) |
mridup | 0:9e5d98ceea74 | 201 | { |
mridup | 0:9e5d98ceea74 | 202 | //only 3 valid modes |
mridup | 5:f17d6351e942 | 203 | //0->off(ip_addr must be set by user), 1->on(auto set by AP), 2->on&customize(miniAP ip_addr can be set by user) |
mridup | 0:9e5d98ceea74 | 204 | if(mode < 0 || mode > 2) { |
mridup | 0:9e5d98ceea74 | 205 | return false; |
mridup | 0:9e5d98ceea74 | 206 | } |
mridup | 5:f17d6351e942 | 207 | |
mridup | 5:f17d6351e942 | 208 | return _parser.send("AT+S.SCFG=ip_use_dhcp,%d\r", mode) |
mridup | 0:9e5d98ceea74 | 209 | && _parser.recv("OK"); |
mridup | 0:9e5d98ceea74 | 210 | } |
mridup | 0:9e5d98ceea74 | 211 | |
mridup | 0:9e5d98ceea74 | 212 | |
mridup | 0:9e5d98ceea74 | 213 | const char *SPWFSA01::getIPAddress(void) |
mridup | 0:9e5d98ceea74 | 214 | { |
mridup | 0:9e5d98ceea74 | 215 | uint32_t n1, n2, n3, n4; |
mridup | 0:9e5d98ceea74 | 216 | |
mridup | 0:9e5d98ceea74 | 217 | if (!(_parser.send("AT+S.STS=ip_ipaddr") |
mridup | 1:becf69a794fb | 218 | && _parser.recv("# ip_ipaddr = %u.%u.%u.%u", &n1, &n2, &n3, &n4) |
mridup | 0:9e5d98ceea74 | 219 | && _parser.recv("OK"))) { |
mridup | 13:f21e4e73bbb6 | 220 | debug_if(dbg_on, "SPWF> getIPAddress error\r\n"); |
mridup | 0:9e5d98ceea74 | 221 | return 0; |
mridup | 0:9e5d98ceea74 | 222 | } |
mridup | 2:40d183d7dc19 | 223 | |
mridup | 0:9e5d98ceea74 | 224 | sprintf((char*)_ip_buffer,"%u.%u.%u.%u", n1, n2, n3, n4); |
mridup | 0:9e5d98ceea74 | 225 | |
mridup | 0:9e5d98ceea74 | 226 | return _ip_buffer; |
mridup | 0:9e5d98ceea74 | 227 | } |
mridup | 0:9e5d98ceea74 | 228 | |
mridup | 0:9e5d98ceea74 | 229 | const char *SPWFSA01::getMACAddress(void) |
mridup | 0:9e5d98ceea74 | 230 | { |
mridup | 2:40d183d7dc19 | 231 | uint32_t n1, n2, n3, n4, n5, n6; |
mridup | 1:becf69a794fb | 232 | |
mridup | 1:becf69a794fb | 233 | if (!(_parser.send("AT+S.GCFG=nv_wifi_macaddr") |
mridup | 2:40d183d7dc19 | 234 | && _parser.recv("# nv_wifi_macaddr = %x:%x:%x:%x:%x:%x", &n1, &n2, &n3, &n4, &n5, &n6) |
mridup | 0:9e5d98ceea74 | 235 | && _parser.recv("OK"))) { |
mridup | 13:f21e4e73bbb6 | 236 | debug_if(dbg_on, "SPWF> getMACAddress error\r\n"); |
mridup | 0:9e5d98ceea74 | 237 | return 0; |
mridup | 0:9e5d98ceea74 | 238 | } |
mridup | 2:40d183d7dc19 | 239 | |
mridup | 2:40d183d7dc19 | 240 | sprintf((char*)_mac_buffer,"%02X:%02X:%02X:%02X:%02X:%02X", n1, n2, n3, n4, n5, n6); |
mridup | 0:9e5d98ceea74 | 241 | |
mridup | 0:9e5d98ceea74 | 242 | return _mac_buffer; |
mridup | 0:9e5d98ceea74 | 243 | } |
mridup | 0:9e5d98ceea74 | 244 | |
mridup | 0:9e5d98ceea74 | 245 | bool SPWFSA01::isConnected(void) |
mridup | 0:9e5d98ceea74 | 246 | { |
mridup | 0:9e5d98ceea74 | 247 | return getIPAddress() != 0; |
mridup | 0:9e5d98ceea74 | 248 | } |
mridup | 0:9e5d98ceea74 | 249 | |
mridup | 3:2af9df1068a5 | 250 | bool SPWFSA01::open(const char *type, int* id, const char* addr, int port) |
mridup | 0:9e5d98ceea74 | 251 | { |
mridup | 13:f21e4e73bbb6 | 252 | Timer timer; |
mridup | 13:f21e4e73bbb6 | 253 | timer.start(); |
mridup | 3:2af9df1068a5 | 254 | |
mridup | 3:2af9df1068a5 | 255 | if(!_parser.send("AT+S.SOCKON=%s,%d,%s,ind", addr, port, type)) |
mridup | 3:2af9df1068a5 | 256 | { |
mridup | 13:f21e4e73bbb6 | 257 | debug_if(dbg_on, "SPWF> error opening socket\r\n"); |
mridup | 3:2af9df1068a5 | 258 | return false; |
mridup | 3:2af9df1068a5 | 259 | } |
mridup | 3:2af9df1068a5 | 260 | |
mridup | 3:2af9df1068a5 | 261 | while(1) |
mridup | 3:2af9df1068a5 | 262 | { |
mridup | 3:2af9df1068a5 | 263 | if( _parser.recv(" ID: %d", id) |
mridup | 3:2af9df1068a5 | 264 | && _parser.recv("OK")) |
mridup | 3:2af9df1068a5 | 265 | break; |
mridup | 13:f21e4e73bbb6 | 266 | |
mridup | 13:f21e4e73bbb6 | 267 | if (timer.read_ms() > SPWFSA01_CONNECT_TIMEOUT) { |
mridup | 13:f21e4e73bbb6 | 268 | return false; |
mridup | 13:f21e4e73bbb6 | 269 | } |
mridup | 13:f21e4e73bbb6 | 270 | |
mridup | 3:2af9df1068a5 | 271 | //TODO:implement time-out functionality in case of no response |
mridup | 4:042d5b9bb6e7 | 272 | //if(timeout) return false; |
mridup | 7:551de95939e2 | 273 | //TODO: deal with errors like "ERROR: Failed to resolve name" |
mridup | 11:643a8c0f07ea | 274 | //TODO: deal with errors like "ERROR: Data mode not available" |
mridup | 3:2af9df1068a5 | 275 | } |
mridup | 0:9e5d98ceea74 | 276 | |
mridup | 3:2af9df1068a5 | 277 | return true; |
mridup | 0:9e5d98ceea74 | 278 | } |
mridup | 0:9e5d98ceea74 | 279 | |
mridup | 0:9e5d98ceea74 | 280 | bool SPWFSA01::send(int id, const void *data, uint32_t amount) |
mridup | 11:643a8c0f07ea | 281 | { |
mridup | 11:643a8c0f07ea | 282 | char _buf[18]; |
mridup | 11:643a8c0f07ea | 283 | |
mridup | 11:643a8c0f07ea | 284 | setTimeout(SPWFSA01_SEND_TIMEOUT); |
mridup | 11:643a8c0f07ea | 285 | |
mridup | 11:643a8c0f07ea | 286 | sprintf((char*)_buf,"AT+S.SOCKW=%d,%d\r", id, amount); |
mridup | 11:643a8c0f07ea | 287 | |
mridup | 0:9e5d98ceea74 | 288 | //May take a second try if device is busy |
mridup | 0:9e5d98ceea74 | 289 | for (unsigned i = 0; i < 2; i++) { |
mridup | 11:643a8c0f07ea | 290 | if (_parser.write((char*)_buf, strlen(_buf)) >=0 |
mridup | 7:551de95939e2 | 291 | && _parser.write((char*)data, (int)amount) >= 0 |
mridup | 7:551de95939e2 | 292 | && _parser.recv("OK")) { |
mridup | 0:9e5d98ceea74 | 293 | return true; |
mridup | 0:9e5d98ceea74 | 294 | } |
mridup | 0:9e5d98ceea74 | 295 | } |
mridup | 0:9e5d98ceea74 | 296 | |
mridup | 0:9e5d98ceea74 | 297 | return false; |
mridup | 0:9e5d98ceea74 | 298 | } |
mridup | 0:9e5d98ceea74 | 299 | |
mansiag | 19:df8d7f70a740 | 300 | /* |
mridup | 0:9e5d98ceea74 | 301 | int32_t SPWFSA01::recv(int id, void *data, uint32_t amount) |
mridup | 0:9e5d98ceea74 | 302 | { |
mansiag | 15:e56c9e744ab6 | 303 | uint32_t recv_amount; |
mridup | 0:9e5d98ceea74 | 304 | int recv_id; |
mansiag | 15:e56c9e744ab6 | 305 | bool wind_recv = true; |
mansiag | 15:e56c9e744ab6 | 306 | |
mridup | 7:551de95939e2 | 307 | if (!(_parser.recv("+WIND:55:Pending Data:%d:%u", &recv_id, &recv_amount) |
mansiag | 15:e56c9e744ab6 | 308 | && recv_id == id |
mansiag | 15:e56c9e744ab6 | 309 | && recv_amount <= amount |
mansiag | 15:e56c9e744ab6 | 310 | && recv_amount%730 |
mansiag | 15:e56c9e744ab6 | 311 | && _parser.send("AT+S.SOCKQ=%d", id) //send a query (will be required for secure sockets) |
mansiag | 15:e56c9e744ab6 | 312 | && _parser.recv(" DATALEN: %u", &recv_amount) |
mansiag | 15:e56c9e744ab6 | 313 | && _parser.recv("OK") |
mansiag | 15:e56c9e744ab6 | 314 | && recv_amount > 0 |
mansiag | 15:e56c9e744ab6 | 315 | && _parser.send("AT+S.SOCKR=%d,%d", id, recv_amount) |
mansiag | 15:e56c9e744ab6 | 316 | && (_parser.read((char*)data, recv_amount) >0) |
mansiag | 15:e56c9e744ab6 | 317 | && _parser.recv("OK"))) { |
mansiag | 15:e56c9e744ab6 | 318 | if(!(recv_amount%730)) |
mansiag | 15:e56c9e744ab6 | 319 | { |
mansiag | 15:e56c9e744ab6 | 320 | // receive all the WIND messages |
mansiag | 15:e56c9e744ab6 | 321 | do { |
mansiag | 15:e56c9e744ab6 | 322 | if (!(_parser.recv("+WIND:55:Pending Data:%d:%u", &recv_id, &recv_amount) |
mansiag | 15:e56c9e744ab6 | 323 | && recv_id == id |
mansiag | 15:e56c9e744ab6 | 324 | && recv_amount <= amount |
mansiag | 15:e56c9e744ab6 | 325 | && recv_amount > 0)) |
mansiag | 15:e56c9e744ab6 | 326 | wind_recv = false; |
mansiag | 15:e56c9e744ab6 | 327 | } while (!(recv_amount%730) && wind_recv); |
mansiag | 15:e56c9e744ab6 | 328 | |
mansiag | 15:e56c9e744ab6 | 329 | // Read all the data pending on a socket |
mansiag | 15:e56c9e744ab6 | 330 | if(!( recv_amount > 0 |
mansiag | 15:e56c9e744ab6 | 331 | && _parser.send("AT+S.SOCKR=%d,%d", id, recv_amount) |
mansiag | 15:e56c9e744ab6 | 332 | && (_parser.read((char*)data, recv_amount) >0) |
mansiag | 15:e56c9e744ab6 | 333 | && _parser.recv("OK"))) { |
mansiag | 15:e56c9e744ab6 | 334 | return -1; |
mansiag | 15:e56c9e744ab6 | 335 | } |
mridup | 13:f21e4e73bbb6 | 336 | } |
mansiag | 15:e56c9e744ab6 | 337 | else { |
mansiag | 15:e56c9e744ab6 | 338 | return -2; |
mridup | 13:f21e4e73bbb6 | 339 | } |
mansiag | 15:e56c9e744ab6 | 340 | } |
mridup | 0:9e5d98ceea74 | 341 | return recv_amount; |
mridup | 0:9e5d98ceea74 | 342 | } |
mansiag | 19:df8d7f70a740 | 343 | */ |
mansiag | 19:df8d7f70a740 | 344 | |
mansiag | 19:df8d7f70a740 | 345 | int32_t SPWFSA01::recv(int id, void *data, uint32_t amount) |
mansiag | 19:df8d7f70a740 | 346 | { |
mridup | 20:01031cd39e73 | 347 | Timer timer; |
mridup | 20:01031cd39e73 | 348 | timer.start(); |
mridup | 20:01031cd39e73 | 349 | |
mansiag | 19:df8d7f70a740 | 350 | uint32_t recv_amount=0; |
mridup | 20:01031cd39e73 | 351 | int recv_id; |
mridup | 20:01031cd39e73 | 352 | |
mridup | 20:01031cd39e73 | 353 | if(!(_parser.recv("+WIND:55:Pending Data:%d:", &recv_id))) { |
mridup | 20:01031cd39e73 | 354 | //do nothing; |
mridup | 20:01031cd39e73 | 355 | debug_if(dbg_on, "SPWF> WIND:55 Timeout\r\n"); |
mridup | 20:01031cd39e73 | 356 | } |
mansiag | 19:df8d7f70a740 | 357 | |
mansiag | 19:df8d7f70a740 | 358 | while(!recv_amount) { |
mansiag | 19:df8d7f70a740 | 359 | if(!(_parser.send("AT+S.SOCKQ=%d", id) //send a query (will be required for secure sockets) |
mansiag | 19:df8d7f70a740 | 360 | && _parser.recv(" DATALEN: %u", &recv_amount) |
mansiag | 19:df8d7f70a740 | 361 | && _parser.recv("OK"))) { |
mansiag | 19:df8d7f70a740 | 362 | return -2; |
mansiag | 19:df8d7f70a740 | 363 | } |
mridup | 20:01031cd39e73 | 364 | if (timer.read_ms() > SPWFSA01_SOCKQ_TIMEOUT) { |
mansiag | 22:2eb9b3c54506 | 365 | return -1; |
mridup | 20:01031cd39e73 | 366 | } |
mansiag | 19:df8d7f70a740 | 367 | } |
mansiag | 19:df8d7f70a740 | 368 | if(recv_amount > amount) |
mansiag | 19:df8d7f70a740 | 369 | recv_amount = amount; |
mansiag | 19:df8d7f70a740 | 370 | |
mansiag | 19:df8d7f70a740 | 371 | _parser.flush(); |
mansiag | 21:cd722edbb644 | 372 | if(!(_parser.send("AT+S.SOCKR=%d,%d", id, recv_amount))) |
mansiag | 21:cd722edbb644 | 373 | return -2; |
mansiag | 21:cd722edbb644 | 374 | |
mansiag | 21:cd722edbb644 | 375 | /* only when the data recv is greater than 3 bytes */ |
mansiag | 21:cd722edbb644 | 376 | if(recv_amount > 3) { |
mansiag | 21:cd722edbb644 | 377 | /* look for WIND messages in data */ |
mansiag | 21:cd722edbb644 | 378 | |
mansiag | 21:cd722edbb644 | 379 | // get first 3 bytes of data |
mansiag | 21:cd722edbb644 | 380 | for(int i=0;i<3;i++) { |
mansiag | 21:cd722edbb644 | 381 | int c = -1; |
mansiag | 21:cd722edbb644 | 382 | while(c < 0) { |
mansiag | 21:cd722edbb644 | 383 | c = _parser.getc(); |
mansiag | 21:cd722edbb644 | 384 | } |
mansiag | 21:cd722edbb644 | 385 | ((char *)data)[i] = c; |
mansiag | 21:cd722edbb644 | 386 | } |
mansiag | 21:cd722edbb644 | 387 | |
mansiag | 21:cd722edbb644 | 388 | /* 0xD: \r && 0xA: \n */ |
mansiag | 21:cd722edbb644 | 389 | if(((char *)data)[0] == 0xD |
mansiag | 21:cd722edbb644 | 390 | &&((char *)data)[1] == 0xA |
mansiag | 21:cd722edbb644 | 391 | &&((char *)data)[2] == '+') { |
mansiag | 21:cd722edbb644 | 392 | ((char *)data)[0] = 0; |
mansiag | 21:cd722edbb644 | 393 | while(((char *)data)[0] != 0xA) { |
mansiag | 21:cd722edbb644 | 394 | ((char *)data)[0] = _parser.getc(); |
mansiag | 21:cd722edbb644 | 395 | } |
mansiag | 21:cd722edbb644 | 396 | |
mansiag | 21:cd722edbb644 | 397 | // complete data is yet to be read |
mansiag | 21:cd722edbb644 | 398 | if(!((_parser.read((char*)data, recv_amount) >0) |
mansiag | 21:cd722edbb644 | 399 | && _parser.recv("OK"))) { |
mansiag | 21:cd722edbb644 | 400 | return -2; |
mansiag | 21:cd722edbb644 | 401 | } |
mansiag | 21:cd722edbb644 | 402 | } |
mansiag | 21:cd722edbb644 | 403 | else { |
mansiag | 21:cd722edbb644 | 404 | // data left to be read is 3 bytes less. |
mansiag | 21:cd722edbb644 | 405 | if(!((_parser.read((char*)data+3, recv_amount-3) >0) |
mansiag | 21:cd722edbb644 | 406 | && _parser.recv("OK"))) { |
mansiag | 21:cd722edbb644 | 407 | return -2; |
mansiag | 19:df8d7f70a740 | 408 | } |
mansiag | 21:cd722edbb644 | 409 | } |
mansiag | 21:cd722edbb644 | 410 | } |
mansiag | 21:cd722edbb644 | 411 | else { |
mansiag | 21:cd722edbb644 | 412 | if(!((_parser.read((char*)data, recv_amount) >0) |
mansiag | 21:cd722edbb644 | 413 | && _parser.recv("OK"))) { |
mansiag | 21:cd722edbb644 | 414 | return -2; |
mansiag | 21:cd722edbb644 | 415 | } |
mansiag | 21:cd722edbb644 | 416 | } |
mansiag | 19:df8d7f70a740 | 417 | return recv_amount; |
mansiag | 19:df8d7f70a740 | 418 | } |
mridup | 0:9e5d98ceea74 | 419 | |
mridup | 0:9e5d98ceea74 | 420 | bool SPWFSA01::close(int id) |
mridup | 0:9e5d98ceea74 | 421 | { |
mridup | 4:042d5b9bb6e7 | 422 | //May take a second try if device is busy or error is returned |
mridup | 0:9e5d98ceea74 | 423 | for (unsigned i = 0; i < 2; i++) { |
mridup | 4:042d5b9bb6e7 | 424 | if (_parser.send("AT+S.SOCKC=%d", id) |
mridup | 0:9e5d98ceea74 | 425 | && _parser.recv("OK")) { |
mridup | 0:9e5d98ceea74 | 426 | return true; |
mridup | 0:9e5d98ceea74 | 427 | } |
mridup | 7:551de95939e2 | 428 | else |
mridup | 7:551de95939e2 | 429 | { |
mridup | 7:551de95939e2 | 430 | if(_parser.recv("ERROR: Pending data")) |
mridup | 13:f21e4e73bbb6 | 431 | debug_if(dbg_on, "SPWF> ERROR!!!!\r\n"); |
mridup | 7:551de95939e2 | 432 | return false; |
mridup | 7:551de95939e2 | 433 | } |
mridup | 6:258b093eec08 | 434 | //TODO: Deal with "ERROR: Pending data" (Closing a socket with pending data) |
mridup | 0:9e5d98ceea74 | 435 | } |
mridup | 0:9e5d98ceea74 | 436 | |
mridup | 0:9e5d98ceea74 | 437 | return false; |
mridup | 0:9e5d98ceea74 | 438 | } |
mridup | 0:9e5d98ceea74 | 439 | |
mridup | 0:9e5d98ceea74 | 440 | void SPWFSA01::setTimeout(uint32_t timeout_ms) |
mridup | 0:9e5d98ceea74 | 441 | { |
mridup | 0:9e5d98ceea74 | 442 | _parser.setTimeout(timeout_ms); |
mridup | 0:9e5d98ceea74 | 443 | } |
mridup | 0:9e5d98ceea74 | 444 | |
mridup | 0:9e5d98ceea74 | 445 | bool SPWFSA01::readable() |
mridup | 0:9e5d98ceea74 | 446 | { |
mridup | 0:9e5d98ceea74 | 447 | return _serial.readable(); |
mridup | 0:9e5d98ceea74 | 448 | } |
mridup | 0:9e5d98ceea74 | 449 | |
mridup | 0:9e5d98ceea74 | 450 | bool SPWFSA01::writeable() |
mridup | 0:9e5d98ceea74 | 451 | { |
mridup | 0:9e5d98ceea74 | 452 | return _serial.writeable(); |
mridup | 0:9e5d98ceea74 | 453 | } |