at: make next_at_command, send_at_command, process_at_result, and append_at_command public methods

Allows other modules to send AT commands as well
This commit is contained in:
Dylan Van Assche
2021-05-12 18:21:29 +02:00
parent 9c4a934a51
commit 9646e0e8df
2 changed files with 40 additions and 29 deletions

View File

@@ -53,7 +53,7 @@ static int configure_serial(const char *tty)
return fd; return fd;
} }
static gboolean send_at_command(struct EG25Manager *manager) gboolean at_send_command(struct EG25Manager *manager)
{ {
char command[256]; char command[256];
struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL; struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL;
@@ -107,7 +107,7 @@ static gboolean send_at_command(struct EG25Manager *manager)
return FALSE; return FALSE;
} }
static void next_at_command(struct EG25Manager *manager) void at_next_command(struct EG25Manager *manager)
{ {
struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL; struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL;
@@ -121,7 +121,7 @@ static void next_at_command(struct EG25Manager *manager)
g_free(at_cmd); g_free(at_cmd);
manager->at_cmds = g_list_remove(manager->at_cmds, at_cmd); manager->at_cmds = g_list_remove(manager->at_cmds, at_cmd);
send_at_command(manager); at_send_command(manager);
} }
static void retry_at_command(struct EG25Manager *manager) static void retry_at_command(struct EG25Manager *manager)
@@ -134,13 +134,14 @@ static void retry_at_command(struct EG25Manager *manager)
at_cmd->retries++; at_cmd->retries++;
if (at_cmd->retries > 3) { if (at_cmd->retries > 3) {
g_critical("Command %s retried %d times, aborting...", at_cmd->cmd, at_cmd->retries); g_critical("Command %s retried %d times, aborting...", at_cmd->cmd, at_cmd->retries);
next_at_command(manager); at_next_command(manager);
} else { } else {
g_timeout_add(500, G_SOURCE_FUNC(send_at_command), manager); g_timeout_add(500, G_SOURCE_FUNC(at_send_command), manager);
} }
} }
static void process_at_result(struct EG25Manager *manager, char *response) void at_process_result(struct EG25Manager *manager,
const char *response)
{ {
struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL; struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL;
@@ -153,17 +154,17 @@ static void process_at_result(struct EG25Manager *manager, char *response)
at_cmd->expected = NULL; at_cmd->expected = NULL;
g_message("Got a different result than expected, changing value..."); g_message("Got a different result than expected, changing value...");
g_message("\t%s\n\t%s", at_cmd->expected, response); g_message("\t%s\n\t%s", at_cmd->expected, response);
send_at_command(manager); at_send_command(manager);
} else { } else {
next_at_command(manager); at_next_command(manager);
} }
} }
static int append_at_command(struct EG25Manager *manager, int at_append_command(struct EG25Manager *manager,
const char *cmd, const char *cmd,
const char *subcmd, const char *subcmd,
const char *value, const char *value,
const char *expected) const char *expected)
{ {
struct AtCommand *at_cmd = calloc(1, sizeof(struct AtCommand)); struct AtCommand *at_cmd = calloc(1, sizeof(struct AtCommand));
@@ -223,10 +224,10 @@ static gboolean modem_response(gint fd,
else if (strstr(response, "ERROR")) else if (strstr(response, "ERROR"))
retry_at_command(manager); retry_at_command(manager);
else if (strstr(response, "OK")) else if (strstr(response, "OK"))
process_at_result(manager, response); at_process_result(manager, response);
else else
// Not a recognized response, try running next command, just in case // Not a recognized response, try running next command, just in case
next_at_command(manager); at_next_command(manager);
} }
return TRUE; return TRUE;
@@ -331,34 +332,34 @@ void at_sequence_configure(struct EG25Manager *manager)
{ {
for (guint i = 0; i < configure_commands->len; i++) { for (guint i = 0; i < configure_commands->len; i++) {
struct AtCommand *cmd = &g_array_index(configure_commands, struct AtCommand, i); struct AtCommand *cmd = &g_array_index(configure_commands, struct AtCommand, i);
append_at_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected); at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected);
} }
send_at_command(manager); at_send_command(manager);
} }
void at_sequence_suspend(struct EG25Manager *manager) void at_sequence_suspend(struct EG25Manager *manager)
{ {
for (guint i = 0; i < suspend_commands->len; i++) { for (guint i = 0; i < suspend_commands->len; i++) {
struct AtCommand *cmd = &g_array_index(suspend_commands, struct AtCommand, i); struct AtCommand *cmd = &g_array_index(suspend_commands, struct AtCommand, i);
append_at_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected); at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected);
} }
send_at_command(manager); at_send_command(manager);
} }
void at_sequence_resume(struct EG25Manager *manager) void at_sequence_resume(struct EG25Manager *manager)
{ {
for (guint i = 0; i < resume_commands->len; i++) { for (guint i = 0; i < resume_commands->len; i++) {
struct AtCommand *cmd = &g_array_index(resume_commands, struct AtCommand, i); struct AtCommand *cmd = &g_array_index(resume_commands, struct AtCommand, i);
append_at_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected); at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected);
} }
send_at_command(manager); at_send_command(manager);
} }
void at_sequence_reset(struct EG25Manager *manager) void at_sequence_reset(struct EG25Manager *manager)
{ {
for (guint i = 0; i < reset_commands->len; i++) { for (guint i = 0; i < reset_commands->len; i++) {
struct AtCommand *cmd = &g_array_index(reset_commands, struct AtCommand, i); struct AtCommand *cmd = &g_array_index(reset_commands, struct AtCommand, i);
append_at_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected); at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected);
} }
send_at_command(manager); at_send_command(manager);
} }

View File

@@ -8,10 +8,20 @@
#include "manager.h" #include "manager.h"
int at_init(struct EG25Manager *data, toml_table_t *config); int at_init(struct EG25Manager *manager, toml_table_t *config);
void at_destroy(struct EG25Manager *data); void at_destroy(struct EG25Manager *manager);
void at_sequence_configure(struct EG25Manager *data); void at_process_result(struct EG25Manager *manager,
void at_sequence_suspend(struct EG25Manager *data); const char *response);
void at_sequence_resume(struct EG25Manager *data); void at_next_command(struct EG25Manager *manager);
void at_sequence_reset(struct EG25Manager *data); gboolean at_send_command(struct EG25Manager *manager);
int at_append_command(struct EG25Manager *manager,
const char *cmd,
const char *subcmd,
const char *value,
const char *expected);
void at_sequence_configure(struct EG25Manager *manager);
void at_sequence_suspend(struct EG25Manager *manager);
void at_sequence_resume(struct EG25Manager *manager);
void at_sequence_reset(struct EG25Manager *manager);