This is boring
SPWFSA01.cpp@29:4fdbec0e3b7b, 2020-04-09 (annotated)
- Committer:
- sng_hws
- Date:
- Thu Apr 09 17:43:06 2020 +0000
- Revision:
- 29:4fdbec0e3b7b
- Parent:
- 27:0dc1402f42a9
no idea
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 | |
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 | 26:70852d26853e | 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 | int32_t SPWFSA01::recv(int id, void *data, uint32_t amount) |
mansiag | 19:df8d7f70a740 | 297 | { |
mansiag | 19:df8d7f70a740 | 298 | uint32_t recv_amount=0; |
mapellil | 24:419285201dba | 299 | int wind_id; |
mapellil | 24:419285201dba | 300 | |
mapellil | 24:419285201dba | 301 | if (socket_closed) { |
mapellil | 24:419285201dba | 302 | socket_closed = 0; |
mapellil | 24:419285201dba | 303 | return -3; |
mapellil | 24:419285201dba | 304 | } |
mapellil | 24:419285201dba | 305 | if(!(_parser.send("AT+S.SOCKQ=%d", id) //send a query (will be required for secure sockets) |
mansiag | 19:df8d7f70a740 | 306 | && _parser.recv(" DATALEN: %u", &recv_amount) |
mansiag | 19:df8d7f70a740 | 307 | && _parser.recv("OK"))) { |
mapellil | 24:419285201dba | 308 | return -2; |
mapellil | 24:419285201dba | 309 | } |
mapellil | 24:419285201dba | 310 | if (recv_amount==0) { return -1; } |
mansiag | 19:df8d7f70a740 | 311 | if(recv_amount > amount) |
mansiag | 19:df8d7f70a740 | 312 | recv_amount = amount; |
mansiag | 19:df8d7f70a740 | 313 | |
mapellil | 24:419285201dba | 314 | int par_timeout = _parser.getTimeout(); |
mapellil | 24:419285201dba | 315 | _parser.setTimeout(0); |
mansiag | 21:cd722edbb644 | 316 | |
mapellil | 24:419285201dba | 317 | while(_parser.recv("+WIND:%d:", &wind_id)) { |
mapellil | 24:419285201dba | 318 | if (wind_id == 58) { |
mapellil | 24:419285201dba | 319 | socket_closed = 1; |
mapellil | 24:419285201dba | 320 | _parser.flush(); |
mansiag | 21:cd722edbb644 | 321 | } |
mapellil | 24:419285201dba | 322 | } |
mapellil | 24:419285201dba | 323 | _parser.setTimeout(par_timeout); |
mapellil | 24:419285201dba | 324 | |
mapellil | 24:419285201dba | 325 | _parser.flush(); |
mapellil | 24:419285201dba | 326 | if(!(_parser.send("AT+S.SOCKR=%d,%d", id, recv_amount))){ |
mapellil | 24:419285201dba | 327 | return -2; |
mapellil | 24:419285201dba | 328 | } |
mapellil | 24:419285201dba | 329 | if(!((_parser.read((char*)data, recv_amount) >0) |
mansiag | 21:cd722edbb644 | 330 | && _parser.recv("OK"))) { |
mapellil | 24:419285201dba | 331 | return -2; |
mapellil | 24:419285201dba | 332 | } |
mansiag | 19:df8d7f70a740 | 333 | return recv_amount; |
mansiag | 19:df8d7f70a740 | 334 | } |
mridup | 0:9e5d98ceea74 | 335 | |
mridup | 0:9e5d98ceea74 | 336 | bool SPWFSA01::close(int id) |
mridup | 0:9e5d98ceea74 | 337 | { |
mapellil | 24:419285201dba | 338 | uint32_t recv_amount=0; |
mapellil | 24:419285201dba | 339 | void * data = NULL; |
mapellil | 24:419285201dba | 340 | |
mapellil | 24:419285201dba | 341 | _parser.setTimeout(SPWFSA01_MISC_TIMEOUT); |
mapellil | 24:419285201dba | 342 | _parser.flush(); |
mapellil | 24:419285201dba | 343 | /* socket flush */ |
mapellil | 24:419285201dba | 344 | if(!(_parser.send("AT+S.SOCKQ=%d", id) //send a query (will be required for secure sockets) |
mapellil | 24:419285201dba | 345 | && _parser.recv(" DATALEN: %u", &recv_amount) |
mapellil | 24:419285201dba | 346 | && _parser.recv("OK"))) { |
mapellil | 24:419285201dba | 347 | return -2; |
mapellil | 24:419285201dba | 348 | } |
mapellil | 24:419285201dba | 349 | if (recv_amount>0) { |
mapellil | 24:419285201dba | 350 | data = malloc (recv_amount+4); |
mapellil | 24:419285201dba | 351 | if(!(_parser.send("AT+S.SOCKR=%d,%d", id, recv_amount))) { |
mapellil | 24:419285201dba | 352 | free (data); |
mapellil | 24:419285201dba | 353 | return -2; |
mapellil | 24:419285201dba | 354 | } |
mapellil | 24:419285201dba | 355 | if(!((_parser.read((char*)data, recv_amount) >0) |
mapellil | 24:419285201dba | 356 | && _parser.recv("OK"))) { |
mapellil | 24:419285201dba | 357 | free (data); |
mapellil | 24:419285201dba | 358 | return -2; |
mapellil | 24:419285201dba | 359 | } |
mapellil | 24:419285201dba | 360 | free (data); |
mapellil | 24:419285201dba | 361 | } |
mapellil | 24:419285201dba | 362 | |
mridup | 4:042d5b9bb6e7 | 363 | //May take a second try if device is busy or error is returned |
mridup | 0:9e5d98ceea74 | 364 | for (unsigned i = 0; i < 2; i++) { |
mridup | 4:042d5b9bb6e7 | 365 | if (_parser.send("AT+S.SOCKC=%d", id) |
mridup | 0:9e5d98ceea74 | 366 | && _parser.recv("OK")) { |
mapellil | 24:419285201dba | 367 | socket_closed = 1; |
mridup | 0:9e5d98ceea74 | 368 | return true; |
mridup | 0:9e5d98ceea74 | 369 | } |
mridup | 7:551de95939e2 | 370 | else |
mapellil | 24:419285201dba | 371 | { |
mapellil | 24:419285201dba | 372 | if(_parser.recv("ERROR: Pending data")) { |
mridup | 13:f21e4e73bbb6 | 373 | debug_if(dbg_on, "SPWF> ERROR!!!!\r\n"); |
mridup | 7:551de95939e2 | 374 | return false; |
mapellil | 24:419285201dba | 375 | } |
mapellil | 24:419285201dba | 376 | } |
mridup | 6:258b093eec08 | 377 | //TODO: Deal with "ERROR: Pending data" (Closing a socket with pending data) |
mridup | 0:9e5d98ceea74 | 378 | } |
mridup | 0:9e5d98ceea74 | 379 | return false; |
mridup | 0:9e5d98ceea74 | 380 | } |
mridup | 0:9e5d98ceea74 | 381 | |
mridup | 0:9e5d98ceea74 | 382 | |
mridup | 0:9e5d98ceea74 | 383 | bool SPWFSA01::readable() |
mridup | 0:9e5d98ceea74 | 384 | { |
mridup | 0:9e5d98ceea74 | 385 | return _serial.readable(); |
mridup | 0:9e5d98ceea74 | 386 | } |
mridup | 0:9e5d98ceea74 | 387 | |
mridup | 0:9e5d98ceea74 | 388 | bool SPWFSA01::writeable() |
mridup | 0:9e5d98ceea74 | 389 | { |
mridup | 0:9e5d98ceea74 | 390 | return _serial.writeable(); |
mridup | 0:9e5d98ceea74 | 391 | } |
mapellil | 25:6b79352bc1fa | 392 | |
mapellil | 25:6b79352bc1fa | 393 | int32_t SPWFSA01::settime(time_t ctTime) |
mapellil | 25:6b79352bc1fa | 394 | { |
mapellil | 25:6b79352bc1fa | 395 | _parser.flush(); |
mapellil | 25:6b79352bc1fa | 396 | //May take a second try if device is busy or error is returned |
mapellil | 25:6b79352bc1fa | 397 | for (unsigned i = 0; i < 2; i++) { |
mapellil | 25:6b79352bc1fa | 398 | if (_parser.send("AT+S.SETTIME=%d", ctTime) |
mapellil | 25:6b79352bc1fa | 399 | && _parser.recv("OK")) { |
mapellil | 25:6b79352bc1fa | 400 | return true; |
mapellil | 25:6b79352bc1fa | 401 | } |
mapellil | 25:6b79352bc1fa | 402 | else |
mapellil | 25:6b79352bc1fa | 403 | { |
mapellil | 25:6b79352bc1fa | 404 | debug_if(dbg_on, "SPWF> ERROR!!!!\r\n"); |
mapellil | 25:6b79352bc1fa | 405 | return false; |
mapellil | 25:6b79352bc1fa | 406 | } |
mapellil | 25:6b79352bc1fa | 407 | } |
mapellil | 25:6b79352bc1fa | 408 | return false; |
mapellil | 25:6b79352bc1fa | 409 | } |
mapellil | 25:6b79352bc1fa | 410 | |
mapellil | 27:0dc1402f42a9 | 411 | int32_t SPWFSA01::gettime(time_t *ctTime) |
mapellil | 27:0dc1402f42a9 | 412 | { |
mapellil | 27:0dc1402f42a9 | 413 | _parser.flush(); |
mapellil | 27:0dc1402f42a9 | 414 | //May take a second try if device is busy or error is returned |
mapellil | 27:0dc1402f42a9 | 415 | for (unsigned i = 0; i < 2; i++) { |
mapellil | 27:0dc1402f42a9 | 416 | if (_parser.send("AT+S.STS=current_time") |
mapellil | 27:0dc1402f42a9 | 417 | && _parser.recv("# current_time = %u", ctTime) |
mapellil | 27:0dc1402f42a9 | 418 | && _parser.recv("OK")) { |
mapellil | 27:0dc1402f42a9 | 419 | return true; |
mapellil | 27:0dc1402f42a9 | 420 | } |
mapellil | 27:0dc1402f42a9 | 421 | else |
mapellil | 27:0dc1402f42a9 | 422 | { |
mapellil | 27:0dc1402f42a9 | 423 | debug_if(dbg_on, "SPWF> ERROR!!!!\r\n"); |
mapellil | 27:0dc1402f42a9 | 424 | return false; |
mapellil | 27:0dc1402f42a9 | 425 | } |
mapellil | 27:0dc1402f42a9 | 426 | } |
mapellil | 27:0dc1402f42a9 | 427 | return false; |
mapellil | 27:0dc1402f42a9 | 428 | } |
mapellil | 27:0dc1402f42a9 | 429 | |
mapellil | 25:6b79352bc1fa | 430 | int32_t SPWFSA01::setTLScertificate(char * cert, unsigned int size, CertType_t type) |
mapellil | 25:6b79352bc1fa | 431 | { |
mapellil | 25:6b79352bc1fa | 432 | _parser.flush(); |
mapellil | 25:6b79352bc1fa | 433 | const char * cert_type="ca"; |
mapellil | 25:6b79352bc1fa | 434 | switch (type) { |
mapellil | 25:6b79352bc1fa | 435 | case RAM_CA_ROOT_CERT: |
mapellil | 25:6b79352bc1fa | 436 | printf ("RAM_CA_ROOT_CERT\n\r"); |
mapellil | 25:6b79352bc1fa | 437 | cert_type="ca"; |
mapellil | 25:6b79352bc1fa | 438 | break; |
mapellil | 25:6b79352bc1fa | 439 | case RAM_CLIENT_CERT: |
mapellil | 25:6b79352bc1fa | 440 | cert_type="cert"; |
mapellil | 25:6b79352bc1fa | 441 | break; |
mapellil | 25:6b79352bc1fa | 442 | case RAM_CLIENT_PRIV_KEY: |
mapellil | 25:6b79352bc1fa | 443 | cert_type="key"; |
mapellil | 25:6b79352bc1fa | 444 | break; |
mapellil | 25:6b79352bc1fa | 445 | case FLASH_CA_ROOT_CERT: |
mapellil | 25:6b79352bc1fa | 446 | cert_type="f_ca"; |
mapellil | 25:6b79352bc1fa | 447 | break; |
mapellil | 25:6b79352bc1fa | 448 | case FLASH_CLIENT_CERT: |
mapellil | 25:6b79352bc1fa | 449 | cert_type="f_cert"; |
mapellil | 25:6b79352bc1fa | 450 | break; |
mapellil | 25:6b79352bc1fa | 451 | case FLASH_CLIENT_PRIV_KEY: |
mapellil | 25:6b79352bc1fa | 452 | cert_type="f_key"; |
mapellil | 25:6b79352bc1fa | 453 | break; |
mapellil | 25:6b79352bc1fa | 454 | default: |
mapellil | 26:70852d26853e | 455 | printf ("Error Unknown certificate type\n\r"); |
mapellil | 25:6b79352bc1fa | 456 | return false; |
mapellil | 25:6b79352bc1fa | 457 | } |
mapellil | 25:6b79352bc1fa | 458 | |
mapellil | 25:6b79352bc1fa | 459 | //May take a second try if device is busy or error is returned |
mapellil | 25:6b79352bc1fa | 460 | for (unsigned i = 0; i < 2; i++) { |
mapellil | 25:6b79352bc1fa | 461 | if (_parser.send("AT+S.TLSCERT=%s,%d\r%s", cert_type, size, cert) |
mapellil | 25:6b79352bc1fa | 462 | && _parser.recv("OK")) { |
mapellil | 25:6b79352bc1fa | 463 | return true; |
mapellil | 25:6b79352bc1fa | 464 | } |
mapellil | 25:6b79352bc1fa | 465 | else |
mapellil | 25:6b79352bc1fa | 466 | { |
mapellil | 25:6b79352bc1fa | 467 | printf ("SET CERT ERROR cert_type %s, cert: %s\n\r", cert_type, cert); |
mapellil | 25:6b79352bc1fa | 468 | debug_if(dbg_on, "SPWF> ERROR!!!!\r\n"); |
mapellil | 25:6b79352bc1fa | 469 | return false; |
mapellil | 25:6b79352bc1fa | 470 | } |
mapellil | 25:6b79352bc1fa | 471 | } |
mapellil | 25:6b79352bc1fa | 472 | return false; |
mapellil | 25:6b79352bc1fa | 473 | |
mapellil | 25:6b79352bc1fa | 474 | } |
mapellil | 25:6b79352bc1fa | 475 | |
mapellil | 25:6b79352bc1fa | 476 | int32_t SPWFSA01::setTLSSRVdomain(char * domain, CertType_t type) |
mapellil | 25:6b79352bc1fa | 477 | { |
mapellil | 25:6b79352bc1fa | 478 | if (type == FLASH_DOMAIN) |
mapellil | 25:6b79352bc1fa | 479 | { |
mapellil | 25:6b79352bc1fa | 480 | if (_parser.send("AT+S.TLSDOMAIN=f_domain,%s", domain)) { |
mapellil | 25:6b79352bc1fa | 481 | return true; |
mapellil | 25:6b79352bc1fa | 482 | } else { |
mapellil | 25:6b79352bc1fa | 483 | printf ("ERROR domain not set \n\r"); |
mapellil | 25:6b79352bc1fa | 484 | return false; |
mapellil | 25:6b79352bc1fa | 485 | } |
mapellil | 25:6b79352bc1fa | 486 | |
mapellil | 25:6b79352bc1fa | 487 | } else { |
mapellil | 25:6b79352bc1fa | 488 | return false; |
mapellil | 25:6b79352bc1fa | 489 | } |
mapellil | 25:6b79352bc1fa | 490 | } |
mapellil | 25:6b79352bc1fa | 491 | |
mapellil | 25:6b79352bc1fa | 492 | int32_t SPWFSA01::cleanTLScertificate(CertType_t type) |
mapellil | 25:6b79352bc1fa | 493 | { |
mapellil | 26:70852d26853e | 494 | _parser.flush(); |
mapellil | 26:70852d26853e | 495 | const char * cert_type="ca"; |
mapellil | 26:70852d26853e | 496 | switch (type) { |
mapellil | 26:70852d26853e | 497 | case RAM_CA_ROOT_CERT: |
mapellil | 26:70852d26853e | 498 | cert_type="ca"; |
mapellil | 26:70852d26853e | 499 | break; |
mapellil | 26:70852d26853e | 500 | case RAM_CLIENT_CERT: |
mapellil | 26:70852d26853e | 501 | cert_type="cert"; |
mapellil | 26:70852d26853e | 502 | break; |
mapellil | 26:70852d26853e | 503 | case RAM_CLIENT_PRIV_KEY: |
mapellil | 26:70852d26853e | 504 | cert_type="key"; |
mapellil | 26:70852d26853e | 505 | break; |
mapellil | 26:70852d26853e | 506 | case FLASH_CA_ROOT_CERT: |
mapellil | 26:70852d26853e | 507 | cert_type="f_ca"; |
mapellil | 26:70852d26853e | 508 | break; |
mapellil | 26:70852d26853e | 509 | case FLASH_CLIENT_CERT: |
mapellil | 26:70852d26853e | 510 | cert_type="f_cert"; |
mapellil | 26:70852d26853e | 511 | break; |
mapellil | 26:70852d26853e | 512 | case FLASH_CLIENT_PRIV_KEY: |
mapellil | 26:70852d26853e | 513 | cert_type="f_key"; |
mapellil | 26:70852d26853e | 514 | break; |
mapellil | 26:70852d26853e | 515 | case ALL: |
mapellil | 26:70852d26853e | 516 | cert_type="all"; |
mapellil | 26:70852d26853e | 517 | break; |
mapellil | 26:70852d26853e | 518 | default: |
mapellil | 26:70852d26853e | 519 | printf ("Error Unknown certificate type\n\r"); |
mapellil | 25:6b79352bc1fa | 520 | return false; |
mapellil | 26:70852d26853e | 521 | } |
mapellil | 26:70852d26853e | 522 | if (_parser.send("AT+S.TLSCERT2=clean,%s", cert_type) && _parser.recv("OK")) { |
mapellil | 26:70852d26853e | 523 | return true; |
mapellil | 26:70852d26853e | 524 | } else { |
mapellil | 26:70852d26853e | 525 | printf ("ERROR clean certificate \n\r"); |
mapellil | 26:70852d26853e | 526 | return false; |
mapellil | 26:70852d26853e | 527 | } |
mapellil | 25:6b79352bc1fa | 528 | } |