diff --git a/src/at.c b/src/at.c index b50ceea..16722c3 100644 --- a/src/at.c +++ b/src/at.c @@ -47,6 +47,21 @@ static int configure_serial(const char *tty) return fd; } +static void at_free_command(struct AtCommand *at_cmd, struct EG25Manager *manager) +{ + if (!at_cmd) + return; + + g_free(at_cmd->cmd); + g_free(at_cmd->subcmd); + g_free(at_cmd->value); + g_free(at_cmd->expected); + g_free(at_cmd); + + if (manager && manager->at_cmds) + manager->at_cmds = g_list_remove(manager->at_cmds, at_cmd); +} + gboolean at_send_command(struct EG25Manager *manager) { char command[256]; @@ -139,16 +154,7 @@ void at_next_command(struct EG25Manager *manager) { struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL; - if (!at_cmd) - return; - - g_free(at_cmd->cmd); - g_free(at_cmd->subcmd); - g_free(at_cmd->value); - g_free(at_cmd->expected); - g_free(at_cmd); - manager->at_cmds = g_list_remove(manager->at_cmds, at_cmd); - + at_free_command(at_cmd, manager); at_send_command(manager); } @@ -388,6 +394,13 @@ void at_destroy(struct EG25Manager *manager) void at_sequence_configure(struct EG25Manager *manager) { + /* + * When configuring a new modem we should avoid processing an old + * command queue, so let's first clear the whole list + */ + if (manager->at_cmds) + g_list_foreach(manager->at_cmds, at_free_command, manager); + for (guint i = 0; i < configure_commands->len; i++) { struct AtCommand *cmd = &g_array_index(configure_commands, struct AtCommand, i); at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected, at_process_result);