This is Wi-Fi interface of WizFi310 for mbed os 5
Dependents: mbed-os-example-wifi
Revision 1:8e468bb83e1e, committed 2017-12-19
- Comitter:
- kaizen
- Date:
- Tue Dec 19 16:59:33 2017 +0900
- Parent:
- 0:d0e3f515f99d
- Commit message:
- This is WizFi310 Library for mbed os5
Changed in this revision
diff -r d0e3f515f99d -r 8e468bb83e1e WizFi310/WizFi310.cpp --- a/WizFi310/WizFi310.cpp Mon Dec 18 23:25:46 2017 +0000 +++ b/WizFi310/WizFi310.cpp Tue Dec 19 16:59:33 2017 +0900 @@ -191,7 +191,8 @@ unsigned int cnt = 0; nsapi_wifi_ap_t ap; - if (!(_parser.send("AT+WSCAN") + // Scan Time out : 50ms + if (!(_parser.send("AT+WSCAN=,,,50") && _parser.recv("Index/SSID/BSSID/RSSI(-dBm)/MaxDataRate(Mbps)/Security/RadioBand(GHz)/Channel"))) { return NSAPI_ERROR_DEVICE_ERROR; @@ -221,12 +222,9 @@ return false; } - if( !(_parser.send("AT+SCON=O,%s,%s,%d,,0",type,addr,port) && _parser.recv("[OK]")) ) { - return false; - } - - if( !(_parser.recv("[CONNECT %d]",&created_sock_id)) ) { - return false; + if( !(_parser.send("AT+SCON=O,%s,%s,%d,,0",type,addr,port) && _parser.recv("[OK]") + && _parser.recv("[CONNECT %d]",&created_sock_id))) { + return false; } if( created_sock_id != id ) { @@ -239,17 +237,21 @@ bool WizFi310::dns_lookup(const char* name, char* ip) { - return _parser.send("AT+FDNS=%s,10000", name) && _parser.recv("%s%*[\r]%*[\n]",ip) - && _parser.recv("[OK]"); + return (_parser.send("AT+FDNS=%s,5000", name) && _parser.recv("%[^\n]\r\n",ip) && _parser.recv("[OK]")); } bool WizFi310::send(int id, const void *data, uint32_t amount) { char str_result[20]; - sprintf(str_result,"[%d,,,%d]",id,amount); + + if(id > 8) { + return false; + } + + sprintf(str_result,"[%d,,,%d]",id,(int)amount); for (unsigned i=0; i<2; i++) { - if( _parser.send("AT+SSEND=%d,,,%d",id, amount) + if( _parser.send("AT+SSEND=%d,,,%d",id, (int)amount) && _parser.recv(str_result) && _parser.write((char*)data, (int)amount) >= 0 && _parser.recv("[OK]") ){ @@ -292,8 +294,8 @@ int32_t WizFi310::recv(int id, void *data, uint32_t amount) { - while (true) { - // check if any packets are ready for us + while (true) { + // check if any packets are ready for us for (struct packet **p = &_packets; *p; p = &(*p)->next) { if ((*p)->id == id) { struct packet *q = *p; @@ -329,9 +331,13 @@ bool WizFi310::close(int id) { char sock_event_msg[15]; - sprintf(sock_event_msg,"[DISCONNECT %d]",id); - if (_parser.send("AT+SMGMT=%d", id) && _parser.recv("[OK]") && _parser.recv(sock_event_msg) ) + if(id > 7) { + return false; + } + + sprintf(sock_event_msg,"[DISCONNECT %d]",id); + if (_parser.send("AT+SMGMT=%d", id) && _parser.recv(sock_event_msg) && _parser.recv("[OK]") ) { return true; }
diff -r d0e3f515f99d -r 8e468bb83e1e WizFi310Interface.cpp --- a/WizFi310Interface.cpp Mon Dec 18 23:25:46 2017 +0000 +++ b/WizFi310Interface.cpp Tue Dec 19 16:59:33 2017 +0900 @@ -30,6 +30,10 @@ #ifndef WIZFI310_MISC_TIMEOUT #define WIZFI310_MISC_TIMEOUT 500 #endif +#ifndef WIZFI310_OPEN_CLOSE_TIMEOUT +#define WIZFI310_OPEN_CLOSE_TIMEOUT 10000 +#endif + #ifndef WIZFI310_DELAY_MS #define WIZFI310_DELAY_MS 300 #endif @@ -163,6 +167,21 @@ return _wizfi310.scan(res, count); } +nsapi_error_t WizFi310Interface::gethostbyname(const char *host, + SocketAddress *address, nsapi_version_t version) +{ + char host_ip[16]; + + if( !_wizfi310.dns_lookup(host,host_ip) ){ + return NSAPI_ERROR_DNS_FAILURE; + } + if ( !address->set_ip_address(host_ip) ){ + return NSAPI_ERROR_DNS_FAILURE; + } + + return NSAPI_ERROR_OK; +} + struct wizfi310_socket { int id; nsapi_protocol_t proto; @@ -173,13 +192,13 @@ int WizFi310Interface::socket_open(void **handle, nsapi_protocol_t proto) { - // Look for an unused socket + // Look for an unused socket int id = -1; for (int i=0; i<WIZFI310_SOCKET_COUNT; i++) { if (!_ids[i]){ id = i; - _ids[i] = true; + //_ids[i] = true; break; } } @@ -197,6 +216,7 @@ socket->proto = proto; socket->connected = false; *handle = socket; + return 0; } @@ -204,7 +224,7 @@ { struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; int err = 0; - _wizfi310.setTimeout(WIZFI310_MISC_TIMEOUT); + _wizfi310.setTimeout(WIZFI310_OPEN_CLOSE_TIMEOUT); if (socket->connected && !_wizfi310.close(socket->id)) { err = NSAPI_ERROR_DEVICE_ERROR; @@ -229,7 +249,7 @@ int WizFi310Interface::socket_connect(void *handle, const SocketAddress &addr) { struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; - _wizfi310.setTimeout(WIZFI310_MISC_TIMEOUT); + _wizfi310.setTimeout(WIZFI310_OPEN_CLOSE_TIMEOUT); const char *proto = (socket->proto == NSAPI_UDP) ? "UCN" : "TCN"; if (!_wizfi310.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) { @@ -237,6 +257,7 @@ } socket->connected = true; + _ids[socket->id] = true; return 0; }
diff -r d0e3f515f99d -r 8e468bb83e1e WizFi310Interface.h --- a/WizFi310Interface.h Mon Dec 18 23:25:46 2017 +0000 +++ b/WizFi310Interface.h Tue Dec 19 16:59:33 2017 +0900 @@ -36,7 +36,7 @@ /* WizFi310Interface constructor */ - WizFi310Interface(PinName tx, PinName rx, bool debug = true); + WizFi310Interface(PinName tx, PinName rx, bool debug = false); //WizFi310Interface(PinName tx, PinName rx, // PinName rts = WIZFI310_RTS_PIN, PinName cts = WIZFI310_CTS_PIN, // PinName reset = WIZFI310_RESET_PIN, PinName alarm = WIZFI310_ALARM_PIN, @@ -144,7 +144,9 @@ * version is chosen by the stack (defaults to NSAPI_UNSPEC) * @return 0 on success, negative error code on failure */ - using NetworkInterface::gethostbyname; + //using NetworkInterface::gethostbyname; + virtual nsapi_error_t gethostbyname(const char *host, + SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC); /** Add a domain name server to list of servers to query *