From 65e7c9032447326f28097fb96706d1e92535ba7c Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Tue, 29 Dec 2015 15:22:23 +0100 Subject: [PATCH] Queue data from within the HayesChannel class --- src/modems/hayes.c | 13 +++++++------ src/modems/hayes/channel.c | 11 +++++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/modems/hayes.c b/src/modems/hayes.c index 240be62..d67b290 100644 --- a/src/modems/hayes.c +++ b/src/modems/hayes.c @@ -1206,7 +1206,7 @@ static int _queue_push_do(Hayes * hayes, HayesChannel * channel) char const * attention; const char suffix[] = "\r\n"; size_t size; - char * p; + char * buf; guint timeout; if(channel->queue == NULL) /* nothing to send */ @@ -1229,17 +1229,18 @@ static int _queue_push_do(Hayes * hayes, HayesChannel * channel) fprintf(stderr, "DEBUG: %s() pushing \"%s\"\n", __func__, attention); #endif size = strlen(prefix) + strlen(attention) + sizeof(suffix); - if((p = realloc(channel->wr_buf, channel->wr_buf_cnt + size)) == NULL) + if((buf = malloc(size)) == NULL + || snprintf(buf, size, "%s%s%s", prefix, attention, + suffix) != (int)size - 1 + || hayeschannel_queue_data(channel, buf, size - 1) != 0) { + free(buf); hayes_command_set_status(command, HCS_ERROR); hayeschannel_queue_pop(channel); return -hayes->helper->error(hayes->helper->modem, strerror( errno), 1); } - channel->wr_buf = p; - snprintf(&channel->wr_buf[channel->wr_buf_cnt], size, "%s%s%s", prefix, - attention, suffix); - channel->wr_buf_cnt += size - 1; + free(buf); if(channel->channel != NULL && channel->wr_source == 0) channel->wr_source = g_io_add_watch(channel->channel, G_IO_OUT, _on_watch_can_write, channel); diff --git a/src/modems/hayes/channel.c b/src/modems/hayes/channel.c index 55d183d..644a146 100644 --- a/src/modems/hayes/channel.c +++ b/src/modems/hayes/channel.c @@ -19,6 +19,7 @@ #ifdef DEBUG # include #endif +#include #include "command.h" #include "channel.h" @@ -73,8 +74,14 @@ void hayeschannel_set_quirks(HayesChannel * channel, unsigned int quirks) int hayeschannel_queue_data(HayesChannel * channel, char const * buf, size_t size) { - /* FIXME implement */ - return -1; + char * p; + + if((p = realloc(channel->wr_buf, channel->wr_buf_cnt + size)) == NULL) + return -1; + channel->wr_buf = p; + memcpy(&channel->wr_buf[channel->wr_buf_cnt], buf, size); + channel->wr_buf_cnt += size; + return 0; }