leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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