]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
ari: expose channel driver's unique id to ARI channel resource
authorMoritz Fain <moritz@fain.io>
Mon, 25 Apr 2022 22:40:49 +0000 (00:40 +0200)
committerJoshua Colp <jcolp@sangoma.com>
Sun, 22 May 2022 20:40:43 +0000 (15:40 -0500)
This change exposes the channel driver's unique id (i.e. the Call-ID
for chan_sip/chan_pjsip based channels) to ARI channel resources
as `protocol_id`.

ASTERISK-30027
Reported by: Moritz Fain
Tested by: Moritz Fain

Change-Id: I7cc6e7a9d29efe74bc27811d788dac20fe559b87

channels/chan_pjsip.c
doc/CHANGES-staging/ari_add_pvt_id_to_channel_resource.txt [new file with mode: 0644]
include/asterisk/stasis_channels.h
main/stasis_channels.c
res/ari/ari_model_validators.c
res/ari/ari_model_validators.h
rest-api/api-docs/channels.json
tests/test_stasis_channels.c

index 97ca12acb0d518504d771dbaaad33d987599db1d..b71710413aaf1db41d1dce0205ef57a1def36663 100644 (file)
@@ -1258,7 +1258,7 @@ static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
        struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
        char *uniqueid = ast_threadstorage_get(&uniqueid_threadbuf, UNIQUEID_BUFSIZE);
 
-       if (!uniqueid) {
+       if (!channel || !uniqueid) {
                return "";
        }
 
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
new file mode 100644 (file)
index 0000000..a4f008f
--- /dev/null
@@ -0,0 +1,7 @@
+Subject: ari
+Subject: stasis_channels
+
+Expose channel driver's unique id (which is the Call-ID for SIP/PJSIP)
+to ARI channel resources as 'protocol_id'.
+
+ASTERISK-30027
index 0f28abcff8255ba6225ed186b3126e74a66dbeb4..8a6a1c06a1080c877c9c9925c34959dbe404a7ee 100644 (file)
@@ -73,6 +73,8 @@ struct ast_channel_snapshot {
        struct varshead *manager_vars;          /*!< Variables to be appended to manager events */
        int tech_properties;                    /*!< Properties of the channel's technology */
        struct varshead *ari_vars;              /*!< Variables to be appended to ARI events */
+
+       AST_STRING_FIELD_EXTENDED(protocol_id); /*!< Channel driver protocol id (i.e. Call-ID for chan_sip/chan_pjsip) */
 };
 
 /*!
index 934de2becf6687706f6cfbd981b9784ed8210067..a70f4b60bb7c9a3b137b0e054060229869d69d01 100644 (file)
@@ -240,7 +240,7 @@ struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *cha
 
        snapshot = ao2_alloc_options(sizeof(*snapshot), channel_snapshot_dtor,
                AO2_ALLOC_OPT_LOCK_NOLOCK);
-       if (!snapshot || ast_string_field_init(snapshot, 1024)) {
+       if (!snapshot || ast_string_field_init(snapshot, 1024) || ast_string_field_init_extended(snapshot, protocol_id)) {
                ao2_cleanup(snapshot);
                return NULL;
        }
@@ -306,6 +306,10 @@ struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *cha
        snapshot->ari_vars = ast_channel_get_ari_vars(chan);
        snapshot->tech_properties = ast_channel_tech(chan)->properties;
 
+       if (ast_channel_tech(chan)->get_pvt_uniqueid) {
+               ast_string_field_set(snapshot, protocol_id, ast_channel_tech(chan)->get_pvt_uniqueid(chan));
+       }
+
        return snapshot;
 }
 
@@ -975,14 +979,15 @@ struct ast_json *ast_channel_snapshot_to_json(
        }
 
        json_chan = ast_json_pack(
-               /* Broken up into groups of three for readability */
-               "{ s: s, s: s, s: s,"
+               /* Broken up into groups for readability */
+               "{ s: s, s: s, s: s, s: s,"
                "  s: o, s: o, s: s,"
                "  s: o, s: o, s: s }",
                /* First line */
                "id", snapshot->uniqueid,
                "name", snapshot->name,
                "state", ast_state2str(snapshot->state),
+               "protocol_id", snapshot->protocol_id,
                /* Second line */
                "caller", ast_json_name_number(
                        snapshot->caller_name, snapshot->caller_number),
index 0bbbb195e2be4d8212f9d451acb1e0c58a7e0272..fffb87a1624f047c3afe8c71414d44a0c04a73b4 100644 (file)
@@ -1043,6 +1043,7 @@ int ast_ari_validate_channel(struct ast_json *json)
        int has_id = 0;
        int has_language = 0;
        int has_name = 0;
+       int has_protocol_id = 0;
        int has_state = 0;
 
        for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
@@ -1135,6 +1136,16 @@ int ast_ari_validate_channel(struct ast_json *json)
                                res = 0;
                        }
                } else
