versione corretta

Dependents:   DISCO_L475VG_IOT01-Sensors-BSP

Committer:
group-Farnell24-IOT-Team
Date:
Tue Aug 21 08:34:28 2018 +0000
Revision:
0:766454e296c3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-Farnell24-IOT-Team 0:766454e296c3 1 /* ISM43362 implementation of NetworkInterfaceAPI
group-Farnell24-IOT-Team 0:766454e296c3 2 * Copyright (c) STMicroelectronics 2017
group-Farnell24-IOT-Team 0:766454e296c3 3 *
group-Farnell24-IOT-Team 0:766454e296c3 4 * Licensed under the Apache License, Version 2.0 (the "License");
group-Farnell24-IOT-Team 0:766454e296c3 5 * you may not use this file except in compliance with the License.
group-Farnell24-IOT-Team 0:766454e296c3 6 * You may obtain a copy of the License at
group-Farnell24-IOT-Team 0:766454e296c3 7 *
group-Farnell24-IOT-Team 0:766454e296c3 8 * http://www.apache.org/licenses/LICENSE-2.0
group-Farnell24-IOT-Team 0:766454e296c3 9 *
group-Farnell24-IOT-Team 0:766454e296c3 10 * Unless required by applicable law or agreed to in writing, software
group-Farnell24-IOT-Team 0:766454e296c3 11 * distributed under the License is distributed on an "AS IS" BASIS,
group-Farnell24-IOT-Team 0:766454e296c3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-Farnell24-IOT-Team 0:766454e296c3 13 * See the License for the specific language governing permissions and
group-Farnell24-IOT-Team 0:766454e296c3 14 * limitations under the License.
group-Farnell24-IOT-Team 0:766454e296c3 15 */
group-Farnell24-IOT-Team 0:766454e296c3 16
group-Farnell24-IOT-Team 0:766454e296c3 17 #include <string.h>
group-Farnell24-IOT-Team 0:766454e296c3 18 #include "ISM43362Interface.h"
group-Farnell24-IOT-Team 0:766454e296c3 19 #include "mbed_debug.h"
group-Farnell24-IOT-Team 0:766454e296c3 20
group-Farnell24-IOT-Team 0:766454e296c3 21 // ao activate / de-activate debug
group-Farnell24-IOT-Team 0:766454e296c3 22 #define ism_debug false
group-Farnell24-IOT-Team 0:766454e296c3 23
group-Farnell24-IOT-Team 0:766454e296c3 24 // Various timeouts for different ISM43362 operations
group-Farnell24-IOT-Team 0:766454e296c3 25 #define ISM43362_CONNECT_TIMEOUT 15000 /* milliseconds */
group-Farnell24-IOT-Team 0:766454e296c3 26 #define ISM43362_SEND_TIMEOUT 1000 /* milliseconds */
group-Farnell24-IOT-Team 0:766454e296c3 27 #define ISM43362_RECV_TIMEOUT 100 /* milliseconds */
group-Farnell24-IOT-Team 0:766454e296c3 28 #define ISM43362_MISC_TIMEOUT 100 /* milliseconds */
group-Farnell24-IOT-Team 0:766454e296c3 29
group-Farnell24-IOT-Team 0:766454e296c3 30 // Tested firmware versions
group-Farnell24-IOT-Team 0:766454e296c3 31 // Example of versions string returned by the module:
group-Farnell24-IOT-Team 0:766454e296c3 32 // "ISM43362-M3G-L44-SPI,C3.5.2.3.BETA9,v3.5.2,v1.4.0.rc1,v8.2.1,120000000,Inventek eS-WiFi"
group-Farnell24-IOT-Team 0:766454e296c3 33 // "ISM43362-M3G-L44-SPI,C3.5.2.2,v3.5.2,v1.4.0.rc1,v8.2.1,120000000,Inventek eS-WiFi"
group-Farnell24-IOT-Team 0:766454e296c3 34 // Only the first version is checked !
group-Farnell24-IOT-Team 0:766454e296c3 35 const char supported_fw_versions[2][15] = {"C3.5.2.3.BETA9", "C3.5.2.2"};
group-Farnell24-IOT-Team 0:766454e296c3 36
group-Farnell24-IOT-Team 0:766454e296c3 37 #define MIN(a,b) (((a)<(b))?(a):(b))
group-Farnell24-IOT-Team 0:766454e296c3 38
group-Farnell24-IOT-Team 0:766454e296c3 39 // ISM43362Interface implementation
group-Farnell24-IOT-Team 0:766454e296c3 40 ISM43362Interface::ISM43362Interface(PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset, PinName datareadypin, PinName wakeup, bool debug)
group-Farnell24-IOT-Team 0:766454e296c3 41 : _ism(mosi, miso, sclk, nss, reset, datareadypin, wakeup, debug)
group-Farnell24-IOT-Team 0:766454e296c3 42 {
group-Farnell24-IOT-Team 0:766454e296c3 43 memset(_ids, 0, sizeof(_ids));
group-Farnell24-IOT-Team 0:766454e296c3 44 memset(_socket_obj, 0, sizeof(_socket_obj));
group-Farnell24-IOT-Team 0:766454e296c3 45 memset(_cbs, 0, sizeof(_cbs));
group-Farnell24-IOT-Team 0:766454e296c3 46 thread_read_socket.start(callback(this, &ISM43362Interface::socket_check_read));
group-Farnell24-IOT-Team 0:766454e296c3 47 }
group-Farnell24-IOT-Team 0:766454e296c3 48
group-Farnell24-IOT-Team 0:766454e296c3 49 int ISM43362Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
group-Farnell24-IOT-Team 0:766454e296c3 50 uint8_t channel)
group-Farnell24-IOT-Team 0:766454e296c3 51 {
group-Farnell24-IOT-Team 0:766454e296c3 52 if (channel != 0) {
group-Farnell24-IOT-Team 0:766454e296c3 53 return NSAPI_ERROR_UNSUPPORTED;
group-Farnell24-IOT-Team 0:766454e296c3 54 }
group-Farnell24-IOT-Team 0:766454e296c3 55
group-Farnell24-IOT-Team 0:766454e296c3 56 set_credentials(ssid, pass, security);
group-Farnell24-IOT-Team 0:766454e296c3 57 return connect();
group-Farnell24-IOT-Team 0:766454e296c3 58 }
group-Farnell24-IOT-Team 0:766454e296c3 59
group-Farnell24-IOT-Team 0:766454e296c3 60 int ISM43362Interface::connect()
group-Farnell24-IOT-Team 0:766454e296c3 61 {
group-Farnell24-IOT-Team 0:766454e296c3 62 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 63 const char* read_version;
group-Farnell24-IOT-Team 0:766454e296c3 64
group-Farnell24-IOT-Team 0:766454e296c3 65 _ism.setTimeout(ISM43362_MISC_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 66
group-Farnell24-IOT-Team 0:766454e296c3 67 // Check all supported firmware versions
group-Farnell24-IOT-Team 0:766454e296c3 68 read_version = _ism.get_firmware_version();
group-Farnell24-IOT-Team 0:766454e296c3 69
group-Farnell24-IOT-Team 0:766454e296c3 70 if (!read_version) {
group-Farnell24-IOT-Team 0:766454e296c3 71 debug_if(ism_debug, "ISM43362: ERROR cannot read firmware version\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 72 return NSAPI_ERROR_DEVICE_ERROR;
group-Farnell24-IOT-Team 0:766454e296c3 73 }
group-Farnell24-IOT-Team 0:766454e296c3 74 debug_if(ism_debug, "ISM43362: read_version = [%s]\r\n", read_version);
group-Farnell24-IOT-Team 0:766454e296c3 75
group-Farnell24-IOT-Team 0:766454e296c3 76 if ((strcmp(read_version, supported_fw_versions[0]) == 0) || (strcmp(read_version, supported_fw_versions[1]) == 0)) {
group-Farnell24-IOT-Team 0:766454e296c3 77 debug_if(ism_debug, "ISM43362: firmware version is OK\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 78 } else {
group-Farnell24-IOT-Team 0:766454e296c3 79 debug_if(ism_debug, "ISM43362: WARNING this firmware version has not been tested !\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 80 }
group-Farnell24-IOT-Team 0:766454e296c3 81
group-Farnell24-IOT-Team 0:766454e296c3 82 if (!_ism.dhcp(true)) {
group-Farnell24-IOT-Team 0:766454e296c3 83 return NSAPI_ERROR_DHCP_FAILURE;
group-Farnell24-IOT-Team 0:766454e296c3 84 }
group-Farnell24-IOT-Team 0:766454e296c3 85
group-Farnell24-IOT-Team 0:766454e296c3 86 _ism.setTimeout(ISM43362_CONNECT_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 87
group-Farnell24-IOT-Team 0:766454e296c3 88 if (!_ism.connect(ap_ssid, ap_pass)) {
group-Farnell24-IOT-Team 0:766454e296c3 89 return NSAPI_ERROR_NO_CONNECTION;
group-Farnell24-IOT-Team 0:766454e296c3 90 }
group-Farnell24-IOT-Team 0:766454e296c3 91
group-Farnell24-IOT-Team 0:766454e296c3 92 _ism.setTimeout(ISM43362_MISC_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 93 if (!_ism.getIPAddress()) {
group-Farnell24-IOT-Team 0:766454e296c3 94 return NSAPI_ERROR_DHCP_FAILURE;
group-Farnell24-IOT-Team 0:766454e296c3 95 }
group-Farnell24-IOT-Team 0:766454e296c3 96
group-Farnell24-IOT-Team 0:766454e296c3 97 _ism.setTimeout(ISM43362_MISC_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 98 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 99
group-Farnell24-IOT-Team 0:766454e296c3 100 return NSAPI_ERROR_OK;
group-Farnell24-IOT-Team 0:766454e296c3 101 }
group-Farnell24-IOT-Team 0:766454e296c3 102
group-Farnell24-IOT-Team 0:766454e296c3 103 nsapi_error_t ISM43362Interface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
group-Farnell24-IOT-Team 0:766454e296c3 104 {
group-Farnell24-IOT-Team 0:766454e296c3 105 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 106 if (address->set_ip_address(name)) {
group-Farnell24-IOT-Team 0:766454e296c3 107 if (version != NSAPI_UNSPEC && address->get_ip_version() != version) {
group-Farnell24-IOT-Team 0:766454e296c3 108 return NSAPI_ERROR_DNS_FAILURE;
group-Farnell24-IOT-Team 0:766454e296c3 109 }
group-Farnell24-IOT-Team 0:766454e296c3 110
group-Farnell24-IOT-Team 0:766454e296c3 111 return NSAPI_ERROR_OK;
group-Farnell24-IOT-Team 0:766454e296c3 112 }
group-Farnell24-IOT-Team 0:766454e296c3 113
group-Farnell24-IOT-Team 0:766454e296c3 114 char *ipbuff = new char[NSAPI_IP_SIZE];
group-Farnell24-IOT-Team 0:766454e296c3 115 int ret = 0;
group-Farnell24-IOT-Team 0:766454e296c3 116 _ism.setTimeout(ISM43362_CONNECT_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 117 if(!_ism.dns_lookup(name, ipbuff)) {
group-Farnell24-IOT-Team 0:766454e296c3 118 ret = NSAPI_ERROR_DEVICE_ERROR;
group-Farnell24-IOT-Team 0:766454e296c3 119 } else {
group-Farnell24-IOT-Team 0:766454e296c3 120 address->set_ip_address(ipbuff);
group-Farnell24-IOT-Team 0:766454e296c3 121 }
group-Farnell24-IOT-Team 0:766454e296c3 122 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 123
group-Farnell24-IOT-Team 0:766454e296c3 124 delete[] ipbuff;
group-Farnell24-IOT-Team 0:766454e296c3 125
group-Farnell24-IOT-Team 0:766454e296c3 126 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 127 }
group-Farnell24-IOT-Team 0:766454e296c3 128
group-Farnell24-IOT-Team 0:766454e296c3 129 int ISM43362Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
group-Farnell24-IOT-Team 0:766454e296c3 130 {
group-Farnell24-IOT-Team 0:766454e296c3 131 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 132
group-Farnell24-IOT-Team 0:766454e296c3 133 memset(ap_ssid, 0, sizeof(ap_ssid));
group-Farnell24-IOT-Team 0:766454e296c3 134 strncpy(ap_ssid, ssid, sizeof(ap_ssid));
group-Farnell24-IOT-Team 0:766454e296c3 135
group-Farnell24-IOT-Team 0:766454e296c3 136 memset(ap_pass, 0, sizeof(ap_pass));
group-Farnell24-IOT-Team 0:766454e296c3 137 strncpy(ap_pass, pass, sizeof(ap_pass));
group-Farnell24-IOT-Team 0:766454e296c3 138
group-Farnell24-IOT-Team 0:766454e296c3 139 ap_sec = security;
group-Farnell24-IOT-Team 0:766454e296c3 140 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 141
group-Farnell24-IOT-Team 0:766454e296c3 142 return 0;
group-Farnell24-IOT-Team 0:766454e296c3 143 }
group-Farnell24-IOT-Team 0:766454e296c3 144
group-Farnell24-IOT-Team 0:766454e296c3 145 int ISM43362Interface::set_channel(uint8_t channel)
group-Farnell24-IOT-Team 0:766454e296c3 146 {
group-Farnell24-IOT-Team 0:766454e296c3 147 return NSAPI_ERROR_UNSUPPORTED;
group-Farnell24-IOT-Team 0:766454e296c3 148 }
group-Farnell24-IOT-Team 0:766454e296c3 149
group-Farnell24-IOT-Team 0:766454e296c3 150 int ISM43362Interface::disconnect()
group-Farnell24-IOT-Team 0:766454e296c3 151 {
group-Farnell24-IOT-Team 0:766454e296c3 152 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 153
group-Farnell24-IOT-Team 0:766454e296c3 154 _ism.setTimeout(ISM43362_MISC_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 155
group-Farnell24-IOT-Team 0:766454e296c3 156 if (!_ism.disconnect()) {
group-Farnell24-IOT-Team 0:766454e296c3 157 return NSAPI_ERROR_DEVICE_ERROR;
group-Farnell24-IOT-Team 0:766454e296c3 158 }
group-Farnell24-IOT-Team 0:766454e296c3 159
group-Farnell24-IOT-Team 0:766454e296c3 160 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 161
group-Farnell24-IOT-Team 0:766454e296c3 162 return NSAPI_ERROR_OK;
group-Farnell24-IOT-Team 0:766454e296c3 163 }
group-Farnell24-IOT-Team 0:766454e296c3 164
group-Farnell24-IOT-Team 0:766454e296c3 165 const char *ISM43362Interface::get_ip_address()
group-Farnell24-IOT-Team 0:766454e296c3 166 {
group-Farnell24-IOT-Team 0:766454e296c3 167 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 168 const char *ret = _ism.getIPAddress();
group-Farnell24-IOT-Team 0:766454e296c3 169 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 170 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 171 }
group-Farnell24-IOT-Team 0:766454e296c3 172
group-Farnell24-IOT-Team 0:766454e296c3 173 const char *ISM43362Interface::get_mac_address()
group-Farnell24-IOT-Team 0:766454e296c3 174 {
group-Farnell24-IOT-Team 0:766454e296c3 175 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 176 const char *ret = _ism.getMACAddress();
group-Farnell24-IOT-Team 0:766454e296c3 177 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 178 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 179 }
group-Farnell24-IOT-Team 0:766454e296c3 180
group-Farnell24-IOT-Team 0:766454e296c3 181 const char *ISM43362Interface::get_gateway()
group-Farnell24-IOT-Team 0:766454e296c3 182 {
group-Farnell24-IOT-Team 0:766454e296c3 183 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 184 const char *ret = _ism.getGateway();
group-Farnell24-IOT-Team 0:766454e296c3 185 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 186 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 187 }
group-Farnell24-IOT-Team 0:766454e296c3 188
group-Farnell24-IOT-Team 0:766454e296c3 189 const char *ISM43362Interface::get_netmask()
group-Farnell24-IOT-Team 0:766454e296c3 190 {
group-Farnell24-IOT-Team 0:766454e296c3 191 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 192 const char *ret = _ism.getNetmask();
group-Farnell24-IOT-Team 0:766454e296c3 193 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 194 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 195 }
group-Farnell24-IOT-Team 0:766454e296c3 196
group-Farnell24-IOT-Team 0:766454e296c3 197 int8_t ISM43362Interface::get_rssi()
group-Farnell24-IOT-Team 0:766454e296c3 198 {
group-Farnell24-IOT-Team 0:766454e296c3 199 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 200 int8_t ret = _ism.getRSSI();
group-Farnell24-IOT-Team 0:766454e296c3 201 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 202 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 203 }
group-Farnell24-IOT-Team 0:766454e296c3 204
group-Farnell24-IOT-Team 0:766454e296c3 205 int ISM43362Interface::scan(WiFiAccessPoint *res, unsigned count)
group-Farnell24-IOT-Team 0:766454e296c3 206 {
group-Farnell24-IOT-Team 0:766454e296c3 207 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 208 _ism.setTimeout(ISM43362_CONNECT_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 209 int ret = _ism.scan(res, count);
group-Farnell24-IOT-Team 0:766454e296c3 210 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 211 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 212 }
group-Farnell24-IOT-Team 0:766454e296c3 213
group-Farnell24-IOT-Team 0:766454e296c3 214 struct ISM43362_socket {
group-Farnell24-IOT-Team 0:766454e296c3 215 int id;
group-Farnell24-IOT-Team 0:766454e296c3 216 nsapi_protocol_t proto;
group-Farnell24-IOT-Team 0:766454e296c3 217 volatile bool connected;
group-Farnell24-IOT-Team 0:766454e296c3 218 SocketAddress addr;
group-Farnell24-IOT-Team 0:766454e296c3 219 char read_data[1400];
group-Farnell24-IOT-Team 0:766454e296c3 220 volatile uint32_t read_data_size;
group-Farnell24-IOT-Team 0:766454e296c3 221 };
group-Farnell24-IOT-Team 0:766454e296c3 222
group-Farnell24-IOT-Team 0:766454e296c3 223 int ISM43362Interface::socket_open(void **handle, nsapi_protocol_t proto)
group-Farnell24-IOT-Team 0:766454e296c3 224 {
group-Farnell24-IOT-Team 0:766454e296c3 225 // Look for an unused socket
group-Farnell24-IOT-Team 0:766454e296c3 226 int id = -1;
group-Farnell24-IOT-Team 0:766454e296c3 227 for (int i = 0; i < ISM43362_SOCKET_COUNT; i++) {
group-Farnell24-IOT-Team 0:766454e296c3 228 if (!_ids[i]) {
group-Farnell24-IOT-Team 0:766454e296c3 229 id = i;
group-Farnell24-IOT-Team 0:766454e296c3 230 _ids[i] = true;
group-Farnell24-IOT-Team 0:766454e296c3 231 break;
group-Farnell24-IOT-Team 0:766454e296c3 232 }
group-Farnell24-IOT-Team 0:766454e296c3 233 }
group-Farnell24-IOT-Team 0:766454e296c3 234
group-Farnell24-IOT-Team 0:766454e296c3 235 if (id == -1) {
group-Farnell24-IOT-Team 0:766454e296c3 236 return NSAPI_ERROR_NO_SOCKET;
group-Farnell24-IOT-Team 0:766454e296c3 237 }
group-Farnell24-IOT-Team 0:766454e296c3 238 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 239 struct ISM43362_socket *socket = new struct ISM43362_socket;
group-Farnell24-IOT-Team 0:766454e296c3 240 if (!socket) {
group-Farnell24-IOT-Team 0:766454e296c3 241 return NSAPI_ERROR_NO_SOCKET;
group-Farnell24-IOT-Team 0:766454e296c3 242 }
group-Farnell24-IOT-Team 0:766454e296c3 243 socket->id = id;
group-Farnell24-IOT-Team 0:766454e296c3 244 debug_if(ism_debug, "socket_open, id=%d", socket->id);
group-Farnell24-IOT-Team 0:766454e296c3 245 memset(socket->read_data, 0, sizeof(socket->read_data));
group-Farnell24-IOT-Team 0:766454e296c3 246 socket->addr = 0;
group-Farnell24-IOT-Team 0:766454e296c3 247 socket->read_data_size = 0;
group-Farnell24-IOT-Team 0:766454e296c3 248 socket->proto = proto;
group-Farnell24-IOT-Team 0:766454e296c3 249 socket->connected = false;
group-Farnell24-IOT-Team 0:766454e296c3 250 *handle = socket;
group-Farnell24-IOT-Team 0:766454e296c3 251 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 252
group-Farnell24-IOT-Team 0:766454e296c3 253 return 0;
group-Farnell24-IOT-Team 0:766454e296c3 254 }
group-Farnell24-IOT-Team 0:766454e296c3 255
group-Farnell24-IOT-Team 0:766454e296c3 256 int ISM43362Interface::socket_close(void *handle)
group-Farnell24-IOT-Team 0:766454e296c3 257 {
group-Farnell24-IOT-Team 0:766454e296c3 258 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 259 struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
group-Farnell24-IOT-Team 0:766454e296c3 260 debug_if(ism_debug, "socket_close, id=%d", socket->id);
group-Farnell24-IOT-Team 0:766454e296c3 261 int err = 0;
group-Farnell24-IOT-Team 0:766454e296c3 262 _ism.setTimeout(ISM43362_MISC_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 263
group-Farnell24-IOT-Team 0:766454e296c3 264 if (!_ism.close(socket->id)) {
group-Farnell24-IOT-Team 0:766454e296c3 265 err = NSAPI_ERROR_DEVICE_ERROR;
group-Farnell24-IOT-Team 0:766454e296c3 266 }
group-Farnell24-IOT-Team 0:766454e296c3 267
group-Farnell24-IOT-Team 0:766454e296c3 268 socket->connected = false;
group-Farnell24-IOT-Team 0:766454e296c3 269 _ids[socket->id] = false;
group-Farnell24-IOT-Team 0:766454e296c3 270 _socket_obj[socket->id] = 0;
group-Farnell24-IOT-Team 0:766454e296c3 271 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 272 delete socket;
group-Farnell24-IOT-Team 0:766454e296c3 273 return err;
group-Farnell24-IOT-Team 0:766454e296c3 274 }
group-Farnell24-IOT-Team 0:766454e296c3 275
group-Farnell24-IOT-Team 0:766454e296c3 276 int ISM43362Interface::socket_bind(void *handle, const SocketAddress &address)
group-Farnell24-IOT-Team 0:766454e296c3 277 {
group-Farnell24-IOT-Team 0:766454e296c3 278 return NSAPI_ERROR_UNSUPPORTED;
group-Farnell24-IOT-Team 0:766454e296c3 279 }
group-Farnell24-IOT-Team 0:766454e296c3 280
group-Farnell24-IOT-Team 0:766454e296c3 281 int ISM43362Interface::socket_listen(void *handle, int backlog)
group-Farnell24-IOT-Team 0:766454e296c3 282 {
group-Farnell24-IOT-Team 0:766454e296c3 283 return NSAPI_ERROR_UNSUPPORTED;
group-Farnell24-IOT-Team 0:766454e296c3 284 }
group-Farnell24-IOT-Team 0:766454e296c3 285
group-Farnell24-IOT-Team 0:766454e296c3 286 int ISM43362Interface::socket_connect(void *handle, const SocketAddress &addr)
group-Farnell24-IOT-Team 0:766454e296c3 287 {
group-Farnell24-IOT-Team 0:766454e296c3 288 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 289 int ret = socket_connect_nolock(handle, addr);
group-Farnell24-IOT-Team 0:766454e296c3 290 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 291 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 292 }
group-Farnell24-IOT-Team 0:766454e296c3 293
group-Farnell24-IOT-Team 0:766454e296c3 294 int ISM43362Interface::socket_connect_nolock(void *handle, const SocketAddress &addr)
group-Farnell24-IOT-Team 0:766454e296c3 295 {
group-Farnell24-IOT-Team 0:766454e296c3 296 struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
group-Farnell24-IOT-Team 0:766454e296c3 297 _ism.setTimeout(ISM43362_CONNECT_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 298 const char *proto = (socket->proto == NSAPI_UDP) ? "1" : "0";
group-Farnell24-IOT-Team 0:766454e296c3 299 if (!_ism.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) {
group-Farnell24-IOT-Team 0:766454e296c3 300 return NSAPI_ERROR_DEVICE_ERROR;
group-Farnell24-IOT-Team 0:766454e296c3 301 }
group-Farnell24-IOT-Team 0:766454e296c3 302 _ids[socket->id] = true;
group-Farnell24-IOT-Team 0:766454e296c3 303 _socket_obj[socket->id] = (uint32_t)socket;
group-Farnell24-IOT-Team 0:766454e296c3 304 socket->connected = true;
group-Farnell24-IOT-Team 0:766454e296c3 305 return 0;
group-Farnell24-IOT-Team 0:766454e296c3 306
group-Farnell24-IOT-Team 0:766454e296c3 307 }
group-Farnell24-IOT-Team 0:766454e296c3 308
group-Farnell24-IOT-Team 0:766454e296c3 309
group-Farnell24-IOT-Team 0:766454e296c3 310
group-Farnell24-IOT-Team 0:766454e296c3 311 void ISM43362Interface::socket_check_read()
group-Farnell24-IOT-Team 0:766454e296c3 312 {
group-Farnell24-IOT-Team 0:766454e296c3 313 while (1) {
group-Farnell24-IOT-Team 0:766454e296c3 314 for (int i = 0; i < ISM43362_SOCKET_COUNT; i++) {
group-Farnell24-IOT-Team 0:766454e296c3 315 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 316 if (_socket_obj[i] != 0) {
group-Farnell24-IOT-Team 0:766454e296c3 317 struct ISM43362_socket *socket = (struct ISM43362_socket *)_socket_obj[i];
group-Farnell24-IOT-Team 0:766454e296c3 318 /* Check if there is something to read for this socket. But if it */
group-Farnell24-IOT-Team 0:766454e296c3 319 /* has already been read : don't read again */
group-Farnell24-IOT-Team 0:766454e296c3 320 if ((socket->connected) && (socket->read_data_size == 0) && _cbs[socket->id].callback) {
group-Farnell24-IOT-Team 0:766454e296c3 321 _ism.setTimeout(1);
group-Farnell24-IOT-Team 0:766454e296c3 322 /* if no callback is set, no need to read ?*/
group-Farnell24-IOT-Team 0:766454e296c3 323 int read_amount = _ism.check_recv_status(socket->id, socket->read_data);
group-Farnell24-IOT-Team 0:766454e296c3 324 if (read_amount > 0) {
group-Farnell24-IOT-Team 0:766454e296c3 325 socket->read_data_size = read_amount;
group-Farnell24-IOT-Team 0:766454e296c3 326 } else if (read_amount < 0) {
group-Farnell24-IOT-Team 0:766454e296c3 327 /* Mark donw connection has been lost or closed */
group-Farnell24-IOT-Team 0:766454e296c3 328 socket->connected = false;
group-Farnell24-IOT-Team 0:766454e296c3 329 }
group-Farnell24-IOT-Team 0:766454e296c3 330 if (read_amount != 0) {
group-Farnell24-IOT-Team 0:766454e296c3 331 /* There is something to read in this socket*/
group-Farnell24-IOT-Team 0:766454e296c3 332 if (_cbs[socket->id].callback) {
group-Farnell24-IOT-Team 0:766454e296c3 333 _cbs[socket->id].callback(_cbs[socket->id].data);
group-Farnell24-IOT-Team 0:766454e296c3 334 }
group-Farnell24-IOT-Team 0:766454e296c3 335 }
group-Farnell24-IOT-Team 0:766454e296c3 336 }
group-Farnell24-IOT-Team 0:766454e296c3 337 }
group-Farnell24-IOT-Team 0:766454e296c3 338 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 339 }
group-Farnell24-IOT-Team 0:766454e296c3 340 wait_ms(50);
group-Farnell24-IOT-Team 0:766454e296c3 341 }
group-Farnell24-IOT-Team 0:766454e296c3 342 }
group-Farnell24-IOT-Team 0:766454e296c3 343
group-Farnell24-IOT-Team 0:766454e296c3 344 int ISM43362Interface::socket_accept(void *server, void **socket, SocketAddress *addr)
group-Farnell24-IOT-Team 0:766454e296c3 345 {
group-Farnell24-IOT-Team 0:766454e296c3 346 return NSAPI_ERROR_UNSUPPORTED;
group-Farnell24-IOT-Team 0:766454e296c3 347 }
group-Farnell24-IOT-Team 0:766454e296c3 348
group-Farnell24-IOT-Team 0:766454e296c3 349 int ISM43362Interface::socket_send(void *handle, const void *data, unsigned size)
group-Farnell24-IOT-Team 0:766454e296c3 350 {
group-Farnell24-IOT-Team 0:766454e296c3 351 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 352 int ret = socket_send_nolock(handle, data, size);
group-Farnell24-IOT-Team 0:766454e296c3 353 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 354 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 355 }
group-Farnell24-IOT-Team 0:766454e296c3 356
group-Farnell24-IOT-Team 0:766454e296c3 357 /* CAREFULL LOCK must be taken before callling this function */
group-Farnell24-IOT-Team 0:766454e296c3 358 int ISM43362Interface::socket_send_nolock(void *handle, const void *data, unsigned size)
group-Farnell24-IOT-Team 0:766454e296c3 359 {
group-Farnell24-IOT-Team 0:766454e296c3 360 struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
group-Farnell24-IOT-Team 0:766454e296c3 361 _ism.setTimeout(ISM43362_SEND_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 362
group-Farnell24-IOT-Team 0:766454e296c3 363 if (size > ES_WIFI_MAX_TX_PACKET_SIZE) {
group-Farnell24-IOT-Team 0:766454e296c3 364 size = ES_WIFI_MAX_TX_PACKET_SIZE;
group-Farnell24-IOT-Team 0:766454e296c3 365 }
group-Farnell24-IOT-Team 0:766454e296c3 366
group-Farnell24-IOT-Team 0:766454e296c3 367 if (!_ism.send(socket->id, data, size)) {
group-Farnell24-IOT-Team 0:766454e296c3 368 debug_if(ism_debug, "socket_send ERROR\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 369 return NSAPI_ERROR_DEVICE_ERROR; // or WOULD_BLOCK ?
group-Farnell24-IOT-Team 0:766454e296c3 370 }
group-Farnell24-IOT-Team 0:766454e296c3 371
group-Farnell24-IOT-Team 0:766454e296c3 372 return size;
group-Farnell24-IOT-Team 0:766454e296c3 373 }
group-Farnell24-IOT-Team 0:766454e296c3 374
group-Farnell24-IOT-Team 0:766454e296c3 375 int ISM43362Interface::socket_recv(void *handle, void *data, unsigned size)
group-Farnell24-IOT-Team 0:766454e296c3 376 {
group-Farnell24-IOT-Team 0:766454e296c3 377 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 378 unsigned recv = 0;
group-Farnell24-IOT-Team 0:766454e296c3 379 struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
group-Farnell24-IOT-Team 0:766454e296c3 380 char *ptr = (char *)data;
group-Farnell24-IOT-Team 0:766454e296c3 381
group-Farnell24-IOT-Team 0:766454e296c3 382 debug_if(ism_debug, "[socket_recv] req=%d\r\n", size);
group-Farnell24-IOT-Team 0:766454e296c3 383
group-Farnell24-IOT-Team 0:766454e296c3 384 if (!socket->connected) {
group-Farnell24-IOT-Team 0:766454e296c3 385 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 386 return NSAPI_ERROR_CONNECTION_LOST;
group-Farnell24-IOT-Team 0:766454e296c3 387 }
group-Farnell24-IOT-Team 0:766454e296c3 388
group-Farnell24-IOT-Team 0:766454e296c3 389 _ism.setTimeout(ISM43362_RECV_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 390
group-Farnell24-IOT-Team 0:766454e296c3 391 if (socket->read_data_size == 0) {
group-Farnell24-IOT-Team 0:766454e296c3 392 /* if no callback is set, no need to read ?*/
group-Farnell24-IOT-Team 0:766454e296c3 393 int read_amount = _ism.check_recv_status(socket->id, socket->read_data);
group-Farnell24-IOT-Team 0:766454e296c3 394 if (read_amount > 0) {
group-Farnell24-IOT-Team 0:766454e296c3 395 socket->read_data_size = read_amount;
group-Farnell24-IOT-Team 0:766454e296c3 396 } else if (read_amount < 0) {
group-Farnell24-IOT-Team 0:766454e296c3 397 socket->connected = false;
group-Farnell24-IOT-Team 0:766454e296c3 398 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 399 return NSAPI_ERROR_CONNECTION_LOST;
group-Farnell24-IOT-Team 0:766454e296c3 400 }
group-Farnell24-IOT-Team 0:766454e296c3 401 }
group-Farnell24-IOT-Team 0:766454e296c3 402
group-Farnell24-IOT-Team 0:766454e296c3 403 if (socket->read_data_size != 0) {
group-Farnell24-IOT-Team 0:766454e296c3 404 debug_if(ism_debug, "read_data_size=%d\r\n", socket->read_data_size);
group-Farnell24-IOT-Team 0:766454e296c3 405 uint32_t i=0;
group-Farnell24-IOT-Team 0:766454e296c3 406 while ((i < socket->read_data_size) && (i < size)) {
group-Farnell24-IOT-Team 0:766454e296c3 407 *ptr++ = socket->read_data[i];
group-Farnell24-IOT-Team 0:766454e296c3 408 i++;
group-Farnell24-IOT-Team 0:766454e296c3 409 }
group-Farnell24-IOT-Team 0:766454e296c3 410
group-Farnell24-IOT-Team 0:766454e296c3 411 debug_if(ism_debug, "Copied i bytes=%d, vs %d requestd\r\n", i, size);
group-Farnell24-IOT-Team 0:766454e296c3 412 recv += i;
group-Farnell24-IOT-Team 0:766454e296c3 413
group-Farnell24-IOT-Team 0:766454e296c3 414 if (i >= socket->read_data_size) {
group-Farnell24-IOT-Team 0:766454e296c3 415 /* All the storeed data has been read, reset buffer */
group-Farnell24-IOT-Team 0:766454e296c3 416 memset(socket->read_data, 0, sizeof(socket->read_data));
group-Farnell24-IOT-Team 0:766454e296c3 417 socket->read_data_size = 0;
group-Farnell24-IOT-Team 0:766454e296c3 418 debug_if(ism_debug, "Socket_recv buffer reset\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 419 } else {
group-Farnell24-IOT-Team 0:766454e296c3 420 /* In case there is remaining data in buffer, update socket content
group-Farnell24-IOT-Team 0:766454e296c3 421 * For now by shift copy of all data (not very efficient to be
group-Farnell24-IOT-Team 0:766454e296c3 422 * revised */
group-Farnell24-IOT-Team 0:766454e296c3 423 while (i < socket->read_data_size) {
group-Farnell24-IOT-Team 0:766454e296c3 424 socket->read_data[i - size] = socket->read_data[i];
group-Farnell24-IOT-Team 0:766454e296c3 425 i++;
group-Farnell24-IOT-Team 0:766454e296c3 426 }
group-Farnell24-IOT-Team 0:766454e296c3 427
group-Farnell24-IOT-Team 0:766454e296c3 428 socket->read_data_size -= size;
group-Farnell24-IOT-Team 0:766454e296c3 429 }
group-Farnell24-IOT-Team 0:766454e296c3 430 } else {
group-Farnell24-IOT-Team 0:766454e296c3 431 debug_if(ism_debug, "Nothing in buffer\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 432 }
group-Farnell24-IOT-Team 0:766454e296c3 433
group-Farnell24-IOT-Team 0:766454e296c3 434 debug_if(ism_debug, "[socket_recv]read_datasize=%d, recv=%d\r\n", socket->read_data_size, recv);
group-Farnell24-IOT-Team 0:766454e296c3 435 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 436
group-Farnell24-IOT-Team 0:766454e296c3 437 if (recv > 0) {
group-Farnell24-IOT-Team 0:766454e296c3 438 return recv;
group-Farnell24-IOT-Team 0:766454e296c3 439 } else {
group-Farnell24-IOT-Team 0:766454e296c3 440 debug_if(ism_debug, "sock_recv returns WOULD BLOCK\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 441 return NSAPI_ERROR_WOULD_BLOCK;
group-Farnell24-IOT-Team 0:766454e296c3 442 }
group-Farnell24-IOT-Team 0:766454e296c3 443 }
group-Farnell24-IOT-Team 0:766454e296c3 444
group-Farnell24-IOT-Team 0:766454e296c3 445 int ISM43362Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
group-Farnell24-IOT-Team 0:766454e296c3 446 {
group-Farnell24-IOT-Team 0:766454e296c3 447 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 448 struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
group-Farnell24-IOT-Team 0:766454e296c3 449
group-Farnell24-IOT-Team 0:766454e296c3 450 if (socket->connected && socket->addr != addr) {
group-Farnell24-IOT-Team 0:766454e296c3 451 _ism.setTimeout(ISM43362_MISC_TIMEOUT);
group-Farnell24-IOT-Team 0:766454e296c3 452 if (!_ism.close(socket->id)) {
group-Farnell24-IOT-Team 0:766454e296c3 453 debug_if(ism_debug, "socket_send ERROR\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 454 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 455 return NSAPI_ERROR_DEVICE_ERROR;
group-Farnell24-IOT-Team 0:766454e296c3 456 }
group-Farnell24-IOT-Team 0:766454e296c3 457 socket->connected = false;
group-Farnell24-IOT-Team 0:766454e296c3 458 _ids[socket->id] = false;
group-Farnell24-IOT-Team 0:766454e296c3 459 _socket_obj[socket->id] = 0;
group-Farnell24-IOT-Team 0:766454e296c3 460 }
group-Farnell24-IOT-Team 0:766454e296c3 461
group-Farnell24-IOT-Team 0:766454e296c3 462 if (!socket->connected) {
group-Farnell24-IOT-Team 0:766454e296c3 463 int err = socket_connect_nolock(socket, addr);
group-Farnell24-IOT-Team 0:766454e296c3 464 if (err < 0) {
group-Farnell24-IOT-Team 0:766454e296c3 465 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 466 return err;
group-Farnell24-IOT-Team 0:766454e296c3 467 }
group-Farnell24-IOT-Team 0:766454e296c3 468 socket->addr = addr;
group-Farnell24-IOT-Team 0:766454e296c3 469 }
group-Farnell24-IOT-Team 0:766454e296c3 470
group-Farnell24-IOT-Team 0:766454e296c3 471 int ret = socket_send_nolock(socket, data, size);
group-Farnell24-IOT-Team 0:766454e296c3 472
group-Farnell24-IOT-Team 0:766454e296c3 473 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 474
group-Farnell24-IOT-Team 0:766454e296c3 475 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 476 }
group-Farnell24-IOT-Team 0:766454e296c3 477
group-Farnell24-IOT-Team 0:766454e296c3 478 int ISM43362Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
group-Farnell24-IOT-Team 0:766454e296c3 479 {
group-Farnell24-IOT-Team 0:766454e296c3 480 int ret = socket_recv(handle, data, size);
group-Farnell24-IOT-Team 0:766454e296c3 481 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 482 if ((ret >= 0) && addr) {
group-Farnell24-IOT-Team 0:766454e296c3 483 struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
group-Farnell24-IOT-Team 0:766454e296c3 484 *addr = socket->addr;
group-Farnell24-IOT-Team 0:766454e296c3 485 }
group-Farnell24-IOT-Team 0:766454e296c3 486 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 487 return ret;
group-Farnell24-IOT-Team 0:766454e296c3 488 }
group-Farnell24-IOT-Team 0:766454e296c3 489
group-Farnell24-IOT-Team 0:766454e296c3 490 void ISM43362Interface::socket_attach(void *handle, void (*cb)(void *), void *data)
group-Farnell24-IOT-Team 0:766454e296c3 491 {
group-Farnell24-IOT-Team 0:766454e296c3 492 _mutex.lock();
group-Farnell24-IOT-Team 0:766454e296c3 493 struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
group-Farnell24-IOT-Team 0:766454e296c3 494 _cbs[socket->id].callback = cb;
group-Farnell24-IOT-Team 0:766454e296c3 495 _cbs[socket->id].data = data;
group-Farnell24-IOT-Team 0:766454e296c3 496 _mutex.unlock();
group-Farnell24-IOT-Team 0:766454e296c3 497 }
group-Farnell24-IOT-Team 0:766454e296c3 498
group-Farnell24-IOT-Team 0:766454e296c3 499 void ISM43362Interface::event() {
group-Farnell24-IOT-Team 0:766454e296c3 500 for (int i = 0; i < ISM43362_SOCKET_COUNT; i++) {
group-Farnell24-IOT-Team 0:766454e296c3 501 if (_cbs[i].callback) {
group-Farnell24-IOT-Team 0:766454e296c3 502 _cbs[i].callback(_cbs[i].data);
group-Farnell24-IOT-Team 0:766454e296c3 503 }
group-Farnell24-IOT-Team 0:766454e296c3 504 }
group-Farnell24-IOT-Team 0:766454e296c3 505 }