From 2f5e8c46a34bd88eba47b0488dcc5d3d0fe242e4 Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Tue, 29 Nov 2011 09:21:57 +0100 Subject: 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. --- src/mm-at-serial-port.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'src') 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 -- cgit v1.2.3-70-g09d2