mirror of
https://gitlab.com/mobian1/eg25-manager.git
synced 2025-08-29 23:32:14 +02:00
at: allow custom callbacks for AT command response processing
This commit is contained in:
32
src/at.c
32
src/at.c
@@ -17,14 +17,6 @@
|
|||||||
|
|
||||||
#include <glib-unix.h>
|
#include <glib-unix.h>
|
||||||
|
|
||||||
struct AtCommand {
|
|
||||||
char *cmd;
|
|
||||||
char *subcmd;
|
|
||||||
char *value;
|
|
||||||
char *expected;
|
|
||||||
int retries;
|
|
||||||
};
|
|
||||||
|
|
||||||
static GArray *configure_commands = NULL;
|
static GArray *configure_commands = NULL;
|
||||||
static GArray *suspend_commands = NULL;
|
static GArray *suspend_commands = NULL;
|
||||||
static GArray *resume_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);
|
len = sprintf(command, "AT+%s=\"%s\"\r\n", at_cmd->cmd, at_cmd->subcmd);
|
||||||
else if (at_cmd->subcmd && at_cmd->value)
|
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);
|
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);
|
ret = write(manager->at_fd, command, len);
|
||||||
if (ret < len)
|
if (ret < len)
|
||||||
@@ -164,7 +157,10 @@ 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,
|
||||||
|
void (*callback)
|
||||||
|
(struct EG25Manager *manager,
|
||||||
|
const char *response))
|
||||||
{
|
{
|
||||||
struct AtCommand *at_cmd = calloc(1, sizeof(struct AtCommand));
|
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);
|
at_cmd->value = g_strdup(value);
|
||||||
if (expected)
|
if (expected)
|
||||||
at_cmd->expected = g_strdup(expected);
|
at_cmd->expected = g_strdup(expected);
|
||||||
|
if (callback)
|
||||||
|
at_cmd->callback = callback;
|
||||||
|
|
||||||
manager->at_cmds = g_list_append(manager->at_cmds, at_cmd);
|
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"))
|
else if (strstr(response, "ERROR"))
|
||||||
retry_at_command(manager);
|
retry_at_command(manager);
|
||||||
else if (strstr(response, "OK"))
|
else if (strstr(response, "OK")) {
|
||||||
at_process_result(manager, response);
|
if (manager->at_callback != NULL)
|
||||||
|
manager->at_callback(manager, response);
|
||||||
|
else
|
||||||
|
g_warning("AT command succesfull but no callback registered");
|
||||||
|
}
|
||||||
else
|
else
|
||||||
// Not a recognized response, try running next command, just in case
|
// Not a recognized response, try running next command, just in case
|
||||||
at_next_command(manager);
|
at_next_command(manager);
|
||||||
@@ -332,7 +334,7 @@ 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);
|
||||||
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);
|
at_send_command(manager);
|
||||||
}
|
}
|
||||||
@@ -341,7 +343,7 @@ 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);
|
||||||
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);
|
at_send_command(manager);
|
||||||
}
|
}
|
||||||
@@ -350,7 +352,7 @@ 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);
|
||||||
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);
|
at_send_command(manager);
|
||||||
}
|
}
|
||||||
@@ -359,7 +361,7 @@ 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);
|
||||||
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);
|
at_send_command(manager);
|
||||||
}
|
}
|
||||||
|
14
src/at.h
14
src/at.h
@@ -8,6 +8,15 @@
|
|||||||
|
|
||||||
#include "manager.h"
|
#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);
|
int at_init(struct EG25Manager *manager, toml_table_t *config);
|
||||||
void at_destroy(struct EG25Manager *manager);
|
void at_destroy(struct EG25Manager *manager);
|
||||||
|
|
||||||
@@ -19,7 +28,10 @@ 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,
|
||||||
|
void (*callback)
|
||||||
|
(struct EG25Manager *manager,
|
||||||
|
const char *response));
|
||||||
|
|
||||||
void at_sequence_configure(struct EG25Manager *manager);
|
void at_sequence_configure(struct EG25Manager *manager);
|
||||||
void at_sequence_suspend(struct EG25Manager *manager);
|
void at_sequence_suspend(struct EG25Manager *manager);
|
||||||
|
@@ -47,6 +47,7 @@ struct EG25Manager {
|
|||||||
int at_fd;
|
int at_fd;
|
||||||
guint at_source;
|
guint at_source;
|
||||||
GList *at_cmds;
|
GList *at_cmds;
|
||||||
|
void (*at_callback)(struct EG25Manager *manager, const char *response);
|
||||||
|
|
||||||
enum EG25State modem_state;
|
enum EG25State modem_state;
|
||||||
gchar *modem_usb_id;
|
gchar *modem_usb_id;
|
||||||
|
Reference in New Issue
Block a user