1 /*****************************************************************************
2 * dr_58.c
3 * Copyright (C) 2001-2011 VideoLAN
4 * $Id$
5 *
6 * Authors: Johann Hanne
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 *****************************************************************************/
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <string.h>
30
31 #if defined(HAVE_INTTYPES_H)
32 #include <inttypes.h>
33 #elif defined(HAVE_STDINT_H)
34 #include <stdint.h>
35 #endif
36
37 #include "../dvbpsi.h"
38 #include "../dvbpsi_private.h"
39 #include "../descriptor.h"
40
41 #include "dr_58.h"
42
43
44 /*****************************************************************************
45 * dvbpsi_DecodeLocalTimeOffsetDr
46 *****************************************************************************/
dvbpsi_DecodeLocalTimeOffsetDr(dvbpsi_descriptor_t * p_descriptor)47 dvbpsi_local_time_offset_dr_t * dvbpsi_DecodeLocalTimeOffsetDr(
48 dvbpsi_descriptor_t * p_descriptor)
49 {
50 dvbpsi_local_time_offset_dr_t * p_decoded;
51 uint8_t * p_data, * p_end;
52 dvbpsi_local_time_offset_t * p_current;
53
54 /* Check the tag */
55 if (!dvbpsi_CanDecodeAsDescriptor(p_descriptor, 0x58))
56 return NULL;
57
58 /* Don't decode twice */
59 if (dvbpsi_IsDescriptorDecoded(p_descriptor))
60 return p_descriptor->p_decoded;
61
62 /* Allocate memory */
63 p_decoded = (dvbpsi_local_time_offset_dr_t*)malloc(sizeof(dvbpsi_local_time_offset_dr_t));
64 if (!p_decoded)
65 return NULL;
66
67 /* Decode data */
68 p_decoded->i_local_time_offsets_number = 0;
69 p_current = p_decoded->p_local_time_offset;
70 p_end = p_descriptor->p_data + p_descriptor->i_length;
71 p_data = p_descriptor->p_data;
72 while(p_data + 13 <= p_end)
73 {
74 memcpy(p_current->i_country_code, p_data, 3);
75 p_current->i_country_region_id = (p_data[3] >> 2) & 0x3f;
76 p_current->i_local_time_offset_polarity = p_data[3] & 0x01;
77 p_current->i_local_time_offset = ((uint16_t)p_data[4] << 8)
78 | (uint16_t)p_data[5];
79 p_current->i_time_of_change = ((uint64_t)p_data[6] << 32)
80 | ((uint64_t)p_data[7] << 24)
81 | ((uint64_t)p_data[8] << 16)
82 | ((uint64_t)p_data[9] << 8)
83 | (uint64_t)p_data[10];
84 p_current->i_next_time_offset = ((uint16_t)p_data[11] << 8)
85 | (uint16_t)p_data[12];
86
87 /* NOTE: Only decode upto DVBPSI_LOCAL_TIME_OFFSET_DR_MAX number of time
88 offsets. The decoding struct cannot hold more then that. */
89 p_decoded->i_local_time_offsets_number++;
90 if (p_decoded->i_local_time_offsets_number == DVBPSI_LOCAL_TIME_OFFSET_DR_MAX)
91 break;
92
93 p_data += 13;
94 p_current++;
95 }
96
97 p_descriptor->p_decoded = (void*)p_decoded;
98
99 return p_decoded;
100 }
101
102 /*****************************************************************************
103 * dvbpsi_GenLocalTimeOffsetDr
104 *****************************************************************************/
dvbpsi_GenLocalTimeOffsetDr(dvbpsi_local_time_offset_dr_t * p_decoded,bool b_duplicate)105 dvbpsi_descriptor_t * dvbpsi_GenLocalTimeOffsetDr(
106 dvbpsi_local_time_offset_dr_t * p_decoded,
107 bool b_duplicate)
108 {
109 if (p_decoded->i_local_time_offsets_number > DVBPSI_LOCAL_TIME_OFFSET_DR_MAX)
110 p_decoded->i_local_time_offsets_number = DVBPSI_LOCAL_TIME_OFFSET_DR_MAX;
111
112 /* Create the descriptor */
113 dvbpsi_descriptor_t * p_descriptor =
114 dvbpsi_NewDescriptor(0x58, p_decoded->i_local_time_offsets_number * 13, NULL);
115 if (!p_descriptor)
116 return NULL;
117
118 /* Encode data */
119 dvbpsi_local_time_offset_t * p_current;
120 uint8_t * p_data;
121
122 p_current = p_decoded->p_local_time_offset;
123 p_data = p_descriptor->p_data;
124
125 for (uint8_t i_num = 0; i_num < p_decoded->i_local_time_offsets_number; i_num++)
126 {
127 memcpy(p_data, p_current->i_country_code, 3);
128 p_data[3] = ((p_current->i_country_region_id & 0x3f) << 2)
129 | 0x02
130 | (p_current->i_local_time_offset_polarity & 0x01);
131 p_data[4] = (p_current->i_local_time_offset >> 8) & 0xff;
132 p_data[5] = p_current->i_local_time_offset & 0xff;
133 p_data[6] = (p_current->i_time_of_change >> 32) & 0xff;
134 p_data[7] = (p_current->i_time_of_change >> 24) & 0xff;
135 p_data[8] = (p_current->i_time_of_change >> 16) & 0xff;
136 p_data[9] = (p_current->i_time_of_change >> 8) & 0xff;
137 p_data[10] = p_current->i_time_of_change & 0xff;
138 p_data[11] = (p_current->i_next_time_offset >> 8) & 0xff;
139 p_data[12] = p_current->i_next_time_offset & 0xff;
140
141 p_data += 13;
142 p_current++;
143 }
144
145 if (b_duplicate)
146 {
147 /* Duplicate decoded data */
148 p_descriptor->p_decoded =
149 dvbpsi_DuplicateDecodedDescriptor(p_decoded,
150 sizeof(dvbpsi_local_time_offset_dr_t));
151 }
152
153 return p_descriptor;
154 }
155