| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2011, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: $ |
|---|
| 11 | * $brcm_Revision: $ |
|---|
| 12 | * $brcm_Date: $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: $ |
|---|
| 19 | * |
|---|
| 20 | ****************************************************************************/ |
|---|
| 21 | |
|---|
| 22 | #include "bstd.h" |
|---|
| 23 | #include "bkni.h" |
|---|
| 24 | #include "dvb_parser.h" |
|---|
| 25 | |
|---|
| 26 | static uint32_t tdt_cur_time; |
|---|
| 27 | static tot_map_t tot_map; |
|---|
| 28 | |
|---|
| 29 | BDBG_MODULE(dvb_tdt); |
|---|
| 30 | |
|---|
| 31 | void dvb_tdt_init(void) |
|---|
| 32 | { |
|---|
| 33 | tdt_cur_time = 0; |
|---|
| 34 | BKNI_Memset(&tot_map, 0, sizeof(tot_map_t)); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | int dvb_tdt_parse(const uint8_t *tdt_buf, size_t *psize) |
|---|
| 38 | { |
|---|
| 39 | uint16_t section_len, desc_len, idx; |
|---|
| 40 | uint8_t tid; |
|---|
| 41 | struct bit_state_t bs; |
|---|
| 42 | |
|---|
| 43 | BDBG_ASSERT((tdt_buf&&psize)); |
|---|
| 44 | BDBG_MSG(("%s: enter (%x)", __FUNCTION__, tid)); |
|---|
| 45 | |
|---|
| 46 | bs.bindex = 0; |
|---|
| 47 | bs.data = (unsigned char *)tdt_buf; |
|---|
| 48 | |
|---|
| 49 | tid = get_bits(8, &bs); |
|---|
| 50 | if ((tid != DVB_TID_TIME_DATE) && (tid != DVB_TID_TIME_OFFSET)) { |
|---|
| 51 | BDBG_WRN(("%s: only time_date and offset are processing (0x%x)", __FUNCTION__, tid)); |
|---|
| 52 | return 0; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | get_bits(4, &bs); /* skip section_syntax_indicator, reserved */ |
|---|
| 56 | section_len = get_bits(12, &bs); |
|---|
| 57 | if (*psize < (section_len+3)) { |
|---|
| 58 | BDBG_WRN(("%s: invalid TDT/TOT table", __FUNCTION__)); |
|---|
| 59 | return 0; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | idx = bs.bindex/8; |
|---|
| 63 | tdt_cur_time = MJD_TO_TIME(tdt_buf[idx], tdt_buf[idx+1]) + |
|---|
| 64 | BCD_TO_SEC(tdt_buf[idx+2], tdt_buf[idx+3], tdt_buf[idx+4]); |
|---|
| 65 | |
|---|
| 66 | bs.bindex += 40; |
|---|
| 67 | |
|---|
| 68 | if ( tid == DVB_TID_TIME_OFFSET ) { |
|---|
| 69 | get_bits(4, &bs); /* skip reserved */ |
|---|
| 70 | desc_len = get_bits(12, &bs); |
|---|
| 71 | /* TODO: how to apply time_offset value into system time */ |
|---|
| 72 | dvb_parse_descriptors((uint8_t *)&tdt_buf[bs.bindex/8], desc_len, DVB_TID_TIME_OFFSET, (void *)&tot_map); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | if (tid == DVB_TID_TIME_DATE) |
|---|
| 76 | return 1; |
|---|
| 77 | else { |
|---|
| 78 | return 0; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | uint32_t dvb_get_cur_time(void) |
|---|
| 83 | { |
|---|
| 84 | return tdt_cur_time; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /* |
|---|
| 88 | * convert MJD (modified julian date) to Y/M/D format |
|---|
| 89 | * convert is applicable between the inclusive dates 1900 March 1 to 2100 February 28 |
|---|
| 90 | * |
|---|
| 91 | * year: from 1900 |
|---|
| 92 | */ |
|---|
| 93 | static void dvb_mjd_to_ymd(uint16_t mjd, int *year, int *month, int *day) |
|---|
| 94 | { |
|---|
| 95 | uint32_t y_delta, m_delta, k; |
|---|
| 96 | |
|---|
| 97 | y_delta = (int)((double)(mjd-15078.2)/(double)365.25); |
|---|
| 98 | m_delta = (int)((double)((double)mjd-14956.1-(int)((double)y_delta*365.25))/30.6001); |
|---|
| 99 | *day = mjd-14956 - (int)((double)y_delta*365.25) - (int)((double)m_delta*30.6001); |
|---|
| 100 | if (m_delta == 14 || m_delta == 15) k = 1; |
|---|
| 101 | else k = 0; |
|---|
| 102 | |
|---|
| 103 | *year = y_delta + k - 80; // in DVB from 1900, b_tm from 1980 |
|---|
| 104 | *month = m_delta -1 - k*12 - 1; // in DVB 1-12, b_tm (0-11) |
|---|
| 105 | } |
|---|