<p>Moritz Fain has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18567">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">ari: expose channel driver's unique id to ARI channel resource<br><br>This change exposes the channel driver's unique id (i.e. the Call-ID<br>for chan_sip/chan_pjsip based channels) to ARI channel resources<br>as `protocol_id`.<br><br>ASTERISK-30027<br>Reported by: Moritz Fain<br>Tested by: Moritz Fain<br><br>Change-Id: I7cc6e7a9d29efe74bc27811d788dac20fe559b87<br>---<br>M channels/chan_pjsip.c<br>A doc/CHANGES-staging/ari_add_pvt_id_to_channel_resource.txt<br>M include/asterisk/stasis_channels.h<br>M main/stasis_channels.c<br>M res/ari/ari_model_validators.c<br>M res/ari/ari_model_validators.h<br>M rest-api/api-docs/channels.json<br>M tests/test_stasis_channels.c<br>8 files changed, 42 insertions(+), 5 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/67/18567/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/channels/chan_pjsip.c b/channels/chan_pjsip.c</span><br><span>index 97ca12a..b717104 100644</span><br><span>--- a/channels/chan_pjsip.c</span><br><span>+++ b/channels/chan_pjsip.c</span><br><span>@@ -1258,7 +1258,7 @@</span><br><span> struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);</span><br><span> char *uniqueid = ast_threadstorage_get(&uniqueid_threadbuf, UNIQUEID_BUFSIZE);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!uniqueid) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!channel || !uniqueid) {</span><br><span> return "";</span><br><span> }</span><br><span> </span><br><span>diff --git a/doc/CHANGES-staging/ari_add_pvt_id_to_channel_resource.txt b/doc/CHANGES-staging/ari_add_pvt_id_to_channel_resource.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..a4f008f</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/ari_add_pvt_id_to_channel_resource.txt</span><br><span>@@ -0,0 +1,7 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: ari</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: stasis_channels</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Expose channel driver's unique id (which is the Call-ID for SIP/PJSIP)</span><br><span style="color: hsl(120, 100%, 40%);">+to ARI channel resources as 'protocol_id'.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ASTERISK-30027</span><br><span>diff --git a/include/asterisk/stasis_channels.h b/include/asterisk/stasis_channels.h</span><br><span>index 0f28abc..8a6a1c0 100644</span><br><span>--- a/include/asterisk/stasis_channels.h</span><br><span>+++ b/include/asterisk/stasis_channels.h</span><br><span>@@ -73,6 +73,8 @@</span><br><span> struct varshead *manager_vars; /*!< Variables to be appended to manager events */</span><br><span> int tech_properties; /*!< Properties of the channel's technology */</span><br><span> struct varshead *ari_vars; /*!< Variables to be appended to ARI events */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STRING_FIELD_EXTENDED(protocol_id); /*!< Channel driver protocol id (i.e. Call-ID for chan_sip/chan_pjsip) */</span><br><span> };</span><br><span> </span><br><span> /*!</span><br><span>diff --git a/main/stasis_channels.c b/main/stasis_channels.c</span><br><span>index 934de2b..a70f4b6 100644</span><br><span>--- a/main/stasis_channels.c</span><br><span>+++ b/main/stasis_channels.c</span><br><span>@@ -240,7 +240,7 @@</span><br><span> </span><br><span> snapshot = ao2_alloc_options(sizeof(*snapshot), channel_snapshot_dtor,</span><br><span> AO2_ALLOC_OPT_LOCK_NOLOCK);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!snapshot || ast_string_field_init(snapshot, 1024)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!snapshot || ast_string_field_init(snapshot, 1024) || ast_string_field_init_extended(snapshot, protocol_id)) {</span><br><span> ao2_cleanup(snapshot);</span><br><span> return NULL;</span><br><span> }</span><br><span>@@ -306,6 +306,10 @@</span><br><span> snapshot->ari_vars = ast_channel_get_ari_vars(chan);</span><br><span> snapshot->tech_properties = ast_channel_tech(chan)->properties;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_channel_tech(chan)->get_pvt_uniqueid) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_string_field_set(snapshot, protocol_id, ast_channel_tech(chan)->get_pvt_uniqueid(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> return snapshot;</span><br><span> }</span><br><span> </span><br><span>@@ -975,14 +979,15 @@</span><br><span> }</span><br><span> </span><br><span> json_chan = ast_json_pack(</span><br><span style="color: hsl(0, 100%, 40%);">- /* Broken up into groups of three for readability */</span><br><span style="color: hsl(0, 100%, 40%);">- "{ s: s, s: s, s: s,"</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Broken up into groups for readability */</span><br><span style="color: hsl(120, 100%, 40%);">+ "{ s: s, s: s, s: s, s: s,"</span><br><span> " s: o, s: o, s: s,"</span><br><span> " s: o, s: o, s: s }",</span><br><span> /* First line */</span><br><span> "id", snapshot->uniqueid,</span><br><span> "name", snapshot->name,</span><br><span> "state", ast_state2str(snapshot->state),</span><br><span style="color: hsl(120, 100%, 40%);">+ "protocol_id", snapshot->protocol_id,</span><br><span> /* Second line */</span><br><span> "caller", ast_json_name_number(</span><br><span> snapshot->caller_name, snapshot->caller_number),</span><br><span>diff --git a/res/ari/ari_model_validators.c b/res/ari/ari_model_validators.c</span><br><span>index 0bbbb19..fffb87a 100644</span><br><span>--- a/res/ari/ari_model_validators.c</span><br><span>+++ b/res/ari/ari_model_validators.c</span><br><span>@@ -1043,6 +1043,7 @@</span><br><span> int has_id = 0;</span><br><span> int has_language = 0;</span><br><span> int has_name = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int has_protocol_id = 0;</span><br><span> int has_state = 0;</span><br><span> </span><br><span> for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {</span><br><span>@@ -1135,6 +1136,16 @@</span><br><span> res = 0;</span><br><span> }</span><br><span> } else</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strcmp("protocol_id", ast_json_object_iter_key(iter)) == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int prop_is_valid;</span><br><span style="color: hsl(120, 100%, 40%);">+ has_protocol_id = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ prop_is_valid = ast_ari_validate_string(</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_json_object_iter_value(iter));</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!prop_is_valid) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "ARI Channel field protocol_id failed validation\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else</span><br><span> if (strcmp("state", ast_json_object_iter_key(iter)) == 0) {</span><br><span> int prop_is_valid;</span><br><span> has_state = 1;</span><br><span>@@ -1193,6 +1204,11 @@</span><br><span> res = 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (!has_protocol_id) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "ARI Channel missing required field protocol_id\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (!has_state) {</span><br><span> ast_log(LOG_ERROR, "ARI Channel missing required field state\n");</span><br><span> res = 0;</span><br><span>diff --git a/res/ari/ari_model_validators.h b/res/ari/ari_model_validators.h</span><br><span>index 583cba3..64f167c 100644</span><br><span>--- a/res/ari/ari_model_validators.h</span><br><span>+++ b/res/ari/ari_model_validators.h</span><br><span>@@ -1353,6 +1353,7 @@</span><br><span> * - id: string (required)</span><br><span> * - language: string (required)</span><br><span> * - name: string (required)</span><br><span style="color: hsl(120, 100%, 40%);">+ * - protocol_id: string (required)</span><br><span> * - state: string (required)</span><br><span> * Dialed</span><br><span> * DialplanCEP</span><br><span>diff --git a/rest-api/api-docs/channels.json b/rest-api/api-docs/channels.json</span><br><span>index 20dd4e9..fc8ba76 100644</span><br><span>--- a/rest-api/api-docs/channels.json</span><br><span>+++ b/rest-api/api-docs/channels.json</span><br><span>@@ -2109,6 +2109,11 @@</span><br><span> "type": "string",</span><br><span> "description": "Unique identifier of the channel.\n\nThis is the same as the Uniqueid field in AMI."</span><br><span> },</span><br><span style="color: hsl(120, 100%, 40%);">+ "protocol_id": {</span><br><span style="color: hsl(120, 100%, 40%);">+ "required": true,</span><br><span style="color: hsl(120, 100%, 40%);">+ "type": "string",</span><br><span style="color: hsl(120, 100%, 40%);">+ "description": "Protocol id from underlying channel driver (i.e. Call-ID for chan_sip/chan_pjsip; will be empty if not applicable or not implemented by driver)."</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span> "name": {</span><br><span> "required": true,</span><br><span> "type": "string",</span><br><span>diff --git a/tests/test_stasis_channels.c b/tests/test_stasis_channels.c</span><br><span>index c483731..0ba8ff4 100644</span><br><span>--- a/tests/test_stasis_channels.c</span><br><span>+++ b/tests/test_stasis_channels.c</span><br><span>@@ -273,7 +273,7 @@</span><br><span> ast_test_validate(test, NULL != snapshot);</span><br><span> </span><br><span> actual = ast_channel_snapshot_to_json(snapshot, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">- expected = ast_json_pack("{ s: s, s: s, s: s, s: s,"</span><br><span style="color: hsl(120, 100%, 40%);">+ expected = ast_json_pack("{ s: s, s: s, s: s, s: s, s: s,"</span><br><span> " s: { s: s, s: s, s: i, s: s, s: s },"</span><br><span> " s: { s: s, s: s },"</span><br><span> " s: { s: s, s: s },"</span><br><span>@@ -284,6 +284,7 @@</span><br><span> "state", "Down",</span><br><span> "accountcode", "acctcode",</span><br><span> "id", ast_channel_uniqueid(chan),</span><br><span style="color: hsl(120, 100%, 40%);">+ "protocol_id", "",</span><br><span> "dialplan",</span><br><span> "context", "context",</span><br><span> "exten", "exten",</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18567">change 18567</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/+/18567"/><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: I7cc6e7a9d29efe74bc27811d788dac20fe559b87 </div>
<div style="display:none"> Gerrit-Change-Number: 18567 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Moritz Fain <moritz@fain.io> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>