test

Fork of WizFi250Interface by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi250Interface.cpp Source File

WizFi250Interface.cpp

00001 /* WizFi250 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 #include "WizFi250Interface.h"
00019 
00020 // Various timeouts for different WizFi250 operations
00021 #define WizFi250_CONNECT_TIMEOUT 15000
00022 #define WizFi250_SEND_TIMEOUT    500
00023 #define WizFi250_RECV_TIMEOUT    0
00024 #define WizFi250_MISC_TIMEOUT    500
00025 
00026 #define WizFi250_DELAY_MS       300
00027 
00028 
00029 // WizFi250Interface implementation
00030 WizFi250Interface::WizFi250Interface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm,  int baud)
00031     : _wizfi250(tx, rx, cts, rts, reset, alarm, baud)
00032 {
00033     memset(_ids, 0, sizeof(_ids));
00034 }
00035 
00036 int WizFi250Interface::connect(
00037     const char *ssid,
00038     const char *pass,
00039     nsapi_security_t security)
00040 {
00041     if (!_wizfi250.startup()) 
00042     {
00043         return NSAPI_ERROR_DEVICE_ERROR;
00044     }
00045 
00046     _wizfi250.setSsid(ssid);
00047     _wizfi250.setSec(WizFi250::SEC_AUTO, pass);
00048     _wizfi250.setAddress("");
00049     
00050     if( _wizfi250.join(WizFi250::WM_STATION) == -1)
00051     {
00052         return NSAPI_ERROR_NO_CONNECTION;
00053     }
00054 
00055     return 0;
00056 }
00057 
00058 int WizFi250Interface::disconnect()
00059 {
00060     if ( _wizfi250.cmdWLEAVE() == -1 )  return NSAPI_ERROR_DEVICE_ERROR;
00061 
00062     return 0;
00063 }
00064 
00065 const char *WizFi250Interface::get_ip_address()
00066 {
00067     return _wizfi250.getIPAddress();
00068 }
00069 
00070 const char *WizFi250Interface::get_mac_address()
00071 {
00072     return _wizfi250.getMACAddress();
00073 }
00074 
00075 struct wizfi250_socket {
00076     int id;
00077     nsapi_protocol_t proto;
00078     bool connected;
00079 };
00080 
00081 int WizFi250Interface::socket_open(void **handle, nsapi_protocol_t proto)
00082 {
00083     // Look for an unused socket
00084 
00085     /*
00086     int id = -1;
00087  
00088     for (int i = 0; i < WIZFI250_SOCKET_COUNT; i++) {
00089         if (_ids[i] == false) {
00090             id = i;
00091             _ids[i] = true;
00092             break;
00093         }
00094     }
00095  
00096     if (id == -1) {
00097         return NSAPI_ERROR_NO_SOCKET;
00098     }
00099     */
00100     
00101     struct wizfi250_socket *socket = new struct wizfi250_socket;
00102     if (!socket) {
00103         return NSAPI_ERROR_NO_SOCKET;
00104     }
00105     
00106     socket->id = -1;
00107     socket->proto = proto;
00108     socket->connected = false;
00109     *handle = socket;
00110     return 0;
00111 }
00112 
00113 int WizFi250Interface::socket_close(void *handle)
00114 {
00115     struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
00116     int err = 0;
00117  
00118     if(socket->id == -1){
00119         err = NSAPI_ERROR_NO_SOCKET;
00120     }
00121     else if (_wizfi250.close(socket->id) == -1) {
00122         err = NSAPI_ERROR_DEVICE_ERROR;
00123     }
00124 
00125     _ids[socket->id] = false;
00126     wait_ms(WizFi250_DELAY_MS);
00127     delete socket;
00128     return err;
00129 }
00130 
00131 int WizFi250Interface::socket_bind(void *handle, const SocketAddress &address)
00132 {
00133     return NSAPI_ERROR_UNSUPPORTED;
00134 }
00135 
00136 int WizFi250Interface::socket_listen(void *handle, int backlog)
00137 {
00138     return NSAPI_ERROR_UNSUPPORTED;
00139 }
00140 
00141 int WizFi250Interface::socket_connect(void *handle, const SocketAddress &addr)
00142 {
00143     int cid=-1;
00144     struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
00145 
00146     WizFi250::Protocol proto = (socket->proto == NSAPI_UDP) ? WizFi250::PROTO_UDP : WizFi250::PROTO_TCP;
00147     if((cid = _wizfi250.open(proto, addr.get_ip_address(), addr.get_port())) == -1 )
00148     {
00149         return NSAPI_ERROR_DEVICE_ERROR;
00150     }
00151 
00152     if(cid >= WIZFI250_SOCKET_COUNT)
00153     {
00154         return NSAPI_ERROR_NO_SOCKET;
00155     }
00156     
00157     _ids[cid] = true;
00158     socket->id = cid;
00159     socket->connected = true;
00160     wait_ms(WizFi250_DELAY_MS);
00161     return 0;
00162 }
00163     
00164 int WizFi250Interface::socket_accept(void **handle, void *server)
00165 {
00166     return NSAPI_ERROR_UNSUPPORTED;
00167 }
00168 
00169 int WizFi250Interface::socket_send(void *handle, const void *data, unsigned size)
00170 {
00171     struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
00172  
00173     if ( _wizfi250.send(socket->id, (const char*)data, size) == -1 ) {
00174         return NSAPI_ERROR_DEVICE_ERROR;
00175     }
00176  
00177     return size;
00178 }
00179 
00180 int WizFi250Interface::socket_recv(void *handle, void *data, unsigned size)
00181 {
00182     struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
00183  
00184     int32_t recv = _wizfi250.recv(socket->id, (char*)data, size);
00185     if (recv < 0) {
00186         return NSAPI_ERROR_WOULD_BLOCK;
00187     }
00188  
00189     return recv;
00190 }
00191 
00192 int WizFi250Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
00193 {
00194     struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;
00195     if (!socket->connected) {
00196         int err = socket_connect(socket, addr);
00197         if (err < 0) {
00198             return err;
00199         }
00200     }
00201     
00202     return socket_send(socket, data, size);
00203 }
00204 
00205 int WizFi250Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
00206 {
00207     struct wizfi250_socket *socket = (struct wizfi250_socket *)handle;    
00208     return socket_recv(socket, data, size);
00209 }
00210 
00211 void WizFi250Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
00212 {
00213 }
00214