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

This commit is contained in:
Joshua C. Colp
2019-01-14 08:38:14 -06:00
committed by Gerrit Code Review
7 changed files with 140 additions and 1 deletions

View File

@@ -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>

View File

@@ -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 @@ struct global_config {
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 @@ unsigned int ast_sip_get_use_callerid_contact(void)
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 @@ int ast_sip_initialize_sorcery_global(void)
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;

View File

@@ -2186,6 +2186,37 @@ static void contact_observer_created(const void *obj)
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 @@ static void contact_observer_updated(const void *obj)
}
}
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);
}
}
/*!