2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2010, Digium, Inc.
6 * David Vossel <dvossel@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Trace internal ast_frames on a channel.
23 * \author David Vossel <dvossel@digium.com>
29 <support_level>extended</support_level>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/framehook.h"
42 <function name="FRAME_TRACE" language="en_US">
44 View internal ast_frames as they are read and written on a channel.
47 <parameter name="filter list type" required="true">
48 <para>A filter can be applied to the trace to limit what frames are viewed. This
49 filter can either be a <literal>white</literal> or <literal>black</literal> list
50 of frame types. When no filter type is present, <literal>white</literal> is
51 used. If no arguments are provided at all, all frames will be output.
54 <para>Below are the different types of frames that can be filtered.</para>
56 <enum name = "DTMF_BEGIN" />
57 <enum name = "DTMF_END" />
58 <enum name = "VOICE" />
59 <enum name = "VIDEO" />
60 <enum name = "CONTROL" />
61 <enum name = "NULL" />
63 <enum name = "TEXT" />
64 <enum name = "IMAGE" />
65 <enum name = "HTML" />
67 <enum name = "MODEM" />
72 <para>Examples:</para>
73 <para>exten => 1,1,Set(FRAME_TRACE(white)=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
74 <para>exten => 1,1,Set(FRAME_TRACE()=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
75 <para>exten => 1,1,Set(FRAME_TRACE(black)=DTMF_BEGIN,DTMF_END); view everything except DTMF frames. </para>
80 static void print_frame(struct ast_frame *frame);
82 enum ast_frame_type type;
85 { AST_FRAME_DTMF_BEGIN, "DTMF_BEGIN" },
86 { AST_FRAME_DTMF_END, "DTMF_END" },
87 { AST_FRAME_VOICE, "VOICE" },
88 { AST_FRAME_VIDEO, "VIDEO" },
89 { AST_FRAME_CONTROL, "CONTROL" },
90 { AST_FRAME_NULL, "NULL" },
91 { AST_FRAME_IAX, "IAX" },
92 { AST_FRAME_TEXT, "TEXT" },
93 { AST_FRAME_IMAGE, "IMAGE" },
94 { AST_FRAME_HTML, "HTML" },
95 { AST_FRAME_CNG, "CNG" },
96 { AST_FRAME_MODEM, "MODEM" },
99 struct frame_trace_data {
100 int list_type; /* 0 = white, 1 = black */
101 int values[ARRAY_LEN(frametype2str)];
104 static void datastore_destroy_cb(void *data) {
108 static const struct ast_datastore_info frame_trace_datastore = {
109 .type = "frametrace",
110 .destroy = datastore_destroy_cb
113 static void hook_destroy_cb(void *framedata)
118 static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
122 struct frame_trace_data *framedata = data;
127 if ((event != AST_FRAMEHOOK_EVENT_WRITE) && (event != AST_FRAMEHOOK_EVENT_READ)) {
131 for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
132 if (frame->frametype == frametype2str[i].type) {
133 if ((framedata->list_type == 0) && (framedata->values[i])) { /* white list */
135 } else if ((framedata->list_type == 1) && (!framedata->values[i])){ /* black list */
143 ast_verbose("%s on Channel %s\n", event == AST_FRAMEHOOK_EVENT_READ ? "<--Read" : "--> Write", ast_channel_name(chan));
149 static int frame_trace_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
151 struct frame_trace_data *framedata;
152 struct ast_datastore *datastore = NULL;
153 struct ast_framehook_interface interface = {
154 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
155 .event_cb = hook_event_cb,
156 .destroy_cb = hook_destroy_cb,
160 if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
164 interface.data = framedata;
166 if (!strcasecmp(data, "black")) {
167 framedata->list_type = 1;
169 for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
170 if (strcasestr(value, frametype2str[i].str)) {
171 framedata->values[i] = 1;
175 ast_channel_lock(chan);
176 i = ast_framehook_attach(chan, &interface);
179 if ((datastore = ast_channel_datastore_find(chan, &frame_trace_datastore, NULL))) {
180 id = datastore->data;
181 ast_framehook_detach(chan, *id);
182 ast_channel_datastore_remove(chan, datastore);
185 if (!(datastore = ast_datastore_alloc(&frame_trace_datastore, NULL))) {
186 ast_framehook_detach(chan, i);
187 ast_channel_unlock(chan);
191 if (!(id = ast_calloc(1, sizeof(int)))) {
192 ast_datastore_free(datastore);
193 ast_framehook_detach(chan, i);
194 ast_channel_unlock(chan);
198 *id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
199 datastore->data = id;
200 ast_channel_datastore_add(chan, datastore);
202 ast_channel_unlock(chan);
207 static void print_frame(struct ast_frame *frame)
209 switch (frame->frametype) {
210 case AST_FRAME_DTMF_END:
211 ast_verbose("FrameType: DTMF END\n");
212 ast_verbose("Digit: 0x%02X '%c'\n", frame->subclass.integer,
213 frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
215 case AST_FRAME_VOICE:
216 ast_verbose("FrameType: VOICE\n");
217 ast_verbose("Codec: %s\n", ast_getformatname(&frame->subclass.format));
218 ast_verbose("MS: %ld\n", frame->len);
219 ast_verbose("Samples: %d\n", frame->samples);
220 ast_verbose("Bytes: %d\n", frame->datalen);
222 case AST_FRAME_VIDEO:
223 ast_verbose("FrameType: VIDEO\n");
224 ast_verbose("Codec: %s\n", ast_getformatname(&frame->subclass.format));
225 ast_verbose("MS: %ld\n", frame->len);
226 ast_verbose("Samples: %d\n", frame->samples);
227 ast_verbose("Bytes: %d\n", frame->datalen);
229 case AST_FRAME_CONTROL:
230 ast_verbose("FrameType: CONTROL\n");
231 switch ((enum ast_control_frame_type) frame->subclass.integer) {
232 case AST_CONTROL_HANGUP:
233 ast_verbose("SubClass: HANGUP\n");
235 case AST_CONTROL_RING:
236 ast_verbose("SubClass: RING\n");
238 case AST_CONTROL_RINGING:
239 ast_verbose("SubClass: RINGING\n");
241 case AST_CONTROL_ANSWER:
242 ast_verbose("SubClass: ANSWER\n");
244 case AST_CONTROL_BUSY:
245 ast_verbose("SubClass: BUSY\n");
247 case AST_CONTROL_TAKEOFFHOOK:
248 ast_verbose("SubClass: TAKEOFFHOOK\n");
250 case AST_CONTROL_OFFHOOK:
251 ast_verbose("SubClass: OFFHOOK\n");
253 case AST_CONTROL_CONGESTION:
254 ast_verbose("SubClass: CONGESTION\n");
256 case AST_CONTROL_FLASH:
257 ast_verbose("SubClass: FLASH\n");
259 case AST_CONTROL_WINK:
260 ast_verbose("SubClass: WINK\n");
262 case AST_CONTROL_OPTION:
263 ast_verbose("SubClass: OPTION\n");
265 case AST_CONTROL_RADIO_KEY:
266 ast_verbose("SubClass: RADIO KEY\n");
268 case AST_CONTROL_RADIO_UNKEY:
269 ast_verbose("SubClass: RADIO UNKEY\n");
271 case AST_CONTROL_PROGRESS:
272 ast_verbose("SubClass: PROGRESS\n");
274 case AST_CONTROL_PROCEEDING:
275 ast_verbose("SubClass: PROCEEDING\n");
277 case AST_CONTROL_HOLD:
278 ast_verbose("SubClass: HOLD\n");
280 case AST_CONTROL_UNHOLD:
281 ast_verbose("SubClass: UNHOLD\n");
283 case AST_CONTROL_VIDUPDATE:
284 ast_verbose("SubClass: VIDUPDATE\n");
286 case _XXX_AST_CONTROL_T38:
287 ast_verbose("SubClass: XXX T38\n");
289 case AST_CONTROL_SRCUPDATE:
290 ast_verbose("SubClass: SRCUPDATE\n");
292 case AST_CONTROL_TRANSFER:
293 ast_verbose("SubClass: TRANSFER\n");
295 case AST_CONTROL_CONNECTED_LINE:
296 ast_verbose("SubClass: CONNECTED LINE\n");
298 case AST_CONTROL_REDIRECTING:
299 ast_verbose("SubClass: REDIRECTING\n");
301 case AST_CONTROL_T38_PARAMETERS:
302 ast_verbose("SubClass: T38 PARAMETERS\n");
305 ast_verbose("SubClass: CC\n");
307 case AST_CONTROL_SRCCHANGE:
308 ast_verbose("SubClass: SRCCHANGE\n");
310 case AST_CONTROL_READ_ACTION:
311 ast_verbose("SubClass: READ ACTION\n");
313 case AST_CONTROL_AOC:
314 ast_verbose("SubClass: AOC\n");
316 case AST_CONTROL_MCID:
317 ast_verbose("SubClass: MCID\n");
319 case AST_CONTROL_INCOMPLETE:
320 ast_verbose("SubClass: INCOMPLETE\n");
322 case AST_CONTROL_END_OF_Q:
323 ast_verbose("SubClass: END_OF_Q\n");
325 case AST_CONTROL_UPDATE_RTP_PEER:
326 ast_verbose("SubClass: UPDATE_RTP_PEER\n");
328 case AST_CONTROL_PVT_CAUSE_CODE:
329 ast_verbose("SubClass: PVT_CAUSE_CODE\n");
331 case AST_CONTROL_STREAM_STOP:
332 ast_verbose("SubClass: STREAM_STOP\n");
334 case AST_CONTROL_STREAM_SUSPEND:
335 ast_verbose("SubClass: STREAM_SUSPEND\n");
337 case AST_CONTROL_STREAM_RESTART:
338 ast_verbose("SubClass: STREAM_RESTART\n");
340 case AST_CONTROL_STREAM_REVERSE:
341 ast_verbose("SubClass: STREAM_REVERSE\n");
343 case AST_CONTROL_STREAM_FORWARD:
344 ast_verbose("SubClass: STREAM_FORWARD\n");
348 if (frame->subclass.integer == -1) {
349 ast_verbose("SubClass: %d\n", frame->subclass.integer);
351 ast_verbose("Bytes: %d\n", frame->datalen);
354 ast_verbose("FrameType: NULL\n");
357 ast_verbose("FrameType: IAX\n");
360 ast_verbose("FrameType: TXT\n");
362 case AST_FRAME_IMAGE:
363 ast_verbose("FrameType: IMAGE\n");
366 ast_verbose("FrameType: HTML\n");
369 ast_verbose("FrameType: CNG\n");
371 case AST_FRAME_MODEM:
372 ast_verbose("FrameType: MODEM\n");
374 case AST_FRAME_DTMF_BEGIN:
375 ast_verbose("FrameType: DTMF BEGIN\n");
376 ast_verbose("Digit: 0x%02X '%c'\n", frame->subclass.integer,
377 frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
381 ast_verbose("Src: %s\n", ast_strlen_zero(frame->src) ? "NOT PRESENT" : frame->src);
385 static struct ast_custom_function frame_trace_function = {
386 .name = "FRAME_TRACE",
387 .write = frame_trace_helper,
390 static int unload_module(void)
392 return ast_custom_function_unregister(&frame_trace_function);
395 static int load_module(void)
397 int res = ast_custom_function_register(&frame_trace_function);
398 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
401 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Frame Trace for internal ast_frame debugging.");