From d3a76999d05ec02331dae5e65ede8a184ae6e759 Mon Sep 17 00:00:00 2001
From: "Kevin P. Fleming" <kpfleming@digium.com>
Date: Thu, 1 Sep 2005 22:28:33 +0000
Subject: [PATCH] various devicestate fixes (issue #5081, take two)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6496 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channel.c            | 47 ++++++++++++++++++--------------------------
 channels/chan_iax2.c |  5 ++---
 devicestate.c        | 13 +++++++++---
 3 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/channel.c b/channel.c
index 7e63bbb2a2..34ae2bf7db 100755
--- a/channel.c
+++ b/channel.c
@@ -2808,34 +2808,25 @@ void ast_set_callerid(struct ast_channel *chan, const char *callerid, const char
 
 int ast_setstate(struct ast_channel *chan, int state)
 {
-	if (chan->_state != state) {
-		int oldstate = chan->_state;
-		chan->_state = state;
-		if (oldstate == AST_STATE_DOWN) {
-			ast_device_state_changed(chan->name);
-			manager_event(EVENT_FLAG_CALL, "Newchannel",
-				"Channel: %s\r\n"
-				"State: %s\r\n"
-				"CallerID: %s\r\n"
-				"CallerIDName: %s\r\n"
-				"Uniqueid: %s\r\n",
-				chan->name, ast_state2str(chan->_state), 
-				chan->cid.cid_num ? chan->cid.cid_num : "<unknown>", 
-				chan->cid.cid_name ? chan->cid.cid_name : "<unknown>", 
-				chan->uniqueid);
-		} else {
-			manager_event(EVENT_FLAG_CALL, "Newstate", 
-				"Channel: %s\r\n"
-				"State: %s\r\n"
-				"CallerID: %s\r\n"
-				"CallerIDName: %s\r\n"
-				"Uniqueid: %s\r\n",
-				chan->name, ast_state2str(chan->_state), 
-				chan->cid.cid_num ? chan->cid.cid_num : "<unknown>", 
-				chan->cid.cid_name ? chan->cid.cid_name : "<unknown>", 
-				chan->uniqueid);
-		}
-	}
+	int oldstate = chan->_state;
+
+	if (oldstate == state)
+		return 0;
+
+	chan->_state = state;
+	ast_device_state_changed(chan->name);
+	manager_event(EVENT_FLAG_CALL,
+		      (oldstate == AST_STATE_DOWN) ? "Newchannel" : "Newstate",
+		      "Channel: %s\r\n"
+		      "State: %s\r\n"
+		      "CallerID: %s\r\n"
+		      "CallerIDName: %s\r\n"
+		      "Uniqueid: %s\r\n",
+		      chan->name, ast_state2str(chan->_state), 
+		      chan->cid.cid_num ? chan->cid.cid_num : "<unknown>", 
+		      chan->cid.cid_name ? chan->cid.cid_name : "<unknown>", 
+		      chan->uniqueid);
+
 	return 0;
 }
 
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index 4b9e5379a1..562108acc3 100755
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -9114,9 +9114,8 @@ static int iax2_devicestate(void *data)
 			/* Peer is registered, or have default IP address
 			   and a valid registration */
 			if (p->historicms == 0 || p->historicms <= p->maxms)
-				res = AST_DEVICE_NOT_INUSE;
-			else
-				res = AST_DEVICE_UNKNOWN;	/* Not reachable */
+				/* let the core figure out whether it is in use or not */
+				res = AST_DEVICE_UNKNOWN;	
 		}
 	} else {
 		if (option_debug > 2) 
diff --git a/devicestate.c b/devicestate.c
index c3e7c52d08..6701a80aea 100755
--- a/devicestate.c
+++ b/devicestate.c
@@ -109,9 +109,16 @@ int ast_device_state(const char *device)
 		return ast_parse_device_state(device);	/* No, try the generic function */
 	else {
 		res = chan_tech->devicestate(number);	/* Ask the channel driver for device state */
-		if (res == AST_DEVICE_UNKNOWN)
-			return ast_parse_device_state(device);
-		else
+		if (res == AST_DEVICE_UNKNOWN) {
+			res = ast_parse_device_state(device);
+			/* at this point we know the device exists, but the channel driver
+			   could not give us a state; if there is no channel state available,
+			   it must be 'not in use'
+			*/
+			if (res == AST_DEVICE_UNKNOWN)
+				res = AST_DEVICE_NOT_INUSE;
+			return res;
+		} else
 			return res;
 	}
 }
-- 
GitLab