/****************************************************************************** * (c)2008-2012 Broadcom Corporation * * This program is the proprietary software of Broadcom Corporation and/or its licensors, * and may only be used, duplicated, modified or distributed pursuant to the terms and * conditions of a separate, written license agreement executed between you and Broadcom * (an "Authorized License"). Except as set forth in an Authorized License, Broadcom grants * no license (express or implied), right to use, or waiver of any kind with respect to the * Software, and Broadcom expressly reserves all rights in and to the Software and all * intellectual property rights therein. IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU * HAVE NO RIGHT TO USE THIS SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY * NOTIFY BROADCOM AND DISCONTINUE ALL USE OF THE SOFTWARE. * * Except as expressly set forth in the Authorized License, * * 1. This program, including its structure, sequence and organization, constitutes the valuable trade * secrets of Broadcom, and you shall use all reasonable efforts to protect the confidentiality thereof, * and to use this information only in connection with your use of Broadcom integrated circuit products. * * 2. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" * AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS OR * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO * THE SOFTWARE. BROADCOM SPECIFICALLY DISCLAIMS ANY AND ALL IMPLIED WARRANTIES * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, * LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION * OR CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT OF * USE OR PERFORMANCE OF THE SOFTWARE. * * 3. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR ITS * LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, INDIRECT, OR * EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY RELATING TO YOUR * USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF * THE POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT * ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE * LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF ESSENTIAL PURPOSE OF * ANY LIMITED REMEDY. * * $brcm_Workfile: message.c $ * $brcm_Revision: 9 $ * $brcm_Date: 2/13/12 5:23p $ * * Module Description: * * Revision History: * * $brcm_Log: /nexus/utils/message.c $ * * 9 2/13/12 5:23p erickson * SW7335-1216: add overflow callback, fix playback pid channel close * * 8 2/8/12 9:51a erickson * SW7335-1216: do actual PMT filtering * * 7 1/24/12 1:10p erickson * SW7425-2199: fix shutdown * * 6 7/11/11 1:13p erickson * SW7335-1216: remove #if 0 around WaitForEvent * * 5 7/1/11 12:50p erickson * SW7335-1216: use default maxContiguousMessageSize so example works with * SW filtering * * 4 12/8/09 3:00p gmohile * SW7408-1 : Add support for platforms with no frontend * * 3 9/18/09 4:30p erickson * SWDEPRECATED-3717: use NEXUS_MessageSettings.bufferSize * * 2 5/4/09 4:25p erickson * PR54807: add basic psip support * * 1 5/4/09 2:38p erickson * PR54807: added message util * * 7 4/13/09 3:31p erickson * PR54058: remove NEXUS_PidChannelSettings.requireMessageBuffer * * 6 3/16/09 3:01p erickson * PR35457: use app-allocated buffer as recommended in Nexus_Usage.pdf * * 5 7/18/08 11:31a jgarrett * PR 44953: Removing -Wstrict-prototypes warnings * * 4 4/23/08 12:18p erickson * PR35457: parse the PAT to make it a more complete example * * 3 2/25/08 9:34a jgarrett * PR 39435: Fixing includes * * 2 1/24/08 12:52p vsilyaev * PR 38682: fixed return type of NEXUS_Platform_GetStreamerInputBand * *****************************************************************************/ #include "nexus_platform.h" #include "nexus_pid_channel.h" #include "nexus_parser_band.h" #include "nexus_message.h" #include "nexus_playback.h" #include "nexus_file.h" #include "bstd.h" #include "bkni.h" #include #include #include BDBG_MODULE(message); static struct { NEXUS_PlaybackHandle playback; NEXUS_PlaypumpHandle playpump; NEXUS_FilePlayHandle file; NEXUS_ParserBand parserBand; BKNI_EventHandle event; } app; #define MAX_MESSAGE_FILTERS 100 struct message_t { unsigned short pid; NEXUS_MessageHandle message; NEXUS_PidChannelHandle pidChannel; } g_message[MAX_MESSAGE_FILTERS]; static void message_callback(void *context, int param) { BSTD_UNUSED(param); BKNI_SetEvent((BKNI_EventHandle)context); } static void overflow_callback(void *context, int param) { BSTD_UNUSED(context); BDBG_WRN(("overflow on pid %#x", g_message[param].pid)); } struct message_opts_t { unsigned short pid; unsigned buffer_size; const char *filename; bool hex; bool ascii; bool psi; bool psip; NEXUS_TransportType transportType; NEXUS_TransportTimestampType tsTimestampType; NEXUS_PlaybackLoopMode endOfStreamAction; }; void cmdline_default_message_opts(struct message_opts_t *opts) { BKNI_Memset(opts, 0, sizeof(*opts)); opts->buffer_size = 4096; opts->transportType = NEXUS_TransportType_eTs; opts->tsTimestampType = NEXUS_TransportTimestampType_eNone; opts->endOfStreamAction = NEXUS_PlaybackLoopMode_eLoop; } static int add_message_filter(const struct message_opts_t *opts, unsigned short pid, const NEXUS_MessageFilter *pFilter) { unsigned i; NEXUS_MessageSettings settings; NEXUS_MessageStartSettings startSettings; NEXUS_Error rc; struct message_t *msg = NULL; for (i=0;ipid = pid; if (app.playback) { msg->pidChannel = NEXUS_Playback_OpenPidChannel(app.playback, pid, NULL); } else { msg->pidChannel = NEXUS_PidChannel_Open(app.parserBand, pid, NULL); } BDBG_ASSERT(msg->pidChannel); NEXUS_Message_GetDefaultSettings(&settings); settings.dataReady.callback = message_callback; settings.dataReady.context = app.event; settings.overflow.callback = overflow_callback; settings.overflow.param = i; /* use default settings.maxContiguousMessageSize */ settings.bufferSize = opts->buffer_size; msg->message = NEXUS_Message_Open(&settings); NEXUS_Message_GetDefaultStartSettings(msg->message, &startSettings); startSettings.pidChannel = msg->pidChannel; if (pFilter) { startSettings.filter = *pFilter; } rc = NEXUS_Message_Start(msg->message, &startSettings); BDBG_ASSERT(!rc); return 0; } static void remove_message_filter(unsigned i) { NEXUS_MessageHandle msg = g_message[i].message; if (!msg) return; g_message[i].message = NULL; NEXUS_Message_Close(msg); } void print_usage(void) { printf( "Usage: nexus message [options] [pvrfile]\n" "-buffer_size X message capture buffer size in bytes\n" "-pid X PID to filter (default is 0)\n" "-hex print contents of all messages in hex\n" "-ascii print contents of all messages in ascii (useful for PSIP)\n" "-psi parse PSI tables\n" "-psip parse PSIP tables\n" "\n" "If pvrfile not given, streamer input will be parsed\n" ); } int cmdline_parse_message_opts(int argc, char **argv, struct message_opts_t *opts) { int curarg = 1; while (curarg < argc) { if (!strcmp("--help", argv[curarg]) || !strcmp("-h", argv[curarg]) || !strcmp("-?", argv[curarg])) { print_usage(); return 1; } else if (!strcmp("-buffer_size", argv[curarg]) && curarg+1 < argc) { opts->buffer_size = atoi(argv[++curarg]); } else if (!strcmp("-pid", argv[curarg]) && curarg+1 < argc) { opts->pid = strtoul(argv[++curarg], NULL, 0); } else if (!strcmp("-hex", argv[curarg])) { opts->hex = true; } else if (!strcmp("-ascii", argv[curarg])) { opts->ascii = true; } else if (!strcmp("-psi", argv[curarg])) { opts->psi = true; opts->pid = 0; /* PID for PAT */ } else if (!strcmp("-psip", argv[curarg])) { opts->psip = true; opts->pid = 0x1ffb; /* PID for MGT */ } else if (!opts->filename) { opts->filename = argv[curarg]; } else { printf("Unknown param: %s\n", argv[curarg]); return -1; } curarg++; } return 0; } int main(int argc, char **argv) { struct message_opts_t opts; NEXUS_Error rc; unsigned count; unsigned i; NEXUS_PlatformSettings platformSettings; cmdline_default_message_opts(&opts); if (cmdline_parse_message_opts(argc, argv, &opts)) { return 0; } NEXUS_Platform_GetDefaultSettings(&platformSettings); platformSettings.openFrontend = false; rc = NEXUS_Platform_Init(NULL); if (rc) return -1; if (opts.filename) { NEXUS_PlaybackSettings playbackSettings; app.playpump = NEXUS_Playpump_Open(0, NULL); BDBG_ASSERT(app.playpump); app.playback = NEXUS_Playback_Create(); BDBG_ASSERT(app.playback); app.file = NEXUS_FilePlay_OpenPosix(opts.filename, NULL); if (!app.file) { fprintf(stderr, "can't open file:%s\n", opts.filename); return -1; } NEXUS_Playback_GetSettings(app.playback, &playbackSettings); playbackSettings.playpump = app.playpump; playbackSettings.playpumpSettings.transportType = opts.transportType; playbackSettings.playpumpSettings.timestamp.type = opts.tsTimestampType; playbackSettings.endOfStreamAction = opts.endOfStreamAction; rc = NEXUS_Playback_SetSettings(app.playback, &playbackSettings); BDBG_ASSERT(!rc); rc = NEXUS_Playback_Start(app.playback, app.file, NULL); BDBG_ASSERT(!rc); } #if NEXUS_NUM_PARSER_BANDS else { NEXUS_InputBand inputBand; NEXUS_ParserBandSettings parserBandSettings; app.parserBand = NEXUS_ParserBand_e0; NEXUS_Platform_GetStreamerInputBand(0, &inputBand); NEXUS_ParserBand_GetSettings(app.parserBand, &parserBandSettings); parserBandSettings.sourceType = NEXUS_ParserBandSourceType_eInputBand; parserBandSettings.sourceTypeSettings.inputBand = inputBand; rc = NEXUS_ParserBand_SetSettings(app.parserBand, &parserBandSettings); BDBG_ASSERT(!rc); } #endif BKNI_CreateEvent(&app.event); BDBG_WRN(("starting message filter")); add_message_filter(&opts, opts.pid, NULL); /* Read the PAT a few times */ for (count=0;;count++) { const uint8_t *buffer; size_t size; unsigned programNum, message_length; if (count == MAX_MESSAGE_FILTERS) { count = 0; } if (!g_message[count].message) { continue; } rc = NEXUS_Message_GetBuffer(g_message[count].message, (const void **)&buffer, &size); BDBG_ASSERT(!rc); if (!size) { BERR_Code rc = BKNI_WaitForEvent(app.event, 5 * 1000); /* wait 5 seconds */ if (rc == NEXUS_TIMEOUT) { BDBG_WRN(("timeout")); rc = -1; break; } BDBG_ASSERT(!rc); continue; } #define TS_READ_16( BUF ) ((uint16_t)((BUF)[0]<<8|(BUF)[1])) #define TS_PSI_GET_SECTION_LENGTH( BUF ) (uint16_t)(TS_READ_16( &(BUF)[1] ) & 0x0FFF) /* We should always get whole PAT's because maxContiguousMessageSize is 4K */ message_length = TS_PSI_GET_SECTION_LENGTH(buffer) + 3; BDBG_ASSERT(size >= (size_t)message_length); if (opts.psi) { if (buffer[0] == 0) { /* Program Association Table */ printf("PAT: size=%d\n", message_length); for (programNum=0;programNum<(unsigned)(TS_PSI_GET_SECTION_LENGTH(buffer)-7)/4;programNum++) { unsigned byteOffset = 8 + programNum*4; unsigned program = TS_READ_16( &buffer[byteOffset] ); unsigned short pid = (uint16_t)(TS_READ_16( &buffer[byteOffset+2] ) & 0x1FFF); NEXUS_MessageFilter filter; printf(" program %#x: pid %#x\n", program, pid); NEXUS_Message_GetDefaultFilter(&filter); /* these fields must match */ filter.mask[0] = 0x00; filter.mask[2] = 0x00; filter.mask[3] = 0x00; /* they must match these values */ filter.coefficient[0] = 0x02; filter.coefficient[2] = (program & 0xFF00) >> 8; filter.coefficient[3] = program & 0xFF; add_message_filter(&opts, pid, &filter); } remove_message_filter(count); } else if (buffer[0] == 1) { /* Conditional Access Table */ printf("CAT: size=%d\n", message_length); } else if (buffer[0] == 2) { unsigned i, byteOffset; /* Program Table */ printf("PMT: pid=%#x size=%d\n", g_message[count].pid, message_length); #if 0 for (i=0;i