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