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