diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2011-11-29 09:21:57 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-03-15 14:14:29 +0100 |
commit | 2f5e8c46a34bd88eba47b0488dcc5d3d0fe242e4 (patch) | |
tree | d21eea9a13cb05067e2a39d6f05f339b4bc5fded | |
parent | 20f53ec15b5f9b7b3251259bedceccfb1ae5114e (diff) |
at-serial-port: allow overwriting unsolicited message handlers
Existing unsolicited message handlers can be overwritten at any time now. This
allows initializing the port with all possible message handlers configured with
a NULL callback, and then setup the proper handlers when we go on enabling the
different interfaces.
-rw-r--r-- | src/mm-at-serial-port.c | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/mm-at-serial-port.c b/src/mm-at-serial-port.c index 5977bda3..7f8fcc53 100644 --- a/src/mm-at-serial-port.c +++ b/src/mm-at-serial-port.c @@ -145,6 +145,14 @@ typedef struct { GDestroyNotify notify; } MMAtUnsolicitedMsgHandler; +static gint +unsolicited_msg_handler_cmp (MMAtUnsolicitedMsgHandler *handler, + GRegex *regex) +{ + return g_strcmp0 (g_regex_get_pattern (handler->regex), + g_regex_get_pattern (regex)); +} + void mm_at_serial_port_add_unsolicited_msg_handler (MMAtSerialPort *self, GRegex *regex, @@ -152,20 +160,32 @@ mm_at_serial_port_add_unsolicited_msg_handler (MMAtSerialPort *self, gpointer user_data, GDestroyNotify notify) { + GSList *existing; MMAtUnsolicitedMsgHandler *handler; MMAtSerialPortPrivate *priv; g_return_if_fail (MM_IS_AT_SERIAL_PORT (self)); g_return_if_fail (regex != NULL); - handler = g_slice_new (MMAtUnsolicitedMsgHandler); - handler->regex = g_regex_ref (regex); + priv = MM_AT_SERIAL_PORT_GET_PRIVATE (self); + + existing = g_slist_find_custom (priv->unsolicited_msg_handlers, + regex, + (GCompareFunc)unsolicited_msg_handler_cmp); + if (existing) { + handler = existing->data; + /* We OVERWRITE any existing one, so if any context data existing, free it */ + if (handler->notify) + handler->notify (handler->user_data); + } else { + handler = g_slice_new (MMAtUnsolicitedMsgHandler); + priv->unsolicited_msg_handlers = g_slist_append (priv->unsolicited_msg_handlers, handler); + handler->regex = g_regex_ref (regex); + } + handler->callback = callback; handler->user_data = user_data; handler->notify = notify; - - priv = MM_AT_SERIAL_PORT_GET_PRIVATE (self); - priv->unsolicited_msg_handlers = g_slist_append (priv->unsolicited_msg_handlers, handler); } static gboolean |