Merge in the bridge_construction branch to make the system use the Bridging API.
[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 /*** MODULEINFO
32         <support_level>core</support_level>
33  ***/
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/app.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/bridging.h"
45
46 /*** DOCUMENTATION
47         <application name="DumpChan" language="en_US">
48                 <synopsis>
49                         Dump Info About The Calling Channel.
50                 </synopsis>
51                 <syntax>
52                         <parameter name="level">
53                                 <para>Minimun verbose level</para>
54                         </parameter>
55                 </syntax>
56                 <description>
57                         <para>Displays information on channel and listing of all channel
58                         variables. If <replaceable>level</replaceable> is specified, output is only
59                         displayed when the verbose level is currently set to that number
60                         or greater.</para>
61                 </description>
62                 <see-also>
63                         <ref type="application">NoOp</ref>
64                         <ref type="application">Verbose</ref>
65                 </see-also>
66         </application>
67  ***/
68
69 static const char app[] = "DumpChan";
70
71 static int serialize_showchan(struct ast_channel *c, char *buf, size_t size)
72 {
73         struct timeval now;
74         long elapsed_seconds = 0;
75         int hour = 0, min = 0, sec = 0;
76         char nf[256];
77         char cgrp[256];
78         char pgrp[256];
79         struct ast_str *write_transpath = ast_str_alloca(256);
80         struct ast_str *read_transpath = ast_str_alloca(256);
81         struct ast_bridge *bridge;
82
83         now = ast_tvnow();
84         memset(buf, 0, size);
85         if (!c)
86                 return 0;
87
88         if (ast_channel_cdr(c)) {
89                 elapsed_seconds = now.tv_sec - ast_channel_cdr(c)->start.tv_sec;
90                 hour = elapsed_seconds / 3600;
91                 min = (elapsed_seconds % 3600) / 60;
92                 sec = elapsed_seconds % 60;
93         }
94
95         ast_channel_lock(c);
96         bridge = ast_channel_get_bridge(c);
97         ast_channel_unlock(c);
98         snprintf(buf,size,
99                 "Name=               %s\n"
100                 "Type=               %s\n"
101                 "UniqueID=           %s\n"
102                 "LinkedID=           %s\n"
103                 "CallerIDNum=        %s\n"
104                 "CallerIDName=       %s\n"
105                 "ConnectedLineIDNum= %s\n"
106                 "ConnectedLineIDName=%s\n"
107                 "DNIDDigits=         %s\n"
108                 "RDNIS=              %s\n"
109                 "Parkinglot=         %s\n"
110                 "Language=           %s\n"
111                 "State=              %s (%d)\n"
112                 "Rings=              %d\n"
113                 "NativeFormat=       %s\n"
114                 "WriteFormat=        %s\n"
115                 "ReadFormat=         %s\n"
116                 "RawWriteFormat=     %s\n"
117                 "RawReadFormat=      %s\n"
118                 "WriteTranscode=     %s %s\n"
119                 "ReadTranscode=      %s %s\n"
120                 "1stFileDescriptor=  %d\n"
121                 "Framesin=           %d %s\n"
122                 "Framesout=          %d %s\n"
123                 "TimetoHangup=       %ld\n"
124                 "ElapsedTime=        %dh%dm%ds\n"
125                 "BridgeID=           %s\n"
126                 "Context=            %s\n"
127                 "Extension=          %s\n"
128                 "Priority=           %d\n"
129                 "CallGroup=          %s\n"
130                 "PickupGroup=        %s\n"
131                 "Application=        %s\n"
132                 "Data=               %s\n"
133                 "Blocking_in=        %s\n",
134                 ast_channel_name(c),
135                 ast_channel_tech(c)->type,
136                 ast_channel_uniqueid(c),
137                 ast_channel_linkedid(c),
138                 S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, "(N/A)"),
139                 S_COR(ast_channel_caller(c)->id.name.valid, ast_channel_caller(c)->id.name.str, "(N/A)"),
140                 S_COR(ast_channel_connected(c)->id.number.valid, ast_channel_connected(c)->id.number.str, "(N/A)"),
141                 S_COR(ast_channel_connected(c)->id.name.valid, ast_channel_connected(c)->id.name.str, "(N/A)"),
142                 S_OR(ast_channel_dialed(c)->number.str, "(N/A)"),
143                 S_COR(ast_channel_redirecting(c)->from.number.valid, ast_channel_redirecting(c)->from.number.str, "(N/A)"),
144                 ast_channel_parkinglot(c),
145                 ast_channel_language(c),        
146                 ast_state2str(ast_channel_state(c)),
147                 ast_channel_state(c),
148                 ast_channel_rings(c), 
149                 ast_getformatname_multiple(nf, sizeof(nf), ast_channel_nativeformats(c)),
150                 ast_getformatname(ast_channel_writeformat(c)),
151                 ast_getformatname(ast_channel_readformat(c)),
152                 ast_getformatname(ast_channel_rawwriteformat(c)),
153                 ast_getformatname(ast_channel_rawreadformat(c)),
154                 ast_channel_writetrans(c) ? "Yes" : "No",
155                 ast_translate_path_to_str(ast_channel_writetrans(c), &write_transpath),
156                 ast_channel_readtrans(c) ? "Yes" : "No",
157                 ast_translate_path_to_str(ast_channel_readtrans(c), &read_transpath),
158                 ast_channel_fd(c, 0),
159                 ast_channel_fin(c) & ~DEBUGCHAN_FLAG, (ast_channel_fin(c) & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
160                 ast_channel_fout(c) & ~DEBUGCHAN_FLAG, (ast_channel_fout(c) & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "",
161                 (long)ast_channel_whentohangup(c)->tv_sec,
162                 hour,
163                 min,
164                 sec,
165                 bridge ? bridge->uniqueid : "(Not bridged)",
166                 ast_channel_context(c),
167                 ast_channel_exten(c),
168                 ast_channel_priority(c),
169                 ast_print_group(cgrp, sizeof(cgrp), ast_channel_callgroup(c)),
170                 ast_print_group(pgrp, sizeof(pgrp), ast_channel_pickupgroup(c)),
171                 ast_channel_appl(c) ? ast_channel_appl(c) : "(N/A)",
172                 ast_channel_data(c) ? S_OR(ast_channel_data(c), "(Empty)") : "(None)",
173                 (ast_test_flag(ast_channel_flags(c), AST_FLAG_BLOCKING) ? ast_channel_blockproc(c) : "(Not Blocking)"));
174
175         ao2_cleanup(bridge);
176         return 0;
177 }
178
179 static int dumpchan_exec(struct ast_channel *chan, const char *data)
180 {
181         struct ast_str *vars = ast_str_thread_get(&ast_str_thread_global_buf, 16);
182         char info[2048];
183         int level = 0;
184         static char *line = "================================================================================";
185
186         if (!ast_strlen_zero(data))
187                 level = atoi(data);
188
189         if (option_verbose >= level) {
190                 serialize_showchan(chan, info, sizeof(info));
191                 pbx_builtin_serialize_variables(chan, &vars);
192                 ast_verbose("\n"
193                         "Dumping Info For Channel: %s:\n"
194                         "%s\n"
195                         "Info:\n"
196                         "%s\n"
197                         "Variables:\n"
198                         "%s%s\n", ast_channel_name(chan), line, info, ast_str_buffer(vars), line);
199         }
200
201         return 0;
202 }
203
204 static int unload_module(void)
205 {
206         return ast_unregister_application(app);
207 }
208
209 static int load_module(void)
210 {
211         return ast_register_application_xml(app, dumpchan_exec);
212 }
213
214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dump Info About The Calling Channel");