summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/cell_modem.c
blob: b279db190c35129b5abdeca455e17ed9a3896074 (plain)
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
#include <stdlib.h>
#include <string.h>

#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <netif/ppp/pppos.h>
#include <netif/ppp/pppapi.h>
#include <driver/uart.h>
#include <driver/gpio.h>
#include <esp_timer.h>
#include <esp_sleep.h>
#include <esp_log.h>

#include "eos.h"
#include "net.h"
#include "power.h"

#include "at_cmd.h"
#include "cell.h"

// XXX: PPP reconnect on failure

#define UART_SIZE_IO_BUF    8192

#define UART_GPIO_TXD       16
#define UART_GPIO_RXD       17
#define UART_GPIO_DTR       32
#define UART_GPIO_RI        35

#define MODEM_ETYPE_ATINIT  1
#define MODEM_ETYPE_STATUS  2
#define MODEM_ETYPE_RING    3

#define AT_CMD_INIT_SIZE    5

#define MIN(X, Y)           (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y)           (((X) > (Y)) ? (X) : (Y))

static const char *TAG = "EOS MODEM";

static char *at_cmd_init[AT_CMD_INIT_SIZE] = {
    "AT+CFGRI=1\r",
    "AT+CSCLK=1\r",
    "AT+CLIP=1\r",
    "AT+CMGF=0\r",
    "AT+CPMS=\"ME\",\"ME\",\"ME\"\r"
};

static SemaphoreHandle_t mutex;

static QueueHandle_t modem_queue;
static QueueHandle_t uart_queue;

static char urc_buf[EOS_CELL_UART_SIZE_BUF];
static char uart_buf[EOS_CELL_UART_SIZE_BUF];
static size_t uart_buf_len;
static char uart_buf_dirty = 0;

static uint8_t RTC_NOINIT_ATTR modem_present;
static uint8_t RTC_NOINIT_ATTR modem_initialized;
static uint8_t RTC_NOINIT_ATTR uart_mode;
static uint8_t uart_mode_next = EOS_CELL_UART_MODE_NONE;
static SemaphoreHandle_t uart_mutex;

static char ppp_apn[EOS_CELL_PDP_SIZE_APN + 1];
static char ppp_usr[EOS_CELL_PDP_SIZE_USR + 1];
static char ppp_pwd[EOS_CELL_PDP_SIZE_PWD + 1];
static SemaphoreHandle_t ppp_mutex;

static ppp_pcb *ppp_handle;
static int ppp_connected;
static ip_addr_t ppp_ipaddr;
static struct netif ppp_netif;

typedef enum {
    UART_EEVT_MODE = UART_EVENT_MAX
} uart_eevt_type_t;

typedef struct {
    uint8_t type;
    unsigned char param[2];
    size_t param_len;
} modem_event_t;

static void modem_send_status(void);

static void atcmd_read(size_t bsize) {
    char *ln_end;
    int rd = 0;

    do {
        char *uart_curr = uart_buf + uart_buf_len;
        int _rd = eos_modem_read(uart_curr, MIN(bsize - rd, sizeof(uart_buf) - uart_buf_len - 1), 100);

        rd += _rd;
        uart_buf_len += _rd;
        uart_buf[uart_buf_len] = '\0';
        while ((ln_end = strchr(uart_curr, '\n'))) {
            char *_ln_end = ln_end;

            while ((_ln_end > uart_buf) && (*(_ln_end - 1) == '\r')) _ln_end--;
            memcpy(urc_buf, uart_buf, _ln_end - uart_buf);
            urc_buf[_ln_end - uart_buf] = '\0';

            uart_buf_len -= ln_end - uart_buf + 1;
            if (uart_buf_len) memmove(uart_buf, ln_end + 1, uart_buf_len);
            if (!uart_buf_dirty) at_urc_process(urc_buf);

            uart_curr = uart_buf;
            uart_buf[uart_buf_len] = '\0';
            uart_buf_dirty = 0;
        }
        if (uart_buf_len == sizeof(uart_buf) - 1) {
            uart_buf_len = 0;
            uart_buf_dirty = 1;
        }
    } while (rd != bsize);
}

