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