diff -Nur channels.old/chan_mobile.c channels/chan_mobile.c --- channels.old/chan_mobile.c 2009-07-25 16:56:44.000000000 +0200 +++ channels/chan_mobile.c 2009-07-25 17:00:31.000000000 +0200 @@ -148,6 +148,7 @@ unsigned int needring:1; /*!< we need to send a RING */ unsigned int answered:1; /*!< we sent/recieved an answer */ unsigned int connected:1; /*!< do we have an rfcomm connection to a device */ + unsigned int group_last_used:1; /*!< mark the last used device */ AST_LIST_ENTRY(mbl_pvt) entry; }; @@ -812,9 +813,18 @@ struct ast_channel *chn = NULL; struct mbl_pvt *pvt; + struct mbl_pvt *device_list[256]; char *dest_dev = NULL; char *dest_num = NULL; int oldformat, group = -1; + int loop_count = 0; + int loop_count2 = 0; + int last_used_device = 0; + int i = 0; + + for (i=0;i<256;i++) { + device_list[i] = NULL; + } if (!data) { ast_log(LOG_WARNING, "Channel requested with no data\n"); @@ -836,20 +846,67 @@ if (dest_num) *dest_num++ = 0x00; + /* Find requested device and make sure it's connected. */ if (((dest_dev[0] == 'g') || (dest_dev[0] == 'G')) && ((dest_dev[1] >= '0') && (dest_dev[1] <= '9'))) { group = atoi(&dest_dev[1]); + AST_RWLIST_RDLOCK(&devices); + AST_RWLIST_TRAVERSE(&devices, pvt, entry) { + if (group > -1 && pvt->group == group && pvt->connected && !pvt->owner) { + break; + } + } + AST_RWLIST_UNLOCK(&devices); } + else if (((dest_dev[0] == 'r') || (dest_dev[0] == 'R')) && ((dest_dev[1] >= '0') && (dest_dev[1] <= '9'))) { + group = atoi(&dest_dev[1]); + AST_RWLIST_RDLOCK(&devices); - /* Find requested device and make sure it's connected. */ - AST_RWLIST_RDLOCK(&devices); - AST_RWLIST_TRAVERSE(&devices, pvt, entry) { - if (group > -1 && pvt->group == group && pvt->connected && !pvt->owner) { - break; - } else if (!strcmp(pvt->id, dest_dev)) { - break; + // Generate a list of all availible devices + AST_RWLIST_TRAVERSE(&devices, pvt, entry) { + if (group > -1 && pvt->group == group) { + device_list[loop_count] = pvt; + loop_count++; + } + } + + // Find last used device + for (i=0;igroup_last_used == 1) { + last_used_device = i; + device_list[i]->group_last_used = 0; + break; + } + } + + // Search for a availible device starting at the last used device + loop_count2 = last_used_device; + + for (i=0;iconnected && !pvt->owner) { + pvt->group_last_used = 1; + break; + } } + + AST_RWLIST_UNLOCK(&devices); } - AST_RWLIST_UNLOCK(&devices); + else { + AST_RWLIST_RDLOCK(&devices); + AST_RWLIST_TRAVERSE(&devices, pvt, entry) { + if (!strcmp(pvt->id, dest_dev)) { + break; + } + } + AST_RWLIST_UNLOCK(&devices); + } + if (!pvt || !pvt->connected || pvt->owner) { ast_log(LOG_WARNING, "Request to call on device %s which is not connected / already in use.\n", dest_dev); *cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;