wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/wifi-x-nucleo-idw01m1/SpwfSAInterface.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 | /* mbed Microcontroller Library |
JMF | 0:24d3eb812fd4 | 2 | * Copyright (c) 20015 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 SpwfSAInterface.cpp |
JMF | 0:24d3eb812fd4 | 20 | * @author STMicroelectronics |
JMF | 0:24d3eb812fd4 | 21 | * @brief Implementation of the NetworkStack for the SPWF Device |
JMF | 0:24d3eb812fd4 | 22 | ****************************************************************************** |
JMF | 0:24d3eb812fd4 | 23 | * @copy |
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, STMICROELECTRONICS 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 2016 STMicroelectronics</center></h2> |
JMF | 0:24d3eb812fd4 | 33 | ****************************************************************************** |
JMF | 0:24d3eb812fd4 | 34 | */ |
JMF | 0:24d3eb812fd4 | 35 | |
JMF | 0:24d3eb812fd4 | 36 | #include "SpwfSAInterface.h" |
JMF | 0:24d3eb812fd4 | 37 | #include "mbed_debug.h" |
JMF | 0:24d3eb812fd4 | 38 | #include "BlockExecuter.h" |
JMF | 0:24d3eb812fd4 | 39 | |
JMF | 0:24d3eb812fd4 | 40 | #if MBED_CONF_RTOS_PRESENT |
JMF | 0:24d3eb812fd4 | 41 | #define SYNC_HANDLER ScopedMutexLock sync_handler(_spwf_mutex) // assuming a recursive mutex |
JMF | 0:24d3eb812fd4 | 42 | #else |
JMF | 0:24d3eb812fd4 | 43 | #define SYNC_HANDLER |
JMF | 0:24d3eb812fd4 | 44 | #endif |
JMF | 0:24d3eb812fd4 | 45 | |
JMF | 0:24d3eb812fd4 | 46 | |
JMF | 0:24d3eb812fd4 | 47 | SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, |
JMF | 0:24d3eb812fd4 | 48 | PinName rts, PinName cts, bool debug, |
JMF | 0:24d3eb812fd4 | 49 | PinName wakeup, PinName reset) |
JMF | 0:24d3eb812fd4 | 50 | : _spwf(tx, rx, rts, cts, *this, debug, wakeup, reset), |
JMF | 0:24d3eb812fd4 | 51 | _dbg_on(debug) |
JMF | 0:24d3eb812fd4 | 52 | { |
JMF | 0:24d3eb812fd4 | 53 | inner_constructor(); |
JMF | 0:24d3eb812fd4 | 54 | reset_credentials(); |
JMF | 0:24d3eb812fd4 | 55 | } |
JMF | 0:24d3eb812fd4 | 56 | |
JMF | 0:24d3eb812fd4 | 57 | nsapi_error_t SpwfSAInterface::init(void) |
JMF | 0:24d3eb812fd4 | 58 | { |
JMF | 0:24d3eb812fd4 | 59 | _spwf.setTimeout(SPWF_INIT_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 60 | |
JMF | 0:24d3eb812fd4 | 61 | if(_spwf.startup(0)) { |
JMF | 0:24d3eb812fd4 | 62 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 63 | } |
JMF | 0:24d3eb812fd4 | 64 | else return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 65 | } |
JMF | 0:24d3eb812fd4 | 66 | |
JMF | 0:24d3eb812fd4 | 67 | nsapi_error_t SpwfSAInterface::connect(void) |
JMF | 0:24d3eb812fd4 | 68 | { |
JMF | 0:24d3eb812fd4 | 69 | int mode; |
JMF | 0:24d3eb812fd4 | 70 | char *pass_phrase = ap_pass; |
JMF | 0:24d3eb812fd4 | 71 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 72 | |
JMF | 0:24d3eb812fd4 | 73 | // check for valid SSID |
JMF | 0:24d3eb812fd4 | 74 | if(ap_ssid[0] == '\0') { |
JMF | 0:24d3eb812fd4 | 75 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 76 | } |
JMF | 0:24d3eb812fd4 | 77 | |
JMF | 0:24d3eb812fd4 | 78 | switch(ap_sec) |
JMF | 0:24d3eb812fd4 | 79 | { |
JMF | 0:24d3eb812fd4 | 80 | case NSAPI_SECURITY_NONE: |
JMF | 0:24d3eb812fd4 | 81 | mode = 0; |
JMF | 0:24d3eb812fd4 | 82 | pass_phrase = NULL; |
JMF | 0:24d3eb812fd4 | 83 | break; |
JMF | 0:24d3eb812fd4 | 84 | case NSAPI_SECURITY_WEP: |
JMF | 0:24d3eb812fd4 | 85 | mode = 1; |
JMF | 0:24d3eb812fd4 | 86 | break; |
JMF | 0:24d3eb812fd4 | 87 | case NSAPI_SECURITY_WPA: |
JMF | 0:24d3eb812fd4 | 88 | case NSAPI_SECURITY_WPA2: |
JMF | 0:24d3eb812fd4 | 89 | mode = 2; |
JMF | 0:24d3eb812fd4 | 90 | break; |
JMF | 0:24d3eb812fd4 | 91 | default: |
JMF | 0:24d3eb812fd4 | 92 | mode = 2; |
JMF | 0:24d3eb812fd4 | 93 | break; |
JMF | 0:24d3eb812fd4 | 94 | } |
JMF | 0:24d3eb812fd4 | 95 | |
JMF | 0:24d3eb812fd4 | 96 | // First: disconnect |
JMF | 0:24d3eb812fd4 | 97 | if(_connected_to_network) { |
JMF | 0:24d3eb812fd4 | 98 | if(!disconnect()) { |
JMF | 0:24d3eb812fd4 | 99 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 100 | } |
JMF | 0:24d3eb812fd4 | 101 | } |
JMF | 0:24d3eb812fd4 | 102 | |
JMF | 0:24d3eb812fd4 | 103 | //initialize the device before connecting |
JMF | 0:24d3eb812fd4 | 104 | if(!_isInitialized) |
JMF | 0:24d3eb812fd4 | 105 | { |
JMF | 0:24d3eb812fd4 | 106 | if(init() != NSAPI_ERROR_OK) return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 107 | _isInitialized=true; |
JMF | 0:24d3eb812fd4 | 108 | } |
JMF | 0:24d3eb812fd4 | 109 | |
JMF | 0:24d3eb812fd4 | 110 | // Then: (re-)connect |
JMF | 0:24d3eb812fd4 | 111 | _spwf.setTimeout(SPWF_CONNECT_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 112 | |
JMF | 0:24d3eb812fd4 | 113 | if (!_spwf.connect(ap_ssid, pass_phrase, mode)) { |
JMF | 0:24d3eb812fd4 | 114 | return NSAPI_ERROR_AUTH_FAILURE; |
JMF | 0:24d3eb812fd4 | 115 | } |
JMF | 0:24d3eb812fd4 | 116 | |
JMF | 0:24d3eb812fd4 | 117 | if (!_spwf.getIPAddress()) { |
JMF | 0:24d3eb812fd4 | 118 | return NSAPI_ERROR_DHCP_FAILURE; |
JMF | 0:24d3eb812fd4 | 119 | } |
JMF | 0:24d3eb812fd4 | 120 | |
JMF | 0:24d3eb812fd4 | 121 | _connected_to_network = true; |
JMF | 0:24d3eb812fd4 | 122 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 123 | } |
JMF | 0:24d3eb812fd4 | 124 | |
JMF | 0:24d3eb812fd4 | 125 | nsapi_error_t SpwfSAInterface::connect(const char *ssid, const char *pass, nsapi_security_t security, |
JMF | 0:24d3eb812fd4 | 126 | uint8_t channel) |
JMF | 0:24d3eb812fd4 | 127 | { |
JMF | 0:24d3eb812fd4 | 128 | nsapi_error_t ret; |
JMF | 0:24d3eb812fd4 | 129 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 130 | |
JMF | 0:24d3eb812fd4 | 131 | if (channel != 0) { |
JMF | 0:24d3eb812fd4 | 132 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 133 | } |
JMF | 0:24d3eb812fd4 | 134 | |
JMF | 0:24d3eb812fd4 | 135 | ret = set_credentials(ssid, pass, security); |
JMF | 0:24d3eb812fd4 | 136 | if(ret != NSAPI_ERROR_OK) return ret; |
JMF | 0:24d3eb812fd4 | 137 | |
JMF | 0:24d3eb812fd4 | 138 | return connect(); |
JMF | 0:24d3eb812fd4 | 139 | } |
JMF | 0:24d3eb812fd4 | 140 | |
JMF | 0:24d3eb812fd4 | 141 | nsapi_error_t SpwfSAInterface::disconnect(void) |
JMF | 0:24d3eb812fd4 | 142 | { |
JMF | 0:24d3eb812fd4 | 143 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 144 | |
JMF | 0:24d3eb812fd4 | 145 | _spwf.setTimeout(SPWF_DISCONNECT_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 146 | |
JMF | 0:24d3eb812fd4 | 147 | if (!_spwf.disconnect()) { |
JMF | 0:24d3eb812fd4 | 148 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 149 | } |
JMF | 0:24d3eb812fd4 | 150 | |
JMF | 0:24d3eb812fd4 | 151 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 152 | } |
JMF | 0:24d3eb812fd4 | 153 | |
JMF | 0:24d3eb812fd4 | 154 | const char *SpwfSAInterface::get_ip_address(void) |
JMF | 0:24d3eb812fd4 | 155 | { |
JMF | 0:24d3eb812fd4 | 156 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 157 | |
JMF | 0:24d3eb812fd4 | 158 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 159 | return _spwf.getIPAddress(); |
JMF | 0:24d3eb812fd4 | 160 | } |
JMF | 0:24d3eb812fd4 | 161 | |
JMF | 0:24d3eb812fd4 | 162 | const char *SpwfSAInterface::get_mac_address(void) |
JMF | 0:24d3eb812fd4 | 163 | { |
JMF | 0:24d3eb812fd4 | 164 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 165 | |
JMF | 0:24d3eb812fd4 | 166 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 167 | return _spwf.getMACAddress(); |
JMF | 0:24d3eb812fd4 | 168 | } |
JMF | 0:24d3eb812fd4 | 169 | |
JMF | 0:24d3eb812fd4 | 170 | const char *SpwfSAInterface::get_gateway(void) |
JMF | 0:24d3eb812fd4 | 171 | { |
JMF | 0:24d3eb812fd4 | 172 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 173 | |
JMF | 0:24d3eb812fd4 | 174 | if(!_connected_to_network) return NULL; |
JMF | 0:24d3eb812fd4 | 175 | |
JMF | 0:24d3eb812fd4 | 176 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 177 | return _spwf.getGateway(); |
JMF | 0:24d3eb812fd4 | 178 | } |
JMF | 0:24d3eb812fd4 | 179 | |
JMF | 0:24d3eb812fd4 | 180 | const char *SpwfSAInterface::get_netmask(void) |
JMF | 0:24d3eb812fd4 | 181 | { |
JMF | 0:24d3eb812fd4 | 182 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 183 | |
JMF | 0:24d3eb812fd4 | 184 | if(!_connected_to_network) return NULL; |
JMF | 0:24d3eb812fd4 | 185 | |
JMF | 0:24d3eb812fd4 | 186 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 187 | return _spwf.getNetmask(); |
JMF | 0:24d3eb812fd4 | 188 | } |
JMF | 0:24d3eb812fd4 | 189 | |
JMF | 0:24d3eb812fd4 | 190 | nsapi_error_t SpwfSAInterface::socket_open(void **handle, nsapi_protocol_t proto) |
JMF | 0:24d3eb812fd4 | 191 | { |
JMF | 0:24d3eb812fd4 | 192 | int internal_id; |
JMF | 0:24d3eb812fd4 | 193 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 194 | |
JMF | 0:24d3eb812fd4 | 195 | for (internal_id = 0; internal_id < SPWFSA_SOCKET_COUNT; internal_id++) { |
JMF | 0:24d3eb812fd4 | 196 | if(_ids[internal_id].internal_id == SPWFSA_SOCKET_COUNT) break; |
JMF | 0:24d3eb812fd4 | 197 | } |
JMF | 0:24d3eb812fd4 | 198 | |
JMF | 0:24d3eb812fd4 | 199 | if(internal_id == SPWFSA_SOCKET_COUNT) { |
JMF | 0:24d3eb812fd4 | 200 | debug_if(_dbg_on, "NO Socket ID Error\r\n"); |
JMF | 0:24d3eb812fd4 | 201 | return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 202 | } |
JMF | 0:24d3eb812fd4 | 203 | |
JMF | 0:24d3eb812fd4 | 204 | spwf_socket_t *socket = &_ids[internal_id]; |
JMF | 0:24d3eb812fd4 | 205 | socket->internal_id = internal_id; |
JMF | 0:24d3eb812fd4 | 206 | socket->spwf_id = SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 207 | socket->server_gone = false; |
JMF | 0:24d3eb812fd4 | 208 | socket->no_more_data = false; |
JMF | 0:24d3eb812fd4 | 209 | socket->proto = proto; |
JMF | 0:24d3eb812fd4 | 210 | socket->addr = SocketAddress(); |
JMF | 0:24d3eb812fd4 | 211 | |
JMF | 0:24d3eb812fd4 | 212 | *handle = socket; |
JMF | 0:24d3eb812fd4 | 213 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 214 | } |
JMF | 0:24d3eb812fd4 | 215 | |
JMF | 0:24d3eb812fd4 | 216 | nsapi_error_t SpwfSAInterface::socket_connect(void *handle, const SocketAddress &addr) |
JMF | 0:24d3eb812fd4 | 217 | { |
JMF | 0:24d3eb812fd4 | 218 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
JMF | 0:24d3eb812fd4 | 219 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 220 | |
JMF | 0:24d3eb812fd4 | 221 | MBED_ASSERT(((unsigned int)socket->internal_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)); |
JMF | 0:24d3eb812fd4 | 222 | |
JMF | 0:24d3eb812fd4 | 223 | if(_socket_has_connected(socket->internal_id)) { |
JMF | 0:24d3eb812fd4 | 224 | return NSAPI_ERROR_IS_CONNECTED; |
JMF | 0:24d3eb812fd4 | 225 | } |
JMF | 0:24d3eb812fd4 | 226 | |
JMF | 0:24d3eb812fd4 | 227 | _spwf.setTimeout(SPWF_OPEN_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 228 | |
JMF | 0:24d3eb812fd4 | 229 | const char *proto = (socket->proto == NSAPI_UDP) ? "u" : "t"; //"s" for secure socket? |
JMF | 0:24d3eb812fd4 | 230 | |
JMF | 0:24d3eb812fd4 | 231 | if(addr.get_ip_version() != NSAPI_IPv4) { // IPv6 not supported (yet) |
JMF | 0:24d3eb812fd4 | 232 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 233 | } |
JMF | 0:24d3eb812fd4 | 234 | |
JMF | 0:24d3eb812fd4 | 235 | { |
JMF | 0:24d3eb812fd4 | 236 | BlockExecuter netsock_wa_obj(Callback<void()>(&_spwf, &SPWFSAxx::_unblock_event_callback), |
JMF | 0:24d3eb812fd4 | 237 | Callback<void()>(&_spwf, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */ |
JMF | 0:24d3eb812fd4 | 238 | |
JMF | 0:24d3eb812fd4 | 239 | /* block asynchronous indications */ |
JMF | 0:24d3eb812fd4 | 240 | if(!_spwf._winds_off()) { |
JMF | 0:24d3eb812fd4 | 241 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 242 | } |
JMF | 0:24d3eb812fd4 | 243 | |
JMF | 0:24d3eb812fd4 | 244 | { |
JMF | 0:24d3eb812fd4 | 245 | BlockExecuter bh_handler(Callback<void()>(&_spwf, &SPWFSAxx::_execute_bottom_halves)); |
JMF | 0:24d3eb812fd4 | 246 | { |
JMF | 0:24d3eb812fd4 | 247 | BlockExecuter winds_enabler(Callback<void()>(&_spwf, &SPWFSAxx::_winds_on)); |
JMF | 0:24d3eb812fd4 | 248 | |
JMF | 0:24d3eb812fd4 | 249 | if(!_spwf.open(proto, &socket->spwf_id, addr.get_ip_address(), addr.get_port())) { |
JMF | 0:24d3eb812fd4 | 250 | MBED_ASSERT(_spwf._call_event_callback_blocked == 1); |
JMF | 0:24d3eb812fd4 | 251 | |
JMF | 0:24d3eb812fd4 | 252 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 253 | } |
JMF | 0:24d3eb812fd4 | 254 | |
JMF | 0:24d3eb812fd4 | 255 | /* check for the module to report a valid id */ |
JMF | 0:24d3eb812fd4 | 256 | MBED_ASSERT(((unsigned int)socket->spwf_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)); |
JMF | 0:24d3eb812fd4 | 257 | |
JMF | 0:24d3eb812fd4 | 258 | _internal_ids[socket->spwf_id] = socket->internal_id; |
JMF | 0:24d3eb812fd4 | 259 | socket->addr = addr; |
JMF | 0:24d3eb812fd4 | 260 | |
JMF | 0:24d3eb812fd4 | 261 | MBED_ASSERT(_spwf._call_event_callback_blocked == 1); |
JMF | 0:24d3eb812fd4 | 262 | |
JMF | 0:24d3eb812fd4 | 263 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 264 | } |
JMF | 0:24d3eb812fd4 | 265 | } |
JMF | 0:24d3eb812fd4 | 266 | } |
JMF | 0:24d3eb812fd4 | 267 | } |
JMF | 0:24d3eb812fd4 | 268 | |
JMF | 0:24d3eb812fd4 | 269 | nsapi_error_t SpwfSAInterface::socket_bind(void *handle, const SocketAddress &address) |
JMF | 0:24d3eb812fd4 | 270 | { |
JMF | 0:24d3eb812fd4 | 271 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 272 | } |
JMF | 0:24d3eb812fd4 | 273 | |
JMF | 0:24d3eb812fd4 | 274 | nsapi_error_t SpwfSAInterface::socket_listen(void *handle, int backlog) |
JMF | 0:24d3eb812fd4 | 275 | { |
JMF | 0:24d3eb812fd4 | 276 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 277 | } |
JMF | 0:24d3eb812fd4 | 278 | |
JMF | 0:24d3eb812fd4 | 279 | nsapi_error_t SpwfSAInterface::socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address) |
JMF | 0:24d3eb812fd4 | 280 | { |
JMF | 0:24d3eb812fd4 | 281 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 282 | } |
JMF | 0:24d3eb812fd4 | 283 | |
JMF | 0:24d3eb812fd4 | 284 | nsapi_error_t SpwfSAInterface::socket_close(void *handle) |
JMF | 0:24d3eb812fd4 | 285 | { |
JMF | 0:24d3eb812fd4 | 286 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
JMF | 0:24d3eb812fd4 | 287 | int internal_id = socket->internal_id; |
JMF | 0:24d3eb812fd4 | 288 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 289 | |
JMF | 0:24d3eb812fd4 | 290 | if(!_socket_is_open(internal_id)) return NSAPI_ERROR_NO_SOCKET; |
JMF | 0:24d3eb812fd4 | 291 | |
JMF | 0:24d3eb812fd4 | 292 | if(_socket_has_connected(socket)) { |
JMF | 0:24d3eb812fd4 | 293 | _spwf.setTimeout(SPWF_CLOSE_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 294 | if (!_spwf.close(socket->spwf_id)) { |
JMF | 0:24d3eb812fd4 | 295 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 296 | } |
JMF | 0:24d3eb812fd4 | 297 | _internal_ids[socket->spwf_id] = SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 298 | } |
JMF | 0:24d3eb812fd4 | 299 | |
JMF | 0:24d3eb812fd4 | 300 | _ids[internal_id].internal_id = SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 301 | _ids[internal_id].spwf_id = SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 302 | |
JMF | 0:24d3eb812fd4 | 303 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 304 | } |
JMF | 0:24d3eb812fd4 | 305 | |
JMF | 0:24d3eb812fd4 | 306 | nsapi_size_or_error_t SpwfSAInterface::socket_send(void *handle, const void *data, unsigned size) |
JMF | 0:24d3eb812fd4 | 307 | { |
JMF | 0:24d3eb812fd4 | 308 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
JMF | 0:24d3eb812fd4 | 309 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 310 | |
JMF | 0:24d3eb812fd4 | 311 | CHECK_NOT_CONNECTED_ERR(); |
JMF | 0:24d3eb812fd4 | 312 | |
JMF | 0:24d3eb812fd4 | 313 | _spwf.setTimeout(SPWF_SEND_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 314 | return _spwf.send(socket->spwf_id, data, size, socket->internal_id); |
JMF | 0:24d3eb812fd4 | 315 | } |
JMF | 0:24d3eb812fd4 | 316 | |
JMF | 0:24d3eb812fd4 | 317 | nsapi_size_or_error_t SpwfSAInterface::socket_recv(void *handle, void *data, unsigned size) |
JMF | 0:24d3eb812fd4 | 318 | { |
JMF | 0:24d3eb812fd4 | 319 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 320 | |
JMF | 0:24d3eb812fd4 | 321 | return _socket_recv(handle, data, size, false); |
JMF | 0:24d3eb812fd4 | 322 | } |
JMF | 0:24d3eb812fd4 | 323 | |
JMF | 0:24d3eb812fd4 | 324 | nsapi_size_or_error_t SpwfSAInterface::_socket_recv(void *handle, void *data, unsigned size, bool datagram) |
JMF | 0:24d3eb812fd4 | 325 | { |
JMF | 0:24d3eb812fd4 | 326 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
JMF | 0:24d3eb812fd4 | 327 | |
JMF | 0:24d3eb812fd4 | 328 | CHECK_NOT_CONNECTED_ERR(); |
JMF | 0:24d3eb812fd4 | 329 | |
JMF | 0:24d3eb812fd4 | 330 | if(!_socket_has_connected(socket)) { |
JMF | 0:24d3eb812fd4 | 331 | return NSAPI_ERROR_WOULD_BLOCK; |
JMF | 0:24d3eb812fd4 | 332 | } else if(socket->no_more_data) { |
JMF | 0:24d3eb812fd4 | 333 | return 0; |
JMF | 0:24d3eb812fd4 | 334 | } |
JMF | 0:24d3eb812fd4 | 335 | |
JMF | 0:24d3eb812fd4 | 336 | _spwf.setTimeout(SPWF_RECV_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 337 | |
JMF | 0:24d3eb812fd4 | 338 | int32_t recv = _spwf.recv(socket->spwf_id, (char*)data, (uint32_t)size, datagram); |
JMF | 0:24d3eb812fd4 | 339 | |
JMF | 0:24d3eb812fd4 | 340 | MBED_ASSERT(!_spwf._is_event_callback_blocked()); |
JMF | 0:24d3eb812fd4 | 341 | MBED_ASSERT((recv != 0) || (size == 0)); |
JMF | 0:24d3eb812fd4 | 342 | |
JMF | 0:24d3eb812fd4 | 343 | if (recv < 0) { |
JMF | 0:24d3eb812fd4 | 344 | if(!_socket_is_still_connected(socket)) { |
JMF | 0:24d3eb812fd4 | 345 | socket->no_more_data = true; |
JMF | 0:24d3eb812fd4 | 346 | return 0; |
JMF | 0:24d3eb812fd4 | 347 | } |
JMF | 0:24d3eb812fd4 | 348 | |
JMF | 0:24d3eb812fd4 | 349 | return NSAPI_ERROR_WOULD_BLOCK; |
JMF | 0:24d3eb812fd4 | 350 | } |
JMF | 0:24d3eb812fd4 | 351 | |
JMF | 0:24d3eb812fd4 | 352 | return recv; |
JMF | 0:24d3eb812fd4 | 353 | } |
JMF | 0:24d3eb812fd4 | 354 | |
JMF | 0:24d3eb812fd4 | 355 | nsapi_size_or_error_t SpwfSAInterface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
JMF | 0:24d3eb812fd4 | 356 | { |
JMF | 0:24d3eb812fd4 | 357 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
JMF | 0:24d3eb812fd4 | 358 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 359 | |
JMF | 0:24d3eb812fd4 | 360 | CHECK_NOT_CONNECTED_ERR(); |
JMF | 0:24d3eb812fd4 | 361 | |
JMF | 0:24d3eb812fd4 | 362 | if ((_socket_has_connected(socket)) && (socket->addr != addr)) { |
JMF | 0:24d3eb812fd4 | 363 | _spwf.setTimeout(SPWF_CLOSE_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 364 | if (!_spwf.close(socket->spwf_id)) { |
JMF | 0:24d3eb812fd4 | 365 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 366 | } |
JMF | 0:24d3eb812fd4 | 367 | _internal_ids[socket->spwf_id] = SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 368 | socket->spwf_id = SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 369 | } |
JMF | 0:24d3eb812fd4 | 370 | |
JMF | 0:24d3eb812fd4 | 371 | _spwf.setTimeout(SPWF_CONN_SND_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 372 | if (!_socket_has_connected(socket)) { |
JMF | 0:24d3eb812fd4 | 373 | nsapi_error_t err = socket_connect(socket, addr); |
JMF | 0:24d3eb812fd4 | 374 | if (err < 0) { |
JMF | 0:24d3eb812fd4 | 375 | return err; |
JMF | 0:24d3eb812fd4 | 376 | } |
JMF | 0:24d3eb812fd4 | 377 | } |
JMF | 0:24d3eb812fd4 | 378 | |
JMF | 0:24d3eb812fd4 | 379 | return socket_send(socket, data, size); |
JMF | 0:24d3eb812fd4 | 380 | } |
JMF | 0:24d3eb812fd4 | 381 | |
JMF | 0:24d3eb812fd4 | 382 | nsapi_size_or_error_t SpwfSAInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
JMF | 0:24d3eb812fd4 | 383 | { |
JMF | 0:24d3eb812fd4 | 384 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
JMF | 0:24d3eb812fd4 | 385 | nsapi_error_t ret; |
JMF | 0:24d3eb812fd4 | 386 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 387 | |
JMF | 0:24d3eb812fd4 | 388 | ret = _socket_recv(socket, data, size, true); |
JMF | 0:24d3eb812fd4 | 389 | if (ret >= 0 && addr) { |
JMF | 0:24d3eb812fd4 | 390 | *addr = socket->addr; |
JMF | 0:24d3eb812fd4 | 391 | } |
JMF | 0:24d3eb812fd4 | 392 | |
JMF | 0:24d3eb812fd4 | 393 | return ret; |
JMF | 0:24d3eb812fd4 | 394 | } |
JMF | 0:24d3eb812fd4 | 395 | |
JMF | 0:24d3eb812fd4 | 396 | void SpwfSAInterface::socket_attach(void *handle, void (*callback)(void *), void *data) |
JMF | 0:24d3eb812fd4 | 397 | { |
JMF | 0:24d3eb812fd4 | 398 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
JMF | 0:24d3eb812fd4 | 399 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 400 | |
JMF | 0:24d3eb812fd4 | 401 | if(!_socket_is_open(socket)) return; // might happen e.g. after module hard fault or voluntary disconnection |
JMF | 0:24d3eb812fd4 | 402 | |
JMF | 0:24d3eb812fd4 | 403 | _cbs[socket->internal_id].callback = callback; |
JMF | 0:24d3eb812fd4 | 404 | _cbs[socket->internal_id].data = data; |
JMF | 0:24d3eb812fd4 | 405 | } |
JMF | 0:24d3eb812fd4 | 406 | |
JMF | 0:24d3eb812fd4 | 407 | void SpwfSAInterface::event(void) { |
JMF | 0:24d3eb812fd4 | 408 | for (int internal_id = 0; internal_id < SPWFSA_SOCKET_COUNT; internal_id++) { |
JMF | 0:24d3eb812fd4 | 409 | if (_cbs[internal_id].callback && (_ids[internal_id].internal_id != SPWFSA_SOCKET_COUNT)) { |
JMF | 0:24d3eb812fd4 | 410 | _cbs[internal_id].callback(_cbs[internal_id].data); |
JMF | 0:24d3eb812fd4 | 411 | } |
JMF | 0:24d3eb812fd4 | 412 | } |
JMF | 0:24d3eb812fd4 | 413 | } |
JMF | 0:24d3eb812fd4 | 414 | |
JMF | 0:24d3eb812fd4 | 415 | nsapi_error_t SpwfSAInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
JMF | 0:24d3eb812fd4 | 416 | { |
JMF | 0:24d3eb812fd4 | 417 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 418 | |
JMF | 0:24d3eb812fd4 | 419 | if((ssid == NULL) || (strlen(ssid) == 0)) { |
JMF | 0:24d3eb812fd4 | 420 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 421 | } |
JMF | 0:24d3eb812fd4 | 422 | |
JMF | 0:24d3eb812fd4 | 423 | if((pass != NULL) && (strlen(pass) > 0)) { |
JMF | 0:24d3eb812fd4 | 424 | if(strlen(pass) < sizeof(ap_pass)) { |
JMF | 0:24d3eb812fd4 | 425 | if(security == NSAPI_SECURITY_NONE) { |
JMF | 0:24d3eb812fd4 | 426 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 427 | } |
JMF | 0:24d3eb812fd4 | 428 | } else { |
JMF | 0:24d3eb812fd4 | 429 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 430 | } |
JMF | 0:24d3eb812fd4 | 431 | } else if(security != NSAPI_SECURITY_NONE) { |
JMF | 0:24d3eb812fd4 | 432 | return NSAPI_ERROR_PARAMETER; |
JMF | 0:24d3eb812fd4 | 433 | } |
JMF | 0:24d3eb812fd4 | 434 | |
JMF | 0:24d3eb812fd4 | 435 | reset_credentials(); |
JMF | 0:24d3eb812fd4 | 436 | |
JMF | 0:24d3eb812fd4 | 437 | ap_sec = security; |
JMF | 0:24d3eb812fd4 | 438 | strncpy(ap_ssid, ssid, sizeof(ap_ssid) - 1); |
JMF | 0:24d3eb812fd4 | 439 | strncpy(ap_pass, pass, sizeof(ap_pass) - 1); |
JMF | 0:24d3eb812fd4 | 440 | |
JMF | 0:24d3eb812fd4 | 441 | return NSAPI_ERROR_OK; |
JMF | 0:24d3eb812fd4 | 442 | } |
JMF | 0:24d3eb812fd4 | 443 | |
JMF | 0:24d3eb812fd4 | 444 | nsapi_error_t SpwfSAInterface::set_channel(uint8_t channel) |
JMF | 0:24d3eb812fd4 | 445 | { |
JMF | 0:24d3eb812fd4 | 446 | return NSAPI_ERROR_UNSUPPORTED; |
JMF | 0:24d3eb812fd4 | 447 | } |
JMF | 0:24d3eb812fd4 | 448 | |
JMF | 0:24d3eb812fd4 | 449 | int8_t SpwfSAInterface::get_rssi(void) |
JMF | 0:24d3eb812fd4 | 450 | { |
JMF | 0:24d3eb812fd4 | 451 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 452 | |
JMF | 0:24d3eb812fd4 | 453 | if(!_connected_to_network) return 0; |
JMF | 0:24d3eb812fd4 | 454 | |
JMF | 0:24d3eb812fd4 | 455 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 456 | return _spwf.getRssi(); |
JMF | 0:24d3eb812fd4 | 457 | } |
JMF | 0:24d3eb812fd4 | 458 | |
JMF | 0:24d3eb812fd4 | 459 | nsapi_size_or_error_t SpwfSAInterface::scan(WiFiAccessPoint *res, unsigned count) |
JMF | 0:24d3eb812fd4 | 460 | { |
JMF | 0:24d3eb812fd4 | 461 | SYNC_HANDLER; |
JMF | 0:24d3eb812fd4 | 462 | |
JMF | 0:24d3eb812fd4 | 463 | nsapi_size_or_error_t ret; |
JMF | 0:24d3eb812fd4 | 464 | |
JMF | 0:24d3eb812fd4 | 465 | //initialize the device before scanning |
JMF | 0:24d3eb812fd4 | 466 | if(!_isInitialized) |
JMF | 0:24d3eb812fd4 | 467 | { |
JMF | 0:24d3eb812fd4 | 468 | if(init() != NSAPI_ERROR_OK) return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 469 | } |
JMF | 0:24d3eb812fd4 | 470 | |
JMF | 0:24d3eb812fd4 | 471 | _spwf.setTimeout(SPWF_SCAN_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 472 | |
JMF | 0:24d3eb812fd4 | 473 | { |
JMF | 0:24d3eb812fd4 | 474 | BlockExecuter netsock_wa_obj(Callback<void()>(&_spwf, &SPWFSAxx::_unblock_event_callback), |
JMF | 0:24d3eb812fd4 | 475 | Callback<void()>(&_spwf, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */ |
JMF | 0:24d3eb812fd4 | 476 | |
JMF | 0:24d3eb812fd4 | 477 | /* block asynchronous indications */ |
JMF | 0:24d3eb812fd4 | 478 | if(!_spwf._winds_off()) { |
JMF | 0:24d3eb812fd4 | 479 | MBED_ASSERT(_spwf._call_event_callback_blocked == 1); |
JMF | 0:24d3eb812fd4 | 480 | |
JMF | 0:24d3eb812fd4 | 481 | return NSAPI_ERROR_DEVICE_ERROR; |
JMF | 0:24d3eb812fd4 | 482 | } |
JMF | 0:24d3eb812fd4 | 483 | |
JMF | 0:24d3eb812fd4 | 484 | ret = _spwf.scan(res, count); |
JMF | 0:24d3eb812fd4 | 485 | |
JMF | 0:24d3eb812fd4 | 486 | /* unblock asynchronous indications */ |
JMF | 0:24d3eb812fd4 | 487 | _spwf._winds_on(); |
JMF | 0:24d3eb812fd4 | 488 | } |
JMF | 0:24d3eb812fd4 | 489 | |
JMF | 0:24d3eb812fd4 | 490 | MBED_ASSERT(!_spwf._is_event_callback_blocked()); |
JMF | 0:24d3eb812fd4 | 491 | |
JMF | 0:24d3eb812fd4 | 492 | //de-initialize the device after scanning |
JMF | 0:24d3eb812fd4 | 493 | if(!_isInitialized) |
JMF | 0:24d3eb812fd4 | 494 | { |
JMF | 0:24d3eb812fd4 | 495 | nsapi_error_t err = disconnect(); |
JMF | 0:24d3eb812fd4 | 496 | if(err != NSAPI_ERROR_OK) return err; |
JMF | 0:24d3eb812fd4 | 497 | } |
JMF | 0:24d3eb812fd4 | 498 | |
JMF | 0:24d3eb812fd4 | 499 | return ret; |
JMF | 0:24d3eb812fd4 | 500 | } |