1 /*
2  * Copyright (c) 2013 - Andre Roth <neolynx@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation version 2.1 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17  *
18  */
19 
20 /**
21  * @file mgt.h
22  * @ingroup dvb_table
23  * @brief Provides the table parser for the ATSC MGT (Master Guide Table)
24  * @copyright GNU Lesser General Public License version 2.1 (LGPLv2.1)
25  * @author Andre Roth
26  *
27  * @par Relevant specs
28  * The table described herein is defined at:
29  * - ATSC A/65:2009
30  *
31  * @see
32  * http://www.etherguidesystems.com/help/sdos/atsc/syntax/tablesections/MGT.aspx
33  *
34  * @par Bug Report
35  * Please submit bug reports and patches to linux-media@vger.kernel.org
36  */
37 
38 #ifndef _MGT_H
39 #define _MGT_H
40 
41 #include <stdint.h>
42 #include <unistd.h> /* ssize_t */
43 
44 #include <libdvbv5/atsc_header.h>
45 
46 /**
47  * @def ATSC_TABLE_MGT
48  *	@brief ATSC MGT table ID
49  *	@ingroup dvb_table
50  */
51 #define ATSC_TABLE_MGT 0xC7
52 
53 /**
54  * @struct atsc_table_mgt_table
55  * @brief ATSC tables descrition at MGT table
56  * @ingroup dvb_table
57  *
58  * @param type		table type
59  * @param pid		table type pid
60  * @param type_version	type type version number
61  * @param size		number of bytes for the table entry
62  * @param desc_length	table type descriptors length
63  * @param descriptor	pointer to struct dvb_desc
64  * @param next		pointer to struct atsc_table_mgt_table
65  *
66  * This structure is used to store the original VCT channel table,
67  * converting the integer fields to the CPU endianness.
68  *
69  * The undocumented parameters are used only internally by the API and/or
70  * are fields that are reserved. They shouldn't be used, as they may change
71  * on future API releases.
72  *
73  * Everything after atsc_table_mgt_table::descriptor (including it) won't
74  * be bit-mapped * to the data parsed from the MPEG TS. So, metadata are
75  * added there.
76  */
77 struct atsc_table_mgt_table {
78 	uint16_t type;
79 	union {
80 		uint16_t bitfield;
81 		struct {
82 			uint16_t pid:13;
83 			uint16_t one:3;
84 		} __attribute__((packed));
85 	} __attribute__((packed));
86         uint8_t type_version:5;
87         uint8_t one2:3;
88         uint32_t size;
89 	union {
90 		uint16_t bitfield2;
91 		struct {
92 			uint16_t desc_length:12;
93 			uint16_t one3:4;
94 		} __attribute__((packed));
95 	} __attribute__((packed));
96 	struct dvb_desc *descriptor;
97 	struct atsc_table_mgt_table *next;
98 } __attribute__((packed));
99 
100 /**
101  * @struct atsc_table_mgt
102  * @brief ATSC MGT table
103  * @ingroup dvb_table
104  *
105  * @param header		struct dvb_table_header content
106  * @param protocol_version	protocol version
107  * @param tables		tables_defined Number of tables defined
108  * @param table			pointer to struct atsc_table_mgt_table
109  * @param descriptor		pointer to struct dvb_desc
110  *
111  * This structure is used to store the original MGT channel table,
112  * converting the integer fields to the CPU endianness.
113  *
114  * The undocumented parameters are used only internally by the API and/or
115  * are fields that are reserved. They shouldn't be used, as they may change
116  * on future API releases.
117  *
118  * Everything after atsc_table_mgt::table (including it) won't
119  * be bit-mapped * to the data parsed from the MPEG TS. So, metadata are
120  * added there.
121  */
122 struct atsc_table_mgt {
123 	struct dvb_table_header header;
124 	uint8_t  protocol_version;
125         uint16_t tables;
126         struct atsc_table_mgt_table *table;
127 	struct dvb_desc *descriptor;
128 } __attribute__((packed));
129 
130 /**
131  * @brief Macro used to find a table inside a MGT table
132  *
133  * @param _table	channel to seek
134  * @param _mgt		pointer to struct atsc_table_mgt_table
135  */
136 #define atsc_mgt_table_foreach( _table, _mgt ) \
137 	if (_mgt && _mgt->_table) \
138 		for( struct atsc_table_mgt_table *_table = _mgt->table; _table; _table = _table->next ) \
139 
140 struct dvb_v5_fe_parms;
141 
142 #ifdef __cplusplus
143 extern "C" {
144 #endif
145 
146 /**
147  * @brief Initializes and parses MGT table
148  * @ingroup dvb_table
149  *
150  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
151  * @param buf buffer containing the MGT raw data
152  * @param buflen length of the buffer
153  * @param table pointer to struct atsc_table_mgt to be allocated and filled
154  *
155  * This function allocates an ATSC MGT table and fills the fields inside
156  * the struct. It also makes sure that all fields will follow the CPU
157  * endianness. Due to that, the content of the buffer may change.
158  *
159  * @return On success, it returns the size of the allocated struct.
160  *	   A negative value indicates an error.
161  */
162 ssize_t atsc_table_mgt_init(struct dvb_v5_fe_parms *parms, const uint8_t *buf,
163 			    ssize_t buflen, struct atsc_table_mgt **table);
164 
165 /**
166  * @brief Frees all data allocated by the MGT table parser
167  * @ingroup dvb_table
168  *
169  * @param table pointer to struct atsc_table_mgt to be freed
170  */
171 void atsc_table_mgt_free(struct atsc_table_mgt *table);
172 
173 /**
174  * @brief Prints the content of the MGT table
175  * @ingroup dvb_table
176  *
177  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
178  * @param table pointer to struct atsc_table_mgt
179  */
180 void atsc_table_mgt_print(struct dvb_v5_fe_parms *parms,
181 			  struct atsc_table_mgt *table);
182 
183 #ifdef __cplusplus
184 }
185 #endif
186 
187 #endif
188