• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * Copyright 2012 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
4  * Author: Konke Radlow <koradlow@gmail.com>
5  */
6 
7 #include <errno.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <sys/types.h>
14 #include <sys/mman.h>
15 
16 #if defined(__OpenBSD__)
17 #include <sys/videoio.h>
18 #else
19 #include <linux/videodev2.h>
20 #endif
21 
22 #include "../include/libv4l2rds.h"
23 
24 /* struct to encapsulate the private state information of the decoding process */
25 /* the fields (except for handle) are for internal use only - new information
26  * is decoded and stored in them until it can be verified and copied to the
27  * public part of the  rds structure (handle) */
28 /* for meaning of abbreviations check the library header libv4l2rds.h */
29 struct rds_private_state {
30 	/* v4l2_rds has to be in first position, to allow typecasting between
31 	 * v4l2_rds and rds_private_state pointers */
32 	struct v4l2_rds handle;
33 
34 	/* current state of rds group decoding */
35 	uint8_t decode_state;
36 
37 	/* temporal storage locations for rds fields */
38 	uint16_t new_pi;
39 	uint8_t new_ps[8];
40 	uint8_t new_ps_valid[8];
41 	uint8_t new_pty;
42 	uint8_t new_ptyn[2][4];
43 	bool new_ptyn_valid[2];
44 	uint8_t new_rt[64];
45 	uint8_t next_rt_segment;
46 	uint8_t new_di;
47 	uint8_t next_di_segment;
48 	uint8_t new_ecc;
49 	uint8_t new_lc;
50 	/* RDS date / time representation */
51 	uint32_t new_mjd;	/* modified Julian Day code */
52 	uint8_t utc_hour;
53 	uint8_t utc_minute;
54 	uint8_t utc_offset;
55 
56 	/* TMC decoding buffers, to store data before it can be verified,
57 	 * and before all parts of a multi-group message have been received */
58 	uint8_t continuity_id;	/* continuity index of current TMC multigroup */
59 	uint8_t grp_seq_id; 	/* group sequence identifier */
60 	bool optional_tmc[112];	/* buffer for up to 112 bits of optional
61 				 * additional data in multi-group
62 				 * messages (112 is the maximal possible length
63 				 * specified by the standard) */
64 
65 	/* TMC groups are only accepted if the same data was received twice,
66 	 * these structs are used as receive buffers to validate TMC groups */
67 	struct v4l2_rds_group prev_tmc_group;
68 	struct v4l2_rds_group prev_tmc_sys_group;
69 	struct v4l2_rds_tmc_msg new_tmc_msg;
70 
71 	/* buffers for rds data, before group type specific decoding can
72 	 * be done */
73 	struct v4l2_rds_group rds_group;
74 	struct v4l2_rds_data rds_data_raw[4];
75 };
76 
77 /* states of the RDS block into group decoding state machine */
78 enum rds_state {
79 	RDS_EMPTY,
80 	RDS_A_RECEIVED,
81 	RDS_B_RECEIVED,
82 	RDS_C_RECEIVED,
83 };
84 
set_bit(uint8_t input,uint8_t bitmask,bool bitvalue)85 static inline uint8_t set_bit(uint8_t input, uint8_t bitmask, bool bitvalue)
86 {
87 	return bitvalue ? input | bitmask : input & ~bitmask;
88 }
89 
90 /* rds_decode_a-d(..): group of functions to decode different RDS blocks
91  * into the RDS group that's currently being received
92  *
93  * block A of RDS group always contains PI code of program */
rds_decode_a(struct rds_private_state * priv_state,struct v4l2_rds_data * rds_data)94 static uint32_t rds_decode_a(struct rds_private_state *priv_state, struct v4l2_rds_data *rds_data)
95 {
96 	struct v4l2_rds *handle = &priv_state->handle;
97 	uint32_t updated_fields = 0;
98 	uint16_t pi = (rds_data->msb << 8) | rds_data->lsb;
99 
100 	/* data in RDS group is uninterpreted */
101 	priv_state->rds_group.pi = pi;
102 
103 	/* compare PI values to detect PI update (Channel Switch)
104 	 * --> new PI is only accepted, if the same PI is received
105 	 * at least 2 times in a row */
106 	if (pi != handle->pi && pi == priv_state->new_pi) {
107 		handle->pi = pi;
108 		handle->valid_fields |= V4L2_RDS_PI;
109 		updated_fields |= V4L2_RDS_PI;
110 	} else if (pi != handle->pi && pi != priv_state->new_pi) {
111 		priv_state->new_pi = pi;
112 	}
113 
114 	return updated_fields;
115 }
116 
117 /* block B of RDS group always contains Group Type Code, Group Type information
118  * Traffic Program Code and Program Type Code as well as 5 bits of Group Type
119  * depending information */
rds_decode_b(struct rds_private_state * priv_state,struct v4l2_rds_data * rds_data)120 static uint32_t rds_decode_b(struct rds_private_state *priv_state, struct v4l2_rds_data *rds_data)
121 {
122 	struct v4l2_rds *handle = &priv_state->handle;
123 	struct v4l2_rds_group *grp = &priv_state->rds_group;
124 	bool traffic_prog;
125 	uint8_t pty;
126 	uint32_t updated_fields = 0;
127 
128 	/* bits 12-15 (4-7 of msb) contain the Group Type Code */
129 	grp->group_id = rds_data->msb >> 4 ;
130 
131 	/* bit 11 (3 of msb) defines Group Type info: 0 = A, 1 = B */
132 	grp->group_version = (rds_data->msb & 0x08) ? 'B' : 'A';
133 
134 	/* bit 10 (2 of msb) defines Traffic program Code */
135 	traffic_prog = rds_data->msb & 0x04;
136 	if (handle->tp != traffic_prog) {
137 		handle->tp = traffic_prog;
138 		updated_fields |= V4L2_RDS_TP;
139 	}
140 	handle->valid_fields |= V4L2_RDS_TP;
141 
142 	/* bits 0-4 contains Group Type depending information */
143 	grp->data_b_lsb = rds_data->lsb & 0x1f;
144 
145 	/* bits 5-9 contain the PTY code */
146 	pty = (rds_data->msb << 3) | (rds_data->lsb >> 5);
147 	pty &= 0x1f; /* mask out 3 irrelevant bits */
148 	/* only accept new PTY if same PTY is received twice in a row
149 	 * and filter out cases where the PTY is already known */
150 	if (handle->pty == pty) {
151 		priv_state->new_pty = pty;
152 		return updated_fields;
153 	}
154 
155 	if (priv_state->new_pty == pty) {
156 		handle->pty = priv_state->new_pty;
157 		updated_fields |= V4L2_RDS_PTY;
158 		handle->valid_fields |= V4L2_RDS_PTY;
159 	} else {
160 		priv_state->new_pty = pty;
161 	}
162 
163 	return updated_fields;
164 }
165 
166 /* block C of RDS group contains either data or the PI code, depending
167  * on the Group Type - store the raw data for later decoding */
rds_decode_c(struct rds_private_state * priv_state,struct v4l2_rds_data * rds_data)168 static void rds_decode_c(struct rds_private_state *priv_state, struct v4l2_rds_data *rds_data)
169 {
170 	struct v4l2_rds_group *grp = &priv_state->rds_group;
171 
172 	grp->data_c_msb = rds_data->msb;
173 	grp->data_c_lsb = rds_data->lsb;
174 	/* we could decode the PI code here, because we already know if the
175 	 * group is of type A or B, but it doesn't give any advantage because
176 	 * we only get here after the PI code has been decoded in the first
177 	 * state of the state machine */
178 }
179 
180 /* block D of RDS group contains data - store the raw data for later decoding */
rds_decode_d(struct rds_private_state * priv_state,struct v4l2_rds_data * rds_data)181 static void rds_decode_d(struct rds_private_state *priv_state, struct v4l2_rds_data *rds_data)
182 {
183 	struct v4l2_rds_group *grp = &priv_state->rds_group;
184 
185 	grp->data_d_msb = rds_data->msb;
186 	grp->data_d_lsb = rds_data->lsb;
187 }
188 
189 /* decodes the RDS radio frequency representation into Hz
190  * @af: 8-bit AF value as transmitted in RDS groups
191  * @is_vhf: boolean value defining  which conversion table to use
192  * @return: frequency in Hz, 0 in case of wrong input values */
rds_decode_af(uint8_t af,bool is_vhf)193 static uint32_t rds_decode_af(uint8_t af, bool is_vhf)
194 {
195 	uint32_t freq = 0;
196 
197 	/* AF = 0 => "not to be used"
198 	 * AF >= 205 => special meanings */
199 	if (af == 0 || af >= 205)
200 		return 0;
201 
202 	/* calculate the AF values in HZ */
203 	if (is_vhf)
204 		freq = 87500000 + af * 100000;
205 	else if (af <= 15)
206 		freq = 152000 + af * 9000;
207 	else
208 		freq = 531000 + af * 9000;
209 
210 	return freq;
211 }
212 
213 /* compare two rds-groups for equality */
214 /* used for decoding RDS-TMC, which has the requirement that the same group
215  * is at least received twice before it is accepted */
rds_compare_group(const struct v4l2_rds_group * a,const struct v4l2_rds_group * b)216 static bool rds_compare_group(const struct v4l2_rds_group *a,
217 				const struct v4l2_rds_group *b)
218 {
219 	if (a->pi != b->pi)
220 		return false;
221 	if (a->group_version != b->group_version)
222 		return false;
223 	if (a->group_id != b->group_id)
224 		return false;
225 
226 	if (a->data_b_lsb != b->data_b_lsb)
227 		return false;
228 	if (a->data_c_lsb != b->data_c_lsb || a->data_c_msb != b->data_c_msb)
229 		return false;
230 	if (a->data_d_lsb != b->data_d_lsb || a->data_d_msb != b->data_d_msb)
231 		return false;
232 	/* all values are equal */
233 	return true;
234 }
235 
236 /* checks if an entry for the given PI already exists and returns the index
237  * of that entry if so. Else it adds a new entry to the TMC-Tuning table and returns
238  * the index of the new field */
rds_add_tmc_station(struct rds_private_state * priv_state,uint16_t pi)239 static int rds_add_tmc_station(struct rds_private_state *priv_state, uint16_t pi)
240 {
241 	struct v4l2_tmc_tuning *tuning = &priv_state->handle.tmc.tuning;
242 	uint8_t index = tuning->index;
243 	uint8_t size = tuning->station_cnt;
244 
245 	/* check if there's an entry for the given PI key */
246 	for (int i = 0; i < tuning->station_cnt; i++) {
247 		if (tuning->station[i].pi == pi) {
248 			return i;
249 		}
250 	}
251 	/* if the the maximum table size is reached, overwrite old
252 	 * entries, starting at the oldest one = 0 */
253 	tuning->station[index].pi = pi;
254 	tuning->index = (index+1 < MAX_TMC_ALT_STATIONS) ? (index+1) : 0;
255 	tuning->station_cnt = (size+1 <= MAX_TMC_ALT_STATIONS) ? (size+1) : MAX_TMC_ALT_STATIONS;
256 	return index;
257 }
258 
259 /* tries to add new AFs to the relevant entry in the list of RDS-TMC providers */
rds_add_tmc_af(struct rds_private_state * priv_state)260 static bool rds_add_tmc_af(struct rds_private_state *priv_state)
261 {
262 	struct v4l2_rds_group *grp = &priv_state->rds_group;
263 	struct v4l2_tmc_alt_freq *afi;
264 	uint16_t pi_on = grp->data_d_msb << 8 | grp->data_d_lsb;
265 	uint8_t variant = grp->data_b_lsb & 0x0f;
266 	uint8_t station_index = rds_add_tmc_station(priv_state, pi_on);
267 	uint8_t af_index;
268 	uint8_t mapped_af_index;
269 	uint32_t freq_a = rds_decode_af(grp->data_c_msb, true);
270 	uint32_t freq_b = rds_decode_af(grp->data_c_lsb, true);
271 
272 	afi = &priv_state->handle.tmc.tuning.station[station_index].afi;
273 	af_index = afi->af_index;
274 	mapped_af_index = afi->mapped_af_index;
275 
276 	/* specific frequencies */
277 	if (variant == 6) {
278 		/* compare the new AFs to the stored ones, reset them to 0 if the AFs are
279 		 * already known */
280 		for (int i = 0; i < afi->af_size; i++) {
281 			freq_a = (freq_a == afi->af[i]) ? 0 : freq_a;
282 			freq_b = (freq_b == afi->af[i]) ? 0 : freq_b;
283 		}
284 		/* return early if there is nothing to do */
285 		if (freq_a == 0 && freq_b == 0)
286 			return false;
287 
288 		/* add the new AFs if they were previously unknown */
289 		if (freq_a != 0) {
290 			afi->af[af_index] = freq_a;
291 			af_index = (af_index+1 < MAX_TMC_AF_CNT) ? af_index+1 : 0;
292 			afi->af_size++;
293 		}
294 		if (freq_b != 0) {
295 			afi->af[af_index] = freq_b;
296 			af_index = (af_index+1 < MAX_TMC_AF_CNT) ? af_index+1 : 0;
297 			afi->af_size++;
298 		}
299 		/* update the information in the handle */
300 		afi->af_index = af_index;
301 		if (afi->af_size >= MAX_TMC_AF_CNT)
302 			afi->af_size = MAX_TMC_AF_CNT;
303 
304 		return true;
305 	}
306 
307 	/* mapped frequency pair */
308 	if (variant == 7) {
309 		/* check if there's already a frequency mapped to the new tuning
310 		 * frequency, update the mapped frequency in this case */
311 		for (int i = 0; i < afi->mapped_af_size; i++) {
312 			if (freq_a == afi->mapped_af_tuning[i]) {
313 				afi->mapped_af[i] = freq_b;
314 				return true;
315 			}
316 		}
317 		/* new pair is unknown, add it to the list */
318 		if (freq_a != 0 && freq_b != 0) {
319 			mapped_af_index = (mapped_af_index+1 >= MAX_TMC_AF_CNT) ? 0 : mapped_af_index + 1;
320 			afi->mapped_af[mapped_af_index] = freq_b;
321 			afi->mapped_af_tuning[mapped_af_index] = freq_a;
322 			afi->mapped_af_size++;
323 		}
324 		/* update the information in the handle */
325 		afi->mapped_af_index = mapped_af_index;
326 		if (afi->mapped_af_size >= MAX_TMC_AF_CNT)
327 			afi->mapped_af_size = MAX_TMC_AF_CNT;
328 
329 		return true;
330 	}
331 	return false;
332 }
333 
334 /* decode additional information of a TMC message into handy representation */
335 /* the additional information of TMC messages is submitted in (up to) 4 blocks of
336  * 28 bits each, which are to be treated as a consecutive bit-array. This data
337  * is represented by the optional_tmc array in the private handle, where each
338  * value represents 1 bit. Each additional information set is defined by a 4-bit
339  * label, and an associated data field for which the length is known */
rds_tmc_decode_additional(struct rds_private_state * priv_state)340 void rds_tmc_decode_additional(struct rds_private_state *priv_state)
341 {
342 	struct v4l2_rds_tmc_msg *msg = &priv_state->handle.tmc.tmc_msg;
343 	struct v4l2_tmc_additional *fields = &msg->additional.fields[0];
344 	const uint8_t label_len = 4;	/* fixed length of a label */
345 	uint8_t len; 		/* length of next data field to be extracted */
346 	uint8_t label;		/* buffer for extracted label */
347 	uint16_t data;		/* buffer for extracted data */
348 	uint8_t array_idx = 0;	/* index for optional_tmc array */
349 	uint8_t *field_idx = &msg->additional.size;	/* index for
350 				 * additional field array */
351 	/* LUT for the length of additional data blocks as defined in
352 	 * ISO 14819-1 sect. 5.5.1 */
353 	static const uint8_t additional_lut[16] = {
354 		3, 3, 5, 5, 5, 8, 8, 8, 8, 11, 16, 16, 16, 16, 0, 0
355 	};
356 
357 	/* reset the additional information from previous messages */
358 	*field_idx = 0;
359 	memset(fields, 0, sizeof(*fields));
360 
361 	/* decode the optional TMC data */
362 	while (array_idx < (msg->length * 28)) {
363 		/* extract the next label */
364 		label = 0;
365 		for (int i = 0; i < label_len; i++) {
366 			if (priv_state->optional_tmc[array_idx++])
367 				label |= 1 << (label_len - 1 - i);
368 		}
369 
370 		/* extract the associated data block */
371 		data = 0;
372 		len = additional_lut[label];	/* length of data block */
373 		for (int i = 0; i < len; i++) {
374 			if (priv_state->optional_tmc[array_idx++])
375 				data |= 1 << (len - 1 - i);
376 		}
377 
378 		/* if  the label is not "reserved for future use", or both
379 		 * fields are 0, store the extracted additional information */
380 		if (label == 15)
381 			continue;
382 		if (label == 0 && data == 0)
383 			continue;
384 		fields[*field_idx].label = label;
385 		fields[*field_idx].data = data;
386 		*field_idx += 1;
387 	}
388 }
389 
390 /* decode the TMC system information that is contained in type 3A groups
391  * that announce the presence of TMC */
rds_decode_tmc_system(struct rds_private_state * priv_state)392 static uint32_t rds_decode_tmc_system(struct rds_private_state *priv_state)
393 {
394 	struct v4l2_rds_group *group = &priv_state->rds_group;
395 	struct v4l2_rds_tmc *tmc = &priv_state->handle.tmc;
396 	uint8_t variant_code;
397 
398 	/* check if the same group was received twice. If not, store new
399 	 * group and return early */
400 	if (!rds_compare_group(&priv_state->prev_tmc_sys_group, &priv_state->rds_group)) {
401 		priv_state->prev_tmc_sys_group = priv_state->rds_group;
402 		return 0;
403 	}
404 	/* bits 14-15 of block 3 contain the variant code */
405 	variant_code = priv_state->rds_group.data_c_msb >> 6;
406 	switch (variant_code) {
407 	case 0x00:
408 		/* bits 11-16 of block 3 contain the LTN */
409 		tmc->ltn = (((group->data_c_msb & 0x0f) << 2)) |
410 			(group->data_c_lsb >> 6);
411 		/* bit 5 of block 3 contains the AFI */
412 		tmc->afi = group->data_c_lsb & 0x20;
413 		/* bit 4 of block 3 contains the Mode */
414 		tmc->enhanced_mode = group->data_c_lsb & 0x10;
415 		/* bits 0-3 of block 3 contain the MGS */
416 		tmc->mgs = group->data_c_lsb & 0x0f;
417 		break;
418 	case 0x01:
419 		/* bits 12-13 of block 3 contain the Gap parameters */
420 		tmc->gap = (group->data_c_msb & 0x30) >> 4;
421 		/* bits 11-16 of block 3 contain the SID */
422 		tmc->sid = (((group->data_c_msb & 0x0f) << 2)) |
423 			(group->data_c_lsb >> 6);
424 		/* timing information is only valid in enhanced mode */
425 		if (!tmc->enhanced_mode)
426 			break;
427 		/* bits 4-5 of block 3 contain the activity time */
428 		tmc->t_a = (group->data_c_lsb & 0x30) >> 4;
429 		/* bits 2-3 of block 3 contain the window time */
430 		tmc->t_w = (group->data_c_lsb & 0x0c) >> 2;
431 		/* bits 0-1 of block 3 contain the delay time */
432 		tmc->t_d = group->data_c_lsb & 0x03;
433 		break;
434 	}
435 	return V4L2_RDS_TMC_SYS;
436 }
437 
438 /* decode a single group TMC message */
rds_decode_tmc_single_group(struct rds_private_state * priv_state)439 static uint32_t rds_decode_tmc_single_group(struct rds_private_state *priv_state)
440 {
441 	struct v4l2_rds_group *grp = &priv_state->rds_group;
442 	struct v4l2_rds_tmc_msg msg;
443 
444 	/* bits 0-2 of group 2 contain the duration value */
445 	msg.dp = grp->data_b_lsb & 0x07;
446 	/* bit 15 of block 3 indicates follow diversion advice */
447 	msg.follow_diversion = grp->data_c_msb & 0x80;
448 	/* bit 14 of block 3 indicates the direction */
449 	msg.neg_direction = grp->data_c_msb & 0x40;
450 	/* bits 11-13 of block 3 contain the extend of the event */
451 	msg.extent = (grp->data_c_msb & 0x38) >> 3;
452 	/* bits 0-10 of block 3 contain the event */
453 	msg.event = ((grp->data_c_msb & 0x07) << 8) | grp->data_c_lsb;
454 	/* bits 0-15 of block 4 contain the location */
455 	msg.location = (grp->data_d_msb << 8) | grp->data_d_lsb;
456 	/* there is no service ID in a single group TMC message, so
457 	 * just set it to 0. */
458 	msg.sid = 0;
459 
460 	/* decoding done, store the new message */
461 	priv_state->handle.tmc.tmc_msg = msg;
462 	priv_state->handle.valid_fields |= V4L2_RDS_TMC_SG;
463 	priv_state->handle.valid_fields &= ~V4L2_RDS_TMC_MG;
464 
465 	return V4L2_RDS_TMC_SG;
466 }
467 
468 /* decode a multi group TMC message and decode the additional fields once
469  * a complete group was decoded */
rds_decode_tmc_multi_group(struct rds_private_state * priv_state)470 static uint32_t rds_decode_tmc_multi_group(struct rds_private_state *priv_state)
471 {
472 	struct v4l2_rds_group *grp = &priv_state->rds_group;
473 	struct v4l2_rds_tmc_msg *msg = &priv_state->new_tmc_msg;
474 	bool message_completed = false;
475 	uint8_t grp_seq_id;
476 	uint64_t buffer;
477 
478 	/* bits 12-13 of block 3 contain the group sequence id, for all
479 	 * multi groups except the first group */
480 	grp_seq_id = (grp->data_c_msb & 0x30) >> 4;
481 
482 	/* beginning of a new multigroup ? */
483 	/* bit 15 of block 3 is the first group indicator */
484 	if (grp->data_c_msb & 0x80) {
485 		/* begine decoding of new message */
486 		memset(msg, 0, sizeof(*msg));
487 		memset(priv_state->optional_tmc, 0, 112*sizeof(bool));
488 		/* bits 0-3 of block 2 contain continuity index */
489 		priv_state->continuity_id = grp->data_b_lsb & 0x07;
490 		/* bit 15 of block 3 indicates follow diversion advice */
491 		msg->follow_diversion = grp->data_c_msb & 0x80;
492 		/* bit 14 of block 3 indicates the direction */
493 		msg->neg_direction = grp->data_c_msb & 0x40;
494 		/* bits 11-13 of block 3 contain the extend of the event */
495 		msg->extent = (grp->data_c_msb & 0x38) >> 3;
496 		/* bits 0-10 of block 3 contain the event */
497 		msg->event = ((grp->data_c_msb & 0x07) << 8) | grp->data_c_lsb;
498 		/* bits 0-15 of block 4 contain the location */
499 		msg->location = (grp->data_d_msb << 8) | grp->data_d_lsb;
500 	}
501 	/* second group of multigroup ? */
502 	/* bit 14 of block 3 ist the second group indicator, and the
503 	 * group continuity id has to match */
504 	else if (grp->data_c_msb & 0x40 &&
505 		(grp->data_b_lsb & 0x07) == priv_state->continuity_id) {
506 		priv_state->grp_seq_id = grp_seq_id;
507 		/* store group for later decoding by transforming the bit values
508 		 * into boolean values and storing them in an array, to ease
509 		 * further handling */
510 		msg->length = 1;
511 		buffer = grp->data_c_msb << 24 | grp->data_c_lsb << 16 |
512 			grp->data_d_msb << 8 | grp->data_d_lsb;
513 		/* the buffer contains 28 bits of additional information */
514 		for (int i = 27; i >= 0; i--) {
515 			if (buffer & (1 << i))
516 				priv_state->optional_tmc[27-i] = true;
517 		}
518 		if (grp_seq_id == 0)
519 			message_completed = true;
520 	}
521 	/* subsequent groups of multigroup ? */
522 	/* group continuity id has to match, and group sequence number has
523 	 * to be smaller by one than the group sequence id */
524 	else if ((grp->data_b_lsb & 0x07) == priv_state->continuity_id &&
525 		(grp_seq_id == priv_state->grp_seq_id-1)) {
526 		priv_state->grp_seq_id = grp_seq_id;
527 		/* store group for later decoding */
528 		msg->length += 1;
529 		buffer = grp->data_c_msb << 24 | grp->data_c_lsb << 16|
530 			grp->data_d_msb << 8 | grp->data_d_lsb;
531 		/* the buffer contains 28 bits of additional information */
532 		for (int i = 27; i >= 0; i--) {
533 			if (buffer & (1 << i))
534 				priv_state->optional_tmc[msg->length*28 + 27 - i] = true;
535 		}
536 		if (grp_seq_id == 0)
537 			message_completed = true;
538 	}
539 
540 	/* complete message received -> decode additional fields and store
541 	 * the new message */
542 	if (message_completed) {
543 		priv_state->handle.tmc.tmc_msg = *msg;
544 		rds_tmc_decode_additional(priv_state);
545 		priv_state->handle.valid_fields |= V4L2_RDS_TMC_MG;
546 		priv_state->handle.valid_fields &= ~V4L2_RDS_TMC_SG;
547 	}
548 
549 	return V4L2_RDS_TMC_MG;
550 }
551 
552 /* decode the RDS-TMC tuning information that is contained in type 8A groups
553  * (variants 4 to 9) that announce the presence alternative transmitters
554  * providing the same RDS-TMC service */
rds_decode_tmc_tuning(struct rds_private_state * priv_state)555 static uint32_t rds_decode_tmc_tuning(struct rds_private_state *priv_state)
556 {
557 	struct v4l2_rds_group *group = &priv_state->rds_group;
558 	struct v4l2_rds_tmc *tmc = &priv_state->handle.tmc;
559 	uint8_t variant_code = group->data_b_lsb & 0x0f;
560 	uint16_t pi_on = (group->data_d_msb << 8) | group->data_d_lsb;
561 	uint8_t index;
562 
563 	/* variants 4 and 5 carry the service provider name */
564 	if (variant_code >= 4 && variant_code <= 5) {
565 		int offset = 4 * (variant_code - 4);
566 		tmc->spn[0 + offset] = group->data_c_msb;
567 		tmc->spn[1 + offset] = group->data_c_lsb;
568 		tmc->spn[2 + offset] = group->data_d_msb;
569 		tmc->spn[3 + offset] = group->data_d_lsb;
570 
571 	/* variant 6 provides specific frequencies for the same RDS-TMC service
572 	 * on a network with a different PI code */
573 	/* variant 7 provides mapped frequency pair information which should only
574 	 * be used if the terminal is tuned to the tuning frequency */
575 	} else if (variant_code == 6 || variant_code == 7) {
576 		rds_add_tmc_af(priv_state);
577 
578 	/* variant 8 indicates up to 2 PI codes of adjacent networks carrying
579 	 * the same RDS-TMC service on all transmitters of the network */
580 	} else if (variant_code == 8) {
581 		uint16_t pi_on_2 = (group->data_c_msb << 8) | group->data_c_lsb;
582 
583 		/* try to add both transmitted PI codes to the table */
584 		rds_add_tmc_station(priv_state, pi_on);
585 		/* PI = 0 is used as a filler code */
586 		if (pi_on_2 != 0)
587 			rds_add_tmc_station(priv_state, pi_on_2);
588 
589 	/* variant 9 provides PI codes of other networks with different system
590 	 * parameters */
591 	} else if (variant_code == 9) {
592 		index = rds_add_tmc_station(priv_state, pi_on);
593 
594 		/* bits 0 - 5 contain the service-ID of the ON */
595 		tmc->tuning.station[index].sid = group->data_c_lsb & 0x3F;
596 		/* bits 6-10 contain the msg parameters of the ON */
597 		tmc->tuning.station[index].msg = (group->data_c_msb & 0x03) << 2;
598 		tmc->tuning.station[index].msg |= (group->data_c_lsb >> 6) & 0x03;
599 		/* bits 11-15 contain the database-ID of the ON */
600 		tmc->tuning.station[index].ltn = group->data_c_msb >> 2;
601 	}
602 
603 	return V4L2_RDS_TMC_TUNING;
604 }
605 
rds_add_oda(struct rds_private_state * priv_state,struct v4l2_rds_oda oda)606 static bool rds_add_oda(struct rds_private_state *priv_state, struct v4l2_rds_oda oda)
607 {
608 	struct v4l2_rds *handle = &priv_state->handle;
609 
610 	/* check if there was already an ODA announced for this group type */
611 	for (int i = 0; i < handle->rds_oda.size; i++) {
612 		if (handle->rds_oda.oda[i].group_id == oda.group_id) {
613 			/* update the AID for this ODA */
614 			handle->rds_oda.oda[i].aid = oda.aid;
615 			return false;
616 		}
617 	}
618 	/* add the new ODA */
619 	if (handle->rds_oda.size >= MAX_ODA_CNT)
620 		return false;
621 	handle->rds_oda.oda[handle->rds_oda.size++] = oda;
622 	return true;
623 }
624 
625 /* add a new AF to the list, if it doesn't exist yet */
rds_add_af_to_list(struct v4l2_rds_af_set * af_set,uint8_t af,bool is_vhf)626 static bool rds_add_af_to_list(struct v4l2_rds_af_set *af_set, uint8_t af, bool is_vhf)
627 {
628 	/* convert the frequency to Hz, skip on errors */
629 	uint32_t freq = rds_decode_af(af, is_vhf);
630 
631 	if (freq == 0)
632 		return false;
633 
634 	/* prevent buffer overflows */
635 	if (af_set->size >= MAX_AF_CNT || af_set->size >= af_set->announced_af)
636 		return false;
637 	/* check if AF already exists */
638 	for (int i = 0; i < af_set->size; i++) {
639 		if (af_set->af[i] == freq)
640 			return false;
641 	}
642 	/* it's a new AF, add it to the list */
643 	af_set->af[af_set->size++] = freq;
644 	return true;
645 }
646 
647 /* extracts the AF information from Block 3 of type 0A groups, and tries
648  * to add them to the AF list with a helper function */
rds_add_af(struct rds_private_state * priv_state)649 static bool rds_add_af(struct rds_private_state *priv_state)
650 {
651 	struct v4l2_rds *handle = &priv_state->handle;
652 
653 	/* AFs are submitted in Block 3 of type 0A groups */
654 	uint8_t c_msb = priv_state->rds_group.data_c_msb;
655 	uint8_t c_lsb = priv_state->rds_group.data_c_lsb;
656 	bool updated_af = false;
657 	struct v4l2_rds_af_set *af_set = &handle->rds_af;
658 
659 	/* the 4 8-bit values in the block's data fields (c_msb/c_lsb,
660 	 * d_msb/d_lsb) represent either a carrier frequency (1..204)
661 	 * or a special meaning (205..255).
662 	 * Translation tables can be found in IEC 62106 section 6.2.1.6 */
663 
664 	/* 250: LF / MF frequency follows */
665 	if (c_msb == 250) {
666 		if (rds_add_af_to_list(af_set, c_lsb, false))
667 			updated_af = true;
668 		c_lsb = 0; /* invalidate */
669 	}
670 	/* 224..249: announcement of AF count (224=0, 249=25) */
671 	if (c_msb >= 224 && c_msb <= 249) {
672 		if (af_set->announced_af != c_msb - 224) {
673 			updated_af = true;
674 			af_set->size = 0;
675 		}
676 		af_set->announced_af = c_msb - 224;
677 	}
678 	/* check if the data represents an AF (for 1 <= val <= 204 the
679 	 * value represents an AF) */
680 	if (c_msb < 205)
681 		if (rds_add_af_to_list(af_set, c_msb, true))
682 			updated_af = true;
683 	if (c_lsb < 205)
684 		if (rds_add_af_to_list(af_set, c_lsb, true))
685 			updated_af = true;
686 	/* did we receive all announced AFs? */
687 	if (af_set->size >= af_set->announced_af && af_set->announced_af != 0)
688 		handle->valid_fields |= V4L2_RDS_AF;
689 	return updated_af;
690 }
691 
692 
693 
694 /* adds one char of the ps name to temporal storage, the value is validated
695  * if it is received twice in a row
696  * @pos:	position of the char within the PS name (0..7)
697  * @ps_char:	the new character to be added
698  * @return:	true, if all 8 temporal ps chars have been validated */
rds_add_ps(struct rds_private_state * priv_state,uint8_t pos,uint8_t ps_char)699 static bool rds_add_ps(struct rds_private_state *priv_state, uint8_t pos, uint8_t ps_char)
700 {
701 	if (ps_char == priv_state->new_ps[pos]) {
702 		priv_state->new_ps_valid[pos] = 1;
703 	} else {
704 		priv_state->new_ps[pos] = ps_char;
705 		memset(priv_state->new_ps_valid, 0, 8);
706 	}
707 
708 	/* check if all ps positions have been validated */
709 	for (int i = 0; i < 8; i++)
710 		if (priv_state->new_ps_valid[i] != 1)
711 			return false;
712 	return true;
713 }
714 
715 /* checks if an entry for the given PI already exists and returns the index
716  * of that entry if so. Else it adds a new entry to the EON table and returns
717  * the index of the new field */
rds_add_eon_entry(struct rds_private_state * priv_state,uint16_t pi)718 static uint8_t rds_add_eon_entry(struct rds_private_state *priv_state, uint16_t pi)
719 {
720 	struct v4l2_rds *handle = &priv_state->handle;
721 	uint8_t index = handle->rds_eon.index;
722 	uint8_t size = handle->rds_eon.size;
723 
724 	/* check if there's an entry for the given PI key */
725 	for (int i = 0; i < handle->rds_eon.size; i++) {
726 		if (handle->rds_eon.eon[i].pi == pi) {
727 			return i;
728 		}
729 	}
730 	/* if the the maximum table size is reached, overwrite old
731 	 * entries, starting at the oldest one = 0 */
732 	handle->rds_eon.eon[index].pi = pi;
733 	handle->rds_eon.eon[index].valid_fields |= V4L2_RDS_PI;
734 	handle->rds_eon.index = (index+1 < MAX_EON_CNT) ? (index+1) : 0;
735 	handle->rds_eon.size = (size+1 <= MAX_EON_CNT) ? (size+1) : MAX_EON_CNT;
736 	return index;
737 }
738 
739 /* checks if an entry for the given PI already exists */
rds_check_eon_entry(struct rds_private_state * priv_state,uint16_t pi)740 static bool rds_check_eon_entry(struct rds_private_state *priv_state, uint16_t pi)
741 {
742 	struct v4l2_rds *handle = &priv_state->handle;
743 
744 	/* check if there's an entry for the given PI key */
745 	for (int i = 0; i <= handle->rds_eon.size; i++) {
746 		if (handle->rds_eon.eon[i].pi == pi) {
747 			return true;
748 		}
749 	}
750 	return false;
751 }
752 
753 /* group of functions to decode successfully received RDS groups into
754  * easily accessible data fields
755  *
756  * group 0: basic tuning and switching */
rds_decode_group0(struct rds_private_state * priv_state)757 static uint32_t rds_decode_group0(struct rds_private_state *priv_state)
758 {
759 	struct v4l2_rds *handle = &priv_state->handle;
760 	struct v4l2_rds_group *grp = &priv_state->rds_group;
761 	bool new_ps = false;
762 	bool tmp;
763 	uint32_t updated_fields = 0;
764 
765 	/* bit 4 of block B contains the TA flag */
766 	tmp = grp->data_b_lsb & 0x10;
767 	if (handle->ta != tmp) {
768 		handle->ta = tmp;
769 		updated_fields |= V4L2_RDS_TA;
770 	}
771 	handle->valid_fields |= V4L2_RDS_TA;
772 
773 	/* bit 3 of block B contains the Music/Speech flag */
774 	tmp = grp->data_b_lsb & 0x08;
775 	if (handle->ms != tmp) {
776 		handle->ms = tmp;
777 		updated_fields |= V4L2_RDS_MS;
778 	}
779 	handle->valid_fields |= V4L2_RDS_MS;
780 
781 	/* bit 0-1 of block b contain program service name and decoder
782 	 * control segment address */
783 	uint8_t segment = grp->data_b_lsb & 0x03;
784 
785 	/* put the received station-name characters into the correct position
786 	 * of the station name, and check if the new PS is validated */
787 	rds_add_ps(priv_state, segment * 2, grp->data_d_msb);
788 	new_ps = rds_add_ps(priv_state, segment * 2 + 1, grp->data_d_lsb);
789 	if (new_ps) {
790 		/* check if new PS is the same as the old one */
791 		if (memcmp(priv_state->new_ps, handle->ps, 8) != 0) {
792 			memcpy(handle->ps, priv_state->new_ps, 8);
793 			updated_fields |= V4L2_RDS_PS;
794 		}
795 		handle->valid_fields |= V4L2_RDS_PS;
796 	}
797 
798 	/* bit 2 of block B contains 1 bit of the Decoder Control Information (DI)
799 	 * the segment number defines the bit position
800 	 * New bits are only accepted if the segments arrive in the correct order */
801 	bool bit2 = grp->data_b_lsb & 0x04;
802 	if (segment == 0 || segment == priv_state->next_di_segment) {
803 		switch (segment) {
804 		case 0:
805 			priv_state->new_di = set_bit(priv_state->new_di,
806 				V4L2_RDS_FLAG_DYNAMIC_PTY, bit2);
807 			priv_state->next_di_segment = 1;
808 			break;
809 		case 1:
810 			priv_state->new_di = set_bit(priv_state->new_di,
811 				V4L2_RDS_FLAG_COMPRESSED, bit2);
812 			priv_state->next_di_segment = 2;
813 			break;
814 		case 2:
815 			priv_state->new_di = set_bit(priv_state->new_di,
816 				V4L2_RDS_FLAG_ARTIFICIAL_HEAD, bit2);
817 			priv_state->next_di_segment = 3;
818 			break;
819 		case 3:
820 			priv_state->new_di = set_bit(priv_state->new_di,
821 				V4L2_RDS_FLAG_STEREO, bit2);
822 			/* check if the value of DI has changed, and store
823 			 * and signal DI update in case */
824 			if (handle->di != priv_state->new_di) {
825 				handle->di = priv_state->new_di;
826 				updated_fields |= V4L2_RDS_DI;
827 			}
828 			priv_state->next_di_segment = 0;
829 			handle->valid_fields |= V4L2_RDS_DI;
830 			break;
831 		}
832 	} else {
833 		/* wrong order of DI segments -> restart */
834 		priv_state->next_di_segment = 0;
835 		priv_state->new_di = 0;
836 	}
837 
838 	/* version A groups contain AFs in block C */
839 	if (grp->group_version == 'A')
840 		if (rds_add_af(priv_state))
841 			updated_fields |= V4L2_RDS_AF;
842 
843 	return updated_fields;
844 }
845 
846 /* group 1: slow labeling codes & program item number */
rds_decode_group1(struct rds_private_state * priv_state)847 static uint32_t rds_decode_group1(struct rds_private_state *priv_state)
848 {
849 	struct v4l2_rds *handle = &priv_state->handle;
850 	struct v4l2_rds_group *grp = &priv_state->rds_group;
851 	uint32_t updated_fields = 0;
852 	uint8_t variant_code = 0;
853 
854 	/* version A groups contain slow labeling codes,
855 	 * version B groups only contain program item number which is a
856 	 * very uncommonly used feature */
857 	if (grp->group_version != 'A')
858 		return 0;
859 
860 	/* bit 14-12 of block c contain the variant code */
861 	variant_code = (grp->data_c_msb >> 4) & 0x07;
862 	if (variant_code == 0) {
863 		/* var 0 -> ECC, only accept if same lc is
864 		 * received twice */
865 		if (grp->data_c_lsb == priv_state->new_ecc) {
866 			handle->valid_fields |= V4L2_RDS_ECC;
867 			if (handle->ecc != grp->data_c_lsb)
868 				updated_fields |= V4L2_RDS_ECC;
869 			handle->ecc = grp->data_c_lsb;
870 		} else {
871 			priv_state->new_ecc = grp->data_c_lsb;
872 		}
873 	} else if (variant_code == 0x03) {
874 		/* var 0x03 -> Language Code, only accept if same lc is
875 		 * received twice */
876 		if (grp->data_c_lsb == priv_state->new_lc) {
877 			handle->valid_fields |= V4L2_RDS_LC;
878 			updated_fields |= V4L2_RDS_LC;
879 			handle->lc = grp->data_c_lsb;
880 		} else {
881 			priv_state->new_lc = grp->data_c_lsb;
882 		}
883 	}
884 	return updated_fields;
885 }
886 
887 /* group 2: radio text */
rds_decode_group2(struct rds_private_state * priv_state)888 static uint32_t rds_decode_group2(struct rds_private_state *priv_state)
889 {
890 	struct v4l2_rds *handle = &priv_state->handle;
891 	struct v4l2_rds_group *grp = &priv_state->rds_group;
892 	uint32_t updated_fields = 0;
893 
894 	/* bit 0-3 of block B contain the segment code */
895 	uint8_t segment = grp->data_b_lsb & 0x0f;
896 	/* bit 4 of block b contains the A/B text flag (new radio text
897 	 * will be transmitted) */
898 	bool rt_ab_flag_n = grp->data_b_lsb & 0x10;
899 
900 	/* new Radio Text will be transmitted */
901 	if (rt_ab_flag_n != handle->rt_ab_flag) {
902 		handle->rt_ab_flag = rt_ab_flag_n;
903 		memset(handle->rt, 0, 64);
904 		handle->valid_fields &= ~V4L2_RDS_RT;
905 		updated_fields |= V4L2_RDS_RT;
906 		priv_state->next_rt_segment = 0;
907 	}
908 
909 	/* further decoding of data depends on type of message (A or B)
910 	 * Type A allows RTs with a max length of 64 chars
911 	 * Type B allows RTs with a max length of 32 chars */
912 	if (grp->group_version == 'A') {
913 		if (segment == 0 || segment == priv_state->next_rt_segment) {
914 			priv_state->new_rt[segment * 4] = grp->data_c_msb;
915 			priv_state->new_rt[segment * 4 + 1] = grp->data_c_lsb;
916 			priv_state->new_rt[segment * 4 + 2] = grp->data_d_msb;
917 			priv_state->new_rt[segment * 4 + 3] = grp->data_d_lsb;
918 			priv_state->next_rt_segment = segment + 1;
919 			if (segment == 0x0f) {
920 				handle->rt_length = 64;
921 				handle->valid_fields |= V4L2_RDS_RT;
922 				if (memcmp(handle->rt, priv_state->new_rt, 64)) {
923 					memcpy(handle->rt, priv_state->new_rt, 64);
924 					updated_fields |= V4L2_RDS_RT;
925 				}
926 				priv_state->next_rt_segment = 0;
927 			}
928 		}
929 	} else {
930 		if (segment == 0 || segment == priv_state->next_rt_segment) {
931 			priv_state->new_rt[segment * 2] = grp->data_d_msb;
932 			priv_state->new_rt[segment * 2 + 1] = grp->data_d_lsb;
933 			/* PI code in block C will be ignored */
934 			priv_state->next_rt_segment = segment + 1;
935 			if (segment == 0x0f) {
936 				handle->rt_length = 32;
937 				handle->valid_fields |= V4L2_RDS_RT;
938 				updated_fields |= V4L2_RDS_RT;
939 				if (memcmp(handle->rt, priv_state->new_rt, 32)) {
940 					memcpy(handle->rt, priv_state->new_rt, 32);
941 					updated_fields |= V4L2_RDS_RT;
942 				}
943 				priv_state->next_rt_segment = 0;
944 			}
945 		}
946 	}
947 
948 	/* determine if complete rt was received
949 	 * a carriage return (0x0d) can end a message early */
950 	for (int i = 0; i < 64; i++) {
951 		if (priv_state->new_rt[i] == 0x0d) {
952 			/* replace CR with terminating character */
953 			priv_state->new_rt[i] = '\0';
954 			handle->rt_length = i;
955 			handle->valid_fields |= V4L2_RDS_RT;
956 			if (memcmp(handle->rt, priv_state->new_rt, handle->rt_length)) {
957 					memcpy(handle->rt, priv_state->new_rt,
958 						handle->rt_length);
959 					updated_fields |= V4L2_RDS_RT;
960 				}
961 			priv_state->next_rt_segment = 0;
962 		}
963 	}
964 	return updated_fields;
965 }
966 
967 /* group 3: Open Data Announcements */
rds_decode_group3(struct rds_private_state * priv_state)968 static uint32_t rds_decode_group3(struct rds_private_state *priv_state)
969 {
970 	struct v4l2_rds *handle = &priv_state->handle;
971 	struct v4l2_rds_group *grp = &priv_state->rds_group;
972 	struct v4l2_rds_oda new_oda;
973 	uint32_t updated_fields = 0;
974 
975 	if (grp->group_version != 'A')
976 		return 0;
977 
978 	/* 0th bit of block b contains Group Type Info version of announced ODA
979 	 * Group Type info: 0 = A, 1 = B */
980 	new_oda.group_version = (grp->data_b_lsb & 0x01) ? 'B' : 'A';
981 	/* 1st to 4th bit contain Group ID of announced ODA */
982 	new_oda.group_id = (grp->data_b_lsb & 0x1e) >> 1;
983 	/* block D contains the 16bit Application Identification Code */
984 	new_oda.aid = (grp->data_d_msb << 8) | grp->data_d_lsb;
985 
986 	/* try to add the new ODA to the set of defined ODAs */
987 	if (rds_add_oda(priv_state, new_oda)) {
988 		handle->decode_information |= V4L2_RDS_ODA;
989 		updated_fields |= V4L2_RDS_ODA;
990 	}
991 
992 	/* if it's a TMC announcement decode the contained information */
993 	if (new_oda.aid == 0xcd46 || new_oda.aid == 0xcd47) {
994 		rds_decode_tmc_system(priv_state);
995 	}
996 
997 	return updated_fields;
998 }
999 
1000 /* decodes the RDS date/time representation into a standard c representation
1001  * that can be used with c-library functions */
rds_decode_mjd(const struct rds_private_state * priv_state)1002 static time_t rds_decode_mjd(const struct rds_private_state *priv_state)
1003 {
1004 	struct tm new_time;
1005 	int y, m, d, k = 0;
1006 	/* offset is given in multiples of half hrs */
1007 	uint32_t offset = priv_state->utc_offset & 0x1f;
1008 	uint32_t local_mjd = priv_state->new_mjd;
1009 	uint8_t local_hour = priv_state->utc_hour;
1010 	uint8_t local_minute = priv_state->utc_minute;
1011 
1012 	/* add / subtract the local offset to get the local time.
1013 	 * The offset is expressed in multiples of half hours */
1014 	if (priv_state->utc_offset & 0x20) { /* bit 5 indicates -/+ */
1015 		local_hour -= offset / 2;
1016 		local_minute -= (offset % 2) * 30;
1017 	} else {
1018 		local_hour += offset / 2;
1019 		local_minute += (offset % 2) * 30;
1020 	}
1021 
1022 	/* the formulas for the conversion are taken from Annex G of the
1023 	 * IEC 62106 RDS standard */
1024 	y = (int)((local_mjd - 15078.2) / 365.25);
1025 	m = (int)((local_mjd - 14956.1 - (int)(y * 365.25)) / 30.6001);
1026 	d = (int)(local_mjd - 14956 - (int)(y * 365.25) - (int)(m * 30.6001));
1027 	if (m == 14 || m == 15)
1028 		k = 1;
1029 	y = y + k;
1030 	m = m - 1 - k*12;
1031 
1032 	/* put the values into a tm struct for conversion into time_t value */
1033 	new_time.tm_sec = 0;
1034 	new_time.tm_min = local_minute;
1035 	new_time.tm_hour = local_hour;
1036 	new_time.tm_mday = d;
1037 	new_time.tm_mon = m - 1;
1038 	new_time.tm_year = y;
1039 	/* offset (submitted by RDS) that was used to compute the local time,
1040 	 * expressed in multiples of half hours, bit 5 indicates -/+ */
1041 	if (priv_state->utc_offset & 0x20)
1042 		new_time.tm_gmtoff = -offset * 1800;
1043 	else
1044 		new_time.tm_gmtoff = offset * 1800;
1045 
1046 	/* convert tm struct to time_t value and return it */
1047 	return mktime(&new_time);
1048 }
1049 
1050 /* group 4: Date and Time */
rds_decode_group4(struct rds_private_state * priv_state)1051 static uint32_t rds_decode_group4(struct rds_private_state *priv_state)
1052 {
1053 	struct v4l2_rds *handle = &priv_state->handle;
1054 	struct v4l2_rds_group *grp = &priv_state->rds_group;
1055 	uint32_t mjd;
1056 	uint32_t updated_fields = 0;
1057 
1058 	if (grp->group_version != 'A')
1059 		return 0;
1060 
1061 	/* bits 0-1 of block b lsb contain bits 15 and 16 of Julian day code
1062 	 * bits 0-7 of block c msb contain bits 7 to 14 of Julian day code
1063 	 * bits 1-7 of block c lsb contain bits 0 to 6 of Julian day code */
1064 	mjd = ((grp->data_b_lsb & 0x03) << 15) |
1065 		(grp->data_c_msb << 7) | (grp->data_c_lsb >> 1);
1066 	/* the same mjd has to be received twice in order to accept the data */
1067 	if (priv_state->new_mjd != mjd) {
1068 		priv_state->new_mjd = mjd;
1069 		return 0;
1070 	}
1071 	/* same mjd received at least twice --> decode time & date */
1072 
1073 	/* bit 0 of block c lsb contains bit 4 of utc_hour
1074 	 * bits 4-7 of block d contains bits 0 to 3 of utc_hour */
1075 	priv_state->utc_hour = ((grp->data_c_lsb & 0x01) << 4) |
1076 		(grp->data_d_msb >> 4);
1077 
1078 	/* bits 0-3 of block d msb contain bits 2 to 5 of utc_minute
1079 	 * bits 6-7 of block d lsb contain bits 0 and 1 utc_minute */
1080 	priv_state->utc_minute = ((grp->data_d_msb & 0x0f) << 2) |
1081 		(grp->data_d_lsb >> 6);
1082 
1083 	/* bits 0-5 of block d lsb contain bits 0 to 5 of local time offset */
1084 	priv_state->utc_offset = grp->data_d_lsb & 0x3f;
1085 
1086 	/* decode RDS time representation into commonly used c representation */
1087 	handle->time = rds_decode_mjd(priv_state);
1088 	updated_fields |= V4L2_RDS_TIME;
1089 	handle->valid_fields |= V4L2_RDS_TIME;
1090 	return updated_fields;
1091 }
1092 
1093 /* group 8A: TMC */
rds_decode_group8(struct rds_private_state * priv_state)1094 static uint32_t rds_decode_group8(struct rds_private_state *priv_state)
1095 {
1096 	struct v4l2_rds_group *grp = &priv_state->rds_group;
1097 	uint8_t tuning_variant;
1098 
1099 	/* TMC uses version A exclusively */
1100 	if (grp->group_version != 'A')
1101 		return 0;
1102 
1103 	/* check if the same group was received twice, store new rds group
1104 	 * and return early if the old group doesn't match the new one */
1105 	if (!rds_compare_group(&priv_state->prev_tmc_group, &priv_state->rds_group)) {
1106 		priv_state->prev_tmc_group = priv_state->rds_group;
1107 		return 0;
1108 	}
1109 	/* modify the old group, to prevent that the same TMC message is decoded
1110 	 * again in the next iteration (the default number of repetitions for
1111 	 * RDS-TMC groups is 3) */
1112 	priv_state->prev_tmc_group.group_version = 0;
1113 
1114 	/* handle the new TMC data depending on the message type */
1115 	/* -> single group message */
1116 	if ((grp->data_b_lsb & V4L2_TMC_SINGLE_GROUP) &&
1117 		!(grp->data_b_lsb & V4L2_TMC_TUNING_INFO)) {
1118 		return rds_decode_tmc_single_group(priv_state);
1119 	}
1120 	/* -> multi group message */
1121 	if (!(grp->data_b_lsb & V4L2_TMC_SINGLE_GROUP) &&
1122 		!(grp->data_b_lsb & V4L2_TMC_TUNING_INFO)) {
1123 		return rds_decode_tmc_multi_group(priv_state);
1124 	}
1125 	/* -> tuning information message, defined for variants 4..9,
1126 	 * submitted in bits 0-3 of block 2 */
1127 	tuning_variant = grp->data_b_lsb & 0x0f;
1128 	if ((grp->data_b_lsb & V4L2_TMC_TUNING_INFO) && tuning_variant >= 4 &&
1129 		tuning_variant <= 9) {
1130 		priv_state->handle.valid_fields |= V4L2_RDS_TMC_TUNING;
1131 		return rds_decode_tmc_tuning(priv_state);
1132 	}
1133 
1134 	return 0;
1135 }
1136 
1137 /* group 10: Program Type Name */
rds_decode_group10(struct rds_private_state * priv_state)1138 static uint32_t rds_decode_group10(struct rds_private_state *priv_state)
1139 {
1140 	struct v4l2_rds *handle = &priv_state->handle;
1141 	struct v4l2_rds_group *grp = &priv_state->rds_group;
1142 	uint32_t updated_fields = 0;
1143 	uint8_t ptyn_tmp[4];
1144 
1145 	/* bit 0 of block B contain the segment code */
1146 	uint8_t segment_code = grp->data_b_lsb & 0x01;
1147 	/* bit 4 of block b contains the A/B text flag (new ptyn
1148 	 * will be transmitted) */
1149 	bool ptyn_ab_flag_n = grp->data_b_lsb & 0x10;
1150 
1151 	if (grp->group_version != 'A')
1152 		return 0;
1153 
1154 	/* new Program Type Text will be transmitted */
1155 	if (ptyn_ab_flag_n != handle->ptyn_ab_flag) {
1156 		handle->ptyn_ab_flag = ptyn_ab_flag_n;
1157 		memset(handle->ptyn, 0, 8 * sizeof(char));
1158 		memset(priv_state->new_ptyn, 0, 8 * sizeof(char));
1159 		memset(priv_state->new_ptyn_valid, 0, 2 * sizeof(bool));
1160 		handle->valid_fields &= ~V4L2_RDS_PTYN;
1161 		updated_fields |= V4L2_RDS_PTYN;
1162 	}
1163 	/* copy chars to designated position within temp text field */
1164 	ptyn_tmp[0] = grp->data_c_msb;
1165 	ptyn_tmp[1] = grp->data_c_lsb;
1166 	ptyn_tmp[2] = grp->data_d_msb;
1167 	ptyn_tmp[3] = grp->data_d_lsb;
1168 
1169 	/* only validate ptyn segment if the same data is received twice */
1170 	if (memcmp(ptyn_tmp, priv_state->new_ptyn[segment_code], 4) == 0) {
1171 		priv_state->new_ptyn_valid[segment_code] = true;
1172 	} else {
1173 		for (int i = 0; i < 4; i++)
1174 			priv_state->new_ptyn[segment_code][i] = ptyn_tmp[i];
1175 		priv_state->new_ptyn_valid[segment_code] = false;
1176 	}
1177 
1178 	/* if both ptyn segments have been validated, accept the new ptyn */
1179 	if (priv_state->new_ptyn_valid[0] && priv_state->new_ptyn_valid[1]) {
1180 		for (int i = 0; i < 4; i++) {
1181 			handle->ptyn[i] = priv_state->new_ptyn[0][i];
1182 			handle->ptyn[4 + i] = priv_state->new_ptyn[1][i];
1183 		}
1184 		handle->valid_fields |= V4L2_RDS_PTYN;
1185 		updated_fields |= V4L2_RDS_PTYN;
1186 	}
1187 	return updated_fields;
1188 }
1189 
1190 /* group 14: EON (Enhanced Other Network) information */
rds_decode_group14(struct rds_private_state * priv_state)1191 static uint32_t rds_decode_group14(struct rds_private_state* priv_state)
1192 {
1193 	struct v4l2_rds *handle = &priv_state->handle;
1194 	struct v4l2_rds_group *grp = &priv_state->rds_group;
1195 	struct v4l2_rds_eon *eon_entry;
1196 	uint32_t updated_fields = 0;
1197 	uint16_t pi_on;
1198 	uint16_t lsf_on;
1199 	uint8_t variant_code;
1200 	uint8_t eon_index;
1201 	uint8_t pty_on;
1202 	bool tp_on, ta_on;
1203 	bool new_a = false, new_b = false;
1204 
1205 	if (grp->group_version != 'A')
1206 		return 0;
1207 
1208 	/* bits 0-3 of group b contain the variant code */
1209 	variant_code = grp->data_b_lsb & 0x0f;
1210 
1211 	/* group d contains the PI code of the ON (Other Network) */
1212 	pi_on = (grp->data_d_msb << 8) | grp->data_d_lsb;
1213 
1214 	/* bit 4 of group b contains the TP status of the ON*/
1215 	tp_on = grp->data_b_lsb & 0x10;
1216 	if (rds_check_eon_entry(priv_state, pi_on)) {
1217 		/* if there's an entry for this PI(ON) update the TP field */
1218 		eon_index = rds_add_eon_entry(priv_state, pi_on);
1219 		eon_entry = &handle->rds_eon.eon[eon_index];
1220 		eon_entry->tp = tp_on;
1221 		eon_entry->valid_fields |= V4L2_RDS_TP;
1222 		updated_fields |= V4L2_RDS_EON;
1223 	}
1224 
1225 	/* perform group variant dependent decoding */
1226 	if ((variant_code >=5 && variant_code <= 11) || variant_code >= 14) {
1227 		/* 5-9 = mapped FM frequencies -> unsupported
1228 		 * 10-11 = unallocated
1229 		 * 14 = PIN(ON) -> unsupported (unused RDS feature)
1230 		 * 15 = reserved for broadcasters use */
1231 		return updated_fields;
1232 	}
1233 
1234 	/* retrieve the EON entry corresponding to the PI(ON) code or add a new
1235 	 * entry to the table if no entry exists */
1236 	eon_index = rds_add_eon_entry(priv_state, pi_on);
1237 	eon_entry = &handle->rds_eon.eon[eon_index];
1238 
1239 	/* PS Name */
1240 	if (variant_code < 4) {
1241 		eon_entry->ps[variant_code*2] = grp->data_c_msb;
1242 		eon_entry->ps[variant_code*2+1] = grp->data_c_lsb;
1243 		eon_entry->valid_fields |= V4L2_RDS_PS;
1244 		updated_fields |= V4L2_RDS_EON;
1245 	}
1246 	/* Alternative frequencies */
1247 	else if (variant_code == 4) {
1248 		uint8_t c_msb = grp->data_c_msb;
1249 		uint8_t c_lsb = grp->data_c_lsb;
1250 
1251 		/* 224..249: announcement of AF count (224=0, 249=25) */
1252 		if (c_msb >= 224 && c_msb <= 249)
1253 			eon_entry->af.announced_af = c_msb - 224;
1254 		/* check if the data represents an AF (for 1 =< val <= 204 the
1255 		 * value represents an AF) */
1256 		if (c_msb < 205)
1257 			new_a = rds_add_af_to_list(&eon_entry->af,
1258 					grp->data_c_msb, true);
1259 		if (c_lsb < 205)
1260 			new_b = rds_add_af_to_list(&eon_entry->af,
1261 					grp->data_c_lsb, true);
1262 		/* check if one of the frequencies was previously unknown */
1263 		if (new_a || new_b) {
1264 			eon_entry->valid_fields |= V4L2_RDS_AF;
1265 			updated_fields |= V4L2_RDS_EON;
1266 		}
1267 	}
1268 	/* Linkage information */
1269 	else if (variant_code == 12) {
1270 		/* group c contains the lsf code */
1271 		lsf_on = (grp->data_c_msb << 8) | grp->data_c_lsb;
1272 		/* check if the lsf code is already known */
1273 		new_a = (eon_entry->lsf == lsf_on);
1274 		if (new_a) {
1275 			eon_entry->lsf = lsf_on;
1276 			eon_entry->valid_fields |= V4L2_RDS_LSF;
1277 			updated_fields |= V4L2_RDS_EON;
1278 		}
1279 	}
1280 	/* PTY(ON) and TA(ON) */
1281 	else if (variant_code == 13) {
1282 		/* bits 15-10 of group c contain the PTY(ON) */
1283 		pty_on = grp->data_c_msb >> 3;
1284 		/* bit 0 of group c contains the TA code */
1285 		ta_on = grp->data_c_lsb & 0x01;
1286 		/* check if the data is new */
1287 		new_a = (eon_entry->pty == pty_on);
1288 		if (new_a) {
1289 			eon_entry->pty = pty_on;
1290 			eon_entry->valid_fields |= V4L2_RDS_PTY;
1291 		}
1292 		new_b = (eon_entry->ta == ta_on);
1293 		eon_entry->ta = ta_on;
1294 		eon_entry->valid_fields |= V4L2_RDS_TA;
1295 		if (new_a || new_b)
1296 			updated_fields |= V4L2_RDS_EON;
1297 	}
1298 	/* set valid field for EON data, if EON table contains entries */
1299 	if (handle->rds_eon.size > 0)
1300 		handle->valid_fields |= V4L2_RDS_EON;
1301 
1302 	return updated_fields;
1303 }
1304 
1305 typedef uint32_t (*decode_group_func)(struct rds_private_state *);
1306 
1307 /* array of function pointers to contain all group specific decoding functions */
1308 static const decode_group_func decode_group[16] = {
1309 	[0] = rds_decode_group0,
1310 	[1] = rds_decode_group1,
1311 	[2] = rds_decode_group2,
1312 	[3] = rds_decode_group3,
1313 	[4] = rds_decode_group4,
1314 	[8] = rds_decode_group8,
1315 	[10] = rds_decode_group10,
1316 	[14] = rds_decode_group14
1317 };
1318 
rds_decode_group(struct rds_private_state * priv_state)1319 static uint32_t rds_decode_group(struct rds_private_state *priv_state)
1320 {
1321 	struct v4l2_rds *handle = &priv_state->handle;
1322 	uint8_t group_id = priv_state->rds_group.group_id;
1323 
1324 	/* count the group type, and decode it if it is supported */
1325 	handle->rds_statistics.group_type_cnt[group_id]++;
1326 	if (decode_group[group_id])
1327 		return (*decode_group[group_id])(priv_state);
1328 	return 0;
1329 }
1330 
v4l2_rds_create(bool is_rbds)1331 struct v4l2_rds *v4l2_rds_create(bool is_rbds)
1332 {
1333 	struct rds_private_state *internal_handle =
1334 		calloc(1, sizeof(struct rds_private_state));
1335 	internal_handle->handle.is_rbds = is_rbds;
1336 
1337 	return (struct v4l2_rds *)internal_handle;
1338 }
1339 
v4l2_rds_destroy(struct v4l2_rds * handle)1340 void v4l2_rds_destroy(struct v4l2_rds *handle)
1341 {
1342 	if (handle)
1343 		free(handle);
1344 }
1345 
v4l2_rds_reset(struct v4l2_rds * handle,bool reset_statistics)1346 void v4l2_rds_reset(struct v4l2_rds *handle, bool reset_statistics)
1347 {
1348 	/* treat the private & the public part of the handle */
1349 	struct rds_private_state *priv_state = (struct rds_private_state *) handle;
1350 
1351 	/* store members of handle that shouldn't be affected by reset */
1352 	bool is_rbds = handle->is_rbds;
1353 	struct v4l2_rds_statistics rds_statistics = handle->rds_statistics;
1354 
1355 	/* reset the handle */
1356 	memset(priv_state, 0, sizeof(*priv_state));
1357 	/* re-initialize members */
1358 	handle->is_rbds = is_rbds;
1359 	if (!reset_statistics)
1360 		handle->rds_statistics = rds_statistics;
1361 }
1362 
1363 /* function decodes raw RDS data blocks into complete groups. Once a full group is
1364  * successfully received, the group is decoded into the fields of the RDS handle.
1365  * Decoding is only done once a complete group was received. This is slower compared
1366  * to decoding the group type independent information up front, but adds a barrier
1367  * against corrupted data (happens regularly when reception is weak) */
v4l2_rds_add(struct v4l2_rds * handle,struct v4l2_rds_data * rds_data)1368 uint32_t v4l2_rds_add(struct v4l2_rds *handle, struct v4l2_rds_data *rds_data)
1369 {
1370 	struct rds_private_state *priv_state = (struct rds_private_state *) handle;
1371 	struct v4l2_rds_data *rds_data_raw = priv_state->rds_data_raw;
1372 	struct v4l2_rds_statistics *rds_stats = &handle->rds_statistics;
1373 	uint32_t updated_fields = 0;
1374 	uint8_t *decode_state = &(priv_state->decode_state);
1375 
1376 	/* get the block id by masking out irrelevant bits */
1377 	int block_id = rds_data->block & V4L2_RDS_BLOCK_MSK;
1378 
1379 	rds_stats->block_cnt++;
1380 	/* check for corrected / uncorrectable errors in the data */
1381 	if ((rds_data->block & V4L2_RDS_BLOCK_ERROR) ||
1382 	    block_id == V4L2_RDS_BLOCK_INVALID) {
1383 		block_id = -1;
1384 		rds_stats->block_error_cnt++;
1385 	} else if (rds_data->block & V4L2_RDS_BLOCK_CORRECTED) {
1386 		rds_stats->block_corrected_cnt++;
1387 	}
1388 
1389 	switch (*decode_state) {
1390 	case RDS_EMPTY:
1391 		if (block_id == 0) {
1392 			*decode_state = RDS_A_RECEIVED;
1393 			/* begin reception of a new data group, reset raw buffer to 0 */
1394 			memset(rds_data_raw, 0, sizeof(*rds_data_raw));
1395 			rds_data_raw[0] = *rds_data;
1396 		} else {
1397 			/* ignore block if it is not the first block of a group */
1398 			rds_stats->group_error_cnt++;
1399 		}
1400 		break;
1401 
1402 	case RDS_A_RECEIVED:
1403 		if (block_id == 1) {
1404 			*decode_state = RDS_B_RECEIVED;
1405 			rds_data_raw[1] = *rds_data;
1406 		} else {
1407 			/* received block with unexpected block id, reset state machine */
1408 			rds_stats->group_error_cnt++;
1409 			*decode_state = RDS_EMPTY;
1410 		}
1411 		break;
1412 
1413 	case RDS_B_RECEIVED:
1414 		/* handle type C and C' blocks alike */
1415 		if (block_id == 2 || block_id ==  4) {
1416 			*decode_state = RDS_C_RECEIVED;
1417 			rds_data_raw[2] = *rds_data;
1418 		} else {
1419 			rds_stats->group_error_cnt++;
1420 			*decode_state = RDS_EMPTY;
1421 		}
1422 		break;
1423 
1424 	case RDS_C_RECEIVED:
1425 		if (block_id == 3) {
1426 			*decode_state = RDS_EMPTY;
1427 			rds_data_raw[3] = *rds_data;
1428 			/* a full group was received */
1429 			rds_stats->group_cnt++;
1430 			/* decode group type independent fields */
1431 			memset(&priv_state->rds_group, 0, sizeof(priv_state->rds_group));
1432 			updated_fields |= rds_decode_a(priv_state, &rds_data_raw[0]);
1433 			updated_fields |= rds_decode_b(priv_state, &rds_data_raw[1]);
1434 			rds_decode_c(priv_state, &rds_data_raw[2]);
1435 			rds_decode_d(priv_state, &rds_data_raw[3]);
1436 			/* decode group type dependent fields */
1437 			updated_fields |= rds_decode_group(priv_state);
1438 			return updated_fields;
1439 		}
1440 		rds_stats->group_error_cnt++;
1441 		*decode_state = RDS_EMPTY;
1442 		break;
1443 
1444 	default:
1445 		/* every unexpected block leads to a reset of the sm */
1446 		rds_stats->group_error_cnt++;
1447 		*decode_state = RDS_EMPTY;
1448 	}
1449 	/* if we reach here, no RDS group was completed */
1450 	return 0;
1451 }
1452 
v4l2_rds_get_pty_str(const struct v4l2_rds * handle)1453 const char *v4l2_rds_get_pty_str(const struct v4l2_rds *handle)
1454 {
1455 	const uint8_t pty = handle->pty;
1456 
1457 	if (pty >= 32)
1458 		return NULL;
1459 
1460 	static const char *rds_lut[32] = {
1461 		"None", "News", "Affairs", "Info", "Sport", "Education", "Drama",
1462 		"Culture", "Science", "Varied Speech", "Pop Music",
1463 		"Rock Music", "Easy Listening", "Light Classics M",
1464 		"Serious Classics", "Other Music", "Weather", "Finance",
1465 		"Children", "Social Affairs", "Religion", "Phone In",
1466 		"Travel & Touring", "Leisure & Hobby", "Jazz Music",
1467 		"Country Music", "National Music", "Oldies Music", "Folk Music",
1468 		"Documentary", "Alarm Test", "Alarm!"
1469 	};
1470 	static const char *rbds_lut[32] = {
1471 		"None", "News", "Information", "Sports", "Talk", "Rock",
1472 		"Classic Rock", "Adult Hits", "Soft Rock", "Top 40", "Country",
1473 		"Oldies", "Soft", "Nostalgia", "Jazz", "Classical",
1474 		"R&B", "Soft R&B", "Foreign Language", "Religious Music",
1475 		"Religious Talk", "Personality", "Public", "College",
1476 		"Spanish Talk", "Spanish Music", "Hip-Hop", "Unassigned",
1477 		"Unassigned", "Weather", "Emergency Test", "Emergency"
1478 	};
1479 
1480 	return handle->is_rbds ? rbds_lut[pty] : rds_lut[pty];
1481 }
1482 
v4l2_rds_get_country_str(const struct v4l2_rds * handle)1483 const char *v4l2_rds_get_country_str(const struct v4l2_rds *handle)
1484 {
1485 	/* defines the  region of the world
1486 	 * 0x0e = Europe, 0x0d = Africa, 0x0a = ITU Region 2,
1487 	 * 0x0f = ITU Region 3 */
1488 	uint8_t ecc_h = handle->ecc >> 4;
1489 	/* sub identifier for the region, valid range 0..4 */
1490 	uint8_t ecc_l = handle->ecc & 0x0f;
1491 	/* bits 12-15 pi contain the country code */
1492 	uint8_t country_code = handle->pi >> 12;
1493 
1494 	/* LUT for European countries
1495 	 * the standard doesn't define every possible value but leaves some
1496 	 * undefined. An exception is e4-7 which is defined as a dash ("-") */
1497 	static const char *e_lut[5][16] = {
1498 	{
1499 		NULL, "DE", "DZ", "AD", "IL", "IT", "BE", "RU", "PS", "AL",
1500 		"AT", "HU", "MT", "DE", NULL, "EG"
1501 	}, {
1502 		NULL, "GR", "CY", "SM", "CH", "JO", "FI", "LU", "BG", "DK",
1503 		"GI", "IQ", "GB", "LY", "RO", "FR"
1504 	}, {
1505 		NULL, "MA", "CZ", "PL", "VA", "SK", "SY", "TN", NULL, "LI",
1506 		"IS", "MC", "LT", "RS", "ES", "NO"
1507 	}, {
1508 		NULL, "ME", "IE", "TR", "MK", NULL, NULL, NULL, "NL", "LV",
1509 		"LB", "AZ", "HR", "KZ", "SE", "BY"
1510 	}, {
1511 		NULL, "MD", "EE", "KG", NULL, NULL, "UA", "-", "PT", "SI",
1512 		"AM", NULL, "GE", NULL, NULL, "BA"
1513 	}
1514 	};
1515 
1516 	/* for now only European countries are supported -> ECC E0 - E4
1517 	 * but the standard defines country codes for the whole world,
1518 	 * that's the reason for returning "unknown" instead of a NULL
1519 	 * pointer until all defined countries are supported */
1520 	if (ecc_h == 0x0e && ecc_l <= 0x04)
1521 		return e_lut[ecc_l][country_code];
1522 	return "Unknown";
1523 }
1524 
rds_language_lut(const uint8_t lc)1525 static const char *rds_language_lut(const uint8_t lc)
1526 {
1527 	const uint8_t max_lc = 127;
1528 	const char *language;
1529 
1530 	static const char *language_lut[128] = {
1531 		"Unknown", "Albanian", "Breton", "Catalan",
1532 		"Croatian", "Welsh", "Czech", "Danish",
1533 		"German", "English", "Spanish", "Esperanto",
1534 		"Estonian", "Basque", "Faroese", "French",
1535 		"Frisian", "Irish", "Gaelic", "Galician",
1536 		"Icelandic", "Italian", "Lappish", "Latin",
1537 		"Latvian", "Luxembourgian", "Lithuanian", "Hungarian",
1538 		"Maltese", "Dutch", "Norwegian", "Occitan",
1539 		"Polish", "Portuguese", "Romanian", "Ramansh",
1540 		"Serbian", "Slovak", "Slovene", "Finnish",
1541 		"Swedish", "Turkish", "Flemish", "Walloon",
1542 		NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1543 		NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1544 		NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1545 		NULL, "Zulu", "Vietnamese", "Uzbek",
1546 		"Urdu", "Ukrainian", "Thai", "Telugu",
1547 		"Tatar", "Tamil", "Tadzhik", "Swahili",
1548 		"Sranan Tongo", "Somali", "Sinhalese", "Shona",
1549 		"Serbo-Croat", "Ruthenian", "Russian", "Quechua",
1550 		"Pushtu", "Punjabi", "Persian", "Papamiento",
1551 		"Oriya", "Nepali", "Ndebele", "Marathi",
1552 		"Moldavian", "Malaysian", "Malagasay", "Macedonian",
1553 		"Laotian", "Korean", "Khmer", "Kazahkh",
1554 		"Kannada", "Japanese", "Indonesian", "Hindi",
1555 		"Hebrew", "Hausa", "Gurani", "Gujurati",
1556 		"Greek", "Georgian", "Fulani", "Dani",
1557 		"Churash", "Chinese", "Burmese", "Bulgarian",
1558 		"Bengali", "Belorussian", "Bambora", "Azerbaijani",
1559 		"Assamese", "Armenian", "Arabic", "Amharic"
1560 	};
1561 
1562 	/* filter invalid values and undefined table entries */
1563 	language = (lc > max_lc) ? "Unknown" : language_lut[lc];
1564 	if (!language)
1565 		return "Unknown";
1566 	return language;
1567 }
1568 
v4l2_rds_get_language_str(const struct v4l2_rds * handle)1569 const char *v4l2_rds_get_language_str(const struct v4l2_rds *handle)
1570 {
1571 	return rds_language_lut(handle->lc);
1572 }
1573 
v4l2_rds_get_coverage_str(const struct v4l2_rds * handle)1574 const char *v4l2_rds_get_coverage_str(const struct v4l2_rds *handle)
1575 {
1576 	/* bits 8-11 contain the area coverage code */
1577 	uint8_t coverage = (handle->pi >> 8) & 0x0f;
1578 	uint8_t nibble = (handle->pi >> 12) & 0x0f;
1579 	static const char *coverage_lut[16] = {
1580 		"Local", "International", "National", "Supra-Regional",
1581 		"Regional 1", "Regional 2", "Regional 3", "Regional 4",
1582 		"Regional 5", "Regional 6", "Regional 7", "Regional 8",
1583 		"Regional 9", "Regional 10", "Regional 11", "Regional 12"
1584 	};
1585 
1586 	/*
1587 	 * Coverage area codes are restricted to the B, D and E PI code
1588 	 * blocks for RBDS.
1589 	 */
1590 	if (!handle->is_rbds ||
1591 	    (nibble == 0xb || nibble == 0xd || nibble == 0xe))
1592 		return coverage_lut[coverage];
1593 	return "Not Available";
1594 }
1595 
v4l2_rds_get_group(const struct v4l2_rds * handle)1596 const struct v4l2_rds_group *v4l2_rds_get_group(const struct v4l2_rds *handle)
1597 {
1598 	struct rds_private_state *priv_state = (struct rds_private_state *) handle;
1599 	return &priv_state->rds_group;
1600 }
1601