forked from ARMmbed/wifi-ism43362
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ISM43362Interface.cpp
815 lines (690 loc) · 23.2 KB
/
ISM43362Interface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/* ISM43362 implementation of NetworkInterfaceAPI
* Copyright (c) STMicroelectronics 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#include <inttypes.h>
#include "ISM43362Interface.h"
#include "mbed_debug.h"
#if MBED_CONF_ISM43362_READ_THREAD_STACK_SIZE != 0
#if MBED_CONF_ISM43362_READ_THREAD_STACK_STATICALLY_ALLOCATED == 1
static uint8_t ism_wifi_thread_stack[MBED_CONF_ISM43362_READ_THREAD_STACK_SIZE];
#else
static uint8_t *ism_wifi_thread_stack = NULL;
#endif // MBED_CONF_ISM43362_READ_THREAD_STACK_STATICALLY_ALLOCATED == 1
#endif // MBED_CONF_ISM43362_READ_THREAD_STACK_SIZE != 0
// Product ID,FW Revision,API Revision,Stack Revision,RTOS Revision,CPU Clock,Product Name
#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
// Set 1 to force debug information
#define ism_interface_debug 0
#define MIN(a,b) (((a)<(b))?(a):(b))
#define ISM43362_WIFI_IF_NAME "is0"
// ISM43362Interface implementation
ISM43362Interface::ISM43362Interface(bool debug)
: _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),
#if MBED_CONF_ISM43362_READ_THREAD_STACK_SIZE != 0
thread_read_socket(osPriorityNormal, MBED_CONF_ISM43362_READ_THREAD_STACK_SIZE, ism_wifi_thread_stack, "ism43362"),
#endif
_conn_stat(NSAPI_STATUS_DISCONNECTED),
_conn_stat_cb(NULL)
{
_ism_debug = ism_interface_debug || debug;
memset(_ids, 0, sizeof(_ids));
memset(_socket_obj, 0, sizeof(_socket_obj));
_ism.attach(this, &ISM43362Interface::update_conn_state_cb);
memset(_cbs, 0, sizeof(_cbs));
memset(ap_ssid, 0, sizeof(ap_ssid));
memset(ap_pass, 0, sizeof(ap_pass));
ap_sec = ISM_SECURITY_UNKNOWN;
thread_read_socket.start(callback(this, &ISM43362Interface::socket_check_read));
_mutex.lock();
// Check all supported firmware versions
_FwVersion = _ism.get_firmware_version();
if (!_FwVersion) {
error("ISM43362Interface: ERROR cannot read firmware version\r\n");
}
debug_if(_ism_debug, "ISM43362Interface: read_version = %" PRIu32 "\r\n", _FwVersion);
/* FW Revision should be with format "CX.X.X.X" with X as a single digit */
if ((_FwVersion < 1000) || (_FwVersion > 9999)) {
debug_if(_ism_debug, "ISM43362Interface: read_version issue\r\n");
}
#if !TARGET_DISCO_F413ZH
if ((int32_t)_FwVersion < ParseNumber((char *)LATEST_FW_VERSION_NUMBER, NULL)) {
debug_if(_ism_debug, "ISM43362Interface: please update FW\r\n");
}
#endif
_connect_status = NSAPI_ERROR_NO_CONNECTION;
_mutex.unlock();
}
nsapi_error_t ISM43362Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
uint8_t channel)
{
if (channel != 0) {
return NSAPI_ERROR_UNSUPPORTED;
}
nsapi_error_t credentials_status = set_credentials(ssid, pass, security);
if (credentials_status) {
return credentials_status;
}
return connect();
}
nsapi_error_t ISM43362Interface::connect()
{
if (strlen(ap_ssid) == 0) {
return NSAPI_ERROR_NO_SSID;
}
_mutex.lock();
if (!_ism.dhcp(true)) {
_mutex.unlock();
return NSAPI_ERROR_DHCP_FAILURE;
}
_connect_status = _ism.connect(ap_ssid, ap_pass, ap_sec);
debug_if(_ism_debug, "ISM43362Interface: connect_status %d\n", _connect_status);
if (_connect_status != NSAPI_ERROR_OK) {
_mutex.unlock();
return _connect_status;
}
if (!_ism.getIPAddress()) {
_connect_status = NSAPI_ERROR_DHCP_FAILURE;
_mutex.unlock();
return NSAPI_ERROR_DHCP_FAILURE;
}
_mutex.unlock();
return NSAPI_ERROR_OK;
}
int ISM43362Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
{
if ((ssid == NULL) || (strlen(ssid) == 0) || (strlen(ssid) > 32)) {
return NSAPI_ERROR_PARAMETER;
}
if (security != NSAPI_SECURITY_NONE) {
if ((pass == NULL) || (strcmp(pass, "") == 0)) {
return NSAPI_ERROR_PARAMETER;
}
}
if (strlen(pass) > 63) {
return NSAPI_ERROR_PARAMETER;
}
_mutex.lock();
memset(ap_ssid, 0, sizeof(ap_ssid));
strncpy(ap_ssid, ssid, sizeof(ap_ssid));
memset(ap_pass, 0, sizeof(ap_pass));
strncpy(ap_pass, pass, sizeof(ap_pass));
switch (security) {
case NSAPI_SECURITY_NONE:
ap_sec = ISM_SECURITY_NONE;
break;
case NSAPI_SECURITY_WEP:
ap_sec = ISM_SECURITY_WEP;
break;
case NSAPI_SECURITY_WPA:
ap_sec = ISM_SECURITY_WPA;
break;
case NSAPI_SECURITY_WPA2:
ap_sec = ISM_SECURITY_WPA2;
break;
case NSAPI_SECURITY_WPA_WPA2:
ap_sec = ISM_SECURITY_WPA_WPA2;
break;
default:
ap_sec = ISM_SECURITY_UNKNOWN;
break;
}
_mutex.unlock();
return NSAPI_ERROR_OK;
}
int ISM43362Interface::set_channel(uint8_t channel)
{
return NSAPI_ERROR_UNSUPPORTED;
}
nsapi_error_t ISM43362Interface::disconnect()
{
_mutex.lock();
if (_connect_status != NSAPI_ERROR_OK) {
_mutex.unlock();
return NSAPI_ERROR_NO_CONNECTION;
}
if (!_ism.disconnect()) {
_mutex.unlock();
return NSAPI_ERROR_DEVICE_ERROR;
}
_connect_status = NSAPI_ERROR_NO_CONNECTION;
_mutex.unlock();
return NSAPI_ERROR_OK;
}
const char *ISM43362Interface::get_ip_address()
{
_mutex.lock();
const char *ret = _ism.getIPAddress();
_mutex.unlock();
return ret;
}
nsapi_error_t ISM43362Interface::get_ip_address(SocketAddress *address)
{
if (!address) {
return NSAPI_ERROR_PARAMETER;
}
_mutex.lock();
if (address->set_ip_address(_ism.getIPAddress())) {
_mutex.unlock();
return NSAPI_ERROR_OK;
}
_mutex.unlock();
return NSAPI_ERROR_NO_ADDRESS;
}
const char *ISM43362Interface::get_mac_address()
{
_mutex.lock();
const char *ret = _ism.getMACAddress();
_mutex.unlock();
return ret;
}
const char *ISM43362Interface::get_gateway()
{
_mutex.lock();
const char *ret = _ism.getGateway();
_mutex.unlock();
return ret;
}
nsapi_error_t ISM43362Interface::get_gateway(SocketAddress *address)
{
if (!address) {
return NSAPI_ERROR_PARAMETER;
}
_mutex.lock();
if (address->set_ip_address(_ism.getGateway())) {
_mutex.unlock();
return NSAPI_ERROR_OK;
}
_mutex.unlock();
return NSAPI_ERROR_NO_ADDRESS;
}
const char *ISM43362Interface::get_netmask()
{
_mutex.lock();
const char *ret = _ism.getNetmask();
_mutex.unlock();
return ret;
}
nsapi_error_t ISM43362Interface::get_netmask(SocketAddress *address)
{
if (!address) {
return NSAPI_ERROR_PARAMETER;
}
_mutex.lock();
if (address->set_ip_address(_ism.getNetmask())) {
_mutex.unlock();
return NSAPI_ERROR_OK;
}
_mutex.unlock();
return NSAPI_ERROR_NO_ADDRESS;
}
char *ISM43362Interface::get_interface_name(char *interface_name)
{
memcpy(interface_name, ISM43362_WIFI_IF_NAME, sizeof(ISM43362_WIFI_IF_NAME));
return interface_name;
}
int8_t ISM43362Interface::get_rssi()
{
_mutex.lock();
int8_t ret = _ism.getRSSI();
_mutex.unlock();
return ret;
}
int ISM43362Interface::scan(WiFiAccessPoint *res, unsigned count)
{
_mutex.lock();
int ret = _ism.scan(res, count);
_mutex.unlock();
return ret;
}
struct ISM43362_socket {
int id;
nsapi_protocol_t proto;
volatile bool connected;
char read_data[1400];
volatile uint32_t read_data_size;
};
int ISM43362Interface::socket_open(void **handle, nsapi_protocol_t proto)
{
int ret = NSAPI_ERROR_OK;
struct ISM43362_socket *socket;
_mutex.lock();
// Look for an unused socket
int id = -1;
for (int i = 0; i < ISM43362_SOCKET_COUNT; i++) {
if (!_ids[i]) {
id = i;
_ids[i] = true;
break;
}
}
if (id == -1) {
debug_if(_ism_debug, "ISM43362Interface: socket_open: no empty slot\n");
ret = NSAPI_ERROR_NO_SOCKET;
goto exit;
}
socket = new struct ISM43362_socket;
if (!socket) {
debug_if(_ism_debug, "ISM43362Interface: socket_open: create socket failed\n");
ret = NSAPI_ERROR_NO_SOCKET;
goto exit;
}
socket->id = id;
debug_if(_ism_debug, "ISM43362Interface: socket_open id=%d proto=%d\n", socket->id, proto);
memset(socket->read_data, 0, sizeof(socket->read_data));
socket->read_data_size = 0;
socket->proto = proto;
socket->connected = false;
*handle = socket;
ret = _ism.open(socket->id, (socket->proto == NSAPI_UDP) ? "1" : "0");
exit:
_mutex.unlock();
return ret;
}
int ISM43362Interface::socket_close(void *handle)
{
_mutex.lock();
struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
debug_if(_ism_debug, "ISM43362Interface: socket_close, id=%d\n", socket->id);
int err = 0;
if (!_ism.close(socket->id)) {
err = NSAPI_ERROR_DEVICE_ERROR;
}
socket->connected = false;
_ids[socket->id] = false;
_socket_obj[socket->id] = 0;
delete socket;
_mutex.unlock();
return err;
}
int ISM43362Interface::socket_bind(void *handle, const SocketAddress &address)
{
int ret = NSAPI_ERROR_OK;
struct ISM43362_socket *socket;
_mutex.lock();
socket = (struct ISM43362_socket *)handle;
debug_if(_ism_debug, "ISM43362Interface: socket_bind, id=%d address=%s port=%d\n", socket->id, address.get_ip_address(), address.get_port());
ret = _ism.bind(socket->id, address.get_ip_address(), address.get_port());
if (ret != NSAPI_ERROR_OK)
{
debug_if(_ism_debug, "ISM43362Interface: socket_bind: bind failed\n");
goto exit;
}
if (socket->proto == NSAPI_UDP)
{
ret = _ism.startServer(socket->id);
if (ret != NSAPI_ERROR_OK)
{
debug_if(_ism_debug, "ISM43362Interface: socket_bind: start UDP server failed\n");
goto exit;
}
_socket_obj[socket->id] = (uint32_t)socket;
}
exit:
_mutex.unlock();
return ret;
}
int ISM43362Interface::socket_listen(void *handle, int backlog)
{
int ret = NSAPI_ERROR_OK;
struct ISM43362_socket *socket;
_mutex.lock();
socket = (struct ISM43362_socket *)handle;
debug_if(_ism_debug, "ISM43362Interface: socket_listen, id=%d backlog=%d\n", socket->id, backlog);
ret = _ism.setServerParam(socket->id, backlog);
if (ret != NSAPI_ERROR_OK)
{
debug_if(_ism_debug, "ISM43362Interface: socket_bind: start UDP server failed\n");
goto exit;
}
ret = _ism.startServer(socket->id);
if (ret != NSAPI_ERROR_OK)
{
debug_if(_ism_debug, "ISM43362Interface: socket_listen: start TCP server failed\n");
goto exit;
}
_socket_obj[socket->id] = (uint32_t)socket;
exit:
_mutex.unlock();
return ret;
}
int ISM43362Interface::socket_connect_nolock(void *handle, const SocketAddress &addr)
{
struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
return _ism.setClientParam(socket->id, addr.get_ip_address(), addr.get_port());;
}
int ISM43362Interface::socket_connect(void *handle, const SocketAddress &addr)
{
int ret = NSAPI_ERROR_OK;
struct ISM43362_socket *socket;
_mutex.lock();
socket = (struct ISM43362_socket *)handle;
debug_if(_ism_debug, "ISM43362Interface: socket_connect, id=%d ip=%s port=%d\n", socket->id, addr.get_ip_address(), addr.get_port());
ret = socket_connect_nolock(handle, addr);
if (ret != NSAPI_ERROR_OK)
{
debug_if(_ism_debug, "ISM43362Interface: socket_connect: connect failed\n");
goto exit;
}
ret = _ism.startClient(socket->id);
if (ret != NSAPI_ERROR_OK)
{
debug_if(_ism_debug, "ISM43362Interface: socket_connect: start client failed\n");
goto exit;
}
socket->connected = true;
_socket_obj[socket->id] = (uint32_t)socket;
exit:
_mutex.unlock();
return ret;
}
void ISM43362Interface::socket_check_read()
{
while (1) {
if (_connect_status == NSAPI_ERROR_OK)
{
for (int i = 0; i < ISM43362_SOCKET_COUNT; i++) {
_mutex.lock();
if (_socket_obj[i] != 0) {
struct ISM43362_socket *socket = (struct ISM43362_socket *)_socket_obj[i];
/* Check if there is something to read for this socket. But if it */
/* has already been read : don't read again */
if ((socket->read_data_size == 0) && _cbs[socket->id].callback) {
/* if no callback is set, no need to read ?*/
// debug_if(_ism_debug, "ISM43362Interface socket_check_read: i %d\r\n", i);
int read_amount = _ism.check_recv_status(socket->id, socket->read_data);
if (read_amount > 0) {
socket->read_data_size = read_amount;
debug_if(_ism_debug, "ISM43362Interface socket_check_read read_amount %d\r\n", read_amount);
} else if (read_amount < 0) {
/* Mark donw connection has been lost or closed */
debug_if(_ism_debug, "ISM43362Interface socket_check_read: i %d closed\r\n", i);
socket->connected = false;
}
if (read_amount != 0) {
/* There is something to read in this socket*/
if (_cbs[socket->id].callback) {
_cbs[socket->id].callback(_cbs[socket->id].data);
}
}
}
}
_mutex.unlock();
}
}
rtos::ThisThread::sleep_for(50);
}
}
int ISM43362Interface::socket_accept(void *server, void **socket, SocketAddress *addr)
{
int ret = NSAPI_ERROR_OK;
char tmp[16];
uint16_t port;
_mutex.lock();
if (server == nullptr || socket == nullptr || addr == nullptr)
{
debug_if(_ism_debug, "ISM43362Interface socket_accept: wrong arguments\r\n");
_mutex.unlock();
return NSAPI_ERROR_PARAMETER;
}
struct ISM43362_socket *serverSocket = (struct ISM43362_socket *)server;
ret = _ism.getClientDetails(serverSocket->id, tmp, sizeof(tmp), &port);
if (ret != NSAPI_ERROR_OK)
{
debug_if(_ism_debug, "ISM43362Interface socket_accept: get client details failed\r\n");
_mutex.unlock();
return ret;
}
SocketAddress clientAddr(tmp, port);
*addr = clientAddr;
serverSocket->connected = true;
/* Reset read buffer */
memset(serverSocket->read_data, 0, sizeof(serverSocket->read_data));
serverSocket->read_data_size = 0;
*socket = server;
exit:
_mutex.unlock();
return ret;
}
int ISM43362Interface::socket_send_nolock(void *handle, const void *data, unsigned size)
{
struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
debug_if(_ism_debug, "ISM43362Interface socket_send_nolock id %d size %u\r\n", socket->id, size);
if (size > ES_WIFI_MAX_TX_PACKET_SIZE) {
size = ES_WIFI_MAX_TX_PACKET_SIZE;
}
if (!_ism.send(socket->id, data, size)) {
debug_if(_ism_debug, "ISM43362Interface: socket_send_nolock ERROR\r\n");
return NSAPI_ERROR_DEVICE_ERROR;
}
return size;
}
int ISM43362Interface::socket_send(void *handle, const void *data, unsigned size)
{
int ret = 0;
_mutex.lock();
struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
if (!socket->connected) {
_mutex.unlock();
return NSAPI_ERROR_NO_CONNECTION;
}
ret = socket_send_nolock(handle, data, size);
_mutex.unlock();
return ret;
}
int ISM43362Interface::socket_recv(void *handle, void *data, unsigned size)
{
_mutex.lock();
unsigned recv = 0;
struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
char *ptr = (char *)data;
if (!socket->connected) {
_mutex.unlock();
return 0;
}
if (socket->read_data_size == 0) {
/* if no callback is set, no need to read ?*/
int read_amount = _ism.check_recv_status(socket->id, socket->read_data);
if (read_amount > 0) {
socket->read_data_size = read_amount;
} else if (read_amount < 0) {
socket->connected = false;
debug_if(_ism_debug, "ISM43362Interface socket_recv: socket closed\r\n");
_mutex.unlock();
return 0;
}
}
if (socket->read_data_size != 0) {
// debug_if(_ism_debug, "ISM43362Interface socket_recv: read_data_size=%d\r\n", socket->read_data_size);
uint32_t i = 0;
while ((i < socket->read_data_size) && (i < size)) {
*ptr++ = socket->read_data[i];
i++;
}
recv += i;
if (i >= socket->read_data_size) {
/* All the storeed data has been read, reset buffer */
memset(socket->read_data, 0, sizeof(socket->read_data));
socket->read_data_size = 0;
// debug_if(_ism_debug, "ISM43362Interface: Socket_recv buffer reset\r\n");
} else {
/* In case there is remaining data in buffer, update socket content
* For now by shift copy of all data (not very efficient to be
* revised */
while (i < socket->read_data_size) {
socket->read_data[i - size] = socket->read_data[i];
i++;
}
socket->read_data_size -= size;
}
}
// else {
// debug_if(_ism_debug, "ISM43362Interface socket_recv: Nothing in buffer\r\n");
// }
_mutex.unlock();
if (recv > 0) {
debug_if(_ism_debug, "ISM43362Interface socket_recv: recv=%d\r\n", recv);
return recv;
} else {
// debug_if(_ism_debug, "ISM43362Interface socket_recv: returns WOULD BLOCK\r\n");
return NSAPI_ERROR_WOULD_BLOCK;
}
}
int ISM43362Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
{
int ret;
_mutex.lock();
struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
if (socket->proto == NSAPI_UDP && !socket->connected)
{
ret = socket_connect_nolock(handle, addr);
if (ret != NSAPI_ERROR_OK)
{
_mutex.unlock();
return ret;
}
ret = _ism.startClient(socket->id);
if (ret != NSAPI_ERROR_OK)
{
_mutex.unlock();
return ret;
}
socket->connected = true;
_socket_obj[socket->id] = (uint32_t)socket;
}
else
{
if (!socket->connected)
{
_mutex.unlock();
return NSAPI_ERROR_NO_CONNECTION;
}
}
ret = socket_send_nolock(socket, data, size);
_mutex.unlock();
return ret;
}
int ISM43362Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
{
int ret = 0;
_mutex.lock();
struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
char *ptr = (char *)data;
char tmp[16];
uint16_t port;
if (addr == nullptr || data == nullptr)
{
debug_if(_ism_debug, "ISM43362Interface socket_recvfrom: wrong arguments\r\n");
_mutex.unlock();
return NSAPI_ERROR_PARAMETER;
}
ret = _ism.getClientDetails(socket->id, tmp, sizeof(tmp), &port);
if (ret != NSAPI_ERROR_OK)
{
debug_if(_ism_debug, "ISM43362Interface socket_recvfrom: get client details failed\r\n");
_mutex.unlock();
return ret;
}
SocketAddress clientAddr(tmp, port);
*addr = clientAddr;
if (socket->read_data_size == 0) {
int read_amount = _ism.check_recv_status(socket->id, socket->read_data);
if (read_amount > 0) {
socket->read_data_size = read_amount;
} else if (read_amount < 0) {
debug_if(_ism_debug, "ISM43362Interface socket_recvfrom: data received failed\r\n");
ret = NSAPI_ERROR_DEVICE_ERROR;
}
}
if (socket->read_data_size > 0) {
uint32_t i = 0;
while ((i < socket->read_data_size) && (i < size)) {
*ptr++ = socket->read_data[i];
i++;
}
ret = i;
if (i >= socket->read_data_size) {
/* All the storeed data has been read, reset buffer */
memset(socket->read_data, 0, sizeof(socket->read_data));
socket->read_data_size = 0;
} else {
/* In case there is remaining data in buffer, update socket content
* For now by shift copy of all data (not very efficient to be
* revised */
while (i < socket->read_data_size) {
socket->read_data[i - size] = socket->read_data[i];
i++;
}
socket->read_data_size -= size;
}
}
_mutex.unlock();
return ret;
}
void ISM43362Interface::socket_attach(void *handle, void (*cb)(void *), void *data)
{
_mutex.lock();
struct ISM43362_socket *socket = (struct ISM43362_socket *)handle;
debug_if(_ism_debug, "ISM43362Interface: socket_attach id %d\n", socket->id);
_cbs[socket->id].callback = cb;
_cbs[socket->id].data = data;
_mutex.unlock();
}
void ISM43362Interface::event()
{
_mutex.lock();
debug_if(_ism_debug, "ISM43362Interface: event\n");
for (int i = 0; i < ISM43362_SOCKET_COUNT; i++) {
if (_cbs[i].callback) {
_cbs[i].callback(_cbs[i].data);
}
}
_mutex.unlock();
}
void ISM43362Interface::attach(Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_mutex.lock();
debug_if(_ism_debug, "ISM43362Interface: attach\n");
_conn_stat_cb = status_cb;
_mutex.unlock();
}
nsapi_connection_status_t ISM43362Interface::get_connection_status() const
{
debug_if(_ism_debug, "ISM43362Interface: get_connection_status %d\n", _conn_stat);
return _conn_stat;
}
void ISM43362Interface::update_conn_state_cb()
{
_mutex.lock();
nsapi_connection_status_t prev_stat = _conn_stat;
_conn_stat = _ism.connection_status();
debug_if(_ism_debug, "ISM43362Interface: update_conn_state_cb %d -> %d\n", prev_stat, _conn_stat);
if (prev_stat == _conn_stat) {
_mutex.unlock();
return;
}
// Inform upper layers
if (_conn_stat_cb) {
_conn_stat_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _conn_stat);
}
_mutex.unlock();
}
#if MBED_CONF_ISM43362_PROVIDE_DEFAULT
WiFiInterface *WiFiInterface::get_default_instance()
{
static ISM43362Interface ism;
return &ism;
}
#endif