static void uart_data_read(uint8_t mode) {
    int rd;
    size_t bsize;

    uart_get_buffered_data_len(UART_NUM_2, &bsize);
    switch (mode) {
        case EOS_CELL_UART_MODE_ATCMD: {
            atcmd_read(bsize);
            break;
        }

        case EOS_CELL_UART_MODE_PPP: {
            rd = 0;

            do {
                int _rd = eos_modem_read(uart_buf, MIN(bsize - rd, sizeof(uart_buf)), 100);
                if (ppp_handle) pppos_input_tcpip(ppp_handle, (uint8_t *)uart_buf, _rd);
                rd += _rd;
            } while (rd != bsize);
            break;
        }

        case EOS_CELL_UART_MODE_RELAY: {
            unsigned char *buf;
            rd = 0;

            do {
                int _rd;

                buf = eos_net_alloc();
                buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_UART_DATA;
                _rd = eos_modem_read(buf + 1, MIN(bsize - rd, EOS_NET_SIZE_BUF - 1), 100);
                eos_net_send(EOS_NET_MTYPE_CELL, buf, _rd + 1);
                rd += _rd;
            } while (rd != bsize);
            break;
        }
    }
}

static void uart_event_task(void *pvParameters) {
    char mode;
    char _mode;
    uart_event_t event;

    eos_power_wait4wake();

    xSemaphoreTake(mutex, portMAX_DELAY);
    _mode = uart_mode;
    xSemaphoreTake(uart_mutex, portMAX_DELAY);

    if (!modem_initialized) {
        int r;

        at_cmd("\r\rAT\r");
        r = at_expect("^OK", NULL, 500);
        if (!r) {
            r = eos_modem_atinit();
            if (!r) {
                modem_present = 1;
            } else {
                _mode = EOS_CELL_UART_MODE_NONE;
                ESP_LOGE(TAG, "Modem init failed");
            }
        }
    }

    if (_mode == EOS_CELL_UART_MODE_NONE) xSemaphoreGive(uart_mutex);
    mode = _mode;
    xSemaphoreGive(mutex);

    while (1) {
        /* Waiting for UART event.
         */
        if (xQueueReceive(uart_queue, &event, portMAX_DELAY)) {
            switch (event.type) {
                case UART_DATA: {
                    /* Event of UART receiving data
                     */
                    if (mode != EOS_CELL_UART_MODE_NONE) uart_data_read(mode);
                    if ((mode != _mode) && (uart_buf_len == 0)) {
                        if (_mode == EOS_CELL_UART_MODE_NONE) xSemaphoreGive(uart_mutex);
                        mode = _mode;
                    }
                    break;
                }

                case UART_EEVT_MODE: {
                    /* Mode change
                     */
                    _mode = (char)event.size;
                    if ((_mode != mode) && ((uart_buf_len == 0) || (mode == EOS_CELL_UART_MODE_NONE))) {
                        if (mode == EOS_CELL_UART_MODE_NONE) {
                            xSemaphoreTake(uart_mutex, portMAX_DELAY);
                            uart_data_read(_mode);
                        }
                        if (_mode == EOS_CELL_UART_MODE_NONE) xSemaphoreGive(uart_mutex);
                        mode = _mode;
                    }
                    break;
                }

                default:
                    break;
            }
        }
    }
    vTaskDelete(NULL);
}

static void uart_change_mode(uint8_t mode) {
    uart_event_t evt;

    evt.type = UART_EEVT_MODE;
    evt.size = mode;
    xQueueSend(uart_queue, &evt, portMAX_DELAY);
}

static void IRAM_ATTR uart_ring_handler(void *arg) {
    modem_event_t evt;

    evt.type = MODEM_ETYPE_RING;
    xQueueSendFromISR(modem_queue, &evt, NULL);
}

