cc

Dependencies:   ATParser

Committer:
mapellil
Date:
Mon Jan 02 14:46:40 2017 +0000
Revision:
25:6b79352bc1fa
Parent:
24:419285201dba
Child:
26:70852d26853e
Added TLS secure socket API

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 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
mapellil 24:419285201dba 26 SPWFSA01::SPWFSA01(PinName tx, PinName rx, PinName reset, PinName wakeup, bool debug)
mapellil 24:419285201dba 27 : _serial(tx, rx, 1024), _parser(_serial),
mapellil 24:419285201dba 28 _reset(reset, PIN_OUTPUT, PullNone, 1),
mapellil 24:419285201dba 29 _wakeup(wakeup, PIN_OUTPUT, PullNone, 0),
mridup 13:f21e4e73bbb6 30 dbg_on(debug)
mridup 0:9e5d98ceea74 31 {
mapellil 24:419285201dba 32 _serial.baud(115200); // LICIO FIXME increase the speed
mridup 0:9e5d98ceea74 33 _parser.debugOn(debug);
mridup 0:9e5d98ceea74 34 }
mridup 0:9e5d98ceea74 35
mridup 0:9e5d98ceea74 36 bool SPWFSA01::startup(int mode)
mridup 0:9e5d98ceea74 37 {
mapellil 24:419285201dba 38 _parser.setTimeout(SPWFSA01_MISC_TIMEOUT);
mridup 0:9e5d98ceea74 39 /*Test module before reset*/
mridup 0:9e5d98ceea74 40 waitSPWFReady();
mridup 0:9e5d98ceea74 41 /*Reset module*/
mridup 0:9e5d98ceea74 42 reset();
mridup 0:9e5d98ceea74 43
mridup 0:9e5d98ceea74 44 /*set local echo to 0*/
mridup 8:a4425b8dd2e4 45 if(!(_parser.send("AT+S.SCFG=localecho1,%d\r", 0) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 46 {
mridup 13:f21e4e73bbb6 47 debug_if(dbg_on, "SPWF> error local echo set\r\n");
mridup 0:9e5d98ceea74 48 return false;
mridup 0:9e5d98ceea74 49 }
mridup 0:9e5d98ceea74 50 /*reset factory settings*/
mridup 0:9e5d98ceea74 51 if(!(_parser.send("AT&F") && _parser.recv("OK")))
mridup 0:9e5d98ceea74 52 {
mridup 13:f21e4e73bbb6 53 debug_if(dbg_on, "SPWF> error AT&F\r\n");
mridup 0:9e5d98ceea74 54 return false;
mridup 0:9e5d98ceea74 55 }
mansiag 23:0b01aa59bb6f 56
mansiag 23:0b01aa59bb6f 57 /*set Wi-Fi mode and rate to b/g/n*/
mansiag 23:0b01aa59bb6f 58 if(!(_parser.send("AT+S.SCFG=wifi_ht_mode,%d\r",1) && _parser.recv("OK")))
mansiag 23:0b01aa59bb6f 59 {
mansiag 23:0b01aa59bb6f 60 debug_if(dbg_on, "SPWF> error setting ht_mode\r\n");
mansiag 23:0b01aa59bb6f 61 return false;
mansiag 23:0b01aa59bb6f 62 }
mansiag 23:0b01aa59bb6f 63
mansiag 23:0b01aa59bb6f 64 if(!(_parser.send("AT+S.SCFG=wifi_opr_rate_mask,0x003FFFCF\r") && _parser.recv("OK")))
mansiag 23:0b01aa59bb6f 65 {
mansiag 23:0b01aa59bb6f 66 debug_if(dbg_on, "SPWF> error setting operational rates\r\n");
mansiag 23:0b01aa59bb6f 67 return false;
mansiag 23:0b01aa59bb6f 68 }
mridup 0:9e5d98ceea74 69
mridup 0:9e5d98ceea74 70 /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/
mridup 0:9e5d98ceea74 71 if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", mode) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 72 {
mridup 13:f21e4e73bbb6 73 debug_if(dbg_on, "SPWF> error wifi mode set\r\n");
mridup 0:9e5d98ceea74 74 return false;
mridup 0:9e5d98ceea74 75 }
mridup 0:9e5d98ceea74 76
mridup 0:9e5d98ceea74 77 /* save current setting in flash */
mridup 0:9e5d98ceea74 78 if(!(_parser.send("AT&W") && _parser.recv("OK")))
mridup 0:9e5d98ceea74 79 {
mridup 13:f21e4e73bbb6 80 debug_if(dbg_on, "SPWF> error AT&W\r\n");
mridup 0:9e5d98ceea74 81 return false;
mridup 0:9e5d98ceea74 82 }
mridup 0:9e5d98ceea74 83
mridup 0:9e5d98ceea74 84 /*reset again and send AT command and check for result (AT->OK)*/
mridup 0:9e5d98ceea74 85 reset();
mridup 0:9e5d98ceea74 86
mridup 0:9e5d98ceea74 87 return true;
mridup 0:9e5d98ceea74 88 }
mridup 0:9e5d98ceea74 89
mridup 0:9e5d98ceea74 90 bool SPWFSA01::hw_reset(void)
mridup 0:9e5d98ceea74 91 {
mapellil 24:419285201dba 92 if (_reset.is_connected()) {
mridup 0:9e5d98ceea74 93 /* reset the pin PC12 */
mridup 0:9e5d98ceea74 94 _reset.write(0);
mridup 0:9e5d98ceea74 95 wait_ms(200);
mridup 0:9e5d98ceea74 96 _reset.write(1);
mridup 0:9e5d98ceea74 97 wait_ms(100);
mridup 0:9e5d98ceea74 98 return 1;
mapellil 24:419285201dba 99 } else { return 0; }
mridup 0:9e5d98ceea74 100 }
mridup 0:9e5d98ceea74 101
mridup 0:9e5d98ceea74 102 bool SPWFSA01::reset(void)
mridup 0:9e5d98ceea74 103 {
mridup 0:9e5d98ceea74 104 if(!_parser.send("AT+CFUN=1")) return false;
mridup 0:9e5d98ceea74 105 while(1) {
mridup 8:a4425b8dd2e4 106 if (_parser.recv("+WIND:32:WiFi Hardware Started\r")) {
mridup 0:9e5d98ceea74 107 return true;
mridup 0:9e5d98ceea74 108 }
mridup 0:9e5d98ceea74 109 }
mridup 0:9e5d98ceea74 110 }
mridup 0:9e5d98ceea74 111
mridup 0:9e5d98ceea74 112 void SPWFSA01::waitSPWFReady(void)
mridup 0:9e5d98ceea74 113 {
mridup 0:9e5d98ceea74 114 //wait_ms(200);
mridup 0:9e5d98ceea74 115 while(1)
mridup 0:9e5d98ceea74 116 if(_parser.send("AT") && _parser.recv("OK"))
mridup 0:9e5d98ceea74 117 //till we get OK from AT command
mridup 0:9e5d98ceea74 118 //printf("\r\nwaiting for reset to complete..\n");
mridup 0:9e5d98ceea74 119 return;
mapellil 24:419285201dba 120
mridup 0:9e5d98ceea74 121 }
mridup 0:9e5d98ceea74 122
mridup 0:9e5d98ceea74 123 /* Security Mode
mridup 0:9e5d98ceea74 124 None = 0,
mridup 0:9e5d98ceea74 125 WEP = 1,
mridup 0:9e5d98ceea74 126 WPA_Personal = 2,
mridup 0:9e5d98ceea74 127 */
mridup 0:9e5d98ceea74 128 bool SPWFSA01::connect(const char *ap, const char *passPhrase, int securityMode)
mridup 0:9e5d98ceea74 129 {
mridup 0:9e5d98ceea74 130 uint32_t n1, n2, n3, n4;
mapellil 24:419285201dba 131
mapellil 24:419285201dba 132 _parser.setTimeout(SPWFSA01_CONNECT_TIMEOUT);
mridup 0:9e5d98ceea74 133 //AT+S.SCFG=wifi_wpa_psk_text,%s\r
mridup 0:9e5d98ceea74 134 if(!(_parser.send("AT+S.SCFG=wifi_wpa_psk_text,%s", passPhrase) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 135 {
mridup 13:f21e4e73bbb6 136 debug_if(dbg_on, "SPWF> error pass set\r\n");
mridup 0:9e5d98ceea74 137 return false;
mridup 0:9e5d98ceea74 138 }
mridup 0:9e5d98ceea74 139 //AT+S.SSIDTXT=%s\r
mridup 0:9e5d98ceea74 140 if(!(_parser.send("AT+S.SSIDTXT=%s", ap) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 141 {
mridup 13:f21e4e73bbb6 142 debug_if(dbg_on, "SPWF> error ssid set\r\n");
mridup 0:9e5d98ceea74 143 return false;
mridup 0:9e5d98ceea74 144 }
mridup 0:9e5d98ceea74 145 //AT+S.SCFG=wifi_priv_mode,%d\r
mridup 0:9e5d98ceea74 146 if(!(_parser.send("AT+S.SCFG=wifi_priv_mode,%d", securityMode) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 147 {
mridup 13:f21e4e73bbb6 148 debug_if(dbg_on, "SPWF> error security mode set\r\n");
mridup 0:9e5d98ceea74 149 return false;
mridup 0:9e5d98ceea74 150 }
mridup 0:9e5d98ceea74 151 //"AT+S.SCFG=wifi_mode,%d\r"
mridup 0:9e5d98ceea74 152 /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/
mridup 0:9e5d98ceea74 153 if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", 1) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 154 {
mridup 13:f21e4e73bbb6 155 debug_if(dbg_on, "SPWF> error wifi mode set\r\n");
mridup 0:9e5d98ceea74 156 return false;
mridup 0:9e5d98ceea74 157 }
mridup 0:9e5d98ceea74 158 //AT&W
mridup 0:9e5d98ceea74 159 /* save current setting in flash */
mridup 0:9e5d98ceea74 160 if(!(_parser.send("AT&W") && _parser.recv("OK")))
mridup 0:9e5d98ceea74 161 {
mridup 13:f21e4e73bbb6 162 debug_if(dbg_on, "SPWF> error AT&W\r\n");
mridup 0:9e5d98ceea74 163 return false;
mridup 0:9e5d98ceea74 164 }
mridup 0:9e5d98ceea74 165 //reset module
mridup 0:9e5d98ceea74 166 reset();
mridup 0:9e5d98ceea74 167
mridup 0:9e5d98ceea74 168 while(1)
mansiag 18:fd46c2ca8eb7 169 if((_parser.recv("+WIND:24:WiFi Up:%u.%u.%u.%u",&n1, &n2, &n3, &n4)))
mridup 0:9e5d98ceea74 170 {
mridup 0:9e5d98ceea74 171 break;
mridup 8:a4425b8dd2e4 172 }
mridup 0:9e5d98ceea74 173
mridup 0:9e5d98ceea74 174 return true;
mridup 0:9e5d98ceea74 175 }
mridup 0:9e5d98ceea74 176
mridup 0:9e5d98ceea74 177 bool SPWFSA01::disconnect(void)
mridup 0:9e5d98ceea74 178 {
mridup 0:9e5d98ceea74 179 //"AT+S.SCFG=wifi_mode,%d\r"
mridup 0:9e5d98ceea74 180 /*set idle mode (0->idle, 1->STA,3->miniAP, 2->IBSS)*/
mridup 0:9e5d98ceea74 181 if(!(_parser.send("AT+S.SCFG=wifi_mode,%d\r", 0) && _parser.recv("OK")))
mridup 0:9e5d98ceea74 182 {
mridup 13:f21e4e73bbb6 183 debug_if(dbg_on, "SPWF> error wifi mode set\r\n");
mridup 0:9e5d98ceea74 184 return false;
mridup 0:9e5d98ceea74 185 }
mridup 0:9e5d98ceea74 186 //AT&W
mridup 0:9e5d98ceea74 187 /* save current setting in flash */
mridup 0:9e5d98ceea74 188 if(!(_parser.send("AT&W") && _parser.recv("OK")))
mridup 0:9e5d98ceea74 189 {
mridup 13:f21e4e73bbb6 190 debug_if(dbg_on, "SPWF> error AT&W\r\n");
mridup 0:9e5d98ceea74 191 return false;
mridup 0:9e5d98ceea74 192 }
mridup 0:9e5d98ceea74 193 //reset module
mridup 0:9e5d98ceea74 194 reset();
mridup 0:9e5d98ceea74 195 return true;
mridup 0:9e5d98ceea74 196 }
mridup 0:9e5d98ceea74 197
mridup 5:f17d6351e942 198 bool SPWFSA01::dhcp(int mode)
mridup 0:9e5d98ceea74 199 {
mridup 0:9e5d98ceea74 200 //only 3 valid modes
mridup 5:f17d6351e942 201 //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 202 if(mode < 0 || mode > 2) {
mridup 0:9e5d98ceea74 203 return false;
mridup 0:9e5d98ceea74 204 }
mridup 5:f17d6351e942 205
mridup 5:f17d6351e942 206 return _parser.send("AT+S.SCFG=ip_use_dhcp,%d\r", mode)
mridup 0:9e5d98ceea74 207 && _parser.recv("OK");
mridup 0:9e5d98ceea74 208 }
mridup 0:9e5d98ceea74 209
mridup 0:9e5d98ceea74 210
mridup 0:9e5d98ceea74 211 const char *SPWFSA01::getIPAddress(void)
mridup 0:9e5d98ceea74 212 {
mridup 0:9e5d98ceea74 213 uint32_t n1, n2, n3, n4;
mridup 0:9e5d98ceea74 214
mridup 0:9e5d98ceea74 215 if (!(_parser.send("AT+S.STS=ip_ipaddr")
mridup 1:becf69a794fb 216 && _parser.recv("# ip_ipaddr = %u.%u.%u.%u", &n1, &n2, &n3, &n4)
mridup 0:9e5d98ceea74 217 && _parser.recv("OK"))) {
mridup 13:f21e4e73bbb6 218 debug_if(dbg_on, "SPWF> getIPAddress error\r\n");
mridup 0:9e5d98ceea74 219 return 0;
mridup 0:9e5d98ceea74 220 }
mridup 2:40d183d7dc19 221
mridup 0:9e5d98ceea74 222 sprintf((char*)_ip_buffer,"%u.%u.%u.%u", n1, n2, n3, n4);
mridup 0:9e5d98ceea74 223
mridup 0:9e5d98ceea74 224 return _ip_buffer;
mridup 0:9e5d98ceea74 225 }
mridup 0:9e5d98ceea74 226
mridup 0:9e5d98ceea74 227 const char *SPWFSA01::getMACAddress(void)
mridup 0:9e5d98ceea74 228 {
mridup 2:40d183d7dc19 229 uint32_t n1, n2, n3, n4, n5, n6;
mridup 1:becf69a794fb 230
mridup 1:becf69a794fb 231 if (!(_parser.send("AT+S.GCFG=nv_wifi_macaddr")
mridup 2:40d183d7dc19 232 && _parser.recv("# nv_wifi_macaddr = %x:%x:%x:%x:%x:%x", &n1, &n2, &n3, &n4, &n5, &n6)
mridup 0:9e5d98ceea74 233 && _parser.recv("OK"))) {
mridup 13:f21e4e73bbb6 234 debug_if(dbg_on, "SPWF> getMACAddress error\r\n");
mridup 0:9e5d98ceea74 235 return 0;
mridup 0:9e5d98ceea74 236 }
mridup 2:40d183d7dc19 237
mridup 2:40d183d7dc19 238 sprintf((char*)_mac_buffer,"%02X:%02X:%02X:%02X:%02X:%02X", n1, n2, n3, n4, n5, n6);
mridup 0:9e5d98ceea74 239
mridup 0:9e5d98ceea74 240 return _mac_buffer;
mridup 0:9e5d98ceea74 241 }
mridup 0:9e5d98ceea74 242
mridup 0:9e5d98ceea74 243 bool SPWFSA01::isConnected(void)
mridup 0:9e5d98ceea74 244 {
mapellil 24:419285201dba 245 return getIPAddress() != 0;
mridup 0:9e5d98ceea74 246 }
mridup 0:9e5d98ceea74 247
mridup 3:2af9df1068a5 248 bool SPWFSA01::open(const char *type, int* id, const char* addr, int port)
mridup 0:9e5d98ceea74 249 {
mridup 13:f21e4e73bbb6 250 Timer timer;
mridup 13:f21e4e73bbb6 251 timer.start();
mapellil 24:419285201dba 252 socket_closed = 0;
mapellil 25:6b79352bc1fa 253
mridup 3:2af9df1068a5 254 if(!_parser.send("AT+S.SOCKON=%s,%d,%s,ind", addr, port, type))
mridup 3:2af9df1068a5 255 {
mridup 13:f21e4e73bbb6 256 debug_if(dbg_on, "SPWF> error opening socket\r\n");
mridup 3:2af9df1068a5 257 return false;
mridup 3:2af9df1068a5 258 }
mapellil 25:6b79352bc1fa 259
mridup 3:2af9df1068a5 260 while(1)
mridup 3:2af9df1068a5 261 {
mridup 3:2af9df1068a5 262 if( _parser.recv(" ID: %d", id)
mridup 3:2af9df1068a5 263 && _parser.recv("OK"))
mridup 3:2af9df1068a5 264 break;
mridup 13:f21e4e73bbb6 265
mridup 13:f21e4e73bbb6 266 if (timer.read_ms() > SPWFSA01_CONNECT_TIMEOUT) {
mridup 13:f21e4e73bbb6 267 return false;
mridup 13:f21e4e73bbb6 268 }
mridup 13:f21e4e73bbb6 269
mridup 3:2af9df1068a5 270 //TODO:implement time-out functionality in case of no response
mridup 4:042d5b9bb6e7 271 //if(timeout) return false;
mridup 7:551de95939e2 272 //TODO: deal with errors like "ERROR: Failed to resolve name"
mridup 11:643a8c0f07ea 273 //TODO: deal with errors like "ERROR: Data mode not available"
mridup 3:2af9df1068a5 274 }
mridup 3:2af9df1068a5 275 return true;
mridup 0:9e5d98ceea74 276 }
mridup 0:9e5d98ceea74 277
mridup 0:9e5d98ceea74 278 bool SPWFSA01::send(int id, const void *data, uint32_t amount)
mridup 11:643a8c0f07ea 279 {
mridup 11:643a8c0f07ea 280 char _buf[18];
mapellil 24:419285201dba 281 _parser.setTimeout(SPWFSA01_SEND_TIMEOUT);
mridup 11:643a8c0f07ea 282
mridup 11:643a8c0f07ea 283 sprintf((char*)_buf,"AT+S.SOCKW=%d,%d\r", id, amount);
mridup 11:643a8c0f07ea 284
mridup 0:9e5d98ceea74 285 //May take a second try if device is busy
mridup 0:9e5d98ceea74 286 for (unsigned i = 0; i < 2; i++) {
mridup 11:643a8c0f07ea 287 if (_parser.write((char*)_buf, strlen(_buf)) >=0
mridup 7:551de95939e2 288 && _parser.write((char*)data, (int)amount) >= 0
mridup 7:551de95939e2 289 && _parser.recv("OK")) {
mridup 0:9e5d98ceea74 290 return true;
mapellil 24:419285201dba 291 }
mridup 0:9e5d98ceea74 292 }
mridup 0:9e5d98ceea74 293 return false;
mridup 0:9e5d98ceea74 294 }
mridup 0:9e5d98ceea74 295
mansiag 19:df8d7f70a740 296
mansiag 19:df8d7f70a740 297 int32_t SPWFSA01::recv(int id, void *data, uint32_t amount)
mansiag 19:df8d7f70a740 298 {
mansiag 19:df8d7f70a740 299 uint32_t recv_amount=0;
mapellil 24:419285201dba 300 int wind_id;
mapellil 24:419285201dba 301
mapellil 24:419285201dba 302 if (socket_closed) {
mapellil 24:419285201dba 303 socket_closed = 0;
mapellil 24:419285201dba 304 return -3;
mapellil 24:419285201dba 305 }
mapellil 24:419285201dba 306 if(!(_parser.send("AT+S.SOCKQ=%d", id) //send a query (will be required for secure sockets)
mansiag 19:df8d7f70a740 307 && _parser.recv(" DATALEN: %u", &recv_amount)
mansiag 19:df8d7f70a740 308 && _parser.recv("OK"))) {
mapellil 24:419285201dba 309 return -2;
mapellil 24:419285201dba 310 }
mapellil 24:419285201dba 311 if (recv_amount==0) { return -1; }
mansiag 19:df8d7f70a740 312 if(recv_amount > amount)
mansiag 19:df8d7f70a740 313 recv_amount = amount;
mansiag 19:df8d7f70a740 314
mapellil 24:419285201dba 315 int par_timeout = _parser.getTimeout();
mapellil 24:419285201dba 316 _parser.setTimeout(0);
mansiag 21:cd722edbb644 317
mapellil 24:419285201dba 318 while(_parser.recv("+WIND:%d:", &wind_id)) {
mapellil 24:419285201dba 319 // printf("Wind received: %d\n\r", wind_id);
mapellil 24:419285201dba 320 if (wind_id == 58) {
mapellil 24:419285201dba 321 socket_closed = 1;
mapellil 24:419285201dba 322 _parser.flush();
mansiag 21:cd722edbb644 323 }
mapellil 24:419285201dba 324 }
mapellil 24:419285201dba 325 _parser.setTimeout(par_timeout);
mapellil 24:419285201dba 326
mapellil 24:419285201dba 327 _parser.flush();
mapellil 24:419285201dba 328 if(!(_parser.send("AT+S.SOCKR=%d,%d", id, recv_amount))){
mapellil 24:419285201dba 329 return -2;
mapellil 24:419285201dba 330 }
mapellil 24:419285201dba 331 if(!((_parser.read((char*)data, recv_amount) >0)
mansiag 21:cd722edbb644 332 && _parser.recv("OK"))) {
mapellil 24:419285201dba 333 return -2;
mapellil 24:419285201dba 334 }
mansiag 19:df8d7f70a740 335 return recv_amount;
mansiag 19:df8d7f70a740 336 }
mridup 0:9e5d98ceea74 337
mridup 0:9e5d98ceea74 338 bool SPWFSA01::close(int id)
mridup 0:9e5d98ceea74 339 {
mapellil 24:419285201dba 340 uint32_t recv_amount=0;
mapellil 24:419285201dba 341 void * data = NULL;
mapellil 24:419285201dba 342
mapellil 24:419285201dba 343 _parser.setTimeout(SPWFSA01_MISC_TIMEOUT);
mapellil 24:419285201dba 344 _parser.flush();
mapellil 24:419285201dba 345 /* socket flush */
mapellil 24:419285201dba 346 if(!(_parser.send("AT+S.SOCKQ=%d", id) //send a query (will be required for secure sockets)
mapellil 24:419285201dba 347 && _parser.recv(" DATALEN: %u", &recv_amount)
mapellil 24:419285201dba 348 && _parser.recv("OK"))) {
mapellil 24:419285201dba 349 return -2;
mapellil 24:419285201dba 350 }
mapellil 24:419285201dba 351 if (recv_amount>0) {
mapellil 24:419285201dba 352 data = malloc (recv_amount+4);
mapellil 24:419285201dba 353 if(!(_parser.send("AT+S.SOCKR=%d,%d", id, recv_amount))) {
mapellil 24:419285201dba 354 free (data);
mapellil 24:419285201dba 355 return -2;
mapellil 24:419285201dba 356 }
mapellil 24:419285201dba 357 // printf ("--->>>Close flushing recv_amount: %d \n\r",recv_amount);
mapellil 24:419285201dba 358 if(!((_parser.read((char*)data, recv_amount) >0)
mapellil 24:419285201dba 359 && _parser.recv("OK"))) {
mapellil 24:419285201dba 360 free (data);
mapellil 24:419285201dba 361 return -2;
mapellil 24:419285201dba 362 }
mapellil 24:419285201dba 363 free (data);
mapellil 24:419285201dba 364 }
mapellil 24:419285201dba 365
mridup 4:042d5b9bb6e7 366 //May take a second try if device is busy or error is returned
mridup 0:9e5d98ceea74 367 for (unsigned i = 0; i < 2; i++) {
mridup 4:042d5b9bb6e7 368 if (_parser.send("AT+S.SOCKC=%d", id)
mridup 0:9e5d98ceea74 369 && _parser.recv("OK")) {
mapellil 24:419285201dba 370 socket_closed = 1;
mridup 0:9e5d98ceea74 371 return true;
mridup 0:9e5d98ceea74 372 }
mridup 7:551de95939e2 373 else
mapellil 24:419285201dba 374 {
mapellil 24:419285201dba 375 if(_parser.recv("ERROR: Pending data")) {
mridup 13:f21e4e73bbb6 376 debug_if(dbg_on, "SPWF> ERROR!!!!\r\n");
mridup 7:551de95939e2 377 return false;
mapellil 24:419285201dba 378 }
mapellil 24:419285201dba 379 }
mridup 6:258b093eec08 380 //TODO: Deal with "ERROR: Pending data" (Closing a socket with pending data)
mridup 0:9e5d98ceea74 381 }
mridup 0:9e5d98ceea74 382 return false;
mridup 0:9e5d98ceea74 383 }
mridup 0:9e5d98ceea74 384
mridup 0:9e5d98ceea74 385
mridup 0:9e5d98ceea74 386 bool SPWFSA01::readable()
mridup 0:9e5d98ceea74 387 {
mridup 0:9e5d98ceea74 388 return _serial.readable();
mridup 0:9e5d98ceea74 389 }
mridup 0:9e5d98ceea74 390
mridup 0:9e5d98ceea74 391 bool SPWFSA01::writeable()
mridup 0:9e5d98ceea74 392 {
mridup 0:9e5d98ceea74 393 return _serial.writeable();
mridup 0:9e5d98ceea74 394 }
mapellil 25:6b79352bc1fa 395
mapellil 25:6b79352bc1fa 396 int32_t SPWFSA01::settime(time_t ctTime)
mapellil 25:6b79352bc1fa 397 {
mapellil 25:6b79352bc1fa 398 _parser.flush();
mapellil 25:6b79352bc1fa 399 //May take a second try if device is busy or error is returned
mapellil 25:6b79352bc1fa 400 for (unsigned i = 0; i < 2; i++) {
mapellil 25:6b79352bc1fa 401 if (_parser.send("AT+S.SETTIME=%d", ctTime)
mapellil 25:6b79352bc1fa 402 && _parser.recv("OK")) {
mapellil 25:6b79352bc1fa 403 return true;
mapellil 25:6b79352bc1fa 404 }
mapellil 25:6b79352bc1fa 405 else
mapellil 25:6b79352bc1fa 406 {
mapellil 25:6b79352bc1fa 407 debug_if(dbg_on, "SPWF> ERROR!!!!\r\n");
mapellil 25:6b79352bc1fa 408 return false;
mapellil 25:6b79352bc1fa 409 }
mapellil 25:6b79352bc1fa 410 }
mapellil 25:6b79352bc1fa 411 return false;
mapellil 25:6b79352bc1fa 412 }
mapellil 25:6b79352bc1fa 413
mapellil 25:6b79352bc1fa 414 int32_t SPWFSA01::setTLScertificate(char * cert, unsigned int size, CertType_t type)
mapellil 25:6b79352bc1fa 415 {
mapellil 25:6b79352bc1fa 416 _parser.flush();
mapellil 25:6b79352bc1fa 417 const char * cert_type="ca";
mapellil 25:6b79352bc1fa 418 switch (type) {
mapellil 25:6b79352bc1fa 419 case RAM_CA_ROOT_CERT:
mapellil 25:6b79352bc1fa 420 printf ("RAM_CA_ROOT_CERT\n\r");
mapellil 25:6b79352bc1fa 421 cert_type="ca";
mapellil 25:6b79352bc1fa 422 break;
mapellil 25:6b79352bc1fa 423 case RAM_CLIENT_CERT:
mapellil 25:6b79352bc1fa 424 cert_type="cert";
mapellil 25:6b79352bc1fa 425 break;
mapellil 25:6b79352bc1fa 426 case RAM_CLIENT_PRIV_KEY:
mapellil 25:6b79352bc1fa 427 cert_type="key";
mapellil 25:6b79352bc1fa 428 break;
mapellil 25:6b79352bc1fa 429 case FLASH_CA_ROOT_CERT:
mapellil 25:6b79352bc1fa 430 cert_type="f_ca";
mapellil 25:6b79352bc1fa 431 break;
mapellil 25:6b79352bc1fa 432 case FLASH_CLIENT_CERT:
mapellil 25:6b79352bc1fa 433 cert_type="f_cert";
mapellil 25:6b79352bc1fa 434 break;
mapellil 25:6b79352bc1fa 435 case FLASH_CLIENT_PRIV_KEY:
mapellil 25:6b79352bc1fa 436 cert_type="f_key";
mapellil 25:6b79352bc1fa 437 break;
mapellil 25:6b79352bc1fa 438 default:
mapellil 25:6b79352bc1fa 439 printf ("Error Unknown certificate type\n\r");
mapellil 25:6b79352bc1fa 440 return false;
mapellil 25:6b79352bc1fa 441 }
mapellil 25:6b79352bc1fa 442
mapellil 25:6b79352bc1fa 443 //May take a second try if device is busy or error is returned
mapellil 25:6b79352bc1fa 444 for (unsigned i = 0; i < 2; i++) {
mapellil 25:6b79352bc1fa 445 if (_parser.send("AT+S.TLSCERT=%s,%d\r%s", cert_type, size, cert)
mapellil 25:6b79352bc1fa 446 && _parser.recv("OK")) {
mapellil 25:6b79352bc1fa 447 // printf ("SET CERT cert_type %s, cert: %s\n\r", cert_type, cert);
mapellil 25:6b79352bc1fa 448 return true;
mapellil 25:6b79352bc1fa 449 }
mapellil 25:6b79352bc1fa 450 else
mapellil 25:6b79352bc1fa 451 {
mapellil 25:6b79352bc1fa 452 printf ("SET CERT ERROR cert_type %s, cert: %s\n\r", cert_type, cert);
mapellil 25:6b79352bc1fa 453 debug_if(dbg_on, "SPWF> ERROR!!!!\r\n");
mapellil 25:6b79352bc1fa 454 return false;
mapellil 25:6b79352bc1fa 455 }
mapellil 25:6b79352bc1fa 456 }
mapellil 25:6b79352bc1fa 457 return false;
mapellil 25:6b79352bc1fa 458
mapellil 25:6b79352bc1fa 459 }
mapellil 25:6b79352bc1fa 460
mapellil 25:6b79352bc1fa 461 int32_t SPWFSA01::setTLSSRVdomain(char * domain, CertType_t type)
mapellil 25:6b79352bc1fa 462 {
mapellil 25:6b79352bc1fa 463 if (type == FLASH_DOMAIN)
mapellil 25:6b79352bc1fa 464 {
mapellil 25:6b79352bc1fa 465 if (_parser.send("AT+S.TLSDOMAIN=f_domain,%s", domain)) {
mapellil 25:6b79352bc1fa 466 // printf ("domain set %s\n\r", domain);
mapellil 25:6b79352bc1fa 467 return true;
mapellil 25:6b79352bc1fa 468 } else {
mapellil 25:6b79352bc1fa 469 printf ("ERROR domain not set \n\r");
mapellil 25:6b79352bc1fa 470 return false;
mapellil 25:6b79352bc1fa 471 }
mapellil 25:6b79352bc1fa 472
mapellil 25:6b79352bc1fa 473 } else {
mapellil 25:6b79352bc1fa 474 return false;
mapellil 25:6b79352bc1fa 475 }
mapellil 25:6b79352bc1fa 476 }
mapellil 25:6b79352bc1fa 477
mapellil 25:6b79352bc1fa 478 int32_t SPWFSA01::cleanTLScertificate(CertType_t type)
mapellil 25:6b79352bc1fa 479 {
mapellil 25:6b79352bc1fa 480 if (type == ALL)
mapellil 25:6b79352bc1fa 481 {
mapellil 25:6b79352bc1fa 482 if (_parser.send("AT+S.TLSCERT2=all")) {
mapellil 25:6b79352bc1fa 483 return true;
mapellil 25:6b79352bc1fa 484 } else {
mapellil 25:6b79352bc1fa 485 printf ("ERROR cleaned \n\r");
mapellil 25:6b79352bc1fa 486 return false;
mapellil 25:6b79352bc1fa 487 }
mapellil 25:6b79352bc1fa 488
mapellil 25:6b79352bc1fa 489 } else {
mapellil 25:6b79352bc1fa 490 return false;
mapellil 25:6b79352bc1fa 491 }
mapellil 25:6b79352bc1fa 492
mapellil 25:6b79352bc1fa 493 }