Consolidate ast_channel.cid.cid_rdnis into ast_channel.redirecting.from.number.
[asterisk/asterisk.git] / apps / app_dumpchan.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2005, Anthony Minessale II.
5  *
6  * Anthony Minessale <anthmct@yahoo.com>
7  *
8  * A license has been granted to Digium (via disclaimer) for the use of
9  * this code.
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2. See the LICENSE file
19  * at the top of the source tree.
20  */
21
22 /*! \file
23  *
24  * \brief Application to dump channel variables
25  *
26  * \author Anthony Minessale <anthmct@yahoo.com>
27  *
28  * \ingroup applications
29  */
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/pbx.h"
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/app.h"
39
40 /*** DOCUMENTATION
41         <application name="DumpChan" language="en_US">
42                 <synopsis>
43                         Dump Info About The Calling Channel.
44                 </synopsis>
45                 <syntax>
46                         <parameter name="level">
47                                 <para>Minimun verbose level</para>
48                         </parameter>
49                 </syntax>
50                 <description>
51                         <para>Displays information on channel and listing of all channel
52                         variables. If <replaceable>level</replaceable> is specified, output is only
53                         displayed when the verbose level is currently set to that number
54                         or greater.</para>
55                 </description>
56                 <see-also>
57                         <ref type="application">NoOp</ref>
58                         <ref type="application">Verbose</ref>
59                 </see-also>
60         </application>
61  ***/
62
63 static const char app[] = "DumpChan";
64
65 static int serialize_showchan(struct ast_channel *c, char *buf, size_t size)
66 {
67         struct timeval now;
68         long elapsed_seconds = 0;
69         int hour = 0, min = 0, sec = 0;
70         char cgrp[BUFSIZ/2];
71         char pgrp[BUFSIZ/2];
72         char formatbuf[BUFSIZ/2];
73
74         now = ast_tvnow();
75         memset(buf, 0, size);
76         if (!c)
77                 return 0;
78
79         if (c->cdr) {
80                 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
81                 hour = elapsed_seconds / 3600;
82                 min = (elapsed_seconds % 3600) / 60;
83                 sec = elapsed_seconds % 60;
84         }
85
86         snprintf(buf,size,
87                         "Name=               %s\n"
88                         "Type=               %s\n"
89                         "UniqueID=           %s\n"
90                         "CallerIDNum=        %s\n"
91                         "CallerIDName=       %s\n"
92                         "DNIDDigits=         %s\n"
93                         "RDNIS=              %s\n"
94                         "Parkinglot=         %s\n"
95                         "Language=           %s\n"
96                         "State=              %s (%d)\n"
97                         "Rings=              %d\n"
98                         "NativeFormat=       %s\n"
99                         "WriteFormat=        %s\n"
100                         "ReadFormat=         %s\n"
101                         "RawWriteFormat=     %s\n"
102                         "RawReadFormat=      %s\n"
103                         "1stFileDescriptor=  %d\n"
104                         "Framesin=           %d %s\n"
105                         "Framesout=          %d %s\n"
106                         "TimetoHangup=       %ld\n"
107                         "ElapsedTime=        %dh%dm%ds\n"
108                         "Context=            %s\n"
109                         "Extension=          %s\n"
110                         "Priority=           %d\n"
111                         "CallGroup=          %s\n"
112                         "PickupGroup=        %s\n"
113                         "Application=        %s\n"
114                         "Data=               %s\n"
115                         "Blocking_in=        %s\n",
116                         c->name,
117                         c->tech->type,
118                         c->uniqueid,
119                         S_OR(c->cid.cid_num, "(N/A)"),
120                         S_OR(c->cid.cid_name, "(N/A)"),
121                         S_OR(c->cid.cid_dnid, "(N/A)"),
122                         S_OR(c->redirecting.from.number, "(N/A)"),
123                         c->parkinglot,
124                         c->language,
125                         ast_state2str(c->_state),
126                         c->_state,
127                         c->rings,
128                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->nativeformats),
129                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->writeformat),
130                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->readformat),
131                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->rawwriteformat),
132                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->rawreadformat),
133                         c->fds[0], c->fin & ~DEBUGCHAN_FLAG, (c->fin & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
134                         c->fout & ~DEBUGCHAN_FLAG, (c->fout & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "", (long)c->whentohangup.tv_sec,
135                         hour,
136                         min,
137                         sec,
138                         c->context,
139                         c->exten,
140                         c->priority,
141                         ast_print_group(cgrp, sizeof(cgrp), c->callgroup),
142                         ast_print_group(pgrp, sizeof(pgrp), c->pickupgroup),
143                         ( c->appl ? c->appl : "(N/A)" ),
144                         ( c-> data ? S_OR(c->data, "(Empty)") : "(None)"),
145                         (ast_test_flag(c, AST_FLAG_BLOCKING) ? c->blockproc : "(Not Blocking)"));
146
147         return 0;
148 }
149
150 static int dumpchan_exec(struct ast_channel *chan, const char *data)
151 {
152         struct ast_str *vars = ast_str_thread_get(&ast_str_thread_global_buf, 16);
153         char info[1024];
154         int level = 0;
155         static char *line = "================================================================================";
156
157         if (!ast_strlen_zero(data))
158                 level = atoi(data);
159
160         if (option_verbose >= level) {
161                 serialize_showchan(chan, info, sizeof(info));
162                 pbx_builtin_serialize_variables(chan, &vars);
163                 ast_verbose("\nDumping Info For Channel: %s:\n%s\nInfo:\n%s\nVariables:\n%s%s\n", chan->name, line, info, ast_str_buffer(vars), line);
164         }
165
166         return 0;
167 }
168
169 static int unload_module(void)
170 {
171         return ast_unregister_application(app);
172 }
173
174 static int load_module(void)
175 {
176         return ast_register_application_xml(app, dumpchan_exec);
177 }
178
179 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dump Info About The Calling Channel");