func_pjsip_endpoint.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2013, Digium, Inc.
00005  *
00006  * Matt Jordan <mjordan@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Get information about a PJSIP endpoint
00022  *
00023  * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
00024  *
00025  * \ingroup functions
00026  *
00027  */
00028 
00029 /*** MODULEINFO
00030    <support_level>core</support_level>
00031    <depend>pjproject</depend>
00032    <depend>res_pjsip</depend>
00033  ***/
00034 
00035 #include "asterisk.h"
00036 
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 403617 $")
00038 
00039 #include <pjsip.h>
00040 #include <pjlib.h>
00041 
00042 #include "asterisk/app.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/module.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/sorcery.h"
00047 #include "asterisk/res_pjsip.h"
00048 
00049 /*** DOCUMENTATION
00050    <function name="PJSIP_ENDPOINT" language="en_US">
00051       <synopsis>
00052          Get information about a PJSIP endpoint
00053       </synopsis>
00054       <syntax>
00055          <parameter name="name" required="true">
00056             <para>The name of the endpoint to query.</para>
00057          </parameter>
00058          <parameter name="field" required="true">
00059             <para>The configuration option for the endpoint to query for.
00060             Supported options are those fields on the
00061             <replaceable>endpoint</replaceable> object in
00062             <filename>pjsip.conf</filename>.</para>
00063             <enumlist>
00064                <configOptionToEnum>
00065                   <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='endpoint']/configOption)"/>
00066                </configOptionToEnum>
00067             </enumlist>
00068          </parameter>
00069       </syntax>
00070    </function>
00071 ***/
00072 
00073 static int pjsip_endpoint_function_read(struct ast_channel *chan,
00074    const char *cmd, char *data, struct ast_str **buf, ssize_t len)
00075 {
00076    struct ast_sorcery *pjsip_sorcery;
00077    char *parsed_data = ast_strdupa(data);
00078    RAII_VAR(void *, endpoint_obj, NULL, ao2_cleanup);
00079    struct ast_variable *change_set;
00080    struct ast_variable *it_change_set;
00081    int res;
00082 
00083    AST_DECLARE_APP_ARGS(args,
00084       AST_APP_ARG(endpoint_name);
00085       AST_APP_ARG(field_name);
00086    );
00087 
00088    /* Check for zero arguments */
00089    if (ast_strlen_zero(parsed_data)) {
00090       ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
00091       return -1;
00092    }
00093 
00094    AST_STANDARD_APP_ARGS(args, parsed_data);
00095 
00096    if (ast_strlen_zero(args.endpoint_name)) {
00097       ast_log(AST_LOG_ERROR, "Cannot call %s without an endpoint name to query\n", cmd);
00098       return -1;
00099    }
00100 
00101    if (ast_strlen_zero(args.field_name)) {
00102       ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
00103       return -1;
00104    }
00105 
00106    pjsip_sorcery = ast_sip_get_sorcery();
00107    if (!pjsip_sorcery) {
00108       ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
00109       return -1;
00110    }
00111 
00112    endpoint_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "endpoint", args.endpoint_name);
00113    if (!endpoint_obj) {
00114       ast_log(AST_LOG_WARNING, "Failed to retrieve information for endpoint '%s'\n", args.endpoint_name);
00115       return -1;
00116    }
00117 
00118    change_set = ast_sorcery_objectset_create(pjsip_sorcery, endpoint_obj);
00119    if (!change_set) {
00120       ast_log(AST_LOG_WARNING, "Failed to retrieve information for endpoint '%s': change set is NULL\n", args.endpoint_name);
00121       return -1;
00122    }
00123 
00124    for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
00125       if (!strcmp(it_change_set->name, args.field_name)) {
00126          if (!strcmp(it_change_set->name, "disallow")) {
00127             ast_str_set(buf, len, "!%s", it_change_set->value);
00128          } else {
00129             ast_str_set(buf, len, "%s", it_change_set->value);
00130          }
00131          break;
00132       }
00133    }
00134 
00135    res = it_change_set ? 0 : 1;
00136    if (res) {
00137       ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP endpoint\n", args.field_name);
00138    }
00139 
00140    ast_variables_destroy(change_set);
00141 
00142    return res;
00143 }
00144 
00145 
00146 static struct ast_custom_function pjsip_endpoint_function = {
00147    .name = "PJSIP_ENDPOINT",
00148    .read2 = pjsip_endpoint_function_read,
00149 };
00150 
00151 static int unload_module(void)
00152 {
00153    return ast_custom_function_unregister(&pjsip_endpoint_function);
00154 }
00155 
00156 static int load_module(void)
00157 {
00158    return ast_custom_function_register(&pjsip_endpoint_function);
00159 }
00160 
00161 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP endpoint");

Generated on Thu Apr 16 06:27:36 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6