diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2008-12-30 20:17:23 +0100 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2008-12-30 20:17:23 +0100 |
commit | eee11dfca5d8c21eaad3f1958a4db2483e634734 (patch) | |
tree | 98cf7cbd51a86a67a21e69a8702d5e4813e3ea28 /plugins | |
parent | e7b4ef1fb306580fa5bb0baabe83149d6f7d93dc (diff) | |
download | connman-eee11dfca5d8c21eaad3f1958a4db2483e634734.tar.gz connman-eee11dfca5d8c21eaad3f1958a4db2483e634734.tar.bz2 connman-eee11dfca5d8c21eaad3f1958a4db2483e634734.zip |
Add support for async events callbacks
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/modem.c | 40 | ||||
-rw-r--r-- | plugins/modem.h | 3 |
2 files changed, 43 insertions, 0 deletions
diff --git a/plugins/modem.c b/plugins/modem.c index b7701704..4668bd38 100644 --- a/plugins/modem.c +++ b/plugins/modem.c @@ -40,11 +40,18 @@ struct modem_data { char *device; GIOChannel *channel; + GSList *callbacks; GSList *commands; char buf[1024]; int offset; }; +struct modem_callback { + char *command; + modem_cb_t function; + void *user_data; +}; + struct modem_cmd { char *cmd; char *arg; @@ -118,6 +125,7 @@ static gboolean modem_event(GIOChannel *channel, { struct modem_data *modem = user_data; struct modem_cmd *cmd; + GSList *list; gsize len; GIOError err; @@ -134,12 +142,26 @@ static gboolean modem_event(GIOChannel *channel, DBG("Read %d bytes (offset %d)", len, modem->offset); + if (g_str_has_suffix(modem->buf, "\r\n") == TRUE) { + for (list = modem->callbacks; list; list = list->next) { + struct modem_callback *callback = list->data; + + if (callback->function == NULL) + continue; + + if (g_strrstr(modem->buf, callback->command) != NULL) + callback->function(modem->buf, + callback->user_data); + } + } + if (g_strrstr(modem->buf, "\r\nERROR\r\n") == NULL && g_strrstr(modem->buf, "\r\nOK\r\n") == NULL) { modem->offset += len; return TRUE; } + memset(modem->buf, 0, sizeof(modem->buf)); modem->offset = 0; cmd = g_slist_nth_data(modem->commands, 0); @@ -232,6 +254,24 @@ int modem_close(struct modem_data *modem) return 0; } +int modem_add_callback(struct modem_data *modem, const char *command, + modem_cb_t function, void *user_data) +{ + struct modem_callback *callback; + + callback = g_try_new0(struct modem_callback, 1); + if (callback == NULL) + return -ENOMEM; + + callback->command = g_strdup(command); + callback->function = function; + callback->user_data = user_data; + + modem->callbacks = g_slist_append(modem->callbacks, callback); + + return 0; +} + static int modem_command_valist(struct modem_data *modem, modem_cb_t callback, void *user_data, const char *command, const char *format, va_list var_args) diff --git a/plugins/modem.h b/plugins/modem.h index ef474d6c..6fabcb36 100644 --- a/plugins/modem.h +++ b/plugins/modem.h @@ -31,6 +31,9 @@ int modem_close(struct modem_data *modem); typedef void (* modem_cb_t) (const char *buf, void *user_data); +int modem_add_callback(struct modem_data *modem, const char *command, + modem_cb_t function, void *user_data); + int modem_command(struct modem_data *modem, modem_cb_t callback, void *user_data, const char *command, const char *format, ...); |