aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libucsi/dvb/linkage_descriptor.h
blob: d2be78933c6c07ddbdd0215a259614f876c13136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * section and descriptor parser
 *
 * Copyright (C) 2005 Kenneth Aafloy (kenneth@linuxtv.org)
 * Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#ifndef _UCSI_DVB_LINKAGE_DESCRIPTOR
#define _UCSI_DVB_LINKAGE_DESCRIPTOR 1

#ifdef __cplusplus
extern "C"
{
#endif

#include <libucsi/descriptor.h>
#include <libucsi/endianops.h>
#include <libucsi/types.h>

/**
 * Possible values for linkage_type.
 */
enum {
	DVB_LINKAGE_TYPE_INFORMATION			= 0x01,
	DVB_LINKAGE_TYPE_EPG				= 0x02,
	DVB_LINKAGE_TYPE_CA_REPLACEMENT			= 0x03,
	DVB_LINKAGE_TYPE_TS_WITH_BAT_NIT		= 0x04,
	DVB_LINKAGE_TYPE_SERVICE_REPLACMENT		= 0x05,
	DVB_LINKAGE_TYPE_DATA_BCAST			= 0x06,
	DVB_LINKAGE_TYPE_RCS_MAP			= 0x07,
	DVB_LINKAGE_TYPE_MOBILE_HANDOVER		= 0x08,
	DVB_LINKAGE_TYPE_SOFTWARE_UPDATE		= 0x09,
	DVB_LINKAGE_TYPE_TS_WITH_SSU_BAT_NIT		= 0x0a,
	DVB_LINKAGE_TYPE_IP_MAC_NOTIFICATION		= 0x0b,
	DVB_LINKAGE_TYPE_TS_WITH_INT_BAT_NIT		= 0x0c,
};

/**
 * Possible values for hand_over_type.
 */
enum {
	DVB_HAND_OVER_TYPE_IDENTICAL_NEIGHBOURING_COUNTRY	= 0x01,
	DVB_HAND_OVER_TYPE_LOCAL_VARIATION			= 0x02,
	DVB_HAND_OVER_TYPE_ASSOCIATED_SERVICE			= 0x03,
};

/**
 * Possible values for origin_type.
 */
enum {
	DVB_ORIGIN_TYPE_NIT				= 0x00,
	DVB_ORIGIN_TYPE_SDT				= 0x01,
};

/**
 * dvb_linkage_descriptor structure.
 */
struct dvb_linkage_descriptor {
	struct descriptor d;

	uint16_t transport_stream_id;
	uint16_t original_network_id;
	uint16_t service_id;
	uint8_t linkage_type;
	/* uint8_t data[] */
} __ucsi_packed;

/**
 * Data for a linkage_type of 0x08.
 */
struct dvb_linkage_data_08 {
  EBIT3(uint8_t hand_over_type		: 4;  ,
	uint8_t reserved		: 3;  ,
	uint8_t origin_type		: 1;  );
	/* uint16_t network_id if hand_over_type == 1,2,3 */
	/* uint16_t initial_service_id if origin_type = 0 */
	/* uint8_t data[] */
} __ucsi_packed;

/**
 * Data for an linkage_type of 0x0b (IP/MAC Notification Table).
 */
struct dvb_linkage_data_0b {
	uint8_t platform_id_data_length;
	/* struct platform_id ids[] */
} __ucsi_packed;

/**
 * Entries in the ids field of a dvb_linkage_data_0b.
 */
struct dvb_platform_id {
  EBIT2(uint32_t platform_id			: 24; ,
	uint8_t platform_name_loop_length	: 8;  );
	/* struct platform_name names[] */
} __ucsi_packed;

/**
 * Entries in the names field of a dvb_platform_id.
 */
struct dvb_platform_name {
	iso639lang_t language_code;
	uint8_t platform_name_length;
	/* uint8_t text[] */
} __ucsi_packed;

/**
 * Data for a linkage_type of 0x0c (IP/MAC Notification Table).
 */
struct dvb_linkage_data_0c {
	uint8_t table_type;
	/* uint16_t bouquet_id if table_type == 0x02 */
} __ucsi_packed;


/**
 * Process a dvb_linkage_descriptor.
 *
 * @param d Generic descriptor pointer.
 * @return dvb_linkage_descriptor pointer, or NULL on error.
 */
static inline struct dvb_linkage_descriptor*
	dvb_linkage_descriptor_codec(struct descriptor* d)
{
	uint32_t pos = 0;
	uint8_t* buf = (uint8_t*) d + 2;
	uint32_t len = d->len;
	struct dvb_linkage_descriptor *p =
		(struct dvb_linkage_descriptor*) d;

	if (len < (sizeof(struct dvb_linkage_descriptor) - 2))
		return NULL;

	bswap16(buf);
	bswap16(buf+2);
	bswap16(buf+4);

	pos += sizeof(struct dvb_linkage_descriptor) - 2;

	if (p->linkage_type == 0x08) {
		struct dvb_linkage_data_08 *d08;

		if ((len - pos) < sizeof(struct dvb_linkage_data_08))
			return NULL;
		d08 = (struct dvb_linkage_data_08 *) (buf+pos);
		pos += sizeof(struct dvb_linkage_data_08);

		switch(d08->hand_over_type) {
		case 1:
		case 2:
		case 3:
			if ((len - pos) < 2)
				return NULL;
			bswap16(buf+pos);
			pos += 2;
			break;
		}
		if (d08->origin_type == 0) {
			if ((len - pos) < 2)
				return NULL;
			bswap16(buf+pos);
			pos+=2;
		}

	} else if (p->linkage_type == 0x0b) {
		uint32_t pos2=0;
		struct dvb_linkage_data_0b *l_0b = (struct dvb_linkage_data_0b *) (buf + pos);

		if ((len - pos) < sizeof(struct dvb_linkage_data_0b))
			return NULL;

		pos += sizeof(struct dvb_linkage_data_0b);
		if ((len - pos) < l_0b->platform_id_data_length)
			return NULL;

		while (pos2 < l_0b->platform_id_data_length) {
			bswap32(buf + pos + pos2);

			struct dvb_platform_id *p_id = (struct dvb_platform_id *) (buf + pos + pos2);
			if ((len - pos - pos2) < p_id->platform_name_loop_length)
				return NULL;

			pos2 += sizeof(struct dvb_platform_id) + p_id->platform_name_loop_length;
		}

		pos += pos2;
	} else if (p->linkage_type == 0x0c) {
		struct dvb_linkage_data_0c *l_0c = (struct dvb_linkage_data_0c *) (buf + pos);

		if ((len - pos) < sizeof(struct dvb_linkage_data_0c))
			return NULL;
		pos += sizeof(struct dvb_linkage_data_0c);

		if (l_0c->table_type == 0x02) {
			if ((len - pos) < 2)
				return NULL;
			bswap16(buf+pos);
		}
	}

	return (struct dvb_linkage_descriptor*) d;
}

/**
 * Accessor for the data field of a dvb_linkage_descriptor.
 *
 * @param d dvb_linkage_descriptor pointer.
 * @return Pointer to the data field.
 */
static inline uint8_t *
	dvb_linkage_descriptor_data(struct dvb_linkage_descriptor *d)
{
	return (uint8_t *) d + sizeof(struct dvb_linkage_descriptor);
}

/**
 * Determine the length of the data field of a dvb_linkage_descriptor.
 *
 * @param d dvb_linkage_descriptor pointer.
 * @return Length of the field in bytes.
 */
static inline int
	dvb_linkage_descriptor_data_length(struct dvb_linkage_descriptor *d)
{
	return d->d.len - 7;
}

/**
 * Accessor for a dvb_linkage_data_08 pointer.
 *
 * @param d dvb_linkage_descriptor pointer.
 * @return Pointer to the data field.
 */
static inline struct dvb_linkage_data_08 *
	dvb_linkage_data_08(struct dvb_linkage_descriptor *d)
{
	if (d->linkage_type != 0x08)
		return NULL;
	return (struct dvb_linkage_data_08 *) dvb_linkage_descriptor_data(d);
}

/**
 * Accessor for the network_id field of a dvb_linkage_data_08.
 *
 * @param d dvb_linkage_descriptor pointer
 * @param d08 dvb_linkage_data_08 pointer.
 * @return network_id, or -1 if not present
 */
static inline int
	dvb_linkage_data_08_network_id(struct dvb_linkage_descriptor *d, struct dvb_linkage_data_08 *d08)
{
	if (d->linkage_type != 0x08)
		return -1;

	switch(d08->hand_over_type) {
	case 1:
	case 2:
	case 3:
		return *((uint16_t*) ((uint8_t*) d08 + sizeof(struct dvb_linkage_data_08)));
	}

	return -1;
}

/**
 * Accessor for the initial_service_id field of a dvb_linkage_data_08.
 *
 * @param d dvb_linkage_descriptor pointer
 * @param d08 dvb_linkage_data_08 pointer.
 * @return initial_service_id, or -1 if not present
 */
static inline int
	dvb_linkage_data_08_initial_service_id(struct dvb_linkage_descriptor *d, struct dvb_linkage_data_08 *d08)
{
	uint8_t *pos;

	if (d->linkage_type != 0x08)
		return -1;
	if (d08->origin_type != 0)
		return -1;

	pos = ((uint8_t*) d08) + sizeof(struct dvb_linkage_data_08);
	switch(d08->hand_over_type) {
	case 1:
	case 2:
	case 3:
		pos +=2;
		break;
	}

	return *((uint16_t*) pos);
}

/**
 * Accessor for the data field of a dvb_linkage_data_08.
 *
 * @param d dvb_linkage_descriptor pointer
 * @param d08 dvb_linkage_data_08 pointer.
 * @param length Pointer to int destination for data length.
 * @return Pointer to the data field, or NULL if invalid
 */
static inline uint8_t *
	dvb_linkage_data_08_data(struct dvb_linkage_descriptor *d, struct dvb_linkage_data_08 *d08, int *length)
{
	uint8_t *pos;
	int used = 0;

	if (d->linkage_type != 0x08) {
		*length = 0;
		return NULL;
	}

	pos = ((uint8_t*) d08) + sizeof(struct dvb_linkage_data_08);
	switch(d08->hand_over_type) {
	case 1:
	case 2:
	case 3:
		pos += 2;
		used += 2;
		break;
	}
	if (d08->origin_type == 0) {
		pos+=2;
		used+=2;
	}

	*length = dvb_linkage_descriptor_data_length(d) - (sizeof(struct dvb_linkage_data_08) + used);
	return pos;
}

/**
 * Accessor for a dvb_linkage_data_0b pointer.
 *
 * @param d dvb_linkage_descriptor pointer.
 * @return Pointer to the data field.
 */
static inline struct dvb_linkage_data_0b *
	dvb_linkage_data_0b(struct dvb_linkage_descriptor *d)
{
	if (d->linkage_type != 0x0b)
		return NULL;
	return (struct dvb_linkage_data_0b *) dvb_linkage_descriptor_data(d);
}

/**
 * Iterator for the platform_id field of a dvb_linkage_data_0b.
 *
 * @param linkage dvb_linkage_data_0b pointer.
 * @param pos Variable containing a pointer to the current dvb_platform_id.
 */
#define dvb_linkage_data_0b_platform_id_for_each(linkage, pos) \
	for ((pos) = dvb_platform_id_first(linkage); \
	     (pos); \
	     (pos) = dvb_platform_id_next(linkage, pos))

/**
 * Iterator for the platform_name field of a dvb_platform_id.
 *
 * @param platid dvb_platform_id pointer.
 * @param pos Variable containing a pointer to the current dvb_platform_name.
 */
#define dvb_platform_id_platform_name_for_each(platid, pos) \
	for ((pos) = dvb_platform_name_first(platid); \
	     (pos); \
	     (pos) = dvb_platform_name_next(platid, pos))

/**
 * Accessor for the text field of a dvb_platform_name.
 *
 * @param p dvb_platform_name pointer.
 * @return Pointer to the field.
 */
static inline uint8_t *
	dvb_platform_name_text(struct dvb_platform_name *p)
{
	return (uint8_t *) p + sizeof(struct dvb_platform_name);
}

/**
 * Accessor for a dvb_linkage_data_0c pointer.
 *
 * @param d dvb_linkage_descriptor pointer.
 * @return Pointer to the data field.
 */
static inline struct dvb_linkage_data_0c *
		dvb_linkage_data_0c(struct dvb_linkage_descriptor *d)
{
	if (d->linkage_type != 0x0c)
		return NULL;
	return (struct dvb_linkage_data_0c *) dvb_linkage_descriptor_data(d);
}

/**
 * Accessor for the bouquet_id field of a dvb_linkage_data_0c if table_id == 0x02.
 *
 * @param l_0c dvb_linkage_data_0c pointer.
 * @return The bouquet field, or -1 on error.
 */
static inline int
	dvb_linkage_data_0c_bouquet_id(struct dvb_linkage_data_0c *l_0c)
{
	if (l_0c->table_type != 0x02)
		return -1;

	return *((uint16_t *) ((uint8_t*) l_0c + 1));
}







/******************************** PRIVATE CODE ********************************/
static inline struct dvb_platform_id *
	dvb_platform_id_first(struct dvb_linkage_data_0b *d)
{
	if (d->platform_id_data_length == 0)
		return NULL;

	return (struct dvb_platform_id *) ((uint8_t *) d + sizeof(struct dvb_linkage_data_0b));
}

static inline struct dvb_platform_id *
	dvb_platform_id_next(struct dvb_linkage_data_0b *d,
				    struct dvb_platform_id *pos)
{
	uint8_t *end = (uint8_t *) d + d->platform_id_data_length;
	uint8_t *next =	(uint8_t *) pos +
			sizeof(struct dvb_platform_id) +
			pos->platform_name_loop_length;

	if (next >= end)
		return NULL;

	return (struct dvb_platform_id *) next;
}

static inline struct dvb_platform_name *
	dvb_platform_name_first(struct dvb_platform_id *p)
{
	if (p->platform_name_loop_length == 0)
		return NULL;

	return (struct dvb_platform_name *) ((uint8_t *) p + sizeof(struct dvb_platform_id));
}

static inline struct dvb_platform_name *
	dvb_platform_name_next(struct dvb_platform_id *p,
				    struct dvb_platform_name *pos)
{
	uint8_t *end = (uint8_t *) p + p->platform_name_loop_length;
	uint8_t *next =	(uint8_t *) pos +
			sizeof(struct dvb_platform_name) +
			pos->platform_name_length;

	if (next >= end)
		return NULL;

	return (struct dvb_platform_name *) next;
}

#ifdef __cplusplus
}
#endif

#endif