Merge changes from team/group/appdocsxml
[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
39 /*** DOCUMENTATION
40         <application name="DumpChan" language="en_US">
41                 <synopsis>
42                         Dump Info About The Calling Channel.
43                 </synopsis>
44                 <syntax>
45                         <parameter name="level">
46                                 <para>Minimun verbose level</para>
47                         </parameter>
48                 </syntax>
49                 <description>
50                         <para>Displays information on channel and listing of all channel
51                         variables. If <replaceable>level</replaceable> is specified, output is only
52                         displayed when the verbose level is currently set to that number
53                         or greater.</para>
54                 </description>
55         </application>
56  ***/
57
58 static char *app = "DumpChan";
59
60 static int serialize_showchan(struct ast_channel *c, char *buf, size_t size)
61 {
62         struct timeval now;
63         long elapsed_seconds = 0;
64         int hour = 0, min = 0, sec = 0;
65         char cgrp[BUFSIZ/2];
66         char pgrp[BUFSIZ/2];
67         char formatbuf[BUFSIZ/2];
68
69         now = ast_tvnow();
70         memset(buf, 0, size);
71         if (!c)
72                 return 0;
73
74         if (c->cdr) {
75                 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
76                 hour = elapsed_seconds / 3600;
77                 min = (elapsed_seconds % 3600) / 60;
78                 sec = elapsed_seconds % 60;
79         }
80
81         snprintf(buf,size,
82                         "Name=               %s\n"
83                         "Type=               %s\n"
84                         "UniqueID=           %s\n"
85                         "CallerIDNum=        %s\n"
86                         "CallerIDName=       %s\n"
87                         "DNIDDigits=         %s\n"
88                         "RDNIS=              %s\n"
89                         "Parkinglot=         %s\n"
90                         "Language=           %s\n"
91                         "State=              %s (%d)\n"
92                         "Rings=              %d\n"
93                         "NativeFormat=       %s\n"
94                         "WriteFormat=        %s\n"
95                         "ReadFormat=         %s\n"
96                         "RawWriteFormat=     %s\n"
97                         "RawReadFormat=      %s\n"
98                         "1stFileDescriptor=  %d\n"
99                         "Framesin=           %d %s\n"
100                         "Framesout=          %d %s\n"
101                         "TimetoHangup=       %ld\n"
102                         "ElapsedTime=        %dh%dm%ds\n"
103                         "Context=            %s\n"
104                         "Extension=          %s\n"
105                         "Priority=           %d\n"
106                         "CallGroup=          %s\n"
107                         "PickupGroup=        %s\n"
108                         "Application=        %s\n"
109                         "Data=               %s\n"
110                         "Blocking_in=        %s\n",
111                         c->name,
112                         c->tech->type,
113                         c->uniqueid,
114                         S_OR(c->cid.cid_num, "(N/A)"),
115                         S_OR(c->cid.cid_name, "(N/A)"),
116                         S_OR(c->cid.cid_dnid, "(N/A)"),
117                         S_OR(c->cid.cid_rdnis, "(N/A)"),
118                         c->parkinglot,
119                         c->language,
120                         ast_state2str(c->_state),
121                         c->_state,
122                         c->rings,
123                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->nativeformats),
124                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->writeformat),
125                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->readformat),
126                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->rawwriteformat),
127                         ast_getformatname_multiple(formatbuf, sizeof(formatbuf), c->rawreadformat),
128                         c->fds[0], c->fin & ~DEBUGCHAN_FLAG, (c->fin & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
129                         c->fout & ~DEBUGCHAN_FLAG, (c->fout & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "", (long)c->whentohangup.tv_sec,
130                         hour,
131                         min,
132                         sec,
133                         c->context,
134                         c->exten,
135                         c->priority,
136                         ast_print_group(cgrp, sizeof(cgrp), c->callgroup),
137                         ast_print_group(pgrp, sizeof(pgrp), c->pickupgroup),
138                         ( c->appl ? c->appl : "(N/A)" ),
139                         ( c-> data ? S_OR(c->data, "(Empty)") : "(None)"),
140                         (ast_test_flag(c, AST_FLAG_BLOCKING) ? c->blockproc : "(Not Blocking)"));
141
142         return 0;
143 }
144
145 static int dumpchan_exec(struct ast_channel *chan, void *data)
146 {
147         struct ast_str *vars = ast_str_alloca(BUFSIZ * 4); /* XXX very large! */
148         char info[1024];
149         int level = 0;
150         static char *line = "================================================================================";
151
152         if (!ast_strlen_zero(data))
153                 level = atoi(data);
154
155         pbx_builtin_serialize_variables(chan, &vars);
156         serialize_showchan(chan, info, sizeof(info));
157         if (option_verbose >= level)
158                 ast_verbose("\nDumping Info For Channel: %s:\n%s\nInfo:\n%s\nVariables:\n%s%s\n", chan->name, line, info, vars->str, line);
159
160         return 0;
161 }
162
163 static int unload_module(void)
164 {
165         return ast_unregister_application(app);
166 }
167
168 static int load_module(void)
169 {
170         return ast_register_application_xml(app, dumpchan_exec);
171 }
172
173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dump Info About The Calling Channel");