forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquectel-bg9x.c
1251 lines (1040 loc) · 31 KB
/
quectel-bg9x.c
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020 Analog Life LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT quectel_bg9x
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(modem_quectel_bg9x, CONFIG_MODEM_LOG_LEVEL);
#include "quectel-bg9x.h"
static struct k_thread modem_rx_thread;
static struct k_work_q modem_workq;
static struct modem_data mdata;
static struct modem_context mctx;
static const struct socket_op_vtable offload_socket_fd_op_vtable;
static K_KERNEL_STACK_DEFINE(modem_rx_stack, CONFIG_MODEM_QUECTEL_BG9X_RX_STACK_SIZE);
static K_KERNEL_STACK_DEFINE(modem_workq_stack, CONFIG_MODEM_QUECTEL_BG9X_RX_WORKQ_STACK_SIZE);
NET_BUF_POOL_DEFINE(mdm_recv_pool, MDM_RECV_MAX_BUF, MDM_RECV_BUF_SIZE, 0, NULL);
static const struct gpio_dt_spec power_gpio = GPIO_DT_SPEC_INST_GET(0, mdm_power_gpios);
static const struct gpio_dt_spec reset_gpio = GPIO_DT_SPEC_INST_GET(0, mdm_reset_gpios);
#if DT_INST_NODE_HAS_PROP(0, mdm_dtr_gpios)
static const struct gpio_dt_spec dtr_gpio = GPIO_DT_SPEC_INST_GET(0, mdm_dtr_gpios);
#endif
#if DT_INST_NODE_HAS_PROP(0, mdm_wdisable_gpios)
static const struct gpio_dt_spec wdisable_gpio = GPIO_DT_SPEC_INST_GET(0, mdm_wdisable_gpios);
#endif
static inline int digits(int n)
{
int count = 0;
while (n != 0) {
n /= 10;
++count;
}
return count;
}
static inline uint32_t hash32(char *str, int len)
{
#define HASH_MULTIPLIER 37
uint32_t h = 0;
int i;
for (i = 0; i < len; ++i) {
h = (h * HASH_MULTIPLIER) + str[i];
}
return h;
}
static inline uint8_t *modem_get_mac(const struct device *dev)
{
struct modem_data *data = dev->data;
uint32_t hash_value;
data->mac_addr[0] = 0x00;
data->mac_addr[1] = 0x10;
/* use IMEI for mac_addr */
hash_value = hash32(mdata.mdm_imei, strlen(mdata.mdm_imei));
UNALIGNED_PUT(hash_value, (uint32_t *)(data->mac_addr + 2));
return data->mac_addr;
}
/* Func: modem_atoi
* Desc: Convert string to long integer, but handle errors
*/
static int modem_atoi(const char *s, const int err_value,
const char *desc, const char *func)
{
int ret;
char *endptr;
ret = (int)strtol(s, &endptr, 10);
if (!endptr || *endptr != '\0') {
LOG_ERR("bad %s '%s' in %s", s, desc,
func);
return err_value;
}
return ret;
}
static inline int find_len(char *data)
{
char buf[10] = {0};
int i;
for (i = 0; i < 10; i++) {
if (data[i] == '\r') {
break;
}
buf[i] = data[i];
}
return ATOI(buf, 0, "rx_buf");
}
/* Func: on_cmd_sockread_common
* Desc: Function to successfully read data from the modem on a given socket.
*/
static int on_cmd_sockread_common(int socket_fd,
struct modem_cmd_handler_data *data,
uint16_t len)
{
struct modem_socket *sock = NULL;
struct socket_read_data *sock_data;
int ret, i;
int socket_data_length;
int bytes_to_skip;
if (!len) {
LOG_ERR("Invalid length, Aborting!");
return -EAGAIN;
}
/* Make sure we still have buf data */
if (!data->rx_buf) {
LOG_ERR("Incorrect format! Ignoring data!");
return -EINVAL;
}
socket_data_length = find_len(data->rx_buf->data);
/* No (or not enough) data available on the socket. */
bytes_to_skip = digits(socket_data_length) + 2 + 4;
if (socket_data_length <= 0) {
LOG_ERR("Length problem (%d). Aborting!", socket_data_length);
return -EAGAIN;
}
/* check to make sure we have all of the data. */
if (net_buf_frags_len(data->rx_buf) < (socket_data_length + bytes_to_skip)) {
LOG_DBG("Not enough data -- wait!");
return -EAGAIN;
}
/* Skip "len" and CRLF */
bytes_to_skip = digits(socket_data_length) + 2;
for (i = 0; i < bytes_to_skip; i++) {
net_buf_pull_u8(data->rx_buf);
}
if (!data->rx_buf->len) {
data->rx_buf = net_buf_frag_del(NULL, data->rx_buf);
}
sock = modem_socket_from_fd(&mdata.socket_config, socket_fd);
if (!sock) {
LOG_ERR("Socket not found! (%d)", socket_fd);
ret = -EINVAL;
goto exit;
}
sock_data = (struct socket_read_data *)sock->data;
if (!sock_data) {
LOG_ERR("Socket data not found! Skip handling (%d)", socket_fd);
ret = -EINVAL;
goto exit;
}
ret = net_buf_linearize(sock_data->recv_buf, sock_data->recv_buf_len,
data->rx_buf, 0, (uint16_t)socket_data_length);
data->rx_buf = net_buf_skip(data->rx_buf, ret);
sock_data->recv_read_len = ret;
if (ret != socket_data_length) {
LOG_ERR("Total copied data is different then received data!"
" copied:%d vs. received:%d", ret, socket_data_length);
ret = -EINVAL;
}
exit:
/* remove packet from list (ignore errors) */
(void)modem_socket_packet_size_update(&mdata.socket_config, sock,
-socket_data_length);
/* don't give back semaphore -- OK to follow */
return ret;
}
/* Func: socket_close
* Desc: Function to close the given socket descriptor.
*/
static void socket_close(struct modem_socket *sock)
{
char buf[sizeof("AT+QICLOSE=##")] = {0};
int ret;
snprintk(buf, sizeof(buf), "AT+QICLOSE=%d", sock->sock_fd);
/* Tell the modem to close the socket. */
ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler,
NULL, 0U, buf,
&mdata.sem_response, MDM_CMD_TIMEOUT);
if (ret < 0) {
LOG_ERR("%s ret:%d", buf, ret);
}
modem_socket_put(&mdata.socket_config, sock->sock_fd);
}
/* Handler: OK */
MODEM_CMD_DEFINE(on_cmd_ok)
{
modem_cmd_handler_set_error(data, 0);
k_sem_give(&mdata.sem_response);
return 0;
}
/* Handler: ERROR */
MODEM_CMD_DEFINE(on_cmd_error)
{
modem_cmd_handler_set_error(data, -EIO);
k_sem_give(&mdata.sem_response);
return 0;
}
/* Handler: +CME Error: <err>[0] */
MODEM_CMD_DEFINE(on_cmd_exterror)
{
modem_cmd_handler_set_error(data, -EIO);
k_sem_give(&mdata.sem_response);
return 0;
}
/* Handler: +CSQ: <signal_power>[0], <qual>[1] */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_rssi_csq)
{
int rssi = ATOI(argv[0], 0, "signal_power");
/* Check the RSSI value. */
if (rssi == 31) {
mdata.mdm_rssi = -51;
} else if (rssi >= 0 && rssi <= 31) {
mdata.mdm_rssi = -114 + ((rssi * 2) + 1);
} else {
mdata.mdm_rssi = -1000;
}
LOG_INF("RSSI: %d", mdata.mdm_rssi);
return 0;
}
/* Handler: +QIOPEN: <connect_id>[0], <err>[1] */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_sockopen)
{
int err = ATOI(argv[1], 0, "sock_err");
LOG_INF("AT+QIOPEN: %d", err);
modem_cmd_handler_set_error(data, err);
k_sem_give(&mdata.sem_sock_conn);
return 0;
}
/* Handler: <manufacturer> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_manufacturer)
{
size_t out_len = net_buf_linearize(mdata.mdm_manufacturer,
sizeof(mdata.mdm_manufacturer) - 1,
data->rx_buf, 0, len);
mdata.mdm_manufacturer[out_len] = '\0';
LOG_INF("Manufacturer: %s", mdata.mdm_manufacturer);
return 0;
}
/* Handler: <model> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_model)
{
size_t out_len = net_buf_linearize(mdata.mdm_model,
sizeof(mdata.mdm_model) - 1,
data->rx_buf, 0, len);
mdata.mdm_model[out_len] = '\0';
/* Log the received information. */
LOG_INF("Model: %s", mdata.mdm_model);
return 0;
}
/* Handler: <rev> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_revision)
{
size_t out_len = net_buf_linearize(mdata.mdm_revision,
sizeof(mdata.mdm_revision) - 1,
data->rx_buf, 0, len);
mdata.mdm_revision[out_len] = '\0';
/* Log the received information. */
LOG_INF("Revision: %s", mdata.mdm_revision);
return 0;
}
/* Handler: <IMEI> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imei)
{
size_t out_len = net_buf_linearize(mdata.mdm_imei,
sizeof(mdata.mdm_imei) - 1,
data->rx_buf, 0, len);
mdata.mdm_imei[out_len] = '\0';
/* Log the received information. */
LOG_INF("IMEI: %s", mdata.mdm_imei);
return 0;
}
#if defined(CONFIG_MODEM_SIM_NUMBERS)
/* Handler: <IMSI> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imsi)
{
size_t out_len = net_buf_linearize(mdata.mdm_imsi,
sizeof(mdata.mdm_imsi) - 1,
data->rx_buf, 0, len);
mdata.mdm_imsi[out_len] = '\0';
/* Log the received information. */
LOG_INF("IMSI: %s", mdata.mdm_imsi);
return 0;
}
/* Handler: <ICCID> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_iccid)
{
size_t out_len;
char *p;
out_len = net_buf_linearize(mdata.mdm_iccid, sizeof(mdata.mdm_iccid) - 1,
data->rx_buf, 0, len);
mdata.mdm_iccid[out_len] = '\0';
/* Skip over the +CCID bit, which modems omit. */
if (mdata.mdm_iccid[0] == '+') {
p = strchr(mdata.mdm_iccid, ' ');
if (p) {
out_len = strlen(p + 1);
memmove(mdata.mdm_iccid, p + 1, len + 1);
}
}
LOG_INF("ICCID: %s", mdata.mdm_iccid);
return 0;
}
#endif /* #if defined(CONFIG_MODEM_SIM_NUMBERS) */
/* Handler: TX Ready */
MODEM_CMD_DIRECT_DEFINE(on_cmd_tx_ready)
{
k_sem_give(&mdata.sem_tx_ready);
return len;
}
/* Handler: SEND OK */
MODEM_CMD_DEFINE(on_cmd_send_ok)
{
modem_cmd_handler_set_error(data, 0);
k_sem_give(&mdata.sem_response);
return 0;
}
/* Handler: SEND FAIL */
MODEM_CMD_DEFINE(on_cmd_send_fail)
{
mdata.sock_written = 0;
modem_cmd_handler_set_error(data, -EIO);
k_sem_give(&mdata.sem_response);
return 0;
}
/* Handler: Read data */
MODEM_CMD_DEFINE(on_cmd_sock_readdata)
{
return on_cmd_sockread_common(mdata.sock_fd, data, len);
}
/* Handler: Data receive indication. */
MODEM_CMD_DEFINE(on_cmd_unsol_recv)
{
struct modem_socket *sock;
int sock_fd;
sock_fd = ATOI(argv[0], 0, "sock_fd");
/* Socket pointer from FD. */
sock = modem_socket_from_fd(&mdata.socket_config, sock_fd);
if (!sock) {
return 0;
}
/* Data ready indication. */
LOG_INF("Data Receive Indication for socket: %d", sock_fd);
modem_socket_data_ready(&mdata.socket_config, sock);
return 0;
}
/* Handler: Socket Close Indication. */
MODEM_CMD_DEFINE(on_cmd_unsol_close)
{
struct modem_socket *sock;
int sock_fd;
sock_fd = ATOI(argv[0], 0, "sock_fd");
sock = modem_socket_from_fd(&mdata.socket_config, sock_fd);
if (!sock) {
return 0;
}
LOG_INF("Socket Close Indication for socket: %d", sock_fd);
/* Tell the modem to close the socket. */
socket_close(sock);
LOG_INF("Socket Closed: %d", sock_fd);
return 0;
}
/* Handler: Modem initialization ready. */
MODEM_CMD_DEFINE(on_cmd_unsol_rdy)
{
k_sem_give(&mdata.sem_response);
return 0;
}
/* Func: send_socket_data
* Desc: This function will send "binary" data over the socket object.
*/
static ssize_t send_socket_data(struct modem_socket *sock,
const struct sockaddr *dst_addr,
struct modem_cmd *handler_cmds,
size_t handler_cmds_len,
const char *buf, size_t buf_len,
k_timeout_t timeout)
{
int ret;
char send_buf[sizeof("AT+QISEND=##,####")] = {0};
char ctrlz = 0x1A;
if (buf_len > MDM_MAX_DATA_LENGTH) {
buf_len = MDM_MAX_DATA_LENGTH;
}
/* Create a buffer with the correct params. */
mdata.sock_written = buf_len;
snprintk(send_buf, sizeof(send_buf), "AT+QISEND=%d,%ld", sock->sock_fd, (long) buf_len);
/* Setup the locks correctly. */
k_sem_take(&mdata.cmd_handler_data.sem_tx_lock, K_FOREVER);
k_sem_reset(&mdata.sem_tx_ready);
/* Send the Modem command. */
ret = modem_cmd_send_nolock(&mctx.iface, &mctx.cmd_handler,
NULL, 0U, send_buf, NULL, K_NO_WAIT);
if (ret < 0) {
goto exit;
}
/* set command handlers */
ret = modem_cmd_handler_update_cmds(&mdata.cmd_handler_data,
handler_cmds, handler_cmds_len,
true);
if (ret < 0) {
goto exit;
}
/* Wait for '>' */
ret = k_sem_take(&mdata.sem_tx_ready, K_MSEC(5000));
if (ret < 0) {
/* Didn't get the data prompt - Exit. */
LOG_DBG("Timeout waiting for tx");
goto exit;
}
/* Write all data on the console and send CTRL+Z. */
mctx.iface.write(&mctx.iface, buf, buf_len);
mctx.iface.write(&mctx.iface, &ctrlz, 1);
/* Wait for 'SEND OK' or 'SEND FAIL' */
k_sem_reset(&mdata.sem_response);
ret = k_sem_take(&mdata.sem_response, timeout);
if (ret < 0) {
LOG_DBG("No send response");
goto exit;
}
ret = modem_cmd_handler_get_error(&mdata.cmd_handler_data);
if (ret != 0) {
LOG_DBG("Failed to send data");
}
exit:
/* unset handler commands and ignore any errors */
(void)modem_cmd_handler_update_cmds(&mdata.cmd_handler_data,
NULL, 0U, false);
k_sem_give(&mdata.cmd_handler_data.sem_tx_lock);
if (ret < 0) {
return ret;
}
/* Return the amount of data written on the socket. */
return mdata.sock_written;
}
/* Func: offload_sendto
* Desc: This function will send data on the socket object.
*/
static ssize_t offload_sendto(void *obj, const void *buf, size_t len,
int flags, const struct sockaddr *to,
socklen_t tolen)
{
int ret;
struct modem_socket *sock = (struct modem_socket *) obj;
/* Here's how sending data works,
* -> We firstly send the "AT+QISEND" command on the given socket and
* specify the length of data to be transferred.
* -> In response to "AT+QISEND" command, the modem may respond with a
* data prompt (>) or not respond at all. If it doesn't respond, we
* exit. If it does respond with a data prompt (>), we move forward.
* -> We plainly write all data on the UART and terminate by sending a
* CTRL+Z. Once the modem receives CTRL+Z, it starts processing the
* data and will respond with either "SEND OK", "SEND FAIL" or "ERROR".
* Here we are registering handlers for the first two responses. We
* already have a handler for the "generic" error response.
*/
struct modem_cmd cmd[] = {
MODEM_CMD_DIRECT(">", on_cmd_tx_ready),
MODEM_CMD("SEND OK", on_cmd_send_ok, 0, ","),
MODEM_CMD("SEND FAIL", on_cmd_send_fail, 0, ","),
};
/* Ensure that valid parameters are passed. */
if (!buf || len == 0) {
errno = EINVAL;
return -1;
}
/* UDP is not supported. */
if (sock->ip_proto == IPPROTO_UDP) {
errno = ENOTSUP;
return -1;
}
if (!sock->is_connected) {
errno = ENOTCONN;
return -1;
}
ret = send_socket_data(sock, to, cmd, ARRAY_SIZE(cmd), buf, len,
MDM_CMD_TIMEOUT);
if (ret < 0) {
errno = -ret;
return -1;
}
/* Data was written successfully. */
errno = 0;
return ret;
}
/* Func: offload_recvfrom
* Desc: This function will receive data on the socket object.
*/
static ssize_t offload_recvfrom(void *obj, void *buf, size_t len,
int flags, struct sockaddr *from,
socklen_t *fromlen)
{
struct modem_socket *sock = (struct modem_socket *)obj;
char sendbuf[sizeof("AT+QIRD=##,####")] = {0};
int ret;
struct socket_read_data sock_data;
/* Modem command to read the data. */
struct modem_cmd data_cmd[] = { MODEM_CMD("+QIRD: ", on_cmd_sock_readdata, 0U, "") };
if (!buf || len == 0) {
errno = EINVAL;
return -1;
}
if (flags & ZSOCK_MSG_PEEK) {
errno = ENOTSUP;
return -1;
}
snprintk(sendbuf, sizeof(sendbuf), "AT+QIRD=%d,%zd", sock->sock_fd, len);
/* Socket read settings */
(void) memset(&sock_data, 0, sizeof(sock_data));
sock_data.recv_buf = buf;
sock_data.recv_buf_len = len;
sock_data.recv_addr = from;
sock->data = &sock_data;
mdata.sock_fd = sock->sock_fd;
/* Tell the modem to give us data (AT+QIRD=sock_fd,data_len). */
ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler,
data_cmd, ARRAY_SIZE(data_cmd), sendbuf, &mdata.sem_response,
MDM_CMD_TIMEOUT);
if (ret < 0) {
errno = -ret;
ret = -1;
goto exit;
}
/* HACK: use dst address as from */
if (from && fromlen) {
*fromlen = sizeof(sock->dst);
memcpy(from, &sock->dst, *fromlen);
}
/* return length of received data */
errno = 0;
ret = sock_data.recv_read_len;
exit:
/* clear socket data */
sock->data = NULL;
return ret;
}
/* Func: offload_read
* Desc: This function reads data from the given socket object.
*/
static ssize_t offload_read(void *obj, void *buffer, size_t count)
{
return offload_recvfrom(obj, buffer, count, 0, NULL, 0);
}
/* Func: offload_write
* Desc: This function writes data to the given socket object.
*/
static ssize_t offload_write(void *obj, const void *buffer, size_t count)
{
return offload_sendto(obj, buffer, count, 0, NULL, 0);
}
/* Func: offload_ioctl
* Desc: Function call to handle various misc requests.
*/
static int offload_ioctl(void *obj, unsigned int request, va_list args)
{
switch (request) {
case ZFD_IOCTL_POLL_PREPARE: {
struct zsock_pollfd *pfd;
struct k_poll_event **pev;
struct k_poll_event *pev_end;
pfd = va_arg(args, struct zsock_pollfd *);
pev = va_arg(args, struct k_poll_event **);
pev_end = va_arg(args, struct k_poll_event *);
return modem_socket_poll_prepare(&mdata.socket_config, obj, pfd, pev, pev_end);
}
case ZFD_IOCTL_POLL_UPDATE: {
struct zsock_pollfd *pfd;
struct k_poll_event **pev;
pfd = va_arg(args, struct zsock_pollfd *);
pev = va_arg(args, struct k_poll_event **);
return modem_socket_poll_update(obj, pfd, pev);
}
default:
errno = EINVAL;
return -1;
}
}
/* Func: offload_connect
* Desc: This function will connect with a provided TCP.
*/
static int offload_connect(void *obj, const struct sockaddr *addr,
socklen_t addrlen)
{
struct modem_socket *sock = (struct modem_socket *) obj;
uint16_t dst_port = 0;
char *protocol = "TCP";
struct modem_cmd cmd[] = { MODEM_CMD("+QIOPEN: ", on_cmd_atcmdinfo_sockopen, 2U, ",") };
char buf[sizeof("AT+QIOPEN=#,#,'###','###',"
"####.####.####.####.####.####.####.####,######,"
"0,0")] = {0};
int ret;
char ip_str[NET_IPV6_ADDR_LEN];
if (sock->id < mdata.socket_config.base_socket_num - 1) {
LOG_ERR("Invalid socket_id(%d) from fd:%d",
sock->id, sock->sock_fd);
errno = EINVAL;
return -1;
}
if (sock->is_connected == true) {
LOG_ERR("Socket is already connected!! socket_id(%d), socket_fd:%d",
sock->id, sock->sock_fd);
errno = EISCONN;
return -1;
}
/* Find the correct destination port. */
if (addr->sa_family == AF_INET6) {
dst_port = ntohs(net_sin6(addr)->sin6_port);
} else if (addr->sa_family == AF_INET) {
dst_port = ntohs(net_sin(addr)->sin_port);
}
/* UDP is not supported. */
if (sock->ip_proto == IPPROTO_UDP) {
errno = ENOTSUP;
return -1;
}
k_sem_reset(&mdata.sem_sock_conn);
ret = modem_context_sprint_ip_addr(addr, ip_str, sizeof(ip_str));
if (ret != 0) {
LOG_ERR("Error formatting IP string %d", ret);
LOG_ERR("Closing the socket!!!");
socket_close(sock);
errno = -ret;
return -1;
}
/* Formulate the complete string. */
snprintk(buf, sizeof(buf), "AT+QIOPEN=%d,%d,\"%s\",\"%s\",%d,0,0", 1, sock->sock_fd, protocol,
ip_str, dst_port);
/* Send out the command. */
ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler,
NULL, 0U, buf,
&mdata.sem_response, K_SECONDS(1));
if (ret < 0) {
LOG_ERR("%s ret:%d", buf, ret);
LOG_ERR("Closing the socket!!!");
socket_close(sock);
errno = -ret;
return -1;
}
/* set command handlers */
ret = modem_cmd_handler_update_cmds(&mdata.cmd_handler_data,
cmd, ARRAY_SIZE(cmd), true);
if (ret < 0) {
goto exit;
}
/* Wait for QI+OPEN */
ret = k_sem_take(&mdata.sem_sock_conn, MDM_CMD_CONN_TIMEOUT);
if (ret < 0) {
LOG_ERR("Timeout waiting for socket open");
LOG_ERR("Closing the socket!!!");
socket_close(sock);
goto exit;
}
ret = modem_cmd_handler_get_error(&mdata.cmd_handler_data);
if (ret != 0) {
LOG_ERR("Closing the socket!!!");
socket_close(sock);
goto exit;
}
/* Connected successfully. */
sock->is_connected = true;
errno = 0;
return 0;
exit:
(void) modem_cmd_handler_update_cmds(&mdata.cmd_handler_data,
NULL, 0U, false);
errno = -ret;
return -1;
}
/* Func: offload_close
* Desc: This function closes the connection with the remote client and
* frees the socket.
*/
static int offload_close(void *obj)
{
struct modem_socket *sock = (struct modem_socket *) obj;
/* Make sure we assigned an id */
if (sock->id < mdata.socket_config.base_socket_num) {
return 0;
}
/* Close the socket only if it is connected. */
if (sock->is_connected) {
socket_close(sock);
}
return 0;
}
/* Func: offload_sendmsg
* Desc: This function sends messages to the modem.
*/
static ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags)
{
ssize_t sent = 0;
int rc;
LOG_DBG("msg_iovlen:%zd flags:%d", msg->msg_iovlen, flags);
for (int i = 0; i < msg->msg_iovlen; i++) {
const char *buf = msg->msg_iov[i].iov_base;
size_t len = msg->msg_iov[i].iov_len;
while (len > 0) {
rc = offload_sendto(obj, buf, len, flags,
msg->msg_name, msg->msg_namelen);
if (rc < 0) {
if (rc == -EAGAIN) {
k_sleep(MDM_SENDMSG_SLEEP);
} else {
sent = rc;
break;
}
} else {
sent += rc;
buf += rc;
len -= rc;
}
}
}
return (ssize_t) sent;
}
/* Func: modem_rx
* Desc: Thread to process all messages received from the Modem.
*/
static void modem_rx(void)
{
while (true) {
/* Wait for incoming data */
k_sem_take(&mdata.iface_data.rx_sem, K_FOREVER);
mctx.cmd_handler.process(&mctx.cmd_handler, &mctx.iface);
}
}
/* Func: modem_rssi_query_work
* Desc: Routine to get Modem RSSI.
*/
static void modem_rssi_query_work(struct k_work *work)
{
struct modem_cmd cmd = MODEM_CMD("+CSQ: ", on_cmd_atcmdinfo_rssi_csq, 2U, ",");
static char *send_cmd = "AT+CSQ";
int ret;
/* query modem RSSI */
ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler,
&cmd, 1U, send_cmd, &mdata.sem_response,
MDM_CMD_TIMEOUT);
if (ret < 0) {
LOG_ERR("AT+CSQ ret:%d", ret);
}
/* Re-start RSSI query work */
if (work) {
k_work_reschedule_for_queue(&modem_workq,
&mdata.rssi_query_work,
K_SECONDS(RSSI_TIMEOUT_SECS));
}
}
/* Func: pin_init
* Desc: Boot up the Modem.
*/
static void pin_init(void)
{
LOG_INF("Setting Modem Pins");
#if DT_INST_NODE_HAS_PROP(0, mdm_wdisable_gpios)
LOG_INF("Deactivate W Disable");
gpio_pin_set_dt(&wdisable_gpio, 0);
k_sleep(K_MSEC(250));
#endif
/* NOTE: Per the BG95 document, the Reset pin is internally connected to the
* Power key pin.
*/
/* MDM_POWER -> 1 for 500-1000 msec. */
gpio_pin_set_dt(&power_gpio, 1);
k_sleep(K_MSEC(750));
/* MDM_POWER -> 0 and wait for ~2secs as UART remains in "inactive" state
* for some time after the power signal is enabled.
*/
gpio_pin_set_dt(&power_gpio, 0);
k_sleep(K_SECONDS(2));
LOG_INF("... Done!");
}
static const struct modem_cmd response_cmds[] = {
MODEM_CMD("OK", on_cmd_ok, 0U, ""),
MODEM_CMD("ERROR", on_cmd_error, 0U, ""),
MODEM_CMD("+CME ERROR: ", on_cmd_exterror, 1U, ""),
};
static const struct modem_cmd unsol_cmds[] = {
MODEM_CMD("+QIURC: \"recv\",", on_cmd_unsol_recv, 1U, ""),
MODEM_CMD("+QIURC: \"closed\",", on_cmd_unsol_close, 1U, ""),
MODEM_CMD("RDY", on_cmd_unsol_rdy, 0U, ""),
};
/* Commands sent to the modem to set it up at boot time. */
static const struct setup_cmd setup_cmds[] = {
SETUP_CMD_NOHANDLE("ATE0"),
SETUP_CMD_NOHANDLE("ATH"),
SETUP_CMD_NOHANDLE("AT+CMEE=1"),
/* Commands to read info from the modem (things like IMEI, Model etc). */
SETUP_CMD("AT+CGMI", "", on_cmd_atcmdinfo_manufacturer, 0U, ""),
SETUP_CMD("AT+CGMM", "", on_cmd_atcmdinfo_model, 0U, ""),
SETUP_CMD("AT+CGMR", "", on_cmd_atcmdinfo_revision, 0U, ""),
SETUP_CMD("AT+CGSN", "", on_cmd_atcmdinfo_imei, 0U, ""),
#if defined(CONFIG_MODEM_SIM_NUMBERS)
SETUP_CMD("AT+CIMI", "", on_cmd_atcmdinfo_imsi, 0U, ""),
SETUP_CMD("AT+QCCID", "", on_cmd_atcmdinfo_iccid, 0U, ""),
#endif /* #if defined(CONFIG_MODEM_SIM_NUMBERS) */
SETUP_CMD_NOHANDLE("AT+QICSGP=1,1,\"" MDM_APN "\",\""
MDM_USERNAME "\",\"" MDM_PASSWORD "\",1"),
};
/* Func: modem_pdp_context_active
* Desc: This helper function is called from modem_setup, and is
* used to open the PDP context. If there is trouble activating the
* PDP context, we try to deactivate and reactivate MDM_PDP_ACT_RETRY_COUNT times.
* If it fails, we return an error.
*/
static int modem_pdp_context_activate(void)
{
int ret;
int retry_count = 0;
ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler,
NULL, 0U, "AT+QIACT=1", &mdata.sem_response,
MDM_CMD_TIMEOUT);
/* If there is trouble activating the PDP context, we try to deactivate/reactive it. */
while (ret == -EIO && retry_count < MDM_PDP_ACT_RETRY_COUNT) {
ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler,
NULL, 0U, "AT+QIDEACT=1", &mdata.sem_response,
MDM_CMD_TIMEOUT);
/* If there's any error for AT+QIDEACT, restart the module. */
if (ret != 0) {
return ret;
}
ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler,
NULL, 0U, "AT+QIACT=1", &mdata.sem_response,
MDM_CMD_TIMEOUT);
retry_count++;
}
if (ret == -EIO && retry_count >= MDM_PDP_ACT_RETRY_COUNT) {
LOG_ERR("Retried activating/deactivating too many times.");
}
return ret;
}
/* Func: modem_setup
* Desc: This function is used to setup the modem from zero. The idea
* is that this function will be called right after the modem is
* powered on to do the stuff necessary to talk to the modem.
*/
static int modem_setup(void)
{
int ret = 0, counter;
int rssi_retry_count = 0, init_retry_count = 0;
/* Setup the pins to ensure that Modem is enabled. */
pin_init();
restart:
counter = 0;
/* stop RSSI delay work */