+               if (strcmp("protocol_id", ast_json_object_iter_key(iter)) == 0) {
+                       int prop_is_valid;
+                       has_protocol_id = 1;
+                       prop_is_valid = ast_ari_validate_string(
+                               ast_json_object_iter_value(iter));
+                       if (!prop_is_valid) {
+                               ast_log(LOG_ERROR, "ARI Channel field protocol_id failed validation\n");
+                               res = 0;
+                       }
+               } else
                if (strcmp("state", ast_json_object_iter_key(iter)) == 0) {
                        int prop_is_valid;
                        has_state = 1;
@@ -1193,6 +1204,11 @@ int ast_ari_validate_channel(struct ast_json *json)
                res = 0;
        }
 
+       if (!has_protocol_id) {
+               ast_log(LOG_ERROR, "ARI Channel missing required field protocol_id\n");
+               res = 0;
+       }
+
        if (!has_state) {
                ast_log(LOG_ERROR, "ARI Channel missing required field state\n");
                res = 0;
index 583cba33a4e7cfb34e0c5a708a32c491c86eb0af..64f167c07cdc1dd20994636980ba16fd055c2a36 100644 (file)
@@ -1353,6 +1353,7 @@ ari_validator ast_ari_validate_application_fn(void);
  * - id: string (required)
  * - language: string (required)
  * - name: string (required)
+ * - protocol_id: string (required)
  * - state: string (required)
  * Dialed
  * DialplanCEP
index 20dd4e9a88ecc818d3aa2fd86a582fd396a456b0..fc8ba76118c62d4f1578d6a690bb5c880250212e 100644 (file)
                                        "type": "string",
                                        "description": "Unique identifier of the channel.\n\nThis is the same as the Uniqueid field in AMI."
                                },
+                               "protocol_id": {
+                                       "required": true,
+                                       "type": "string",
+                                       "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)."
+                               },
                                "name": {
                                        "required": true,
                                        "type": "string",
index c483731ee6cd4230d4da2cab7f5d1d6dc3e04193..0ba8ff4ccedcdcc2a614f039e654232378c26cf8 100644 (file)
@@ -273,7 +273,7 @@ AST_TEST_DEFINE(channel_snapshot_json)
        ast_test_validate(test, NULL != snapshot);
 
        actual = ast_channel_snapshot_to_json(snapshot, NULL);
-       expected = ast_json_pack("{ s: s, s: s, s: s, s: s,"
+       expected = ast_json_pack("{ s: s, s: s, s: s, s: s, s: s,"
                                 "  s: { s: s, s: s, s: i, s: s, s: s },"
                                 "  s: { s: s, s: s },"
                                 "  s: { s: s, s: s },"
@@ -284,6 +284,7 @@ AST_TEST_DEFINE(channel_snapshot_json)
                                 "state", "Down",
                                 "accountcode", "acctcode",
                                 "id", ast_channel_uniqueid(chan),
+                                "protocol_id", "",
                                 "dialplan",
                                 "context", "context",
                                 "exten", "exten",