Generic Pelion Device Management example for various Advantech modules.
This example is known to work great on the following platforms:
- WISE-1530 WiFi Module using DB-1505 carrier board and external SD card reader.
Example Functionality
This example showcases the following device functionality:
- On timer button increment, simulate Pelion LWM2M button resource change
Use this example with Mbed CLI
1. Import the application into your desktop:
mbed import https://os.mbed.com/teams/Advantech/code/pelion-example-common cd pelion-example-common
2. Download your developer certificate from pelion portal
3. Compile the program
mbed compile -t <toolchain> -m <TARGET_BOARD>
(supported toolchains : GCC_ARM / ARM / IAR)
4. Copy the binary file pelion-example-common.bin to your mbed device.
drivers/network/COMPONENT_WIFI_IDW04A1/SpwfSAInterface.cpp@0:43ff9e3bc244, 2019-03-12 (annotated)
- Committer:
- chuanga
- Date:
- Tue Mar 12 13:48:39 2019 +0800
- Revision:
- 0:43ff9e3bc244
copying sources from github repository
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chuanga | 0:43ff9e3bc244 | 1 | /* mbed Microcontroller Library |
chuanga | 0:43ff9e3bc244 | 2 | * Copyright (c) 20015 ARM Limited |
chuanga | 0:43ff9e3bc244 | 3 | * |
chuanga | 0:43ff9e3bc244 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
chuanga | 0:43ff9e3bc244 | 5 | * you may not use this file except in compliance with the License. |
chuanga | 0:43ff9e3bc244 | 6 | * You may obtain a copy of the License at |
chuanga | 0:43ff9e3bc244 | 7 | * |
chuanga | 0:43ff9e3bc244 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
chuanga | 0:43ff9e3bc244 | 9 | * |
chuanga | 0:43ff9e3bc244 | 10 | * Unless required by applicable law or agreed to in writing, software |
chuanga | 0:43ff9e3bc244 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
chuanga | 0:43ff9e3bc244 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
chuanga | 0:43ff9e3bc244 | 13 | * See the License for the specific language governing permissions and |
chuanga | 0:43ff9e3bc244 | 14 | * limitations under the License. |
chuanga | 0:43ff9e3bc244 | 15 | */ |
chuanga | 0:43ff9e3bc244 | 16 | |
chuanga | 0:43ff9e3bc244 | 17 | /** |
chuanga | 0:43ff9e3bc244 | 18 | ****************************************************************************** |
chuanga | 0:43ff9e3bc244 | 19 | * @file SpwfSAInterface.cpp |
chuanga | 0:43ff9e3bc244 | 20 | * @author STMicroelectronics |
chuanga | 0:43ff9e3bc244 | 21 | * @brief Implementation of the NetworkStack for the SPWF Device |
chuanga | 0:43ff9e3bc244 | 22 | ****************************************************************************** |
chuanga | 0:43ff9e3bc244 | 23 | * @copy |
chuanga | 0:43ff9e3bc244 | 24 | * |
chuanga | 0:43ff9e3bc244 | 25 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
chuanga | 0:43ff9e3bc244 | 26 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
chuanga | 0:43ff9e3bc244 | 27 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY |
chuanga | 0:43ff9e3bc244 | 28 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
chuanga | 0:43ff9e3bc244 | 29 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
chuanga | 0:43ff9e3bc244 | 30 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
chuanga | 0:43ff9e3bc244 | 31 | * |
chuanga | 0:43ff9e3bc244 | 32 | * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2> |
chuanga | 0:43ff9e3bc244 | 33 | ****************************************************************************** |
chuanga | 0:43ff9e3bc244 | 34 | */ |
chuanga | 0:43ff9e3bc244 | 35 | |
chuanga | 0:43ff9e3bc244 | 36 | #include "SpwfSAInterface.h" |
chuanga | 0:43ff9e3bc244 | 37 | #include "mbed_debug.h" |
chuanga | 0:43ff9e3bc244 | 38 | #include "BlockExecuter.h" |
chuanga | 0:43ff9e3bc244 | 39 | |
chuanga | 0:43ff9e3bc244 | 40 | #if MBED_CONF_RTOS_PRESENT |
chuanga | 0:43ff9e3bc244 | 41 | #define SYNC_HANDLER ScopedMutexLock sync_handler(_spwf_mutex) // assuming a recursive mutex |
chuanga | 0:43ff9e3bc244 | 42 | #else |
chuanga | 0:43ff9e3bc244 | 43 | #define SYNC_HANDLER |
chuanga | 0:43ff9e3bc244 | 44 | #endif |
chuanga | 0:43ff9e3bc244 | 45 | |
chuanga | 0:43ff9e3bc244 | 46 | |
chuanga | 0:43ff9e3bc244 | 47 | SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, |
chuanga | 0:43ff9e3bc244 | 48 | PinName rts, PinName cts, bool debug, |
chuanga | 0:43ff9e3bc244 | 49 | PinName wakeup, PinName reset) |
chuanga | 0:43ff9e3bc244 | 50 | : _spwf(tx, rx, rts, cts, *this, debug, wakeup, reset), |
chuanga | 0:43ff9e3bc244 | 51 | _dbg_on(debug) |
chuanga | 0:43ff9e3bc244 | 52 | { |
chuanga | 0:43ff9e3bc244 | 53 | inner_constructor(); |
chuanga | 0:43ff9e3bc244 | 54 | reset_credentials(); |
chuanga | 0:43ff9e3bc244 | 55 | } |
chuanga | 0:43ff9e3bc244 | 56 | |
chuanga | 0:43ff9e3bc244 | 57 | nsapi_error_t SpwfSAInterface::init(void) |
chuanga | 0:43ff9e3bc244 | 58 | { |
chuanga | 0:43ff9e3bc244 | 59 | _spwf.setTimeout(SPWF_INIT_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 60 | |
chuanga | 0:43ff9e3bc244 | 61 | if(_spwf.startup(0)) { |
chuanga | 0:43ff9e3bc244 | 62 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 63 | } |
chuanga | 0:43ff9e3bc244 | 64 | else return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 65 | } |
chuanga | 0:43ff9e3bc244 | 66 | |
chuanga | 0:43ff9e3bc244 | 67 | nsapi_error_t SpwfSAInterface::connect(void) |
chuanga | 0:43ff9e3bc244 | 68 | { |
chuanga | 0:43ff9e3bc244 | 69 | int mode; |
chuanga | 0:43ff9e3bc244 | 70 | char *pass_phrase = ap_pass; |
chuanga | 0:43ff9e3bc244 | 71 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 72 | |
chuanga | 0:43ff9e3bc244 | 73 | // check for valid SSID |
chuanga | 0:43ff9e3bc244 | 74 | if(ap_ssid[0] == '\0') { |
chuanga | 0:43ff9e3bc244 | 75 | return NSAPI_ERROR_PARAMETER; |
chuanga | 0:43ff9e3bc244 | 76 | } |
chuanga | 0:43ff9e3bc244 | 77 | |
chuanga | 0:43ff9e3bc244 | 78 | switch(ap_sec) |
chuanga | 0:43ff9e3bc244 | 79 | { |
chuanga | 0:43ff9e3bc244 | 80 | case NSAPI_SECURITY_NONE: |
chuanga | 0:43ff9e3bc244 | 81 | mode = 0; |
chuanga | 0:43ff9e3bc244 | 82 | pass_phrase = NULL; |
chuanga | 0:43ff9e3bc244 | 83 | break; |
chuanga | 0:43ff9e3bc244 | 84 | case NSAPI_SECURITY_WEP: |
chuanga | 0:43ff9e3bc244 | 85 | mode = 1; |
chuanga | 0:43ff9e3bc244 | 86 | break; |
chuanga | 0:43ff9e3bc244 | 87 | case NSAPI_SECURITY_WPA: |
chuanga | 0:43ff9e3bc244 | 88 | case NSAPI_SECURITY_WPA2: |
chuanga | 0:43ff9e3bc244 | 89 | mode = 2; |
chuanga | 0:43ff9e3bc244 | 90 | break; |
chuanga | 0:43ff9e3bc244 | 91 | default: |
chuanga | 0:43ff9e3bc244 | 92 | mode = 2; |
chuanga | 0:43ff9e3bc244 | 93 | break; |
chuanga | 0:43ff9e3bc244 | 94 | } |
chuanga | 0:43ff9e3bc244 | 95 | |
chuanga | 0:43ff9e3bc244 | 96 | // First: disconnect |
chuanga | 0:43ff9e3bc244 | 97 | if(_connected_to_network) { |
chuanga | 0:43ff9e3bc244 | 98 | if(!disconnect()) { |
chuanga | 0:43ff9e3bc244 | 99 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 100 | } |
chuanga | 0:43ff9e3bc244 | 101 | } |
chuanga | 0:43ff9e3bc244 | 102 | |
chuanga | 0:43ff9e3bc244 | 103 | //initialize the device before connecting |
chuanga | 0:43ff9e3bc244 | 104 | if(!_isInitialized) |
chuanga | 0:43ff9e3bc244 | 105 | { |
chuanga | 0:43ff9e3bc244 | 106 | if(init() != NSAPI_ERROR_OK) return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 107 | _isInitialized=true; |
chuanga | 0:43ff9e3bc244 | 108 | } |
chuanga | 0:43ff9e3bc244 | 109 | |
chuanga | 0:43ff9e3bc244 | 110 | // Then: (re-)connect |
chuanga | 0:43ff9e3bc244 | 111 | _spwf.setTimeout(SPWF_CONNECT_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 112 | |
chuanga | 0:43ff9e3bc244 | 113 | if (!_spwf.connect(ap_ssid, pass_phrase, mode)) { |
chuanga | 0:43ff9e3bc244 | 114 | return NSAPI_ERROR_AUTH_FAILURE; |
chuanga | 0:43ff9e3bc244 | 115 | } |
chuanga | 0:43ff9e3bc244 | 116 | |
chuanga | 0:43ff9e3bc244 | 117 | if (!_spwf.getIPAddress()) { |
chuanga | 0:43ff9e3bc244 | 118 | return NSAPI_ERROR_DHCP_FAILURE; |
chuanga | 0:43ff9e3bc244 | 119 | } |
chuanga | 0:43ff9e3bc244 | 120 | |
chuanga | 0:43ff9e3bc244 | 121 | _connected_to_network = true; |
chuanga | 0:43ff9e3bc244 | 122 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 123 | } |
chuanga | 0:43ff9e3bc244 | 124 | |
chuanga | 0:43ff9e3bc244 | 125 | nsapi_error_t SpwfSAInterface::connect(const char *ssid, const char *pass, nsapi_security_t security, |
chuanga | 0:43ff9e3bc244 | 126 | uint8_t channel) |
chuanga | 0:43ff9e3bc244 | 127 | { |
chuanga | 0:43ff9e3bc244 | 128 | nsapi_error_t ret; |
chuanga | 0:43ff9e3bc244 | 129 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 130 | |
chuanga | 0:43ff9e3bc244 | 131 | if (channel != 0) { |
chuanga | 0:43ff9e3bc244 | 132 | return NSAPI_ERROR_UNSUPPORTED; |
chuanga | 0:43ff9e3bc244 | 133 | } |
chuanga | 0:43ff9e3bc244 | 134 | |
chuanga | 0:43ff9e3bc244 | 135 | ret = set_credentials(ssid, pass, security); |
chuanga | 0:43ff9e3bc244 | 136 | if(ret != NSAPI_ERROR_OK) return ret; |
chuanga | 0:43ff9e3bc244 | 137 | |
chuanga | 0:43ff9e3bc244 | 138 | return connect(); |
chuanga | 0:43ff9e3bc244 | 139 | } |
chuanga | 0:43ff9e3bc244 | 140 | |
chuanga | 0:43ff9e3bc244 | 141 | nsapi_error_t SpwfSAInterface::disconnect(void) |
chuanga | 0:43ff9e3bc244 | 142 | { |
chuanga | 0:43ff9e3bc244 | 143 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 144 | |
chuanga | 0:43ff9e3bc244 | 145 | _spwf.setTimeout(SPWF_DISCONNECT_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 146 | CHECK_NOT_CONNECTED_ERR(); |
chuanga | 0:43ff9e3bc244 | 147 | |
chuanga | 0:43ff9e3bc244 | 148 | if (!_spwf.disconnect()) { |
chuanga | 0:43ff9e3bc244 | 149 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 150 | } |
chuanga | 0:43ff9e3bc244 | 151 | |
chuanga | 0:43ff9e3bc244 | 152 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 153 | } |
chuanga | 0:43ff9e3bc244 | 154 | |
chuanga | 0:43ff9e3bc244 | 155 | const char *SpwfSAInterface::get_ip_address(void) |
chuanga | 0:43ff9e3bc244 | 156 | { |
chuanga | 0:43ff9e3bc244 | 157 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 158 | |
chuanga | 0:43ff9e3bc244 | 159 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 160 | return _spwf.getIPAddress(); |
chuanga | 0:43ff9e3bc244 | 161 | } |
chuanga | 0:43ff9e3bc244 | 162 | |
chuanga | 0:43ff9e3bc244 | 163 | const char *SpwfSAInterface::get_mac_address(void) |
chuanga | 0:43ff9e3bc244 | 164 | { |
chuanga | 0:43ff9e3bc244 | 165 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 166 | |
chuanga | 0:43ff9e3bc244 | 167 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 168 | return _spwf.getMACAddress(); |
chuanga | 0:43ff9e3bc244 | 169 | } |
chuanga | 0:43ff9e3bc244 | 170 | |
chuanga | 0:43ff9e3bc244 | 171 | const char *SpwfSAInterface::get_gateway(void) |
chuanga | 0:43ff9e3bc244 | 172 | { |
chuanga | 0:43ff9e3bc244 | 173 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 174 | |
chuanga | 0:43ff9e3bc244 | 175 | if(!_connected_to_network) return NULL; |
chuanga | 0:43ff9e3bc244 | 176 | |
chuanga | 0:43ff9e3bc244 | 177 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 178 | return _spwf.getGateway(); |
chuanga | 0:43ff9e3bc244 | 179 | } |
chuanga | 0:43ff9e3bc244 | 180 | |
chuanga | 0:43ff9e3bc244 | 181 | const char *SpwfSAInterface::get_netmask(void) |
chuanga | 0:43ff9e3bc244 | 182 | { |
chuanga | 0:43ff9e3bc244 | 183 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 184 | |
chuanga | 0:43ff9e3bc244 | 185 | if(!_connected_to_network) return NULL; |
chuanga | 0:43ff9e3bc244 | 186 | |
chuanga | 0:43ff9e3bc244 | 187 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 188 | return _spwf.getNetmask(); |
chuanga | 0:43ff9e3bc244 | 189 | } |
chuanga | 0:43ff9e3bc244 | 190 | |
chuanga | 0:43ff9e3bc244 | 191 | nsapi_error_t SpwfSAInterface::socket_open(void **handle, nsapi_protocol_t proto) |
chuanga | 0:43ff9e3bc244 | 192 | { |
chuanga | 0:43ff9e3bc244 | 193 | int internal_id; |
chuanga | 0:43ff9e3bc244 | 194 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 195 | |
chuanga | 0:43ff9e3bc244 | 196 | for (internal_id = 0; internal_id < SPWFSA_SOCKET_COUNT; internal_id++) { |
chuanga | 0:43ff9e3bc244 | 197 | if(_ids[internal_id].internal_id == SPWFSA_SOCKET_COUNT) break; |
chuanga | 0:43ff9e3bc244 | 198 | } |
chuanga | 0:43ff9e3bc244 | 199 | |
chuanga | 0:43ff9e3bc244 | 200 | if(internal_id == SPWFSA_SOCKET_COUNT) { |
chuanga | 0:43ff9e3bc244 | 201 | debug_if(_dbg_on, "NO Socket ID Error\r\n"); |
chuanga | 0:43ff9e3bc244 | 202 | return NSAPI_ERROR_NO_SOCKET; |
chuanga | 0:43ff9e3bc244 | 203 | } |
chuanga | 0:43ff9e3bc244 | 204 | |
chuanga | 0:43ff9e3bc244 | 205 | spwf_socket_t *socket = &_ids[internal_id]; |
chuanga | 0:43ff9e3bc244 | 206 | socket->internal_id = internal_id; |
chuanga | 0:43ff9e3bc244 | 207 | socket->spwf_id = SPWFSA_SOCKET_COUNT; |
chuanga | 0:43ff9e3bc244 | 208 | socket->server_gone = false; |
chuanga | 0:43ff9e3bc244 | 209 | socket->no_more_data = false; |
chuanga | 0:43ff9e3bc244 | 210 | socket->proto = proto; |
chuanga | 0:43ff9e3bc244 | 211 | socket->addr = SocketAddress(); |
chuanga | 0:43ff9e3bc244 | 212 | |
chuanga | 0:43ff9e3bc244 | 213 | *handle = socket; |
chuanga | 0:43ff9e3bc244 | 214 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 215 | } |
chuanga | 0:43ff9e3bc244 | 216 | |
chuanga | 0:43ff9e3bc244 | 217 | nsapi_error_t SpwfSAInterface::socket_connect(void *handle, const SocketAddress &addr) |
chuanga | 0:43ff9e3bc244 | 218 | { |
chuanga | 0:43ff9e3bc244 | 219 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
chuanga | 0:43ff9e3bc244 | 220 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 221 | |
chuanga | 0:43ff9e3bc244 | 222 | MBED_ASSERT(((unsigned int)socket->internal_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)); |
chuanga | 0:43ff9e3bc244 | 223 | |
chuanga | 0:43ff9e3bc244 | 224 | if(_socket_has_connected(socket->internal_id)) { |
chuanga | 0:43ff9e3bc244 | 225 | return NSAPI_ERROR_IS_CONNECTED; |
chuanga | 0:43ff9e3bc244 | 226 | } |
chuanga | 0:43ff9e3bc244 | 227 | |
chuanga | 0:43ff9e3bc244 | 228 | _spwf.setTimeout(SPWF_OPEN_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 229 | |
chuanga | 0:43ff9e3bc244 | 230 | const char *proto = (socket->proto == NSAPI_UDP) ? "u" : "t"; //"s" for secure socket? |
chuanga | 0:43ff9e3bc244 | 231 | |
chuanga | 0:43ff9e3bc244 | 232 | if(addr.get_ip_version() != NSAPI_IPv4) { // IPv6 not supported (yet) |
chuanga | 0:43ff9e3bc244 | 233 | return NSAPI_ERROR_UNSUPPORTED; |
chuanga | 0:43ff9e3bc244 | 234 | } |
chuanga | 0:43ff9e3bc244 | 235 | |
chuanga | 0:43ff9e3bc244 | 236 | { |
chuanga | 0:43ff9e3bc244 | 237 | BlockExecuter netsock_wa_obj(Callback<void()>(&_spwf, &SPWFSAxx::_unblock_event_callback), |
chuanga | 0:43ff9e3bc244 | 238 | Callback<void()>(&_spwf, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */ |
chuanga | 0:43ff9e3bc244 | 239 | |
chuanga | 0:43ff9e3bc244 | 240 | /* block asynchronous indications */ |
chuanga | 0:43ff9e3bc244 | 241 | if(!_spwf._winds_off()) { |
chuanga | 0:43ff9e3bc244 | 242 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 243 | } |
chuanga | 0:43ff9e3bc244 | 244 | |
chuanga | 0:43ff9e3bc244 | 245 | { |
chuanga | 0:43ff9e3bc244 | 246 | BlockExecuter bh_handler(Callback<void()>(&_spwf, &SPWFSAxx::_execute_bottom_halves)); |
chuanga | 0:43ff9e3bc244 | 247 | { |
chuanga | 0:43ff9e3bc244 | 248 | BlockExecuter winds_enabler(Callback<void()>(&_spwf, &SPWFSAxx::_winds_on)); |
chuanga | 0:43ff9e3bc244 | 249 | |
chuanga | 0:43ff9e3bc244 | 250 | if(!_spwf.open(proto, &socket->spwf_id, addr.get_ip_address(), addr.get_port())) { |
chuanga | 0:43ff9e3bc244 | 251 | MBED_ASSERT(_spwf._call_event_callback_blocked == 1); |
chuanga | 0:43ff9e3bc244 | 252 | |
chuanga | 0:43ff9e3bc244 | 253 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 254 | } |
chuanga | 0:43ff9e3bc244 | 255 | |
chuanga | 0:43ff9e3bc244 | 256 | /* check for the module to report a valid id */ |
chuanga | 0:43ff9e3bc244 | 257 | MBED_ASSERT(((unsigned int)socket->spwf_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)); |
chuanga | 0:43ff9e3bc244 | 258 | |
chuanga | 0:43ff9e3bc244 | 259 | _internal_ids[socket->spwf_id] = socket->internal_id; |
chuanga | 0:43ff9e3bc244 | 260 | socket->addr = addr; |
chuanga | 0:43ff9e3bc244 | 261 | |
chuanga | 0:43ff9e3bc244 | 262 | MBED_ASSERT(_spwf._call_event_callback_blocked == 1); |
chuanga | 0:43ff9e3bc244 | 263 | |
chuanga | 0:43ff9e3bc244 | 264 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 265 | } |
chuanga | 0:43ff9e3bc244 | 266 | } |
chuanga | 0:43ff9e3bc244 | 267 | } |
chuanga | 0:43ff9e3bc244 | 268 | } |
chuanga | 0:43ff9e3bc244 | 269 | |
chuanga | 0:43ff9e3bc244 | 270 | nsapi_error_t SpwfSAInterface::socket_bind(void *handle, const SocketAddress &address) |
chuanga | 0:43ff9e3bc244 | 271 | { |
chuanga | 0:43ff9e3bc244 | 272 | return NSAPI_ERROR_UNSUPPORTED; |
chuanga | 0:43ff9e3bc244 | 273 | } |
chuanga | 0:43ff9e3bc244 | 274 | |
chuanga | 0:43ff9e3bc244 | 275 | nsapi_error_t SpwfSAInterface::socket_listen(void *handle, int backlog) |
chuanga | 0:43ff9e3bc244 | 276 | { |
chuanga | 0:43ff9e3bc244 | 277 | return NSAPI_ERROR_UNSUPPORTED; |
chuanga | 0:43ff9e3bc244 | 278 | } |
chuanga | 0:43ff9e3bc244 | 279 | |
chuanga | 0:43ff9e3bc244 | 280 | nsapi_error_t SpwfSAInterface::socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address) |
chuanga | 0:43ff9e3bc244 | 281 | { |
chuanga | 0:43ff9e3bc244 | 282 | return NSAPI_ERROR_UNSUPPORTED; |
chuanga | 0:43ff9e3bc244 | 283 | } |
chuanga | 0:43ff9e3bc244 | 284 | |
chuanga | 0:43ff9e3bc244 | 285 | nsapi_error_t SpwfSAInterface::socket_close(void *handle) |
chuanga | 0:43ff9e3bc244 | 286 | { |
chuanga | 0:43ff9e3bc244 | 287 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
chuanga | 0:43ff9e3bc244 | 288 | int internal_id = socket->internal_id; |
chuanga | 0:43ff9e3bc244 | 289 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 290 | |
chuanga | 0:43ff9e3bc244 | 291 | if(!_socket_is_open(internal_id)) return NSAPI_ERROR_NO_SOCKET; |
chuanga | 0:43ff9e3bc244 | 292 | |
chuanga | 0:43ff9e3bc244 | 293 | if(_socket_has_connected(socket)) { |
chuanga | 0:43ff9e3bc244 | 294 | _spwf.setTimeout(SPWF_CLOSE_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 295 | if (!_spwf.close(socket->spwf_id)) { |
chuanga | 0:43ff9e3bc244 | 296 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 297 | } |
chuanga | 0:43ff9e3bc244 | 298 | _internal_ids[socket->spwf_id] = SPWFSA_SOCKET_COUNT; |
chuanga | 0:43ff9e3bc244 | 299 | } |
chuanga | 0:43ff9e3bc244 | 300 | |
chuanga | 0:43ff9e3bc244 | 301 | _ids[internal_id].internal_id = SPWFSA_SOCKET_COUNT; |
chuanga | 0:43ff9e3bc244 | 302 | _ids[internal_id].spwf_id = SPWFSA_SOCKET_COUNT; |
chuanga | 0:43ff9e3bc244 | 303 | |
chuanga | 0:43ff9e3bc244 | 304 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 305 | } |
chuanga | 0:43ff9e3bc244 | 306 | |
chuanga | 0:43ff9e3bc244 | 307 | nsapi_size_or_error_t SpwfSAInterface::socket_send(void *handle, const void *data, unsigned size) |
chuanga | 0:43ff9e3bc244 | 308 | { |
chuanga | 0:43ff9e3bc244 | 309 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
chuanga | 0:43ff9e3bc244 | 310 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 311 | |
chuanga | 0:43ff9e3bc244 | 312 | CHECK_NOT_CONNECTED_ERR(); |
chuanga | 0:43ff9e3bc244 | 313 | |
chuanga | 0:43ff9e3bc244 | 314 | _spwf.setTimeout(SPWF_SEND_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 315 | return _spwf.send(socket->spwf_id, data, size, socket->internal_id); |
chuanga | 0:43ff9e3bc244 | 316 | } |
chuanga | 0:43ff9e3bc244 | 317 | |
chuanga | 0:43ff9e3bc244 | 318 | nsapi_size_or_error_t SpwfSAInterface::socket_recv(void *handle, void *data, unsigned size) |
chuanga | 0:43ff9e3bc244 | 319 | { |
chuanga | 0:43ff9e3bc244 | 320 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 321 | |
chuanga | 0:43ff9e3bc244 | 322 | return _socket_recv(handle, data, size, false); |
chuanga | 0:43ff9e3bc244 | 323 | } |
chuanga | 0:43ff9e3bc244 | 324 | |
chuanga | 0:43ff9e3bc244 | 325 | nsapi_size_or_error_t SpwfSAInterface::_socket_recv(void *handle, void *data, unsigned size, bool datagram) |
chuanga | 0:43ff9e3bc244 | 326 | { |
chuanga | 0:43ff9e3bc244 | 327 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
chuanga | 0:43ff9e3bc244 | 328 | |
chuanga | 0:43ff9e3bc244 | 329 | CHECK_NOT_CONNECTED_ERR(); |
chuanga | 0:43ff9e3bc244 | 330 | |
chuanga | 0:43ff9e3bc244 | 331 | if(!_socket_has_connected(socket)) { |
chuanga | 0:43ff9e3bc244 | 332 | return NSAPI_ERROR_WOULD_BLOCK; |
chuanga | 0:43ff9e3bc244 | 333 | } else if(socket->no_more_data) { |
chuanga | 0:43ff9e3bc244 | 334 | return 0; |
chuanga | 0:43ff9e3bc244 | 335 | } |
chuanga | 0:43ff9e3bc244 | 336 | |
chuanga | 0:43ff9e3bc244 | 337 | _spwf.setTimeout(SPWF_RECV_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 338 | |
chuanga | 0:43ff9e3bc244 | 339 | int32_t recv = _spwf.recv(socket->spwf_id, (char*)data, (uint32_t)size, datagram); |
chuanga | 0:43ff9e3bc244 | 340 | |
chuanga | 0:43ff9e3bc244 | 341 | MBED_ASSERT(!_spwf._is_event_callback_blocked()); |
chuanga | 0:43ff9e3bc244 | 342 | MBED_ASSERT((recv != 0) || (size == 0)); |
chuanga | 0:43ff9e3bc244 | 343 | |
chuanga | 0:43ff9e3bc244 | 344 | if (recv < 0) { |
chuanga | 0:43ff9e3bc244 | 345 | if(!_socket_is_still_connected(socket)) { |
chuanga | 0:43ff9e3bc244 | 346 | socket->no_more_data = true; |
chuanga | 0:43ff9e3bc244 | 347 | return 0; |
chuanga | 0:43ff9e3bc244 | 348 | } |
chuanga | 0:43ff9e3bc244 | 349 | |
chuanga | 0:43ff9e3bc244 | 350 | return NSAPI_ERROR_WOULD_BLOCK; |
chuanga | 0:43ff9e3bc244 | 351 | } |
chuanga | 0:43ff9e3bc244 | 352 | |
chuanga | 0:43ff9e3bc244 | 353 | return recv; |
chuanga | 0:43ff9e3bc244 | 354 | } |
chuanga | 0:43ff9e3bc244 | 355 | |
chuanga | 0:43ff9e3bc244 | 356 | nsapi_size_or_error_t SpwfSAInterface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
chuanga | 0:43ff9e3bc244 | 357 | { |
chuanga | 0:43ff9e3bc244 | 358 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
chuanga | 0:43ff9e3bc244 | 359 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 360 | |
chuanga | 0:43ff9e3bc244 | 361 | CHECK_NOT_CONNECTED_ERR(); |
chuanga | 0:43ff9e3bc244 | 362 | |
chuanga | 0:43ff9e3bc244 | 363 | if ((_socket_has_connected(socket)) && (socket->addr != addr)) { |
chuanga | 0:43ff9e3bc244 | 364 | _spwf.setTimeout(SPWF_CLOSE_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 365 | if (!_spwf.close(socket->spwf_id)) { |
chuanga | 0:43ff9e3bc244 | 366 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 367 | } |
chuanga | 0:43ff9e3bc244 | 368 | _internal_ids[socket->spwf_id] = SPWFSA_SOCKET_COUNT; |
chuanga | 0:43ff9e3bc244 | 369 | socket->spwf_id = SPWFSA_SOCKET_COUNT; |
chuanga | 0:43ff9e3bc244 | 370 | } |
chuanga | 0:43ff9e3bc244 | 371 | |
chuanga | 0:43ff9e3bc244 | 372 | _spwf.setTimeout(SPWF_CONN_SND_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 373 | if (!_socket_has_connected(socket)) { |
chuanga | 0:43ff9e3bc244 | 374 | nsapi_error_t err = socket_connect(socket, addr); |
chuanga | 0:43ff9e3bc244 | 375 | if (err < 0) { |
chuanga | 0:43ff9e3bc244 | 376 | return err; |
chuanga | 0:43ff9e3bc244 | 377 | } |
chuanga | 0:43ff9e3bc244 | 378 | } |
chuanga | 0:43ff9e3bc244 | 379 | |
chuanga | 0:43ff9e3bc244 | 380 | return socket_send(socket, data, size); |
chuanga | 0:43ff9e3bc244 | 381 | } |
chuanga | 0:43ff9e3bc244 | 382 | |
chuanga | 0:43ff9e3bc244 | 383 | nsapi_size_or_error_t SpwfSAInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
chuanga | 0:43ff9e3bc244 | 384 | { |
chuanga | 0:43ff9e3bc244 | 385 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
chuanga | 0:43ff9e3bc244 | 386 | nsapi_error_t ret; |
chuanga | 0:43ff9e3bc244 | 387 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 388 | |
chuanga | 0:43ff9e3bc244 | 389 | ret = _socket_recv(socket, data, size, true); |
chuanga | 0:43ff9e3bc244 | 390 | if (ret >= 0 && addr) { |
chuanga | 0:43ff9e3bc244 | 391 | *addr = socket->addr; |
chuanga | 0:43ff9e3bc244 | 392 | } |
chuanga | 0:43ff9e3bc244 | 393 | |
chuanga | 0:43ff9e3bc244 | 394 | return ret; |
chuanga | 0:43ff9e3bc244 | 395 | } |
chuanga | 0:43ff9e3bc244 | 396 | |
chuanga | 0:43ff9e3bc244 | 397 | void SpwfSAInterface::socket_attach(void *handle, void (*callback)(void *), void *data) |
chuanga | 0:43ff9e3bc244 | 398 | { |
chuanga | 0:43ff9e3bc244 | 399 | spwf_socket_t *socket = (spwf_socket_t*)handle; |
chuanga | 0:43ff9e3bc244 | 400 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 401 | |
chuanga | 0:43ff9e3bc244 | 402 | if(!_socket_is_open(socket)) return; // might happen e.g. after module hard fault or voluntary disconnection |
chuanga | 0:43ff9e3bc244 | 403 | |
chuanga | 0:43ff9e3bc244 | 404 | _cbs[socket->internal_id].callback = callback; |
chuanga | 0:43ff9e3bc244 | 405 | _cbs[socket->internal_id].data = data; |
chuanga | 0:43ff9e3bc244 | 406 | } |
chuanga | 0:43ff9e3bc244 | 407 | |
chuanga | 0:43ff9e3bc244 | 408 | void SpwfSAInterface::event(void) { |
chuanga | 0:43ff9e3bc244 | 409 | for (int internal_id = 0; internal_id < SPWFSA_SOCKET_COUNT; internal_id++) { |
chuanga | 0:43ff9e3bc244 | 410 | if (_cbs[internal_id].callback && (_ids[internal_id].internal_id != SPWFSA_SOCKET_COUNT)) { |
chuanga | 0:43ff9e3bc244 | 411 | _cbs[internal_id].callback(_cbs[internal_id].data); |
chuanga | 0:43ff9e3bc244 | 412 | } |
chuanga | 0:43ff9e3bc244 | 413 | } |
chuanga | 0:43ff9e3bc244 | 414 | } |
chuanga | 0:43ff9e3bc244 | 415 | |
chuanga | 0:43ff9e3bc244 | 416 | nsapi_error_t SpwfSAInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
chuanga | 0:43ff9e3bc244 | 417 | { |
chuanga | 0:43ff9e3bc244 | 418 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 419 | |
chuanga | 0:43ff9e3bc244 | 420 | if((ssid == NULL) || (strlen(ssid) == 0)) { |
chuanga | 0:43ff9e3bc244 | 421 | return NSAPI_ERROR_PARAMETER; |
chuanga | 0:43ff9e3bc244 | 422 | } |
chuanga | 0:43ff9e3bc244 | 423 | |
chuanga | 0:43ff9e3bc244 | 424 | if((pass != NULL) && (strlen(pass) > 0)) { |
chuanga | 0:43ff9e3bc244 | 425 | if(strlen(pass) < sizeof(ap_pass)) { |
chuanga | 0:43ff9e3bc244 | 426 | if(security == NSAPI_SECURITY_NONE) { |
chuanga | 0:43ff9e3bc244 | 427 | return NSAPI_ERROR_PARAMETER; |
chuanga | 0:43ff9e3bc244 | 428 | } |
chuanga | 0:43ff9e3bc244 | 429 | } else { |
chuanga | 0:43ff9e3bc244 | 430 | return NSAPI_ERROR_PARAMETER; |
chuanga | 0:43ff9e3bc244 | 431 | } |
chuanga | 0:43ff9e3bc244 | 432 | } else if(security != NSAPI_SECURITY_NONE) { |
chuanga | 0:43ff9e3bc244 | 433 | return NSAPI_ERROR_PARAMETER; |
chuanga | 0:43ff9e3bc244 | 434 | } |
chuanga | 0:43ff9e3bc244 | 435 | |
chuanga | 0:43ff9e3bc244 | 436 | reset_credentials(); |
chuanga | 0:43ff9e3bc244 | 437 | |
chuanga | 0:43ff9e3bc244 | 438 | ap_sec = security; |
chuanga | 0:43ff9e3bc244 | 439 | strncpy(ap_ssid, ssid, sizeof(ap_ssid) - 1); |
chuanga | 0:43ff9e3bc244 | 440 | strncpy(ap_pass, pass, sizeof(ap_pass) - 1); |
chuanga | 0:43ff9e3bc244 | 441 | |
chuanga | 0:43ff9e3bc244 | 442 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 443 | } |
chuanga | 0:43ff9e3bc244 | 444 | |
chuanga | 0:43ff9e3bc244 | 445 | nsapi_error_t SpwfSAInterface::set_channel(uint8_t channel) |
chuanga | 0:43ff9e3bc244 | 446 | { |
chuanga | 0:43ff9e3bc244 | 447 | return NSAPI_ERROR_UNSUPPORTED; |
chuanga | 0:43ff9e3bc244 | 448 | } |
chuanga | 0:43ff9e3bc244 | 449 | |
chuanga | 0:43ff9e3bc244 | 450 | int8_t SpwfSAInterface::get_rssi(void) |
chuanga | 0:43ff9e3bc244 | 451 | { |
chuanga | 0:43ff9e3bc244 | 452 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 453 | |
chuanga | 0:43ff9e3bc244 | 454 | if(!_connected_to_network) return 0; |
chuanga | 0:43ff9e3bc244 | 455 | |
chuanga | 0:43ff9e3bc244 | 456 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 457 | return _spwf.getRssi(); |
chuanga | 0:43ff9e3bc244 | 458 | } |
chuanga | 0:43ff9e3bc244 | 459 | |
chuanga | 0:43ff9e3bc244 | 460 | nsapi_size_or_error_t SpwfSAInterface::scan(WiFiAccessPoint *res, unsigned count) |
chuanga | 0:43ff9e3bc244 | 461 | { |
chuanga | 0:43ff9e3bc244 | 462 | SYNC_HANDLER; |
chuanga | 0:43ff9e3bc244 | 463 | |
chuanga | 0:43ff9e3bc244 | 464 | nsapi_size_or_error_t ret; |
chuanga | 0:43ff9e3bc244 | 465 | |
chuanga | 0:43ff9e3bc244 | 466 | //initialize the device before scanning |
chuanga | 0:43ff9e3bc244 | 467 | if(!_isInitialized) |
chuanga | 0:43ff9e3bc244 | 468 | { |
chuanga | 0:43ff9e3bc244 | 469 | if(init() != NSAPI_ERROR_OK) return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 470 | } |
chuanga | 0:43ff9e3bc244 | 471 | |
chuanga | 0:43ff9e3bc244 | 472 | _spwf.setTimeout(SPWF_SCAN_TIMEOUT); |
chuanga | 0:43ff9e3bc244 | 473 | |
chuanga | 0:43ff9e3bc244 | 474 | { |
chuanga | 0:43ff9e3bc244 | 475 | BlockExecuter netsock_wa_obj(Callback<void()>(&_spwf, &SPWFSAxx::_unblock_event_callback), |
chuanga | 0:43ff9e3bc244 | 476 | Callback<void()>(&_spwf, &SPWFSAxx::_block_event_callback)); /* disable calling (external) callback in IRQ context */ |
chuanga | 0:43ff9e3bc244 | 477 | |
chuanga | 0:43ff9e3bc244 | 478 | /* block asynchronous indications */ |
chuanga | 0:43ff9e3bc244 | 479 | if(!_spwf._winds_off()) { |
chuanga | 0:43ff9e3bc244 | 480 | MBED_ASSERT(_spwf._call_event_callback_blocked == 1); |
chuanga | 0:43ff9e3bc244 | 481 | |
chuanga | 0:43ff9e3bc244 | 482 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 483 | } |
chuanga | 0:43ff9e3bc244 | 484 | |
chuanga | 0:43ff9e3bc244 | 485 | ret = _spwf.scan(res, count); |
chuanga | 0:43ff9e3bc244 | 486 | |
chuanga | 0:43ff9e3bc244 | 487 | /* unblock asynchronous indications */ |
chuanga | 0:43ff9e3bc244 | 488 | _spwf._winds_on(); |
chuanga | 0:43ff9e3bc244 | 489 | } |
chuanga | 0:43ff9e3bc244 | 490 | |
chuanga | 0:43ff9e3bc244 | 491 | MBED_ASSERT(!_spwf._is_event_callback_blocked()); |
chuanga | 0:43ff9e3bc244 | 492 | |
chuanga | 0:43ff9e3bc244 | 493 | //de-initialize the device after scanning |
chuanga | 0:43ff9e3bc244 | 494 | if(!_isInitialized) |
chuanga | 0:43ff9e3bc244 | 495 | { |
chuanga | 0:43ff9e3bc244 | 496 | nsapi_error_t err = disconnect(); |
chuanga | 0:43ff9e3bc244 | 497 | if((err != NSAPI_ERROR_OK) && (err != NSAPI_ERROR_NO_CONNECTION)) return err; |
chuanga | 0:43ff9e3bc244 | 498 | } |
chuanga | 0:43ff9e3bc244 | 499 | |
chuanga | 0:43ff9e3bc244 | 500 | return ret; |
chuanga | 0:43ff9e3bc244 | 501 | } |
chuanga | 0:43ff9e3bc244 | 502 | |
chuanga | 0:43ff9e3bc244 | 503 | #if MBED_CONF_IDW0XX1_PROVIDE_DEFAULT |
chuanga | 0:43ff9e3bc244 | 504 | |
chuanga | 0:43ff9e3bc244 | 505 | WiFiInterface *WiFiInterface::get_default_instance() { |
chuanga | 0:43ff9e3bc244 | 506 | static SpwfSAInterface spwf(MBED_CONF_IDW0XX1_TX, MBED_CONF_IDW0XX1_RX); |
chuanga | 0:43ff9e3bc244 | 507 | return &spwf; |
chuanga | 0:43ff9e3bc244 | 508 | } |
chuanga | 0:43ff9e3bc244 | 509 | |
chuanga | 0:43ff9e3bc244 | 510 | #endif |