From f386d851fa70d7cc0497fc0ff419722d02152d7c Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Fri, 11 Dec 2020 12:47:14 +0100 Subject: [PATCH] at: make sure we read the full response before processing it --- src/at.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/at.c b/src/at.c index 069fed0..c0cd92b 100644 --- a/src/at.c +++ b/src/at.c @@ -31,7 +31,7 @@ static int configure_serial(const char *tty) struct termios ttycfg; int fd; - fd = open(tty, O_RDWR | O_NOCTTY); + fd = open(tty, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd > 0) { tcgetattr(fd, &ttycfg); ttycfg.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON); @@ -143,7 +143,6 @@ static int append_at_command(struct EG25Manager *manager, if (!at_cmd) return -1; - at_cmd->retries = 0; at_cmd->cmd = g_strdup(cmd); if (subcmd) at_cmd->subcmd = g_strdup(subcmd); @@ -157,22 +156,33 @@ static int append_at_command(struct EG25Manager *manager, return 0; } +#define READ_BUFFER_SIZE 256 + static gboolean modem_response(gint fd, GIOCondition event, gpointer data) { struct EG25Manager *manager = data; - char response[256]; - int ret; + char response[READ_BUFFER_SIZE*4+1]; + char tmp[READ_BUFFER_SIZE]; + ssize_t ret, pos = 0; /* - * TODO: several reads can be necessary to get the full response, we could - * loop until we read 0 chars with a reasonable delay between attempts + * Several reads can be necessary to get the full response, so we loop + * until we read 0 chars with a reasonable delay between attempts * (remember the transfer rate is 115200 here) */ - ret = read(fd, response, sizeof(response)); - if (ret > 0) { - response[ret] = 0; + do { + ret = read(fd, tmp, sizeof(tmp)); + if (ret > 0) { + memcpy(&response[pos], tmp, ret); + pos += ret; + usleep(10000); + } + } while (ret > 0 && pos < (sizeof(response) - 1)); + + if (pos > 0) { + response[pos] = 0; g_strstrip(response); if (strlen(response) == 0) return TRUE;