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,
161 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
165 if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
169 interface.data = framedata;
171 if (!strcasecmp(data, "black")) {
172 framedata->list_type = 1;
174 for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
175 if (strcasestr(value, frametype2str[i].str)) {
176 framedata->values[i] = 1;
180 ast_channel_lock(chan);
181 i = ast_framehook_attach(chan, &interface);
184 if ((datastore = ast_channel_datastore_find(chan, &frame_trace_datastore, NULL))) {
185 id = datastore->data;
186 ast_framehook_detach(chan, *id);
187 ast_channel_datastore_remove(chan, datastore);
190 if (!(datastore = ast_datastore_alloc(&frame_trace_datastore, NULL))) {
191 ast_framehook_detach(chan, i);
192 ast_channel_unlock(chan);
196 if (!(id = ast_calloc(1, sizeof(int)))) {
197 ast_datastore_free(datastore);
198 ast_framehook_detach(chan, i);
199 ast_channel_unlock(chan);
203 *id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
204 datastore->data = id;
205 ast_channel_datastore_add(chan, datastore);
207 ast_channel_unlock(chan);
212 static void print_frame(struct ast_frame *frame)
214 switch (frame->frametype) {
215 case AST_FRAME_DTMF_END:
216 ast_verbose("FrameType: DTMF END\n");
217 ast_verbose("Digit: 0x%02X '%c'\n", (unsigned)frame->subclass.integer,
218 frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
220 case AST_FRAME_VOICE:
221 ast_verbose("FrameType: VOICE\n");
222 ast_verbose("Codec: %s\n", ast_format_get_name(frame->subclass.format));
223 ast_verbose("MS: %ld\n", frame->len);
224 ast_verbose("Samples: %d\n", frame->samples);
225 ast_verbose("Bytes: %d\n", frame->datalen);
227 case AST_FRAME_VIDEO:
228 ast_verbose("FrameType: VIDEO\n");
229 ast_verbose("Codec: %s\n", ast_format_get_name(frame->subclass.format));
230 ast_verbose("MS: %ld\n", frame->len);
231 ast_verbose("Samples: %d\n", frame->samples);
232 ast_verbose("Bytes: %d\n", frame->datalen);
234 case AST_FRAME_CONTROL:
235 ast_verbose("FrameType: CONTROL\n");
236 switch ((enum ast_control_frame_type) frame->subclass.integer) {
237 case AST_CONTROL_HANGUP:
238 ast_verbose("SubClass: HANGUP\n");
240 case AST_CONTROL_RING:
241 ast_verbose("SubClass: RING\n");
243 case AST_CONTROL_RINGING:
244 ast_verbose("SubClass: RINGING\n");
246 case AST_CONTROL_ANSWER:
247 ast_verbose("SubClass: ANSWER\n");
249 case AST_CONTROL_BUSY:
250 ast_verbose("SubClass: BUSY\n");
252 case AST_CONTROL_TAKEOFFHOOK:
253 ast_verbose("SubClass: TAKEOFFHOOK\n");
255 case AST_CONTROL_OFFHOOK:
256 ast_verbose("SubClass: OFFHOOK\n");
258 case AST_CONTROL_CONGESTION:
259 ast_verbose("SubClass: CONGESTION\n");
261 case AST_CONTROL_FLASH:
262 ast_verbose("SubClass: FLASH\n");
264 case AST_CONTROL_WINK:
265 ast_verbose("SubClass: WINK\n");
267 case AST_CONTROL_OPTION:
268 ast_verbose("SubClass: OPTION\n");
270 case AST_CONTROL_RADIO_KEY:
271 ast_verbose("SubClass: RADIO KEY\n");
273 case AST_CONTROL_RADIO_UNKEY:
274 ast_verbose("SubClass: RADIO UNKEY\n");
276 case AST_CONTROL_PROGRESS:
277 ast_verbose("SubClass: PROGRESS\n");
279 case AST_CONTROL_PROCEEDING:
280 ast_verbose("SubClass: PROCEEDING\n");
282 case AST_CONTROL_HOLD:
283 ast_verbose("SubClass: HOLD\n");
285 case AST_CONTROL_UNHOLD:
286 ast_verbose("SubClass: UNHOLD\n");
288 case AST_CONTROL_VIDUPDATE:
289 ast_verbose("SubClass: VIDUPDATE\n");
291 case _XXX_AST_CONTROL_T38:
292 ast_verbose("SubClass: XXX T38\n");
294 case AST_CONTROL_SRCUPDATE:
295 ast_verbose("SubClass: SRCUPDATE\n");
297 case AST_CONTROL_TRANSFER:
298 ast_verbose("SubClass: TRANSFER\n");
300 case AST_CONTROL_CONNECTED_LINE:
301 ast_verbose("SubClass: CONNECTED LINE\n");
303 case AST_CONTROL_REDIRECTING:
304 ast_verbose("SubClass: REDIRECTING\n");
306 case AST_CONTROL_T38_PARAMETERS:
307 ast_verbose("SubClass: T38 PARAMETERS\n");
310 ast_verbose("SubClass: CC\n");
312 case AST_CONTROL_SRCCHANGE:
313 ast_verbose("SubClass: SRCCHANGE\n");
315 case AST_CONTROL_READ_ACTION:
316 ast_verbose("SubClass: READ ACTION\n");
318 case AST_CONTROL_AOC:
319 ast_verbose("SubClass: AOC\n");
321 case AST_CONTROL_MCID:
322 ast_verbose("SubClass: MCID\n");
324 case AST_CONTROL_INCOMPLETE:
325 ast_verbose("SubClass: INCOMPLETE\n");
327 case AST_CONTROL_END_OF_Q:
328 ast_verbose("SubClass: END_OF_Q\n");
330 case AST_CONTROL_UPDATE_RTP_PEER:
331 ast_verbose("SubClass: UPDATE_RTP_PEER\n");
333 case AST_CONTROL_PVT_CAUSE_CODE:
334 ast_verbose("SubClass: PVT_CAUSE_CODE\n");
336 case AST_CONTROL_STREAM_STOP:
337 ast_verbose("SubClass: STREAM_STOP\n");
339 case AST_CONTROL_STREAM_SUSPEND:
340 ast_verbose("SubClass: STREAM_SUSPEND\n");
342 case AST_CONTROL_STREAM_RESTART:
343 ast_verbose("SubClass: STREAM_RESTART\n");
345 case AST_CONTROL_STREAM_REVERSE:
346 ast_verbose("SubClass: STREAM_REVERSE\n");
348 case AST_CONTROL_STREAM_FORWARD:
349 ast_verbose("SubClass: STREAM_FORWARD\n");
351 case AST_CONTROL_RECORD_CANCEL:
352 ast_verbose("SubClass: RECORD_CANCEL\n");
354 case AST_CONTROL_RECORD_STOP:
355 ast_verbose("SubClass: RECORD_STOP\n");
357 case AST_CONTROL_RECORD_SUSPEND:
358 ast_verbose("SubClass: RECORD_SUSPEND\n");
360 case AST_CONTROL_RECORD_MUTE:
361 ast_verbose("SubClass: RECORD_MUTE\n");
365 if (frame->subclass.integer == -1) {
366 ast_verbose("SubClass: %d\n", frame->subclass.integer);
368 ast_verbose("Bytes: %d\n", frame->datalen);
371 ast_verbose("FrameType: NULL\n");
374 ast_verbose("FrameType: IAX\n");
377 ast_verbose("FrameType: TXT\n");
379 case AST_FRAME_IMAGE:
380 ast_verbose("FrameType: IMAGE\n");
383 ast_verbose("FrameType: HTML\n");
386 ast_verbose("FrameType: CNG\n");
388 case AST_FRAME_MODEM:
389 ast_verbose("FrameType: MODEM\n");
391 case AST_FRAME_DTMF_BEGIN:
392 ast_verbose("FrameType: DTMF BEGIN\n");
393 ast_verbose("Digit: 0x%02X '%c'\n", (unsigned)frame->subclass.integer,
394 frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
396 case AST_FRAME_BRIDGE_ACTION:
397 ast_verbose("FrameType: Bridge\n");
398 ast_verbose("SubClass: %d\n", frame->subclass.integer);
400 case AST_FRAME_BRIDGE_ACTION_SYNC:
401 ast_verbose("Frametype: Synchronous Bridge\n");
402 ast_verbose("Subclass: %d\n", frame->subclass.integer);
406 ast_verbose("Src: %s\n", ast_strlen_zero(frame->src) ? "NOT PRESENT" : frame->src);
410 static struct ast_custom_function frame_trace_function = {
411 .name = "FRAME_TRACE",
412 .write = frame_trace_helper,
415 static int unload_module(void)
417 return ast_custom_function_unregister(&frame_trace_function);
420 static int load_module(void)
422 int res = ast_custom_function_register(&frame_trace_function);
423 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
426 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Frame Trace for internal ast_frame debugging.");