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