oops
[asterisk/asterisk.git] / apps / app_dumpchan.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Application to dump channel variables
5  * 
6  * Copyright (C) 2004, Anthony Minessale II.
7  *
8  * Anthony Minessale <anthmct@yahoo.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License (and disclaimed to Digium)
12  */
13
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/options.h>
20 #include <asterisk/utils.h>
21 #include <asterisk/lock.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25
26 static char *tdesc = "Dump Info About The Calling Channel";
27 static char *app = "DumpChan";
28 static char *synopsis = "Dump Info About The Calling Channel";
29 static char *desc = 
30 "   DumpChan([<min_verbose_level>])\n"
31 "Displays information on channel and listing of all channel\n"
32 "variables.  If min_verbose_level is specified, output is only\n"
33 "display when the verbose level is currently set to that number\n"
34 "or greater.  Always returns 0.\n\n";
35
36 STANDARD_LOCAL_USER;
37
38 LOCAL_USER_DECL;
39
40 static int ast_serialize_showchan(struct ast_channel *c, char *buf, size_t size)
41 {
42         struct timeval now;
43         long elapsed_seconds=0;
44         int hour=0, min=0, sec=0;
45         gettimeofday(&now, NULL);
46         memset(buf,0,size);
47         if (!c)
48                 return 0;
49
50         if (c->cdr) {
51                 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
52                 hour = elapsed_seconds / 3600;
53                 min = (elapsed_seconds % 3600) / 60;
54                 sec = elapsed_seconds % 60;
55         }
56
57         snprintf(buf,size, 
58                          "Name=%s\n"
59                          "Type=%s\n"
60                          "UniqueID=%s\n"
61                          "CallerID=%s\n"
62                          "CallerIDName=%s\n"
63                          "DNIDDigits=%s\n"
64                          "State=%s(%d)\n"
65                          "Rings=%d\n"
66                          "NativeFormat=%d\n"
67                          "WriteFormat=%d\n"
68                          "ReadFormat=%d\n"
69                          "1stFileDescriptor=%d\n"
70                          "Framesin=%d%s\n"
71                          "Framesout=%d%s\n"
72                          "TimetoHangup=%ld\n"
73                          "ElapsedTime=%dh%dm%ds\n"
74                          "Context=%s\n"
75                          "Extension=%s\n"
76                          "Priority=%d\n"
77                          "CallGroup=%d\n"
78                          "PickupGroup=%d\n"
79                          "Application=%s\n"
80                          "Data=%s\n"
81                          "Stack=%d\n"
82                          "Blocking_in=%s\n",
83                          c->name,
84                          c->type,
85                          c->uniqueid,
86                          (c->cid.cid_num ? c->cid.cid_num : "(N/A)"),
87                          (c->cid.cid_name ? c->cid.cid_name : "(N/A)"),
88                          (c->cid.cid_dnid ? c->cid.cid_dnid : "(N/A)" ),
89                          ast_state2str(c->_state),
90                          c->_state,
91                          c->rings,
92                          c->nativeformats,
93                          c->writeformat,
94                          c->readformat,
95                          c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
96                          c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
97                          hour,
98                          min,
99                          sec,
100                          c->context,
101                          c->exten,
102                          c->priority,
103                          c->callgroup,
104                          c->pickupgroup,
105                          ( c->appl ? c->appl : "(N/A)" ),
106                          ( c-> data ? (!ast_strlen_zero(c->data) ? c->data : "(Empty)") : "(None)"),
107                          c->stack,
108                          (c->blocking ? c->blockproc : "(Not Blocking)"));
109
110         return 0;
111 }
112
113 static int dumpchan_exec(struct ast_channel *chan, void *data)
114 {
115         int res=0;
116         struct localuser *u;
117         char vars[1024];
118         char info[1024];
119         int level = 0;
120         static char *line = "================================================================================";
121         LOCAL_USER_ADD(u);
122
123         if (data) {
124                 level = atoi(data);
125         }
126
127         pbx_builtin_serialize_variables(chan, vars, sizeof(vars));
128         ast_serialize_showchan(chan, info, sizeof(info));
129         if (option_verbose >= level)
130                 ast_verbose("\nDumping Info For Channel: %s:\n%s\nInfo:\n%s\nVariables:\n%s%s\n",chan->name,line,info,vars,line);
131
132         LOCAL_USER_REMOVE(u);
133         return res;
134 }
135
136 int unload_module(void)
137 {
138         STANDARD_HANGUP_LOCALUSERS;
139         return ast_unregister_application(app);
140 }
141
142 int load_module(void)
143 {
144         return ast_register_application(app, dumpchan_exec, synopsis, desc);
145 }
146
147 char *description(void)
148 {
149         return tdesc;
150 }
151
152 int usecount(void)
153 {
154         int res;
155         STANDARD_USECOUNT(res);
156         return res;
157 }
158
159 char *key()
160 {
161         return ASTERISK_GPL_KEY;
162 }
163