2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2015, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Get information about a PJSIP contact
23 * \author \verbatim Joshua Colp <jcolp@digium.com> \endverbatim
30 <support_level>core</support_level>
31 <depend>pjproject</depend>
32 <depend>res_pjsip</depend>
40 #include "asterisk/app.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/sorcery.h"
44 #include "asterisk/res_pjsip.h"
47 <function name="PJSIP_CONTACT" language="en_US">
49 Get information about a PJSIP contact
52 <parameter name="name" required="true">
53 <para>The name of the contact to query.</para>
55 <parameter name="field" required="true">
56 <para>The configuration option for the contact to query for.
57 Supported options are those fields on the
58 <replaceable>contact</replaceable> object.</para>
61 <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='contact']/configOption)"/>
69 static int contact_function_get_permanent(void *obj, void *arg, int flags)
73 if (!strcmp(ast_sorcery_object_get_id(obj), id)) {
74 return CMP_MATCH | CMP_STOP;
80 static int pjsip_contact_function_read(struct ast_channel *chan,
81 const char *cmd, char *data, struct ast_str **buf, ssize_t len)
83 struct ast_sorcery *pjsip_sorcery;
84 char *parsed_data = ast_strdupa(data);
86 RAII_VAR(struct ast_sip_contact *, contact_obj, NULL, ao2_cleanup);
87 RAII_VAR(struct ast_sip_contact_status *, contact_status, NULL, ao2_cleanup);
90 AST_DECLARE_APP_ARGS(args,
91 AST_APP_ARG(contact_name);
92 AST_APP_ARG(field_name);
95 /* Check for zero arguments */
96 if (ast_strlen_zero(parsed_data)) {
97 ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
101 AST_STANDARD_APP_ARGS(args, parsed_data);
103 if (ast_strlen_zero(args.contact_name)) {
104 ast_log(AST_LOG_ERROR, "Cannot call %s without a contact name to query\n", cmd);
108 if (ast_strlen_zero(args.field_name)) {
109 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
113 pjsip_sorcery = ast_sip_get_sorcery();
114 if (!pjsip_sorcery) {
115 ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
119 /* Determine if this is a permanent contact or a normal contact */
120 if ((contact_name = strstr(args.contact_name, "@@"))) {
121 size_t aor_name_len = contact_name - args.contact_name;
122 char aor_name[aor_name_len + 1];
123 RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
125 /* Grab only the AOR name so we can retrieve the AOR which will give us the contact */
126 strncpy(aor_name, args.contact_name, aor_name_len);
127 aor_name[aor_name_len] = '\0';
129 aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", aor_name);
131 ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
135 contact_obj = ao2_callback(aor_obj->permanent_contacts, 0, contact_function_get_permanent, args.contact_name);
137 contact_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "contact", args.contact_name);
141 ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
145 contact_status = ast_sorcery_retrieve_by_id(pjsip_sorcery, CONTACT_STATUS, ast_sorcery_object_get_id(contact_obj));
147 if (!strcmp(args.field_name, "status")) {
148 ast_str_set(buf, len, "%s", ast_sip_get_contact_status_label(contact_status->status));
149 } else if (!strcmp(args.field_name, "rtt")) {
150 if (contact_status->status == UNKNOWN) {
151 ast_str_set(buf, len, "%s", "N/A");
153 ast_str_set(buf, len, "%" PRId64, contact_status->rtt);
156 struct ast_variable *change_set;
157 struct ast_variable *it_change_set;
159 change_set = ast_sorcery_objectset_create(pjsip_sorcery, contact_obj);
162 ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s': change set is NULL\n", args.contact_name);
166 for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
167 if (!strcmp(it_change_set->name, args.field_name)) {
168 ast_str_set(buf, len, "%s", it_change_set->value);
173 if (!it_change_set) {
174 ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP contact\n", args.field_name);
179 ast_variables_destroy(change_set);
186 static struct ast_custom_function pjsip_contact_function = {
187 .name = "PJSIP_CONTACT",
188 .read2 = pjsip_contact_function_read,
191 static int unload_module(void)
193 return ast_custom_function_unregister(&pjsip_contact_function);
196 static int load_module(void)
198 return ast_custom_function_register(&pjsip_contact_function);
201 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP contact");