From 3d409eb793d0cba852dbaa42dc9b0e6d8fb805d0 Mon Sep 17 00:00:00 2001
From: Russell Bryant <russell@russellbryant.com>
Date: Wed, 2 May 2007 15:46:49 +0000
Subject: [PATCH] Update the device state functionality of chan_local such that
 it will return NOT_INUSE or INUSE when Local channels are in use as opposed
 to just UNKNOWN. It will still return INVALID if the extension doesn't exist
 at all. (issue #8048, patch from tim_ringenbach)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@62673 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 CHANGES               |  3 +++
 channels/chan_local.c | 16 ++++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/CHANGES b/CHANGES
index 23995bb149..ad64b4e900 100644
--- a/CHANGES
+++ b/CHANGES
@@ -182,3 +182,6 @@ Miscellaneous
      back to the person that did the transfer if the transfer is not successful.
      See the options "atxferdropcall", "atxferloopdelay", and "atxfercallbackretries"
      in features.conf.sample.
+  * The device state functionality in the Local channel driver has been updated
+     to indicate INUSE or NOT_INUSE when a Local channel is being used as opposed
+     to just UNKNOWN if the extension exists.
diff --git a/channels/chan_local.c b/channels/chan_local.c
index 15a38a693a..4b3484b4bc 100644
--- a/channels/chan_local.c
+++ b/channels/chan_local.c
@@ -129,6 +129,7 @@ static int local_devicestate(void *data)
 	char *exten = ast_strdupa(data);
 	char *context = NULL, *opts = NULL;
 	int res;
+	struct local_pvt *lp;
 
 	if (!(context = strchr(exten, '@'))) {
 		ast_log(LOG_WARNING, "Someone used Local/%s somewhere without a @context. This is bad.\n", exten);
@@ -143,11 +144,22 @@ static int local_devicestate(void *data)
 
 	if (option_debug > 2)
 		ast_log(LOG_DEBUG, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
+
 	res = ast_exists_extension(NULL, context, exten, 1, NULL);
 	if (!res)		
 		return AST_DEVICE_INVALID;
-	else
-		return AST_DEVICE_UNKNOWN;
+	
+	res = AST_DEVICE_NOT_INUSE;
+	AST_LIST_LOCK(&locals);
+	AST_LIST_TRAVERSE(&locals, lp, list) {
+		if (!strcmp(exten, lp->exten) && !strcmp(context, lp->context) && lp->owner) {
+			res = AST_DEVICE_INUSE;
+			break;
+		}
+	}
+	AST_LIST_UNLOCK(&locals);
+
+	return res;
 }
 
 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us)
-- 
GitLab