/*************************************************************************** * Copyright (c) 2003-2006, Broadcom Corporation * All Rights Reserved * Confidential Property of Broadcom Corporation * * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. * * $brcm_Workfile: $ * $brcm_Revision: $ * $brcm_Date: $ * * Module Description: * * Revision History: * * $brcm_Log: $ * ***************************************************************************/ #include "chan_mgr.h" #include "bstd.h" #include "bavc.h" #include "ts_psi.h" #include "ts_pat.h" #include "ts_pmt.h" #include "ts_scte_18.h" #include "bapp.h" #include "psip_ett.h" #include "bsettop_smessage.h" #include "image_recv.h" #include "ca_parser.h" #include "bos_task_priorities.h" #if defined(ENABLE_UMESSAGE) #include "umessage.h" #include "cmode.h" #endif #include "bapp_util.h" #include "ch_map.h" #ifdef FOR_DOLBY_CERTIFICATION #include "bchp_hifidac_ctrl0.h" #include "projinc.h" #endif #ifdef HAS_VBI #include "pes_scte_127.h" #endif #include "ram_header.h" BDBG_MODULE(chan_mgr); /* Register software module with debug interface */ /* PR32033 fix, for ETT 10.5- 10.10, need more buffer and timeout */ #define CHM_MIN(x,y) ((x < y) ? x : y) #ifdef BCM_DEBUG static const char *s_chm_cmd_name[] = { "eCHM_CANCEL","eCHM_TUNE","eCHM_INFO","eCHM_STOP","eCHM_CHECK_SIGNAL","eCHM_HUNT","eCHM_HUNTING","eCHM_PENDING_INIT","eCHM_DOWNLOAD","eCHM_SAP", "eCHM_DSUB"}; #endif #define CURR_CH 0 #define NEXT_CH 1 #define PREV_CH 2 #define CHM_INT_STK_SIZE 1024 /* in words */ #define CHM_CHECK_SIGNAL_FREQ 1000 #define CHM_CHECK_CHMAP_FREQ 5000 #define CHM_CHECK_PROG_FREQ 1000 #define PAT_BUF_LEN (1024 + 188) #define CAT_BUF_LEN (1024 + 188) #define PMT_BUF_LEN (1024 + 188) #define STT_BUF_LEN (256 + 188) #define NIT_BUF_LEN (4400 + 188) #define NTT_BUF_LEN (4400 + 188) #define SVCT_BUF_LEN (4400 + 188) #define EAS_BUF_LEN (1024 + 188) #define EMM_BUF_LEN (1024 + 188) #define CVT_BUF_LEN (1024 + 188) #ifdef CONFIG_DVB #define SDT_BUF_LEN (4400 + 188) #define EIT_BUF_LEN (4400 + 188) #define TDT_BUF_LEN (1024 + 188) #endif #define CHM_IDLE_TIMEOUT 30 #define CAT_TIMEOUT 3000 #define PAT_TIMEOUT 800 #define PMT_TIMEOUT 800 #define STT_TIMEOUT 1200 #define EAS_TIMEOUT 1200 /* These timeouts are for testing only and need to be adjusted to match actual system cycle times (in ms) */ #define NIT_TIMEOUT 20000 #define NTT_TIMEOUT 20000 #define SVCT_TIMEOUT 35000 #define HUNT_TIMEOUT 35000 #define CHM_UPDATING_DELAY 4000 #ifdef CONFIG_DVB /* need adjustment */ #define SDT_TIMEOUT 20000 #endif #define CHM_DEF_VCT_ID CONFIG_DEF_VCT_ID #define CHM_DEF_NETWORK_PID CONFIG_NETWORK_PID #define CHM_MUTEX_TIMEOUT 10 #define MAX_TIME_SLOTS 4 #define MGT_CYCLE_TIMEOUT 1 /* in seconds */ #define SCTE_18_ALERT_PRIORITY 0 /* all priority */ /* frequency vector is encoded in 0.25MHz increments */ #define CHM_VECTOR_MULTIPLIER (25 * 10000) #define CHM_MIN(x,y) ((x < y) ? x : y) #ifndef toupper #define toupper(x) (x&0xDF) #endif typedef struct iso_lang_map_t { char iso_lang_code[3]; bapp_lang_t lang; }iso_lang_map_t; typedef enum chm_err_t { eCHM_ERR_OK = 0, eCHM_ERR_FAILED = -1, eCHM_ERR_TIMEOUT = -2, eCHM_ERR_CANCELED = -3, eCHM_ERR_NO_PAT = -4 }chm_err_t; const static iso_lang_map_t s_iso_lang_map[] = { { { 'e','n','g'},eLANG_ENGLISH}, { { 'f','r','a'},eLANG_FRENCH}, { { 'f','r','e'},eLANG_FRENCH}, { { 'e','s','l'},eLANG_SPANISH}, { { 's','p','a'},eLANG_SPANISH}, }; const static int s_iso_lang_map_num = sizeof(s_iso_lang_map)/sizeof(iso_lang_map_t); /* These files are in flash_install_.c and are assumed to be RAM only and * are therefore not access through dispatch table. */ extern void flash_install(uint32_t base, uint32_t offset, uint8_t * data, size_t len); extern void chip_reset(void); #define MAX_MAJOR_CH p_chm->num_freq static int chm_post_app_event(chm_mgr_t *p_chm, /* Channel manager reference */ chm_event_t *p_event /* Event to post to app queue */ ); static int chm_stop_decode(chm_mgr_t *p_chm); static int chm_tune_freq(chm_mgr_t *p_chm,unsigned int freq_hz,unsigned int freq_idx); static int chm_rotate_audio(chm_mgr_t *p_chm); static int chm_cvt_start(chm_mgr_t *p_chm, unsigned short network_pid); static void chm_cvt_stop(chm_mgr_t *p_chm); static int chm_stt_start(chm_mgr_t *p_chm, unsigned short network_pid); static void chm_stt_stop(chm_mgr_t *p_chm); static int chm_emm_start(chm_mgr_t *p_chm, unsigned short emm_pid); static void chm_emm_stop(chm_mgr_t *p_chm); static void chm_cache_pids(chm_mgr_t *p_chm); static int chm_cat_start(chm_mgr_t *p_chm); static void chm_cat_stop(chm_mgr_t *p_chm); #ifdef CONFIG_DVB static void chm_dvb_nit_cb(nit_t *nit, void *data); static void chm_dvb_sdt_cb(sdt_t *sdt, void *data); static void chm_dvb_nit_stop(chm_mgr_t *p_chm); static int chm_dvb_nit_start(chm_mgr_t *p_chm, bband_t band); static void chm_dvb_sdt_stop(chm_mgr_t *p_chm); static int chm_dvb_sdt_start(chm_mgr_t *p_chm, bband_t band); static void chm_dvb_tdt_stop(chm_mgr_t *p_chm); static int chm_dvb_tdt_start(chm_mgr_t *p_chm, bband_t band); static void chm_dvb_eit_stop(chm_mgr_t *p_chm); static int chm_dvb_eit_start(chm_mgr_t *p_chm, bband_t band); #endif #ifdef HAS_VBI static int chm_scte127_start(chm_mgr_t *p_chm, uint16_t scte127_pid); #endif #ifdef CONFIG_DVB_SUBTITLE static void chm_subtitle_stop(chm_mgr_t *p_chm); static int chm_subtitle_start(chm_mgr_t *p_chm, uint16_t dsub_pid); static int chm_rotate_dsub(chm_mgr_t *p_chm); #endif bresult chm_write_software(struct image_t * image); static bool chm_check_sc(chm_mgr_t *p_chm, bband_t band, unsigned short pid); static void chm_stop_stream(chm_mgr_t *p_chm, chm_stream_t *p_stream); //#define DBG_DUMP #ifdef DBG_DUMP static void DBG_HEXDUMP(unsigned char* ptr, unsigned long len) { unsigned long i; for (i=0; ilocal_offset = 3600 * g_xds_time_offset; p_chm->local_time_source = eTS_XDS; p_chm->local_dst_obs = g_xds_dst; } if (p_chm->local_time_source == eTS_NONE) return eCHM_ERR_FAILED; *local_offset = p_chm->local_offset; *dst = ((p_chm->stt.daylight_savings.DS_status != 0) && p_chm->local_dst_obs) ? true : false; BDBG_MSG(("STT system_time = %u\n",p_chm->stt.system_time)); BDBG_MSG(("STT GPS_UTC_offset = %u\n",p_chm->stt.GPS_UTC_offset)); BDBG_MSG(("STT DS_status = %d\n",p_chm->stt.daylight_savings.DS_status)); BDBG_ERR(("Timesource %d, local_offset = %d, dst\n",p_chm->local_time_source,p_chm->local_offset,p_chm->local_dst_obs)); if (*dst) { *utc_secs += 3600; } return 0; #else #ifdef CONFIG_DVB *utc_secs = dvb_get_cur_time(); *local_offset = 0; /* TODO:: */ *dst = false; /* TODO */ if (*utc_secs) return eCHM_ERR_OK; else #endif return eCHM_ERR_FAILED; #endif } /* Summary: Get the current utc time in seconds. Return non-zero on failure. */ static void chm_send_status(chm_mgr_t *p_chm,unsigned int freq, bool locked) { bapp_t *p_app = (bapp_t*)p_chm->p_app; btuner_status status; chm_signal_event_t *p_sig = &(p_chm->signal_evt); memset(&(p_chm->signal_evt),0,sizeof(chm_signal_event_t)); p_sig->type = eCHM_EVT_SIGNAL; /* check power level only if we are in tuner status screen */ if ((p_app->screen_id == eSCREEN_ANTENNA) || (p_app->screen_id == eSCREEN_TUNER_STATUS) || (p_app->screen_id == eSCREEN_BANNER) || (p_app->screen_id == eSCREEN_BANNER_SMALL)) { status.get_power = true; } else { status.get_power = false; } if (!locked) { p_sig->freq_hz = freq; } else if (btuner_get_status(p_chm->tuner,&status) == b_ok) { /* Send message to app */ p_sig->freq_hz = status.freq; p_sig->SNR = status.snr; //p_sig->qam_b_mode = status.mode; p_sig->power = status.power; p_sig->lock = status.lock; } chm_post_app_event(p_chm,(chm_event_t*)&(p_chm->signal_evt)); } /* Summary: Check for a valid language code for the current language settings. Returns the bapp_lang_t number or < 0 if not a valid language. */ static int chm_valid_lang(chm_mgr_t *p_chm, /* Channel manager reference */ char *p_code /* Three byte iso 639-2 language code */ ) { bapp_lang_t lang = iso_639_to_lang(p_code); BDBG_MSG(("ISO 639-2 language not found %c%c%c\n",p_code[0],p_code[1],p_code[2])); return (lang == eLANG_MAX) ? eCHM_ERR_FAILED : (int)lang; } /* Summary: SCTE-65 Parser Callback. */ void chm_mms_cb(NIT_MMS_RECORD *p_MMS, void *data) { chm_mgr_t *p_chm = (chm_mgr_t *)data; mms_t mms; if (p_MMS->transmission_system != ITU_T_ANNEX_B_TX_SYS) { BDBG_MSG(("%s Invalid Transmission System %d\n",__FUNCTION__,p_MMS->transmission_system)); //return;//Allow all the transmission_system type } BDBG_MSG(("%s(%d,%d,%d,%d)\n",__FUNCTION__,p_MMS->idx,p_MMS->modulation_format,p_MMS->inner_coding_mode,p_MMS->symbol_rate)); memset(&mms,0,sizeof(mms_t)); mms.idx = p_MMS->idx; mms.code_rate = p_MMS->inner_coding_mode; mms.modulation = p_MMS->modulation_format; p_chm->mms_flag = true; switch (p_MMS->modulation_format) { case MOD_QAM64: mms.modulation = eQAM_64; break; case MOD_QAM256: mms.modulation = eQAM_256; break; case MOD_QAM1024: mms.modulation = eQAM_256; break; default: mms.modulation = eQAM_Scan; break; } mms.symbol_rate = p_MMS->symbol_rate; ch_map_add_mms(&(p_chm->ch_map),&mms); } /* Summary: SCTE-65 Parser Callback. */ void chm_cds_cb(unsigned int freq_khz, int idx, void *data) { chm_mgr_t *p_chm = (chm_mgr_t *)data; freq_t freq; BDBG_MSG(("%s(%d,%d)\n",__FUNCTION__,freq_khz,idx)); memset(&freq,0,sizeof(freq_t)); freq.idx = idx; freq.freq_khz = freq_khz; ch_map_add_freq(&(p_chm->ch_map),&freq); } /* Summary: SCTE-65 Parser Callback. */ void chm_sns_cb(SI_NTT_SNS_t *sns, void *data) { chm_mgr_t *p_chm = (chm_mgr_t *)data; st_t st; unsigned char code[3]; int len; bapp_lang_t lang = eLANG_ENGLISH; code[0] = (unsigned char)((sns->iso639 >> 16) & 0xFF); code[1] = (unsigned char)((sns->iso639 >> 8) & 0xFF); code[2] = (unsigned char)((sns->iso639 >> 0) & 0xFF); lang = (bapp_lang_t)chm_valid_lang(p_chm,(char*)code); if ((int)lang < 0) { BDBG_MSG(("INVALID ISO639 Language Code = %c%c%c\n",code[0],code[1],code[2])); } else { BDBG_MSG(("bapp_lang_t = %d(%c%c%c)\n",lang,code[0],code[1],code[2])); } memset(&st,0,sizeof(st_t)); st.source_id = sns->source_id; if (sns->mtt[0] != 0) { BDBG_WRN(("Only latin character set supported 0x%02x\n",sns->mtt[0])); return; } len = sns->mtt[1]; if (len >= MAX_VCH_LEN) { len = MAX_VCH_LEN - 1; } BDBG_MSG(("name_length = %d\n",len)); memcpy(st.name,&sns->mtt[2],len); st.name[len] = 0; /* null terminate string */ BDBG_MSG(("name = %s\n",st.name)); ch_map_add_st(&(p_chm->ch_map),&st); } /* Summary: SCTE-65 Parser Callback. */ void chm_vcm_cb(SI_SVCT_VCM_CHANNEL *channel, void *data) { chm_mgr_t *p_chm = (chm_mgr_t *)data; vch_t vch; bapp_t *p_app = (bapp_t*)p_chm->p_app; memset(&vch,0,sizeof(vch)); if (channel->ChanPropUnion.mpeg_prop.prog_num == 0x0000) { BDBG_WRN(("%s analog program or invalid program number\n",__FUNCTION__)); return; } if (channel->vct_id != p_app->settings.VCT_ID) { BDBG_WRN(("%s SVCT VCT_ID != current VCT_ID (0x%04x != 0x%04x)\n",__FUNCTION__,channel->vct_id,p_app->settings.VCT_ID)); if (p_app->settings.VCT_ID != 0xFFFF) { DBG_HEXDUMP(p_chm->svct_buf,16); return; } } if (channel->channel_type != NORMAL_CHAN) { BDBG_MSG(("%s hidden - %d\n",__FUNCTION__,channel->channel_type)); } vch.ch_num = channel->channum1; vch.freq_idx = channel->CDS_reference; vch.mms_idx = channel->ChanPropUnion.mpeg_prop.MMS_reference; vch.program_num = channel->ChanPropUnion.mpeg_prop.prog_num; vch.source_id = channel->source_ID; vch.vchflags = channel->channel_type; ch_map_add_vch(&(p_chm->ch_map),&vch); } /* Summary: Check for cancel, returns true if a cancel occured. */ #ifdef BCM_DEBUG static int chm_check_cancel_ex(chm_mgr_t *p_chm ,const char* func, int line) #define chm_check_cancel(chm) chm_check_cancel_ex(chm,__FUNCTION__,__LINE__) #else static int chm_check_cancel(chm_mgr_t *p_chm ) #endif { chm_cmd_event_t *p_cmd; if (p_chm->cmd == eCHM_CANCEL) return 1; p_cmd = (chm_cmd_event_t*)bos_pend_event(p_chm->queue,0); if (!p_cmd) return 0; if (p_cmd->cmd_id == eCHM_CANCEL) { p_chm->cmd = eCHM_CANCEL; BDBG_WRN(("Command canceled(%s:%d)...\n",func,line)); /* Send message to app */ p_chm->chm_evt.type = eCHM_EVT_CANCEL; p_chm->chm_evt.id = 0; chm_post_app_event(p_chm,&p_chm->chm_evt); } else { if (p_cmd->cmd_id == eCHM_SAP) { chm_rotate_audio(p_chm); } #ifdef CONFIG_DVB_SUBTITLE else if (p_cmd->cmd_id == eCHM_DSUB) { chm_rotate_dsub(p_chm); } #endif else { BDBG_WRN(("Throwing away command %d(%s:%d)...\n", p_chm->cmd ,func,line)); } } return(p_chm->cmd == eCHM_CANCEL) ? 1 : 0; } static void * chm_smessage_callback(void *context, size_t data_size) { chm_mgr_t *p_chm = (chm_mgr_t *)context; /* ignore return value */ bos_post_event(p_chm->gen_queue, (b_event_t*)data_size); return NULL; } static int chm_getmessage(chm_mgr_t *p_chm, /* Channel manager reference */ void *buf, /* buffer to copy section into */ unsigned int *len, /* [in] max size of buffer, [out] section size copied into buffer */ unsigned int timeout /* timeout in milliseconds */ ) { bresult bres; size_t msg_size; int result = eCHM_ERR_FAILED; smessage_stream_params_t params; smessage_stream_params_init(¶ms, p_chm->smsg); params.band = p_chm->msg_params.band; params.pid = p_chm->msg_params.pid; params.filter = p_chm->msg_params.filter; params.buffer = buf; params.buffer_size = *len; params.crc_disabled = p_chm->msg_params.crc_disabled; params.data_ready_callback = chm_smessage_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; params.priv = (void *)4; BDBG_MSG(("pid %x fil %x tim %u len %u", params.pid, params.filter.coef[0], timeout, *len)); bres = smessage_start(¶ms, p_chm->smsg); if (b_ok != bres) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); goto ExitFunc; } msg_size = (size_t) bos_pend_event(p_chm->gen_queue, timeout); if (0 == msg_size) { result = eCHM_ERR_TIMEOUT; } else { *len = msg_size; result = eCHM_ERR_OK; } bres = smessage_stop(p_chm->smsg); if (b_ok != bres) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); result = eCHM_ERR_FAILED; goto ExitFunc; } ExitFunc: return result; } /* Summary: Add a chanell to the channel map. Description: Add the channel to the channel map in sorted order. */ static unsigned short s_channel_num = 100; static void chm_add_channel(chm_mgr_t *p_chm, /* Channel manager reference */ vch_t *p_vch ) { freq_t freq; st_t st; mms_t mms; btuner_status status; freq.idx = (unsigned char)p_vch->freq_idx; freq.freq_khz = p_chm->freq_table[p_vch->freq_idx]; ch_map_add_freq(&(p_chm->ch_map),&freq); memset(&mms,0,sizeof(mms)); mms.idx = (unsigned char)p_vch->freq_idx; status.get_power = false; if (btuner_get_status(p_chm->tuner,&status) == b_ok) mms.modulation = status.mode; else mms.modulation = eQAM_Scan; ch_map_add_mms(&(p_chm->ch_map),&mms); p_vch->ch_num = (unsigned short)s_channel_num++; p_vch->source_id = (unsigned short)p_vch->ch_num; p_vch->mms_idx = (unsigned char)mms.idx; ch_map_add_vch(&(p_chm->ch_map),p_vch); st.source_id = p_vch->source_id; snprintf(st.name,MAX_VCH_LEN,"Ch - %d.%d",p_vch->freq_idx + 2,p_vch->program_num); ch_map_add_st(&(p_chm->ch_map),&st); } /* Summary: Stop monitoring.. */ void chm_nit_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->nit_msg) { if (b_ok != smessage_stop(p_chm->nit_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); return; } smessage_close(p_chm->nit_msg); p_chm->nit_msg = NULL; } } /* Summary: Stop monitoring.. */ void chm_ntt_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->ntt_msg) { if (b_ok != smessage_stop(p_chm->ntt_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); return; } smessage_close(p_chm->ntt_msg); p_chm->ntt_msg = NULL; } } /* Summary: Stop monitoring.. */ void chm_svct_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->svct_msg) { if (b_ok != smessage_stop(p_chm->svct_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); return; } smessage_close(p_chm->svct_msg); p_chm->svct_msg = NULL; } } #ifdef HAS_VBI /* Summary: Stop SCTE 127 monitoring.. */ static void chm_scte127_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->scte127_msg) { if (b_ok != smessage_stop(p_chm->scte127_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); return; } smessage_close(p_chm->scte127_msg); p_chm->scte127_msg = NULL; } } #endif /* Summary: Stop EAS monitoring.. */ static void chm_eas_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->eas_msg) { if (b_ok != smessage_stop(p_chm->eas_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); return; } smessage_close(p_chm->eas_msg); p_chm->eas_msg = NULL; p_chm->eas_timer_started = false; } } /* Summary: Stop CVT monitoring.. */ static void chm_cvt_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->cvt_msg) { if (b_ok != smessage_stop(p_chm->cvt_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); return; } smessage_close(p_chm->cvt_msg); p_chm->cvt_msg = NULL; } } /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: message_callback * DESCRIPTION: Callback function normally called when a complete message has * been received and passed the filter criteria. ******************************************************************************/ void * chm_smessage_nit_callback(void * context, size_t size) { chm_mgr_t * p_chm = (chm_mgr_t*)context; SI_RET_CODE rc; unsigned int crc32; ch_map_t * p_ch_map = bapp_cur_ch_map(p_chm->p_app); crc32 = chm_section_crc(p_chm->nit_buf,size); p_chm->mms_flag = false; if ((rc = SI_NIT_parse(p_chm->nit_buf)) != SI_SUCCESS) { BDBG_WRN(("NIT parse error %d\n",rc)); } else { if (p_chm->mms_flag) p_chm->mms_cnt++; else p_chm->cds_cnt++; } if ((p_chm->ch_map.freq_map.num_freq > 0) && SI_NIT_CDS_Complete() && ch_map_cmp_freq(&(p_chm->ch_map.freq_map),&(p_ch_map->freq_map))) { p_chm->cds_changed = true; BDBG_WRN(("Updated NIT-CDS\n")); } if ((p_chm->ch_map.mms_map.num_mms > 0) && SI_NIT_MMS_Complete() && ch_map_cmp_mms(&(p_chm->ch_map.mms_map),&(p_ch_map->mms_map))) { p_chm->mms_changed = true; BDBG_WRN(("Updated NIT-MMS\n")); } // DBG_HEXDUMP(p_chm->nit_buf,size); return(void*)(p_chm->nit_buf); } /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: message_callback * DESCRIPTION: Callback function normally called when a complete message has * been received and passed the filter criteria. ******************************************************************************/ void * chm_smessage_ntt_callback(void * context, size_t size) { chm_mgr_t * p_chm = (chm_mgr_t*)context; SI_RET_CODE rc; ch_map_t * p_ch_map = bapp_cur_ch_map(p_chm->p_app); BDBG_WRN(("#### NTT Event ####\n")); if ((rc = SI_NTT_parse(p_chm->ntt_buf)) != SI_SUCCESS) { BDBG_WRN(("NTT parse error %d\n",rc)); } else { p_chm->sns_cnt++; if ((p_chm->ch_map.st_map.num_st > 0) && SI_NTT_SNS_Complete() && ch_map_cmp_st(&(p_chm->ch_map.st_map),&(p_ch_map->st_map))) { p_chm->sns_changed = true; BDBG_WRN(("Updated NTT\n")); } } return(void*)p_chm->ntt_buf; } /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: message_callback * DESCRIPTION: Callback function normally called when a complete message has * been received and passed the filter criteria. ******************************************************************************/ void * chm_smessage_svct_callback(void * context, size_t size) { chm_mgr_t * p_chm = (chm_mgr_t*)context; SI_RET_CODE rc; unsigned int crc32; ch_map_t * p_ch_map = bapp_cur_ch_map(p_chm->p_app); crc32 = chm_section_crc(p_chm->svct_buf,size); BDBG_WRN(("#### SVCT Event ####\n")); if ((rc = SI_SVCT_parse(p_chm->svct_buf)) != SI_SUCCESS) { BDBG_WRN(("SVCT parse error %d\n",rc)); } else { p_chm->vcm_cnt++; if ((p_chm->ch_map.vch_map.num_vch > 0) && SI_SVCT_Complete() && ch_map_cmp_vch(&(p_chm->ch_map.vch_map),&(p_ch_map->vch_map))) { p_chm->vcm_changed = true; BDBG_WRN(("Updated SVCT\n")); } } return(void*)p_chm->svct_buf; } /* Summary: Start nit monitoring.. */ int chm_nit_start(chm_mgr_t *p_chm, unsigned short network_pid) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; chm_nit_stop(p_chm); p_chm->cds_changed = false; p_chm->mms_changed = false; p_chm->nit_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->nit_msg) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->nit_msg); params.band = p_chm->band; params.pid = (uint16_t)network_pid; params.filter.coef[0] = 0xc2; /* Table ID for NIT */ params.filter.mask[0] = 0x00; params.filter.excl[0] = 0xff; /* All PSIP tables have protocol_version which must be = 0 */ params.filter.coef[3] = 0x00; /* protocol_version */ params.filter.mask[3] = 0x00; params.filter.excl[3] = 0xff; params.buffer = p_chm->nit_buf; params.buffer_size = NIT_BUF_LEN; /* processing is done in callback */ params.data_ready_callback = chm_smessage_nit_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; /* Always reset the starting version numbers to work around cases where headend sends new map but does not change version */ SI_NIT_Init (&p_chm->cds_cb,&p_chm->mms_cb,0xFF,0xFF); if (b_ok != smessage_start(¶ms, p_chm->nit_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } /* Summary: Start ntt monitoring.. */ int chm_ntt_start(chm_mgr_t *p_chm, unsigned short network_pid) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; chm_ntt_stop(p_chm); p_chm->sns_changed = false; p_chm->ntt_msg = smessage_open(smessage_format_psi); BDBG_MSG(("%s:%d ntt_msg = 0x%08x\n",__FUNCTION__,__LINE__,p_chm->ntt_msg)); if (NULL == p_chm->ntt_msg) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->ntt_msg); params.band = p_chm->band; params.pid = (uint16_t)network_pid; params.filter.coef[0] = 0xc3; /* Table ID for NTT */ params.filter.mask[0] = 0x00; params.filter.excl[0] = 0xff; /* All PSIP tables have protocol_version which must be = 0 */ params.filter.coef[3] = 0x00; /* protocol_version */ params.filter.mask[3] = 0x00; params.filter.excl[3] = 0xff; params.buffer = p_chm->ntt_buf; params.buffer_size = NTT_BUF_LEN; /* processing is done in callback */ params.data_ready_callback = chm_smessage_ntt_callback; params.overflow = NULL; /* Always reset the starting version numbers to work around cases where headend sends new map but does not change version */ params.callback_context = (void *)p_chm; SI_NTT_Init (&p_chm->sns_cb,0xFF); if (b_ok != smessage_start(¶ms, p_chm->ntt_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } /* Summary: Start svct monitoring.. */ int chm_svct_start(chm_mgr_t *p_chm, unsigned short network_pid) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; bapp_t *p_app = (bapp_t*)p_chm->p_app; chm_svct_stop(p_chm); p_chm->vcm_changed = false; p_chm->svct_msg = smessage_open(smessage_format_psi); BDBG_MSG(("%s:%d svct_msg = 0x%08x\n",__FUNCTION__,__LINE__,p_chm->svct_msg)); if (NULL == p_chm->svct_msg) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->svct_msg); params.band = p_chm->band; params.pid = (uint16_t)network_pid; params.filter.coef[0] = 0xc4; /* Table ID for SVCT */ params.filter.mask[0] = 0x00; params.filter.excl[0] = 0xff; /* All PSIP tables have protocol_version which must be = 0 */ params.filter.coef[3] = 0x00; /* protocol_version */ params.filter.mask[3] = 0x00; params.filter.excl[3] = 0xff; params.filter.coef[4] = 0x00; /* protocol_version */ params.filter.mask[4] = 0xf0; params.filter.excl[4] = 0xff; if (p_app->settings.VCT_ID != 0xFFFF) { /* little endian!!!! */ params.filter.coef[5] = (unsigned char)((p_app->settings.VCT_ID >> 8) & 0xFF); params.filter.mask[5] = 0x00; params.filter.excl[5] = 0xff; params.filter.coef[6] = (unsigned char)(p_app->settings.VCT_ID & 0xFF); params.filter.mask[6] = 0x00; params.filter.excl[6] = 0xff; } params.buffer = p_chm->svct_buf; params.buffer_size = SVCT_BUF_LEN; /* processing is done in callback */ params.data_ready_callback = chm_smessage_svct_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; /* Always reset the starting version numbers to work around cases where headend sends new map but does not change version */ SI_SVCT_Init (&p_chm->vcm_cb,0xFF); if (b_ok != smessage_start(¶ms, p_chm->svct_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } /* Summary: Get the program info for the program. Description: Get the PMT for the program. Returns non-zero on failure. */ static int chm_get_program_pid(chm_mgr_t *p_chm, /* Channel manager reference */ bband_t band, unsigned short prog_number, unsigned short *PID ) { unsigned int section_size, pmt_section_size; TS_PMT_stream pmt; TS_PAT_program pat; int result = eCHM_ERR_FAILED; int idx,p_idx; *PID = 0xFFFF; smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = band; p_chm->msg_params.pid = (unsigned short)0; /* PAT */ section_size = PAT_BUF_LEN; if ((result = chm_getmessage(p_chm,p_chm->pat_buf,§ion_size, PAT_TIMEOUT)) != 0) { BDBG_WRN(("Error getting PAT 0x%04x = %d\n",prog_number, result)); return result; } if (TS_PAT_validate(p_chm->pat_buf, section_size) != true) { BDBG_WRN(("TS_PAT_validate failed 0x%04x\n",prog_number)); return eCHM_ERR_FAILED; } if (TS_PAT_getNumPrograms(p_chm->pat_buf) == 0) { BDBG_WRN(("TS_PAT_getNumPrograms = 0\n")); return eCHM_ERR_FAILED; } BDBG_WRN(("TS_PAT_getNumPrograms %d\n",TS_PAT_getNumPrograms(p_chm->pat_buf))); for (idx = 0; idx < TS_PAT_getNumPrograms(p_chm->pat_buf); idx++) { if (TS_PAT_getProgram( p_chm->pat_buf, section_size, idx, &pat ) != b_ok) { BDBG_WRN(("TS_PAT_validate failed 0x%04x\n",prog_number)); return eCHM_ERR_FAILED; } BDBG_WRN(("TS_PAT_getProgram(%d,0x%04x)\n", pat.program_number,pat.PID)); if (pat.program_number != prog_number) continue; smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = band; p_chm->msg_params.pid = (unsigned short)pat.PID; /* PMT */ p_chm->msg_params.filter.coef[0] = 0x2; p_chm->msg_params.filter.mask[0] = 0x00; p_chm->msg_params.filter.excl[0] = 0xff; pmt_section_size = PMT_BUF_LEN; if ((result = chm_getmessage(p_chm,p_chm->pmt_buf,&pmt_section_size, PMT_TIMEOUT)) != 0) { BDBG_ERR(("Error getting PAT 0x%04x\n",prog_number)); return result; } //p_chm->pmt_crc = chm_section_crc(p_chm->pmt_buf,pmt_section_size); for (p_idx = 0; p_idx < TS_PMT_getNumStreams(p_chm->pmt_buf,pmt_section_size); p_idx++) { if (TS_PMT_getStream( p_chm->pmt_buf, pmt_section_size, p_idx, &pmt ) != b_ok) { BDBG_WRN(("Failure processing PMT %d\n",p_idx)); return eCHM_ERR_FAILED; } if (pmt.stream_type == 0x0B) /* DSMCC */ { BDBG_WRN(("DSMCC PID[%d] = 0x%04x\n",p_idx,pmt.elementary_PID )); *PID = pmt.elementary_PID; return eCHM_ERR_OK; } else { /* We only support the above 3 types of stream */ BDBG_MSG(("Unsupported stream type[0x%02x]\n", pmt.stream_type )); } } } return result; } /* Summary: Get the program info for the program. Description: Get the PMT for the program. Returns non-zero on failure. */ static int chm_get_program(chm_mgr_t *p_chm, /* Channel manager reference */ vch_t *p_vch) { unsigned int section_size, pmt_section_size; TS_PMT_stream pmt; TS_PAT_program pat; TS_PSI_descriptor psi_desc; int result = 0; int idx,p_idx,desc_idx,num_prog; bapp_t *p_app = (bapp_t*)p_chm->p_app; unsigned int prog_ticks; bsettop_av_stream_type_t *stream_type; #ifdef CONFIG_DVB_SUBTITLE unsigned char dsub_lang[MAX_DSUB_PIDS][3]; unsigned short dsub_page[MAX_DSUB_PIDS]; unsigned char dsub_cnt, i; #endif BDBG_MSG(("%s:%d\n",__FUNCTION__,__LINE__)); prog_ticks = bos_getticks(); smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = p_chm->band; p_chm->msg_params.pid = (unsigned short)0; /* PAT */ section_size = PAT_BUF_LEN; if ((result = chm_getmessage(p_chm,p_chm->pat_buf,§ion_size, PAT_TIMEOUT)) != 0) { BDBG_WRN(("%s(%d): Error getting PAT 0x%04x = %d\n",__FUNCTION__,__LINE__,p_chm->vch.program_num, result)); return result; } p_chm->pat_crc = chm_section_crc(p_chm->pat_buf,section_size); p_chm->pat_cnt++; if (TS_PAT_validate(p_chm->pat_buf, section_size) != true) { BDBG_WRN(("TS_PAT_validate failed 0x%04x\n",p_chm->vch.program_num)); return eCHM_ERR_FAILED; } num_prog = TS_PAT_getNumPrograms(p_chm->pat_buf); if (num_prog == 0) { BDBG_WRN(("TS_PAT_getNumPrograms = 0\n")); return eCHM_ERR_FAILED; } BDBG_MSG(("TS_PAT_getNumPrograms %d\n",num_prog)); for (idx = 0; idx < num_prog; idx++) { if (TS_PAT_getProgram( p_chm->pat_buf, section_size, idx, &pat ) != b_ok) { BDBG_WRN(("TS_PAT_validate failed 0x%04x\n",idx)); return eCHM_ERR_FAILED; } BDBG_MSG(("TS_PAT_getProgram(%d,0x%04x)\n", pat.program_number,pat.PID)); /* Check for new network PID */ if (pat.program_number == 0) { if (pat.PID != p_app->settings.network_PID) { p_chm->network_pid = pat.PID; } } if (p_chm->vch.program_num != pat.program_number) continue; p_vch->num_audio = 0; p_vch->cur_audio = 0; p_vch->pcr_pid = 0; p_vch->video_pid = 0; p_vch->scte127_pid = 0; p_vch->audio_pid[0] = 0; #ifdef CONFIG_DVB_SUBTITLE p_vch->num_dsub = 0; p_vch->cur_dsub = 0; #endif smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = p_chm->band; p_chm->msg_params.pid = (unsigned short)pat.PID; /* PMT */ p_chm->msg_params.filter.coef[0] = 0x2; p_chm->msg_params.filter.mask[0] = 0x00; p_chm->msg_params.filter.excl[0] = 0xff; pmt_section_size = PMT_BUF_LEN; if ((result = chm_getmessage(p_chm,p_chm->pmt_buf,&pmt_section_size, PMT_TIMEOUT)) != 0) { BDBG_ERR(("%s(%d): Error getting PAT 0x%04x = %d\n",__FUNCTION__,__LINE__,p_chm->vch.program_num, result)); return result; } p_chm->pmt_crc = chm_section_crc(p_chm->pmt_buf,pmt_section_size); p_chm->pmt_cnt++; p_vch->pcr_pid = TS_PMT_getPcrPid(p_chm->pmt_buf,pmt_section_size); /* Get CA pid */ desc_idx = 0; p_vch->ca_pid = 0; while ((psi_desc = TS_PMT_getDescriptor(p_chm->pmt_buf, pmt_section_size,desc_idx++)) != NULL) { static ca_descriptor ca_desc; if (desc_idx > 0xFF) { BDBG_ERR(("PMT Desc limit exceeded %d\n",desc_idx)); break; } if (chm_check_cancel(p_chm)) { return eCHM_ERR_CANCELED; } if (psi_desc[0] != 9) continue; if (ca_parser(psi_desc, pmt_section_size, &ca_desc) == 0) continue; if (ca_desc.CA_system_ID == p_app->settings.CA_System_ID) { p_vch->ca_pid = ca_desc.CA_PID; BDBG_WRN(("CA PID = 0x%04x\n",p_vch->ca_pid)); p_app->settings.SCP_operating_mode = 0; if (ca_desc.CA_system_ID == 0x0E11) { /* Send message to app */ if ((ca_desc.priv_data[0] == 0x5) && (ca_desc.priv_data[1] == 0x1)) p_app->settings.SCP_operating_mode = ca_desc.priv_data[2]; } break; } else { p_chm->system_id_errors++; } } for (p_idx = 0; p_idx < TS_PMT_getNumStreams(p_chm->pmt_buf,pmt_section_size); p_idx++) { TS_PSI_descriptor psi_desc,lang_desc; int strm_idx; if (TS_PMT_getStream( p_chm->pmt_buf, pmt_section_size, p_idx, &pmt ) != b_ok) { BDBG_WRN(("Failure processing PMT %d\n",p_idx)); return eCHM_ERR_FAILED; } strm_idx = 0; lang_desc = NULL; /* default is not supported */ //p_vch->scte127_pid = 0; #ifdef CONFIG_DVB_SUBTITLE dsub_cnt = 0; #endif while ((psi_desc = TS_PMT_getStreamDescriptor(p_chm->pmt_buf,pmt_section_size, p_idx,strm_idx)) != NULL) { #ifdef CONFIG_DVB_SUBTITLE if (psi_desc[0] == TS_PSI_DT_DVB_Subtitling) { if ((psi_desc[5]<0x10) || (psi_desc[5]>0x23)) continue; /* not DVB subtitle */ /* could multiple subtitle program in same pid */ dsub_lang[dsub_cnt][0] = toupper(psi_desc[2]); dsub_lang[dsub_cnt][1] = toupper(psi_desc[3]); dsub_lang[dsub_cnt][2] = toupper(psi_desc[4]); dsub_page[dsub_cnt] = psi_desc[6]<<8|psi_desc[7]; dsub_cnt++; } #endif if (psi_desc[0] == TS_PSI_DT_ISO_639_Language) /* 0x0A ISO-639 Language descriptor */ { BDBG_MSG(("ISO-639 DESC\n" )); lang_desc = psi_desc; /* if get VBI desc alrady */ if (p_vch->scte127_pid) break; } if (0 == p_vch->scte127_pid) { if (TS_PSI_DT_DVB_VBI_Data == psi_desc[0]) { BDBG_MSG(("SCTE 127 supported, pid=0x%04x", pmt.elementary_PID)); p_vch->scte127_pid = pmt.elementary_PID; /* if get lang desc alrady */ if (lang_desc) break; } } strm_idx++; if (strm_idx > 0xFF) { BDBG_ERR(("PMT Stream Desc limit exceeded %d\n",strm_idx)); break; } if (chm_check_cancel(p_chm)) { return eCHM_ERR_CANCELED; } } if ((stream_type = bdecode_supported_video(pmt.stream_type)) != NULL) { BDBG_MSG(("Video PID[%d] = 0x%04x\n",p_idx,pmt.elementary_PID )); if (!p_vch->video_pid) { p_vch->video_pid = pmt.elementary_PID; p_vch->video_type = stream_type->codec_id; } } else if ((stream_type = bdecode_supported_audio(pmt.stream_type)) != NULL) { BDBG_MSG(("Audio PID[%d] = 0x%04x\n",p_vch->num_audio,pmt.elementary_PID )); if (!p_vch->audio_pid[p_vch->num_audio]) { p_vch->audio_pid[p_vch->num_audio] = pmt.elementary_PID; p_vch->audio_type[p_vch->num_audio] = stream_type->codec_id; if (lang_desc) { p_vch->audio_lang[p_vch->num_audio][0] = toupper(lang_desc[2]); p_vch->audio_lang[p_vch->num_audio][1] = toupper(lang_desc[3]); p_vch->audio_lang[p_vch->num_audio][2] = toupper(lang_desc[4]); } p_vch->num_audio++; } } #ifdef CONFIG_DVB_SUBTITLE else if (pmt.stream_type == PES_PRIVATE_STREAM_TYPE) { dsub_cnt = (dsub_cnt>MAX_DSUB_PIDS)?MAX_DSUB_PIDS:dsub_cnt; for (i=0; inum_dsub >= MAX_DSUB_PIDS) break; p_vch->dsub_pid[p_vch->num_dsub] = pmt.elementary_PID; p_vch->dsub_lang[p_vch->num_dsub][0] = dsub_lang[i][0]; p_vch->dsub_lang[p_vch->num_dsub][1] = dsub_lang[i][1]; p_vch->dsub_lang[p_vch->num_dsub][2] = dsub_lang[i][2]; p_vch->dsub_page[p_vch->num_dsub] = dsub_page[i]; BDBG_MSG(("DSUB PID [%d] = 0x%04x lang:%c%c%c, page:%d", p_vch->num_dsub, pmt.elementary_PID, dsub_lang[i][0], dsub_lang[i][1], dsub_lang[i][2], dsub_page[i])); p_vch->num_dsub++; } } #endif else { BDBG_MSG(("Unsupported stream type[0x%02x]\n", pmt.stream_type)); } } } prog_ticks = bos_getticks() - prog_ticks; p_chm->status_evt.type = eCHM_EVT_STATUS; p_chm->status_evt.id = eCHM_STATUS_SI_MS; p_chm->status_evt.ticks = TICKS_TO_MS(prog_ticks); chm_post_app_event(p_chm,&p_chm->status_evt); return result; } /* Summary: Scan for channels using PSI. Description: Scan for channels using PSI. Returns number of channels added. */ static int chm_scan_psi(chm_mgr_t *p_chm, /* Channel manager reference */ bband_t band, int freq_idx) { unsigned int section_size, pmt_section_size; TS_PMT_stream pmt; TS_PAT_program pat; int result = 0; int idx,p_idx,tresult, desc_idx; vch_t vch; TS_PSI_descriptor psi_desc; unsigned short ca_pid = 0; #ifdef CONFIG_DVB_SUBTITLE unsigned char dsub_lang[MAX_DSUB_PIDS][3]; unsigned short dsub_page[MAX_DSUB_PIDS]; unsigned char dsub_cnt, i; #endif memset(&vch,0,sizeof(vch)); /* Get the PAT */ smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = band; p_chm->msg_params.pid = (unsigned short)0; /* PAT */ section_size = PAT_BUF_LEN; if ((result = chm_getmessage(p_chm,p_chm->pat_buf,§ion_size, PAT_TIMEOUT * 10)) != 0) { BDBG_WRN(("%s Timeout getting PAT\n",__FUNCTION__)); return result; } p_chm->pat_crc = chm_section_crc(p_chm->pat_buf,section_size); if (TS_PAT_validate(p_chm->pat_buf, section_size) != true) { BDBG_WRN(("TS_PAT_validate failed\n")); return eCHM_ERR_FAILED; } BDBG_WRN(("%s:%d section_size = %d\n",__FUNCTION__,__LINE__,section_size)); BDBG_WRN(("Programs %d\n",TS_PAT_getNumPrograms(p_chm->pat_buf))); if (TS_PAT_getNumPrograms(p_chm->pat_buf) == 0) { BDBG_WRN(("%s TS_PAT_getNumPrograms = 0,section_size = %d\n",__FUNCTION__,section_size)); DBG_HEXDUMP(p_chm->pat_buf,section_size); return eCHM_ERR_FAILED; } for (idx = 0; idx < TS_PAT_getNumPrograms(p_chm->pat_buf); idx++) { if (chm_check_cancel(p_chm)) { return eCHM_ERR_CANCELED; } memset(&vch,0,sizeof(vch)); section_size = PAT_BUF_LEN; if (TS_PAT_getProgram( p_chm->pat_buf, section_size, idx, &pat ) != b_ok) { BDBG_WRN(("TS_PAT_validate failed 0x%04x\n",pat.PID)); return eCHM_ERR_FAILED; } //p_chm->pat_crc = chm_section_crc(p_chm->pat_buf,section_size); smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = band; p_chm->msg_params.pid = (unsigned short)pat.PID; /* PMT */ p_chm->msg_params.filter.coef[0] = 0x2; p_chm->msg_params.filter.mask[0] = 0x00; p_chm->msg_params.filter.excl[0] = 0xff; pmt_section_size = PMT_BUF_LEN; if ((tresult = chm_getmessage(p_chm,p_chm->pmt_buf,&pmt_section_size, PMT_TIMEOUT)) != 0) { BDBG_WRN(("Failed getting PMT 0x%04x\n",pat.PID)); continue; } p_chm->pmt_crc = chm_section_crc(p_chm->pmt_buf,pmt_section_size); vch.freq_idx = freq_idx; vch.pcr_pid = TS_PMT_getPcrPid(p_chm->pmt_buf,pmt_section_size); ca_pid = 0; vch.program_num = pat.program_number; /* Get CA pid for cable */ desc_idx = 0; #ifdef CONFIG_DVB_SUBTITLE vch.num_dsub = 0; vch.cur_dsub = 0; int strm_idx; #endif while ((psi_desc = TS_PMT_getDescriptor(p_chm->pmt_buf, pmt_section_size,desc_idx++)) != NULL) { if (desc_idx > 0xFF) { BDBG_ERR(("PMT Desc limit exceeded %d\n",desc_idx)); break; } if (chm_check_cancel(p_chm)) { return eCHM_ERR_CANCELED; } if (psi_desc[0] == 9) { ca_pid = (((unsigned short)(psi_desc[4] & 0x1F)) << 8) | (unsigned short)psi_desc[5]; BDBG_MSG(("CA PID = 0x%04x\n",ca_pid)); break; } } if (ca_pid != 0x0000) { BDBG_MSG(("Program has CA PID 0x%02x check for scrambling control bit.\n",ca_pid)); vch.ca_pid = ca_pid; } BDBG_MSG(("Streams = %d\n",TS_PMT_getNumStreams(p_chm->pmt_buf,pmt_section_size))); for (p_idx = 0; p_idx < TS_PMT_getNumStreams(p_chm->pmt_buf,pmt_section_size); p_idx++) { bsettop_av_stream_type_t *stream_type; if (TS_PMT_getStream( p_chm->pmt_buf, pmt_section_size, p_idx, &pmt ) != b_ok) { BDBG_WRN(("Failure processing PMT %d\n",p_idx)); return eCHM_ERR_FAILED; } #ifdef CONFIG_DVB_SUBTITLE dsub_cnt = 0; strm_idx = 0; while ((psi_desc = TS_PMT_getStreamDescriptor(p_chm->pmt_buf,pmt_section_size, p_idx,strm_idx)) != NULL) { if (psi_desc[0] == TS_PSI_DT_DVB_Subtitling) { if ((psi_desc[5]<0x10) || (psi_desc[5]>0x23)) continue; /* not DVB subtitle */ dsub_lang[dsub_cnt][0] = toupper(psi_desc[2]); dsub_lang[dsub_cnt][1] = toupper(psi_desc[3]); dsub_lang[dsub_cnt][2] = toupper(psi_desc[4]); dsub_page[dsub_cnt] = psi_desc[6]<<8|psi_desc[7]; dsub_cnt++; } strm_idx++; if (strm_idx > 0xFF) { BDBG_ERR(("PMT Stream Desc limit exceeded %d\n",strm_idx)); break; } if (chm_check_cancel(p_chm)) { return eCHM_ERR_CANCELED; } } #endif if ((stream_type = bdecode_supported_video(pmt.stream_type)) != NULL) { BDBG_MSG(("Video PID[%d] = 0x%04x\n",p_idx,pmt.elementary_PID )); if (!vch.video_pid) { vch.video_pid = pmt.elementary_PID; vch.video_type = stream_type->codec_id; } } else if ((stream_type = bdecode_supported_audio(pmt.stream_type)) != NULL) { BDBG_MSG(("Audio PID[%d] = 0x%04x\n",vch.num_audio,pmt.elementary_PID )); if (!vch.audio_pid[vch.num_audio]) { vch.audio_pid[vch.num_audio] = pmt.elementary_PID; vch.audio_type[vch.num_audio] = stream_type->codec_id; vch.num_audio++; } } #ifdef CONFIG_DVB_SUBTITLE else if (pmt.stream_type == PES_PRIVATE_STREAM_TYPE) { dsub_cnt = (dsub_cnt > MAX_DSUB_PIDS)?MAX_DSUB_PIDS:dsub_cnt; for (i=0; i=MAX_DSUB_PIDS) break; BDBG_WRN(("%s: private PID[%d] = 0x%04x page:%d [%c%c%c]", __FUNCTION__, vch.num_dsub, pmt.elementary_PID, dsub_page[i], dsub_lang[i][0], dsub_lang[i][1], dsub_lang[i][2])); vch.dsub_pid[vch.num_dsub] = pmt.elementary_PID; vch.dsub_lang[vch.num_dsub][0] = dsub_lang[i][0]; vch.dsub_lang[vch.num_dsub][1] = dsub_lang[i][1]; vch.dsub_lang[vch.num_dsub][2] = dsub_lang[i][2]; vch.dsub_page[vch.num_dsub] = dsub_page[i]; vch.num_dsub++; } } #endif } if ((vch.video_pid) || (vch.audio_pid[0])) { bool clear = true;; unsigned short pid; if (vch.video_pid) pid = (unsigned short)vch.video_pid; else pid = (unsigned short)vch.audio_pid[0]; /* correct pid in case video pid carries too few frames */ if(vch.pcr_pid == vch.audio_pid[0]) pid = (unsigned short)vch.audio_pid[0]; if (vch.ca_pid != 0x0000) clear = chm_check_sc(p_chm, band, pid); else clear = 1; if (clear) { bapp_t *p_app = (bapp_t *)p_chm->p_app; p_chm->chm_pat_evt.type = eCHM_EVT_STATUS; p_chm->chm_pat_evt.id = eCHM_STATUS_PAT; p_chm->chm_pat_evt.ticks = 0; chm_post_app_event(p_chm,&p_chm->chm_pat_evt); chm_add_channel(p_chm,&vch); /* in the chm_scan_psi, it will post STATUS_PAT. but sometimes the event is missed.. don't know why.. */ if (p_app->screen_id == eSCREEN_CH_SCAN_PROGRESS) { bos_sleep(100); } result += 1; } } } return result; } /* Summary: Generic tuner cancel callback. */ void chm_tune_cancel_callback(void *context) { chm_mgr_t *p_chm = ( chm_mgr_t *)context; if (chm_check_cancel(p_chm)) { BDBG_WRN(("### CANCEL TUNE ###\n")); p_chm->tuner_params.cancel = true; } } /* Summary: Stop the STT. return non-zero on failure */ static void chm_stt_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->stt_msg) { BDBG_WRN(("%s\n",__FUNCTION__)); if (b_ok != smessage_stop(p_chm->stt_msg)) { BDBG_ERR(("%s:%d",__FUNCTION__, __LINE__)); return; } smessage_close(p_chm->stt_msg); p_chm->stt_msg = NULL; } } /* Summary: Scan for channels to create a channel map. Description: Scan for all available channels. */ static void chm_stream_close(chm_mgr_t *p_chm /* Channel manager reference */ ) { } /* Summary: Callback function normally called when a complete message has been received and passed the filter criteria. */ bool found_system = false; static void * chm_smessage_cat_callback(void * context, size_t section_size) { chm_mgr_t * p_chm = (chm_mgr_t*)context; unsigned int len = section_size; unsigned char *ptr; ca_descriptor ca_desc; found_system = false; p_chm->cat_cnt++; if ((ptr = cat_parser(p_chm->cat_buf, &len)) != NULL) { while ((len > 9) && ptr) { /* 6 + 4 for CRC */ if (ca_parser(ptr, len, &ca_desc) != 0) { ptr += ca_desc.priv_length + 6; len -= (ca_desc.priv_length + 6); BDBG_MSG(("%s CA_System_ID = 0x%04x, EMM_PID = 0x%04x.\n",__FUNCTION__, ca_desc.CA_system_ID,ca_desc.CA_PID)); if ((ca_desc.CA_system_ID == 0x4749) || (ca_desc.CA_system_ID == 0x0E11)) { /* Send message to app */ chm_ca_event_t *p_ca_evt = (chm_ca_event_t*)&p_chm->ca_evt; p_ca_evt->type = eCHM_EVT_CA; p_ca_evt->CA_System_ID = ca_desc.CA_system_ID; p_ca_evt->EMM_Provider_ID = 0; p_ca_evt->SCP_operating_mode = 0; if (ca_desc.CA_system_ID == 0x4749) p_ca_evt->EMM_Provider_ID = (unsigned short)ca_desc.priv_data[0] | ((unsigned short)ca_desc.priv_data[1] << 8); else if ((ca_desc.CA_system_ID == 0x0E11) && (ca_desc.priv_data[0] == 0x5) && (ca_desc.priv_data[1] == 0x1)) p_ca_evt->SCP_operating_mode = ca_desc.priv_data[2]; p_ca_evt->EMM_PID = ca_desc.CA_PID; p_ca_evt->freq_khz = p_chm->last_freq_hz; chm_post_app_event(p_chm,(chm_event_t*)p_ca_evt); found_system = true; } else { p_chm->system_id_errors++; BDBG_MSG(("INVALID CA SYSTEM 0x%04x\n",ca_desc.CA_system_ID)); } } else { BDBG_MSG(("%s ca_parse failed.\n",__FUNCTION__)); break; } } } else { BDBG_MSG(("%s Timeout getting CAT\n",__FUNCTION__)); } return p_chm->cat_buf; } /* Summary: Start CAT monitoring.. */ static int chm_cat_start(chm_mgr_t *p_chm) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; chm_cat_stop(p_chm); p_chm->cat_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->cat_msg) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->cat_msg); params.band = p_chm->band; params.pid = 1; params.buffer = p_chm->cat_buf; params.buffer_size = CAT_BUF_LEN; params.data_ready_callback = chm_smessage_cat_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; if (b_ok != smessage_start(¶ms, p_chm->cat_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } /* Summary: Stop CAT monitoring.. */ static void chm_cat_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->cat_msg) { if (b_ok != smessage_stop(p_chm->cat_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); return; } smessage_close(p_chm->cat_msg); p_chm->cat_msg = NULL; } } /* Summary: Scan for the CAT/CA descriptor. Description: Scan for the CAT/CA descriptor. Returns non-zero on error. */ static int chm_scan_cat(chm_mgr_t *p_chm, /* Channel manager reference */ bband_t band, unsigned int freq_khz) { unsigned int section_size; int result = eCHM_ERR_FAILED; /* Get the VCT */ smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = band; p_chm->msg_params.pid = (unsigned short)1; /* CAT */ section_size = CAT_BUF_LEN; if ((result = chm_getmessage(p_chm,p_chm->cat_buf,§ion_size, CAT_TIMEOUT)) != 0) { BDBG_WRN(("%s Timeout getting CAT\n",__FUNCTION__)); return result; } chm_smessage_cat_callback(p_chm,section_size); result = (found_system) ? eCHM_ERR_OK : eCHM_ERR_FAILED; return result; } #if 0 /* Summary: Scan for the CAT/CA descriptor. Description: Scan for the CAT/CA descriptor. Returns non-zero on error. */ static int chm_scan_cat(chm_mgr_t *p_chm, /* Channel manager reference */ bband_t band, unsigned int freq_khz) { unsigned int section_size,len; unsigned char *ptr; static ca_descriptor ca_desc; int result = eCHM_ERR_FAILED; bapp_t *p_app = (bapp_t*)p_chm->p_app; /* Get the VCT */ smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = band; p_chm->msg_params.pid = (unsigned short)1; /* CAT */ section_size = CAT_BUF_LEN; if ((result = chm_getmessage(p_chm,p_chm->cat_buf,§ion_size, CAT_TIMEOUT)) != 0) { BDBG_WRN(("%s Timeout getting CAT\n",__FUNCTION__)); return result; } p_chm->cat_cnt++; if ((ptr = cat_parser(p_chm->cat_buf, &len)) != NULL) { while ((len > 9) && ptr) { /* 6 + 4 for CRC */ if (ca_parser(ptr, len, &ca_desc)) { ptr += ca_desc.priv_length + 6; len -= (ca_desc.priv_length + 6); BDBG_WRN(("%s CA_System_ID = 0x%04x, EMM_PID = 0x%04x.\n",__FUNCTION__, ca_desc.CA_system_ID,ca_desc.CA_PID)); if (freq_khz == 0) { if (ca_desc.CA_system_ID != p_app->settings.CA_System_ID) { p_chm->system_id_errors++; } } else { if ((ca_desc.CA_system_ID == 0x4749) || (ca_desc.CA_system_ID == 0x0E11)) { /* Send message to app */ chm_ca_event_t *p_ca_evt = (chm_ca_event_t*)&p_chm->ca_evt; p_ca_evt->type = eCHM_EVT_CA; p_ca_evt->CA_System_ID = ca_desc.CA_system_ID; p_ca_evt->EMM_Provider_ID = 0; p_ca_evt->SCP_operating_mode = 0; if (ca_desc.CA_system_ID == 0x4749) p_ca_evt->EMM_Provider_ID = (unsigned short)ca_desc.priv_data[0] | ((unsigned short)ca_desc.priv_data[1] << 8); else if ((ca_desc.CA_system_ID == 0x0E11) && (ca_desc.priv_data[0] == 0x5) && (ca_desc.priv_data[1] == 0x1)) p_ca_evt->SCP_operating_mode = ca_desc.priv_data[2]; p_ca_evt->EMM_PID = ca_desc.CA_PID; p_ca_evt->freq_khz = freq_khz; chm_post_app_event(p_chm,(chm_event_t*)p_ca_evt); result = eCHM_ERR_OK; } else { p_chm->system_id_errors++; BDBG_WRN(("INVALID CA SYSTEM 0x%04x\n",ca_desc.CA_system_ID)); } } } else { BDBG_WRN(("%s ca_parse failed.\n",__FUNCTION__)); result = eCHM_ERR_FAILED; break; } } } else { BDBG_WRN(("%s Timeout getting CAT\n",__FUNCTION__)); } return result; } #endif /* Summary: Scan for the network PID. Description: Scan for the network PID in pat PMT with program id 0. Returns the network PID. */ static int chm_scan_network(chm_mgr_t *p_chm, /* Channel manager reference */ bband_t band, unsigned short *network_PID) { unsigned int section_size; TS_PAT_program pat; int result = eCHM_ERR_FAILED; int idx; *network_PID = CHM_DEF_NETWORK_PID; /* Default PID value for this network */ /* Get the VCT */ smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = band; p_chm->msg_params.pid = (unsigned short)0; /* PAT */ section_size = PAT_BUF_LEN; if ((result = chm_getmessage(p_chm,p_chm->pat_buf,§ion_size, PAT_TIMEOUT)) != 0) { BDBG_WRN(("%s Timeout getting PAT\n",__FUNCTION__)); return result; } if (TS_PAT_validate(p_chm->pat_buf, section_size) != true) { BDBG_WRN(("TS_PAT_validate failed\n")); return eCHM_ERR_FAILED; } p_chm->pat_cnt++; for (idx = 0; idx < TS_PAT_getNumPrograms(p_chm->pat_buf); idx++) { if (chm_check_cancel(p_chm)) { return eCHM_ERR_CANCELED; } section_size = PAT_BUF_LEN; if (TS_PAT_getProgram( p_chm->pat_buf, section_size, idx, &pat ) != b_ok) { BDBG_WRN(("TS_PAT_validate failed 0x%04x\n",pat.PID)); return eCHM_ERR_FAILED; } p_chm->pmt_cnt++; if (pat.program_number == 0 ) { *network_PID = pat.PID; /* Send message to app */ p_chm->chm_network_evt.type = eCHM_EVT_NETWORK; p_chm->chm_network_evt.id = *network_PID; chm_post_app_event(p_chm,&p_chm->chm_network_evt); } } return result; } /* Summary: Return the hunt mode progress in percent. */ int chm_get_hunt_progress(chm_mgr_t *p_chm) { int total; if (p_chm->num_freq == 0) { bapp_freq_table_get(p_chm->fe_type,(const unsigned int **)&(p_chm->freq_table),&(p_chm->num_freq)); } if (p_chm->num_freq == 0) { BDBG_WRN(("%s:%d p_chm->num_freq = %d\n",__FUNCTION__,__LINE__,p_chm->num_freq)); return 0; } total = p_chm->num_freq; if (p_chm->cmd == eCHM_HUNTING) { if ((p_chm->hunt_cnt + 1) % 2) total *= 2; } return (p_chm->select_idx * 100)/total; } /* Summary: Scan for channels to create a channel map. Description: Scan for all available channels. */ static void chm_scan(chm_mgr_t *p_chm /* Channel manager reference */ ) { unsigned int freq; int num; bapp_freq_table_get(p_chm->fe_type,(const unsigned int **)&(p_chm->freq_table),&(p_chm->num_freq)); for (p_chm->select_idx = 0; p_chm->select_idx < p_chm->num_freq; p_chm->select_idx++) { freq = (unsigned int)(p_chm->freq_table[p_chm->select_idx]); freq *= 1000; /* convert to Hz */ BDBG_WRN(("Scan[%d] freq = %d\n",p_chm->select_idx,freq)); /* Check to see if scan was canceled */ if (chm_check_cancel(p_chm)) break; /* output progress event id will be % done */ p_chm->chm_pat_evt.type = eCHM_EVT_STATUS; p_chm->chm_pat_evt.id = eCHM_STATUS_NOPAT; p_chm->chm_pat_evt.ticks = (unsigned int)(((p_chm->select_idx+1)*100)/p_chm->num_freq); chm_post_app_event(p_chm,&p_chm->chm_pat_evt); /* Try tuning */ btuner_params_init(&p_chm->tuner_params, p_chm->tuner); p_chm->tuner_params.cancel_callback = chm_tune_cancel_callback; p_chm->tuner_params.cancel_callback_context = p_chm; p_chm->tuner_params.wait_for_lock = true; p_chm->tuner_params.fecMode = eFEC_ANNEX_B; p_chm->tuner_params.qamMode = eQAM_Scan; if (btuner_tune(p_chm->tuner, freq, &p_chm->tuner_params) < 0) { BDBG_WRN(("FREQ[%d] freq = %d not locked, try next freq\n",p_chm->select_idx,freq)); chm_send_status(p_chm,freq,false); continue; } else { BDBG_WRN(("btuner_tune_xxx[%d] freq = %d, success\n",p_chm->select_idx,freq)); chm_send_status(p_chm,freq,true); } p_chm->band = TUNER_BAND; if ((num = chm_scan_psi(p_chm,p_chm->band,p_chm->select_idx)) <= 0) { BDBG_WRN(("chm_scan_psi found no channels\n")); } else { BDBG_MSG(("PSI channels %d \n", num)); } } } #ifdef CONFIG_DVB static int chm_scan_dvb_sdt(chm_mgr_t *p_chm, bband_t band, int freq_idx) { unsigned short num_vch, num_st; unsigned char num_freq; unsigned int timeout; bool done = false; timeout = bos_getticks() + MS_TO_TICKS(HUNT_TIMEOUT); chm_dvb_nit_start(p_chm, band); do { if (chm_check_cancel(p_chm)) break; if (dvb_nit_complete()) done = true; else done = (timeoutch_map, &num_vch, &num_st, &num_freq); if (!num_freq) { BDBG_WRN(("no frequency is delivered...")); return 0; } chm_dvb_sdt_start(p_chm, band); do { if (chm_check_cancel(p_chm)) break; if (dvb_nit_complete() && dvb_sdt_complete(num_freq)) done = true; else done = (timeoutch_map, &num_vch, &num_st, &num_freq); BDBG_WRN(("DVB scan completed... %d:", num_vch)); if (num_vch) { chm_cache_pids(p_chm); } return (timeout < bos_getticks())?eCHM_ERR_TIMEOUT:num_vch; } static void chm_scan_dvb(chm_mgr_t *p_chm) { unsigned int freq; int num; uint16_t network_pid; bapp_freq_table_get(p_chm->fe_type, (const unsigned int **)&(p_chm->freq_table), &(p_chm->num_freq)); for (p_chm->select_idx = 0; p_chm->select_idx < p_chm->num_freq; p_chm->select_idx++) { freq = (unsigned int)(p_chm->freq_table[p_chm->select_idx]); freq *= 1000; BDBG_WRN(("Scan[%d] freq = %d", p_chm->select_idx, freq)); if (chm_check_cancel(p_chm)) break; btuner_params_init(&p_chm->tuner_params, p_chm->tuner); p_chm->tuner_params.cancel_callback = chm_tune_cancel_callback; p_chm->tuner_params.cancel_callback_context = p_chm; p_chm->tuner_params.wait_for_lock = true; p_chm->tuner_params.fecMode = eFEC_ANNEX_B; p_chm->tuner_params.qamMode = eQAM_Scan; if (btuner_tune(p_chm->tuner, freq, &p_chm->tuner_params)<0) { BDBG_WRN(("FREQ[%d] freq=%d not locked, try next freq", p_chm->select_idx, freq)); chm_send_status(p_chm,freq,false); } else { BDBG_WRN(("btuner_tune [%x] freq = %d, success", p_chm->select_idx, freq)); chm_send_status(p_chm,freq,true); } p_chm->band = TUNER_BAND; if (chm_scan_network(p_chm, p_chm->band, &network_pid) != eCHM_ERR_OK) { BDBG_WRN(("chm_scan_network = no network PID found, use default")); } if (network_pid != 0x10) { BDBG_WRN(("invalid network PID for DVB")); continue; } p_chm->chm_network_evt.type = eCHM_EVT_NETWORK; p_chm->chm_network_evt.id = network_pid; chm_post_app_event(p_chm, &p_chm->chm_network_evt); if ((num=chm_scan_dvb_sdt(p_chm,p_chm->band,p_chm->select_idx))<=0) { BDBG_WRN(("chm_scan_psi found no channels")); } else { /* TODO:: check multi-frequency which deliver either NIT or SDT */ BDBG_WRN(("PSI channels %d", num)); } } } #endif /* Summary: Scan for channels using SCTE 65. Description: Scan for channels SCTE 65. Returns number of channels added. */ static int chm_scan_scte65(chm_mgr_t *p_chm, /* Channel manager reference */ bband_t band, unsigned short network_PID ) { unsigned short num_vch, num_st; unsigned char num_freq ; unsigned int timeout; bool done = false; timeout = bos_getticks() + MS_TO_TICKS(HUNT_TIMEOUT); BDBG_MSG(("%s Network PID = 0x%04x\n",__FUNCTION__,network_PID)); /* scan for SCTE 65 tables */ if (chm_nit_start(p_chm,network_PID) != eCHM_ERR_OK) return eCHM_ERR_FAILED; if (p_chm->cmd != eCHM_HUNTING) { if (chm_svct_start(p_chm,network_PID) != eCHM_ERR_OK) return eCHM_ERR_FAILED; if (chm_ntt_start(p_chm,network_PID) != eCHM_ERR_OK) return eCHM_ERR_FAILED; } do { /* Check to see if scan was canceled */ if (chm_check_cancel(p_chm)) break; ch_map_get_counts(&p_chm->ch_map,&num_vch,&num_st,&num_freq); /* When in hunt mode only get require CDS then transition into pending init mode */ if (p_chm->cmd == eCHM_HUNTING) { if (SI_NIT_CDS_Complete() && SI_NIT_MMS_Complete()) done = true; else done = (timeout < bos_getticks()); } else { /* In Pending Init mode both CDS and VCH are required to exit. */ if (p_chm->cds_changed && p_chm->mms_changed && p_chm->vcm_changed && p_chm->sns_changed) done = true; else done = (timeout < bos_getticks()) && (num_freq > 0) && (num_vch > 0); } if (chm_check_cancel(p_chm)) { return eCHM_ERR_CANCELED; } bos_sleep(CHM_IDLE_TIMEOUT); }while (!done); chm_nit_stop(p_chm); if (p_chm->cmd != eCHM_HUNTING) { chm_svct_stop(p_chm); chm_ntt_stop(p_chm); } ch_map_get_counts(&p_chm->ch_map,&num_vch,&num_st,&num_freq); BDBG_MSG(("%s num_vch = %d,num_st = %d,num_freq = %d \n",__FUNCTION__,num_vch,num_st,num_freq)); return(timeout < bos_getticks()) ? eCHM_ERR_TIMEOUT : num_vch; } /* Summary: process the MMS string and associated tables. Returns non-zero on failure. */ int chm_process_mss(chm_mgr_t *p_chm, /* Channel manager reference */ PSIP_MSS_string p_mms, /* Pointer to MMS */ unsigned char* mms_buf, /* string buffer */ unsigned int mms_len /* string buffer length */ ) { int str_idx,lsize; char *p_code; BERR_Code retcode; int num_str; bapp_t *p_app; if (!p_mms) return eCHM_ERR_FAILED; num_str = PSIP_MSS_getNumStrings(p_mms); p_app = (bapp_t*)p_chm->p_app; memset(mms_buf,0,mms_len); for (str_idx = 0; str_idx < num_str; ++str_idx) { if ((retcode = PSIP_MSS_getCode(p_mms,str_idx, &p_code)) != BERR_SUCCESS) { BDBG_WRN(("PSIP_MSS_getCode:%d failed %d\n",__LINE__,retcode)); memset(mms_buf,0,mms_len); continue; } lsize = mms_len; if (chm_valid_lang(p_chm,p_code) != (int)p_app->lang) { #ifdef BDBG_DEBUG_BUILD if ((retcode = PSIP_MSS_getString(p_mms,str_idx,&lsize, (char*)mms_buf)) != BERR_SUCCESS) { BDBG_WRN(("PSIP_MSS_getCode:%d failed %d\n",__LINE__,retcode)); } if (lsize > 0) { BDBG_MSG(("Lang mismatch %c%c%c %s\n",p_code[0],p_code[1],p_code[2],mms_buf)); } memset(mms_buf,0,mms_len); #endif continue; } if ((retcode = PSIP_MSS_getString(p_mms,str_idx,&lsize, (char*)mms_buf)) != BERR_SUCCESS) { BDBG_ERR(("MSS invalid %d\n",retcode)); memset(mms_buf,0,mms_len); } BDBG_MSG(("%s:%d idx = %d, %d/%d-%s\n",__FUNCTION__,__LINE__,str_idx,lsize,mms_len,mms_buf)); return 0; } /* in case no valid language exists just use first string */ if (num_str > 0) { lsize = mms_len; if ((retcode = PSIP_MSS_getString(p_mms,0,&lsize, (char*)mms_buf)) == BERR_SUCCESS) { BDBG_WRN(("No language match using default\n")); return eCHM_ERR_OK; } else { BDBG_WRN(("PSIP_MSS_getString failed %d\n",retcode)); } } BDBG_WRN(("%s:%d no valid string found, num_str = %d\n",__FUNCTION__,__LINE__,num_str)); return eCHM_ERR_FAILED; } /* Summary: Hunt for an SCTE65 Channel on the given frequency. */ static bool chm_hunt_channel(chm_mgr_t *p_chm, unsigned int freq_hz, unsigned short *network_pid) { unsigned short num_vch; unsigned short num_st; unsigned char num_freq; int num; bapp_t *p_app = (bapp_t*)p_chm->p_app; bapp_freq_table_get(p_chm->fe_type,(const unsigned int **)&(p_chm->freq_table),&(p_chm->num_freq)); btuner_params_init(&p_chm->tuner_params, p_chm->tuner); p_chm->tuner_params.cancel_callback = chm_tune_cancel_callback; p_chm->tuner_params.cancel_callback_context = p_chm; p_chm->tuner_params.wait_for_lock = true; p_chm->tuner_params.fecMode = eFEC_ANNEX_B; p_chm->tuner_params.qamMode = eQAM_Scan; if (btuner_tune(p_chm->tuner, freq_hz, &p_chm->tuner_params) < 0) { BDBG_WRN(("FREQ[%d] freq = %d not locked, try next freq\n",p_chm->select_idx,freq_hz)); /* Send message to app */ chm_send_status(p_chm,freq_hz,false); return false; } else { BDBG_WRN(("btuner_tune_[%d] freq = %d, success\n",p_chm->select_idx,freq_hz)); chm_send_status(p_chm,freq_hz,true); } p_chm->band = TUNER_BAND; if (chm_scan_cat(p_chm,p_chm->band,freq_hz) != eCHM_ERR_OK) { BDBG_WRN(("chm_scan_cat - No valid EMM PID found\n")); #ifdef CONFIG_REQUIRE_CAT return false; #endif } if (chm_scan_network(p_chm,p_chm->band,network_pid) != eCHM_ERR_OK) { BDBG_WRN(("chm_scan_network - No network PID found, use default\n")); } p_chm->chm_network_evt.type = eCHM_EVT_NETWORK; p_chm->chm_network_evt.id = *network_pid; chm_post_app_event(p_chm,&p_chm->chm_network_evt); chm_emm_start( p_chm, p_app->settings.EMM_PID ); chm_stt_start(p_chm,*network_pid); if ((num = chm_scan_scte65(p_chm,p_chm->band,*network_pid)) <= 0) { BDBG_WRN(("chm_scan_scte65 no virtual channels\n")); } else { BDBG_MSG(("chm_scan_scte65 found %d \n", num)); } chm_stt_stop(p_chm); chm_emm_stop(p_chm); ch_map_get_counts(&p_chm->ch_map,&num_vch,&num_st,&num_freq); return(num_freq > 0); } /* Summary: Process pending initialization state. */ static void chm_pending_init(chm_mgr_t *p_chm /* Channel manager reference */ ) { unsigned short network_pid,vct_id; bapp_t *p_app = (bapp_t*)p_chm->p_app; ch_map_t *p_ch_map = bapp_cur_ch_map(p_app); unsigned int timeout; if (p_app->settings.activated && (p_app->settings.timeout_cnt > 0) && (p_ch_map->vch_map.num_vch > 0)) { p_chm->cmd = eCHM_CANCEL; p_chm->chmap_evt.type = eCHM_EVT_STATUS; p_chm->chmap_evt.id = eCHM_STATUS_CHMAP; BDBG_WRN(("%s - Activated and has a map\n",__FUNCTION__)); p_chm->chmap_evt.ticks = 0; chm_post_app_event(p_chm,&p_chm->chmap_evt); return; } p_chm->chm_pending_evt.type = eCHM_EVT_STATUS; p_chm->chm_pending_evt.id = eCHM_STATUS_PENDING_INIT; chm_post_app_event(p_chm,&p_chm->chm_pending_evt); if (chm_scan_network(p_chm,p_chm->band,&network_pid) != eCHM_ERR_OK) { BDBG_WRN(("chm_scan_network - No network PID found, use default\n")); } chm_cvt_start(p_chm,network_pid); chm_stt_start(p_chm,network_pid); /* scan for VCH if none exists */ vct_id = p_app->settings.VCT_ID; if (p_chm->ch_map.vch_map.num_vch == 0) { if (chm_svct_start(p_chm,network_pid) != eCHM_ERR_OK) { p_chm->cmd = eCHM_CANCEL; return; } if (chm_ntt_start(p_chm,network_pid) != eCHM_ERR_OK) { p_chm->cmd = eCHM_CANCEL; return; } } chm_emm_start(p_chm,p_app->settings.EMM_PID); timeout = bos_getticks() + MS_TO_TICKS(SVCT_TIMEOUT); /* For testing ### REMOVE */ #if 1 #warning "FOR TEST PURPOSES ONLY: Activate and set default timeout count to 8" p_app->settings.activated = 1; p_app->settings.timeout_cnt = 8; #endif while ((p_chm->ch_map.vch_map.num_vch == 0) || (timeout > bos_getticks()) || !p_app->settings.activated) { if (p_chm->cds_changed && p_chm->mms_changed && p_chm->vcm_changed && p_chm->sns_changed) { BDBG_MSG(("%s - Full channel map received\n",__FUNCTION__)); // if we are not expecting a config message and activated, break out of the while loop. if (p_app->settings.activated) { BDBG_WRN(("Full Channel Map Received and Activated!\n" )); bos_sleep(CHM_IDLE_TIMEOUT); break; } } /* Check to see if scan was canceled */ if (chm_check_cancel(p_chm)) { /* leave pending state */ p_chm->cmd = eCHM_CANCEL; break; } /* Check to see if configuration message was received. If so restart svct. */ if (vct_id != p_app->settings.VCT_ID) { vct_id = p_app->settings.VCT_ID; BDBG_WRN(("configuration message was received while pending, get SVCT with VCTID = 0x%04x!\n",vct_id)); timeout = bos_getticks() + MS_TO_TICKS(SVCT_TIMEOUT); if (chm_svct_start(p_chm,network_pid) != eCHM_ERR_OK) { p_chm->cmd = eCHM_CANCEL; return; } } bos_sleep(CHM_IDLE_TIMEOUT); } if (chm_check_cancel(p_chm)) { /* leave scan state */ p_chm->cmd = eCHM_CANCEL; return; } chm_cache_pids(p_chm); p_chm->chmap_evt.ticks = ch_map_cmp(&(p_chm->ch_map),&(p_app->settings.ch_map)); if (p_chm->chmap_evt.ticks) { /* copy channel map so application can perform update without risk of modification by channel manager */ p_chm->chmap_evt.type = eCHM_EVT_STATUS; p_chm->chmap_evt.id = eCHM_STATUS_CHMAP; BDBG_WRN(("%s - Activated and New Channel Map\n",__FUNCTION__)); chm_post_app_event(p_chm,&p_chm->chmap_evt); bos_sleep(200); } chm_ntt_stop(p_chm); chm_svct_stop(p_chm); chm_stt_stop(p_chm); chm_cvt_stop(p_chm); chm_emm_stop(p_chm); if (chm_check_cancel(p_chm)) { /* leave scan state */ p_chm->cmd = eCHM_CANCEL; return; } /* Force transition into normal mode */ if (!p_chm->chmap_evt.ticks) { p_chm->chmap_evt.type = eCHM_EVT_STATUS; p_chm->chmap_evt.id = eCHM_STATUS_CHMAP; p_chm->chmap_evt.ticks = 0x0; BDBG_WRN(("%s - Send empty map event \n",__FUNCTION__)); chm_post_app_event(p_chm,&p_chm->chmap_evt); } p_chm->cmd = eCHM_CANCEL; } /* Summary: Hunt for an SCTE65 Channel Map. Description: Scan for all available channels. */ static void chm_hunt(chm_mgr_t *p_chm /* Channel manager reference */ ) { unsigned int freq; unsigned short network_pid; bapp_t *p_app = (bapp_t*)p_chm->p_app; // reset last frequency to force tune first channel change after getting a new map p_chm->last_freq_hz = 0; p_chm->chm_hunt_evt.type = eCHM_EVT_STATUS; p_chm->chm_hunt_evt.id = eCHM_STATUS_HUNT; chm_post_app_event(p_chm,&p_chm->chm_hunt_evt); chm_stop_decode(p_chm); /* stop all streams */ chm_stop_stream(p_chm,&(p_chm->chm_stream[CURR_CH])); chm_stop_stream(p_chm,&(p_chm->chm_stream[PREV_CH])); chm_stop_stream(p_chm,&(p_chm->chm_stream[NEXT_CH])); /* Completely reset and start over */ ch_map_reset(&(p_chm->ch_map)); #ifdef CONFIG_DVB dvb_nit_init(&p_chm->dvb_nit_cb); dvb_sdt_init(&p_chm->dvb_sdt_cb); dvb_eit_init(); dvb_tdt_init(); #endif /* Add streamer channel */ if (p_app->settings.psi_scan) { #ifdef CONFIG_DVB BDBG_WRN(("%s - PSI Scan Channel Map for DVB\n",__FUNCTION__)); if (p_app->settings.dvb_scan) chm_scan_dvb(p_chm); else chm_scan(p_chm); #else BDBG_WRN(("%s - PSI Scan Channel Map\n",__FUNCTION__)); /* Fallback to simple SI scan */ chm_scan(p_chm); #endif p_chm->chmap_evt.type = eCHM_EVT_STATUS; p_chm->chmap_evt.id = eCHM_STATUS_CHMAP; p_chm->chmap_evt.ticks = 0xF; /* copy channel map so application can perform update without risk of modification by channel manager */ chm_post_app_event(p_chm,&p_chm->chmap_evt); BDBG_WRN(("%s - PSI Scan Channel Map\n",__FUNCTION__)); bos_sleep(200); /* leave scan state */ p_chm->cmd = eCHM_CANCEL; return; } /* Check "turn on QAM" */ if (p_app->settings.turn_on_freq_khz != 0) { if (!chm_hunt_channel(p_chm,p_app->settings.turn_on_freq_khz * 1000,&network_pid)) { BDBG_WRN(("Turn on QAM tune failed %d KHz.\n",p_app->settings.turn_on_freq_khz)); p_app->settings.turn_on_freq_khz = 0; p_app->settings_dirty |= DIRTY_MISC; } else { BDBG_WRN(("Turn on QAM tune %d KHz. OK\n",p_app->settings.turn_on_freq_khz)); /* leave scan state */ p_chm->cmd = eCHM_PENDING_INIT; p_chm->select_idx = p_chm->num_freq; return; } } /* check for SCTE-65 data */ for (p_chm->select_idx = 0; p_chm->select_idx < p_chm->num_freq; p_chm->select_idx++) { freq = (unsigned int)p_chm->freq_table[p_chm->select_idx]; freq *= 1000; /* convert to Hz */ BDBG_WRN(("Hunt[%d of %d] freq = %d\n",p_chm->select_idx,p_chm->num_freq,freq)); /* Check to see if scan was canceled */ if (chm_check_cancel(p_chm)) { /* leave scan state */ p_chm->cmd = eCHM_CANCEL; return; } if (!chm_hunt_channel(p_chm,freq,&network_pid)) { BDBG_WRN(("Hunt failed %d Hz.\n",freq)); } else { BDBG_WRN(("Set turn on QAM to %d Hz. OK\n",freq)); p_app->settings.turn_on_freq_khz = freq/1000; p_app->settings_dirty |= DIRTY_MISC; /* leave scan state */ p_chm->cmd = eCHM_PENDING_INIT; p_chm->select_idx = p_chm->num_freq; break; } } p_chm->hunt_cnt++; } /* Summary: Update the program pids associated with the freq_idx. Assumes that the channel manager channel map semaphore is already acquired. Returns chm error codes. */ static int chm_update_programs(chm_mgr_t *p_chm, /* Channel manager reference */ int freq_idx /* frequency index */ ) { unsigned int section_size, pmt_section_size; TS_PMT_stream pmt; TS_PAT_program pat; int result = 0; int idx,p_idx; vch_t vch; TS_PSI_descriptor psi_desc,lang_desc; int strm_idx,vch_idx,desc_idx; bapp_t *p_app = (bapp_t*)p_chm->p_app; #ifdef CONFIG_DVB_SUBTITLE unsigned char dsub_lang[MAX_DSUB_PIDS][3]; unsigned short dsub_page[MAX_DSUB_PIDS]; unsigned char dsub_cnt, i; #endif smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = p_chm->band; p_chm->msg_params.pid = (unsigned short)0; /* PAT */ section_size = PAT_BUF_LEN; memset(&vch,0,sizeof(vch)); if ((result = chm_getmessage(p_chm,p_chm->pat_buf,§ion_size, PAT_TIMEOUT)) != 0) { BDBG_WRN(("%s(%d): Error getting PAT 0x%04x = %d\n",__FUNCTION__,__LINE__,p_chm->vch.program_num, result)); return eCHM_ERR_NO_PAT; } if (TS_PAT_validate(p_chm->pat_buf, section_size) != true) { BDBG_WRN(("TS_PAT_validate failed\n")); return eCHM_ERR_NO_PAT; } if (TS_PAT_getNumPrograms(p_chm->pat_buf) == 0) { BDBG_WRN(("TS_PAT_getNumPrograms = 0\n")); return eCHM_ERR_NO_PAT; } BDBG_WRN(("TS_PAT_getNumPrograms %d\n",TS_PAT_getNumPrograms(p_chm->pat_buf))); for (vch_idx = 0; vch_idx < p_chm->ch_map.vch_map.num_vch; vch_idx++) { /* Only process entries which match the current frequency index */ if (p_chm->ch_map.vch_map.vch[vch_idx].freq_idx != freq_idx) continue; /* copy current vch */ vch = p_chm->ch_map.vch_map.vch[vch_idx]; /* Process the PAT to find the correct program to update */ for (idx = 0; idx < TS_PAT_getNumPrograms(p_chm->pat_buf); idx++) { if (TS_PAT_getProgram( p_chm->pat_buf, section_size, idx, &pat ) != b_ok) { BDBG_WRN(("TS_PAT_getProgram(%d,0x%04x)\n", idx,pat.PID)); return eCHM_ERR_NO_PAT; } if (pat.program_number != vch.program_num) { BDBG_MSG(("pat.program_number != prog_number (%d != %d)\n", pat.program_number, vch.program_num)); continue; } smessage_stream_params_init(&p_chm->msg_params, NULL); p_chm->msg_params.band = p_chm->band; p_chm->msg_params.pid = (unsigned short)pat.PID; /* PMT */ p_chm->msg_params.filter.coef[0] = 0x2; p_chm->msg_params.filter.mask[0] = 0x00; p_chm->msg_params.filter.excl[0] = 0xff; pmt_section_size = PMT_BUF_LEN; if ((result = chm_getmessage(p_chm,p_chm->pmt_buf,&pmt_section_size, PMT_TIMEOUT)) != 0) { BDBG_ERR(("Error getting PMT 0x%04x, prog %d\n",pat.PID,vch.program_num)); continue; } p_chm->pmt_crc = chm_section_crc(p_chm->pmt_buf,pmt_section_size); vch.num_audio = 0; vch.cur_audio = 0; vch.pcr_pid = 0; vch.video_pid = 0; vch.scte127_pid = 0; #ifdef CONFIG_DVB_SUBTITLE vch.num_dsub = 0; vch.cur_dsub = 0; #endif memset(vch.audio_pid,0,sizeof(unsigned short) * MAX_AUDIO_PIDS); memset(vch.audio_type,0,sizeof(unsigned char) * MAX_AUDIO_PIDS); memset(vch.audio_lang,'_',sizeof(char) * MAX_AUDIO_PIDS * 3); vch.pcr_pid = TS_PMT_getPcrPid(p_chm->pmt_buf,pmt_section_size); /* Get CA pid */ desc_idx = 0; vch.ca_pid = 0; while ((psi_desc = TS_PMT_getDescriptor(p_chm->pmt_buf, pmt_section_size,desc_idx++)) != NULL) { ca_descriptor ca_desc; if (desc_idx > 0xFF) { BDBG_ERR(("PMT Desc limit exceeded %d\n",desc_idx)); break; } if (chm_check_cancel(p_chm)) { return eCHM_ERR_CANCELED; } if (psi_desc[0] != 9) continue; if (ca_parser(psi_desc, pmt_section_size, &ca_desc) == 0) continue; if (ca_desc.CA_system_ID == p_app->settings.CA_System_ID) { vch.ca_pid = ca_desc.CA_PID; BDBG_MSG(("CA PID = 0x%04x\n",vch.ca_pid)); p_app->settings.SCP_operating_mode = 0; if (ca_desc.CA_system_ID == 0x0E11) { /* Send message to app */ if ((ca_desc.priv_data[0] == 0x5) && (ca_desc.priv_data[1] == 0x1)) p_app->settings.SCP_operating_mode = ca_desc.priv_data[2]; } break; } else { p_chm->system_id_errors++; } } for (p_idx = 0; p_idx < TS_PMT_getNumStreams(p_chm->pmt_buf,pmt_section_size); p_idx++) { bsettop_av_stream_type_t *stream_type; if (TS_PMT_getStream( p_chm->pmt_buf, pmt_section_size, p_idx, &pmt ) != b_ok) { BDBG_WRN(("Failure processing PMT %d\n",p_idx)); break; } strm_idx = 0; lang_desc = NULL; #ifdef CONFIG_DVB_SUBTITLE dsub_cnt = 0; #endif /* default is not supported */ while ((psi_desc = TS_PMT_getStreamDescriptor(p_chm->pmt_buf,pmt_section_size, p_idx,strm_idx)) != NULL) { #ifdef CONFIG_DVB_SUBTITLE if (psi_desc[0] == TS_PSI_DT_DVB_Subtitling) { if ((psi_desc[5]<0x10) || (psi_desc[5]>0x23)) continue; /* not DVB subtitle */ dsub_lang[dsub_cnt][0] = toupper(psi_desc[2]); dsub_lang[dsub_cnt][1] = toupper(psi_desc[3]); dsub_lang[dsub_cnt][2] = toupper(psi_desc[4]); dsub_page[dsub_cnt] = psi_desc[6]<<8|psi_desc[7]; dsub_cnt++; } #endif if (psi_desc[0] == TS_PSI_DT_ISO_639_Language) /* 0x0A ISO-639 Language descriptor */ { BDBG_MSG(("ISO-639 DESC\n" )); lang_desc = psi_desc; /* if get VBI desc alrady */ if (vch.scte127_pid) break; } if (0 == vch.scte127_pid) { if (TS_PSI_DT_DVB_VBI_Data == psi_desc[0]) { BDBG_MSG(("SCTE 127 supported, pid=0x%04x", pmt.elementary_PID)); vch.scte127_pid = pmt.elementary_PID; /* if get lang desc alrady */ if (lang_desc) break; } } strm_idx++; if (strm_idx > 0xFF) { BDBG_ERR(("PMT Stream Desc limit exceeded %d\n",strm_idx)); break; } if (chm_check_cancel(p_chm)) { return eCHM_ERR_CANCELED; } } if ((stream_type = bdecode_supported_video(pmt.stream_type)) != NULL) { BDBG_MSG(("Video PID[%d] = 0x%04x\n",p_idx,pmt.elementary_PID )); if (!vch.video_pid) { vch.video_pid = pmt.elementary_PID; vch.video_type = stream_type->codec_id; } } else if ((stream_type = bdecode_supported_audio(pmt.stream_type)) != NULL) { BDBG_MSG(("Audio PID[%d] = 0x%04x\n",vch.num_audio,pmt.elementary_PID )); if (!vch.audio_pid[vch.num_audio]) { vch.audio_pid[vch.num_audio] = pmt.elementary_PID; vch.audio_type[vch.num_audio] = stream_type->codec_id; if (lang_desc) { vch.audio_lang[vch.num_audio][0] = toupper(lang_desc[2]); vch.audio_lang[vch.num_audio][1] = toupper(lang_desc[3]); vch.audio_lang[vch.num_audio][2] = toupper(lang_desc[4]); } BDBG_MSG(("Audio PID[%d][%c%c%c] = 0x%04x, type=0x%04x\n",p_idx,vch.audio_lang[vch.num_audio][0], vch.audio_lang[vch.num_audio][1],vch.audio_lang[vch.num_audio][2],pmt.elementary_PID, pmt.stream_type)); vch.num_audio++; } } #ifdef CONFIG_DVB_SUBTITLE else if (pmt.stream_type == PES_PRIVATE_STREAM_TYPE) { dsub_cnt = (dsub_cnt>MAX_DSUB_PIDS)?MAX_DSUB_PIDS:dsub_cnt; for (i=0; i= MAX_DSUB_PIDS) break; BDBG_WRN(("%s: private PID[%d] = 0x%04x page:%d [%c%c%c]", __FUNCTION__, vch.num_dsub, pmt.elementary_PID, dsub_page[i], dsub_lang[i][0], dsub_lang[i][1], dsub_lang[i][2])); vch.dsub_pid[vch.num_dsub] = pmt.elementary_PID; vch.dsub_lang[vch.num_dsub][0] = dsub_lang[i][0]; vch.dsub_lang[vch.num_dsub][1] = dsub_lang[i][1]; vch.dsub_lang[vch.num_dsub][2] = dsub_lang[i][2]; vch.dsub_page[vch.num_dsub] = dsub_page[i]; vch.num_dsub++; } } #endif else { BDBG_MSG(("Unsupported stream type[0x%02x]\n", pmt.stream_type)); } } /* for (p_idx = 0; p_idx < TS_PMT_getNumStreams(p_chm->pmt_buf,pmt_section_size); p_idx++) */ p_chm->ch_map.vch_map.vch[vch_idx] = vch; break; } /* for (idx = 0; idx < TS_PAT_getNumPrograms(p_chm->pat_buf); idx++) */ } /* for (vch_idx = 0; vch_idx < p_chm->ch_map.vch_map.num_vch; vch_idx++) */ return result; } /* Summary: Hunt for an SCTE65 Channel Map. Description: Scan for all available channels. */ static void chm_cache_pids(chm_mgr_t *p_chm /* Channel manager reference */ ) { int freq_idx,vch_idx,result; unsigned int freq; bool found; bapp_t *p_app = (bapp_t*)p_chm->p_app; if (bos_acquire_mutex(&(p_chm->ch_map_mutex),45) != b_ok) { BDBG_WRN(("%s:%d failed to acquire channel map mutex\n", __FUNCTION__,__LINE__)); return; } for (freq_idx = 0; freq_idx < p_chm->ch_map.freq_map.num_freq; ++freq_idx) { found = false; for (vch_idx = 0; vch_idx < p_chm->ch_map.vch_map.num_vch; vch_idx++) { if (p_chm->ch_map.vch_map.vch[vch_idx].freq_idx == p_chm->ch_map.freq_map.freq[freq_idx].idx) { found = true; break; } } if (!found) continue; freq = (unsigned int)(p_chm->ch_map.freq_map.freq[freq_idx].freq_khz); freq *= 1000; /* convert to Hz */ BDBG_WRN(("%s cache[%d] freq = %d\n",__FUNCTION__,freq_idx,freq)); /* Check to see if scan was canceled */ if (chm_check_cancel(p_chm)) break; /* Try tuning */ btuner_params_init(&p_chm->tuner_params, p_chm->tuner); p_chm->tuner_params.cancel_callback = chm_tune_cancel_callback; p_chm->tuner_params.cancel_callback_context = p_chm; p_chm->tuner_params.wait_for_lock = true; p_chm->tuner_params.fecMode = eFEC_ANNEX_B; p_chm->tuner_params.qamMode = eQAM_Scan; if (btuner_tune(p_chm->tuner, freq, &p_chm->tuner_params) < 0) { BDBG_WRN(("FREQ[%d] freq = %d not locked, try next freq\n",freq_idx,freq)); chm_send_status(p_chm,freq,false); continue; } else { BDBG_WRN(("btuner_tune_xxx[%d] freq = %d, success\n",freq_idx,freq)); chm_send_status(p_chm,freq,true); } p_chm->band = TUNER_BAND; if ((result = chm_update_programs(p_chm,p_chm->ch_map.freq_map.freq[freq_idx].idx)) != eCHM_ERR_OK) { BDBG_WRN(("chm_update_programs failed %d\n",result)); if (result == eCHM_ERR_TIMEOUT) break; else continue; } } if (p_app->settings.turn_on_freq_khz) { /* Go back to turn on QAM in case the map has channels that don't exist */ freq = (unsigned int)p_app->settings.turn_on_freq_khz * 1000; /* Freq in HZ !!! */ BDBG_WRN(("%s Go to turn on QAM freq %dKHz\n",__FUNCTION__,freq)); /* Try tuning */ btuner_params_init(&p_chm->tuner_params, p_chm->tuner); p_chm->tuner_params.cancel_callback = chm_tune_cancel_callback; p_chm->tuner_params.cancel_callback_context = p_chm; p_chm->tuner_params.wait_for_lock = true; p_chm->tuner_params.fecMode = eFEC_ANNEX_B; p_chm->tuner_params.qamMode = eQAM_Scan; if (btuner_tune(p_chm->tuner, freq, &p_chm->tuner_params) < 0) { BDBG_WRN(("Turn on freq = %dHz not locked, try next freq\n",freq)); chm_send_status(p_chm,freq,false); } else { BDBG_WRN(("Turn on freq = %dHz, success\n",freq)); chm_send_status(p_chm,freq,true); } } bos_release_mutex(&(p_chm->ch_map_mutex)); } /* Summary: Post an event the app queue to provide status. */ static int chm_post_app_event_cb(chm_mgr_t *p_chm, /* Channel manager reference */ chm_event_t *p_event /* Event to post to app queue */ ) { bapp_t *p_app = (bapp_t*)p_chm->p_app; bos_post_event(p_app->msg_queue,(b_event_t*)p_event); return 0; } /* Summary: Post an event the app queue to provide status. */ static int chm_post_app_event(chm_mgr_t *p_chm, /* Channel manager reference */ chm_event_t *p_event /* Event to post to app queue */ ) { int result = chm_post_app_event_cb(p_chm,p_event); bos_sleep(10); return result; } #ifdef HAS_VBI /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: chm_scte_feed_callback * DESCRIPTION: feed scte 127 data for encoding ******************************************************************************/ static pscte_127_handle s_pscte_127 = NULL; void chm_scte_feed_callback(int field) { if (s_pscte_127 && scte_127_has_data(s_pscte_127, field)) { scte_127_feed_data_buf(s_pscte_127, field); } } /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: message_callback * DESCRIPTION: Callback function normally called when a complete message has * been received and passed the filter criteria. ******************************************************************************/ void * chm_smessage_scte127_callback(void * context, size_t size) { chm_mgr_t * p_chm = (chm_mgr_t*)context; bapp_t *p_app; uint8_t *ptr, *ptr1; int len; p_app = (bapp_t*)p_chm->p_app; //BDBG_MSG(("#### SCTE127 Event (size=%d)####\n", size)); ptr = ptr1 = p_chm->scte127_buf; if (size != p_chm->scte127_buf_size) { if (size % 188) { BDBG_WRN(("Size not match, make adjustment if not multiple of 188")); size = (size / 188) * 188; } } for (; size; size -= 188) { ptr = pes_parser(ptr, &len); if (ptr) { /* should be data unit id byte */ len = (188 - len); scte_127_pes_data_field_parser(p_chm->pscte_127, ptr, len); } else { /* if not private data */ if (0xff == len) break; BDBG_WRN(("Invalid SCTE127 packet")); } ptr1 += 188; ptr = ptr1; } return(void*)p_chm->scte127_buf; } #endif /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: message_callback * DESCRIPTION: Callback function normally called when a complete message has * been received and passed the filter criteria. ******************************************************************************/ void * chm_smessage_eas_callback(void * context, size_t size) { TS_SCTE_18_header header; /* header structure */ chm_mgr_t * p_chm = (chm_mgr_t*)context; bapp_t *p_app; unsigned int timegpssec, event_end_time; unsigned short source_id,exp_source_id; b_timeval cur; bool found_sourceid = false; size_t ex_size,i; uint8_t *pOffset; p_app = (bapp_t*)p_chm->p_app; BDBG_WRN(("#### EAS Event ####\n")); p_chm->eas_cnt++; if (!p_app->power) { BDBG_WRN(("####EAS Event: DTA in standby state: ignore. #### \n")); goto ExitFunc; } if (TS_SCTE_18_getSectionHeader(p_chm->eas_buf, size, &header) != 0) { BDBG_ERR(("#### TS_SCTE_18_getSectionHeader failed ####\n")); goto ExitFunc; } /* for priority == 0, initialize sequence number only per DTA30-DIG-I01-081210 */ if (0 == header.alert_priority) { /* establish a new sequence number */ p_chm->sequence_number = header.sequence_number; goto ExitFunc; } if (p_chm->sequence_number == header.sequence_number) { BDBG_ERR(("#### EAS Event %d duplicate ####\n",header.sequence_number)); goto ExitFunc; } p_chm->sequence_number = header.sequence_number; if ((int8_t)header.alert_priority < SCTE_18_ALERT_PRIORITY) { BDBG_WRN(("#### EAS Event alert priority %d < %d, ignore.#### \n", header.alert_priority, SCTE_18_ALERT_PRIORITY)); goto ExitFunc; } if ((int8_t)header.alert_message_time_remaining < 0 || header.alert_message_time_remaining > 120) { BDBG_WRN(("#### EAS Event alert_message_time_remaining %d out of range ####\n",header.alert_message_time_remaining)); goto ExitFunc; } if (p_chm->EAS_event_ID == header.EAS_event_ID) { /* same EAS message, but only time remaining changed */ GETTIMEOFDAY(&cur); if (header.alert_message_time_remaining) p_app->eas_timeout = cur.tv_sec + header.alert_message_time_remaining; else p_app->eas_timeout = 0; BDBG_MSG(("#### EAS Event %d already processed ####\n",header.EAS_event_ID)); goto ExitFunc; } /* new EAS message is distributed */ p_chm->EAS_event_ID = header.EAS_event_ID; if (header.event_start_time && header.event_duration) { /* 15 mins to 100 hours */ if (header.event_duration < 15 || header.event_duration > 600) { BDBG_WRN(("#### EAS Event event_duration %d out of range ####\n",header.event_duration)); goto ExitFunc; } GETTIMEOFDAY(&cur); timegpssec = p_app->system_time + cur.tv_sec; event_end_time = header.event_start_time+(header.event_duration * 60); if (event_end_time < timegpssec) { BDBG_WRN(("#### EAS Event has expired %d < %d, ignore.#### \n", event_end_time, timegpssec)); goto ExitFunc; } } /* if exception list existed, check to see if current channel is in exception list */ if (header.exception_count) { BDBG_WRN(("#### EAS exception count = %d\n",header.exception_count)); pOffset = TS_SCTE_18_getExpectionOffset((const char *)p_chm->eas_buf, &ex_size); if (pOffset) { vch_t vch; st_t st; freq_t freq; TS_SCTE_18_exception s; if (!ch_map_get_current(bapp_cur_ch_map(p_app),&vch,&st,&freq)) { BDBG_WRN(("#### EAS Current channel map invalid.#### \n")); goto ExitFunc; } for (i = 0; i < header.exception_count; i++) { if (TS_SCTE_18_getException(pOffset, i, &s)) { BDBG_WRN(("#### EAS exception (%d,0x%04x,0x%04x)\n",s.in_band_reference,s.major_channel_number,s.minor_channel_number)); if (s.in_band_reference && ch_map_find_soure_id(bapp_cur_ch_map(p_app), (unsigned char)s.major_channel_number, (unsigned short)s.minor_channel_number,&exp_source_id)) { if (exp_source_id == vch.source_id) { BDBG_WRN(("#### 0x%04x in exception list, discard\n",exp_source_id)); goto ExitFunc; } } else { //use source ID if (s.OOB_source_ID == vch.source_id) { BDBG_WRN(("#### OOB 0x%04x in exception list, discard\n",exp_source_id)); goto ExitFunc; } } } else { BDBG_WRN(("#### EAS TS_SCTE_18_getException %d failed\n",i)); goto ExitFunc; } } } } /* try details OOB source ID first */ if (header.details_OOB_source_ID) { BDBG_WRN(("#### EAS Attempt to set details source ID = 0x%04x.#### \n", header.details_OOB_source_ID)); source_id = header.details_OOB_source_ID; found_sourceid = ch_map_set(bapp_cur_ch_map(p_app),source_id); } /* if OOB source ID or not in the VCM */ if (!found_sourceid && (header.details_major_channel_number && header.details_minor_channel_number)) { BDBG_WRN(("#### EAS Attempt to set major.minor [0x%04x.0x%04x] #### \n", header.details_major_channel_number, header.details_minor_channel_number)); if (!ch_map_find_soure_id(bapp_cur_ch_map(p_app), (unsigned char)header.details_major_channel_number, (unsigned short)header.details_minor_channel_number, &source_id)) { /* try audio only */ if (15 == header.alert_priority && header.audio_OOB_source_ID) { BDBG_WRN(("#### EAS Attempt to set audio source ID = 0x%04x.#### \n", header.audio_OOB_source_ID)); source_id = header.audio_OOB_source_ID; found_sourceid = ch_map_set(bapp_cur_ch_map(p_app),source_id); } else { BDBG_WRN(("#### EAS no details source ID available\n")); goto ExitFunc; } } else found_sourceid = true; } /* Check for Channel Descriptor */ if (!found_sourceid) { /* try channel description */ TS_SCTE_18_in_band_channel_descriptor ib_channel_desc; pOffset = TS_SCTE_18_getDescriptorOffset((const char *)p_chm->eas_buf, &ex_size); if (pOffset == NULL) { BDBG_WRN(("#### EAS Channel Descriptor not found #### \n")); goto ExitFunc; } if (!TS_SCTE_18_getInBandChannelDescriptor(pOffset, &ib_channel_desc,ex_size)) { BDBG_WRN(("#### EAS Channel Desc not found #### \n")); goto ExitFunc; } if (!ch_map_find_soure_id(bapp_cur_ch_map(p_app), (unsigned char)ib_channel_desc.exception_RF_channel, (unsigned short)ib_channel_desc.exception_program_number, &source_id)) { BDBG_WRN(("#### EAS sourceID not found for ib channel desc [0x%04x.0x%04x] #### \n", ib_channel_desc.exception_RF_channel, ib_channel_desc.exception_program_number)); goto ExitFunc; } } if ( ((header.location_code_count) < 1) || ((header.location_code_count) > 31) ) { BDBG_WRN(("#### EAS location code count invalid %d #### \n",header.location_code_count)); goto ExitFunc; } else { pOffset = TS_SCTE_18_getLocationOffset((const char *)p_chm->eas_buf, &size); if (pOffset) { TS_SCTE_18_location_code s; for (i = 0; i < header.location_code_count; i++) { if (TS_SCTE_18_getLocationCode(pOffset, i, &s)) { if ( (((int8_t)s.state_code) < 0) || ((s.state_code) > 99) || (((int8_t)s.county_subdivision) < 0) || ((s.county_subdivision) > 9) || (((int8_t)s.county_code) < 0) || ((s.county_code) > 999) ) { BDBG_WRN(("#### EAS state code %d or subdivision %d country_code %d invalid#### \n",s.state_code, s.county_subdivision, s.county_code)); goto ExitFunc; } } else { goto ExitFunc; } } } else { goto ExitFunc; } } if (ch_map_set(bapp_cur_ch_map(p_app),source_id)) { BDBG_WRN(("#### EAS source ID = 0x%04x found (timeout %d secs).#### \n", source_id,header.alert_message_time_remaining)); p_chm->chm_eas_evt.type = eCHM_EVT_EAS; p_chm->chm_eas_evt.id = (header.alert_message_time_remaining << 16) | source_id; p_chm->eas_timer_started = true; memcpy(p_app->eas_buf, p_chm->eas_buf, size); p_app->eas_buf_size = size; chm_post_app_event_cb(p_chm,&p_chm->chm_eas_evt); } else { BDBG_ERR(("#### EAS source ID = 0x%04x not found.#### \n", source_id)); } ExitFunc: return(void*)p_chm->eas_buf; } /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: message_callback * DESCRIPTION: Callback function normally called when a complete message has * been received and passed the filter criteria. ******************************************************************************/ #if defined(ENABLE_UMESSAGE) static struct udta_message umessage; static struct udta_subcommand usubcommand; static struct udta_network_config unetwork_config; static struct udta_descriptor udescriptor; static struct udta_time_zone_descriptor utime_zone_descriptor; static struct udta_eas_location_descriptor ueas_descriptor; static struct udta_site_announcement_subcommand usite; static struct udta_phone_number_descriptor uphone_descriptor; static struct udta_reset_user_processor_subcommand ureset; static struct udta_key_table_subcommand keyt; static struct udta_eas_filter_control_subcommand efc; static struct udta_content_control ccm; static struct udta_scp_amm amm; static struct udta_scp_acm acm; static struct udta_encrypted_global_key egk; static struct udta_soc_type soc_type; void* chm_smessage_emm_callback(void * context, size_t size) { chm_mgr_t * p_chm = (chm_mgr_t*)context; unsigned char hw_address[6]; unsigned char singlecast_address[6]; struct udta_subcommand_count cnt; struct udta_parser_state pst; uint16_t parsed_len; int subcmd_count, idx; bapp_t *p_app; p_chm->emm_count++; BDBG_MSG(("#### %s #### \n", __FUNCTION__)); p_app = (bapp_t*)p_chm->p_app; if (!get_hw_address(hw_address)) return(void*)(p_chm->emm_buf); memset(&umessage,0,sizeof(umessage)); udta_parser_start(p_chm->emm_buf, &pst); udta_parse_message(&pst, &umessage); if (umessage.address_type == 1) { unsigned int utc_time; singlecast_address[0] = umessage.singlecast_address[4]; singlecast_address[1] = umessage.singlecast_address[3]; singlecast_address[2] = umessage.singlecast_address[2]; singlecast_address[3] = umessage.singlecast_address[1]; singlecast_address[4] = umessage.singlecast_address[0]; if (memcmp(singlecast_address,hw_address,5) == 0) { /* valid singlecast message */ get_utc_time(p_app,&utc_time); p_app->settings.addressable_time = utc_time; } else { return(void*)(p_chm->emm_buf); } } else if (umessage.address_type == 0) { BDBG_WRN(("Broadcast message: %d\n",umessage.message_type)); } else { goto ExitFunc; } BDBG_WRN(("EMM message type 0x%02X\n", umessage.message_type)); switch (umessage.message_type) { case UDTA_CONTENT_CONTROL: p_chm->ccm_cnt++; udta_parse_content_control(&pst, &ccm); for (idx = 0; idx < ccm.soc_type_count; ++idx) { udta_parse_soc_type(&pst, &soc_type); /* TODO put into soc type structure */ } /* TODO Send to application for storage */ break; case UDTA_SCP_AMM: p_chm->amm_cnt++; udta_parse_scp_amm(&pst, &amm); /* TODO Send to application for storage */ break; case UDTA_SCP_ACM_EVEN: p_chm->acm_even_cnt++; udta_parse_scp_acm(&pst, &acm); cmode_acm_handler(0x80, &acm, 0); break; case UDTA_SCP_ACM_ODD: p_chm->acm_odd_cnt++; udta_parse_scp_acm(&pst, &acm); cmode_acm_handler(0x80, &acm, 0); break; case UDTA_CONFIGURATION_1: case UDTA_CONFIGURATION_2: p_chm->cfg_cnt++; udta_parse_subcommand_count(&pst, &cnt); subcmd_count = cnt.subcommand_count; while (subcmd_count--) { udta_parse_subcommand(&pst,&usubcommand); BDBG_WRN(("DCT CONFIG Subcommand %d\n",usubcommand.subcommand_id)); switch (usubcommand.subcommand_id) { case UDTA_NETWORK_CONFIG: udta_parse_network_config(&pst, &unetwork_config); if (p_chm->cmd == eCHM_PENDING_INIT) { /* since we are alread in pending init mode there is no need to notify application of VCT_ID change. */ if (p_app->settings.VCT_ID != unetwork_config.home_vct_id) { BDBG_WRN(("%s:%d Configuration VCT_ID %d does not match current value %d\n", __FUNCTION__,__LINE__,unetwork_config.home_vct_id, p_app->settings.VCT_ID)); p_chm->ch_map.vch_map.num_vch = 0; p_app->settings.VCT_ID = unetwork_config.home_vct_id; p_app->settings.activated = false; } } else { p_chm->vct_id_evt.type = eCHM_EVT_VCT_ID; p_chm->vct_id_evt.id = unetwork_config.home_vct_id; chm_post_app_event(p_chm,&p_chm->vct_id_evt); } BDBG_WRN(("chm_smessage_emm_callback()- DCT Network Configuration Message Received! VctId: %d \n", unetwork_config.home_vct_id )); if (p_chm->cmd == eCHM_PENDING_INIT) { /* since we are alread in pending init mode there is no need to notify application of VCT_ID change. */ p_app->settings.timeout = unetwork_config.site_timeout; p_app->settings.timeout_cnt = unetwork_config.site_timeout; p_app->settings.location = unetwork_config.site_code; p_app->settings.timeout_policy = unetwork_config.timeout_policy; p_app->settings.max_location_errors = unetwork_config.max_site_errors; if (p_app->settings.VCT_ID != unetwork_config.home_vct_id) { BDBG_WRN(("%s:%d Configuration VCT_ID %d does not match current value %d\n", __FUNCTION__,__LINE__,unetwork_config.home_vct_id,p_app->settings.VCT_ID)); p_chm->ch_map.vch_map.num_vch = 0; p_app->settings.VCT_ID = unetwork_config.home_vct_id; p_app->settings.activated = false; } } p_chm->config_evt.type = eCHM_EVT_CONFIGURATION; p_chm->config_evt.location = unetwork_config.site_code; p_chm->config_evt.timeout = unetwork_config.site_timeout; p_chm->config_evt.timeout_policy = unetwork_config.timeout_policy; p_chm->config_evt.max_location_errors = unetwork_config.max_site_errors; if (p_chm->config_evt.VCT_ID != unetwork_config.home_vct_id) { p_chm->refresh_count++; BDBG_WRN(("Changing VCT-ID from 0x%04X to 0x%04X; count = %d\n", p_chm->config_evt.VCT_ID, unetwork_config.home_vct_id, p_chm->refresh_count)); } p_chm->config_evt.VCT_ID = unetwork_config.home_vct_id; chm_post_app_event(p_chm,(chm_event_t*)&p_chm->config_evt); parsed_len = pst.bits.bindex >> 3; /* if we have more message body parse descriptors */ while(parsed_len < umessage.message_length){ udta_parse_descriptor(&pst, &udescriptor); switch(udescriptor.descriptor_tag){ case UDTA_TIME_ZONE_DESCRIPTOR: udta_parse_timezone_descriptor(&pst, &utime_zone_descriptor); p_chm->tz_evt.type = eCHM_EVT_TIMEZONE; p_chm->tz_evt.dst_delta = utime_zone_descriptor.daylight_savings_delta_time; p_chm->tz_evt.dst_entry = utime_zone_descriptor.daylight_savings_entry_time; p_chm->tz_evt.dst_exit = utime_zone_descriptor.daylight_savings_exit_time; p_chm->tz_evt.utc_offset = utime_zone_descriptor.time_zone_offset; chm_post_app_event(p_chm,(chm_event_t*)&p_chm->tz_evt); break; case UDTA_EAS_LOCATION_DESCRIPTOR: udta_parse_eas_location_descriptor(&pst, &ueas_descriptor); break; default: udta_skip_descriptor(&pst, &udescriptor); break; } parsed_len = pst.bits.bindex >> 3; } break; default: udta_skip_subcommand(&pst, &usubcommand); break; } } break; case UDTA_OPERATIONS_1: case UDTA_OPERATIONS_2: p_chm->ops_cnt++; udta_parse_subcommand_count(&pst, &cnt); subcmd_count = cnt.subcommand_count; while (subcmd_count--) { udta_parse_subcommand(&pst, &usubcommand); BDBG_WRN(("DCT OPP Subcommand %d\n",usubcommand.subcommand_id)); switch (usubcommand.subcommand_id) { case UDTA_INITIALIZE: BDBG_WRN(("chm_smessage_emm_callback() DCT_INITIALIZE_SCMD \n" )); p_chm->reset_evt.type = eCHM_EVT_RESET; p_chm->reset_evt.id = 4; chm_post_app_event(p_chm,&p_chm->reset_evt); break; case UDTA_RESET_USER_PROCESSOR: BDBG_WRN(("chm_smessage_emm_callback() DCT_RESET_USER_PROCESSOR_SCMD \n" )); udta_parse_reset_user_processor_subcommand(&pst, &ureset); p_chm->reset_evt.type = eCHM_EVT_RESET; p_chm->reset_evt.id = ureset.reset_command; /* 0,1,2 */ chm_post_app_event(p_chm,&p_chm->reset_evt); break; case UDTA_CONNECT: if (!p_app->settings.activated) { BDBG_WRN(("CONNECT message received - activating\n")); } p_chm->activate_evt.type = eCHM_EVT_ACTIVATION; p_chm->activate_evt.id = 1; /* activate */ chm_post_app_event(p_chm,&p_chm->activate_evt); break; case UDTA_DISCONNECT: p_chm->activate_evt.type = eCHM_EVT_ACTIVATION; p_chm->activate_evt.id = 0; /* deactivate */ chm_post_app_event(p_chm,&p_chm->activate_evt); break; case UDTA_SITE_ANNOUNCEMENT: udta_parse_site_announcement_subcommand(&pst, &usite); p_chm->location_evt.type = eCHM_EVT_LOCATION; p_chm->location_evt.id = usite.site_code; chm_post_app_event(p_chm,&p_chm->location_evt); parsed_len = pst.bits.bindex >> 3; while(parsed_len < umessage.message_length){ udta_parse_descriptor(&pst, &udescriptor); if(UDTA_PHONE_NUMBER_DESCRIPTOR == udescriptor.descriptor_tag){ uphone_descriptor.length = udescriptor.descriptor_lenght; udta_parse_phone_number_descriptor(&pst, &uphone_descriptor); uphone_descriptor.phone_number_string[uphone_descriptor.length] = 0; // ensure string terminator strcpy(p_chm->phone_evt.data,uphone_descriptor.phone_number_string); p_chm->phone_evt.type = eCHM_EVT_PHONENUMBER; chm_post_app_event(p_chm,(chm_event_t*)&p_chm->phone_evt); }else{ udta_skip_descriptor(&pst, &udescriptor); } parsed_len = pst.bits.bindex >> 3; } break; case UDTA_KEY_TABLE: p_chm->key_table_cnt++; udta_parse_key_table_subcommand(&pst, &keyt); for (idx = 0; idx < keyt.number_of_global_keys; ++idx) { udta_parse_encrypted_global_key(&pst, &egk); /* TODO Put into EGK structure */ } /* TODO Send key table to application for storage. */ break; case UDTA_EAS_FILTER_CONTROL: p_chm->eas_filter_cnt++; udta_parse_eas_filter_control_subcommand(&pst, &efc); /* TODO Send filter control information to application for storage. */ break; default: udta_skip_subcommand(&pst, &usubcommand); break; } } break; default: BDBG_WRN(("#### Unsupported MSP Command 0x%02x #### \n", umessage.message_type)); break; } ExitFunc: return(void*)(p_chm->emm_buf); } #else void* chm_smessage_emm_callback(void * context, size_t size) { chm_mgr_t * p_chm = (chm_mgr_t*)context; return(void*)(p_chm->emm_buf); } #endif /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: message_callback * DESCRIPTION: Callback function normally called when a complete message has * been received and passed the filter criteria. ******************************************************************************/ void * chm_smessage_stt_callback(void * context, size_t size) { chm_mgr_t * p_chm; b_tm utc_time; b_timeval tv; p_chm = (chm_mgr_t*)context; p_chm->time_evt.type = eCHM_EVT_TIME; if (SI_STT_parse(p_chm->stt_buf) != SI_SUCCESS) { BDBG_WRN(("%s SI_STT_parse failed\n",__FUNCTION__)); return(void*)p_chm->stt_buf; } p_chm->stt_cnt++; /* current platform seconds when STT was received */ GETTIMEOFDAY(&tv); /* Current UTC time according to STT */ p_chm->time_evt.system_time = SI_STT_Get_Sys_Time() - SI_STT_Get_GPS_UTC_Offset(); p_chm->time_evt.system_offset = tv.tv_sec; p_chm->time_evt.utc_offset = SI_STT_Get_GPS_UTC_Offset(); chm_post_app_event(p_chm,(chm_event_t*)&p_chm->time_evt); utctime(p_chm->time_evt.system_time,&utc_time); BDBG_WRN(("STT UTC = %2d/%2d/%4d %2d:%2d:%2d\n", utc_time.tm_mon + 1,utc_time.tm_mday,utc_time.tm_year + 1980, utc_time.tm_hour,utc_time.tm_min,utc_time.tm_sec)); return(void*)p_chm->stt_buf; } /* Destination for cvt parser so the actual data would not be changed while it is being processed by another thread */ static struct cvt_info_t cvt_info; /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: message_callback * DESCRIPTION: Callback function normally called when a complete message has * been received and passed the filter criteria. ******************************************************************************/ void * chm_smessage_cvt_callback(void * context, size_t size) { chm_mgr_t * p_chm = (chm_mgr_t*)context; bapp_t *p_app; int res,i; unsigned short vch_num; p_app = (bapp_t*)p_chm->p_app; BDBG_ERR(("#### CVT Event ####\n")); ch_map_get_vch_num(bapp_cur_ch_map(p_app),&vch_num); if (vch_num == 0) { BDBG_WRN(("%s No channels in map.\n",__FUNCTION__)); goto ExitFunc; } res = cvt_parse(p_chm->cvt_buf, size, &cvt_info); if (res != 0) { BDBG_WRN(("#### CVT Inavlid.####, res=%d\n", res)); goto ExitFunc; } p_chm->cvt_cnt++; //#define CVT_DEBUG #ifdef CVT_DEBUG BDBG_WRN(("protocol_version = %d", cvt_info.protocol_version)); BDBG_WRN(("configuration_count_change = %d", cvt_info.configuration_count_change)); BDBG_WRN(("vendor_id = %d (0x%08x)", cvt_info.vendor_id, cvt_info.vendor_id)); BDBG_WRN(("hardware_version_id = %d (0x%08x)", cvt_info.hardware_version_id, cvt_info.hardware_version_id)); BDBG_WRN(("location_type = %d", cvt_info.location_type)); BDBG_WRN(("source_ID = %d", cvt_info.source_ID)); BDBG_WRN(("frequency_vector = %d (0x%04x)", cvt_info.frequency_vector, cvt_info.frequency_vector)); BDBG_WRN(("modulation_type = %d", cvt_info.modulation_type)); BDBG_WRN(("program_number = %d (0x%04x)", cvt_info.program_number & 0xffff, cvt_info.program_number & 0xffff)); BDBG_WRN(("pid = %d (0x%04x)", cvt_info.pid, cvt_info.pid)); BDBG_WRN(("code_file_name_length = %d", cvt_info.code_file_name_length)); cvt_info.code_file_name[cvt_info.code_file_name_length] = '\0'; BDBG_WRN(("code_file_name = %s", cvt_info.code_file_name)); #endif if (cvt_info.vendor_id != p_chm->vendor_id) { BDBG_ERR(("#### CVT Vendor ID mismatch (0x%08x != 0x%08x) ####\n",cvt_info.vendor_id, p_chm->vendor_id)); goto ExitFunc; } if (cvt_info.hardware_version_id != p_chm->hardware_version_id) { BDBG_ERR(("#### CVT Vendor ID mismatch (0x%08x != 0x%08x) ####\n",cvt_info.vendor_id, p_chm->vendor_id)); goto ExitFunc; } BDBG_WRN(("#### CVT Got Vendor ID (0x%08x) ####\n",cvt_info.vendor_id)); if (true == p_chm->cvt_valid) { if (cvt_info.configuration_count_change == p_chm->cvt.configuration_count_change) { BDBG_WRN(("configuration_count_change %d:%d", cvt_info.configuration_count_change, p_chm->cvt.configuration_count_change)); goto ExitFunc; } i = 0; if (cvt_info.code_file_name_length == p_chm->cvt.code_file_name_length) { for (i = 0; i < cvt_info.code_file_name_length; ++i) { if (cvt_info.code_file_name[i] != p_chm->cvt.code_file_name[i]) { break; } } } if (i == cvt_info.code_file_name_length) { BDBG_WRN(("CVT file names match")); goto ExitFunc; } } if (p_chm->cmd == eCHM_HUNTING) p_chm->cmd = eCHM_CANCEL; if (p_chm->num_freq == 0) { bapp_freq_table_get(p_chm->fe_type,(const unsigned int **)&(p_chm->freq_table),&(p_chm->num_freq)); } if (p_chm->num_freq == 0) { BDBG_WRN(("%s:%d p_chm->num_freq = %d\n",__FUNCTION__,__LINE__,p_chm->num_freq)); goto ExitFunc; } /* copy structure to channel manager and mark cvt as valid */ memcpy(&(p_chm->cvt), &cvt_info, sizeof(struct cvt_info_t)); p_chm->cvt_valid = true; p_chm->download_evt.type = eCHM_EVT_DOWNLOAD; p_chm->download_evt.id = (p_chm->select_idx * (100/p_chm->num_freq))/p_chm->num_freq; chm_post_app_event_cb(p_chm,&p_chm->download_evt); ExitFunc: return(void*)p_chm->cvt_buf; } /* Summary: Get the STT. return non-zero on failure */ static int chm_stt_start(chm_mgr_t *p_chm, /* Channel manager reference */ unsigned short network_pid) { int cerr; smessage_stream_params_t params; cerr = eCHM_ERR_OK; BDBG_MSG(("%s:%d (0x%04x)",__FUNCTION__, __LINE__,network_pid)); chm_stt_stop(p_chm); p_chm->stt_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->stt_msg) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->stt_msg); params.band = p_chm->band; params.pid = (uint16_t)network_pid; params.filter.coef[0] = 0xC5; params.filter.mask[0] = 0x00; /* All PSIP tables have protocol_version which must be = 0 */ params.filter.coef[3] = 0x00; /* protocol_version */ params.filter.mask[3] = 0x00; params.buffer = p_chm->stt_buf; params.buffer_size = STT_BUF_LEN; /* processing is done in callback */ params.data_ready_callback = chm_smessage_stt_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; BDBG_WRN(("%s\n",__FUNCTION__)); if (b_ok != smessage_start(¶ms, p_chm->stt_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } /* Summary: Stop checking for EMM messages. return non-zero on failure */ static void chm_emm_stop(chm_mgr_t *p_chm /* Channel manager reference */ ) { if (NULL != p_chm->emm_msg) { if (b_ok != smessage_stop(p_chm->emm_msg)) { BDBG_ERR(("%s:%d",__FUNCTION__, __LINE__)); return; } smessage_close(p_chm->emm_msg); p_chm->emm_msg = NULL; } } /* Summary: Start checking for EMM messages. return non-zero on failure */ static int chm_emm_start(chm_mgr_t *p_chm, /* Channel manager reference */ unsigned short emm_pid) { int cerr; smessage_stream_params_t params; cerr = eCHM_ERR_OK; BDBG_MSG(("%s:%d (0x%04x)",__FUNCTION__, __LINE__,emm_pid)); chm_emm_stop(p_chm); p_chm->emm_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->emm_msg) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->emm_msg); params.band = p_chm->band; params.pid = (uint16_t)emm_pid; params.filter.coef[0] = 0x80; params.filter.mask[0] = 0x0f; /* filter on byte 2 */ params.filter.coef[1] = 0x00; params.filter.mask[1] = 0x1f; #if 0 /* All PSIP tables have protocol_version which must be = 0 */ params.filter.coef[3] = 0x00; /* protocol_version */ params.filter.mask[3] = 0x00; #endif params.buffer = p_chm->emm_buf; params.buffer_size = EMM_BUF_LEN; /* processing is done in callback */ params.data_ready_callback = chm_smessage_emm_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; if (b_ok != smessage_start(¶ms, p_chm->emm_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } /* Summary: Tune to the current frequency. */ static int chm_tune_freq(chm_mgr_t *p_chm,unsigned int freq_hz,unsigned int freq_idx) { bapp_t *p_app = (bapp_t*)p_chm->p_app; mms_t mms; unsigned int tune_ticks; BDBG_WRN(("%s: freq_hz=%d, last_freq_hz=%d\n",__FUNCTION__, freq_hz, p_chm->last_freq_hz)); if (p_chm->force_tune) { p_chm->last_freq_hz = 0; p_chm->force_tune = false; } if (p_chm->last_freq_hz != freq_hz) { btuner_params_init(&p_chm->tuner_params, p_chm->tuner); p_chm->tuner_params.cancel_callback = chm_tune_cancel_callback; p_chm->tuner_params.cancel_callback_context = p_chm; p_chm->tuner_params.wait_for_lock = true; p_chm->tuner_params.fecMode = eFEC_ANNEX_B; p_chm->tuner_params.qamMode = eQAM_Scan; if (ch_map_get_mms(bapp_cur_ch_map(p_app),p_chm->vch.mms_idx,&mms)) { /* This is where QAM optimizations go */ if ((mms.modulation != eQAM_64) && (mms.modulation != eQAM_256)) p_chm->tuner_params.qamMode = eQAM_Scan; else p_chm->tuner_params.qamMode = mms.modulation; } else p_chm->tuner_params.qamMode = eQAM_Scan; BDBG_WRN(("Tune to frequency = %d (0x%02x)\n",freq_hz,p_chm->tuner_params.qamMode)); tune_ticks = bos_getticks(); p_chm->band = btuner_tune(p_chm->tuner, freq_hz, &p_chm->tuner_params); tune_ticks = bos_getticks() - tune_ticks; p_chm->status_evt.type = eCHM_EVT_STATUS; p_chm->status_evt.id = eCHM_STATUS_TUNE_MS; p_chm->status_evt.ticks = TICKS_TO_MS(tune_ticks); chm_post_app_event(p_chm,(chm_event_t*)&p_chm->status_evt); if (p_chm->band < 0) { BDBG_WRN(("Tune failed (freq = %d)\n",freq_hz)); p_chm->last_freq_hz = 0; /* Send message to app */ chm_send_status(p_chm,freq_hz,false); return eCHM_ERR_FAILED; } p_chm->last_freq_hz = freq_hz; } else { BDBG_WRN(("Already tuned to frequency = %d\n",freq_hz)); } p_chm->status_evt.ticks = TICKS_TO_MS((bos_getticks() - tune_ticks)); chm_post_app_event(p_chm,(chm_event_t*)&p_chm->status_evt); BDBG_ERR(("%s:%d\n",__FUNCTION__, __LINE__)); chm_send_status(p_chm,freq_hz,true); return eCHM_ERR_OK; } /* Summary: Rotate audio pids. */ static int chm_rotate_audio(chm_mgr_t *p_chm) { #ifdef CONFIG_HAS_AUDIO bapp_t *p_app = (bapp_t*)p_chm->p_app; vch_t vch; st_t st; freq_t freq; if (!p_chm->curr_stream || !p_chm->curr_stream->stream) return eCHM_ERR_FAILED; if (!ch_map_get_current(bapp_cur_ch_map(p_app),&vch,&st,&freq)) { BDBG_WRN(("%s Invalid current channel or map.\n",__FUNCTION__)); return 0; } BDBG_WRN(("%s from 0x%04x to 0x%04x\n",__FUNCTION__,p_chm->mpeg[CURR_CH].audio[0].pid, vch.audio_pid[vch.cur_audio])); if (p_app->audio && (p_chm->mpeg[CURR_CH].audio[0].pid != 0)) { BDBG_WRN(("baudio_decode_stop 0x%04x\n",p_chm->mpeg[CURR_CH].audio[0].pid)); baudio_decode_stop(p_app->audio); } p_chm->mpeg[CURR_CH].audio[0].pid = vch.audio_pid[vch.cur_audio]; if (p_app->audio) { if (p_chm->mpeg[CURR_CH].audio[0].pid != 0) { BDBG_WRN(("baudio_decode_start 0x%04x\n",p_chm->mpeg[CURR_CH].audio[0].pid)); bstream_update_mpeg(p_chm->curr_stream->stream,&p_chm->mpeg[CURR_CH]); if (baudio_decode_start(p_app->audio, (void *)1, p_chm->curr_stream->stream)) { BDBG_WRN(("baudio_decode_start failed\n")); return eCHM_ERR_FAILED; } } } #endif return eCHM_ERR_OK; } #ifdef CONFIG_DVB_SUBTITLE static int chm_rotate_dsub(chm_mgr_t *p_chm) { bapp_t *p_app = (bapp_t *)p_chm->p_app; vch_t tmp_vch; st_t tmp_st; freq_t tmp_freq; if (p_app->dsub) { chm_subtitle_stop(p_chm); ch_map_get_current(bapp_cur_ch_map(p_app), &tmp_vch, &tmp_st, &tmp_freq); if (tmp_vch.dsub_pid[tmp_vch.cur_dsub]) { BDBG_WRN(("subtitle_start %d 0x%x", tmp_vch.cur_dsub, tmp_vch.dsub_pid[tmp_vch.cur_dsub])); chm_subtitle_start(p_chm, tmp_vch.dsub_pid[tmp_vch.cur_dsub]); } } return eCHM_ERR_OK; } #endif /* Summary: Open a stream given an mpeg program structure. */ static int chm_start_stream(chm_mgr_t *p_chm, chm_stream_t *p_stream,bstream_mpeg *p_mpeg) { BDBG_ASSERT(p_stream); BDBG_ASSERT(p_mpeg); if (p_stream->stream) { bstream_close(p_stream->stream); p_stream->stream = NULL; } p_stream->stream = bstream_open(p_chm->band,p_mpeg); if (p_stream->stream) { p_stream->freq_hz = p_chm->last_freq_hz; p_stream->pid = p_mpeg->video[0].pid; } return (p_stream->stream) ? eCHM_ERR_OK : eCHM_ERR_FAILED; } /* Summary: Close the stream. */ void chm_stop_stream(chm_mgr_t *p_chm, chm_stream_t *p_stream) { BDBG_ASSERT(p_stream); if (p_stream->stream) { bstream_close(p_stream->stream); p_stream->stream = NULL; p_stream->pid = 0; } } /* Summary: Stop unused stream. */ static void chm_stop_unused_streams(chm_mgr_t *p_chm) { int mpeg_idx,stream_idx; bool match; for (stream_idx = 0; stream_idx < BSETTOP_MAX_STREAMS; ++stream_idx) { match = false; for (mpeg_idx = 0; mpeg_idx < BSETTOP_MAX_STREAMS; ++mpeg_idx) { if (p_chm->mpeg[mpeg_idx].video[0].pid == p_chm->chm_stream[stream_idx].pid) { match = true; continue; } } if (!match) { chm_stop_stream(p_chm,&(p_chm->chm_stream[stream_idx])); } } } /* Summary: Find the stream with this PID . */ static chm_stream_t *chm_find_stream(chm_mgr_t *p_chm, unsigned short pid) { int stream_idx; BDBG_ASSERT(p_chm); for (stream_idx = 0; stream_idx < BSETTOP_MAX_STREAMS; ++stream_idx) { if (pid == p_chm->chm_stream[stream_idx].pid) { return &(p_chm->chm_stream[stream_idx]); } } return NULL; } /* Summary: Find an unused stream. */ static chm_stream_t *chm_find_unused_stream(chm_mgr_t *p_chm) { int stream_idx; BDBG_ASSERT(p_chm); for (stream_idx = 0; stream_idx < BSETTOP_MAX_STREAMS; ++stream_idx) { if (p_chm->chm_stream[stream_idx].stream == NULL) { return &(p_chm->chm_stream[stream_idx]); } } return NULL; } /* Summary: Start audio and video decode. Description: Handle tuning to the current channel. Returns non-zero on failure. */ static int chm_start_decode(chm_mgr_t *p_chm, bool force_restart) { bapp_t *p_app = (bapp_t*)p_chm->p_app; chm_stream_t *tmp_stream; if ((p_chm->mpeg[CURR_CH].audio[0].pid == 0x0000) && (p_chm->mpeg[CURR_CH].video[0].pid == 0x0000)) { BDBG_WRN(("%s (0x%04x,0x%04x,0x%04x,%d) failed \n", __func__,p_chm->mpeg[CURR_CH].video[0].pid,p_chm->mpeg[CURR_CH].pcr_pid, p_chm->mpeg[CURR_CH].audio[0].pid,p_chm->band)); return eCHM_ERR_FAILED; } if (p_chm->mpeg[CURR_CH].video[0].pid == 0x0000) { bdecode_config cfg; bdecode_get_config(p_app->decode,&cfg); cfg.mute = 1; cfg.channel_change = 0; bdecode_set_config(p_app->decode,&cfg); } /* Stop the current decode (but not the streams) */ chm_stop_decode(p_chm); /* Handle audio only decode */ if (p_app->audio && (p_chm->mpeg[CURR_CH].audio[0].pid != 0x0000) && (p_chm->mpeg[CURR_CH].video[0].pid == 0x0000)) { BDBG_WRN(("%s (0x%04x,0x%04x,0x%04x,%d) audio only decode \n", __func__,p_chm->mpeg[CURR_CH].video[0].pid,p_chm->mpeg[CURR_CH].pcr_pid, p_chm->mpeg[CURR_CH].audio[0].pid,p_chm->band)); chm_stop_stream(p_chm,&(p_chm->chm_stream[CURR_CH])); chm_stop_stream(p_chm,&(p_chm->chm_stream[PREV_CH])); chm_stop_stream(p_chm,&(p_chm->chm_stream[NEXT_CH])); if (chm_start_stream(p_chm,&(p_chm->chm_stream[CURR_CH]),&(p_chm->mpeg[CURR_CH]))) { BDBG_WRN(("chm_start_stream failed\n")); return eCHM_ERR_FAILED; } p_chm->curr_stream = &(p_chm->chm_stream[CURR_CH]); if (baudio_decode_start(p_app->audio, (void *)1, p_chm->curr_stream->stream)) { BDBG_WRN(("baudio_decode_start failed\n")); return eCHM_ERR_FAILED; } p_chm->decoding_audio = true; return eCHM_ERR_OK; } /* Check if all streams need to be restarted */ if (force_restart) { chm_stop_stream(p_chm,&(p_chm->chm_stream[CURR_CH])); chm_stop_stream(p_chm,&(p_chm->chm_stream[PREV_CH])); chm_stop_stream(p_chm,&(p_chm->chm_stream[NEXT_CH])); if (chm_start_stream(p_chm,&(p_chm->chm_stream[CURR_CH]),&(p_chm->mpeg[CURR_CH]))) { BDBG_WRN(("chm_start_stream failed\n")); return eCHM_ERR_FAILED; } p_chm->curr_stream = &(p_chm->chm_stream[CURR_CH]); if (p_chm->mpeg[NEXT_CH].video[0].pid != 0x0000) { if (chm_start_stream(p_chm,&(p_chm->chm_stream[NEXT_CH]),&(p_chm->mpeg[NEXT_CH]))) { BDBG_WRN(("chm_start_stream next failed\n")); } } if (p_chm->mpeg[PREV_CH].video[0].pid != 0x0000) { if (chm_start_stream(p_chm,&(p_chm->chm_stream[PREV_CH]),&(p_chm->mpeg[PREV_CH]))) { BDBG_WRN(("chm_start_stream prev failed\n")); } } } else { chm_stop_unused_streams(p_chm); p_chm->curr_stream = chm_find_stream(p_chm,p_chm->mpeg[CURR_CH].video[0].pid); if (p_chm->curr_stream == NULL) { if (chm_start_stream(p_chm,&(p_chm->chm_stream[CURR_CH]),&(p_chm->mpeg[CURR_CH]))) { BDBG_WRN(("chm_start_stream current failed\n")); return eCHM_ERR_FAILED; } p_chm->curr_stream = &(p_chm->chm_stream[CURR_CH]); } if (p_chm->mpeg[NEXT_CH].video[0].pid != 0x0000) { tmp_stream = chm_find_stream(p_chm,p_chm->mpeg[NEXT_CH].video[0].pid); if (tmp_stream == NULL) { tmp_stream = chm_find_unused_stream(p_chm); if (tmp_stream) { if (chm_start_stream(p_chm,tmp_stream,&(p_chm->mpeg[NEXT_CH]))) { BDBG_WRN(("chm_start_stream next failed\n")); } } } } if (p_chm->mpeg[PREV_CH].video[0].pid != 0x0000) { tmp_stream = chm_find_stream(p_chm,p_chm->mpeg[PREV_CH].video[0].pid); if (tmp_stream == NULL) { tmp_stream = chm_find_unused_stream(p_chm); if (tmp_stream) { if (chm_start_stream(p_chm,tmp_stream,&(p_chm->mpeg[PREV_CH]))) { BDBG_WRN(("chm_start_stream next failed\n")); } } } } } if (!p_chm->curr_stream) { BDBG_WRN(("%s current stream NULL\n",__FUNCTION__)); return eCHM_ERR_FAILED; } BDBG_WRN(("%s(0x%04x,0x%04x,0x%04x,%d)\n", __func__,p_chm->mpeg[CURR_CH].video[0].pid,p_chm->mpeg[CURR_CH].pcr_pid, p_chm->mpeg[CURR_CH].audio[0].pid,p_chm->band)); timing_profile_start(p_app->ptp_first_pts); timing_profile_start(p_app->ptp_decode); if (bdecode_start(p_app->decode, p_chm->curr_stream->stream, p_app->window[0])) { BDBG_WRN(("bdecode_start failed\n")); return eCHM_ERR_FAILED; } p_chm->decoding_video = true; if (p_app->audio && (p_chm->mpeg[CURR_CH].audio[0].pid != 0)) { if (baudio_decode_start(p_app->audio, (void *)1, p_chm->curr_stream->stream)) { BDBG_WRN(("baudio_decode_start failed\n")); return eCHM_ERR_FAILED; } } p_chm->decoding_audio = true; return eCHM_ERR_OK; } /* Summary: Stop audio and video decode. */ static int chm_stop_decode(chm_mgr_t *p_chm) { bapp_t *p_app = (bapp_t*)p_chm->p_app; chm_eas_stop(p_chm); chm_cvt_stop(p_chm); chm_nit_stop(p_chm); chm_ntt_stop(p_chm); chm_svct_stop(p_chm); chm_emm_stop(p_chm); chm_cat_stop(p_chm); #ifdef CONFIG_DVB chm_dvb_nit_stop(p_chm); chm_dvb_sdt_stop(p_chm); chm_dvb_tdt_stop(p_chm); chm_dvb_eit_stop(p_chm); #endif if (p_app->decode && p_chm->decoding_video) bdecode_stop(p_app->decode); if (p_app->audio && p_chm->decoding_audio) baudio_decode_stop(p_app->audio); p_chm->decoding_video = false; p_chm->decoding_audio = false; return 0; } /* Summary: Tune to the current frequency and enter steady channel processing state.. Description: Tune to the current frequency and enter steady channel processing state.. */ static int chm_tune_current(chm_mgr_t *p_chm) { unsigned int freq_hz; // bapp_t *p_app = (bapp_t*)p_chm->p_app; if (chm_check_cancel(p_chm)) return eCHM_ERR_CANCELED; chm_eas_stop(p_chm); chm_cvt_stop(p_chm); chm_nit_stop(p_chm); chm_ntt_stop(p_chm); chm_svct_stop(p_chm); chm_emm_stop(p_chm); #ifdef CONFIG_DVB chm_dvb_nit_stop(p_chm); chm_dvb_sdt_stop(p_chm); chm_dvb_tdt_stop(p_chm); chm_dvb_eit_stop(p_chm); #endif freq_hz = p_chm->freq.freq_khz * 1000; if (chm_tune_freq(p_chm,freq_hz,p_chm->freq.idx) != 0) { BDBG_WRN(("chm_tune_freq failed, try to retune and reacq")); /* force to re-tune */ p_chm->force_tune = true; if (chm_tune_freq(p_chm,freq_hz,p_chm->freq.idx) == 0) { return eCHM_ERR_OK; } BDBG_WRN(("chm_tune_freq failed\n")); return eCHM_ERR_FAILED; } return eCHM_ERR_OK; } /* Summary: Tune to the current frequency and get the VCT. Description: Tune to the current frequency and get the VCT. Returns non-zero on failure. */ static int chm_config_mpeg(chm_mgr_t *p_chm, vch_t *p_vch) { unsigned short ch_num,ch_num2; vch_t tmp_vch; st_t tmp_st; freq_t tmp_freq; bapp_t *p_app = (bapp_t*)p_chm->p_app; memset(&(p_chm->mpeg[CURR_CH]),0,sizeof(bstream_mpeg)); memset(&(p_chm->mpeg[PREV_CH]),0,sizeof(bstream_mpeg)); memset(&(p_chm->mpeg[NEXT_CH]),0,sizeof(bstream_mpeg)); p_chm->mpeg[CURR_CH].pcr_pid = p_vch->pcr_pid; p_chm->mpeg[CURR_CH].video[0].pid = p_vch->video_pid; p_chm->mpeg[CURR_CH].video[0].format = p_vch->video_type; p_chm->mpeg[CURR_CH].audio[0].pid = p_vch->audio_pid[p_vch->cur_audio]; p_chm->mpeg[CURR_CH].audio[0].format = p_vch->audio_type[p_vch->cur_audio]; if (p_chm->mpeg[CURR_CH].audio[0].pid == 0) { BDBG_WRN(("### All audio PIDs are 0x0000 ###\n")); } ch_num = ch_num2 = p_vch->ch_num; if (ch_map_set_next(bapp_cur_ch_map(p_app))) { if (ch_map_get_current(bapp_cur_ch_map(p_app),&tmp_vch,&tmp_st, &tmp_freq)) { if ((tmp_vch.freq_idx == p_vch->freq_idx) && (tmp_vch.ch_num != ch_num)/*&& (tmp_vch.video_type == BAVC_VideoCompressionStd_eMPEG2)*/) { p_chm->mpeg[NEXT_CH].video[0].pid = tmp_vch.video_pid; p_chm->mpeg[NEXT_CH].video[0].format = tmp_vch.video_type; p_chm->mpeg[NEXT_CH].audio[0].pid = tmp_vch.audio_pid[tmp_vch.cur_audio]; p_chm->mpeg[NEXT_CH].pcr_pid = tmp_vch.pcr_pid; p_chm->mpeg[NEXT_CH].audio[0].format = tmp_vch.audio_type[tmp_vch.cur_audio]; ch_num2 = tmp_vch.ch_num; } } } ch_map_set_ch(bapp_cur_ch_map(p_app),ch_num); if (ch_map_set_prev(bapp_cur_ch_map(p_app))) { if (ch_map_get_current(bapp_cur_ch_map(p_app),&tmp_vch,&tmp_st, &tmp_freq)) { if ((tmp_vch.freq_idx == p_vch->freq_idx) && (tmp_vch.ch_num != ch_num) && (tmp_vch.ch_num != ch_num2)/*&& (tmp_vch.video_type == BAVC_VideoCompressionStd_eMPEG2)*/) { p_chm->mpeg[PREV_CH].video[0].pid = tmp_vch.video_pid; p_chm->mpeg[PREV_CH].video[0].format = tmp_vch.video_type; p_chm->mpeg[PREV_CH].audio[0].pid = tmp_vch.audio_pid[tmp_vch.cur_audio]; p_chm->mpeg[PREV_CH].pcr_pid = tmp_vch.pcr_pid; p_chm->mpeg[PREV_CH].audio[0].format = tmp_vch.audio_type[tmp_vch.cur_audio]; } } } ch_map_set_ch(bapp_cur_ch_map(p_app),ch_num); return 0; } /* Summary: Tune to the current frequency and enter steady channel processing state.. Description: Tune to the current frequency and enter steady channel processing state.. */ int chm_tune(chm_mgr_t *p_chm) { int result = eCHM_ERR_FAILED; bool force_restart = false; bapp_t *p_app = (bapp_t*)p_chm->p_app; BDBG_WRN(("%s entry (line %d)\n",__FUNCTION__,__LINE__)); if (chm_check_cancel(p_chm)) { BDBG_WRN(("%s cancel detected at line %d\n",__FUNCTION__,__LINE__)); return eCHM_ERR_CANCELED; } if (!ch_map_get_current(bapp_cur_ch_map(p_app),&p_chm->vch,&p_chm->st,&p_chm->freq)) { BDBG_WRN(("%s ch_map_get_current failed\n",__FUNCTION__)); p_chm->cmd = eCHM_CANCEL; return eCHM_ERR_FAILED; } p_chm->vch.cur_audio = 0; force_restart = ((p_chm->freq.freq_khz * 1000) == p_chm->last_freq_hz) ? false : true; if (force_restart) chm_stop_decode(p_chm); timing_profile_start(p_app->ptp_tune); if ((result = chm_tune_current(p_chm)) != eCHM_ERR_OK) { BDBG_WRN(("%s chm_tune_current failed\n",__FUNCTION__)); timing_profile_stop(p_app->ptp_tune); return result; } timing_profile_stop(p_app->ptp_tune); #ifdef HAS_VBI if (p_chm->pscte_127) { scte_127_reset(p_chm->pscte_127); scte_127_enable(p_chm->pscte_127, false); chm_scte127_stop(p_chm); } #endif #ifdef CONFIG_DVB_SUBTITLE if (p_app->dsub) { chm_subtitle_stop(p_chm); } p_chm->vch.cur_dsub = 0; #endif if (chm_check_cancel(p_chm)) { BDBG_WRN(("%s cancel detected at line %d\n",__FUNCTION__,__LINE__)); return eCHM_ERR_CANCELED; } timing_profile_start(p_app->ptp_psi); /* if no pids exist get them */ if ((p_chm->vch.video_pid == 0) && (p_chm->vch.audio_pid[0] == 0) && (p_chm->vch.pcr_pid == 0)) { BDBG_WRN(("%s no PIDS\n",__FUNCTION__)); if ((result = chm_get_program(p_chm,&p_chm->vch)) != 0) { BDBG_WRN(("chm_get_program failed %d\n",result)); p_chm->chm_pat_evt.type = eCHM_EVT_STATUS; p_chm->chm_pat_evt.id = eCHM_STATUS_NOPAT; p_chm->chm_pat_evt.ticks = 0; chm_post_app_event(p_chm,&p_chm->chm_pat_evt); timing_profile_stop(p_app->ptp_psi); return result; } else { /* need to update the channel map - Alternate Audio pid selection fix */ ch_map_copy(&(p_chm->ch_map),bapp_cur_ch_map(p_app),0xF); if (bos_acquire_mutex(&(p_chm->ch_map_mutex),45) == b_ok) { if ((result = chm_update_programs(p_chm,p_chm->freq.idx)) != eCHM_ERR_OK) { BDBG_WRN(("chm_update_programs failed %d\n",result)); } bos_release_mutex(&(p_chm->ch_map_mutex)); p_chm->chm_pat_evt.type = eCHM_EVT_STATUS; p_chm->chm_pat_evt.id = eCHM_STATUS_PAT; p_chm->chm_pat_evt.ticks = 0; chm_post_app_event(p_chm,&p_chm->chm_pat_evt); p_chm->chmap_evt.ticks = 0x82; p_chm->chmap_evt.type = eCHM_EVT_STATUS; p_chm->chmap_evt.id = eCHM_STATUS_CHMAP; chm_post_app_event(p_chm, &p_chm->chmap_evt); bos_sleep(100); } else { BDBG_WRN(("%s:%d failed to acquire channel map mutex\n", __FUNCTION__,__LINE__)); } } } else { p_chm->pmt_crc = p_chm->pat_crc = 0; p_chm->chm_pat_evt.type = eCHM_EVT_STATUS; p_chm->chm_pat_evt.id = eCHM_STATUS_NOPAT; p_chm->chm_pat_evt.ticks = 0; chm_post_app_event(p_chm,&p_chm->chm_pat_evt); } timing_profile_stop(p_app->ptp_psi); if (chm_check_cancel(p_chm)) { BDBG_WRN(("%s cancel detected at line %d\n",__FUNCTION__,__LINE__)); return eCHM_ERR_CANCELED; } chm_stream_close(p_chm); /* Do not start decode if PIDs are all 0. Doing so can prevent SI filtering from operating correctly. */ if ((p_chm->vch.video_pid != 0) || (p_chm->vch.audio_pid[0] != 0) || (p_chm->vch.pcr_pid != 0)) { p_app->lang = chm_config_mpeg(p_chm,&p_chm->vch); if ((result = chm_start_decode(p_chm,force_restart)) != 0) { BDBG_WRN(("chm_start_decode failed\n")); return result; } } if (chm_check_cancel(p_chm)) { BDBG_WRN(("%s cancel detected at line %d\n",__FUNCTION__,__LINE__)); return eCHM_ERR_CANCELED; } if (result == 0) { /* start decode success so transition to info acquire state */ p_chm->cmd = eCHM_INFO; } return result; } /* Summary: Do a tune for the purpose of checking the signal strength. */ int chm_check_signal(chm_mgr_t *p_chm) { int result = eCHM_ERR_FAILED; btuner_status status; bapp_t *p_app = (bapp_t*)p_chm->p_app; unsigned int freq_hz; if (chm_check_cancel(p_chm)) return eCHM_ERR_CANCELED; if (!ch_map_get_current(bapp_cur_ch_map(p_app),&p_chm->vch,&p_chm->st,&p_chm->freq)) { BDBG_WRN(("%s ch_map_get_current failed\n",__FUNCTION__)); return eCHM_ERR_FAILED; } freq_hz = p_chm->freq.freq_khz * 1000; if (chm_tune_freq(p_chm,freq_hz,p_chm->freq.idx) != 0) { p_chm->signal_evt.type = eCHM_EVT_SIGNAL; p_chm->signal_evt.power = 0; p_chm->signal_evt.SNR = 0; p_chm->signal_evt.freq_hz = freq_hz; p_chm->signal_evt.lock = 0; chm_post_app_event(p_chm,(chm_event_t*)&p_chm->signal_evt); BDBG_WRN(("bapp_tune_current_freq failed\n")); return result; } while (!chm_check_cancel(p_chm)) { /* check power level only if we are in tuner status screen */ if ((p_app->screen_id == eSCREEN_ANTENNA) || (p_app->screen_id == eSCREEN_TUNER_STATUS) || (p_app->screen_id == eSCREEN_BANNER) || (p_app->screen_id == eSCREEN_BANNER_SMALL)) { status.get_power = true; } else { status.get_power = false; } if (btuner_get_status(p_chm->tuner,&status) == b_ok) { p_chm->signal_evt.type = eCHM_EVT_SIGNAL; p_chm->signal_evt.power = status.power; p_chm->signal_evt.SNR = status.snr; p_chm->signal_evt.freq_hz = freq_hz; //p_chm->signal_evt.qam_b_mode = status.mode; p_chm->signal_evt.lock = status.lock; chm_post_app_event(p_chm,(chm_event_t*)&p_chm->signal_evt); } bos_sleep(50); } return result; } #ifdef HAS_VBI /* Summary: Start SCTE 127 monitoring... */ static int chm_scte127_start(chm_mgr_t *p_chm, uint16_t scte127_pid) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; BDBG_WRN(("#### SCTE127 0x%x\n", scte127_pid)); if (!p_chm->pscte_127) { BDBG_ERR(("%s:%d: SCTE 127 module is not opened",__FILE__, __LINE__)); return eCHM_ERR_FAILED; } //chm_scte127_stop(p_chm); /* clear any pending queue if any */ scte_127_reset(p_chm->pscte_127); scte_127_enable(p_chm->pscte_127, true); //p_chm->scte127_msg = smessage_open(bmessage_format_ts); p_chm->scte127_msg = smessage_open(smessage_format_tsc); if (NULL == p_chm->scte127_msg) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->scte127_msg); params.band = p_chm->band; params.pid = scte127_pid; params.buffer = p_chm->scte127_buf; params.buffer_size = p_chm->scte127_buf_size; /* processing is done in callback */ params.data_ready_callback = chm_smessage_scte127_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; params.crc_disabled = true; if (b_ok != smessage_start(¶ms, p_chm->scte127_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } #endif /* Summary: Start EAS monitoring.. */ static int chm_eas_start(chm_mgr_t *p_chm, unsigned short network_pid) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; chm_eas_stop(p_chm); p_chm->eas_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->eas_msg) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->eas_msg); params.band = p_chm->band; params.pid = 0x1ffb; params.filter.coef[0] = 0xD8; params.filter.mask[0] = 0x00; params.filter.excl[0] = 0xff; /* All PSIP tables have protocol_version which must be = 0 */ params.filter.coef[8] = 0x00; /* protocol_version */ params.filter.mask[8] = 0x00; params.buffer = p_chm->eas_buf; params.buffer_size = EAS_BUF_LEN; /* processing is done in callback */ params.data_ready_callback = chm_smessage_eas_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; if (b_ok != smessage_start(¶ms, p_chm->eas_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } p_chm->eas_timer_started = false; ExitFunc: return cerr; } /* Summary: Start CVT monitoring.. */ static int chm_cvt_start(chm_mgr_t *p_chm, unsigned short network_pid) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; BDBG_MSG(("%s:%d (0x%04x)",__FUNCTION__, __LINE__,network_pid)); chm_cvt_stop(p_chm); p_chm->cvt_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->cvt_msg) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->cvt_msg); params.band = p_chm->band; params.pid = (uint16_t)network_pid; params.filter.coef[0] = 0xD9; params.filter.mask[0] = 0x00; params.filter.coef[3] = 0x9f; params.filter.mask[3] = 0x00; params.filter.coef[4] = 0x9C; params.filter.mask[4] = 0x00; //params.filter.coef[5] = 0x05; //params.filter.mask[5] = 0x00; //params.filter.coef[5] = 0x00; //params.filter.mask[5] = 0x07; /* APDU 0-5, will let cvt_parse to filter out tables other than 2 and 5 */ params.buffer = p_chm->cvt_buf; params.buffer_size = CVT_BUF_LEN; /* processing is done in callback */ params.data_ready_callback = chm_smessage_cvt_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; if (b_ok != smessage_start(¶ms, p_chm->cvt_msg)) { BDBG_ERR(("%s:%d",__FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } /****************************************************************************** * INPUTS: context * OUTPUTS: none. * RETURNS: none * FUNCTION: message_callback * DESCRIPTION: Callback function normally called when a complete message has * been received and passed the filter criteria. ******************************************************************************/ void chm_download(chm_mgr_t * p_chm) { bapp_t *p_app; unsigned int freq_hz; vch_t vch; freq_t freq; st_t st; unsigned short ch_num; unsigned short PID; struct image_t * image; size_t retry; bresult bres; p_app = (bapp_t*)p_chm->p_app; /* disable all message processing during download */ chm_eas_stop(p_chm); chm_stt_stop(p_chm); chm_cvt_stop(p_chm); chm_nit_stop(p_chm); chm_svct_stop(p_chm); chm_ntt_stop(p_chm); chm_emm_stop(p_chm); ch_map_get_current(bapp_cur_ch_map(p_app), &vch,&st,&freq); ch_num = vch.ch_num; PID = 0xFFFF; freq_hz = (unsigned int)-1; BDBG_ERR(("#### DSMCC DOWNLOAD ####\n")); switch (p_chm->cvt.location_type) { case 0: if (!ch_map_set(bapp_cur_ch_map(p_app),p_chm->cvt.source_ID)) { BDBG_WRN(("#### CVT Failed to locate download source ID = 0x%04x.#### \n", p_chm->cvt.source_ID)); break; } else { if (!ch_map_get_current(&(p_app->settings.ch_map), &vch,&st,&freq)) { BDBG_WRN(("#### CVT Could not get frequency for = 0x%04x #### \n", p_chm->cvt.source_ID)); break; } freq_hz = freq.freq_khz * 1000; } break; case 1: freq_hz = p_chm->cvt.frequency_vector * CHM_VECTOR_MULTIPLIER; PID = p_chm->cvt.pid; break; case 2: freq_hz = p_chm->cvt.frequency_vector * CHM_VECTOR_MULTIPLIER; vch.program_num = p_chm->cvt.program_number; break; } if (freq_hz == (unsigned int)-1) goto failed; BDBG_WRN(("#### CVT Tune %d #### \n", freq.freq_khz * 1000)); if (chm_tune_freq(p_chm,freq_hz,freq.idx) != eCHM_ERR_OK) goto failed; if (PID == 0xFFFF) { if (chm_get_program_pid(p_chm,p_chm->band, vch.program_num,&PID ) != eCHM_ERR_OK) { BDBG_WRN(("#### CVT Could not find program %d for source_ID 0x%04x, freq = %d\n", vch.program_num,p_chm->cvt.source_ID,freq_hz)); goto failed; } } if (image_init() != b_ok) { BDBG_WRN(("#### CVT Download Init Failed #### \n")); goto failed; } retry = 3; while (0 != retry) { if ( image_receive(p_chm->band,PID,&image, p_chm->cvt.code_file_name_length, &(p_chm->cvt.code_file_name[0])) == b_ok) { break; } BDBG_WRN(("#### CVT Download Failed #### \n")); --retry; } if (0 == retry) { goto failed; } chm_stop_decode(p_chm); bres = chm_write_software(image); if (b_ok != bres) { goto failed; } /* restore channel number to previous video channel */ ch_map_set_ch(bapp_cur_ch_map(p_app),ch_num); p_app->settings_dirty |= DIRTY_CHANNEL; bapp_save_settings(p_app); chip_reset(); failed: p_chm->cmd = eCHM_TUNE; ch_map_set_ch(bapp_cur_ch_map(p_app),ch_num); } /* Summary: Steady state loop.. */ int chm_start_scte65(chm_mgr_t *p_chm,unsigned short network_pid) { bapp_t *p_app = (bapp_t*)p_chm->p_app; chm_emm_start(p_chm,p_app->settings.EMM_PID); if (!p_app->settings.psi_scan) { ch_map_reset(&(p_chm->ch_map)); chm_stt_start(p_chm,network_pid); chm_cvt_start(p_chm,network_pid); chm_nit_start(p_chm, network_pid); chm_svct_start(p_chm, network_pid); chm_ntt_start(p_chm, network_pid); } return eCHM_PROG_IDLE; } #define CHECK_PROGRAM #ifdef CHECK_PROGRAM /* Summary: check the program for changes.. */ int chm_check_program(chm_mgr_t *p_chm) { int result = eCHM_PROG_IDLE; bapp_t *p_app = (bapp_t*)p_chm->p_app; vch_t *vch; unsigned int pmt_crc,pat_crc; bool restart = false; pmt_crc = p_chm->pmt_crc; pat_crc = p_chm->pat_crc; /* temporary until we do smart PAT */ vch = &p_chm->vch; BDBG_MSG(("***** chm_check_program *****\n")); if ((result = chm_get_program(p_chm,vch)) != 0) { BDBG_WRN(("chm_get_program failed %d\n",result)); return eCHM_PROG_IDLE; /* return state expected */ } else { if (p_chm->network_pid != p_app->settings.network_PID) { /* Send message to app */ p_chm->chm_network_evt.type = eCHM_EVT_NETWORK; p_chm->chm_network_evt.id = p_chm->network_pid; chm_post_app_event(p_chm,&p_chm->chm_network_evt); result = eCHM_PROG_SCTE65; } /* Check the PIDs match vcm PIDs */ if ((pat_crc != p_chm->pat_crc) || (pmt_crc != p_chm->pmt_crc)) { if (vch->pcr_pid != p_chm->vch.pcr_pid) { BDBG_WRN(("VCM PCR PIDs do not match(0x%04x != 0x%04x)\n",vch->pcr_pid, p_chm->vch.pcr_pid)); p_chm->vch.pcr_pid = vch->pcr_pid; restart = true; } if (vch->video_pid != p_chm->vch.video_pid) { BDBG_WRN(("VCM Video PIDs do not match(0x%04x != 0x%04x)\n",vch->video_pid, p_chm->vch.video_pid)); p_chm->vch.video_pid = vch->video_pid; restart = true; } if ((vch->num_audio < p_chm->vch.cur_audio) || (p_chm->vch.audio_pid[p_chm->vch.cur_audio] != vch->audio_pid[p_chm->vch.cur_audio])) { /*Fixed error where cur_audio was set to num_audio. */ if (p_chm->vch.cur_audio >= vch->num_audio) p_chm->vch.cur_audio = 0; BDBG_WRN(("VCM Audio PID no longer valid(0x%04x != 0x%04x)\n", vch->audio_pid[p_chm->vch.cur_audio],p_chm->vch.audio_pid[p_chm->vch.cur_audio])); memcpy(p_chm->vch.audio_pid,vch->audio_pid,sizeof(unsigned short) * MAX_AUDIO_PIDS); memcpy(p_chm->vch.audio_type,vch->audio_type,sizeof(unsigned char) * MAX_AUDIO_PIDS); memcpy(p_chm->vch.audio_lang,vch->audio_lang,sizeof(char) * MAX_AUDIO_PIDS * 3); restart = true; } #ifdef CONFIG_DVB_SUBTITLE if ((vch->num_dsub < p_chm->vch.cur_dsub) || (p_chm->vch.dsub_pid[p_chm->vch.cur_dsub] != vch->dsub_pid[p_chm->vch.cur_dsub])) { if (p_chm->vch.cur_dsub >= vch->num_dsub) p_chm->vch.cur_dsub = 0; BDBG_WRN(("VCM DSUB PID no longer valid (0x%04x != 0x%04x)", vch->dsub_pid[p_chm->vch.cur_dsub], p_chm->vch.dsub_pid[p_chm->vch.cur_dsub])); BKNI_Memcpy(p_chm->vch.dsub_pid, vch->dsub_pid, sizeof(unsigned short)*MAX_DSUB_PIDS); BKNI_Memcpy(p_chm->vch.dsub_lang, vch->dsub_lang, sizeof(unsigned char)*MAX_DSUB_PIDS*3); BKNI_Memcpy(p_chm->vch.dsub_page, vch->dsub_page, sizeof(unsigned short)*MAX_DSUB_PIDS); restart = true; } #endif if (restart) { chm_stop_decode(p_chm); p_app->lang = chm_config_mpeg(p_chm,&p_chm->vch); if ((result = chm_start_decode(p_chm,true)) != 0) { BDBG_WRN(("%s chm_start_decode failed\n",__FUNCTION__)); } } } else { BDBG_MSG(("####pat_crc or pmt_crc not match####\n")); } } return eCHM_PROG_IDLE; } #endif /* Summary: Steady state loop.. */ int chm_info(chm_mgr_t *p_chm) { bapp_t *p_app = (bapp_t*)p_chm->p_app; p_chm->network_pid = p_app->settings.network_PID; while (!chm_check_cancel(p_chm)) { chm_state_t curstate = p_chm->state; switch (curstate) { case eCHM_PROG_INIT: chm_eas_start(p_chm, 0x1FFB); p_chm->state = eCHM_PROG_CAT; break; case eCHM_PROG_CAT: chm_cat_start(p_chm); if (p_chm->network_pid != 0) p_chm->state = eCHM_PROG_SCTE65; else p_chm->state = eCHM_PROG_IDLE; break; case eCHM_PROG_SCTE65: p_chm->state = chm_start_scte65(p_chm,p_chm->network_pid); break; case eCHM_PROG_IDLE: { static unsigned int last_signal_check = 0; // static unsigned int last_chmap_check = 0; #ifdef CHECK_PROGRAM static unsigned int last_prog_check = 0; if (last_prog_check < bos_getticks()) { p_chm->state = chm_check_program(p_chm); #ifdef HAS_VBI /* with latest changes, the scte127_pid could be 0 when in eCHM_PROG_INIT state, so check here again */ if (p_chm->pscte_127 && p_chm->vch.scte127_pid && !p_chm->scte127_msg) { BDBG_WRN(("Start SCTE 127 packet checking...")); chm_scte127_start(p_chm, p_chm->vch.scte127_pid); } #endif last_prog_check = bos_getticks() + MS_TO_TICKS(CHM_CHECK_PROG_FREQ); } #endif #ifdef CONFIG_DVB_SUBTITLE if (p_app->dsub && p_chm->vch.dsub_pid[p_chm->vch.cur_dsub] && !p_chm->subtitle_msg) { vch_t tmp_vch; st_t tmp_st; freq_t tmp_freq; ch_map_get_current(bapp_cur_ch_map(p_app), &tmp_vch, &tmp_st, &tmp_freq); if (tmp_vch.dsub_pid[tmp_vch.cur_dsub]) { BDBG_WRN(("%s start dvb subtitle %d %x", __FUNCTION__, tmp_vch.cur_dsub, tmp_vch.dsub_pid[tmp_vch.cur_dsub])); chm_subtitle_start(p_chm, tmp_vch.dsub_pid[tmp_vch.cur_dsub]); } } #endif #ifdef CONFIG_DVB if (!p_chm->dvb_eit_msg) chm_dvb_eit_start(p_chm, p_chm->band); if (!p_chm->dvb_tdt_msg) chm_dvb_tdt_start(p_chm, p_chm->band); #endif if (last_signal_check < bos_getticks()) { chm_send_status(p_chm,0,true); last_signal_check = bos_getticks() + MS_TO_TICKS(CHM_CHECK_SIGNAL_FREQ); } p_chm->chmap_evt.ticks = 0; p_chm->chmap_evt.type = eCHM_EVT_STATUS; p_chm->chmap_evt.id = eCHM_STATUS_CHMAP; /* Compare the NIT - MMS to the current one */ if (p_chm->mms_changed) { if (ch_map_cmp_mms(&(p_chm->ch_map.mms_map),&(p_app->settings.ch_map.mms_map)) != 0) p_chm->chmap_evt.ticks |= 0x8; } /* Compare the NIT - CDS to the current one */ if (p_chm->cds_changed) { if (ch_map_cmp_freq(&(p_chm->ch_map.freq_map),&(p_app->settings.ch_map.freq_map)) != 0) p_chm->chmap_evt.ticks |= 0x1; } /* Compare the NTT to the current one */ if (p_chm->sns_changed) { if (ch_map_cmp_st(&(p_chm->ch_map.st_map),&(p_app->settings.ch_map.st_map)) != 0) p_chm->chmap_evt.ticks |= 0x4; } /* Compare the SVCT to the current one */ if (p_chm->vcm_changed) { if (bos_acquire_mutex(&(p_chm->ch_map_mutex),45) == b_ok) { if (chm_update_programs(p_chm,p_chm->freq.idx) != eCHM_ERR_OK) { BDBG_WRN(("chm_update_programs failed %s\n",__FUNCTION__)); } bos_release_mutex(&(p_chm->ch_map_mutex)); } else { BDBG_WRN(("%s:%d failed to acquire channel map mutex\n", __FUNCTION__,__LINE__)); } if (ch_map_cmp_vch(&(p_chm->ch_map.vch_map),&(p_app->settings.ch_map.vch_map)) != 0) p_chm->chmap_evt.ticks |= 0x2; } if (p_chm->chmap_evt.ticks) { /* copy channel map so application can perform update without risk of modification by channel manager */ chm_post_app_event(p_chm,&p_chm->chmap_evt); p_chm->state = eCHM_PROG_UPDATING; /* p_chm->xxx_changed flags will be reset when filters are restarted */ } } break; /* Give application time to update channel map before searching for SCTE65 again */ case eCHM_PROG_UPDATING: { static unsigned int updating_check = 0; if (updating_check < bos_getticks()) { if (updating_check == 0) { updating_check = bos_getticks() + MS_TO_TICKS(CHM_UPDATING_DELAY); } else { updating_check = 0; p_chm->state = eCHM_PROG_SCTE65; } } break; } } bos_sleep(CHM_IDLE_TIMEOUT); if (p_chm->state == curstate) { BDBG_MSG(("p_chm->state is still %d\n",p_chm->state)); } else { BDBG_MSG(("chm state changed from %d to %d\n",curstate,p_chm->state)); } } return eCHM_ERR_OK; } /* Summary: Channel Manager Task. */ static void chm_handler(void *data) { chm_cmd_t orig_cmd; chm_mgr_t *p_chm = (chm_mgr_t *)data; chm_cmd_event_t *p_cmd; while (1) { orig_cmd = p_chm->cmd; switch (p_chm->cmd) { case eCHM_CANCEL: p_cmd = (chm_cmd_event_t*)bos_pend_event(p_chm->queue,-1); if (p_cmd == NULL) break; p_chm->cmd = (chm_cmd_t)p_cmd->cmd_id; break; case eCHM_SAP: p_chm->cmd = eCHM_CANCEL; break; case eCHM_STOP: chm_stop_decode(p_chm); p_chm->cmd = eCHM_CANCEL; break; case eCHM_TUNE: chm_tune(p_chm); break; case eCHM_HUNT: p_chm->hunt_cnt = 0; p_chm->cmd = eCHM_HUNTING; break; case eCHM_HUNTING: chm_hunt(p_chm); break; case eCHM_PENDING_INIT: chm_pending_init(p_chm); break; case eCHM_INFO: p_chm->state = eCHM_PROG_INIT; chm_info(p_chm); break; case eCHM_CHECK_SIGNAL: chm_check_signal(p_chm); break; case eCHM_DOWNLOAD: chm_download(p_chm); break; case eCHM_DSUB: #ifdef CONFIG_DVB_SUBTITLE p_chm->cmd = eCHM_CANCEL; #endif break; } if (orig_cmd != p_chm->cmd) { BDBG_WRN(("CHM CMD changed from %s to %s\n", s_chm_cmd_name[orig_cmd],s_chm_cmd_name[p_chm->cmd])); } /* Yield a little */ bos_sleep(40); } } /* Summary: Initialize the channel manager. */ void chm_init(chm_mgr_t *p_chm, void *p_app) { b_task_params params; bresult rc; memset(p_chm,0,sizeof(chm_mgr_t)); p_chm->p_app = p_app; p_chm->fe_type = eFREQ_TABLE_NORTH_AMERICA_QAM; rc = bos_create_queue(&p_chm->queue,p_chm->event,MAX_CHM_EVENT); BDBG_ASSERT(rc == b_ok); rc = bos_create_queue(&p_chm->gen_queue,p_chm->gen_event,MAX_MSG_EVENTS); BDBG_ASSERT(rc == b_ok); rc = bos_create_mutex(&p_chm->mutex); BDBG_ASSERT(rc == b_ok); rc = bos_create_mutex(&p_chm->ch_map_mutex); BDBG_ASSERT(rc == b_ok); ch_map_init(&(p_chm->ch_map),&(p_chm->ch_map_mutex)); p_chm->stack = malloc(CHM_INT_STK_SIZE * 4); BDBG_ASSERT(p_chm->stack); params.priority = CHM_INT_PRIORITY; params.stack = p_chm->stack; params.name = "chan_mgr"; params.stack_size = CHM_INT_STK_SIZE; p_chm->svct_buf = malloc(SVCT_BUF_LEN); p_chm->pat_buf = malloc(PAT_BUF_LEN); p_chm->cat_buf = malloc(CAT_BUF_LEN); p_chm->pmt_buf = malloc(PMT_BUF_LEN); p_chm->stt_buf = malloc(STT_BUF_LEN); p_chm->nit_buf = malloc(NIT_BUF_LEN); p_chm->ntt_buf = malloc(NTT_BUF_LEN); p_chm->eas_buf = malloc(EAS_BUF_LEN); p_chm->cvt_buf = malloc(EAS_BUF_LEN); p_chm->emm_buf = malloc(EMM_BUF_LEN); #ifdef CONFIG_DVB p_chm->dvb_nit_buf = malloc(NIT_BUF_LEN); p_chm->dvb_sdt_buf = malloc(SDT_BUF_LEN); p_chm->dvb_eit_buf = malloc(EIT_BUF_LEN); p_chm->dvb_tdt_buf = malloc(TDT_BUF_LEN); p_chm->dvb_sdt_cb.data = p_chm; p_chm->dvb_sdt_cb.cb = chm_dvb_sdt_cb; p_chm->dvb_nit_cb.data = p_chm; p_chm->dvb_nit_cb.cb = chm_dvb_nit_cb; dvb_nit_init(&p_chm->dvb_nit_cb); dvb_sdt_init(&p_chm->dvb_sdt_cb); dvb_eit_init(); dvb_tdt_init(); #endif BDBG_ASSERT(p_chm->svct_buf && p_chm->pat_buf && p_chm->pmt_buf && p_chm->stt_buf && p_chm->nit_buf && p_chm->ntt_buf && p_chm->cat_buf); p_chm->vcm_cb.data = p_chm; p_chm->vcm_cb.cb = chm_vcm_cb; p_chm->sns_cb.data = p_chm; p_chm->sns_cb.cb = chm_sns_cb; p_chm->cds_cb.data = p_chm; p_chm->cds_cb.cb = chm_cds_cb; p_chm->mms_cb.data = p_chm; p_chm->mms_cb.cb = chm_mms_cb; SI_SVCT_Init (&p_chm->vcm_cb,0xFF); SI_NTT_Init (&p_chm->sns_cb,0xFF); SI_NIT_Init (&p_chm->cds_cb,&p_chm->mms_cb,0xFF,0xFF); SI_STT_Init (); #ifdef HAS_VBI p_chm->pscte_127 = scte_127_init(); BDBG_ASSERT(p_chm->pscte_127); s_pscte_127 = p_chm->pscte_127; p_chm->scte127_buf_size = 188 * 1; /* currently 1 packet */ p_chm->scte127_buf = malloc(p_chm->scte127_buf_size); BDBG_ASSERT(p_chm->scte127_buf); #endif #ifdef CONFIG_DVB_SUBTITLE p_chm->subtitle_buf_size = 188*1; p_chm->subtitle_buf = malloc(p_chm->subtitle_buf_size); #endif bstream_mpeg_init(&p_chm->mpeg[CURR_CH]); bstream_mpeg_init(&p_chm->mpeg[NEXT_CH]); bstream_mpeg_init(&p_chm->mpeg[PREV_CH]); p_chm->tuner = btuner_open((0)); BAPP_ASSERT(p_chm->tuner); #ifdef BQAM_SCRIPT #if (BCHP_CHIP == 3543) p_chm->tuner_params.power_tune = true; /* conduct a power tune in first time */ #endif #endif p_chm->smsg = smessage_open(smessage_format_psi); BAPP_ASSERT(p_chm->smsg); p_chm->vendor_id = DEF_VENDOR_ID; p_chm->hardware_version_id = DEF_HARDWARE_ID; p_chm->cvt_valid = false; #if defined(ENABLE_UMESSAGE) cmode_init(); #endif bos_start_task(&p_chm->task,¶ms,chm_handler,p_chm); #ifdef BCM_DEBUG BDBG_SetModuleLevel("chan_mgr",BDBG_eWrn); #endif } /* Summary: Function to pass a command to the channel manager. Any results and notification is handled by passing events to the app via the msg_queue. */ void chm_cmd(chm_mgr_t *p_chm,chm_cmd_event_t *p_cmd_evt) { chm_cmd_t cmd; BAPP_ASSERT(p_chm); BAPP_ASSERT(p_cmd_evt); cmd = p_cmd_evt->cmd_id; /* Handle special case to rotate audio */ if ((cmd == eCHM_SAP) && (p_chm->cmd != eCHM_CANCEL)) { BDBG_WRN(("Command %s, during %s\n",s_chm_cmd_name[cmd], s_chm_cmd_name[p_chm->cmd])); bos_post_event(p_chm->queue,(b_event_t*)p_cmd_evt); return; } #ifdef CONFIG_DVB_SUBTITLE if ((cmd == eCHM_DSUB) && (p_chm->cmd != eCHM_CANCEL)) { BDBG_WRN(("Command %s, during %s", s_chm_cmd_name[cmd], s_chm_cmd_name[p_chm->cmd])); bos_post_event(p_chm->queue,(b_event_t*)p_cmd_evt); return; } #endif if ((cmd != eCHM_CANCEL) && (p_chm->cmd != eCHM_CANCEL)) { BDBG_WRN(("Canceling previous command %s for %s\n", s_chm_cmd_name[p_chm->cmd],s_chm_cmd_name[cmd])); p_chm->cancel_cmd.cmd_id = eCHM_CANCEL; bos_post_event(p_chm->queue,(b_event_t*)&p_chm->cancel_cmd); bos_sleep(40); /* yield so thread can process event right away */ } bos_post_event(p_chm->queue,(b_event_t*)p_cmd_evt); bos_sleep(10); /* yield so thread can process event right away */ } bresult chm_write_software(struct image_t * image) { return image_write(image); } #define CHM_SC_MASK 0xc0 #define CHM_SC_BUFFER_SIZE (188*10) static unsigned char chm_sc_buffer[CHM_SC_BUFFER_SIZE]; static void * chm_sc_callback(void * c, size_t msg_size) { unsigned int * pdata_ready = (unsigned int *)c; *pdata_ready = msg_size; return NULL; } bool chm_check_sc(chm_mgr_t *p_chm, bband_t band, unsigned short pid) { smessage_stream_t ms; smessage_stream_params_t params; bresult bres; bool result; unsigned int count; volatile unsigned int data_ready; unsigned char * walker; result = false; ms = smessage_open(smessage_format_tsc); if (NULL == ms) { BDBG_ERR(("%s : smessage_open failed", __PRETTY_FUNCTION__)); goto ExitFunc; } smessage_stream_params_init(¶ms, ms); params.band = band; params.pid = pid; params.buffer_size = CHM_SC_BUFFER_SIZE; params.buffer = chm_sc_buffer; params.data_ready_callback = chm_sc_callback; params.overflow = chm_sc_callback; params.callback_context = (void *)&data_ready; data_ready = 0; bres = smessage_start(¶ms, ms); if (b_ok != bres) { smessage_close(ms); BDBG_ERR(("%s : smessage_start", __PRETTY_FUNCTION__)); goto ExitFunc; } /* Updating unsigned int is atomic operation on mips. We can just wait for callback to update our flag when data is ready */ count = 3; do { bos_sleep(50); count--; }while ((0 == data_ready)&&(0 != count)); bres = smessage_stop(ms); if (b_ok != bres) { smessage_close(ms); BDBG_ERR(("%s : smessage_stop", __PRETTY_FUNCTION__)); goto ExitFunc; } smessage_close(ms); /* Figure out if we got data and if sc flags are set */ if (0 != data_ready) { /* We have data. Check few packets for sc bits */ walker = chm_sc_buffer; for (count = 0; count < 5; count++) { if (0 != (walker[3] & CHM_SC_MASK)) { goto ExitFunc; } walker += 188; } result = true; } ExitFunc: BDBG_ERR(("pid %d %s", pid, (result == true ? "clear" : "encr"))); return result; } #ifdef CONFIG_DVB /* * Update the source desc entry * If NIT delivers the channel list about SDT's service id, just add or update st * Otherwise add vch entry first and then insert SDT entry */ static void chm_dvb_sdt_cb(sdt_t *sdt, void *data) { chm_mgr_t *p_chm = (chm_mgr_t *)data; vch_t vch; st_t st; int freq_idx; if (!p_chm->ch_map.freq_map.num_freq) { BDBG_WRN(("frequency map doesn't exist")); return; } for (freq_idx=0; freq_idxch_map.freq_map.num_freq; freq_idx++) { if (p_chm->ch_map.freq_map.freq[freq_idx].freq_khz == sdt->frequency) break; } if (freq_idx == p_chm->ch_map.freq_map.num_freq) { BDBG_WRN(("frequency map is not matched with sdt's frequency")); return; } vch.program_num = sdt->service_id; /* some stream didn't deliver correct channel list in nit. * in this case, add vch based on the SDT table */ if (ch_map_is_new_vch(&(p_chm->ch_map), &vch)) { if (freq_idx == p_chm->ch_map.freq_map.num_freq) { BDBG_WRN(("couldn't find frequency entry for SDT entry")); return; } BKNI_Memset(&vch, 0, sizeof(vch_t)); vch.freq_idx = freq_idx; vch.ch_num = (unsigned short)s_channel_num++; vch.source_id = sdt->service_id; vch.mms_idx = freq_idx; //p_chm->ch_map.mms_map.num_mms-1; vch.program_num = sdt->service_id; vch.vchflags = 0; ch_map_add_vch(&(p_chm->ch_map),&vch); } BKNI_Memset(&st, 0, sizeof(st_t)); st.source_id = sdt->service_id; if (sdt->service_name[0]) { if (sdt->service_name[0] < 0x20) BKNI_Memcpy(st.name, &sdt->service_name[1], strlen(sdt->service_name)-1); else BKNI_Memcpy(st.name, sdt->service_name, strlen(sdt->service_name)); } else { snprintf(st.name,MAX_VCH_LEN,"Ch - %d.%d",vch.freq_idx + 2,vch.program_num); } ch_map_add_update_st(&(p_chm->ch_map),&st); } /* * NIT delivers * - list of frequency where system is supporting * - list of services even though service is in other frequency */ static void chm_dvb_nit_cb(nit_t *nit, void *data) { chm_mgr_t *p_chm = (chm_mgr_t *)data; freq_t freq; vch_t vch; st_t st; mms_t mms; int i; if (nit && nit->frequency) { freq.idx = (unsigned char)p_chm->ch_map.freq_map.num_freq; freq.freq_khz = nit->frequency; ch_map_add_freq(&(p_chm->ch_map), &freq); BKNI_Memset(&mms, 0, sizeof(mms)); mms.idx = freq.idx; mms.symbol_rate = nit->symbol_rate; switch(nit->symbol_rate) { case eQAM_64: mms.modulation = eQAM_64; break; case eQAM_256: case eQAM_1024: mms.modulation = eQAM_256; break; default: mms.modulation = eQAM_Scan; break; } mms.code_rate = nit->code_rate; p_chm->mms_flag = true; ch_map_add_mms(&(p_chm->ch_map), &mms); BDBG_WRN(("[%d] %d KHz (%d)", p_chm->ch_map.freq_map.num_freq-1, freq.freq_khz, nit->program_cnt)); for (i=0; iprogram_cnt; i++) { BKNI_Memset(&vch, 0, sizeof(vch_t)); vch.freq_idx = freq.idx; vch.ch_num = (unsigned short)s_channel_num++; vch.source_id = nit->program_id[i]; vch.mms_idx = freq.idx; vch.program_num = nit->program_id[i]; vch.vchflags = 0; ch_map_add_vch(&(p_chm->ch_map), &vch); BKNI_Memset(&st, 0, sizeof(st_t)); st.source_id = nit->program_id[i]; snprintf(st.name,MAX_VCH_LEN,"Ch - %d.%d", vch.freq_idx+2, vch.program_num); ch_map_add_st(&(p_chm->ch_map), &st); } } } static void chm_dvb_nit_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->dvb_nit_msg) { if (b_ok != smessage_stop(p_chm->dvb_nit_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); return; } smessage_close(p_chm->dvb_nit_msg); p_chm->dvb_nit_msg = NULL; } } static void chm_dvb_sdt_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->dvb_sdt_msg) { if (b_ok != smessage_stop(p_chm->dvb_sdt_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); return; } smessage_close(p_chm->dvb_sdt_msg); p_chm->dvb_sdt_msg = NULL; } } static void chm_dvb_eit_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->dvb_eit_msg) { if (b_ok != smessage_stop(p_chm->dvb_eit_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); return; } smessage_close(p_chm->dvb_eit_msg); p_chm->dvb_eit_msg = NULL; } } static void chm_dvb_tdt_stop(chm_mgr_t *p_chm) { if (NULL != p_chm->dvb_tdt_msg) { if (b_ok != smessage_stop(p_chm->dvb_tdt_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); return; } smessage_close(p_chm->dvb_tdt_msg); p_chm->dvb_tdt_msg = NULL; } } static void *chm_smessage_dvb_nit_callback(void *context, size_t size) { chm_mgr_t *p_chm = (chm_mgr_t *)context; unsigned int nit_section_size = size; unsigned int crc32; crc32 = chm_section_crc(p_chm->dvb_nit_buf, size); BDBG_MSG(("### DVB NIT EVENT ###\n")); dvb_nit_parse(p_chm->dvb_nit_buf, &nit_section_size); return(void*)p_chm->dvb_nit_buf; } static int chm_dvb_nit_start(chm_mgr_t *p_chm, bband_t band) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; chm_dvb_nit_stop(p_chm); BDBG_WRN(("dvb_nit_start...")); p_chm->dvb_nit_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->dvb_nit_msg) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->dvb_nit_msg); params.band = p_chm->band; params.pid = DVB_PID_NIT; params.filter.coef[0] = DVB_TID_NIT_OTH; params.filter.mask[0] = 0x01; params.filter.excl[0] = 0xff; params.buffer = p_chm->dvb_nit_buf; params.buffer_size = NIT_BUF_LEN; params.data_ready_callback = chm_smessage_dvb_nit_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; if (b_ok != smessage_start(¶ms, p_chm->dvb_nit_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } static void *chm_smessage_dvb_sdt_callback(void *context, size_t size) { chm_mgr_t *p_chm = (chm_mgr_t *)context; unsigned int sdt_section_size = size; unsigned int crc32; crc32 = chm_section_crc(p_chm->dvb_sdt_buf, size); BDBG_MSG(("### DVB SDT EVENT ###")); dvb_sdt_parse(p_chm->dvb_sdt_buf, &sdt_section_size); return(void*)p_chm->dvb_sdt_buf; } static int chm_dvb_sdt_start(chm_mgr_t *p_chm, bband_t band) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; chm_dvb_sdt_stop(p_chm); BDBG_WRN(("dvb_sdt_start...")); p_chm->dvb_sdt_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->dvb_sdt_msg) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->dvb_sdt_msg); params.band = band; params.pid = DVB_PID_SDT; params.filter.coef[0] = DVB_TID_SDT_OTH; params.filter.mask[0] = 0x04; params.filter.excl[0] = 0xff; params.buffer = p_chm->dvb_sdt_buf; params.buffer_size = SDT_BUF_LEN; params.data_ready_callback = chm_smessage_dvb_sdt_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; if (b_ok != smessage_start(¶ms, p_chm->dvb_sdt_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } static void *chm_smessage_dvb_eit_callback(void *context, size_t size) { chm_mgr_t *p_chm = (chm_mgr_t *)context; unsigned int eit_section_size = size; unsigned int crc32; crc32 = chm_section_crc(p_chm->dvb_eit_buf, size); dvb_eit_parse(p_chm->dvb_eit_buf, &eit_section_size); return(void*)p_chm->dvb_eit_buf; } static int chm_dvb_eit_start(chm_mgr_t *p_chm, bband_t band) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; chm_dvb_eit_stop(p_chm); BDBG_MSG(("eit_start...")); p_chm->dvb_eit_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->dvb_eit_msg) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->dvb_eit_msg); params.band = band; params.pid = DVB_PID_EIT; #ifndef SUPPORT_DVB_SCHED_EVENT params.filter.coef[0] = DVB_TID_EIT_OTH; params.filter.mask[0] = 0x01; #else params.filter.coef[0] = 0x5F; // not process EIT_SCHED_OTH params.filter.mask[0] = 0x1F; #endif params.filter.excl[0] = 0xFF; params.buffer = p_chm->dvb_eit_buf; params.buffer_size = EIT_BUF_LEN; params.data_ready_callback = chm_smessage_dvb_eit_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; if (b_ok != smessage_start(¶ms, p_chm->dvb_eit_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } static void *chm_smessage_dvb_tdt_callback(void *context, size_t size) { chm_mgr_t *p_chm = (chm_mgr_t *)context; unsigned int tdt_section_size = size; unsigned int crc32; b_tm cur_time; crc32 = chm_section_crc(p_chm->dvb_tdt_buf, size); if (dvb_tdt_parse(p_chm->dvb_tdt_buf, &tdt_section_size)) { crc32 = dvb_get_cur_time(); utctime(crc32, &cur_time); BDBG_MSG(("############### %d/%02d/%02d %02d:%02d:%02d", cur_time.tm_year+1980, cur_time.tm_mon+1, cur_time.tm_mday, cur_time.tm_hour, cur_time.tm_min, cur_time.tm_sec)); } return (void *)p_chm->dvb_tdt_buf; } static int chm_dvb_tdt_start(chm_mgr_t *p_chm, bband_t band) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; chm_dvb_tdt_stop(p_chm); BDBG_MSG(("tdt_start...")); p_chm->dvb_tdt_msg = smessage_open(smessage_format_psi); if (NULL == p_chm->dvb_tdt_msg) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->dvb_tdt_msg); params.band = band; params.pid = DVB_PID_TDT; params.filter.coef[0] = DVB_TID_TIME_OFFSET; params.filter.mask[0] = 0x03; params.filter.excl[0] = 0xFF; params.buffer = p_chm->dvb_tdt_buf; params.buffer_size = TDT_BUF_LEN; params.data_ready_callback = chm_smessage_dvb_tdt_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; if (b_ok != smessage_start(¶ms, p_chm->dvb_tdt_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } #endif #ifdef CONFIG_DVB_SUBTITLE static void chm_subtitle_stop(chm_mgr_t *p_chm) { BDBG_WRN(("STOP dvb subtitle 0x%x", p_chm->subtitle_msg)); if (NULL != p_chm->subtitle_msg) { if (b_ok != smessage_stop(p_chm->subtitle_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); return; } smessage_close(p_chm->subtitle_msg); p_chm->subtitle_msg = NULL; } } void *chm_smessage_subtitle_callback(void *context, size_t size) { chm_mgr_t *p_chm = (chm_mgr_t *)context; bapp_dsub_process(p_chm->subtitle_buf, size); return (void *)p_chm->subtitle_buf; } static int chm_subtitle_start(chm_mgr_t *p_chm, uint16_t dsub_pid) { int cerr = eCHM_ERR_OK; smessage_stream_params_t params; BDBG_WRN(("Start DVB SUBTITLE pid 0x%04x", dsub_pid)); p_chm->subtitle_msg = smessage_open(smessage_format_tsc); if (NULL == p_chm->subtitle_msg) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; goto ExitFunc; } smessage_stream_params_init(¶ms, p_chm->subtitle_msg); params.band = p_chm->band; params.pid = dsub_pid; params.buffer = p_chm->subtitle_buf; params.buffer_size = p_chm->subtitle_buf_size; params.data_ready_callback = chm_smessage_subtitle_callback; params.overflow = NULL; params.callback_context = (void *)p_chm; params.crc_disabled = true; if (b_ok != smessage_start(¶ms, p_chm->subtitle_msg)) { BDBG_ERR(("%s:%d", __FILE__, __LINE__)); cerr = eCHM_ERR_FAILED; } ExitFunc: return cerr; } #endif