Skip to content

Commit

Permalink
Make hints for invalid SIP devices return Unavail, not idle
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Terry Wilson committed Mar 13, 2012
1 parent 7876521 commit 699d2bd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 88 deletions.
2 changes: 0 additions & 2 deletions channels/chan_sip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 3 additions & 8 deletions include/asterisk/devicestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/*!
Expand Down
100 changes: 24 additions & 76 deletions main/devicestate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_devicestate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 699d2bd

Please sign in to comment.