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.
This commit is contained in:
Xavier Del Campo
2021-09-28 22:51:08 +02:00
parent 5c61d41090
commit 5102902692

View File

@@ -9,6 +9,7 @@
#include "gpio.h"
#include "gnss.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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 {