pmaports/temp/eg25-manager/0007-at-allow-custom-callbacks-for-AT-command-response-pr.patch

159 lines
5.9 KiB
Diff
Raw Normal View History

From 427944b75e0a4abc4cc2af2f16858739a8833e6e Mon Sep 17 00:00:00 2001
From: Dylan Van Assche <me@dylanvanassche.be>
Date: Wed, 12 May 2021 18:34:03 +0200
Subject: [PATCH 7/8] at: allow custom callbacks for AT command response
processing
---
src/at.c | 32 +++++++++++++++++---------------
src/at.h | 14 +++++++++++++-
src/manager.h | 1 +
3 files changed, 31 insertions(+), 16 deletions(-)
diff --git a/src/at.c b/src/at.c
index 533ed1c..35b7042 100644
--- a/src/at.c
+++ b/src/at.c
@@ -16,14 +16,6 @@
#include <glib-unix.h>
-struct AtCommand {
- char *cmd;
- char *subcmd;
- char *value;
- char *expected;
- int retries;
-};
-
static GArray *configure_commands = NULL;
static GArray *suspend_commands = NULL;
static GArray *resume_commands = NULL;
@@ -69,6 +61,7 @@ gboolean at_send_command(struct EG25Manager *manager)
len = sprintf(command, "AT+%s=\"%s\"\r\n", at_cmd->cmd, at_cmd->subcmd);
else if (at_cmd->subcmd && at_cmd->value)
len = sprintf(command, "AT+%s=\"%s\",%s\r\n", at_cmd->cmd, at_cmd->subcmd, at_cmd->value);
+ manager->at_callback = at_cmd->callback;
ret = write(manager->at_fd, command, len);
if (ret < len)
@@ -154,7 +147,10 @@ int at_append_command(struct EG25Manager *manager,
const char *cmd,
const char *subcmd,
const char *value,
- const char *expected)
+ const char *expected,
+ void (*callback)
+ (struct EG25Manager *manager,
+ const char *response))
{
struct AtCommand *at_cmd = calloc(1, sizeof(struct AtCommand));
@@ -168,6 +164,8 @@ int at_append_command(struct EG25Manager *manager,
at_cmd->value = g_strdup(value);
if (expected)
at_cmd->expected = g_strdup(expected);
+ if (callback)
+ at_cmd->callback = callback;
manager->at_cmds = g_list_append(manager->at_cmds, at_cmd);
@@ -217,8 +215,12 @@ static gboolean modem_response(gint fd,
*/
else if (strstr(response, "ERROR") && !strstr(response, "fast/poweroff"))
retry_at_command(manager);
- else if (strstr(response, "OK"))
- at_process_result(manager, response);
+ else if (strstr(response, "OK")) {
+ if (manager->at_callback != NULL)
+ manager->at_callback(manager, response);
+ else
+ g_warning("AT command succesfull but no callback registered");
+ }
else
// Not a recognized response, try running next command, just in case
at_next_command(manager);
@@ -326,7 +328,7 @@ 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);
- at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected);
+ at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected, at_process_result);
}
at_send_command(manager);
}
@@ -335,7 +337,7 @@ 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);
- at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected);
+ at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected, at_process_result);
}
at_send_command(manager);
}
@@ -344,7 +346,7 @@ 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);
- at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected);
+ at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected, at_process_result);
}
at_send_command(manager);
}
@@ -353,7 +355,7 @@ 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);
- at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected);
+ at_append_command(manager, cmd->cmd, cmd->subcmd, cmd->value, cmd->expected, at_process_result);
}
at_send_command(manager);
}
diff --git a/src/at.h b/src/at.h
index e0445af..0364e2c 100644
--- a/src/at.h
+++ b/src/at.h
@@ -8,6 +8,15 @@
#include "manager.h"
+typedef struct AtCommand {
+ char *cmd;
+ char *subcmd;
+ char *value;
+ char *expected;
+ void (*callback)(struct EG25Manager *manager, const char *response);
+ int retries;
+} AtCommand;
+
int at_init(struct EG25Manager *manager, toml_table_t *config);
void at_destroy(struct EG25Manager *manager);
@@ -19,7 +28,10 @@ int at_append_command(struct EG25Manager *manager,
const char *cmd,
const char *subcmd,
const char *value,
- const char *expected);
+ const char *expected,
+ void (*callback)
+ (struct EG25Manager *manager,
+ const char *response));
void at_sequence_configure(struct EG25Manager *manager);
void at_sequence_suspend(struct EG25Manager *manager);
diff --git a/src/manager.h b/src/manager.h
index 30028e6..3cc0118 100644
--- a/src/manager.h
+++ b/src/manager.h
@@ -47,6 +47,7 @@ struct EG25Manager {
int at_fd;
guint at_source;
GList *at_cmds;
+ void (*at_callback)(struct EG25Manager *manager, const char *response);
enum EG25State modem_state;
gchar *modem_usb_id;
--
2.31.1