From 510290269278239490e3850613f568728e84fdcb Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Date: Tue, 28 Sep 2021 22:51:08 +0200 Subject: [PATCH 1/3] at_send_command: call write(2) in a loop write(2) might return less than expected due to various reasons. Therefore, unless we are dealing with a critical error, it must be called in a loop until all bytes are written. --- src/at.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/at.c b/src/at.c index 2527bca..4d3b3f3 100644 --- a/src/at.c +++ b/src/at.c @@ -9,6 +9,7 @@ #include "gpio.h" #include "gnss.h" +#include #include #include #include @@ -50,7 +51,7 @@ gboolean at_send_command(struct EG25Manager *manager) { char command[256]; struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL; - int ret, len = 0; + int ret, len = 0, pos = 0; if (at_cmd) { /* Wake up the modem from soft sleep before sending an AT command */ @@ -82,9 +83,27 @@ gboolean at_send_command(struct EG25Manager *manager) manager->at_callback = at_cmd->callback; - ret = write(manager->at_fd, command, len); - if (ret < len) - g_warning("Couldn't write full AT command: wrote %d/%d bytes", ret, len); + do { + ret = write(manager->at_fd, &command[pos], len); + + if (ret < 0) { + switch (errno) { + case EAGAIN: + case EINTR: + /* Try again. */ + break; + + default: + g_warning("write(2): %s", strerror(errno)); + at_next_command(manager); + return FALSE; + } + } + else { + len -= ret; + pos += ret; + } + } while (len > 0); g_message("Sending command: %s", g_strstrip(command)); } else { From 082cf996d132028e7ca08f033119de1c77b6039e Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Thu, 30 Sep 2021 19:59:24 +0000 Subject: [PATCH 2/3] Use a more meaningful message on AT send failure --- src/at.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/at.c b/src/at.c index 4d3b3f3..64ad466 100644 --- a/src/at.c +++ b/src/at.c @@ -94,7 +94,7 @@ gboolean at_send_command(struct EG25Manager *manager) break; default: - g_warning("write(2): %s", strerror(errno)); + g_warning("error sending AT command: %s", strerror(errno)); at_next_command(manager); return FALSE; } From 6b2f0e8fbd54a68499881437a3af8535cdbf773a Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Date: Sat, 2 Oct 2021 11:17:47 +0200 Subject: [PATCH 3/3] at.c: fix misleading g_message --- src/at.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/at.c b/src/at.c index 64ad466..b50ceea 100644 --- a/src/at.c +++ b/src/at.c @@ -83,6 +83,8 @@ gboolean at_send_command(struct EG25Manager *manager) manager->at_callback = at_cmd->callback; + g_message("Sending command: %s", g_strstrip(command)); + do { ret = write(manager->at_fd, &command[pos], len); @@ -105,7 +107,7 @@ gboolean at_send_command(struct EG25Manager *manager) } } while (len > 0); - g_message("Sending command: %s", g_strstrip(command)); + g_message("Successfully sent command: %s", g_strstrip(command)); } else { /* Allow the modem to enter soft sleep again when we sent the AT command*/ gpio_sequence_sleep(manager);