Make hints for invalid SIP devices return Unavail, not idle
authorTerry Wilson <twilson@digium.com>
Tue, 13 Mar 2012 20:06:57 +0000 (20:06 +0000)
committerTerry Wilson <twilson@digium.com>
Tue, 13 Mar 2012 20:06:57 +0000 (20:06 +0000)
This patch drastically simplifies the device state aggegation code.
The old method was not only overly complex, but also made it impossible
to return AST_DEVICE_INVALID from the aggregation code. The unit test
update is as a result of fixing that bug.

The SIP change stems from a bug introduced by removing a DNS lookup
for hostname-based SIP channels.

(closes issue ASTERISK-16702)
Review: https://reviewboard.asterisk.org/r/1808/
........

Merged revisions 358943 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 358944 from http://svn.asterisk.org/svn/asterisk/branches/10

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@358945 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/chan_sip.c
include/asterisk/devicestate.h
main/devicestate.c
tests/test_devicestate.c

index 3aa2bf2..3e9fee3 100644 (file)
@@ -27507,8 +27507,6 @@ static int sip_devicestate(const char *data)
                        res = AST_DEVICE_UNAVAILABLE;
                }
                sip_unref_peer(p, "sip_unref_peer, from sip_devicestate, release ref from sip_find_peer");
-       } else {
-               res = AST_DEVICE_UNKNOWN;
        }
 
        return res;
index 2a53ebb..66ca2bd 100644 (file)
@@ -254,14 +254,9 @@ enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregat
  * This struct is only here so that it can be easily declared on the stack.
  */
 struct ast_devstate_aggregate {
-       unsigned int all_unknown:1;
-       unsigned int all_unavail:1;
-       unsigned int all_busy:1;
-       unsigned int all_free:1;
-       unsigned int on_hold:1;
-       unsigned int busy:1;
-       unsigned int in_use:1;
-       unsigned int ring:1;
+       unsigned int ringing:1;
+       unsigned int inuse:1;
+       enum ast_device_state state;
 };
 
 /*!
index 1002e38..6592625 100644 (file)
@@ -732,91 +732,39 @@ int ast_device_state_engine_init(void)
 void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
 {
        memset(agg, 0, sizeof(*agg));
-
-       agg->all_unknown = 1;
-       agg->all_unavail = 1;
-       agg->all_busy = 1;
-       agg->all_free = 1;
+       agg->state = AST_DEVICE_INVALID;
 }
 
 void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state)
 {
-       switch (state) {
-       case AST_DEVICE_NOT_INUSE:
-               agg->all_unknown = 0;
-               agg->all_unavail = 0;
-               agg->all_busy = 0;
-               break;
-       case AST_DEVICE_INUSE:
-               agg->in_use = 1;
-               agg->all_unavail = 0;
-               agg->all_free = 0;
-               agg->all_unknown = 0;
-               break;
-       case AST_DEVICE_RINGING:
-               agg->ring = 1;
-               agg->all_unavail = 0;
-               agg->all_free = 0;
-               agg->all_unknown = 0;
-               break;
-       case AST_DEVICE_RINGINUSE:
-               agg->in_use = 1;
-               agg->ring = 1;
-               agg->all_unavail = 0;
-               agg->all_free = 0;
-               agg->all_unknown = 0;
-               break;
-       case AST_DEVICE_ONHOLD:
-               agg->all_unknown = 0;
-               agg->all_unavail = 0;
-               agg->all_free = 0;
-               agg->on_hold = 1;
-               break;
-       case AST_DEVICE_BUSY:
-               agg->all_unknown = 0;
-               agg->all_unavail = 0;
-               agg->all_free = 0;
-               agg->busy = 1;
-               agg->in_use = 1;
-               break;
-       case AST_DEVICE_UNAVAILABLE:
-               agg->all_unknown = 0;
-       case AST_DEVICE_INVALID:
-               agg->all_busy = 0;
-               agg->all_free = 0;
-               break;
-       case AST_DEVICE_UNKNOWN:
-               agg->all_busy = 0;
-               agg->all_free = 0;
-               break;
-       case AST_DEVICE_TOTAL: /* not a device state, included for completeness. */
-               break;
+       static enum ast_device_state state_order[] = {
+               1, /* AST_DEVICE_UNKNOWN */
+               3, /* AST_DEVICE_NOT_INUSE */
+               6, /* AST_DEVICE_INUSE */
+               7, /* AST_DEVICE_BUSY */
+               0, /* AST_DEVICE_INVALID */
+               2, /* AST_DEVICE_UNAVAILABLE */
+               5, /* AST_DEVICE_RINGING */
+               8, /* AST_DEVICE_RINGINUSE */
+               4, /* AST_DEVICE_ONHOLD */
+       };
+
+       if (state == AST_DEVICE_RINGING) {
+               agg->ringing = 1;
+       } else if (state == AST_DEVICE_INUSE || state == AST_DEVICE_ONHOLD || state == AST_DEVICE_BUSY) {
+               agg->inuse = 1;
        }
-}
 
+       if (agg->ringing && agg->inuse) {
+               agg->state = AST_DEVICE_RINGINUSE;
+       } else if (state_order[state] > state_order[agg->state]) {
+               agg->state = state;
+       }
+}
 
 enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
 {
-       if (agg->all_free)
-               return AST_DEVICE_NOT_INUSE;
-       if ((agg->in_use || agg->on_hold) && agg->ring)
-               return AST_DEVICE_RINGINUSE;
-       if (agg->ring)
-               return AST_DEVICE_RINGING;
-       if (agg->busy)
-               return AST_DEVICE_BUSY;
-       if (agg->in_use)
-               return AST_DEVICE_INUSE;
-       if (agg->on_hold)
-               return AST_DEVICE_ONHOLD;
-       if (agg->all_busy)
-               return AST_DEVICE_BUSY;
-       if (agg->all_unknown)
-               return AST_DEVICE_UNKNOWN;
-       if (agg->all_unavail)
-               return AST_DEVICE_UNAVAILABLE;
-
-       return AST_DEVICE_NOT_INUSE;
+       return agg->state;
 }
 
 int ast_enable_distributed_devstate(void)
index ede2b23..f5f209b 100644 (file)
@@ -83,7 +83,7 @@ static int combined_results[] = {
        AST_DEVICE_NOT_INUSE,
        AST_DEVICE_INUSE,
        AST_DEVICE_BUSY,
-       AST_DEVICE_UNKNOWN,
+       AST_DEVICE_INVALID,
        AST_DEVICE_UNAVAILABLE,
        AST_DEVICE_RINGING,
        AST_DEVICE_RINGINUSE,
@@ -167,7 +167,7 @@ static int exten_results[] = {
        AST_EXTENSION_NOT_INUSE,
        AST_EXTENSION_INUSE,
        AST_EXTENSION_BUSY,
-       AST_EXTENSION_NOT_INUSE,
+       AST_EXTENSION_UNAVAILABLE,
        AST_EXTENSION_UNAVAILABLE,
        AST_EXTENSION_RINGING,
        AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,