From 3a205f7e9baf702dccc572ae2342990052c23155 Mon Sep 17 00:00:00 2001
From: Dylan Van Assche <me@dylanvanassche.be>
Date: Wed, 12 May 2021 18:21:29 +0200
Subject: [PATCH 5/8] 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
---
 src/at.c | 47 ++++++++++++++++++++++++-----------------------
 src/at.h | 22 ++++++++++++++++------
 2 files changed, 40 insertions(+), 29 deletions(-)

diff --git a/src/at.c b/src/at.c
index 3ccc5d6..1e605a1 100644
--- a/src/at.c
+++ b/src/at.c
@@ -52,7 +52,7 @@ static int configure_serial(const char *tty)
     return fd;
 }
 
-static gboolean send_at_command(struct EG25Manager *manager)
+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;
@@ -97,7 +97,7 @@ static gboolean send_at_command(struct EG25Manager *manager)
     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;
 
@@ -111,7 +111,7 @@ static void next_at_command(struct EG25Manager *manager)
     g_free(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)
@@ -124,13 +124,14 @@ static void retry_at_command(struct EG25Manager *manager)
     at_cmd->retries++;
     if (at_cmd->retries > 3) {
         g_critical("Command %s retried %d times, aborting...", at_cmd->cmd, at_cmd->retries);
-        next_at_command(manager);
+        at_next_command(manager);
     } 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;
 
@@ -143,17 +144,17 @@ static void process_at_result(struct EG25Manager *manager, char *response)
         at_cmd->expected = NULL;
         g_message("Got a different result than expected, changing value...");
         g_message("\t%s\n\t%s", at_cmd->expected, response);
-        send_at_command(manager);
+        at_send_command(manager);
     } else {
-        next_at_command(manager);
+        at_next_command(manager);
     }
 }
 
-static int append_at_command(struct EG25Manager *manager,
-                             const char         *cmd,
-                             const char         *subcmd,
-                             const char         *value,
-                             const char         *expected)
+int at_append_command(struct EG25Manager *manager,
+                      const char         *cmd,
+                      const char         *subcmd,
+                      const char         *value,
+                      const char         *expected)
 {
     struct AtCommand *at_cmd = calloc(1, sizeof(struct AtCommand));
 
@@ -217,10 +218,10 @@ static gboolean modem_response(gint fd,
         else if (strstr(response, "ERROR") && !strstr(response, "fast/poweroff"))
             retry_at_command(manager);
         else if (strstr(response, "OK"))
-            process_at_result(manager, response);
+            at_process_result(manager, response);
         else
             // Not a recognized response, try running next command, just in case
-            next_at_command(manager);
+            at_next_command(manager);
     }
 
     return TRUE;
@@ -325,34 +326,34 @@ void at_sequence_configure(struct EG25Manager *manager)
 {
     for (guint i = 0; i < configure_commands->len; 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)
 {
     for (guint i = 0; i < suspend_commands->len; 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)
 {
     for (guint i = 0; i < resume_commands->len; 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)
 {
     for (guint i = 0; i < reset_commands->len; 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);
 }
diff --git a/src/at.h b/src/at.h
index ba294a4..e0445af 100644
--- a/src/at.h
+++ b/src/at.h
@@ -8,10 +8,20 @@
 
 #include "manager.h"
 
-int at_init(struct EG25Manager *data, toml_table_t *config);
-void at_destroy(struct EG25Manager *data);
+int at_init(struct EG25Manager *manager, toml_table_t *config);
+void at_destroy(struct EG25Manager *manager);
 
-void at_sequence_configure(struct EG25Manager *data);
-void at_sequence_suspend(struct EG25Manager *data);
-void at_sequence_resume(struct EG25Manager *data);
-void at_sequence_reset(struct EG25Manager *data);
+void at_process_result(struct EG25Manager *manager,
+                       const char         *response);
+void at_next_command(struct EG25Manager *manager);
+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);
-- 
2.31.1