static void modem_init_gpio(void) {
    // Configuration for the DTR/RI lines
    gpio_config_t io_conf = {};

    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pin_bit_mask = ((uint64_t)1 << UART_GPIO_DTR);
    io_conf.pull_up_en = 0;
    io_conf.pull_down_en = 0;
    gpio_config(&io_conf);
    gpio_set_level(UART_GPIO_DTR, 0);

    io_conf.intr_type = GPIO_INTR_NEGEDGE;
    io_conf.mode = GPIO_MODE_INPUT;
    io_conf.pin_bit_mask = ((uint64_t)1 << UART_GPIO_RI);
    io_conf.pull_up_en = 0;
    io_conf.pull_down_en = 0;
    gpio_config(&io_conf);
    gpio_isr_handler_add(UART_GPIO_RI, uart_ring_handler, NULL);
}

static size_t modem_get_status(unsigned char *buffer) {
    size_t len;

    len = 1;
    if (modem_initialized) {
        switch (uart_mode) {
            case EOS_CELL_UART_MODE_ATCMD: {
                buffer[0] = EOS_CELL_STATUS_IDLE;
                break;
            }

            case EOS_CELL_UART_MODE_RELAY: {
                buffer[0] = EOS_CELL_STATUS_RELAY;
                break;
            }

            case EOS_CELL_UART_MODE_PPP: {
                buffer[0] = EOS_CELL_STATUS_PPP;
                buffer[1] = ppp_connected;
                len = 2;
                break;
            }
        }
    } else {
        buffer[0] = EOS_CELL_STATUS_RESET;
    }

    return len;
}

static void modem_send_status(void) {
    modem_event_t evt;

    evt.type = MODEM_ETYPE_STATUS;
    evt.param_len = modem_get_status(evt.param);
    xQueueSend(modem_queue, &evt, portMAX_DELAY);
}

static void modem_atinit_handler(char *urc, regmatch_t m[]) {
    modem_event_t evt;

    evt.type = MODEM_ETYPE_ATINIT;
    xQueueSend(modem_queue, &evt, portMAX_DELAY);
}

static int modem_atinit(void) {
    int r;

    xSemaphoreTake(mutex, portMAX_DELAY);
    uart_change_mode(EOS_CELL_UART_MODE_NONE);
    r = xSemaphoreTake(uart_mutex, 1000);
    if (r == pdFALSE) {
        uart_change_mode(uart_mode);
        xSemaphoreGive(mutex);
        return EOS_ERR_TIMEOUT;
    }

    r = eos_modem_atinit();
    uart_change_mode(uart_mode);
    xSemaphoreGive(uart_mutex);
    xSemaphoreGive(mutex);

    return r;
}

static void modem_event_task(void *pvParameters) {
    modem_event_t evt;

    while (1) {
        if (xQueueReceive(modem_queue, &evt, portMAX_DELAY)) {
            switch (evt.type) {
                case MODEM_ETYPE_ATINIT: {
                    int r;

                    r = modem_atinit();
                    if (!r) {
                        modem_present = 1;
                    } else {
                        ESP_LOGE(TAG, "Modem init failed");
                    }
                    break;
                }

                case MODEM_ETYPE_STATUS: {
                    unsigned char *buf;

                    buf = eos_net_alloc();
                    buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_STATUS;
                    memcpy(buf + 1, evt.param, evt.param_len);
                    eos_net_send(EOS_NET_MTYPE_CELL, buf, evt.param_len + 1);
                    break;
                }

                case MODEM_ETYPE_RING: {
                    ESP_LOGI(TAG, "URC from RI");
                    break;
                }
            }
            /* Obsolete
            uint64_t t_start = esp_timer_get_time();
            if (xQueueReceive(modem_queue, &level, 200 / portTICK_RATE_MS) && (level == 1)) {
                uint64_t t_end = esp_timer_get_time();
                ESP_LOGI(TAG, "URC:%u", (uint32_t)(t_end - t_start));
            } else {
                ESP_LOGI(TAG, "RING");
            }
            */
        }
    }
    vTaskDelete(NULL);
}

static char *memstr(char *mem, size_t size, char *str) {
    size_t i = 0;
    char *max_mem;

    if (str[0] == '\0') return NULL;

    max_mem = mem + size;

    while (mem < max_mem) {
        if (*mem != str[i]) {
            mem -= i;
            i = 0;
        } else {
            if (str[i+1] == '\0') return mem - i;
            i++;
        }
        mem++;
    }

    return NULL;
}

