func_frame_trace.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2010, Digium, Inc.
00005  *
00006  * David Vossel <dvossel@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Trace internal ast_frames on a channel.
00022  *
00023  * \author David Vossel <dvossel@digium.com>
00024  *
00025  * \ingroup functions
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>extended</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 424473 $")
00035 
00036 #include "asterisk/module.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/framehook.h"
00040 
00041 /*** DOCUMENTATION
00042    <function name="FRAME_TRACE" language="en_US">
00043       <synopsis>
00044          View internal ast_frames as they are read and written on a channel.
00045       </synopsis>
00046       <syntax>
00047          <parameter name="filter list type" required="true">
00048             <para>A filter can be applied to the trace to limit what frames are viewed.  This
00049             filter can either be a <literal>white</literal> or <literal>black</literal> list
00050             of frame types.  When no filter type is present, <literal>white</literal> is
00051             used.  If no arguments are provided at all, all frames will be output.
00052             </para>
00053 
00054             <para>Below are the different types of frames that can be filtered.</para>
00055             <enumlist>
00056                <enum name = "DTMF_BEGIN" />
00057                <enum name = "DTMF_END" />
00058                <enum name = "VOICE" />
00059                <enum name = "VIDEO" />
00060                <enum name = "CONTROL" />
00061                <enum name = "NULL" />
00062                <enum name = "IAX" />
00063                <enum name = "TEXT" />
00064                <enum name = "IMAGE" />
00065                <enum name = "HTML" />
00066                <enum name = "CNG" />
00067                <enum name = "MODEM" />
00068             </enumlist>
00069          </parameter>
00070       </syntax>
00071       <description>
00072          <para>Examples:</para>
00073          <para>exten => 1,1,Set(FRAME_TRACE(white)=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
00074          <para>exten => 1,1,Set(FRAME_TRACE()=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
00075          <para>exten => 1,1,Set(FRAME_TRACE(black)=DTMF_BEGIN,DTMF_END); view everything except DTMF frames. </para>
00076       </description>
00077    </function>
00078  ***/
00079 
00080 static void print_frame(struct ast_frame *frame);
00081 static struct {
00082    enum ast_frame_type type;
00083    const char *str;
00084 } frametype2str[] = {
00085    { AST_FRAME_DTMF_BEGIN,   "DTMF_BEGIN" },
00086    { AST_FRAME_DTMF_END,   "DTMF_END" },
00087    { AST_FRAME_VOICE,   "VOICE" },
00088    { AST_FRAME_VIDEO,   "VIDEO" },
00089    { AST_FRAME_CONTROL,   "CONTROL" },
00090    { AST_FRAME_NULL,   "NULL" },
00091    { AST_FRAME_IAX,   "IAX" },
00092    { AST_FRAME_TEXT,   "TEXT" },
00093    { AST_FRAME_IMAGE,   "IMAGE" },
00094    { AST_FRAME_HTML,   "HTML" },
00095    { AST_FRAME_CNG,   "CNG" },
00096    { AST_FRAME_MODEM,   "MODEM" },
00097 };
00098 
00099 struct frame_trace_data {
00100    int list_type; /* 0 = white, 1 = black */
00101    int values[ARRAY_LEN(frametype2str)];
00102 };
00103 
00104 static void datastore_destroy_cb(void *data) {
00105    ast_free(data);
00106 }
00107 
00108 static const struct ast_datastore_info frame_trace_datastore = {
00109    .type = "frametrace",
00110    .destroy = datastore_destroy_cb
00111 };
00112 
00113 static void hook_destroy_cb(void *framedata)
00114 {
00115    ast_free(framedata);
00116 }
00117 
00118 static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
00119 {
00120    int i;
00121    int show_frame = 0;
00122    struct frame_trace_data *framedata = data;
00123    if (!frame) {
00124       return frame;
00125    }
00126 
00127    if ((event != AST_FRAMEHOOK_EVENT_WRITE) && (event != AST_FRAMEHOOK_EVENT_READ)) {
00128       return frame;
00129    }
00130 
00131    for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
00132       if (frame->frametype == frametype2str[i].type) {
00133          if ((framedata->list_type == 0) && (framedata->values[i])) { /* white list */
00134             show_frame = 1;
00135          } else if ((framedata->list_type == 1) && (!framedata->values[i])){ /* black list */
00136             show_frame = 1;
00137          }
00138          break;
00139       }
00140    }
00141 
00142    if (show_frame) {
00143       ast_verbose("%s on Channel %s\n", event == AST_FRAMEHOOK_EVENT_READ ? "<--Read" : "--> Write", ast_channel_name(chan));
00144       print_frame(frame);
00145    }
00146    return frame;
00147 }
00148 
00149 static int frame_trace_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00150 {
00151    struct frame_trace_data *framedata;
00152    struct ast_datastore *datastore = NULL;
00153    struct ast_framehook_interface interface = {
00154       .version = AST_FRAMEHOOK_INTERFACE_VERSION,
00155       .event_cb = hook_event_cb,
00156       .destroy_cb = hook_destroy_cb,
00157    };
00158    int i = 0;
00159 
00160    if (!chan) {
00161       ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
00162       return -1;
00163    }
00164 
00165    if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
00166       return 0;
00167    }
00168 
00169    interface.data = framedata;
00170 
00171    if (!strcasecmp(data, "black")) {
00172       framedata->list_type = 1;
00173    }
00174    for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
00175       if (strcasestr(value, frametype2str[i].str)) {
00176          framedata->values[i] = 1;
00177       }
00178    }
00179 
00180    ast_channel_lock(chan);
00181    i = ast_framehook_attach(chan, &interface);
00182    if (i >= 0) {
00183       int *id;
00184       if ((datastore = ast_channel_datastore_find(chan, &frame_trace_datastore, NULL))) {
00185          id = datastore->data;
00186          ast_framehook_detach(chan, *id);
00187          ast_channel_datastore_remove(chan, datastore);
00188          ast_datastore_free(datastore);
00189       }
00190 
00191       if (!(datastore = ast_datastore_alloc(&frame_trace_datastore, NULL))) {
00192          ast_framehook_detach(chan, i);
00193          ast_channel_unlock(chan);
00194          return 0;
00195       }
00196 
00197       if (!(id = ast_calloc(1, sizeof(int)))) {
00198          ast_datastore_free(datastore);
00199          ast_framehook_detach(chan, i);
00200          ast_channel_unlock(chan);
00201          return 0;
00202       }
00203 
00204       *id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
00205       datastore->data = id;
00206       ast_channel_datastore_add(chan, datastore);
00207    }
00208    ast_channel_unlock(chan);
00209 
00210    return 0;
00211 }
00212 
00213 static void print_frame(struct ast_frame *frame)
00214 {
00215    switch (frame->frametype) {
00216    case AST_FRAME_DTMF_END:
00217       ast_verbose("FrameType: DTMF END\n");
00218       ast_verbose("Digit: 0x%02X '%c'\n", (unsigned)frame->subclass.integer,
00219          frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
00220       break;
00221    case AST_FRAME_VOICE:
00222       ast_verbose("FrameType: VOICE\n");
00223       ast_verbose("Codec: %s\n", ast_format_get_name(frame->subclass.format));
00224       ast_verbose("MS: %ld\n", frame->len);
00225       ast_verbose("Samples: %d\n", frame->samples);
00226       ast_verbose("Bytes: %d\n", frame->datalen);
00227       break;
00228    case AST_FRAME_VIDEO:
00229       ast_verbose("FrameType: VIDEO\n");
00230       ast_verbose("Codec: %s\n", ast_format_get_name(frame->subclass.format));
00231       ast_verbose("MS: %ld\n", frame->len);
00232       ast_verbose("Samples: %d\n", frame->samples);
00233       ast_verbose("Bytes: %d\n", frame->datalen);
00234       break;
00235    case AST_FRAME_CONTROL:
00236       ast_verbose("FrameType: CONTROL\n");
00237       switch ((enum ast_control_frame_type) frame->subclass.integer) {
00238       case AST_CONTROL_HANGUP:
00239          ast_verbose("SubClass: HANGUP\n");
00240          break;
00241       case AST_CONTROL_RING:
00242          ast_verbose("SubClass: RING\n");
00243          break;
00244       case AST_CONTROL_RINGING:
00245          ast_verbose("SubClass: RINGING\n");
00246          break;
00247       case AST_CONTROL_ANSWER:
00248          ast_verbose("SubClass: ANSWER\n");
00249          break;
00250       case AST_CONTROL_BUSY:
00251          ast_verbose("SubClass: BUSY\n");
00252          break;
00253       case AST_CONTROL_TAKEOFFHOOK:
00254          ast_verbose("SubClass: TAKEOFFHOOK\n");
00255          break;
00256       case AST_CONTROL_OFFHOOK:
00257          ast_verbose("SubClass: OFFHOOK\n");
00258          break;
00259       case AST_CONTROL_CONGESTION:
00260          ast_verbose("SubClass: CONGESTION\n");
00261          break;
00262       case AST_CONTROL_FLASH:
00263          ast_verbose("SubClass: FLASH\n");
00264          break;
00265       case AST_CONTROL_WINK:
00266          ast_verbose("SubClass: WINK\n");
00267          break;
00268       case AST_CONTROL_OPTION:
00269          ast_verbose("SubClass: OPTION\n");
00270          break;
00271       case AST_CONTROL_RADIO_KEY:
00272          ast_verbose("SubClass: RADIO KEY\n");
00273          break;
00274       case AST_CONTROL_RADIO_UNKEY:
00275          ast_verbose("SubClass: RADIO UNKEY\n");
00276          break;
00277       case AST_CONTROL_PROGRESS:
00278          ast_verbose("SubClass: PROGRESS\n");
00279          break;
00280       case AST_CONTROL_PROCEEDING:
00281          ast_verbose("SubClass: PROCEEDING\n");
00282          break;
00283       case AST_CONTROL_HOLD:
00284          ast_verbose("SubClass: HOLD\n");
00285          break;
00286       case AST_CONTROL_UNHOLD:
00287          ast_verbose("SubClass: UNHOLD\n");
00288          break;
00289       case AST_CONTROL_VIDUPDATE:
00290          ast_verbose("SubClass: VIDUPDATE\n");
00291          break;
00292       case _XXX_AST_CONTROL_T38:
00293          ast_verbose("SubClass: XXX T38\n");
00294          break;
00295       case AST_CONTROL_SRCUPDATE:
00296          ast_verbose("SubClass: SRCUPDATE\n");
00297          break;
00298       case AST_CONTROL_TRANSFER:
00299          ast_verbose("SubClass: TRANSFER\n");
00300          break;
00301       case AST_CONTROL_CONNECTED_LINE:
00302          ast_verbose("SubClass: CONNECTED LINE\n");
00303          break;
00304       case AST_CONTROL_REDIRECTING:
00305          ast_verbose("SubClass: REDIRECTING\n");
00306          break;
00307       case AST_CONTROL_T38_PARAMETERS:
00308          ast_verbose("SubClass: T38 PARAMETERS\n");
00309          break;
00310       case AST_CONTROL_CC:
00311          ast_verbose("SubClass: CC\n");
00312          break;
00313       case AST_CONTROL_SRCCHANGE:
00314          ast_verbose("SubClass: SRCCHANGE\n");
00315          break;
00316       case AST_CONTROL_READ_ACTION:
00317          ast_verbose("SubClass: READ ACTION\n");
00318          break;
00319       case AST_CONTROL_AOC:
00320          ast_verbose("SubClass: AOC\n");
00321          break;
00322       case AST_CONTROL_MCID:
00323          ast_verbose("SubClass: MCID\n");
00324          break;
00325       case AST_CONTROL_INCOMPLETE:
00326          ast_verbose("SubClass: INCOMPLETE\n");
00327          break;
00328       case AST_CONTROL_END_OF_Q:
00329          ast_verbose("SubClass: END_OF_Q\n");
00330          break;
00331       case AST_CONTROL_UPDATE_RTP_PEER:
00332          ast_verbose("SubClass: UPDATE_RTP_PEER\n");
00333          break;
00334       case AST_CONTROL_PVT_CAUSE_CODE:
00335          ast_verbose("SubClass: PVT_CAUSE_CODE\n");
00336          break;
00337       case AST_CONTROL_MASQUERADE_NOTIFY:
00338          /* Should never happen. */
00339          ast_assert(0);
00340          break;
00341       case AST_CONTROL_STREAM_STOP:
00342          ast_verbose("SubClass: STREAM_STOP\n");
00343          break;
00344       case AST_CONTROL_STREAM_SUSPEND:
00345          ast_verbose("SubClass: STREAM_SUSPEND\n");
00346          break;
00347       case AST_CONTROL_STREAM_RESTART:
00348          ast_verbose("SubClass: STREAM_RESTART\n");
00349          break;
00350       case AST_CONTROL_STREAM_REVERSE:
00351          ast_verbose("SubClass: STREAM_REVERSE\n");
00352          break;
00353       case AST_CONTROL_STREAM_FORWARD:
00354          ast_verbose("SubClass: STREAM_FORWARD\n");
00355          break;
00356       case AST_CONTROL_RECORD_CANCEL:
00357          ast_verbose("SubClass: RECORD_CANCEL\n");
00358          break;
00359       case AST_CONTROL_RECORD_STOP:
00360          ast_verbose("SubClass: RECORD_STOP\n");
00361          break;
00362       case AST_CONTROL_RECORD_SUSPEND:
00363          ast_verbose("SubClass: RECORD_SUSPEND\n");
00364          break;
00365       case AST_CONTROL_RECORD_MUTE:
00366          ast_verbose("SubClass: RECORD_MUTE\n");
00367          break;
00368       }
00369 
00370       if (frame->subclass.integer == -1) {
00371          ast_verbose("SubClass: %d\n", frame->subclass.integer);
00372       }
00373       ast_verbose("Bytes: %d\n", frame->datalen);
00374       break;
00375    case AST_FRAME_NULL:
00376       ast_verbose("FrameType: NULL\n");
00377       break;
00378    case AST_FRAME_IAX:
00379       ast_verbose("FrameType: IAX\n");
00380       break;
00381    case AST_FRAME_TEXT:
00382       ast_verbose("FrameType: TXT\n");
00383       break;
00384    case AST_FRAME_IMAGE:
00385       ast_verbose("FrameType: IMAGE\n");
00386       break;
00387    case AST_FRAME_HTML:
00388       ast_verbose("FrameType: HTML\n");
00389       break;
00390    case AST_FRAME_CNG:
00391       ast_verbose("FrameType: CNG\n");
00392       break;
00393    case AST_FRAME_MODEM:
00394       ast_verbose("FrameType: MODEM\n");
00395       break;
00396    case AST_FRAME_DTMF_BEGIN:
00397       ast_verbose("FrameType: DTMF BEGIN\n");
00398       ast_verbose("Digit: 0x%02X '%c'\n", (unsigned)frame->subclass.integer,
00399          frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
00400       break;
00401    case AST_FRAME_BRIDGE_ACTION:
00402       ast_verbose("FrameType: Bridge\n");
00403       ast_verbose("SubClass: %d\n", frame->subclass.integer);
00404       break;
00405    case AST_FRAME_BRIDGE_ACTION_SYNC:
00406       ast_verbose("Frametype: Synchronous Bridge\n");
00407       ast_verbose("Subclass: %d\n", frame->subclass.integer);
00408       break;
00409    }
00410 
00411    ast_verbose("Src: %s\n", ast_strlen_zero(frame->src) ? "NOT PRESENT" : frame->src);
00412    ast_verbose("\n");
00413 }
00414 
00415 static struct ast_custom_function frame_trace_function = {
00416    .name = "FRAME_TRACE",
00417    .write = frame_trace_helper,
00418 };
00419 
00420 static int unload_module(void)
00421 {
00422    return ast_custom_function_unregister(&frame_trace_function);
00423 }
00424 
00425 static int load_module(void)
00426 {
00427    int res = ast_custom_function_register(&frame_trace_function);
00428    return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00429 }
00430 
00431 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Frame Trace for internal ast_frame debugging.");
00432 
00433 

Generated on Thu Apr 16 06:27:35 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6