15fb3baba9b99b5f5d91faac77e7653e653a3f4b
[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  * disclaimed to Digium
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief Application to dump channel variables
24  * 
25  * \ingroup applications
26  */
27
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/file.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41 #include "asterisk/options.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/utils.h"
45
46 static char *tdesc = "Dump Info About The Calling Channel";
47 static char *app = "DumpChan";
48 static char *synopsis = "Dump Info About The Calling Channel";
49 static char *desc = 
50 "   DumpChan([<min_verbose_level>])\n"
51 "Displays information on channel and listing of all channel\n"
52 "variables. If min_verbose_level is specified, output is only\n"
53 "displayed when the verbose level is currently set to that number\n"
54 "or greater. \n";
55
56 STANDARD_LOCAL_USER;
57
58 LOCAL_USER_DECL;
59
60 static int ast_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[256];
66         char pgrp[256];
67         
68         now = ast_tvnow();
69         memset(buf,0,size);
70         if (!c)
71                 return 0;
72
73         if (c->cdr) {
74                 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
75                 hour = elapsed_seconds / 3600;
76                 min = (elapsed_seconds % 3600) / 60;
77                 sec = elapsed_seconds % 60;
78         }
79
80         snprintf(buf,size, 
81                          "Name=               %s\n"
82                          "Type=               %s\n"
83                          "UniqueID=           %s\n"
84                          "CallerID=           %s\n"
85                          "CallerIDName=       %s\n"
86                          "DNIDDigits=         %s\n"
87                          "State=              %s (%d)\n"
88                          "Rings=              %d\n"
89                          "NativeFormat=       %d\n"
90                          "WriteFormat=        %d\n"
91                          "ReadFormat=         %d\n"
92                          "1stFileDescriptor=  %d\n"
93                          "Framesin=           %d %s\n"
94                          "Framesout=          %d %s\n"
95                          "TimetoHangup=       %ld\n"
96                          "ElapsedTime=        %dh%dm%ds\n"
97                          "Context=            %s\n"
98                          "Extension=          %s\n"
99                          "Priority=           %d\n"
100                          "CallGroup=          %s\n"
101                          "PickupGroup=        %s\n"
102                          "Application=        %s\n"
103                          "Data=               %s\n"
104                          "Blocking_in=        %s\n",
105                          c->name,
106                          c->type,
107                          c->uniqueid,
108                          (c->cid.cid_num ? c->cid.cid_num : "(N/A)"),
109                          (c->cid.cid_name ? c->cid.cid_name : "(N/A)"),
110                          (c->cid.cid_dnid ? c->cid.cid_dnid : "(N/A)" ),
111                          ast_state2str(c->_state),
112                          c->_state,
113                          c->rings,
114                          c->nativeformats,
115                          c->writeformat,
116                          c->readformat,
117                          c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
118                          c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (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 ? (!ast_strlen_zero(c->data) ? 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         int res=0;
137         struct localuser *u;
138         char vars[1024];
139         char info[1024];
140         int level = 0;
141         static char *line = "================================================================================";
142         
143         LOCAL_USER_ADD(u);
144
145         if (!ast_strlen_zero(data)) {
146                 level = atoi(data);
147         }
148
149         pbx_builtin_serialize_variables(chan, vars, sizeof(vars));
150         ast_serialize_showchan(chan, info, sizeof(info));
151         if (option_verbose >= level)
152                 ast_verbose("\nDumping Info For Channel: %s:\n%s\nInfo:\n%s\nVariables:\n%s%s\n",chan->name, line, info, vars, line);
153
154         LOCAL_USER_REMOVE(u);
155         
156         return res;
157 }
158
159 int unload_module(void)
160 {
161         int res;
162
163         res = ast_unregister_application(app);
164
165         STANDARD_HANGUP_LOCALUSERS;
166
167         return res;
168 }
169
170 int load_module(void)
171 {
172         return ast_register_application(app, dumpchan_exec, synopsis, desc);
173 }
174
175 char *description(void)
176 {
177         return tdesc;
178 }
179
180 int usecount(void)
181 {
182         int res;
183         STANDARD_USECOUNT(res);
184         return res;
185 }
186
187 char *key()
188 {
189         return ASTERISK_GPL_KEY;
190 }
191