-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsm_at_client.c
More file actions
1035 lines (849 loc) · 23.8 KB
/
sm_at_client.c
File metadata and controls
1035 lines (849 loc) · 23.8 KB
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) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <assert.h>
#include <stdlib.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/shell/shell.h>
#include <hal/nrf_gpio.h>
#include <sm_at_client.h>
#include <zephyr/shell/shell_uart.h>
#include <zephyr/shell/shell_rtt.h>
#include <zephyr/pm/device.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(sm_at_client, CONFIG_SM_AT_CLIENT_LOG_LEVEL);
#define UART_RX_MARGIN_MS 10
#define UART_RX_TIMEOUT_US 2000
#define UART_ERROR_DELAY_MS 500
#define UART_TX_TIMEOUT_US 100000
/* Serial Modem has formatted AT response based on TS 27.007 */
#define AT_CMD_OK_STR "\r\nOK\r\n"
#define AT_CMD_ERROR_STR "\r\nERROR\r\n"
#define AT_CMD_CMS_STR "\r\n+CMS ERROR:"
#define AT_CMD_CME_STR "\r\n+CME ERROR:"
static const struct device *uart_dev = DEVICE_DT_GET(DT_CHOSEN(ncs_sm_uart));
static void rx_process(struct k_work *work);
static void dtr_uart_enable_work_fn(struct k_work *work);
static void dtr_uart_disable_work_fn(struct k_work *work);
K_WORK_DELAYABLE_DEFINE(rx_process_work, rx_process);
K_WORK_DEFINE(dtr_uart_enable_work, dtr_uart_enable_work_fn);
K_WORK_DELAYABLE_DEFINE(dtr_uart_disable_work, dtr_uart_disable_work_fn);
struct rx_buf_t {
atomic_t ref_counter;
uint8_t buf[CONFIG_SM_AT_CLIENT_UART_RX_BUF_SIZE];
};
BUILD_ASSERT(CONFIG_SM_AT_CLIENT_AT_CMD_RESP_MAX_SIZE > CONFIG_SM_AT_CLIENT_UART_RX_BUF_SIZE);
/* Slabs for RX buffer. */
#define UART_SLAB_BLOCK_SIZE sizeof(struct rx_buf_t)
#define UART_SLAB_BLOCK_COUNT CONFIG_SM_UART_RX_BUF_COUNT
#define UART_SLAB_ALIGNMENT sizeof(uint32_t)
BUILD_ASSERT(UART_SLAB_BLOCK_SIZE % UART_SLAB_ALIGNMENT == 0);
K_MEM_SLAB_DEFINE_STATIC(rx_slab, UART_SLAB_BLOCK_SIZE, CONFIG_SM_AT_CLIENT_UART_RX_BUF_COUNT,
UART_SLAB_ALIGNMENT);
/* Event queue for RX buffer usage. */
struct rx_event_t {
uint8_t *buf;
size_t len;
};
#define UART_RX_EVENT_COUNT CONFIG_SM_AT_CLIENT_UART_RX_BUF_COUNT
K_MSGQ_DEFINE(rx_event_queue, sizeof(struct rx_event_t), UART_RX_EVENT_COUNT, 1);
/* Ring buffer for TX data. */
RING_BUF_DECLARE(tx_buf, CONFIG_SM_AT_CLIENT_UART_TX_BUF_SIZE);
static K_SEM_DEFINE(tx_done_sem, 0, 1);
static K_SEM_DEFINE(uart_disabled_sem, 0, 1);
enum sm_at_client_uart_state {
SM_AT_CLIENT_TX_ENABLED_BIT,
SM_AT_CLIENT_RX_ENABLED_BIT,
SM_AT_CLIENT_RX_RECOVERY_BIT,
SM_AT_CLIENT_RX_RECOVERY_DISABLED_BIT
};
static atomic_t uart_state;
static sm_ri_handler_t ri_handler;
static K_SEM_DEFINE(at_rsp, 0, 1);
static sm_data_handler_t data_handler;
static enum at_cmd_state sm_at_state;
static bool initialized;
#if defined(CONFIG_SM_AT_CLIENT_SHELL)
static const struct shell *global_shell;
static const char at_usage_str[] = "Usage: sm <at_command>";
#endif
extern void sm_monitor_dispatch(const char *notif, size_t len);
extern char *strnstr(const char *haystack, const char *needle, size_t haystack_sz);
static int dtr_uart_enable(void);
struct dtr_config {
/* DTR */
const struct gpio_dt_spec dtr_gpio;
/* RI */
const struct gpio_dt_spec ri_gpio;
/* Automatically activate DTR UART from RI and disable it after inactivity. */
bool automatic;
k_timeout_t inactivity;
/* Current DTR state. */
bool active;
};
static struct dtr_config dtr_config = {
.dtr_gpio = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(dte_dtr), dtr_gpios, {0}),
.ri_gpio = GPIO_DT_SPEC_GET_OR(DT_NODELABEL(dte_dtr), ri_gpios, {0}),
};
static void gpio_cb_func(const struct device *dev, struct gpio_callback *gpio_cb, uint32_t pins);
static struct gpio_callback gpio_cb;
static inline struct rx_buf_t *block_start_get(uint8_t *buf)
{
size_t block_num;
/* Find the correct block. */
block_num = (((size_t)buf - offsetof(struct rx_buf_t, buf) - (size_t)rx_slab.buffer) /
UART_SLAB_BLOCK_SIZE);
return (struct rx_buf_t *) &rx_slab.buffer[block_num * UART_SLAB_BLOCK_SIZE];
}
static struct rx_buf_t *buf_alloc(void)
{
struct rx_buf_t *buf;
int err;
err = k_mem_slab_alloc(&rx_slab, (void **) &buf, K_NO_WAIT);
if (err) {
return NULL;
}
atomic_set(&buf->ref_counter, 1);
return buf;
}
static void buf_ref(void *buf)
{
atomic_inc(&(block_start_get(buf)->ref_counter));
}
static void buf_unref(void *buf)
{
struct rx_buf_t *uart_buf = block_start_get(buf);
atomic_t ref_counter = atomic_dec(&uart_buf->ref_counter);
/* atomic_dec returns the previous value of the atomic. */
if (ref_counter == 1) {
k_mem_slab_free(&rx_slab, (void *)uart_buf);
}
}
static int rx_enable(void)
{
struct rx_buf_t *buf;
int ret;
if (atomic_test_bit(&uart_state, SM_AT_CLIENT_RX_ENABLED_BIT)) {
return 0;
}
buf = buf_alloc();
if (!buf) {
LOG_ERR("Failed to allocate RX buffer");
return -ENOMEM;
}
ret = uart_rx_enable(uart_dev, buf->buf, sizeof(buf->buf), UART_RX_TIMEOUT_US);
if (ret == -EBUSY) {
LOG_WRN("UART RX already enabled");
buf_unref(buf->buf);
} else if (ret) {
LOG_ERR("uart_rx_enable failed: %d", ret);
buf_unref(buf->buf);
return ret;
}
atomic_set_bit(&uart_state, SM_AT_CLIENT_RX_ENABLED_BIT);
return 0;
}
static int rx_disable(void)
{
int err;
atomic_set_bit(&uart_state, SM_AT_CLIENT_RX_RECOVERY_DISABLED_BIT);
while (atomic_test_bit(&uart_state, SM_AT_CLIENT_RX_RECOVERY_BIT)) {
/* Wait until possible recovery is complete. */
k_sleep(K_MSEC(10));
}
if (!atomic_test_bit(&uart_state, SM_AT_CLIENT_RX_ENABLED_BIT)) {
return 0;
}
k_sem_reset(&uart_disabled_sem);
err = uart_rx_disable(uart_dev);
if (err && err != -EFAULT) {
LOG_ERR("UART RX disable failed: %d", err);
return err;
}
/* Wait until RX is actually disabled. */
err = k_sem_take(&uart_disabled_sem, K_MSEC(100));
if (err) {
LOG_ERR("UART RX disable timeout: %d", err);
}
atomic_clear_bit(&uart_state, SM_AT_CLIENT_RX_ENABLED_BIT);
return 0;
}
static void rx_recovery(void)
{
int err;
if (atomic_test_bit(&uart_state, SM_AT_CLIENT_RX_RECOVERY_DISABLED_BIT)) {
return;
}
atomic_set_bit(&uart_state, SM_AT_CLIENT_RX_RECOVERY_BIT);
err = rx_enable();
if (err) {
k_work_schedule(&rx_process_work, K_MSEC(UART_RX_MARGIN_MS));
}
atomic_clear_bit(&uart_state, SM_AT_CLIENT_RX_RECOVERY_BIT);
}
/* Attempts to find AT responses in the UART buffer. */
static size_t parse_at_response(const char *data, size_t datalen)
{
/* Serial Modem AT responses are formatted based on TS 27.007. */
static const char * const at_responses[] = {
[AT_CMD_OK] = "\r\nOK\r\n",
[AT_CMD_ERROR] = "\r\nERROR\r\n",
[AT_CMD_ERROR_CMS] = "\r\n+CMS ERROR:",
[AT_CMD_ERROR_CME] = "\r\n+CME ERROR:"
};
const char *match = NULL;
/* Look for all the AT state responses. */
for (size_t at_state = 0; at_state != ARRAY_SIZE(at_responses); ++at_state) {
match = strnstr(data, at_responses[at_state], datalen);
if (match) {
if (at_state == AT_CMD_OK || at_state == AT_CMD_ERROR) {
/* Found a match. */
sm_at_state = at_state;
return match - data + strlen(at_responses[at_state]);
}
/* Search for the "\r\n" at the end of the response. */
match += strlen(at_responses[at_state]);
match = strnstr(match, "\r\n", datalen - (match - data));
if (match) {
/* Found a match. */
sm_at_state = at_state;
return match - data + 2;
}
}
}
return 0;
}
static void response_handler(const uint8_t *data, const size_t len)
{
static uint8_t at_cmd_resp[CONFIG_SM_AT_CLIENT_AT_CMD_RESP_MAX_SIZE];
static size_t resp_len;
size_t copy_len;
LOG_HEXDUMP_DBG(data, len, "RX");
copy_len = MIN(len, sizeof(at_cmd_resp) - resp_len);
memcpy(at_cmd_resp + resp_len, data, copy_len);
resp_len += copy_len;
if (sm_at_state == AT_CMD_PENDING) {
size_t processed = parse_at_response(at_cmd_resp, resp_len);
if (processed == 0 && resp_len == sizeof(at_cmd_resp)) {
LOG_ERR("AT-response overflow. Increase "
"CONFIG_SM_AT_CLIENT_AT_CMD_RESP_MAX_SIZE");
processed = resp_len;
}
if (processed > 0) {
#if defined(CONFIG_SM_AT_CLIENT_SHELL)
shell_print(global_shell, "%.*s", processed, (char *)at_cmd_resp);
#endif
if (processed < resp_len) {
/* In case there is data or unsolicited notification
* after the AT-command response, shift the remaining data
* to the beginning of the buffer.
*/
memmove(at_cmd_resp, at_cmd_resp + processed, resp_len - processed);
}
resp_len -= processed;
/* Copy the possibly remaining data to the buffer. */
if (copy_len < len) {
size_t copy2_len = MIN(len - copy_len, sizeof(at_cmd_resp) - resp_len);
memcpy(at_cmd_resp + resp_len, data + copy_len, copy2_len);
resp_len += copy2_len;
}
k_sem_give(&at_rsp);
}
}
if (sm_at_state != AT_CMD_PENDING && resp_len > 0) {
sm_monitor_dispatch((const char *)at_cmd_resp, resp_len);
#if defined(CONFIG_SM_AT_CLIENT_SHELL)
shell_print(global_shell, "%.*s", resp_len, (char *)at_cmd_resp);
#endif
resp_len = 0;
}
if (data_handler) {
data_handler(data, len);
}
}
static void rx_process(struct k_work *work)
{
struct rx_event_t rx_event;
while (k_msgq_get(&rx_event_queue, &rx_event, K_NO_WAIT) == 0) {
response_handler(rx_event.buf, rx_event.len);
buf_unref(rx_event.buf);
}
rx_recovery();
}
static int tx_start(void)
{
uint8_t *buf;
size_t len;
int err;
if (!atomic_test_bit(&uart_state, SM_AT_CLIENT_TX_ENABLED_BIT)) {
return -EAGAIN;
}
len = ring_buf_get_claim(&tx_buf, &buf, ring_buf_capacity_get(&tx_buf));
err = uart_tx(uart_dev, buf, len, UART_TX_TIMEOUT_US);
if (err) {
LOG_ERR("UART TX error: %d", err);
ring_buf_get_finish(&tx_buf, 0);
return err;
}
return 0;
}
/* Write the data to tx_buffer and trigger sending. */
static int tx_write(const uint8_t *data, size_t len, bool flush)
{
size_t ret;
size_t sent = 0;
int err;
LOG_HEXDUMP_DBG(data, len, "TX");
if (dtr_config.automatic && !dtr_config.active) {
err = dtr_uart_enable();
if (err) {
LOG_ERR("Failed to enable DTR (%d).", err);
}
}
while (sent < len) {
ret = ring_buf_put(&tx_buf, data + sent, len - sent);
if (ret) {
sent += ret;
} else {
/* Buffer full, block and start TX. */
if (atomic_test_bit(&uart_state, SM_AT_CLIENT_TX_ENABLED_BIT) == 0) {
LOG_ERR("TX disabled, %zu bytes dropped", len - sent);
return -EIO;
}
k_sem_take(&tx_done_sem, K_FOREVER);
err = tx_start();
if (err) {
LOG_ERR("TX buf overflow, %d dropped. Unable to send: %d",
len - sent,
err);
k_sem_give(&tx_done_sem);
return err;
}
}
}
if (flush) {
if (atomic_test_bit(&uart_state, SM_AT_CLIENT_TX_ENABLED_BIT) == 0) {
LOG_INF("TX disabled, data will be sent when enabled");
return -EAGAIN;
}
if (k_sem_take(&tx_done_sem, K_NO_WAIT) == 0) {
err = tx_start();
if (err) {
LOG_ERR("tx_start failed: %d", err);
k_sem_give(&tx_done_sem);
return err;
}
}
}
return 0;
}
static int tx_enable(void)
{
if (!atomic_test_and_set_bit(&uart_state, SM_AT_CLIENT_TX_ENABLED_BIT)) {
k_sem_give(&tx_done_sem);
}
return 0;
}
static int tx_disable(k_timeout_t timeout)
{
int err;
if (!atomic_test_and_clear_bit(&uart_state, SM_AT_CLIENT_TX_ENABLED_BIT)) {
return 0;
}
if (k_sem_take(&tx_done_sem, timeout) == 0) {
return 0;
}
err = uart_tx_abort(uart_dev);
if (!err) {
LOG_INF("TX aborted");
} else if (err != -EFAULT) {
LOG_ERR("uart_tx_abort failed (%d).", err);
return err;
}
return 0;
}
static void reschedule_disable(void)
{
if (dtr_config.active && dtr_config.automatic) {
/* Restart the inactivity timer. */
k_work_reschedule(&dtr_uart_disable_work, dtr_config.inactivity);
}
}
static void uart_callback(const struct device*, struct uart_event *evt, void*)
{
struct rx_buf_t *buf;
struct rx_event_t rx_event;
int err;
switch (evt->type) {
case UART_TX_DONE:
err = ring_buf_get_finish(&tx_buf, evt->data.tx.len);
if (err) {
LOG_ERR("UART_TX_%s, ring_buf_get_finish: %d", "DONE", err);
}
if (ring_buf_is_empty(&tx_buf)) {
k_sem_give(&tx_done_sem);
reschedule_disable();
break;
}
err = tx_start();
if (err) {
LOG_ERR("tx_start failed: %d", err);
k_sem_give(&tx_done_sem);
}
break;
case UART_TX_ABORTED:
err = ring_buf_get_finish(&tx_buf, evt->data.tx.len);
if (err) {
LOG_ERR("UART_TX_%s, ring_buf_get_finish: %d", "ABORTED", err);
}
LOG_WRN("UART_TX_ABORTED, dropped: %d bytes", ring_buf_size_get(&tx_buf));
ring_buf_reset(&tx_buf);
k_sem_give(&tx_done_sem);
k_sem_reset(&at_rsp);
reschedule_disable();
break;
case UART_RX_RDY:
buf_ref(evt->data.rx.buf);
rx_event.buf = &evt->data.rx.buf[evt->data.rx.offset];
rx_event.len = evt->data.rx.len;
err = k_msgq_put(&rx_event_queue, &rx_event, K_NO_WAIT);
if (err) {
LOG_ERR("RX_RDY failure: %d, dropped: %d", err, evt->data.rx.len);
buf_unref(evt->data.rx.buf);
break;
}
k_work_submit((struct k_work *)&rx_process_work);
if (ring_buf_is_empty(&tx_buf)) {
reschedule_disable();
}
break;
case UART_RX_BUF_REQUEST:
buf = buf_alloc();
if (!buf) {
break;
}
err = uart_rx_buf_rsp(uart_dev, buf->buf, sizeof(buf->buf));
if (err) {
LOG_WRN("Disabling UART RX: %d", err);
buf_unref(buf);
}
break;
case UART_RX_BUF_RELEASED:
if (evt->data.rx_buf.buf) {
buf_unref(evt->data.rx_buf.buf);
}
break;
case UART_RX_DISABLED:
atomic_clear_bit(&uart_state, SM_AT_CLIENT_RX_ENABLED_BIT);
k_sem_give(&uart_disabled_sem);
k_work_submit((struct k_work *)&rx_process_work);
break;
default:
break;
}
}
static int uart_init(const struct device *uart_dev)
{
int err;
struct uart_config uart_conf;
if (!device_is_ready(uart_dev)) {
LOG_ERR("UART device not ready");
return -ENODEV;
}
err = uart_config_get(uart_dev, &uart_conf);
if (err) {
LOG_ERR("uart_config_get: %d", err);
return err;
}
LOG_INF("UART baud: %d d/p/s-bits: %d/%d/%d HWFC: %d",
uart_conf.baudrate, uart_conf.data_bits, uart_conf.parity,
uart_conf.stop_bits, uart_conf.flow_ctrl);
/* Wait for the UART line to become valid */
uint32_t start_time = k_uptime_get_32();
do {
err = uart_err_check(uart_dev);
if (err) {
uint32_t now = k_uptime_get_32();
if (now - start_time > UART_ERROR_DELAY_MS) {
LOG_ERR("UART check failed: %d", err);
return -EIO;
}
k_sleep(K_MSEC(10));
}
} while (err);
/* Register async handling callback */
err = uart_callback_set(uart_dev, uart_callback, NULL);
if (err) {
LOG_ERR("Cannot set callback: %d", err);
return -EFAULT;
}
/* Enable RX */
atomic_clear(&uart_state);
err = rx_enable();
/* Enable TX */
tx_enable();
return err;
}
static int dtr_pin_set(bool level)
{
int err;
if (gpio_is_ready_dt(&dtr_config.dtr_gpio)) {
err = gpio_pin_set_dt(&dtr_config.dtr_gpio, level);
if (err) {
LOG_ERR("Failed to set DTR pin: %d", err);
return -EFAULT;
}
} else {
LOG_WRN("DTR pin not configured");
return -EFAULT;
}
return 0;
}
static int dtr_uart_disable(void)
{
int err;
LOG_DBG("DTR UART disable: %s.", "Start");
/* Set DTR state at beginning so that we have correct state when RI comes. */
dtr_config.active = false;
/* Wait until TX is done and disable TX. */
err = tx_disable(K_NO_WAIT);
if (err) {
LOG_ERR("TX disable failed (%d).", err);
return err;
}
/* Ask Serial Modem to disable UART. */
err = dtr_pin_set(0);
if (err) {
LOG_ERR("Failed to set DTR pin (%d).", err);
return err;
}
/* Optional: Wait for possible Serial Modem TX to complete. */
/* k_sleep(K_MSEC(100)); */
/* Disable RX: UART is automatically suspended. */
err = rx_disable();
if (err) {
LOG_ERR("RX disable failed (%d).", err);
return err;
}
LOG_DBG("DTR UART disable: %s.", "Done");
return 0;
}
static void dtr_uart_disable_work_fn(struct k_work *work)
{
int err;
ARG_UNUSED(work);
err = dtr_uart_disable();
if (err) {
LOG_ERR("Failed to disable DTR UART (%d).", err);
}
}
static int dtr_uart_enable(void)
{
int err;
LOG_DBG("DTR UART enable: %s.", "Start");
/* Set DTR state at beginning so that we have correct state when RI comes. */
dtr_config.active = true;
/* Enable RX: UART is automatically resumed. */
atomic_clear_bit(&uart_state, SM_AT_CLIENT_RX_RECOVERY_DISABLED_BIT);
err = rx_enable();
if (err) {
LOG_ERR("Failed to enable RX (%d).", err);
return err;
}
/* Ask Serial Modem to enable UART. */
err = dtr_pin_set(1);
if (err) {
LOG_ERR("Failed to set DTR pin (%d).", err);
return err;
}
/* Optional: Wait for Serial Modem to be ready */
/* k_sleep(K_MSEC(100)); */
/* Enable TX. */
err = tx_enable();
if (err) {
LOG_ERR("Failed to enable TX (%d).", err);
return err;
}
/* Start TX in case there is pending data. */
if (!ring_buf_is_empty(&tx_buf)) {
err = tx_start();
if (err) {
LOG_ERR("Failed to start TX (%d).", err);
return err;
}
}
LOG_DBG("DTR UART enable: %s.", "Done");
return 0;
}
static void dtr_uart_enable_work_fn(struct k_work *work)
{
int err;
ARG_UNUSED(work);
err = dtr_uart_enable();
if (err) {
LOG_ERR("Failed to enable DTR UART (%d).", err);
}
}
static void gpio_cb_func(const struct device *dev, struct gpio_callback *gpio_cb, uint32_t pins)
{
if ((BIT(dtr_config.ri_gpio.pin) & pins) == 0) {
return;
}
if (dtr_config.automatic && !dtr_config.active) {
/* Wake up the application */
k_work_submit(&dtr_uart_enable_work);
}
if (ri_handler) {
ri_handler();
} else {
LOG_INF("Ring Indicate received, but no handler registered");
}
}
static int gpio_init(void)
{
int err;
if (gpio_is_ready_dt(&dtr_config.dtr_gpio)) {
err = gpio_pin_configure_dt(&dtr_config.dtr_gpio, GPIO_OUTPUT_ACTIVE);
if (err) {
LOG_ERR("Failed to configure DTR pin: %d", err);
return -EFAULT;
}
} else {
LOG_WRN("DTR GPIO is not ready");
}
if (gpio_is_ready_dt(&dtr_config.ri_gpio)) {
gpio_flags_t flags;
nrf_gpio_pin_sense_t sense;
err = gpio_pin_configure_dt(&dtr_config.ri_gpio, GPIO_INPUT);
if (err) {
LOG_ERR("GPIO config error: %d", err);
return err;
}
err = gpio_pin_get_config_dt(&dtr_config.ri_gpio, &flags);
if (err) {
LOG_ERR("Failed to get RI pin config: %d", err);
return err;
}
if (flags & GPIO_PULL_DOWN) {
LOG_DBG("Wakeup sense %s", "high");
sense = NRF_GPIO_PIN_SENSE_HIGH;
} else {
LOG_DBG("Wakeup sense %s", "low");
sense = NRF_GPIO_PIN_SENSE_LOW;
}
int port = DT_PROP(DT_PHANDLE(DT_NODELABEL(dte_dtr), ri_gpios), port);
uint32_t abs_pin = NRF_PIN_PORT_TO_PIN_NUMBER(dtr_config.ri_gpio.pin, port);
nrf_gpio_cfg_sense_set(abs_pin, sense);
gpio_init_callback(&gpio_cb, gpio_cb_func, BIT(dtr_config.ri_gpio.pin));
err = gpio_add_callback_dt(&dtr_config.ri_gpio, &gpio_cb);
if (err) {
LOG_WRN("GPIO add callback error: %d", err);
return err;
}
err = gpio_pin_interrupt_configure_dt(&dtr_config.ri_gpio, GPIO_INT_EDGE_TO_ACTIVE);
if (err) {
LOG_WRN("GPIO interrupt configure error: %d", err);
return err;
}
} else {
LOG_WRN("RI GPIO is not ready");
}
return 0;
}
int sm_at_client_init(sm_data_handler_t handler, bool automatic, k_timeout_t inactivity)
{
int err;
if (initialized) {
return -EALREADY;
}
initialized = true;
data_handler = handler;
ri_handler = NULL;
sm_at_state = AT_CMD_OK;
err = gpio_init();
if (err) {
LOG_ERR("GPIO init (%d)", err);
return -EFAULT;
}
err = uart_init(uart_dev);
if (err) {
LOG_ERR("UART init (%d)", err);
return -EFAULT;
}
/* Initialize shell pointer so it's available for printing in callbacks */
#if defined(CONFIG_SHELL_BACKEND_SERIAL)
global_shell = shell_backend_uart_get_ptr();
#elif defined(CONFIG_SHELL_BACKEND_RTT)
global_shell = shell_backend_rtt_get_ptr();
#endif
dtr_config.automatic = automatic;
dtr_config.inactivity = inactivity;
dtr_config.active = true;
reschedule_disable();
return 0;
}
int sm_at_client_uninit(void)
{
int err;
err = dtr_uart_disable();
if (err) {
LOG_ERR("Failed to disable DTR UART (%d).", err);
}
err = gpio_pin_configure_dt(&dtr_config.dtr_gpio, GPIO_DISCONNECTED);
if (err) {
LOG_ERR("Failed to disconnect DTR pin: %d", err);
}
data_handler = NULL;
ri_handler = NULL;
sm_at_state = AT_CMD_OK;
initialized = false;
return 0;
}
int sm_at_client_register_ri_handler(sm_ri_handler_t handler)
{
if (!gpio_is_ready_dt(&dtr_config.ri_gpio)) {
LOG_WRN("RI GPIO is not ready");
return -EFAULT;
}
ri_handler = handler;
return 0;
}
int sm_at_client_send_cmd(const char *const command, uint32_t timeout)
{
int ret;
if (!initialized) {
LOG_ERR("AT client not initialized");
return -EPERM;
}
sm_at_state = AT_CMD_PENDING;
ret = tx_write(command, strlen(command), false);
if (ret < 0) {
return ret;
}
/* send AT command terminator */
#if defined(CONFIG_SM_AT_CLIENT_CR_LF_TERMINATION)
ret = tx_write("\r\n", 2, true);
#elif defined(CONFIG_SM_AT_CLIENT_CR_TERMINATION)
ret = tx_write("\r", 1, true);
#elif defined(CONFIG_SM_AT_CLIENT_LF_TERMINATION)
ret = tx_write("\n", 1, true);
#endif
if (ret < 0) {
return ret;
}
if (timeout != 0) {
ret = k_sem_take(&at_rsp, K_SECONDS(timeout));
} else {
ret = k_sem_take(&at_rsp, K_FOREVER);
}
if (ret) {
LOG_ERR("timeout");
sm_at_state = AT_CMD_ERROR;
return ret;
}
return sm_at_state;
}
int sm_at_client_send_data(const uint8_t *const data, size_t datalen)
{
if (!initialized) {
LOG_ERR("AT client not initialized");
return -EPERM;
}
return tx_write(data, datalen, true);
}
void sm_at_client_automatic_dtr_uart(k_timeout_t inactivity)
{
if (!initialized) {
LOG_ERR("AT client not initialized");
return;
}
dtr_config.automatic = true;
dtr_config.inactivity = inactivity;
if (!dtr_config.active && !ring_buf_is_empty(&tx_buf)) {
/* Automatic mode enabled with pending TX data: trigger enable. */
k_work_submit(&dtr_uart_enable_work);
}
if (dtr_config.active) {
/* Restart the inactivity timer. */
k_work_reschedule(&dtr_uart_disable_work, dtr_config.inactivity);
}
}
void sm_at_client_disable_dtr_uart(void)
{
if (!initialized) {
LOG_ERR("AT client not initialized");
return;
}
dtr_config.automatic = false;
k_work_cancel(&dtr_uart_enable_work);
k_work_reschedule(&dtr_uart_disable_work, K_NO_WAIT);
}
void sm_at_client_enable_dtr_uart(void)
{
if (!initialized) {
LOG_ERR("AT client not initialized");
return;
}
dtr_config.automatic = false;
k_work_cancel_delayable(&dtr_uart_disable_work);
k_work_submit(&dtr_uart_enable_work);
}
#if defined(CONFIG_SM_AT_CLIENT_SHELL)
int sm_at_client_shell(const struct shell *shell, size_t argc, char **argv)
{
if (argc < 2) {
shell_print(shell, "%s", at_usage_str);
return 0;
}
return sm_at_client_send_cmd(argv[1], 10);
}
int sm_at_client_shell_smsh_dtr_uart_disable(const struct shell *shell, size_t argc, char **argv)
{
shell_print(shell, "Disable DTR UART.");
sm_at_client_disable_dtr_uart();
return 0;
}
int sm_at_client_shell_smsh_dtr_uart_enable(const struct shell *shell, size_t argc, char **argv)
{
shell_print(shell, "Enable DTR UART.");
sm_at_client_enable_dtr_uart();
return 0;
}
int sm_at_client_shell_smsh_dtr_uart_auto(const struct shell *shell, size_t argc, char **argv)
{
uint32_t timeout = 0;
if (argc >= 2) {
timeout = strtoul(argv[1], NULL, 10);
}
if (timeout == 0) {
shell_print(shell, "Usage: smsh uart auto <timeout in ms>");