This library can be used in mbed driver or mbed OS2. So If you want to use WizFi310 on mbed OS5, You have to use another WizFi310 library(wizfi310-driver). That is git repository for wizfi310-driver. - https://github.com/ARMmbed/wizfi310-driver
Dependents: KT_IoTMakers_WizFi310_Example WizFi310_STATION_HelloWorld WizFi310_DNS_TCP_HelloWorld WizFi310_Ubidots ... more
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 #include "WizFi310Interface.h" 00019 00020 // Various timeouts for different WizFi310 operations 00021 #define WizFi310_CONNECT_TIMEOUT 15000 00022 #define WizFi310_SEND_TIMEOUT 500 00023 #define WizFi310_RECV_TIMEOUT 0 00024 #define WizFi310_MISC_TIMEOUT 500 00025 00026 #define WizFi310_DELAY_MS 300 00027 00028 00029 // WizFi310Interface implementation 00030 WizFi310Interface::WizFi310Interface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) 00031 : _wizfi310(tx, rx, cts, rts, reset, alarm, baud) 00032 { 00033 memset(_ids, 0, sizeof(_ids)); 00034 } 00035 00036 int WizFi310Interface::connect( 00037 const char *ssid, 00038 const char *pass, 00039 nsapi_security_t security) 00040 { 00041 if (!_wizfi310.startup()) 00042 { 00043 return NSAPI_ERROR_DEVICE_ERROR; 00044 } 00045 00046 _wizfi310.setSsid(ssid); 00047 _wizfi310.setSec(security, pass); 00048 _wizfi310.setAddress(""); 00049 00050 if( _wizfi310.join(WizFi310::WM_STATION) == -1) 00051 { 00052 return NSAPI_ERROR_NO_CONNECTION; 00053 } 00054 00055 return 0; 00056 } 00057 00058 int WizFi310Interface::connectAP( 00059 const char *ssid, 00060 const char *pass, 00061 nsapi_security_t security) 00062 { 00063 if (!_wizfi310.startup()) 00064 { 00065 return NSAPI_ERROR_DEVICE_ERROR; 00066 } 00067 00068 _wizfi310.setSsid(ssid); 00069 _wizfi310.setSec(security, pass); 00070 //_wizfi310.setAddress("192.168.1.1"); 00071 _wizfi310.setAddress("192.168.100.1","255.255.255.0","192.168.100.1"); 00072 00073 if( _wizfi310.join(WizFi310::WM_AP) == -1) 00074 { 00075 return NSAPI_ERROR_NO_CONNECTION; 00076 } 00077 00078 return 0; 00079 } 00080 00081 00082 int WizFi310Interface::disconnect() 00083 { 00084 if ( _wizfi310.cmdWLEAVE() == -1 ) return NSAPI_ERROR_DEVICE_ERROR; 00085 00086 return 0; 00087 } 00088 00089 const char *WizFi310Interface::get_ip_address() 00090 { 00091 return _wizfi310.getIPAddress(); 00092 } 00093 00094 const char *WizFi310Interface::get_mac_address() 00095 { 00096 return _wizfi310.getMACAddress(); 00097 } 00098 00099 struct wizfi310_socket { 00100 int id; 00101 nsapi_protocol_t proto; 00102 bool connected; 00103 uint16_t port; 00104 }; 00105 00106 int WizFi310Interface::socket_open(void **handle, nsapi_protocol_t proto) 00107 { 00108 // Look for an unused socket 00109 00110 /* 00111 int id = -1; 00112 00113 for (int i = 0; i < WIZFI310_SOCKET_COUNT; i++) { 00114 if (_ids[i] == false) { 00115 id = i; 00116 _ids[i] = true; 00117 break; 00118 } 00119 } 00120 00121 if (id == -1) { 00122 return NSAPI_ERROR_NO_SOCKET; 00123 } 00124 */ 00125 00126 struct wizfi310_socket *socket = new struct wizfi310_socket; 00127 if (!socket) { 00128 delete socket; 00129 return NSAPI_ERROR_NO_SOCKET; 00130 } 00131 00132 socket->id = -1; 00133 socket->proto = proto; 00134 socket->connected = false; 00135 *handle = socket; 00136 return 0; 00137 } 00138 00139 int WizFi310Interface::socket_close(void *handle) 00140 { 00141 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; 00142 int err = 0; 00143 00144 if(socket->id == -1){ 00145 err = NSAPI_ERROR_NO_SOCKET; 00146 } 00147 else if (_wizfi310.close(socket->id) == -1) { 00148 err = NSAPI_ERROR_DEVICE_ERROR; 00149 } 00150 00151 _ids[socket->id] = false; 00152 wait_ms(WizFi310_DELAY_MS); 00153 delete socket; 00154 return err; 00155 } 00156 00157 int WizFi310Interface::socket_bind(void *handle, const SocketAddress &address) 00158 { 00159 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; 00160 socket->port = address.get_port(); 00161 00162 return 0; 00163 } 00164 00165 int WizFi310Interface::socket_listen(void *handle, int backlog) 00166 { 00167 int cid=-1; 00168 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; 00169 00170 if((cid = _wizfi310.listen(WizFi310::PROTO_TCP, socket->port)) == -1 ) 00171 { 00172 return NSAPI_ERROR_DEVICE_ERROR; 00173 } 00174 if(cid >= WIZFI310_SOCKET_COUNT) 00175 { 00176 return NSAPI_ERROR_NO_SOCKET; 00177 } 00178 _ids[cid] = true; 00179 socket->id = cid; 00180 socket->connected = false; 00181 return 0; 00182 } 00183 00184 int WizFi310Interface::socket_connect(void *handle, const SocketAddress &addr) 00185 { 00186 int cid=-1; 00187 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; 00188 00189 WizFi310::Protocol proto = (socket->proto == NSAPI_UDP) ? WizFi310::PROTO_UDP : WizFi310::PROTO_TCP; 00190 00191 if((cid = _wizfi310.open(proto, addr.get_ip_address(), addr.get_port())) == -1 ) 00192 { 00193 return NSAPI_ERROR_DEVICE_ERROR; 00194 } 00195 00196 if(cid >= WIZFI310_SOCKET_COUNT) 00197 { 00198 return NSAPI_ERROR_NO_SOCKET; 00199 } 00200 00201 _ids[cid] = true; 00202 socket->id = cid; 00203 socket->connected = true; 00204 wait_ms(WizFi310_DELAY_MS); 00205 return 0; 00206 } 00207 00208 int WizFi310Interface::socket_accept(void **handle, void *server) 00209 { 00210 struct wizfi310_socket *new_socket = new struct wizfi310_socket; 00211 00212 if( !new_socket ) 00213 { 00214 return NSAPI_ERROR_NO_SOCKET; 00215 } 00216 00217 memset(new_socket, 0, sizeof(new_socket)); 00218 00219 for(int cid=0; cid<WIZFI310_SOCKET_COUNT; cid++) 00220 { 00221 if( _wizfi310.accept(cid) != -1 ) 00222 { 00223 _ids[cid] = true; 00224 new_socket->id = cid; 00225 new_socket->connected = true; 00226 00227 *handle = new_socket; 00228 00229 00230 return 0; 00231 } 00232 } 00233 delete new_socket; 00234 return NSAPI_ERROR_WOULD_BLOCK; 00235 } 00236 00237 int WizFi310Interface::socket_send(void *handle, const void *data, unsigned size) 00238 { 00239 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; 00240 00241 if ( _wizfi310.send(socket->id, (const char*)data, size) == -1 ) { 00242 return NSAPI_ERROR_DEVICE_ERROR; 00243 } 00244 00245 return size; 00246 } 00247 00248 int WizFi310Interface::socket_recv(void *handle, void *data, unsigned size) 00249 { 00250 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; 00251 00252 int32_t recv = _wizfi310.recv(socket->id, (char*)data, size); 00253 if (recv == 0) { 00254 return NSAPI_ERROR_WOULD_BLOCK; 00255 } 00256 else if(recv == -1){ 00257 return NSAPI_ERROR_NO_SOCKET; 00258 } 00259 00260 return recv; 00261 } 00262 00263 int WizFi310Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) 00264 { 00265 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; 00266 if (!socket->connected) { 00267 int err = socket_connect(socket, addr); 00268 if (err < 0) { 00269 return err; 00270 } 00271 } 00272 00273 return socket_send(socket, data, size); 00274 } 00275 00276 int WizFi310Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) 00277 { 00278 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; 00279 return socket_recv(socket, data, size); 00280 } 00281 00282 void WizFi310Interface::socket_attach(void *handle, void (*callback)(void *), void *data) 00283 { 00284 }
Generated on Wed Jul 13 2022 18:48:26 by 1.7.2