static uint32_t ppp_output_cb(ppp_pcb *pcb, uint8_t *data, uint32_t len, void *ctx) {
    size_t rv;

	xSemaphoreTake(ppp_mutex, portMAX_DELAY);
    rv = eos_modem_write(data, len);
	xSemaphoreGive(ppp_mutex);

    return rv;
}

/* PPP status callback */
static void ppp_status_cb(ppp_pcb *pcb, int err_code, void *ctx) {
    struct netif *pppif = ppp_netif(pcb);

    LWIP_UNUSED_ARG(ctx);

    switch (err_code) {
        case PPPERR_NONE: {
            ESP_LOGI(TAG, "status_cb: Connect");
            ESP_LOGI(TAG,"   our_ipaddr  = %s\n", ipaddr_ntoa(&pppif->ip_addr));
            ESP_LOGI(TAG,"   his_ipaddr  = %s\n", ipaddr_ntoa(&pppif->gw));
            ESP_LOGI(TAG,"   netmask     = %s\n", ipaddr_ntoa(&pppif->netmask));

            xSemaphoreTake(mutex, portMAX_DELAY);
            ppp_connected = 1;
            ip_addr_copy(ppp_ipaddr, pppif->ip_addr);
            modem_send_status();
            xSemaphoreGive(mutex);

            return;
        }
        case PPPERR_PARAM: {
            ESP_LOGE(TAG, "status_cb: Invalid parameter");
            break;
        }
        case PPPERR_OPEN: {
            ESP_LOGE(TAG, "status_cb: Unable to open PPP session");
            break;
        }
        case PPPERR_DEVICE: {
            ESP_LOGE(TAG, "status_cb: Invalid I/O device for PPP");
            break;
        }
        case PPPERR_ALLOC: {
            ESP_LOGE(TAG, "status_cb: Unable to allocate resources");
            break;
        }
        case PPPERR_USER: {
            ESP_LOGI(TAG, "status_cb: Disconnect");
            break;
        }
        case PPPERR_CONNECT: {
            ESP_LOGE(TAG, "status_cb: Connection lost");
            break;
        }
        case PPPERR_AUTHFAIL: {
            ESP_LOGE(TAG, "status_cb: Failed authentication challenge");
            break;
        }
        case PPPERR_PROTOCOL: {
            ESP_LOGE(TAG, "status_cb: Failed to meet protocol");
            break;
        }
        case PPPERR_PEERDEAD: {
            ESP_LOGE(TAG, "status_cb: Connection timeout");
            break;
        }
        case PPPERR_IDLETIMEOUT: {
            ESP_LOGE(TAG, "status_cb: Idle Timeout");
            break;
        }
        case PPPERR_CONNECTTIME: {
            ESP_LOGE(TAG, "status_cb: Max connect time reached");
            break;
        }
        case PPPERR_LOOPBACK: {
            ESP_LOGE(TAG, "status_cb: Loopback detected");
            break;
        }
        default: {
            ESP_LOGE(TAG, "status_cb: Unknown error code %d", err_code);
            break;
        }
    }

    xSemaphoreTake(mutex, portMAX_DELAY);
    if (uart_mode_next != EOS_CELL_UART_MODE_NONE) {
        uart_mode = uart_mode_next;
        uart_mode_next = EOS_CELL_UART_MODE_NONE;
    } else {
        uart_mode = EOS_CELL_UART_MODE_ATCMD;
    }
    ppp_connected = 0;
    ip_addr_set_zero(&ppp_ipaddr);
    modem_send_status();
    uart_change_mode(EOS_CELL_UART_MODE_NONE);
    xSemaphoreTake(uart_mutex, portMAX_DELAY);

    ppp_handle = NULL;
    uart_change_mode(uart_mode);

    xSemaphoreGive(uart_mutex);
    xSemaphoreGive(mutex);

    ppp_free(pcb);
}

