cc

Dependencies:   ATParser

Committer:
mridup
Date:
Tue Aug 02 07:03:54 2016 +0000
Revision:
13:f21e4e73bbb6
Parent:
12:00bc9c029aec
Child:
14:868b0fd4e84a
debug_if, sock open time out, sock recv returns "device error" apart from "would block".

Who changed what in which revision?

UserRevisionLine numberNew 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 0:9e5d98ceea74 20 #define SPWFSA01_CONNECT_TIMEOUT 15000
mridup 0:9e5d98ceea74 21 #define SPWFSA01_SEND_TIMEOUT 500
mridup 10:580379efe167 22 #define SPWFSA01_RECV_TIMEOUT 1500//some commands like AT&F/W takes some time to get the result back!
mridup 0:9e5d98ceea74 23 #define SPWFSA01_MISC_TIMEOUT 500
mridup 0:9e5d98ceea74 24
mridup 0:9e5d98ceea74 25 SPWFSA01::SPWFSA01(PinName tx, PinName rx, bool debug)
mridup 0:9e5d98ceea74 26 : _serial(tx, rx, 1024), _parser(_serial),
mridup 13:f21e4e73bbb6 27 _wakeup(PC_8, PIN_INPUT, PullNone, 0), _reset(PC_12, PIN_INPUT, PullNone, 1),
mridup 13:f21e4e73bbb6 28 dbg_on(debug)
mridup 0:9e5d98ceea74 29 //Pin PC_8 is wakeup pin
mridup 0:9e5d98ceea74 30 //Pin PA_12 is reset pin
mridup 0:9e5d98ceea74 31 {
mridup 0:9e5d98ceea74 32 _serial.baud(115200);
mridup 0:9e5d98ceea74 33 _reset.output();
mridup 0:9e5d98ceea74 34 _wakeup.output();
mridup 0:9e5d98ceea74 35 _parser.debugOn(debug);
mridup 0:9e5d98ceea74 36 }
mridup 0:9e5d98ceea74 37
mridup 0:9e5d98ceea74 38 bool SPWFSA01::startup(int mode)
mridup 0:9e5d98ceea74 39 {
mridup 0:9e5d98ceea74 40 setTimeout(SPWFSA01_RECV_TIMEOUT);
mridup 0:9e5d98ceea74 41
mridup 0:9e5d98ceea74 42 /*Test module before reset*/
mridup 0:9e5d98ceea74 43 waitSPWFReady();
mridup 0:9e5d98ceea74 44 /*Reset module*/
mridup 0:9e5d98ceea74 45 reset();
mridup 0:9e5d98ceea74 46
mridup 0:9e5d98ceea74 47 /*set local echo to 0*/
mridup 8:a4425b8dd2e4 48 if(!(_parser.send("AT+S.SCFG=localecho1,%d\r", 0) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 49 {
mridup 13:f21e4e73bbb6 50 debug_if(dbg_on, "SPWF> error local echo set\r\n");
mridup 0:9e5d98ceea74 51 return false;
mridup 0:9e5d98ceea74 52 }
mridup 0:9e5d98ceea74 53 /*reset factory settings*/
mridup 0:9e5d98ceea74 54 if(!(_parser.send("AT&F") && _parser.recv("OK")))
mridup 0:9e5d98ceea74 55 {
mridup 13:f21e4e73bbb6 56 debug_if(dbg_on, "SPWF> error AT&F\r\n");
mridup 0:9e5d98ceea74 57 return false;
mridup 0:9e5d98ceea74 58 }
mridup 0:9e5d98ceea74 59
mridup 0:9e5d98ceea74 60 /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/
mridup 0:9e5d98ceea74 61 if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", mode) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 62 {
mridup 13:f21e4e73bbb6 63 debug_if(dbg_on, "SPWF> error wifi mode set\r\n");
mridup 0:9e5d98ceea74 64 return false;
mridup 0:9e5d98ceea74 65 }
mridup 0:9e5d98ceea74 66
mridup 0:9e5d98ceea74 67 /* save current setting in flash */
mridup 0:9e5d98ceea74 68 if(!(_parser.send("AT&W") && _parser.recv("OK")))
mridup 0:9e5d98ceea74 69 {
mridup 13:f21e4e73bbb6 70 debug_if(dbg_on, "SPWF> error AT&W\r\n");
mridup 0:9e5d98ceea74 71 return false;
mridup 0:9e5d98ceea74 72 }
mridup 0:9e5d98ceea74 73
mridup 0:9e5d98ceea74 74 /*reset again and send AT command and check for result (AT->OK)*/
mridup 0:9e5d98ceea74 75 reset();
mridup 0:9e5d98ceea74 76
mridup 0:9e5d98ceea74 77 return true;
mridup 0:9e5d98ceea74 78 }
mridup 0:9e5d98ceea74 79
mridup 0:9e5d98ceea74 80 bool SPWFSA01::hw_reset(void)
mridup 0:9e5d98ceea74 81 {
mridup 0:9e5d98ceea74 82 /* reset the pin PC12 */
mridup 0:9e5d98ceea74 83 _reset.write(0);
mridup 0:9e5d98ceea74 84 wait_ms(200);
mridup 0:9e5d98ceea74 85 _reset.write(1);
mridup 0:9e5d98ceea74 86 wait_ms(100);
mridup 0:9e5d98ceea74 87 return 1;
mridup 0:9e5d98ceea74 88 }
mridup 0:9e5d98ceea74 89
mridup 0:9e5d98ceea74 90 bool SPWFSA01::reset(void)
mridup 0:9e5d98ceea74 91 {
mridup 0:9e5d98ceea74 92 if(!_parser.send("AT+CFUN=1")) return false;
mridup 0:9e5d98ceea74 93 while(1) {
mridup 8:a4425b8dd2e4 94 if (_parser.recv("+WIND:32:WiFi Hardware Started\r")) {
mridup 0:9e5d98ceea74 95 return true;
mridup 0:9e5d98ceea74 96 }
mridup 0:9e5d98ceea74 97 }
mridup 0:9e5d98ceea74 98 }
mridup 0:9e5d98ceea74 99
mridup 0:9e5d98ceea74 100 void SPWFSA01::waitSPWFReady(void)
mridup 0:9e5d98ceea74 101 {
mridup 0:9e5d98ceea74 102 //wait_ms(200);
mridup 0:9e5d98ceea74 103 while(1)
mridup 0:9e5d98ceea74 104 if(_parser.send("AT") && _parser.recv("OK"))
mridup 0:9e5d98ceea74 105 //till we get OK from AT command
mridup 0:9e5d98ceea74 106 //printf("\r\nwaiting for reset to complete..\n");
mridup 0:9e5d98ceea74 107 return;
mridup 0:9e5d98ceea74 108
mridup 0:9e5d98ceea74 109 }
mridup 0:9e5d98ceea74 110
mridup 0:9e5d98ceea74 111 /* Security Mode
mridup 0:9e5d98ceea74 112 None = 0,
mridup 0:9e5d98ceea74 113 WEP = 1,
mridup 0:9e5d98ceea74 114 WPA_Personal = 2,
mridup 0:9e5d98ceea74 115 */
mridup 0:9e5d98ceea74 116 bool SPWFSA01::connect(const char *ap, const char *passPhrase, int securityMode)
mridup 0:9e5d98ceea74 117 {
mridup 0:9e5d98ceea74 118 uint32_t n1, n2, n3, n4;
mridup 0:9e5d98ceea74 119
mridup 0:9e5d98ceea74 120 //AT+S.SCFG=wifi_wpa_psk_text,%s\r
mridup 0:9e5d98ceea74 121 if(!(_parser.send("AT+S.SCFG=wifi_wpa_psk_text,%s", passPhrase) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 122 {
mridup 13:f21e4e73bbb6 123 debug_if(dbg_on, "SPWF> error pass set\r\n");
mridup 0:9e5d98ceea74 124 return false;
mridup 0:9e5d98ceea74 125 }
mridup 0:9e5d98ceea74 126 //AT+S.SSIDTXT=%s\r
mridup 0:9e5d98ceea74 127 if(!(_parser.send("AT+S.SSIDTXT=%s", ap) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 128 {
mridup 13:f21e4e73bbb6 129 debug_if(dbg_on, "SPWF> error ssid set\r\n");
mridup 0:9e5d98ceea74 130 return false;
mridup 0:9e5d98ceea74 131 }
mridup 0:9e5d98ceea74 132 //AT+S.SCFG=wifi_priv_mode,%d\r
mridup 0:9e5d98ceea74 133 if(!(_parser.send("AT+S.SCFG=wifi_priv_mode,%d", securityMode) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 134 {
mridup 13:f21e4e73bbb6 135 debug_if(dbg_on, "SPWF> error security mode set\r\n");
mridup 0:9e5d98ceea74 136 return false;
mridup 0:9e5d98ceea74 137 }
mridup 0:9e5d98ceea74 138 //"AT+S.SCFG=wifi_mode,%d\r"
mridup 0:9e5d98ceea74 139 /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/
mridup 0:9e5d98ceea74 140 if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", 1) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 141 {
mridup 13:f21e4e73bbb6 142 debug_if(dbg_on, "SPWF> error wifi mode set\r\n");
mridup 0:9e5d98ceea74 143 return false;
mridup 0:9e5d98ceea74 144 }
mridup 0:9e5d98ceea74 145 //AT&W
mridup 0:9e5d98ceea74 146 /* save current setting in flash */
mridup 0:9e5d98ceea74 147 if(!(_parser.send("AT&W") && _parser.recv("OK")))
mridup 0:9e5d98ceea74 148 {
mridup 13:f21e4e73bbb6 149 debug_if(dbg_on, "SPWF> error AT&W\r\n");
mridup 0:9e5d98ceea74 150 return false;
mridup 0:9e5d98ceea74 151 }
mridup 0:9e5d98ceea74 152 //reset module
mridup 0:9e5d98ceea74 153 reset();
mridup 0:9e5d98ceea74 154
mridup 0:9e5d98ceea74 155 while(1)
mridup 8:a4425b8dd2e4 156 if((_parser.recv("+WIND:24:WiFi Up:%u.%u.%u.%u\r",&n1, &n2, &n3, &n4)))
mridup 0:9e5d98ceea74 157 {
mridup 0:9e5d98ceea74 158 break;
mridup 8:a4425b8dd2e4 159 }
mridup 0:9e5d98ceea74 160
mridup 0:9e5d98ceea74 161 return true;
mridup 0:9e5d98ceea74 162 }
mridup 0:9e5d98ceea74 163
mridup 0:9e5d98ceea74 164 bool SPWFSA01::disconnect(void)
mridup 0:9e5d98ceea74 165 {
mridup 0:9e5d98ceea74 166 //"AT+S.SCFG=wifi_mode,%d\r"
mridup 0:9e5d98ceea74 167 /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/
mridup 0:9e5d98ceea74 168 if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", 0) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 169 {
mridup 13:f21e4e73bbb6 170 debug_if(dbg_on, "SPWF> error wifi mode set\r\n");
mridup 0:9e5d98ceea74 171 return false;
mridup 0:9e5d98ceea74 172 }
mridup 0:9e5d98ceea74 173 //AT&W
mridup 0:9e5d98ceea74 174 /* save current setting in flash */
mridup 0:9e5d98ceea74 175 if(!(_parser.send("AT&W") && _parser.recv("OK")))
mridup 0:9e5d98ceea74 176 {
mridup 13:f21e4e73bbb6 177 debug_if(dbg_on, "SPWF> error AT&W\r\n");
mridup 0:9e5d98ceea74 178 return false;
mridup 0:9e5d98ceea74 179 }
mridup 0:9e5d98ceea74 180 //reset module
mridup 0:9e5d98ceea74 181 reset();
mridup 0:9e5d98ceea74 182 return true;
mridup 0:9e5d98ceea74 183 }
mridup 0:9e5d98ceea74 184
mridup 5:f17d6351e942 185 bool SPWFSA01::dhcp(int mode)
mridup 0:9e5d98ceea74 186 {
mridup 0:9e5d98ceea74 187 //only 3 valid modes
mridup 5:f17d6351e942 188 //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 189 if(mode < 0 || mode > 2) {
mridup 0:9e5d98ceea74 190 return false;
mridup 0:9e5d98ceea74 191 }
mridup 5:f17d6351e942 192
mridup 5:f17d6351e942 193 return _parser.send("AT+S.SCFG=ip_use_dhcp,%d\r", mode)
mridup 0:9e5d98ceea74 194 && _parser.recv("OK");
mridup 0:9e5d98ceea74 195 }
mridup 0:9e5d98ceea74 196
mridup 0:9e5d98ceea74 197
mridup 0:9e5d98ceea74 198 const char *SPWFSA01::getIPAddress(void)
mridup 0:9e5d98ceea74 199 {
mridup 0:9e5d98ceea74 200 uint32_t n1, n2, n3, n4;
mridup 0:9e5d98ceea74 201
mridup 0:9e5d98ceea74 202 if (!(_parser.send("AT+S.STS=ip_ipaddr")
mridup 1:becf69a794fb 203 && _parser.recv("# ip_ipaddr = %u.%u.%u.%u", &n1, &n2, &n3, &n4)
mridup 0:9e5d98ceea74 204 && _parser.recv("OK"))) {
mridup 13:f21e4e73bbb6 205 debug_if(dbg_on, "SPWF> getIPAddress error\r\n");
mridup 0:9e5d98ceea74 206 return 0;
mridup 0:9e5d98ceea74 207 }
mridup 2:40d183d7dc19 208
mridup 0:9e5d98ceea74 209 sprintf((char*)_ip_buffer,"%u.%u.%u.%u", n1, n2, n3, n4);
mridup 0:9e5d98ceea74 210
mridup 0:9e5d98ceea74 211 return _ip_buffer;
mridup 0:9e5d98ceea74 212 }
mridup 0:9e5d98ceea74 213
mridup 0:9e5d98ceea74 214 const char *SPWFSA01::getMACAddress(void)
mridup 0:9e5d98ceea74 215 {
mridup 2:40d183d7dc19 216 uint32_t n1, n2, n3, n4, n5, n6;
mridup 1:becf69a794fb 217
mridup 1:becf69a794fb 218 if (!(_parser.send("AT+S.GCFG=nv_wifi_macaddr")
mridup 2:40d183d7dc19 219 && _parser.recv("# nv_wifi_macaddr = %x:%x:%x:%x:%x:%x", &n1, &n2, &n3, &n4, &n5, &n6)
mridup 0:9e5d98ceea74 220 && _parser.recv("OK"))) {
mridup 13:f21e4e73bbb6 221 debug_if(dbg_on, "SPWF> getMACAddress error\r\n");
mridup 0:9e5d98ceea74 222 return 0;
mridup 0:9e5d98ceea74 223 }
mridup 2:40d183d7dc19 224
mridup 2:40d183d7dc19 225 sprintf((char*)_mac_buffer,"%02X:%02X:%02X:%02X:%02X:%02X", n1, n2, n3, n4, n5, n6);
mridup 0:9e5d98ceea74 226
mridup 0:9e5d98ceea74 227 return _mac_buffer;
mridup 0:9e5d98ceea74 228 }
mridup 0:9e5d98ceea74 229
mridup 0:9e5d98ceea74 230 bool SPWFSA01::isConnected(void)
mridup 0:9e5d98ceea74 231 {
mridup 0:9e5d98ceea74 232 return getIPAddress() != 0;
mridup 0:9e5d98ceea74 233 }
mridup 0:9e5d98ceea74 234
mridup 3:2af9df1068a5 235 bool SPWFSA01::open(const char *type, int* id, const char* addr, int port)
mridup 0:9e5d98ceea74 236 {
mridup 13:f21e4e73bbb6 237 Timer timer;
mridup 13:f21e4e73bbb6 238 timer.start();
mridup 3:2af9df1068a5 239
mridup 3:2af9df1068a5 240 if(!_parser.send("AT+S.SOCKON=%s,%d,%s,ind", addr, port, type))
mridup 3:2af9df1068a5 241 {
mridup 13:f21e4e73bbb6 242 debug_if(dbg_on, "SPWF> error opening socket\r\n");
mridup 3:2af9df1068a5 243 return false;
mridup 3:2af9df1068a5 244 }
mridup 3:2af9df1068a5 245
mridup 3:2af9df1068a5 246 while(1)
mridup 3:2af9df1068a5 247 {
mridup 3:2af9df1068a5 248 if( _parser.recv(" ID: %d", id)
mridup 3:2af9df1068a5 249 && _parser.recv("OK"))
mridup 3:2af9df1068a5 250 break;
mridup 13:f21e4e73bbb6 251
mridup 13:f21e4e73bbb6 252 if (timer.read_ms() > SPWFSA01_CONNECT_TIMEOUT) {
mridup 13:f21e4e73bbb6 253 return false;
mridup 13:f21e4e73bbb6 254 }
mridup 13:f21e4e73bbb6 255
mridup 3:2af9df1068a5 256 //TODO:implement time-out functionality in case of no response
mridup 4:042d5b9bb6e7 257 //if(timeout) return false;
mridup 7:551de95939e2 258 //TODO: deal with errors like "ERROR: Failed to resolve name"
mridup 11:643a8c0f07ea 259 //TODO: deal with errors like "ERROR: Data mode not available"
mridup 3:2af9df1068a5 260 }
mridup 0:9e5d98ceea74 261
mridup 3:2af9df1068a5 262 return true;
mridup 0:9e5d98ceea74 263 }
mridup 0:9e5d98ceea74 264
mridup 0:9e5d98ceea74 265 bool SPWFSA01::send(int id, const void *data, uint32_t amount)
mridup 11:643a8c0f07ea 266 {
mridup 11:643a8c0f07ea 267 char _buf[18];
mridup 11:643a8c0f07ea 268
mridup 11:643a8c0f07ea 269 setTimeout(SPWFSA01_SEND_TIMEOUT);
mridup 11:643a8c0f07ea 270
mridup 11:643a8c0f07ea 271 sprintf((char*)_buf,"AT+S.SOCKW=%d,%d\r", id, amount);
mridup 11:643a8c0f07ea 272
mridup 0:9e5d98ceea74 273 //May take a second try if device is busy
mridup 0:9e5d98ceea74 274 for (unsigned i = 0; i < 2; i++) {
mridup 11:643a8c0f07ea 275 if (_parser.write((char*)_buf, strlen(_buf)) >=0
mridup 7:551de95939e2 276 && _parser.write((char*)data, (int)amount) >= 0
mridup 7:551de95939e2 277 && _parser.recv("OK")) {
mridup 0:9e5d98ceea74 278 return true;
mridup 0:9e5d98ceea74 279 }
mridup 0:9e5d98ceea74 280 }
mridup 0:9e5d98ceea74 281
mridup 0:9e5d98ceea74 282 return false;
mridup 0:9e5d98ceea74 283 }
mridup 0:9e5d98ceea74 284
mridup 0:9e5d98ceea74 285 int32_t SPWFSA01::recv(int id, void *data, uint32_t amount)
mridup 0:9e5d98ceea74 286 {
mridup 13:f21e4e73bbb6 287 uint32_t recv_amount, read_amt;
mridup 0:9e5d98ceea74 288 int recv_id;
mridup 13:f21e4e73bbb6 289 char _buf[18];
mridup 0:9e5d98ceea74 290
mridup 7:551de95939e2 291 if (!(_parser.recv("+WIND:55:Pending Data:%d:%u", &recv_id, &recv_amount)
mridup 6:258b093eec08 292 && recv_id == id
mridup 10:580379efe167 293 //&& _parser.send("AT+S.SOCKQ=%d", id)//send a query (will be required for secure sockets)
mridup 10:580379efe167 294 //&& _parser.recv(" DATALEN: %u", &recv_amount)
mridup 10:580379efe167 295 //&& _parser.recv("OK")
mridup 13:f21e4e73bbb6 296 && recv_amount <= amount)) {
mridup 13:f21e4e73bbb6 297 return -1;//Block call
mridup 13:f21e4e73bbb6 298 }
mridup 13:f21e4e73bbb6 299 //&& sprintf((char*)_buf,"AT+S.SOCKR=%d,%d\r", id, recv_amount)
mridup 13:f21e4e73bbb6 300 //&& _parser.write((char*)_buf, strlen(_buf))
mridup 13:f21e4e73bbb6 301 if (!_parser.send("AT+S.SOCKR=%d,%d", id, recv_amount)) {
mridup 13:f21e4e73bbb6 302 //if error, resend SOCKR again to flush the socket
mridup 13:f21e4e73bbb6 303 //This should prevent Error: Pending Data errors or the next WIND showing this pending data
mridup 13:f21e4e73bbb6 304 sprintf((char*)_buf,"AT+S.SOCKR=%d,%d\r", id, recv_amount);
mridup 13:f21e4e73bbb6 305 _parser.write((char*)_buf, strlen(_buf));
mridup 13:f21e4e73bbb6 306 debug_if(dbg_on, "SPWF> read send SOCKR error\r\n");
mridup 13:f21e4e73bbb6 307 return -2;//return NSAPI_ERROR_DEVICE_ERROR
mridup 13:f21e4e73bbb6 308 }
mridup 13:f21e4e73bbb6 309
mridup 13:f21e4e73bbb6 310 read_amt = _parser.read((char*)data, recv_amount);
mridup 13:f21e4e73bbb6 311 if (read_amt<=0) {
mridup 13:f21e4e73bbb6 312 debug_if(dbg_on, "SPWF> read recv SOCKR error: read_amt = %d\r\n", read_amt);
mridup 13:f21e4e73bbb6 313 return -2;//return NSAPI_ERROR_DEVICE_ERROR
mridup 0:9e5d98ceea74 314 }
mridup 13:f21e4e73bbb6 315
mridup 13:f21e4e73bbb6 316 //Sometimes OK is not received because _parser.read() reads in ERROR statement. "E.g. ERROR: Command not found".
mridup 13:f21e4e73bbb6 317 //ERROR statement hence takes up space in recv_amount and hence OK is not read as a result.
mridup 11:643a8c0f07ea 318
mridup 13:f21e4e73bbb6 319 if (!_parser.recv("OK")) {
mridup 13:f21e4e73bbb6 320 debug_if(dbg_on, "SPWF> read recv SOCKR OK error\r\n");
mridup 13:f21e4e73bbb6 321
mridup 13:f21e4e73bbb6 322 if(std::strncmp("\r\nERROR: ", (const char* )data, 9)==0) {
mridup 13:f21e4e73bbb6 323 debug_if(dbg_on, "SPWF> Module Error Reply\r\n");
mridup 13:f21e4e73bbb6 324 return -2;
mridup 13:f21e4e73bbb6 325 }
mridup 13:f21e4e73bbb6 326
mridup 13:f21e4e73bbb6 327 /*int i = 0;
mridup 13:f21e4e73bbb6 328 char * buf = (char*)data;
mridup 13:f21e4e73bbb6 329
mridup 13:f21e4e73bbb6 330 for ( ; i < recv_amount; i++) {
mridup 13:f21e4e73bbb6 331 debug_if(dbg_on, ("%d = %c\r\n", i, buf[i]);
mridup 13:f21e4e73bbb6 332 }*/
mridup 13:f21e4e73bbb6 333 return -2;//return NSAPI_ERROR_DEVICE_ERROR
mridup 13:f21e4e73bbb6 334 }
mridup 11:643a8c0f07ea 335
mridup 0:9e5d98ceea74 336 return recv_amount;
mridup 0:9e5d98ceea74 337 }
mridup 0:9e5d98ceea74 338
mridup 0:9e5d98ceea74 339 bool SPWFSA01::close(int id)
mridup 0:9e5d98ceea74 340 {
mridup 4:042d5b9bb6e7 341 //May take a second try if device is busy or error is returned
mridup 0:9e5d98ceea74 342 for (unsigned i = 0; i < 2; i++) {
mridup 4:042d5b9bb6e7 343 if (_parser.send("AT+S.SOCKC=%d", id)
mridup 0:9e5d98ceea74 344 && _parser.recv("OK")) {
mridup 0:9e5d98ceea74 345 return true;
mridup 0:9e5d98ceea74 346 }
mridup 7:551de95939e2 347 else
mridup 7:551de95939e2 348 {
mridup 7:551de95939e2 349 if(_parser.recv("ERROR: Pending data"))
mridup 13:f21e4e73bbb6 350 debug_if(dbg_on, "SPWF> ERROR!!!!\r\n");
mridup 7:551de95939e2 351 return false;
mridup 7:551de95939e2 352 }
mridup 6:258b093eec08 353 //TODO: Deal with "ERROR: Pending data" (Closing a socket with pending data)
mridup 0:9e5d98ceea74 354 }
mridup 0:9e5d98ceea74 355
mridup 0:9e5d98ceea74 356 return false;
mridup 0:9e5d98ceea74 357 }
mridup 0:9e5d98ceea74 358
mridup 0:9e5d98ceea74 359 void SPWFSA01::setTimeout(uint32_t timeout_ms)
mridup 0:9e5d98ceea74 360 {
mridup 0:9e5d98ceea74 361 _parser.setTimeout(timeout_ms);
mridup 0:9e5d98ceea74 362 }
mridup 0:9e5d98ceea74 363
mridup 0:9e5d98ceea74 364 bool SPWFSA01::readable()
mridup 0:9e5d98ceea74 365 {
mridup 0:9e5d98ceea74 366 return _serial.readable();
mridup 0:9e5d98ceea74 367 }
mridup 0:9e5d98ceea74 368
mridup 0:9e5d98ceea74 369 bool SPWFSA01::writeable()
mridup 0:9e5d98ceea74 370 {
mridup 0:9e5d98ceea74 371 return _serial.writeable();
mridup 0:9e5d98ceea74 372 }