wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/wizfi310-driver/WizFi310Interface.cpp@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| JMF | 0:24d3eb812fd4 | 1 | /* WizFi310 implementation of NetworkInterfaceAPI |
| JMF | 0:24d3eb812fd4 | 2 | * Copyright (c) 2015 ARM Limited |
| JMF | 0:24d3eb812fd4 | 3 | * |
| JMF | 0:24d3eb812fd4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| JMF | 0:24d3eb812fd4 | 5 | * you may not use this file except in compliance with the License. |
| JMF | 0:24d3eb812fd4 | 6 | * You may obtain a copy of the License at |
| JMF | 0:24d3eb812fd4 | 7 | * |
| JMF | 0:24d3eb812fd4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| JMF | 0:24d3eb812fd4 | 9 | * |
| JMF | 0:24d3eb812fd4 | 10 | * Unless required by applicable law or agreed to in writing, software |
| JMF | 0:24d3eb812fd4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| JMF | 0:24d3eb812fd4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| JMF | 0:24d3eb812fd4 | 13 | * See the License for the specific language governing permissions and |
| JMF | 0:24d3eb812fd4 | 14 | * limitations under the License. |
| JMF | 0:24d3eb812fd4 | 15 | */ |
| JMF | 0:24d3eb812fd4 | 16 | |
| JMF | 0:24d3eb812fd4 | 17 | /** |
| JMF | 0:24d3eb812fd4 | 18 | ****************************************************************************** |
| JMF | 0:24d3eb812fd4 | 19 | * @file WizFi310Interface.h |
| JMF | 0:24d3eb812fd4 | 20 | * @author Gateway Team |
| JMF | 0:24d3eb812fd4 | 21 | * @brief Implementation file of the NetworkStack for the WizFi310 WiFi Device |
| JMF | 0:24d3eb812fd4 | 22 | ****************************************************************************** |
| JMF | 0:24d3eb812fd4 | 23 | * @attention |
| JMF | 0:24d3eb812fd4 | 24 | * |
| JMF | 0:24d3eb812fd4 | 25 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
| JMF | 0:24d3eb812fd4 | 26 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
| JMF | 0:24d3eb812fd4 | 27 | * TIME. AS A RESULT, WIZnet SHALL NOT BE HELD LIABLE FOR ANY |
| JMF | 0:24d3eb812fd4 | 28 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
| JMF | 0:24d3eb812fd4 | 29 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
| JMF | 0:24d3eb812fd4 | 30 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
| JMF | 0:24d3eb812fd4 | 31 | * |
| JMF | 0:24d3eb812fd4 | 32 | * <h2><center>© COPYRIGHT 2017 WIZnet Co.,Ltd.</center></h2> |
| JMF | 0:24d3eb812fd4 | 33 | ****************************************************************************** |
| JMF | 0:24d3eb812fd4 | 34 | */ |
| JMF | 0:24d3eb812fd4 | 35 | |
| JMF | 0:24d3eb812fd4 | 36 | #include <string.h> |
| JMF | 0:24d3eb812fd4 | 37 | #include "WizFi310Interface.h" |
| JMF | 0:24d3eb812fd4 | 38 | #include "mbed_debug.h" |
| JMF | 0:24d3eb812fd4 | 39 | |
| JMF | 0:24d3eb812fd4 | 40 | using namespace mbed; |
| JMF | 0:24d3eb812fd4 | 41 | // Various timeouts for different WizFi310 operations |
| JMF | 0:24d3eb812fd4 | 42 | #ifndef WIZFI310_CONNECT_TIMEOUT |
| JMF | 0:24d3eb812fd4 | 43 | #define WIZFI310_CONNECT_TIMEOUT 15000 |
| JMF | 0:24d3eb812fd4 | 44 | #endif |
| JMF | 0:24d3eb812fd4 | 45 | #ifndef WIZFI310_SEND_TIMEOUT |
| JMF | 0:24d3eb812fd4 | 46 | #define WIZFI310_SEND_TIMEOUT 500 |
| JMF | 0:24d3eb812fd4 | 47 | #endif |
| JMF | 0:24d3eb812fd4 | 48 | #ifndef WIZFI310_RECV_TIMEOUT |
| JMF | 0:24d3eb812fd4 | 49 | #define WIZFI310_RECV_TIMEOUT 0 |
| JMF | 0:24d3eb812fd4 | 50 | #endif |
| JMF | 0:24d3eb812fd4 | 51 | #ifndef WIZFI310_MISC_TIMEOUT |
| JMF | 0:24d3eb812fd4 | 52 | #define WIZFI310_MISC_TIMEOUT 500 |
| JMF | 0:24d3eb812fd4 | 53 | #endif |
| JMF | 0:24d3eb812fd4 | 54 | #ifndef WIZFI310_OPEN_TIMEOUT |
| JMF | 0:24d3eb812fd4 | 55 | #define WIZFI310_OPEN_TIMEOUT 10000 |
| JMF | 0:24d3eb812fd4 | 56 | #endif |
| JMF | 0:24d3eb812fd4 | 57 | #ifndef WIZFI310_CLOSE_TIMEOUT |
| JMF | 0:24d3eb812fd4 | 58 | #define WIZFI310_CLOSE_TIMEOUT 500 |
| JMF | 0:24d3eb812fd4 | 59 | #endif |
| JMF | 0:24d3eb812fd4 | 60 | |
| JMF | 0:24d3eb812fd4 | 61 | #ifndef WIZFI310_MAX_CONNECT_COUNT |
| JMF | 0:24d3eb812fd4 | 62 | #define WIZFI310_MAX_CONNECT 2 |
| JMF | 0:24d3eb812fd4 | 63 | #endif |
| JMF | 0:24d3eb812fd4 | 64 | |
| JMF | 0:24d3eb812fd4 | 65 | #ifndef WIZFI310_DELAY_MS |
| JMF | 0:24d3eb812fd4 | 66 | #define WIZFI310_DELAY_MS 300 |
| JMF | 0:24d3eb812fd4 | 67 | #endif |
| JMF | 0:24d3eb812fd4 | 68 | |
| JMF | 0:24d3eb812fd4 | 69 | // WizFi310Interface implementation |
| JMF | 0:24d3eb812fd4 | 70 | WizFi310Interface::WizFi310Interface(PinName tx, PinName rx, bool debug) |
| JMF | 0:24d3eb812fd4 | 71 | : _wizfi310(tx, rx, debug) |
| JMF | 0:24d3eb812fd4 | 72 | { |
| JMF | 0:24d3eb812fd4 | 73 | memset(_ids, 0, sizeof(_ids)); |
| JMF | 0:24d3eb812fd4 | 74 | memset(_cbs, 0, sizeof(_cbs)); |
| JMF | 0:24d3eb812fd4 | 75 | |
| JMF | 0:24d3eb812fd4 | 76 | _wizfi310.attach(this, &WizFi310Interface::event); |
| JMF | 0:24d3eb812fd4 | 77 | } |
| JMF | 0:24d3eb812fd4 | 78 | |
| JMF | 0:24d3eb812fd4 | 79 | int WizFi310Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, |
| JMF | 0:24d3eb812fd4 | 80 | uint8_t channel) |
| JMF | 0:24d3eb812fd4 | 81 | { |
| JMF | 0:24d3eb812fd4 | 82 | if (channel != 0) { |
| JMF | 0:24d3eb812fd4 | 83 | return NSAPI_ERROR_UNSUPPORTED; |
| JMF | 0:24d3eb812fd4 | 84 | } |
| JMF | 0:24d3eb812fd4 | 85 | |
| JMF | 0:24d3eb812fd4 | 86 | set_credentials(ssid, pass, security); |
| JMF | 0:24d3eb812fd4 | 87 | |
| JMF | 0:24d3eb812fd4 | 88 | return connect(); |
| JMF | 0:24d3eb812fd4 | 89 | } |
| JMF | 0:24d3eb812fd4 | 90 | |
| JMF | 0:24d3eb812fd4 | 91 | int WizFi310Interface::connect() |
| JMF | 0:24d3eb812fd4 | 92 | { |
| JMF | 0:24d3eb812fd4 | 93 | char sec[10]; |
| JMF | 0:24d3eb812fd4 | 94 | int i; |
| JMF | 0:24d3eb812fd4 | 95 | |
| JMF | 0:24d3eb812fd4 | 96 | _wizfi310.setTimeout(WIZFI310_CONNECT_TIMEOUT); |
| JMF | 0:24d3eb812fd4 | 97 | |
| JMF | 0:24d3eb812fd4 | 98 | _wizfi310.startup(0); |
| JMF | 0:24d3eb812fd4 | 99 | |
| JMF | 0:24d3eb812fd4 | 100 | if( !_wizfi310.dhcp(true) ) |
| JMF | 0:24d3eb812fd4 | 101 | { |
| JMF | 0:24d3eb812fd4 | 102 | return NSAPI_ERROR_DHCP_FAILURE; |
| JMF | 0:24d3eb812fd4 | 103 | } |
| JMF | 0:24d3eb812fd4 | 104 | |
| JMF | 0:24d3eb812fd4 | 105 | if( ap_sec == NSAPI_SECURITY_NONE && (strlen(ap_pass) > 0) ) |
| JMF | 0:24d3eb812fd4 | 106 | { |
| JMF | 0:24d3eb812fd4 | 107 | ap_sec = NSAPI_SECURITY_UNKNOWN; |
| JMF | 0:24d3eb812fd4 | 108 | } |
| JMF | 0:24d3eb812fd4 | 109 | |
| JMF | 0:24d3eb812fd4 | 110 | switch( ap_sec ) |
| JMF | 0:24d3eb812fd4 | 111 | { |
| JMF | 0:24d3eb812fd4 | 112 | case NSAPI_SECURITY_NONE: |
| JMF | 0:24d3eb812fd4 | 113 | strncpy(sec,"OPEN",strlen("OPEN")+1); |
| JMF | 0:24d3eb812fd4 | 114 | break; |
| JMF | 0:24d3eb812fd4 | 115 | case NSAPI_SECURITY_WEP: |
| JMF | 0:24d3eb812fd4 | 116 | strncpy(sec,"WEP",strlen("WEP")+1); |
| JMF | 0:24d3eb812fd4 | 117 | break; |
| JMF | 0:24d3eb812fd4 | 118 | case NSAPI_SECURITY_WPA: |
| JMF | 0:24d3eb812fd4 | 119 | strncpy(sec,"WPA",strlen("WPA")+1); |
| JMF | 0:24d3eb812fd4 | 120 | break; |
| JMF | 0:24d3eb812fd4 | 121 | case NSAPI_SECURITY_WPA2: |
| JMF | 0:24d3eb812fd4 | 122 | strncpy(sec,"WPA2",strlen("WPA2")+1); |
| JMF | 0:24d3eb812fd4 | 123 | break; |
| JMF | 0:24d3eb812fd4 | 124 | case NSAPI_SECURITY_WPA_WPA2: |
| JMF | 0:24d3eb812fd4 | 125 | strncpy(sec,"WPAWPA2",strlen("WPAWPA2")+1); |
| JMF | 0:24d3eb812fd4 | 126 | break; |
| JMF | 0:24d3eb812fd4 | 127 | default: |
| JMF | 0:24d3eb812fd4 | 128 | strncpy(sec,"",strlen("")+1); |
| JMF | 0:24d3eb812fd4 | 129 | break; |
| JMF | 0:24d3eb812fd4 | 130 | } |
| JMF | 0:24d3eb812fd4 | 131 | |
| JMF | 0:24d3eb812fd4 | 132 | for( i=0; i<WIZFI310_MAX_CONNECT; i++ ) |
| JMF | 0:24d3eb812fd4 | 133 | { |
| JMF | 0:24d3eb812fd4 | 134 | if( _wizfi310.connect(ap_ssid, ap_pass, sec) ) { |
| JMF | 0:24d3eb812fd4 | 135 | break; |
| JMF | 0:24d3eb812fd4 | 136 | } |
| JMF | 0:24d3eb812fd4 | 137 | |
| JMF | 0:24d3eb812fd4 | 138 | _wizfi310.reset(); |
| JMF | 0:24d3eb812fd4 | 139 | } |
| JMF | 0:24d3eb812fd4 | 140 | |
| JMF | 0:24d3eb812fd4 | 141 | if( i > WIZFI310_MAX_CONNECT ){ |
| JMF | 0:24d3eb812fd4 | 142 | return NSAPI_ERROR_NO_CONNECTION; |
| JMF | 0:24d3eb812fd4 | 143 | } |
| JMF | 0:24d3eb812fd4 | 144 | |
| JMF | 0:24d3eb812fd4 | 145 | if( !_wizfi310.getIPAddress() ) |
| JMF | 0:24d3eb812fd4 | 146 | { |
| JMF | 0:24d3eb812fd4 | 147 | return NSAPI_ERROR_DHCP_FAILURE; |
| JMF | 0:24d3eb812fd4 | 148 | } |
| JMF | 0:24d3eb812fd4 | 149 | |
| JMF | 0:24d3eb812fd4 | 150 | return NSAPI_ERROR_OK; |
| JMF | 0:24d3eb812fd4 | 151 | } |
| JMF | 0:24d3eb812fd4 | 152 | |
| JMF | 0:24d3eb812fd4 | 153 | |
| JMF | 0:24d3eb812fd4 | 154 | int WizFi310Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
| JMF | 0:24d3eb812fd4 | 155 | { |
| JMF | 0:24d3eb812fd4 | 156 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
| JMF | 0:24d3eb812fd4 | 157 | strncpy(ap_ssid, ssid, sizeof(ap_ssid)); |
| JMF | 0:24d3eb812fd4 | 158 | |
| JMF | 0:24d3eb812fd4 | 159 | memset(ap_pass, 0, sizeof(ap_pass)); |
| JMF | 0:24d3eb812fd4 | 160 | strncpy(ap_pass, pass, sizeof(ap_pass)); |
| JMF | 0:24d3eb812fd4 | 161 | |
| JMF | 0:24d3eb812fd4 | 162 | ap_sec = security; |
| JMF | 0:24d3eb812fd4 | 163 | return 0; |
| JMF | 0:24d3eb812fd4 | 164 | } |
| JMF | 0:24d3eb812fd4 | 165 | |
| JMF | 0:24d3eb812fd4 | 166 | |
| JMF | 0:24d3eb812fd4 | 167 | int WizFi310Interface::set_channel(uint8_t channel) |
| JMF | 0:24d3eb812fd4 | 168 | { |
| JMF | 0:24d3eb812fd4 | 169 | return 0; |
| JMF | 0:24d3eb812fd4 | 170 | } |
| JMF | 0:24d3eb812fd4 | 171 | |
| JMF | 0:24d3eb812fd4 | 172 | int WizFi310Interface::disconnect() |
| JMF | 0:24d3eb812fd4 | 173 | { |
| JMF | 0:24d3eb812fd4 | 174 | _wizfi310.setTimeout(WIZFI310_MISC_TIMEOUT); |
| JMF | 0:24d3eb812fd4 | 175 | |
| JMF | 0:24d3eb812fd4 | 176 | if (!_wizfi310.disconnect()) |
| JMF | 0:24d3eb812fd4 | 177 | { |
| JMF | 0:24d3eb812fd4 | 178 | return NSAPI_ERROR_DEVICE_ERROR; |
| JMF | 0:24d3eb812fd4 | 179 | } |
| JMF | 0:24d3eb812fd4 | 180 | return NSAPI_ERROR_OK; |
| JMF | 0:24d3eb812fd4 | 181 | } |
| JMF | 0:24d3eb812fd4 | 182 | |
| JMF | 0:24d3eb812fd4 | 183 | const char *WizFi310Interface::get_ip_address() |
| JMF | 0:24d3eb812fd4 | 184 | { |
| JMF | 0:24d3eb812fd4 | 185 | return _wizfi310.getIPAddress(); |
| JMF | 0:24d3eb812fd4 | 186 | } |
| JMF | 0:24d3eb812fd4 | 187 | |
| JMF | 0:24d3eb812fd4 | 188 | const char *WizFi310Interface::get_mac_address() |
| JMF | 0:24d3eb812fd4 | 189 | { |
| JMF | 0:24d3eb812fd4 | 190 | return _wizfi310.getMACAddress(); |
| JMF | 0:24d3eb812fd4 | 191 | } |
| JMF | 0:24d3eb812fd4 | 192 | |
| JMF | 0:24d3eb812fd4 | 193 | const char *WizFi310Interface::get_gateway() |
| JMF | 0:24d3eb812fd4 | 194 | { |
| JMF | 0:24d3eb812fd4 | 195 | return _wizfi310.getGateway(); |
| JMF | 0:24d3eb812fd4 | 196 | } |
| JMF | 0:24d3eb812fd4 | 197 | |
| JMF | 0:24d3eb812fd4 | 198 | const char *WizFi310Interface::get_netmask() |
| JMF | 0:24d3eb812fd4 | 199 | { |
| JMF | 0:24d3eb812fd4 | 200 | return _wizfi310.getNetmask(); |
| JMF | 0:24d3eb812fd4 | 201 | } |
| JMF | 0:24d3eb812fd4 | 202 | |
| JMF | 0:24d3eb812fd4 | 203 | int8_t WizFi310Interface::get_rssi() |
| JMF | 0:24d3eb812fd4 | 204 | { |
| JMF | 0:24d3eb812fd4 | 205 | return _wizfi310.getRSSI(); |
| JMF | 0:24d3eb812fd4 | 206 | } |
| JMF | 0:24d3eb812fd4 | 207 | |
| JMF | 0:24d3eb812fd4 | 208 | int WizFi310Interface::scan(WiFiAccessPoint *res, unsigned count) |
| JMF | 0:24d3eb812fd4 | 209 | { |
| JMF | 0:24d3eb812fd4 | 210 | return _wizfi310.scan(res, count); |
| JMF | 0:24d3eb812fd4 | 211 | } |
| JMF | 0:24d3eb812fd4 | 212 | |
| JMF | 0:24d3eb812fd4 | 213 | nsapi_error_t WizFi310Interface::gethostbyname(const char *host, |
| JMF | 0:24d3eb812fd4 | 214 | SocketAddress *address, nsapi_version_t version) |
| JMF | 0:24d3eb812fd4 | 215 | { |
| JMF | 0:24d3eb812fd4 | 216 | char host_ip[16]; |
| JMF | 0:24d3eb812fd4 | 217 | |
| JMF | 0:24d3eb812fd4 | 218 | if( !_wizfi310.dns_lookup(host,host_ip) ){ |
| JMF | 0:24d3eb812fd4 | 219 | return NSAPI_ERROR_DNS_FAILURE; |
| JMF | 0:24d3eb812fd4 | 220 | } |
| JMF | 0:24d3eb812fd4 | 221 | if ( !address->set_ip_address(host_ip) ){ |
| JMF | 0:24d3eb812fd4 | 222 | return NSAPI_ERROR_DNS_FAILURE; |
| JMF | 0:24d3eb812fd4 | 223 | } |
| JMF | 0:24d3eb812fd4 | 224 | |
| JMF | 0:24d3eb812fd4 | 225 | return NSAPI_ERROR_OK; |
| JMF | 0:24d3eb812fd4 | 226 | } |
| JMF | 0:24d3eb812fd4 | 227 | |
| JMF | 0:24d3eb812fd4 | 228 | struct wizfi310_socket { |
| JMF | 0:24d3eb812fd4 | 229 | int id; |
| JMF | 0:24d3eb812fd4 | 230 | nsapi_protocol_t proto; |
| JMF | 0:24d3eb812fd4 | 231 | bool connected; |
| JMF | 0:24d3eb812fd4 | 232 | SocketAddress addr; |
| JMF | 0:24d3eb812fd4 | 233 | }; |
| JMF | 0:24d3eb812fd4 | 234 | |
| JMF | 0:24d3eb812fd4 | 235 | |
| JMF | 0:24d3eb812fd4 | 236 | int WizFi310Interface::socket_open(void **handle, nsapi_protocol_t proto) |
| JMF | 0:24d3eb812fd4 | 237 | { |
| JMF | 0:24d3eb812fd4 | 238 | // Look for an unused socket |
| JMF | 0:24d3eb812fd4 | 239 | int id = -1; |
| JMF | 0:24d3eb812fd4 | 240 | |
| JMF | 0:24d3eb812fd4 | 241 | for (int i=0; i<WIZFI310_SOCKET_COUNT; i++) { |
| JMF | 0:24d3eb812fd4 | 242 | if (!_ids[i]){ |
| JMF | 0:24d3eb812fd4 | 243 | id = i; |
| JMF | 0:24d3eb812fd4 | 244 | //_ids[i] = true; |
| JMF | 0:24d3eb812fd4 | 245 | break; |
| JMF | 0:24d3eb812fd4 | 246 | } |
| JMF | 0:24d3eb812fd4 | 247 | } |
| JMF | 0:24d3eb812fd4 | 248 | |
| JMF | 0:24d3eb812fd4 | 249 | if (id == -1){ |
| JMF | 0:24d3eb812fd4 | 250 | return NSAPI_ERROR_NO_SOCKET; |
| JMF | 0:24d3eb812fd4 | 251 | } |
| JMF | 0:24d3eb812fd4 | 252 | |
| JMF | 0:24d3eb812fd4 | 253 | struct wizfi310_socket *socket = new struct wizfi310_socket; |
| JMF | 0:24d3eb812fd4 | 254 | if (!socket){ |
| JMF | 0:24d3eb812fd4 | 255 | return NSAPI_ERROR_NO_SOCKET; |
| JMF | 0:24d3eb812fd4 | 256 | } |
| JMF | 0:24d3eb812fd4 | 257 | |
| JMF | 0:24d3eb812fd4 | 258 | socket->id = id; |
| JMF | 0:24d3eb812fd4 | 259 | socket->proto = proto; |
| JMF | 0:24d3eb812fd4 | 260 | socket->connected = false; |
| JMF | 0:24d3eb812fd4 | 261 | *handle = socket; |
| JMF | 0:24d3eb812fd4 | 262 | |
| JMF | 0:24d3eb812fd4 | 263 | return 0; |
| JMF | 0:24d3eb812fd4 | 264 | } |
| JMF | 0:24d3eb812fd4 | 265 | |
| JMF | 0:24d3eb812fd4 | 266 | int WizFi310Interface::socket_close(void *handle) |
| JMF | 0:24d3eb812fd4 | 267 | { |
| JMF | 0:24d3eb812fd4 | 268 | struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; |
| JMF | 0:24d3eb812fd4 | 269 | int err = 0; |
| JMF | 0:24d3eb812fd4 | 270 | _wizfi310.setTimeout(WIZFI310_CLOSE_TIMEOUT); |
| JMF | 0:24d3eb812fd4 | 271 | |
| JMF | 0:24d3eb812fd4 | 272 | if (socket->connected && !_wizfi310.close(socket->id)) { |
| JMF | 0:24d3eb812fd4 | 273 | err = NSAPI_ERROR_DEVICE_ERROR; |
| JMF | 0:24d3eb812fd4 | 274 | } |
| JMF | 0:24d3eb812fd4 | 275 | |
| JMF | 0:24d3eb812fd4 | 276 | socket->connected = false; |
| JMF | 0:24d3eb812fd4 | 277 | _ids[socket->id] = false; |
| JMF | 0:24d3eb812fd4 | 278 | delete socket; |
| JMF | 0:24d3eb812fd4 | 279 | return err; |
| JMF | 0:24d3eb812fd4 | 280 | } |
| JMF | 0:24d3eb812fd4 | 281 | |
| JMF | 0:24d3eb812fd4 | 282 | int WizFi310Interface::socket_bind(void *handle, const SocketAddress &address) |
| JMF | 0:24d3eb812fd4 | 283 | { |
| JMF | 0:24d3eb812fd4 | 284 | return NSAPI_ERROR_UNSUPPORTED; |
| JMF | 0:24d3eb812fd4 | 285 | } |
| JMF | 0:24d3eb812fd4 | 286 | |
| JMF | 0:24d3eb812fd4 | 287 | int WizFi310Interface::socket_listen(void *handle, int backlog) |
| JMF | 0:24d3eb812fd4 | 288 | { |
| JMF | 0:24d3eb812fd4 | 289 | return NSAPI_ERROR_UNSUPPORTED; |
| JMF | 0:24d3eb812fd4 | 290 | } |
| JMF | 0:24d3eb812fd4 | 291 | |
| JMF | 0:24d3eb812fd4 | 292 | int WizFi310Interface::socket_connect(void *handle, const SocketAddress &addr) |
| JMF | 0:24d3eb812fd4 | 293 | { |
| JMF | 0:24d3eb812fd4 | 294 | struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; |
| JMF | 0:24d3eb812fd4 | 295 | _wizfi310.setTimeout(WIZFI310_OPEN_TIMEOUT); |
| JMF | 0:24d3eb812fd4 | 296 | |
| JMF | 0:24d3eb812fd4 | 297 | const char *proto = (socket->proto == NSAPI_UDP) ? "UCN" : "TCN"; |
| JMF | 0:24d3eb812fd4 | 298 | if (!_wizfi310.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) { |
| JMF | 0:24d3eb812fd4 | 299 | return NSAPI_ERROR_DEVICE_ERROR; |
| JMF | 0:24d3eb812fd4 | 300 | } |
| JMF | 0:24d3eb812fd4 | 301 | |
| JMF | 0:24d3eb812fd4 | 302 | socket->connected = true; |
| JMF | 0:24d3eb812fd4 | 303 | _ids[socket->id] = true; |
| JMF | 0:24d3eb812fd4 | 304 | return 0; |
| JMF | 0:24d3eb812fd4 | 305 | } |
| JMF | 0:24d3eb812fd4 | 306 | |
| JMF | 0:24d3eb812fd4 | 307 | int WizFi310Interface::socket_accept(void *server, void **socket, SocketAddress *addr) |
| JMF | 0:24d3eb812fd4 | 308 | { |
| JMF | 0:24d3eb812fd4 | 309 | return NSAPI_ERROR_UNSUPPORTED; |
| JMF | 0:24d3eb812fd4 | 310 | } |
| JMF | 0:24d3eb812fd4 | 311 | |
| JMF | 0:24d3eb812fd4 | 312 | int WizFi310Interface::socket_send(void *handle, const void *data, unsigned size) |
| JMF | 0:24d3eb812fd4 | 313 | { |
| JMF | 0:24d3eb812fd4 | 314 | struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; |
| JMF | 0:24d3eb812fd4 | 315 | _wizfi310.setTimeout(WIZFI310_SEND_TIMEOUT); |
| JMF | 0:24d3eb812fd4 | 316 | |
| JMF | 0:24d3eb812fd4 | 317 | if (!_wizfi310.send(socket->id, data, size)) { |
| JMF | 0:24d3eb812fd4 | 318 | return NSAPI_ERROR_DEVICE_ERROR; |
| JMF | 0:24d3eb812fd4 | 319 | } |
| JMF | 0:24d3eb812fd4 | 320 | |
| JMF | 0:24d3eb812fd4 | 321 | return size; |
| JMF | 0:24d3eb812fd4 | 322 | } |
| JMF | 0:24d3eb812fd4 | 323 | |
| JMF | 0:24d3eb812fd4 | 324 | int WizFi310Interface::socket_recv(void *handle, void *data, unsigned size) |
| JMF | 0:24d3eb812fd4 | 325 | { |
| JMF | 0:24d3eb812fd4 | 326 | struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; |
| JMF | 0:24d3eb812fd4 | 327 | _wizfi310.setTimeout(WIZFI310_RECV_TIMEOUT); |
| JMF | 0:24d3eb812fd4 | 328 | |
| JMF | 0:24d3eb812fd4 | 329 | int32_t recv = _wizfi310.recv(socket->id, data, size); |
| JMF | 0:24d3eb812fd4 | 330 | if (recv < 0) { |
| JMF | 0:24d3eb812fd4 | 331 | return NSAPI_ERROR_WOULD_BLOCK; |
| JMF | 0:24d3eb812fd4 | 332 | } |
| JMF | 0:24d3eb812fd4 | 333 | |
| JMF | 0:24d3eb812fd4 | 334 | return recv; |
| JMF | 0:24d3eb812fd4 | 335 | } |
| JMF | 0:24d3eb812fd4 | 336 | |
| JMF | 0:24d3eb812fd4 | 337 | int WizFi310Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
| JMF | 0:24d3eb812fd4 | 338 | { |
| JMF | 0:24d3eb812fd4 | 339 | struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; |
| JMF | 0:24d3eb812fd4 | 340 | |
| JMF | 0:24d3eb812fd4 | 341 | if (socket->connected && socket->addr != addr) { |
| JMF | 0:24d3eb812fd4 | 342 | _wizfi310.setTimeout(WIZFI310_MISC_TIMEOUT); |
| JMF | 0:24d3eb812fd4 | 343 | if (!_wizfi310.close(socket->id)) { |
| JMF | 0:24d3eb812fd4 | 344 | return NSAPI_ERROR_DEVICE_ERROR; |
| JMF | 0:24d3eb812fd4 | 345 | } |
| JMF | 0:24d3eb812fd4 | 346 | socket->connected = false; |
| JMF | 0:24d3eb812fd4 | 347 | } |
| JMF | 0:24d3eb812fd4 | 348 | |
| JMF | 0:24d3eb812fd4 | 349 | if (!socket->connected) { |
| JMF | 0:24d3eb812fd4 | 350 | int err = socket_connect(socket, addr); |
| JMF | 0:24d3eb812fd4 | 351 | if (err < 0 ) { |
| JMF | 0:24d3eb812fd4 | 352 | return err; |
| JMF | 0:24d3eb812fd4 | 353 | } |
| JMF | 0:24d3eb812fd4 | 354 | socket->addr = addr; |
| JMF | 0:24d3eb812fd4 | 355 | } |
| JMF | 0:24d3eb812fd4 | 356 | |
| JMF | 0:24d3eb812fd4 | 357 | return socket_send(socket, data, size); |
| JMF | 0:24d3eb812fd4 | 358 | } |
| JMF | 0:24d3eb812fd4 | 359 | |
| JMF | 0:24d3eb812fd4 | 360 | int WizFi310Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
| JMF | 0:24d3eb812fd4 | 361 | { |
| JMF | 0:24d3eb812fd4 | 362 | struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; |
| JMF | 0:24d3eb812fd4 | 363 | int ret = socket_recv(socket, data, size); |
| JMF | 0:24d3eb812fd4 | 364 | if (ret >= 0 && addr) { |
| JMF | 0:24d3eb812fd4 | 365 | *addr = socket->addr; |
| JMF | 0:24d3eb812fd4 | 366 | } |
| JMF | 0:24d3eb812fd4 | 367 | |
| JMF | 0:24d3eb812fd4 | 368 | return ret; |
| JMF | 0:24d3eb812fd4 | 369 | } |
| JMF | 0:24d3eb812fd4 | 370 | |
| JMF | 0:24d3eb812fd4 | 371 | void WizFi310Interface::socket_attach(void *handle, void (*callback)(void *), void *data) |
| JMF | 0:24d3eb812fd4 | 372 | { |
| JMF | 0:24d3eb812fd4 | 373 | struct wizfi310_socket *socket = (struct wizfi310_socket *)handle; |
| JMF | 0:24d3eb812fd4 | 374 | _cbs[socket->id].callback = callback; |
| JMF | 0:24d3eb812fd4 | 375 | _cbs[socket->id].data = data; |
| JMF | 0:24d3eb812fd4 | 376 | } |
| JMF | 0:24d3eb812fd4 | 377 | |
| JMF | 0:24d3eb812fd4 | 378 | void WizFi310Interface::event() |
| JMF | 0:24d3eb812fd4 | 379 | { |
| JMF | 0:24d3eb812fd4 | 380 | for(int i=0; i<WIZFI310_SOCKET_COUNT; i++){ |
| JMF | 0:24d3eb812fd4 | 381 | if (_cbs[i].callback) { |
| JMF | 0:24d3eb812fd4 | 382 | _cbs[i].callback(_cbs[i].data); |
| JMF | 0:24d3eb812fd4 | 383 | } |
| JMF | 0:24d3eb812fd4 | 384 | } |
| JMF | 0:24d3eb812fd4 | 385 | } |