15
15
* limitations under the License.
16
16
*/
17
17
18
+ #include " rtos/Kernel.h"
18
19
#include " QUECTEL/M26/QUECTEL_M26_CellularStack.h"
19
20
#include " CellularLog.h"
20
21
22
+ #define SOCKET_SEND_READY_TIMEOUT (30 *1000 )
23
+ #define SOCKET_READ_TIMEOUT 1000
24
+
21
25
using namespace mbed ;
22
26
23
27
QUECTEL_M26_CellularStack::QUECTEL_M26_CellularStack (ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type) : AT_CellularStack(atHandler, cid, stack_type)
24
28
{
25
- _at.set_urc_handler (" +QIRDI:" , mbed::Callback<void ()>(this , &QUECTEL_M26_CellularStack::urc_qiurc));
29
+ _at.set_urc_handler (" +QIRDI:" , Callback<void ()>(this , &QUECTEL_M26_CellularStack::urc_qiurc));
30
+
31
+ _at.set_urc_handler (" 0, CLOSED" , Callback<void ()>(this , &QUECTEL_M26_CellularStack::socket_closed_0));
32
+ _at.set_urc_handler (" 1, CLOSED" , Callback<void ()>(this , &QUECTEL_M26_CellularStack::socket_closed_1));
33
+ _at.set_urc_handler (" 2, CLOSED" , Callback<void ()>(this , &QUECTEL_M26_CellularStack::socket_closed_2));
34
+ _at.set_urc_handler (" 3, CLOSED" , Callback<void ()>(this , &QUECTEL_M26_CellularStack::socket_closed_3));
35
+ _at.set_urc_handler (" 4, CLOSED" , Callback<void ()>(this , &QUECTEL_M26_CellularStack::socket_closed_4));
36
+ _at.set_urc_handler (" 5, CLOSED" , Callback<void ()>(this , &QUECTEL_M26_CellularStack::socket_closed_5));
26
37
}
27
38
28
39
QUECTEL_M26_CellularStack::~QUECTEL_M26_CellularStack ()
29
40
{
41
+ _at.set_urc_handler (" 5, CLOSED" , NULL );
42
+ _at.set_urc_handler (" 4, CLOSED" , NULL );
43
+ _at.set_urc_handler (" 3, CLOSED" , NULL );
44
+ _at.set_urc_handler (" 2, CLOSED" , NULL );
45
+ _at.set_urc_handler (" 1, CLOSED" , NULL );
46
+ _at.set_urc_handler (" 0, CLOSED" , NULL );
47
+
48
+ _at.set_urc_handler (" +QIRDI:" , NULL );
30
49
}
31
50
32
51
nsapi_error_t QUECTEL_M26_CellularStack::socket_listen (nsapi_socket_t handle, int backlog)
@@ -44,6 +63,45 @@ nsapi_error_t QUECTEL_M26_CellularStack::socket_bind(nsapi_socket_t handle, cons
44
63
return NSAPI_ERROR_UNSUPPORTED;
45
64
}
46
65
66
+ void QUECTEL_M26_CellularStack::socket_closed (int sock_id)
67
+ {
68
+ CellularSocket *sock = find_socket (sock_id);
69
+ if (sock) {
70
+ tr_info (" Socket closed %d" , sock_id);
71
+ sock->closed = true ;
72
+ }
73
+ }
74
+
75
+ void QUECTEL_M26_CellularStack::socket_closed_0 ()
76
+ {
77
+ socket_closed (0 );
78
+ }
79
+
80
+ void QUECTEL_M26_CellularStack::socket_closed_1 ()
81
+ {
82
+ socket_closed (1 );
83
+ }
84
+
85
+ void QUECTEL_M26_CellularStack::socket_closed_2 ()
86
+ {
87
+ socket_closed (2 );
88
+ }
89
+
90
+ void QUECTEL_M26_CellularStack::socket_closed_3 ()
91
+ {
92
+ socket_closed (3 );
93
+ }
94
+
95
+ void QUECTEL_M26_CellularStack::socket_closed_4 ()
96
+ {
97
+ socket_closed (4 );
98
+ }
99
+
100
+ void QUECTEL_M26_CellularStack::socket_closed_5 ()
101
+ {
102
+ socket_closed (5 );
103
+ }
104
+
47
105
void QUECTEL_M26_CellularStack::urc_qiurc ()
48
106
{
49
107
int sock_id = 0 ;
@@ -348,7 +406,6 @@ nsapi_error_t QUECTEL_M26_CellularStack::create_socket_impl(CellularSocket *sock
348
406
socket->created = ((ret_val == NSAPI_ERROR_OK) && (modem_connect_id == request_connect_id));
349
407
return ret_val;
350
408
} else {
351
- tr_warn (" QUECTEL_M26_CellularStack:%s:%u: Do not support TCP Listner/UDP Service Mode [%d,%d]" , __FUNCTION__, __LINE__, socket->created , ret_val);
352
409
ret_val = NSAPI_ERROR_OK;
353
410
}
354
411
@@ -360,16 +417,16 @@ nsapi_error_t QUECTEL_M26_CellularStack::create_socket_impl(CellularSocket *sock
360
417
nsapi_size_or_error_t QUECTEL_M26_CellularStack::socket_sendto_impl (CellularSocket *socket, const SocketAddress &address,
361
418
const void *data, nsapi_size_t size)
362
419
{
363
- int sent_len = (size > M26_SENT_BYTE_MAX) ? M26_SENT_BYTE_MAX : size;
420
+ int sent_len = size;
421
+ int sent_acked = 0 ;
364
422
int sent_nacked = 0 ;
365
423
int sent_len_before = 0 ;
366
424
int sent_len_after = 0 ;
367
- int sent_acked;
368
425
nsapi_error_t error;
369
426
370
427
tr_debug (" QUECTEL_M26_CellularStack:%s:%u:[%d-%d]" , __FUNCTION__, __LINE__, sent_len, size);
371
428
372
- if (sent_len == 0 ) {
429
+ if (sent_len == 0 || size > M26_SENT_BYTE_MAX ) {
373
430
tr_error (" QUECTEL_M26_CellularStack:%s:%u:[NSAPI_ERROR_PARAMETER]" , __FUNCTION__, __LINE__);
374
431
return NSAPI_ERROR_PARAMETER;
375
432
}
@@ -385,23 +442,28 @@ nsapi_size_or_error_t QUECTEL_M26_CellularStack::socket_sendto_impl(CellularSock
385
442
}
386
443
387
444
if (socket->proto == NSAPI_TCP) {
388
- _at.cmd_start (" AT+QISACK=" );
389
- _at.write_int (socket->id );
390
- _at.cmd_stop ();
391
- _at.resp_start (" +QISACK:" );
392
- sent_len_before = _at.read_int ();
393
- sent_acked = _at.read_int ();
394
- sent_nacked = _at.read_int ();
395
- _at.resp_stop ();
445
+ bool ready_to_send = false ;
446
+ uint64_t start_time = rtos::Kernel::get_ms_count ();
447
+ while (!ready_to_send && start_time < rtos::Kernel::get_ms_count () + SOCKET_SEND_READY_TIMEOUT) {
448
+ _at.cmd_start (" AT+QISACK=" );
449
+ _at.write_int (socket->id );
450
+ _at.cmd_stop ();
451
+ _at.resp_start (" +QISACK:" );
452
+ sent_len_before = _at.read_int ();
453
+ sent_acked = _at.read_int ();
454
+ sent_nacked = _at.read_int ();
455
+ _at.resp_stop ();
396
456
397
- if (_at.get_last_error () != NSAPI_ERROR_OK) {
398
- tr_error (" QUECTEL_M26_CellularStack:%s:%u:[NSAPI_ERROR_DEVICE_ERROR]" , __FUNCTION__, __LINE__);
399
- return NSAPI_ERROR_DEVICE_ERROR;
400
- }
457
+ if (_at.get_last_error () != NSAPI_ERROR_OK) {
458
+ tr_error (" QUECTEL_M26_CellularStack:%s:%u:[NSAPI_ERROR_DEVICE_ERROR]" , __FUNCTION__, __LINE__);
459
+ return NSAPI_ERROR_DEVICE_ERROR;
460
+ }
401
461
402
- if (sent_nacked != 0 ) {
403
- tr_debug (" QUECTEL_M26_CellularStack:%s:%u:[NSAPI_ERROR_WOULD_BLOCK]" , __FUNCTION__, __LINE__);
404
- return NSAPI_ERROR_WOULD_BLOCK;
462
+ if (sent_nacked == 0 ) {
463
+ ready_to_send = true ;
464
+ } else {
465
+ tr_debug (" QUECTEL_M26_CellularStack:%s:%u:[NSAPI_ERROR_WOULD_BLOCK]" , __FUNCTION__, __LINE__);
466
+ }
405
467
}
406
468
}
407
469
@@ -455,35 +517,53 @@ nsapi_size_or_error_t QUECTEL_M26_CellularStack::socket_sendto_impl(CellularSock
455
517
nsapi_size_or_error_t QUECTEL_M26_CellularStack::socket_recvfrom_impl (CellularSocket *socket, SocketAddress *address,
456
518
void *buffer, nsapi_size_t size)
457
519
{
458
- nsapi_size_or_error_t recv_len = (size > M26_RECV_BYTE_MAX) ? M26_RECV_BYTE_MAX : size;
459
- int recv_len_after = 0 ;
460
520
int port;
461
521
char type[8 ];
462
522
char ip_address[NSAPI_IP_SIZE + 1 ];
463
523
464
524
tr_debug (" QUECTEL_M26_CellularStack:%s:%u:[%d]" , __FUNCTION__, __LINE__, size);
465
- _at.cmd_start (" AT+QIRD=" );
466
- _at.write_int (0 ); /* at+qifgcnt 0-1 */
467
- _at.write_int (1 ); /* 1-Client, 2-Server */
468
- _at.write_int (socket->id );
469
- _at.write_int (recv_len);
470
- _at.cmd_stop ();
471
525
472
- _at.resp_start (" +QIRD:" );
473
- if (_at.info_resp ()) {
474
- _at.set_delimiter (' :' );
475
- _at.read_string (ip_address, sizeof (ip_address));
476
- _at.set_default_delimiter ();
477
- port = _at.read_int ();
478
- _at.read_string (type, sizeof (type));
479
- recv_len_after = _at.read_int ();
480
- if (recv_len_after > 0 ) {
481
- _at.read_bytes ((uint8_t *)buffer, recv_len_after);
526
+ uint64_t start_time = rtos::Kernel::get_ms_count ();
527
+ nsapi_size_t len = 0 ;
528
+ for (; len < size;) {
529
+ int read_len = (size - len > M26_RECV_BYTE_MAX) ? M26_RECV_BYTE_MAX : size - len;
530
+ _at.cmd_start (" AT+QIRD=" );
531
+ _at.write_int (0 ); /* at+qifgcnt 0-1 */
532
+ _at.write_int (1 ); /* 1-Client, 2-Server */
533
+ _at.write_int (socket->id );
534
+ _at.write_int (read_len);
535
+ _at.cmd_stop ();
536
+
537
+ nsapi_size_t recv_len = 0 ;
538
+ _at.resp_start (" +QIRD:" );
539
+ if (_at.info_resp ()) {
540
+ _at.set_delimiter (' :' );
541
+ _at.read_string (ip_address, sizeof (ip_address));
542
+ _at.set_default_delimiter ();
543
+ port = _at.read_int ();
544
+ _at.read_string (type, sizeof (type));
545
+ recv_len = _at.read_int ();
546
+ _at.read_bytes ((uint8_t *)buffer + len, recv_len);
547
+ len += recv_len;
548
+ }
549
+ _at.resp_stop ();
550
+
551
+ if (_at.get_last_error () != NSAPI_ERROR_OK) {
552
+ tr_warn (" QUECTEL_M26_CellularStack:%s:%u:[ERROR NSAPI_ERROR_OK]" , __FUNCTION__, __LINE__);
553
+ return NSAPI_ERROR_DEVICE_ERROR;
554
+ }
555
+
556
+ if (rtos::Kernel::get_ms_count () > start_time + SOCKET_READ_TIMEOUT) {
557
+ tr_warn (" QUECTEL_M26_CellularStack:%s:%u:[ERROR NSAPI_ERROR_TIMEOUT]" , __FUNCTION__, __LINE__);
558
+ return NSAPI_ERROR_TIMEOUT;
559
+ }
560
+
561
+ if (recv_len == 0 || recv_len < read_len) {
562
+ break ;
482
563
}
483
564
}
484
- _at.resp_stop ();
485
565
486
- if (!recv_len_after || (_at. get_last_error () != NSAPI_ERROR_OK) ) {
566
+ if (len == 0 ) {
487
567
tr_debug (" QUECTEL_M26_CellularStack:%s:%u:[ERROR NSAPI_ERROR_WOULD_BLOCK]" , __FUNCTION__, __LINE__);
488
568
return NSAPI_ERROR_WOULD_BLOCK;
489
569
}
@@ -493,6 +573,6 @@ nsapi_size_or_error_t QUECTEL_M26_CellularStack::socket_recvfrom_impl(CellularSo
493
573
address->set_port (port);
494
574
}
495
575
496
- tr_debug (" QUECTEL_M26_CellularStack:%s:%u:[%d]" , __FUNCTION__, __LINE__, recv_len_after );
497
- return recv_len_after ;
576
+ tr_debug (" QUECTEL_M26_CellularStack:%s:%u:[%d]" , __FUNCTION__, __LINE__, len );
577
+ return len ;
498
578
}
0 commit comments