Jim Flynn / Mbed OS wifi_Example

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi310Interface.cpp Source File

WizFi310Interface.cpp

00001 /* WizFi310 implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 /**
00018   ******************************************************************************
00019   * @file    WizFi310Interface.h
00020   * @author  Gateway Team
00021   * @brief   Implementation file of the NetworkStack for the WizFi310 WiFi Device
00022   ******************************************************************************
00023   * @attention
00024   *
00025   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00026   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00027   * TIME. AS A RESULT, WIZnet SHALL NOT BE HELD LIABLE FOR ANY
00028   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00029   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00030   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00031   *
00032   * <h2><center>&copy; COPYRIGHT 2017 WIZnet Co.,Ltd.</center></h2>
00033   ******************************************************************************
00034   */
00035 
00036 #include <string.h>
00037 #include "WizFi310Interface.h"
00038 #include "mbed_debug.h"
00039 
00040 using namespace mbed;
00041 // Various timeouts for different WizFi310 operations
00042 #ifndef WIZFI310_CONNECT_TIMEOUT
00043 #define WIZFI310_CONNECT_TIMEOUT 15000
00044 #endif
00045 #ifndef WIZFI310_SEND_TIMEOUT
00046 #define WIZFI310_SEND_TIMEOUT    500
00047 #endif
00048 #ifndef WIZFI310_RECV_TIMEOUT
00049 #define WIZFI310_RECV_TIMEOUT    0
00050 #endif
00051 #ifndef WIZFI310_MISC_TIMEOUT
00052 #define WIZFI310_MISC_TIMEOUT    500
00053 #endif
00054 #ifndef WIZFI310_OPEN_TIMEOUT
00055 #define WIZFI310_OPEN_TIMEOUT   10000
00056 #endif
00057 #ifndef WIZFI310_CLOSE_TIMEOUT
00058 #define WIZFI310_CLOSE_TIMEOUT   500
00059 #endif
00060 
00061 #ifndef WIZFI310_MAX_CONNECT_COUNT
00062 #define WIZFI310_MAX_CONNECT    2
00063 #endif
00064 
00065 #ifndef WIZFI310_DELAY_MS
00066 #define WIZFI310_DELAY_MS       300
00067 #endif
00068 
00069 // WizFi310Interface implementation
00070 WizFi310Interface::WizFi310Interface(PinName tx, PinName rx, bool debug)
00071     : _wizfi310(tx, rx, debug)
00072 {
00073     memset(_ids, 0, sizeof(_ids));
00074     memset(_cbs, 0, sizeof(_cbs));
00075 
00076     _wizfi310.attach(this, &WizFi310Interface::event);
00077 }
00078 
00079 int WizFi310Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
00080                                uint8_t channel)
00081 {
00082     if (channel != 0) {
00083         return NSAPI_ERROR_UNSUPPORTED;
00084     }
00085 
00086     set_credentials(ssid, pass, security);
00087 
00088     return connect();
00089 }
00090 
00091 int WizFi310Interface::connect()
00092 {
00093     char sec[10];
00094     int i;
00095 
00096     _wizfi310.setTimeout(WIZFI310_CONNECT_TIMEOUT);
00097 
00098     _wizfi310.startup(0);
00099 
00100     if( !_wizfi310.dhcp(true) )
00101     {
00102         return NSAPI_ERROR_DHCP_FAILURE;
00103     }
00104 
00105     if( ap_sec == NSAPI_SECURITY_NONE && (strlen(ap_pass) > 0) )
00106     {
00107         ap_sec = NSAPI_SECURITY_UNKNOWN;
00108     }
00109 
00110     switch( ap_sec )
00111     {
00112         case NSAPI_SECURITY_NONE:
00113             strncpy(sec,"OPEN",strlen("OPEN")+1);
00114             break;
00115         case NSAPI_SECURITY_WEP:
00116             strncpy(sec,"WEP",strlen("WEP")+1);
00117             break;
00118         case NSAPI_SECURITY_WPA:
00119             strncpy(sec,"WPA",strlen("WPA")+1);
00120             break;
00121         case NSAPI_SECURITY_WPA2:
00122             strncpy(sec,"WPA2",strlen("WPA2")+1);
00123             break;
00124         case NSAPI_SECURITY_WPA_WPA2:
00125             strncpy(sec,"WPAWPA2",strlen("WPAWPA2")+1);
00126             break;
00127         default:
00128             strncpy(sec,"",strlen("")+1);
00129             break;
00130     }
00131 
00132     for( i=0; i<WIZFI310_MAX_CONNECT; i++ )
00133     {
00134         if( _wizfi310.connect(ap_ssid, ap_pass, sec) ) {
00135             break;
00136         }
00137 
00138         _wizfi310.reset();
00139     }
00140 
00141     if( i > WIZFI310_MAX_CONNECT ){
00142         return NSAPI_ERROR_NO_CONNECTION;
00143     }
00144 
00145     if( !_wizfi310.getIPAddress() )
00146     {
00147         return NSAPI_ERROR_DHCP_FAILURE;
00148     }
00149 
00150     return NSAPI_ERROR_OK;
00151 }
00152 
00153 
00154 int WizFi310Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
00155 {
00156     memset(ap_ssid, 0, sizeof(ap_ssid));
00157     strncpy(ap_ssid, ssid, sizeof(ap_ssid));
00158 
00159     memset(ap_pass, 0, sizeof(ap_pass));
00160     strncpy(ap_pass, pass, sizeof(ap_pass));
00161 
00162     ap_sec = security;
00163     return 0;
00164 }
00165 
00166 
00167 int WizFi310Interface::set_channel(uint8_t channel)
00168 {
00169     return 0;
00170 }
00171 
00172 int WizFi310Interface::disconnect()
00173 {
00174     _wizfi310.setTimeout(WIZFI310_MISC_TIMEOUT);
00175 
00176     if (!_wizfi310.disconnect())
00177     {
00178         return NSAPI_ERROR_DEVICE_ERROR;
00179     }
00180     return NSAPI_ERROR_OK;
00181 }
00182 
00183 const char *WizFi310Interface::get_ip_address()
00184 {
00185     return _wizfi310.getIPAddress();
00186 }
00187 
00188 const char *WizFi310Interface::get_mac_address()
00189 {
00190     return _wizfi310.getMACAddress();
00191 }
00192 
00193 const char *WizFi310Interface::get_gateway()
00194 {
00195     return _wizfi310.getGateway();
00196 }
00197 
00198 const char *WizFi310Interface::get_netmask()
00199 {
00200     return _wizfi310.getNetmask();
00201 }
00202 
00203 int8_t WizFi310Interface::get_rssi()
00204 {
00205     return _wizfi310.getRSSI();
00206 }
00207 
00208 int WizFi310Interface::scan(WiFiAccessPoint *res, unsigned count)
00209 {
00210     return _wizfi310.scan(res, count);
00211 }
00212 
00213 nsapi_error_t WizFi310Interface::gethostbyname(const char *host,
00214             SocketAddress *address, nsapi_version_t version)
00215 {
00216     char host_ip[16];
00217 
00218     if( !_wizfi310.dns_lookup(host,host_ip) ){
00219         return NSAPI_ERROR_DNS_FAILURE;
00220     }
00221     if ( !address->set_ip_address(host_ip) ){
00222         return NSAPI_ERROR_DNS_FAILURE;
00223     }
00224 
00225     return NSAPI_ERROR_OK;
00226 }
00227 
00228 struct wizfi310_socket {
00229     int id;
00230     nsapi_protocol_t proto;
00231     bool connected;
00232     SocketAddress addr;
00233 };
00234 
00235 
00236 int WizFi310Interface::socket_open(void **handle, nsapi_protocol_t proto)
00237 {
00238     // Look for an unused socket
00239     int id = -1;
00240 
00241     for (int i=0; i<WIZFI310_SOCKET_COUNT; i++) {
00242         if (!_ids[i]){
00243             id = i;
00244             //_ids[i] = true;
00245             break;
00246         }
00247     }
00248 
00249     if (id == -1){
00250         return NSAPI_ERROR_NO_SOCKET;
00251     }
00252 
00253     struct wizfi310_socket *socket = new struct wizfi310_socket;
00254     if (!socket){
00255         return NSAPI_ERROR_NO_SOCKET;
00256     }
00257 
00258     socket->id = id;
00259     socket->proto = proto;
00260     socket->connected = false;
00261     *handle = socket;
00262 
00263     return 0;
00264 }
00265 
00266 int WizFi310Interface::socket_close(void *handle)
00267 {
00268     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00269     int err = 0;
00270     _wizfi310.setTimeout(WIZFI310_CLOSE_TIMEOUT);
00271 
00272     if (socket->connected && !_wizfi310.close(socket->id)) {
00273         err = NSAPI_ERROR_DEVICE_ERROR;
00274     }
00275 
00276     socket->connected = false;
00277     _ids[socket->id] = false;
00278     delete socket;
00279     return err;
00280 }
00281 
00282 int WizFi310Interface::socket_bind(void *handle, const SocketAddress &address)
00283 {
00284     return NSAPI_ERROR_UNSUPPORTED;
00285 }
00286     
00287 int WizFi310Interface::socket_listen(void *handle, int backlog)
00288 {
00289     return NSAPI_ERROR_UNSUPPORTED;
00290 }
00291 
00292 int WizFi310Interface::socket_connect(void *handle, const SocketAddress &addr)
00293 {
00294     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00295     _wizfi310.setTimeout(WIZFI310_OPEN_TIMEOUT);
00296 
00297     const char *proto = (socket->proto == NSAPI_UDP) ? "UCN" : "TCN";
00298     if (!_wizfi310.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) {
00299         return NSAPI_ERROR_DEVICE_ERROR;
00300     }
00301 
00302     socket->connected = true;
00303     _ids[socket->id] = true;
00304     return 0;
00305 }
00306     
00307 int WizFi310Interface::socket_accept(void *server, void **socket, SocketAddress *addr)
00308 {
00309     return NSAPI_ERROR_UNSUPPORTED;
00310 }
00311     
00312 int WizFi310Interface::socket_send(void *handle, const void *data, unsigned size)
00313 {
00314     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00315     _wizfi310.setTimeout(WIZFI310_SEND_TIMEOUT);
00316 
00317     if (!_wizfi310.send(socket->id, data, size)) {
00318         return NSAPI_ERROR_DEVICE_ERROR;
00319     }
00320 
00321     return size;
00322 }
00323     
00324 int WizFi310Interface::socket_recv(void *handle, void *data, unsigned size)
00325 {
00326     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00327     _wizfi310.setTimeout(WIZFI310_RECV_TIMEOUT);
00328 
00329     int32_t recv = _wizfi310.recv(socket->id, data, size);
00330     if (recv < 0) {
00331         return NSAPI_ERROR_WOULD_BLOCK;
00332     }
00333 
00334     return recv;
00335 }
00336     
00337 int WizFi310Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
00338 {
00339     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00340 
00341     if (socket->connected && socket->addr != addr) {
00342         _wizfi310.setTimeout(WIZFI310_MISC_TIMEOUT);
00343         if (!_wizfi310.close(socket->id)) {
00344             return NSAPI_ERROR_DEVICE_ERROR;
00345         }
00346         socket->connected = false;
00347     }
00348 
00349     if (!socket->connected) {
00350         int err = socket_connect(socket, addr);
00351         if (err < 0 ) {
00352             return err;
00353         }
00354         socket->addr = addr;
00355     }
00356 
00357     return socket_send(socket, data, size);
00358 }
00359     
00360 int WizFi310Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
00361 {
00362     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00363     int ret = socket_recv(socket, data, size);
00364     if (ret >= 0 && addr) {
00365         *addr = socket->addr;
00366     }
00367 
00368     return ret;
00369 }
00370     
00371 void WizFi310Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
00372 {
00373     struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
00374     _cbs[socket->id].callback = callback;
00375     _cbs[socket->id].data = data;
00376 }
00377 
00378 void WizFi310Interface::event()
00379 {
00380     for(int i=0; i<WIZFI310_SOCKET_COUNT; i++){
00381         if (_cbs[i].callback) {
00382             _cbs[i].callback(_cbs[i].data);
00383         }
00384     }
00385 }