Wed Jul 14 06:25:48 2010

Asterisk developer's documentation


func_connectedline.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007, Gareth Palmer
00005  *
00006  * Gareth Palmer <gareth@acsdata.co.nz>
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 Connected Line dialplan function
00022  *
00023  * \ingroup functions
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 263541 $")
00029 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <sys/types.h>
00034 
00035 #include "asterisk/module.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/utils.h"
00040 #include "asterisk/app.h"
00041 #include "asterisk/options.h"
00042 #include "asterisk/callerid.h"
00043 
00044 /*
00045  * Do not document the CONNECTEDLINE(source) datatype.
00046  * It has turned out to not be needed.  The source value is really
00047  * only useful as a possible tracing aid.
00048  */
00049 /*** DOCUMENTATION
00050    <function name="CONNECTEDLINE" language="en_US">
00051       <synopsis>
00052          Gets or sets Connected Line data on the channel.
00053       </synopsis>
00054       <syntax>
00055          <parameter name="datatype" required="true">
00056             <para>The allowable datatypes are:</para>
00057             <enumlist>
00058                <enum name = "all" />
00059                <enum name = "num" />
00060                <enum name = "name" />
00061                <enum name = "tag" />
00062                <enum name = "ton" />
00063                <enum name = "pres" />
00064                <enum name = "subaddr[-valid]|[-type]|[-odd]">
00065                   <para>ISDN Connected line subaddress</para>
00066                </enum>
00067             </enumlist>
00068          </parameter>
00069          <parameter name="i">
00070             <para>If set, this will prevent the channel from sending out protocol
00071             messages because of the value being set</para>
00072          </parameter>
00073       </syntax>
00074       <description>
00075          <para>Gets or sets Connected Line data on the channel.</para>
00076       </description>
00077    </function>
00078  ***/
00079 
00080 static int connectedline_read(struct ast_channel *chan, const char *cmd, char *data,
00081                char *buf, size_t len)
00082 {
00083    /* Ensure that the buffer is empty */
00084    *buf = 0;
00085 
00086    if (!chan)
00087       return -1;
00088 
00089    ast_channel_lock(chan);
00090 
00091    if (!strncasecmp("all", data, 3)) {
00092       snprintf(buf, len, "\"%s\" <%s>",
00093           S_OR(chan->connected.id.name, ""),
00094           S_OR(chan->connected.id.number, ""));
00095    } else if (!strncasecmp("name", data, 4)) {
00096       if (chan->connected.id.name) {
00097          ast_copy_string(buf, chan->connected.id.name, len);
00098       }
00099    } else if (!strncasecmp("num", data, 3)) {
00100       if (chan->connected.id.number) {
00101          ast_copy_string(buf, chan->connected.id.number, len);
00102       }
00103    } else if (!strncasecmp("tag", data, 3)) {
00104       if (chan->connected.id.tag) {
00105          ast_copy_string(buf, chan->connected.id.tag, len);
00106       }
00107    } else if (!strncasecmp("ton", data, 3)) {
00108       snprintf(buf, len, "%d", chan->connected.id.number_type);
00109    } else if (!strncasecmp("pres", data, 4)) {
00110       ast_copy_string(buf, ast_named_caller_presentation(chan->connected.id.number_presentation), len);
00111    } else if (!strncasecmp("source", data, 6)) {
00112       ast_copy_string(buf, ast_connected_line_source_name(chan->connected.source), len);
00113    } else if (!strncasecmp("subaddr", data, 7)) {
00114       /* also matches subaddr-valid, subaddr-type, subaddr-odd, subaddr */
00115       if (!strncasecmp(data + 7 ,"-valid", 6)) {      /* subaddr-valid */
00116          snprintf(buf, len, "%d", chan->connected.id.subaddress.valid);
00117       } else if (!strncasecmp(data + 7 ,"-type", 5)) {   /* subaddr-type */
00118          snprintf(buf, len, "%d", chan->connected.id.subaddress.type);
00119       } else if (!strncasecmp(data + 7 ,"-odd", 4)) {    /* subaddr-odd */
00120          snprintf(buf, len, "%d", chan->connected.id.subaddress.odd_even_indicator);
00121       } else {                /* subaddr */
00122          if (chan->connected.id.subaddress.str) {
00123             ast_copy_string(buf, chan->connected.id.subaddress.str, len);
00124          }
00125       }
00126    } else {
00127       ast_log(LOG_ERROR, "Unknown connectedline data type '%s'.\n", data);
00128    }
00129 
00130    ast_channel_unlock(chan);
00131 
00132    return 0;
00133 }
00134 
00135 static int connectedline_write(struct ast_channel *chan, const char *cmd, char *data,
00136                 const char *value)
00137 {
00138    struct ast_party_connected_line connected;
00139    char *val;
00140    char *option;
00141    void (*set_it)(struct ast_channel *chan, const struct ast_party_connected_line *connected);
00142 
00143    if (!value || !chan) {
00144       return -1;
00145    }
00146 
00147    /* Determine if the update indication inhibit option is present */
00148    option = strchr(data, ',');
00149    if (option) {
00150       option = ast_skip_blanks(option + 1);
00151       switch (*option) {
00152       case 'i':
00153          set_it = ast_channel_set_connected_line;
00154          break;
00155 
00156       default:
00157          ast_log(LOG_ERROR, "Unknown connectedline option '%s'.\n", option);
00158          return 0;
00159       }
00160    }
00161    else {
00162       set_it = ast_channel_update_connected_line;
00163    }
00164 
00165    ast_channel_lock(chan);
00166    ast_party_connected_line_set_init(&connected, &chan->connected);
00167    ast_channel_unlock(chan);
00168 
00169    value = ast_skip_blanks(value);
00170 
00171    if (!strncasecmp("all", data, 3)) {
00172       char name[256];
00173       char num[256];
00174 
00175       ast_callerid_split(value, name, sizeof(name), num, sizeof(num));
00176       connected.id.name = name;
00177       connected.id.number = num;
00178       set_it(chan, &connected);
00179    } else if (!strncasecmp("name", data, 4)) {
00180       connected.id.name = ast_strdupa(value);
00181       ast_trim_blanks(connected.id.name);
00182       set_it(chan, &connected);
00183    } else if (!strncasecmp("num", data, 3)) {
00184       connected.id.number = ast_strdupa(value);
00185       ast_trim_blanks(connected.id.number);
00186       set_it(chan, &connected);
00187    } else if (!strncasecmp("tag", data, 3)) {
00188       connected.id.tag = ast_strdupa(value);
00189       ast_trim_blanks(connected.id.tag);
00190       set_it(chan, &connected);
00191    } else if (!strncasecmp("ton", data, 3)) {
00192       val = ast_strdupa(value);
00193       ast_trim_blanks(val);
00194 
00195       if (('0' <= val[0]) && (val[0] <= '9')) {
00196          connected.id.number_type = atoi(val);
00197          set_it(chan, &connected);
00198       } else {
00199          ast_log(LOG_ERROR, "Unknown connectedline type of number '%s', value unchanged\n", val);
00200       }
00201    } else if (!strncasecmp("pres", data, 4)) {
00202       int pres;
00203 
00204       val = ast_strdupa(value);
00205       ast_trim_blanks(val);
00206 
00207       if (('0' <= val[0]) && (val[0] <= '9')) {
00208          pres = atoi(val);
00209       } else {
00210          pres = ast_parse_caller_presentation(val);
00211       }
00212 
00213       if (pres < 0) {
00214          ast_log(LOG_ERROR, "Unknown connectedline number presentation '%s', value unchanged\n", val);
00215       } else {
00216          connected.id.number_presentation = pres;
00217          set_it(chan, &connected);
00218       }
00219    } else if (!strncasecmp("source", data, 6)) {
00220       int source;
00221 
00222       val = ast_strdupa(value);
00223       ast_trim_blanks(val);
00224 
00225       if (('0' <= val[0]) && (val[0] <= '9')) {
00226          source = atoi(val);
00227       } else {
00228          source = ast_connected_line_source_parse(val);
00229       }
00230 
00231       if (source < 0) {
00232          ast_log(LOG_ERROR, "Unknown connectedline source '%s', value unchanged\n", val);
00233       } else {
00234          connected.source = source;
00235          set_it(chan, &connected);
00236       }
00237    } else if (!strncasecmp("subaddr", data, 7)) { /* outbound: set calling subaddress */
00238       /* also matches subaddr-valid, subaddr-type, subaddr-odd, subaddr */
00239       if (!strncasecmp(data + 7 ,"-valid", 6)) {      /* subaddr-valid */
00240          connected.id.subaddress.valid = atoi(value) ? 1 : 0;
00241       } else if (!strncasecmp(data + 7 ,"-type", 5)) {   /* subaddr-type */
00242          connected.id.subaddress.type = atoi(value) ? 2 : 0;
00243       } else if (!strncasecmp(data + 7 ,"-odd", 4)) {    /* subaddr-odd */
00244          connected.id.subaddress.odd_even_indicator = atoi(value) ? 1 : 0;
00245       } else {                /* subaddr */
00246          connected.id.subaddress.str = ast_strdupa(value);
00247          ast_trim_blanks(connected.id.subaddress.str);
00248       }
00249       set_it(chan, &connected);
00250    } else {
00251       ast_log(LOG_ERROR, "Unknown connectedline data type '%s'.\n", data);
00252    }
00253 
00254    return 0;
00255 }
00256 
00257 static struct ast_custom_function connectedline_function = {
00258    .name = "CONNECTEDLINE",
00259    .read = connectedline_read,
00260    .write = connectedline_write,
00261 };
00262 
00263 static int unload_module(void)
00264 {
00265    return ast_custom_function_unregister(&connectedline_function);
00266 }
00267 
00268 static int load_module(void)
00269 {
00270    return ast_custom_function_register(&connectedline_function)
00271       ? AST_MODULE_LOAD_DECLINE
00272       : AST_MODULE_LOAD_SUCCESS;
00273 }
00274 
00275 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Connected Line dialplan function");

Generated on Wed Jul 14 06:25:48 2010 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6