at: make sure we read the full response before processing it

This commit is contained in:
Arnaud Ferraris
2020-12-11 12:47:14 +01:00
parent 5bc8443c38
commit f386d851fa

View File

@@ -31,7 +31,7 @@ static int configure_serial(const char *tty)
struct termios ttycfg; struct termios ttycfg;
int fd; int fd;
fd = open(tty, O_RDWR | O_NOCTTY); fd = open(tty, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd > 0) { if (fd > 0) {
tcgetattr(fd, &ttycfg); tcgetattr(fd, &ttycfg);
ttycfg.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON); 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) if (!at_cmd)
return -1; return -1;
at_cmd->retries = 0;
at_cmd->cmd = g_strdup(cmd); at_cmd->cmd = g_strdup(cmd);
if (subcmd) if (subcmd)
at_cmd->subcmd = g_strdup(subcmd); at_cmd->subcmd = g_strdup(subcmd);
@@ -157,22 +156,33 @@ static int append_at_command(struct EG25Manager *manager,
return 0; return 0;
} }
#define READ_BUFFER_SIZE 256
static gboolean modem_response(gint fd, static gboolean modem_response(gint fd,
GIOCondition event, GIOCondition event,
gpointer data) gpointer data)
{ {
struct EG25Manager *manager = data; struct EG25Manager *manager = data;
char response[256]; char response[READ_BUFFER_SIZE*4+1];
int ret; char tmp[READ_BUFFER_SIZE];
ssize_t ret, pos = 0;
/* /*
* TODO: several reads can be necessary to get the full response, we could * Several reads can be necessary to get the full response, so we loop
* loop until we read 0 chars with a reasonable delay between attempts * until we read 0 chars with a reasonable delay between attempts
* (remember the transfer rate is 115200 here) * (remember the transfer rate is 115200 here)
*/ */
ret = read(fd, response, sizeof(response)); do {
ret = read(fd, tmp, sizeof(tmp));
if (ret > 0) { if (ret > 0) {
response[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); g_strstrip(response);
if (strlen(response) == 0) if (strlen(response) == 0)
return TRUE; return TRUE;