From c3957b21e1aeb3a297b47455e7766dbb4a177d94 Mon Sep 17 00:00:00 2001
From: Tilghman Lesher <tilghman@meg.abyt.es>
Date: Fri, 4 Jan 2008 23:12:43 +0000
Subject: [PATCH] Merged revisions 96575 via svnmerge from
 https://origsvn.digium.com/svn/asterisk/branches/1.4

........
r96575 | tilghman | 2008-01-04 17:03:40 -0600 (Fri, 04 Jan 2008) | 7 lines

Fix the problem of notification of a device state change to a device with a '-'
in the name.  Could probably do with a better fix in trunk, but this bug has
been open way too long without a better solution.
Reported by: stevedavies
Patch by: tilghman
(Closes issue #9668)

........


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@96576 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 main/devicestate.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/main/devicestate.c b/main/devicestate.c
index 88b095b05d..d7790ba926 100644
--- a/main/devicestate.c
+++ b/main/devicestate.c
@@ -432,7 +432,7 @@ static void do_state_change(const char *device)
 	devstate_event(device, state, CACHE_OFF);
 }
 
-static int __ast_devstate_changed_literal(enum ast_device_state state, char *buf)
+static int __ast_devstate_changed_literal(enum ast_device_state state, char *buf, int norecurse)
 {
 	char *device;
 	struct state_change *change;
@@ -442,10 +442,6 @@ static int __ast_devstate_changed_literal(enum ast_device_state state, char *buf
 
 	device = buf;
 
-	tmp = strrchr(device, '-');
-	if (tmp)
-		*tmp = '\0';
-
 	if (state != AST_DEVICE_UNKNOWN) {
 		devstate_event(device, state, CACHE_ON);
 	} else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
@@ -461,6 +457,18 @@ static int __ast_devstate_changed_literal(enum ast_device_state state, char *buf
 		AST_LIST_UNLOCK(&state_changes);
 	}
 
+	/* The problem with this API is that a device may be called with the unique
+	 * identifier appended or not, but it's separated from the channel name
+	 * with a '-', which is also a legitimate character in a channel name.  So,
+	 * we have to force both names to get their names checked for state changes
+	 * to ensure that the right one gets notified.  Not a huge performance hit,
+	 * but it might could be fixed by an enterprising programmer in trunk.
+	 */
+	if (!norecurse && (tmp = strrchr(device, '-'))) {
+		*tmp = '\0';
+		__ast_devstate_changed_literal(state, tmp, 1);
+	}
+	
 	return 1;
 }
 
@@ -470,7 +478,7 @@ int ast_devstate_changed_literal(enum ast_device_state state, const char *dev)
 
 	buf = ast_strdupa(dev);
 
-	return __ast_devstate_changed_literal(state, buf);
+	return __ast_devstate_changed_literal(state, buf, 0);
 }
 
 int ast_device_state_changed_literal(const char *dev)
@@ -479,7 +487,7 @@ int ast_device_state_changed_literal(const char *dev)
 
 	buf = ast_strdupa(dev);
 
-	return __ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf);
+	return __ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf, 0);
 }
 
 int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...) 
@@ -491,7 +499,7 @@ int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...)
 	vsnprintf(buf, sizeof(buf), fmt, ap);
 	va_end(ap);
 
-	return __ast_devstate_changed_literal(state, buf);
+	return __ast_devstate_changed_literal(state, buf, 0);
 }
 
 /*! \brief Accept change notification, add it to change queue */
@@ -504,7 +512,7 @@ int ast_device_state_changed(const char *fmt, ...)
 	vsnprintf(buf, sizeof(buf), fmt, ap);
 	va_end(ap);
 
-	return __ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf);
+	return __ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf, 0);
 }
 
 /*! \brief Go through the dev state change queue and update changes in the dev state thread */
-- 
GitLab