static int ppp_pause(uint32_t timeout) {
    int done = 0;
    int len = 0;
    int rv = EOS_OK;
    int start = 0;
    int r;

    char *ok_str = NULL;
    uint64_t t_start;
    uint32_t dt, _dt;

    uart_change_mode(EOS_CELL_UART_MODE_NONE);

    t_start = esp_timer_get_time();
    r = xSemaphoreTake(uart_mutex, timeout ? timeout / portTICK_PERIOD_MS : portMAX_DELAY);
    if (r == pdFALSE) return EOS_ERR_TIMEOUT;
    if (timeout) {
        dt = ((esp_timer_get_time() - t_start) / 1000);
        if (dt >= timeout) {
            uart_change_mode(EOS_CELL_UART_MODE_PPP);
            xSemaphoreGive(uart_mutex);
            return EOS_ERR_TIMEOUT;
        }
    }
    r = xSemaphoreTake(ppp_mutex, timeout ? (timeout - dt) / portTICK_PERIOD_MS : portMAX_DELAY);
    if (r == pdFALSE) {
        uart_change_mode(EOS_CELL_UART_MODE_PPP);
        xSemaphoreGive(uart_mutex);
        return EOS_ERR_TIMEOUT;
    }
    eos_modem_flush();

    _dt = ((esp_timer_get_time() - t_start) / 1000);
    do {
        len = eos_modem_read(uart_buf + uart_buf_len, sizeof(uart_buf) - uart_buf_len, 10);
        dt = ((esp_timer_get_time() - t_start) / 1000);
        if ((dt - _dt) >= 1000) {
            _dt =dt;
            at_cmd("+++");
            start = 1;
        }
        if (start && (len > 0)) {
            if (uart_buf_len > 5) {
                ok_str = memstr(uart_buf + uart_buf_len - 5, len + 5, "\r\nOK\r\n");
            } else {
                ok_str = memstr(uart_buf, uart_buf_len + len, "\r\nOK\r\n");
            }
            uart_buf_len += len;
        }
        if (ok_str) {
            if (ppp_handle) pppos_input_tcpip(ppp_handle, (uint8_t *)uart_buf, ok_str - uart_buf);
            ok_str += 6;
            uart_buf_len -= ok_str - uart_buf;
            if (uart_buf_len) memmove(uart_buf, ok_str, uart_buf_len);
            done = 1;
        } else if (uart_buf_len == sizeof(uart_buf)) {
            if (ppp_handle) pppos_input_tcpip(ppp_handle, (uint8_t *)uart_buf, sizeof(uart_buf) / 2);
            memcpy(uart_buf, uart_buf + sizeof(uart_buf) / 2, sizeof(uart_buf) / 2);
            uart_buf_len = sizeof(uart_buf) / 2;
        }
        if (!done && timeout && (dt >= timeout)) {
            uart_change_mode(EOS_CELL_UART_MODE_PPP);
            xSemaphoreGive(uart_mutex);
            xSemaphoreGive(ppp_mutex);
            return EOS_ERR_TIMEOUT;
        }
    } while (!done);

    return rv;
}

static int ppp_resume(void) {
    int r;
    int rv = EOS_OK;

    at_cmd("ATO\r");
    r = at_expect("^CONNECT", "^(ERROR|NO CARRIER)", 1000);
    if (r) rv = EOS_ERR;

    uart_change_mode(EOS_CELL_UART_MODE_PPP);
    xSemaphoreGive(uart_mutex);
    xSemaphoreGive(ppp_mutex);

    return rv;
}

static int ppp_setup(void) {
    int r;
    char cmd[EOS_CELL_PDP_SIZE_APN + 32];
    int cmd_len;

    cmd_len = snprintf(cmd, sizeof(cmd), "AT+CGDCONT=1,\"IP\",\"%s\"\r", ppp_apn);
    if ((cmd_len < 0) || (cmd_len >= sizeof(cmd))) return EOS_ERR;

    uart_change_mode(EOS_CELL_UART_MODE_NONE);
    r = xSemaphoreTake(uart_mutex, 1000 / portTICK_PERIOD_MS);
    if (r == pdFALSE) {
        uart_change_mode(uart_mode);
        return EOS_ERR_TIMEOUT;
    }

    at_cmd(cmd);
    r = at_expect("^OK", "^(ERROR|NO CARRIER)", 1000);
    if (r) {
        uart_change_mode(uart_mode);
        xSemaphoreGive(uart_mutex);
        return EOS_ERR;
    }

    at_cmd("AT+CGDATA=\"PPP\",1\r");
    r = at_expect("^CONNECT", "^NO CARRIER", 1000);
    if (r) {
        uart_change_mode(uart_mode);
        xSemaphoreGive(uart_mutex);
        return EOS_ERR;
    }

    ppp_handle = pppapi_pppos_create(&ppp_netif, ppp_output_cb, ppp_status_cb, NULL);
    ppp_set_usepeerdns(ppp_handle, 1);
    ppp_set_default(ppp_handle);
    ppp_set_auth(ppp_handle, PPPAUTHTYPE_ANY, ppp_usr, ppp_pwd);
    ppp_connect(ppp_handle, 0);

    uart_change_mode(EOS_CELL_UART_MODE_PPP);
    xSemaphoreGive(uart_mutex);

    return EOS_OK;
}

