cc

Dependencies:   ATParser

Committer:
mridup
Date:
Thu Jun 30 10:08:10 2016 +0000
Revision:
6:258b093eec08
Parent:
5:f17d6351e942
Child:
7:551de95939e2
spwf TCP UDP socket recv.

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