mirror of
https://gitlab.com/mobian1/eg25-manager.git
synced 2025-08-29 07:12:08 +02:00
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:
27
src/at.c
27
src/at.c
@@ -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 {
|
||||
|
Reference in New Issue
Block a user