at: allow custom callbacks for AT command response processing

This commit is contained in:
Dylan Van Assche
2021-05-12 18:34:03 +02:00
parent dac50e34eb
commit 5da7c88fc4
3 changed files with 31 additions and 16 deletions

View File

@@ -17,14 +17,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;
@@ -74,6 +66,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)
@@ -164,7 +157,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));
@@ -178,6 +174,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);
@@ -223,8 +221,12 @@ static gboolean modem_response(gint fd,
}
else if (strstr(response, "ERROR"))
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);
@@ -332,7 +334,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);
}
@@ -341,7 +343,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);
}
@@ -350,7 +352,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);
}
@@ -359,7 +361,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);
}

View File

@@ -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);

View File

@@ -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;