cliff

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