void eos_modem_init(void) {
    uart_config_t uart_config = {
       .baud_rate = 115200,
       .data_bits = UART_DATA_8_BITS,
       .parity    = UART_PARITY_DISABLE,
       .stop_bits = UART_STOP_BITS_1,
       .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    uart_param_config(UART_NUM_2, &uart_config);
    uart_set_pin(UART_NUM_2, UART_GPIO_TXD, UART_GPIO_RXD, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    uart_driver_install(UART_NUM_2, UART_SIZE_IO_BUF, UART_SIZE_IO_BUF, 10, &uart_queue, 0);

    if (eos_power_wakeup_cause() == EOS_PWR_WAKE_RST) {
        uart_mode = EOS_CELL_UART_MODE_ATCMD;
        modem_present = 0;
        modem_initialized = 0;
        modem_init_gpio();
    }

    mutex = xSemaphoreCreateBinary();
    xSemaphoreGive(mutex);

    uart_mutex = xSemaphoreCreateBinary();
    xSemaphoreGive(uart_mutex);

    ppp_mutex = xSemaphoreCreateBinary();
    xSemaphoreGive(ppp_mutex);

    modem_queue = xQueueCreate(4, sizeof(modem_event_t));
    xTaskCreate(uart_event_task, "uart_event", EOS_TASK_SSIZE_UART, NULL, EOS_TASK_PRIORITY_UART, NULL);
    xTaskCreate(modem_event_task, "modem_event", EOS_TASK_SSIZE_MODEM, NULL, EOS_TASK_PRIORITY_MODEM, NULL);

    at_init();
    at_urc_insert("^PB DONE", modem_atinit_handler, REG_EXTENDED);
    at_urc_insert("^\\+CME ERROR: SIM not inserted", modem_atinit_handler, REG_EXTENDED);

    ESP_LOGI(TAG, "INIT");
}

int eos_modem_atinit(void) {
    int echo_on = 0;
    int tries = 3;
    int i, r;

    do {
        at_cmd("AT\r");
        r = at_expect("^AT", "^OK", 1000);
        if (r >= 0) {
            echo_on = !r;
            if (echo_on) {
                r = at_expect("^OK", NULL, 1000);
            }
            break;
        }
        tries--;
    } while (tries);

    if (tries == 0) return EOS_ERR_TIMEOUT;

    if (echo_on) {
        at_cmd("AT&F\r");
        r = at_expect("^AT&F", NULL, 1000);
        r = at_expect("^OK", NULL, 1000);
    } else {
        at_cmd("AT&F\r");
        r = at_expect("^OK", NULL, 1000);
    }
    at_cmd("ATE0\r");
    r = at_expect("^ATE0", NULL, 1000);
    r = at_expect("^OK", "^ERROR", 1000);

    for (i=0; i<AT_CMD_INIT_SIZE; i++) {
        at_cmd(at_cmd_init[i]);
        r = at_expect("^OK", "^ERROR", 1000);
    }

    modem_initialized = 1;

    eos_cell_voice_init();
    eos_cell_sms_init();
    eos_cell_ussd_init();

    modem_send_status();

    return EOS_OK;
}

void eos_modem_flush(void){
    uart_wait_tx_done(UART_NUM_2, portMAX_DELAY);
}

size_t eos_modem_write(void *data, size_t size) {
    return uart_write_bytes(UART_NUM_2, (const char *)data, size);
}

size_t eos_modem_read(void *data, size_t size, uint32_t timeout) {
    return uart_read_bytes(UART_NUM_2, (uint8_t *)data, size, timeout / portTICK_RATE_MS);
}

int eos_modem_readln(char *buf, size_t buf_size, uint32_t timeout) {
    char *ln_end = NULL;
    size_t buf_len = 0;
    uint64_t t_start = esp_timer_get_time();

    buf[0] = '\0';
    if (uart_buf_len) {
        buf_len = MIN(buf_size -1, uart_buf_len);
        memcpy(buf, uart_buf, buf_len);
        buf[buf_len] = '\0';
        ln_end = strchr(buf, '\n');

        uart_buf_len -= buf_len;
        if (uart_buf_len) memmove(uart_buf, uart_buf + buf_len, uart_buf_len);
    }

    while (ln_end == NULL) {
        int rv = EOS_OK;
        int len;

        if (buf_len == buf_size - 1) rv = EOS_ERR_FULL;
        if (!rv && timeout && ((uint32_t)((esp_timer_get_time() - t_start) / 1000) > timeout)) rv = EOS_ERR_TIMEOUT;
        if (rv) {
            uart_buf_dirty = 1;
            return rv;
        }

        len = eos_modem_read(buf + buf_len, MIN(buf_size - buf_len - 1, sizeof(uart_buf) - uart_buf_len), 10);
        if (len > 0) {
            buf[buf_len + len] = '\0';
            ln_end = strchr(buf + buf_len, '\n');
            buf_len += len;
        }
    }
    buf_len -= ln_end - buf + 1;
    if (buf_len) {
        if (uart_buf_len) memmove(uart_buf + buf_len, uart_buf, uart_buf_len);
        memcpy(uart_buf, ln_end + 1, buf_len);
        uart_buf_len += buf_len;
    }

    while ((ln_end > buf) && (*(ln_end - 1) == '\r')) ln_end--;
    *ln_end = '\0';

    return EOS_OK;
}

int eos_modem_present(void) {
    int rv;

    xSemaphoreTake(mutex, portMAX_DELAY);
    rv = modem_present;
    xSemaphoreGive(mutex);

    return rv;
}

uint8_t eos_modem_get_mode(void) {
    uint8_t ret;

    xSemaphoreTake(mutex, portMAX_DELAY);
    if (modem_initialized) {
        ret = uart_mode;
    } else {
        ret = EOS_CELL_UART_MODE_NONE;
    }
    xSemaphoreGive(mutex);

    return ret;
}

size_t eos_modem_get_status(unsigned char *buffer) {
    size_t len;

    xSemaphoreTake(mutex, portMAX_DELAY);
    len = modem_get_status(buffer);
    xSemaphoreGive(mutex);

    return len;
}

int eos_modem_set_mode(uint8_t mode) {
    int rv = EOS_OK;

    xSemaphoreTake(mutex, portMAX_DELAY);
    if (!modem_initialized) rv = EOS_ERR_BUSY;
    if (!rv && (mode != uart_mode)) {
        if ((uart_mode == EOS_CELL_UART_MODE_PPP) && ppp_handle) {
            uart_mode_next = mode;
            pppapi_close(ppp_handle, 0);
        } else {
            if (mode == EOS_CELL_UART_MODE_PPP) {
                rv = ppp_setup();
            } else {
                uart_change_mode(mode);
            }
            if (!rv) {
                uart_mode = mode;
                modem_send_status();
            }
        }
    }
    xSemaphoreGive(mutex);

    return rv;
}

int eos_modem_take(uint32_t timeout) {
    int rv = EOS_OK;

    xSemaphoreTake(mutex, portMAX_DELAY);
    if (!modem_initialized) rv = EOS_ERR_BUSY;
    if (!rv) {
        if (uart_mode == EOS_CELL_UART_MODE_PPP) {
            rv = ppp_pause(timeout);
        } else {
            int r;

            uart_change_mode(EOS_CELL_UART_MODE_NONE);
            r = xSemaphoreTake(uart_mutex, timeout ? timeout / portTICK_PERIOD_MS : portMAX_DELAY);
            if (r == pdFALSE) {
                uart_change_mode(uart_mode);
                rv = EOS_ERR_TIMEOUT;
            }
        }
    }
    if (rv) xSemaphoreGive(mutex);

    return rv;
}

void eos_modem_give(void) {
    if (uart_mode == EOS_CELL_UART_MODE_PPP) {
        int rv = ppp_resume();
        if (rv) ESP_LOGW(TAG, "PPP resume failed");
    } else {
        uart_change_mode(uart_mode);
        xSemaphoreGive(uart_mutex);
    }
    xSemaphoreGive(mutex);
}

void eos_modem_sleep(void) {
    int r;

    xSemaphoreTake(mutex, portMAX_DELAY);
    uart_change_mode(EOS_CELL_UART_MODE_NONE);
    r = xSemaphoreTake(uart_mutex, 1000 / portTICK_PERIOD_MS);
    if (r == pdFALSE) {
        ESP_LOGE(TAG, "Obtaining mutex before sleep failed");
    }
    gpio_set_level(UART_GPIO_DTR, 1);
}

void eos_modem_deep_sleep(void) {
    gpio_hold_en(UART_GPIO_DTR);
}

void eos_modem_wake(uint8_t source, uint8_t mode) {
    if (source == EOS_PWR_WAKE_UART) {
        modem_event_t evt;

        evt.type = MODEM_ETYPE_RING;
        xQueueSend(modem_queue, &evt, portMAX_DELAY);
    }

    switch (mode) {
        case EOS_PWR_SMODE_LIGHT: {
            gpio_set_intr_type(UART_GPIO_RI, GPIO_INTR_NEGEDGE);
            gpio_isr_handler_add(UART_GPIO_RI, uart_ring_handler, NULL);
            gpio_set_level(UART_GPIO_DTR, 0);

            uart_change_mode(uart_mode);
            xSemaphoreGive(uart_mutex);
            xSemaphoreGive(mutex);

            break;
        }

        case EOS_PWR_SMODE_DEEP: {
            gpio_hold_dis(UART_GPIO_DTR);
            modem_init_gpio();

            break;
        }
    }
}

int eos_modem_reset(void) {
    int rv;

    rv = eos_modem_take(1000);
    if (rv) return rv;

    at_cmd("AT+CRESET\r");
    at_expect("^OK", NULL, 1000);

    uart_change_mode(EOS_CELL_UART_MODE_ATCMD);
    xSemaphoreGive(uart_mutex);

    uart_mode = EOS_CELL_UART_MODE_ATCMD;
    modem_initialized = 0;
    modem_send_status();
    xSemaphoreGive(mutex);

    return EOS_OK;
}

void eos_ppp_get_apn(char *apn) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    strcpy(apn, ppp_apn);
    xSemaphoreGive(mutex);
}

void eos_ppp_set_apn(char *apn) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    strncpy(ppp_apn, apn, sizeof(ppp_apn) - 1);
    ppp_apn[sizeof(ppp_apn) - 1] = '\0';
    xSemaphoreGive(mutex);
}

void eos_ppp_get_usr(char *usr) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    strcpy(usr, ppp_usr);
    xSemaphoreGive(mutex);
}

void eos_ppp_set_usr(char *usr) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    strncpy(ppp_usr, usr, sizeof(ppp_usr) - 1);
    ppp_usr[sizeof(ppp_usr) - 1] = '\0';
    xSemaphoreGive(mutex);
}

void eos_ppp_get_pwd(char *pwd) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    strcpy(pwd, ppp_pwd);
    xSemaphoreGive(mutex);
}

void eos_ppp_set_pwd(char *pwd) {
    xSemaphoreTake(mutex, portMAX_DELAY);
    strncpy(ppp_pwd, pwd, sizeof(ppp_pwd) - 1);
    ppp_pwd[sizeof(ppp_pwd) - 1] = '\0';
    xSemaphoreGive(mutex);
}

int eos_ppp_connect(void) {
    return eos_modem_set_mode(EOS_CELL_UART_MODE_PPP);
}

void eos_ppp_disconnect(void) {
    eos_modem_set_mode(EOS_CELL_UART_MODE_ATCMD);
}