<p>sungtae kim has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/11382">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res/ari/resource_channels.c: Added hangup reason code for channels<br><br>Currently, DELETE /ari/channels/<channelID> supports only few hangup reasons.<br>It's good enough for simple use, but when it needs to set the detail reason,<br>it comes challenges.<br>Added reason_code query parameter for that.<br><br>ASTERISK-28385<br><br>Change-Id: I1cf1d991ffd759d0591b347445a55f416ddc3ff2<br>---<br>M res/ari/resource_channels.c<br>M res/ari/resource_channels.h<br>M res/res_ari_channels.c<br>M rest-api/api-docs/channels.json<br>4 files changed, 78 insertions(+), 15 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/82/11382/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/ari/resource_channels.c b/res/ari/resource_channels.c</span><br><span>index bdb3534..d20d0c4 100644</span><br><span>--- a/res/ari/resource_channels.c</span><br><span>+++ b/res/ari/resource_channels.c</span><br><span>@@ -55,6 +55,40 @@</span><br><span> #include <limits.h></span><br><span> </span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Return the corresponded hangup code of the given reason */</span><br><span style="color: hsl(120, 100%, 40%);">+static int convert_reason_to_hangup_code(const char* reason)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcmp(reason, "normal")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_NORMAL;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "busy")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_BUSY;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "congestion")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_CONGESTION;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "no_answer")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_NOANSWER;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "timeout")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_NO_USER_RESPONSE;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "rejected")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_CALL_REJECTED;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "unallocated")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_UNALLOCATED;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "normal_unspecified")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_NORMAL_UNSPECIFIED;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "number_incomplete")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_INVALID_NUMBER_FORMAT;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "codec_mismatch")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "interworking")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_INTERWORKING;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!strcmp(reason, "failure")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_FAILURE;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if(!strcmp(reason, "answered_elsewhere")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_CAUSE_ANSWERED_ELSEWHERE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*!</span><br><span> * \brief Finds the control object for a channel, filling the response with an</span><br><span> * error, if appropriate.</span><br><span>@@ -782,21 +816,34 @@</span><br><span> return;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_strlen_zero(args->reason) || !strcmp(args->reason, "normal")) {</span><br><span style="color: hsl(0, 100%, 40%);">- cause = AST_CAUSE_NORMAL;</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!strcmp(args->reason, "busy")) {</span><br><span style="color: hsl(0, 100%, 40%);">- cause = AST_CAUSE_BUSY;</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!strcmp(args->reason, "congestion")) {</span><br><span style="color: hsl(0, 100%, 40%);">- cause = AST_CAUSE_CONGESTION;</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!strcmp(args->reason, "no_answer")) {</span><br><span style="color: hsl(0, 100%, 40%);">- cause = AST_CAUSE_NOANSWER;</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_ari_response_error(</span><br><span style="color: hsl(0, 100%, 40%);">- response, 400, "Invalid Reason",</span><br><span style="color: hsl(0, 100%, 40%);">- "Invalid reason for hangup provided");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(args->reason) && !ast_strlen_zero(args->reason_code)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_ari_response_error(response, 400, "Bad Request",</span><br><span style="color: hsl(120, 100%, 40%);">+ "The reason and reason_code can't both be specified");</span><br><span> return;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(args->reason_code)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* reason_code allows any hangup code */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sscanf(args->reason_code, "%30d", &cause) != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_ari_response_error(</span><br><span style="color: hsl(120, 100%, 40%);">+ response, 400, "Invalid Reason Code",</span><br><span style="color: hsl(120, 100%, 40%);">+ "Invalid reason for hangup reason code provided");</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!ast_strlen_zero(args->reason)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* reason allows only listed hangup reason */</span><br><span style="color: hsl(120, 100%, 40%);">+ cause = convert_reason_to_hangup_code(args->reason);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (cause == -1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_ari_response_error(</span><br><span style="color: hsl(120, 100%, 40%);">+ response, 400, "Invalid Reason",</span><br><span style="color: hsl(120, 100%, 40%);">+ "Invalid reason for hangup reason provided");</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* not specified. set default hangup */</span><br><span style="color: hsl(120, 100%, 40%);">+ cause = AST_CAUSE_NORMAL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> ast_channel_hangupcause_set(chan, cause);</span><br><span> ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);</span><br><span> </span><br><span>diff --git a/res/ari/resource_channels.h b/res/ari/resource_channels.h</span><br><span>index 3077297..adecb3d 100644</span><br><span>--- a/res/ari/resource_channels.h</span><br><span>+++ b/res/ari/resource_channels.h</span><br><span>@@ -171,7 +171,9 @@</span><br><span> struct ast_ari_channels_hangup_args {</span><br><span> /*! Channel's id */</span><br><span> const char *channel_id;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! Reason for hanging up the channel */</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! The reason code for hanging up the channel for detail use. Mutually exclusive with 'reason'. See detail hangup codes at here. https://wiki.asterisk.org/wiki/display/AST/Hangup+Cause+Mappings */</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *reason_code;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Reason for hanging up the channel for simple use. Mutually exclusive with 'reason_code'. */</span><br><span> const char *reason;</span><br><span> };</span><br><span> /*!</span><br><span>diff --git a/res/res_ari_channels.c b/res/res_ari_channels.c</span><br><span>index 5119d64..6b45210 100644</span><br><span>--- a/res/res_ari_channels.c</span><br><span>+++ b/res/res_ari_channels.c</span><br><span>@@ -479,6 +479,10 @@</span><br><span> {</span><br><span> struct ast_json *field;</span><br><span> /* Parse query parameters out of it */</span><br><span style="color: hsl(120, 100%, 40%);">+ field = ast_json_object_get(body, "reason_code");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (field) {</span><br><span style="color: hsl(120, 100%, 40%);">+ args->reason_code = ast_json_string_get(field);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> field = ast_json_object_get(body, "reason");</span><br><span> if (field) {</span><br><span> args->reason = ast_json_string_get(field);</span><br><span>@@ -506,6 +510,9 @@</span><br><span> #endif /* AST_DEVMODE */</span><br><span> </span><br><span> for (i = get_params; i; i = i->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strcmp(i->name, "reason_code") == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ args.reason_code = (i->value);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else</span><br><span> if (strcmp(i->name, "reason") == 0) {</span><br><span> args.reason = (i->value);</span><br><span> } else</span><br><span>diff --git a/rest-api/api-docs/channels.json b/rest-api/api-docs/channels.json</span><br><span>index fdeb0b8..446cdd8 100644</span><br><span>--- a/rest-api/api-docs/channels.json</span><br><span>+++ b/rest-api/api-docs/channels.json</span><br><span>@@ -324,13 +324,20 @@</span><br><span> "dataType": "string"</span><br><span> },</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ "name": "reason_code",</span><br><span style="color: hsl(120, 100%, 40%);">+ "description": "The reason code for hanging up the channel for detail use. Mutually exclusive with 'reason'. See detail hangup codes at here. https://wiki.asterisk.org/wiki/display/AST/Hangup+Cause+Mappings",</span><br><span style="color: hsl(120, 100%, 40%);">+ "paramType": "query",</span><br><span style="color: hsl(120, 100%, 40%);">+ "required": false,</span><br><span style="color: hsl(120, 100%, 40%);">+ "allowMultiple": false,</span><br><span style="color: hsl(120, 100%, 40%);">+ "dataType": "string"</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+ {</span><br><span> "name": "reason",</span><br><span style="color: hsl(0, 100%, 40%);">- "description": "Reason for hanging up the channel",</span><br><span style="color: hsl(120, 100%, 40%);">+ "description": "Reason for hanging up the channel for simple use. Mutually exclusive with 'reason_code'.",</span><br><span> "paramType": "query",</span><br><span> "required": false,</span><br><span> "allowMultiple": false,</span><br><span> "dataType": "string",</span><br><span style="color: hsl(0, 100%, 40%);">- "defalutValue": "normal",</span><br><span> "allowableValues": {</span><br><span> "valueType": "LIST",</span><br><span> "values": [</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/11382">change 11382</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/+/11382"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-Change-Id: I1cf1d991ffd759d0591b347445a55f416ddc3ff2 </div>
<div style="display:none"> Gerrit-Change-Number: 11382 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: sungtae kim <pchero21@gmail.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>