[Asterisk-code-review] res pjsip: add option to enable ContactStatus event when con... (asterisk[master])

Joshua C. Colp asteriskteam at digium.com
Mon Jan 14 08:38:15 CST 2019


Joshua C. Colp has submitted this change and it was merged. ( https://gerrit.asterisk.org/10870 )

Change subject: res_pjsip: add option to enable ContactStatus event when contact is updated
......................................................................

res_pjsip: add option to enable ContactStatus event when contact is updated

The commit I2f97ebfa79969a36a97bb7b9afd5b6268cf1a07d removed sending out
the ContactStatus AMI event when a contact is updated.
Thist change broke things which rely on old behavior.

This patch adds a new PJSIP global configuration option
'send_contact_status_on_update_registration' to be able to preserve old
ContactStatus behavior.
By default new behavior, i.e. the ContactStatus event will not be sent when a
device refreshes its registration.

Change-Id: I706adf7584e7077eb6bde6d9799ca408bc82ce46
---
M CHANGES
M configs/samples/pjsip.conf.sample
A contrib/ast-db-manage/config/versions/0838f8db6a61_pjsip_add_send_contact_status_on_update_.py
M include/asterisk/res_pjsip.h
M res/res_pjsip.c
M res/res_pjsip/config_global.c
M res/res_pjsip/pjsip_options.c
7 files changed, 140 insertions(+), 1 deletion(-)

Approvals:
  Joshua C. Colp: Looks good to me, but someone else must approve; Approved for Submit
  George Joseph: Looks good to me, approved



diff --git a/CHANGES b/CHANGES
index c9b8f5f..3459b47 100644
--- a/CHANGES
+++ b/CHANGES
@@ -71,6 +71,15 @@
    ast_bridge_snapshot_update structure as it's data.  It contains
    the last snapshot and the new one.
 
+------------------------------------------------------------------------------
+--- Functionality changes from Asterisk 16.1.0 to Asterisk 16.2.0 ------------
+------------------------------------------------------------------------------
+
+res_pjsip
+------------------
+ * Added "send_contact_status_on_update_registration" global configuration option
+   to enable sending AMI ContactStatus event when a device refreshes its registration.
+
 Features
 ------------------
  * Before Asterisk 12, when using the automon or automixmon features defined
diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample
index 4750260..0ed01f0 100644
--- a/configs/samples/pjsip.conf.sample
+++ b/configs/samples/pjsip.conf.sample
@@ -1145,6 +1145,11 @@
                     ; from incoming SIP URI user fields are always truncated at the
                     ; first semicolon.
 
+;send_contact_status_on_update_registration=no ; Enable sending AMI ContactStatus
+                    ; event when a device refreshes its registration
+                    ; (default: "no")
+
+
 ; MODULE PROVIDING BELOW SECTION(S): res_pjsip_acl
 ;==========================ACL SECTION OPTIONS=========================
 ;[acl]
diff --git a/contrib/ast-db-manage/config/versions/0838f8db6a61_pjsip_add_send_contact_status_on_update_.py b/contrib/ast-db-manage/config/versions/0838f8db6a61_pjsip_add_send_contact_status_on_update_.py
new file mode 100644
index 0000000..2d31291
--- /dev/null
+++ b/contrib/ast-db-manage/config/versions/0838f8db6a61_pjsip_add_send_contact_status_on_update_.py
@@ -0,0 +1,39 @@
+"""pjsip add send_contact_status_on_update_registration
+
+Revision ID: 0838f8db6a61
+Revises: 1ac563b350a8
+Create Date: 2018-12-18 14:45:07.811415
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '0838f8db6a61'
+down_revision = '1ac563b350a8'
+
+from alembic import op
+import sqlalchemy as sa
+from sqlalchemy.dialects.postgresql import ENUM
+
+AST_BOOL_NAME = 'ast_bool_values'
+# We'll just ignore the n/y and f/t abbreviations as Asterisk does not write
+# those aliases.
+AST_BOOL_VALUES = [ '0', '1',
+                    'off', 'on',
+                    'false', 'true',
+                    'no', 'yes' ]
+
+
+def upgrade():
+    ############################# Enums ##############################
+
+    # ast_bool_values has already been created, so use postgres enum object
+    # type to get around "already created" issue - works okay with mysql
+    ast_bool_values = ENUM(*AST_BOOL_VALUES, name=AST_BOOL_NAME, create_type=False)
+
+    op.add_column('ps_globals', sa.Column('send_contact_status_on_update_registration', ast_bool_values))
+
+
+def downgrade():
+    if op.get_context().bind.dialect.name == 'mssql':
+        op.drop_constraint('ck_ps_globals_send_contact_status_on_update_registration_ast_bool_values', 'ps_globals')
+    op.drop_column('ps_globals', 'send_contact_status_on_update_registration')
diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index fc4f809..eabedee 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -2795,6 +2795,15 @@
 unsigned int ast_sip_get_ignore_uri_user_options(void);
 
 /*!
+ * \brief Retrieve the global setting 'send_contact_status_on_update_registration'.
+ * \since 16.2.0
+ *
+ * \retval non zero if need to send AMI ContactStatus event when a contact is updated.
+ */
+unsigned int ast_sip_get_send_contact_status_on_update_registration(void);
+
+
+/*!
  * \brief Truncate the URI user field options string if enabled.
  * \since 13.12.0
  *
diff --git a/res/res_pjsip.c b/res/res_pjsip.c
index 96b7d72..4f18010 100644
--- a/res/res_pjsip.c
+++ b/res/res_pjsip.c
@@ -1905,6 +1905,9 @@
 						generated Contact headers.</para>
 					</description>
 				</configOption>
+				<configOption name="send_contact_status_on_update_registration" default="no">
+					<synopsis>Enable sending AMI ContactStatus event when a device refreshes its registration.</synopsis>
+				</configOption>
 			</configObject>
 		</configFile>
 	</configInfo>
diff --git a/res/res_pjsip/config_global.c b/res/res_pjsip/config_global.c
index a02cacd..61a21c2 100644
--- a/res/res_pjsip/config_global.c
+++ b/res/res_pjsip/config_global.c
@@ -49,6 +49,7 @@
 #define DEFAULT_MWI_DISABLE_INITIAL_UNSOLICITED 0
 #define DEFAULT_IGNORE_URI_USER_OPTIONS 0
 #define DEFAULT_USE_CALLERID_CONTACT 0
+#define DEFAULT_SEND_CONTACT_STATUS_ON_UPDATE_REGISTRATION 0
 
 /*!
  * \brief Cached global config object
@@ -106,6 +107,8 @@
 	unsigned int ignore_uri_user_options;
 	/*! Nonzero if CALLERID(num) is to be used as the default contact username instead of default_from_user */
 	unsigned int use_callerid_contact;
+	/*! Nonzero if need to send AMI ContactStatus event when a contact is updated */
+	unsigned int send_contact_status_on_update_registration;
 };
 
 static void global_destructor(void *obj)
@@ -420,6 +423,21 @@
 	return use_callerid_contact;
 }
 
+unsigned int ast_sip_get_send_contact_status_on_update_registration(void)
+{
+	unsigned int send_contact_status_on_update_registration;
+	struct global_config *cfg;
+
+	cfg = get_global_cfg();
+	if (!cfg) {
+		return DEFAULT_SEND_CONTACT_STATUS_ON_UPDATE_REGISTRATION;
+	}
+
+	send_contact_status_on_update_registration = cfg->send_contact_status_on_update_registration;
+	ao2_ref(cfg, -1);
+	return send_contact_status_on_update_registration;
+}
+
 /*!
  * \internal
  * \brief Observer to set default global object if none exist.
@@ -574,6 +592,9 @@
 	ast_sorcery_object_field_register(sorcery, "global", "use_callerid_contact",
 		DEFAULT_USE_CALLERID_CONTACT ? "yes" : "no",
 		OPT_YESNO_T, 1, FLDSET(struct global_config, use_callerid_contact));
+	ast_sorcery_object_field_register(sorcery, "global", "send_contact_status_on_update_registration",
+		DEFAULT_SEND_CONTACT_STATUS_ON_UPDATE_REGISTRATION ? "yes" : "no",
+		OPT_YESNO_T, 1, FLDSET(struct global_config, send_contact_status_on_update_registration));
 
 	if (ast_sorcery_instance_observer_add(sorcery, &observer_callbacks_global)) {
 		return -1;
diff --git a/res/res_pjsip/pjsip_options.c b/res/res_pjsip/pjsip_options.c
index f2439c4..a7aefaa 100644
--- a/res/res_pjsip/pjsip_options.c
+++ b/res/res_pjsip/pjsip_options.c
@@ -2186,6 +2186,37 @@
 		sip_options_contact_add_management_task, (void *) obj);
 }
 
+/*!
+ * \brief Task which updates a dynamic contact to an AOR
+ * \note Run by aor_options->serializer
+ */
+static int sip_options_contact_update_task(void *obj)
+{
+	struct sip_options_contact_observer_task_data *task_data = obj;
+	struct ast_sip_contact_status *contact_status;
+
+	contact_status = ast_sip_get_contact_status(task_data->contact);
+	if (contact_status) {
+		switch (contact_status->status) {
+		case CREATED:
+		case UNAVAILABLE:
+		case AVAILABLE:
+		case UNKNOWN:
+			/* Refresh the ContactStatus AMI events. */
+			sip_options_contact_status_update(contact_status);
+			break;
+		case REMOVED:
+			break;
+		}
+		ao2_ref(contact_status, -1);
+	}
+
+	ao2_ref(task_data->contact, -1);
+	ao2_ref(task_data->aor_options, -1);
+	ast_free(task_data);
+	return 0;
+}
+
 /*! \brief Observer callback invoked on contact update */
 static void contact_observer_updated(const void *obj)
 {
@@ -2206,7 +2237,29 @@
 		}
 	}
 
-	ao2_cleanup(aor_options);
+	if (aor_options && ast_sip_get_send_contact_status_on_update_registration()) {
+		struct sip_options_contact_observer_task_data *task_data;
+
+		task_data = ast_malloc(sizeof(*task_data));
+		if (!task_data) {
+			ao2_ref(aor_options, -1);
+			return;
+		}
+
+		task_data->contact = (struct ast_sip_contact *) contact;
+		/* task_data takes ownership of aor_options and will take care of releasing the ref */
+		task_data->aor_options = aor_options;
+
+		ao2_ref(task_data->contact, +1);
+		if (ast_sip_push_task(task_data->aor_options->serializer,
+			sip_options_contact_update_task, task_data)) {
+			ao2_ref(task_data->contact, -1);
+			ao2_ref(task_data->aor_options, -1);
+			ast_free(task_data);
+		}
+	} else {
+		ao2_cleanup(aor_options);
+	}
 }
 
 /*!

-- 
To view, visit https://gerrit.asterisk.org/10870
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I706adf7584e7077eb6bde6d9799ca408bc82ce46
Gerrit-Change-Number: 10870
Gerrit-PatchSet: 1
Gerrit-Owner: Alexei Gradinari <alex2grad at gmail.com>
Gerrit-Reviewer: Friendly Automation (1000185)
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua C. Colp <jcolp at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190114/77e5ebf7/attachment-0001.html>


More information about the asterisk-code-review mailing list