<p>Richard Mudgett has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/13516">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">app_chanisavail.c: Simplify dialplan using ChanIsAvail.<br><br>Dialplan has to be careful about passing an empty device list or empty<br>positions in the list. As a result, dialplan has to check for these<br>conditions before using ChanIsAvail. Simplify dialplan by making<br>ChanIsAvail handle these conditions gracefully.<br><br>* Made tolerate empty positions in the device list.<br><br>* Simplified the code and eliminated some unnecessary indention.<br><br>ASTERISK-28638<br><br>Change-Id: I9e4b67e2cbf26b2417c2d03485b8568e898931d3<br>---<br>M apps/app_chanisavail.c<br>A doc/CHANGES-staging/app_chanisavail_empty_device_list.txt<br>2 files changed, 79 insertions(+), 69 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/16/13516/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/apps/app_chanisavail.c b/apps/app_chanisavail.c</span><br><span>index 1c7c1ec..62c9317 100644</span><br><span>--- a/apps/app_chanisavail.c</span><br><span>+++ b/apps/app_chanisavail.c</span><br><span>@@ -51,16 +51,18 @@</span><br><span> Check channel availability</span><br><span> </synopsis></span><br><span> <syntax></span><br><span style="color: hsl(0, 100%, 40%);">- <parameter name="Technology/Resource" required="true" argsep="&"></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="Technology/Resource" required="false" argsep="&"></span><br><span style="color: hsl(120, 100%, 40%);">+ <argument name="Technology/Resource" required="true"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Specification of the device(s) to check. These must be in the format of</span><br><span style="color: hsl(120, 100%, 40%);">+ <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable></span><br><span style="color: hsl(120, 100%, 40%);">+ represents a particular channel driver, and <replaceable>Resource</replaceable></span><br><span style="color: hsl(120, 100%, 40%);">+ represents a resource available to that particular channel driver.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </argument></span><br><span> <argument name="Technology2/Resource2" multiple="true"></span><br><span> <para>Optional extra devices to check</para></span><br><span> <para>If you need more than one enter them as</span><br><span> Technology2/Resource2&Technology3/Resource3&.....</para></span><br><span> </argument></span><br><span style="color: hsl(0, 100%, 40%);">- <para>Specification of the device(s) to check. These must be in the format of</span><br><span style="color: hsl(0, 100%, 40%);">- <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable></span><br><span style="color: hsl(0, 100%, 40%);">- represents a particular channel driver, and <replaceable>Resource</replaceable></span><br><span style="color: hsl(0, 100%, 40%);">- represents a resource available to that particular channel driver.</para></span><br><span> </parameter></span><br><span> <parameter name="options" required="false"></span><br><span> <optionlist></span><br><span>@@ -99,25 +101,28 @@</span><br><span> </span><br><span> static int chanavail_exec(struct ast_channel *chan, const char *data)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">- int inuse=-1, option_state=0, string_compare=0, option_all_avail=0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int inuse = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ int option_state = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int string_compare = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int option_all_avail = 0;</span><br><span> int status;</span><br><span style="color: hsl(0, 100%, 40%);">- char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *info;</span><br><span style="color: hsl(120, 100%, 40%);">+ char trychan[512];</span><br><span style="color: hsl(120, 100%, 40%);">+ char *rest;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *tech;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *number;</span><br><span> struct ast_str *tmp_availchan = ast_str_alloca(2048);</span><br><span> struct ast_str *tmp_availorig = ast_str_alloca(2048);</span><br><span> struct ast_str *tmp_availstat = ast_str_alloca(2048);</span><br><span> struct ast_str *tmp_availcause = ast_str_alloca(2048);</span><br><span> struct ast_channel *tempchan;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_custom_function *cdr_prop_func = ast_custom_function_find("CDR_PROP");</span><br><span> AST_DECLARE_APP_ARGS(args,</span><br><span> AST_APP_ARG(reqchans);</span><br><span> AST_APP_ARG(options);</span><br><span> );</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_strlen_zero(data)) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "ChanIsAvail requires an argument (DAHDI/1&DAHDI/2)\n");</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- info = ast_strdupa(data);</span><br><span style="color: hsl(120, 100%, 40%);">+ info = ast_strdupa(data ?: "");</span><br><span> </span><br><span> AST_STANDARD_APP_ARGS(args, info);</span><br><span> </span><br><span>@@ -132,68 +137,68 @@</span><br><span> string_compare = 1;</span><br><span> }</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- peers = args.reqchans;</span><br><span style="color: hsl(0, 100%, 40%);">- if (peers) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_custom_function *cdr_prop_func = ast_custom_function_find("CDR_PROP");</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- cur = peers;</span><br><span style="color: hsl(0, 100%, 40%);">- do {</span><br><span style="color: hsl(0, 100%, 40%);">- /* remember where to start next time */</span><br><span style="color: hsl(0, 100%, 40%);">- rest = strchr(cur, '&');</span><br><span style="color: hsl(0, 100%, 40%);">- if (rest) {</span><br><span style="color: hsl(0, 100%, 40%);">- *rest = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- rest++;</span><br><span style="color: hsl(120, 100%, 40%);">+ rest = args.reqchans;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!rest) {</span><br><span style="color: hsl(120, 100%, 40%);">+ rest = "";</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((tech = strsep(&rest, "&"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ tech = ast_strip(tech);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ number = strchr(tech, '/');</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!number) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(tech)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid '%s': Argument takes format (technology/resource)\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ tech);</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- tech = cur;</span><br><span style="color: hsl(0, 100%, 40%);">- number = strchr(tech, '/');</span><br><span style="color: hsl(0, 100%, 40%);">- if (!number) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_append(&tmp_availstat, 0, "%s%d",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_strlen(tmp_availstat) ? "&" : "", AST_DEVICE_INVALID);</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ *number++ = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ status = AST_DEVICE_UNKNOWN;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (string_compare) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* ast_parse_device_state checks for "SIP/1234" as a channel name.</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_device_state will ask the SIP driver for the channel state. */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(trychan, sizeof(trychan), "%s/%s", tech, number);</span><br><span style="color: hsl(120, 100%, 40%);">+ status = inuse = ast_parse_device_state(trychan);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (option_state) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If the pbx says in use then don't bother trying further.</span><br><span style="color: hsl(120, 100%, 40%);">+ This is to permit testing if someone's on a call, even if the</span><br><span style="color: hsl(120, 100%, 40%);">+ channel can permit more calls (ie callwaiting, sip calls, etc). */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(trychan, sizeof(trychan), "%s/%s", tech, number);</span><br><span style="color: hsl(120, 100%, 40%);">+ status = inuse = ast_device_state(trychan);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_append(&tmp_availstat, 0, "%s%d",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_strlen(tmp_availstat) ? "&" : "", status);</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((inuse <= (int) AST_DEVICE_NOT_INUSE)</span><br><span style="color: hsl(120, 100%, 40%);">+ && (tempchan = ast_request(tech, ast_channel_nativeformats(chan), NULL, chan, number, &status))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_append(&tmp_availchan, 0, "%s%s",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_strlen(tmp_availchan) ? "&" : "", ast_channel_name(tempchan));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_append(&tmp_availorig, 0, "%s%s/%s",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_strlen(tmp_availorig) ? "&" : "", tech, number);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_append(&tmp_availcause, 0, "%s%d",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_strlen(tmp_availcause) ? "&" : "", status);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Disable CDR for this temporary channel. */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (cdr_prop_func) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_func_write(tempchan, "CDR_PROP(disable)", "1");</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- *number = '\0';</span><br><span style="color: hsl(0, 100%, 40%);">- number++;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- status = AST_DEVICE_UNKNOWN;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_hangup(tempchan);</span><br><span style="color: hsl(120, 100%, 40%);">+ tempchan = NULL;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (string_compare) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* ast_parse_device_state checks for "SIP/1234" as a channel name.</span><br><span style="color: hsl(0, 100%, 40%);">- ast_device_state will ask the SIP driver for the channel state. */</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);</span><br><span style="color: hsl(0, 100%, 40%);">- status = inuse = ast_parse_device_state(trychan);</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (option_state) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* If the pbx says in use then don't bother trying further.</span><br><span style="color: hsl(0, 100%, 40%);">- This is to permit testing if someone's on a call, even if the</span><br><span style="color: hsl(0, 100%, 40%);">- channel can permit more calls (ie callwaiting, sip calls, etc). */</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);</span><br><span style="color: hsl(0, 100%, 40%);">- status = inuse = ast_device_state(trychan);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!option_all_avail) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(tmp, sizeof(tmp), "%d", status);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_str_append(&tmp_availstat, 0, "%s%s", ast_str_strlen(tmp_availstat) ? "&" : "", tmp);</span><br><span style="color: hsl(0, 100%, 40%);">- if ((inuse <= 1) && (tempchan = ast_request(tech, ast_channel_nativeformats(chan), NULL, chan, number, &status))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_str_append(&tmp_availchan, 0, "%s%s", ast_str_strlen(tmp_availchan) ? "&" : "", ast_channel_name(tempchan));</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_str_append(&tmp_availorig, 0, "%s%s", ast_str_strlen(tmp_availorig) ? "&" : "", tmp);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf(tmp, sizeof(tmp), "%d", status);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_str_append(&tmp_availcause, 0, "%s%s", ast_str_strlen(tmp_availcause) ? "&" : "", tmp);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Disable CDR for this temporary channel. */</span><br><span style="color: hsl(0, 100%, 40%);">- if (cdr_prop_func) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_func_write(tempchan, "CDR_PROP(disable)", "1");</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_hangup(tempchan);</span><br><span style="color: hsl(0, 100%, 40%);">- tempchan = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (!option_all_avail) {</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- cur = rest;</span><br><span style="color: hsl(0, 100%, 40%);">- } while (cur);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span> </span><br><span> pbx_builtin_setvar_helper(chan, "AVAILCHAN", ast_str_buffer(tmp_availchan));</span><br><span>diff --git a/doc/CHANGES-staging/app_chanisavail_empty_device_list.txt b/doc/CHANGES-staging/app_chanisavail_empty_device_list.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..bf06c31</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/app_chanisavail_empty_device_list.txt</span><br><span>@@ -0,0 +1,5 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: app_chanisavail</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+The ChanIsAvail application now tolerates empty positions in the supplied</span><br><span style="color: hsl(120, 100%, 40%);">+device list. Dialplan can now be simplified by not having to check for</span><br><span style="color: hsl(120, 100%, 40%);">+empty positions in the device list.</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/13516">change 13516</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/13516"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: I9e4b67e2cbf26b2417c2d03485b8568e898931d3 </div>
<div style="display:none"> Gerrit-Change-Number: 13516 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Richard Mudgett <rmudgett@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>