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_ISM43362/ISM43362Interface.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 | /* ISM43362 implementation of NetworkInterfaceAPI |
| chuanga | 0:43ff9e3bc244 | 2 | * Copyright (c) STMicroelectronics 2018 |
| 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 | #include <string.h> |
| chuanga | 0:43ff9e3bc244 | 18 | #include "ISM43362Interface.h" |
| chuanga | 0:43ff9e3bc244 | 19 | #include "mbed_debug.h" |
| chuanga | 0:43ff9e3bc244 | 20 | |
| chuanga | 0:43ff9e3bc244 | 21 | // Product ID,FW Revision,API Revision,Stack Revision,RTOS Revision,CPU Clock,Product Name |
| chuanga | 0:43ff9e3bc244 | 22 | #define LATEST_FW_VERSION_NUMBER "C3.5.2.5" // ISM43362-M3G-L44-SPI,C3.5.2.5.STM,v3.5.2,v1.4.0.rc1,v8.2.1,120000000,Inventek eS-WiFi |
| chuanga | 0:43ff9e3bc244 | 23 | |
| chuanga | 0:43ff9e3bc244 | 24 | // activate / de-activate debug |
| chuanga | 0:43ff9e3bc244 | 25 | #define ism_interface_debug 0 |
| chuanga | 0:43ff9e3bc244 | 26 | |
| chuanga | 0:43ff9e3bc244 | 27 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
| chuanga | 0:43ff9e3bc244 | 28 | |
| chuanga | 0:43ff9e3bc244 | 29 | // ISM43362Interface implementation |
| chuanga | 0:43ff9e3bc244 | 30 | ISM43362Interface::ISM43362Interface(bool debug) |
| chuanga | 0:43ff9e3bc244 | 31 | : _ism(MBED_CONF_ISM43362_WIFI_MOSI, MBED_CONF_ISM43362_WIFI_MISO, MBED_CONF_ISM43362_WIFI_SCLK, MBED_CONF_ISM43362_WIFI_NSS, MBED_CONF_ISM43362_WIFI_RESET, MBED_CONF_ISM43362_WIFI_DATAREADY, MBED_CONF_ISM43362_WIFI_WAKEUP, debug), |
| chuanga | 0:43ff9e3bc244 | 32 | _conn_stat(NSAPI_STATUS_DISCONNECTED), |
| chuanga | 0:43ff9e3bc244 | 33 | _conn_stat_cb(NULL) |
| chuanga | 0:43ff9e3bc244 | 34 | { |
| chuanga | 0:43ff9e3bc244 | 35 | _ism_debug = ism_interface_debug || debug; |
| chuanga | 0:43ff9e3bc244 | 36 | memset(_ids, 0, sizeof(_ids)); |
| chuanga | 0:43ff9e3bc244 | 37 | memset(_socket_obj, 0, sizeof(_socket_obj)); |
| chuanga | 0:43ff9e3bc244 | 38 | _ism.attach(this, &ISM43362Interface::update_conn_state_cb); |
| chuanga | 0:43ff9e3bc244 | 39 | memset(_cbs, 0, sizeof(_cbs)); |
| chuanga | 0:43ff9e3bc244 | 40 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
| chuanga | 0:43ff9e3bc244 | 41 | memset(ap_pass, 0, sizeof(ap_pass)); |
| chuanga | 0:43ff9e3bc244 | 42 | ap_sec = ISM_SECURITY_UNKNOWN; |
| chuanga | 0:43ff9e3bc244 | 43 | |
| chuanga | 0:43ff9e3bc244 | 44 | thread_read_socket.start(callback(this, &ISM43362Interface::socket_check_read)); |
| chuanga | 0:43ff9e3bc244 | 45 | |
| chuanga | 0:43ff9e3bc244 | 46 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 47 | |
| chuanga | 0:43ff9e3bc244 | 48 | // Check all supported firmware versions |
| chuanga | 0:43ff9e3bc244 | 49 | _FwVersion = _ism.get_firmware_version(); |
| chuanga | 0:43ff9e3bc244 | 50 | |
| chuanga | 0:43ff9e3bc244 | 51 | if (!_FwVersion) { |
| chuanga | 0:43ff9e3bc244 | 52 | error("ISM43362Interface: ERROR cannot read firmware version\r\n"); |
| chuanga | 0:43ff9e3bc244 | 53 | } |
| chuanga | 0:43ff9e3bc244 | 54 | |
| chuanga | 0:43ff9e3bc244 | 55 | debug_if(_ism_debug, "ISM43362Interface: read_version = %lu\r\n", _FwVersion); |
| chuanga | 0:43ff9e3bc244 | 56 | /* FW Revision should be with format "CX.X.X.X" with X as a single digit */ |
| chuanga | 0:43ff9e3bc244 | 57 | if ( (_FwVersion < 1000) || (_FwVersion > 9999) ) { |
| chuanga | 0:43ff9e3bc244 | 58 | debug_if(_ism_debug, "ISM43362Interface: read_version issue\r\n"); |
| chuanga | 0:43ff9e3bc244 | 59 | } |
| chuanga | 0:43ff9e3bc244 | 60 | |
| chuanga | 0:43ff9e3bc244 | 61 | #if TARGET_DISCO_L475VG_IOT01A |
| chuanga | 0:43ff9e3bc244 | 62 | if (_FwVersion < ParseNumber(LATEST_FW_VERSION_NUMBER, NULL)) { |
| chuanga | 0:43ff9e3bc244 | 63 | debug_if(_ism_debug, "ISM43362Interface: please update FW\r\n"); |
| chuanga | 0:43ff9e3bc244 | 64 | } |
| chuanga | 0:43ff9e3bc244 | 65 | #endif |
| chuanga | 0:43ff9e3bc244 | 66 | |
| chuanga | 0:43ff9e3bc244 | 67 | _connect_status = NSAPI_ERROR_NO_CONNECTION; |
| chuanga | 0:43ff9e3bc244 | 68 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 69 | } |
| chuanga | 0:43ff9e3bc244 | 70 | |
| chuanga | 0:43ff9e3bc244 | 71 | nsapi_error_t ISM43362Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, |
| chuanga | 0:43ff9e3bc244 | 72 | uint8_t channel) |
| chuanga | 0:43ff9e3bc244 | 73 | { |
| chuanga | 0:43ff9e3bc244 | 74 | if (channel != 0) { |
| chuanga | 0:43ff9e3bc244 | 75 | return NSAPI_ERROR_UNSUPPORTED; |
| chuanga | 0:43ff9e3bc244 | 76 | } |
| chuanga | 0:43ff9e3bc244 | 77 | |
| chuanga | 0:43ff9e3bc244 | 78 | nsapi_error_t credentials_status = set_credentials(ssid, pass, security); |
| chuanga | 0:43ff9e3bc244 | 79 | if (credentials_status) { |
| chuanga | 0:43ff9e3bc244 | 80 | return credentials_status; |
| chuanga | 0:43ff9e3bc244 | 81 | } |
| chuanga | 0:43ff9e3bc244 | 82 | |
| chuanga | 0:43ff9e3bc244 | 83 | return connect(); |
| chuanga | 0:43ff9e3bc244 | 84 | } |
| chuanga | 0:43ff9e3bc244 | 85 | |
| chuanga | 0:43ff9e3bc244 | 86 | nsapi_error_t ISM43362Interface::connect() |
| chuanga | 0:43ff9e3bc244 | 87 | { |
| chuanga | 0:43ff9e3bc244 | 88 | if (strlen(ap_ssid) == 0) { |
| chuanga | 0:43ff9e3bc244 | 89 | return NSAPI_ERROR_NO_SSID; |
| chuanga | 0:43ff9e3bc244 | 90 | } |
| chuanga | 0:43ff9e3bc244 | 91 | |
| chuanga | 0:43ff9e3bc244 | 92 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 93 | |
| chuanga | 0:43ff9e3bc244 | 94 | if (!_ism.dhcp(true)) { |
| chuanga | 0:43ff9e3bc244 | 95 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 96 | return NSAPI_ERROR_DHCP_FAILURE; |
| chuanga | 0:43ff9e3bc244 | 97 | } |
| chuanga | 0:43ff9e3bc244 | 98 | |
| chuanga | 0:43ff9e3bc244 | 99 | _connect_status = _ism.connect(ap_ssid, ap_pass, ap_sec); |
| chuanga | 0:43ff9e3bc244 | 100 | debug_if(_ism_debug, "ISM43362Interface: connect_status %d\n", _connect_status); |
| chuanga | 0:43ff9e3bc244 | 101 | |
| chuanga | 0:43ff9e3bc244 | 102 | if (_connect_status != NSAPI_ERROR_OK) { |
| chuanga | 0:43ff9e3bc244 | 103 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 104 | return _connect_status; |
| chuanga | 0:43ff9e3bc244 | 105 | } |
| chuanga | 0:43ff9e3bc244 | 106 | |
| chuanga | 0:43ff9e3bc244 | 107 | if (!_ism.getIPAddress()) { |
| chuanga | 0:43ff9e3bc244 | 108 | _connect_status = NSAPI_ERROR_DHCP_FAILURE; |
| chuanga | 0:43ff9e3bc244 | 109 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 110 | return NSAPI_ERROR_DHCP_FAILURE; |
| chuanga | 0:43ff9e3bc244 | 111 | } |
| chuanga | 0:43ff9e3bc244 | 112 | |
| chuanga | 0:43ff9e3bc244 | 113 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 114 | |
| chuanga | 0:43ff9e3bc244 | 115 | return NSAPI_ERROR_OK; |
| chuanga | 0:43ff9e3bc244 | 116 | } |
| chuanga | 0:43ff9e3bc244 | 117 | |
| chuanga | 0:43ff9e3bc244 | 118 | nsapi_error_t ISM43362Interface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version) |
| chuanga | 0:43ff9e3bc244 | 119 | { |
| chuanga | 0:43ff9e3bc244 | 120 | if (strlen(name) == 0) { |
| chuanga | 0:43ff9e3bc244 | 121 | return NSAPI_ERROR_NO_SOCKET; |
| chuanga | 0:43ff9e3bc244 | 122 | } |
| chuanga | 0:43ff9e3bc244 | 123 | |
| chuanga | 0:43ff9e3bc244 | 124 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 125 | if (address->set_ip_address(name)) { |
| chuanga | 0:43ff9e3bc244 | 126 | if (version != NSAPI_UNSPEC && address->get_ip_version() != version) { |
| chuanga | 0:43ff9e3bc244 | 127 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 128 | return NSAPI_ERROR_DNS_FAILURE; |
| chuanga | 0:43ff9e3bc244 | 129 | } |
| chuanga | 0:43ff9e3bc244 | 130 | |
| chuanga | 0:43ff9e3bc244 | 131 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 132 | return NSAPI_ERROR_OK; |
| chuanga | 0:43ff9e3bc244 | 133 | } |
| chuanga | 0:43ff9e3bc244 | 134 | |
| chuanga | 0:43ff9e3bc244 | 135 | char *ipbuff = new char[NSAPI_IP_SIZE]; |
| chuanga | 0:43ff9e3bc244 | 136 | int ret = 0; |
| chuanga | 0:43ff9e3bc244 | 137 | if (!_ism.dns_lookup(name, ipbuff)) { |
| chuanga | 0:43ff9e3bc244 | 138 | ret = NSAPI_ERROR_DNS_FAILURE; |
| chuanga | 0:43ff9e3bc244 | 139 | } else { |
| chuanga | 0:43ff9e3bc244 | 140 | address->set_ip_address(ipbuff); |
| chuanga | 0:43ff9e3bc244 | 141 | } |
| chuanga | 0:43ff9e3bc244 | 142 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 143 | |
| chuanga | 0:43ff9e3bc244 | 144 | delete[] ipbuff; |
| chuanga | 0:43ff9e3bc244 | 145 | |
| chuanga | 0:43ff9e3bc244 | 146 | return ret; |
| chuanga | 0:43ff9e3bc244 | 147 | } |
| chuanga | 0:43ff9e3bc244 | 148 | |
| chuanga | 0:43ff9e3bc244 | 149 | int ISM43362Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
| chuanga | 0:43ff9e3bc244 | 150 | { |
| chuanga | 0:43ff9e3bc244 | 151 | if ((ssid == NULL) || (strlen(ssid) == 0) || (strlen(ssid) > 32)) { |
| chuanga | 0:43ff9e3bc244 | 152 | return NSAPI_ERROR_PARAMETER; |
| chuanga | 0:43ff9e3bc244 | 153 | } |
| chuanga | 0:43ff9e3bc244 | 154 | |
| chuanga | 0:43ff9e3bc244 | 155 | if (security != NSAPI_SECURITY_NONE) { |
| chuanga | 0:43ff9e3bc244 | 156 | if ((pass == NULL) || (strcmp(pass, "") == 0)) { |
| chuanga | 0:43ff9e3bc244 | 157 | return NSAPI_ERROR_PARAMETER; |
| chuanga | 0:43ff9e3bc244 | 158 | } |
| chuanga | 0:43ff9e3bc244 | 159 | } |
| chuanga | 0:43ff9e3bc244 | 160 | |
| chuanga | 0:43ff9e3bc244 | 161 | if (strlen(pass) > 63) { |
| chuanga | 0:43ff9e3bc244 | 162 | return NSAPI_ERROR_PARAMETER; |
| chuanga | 0:43ff9e3bc244 | 163 | } |
| chuanga | 0:43ff9e3bc244 | 164 | |
| chuanga | 0:43ff9e3bc244 | 165 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 166 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
| chuanga | 0:43ff9e3bc244 | 167 | strncpy(ap_ssid, ssid, sizeof(ap_ssid)); |
| chuanga | 0:43ff9e3bc244 | 168 | |
| chuanga | 0:43ff9e3bc244 | 169 | memset(ap_pass, 0, sizeof(ap_pass)); |
| chuanga | 0:43ff9e3bc244 | 170 | strncpy(ap_pass, pass, sizeof(ap_pass)); |
| chuanga | 0:43ff9e3bc244 | 171 | |
| chuanga | 0:43ff9e3bc244 | 172 | switch (security) { |
| chuanga | 0:43ff9e3bc244 | 173 | case NSAPI_SECURITY_NONE: |
| chuanga | 0:43ff9e3bc244 | 174 | ap_sec = ISM_SECURITY_NONE; |
| chuanga | 0:43ff9e3bc244 | 175 | break; |
| chuanga | 0:43ff9e3bc244 | 176 | case NSAPI_SECURITY_WEP: |
| chuanga | 0:43ff9e3bc244 | 177 | ap_sec = ISM_SECURITY_WEP; |
| chuanga | 0:43ff9e3bc244 | 178 | break; |
| chuanga | 0:43ff9e3bc244 | 179 | case NSAPI_SECURITY_WPA: |
| chuanga | 0:43ff9e3bc244 | 180 | ap_sec = ISM_SECURITY_WPA; |
| chuanga | 0:43ff9e3bc244 | 181 | break; |
| chuanga | 0:43ff9e3bc244 | 182 | case NSAPI_SECURITY_WPA2: |
| chuanga | 0:43ff9e3bc244 | 183 | ap_sec = ISM_SECURITY_WPA2; |
| chuanga | 0:43ff9e3bc244 | 184 | break; |
| chuanga | 0:43ff9e3bc244 | 185 | case NSAPI_SECURITY_WPA_WPA2: |
| chuanga | 0:43ff9e3bc244 | 186 | ap_sec = ISM_SECURITY_WPA_WPA2; |
| chuanga | 0:43ff9e3bc244 | 187 | break; |
| chuanga | 0:43ff9e3bc244 | 188 | default: |
| chuanga | 0:43ff9e3bc244 | 189 | ap_sec = ISM_SECURITY_UNKNOWN; |
| chuanga | 0:43ff9e3bc244 | 190 | break; |
| chuanga | 0:43ff9e3bc244 | 191 | } |
| chuanga | 0:43ff9e3bc244 | 192 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 193 | |
| chuanga | 0:43ff9e3bc244 | 194 | return NSAPI_ERROR_OK; |
| chuanga | 0:43ff9e3bc244 | 195 | } |
| chuanga | 0:43ff9e3bc244 | 196 | |
| chuanga | 0:43ff9e3bc244 | 197 | int ISM43362Interface::set_channel(uint8_t channel) |
| chuanga | 0:43ff9e3bc244 | 198 | { |
| chuanga | 0:43ff9e3bc244 | 199 | return NSAPI_ERROR_UNSUPPORTED; |
| chuanga | 0:43ff9e3bc244 | 200 | } |
| chuanga | 0:43ff9e3bc244 | 201 | |
| chuanga | 0:43ff9e3bc244 | 202 | nsapi_error_t ISM43362Interface::disconnect() |
| chuanga | 0:43ff9e3bc244 | 203 | { |
| chuanga | 0:43ff9e3bc244 | 204 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 205 | |
| chuanga | 0:43ff9e3bc244 | 206 | if (_connect_status != NSAPI_ERROR_OK) { |
| chuanga | 0:43ff9e3bc244 | 207 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 208 | return NSAPI_ERROR_NO_CONNECTION; |
| chuanga | 0:43ff9e3bc244 | 209 | } |
| chuanga | 0:43ff9e3bc244 | 210 | |
| chuanga | 0:43ff9e3bc244 | 211 | if (!_ism.disconnect()) { |
| chuanga | 0:43ff9e3bc244 | 212 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 213 | return NSAPI_ERROR_DEVICE_ERROR; |
| chuanga | 0:43ff9e3bc244 | 214 | } |
| chuanga | 0:43ff9e3bc244 | 215 | |
| chuanga | 0:43ff9e3bc244 | 216 | _connect_status = NSAPI_ERROR_NO_CONNECTION; |
| chuanga | 0:43ff9e3bc244 | 217 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 218 | return NSAPI_ERROR_OK; |
| chuanga | 0:43ff9e3bc244 | 219 | } |
| chuanga | 0:43ff9e3bc244 | 220 | |
| chuanga | 0:43ff9e3bc244 | 221 | const char *ISM43362Interface::get_ip_address() |
| chuanga | 0:43ff9e3bc244 | 222 | { |
| chuanga | 0:43ff9e3bc244 | 223 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 224 | const char *ret = _ism.getIPAddress(); |
| chuanga | 0:43ff9e3bc244 | 225 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 226 | return ret; |
| chuanga | 0:43ff9e3bc244 | 227 | } |
| chuanga | 0:43ff9e3bc244 | 228 | |
| chuanga | 0:43ff9e3bc244 | 229 | const char *ISM43362Interface::get_mac_address() |
| chuanga | 0:43ff9e3bc244 | 230 | { |
| chuanga | 0:43ff9e3bc244 | 231 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 232 | const char *ret = _ism.getMACAddress(); |
| chuanga | 0:43ff9e3bc244 | 233 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 234 | return ret; |
| chuanga | 0:43ff9e3bc244 | 235 | } |
| chuanga | 0:43ff9e3bc244 | 236 | |
| chuanga | 0:43ff9e3bc244 | 237 | const char *ISM43362Interface::get_gateway() |
| chuanga | 0:43ff9e3bc244 | 238 | { |
| chuanga | 0:43ff9e3bc244 | 239 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 240 | const char *ret = _ism.getGateway(); |
| chuanga | 0:43ff9e3bc244 | 241 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 242 | return ret; |
| chuanga | 0:43ff9e3bc244 | 243 | } |
| chuanga | 0:43ff9e3bc244 | 244 | |
| chuanga | 0:43ff9e3bc244 | 245 | const char *ISM43362Interface::get_netmask() |
| chuanga | 0:43ff9e3bc244 | 246 | { |
| chuanga | 0:43ff9e3bc244 | 247 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 248 | const char *ret = _ism.getNetmask(); |
| chuanga | 0:43ff9e3bc244 | 249 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 250 | return ret; |
| chuanga | 0:43ff9e3bc244 | 251 | } |
| chuanga | 0:43ff9e3bc244 | 252 | |
| chuanga | 0:43ff9e3bc244 | 253 | int8_t ISM43362Interface::get_rssi() |
| chuanga | 0:43ff9e3bc244 | 254 | { |
| chuanga | 0:43ff9e3bc244 | 255 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 256 | int8_t ret = _ism.getRSSI(); |
| chuanga | 0:43ff9e3bc244 | 257 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 258 | return ret; |
| chuanga | 0:43ff9e3bc244 | 259 | } |
| chuanga | 0:43ff9e3bc244 | 260 | |
| chuanga | 0:43ff9e3bc244 | 261 | int ISM43362Interface::scan(WiFiAccessPoint *res, unsigned count) |
| chuanga | 0:43ff9e3bc244 | 262 | { |
| chuanga | 0:43ff9e3bc244 | 263 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 264 | int ret = _ism.scan(res, count); |
| chuanga | 0:43ff9e3bc244 | 265 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 266 | return ret; |
| chuanga | 0:43ff9e3bc244 | 267 | } |
| chuanga | 0:43ff9e3bc244 | 268 | |
| chuanga | 0:43ff9e3bc244 | 269 | struct ISM43362_socket { |
| chuanga | 0:43ff9e3bc244 | 270 | int id; |
| chuanga | 0:43ff9e3bc244 | 271 | nsapi_protocol_t proto; |
| chuanga | 0:43ff9e3bc244 | 272 | volatile bool connected; |
| chuanga | 0:43ff9e3bc244 | 273 | SocketAddress addr; |
| chuanga | 0:43ff9e3bc244 | 274 | char read_data[1400]; |
| chuanga | 0:43ff9e3bc244 | 275 | volatile uint32_t read_data_size; |
| chuanga | 0:43ff9e3bc244 | 276 | }; |
| chuanga | 0:43ff9e3bc244 | 277 | |
| chuanga | 0:43ff9e3bc244 | 278 | int ISM43362Interface::socket_open(void **handle, nsapi_protocol_t proto) |
| chuanga | 0:43ff9e3bc244 | 279 | { |
| chuanga | 0:43ff9e3bc244 | 280 | // Look for an unused socket |
| chuanga | 0:43ff9e3bc244 | 281 | int id = -1; |
| chuanga | 0:43ff9e3bc244 | 282 | for (int i = 0; i < ISM43362_SOCKET_COUNT; i++) { |
| chuanga | 0:43ff9e3bc244 | 283 | if (!_ids[i]) { |
| chuanga | 0:43ff9e3bc244 | 284 | id = i; |
| chuanga | 0:43ff9e3bc244 | 285 | _ids[i] = true; |
| chuanga | 0:43ff9e3bc244 | 286 | break; |
| chuanga | 0:43ff9e3bc244 | 287 | } |
| chuanga | 0:43ff9e3bc244 | 288 | } |
| chuanga | 0:43ff9e3bc244 | 289 | |
| chuanga | 0:43ff9e3bc244 | 290 | if (id == -1) { |
| chuanga | 0:43ff9e3bc244 | 291 | return NSAPI_ERROR_NO_SOCKET; |
| chuanga | 0:43ff9e3bc244 | 292 | } |
| chuanga | 0:43ff9e3bc244 | 293 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 294 | struct ISM43362_socket *socket = new struct ISM43362_socket; |
| chuanga | 0:43ff9e3bc244 | 295 | if (!socket) { |
| chuanga | 0:43ff9e3bc244 | 296 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 297 | return NSAPI_ERROR_NO_SOCKET; |
| chuanga | 0:43ff9e3bc244 | 298 | } |
| chuanga | 0:43ff9e3bc244 | 299 | socket->id = id; |
| chuanga | 0:43ff9e3bc244 | 300 | debug_if(_ism_debug, "ISM43362Interface: socket_open, id=%d\n", socket->id); |
| chuanga | 0:43ff9e3bc244 | 301 | memset(socket->read_data, 0, sizeof(socket->read_data)); |
| chuanga | 0:43ff9e3bc244 | 302 | socket->addr = 0; |
| chuanga | 0:43ff9e3bc244 | 303 | socket->read_data_size = 0; |
| chuanga | 0:43ff9e3bc244 | 304 | socket->proto = proto; |
| chuanga | 0:43ff9e3bc244 | 305 | socket->connected = false; |
| chuanga | 0:43ff9e3bc244 | 306 | *handle = socket; |
| chuanga | 0:43ff9e3bc244 | 307 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 308 | |
| chuanga | 0:43ff9e3bc244 | 309 | return 0; |
| chuanga | 0:43ff9e3bc244 | 310 | } |
| chuanga | 0:43ff9e3bc244 | 311 | |
| chuanga | 0:43ff9e3bc244 | 312 | int ISM43362Interface::socket_close(void *handle) |
| chuanga | 0:43ff9e3bc244 | 313 | { |
| chuanga | 0:43ff9e3bc244 | 314 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 315 | struct ISM43362_socket *socket = (struct ISM43362_socket *)handle; |
| chuanga | 0:43ff9e3bc244 | 316 | debug_if(_ism_debug, "ISM43362Interface: socket_close, id=%d\n", socket->id); |
| chuanga | 0:43ff9e3bc244 | 317 | int err = 0; |
| chuanga | 0:43ff9e3bc244 | 318 | |
| chuanga | 0:43ff9e3bc244 | 319 | if (!_ism.close(socket->id)) { |
| chuanga | 0:43ff9e3bc244 | 320 | err = NSAPI_ERROR_DEVICE_ERROR; |
| chuanga | 0:43ff9e3bc244 | 321 | } |
| chuanga | 0:43ff9e3bc244 | 322 | |
| chuanga | 0:43ff9e3bc244 | 323 | socket->connected = false; |
| chuanga | 0:43ff9e3bc244 | 324 | _ids[socket->id] = false; |
| chuanga | 0:43ff9e3bc244 | 325 | _socket_obj[socket->id] = 0; |
| chuanga | 0:43ff9e3bc244 | 326 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 327 | delete socket; |
| chuanga | 0:43ff9e3bc244 | 328 | return err; |
| chuanga | 0:43ff9e3bc244 | 329 | } |
| chuanga | 0:43ff9e3bc244 | 330 | |
| chuanga | 0:43ff9e3bc244 | 331 | int ISM43362Interface::socket_bind(void *handle, const SocketAddress &address) |
| chuanga | 0:43ff9e3bc244 | 332 | { |
| chuanga | 0:43ff9e3bc244 | 333 | return NSAPI_ERROR_UNSUPPORTED; |
| chuanga | 0:43ff9e3bc244 | 334 | } |
| chuanga | 0:43ff9e3bc244 | 335 | |
| chuanga | 0:43ff9e3bc244 | 336 | int ISM43362Interface::socket_listen(void *handle, int backlog) |
| chuanga | 0:43ff9e3bc244 | 337 | { |
| chuanga | 0:43ff9e3bc244 | 338 | return NSAPI_ERROR_UNSUPPORTED; |
| chuanga | 0:43ff9e3bc244 | 339 | } |
| chuanga | 0:43ff9e3bc244 | 340 | |
| chuanga | 0:43ff9e3bc244 | 341 | int ISM43362Interface::socket_connect(void *handle, const SocketAddress &addr) |
| chuanga | 0:43ff9e3bc244 | 342 | { |
| chuanga | 0:43ff9e3bc244 | 343 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 344 | int ret = socket_connect_nolock(handle, addr); |
| chuanga | 0:43ff9e3bc244 | 345 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 346 | return ret; |
| chuanga | 0:43ff9e3bc244 | 347 | } |
| chuanga | 0:43ff9e3bc244 | 348 | |
| chuanga | 0:43ff9e3bc244 | 349 | int ISM43362Interface::socket_connect_nolock(void *handle, const SocketAddress &addr) |
| chuanga | 0:43ff9e3bc244 | 350 | { |
| chuanga | 0:43ff9e3bc244 | 351 | struct ISM43362_socket *socket = (struct ISM43362_socket *)handle; |
| chuanga | 0:43ff9e3bc244 | 352 | const char *proto = (socket->proto == NSAPI_UDP) ? "1" : "0"; |
| chuanga | 0:43ff9e3bc244 | 353 | if (!_ism.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) { |
| chuanga | 0:43ff9e3bc244 | 354 | return NSAPI_ERROR_DEVICE_ERROR; |
| chuanga | 0:43ff9e3bc244 | 355 | } |
| chuanga | 0:43ff9e3bc244 | 356 | _ids[socket->id] = true; |
| chuanga | 0:43ff9e3bc244 | 357 | _socket_obj[socket->id] = (uint32_t)socket; |
| chuanga | 0:43ff9e3bc244 | 358 | socket->connected = true; |
| chuanga | 0:43ff9e3bc244 | 359 | return 0; |
| chuanga | 0:43ff9e3bc244 | 360 | |
| chuanga | 0:43ff9e3bc244 | 361 | } |
| chuanga | 0:43ff9e3bc244 | 362 | |
| chuanga | 0:43ff9e3bc244 | 363 | |
| chuanga | 0:43ff9e3bc244 | 364 | |
| chuanga | 0:43ff9e3bc244 | 365 | void ISM43362Interface::socket_check_read() |
| chuanga | 0:43ff9e3bc244 | 366 | { |
| chuanga | 0:43ff9e3bc244 | 367 | while (1) { |
| chuanga | 0:43ff9e3bc244 | 368 | for (int i = 0; i < ISM43362_SOCKET_COUNT; i++) { |
| chuanga | 0:43ff9e3bc244 | 369 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 370 | if (_socket_obj[i] != 0) { |
| chuanga | 0:43ff9e3bc244 | 371 | struct ISM43362_socket *socket = (struct ISM43362_socket *)_socket_obj[i]; |
| chuanga | 0:43ff9e3bc244 | 372 | /* Check if there is something to read for this socket. But if it */ |
| chuanga | 0:43ff9e3bc244 | 373 | /* has already been read : don't read again */ |
| chuanga | 0:43ff9e3bc244 | 374 | if ((socket->connected) && (socket->read_data_size == 0) && _cbs[socket->id].callback) { |
| chuanga | 0:43ff9e3bc244 | 375 | /* if no callback is set, no need to read ?*/ |
| chuanga | 0:43ff9e3bc244 | 376 | int read_amount = _ism.check_recv_status(socket->id, socket->read_data); |
| chuanga | 0:43ff9e3bc244 | 377 | // debug_if(_ism_debug, "ISM43362Interface socket_check_read: i %d read_amount %d \r\n", i, read_amount); |
| chuanga | 0:43ff9e3bc244 | 378 | if (read_amount > 0) { |
| chuanga | 0:43ff9e3bc244 | 379 | socket->read_data_size = read_amount; |
| chuanga | 0:43ff9e3bc244 | 380 | } else if (read_amount < 0) { |
| chuanga | 0:43ff9e3bc244 | 381 | /* Mark donw connection has been lost or closed */ |
| chuanga | 0:43ff9e3bc244 | 382 | debug_if(_ism_debug, "ISM43362Interface socket_check_read: i %d closed\r\n", i); |
| chuanga | 0:43ff9e3bc244 | 383 | socket->connected = false; |
| chuanga | 0:43ff9e3bc244 | 384 | } |
| chuanga | 0:43ff9e3bc244 | 385 | if (read_amount != 0) { |
| chuanga | 0:43ff9e3bc244 | 386 | /* There is something to read in this socket*/ |
| chuanga | 0:43ff9e3bc244 | 387 | if (_cbs[socket->id].callback) { |
| chuanga | 0:43ff9e3bc244 | 388 | _cbs[socket->id].callback(_cbs[socket->id].data); |
| chuanga | 0:43ff9e3bc244 | 389 | } |
| chuanga | 0:43ff9e3bc244 | 390 | } |
| chuanga | 0:43ff9e3bc244 | 391 | } |
| chuanga | 0:43ff9e3bc244 | 392 | } |
| chuanga | 0:43ff9e3bc244 | 393 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 394 | } |
| chuanga | 0:43ff9e3bc244 | 395 | wait_ms(50); |
| chuanga | 0:43ff9e3bc244 | 396 | } |
| chuanga | 0:43ff9e3bc244 | 397 | } |
| chuanga | 0:43ff9e3bc244 | 398 | |
| chuanga | 0:43ff9e3bc244 | 399 | int ISM43362Interface::socket_accept(void *server, void **socket, SocketAddress *addr) |
| chuanga | 0:43ff9e3bc244 | 400 | { |
| chuanga | 0:43ff9e3bc244 | 401 | return NSAPI_ERROR_UNSUPPORTED; |
| chuanga | 0:43ff9e3bc244 | 402 | } |
| chuanga | 0:43ff9e3bc244 | 403 | |
| chuanga | 0:43ff9e3bc244 | 404 | int ISM43362Interface::socket_send(void *handle, const void *data, unsigned size) |
| chuanga | 0:43ff9e3bc244 | 405 | { |
| chuanga | 0:43ff9e3bc244 | 406 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 407 | int ret = socket_send_nolock(handle, data, size); |
| chuanga | 0:43ff9e3bc244 | 408 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 409 | return ret; |
| chuanga | 0:43ff9e3bc244 | 410 | } |
| chuanga | 0:43ff9e3bc244 | 411 | |
| chuanga | 0:43ff9e3bc244 | 412 | /* CAREFULL LOCK must be taken before callling this function */ |
| chuanga | 0:43ff9e3bc244 | 413 | int ISM43362Interface::socket_send_nolock(void *handle, const void *data, unsigned size) |
| chuanga | 0:43ff9e3bc244 | 414 | { |
| chuanga | 0:43ff9e3bc244 | 415 | struct ISM43362_socket *socket = (struct ISM43362_socket *)handle; |
| chuanga | 0:43ff9e3bc244 | 416 | |
| chuanga | 0:43ff9e3bc244 | 417 | if (size > ES_WIFI_MAX_TX_PACKET_SIZE) { |
| chuanga | 0:43ff9e3bc244 | 418 | size = ES_WIFI_MAX_TX_PACKET_SIZE; |
| chuanga | 0:43ff9e3bc244 | 419 | } |
| chuanga | 0:43ff9e3bc244 | 420 | |
| chuanga | 0:43ff9e3bc244 | 421 | if (!_ism.send(socket->id, data, size)) { |
| chuanga | 0:43ff9e3bc244 | 422 | debug_if(_ism_debug, "ISM43362Interface: socket_send ERROR\r\n"); |
| chuanga | 0:43ff9e3bc244 | 423 | return NSAPI_ERROR_DEVICE_ERROR; // or WOULD_BLOCK ? |
| chuanga | 0:43ff9e3bc244 | 424 | } |
| chuanga | 0:43ff9e3bc244 | 425 | |
| chuanga | 0:43ff9e3bc244 | 426 | return size; |
| chuanga | 0:43ff9e3bc244 | 427 | } |
| chuanga | 0:43ff9e3bc244 | 428 | |
| chuanga | 0:43ff9e3bc244 | 429 | int ISM43362Interface::socket_recv(void *handle, void *data, unsigned size) |
| chuanga | 0:43ff9e3bc244 | 430 | { |
| chuanga | 0:43ff9e3bc244 | 431 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 432 | unsigned recv = 0; |
| chuanga | 0:43ff9e3bc244 | 433 | struct ISM43362_socket *socket = (struct ISM43362_socket *)handle; |
| chuanga | 0:43ff9e3bc244 | 434 | char *ptr = (char *)data; |
| chuanga | 0:43ff9e3bc244 | 435 | |
| chuanga | 0:43ff9e3bc244 | 436 | // debug_if(_ism_debug, "ISM43362Interface socket_recv: req=%d read_data_size=%d connected %d\r\n", size, socket->read_data_size, socket->connected); |
| chuanga | 0:43ff9e3bc244 | 437 | |
| chuanga | 0:43ff9e3bc244 | 438 | if (!socket->connected) { |
| chuanga | 0:43ff9e3bc244 | 439 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 440 | return 0; |
| chuanga | 0:43ff9e3bc244 | 441 | } |
| chuanga | 0:43ff9e3bc244 | 442 | |
| chuanga | 0:43ff9e3bc244 | 443 | if (socket->read_data_size == 0) { |
| chuanga | 0:43ff9e3bc244 | 444 | /* if no callback is set, no need to read ?*/ |
| chuanga | 0:43ff9e3bc244 | 445 | int read_amount = _ism.check_recv_status(socket->id, socket->read_data); |
| chuanga | 0:43ff9e3bc244 | 446 | if (read_amount > 0) { |
| chuanga | 0:43ff9e3bc244 | 447 | socket->read_data_size = read_amount; |
| chuanga | 0:43ff9e3bc244 | 448 | } else if (read_amount < 0) { |
| chuanga | 0:43ff9e3bc244 | 449 | socket->connected = false; |
| chuanga | 0:43ff9e3bc244 | 450 | debug_if(_ism_debug, "ISM43362Interface socket_recv: socket closed\r\n"); |
| chuanga | 0:43ff9e3bc244 | 451 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 452 | return 0; |
| chuanga | 0:43ff9e3bc244 | 453 | } |
| chuanga | 0:43ff9e3bc244 | 454 | } |
| chuanga | 0:43ff9e3bc244 | 455 | |
| chuanga | 0:43ff9e3bc244 | 456 | if (socket->read_data_size != 0) { |
| chuanga | 0:43ff9e3bc244 | 457 | // debug_if(_ism_debug, "ISM43362Interface socket_recv: read_data_size=%d\r\n", socket->read_data_size); |
| chuanga | 0:43ff9e3bc244 | 458 | uint32_t i = 0; |
| chuanga | 0:43ff9e3bc244 | 459 | while ((i < socket->read_data_size) && (i < size)) { |
| chuanga | 0:43ff9e3bc244 | 460 | *ptr++ = socket->read_data[i]; |
| chuanga | 0:43ff9e3bc244 | 461 | i++; |
| chuanga | 0:43ff9e3bc244 | 462 | } |
| chuanga | 0:43ff9e3bc244 | 463 | |
| chuanga | 0:43ff9e3bc244 | 464 | recv += i; |
| chuanga | 0:43ff9e3bc244 | 465 | |
| chuanga | 0:43ff9e3bc244 | 466 | if (i >= socket->read_data_size) { |
| chuanga | 0:43ff9e3bc244 | 467 | /* All the storeed data has been read, reset buffer */ |
| chuanga | 0:43ff9e3bc244 | 468 | memset(socket->read_data, 0, sizeof(socket->read_data)); |
| chuanga | 0:43ff9e3bc244 | 469 | socket->read_data_size = 0; |
| chuanga | 0:43ff9e3bc244 | 470 | // debug_if(_ism_debug, "ISM43362Interface: Socket_recv buffer reset\r\n"); |
| chuanga | 0:43ff9e3bc244 | 471 | } else { |
| chuanga | 0:43ff9e3bc244 | 472 | /* In case there is remaining data in buffer, update socket content |
| chuanga | 0:43ff9e3bc244 | 473 | * For now by shift copy of all data (not very efficient to be |
| chuanga | 0:43ff9e3bc244 | 474 | * revised */ |
| chuanga | 0:43ff9e3bc244 | 475 | while (i < socket->read_data_size) { |
| chuanga | 0:43ff9e3bc244 | 476 | socket->read_data[i - size] = socket->read_data[i]; |
| chuanga | 0:43ff9e3bc244 | 477 | i++; |
| chuanga | 0:43ff9e3bc244 | 478 | } |
| chuanga | 0:43ff9e3bc244 | 479 | |
| chuanga | 0:43ff9e3bc244 | 480 | socket->read_data_size -= size; |
| chuanga | 0:43ff9e3bc244 | 481 | } |
| chuanga | 0:43ff9e3bc244 | 482 | } |
| chuanga | 0:43ff9e3bc244 | 483 | // else { |
| chuanga | 0:43ff9e3bc244 | 484 | // debug_if(_ism_debug, "ISM43362Interface socket_recv: Nothing in buffer\r\n"); |
| chuanga | 0:43ff9e3bc244 | 485 | // } |
| chuanga | 0:43ff9e3bc244 | 486 | |
| chuanga | 0:43ff9e3bc244 | 487 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 488 | |
| chuanga | 0:43ff9e3bc244 | 489 | if (recv > 0) { |
| chuanga | 0:43ff9e3bc244 | 490 | debug_if(_ism_debug, "ISM43362Interface socket_recv: recv=%d\r\n", recv); |
| chuanga | 0:43ff9e3bc244 | 491 | return recv; |
| chuanga | 0:43ff9e3bc244 | 492 | } else { |
| chuanga | 0:43ff9e3bc244 | 493 | debug_if(_ism_debug, "ISM43362Interface socket_recv: returns WOULD BLOCK\r\n"); |
| chuanga | 0:43ff9e3bc244 | 494 | return NSAPI_ERROR_WOULD_BLOCK; |
| chuanga | 0:43ff9e3bc244 | 495 | } |
| chuanga | 0:43ff9e3bc244 | 496 | } |
| chuanga | 0:43ff9e3bc244 | 497 | |
| chuanga | 0:43ff9e3bc244 | 498 | int ISM43362Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
| chuanga | 0:43ff9e3bc244 | 499 | { |
| chuanga | 0:43ff9e3bc244 | 500 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 501 | struct ISM43362_socket *socket = (struct ISM43362_socket *)handle; |
| chuanga | 0:43ff9e3bc244 | 502 | |
| chuanga | 0:43ff9e3bc244 | 503 | if (socket->connected && socket->addr != addr) { |
| chuanga | 0:43ff9e3bc244 | 504 | if (!_ism.close(socket->id)) { |
| chuanga | 0:43ff9e3bc244 | 505 | debug_if(_ism_debug, "ISM43362Interface: socket_sendto ERROR\r\n"); |
| chuanga | 0:43ff9e3bc244 | 506 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 507 | return NSAPI_ERROR_DEVICE_ERROR; |
| chuanga | 0:43ff9e3bc244 | 508 | } |
| chuanga | 0:43ff9e3bc244 | 509 | socket->connected = false; |
| chuanga | 0:43ff9e3bc244 | 510 | _ids[socket->id] = false; |
| chuanga | 0:43ff9e3bc244 | 511 | _socket_obj[socket->id] = 0; |
| chuanga | 0:43ff9e3bc244 | 512 | } |
| chuanga | 0:43ff9e3bc244 | 513 | |
| chuanga | 0:43ff9e3bc244 | 514 | if (!socket->connected) { |
| chuanga | 0:43ff9e3bc244 | 515 | int err = socket_connect_nolock(socket, addr); |
| chuanga | 0:43ff9e3bc244 | 516 | if (err < 0) { |
| chuanga | 0:43ff9e3bc244 | 517 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 518 | return err; |
| chuanga | 0:43ff9e3bc244 | 519 | } |
| chuanga | 0:43ff9e3bc244 | 520 | socket->addr = addr; |
| chuanga | 0:43ff9e3bc244 | 521 | } |
| chuanga | 0:43ff9e3bc244 | 522 | |
| chuanga | 0:43ff9e3bc244 | 523 | int ret = socket_send_nolock(socket, data, size); |
| chuanga | 0:43ff9e3bc244 | 524 | |
| chuanga | 0:43ff9e3bc244 | 525 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 526 | |
| chuanga | 0:43ff9e3bc244 | 527 | return ret; |
| chuanga | 0:43ff9e3bc244 | 528 | } |
| chuanga | 0:43ff9e3bc244 | 529 | |
| chuanga | 0:43ff9e3bc244 | 530 | int ISM43362Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
| chuanga | 0:43ff9e3bc244 | 531 | { |
| chuanga | 0:43ff9e3bc244 | 532 | int ret = socket_recv(handle, data, size); |
| chuanga | 0:43ff9e3bc244 | 533 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 534 | if ((ret >= 0) && addr) { |
| chuanga | 0:43ff9e3bc244 | 535 | struct ISM43362_socket *socket = (struct ISM43362_socket *)handle; |
| chuanga | 0:43ff9e3bc244 | 536 | *addr = socket->addr; |
| chuanga | 0:43ff9e3bc244 | 537 | } |
| chuanga | 0:43ff9e3bc244 | 538 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 539 | return ret; |
| chuanga | 0:43ff9e3bc244 | 540 | } |
| chuanga | 0:43ff9e3bc244 | 541 | |
| chuanga | 0:43ff9e3bc244 | 542 | void ISM43362Interface::socket_attach(void *handle, void (*cb)(void *), void *data) |
| chuanga | 0:43ff9e3bc244 | 543 | { |
| chuanga | 0:43ff9e3bc244 | 544 | _mutex.lock(); |
| chuanga | 0:43ff9e3bc244 | 545 | struct ISM43362_socket *socket = (struct ISM43362_socket *)handle; |
| chuanga | 0:43ff9e3bc244 | 546 | _cbs[socket->id].callback = cb; |
| chuanga | 0:43ff9e3bc244 | 547 | _cbs[socket->id].data = data; |
| chuanga | 0:43ff9e3bc244 | 548 | _mutex.unlock(); |
| chuanga | 0:43ff9e3bc244 | 549 | } |
| chuanga | 0:43ff9e3bc244 | 550 | |
| chuanga | 0:43ff9e3bc244 | 551 | void ISM43362Interface::event() |
| chuanga | 0:43ff9e3bc244 | 552 | { |
| chuanga | 0:43ff9e3bc244 | 553 | for (int i = 0; i < ISM43362_SOCKET_COUNT; i++) { |
| chuanga | 0:43ff9e3bc244 | 554 | if (_cbs[i].callback) { |
| chuanga | 0:43ff9e3bc244 | 555 | _cbs[i].callback(_cbs[i].data); |
| chuanga | 0:43ff9e3bc244 | 556 | } |
| chuanga | 0:43ff9e3bc244 | 557 | } |
| chuanga | 0:43ff9e3bc244 | 558 | } |
| chuanga | 0:43ff9e3bc244 | 559 | |
| chuanga | 0:43ff9e3bc244 | 560 | void ISM43362Interface::attach(Callback<void(nsapi_event_t, intptr_t)> status_cb) |
| chuanga | 0:43ff9e3bc244 | 561 | { |
| chuanga | 0:43ff9e3bc244 | 562 | debug_if(_ism_debug, "ISM43362Interface: attach\n"); |
| chuanga | 0:43ff9e3bc244 | 563 | _conn_stat_cb = status_cb; |
| chuanga | 0:43ff9e3bc244 | 564 | } |
| chuanga | 0:43ff9e3bc244 | 565 | |
| chuanga | 0:43ff9e3bc244 | 566 | nsapi_connection_status_t ISM43362Interface::get_connection_status() const |
| chuanga | 0:43ff9e3bc244 | 567 | { |
| chuanga | 0:43ff9e3bc244 | 568 | debug_if(_ism_debug, "ISM43362Interface: get_connection_status %d\n", _conn_stat); |
| chuanga | 0:43ff9e3bc244 | 569 | return _conn_stat; |
| chuanga | 0:43ff9e3bc244 | 570 | } |
| chuanga | 0:43ff9e3bc244 | 571 | |
| chuanga | 0:43ff9e3bc244 | 572 | |
| chuanga | 0:43ff9e3bc244 | 573 | void ISM43362Interface::update_conn_state_cb() |
| chuanga | 0:43ff9e3bc244 | 574 | { |
| chuanga | 0:43ff9e3bc244 | 575 | nsapi_connection_status_t prev_stat = _conn_stat; |
| chuanga | 0:43ff9e3bc244 | 576 | _conn_stat = _ism.connection_status(); |
| chuanga | 0:43ff9e3bc244 | 577 | debug_if(_ism_debug, "ISM43362Interface: update_conn_state_cb %d -> %d\n", prev_stat, _conn_stat); |
| chuanga | 0:43ff9e3bc244 | 578 | |
| chuanga | 0:43ff9e3bc244 | 579 | if (prev_stat == _conn_stat) { |
| chuanga | 0:43ff9e3bc244 | 580 | return; |
| chuanga | 0:43ff9e3bc244 | 581 | } |
| chuanga | 0:43ff9e3bc244 | 582 | |
| chuanga | 0:43ff9e3bc244 | 583 | // Inform upper layers |
| chuanga | 0:43ff9e3bc244 | 584 | if (_conn_stat_cb) { |
| chuanga | 0:43ff9e3bc244 | 585 | _conn_stat_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _conn_stat); |
| chuanga | 0:43ff9e3bc244 | 586 | } |
| chuanga | 0:43ff9e3bc244 | 587 | } |
| chuanga | 0:43ff9e3bc244 | 588 | |
| chuanga | 0:43ff9e3bc244 | 589 | |
| chuanga | 0:43ff9e3bc244 | 590 | #if MBED_CONF_ISM43362_PROVIDE_DEFAULT |
| chuanga | 0:43ff9e3bc244 | 591 | |
| chuanga | 0:43ff9e3bc244 | 592 | WiFiInterface *WiFiInterface::get_default_instance() { |
| chuanga | 0:43ff9e3bc244 | 593 | static ISM43362Interface ism; |
| chuanga | 0:43ff9e3bc244 | 594 | return &ism; |
| chuanga | 0:43ff9e3bc244 | 595 | } |
| chuanga | 0:43ff9e3bc244 | 596 | |
| chuanga | 0:43ff9e3bc244 | 597 | #endif |