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

Commit 1ddb6cb

Browse files
committed
Adding support for AT commands that are longer than 124 bytes.
1 parent e264b4c commit 1ddb6cb

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) / portTICK_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
@@ -116,6 +116,7 @@ extern TaskHandle_t xLTETaskHndl;
116116
/******************************************************************************
117117
DECLARE PRIVATE FUNCTIONS
118118
******************************************************************************/
119+
static bool lte_push_at_command_ext_cont (char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len, bool continuation);
119120
static bool lte_push_at_command_ext (char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len);
120121
static bool lte_push_at_command (char *cmd_str, uint32_t timeout);
121122
static void lte_pause_ppp(void);
@@ -172,19 +173,24 @@ static void lte_callback_handler(void* arg)
172173
}
173174
}
174175

175-
static bool lte_push_at_command_ext(char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len) {
176-
lte_task_cmd_data_t cmd = { .timeout = timeout, .dataLen = len};
176+
static bool lte_push_at_command_ext_cont (char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len, bool continuation)
177+
{
178+
lte_task_cmd_data_t cmd = { .timeout = timeout, .dataLen = len, .expect_continuation = continuation};
177179
memcpy(cmd.data, cmd_str, len);
178180
//printf("[CMD] %s\n", cmd_str);
179181
lteppp_send_at_command(&cmd, &modlte_rsp);
180-
if ((expected_rsp == NULL) || (strstr(modlte_rsp.data, expected_rsp) != NULL)) {
182+
if (continuation || (expected_rsp == NULL) || (strstr(modlte_rsp.data, expected_rsp) != NULL)) {
181183
//printf("[OK] %s\n", modlte_rsp.data);
182184
return true;
183185
}
184186
//printf("[FAIL] %s\n", modlte_rsp.data);
185187
return false;
186188
}
187189

190+
static bool lte_push_at_command_ext(char *cmd_str, uint32_t timeout, const char *expected_rsp, size_t len) {
191+
return lte_push_at_command_ext_cont(cmd_str, timeout, expected_rsp, len, false);
192+
}
193+
188194
static bool lte_push_at_command (char *cmd_str, uint32_t timeout) {
189195
return lte_push_at_command_ext(cmd_str, timeout, LTE_OK_RSP, strlen(cmd_str));
190196
}
@@ -1091,18 +1097,46 @@ STATIC mp_obj_t lte_send_at_cmd(mp_uint_t n_args, const mp_obj_t *pos_args, mp_m
10911097
lte_check_init();
10921098
lte_check_inppp();
10931099
STATIC const mp_arg_t allowed_args[] = {
1094-
{ MP_QSTR_cmd, MP_ARG_OBJ, {.u_obj = mp_const_none} },
1100+
{ MP_QSTR_cmd, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
1101+
{ MP_QSTR_delay, MP_ARG_INT, {.u_int = 10000} }
10951102
};
10961103
// parse args
10971104
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
1105+
uint32_t argLength = MP_ARRAY_SIZE(allowed_args);
10981106
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
10991107
if (args[0].u_obj == mp_const_none) {
11001108
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "the command must be specified!"));
11011109
}
1110+
uint32_t timeout = LTE_RX_TIMEOUT_MAX_MS;
11021111
if (MP_OBJ_IS_STR_OR_BYTES(args[0].u_obj))
11031112
{
11041113
size_t len;
1105-
lte_push_at_command_ext((char *)(mp_obj_str_get_data(args[0].u_obj, &len)), LTE_RX_TIMEOUT_MAX_MS, NULL, len);
1114+
char* command = (char *)(mp_obj_str_get_data(args[0].u_obj, &len));
1115+
1116+
if(argLength > 1) {
1117+
timeout = args[1].u_int;
1118+
}
1119+
1120+
if(len <= LTE_AT_CMD_DATA_SIZE_MAX)
1121+
lte_push_at_command_ext_cont(command, timeout, NULL, len, false);
1122+
else {
1123+
size_t chunk_count = len / LTE_AT_CMD_DATA_SIZE_MAX;
1124+
size_t remaining_bytes = len % LTE_AT_CMD_DATA_SIZE_MAX;
1125+
1126+
bool expect_continuation = false;
1127+
char* chunk_start = command;
1128+
for(size_t i=0; i<chunk_count; ++i) {
1129+
expect_continuation = (i < (chunk_count - 1)) || remaining_bytes;
1130+
1131+
lte_push_at_command_ext_cont(chunk_start, timeout, NULL, LTE_AT_CMD_DATA_SIZE_MAX, expect_continuation);
1132+
1133+
chunk_start += LTE_AT_CMD_DATA_SIZE_MAX;
1134+
}
1135+
1136+
if(remaining_bytes) {
1137+
lte_push_at_command_ext_cont(chunk_start, timeout, NULL, remaining_bytes, false);
1138+
}
1139+
}
11061140
}
11071141
else
11081142
{

0 commit comments

Comments
 (0)