8000 Adding support for AT commands that are longer than 124 bytes. · pycom/pycom-micropython-sigfox@9384d0d · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 9384d0d

Browse files
committed
Adding support for AT commands that are longer than 124 bytes.
1 parent a159dee commit 9384d0d

File tree

3 files changed

+66
-19
lines changed

3 files changed

+66
-19
lines changed

esp32/lte/lteppp.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static bool lte_uart_break_evt = false;
9292
******************************************************************************/
9393
static void TASK_LTE (void *pvParameters);
9494
static void TASK_UART_EVT (void *pvParameters);
95-
static bool lteppp_send_at_cmd_exp(const char *cmd, uint32_t timeout, const char *expected_rsp, void* data_rem, size_t len);
95+
static bool lteppp_send_at_cmd_exp(const char *cmd, uint32_t timeout, const char *expected_rsp, void* data_rem, size_t len, bool expect_continuation);
9696
static bool lteppp_send_at_cmd(const char *cmd, uint32_t timeout);
9797
static bool lteppp_check_sim_present(void);
9898
static void lteppp_status_cb (ppp_pcb *pcb, int err_code, void *ctx);
@@ -233,7 +233,9 @@ void lteppp_disconnect(void) {
233233

234234
void lteppp_send_at_command (lte_task_cmd_data_t *cmd, lte_task_rsp_data_t *rsp) {
235235
xQueueSend(xCmdQueue, (void *)cmd, (TickType_t)portMAX_DELAY);
236-
xQueueReceive(xRxQueue, rsp, (TickType_t)portMAX_DELAY);
236+
237+
if(!cmd->expect_continuation)
238+
xQueueReceive(xRxQueue, rsp, (TickType_t)portMAX_DELAY);
237239
}
238240

239241
bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_mp, void* data_rem) {
@@ -526,8 +528,9 @@ static void TASK_LTE (void *pvParameters) {
526528
xSemaphoreGive(xLTESem);
527529
state = lteppp_get_state();
528530
if (xQueueReceive(xCmdQueue, lteppp_trx_buffer, 0)) {
529-
lteppp_send_at_cmd_exp(lte_task_cmd->data, lte_task_cmd->timeout, NULL, &(lte_task_rsp->data_remaining), lte_task_cmd->dataLen);
530-
xQueueSend(xRxQueue, (void *)lte_task_rsp, (TickType_t)portMAX_DELAY);
531+
lteppp_send_at_cmd_exp(lte_task_cmd->data, lte_task_cmd->timeout, NULL, &(lte_task_rsp->data_remaining), lte_task_cmd->dataLen, lte_task_cmd->expect_continuation);
532+
if(!lte_task_cmd->expect_continuation)
533+
xQueueSend(xRxQueue, (void *)lte_task_rsp, (TickType_t)portMAX_DELAY);
531534
}
532535
else if(state == E_LTE_PPP && lte_uart_break_evt)
533536
{
@@ -613,7 +616,7 @@ static void TASK_UART_EVT (void *pvParameters)
613616
}
614617

615618

616-
static bool lteppp_send_at_cmd_exp (const char *cmd, uint32_t timeout, const char *expected_rsp, void* data_rem, size_t len) {
619+
static bool lteppp_send_at_cmd_exp (const char *cmd, uint32_t timeout, const char *expected_rsp, void* data_rem, size_t len, bool expect_continuation) {
617620

618621
if(strstr(cmd, "Pycom_Dummy") != NULL)
619622
{
@@ -654,22 +657,30 @@ static bool lteppp_send_at_cmd_exp (const char *cmd, uint32_t timeout, const cha
654657
}
655658
#endif
656659
// flush the rx buffer first
657-
uart_flush(LTE_UART_ID);
660+
if(!expect_continuation ||
661+
(len >= 2 && cmd[0] == 'A' && cmd[1] == 'T')) // starts with AT
662+
uart_flush(LTE_UART_ID);
658663
// uart_read_bytes(LTE_UART_ID, (uint8_t *)tmp_buf, sizeof(tmp_buf), 5 / portTICK_RATE_MS);
659664
// then send the command
660665
uart_write_bytes(LTE_UART_ID, cmd, cmd_len);
661-
if (strcmp(cmd, "+++")) {
662-
uart_write_bytes(LTE_UART_ID, "\r", 1);
663-
}
664-
uart_wait_tx_done(LTE_UART_ID, LTE_TRX_WAIT_MS(cmd_len) / por 8000 tTICK_RATE_MS);
665-
vTaskDelay(2 / portTICK_RATE_MS);
666666

667-
return lteppp_wait_at_rsp(expected_rsp, timeout, false, data_rem);
667+
if(expect_continuation)
668+
return true;
669+
else {
670+
if (strcmp(cmd, "+++")) {
671+
uart_write_bytes(LTE_UART_ID, "\r", 1);
672+
}
673+
674+
uart_wait_tx_done(LTE_UART_ID, LTE_TRX_WAIT_MS(cmd_len) / portTICK_RATE_MS);
675+
vTaskDelay(2 / portTICK_RATE_MS);
676+
677+
return lteppp_wait_at_rsp(expected_rsp, timeout, false, data_rem);
678+
}
668679
}
669680
}
670681

671682
static bool lteppp_send_at_cmd(const char *cmd, uint32_t timeout) {
672-
return lteppp_send_at_cmd_exp (cmd, timeout, LTE_OK_RSP, NULL, strlen(cmd) );
683+
return lteppp_send_at_cmd_exp (cmd, timeout, LTE_OK_RSP, NULL, strlen(cmd), false);
673684
}
674685

675686
static bool lteppp_check_sim_present(void) {

esp32/lte/lteppp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define LTE_CMD_QUEUE_SIZE_MAX (1)
2020
#define LTE_RSP_QUEUE_SIZE_MAX (1)
2121
#define LTE_AT_CMD_SIZE_MAX (128)
22+
#define LTE_AT_CMD_DATA_SIZE_MAX (LTE_AT_CMD_SIZE_MAX - 4)
2223
#define LTE_AT_RSP_SIZE_MAX (LTE_UART_BUFFER_SIZE)
2324

2425
#define LTE_OK_RSP "OK"
@@ -71,8 +72,9 @@ typedef struct {
7172
#endif
7273
typedef struct {
7374
uint32_t timeout;
74-
char data[LTE_AT_CMD_SIZE_MAX - 4];
75+
char data[LTE_AT_CMD_DATA_SIZE_MAX];
7576
size_t dataLen;
77+
bool expect_continuation;
7678
} lte_task_cmd_data_t;
7779
#pragma pack(1)
7880
typedef struct {

esp32/mods/modlte.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ extern TaskHandle_t xLTETaskHndl;
136136
/******************************************************************************
137137
DECLARE PRIVATE FUNCTIONS
138138
******************************************************************************/
139+
static bool lte_push_at_command_ext_cont (char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len, bool continuation);
139140
static bool lte_push_at_command_ext (char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len);
140141
static bool lte_push_at_command (char *cmd_str, uint32_t timeout);
141142
static void lte_pause_ppp(void);
@@ -192,14 +193,15 @@ static void lte_callback_handler(void* arg)
192193
}
193194
}
194195

195-
static bool lte_push_at_command_ext(char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len) {
196-
lte_task_cmd_data_t cmd = { .timeout = timeout, .dataLen = len};
196+
static bool lte_push_at_command_ext_cont (char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len, bool continuation)
197+
{
198+
lte_task_cmd_data_t cmd = { .timeout = timeout, .dataLen = len, .expect_continuation = continuation};
197199
memcpy(cmd.data, cmd_str, len);
198200
uint32_t start = mp_hal_ticks_ms();
199201
if (lte_debug)
200202
printf("[AT] %u %s\n", start, cmd_str);
201203
lteppp_send_at_command(&cmd, &modlte_rsp);
202-
if ((expected_rsp == NULL) || (strstr(modlte_rsp.data, expected_rsp) != NULL)) {
204+
if (continuation || (expected_rsp == NULL) || (strstr(modlte_rsp.data, expected_rsp) != NULL)) {
203205
if (lte_debug)
204206
printf("[AT-OK] +%u %s\n", mp_hal_ticks_ms()-start, modlte_rsp.data);
205207
return true;
@@ -209,6 +211,10 @@ static bool lte_push_at_command_ext(char *cmd_str, uint32_t timeout, const char
209211
return false;
210212
}
211213

214+
static bool lte_push_at_command_ext(char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len) {
215+
return lte_push_at_command_ext_cont(cmd_str, timeout, expected_rsp, len, false);
216+
}
217+
212218
static bool lte_push_at_command (char *cmd_str, uint32_t timeout) {
213219
return lte_push_at_command_ext(cmd_str, timeout, LTE_OK_RSP, strlen(cmd_str));
214220
}
@@ -1229,18 +1235,46 @@ STATIC mp_obj_t lte_send_at_cmd(mp_uint_t n_args, const mp_obj_t *pos_args, mp_m
12291235
lte_check_init();
12301236
lte_check_inppp();
12311237
STATIC const mp_arg_t allowed_args[] = {
1232-
{ MP_QSTR_cmd, MP_ARG_OBJ, {.u_obj = mp_const_none} },
1238+
{ MP_QSTR_cmd, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
1239+
{ MP_QSTR_delay, MP_ARG_INT, {.u_int = 10000} }
12331240
};
12341241
// parse args
12351242
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
1243+
uint32_t argLength = MP_ARRAY_SIZE(allowed_args);
12361244
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
12371245
if (args[0].u_obj == mp_const_none) {
12381246
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "the command must be specified!"));
12391247
}
1248+
uint32_t timeout = LTE_RX_TIMEOUT_MAX_MS;
12401249
if (MP_OBJ_IS_STR_OR_BYTES(args[0].u_obj))
12411250
{
12421251
size_t len;
1243-
lte_push_at_command_ext((char *)(mp_obj_str_get_data(args[0].u_obj, &len)), LTE_RX_TIMEOUT_MAX_MS, NULL, len);
1252+
char* command = (char *)(mp_obj_str_get_data(args[0].u_obj, &len));
1253+
1254+
if(argLength > 1) {
1255+
timeout = args[1].u_int;
1256+
}
1257+
1258+
if(len <= LTE_AT_CMD_DATA_SIZE_MAX)
1259+
lte_push_at_command_ext_cont(command, timeout, NULL, len, false);
1260+
else {
1261+
size_t chunk_count = len / LTE_AT_CMD_DATA_SIZE_MAX;
1262+
size_t remaining_bytes = len % LTE_AT_CMD_DATA_SIZE_MAX;
1263+
1264+
bool expect_continuation = false;
1265+
char* chunk_start = command;
1266+
for(size_t i=0; i<chunk_count; ++i) {
1267+
expect_continuation = (i < (chunk_count - 1)) || remaining_bytes;
1268+
1269+
lte_push_at_command_ext_cont(chunk_start, timeout, NULL, LTE_AT_CMD_DATA_SIZE_MAX, expect_continuation);
1270+
1271+
chunk_start += LTE_AT_CMD_DATA_SIZE_MAX;
1272+
}
1273+
1274+
if(remaining_bytes) {
1275+
lte_push_at_command_ext_cont(chunk_start, timeout, NULL, remaining_bytes, false);
1276+
}
1277+
}
12441278
}
12451279
else
12461280
{

0 commit comments

Comments
 (0)
0