• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 /*
3  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
4  */
5 
6 #include <array>
7 #include <string>
8 
9 #include <sys/ioctl.h>
10 
11 #include "cec-follower.h"
12 #include "compiler.h"
13 
14 #define NUM_ANALOG_FREQS 3
15 #define NUM_DIGITAL_CHANS 3
16 #define TOT_ANALOG_FREQS analog_freqs_khz[0][0].size()
17 #define TOT_DIGITAL_CHANS digital_arib_data[0].size() + digital_atsc_data[0].size() + digital_dvb_data[0].size()
18 
19 enum Months { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
20 
21 struct service_info {
22 	unsigned tsid;
23 	unsigned onid;
24 	unsigned sid;
25 	unsigned fmt;
26 	unsigned major;
27 	unsigned minor;
28 };
29 
30 /*
31  * This table contains the digital television channels for ARIB (ISDB).  There
32  * are a total of three channels that are identified by digital IDs or by
33  * channel.
34  *
35  * CEC 17 of the 1.4 specification lists the available digital identification
36  * methods, IDs, and channel data.
37  *
38  * Digital channel data for ARIB-BS is from:
39  *
40  * https://sichbopvr.com/frequency-tables/19-20E
41  *
42  * No public data was found for ARIB-BS so data is just copied.
43  *
44  * Digital channel data for ARIB-T is from:
45  *
46  * https://sichbopvr.com/frequency-tables/Brazil/Rio%20de%20Janeiro/Rio%20De%20Janeiro
47  */
48 using arib = std::array<service_info, NUM_DIGITAL_CHANS>;
49 static constexpr std::array<arib, 2> digital_arib_data{
50 	// satellite, arib-bs
51 	arib{
52 		// tsid, onid, sid, fmt, major, minor
53 		service_info{ 1032, 1, 30203, 1, 0, 30203 },
54 		service_info{ 1046, 1, 30505, 1, 0, 30505 },
55 		service_info{ 1060, 1, 30609, 1, 0, 30609 },
56 	},
57 	// terrestrial, arib-t
58 	arib{
59 		// tsid, onid, sid, fmt, major, minor
60 		service_info{ 1519, 1519, 48608, 1, 0, 48608 },
61 		service_info{ 1624, 1624, 51992, 1, 0, 51992 },
62 		service_info{ 1905, 1905, 60960, 1, 0, 60960 },
63 	},
64 };
65 
66 /*
67  * This table contains the digital television channels for ATSC.  There
68  * are a total of three channels that are identified by digital IDs or by
69  * channel.
70  *
71  * CEC 17 of the 1.4 specification lists the available digital identification
72  * methods, IDs, and channel data.
73  *
74  * Digital channel data for atsc-sat is from:
75  *
76  * https://sichbopvr.com/frequency-tables/28-50E
77  *
78  * No public data was found for atsc-sat so data is just copied.
79  *
80  * Digital channel data for atsc-t is from:
81  *
82  * https://sichbopvr.com/frequency-tables/United%20States/Illinois/Caseyville
83  *
84  * ATSC does not use ONIDs and SID will be used as the program number.  All ATSC
85  * channel number formats are 2 part.
86  */
87 using atsc = std::array<service_info, NUM_DIGITAL_CHANS>;
88 static constexpr std::array<atsc, 2> digital_atsc_data{
89 	// satellite, atsc-sat
90 	atsc{
91 		// tsid, onid, sid, fmt, major, minor
92 		service_info{ 2065, 0, 50316, 2, 3, 50316 },
93 		service_info{ 2090, 0, 50882, 2, 3, 50882 },
94 		service_info{ 2122, 0, 55295, 2, 3, 55295 },
95 	},
96 	// terrestrial, atsc-t
97 	atsc{
98 		// tsid, onid, sid, fmt, major, minor
99 		service_info{ 1675, 0, 1, 2, 4, 1 },
100 		service_info{ 1675, 0, 2, 2, 4, 2 },
101 		service_info{ 1675, 0, 3, 2, 4, 3 },
102 	},
103 };
104 
105 /*
106  * This table contains the digital television channels for DVB.  There are a
107  * total of three channels that are identified by digital IDs or by channel.
108  *
109  * CEC 17 of the 1.4 specification lists the available digital identification
110  * methods, IDs, and channel data.
111  *
112  * Digital channel data for DVB-S2 is from:
113  *
114  * https://www.satellite-calculations.com/DVB/getchannellist.php?1west/Swedish_Nordig_Channel_List.htm
115  *
116  * Digital channel data for DVB-T is from:
117  *
118  * https://sichbopvr.com/frequency-tables/Denmark/Hovedstaden/Copenhagen
119  * https://sichbopvr.com/frequency-tables/Sweden/Skane%20Lan/Malm%c3%b6
120  *
121  */
122 using dvb = std::array<service_info, NUM_DIGITAL_CHANS>;
123 static constexpr std::array<dvb, 2> digital_dvb_data{
124 	// satellite, dvb-s2
125 	dvb{
126 		// tsid, onid, sid, fmt, major, minor
127 		service_info{ 61, 70, 7193, 1, 0, 24 },
128 		service_info{ 65, 70, 7040, 1, 0, 72 },
129 		service_info{ 28, 70, 7012, 1, 0, 101 },
130 	},
131 	// terrestrial, dvb-t
132 	dvb{
133 		// tsid, onid, sid, fmt, major, minor
134 		service_info{ 1002, 8400, 2025, 1, 0, 21 },
135 		service_info{ 1004, 8400, 84, 1, 0, 31 },
136 		service_info{ 1004, 8945, 1040, 1, 0, 1040 },
137 	},
138 };
139 
140 /*
141  * This table contains analog television channel frequencies in KHz.  There are
142  * a total of three frequencies per analog broadcast type and broadcast system.
143  *
144  * CEC 17 and CEC Table 31 of the 1.4 specification lists the available analog
145  * broadcast types and broadcast systems.
146  *
147  * The table is indexed by [ana_bcast_type][bcast_system][NUM_ANALOG_FREQS].
148  *
149  * Analog channel frequencies are from Wikipedia:
150  *
151  * https://en.wikipedia.org/wiki/Television_channel_frequencies
152  */
153 using khz = std::array<unsigned int, NUM_ANALOG_FREQS>;
154 using freqs = std::array<khz, 9>;
155 static constexpr std::array<freqs, 3> analog_freqs_khz{
156 	// cable
157 	freqs{
158 		// pal-bg
159 		khz{ 471250, 479250, 487250 },
160 		// secam-lq
161 		khz{ 615250, 623250, 631250 },
162 		// pal-m
163 		khz{ 501250, 507250, 513250 },
164 		// ntsc-m
165 		khz{ 519250, 525250, 531250 },
166 		// pal-i
167 		khz{ 45750, 53750, 61750 },
168 		// secam-dk
169 		khz{ 759250, 767250, 775250 },
170 		// secam-bg
171 		khz{ 495250, 503250, 511250 },
172 		// secam-l
173 		khz{ 639250, 647250, 655250 },
174 		// pal-dk
175 		khz{ 783250, 791250, 799250 },
176 	},
177 	// satellite
178 	freqs{
179 		// pal-bg
180 		khz{ 519250, 527250, 535250 },
181 		// secam-lq
182 		khz{ 663250, 671250, 679250 },
183 		// pal-m
184 		khz{ 537250, 543250, 549250 },
185 		// ntsc-m
186 		khz{ 555250, 561250, 567250 },
187 		// pal-i
188 		khz{ 175250, 183250, 191250 },
189 		// secam-dk
190 		khz{ 807250, 815250, 823250 },
191 		// secam-bg
192 		khz{ 543250, 551250, 559250 },
193 		// secam-l
194 		khz{ 687250, 695250, 703250 },
195 		// pal-dk
196 		khz{ 831250, 839250, 847250 },
197 	},
198 	// terrestrial
199 	freqs{
200 		// pal-bg
201 		khz{ 567250, 575250, 583250 },
202 		// secam-lq
203 		khz{ 711250, 719250, 727250 },
204 		// pal-m
205 		khz{ 573250, 579250, 585250 },
206 		// ntsc-m
207 		khz{ 591250, 597250, 603250 },
208 		// pal-i
209 		khz{ 199250, 207250, 215250 },
210 		// secam-dk
211 		khz{ 145250, 153250, 161250 },
212 		// secam-bg
213 		khz{ 591250, 599250, 607250 },
214 		// secam-l
215 		khz{ 735250, 743250, 751250 },
216 		// pal-dk
217 		khz{ 169250, 177250, 185250 },
218 	},
219 };
220 
tuner_dev_info_init(struct state * state)221 void tuner_dev_info_init(struct state *state)
222 {
223 	struct cec_op_tuner_device_info *info = &state->tuner_dev_info;
224 	struct cec_op_digital_service_id *digital = &info->digital;
225 
226 	state->service_idx = 0;
227 	info->rec_flag = CEC_OP_REC_FLAG_NOT_USED;
228 	info->tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_DIGITAL;
229 	info->is_analog = false;
230 	digital->service_id_method = state->service_by_dig_id ?
231 		CEC_OP_SERVICE_ID_METHOD_BY_DIG_ID :
232 		CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL;
233 	digital->dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_BS;
234 	if (state->service_by_dig_id) {
235 		digital->arib.transport_id = digital_arib_data[0][0].tsid;
236 		digital->arib.service_id = digital_arib_data[0][0].sid;
237 		digital->arib.orig_network_id = digital_arib_data[0][0].onid;
238 	} else {
239 		digital->channel.channel_number_fmt = digital_arib_data[0][0].fmt;
240 		digital->channel.major = digital_arib_data[0][0].major;
241 		digital->channel.minor = digital_arib_data[0][0].minor;
242 	}
243 }
244 
digital_get_service_offset(const struct service_info * info,const struct cec_op_digital_service_id * digital)245 static int digital_get_service_offset(const struct service_info *info,
246 				      const struct cec_op_digital_service_id *digital)
247 {
248 	__u8 method = digital->service_id_method;
249 	const struct cec_op_arib_data *arib = &digital->arib;
250 	const struct cec_op_atsc_data *atsc = &digital->atsc;
251 	const struct cec_op_dvb_data *dvb = &digital->dvb;
252 	const struct cec_op_channel_data *channel = &digital->channel;
253 
254 	for (int i = 0; i < NUM_DIGITAL_CHANS; i++, info++) {
255 		switch (method) {
256 		case CEC_OP_SERVICE_ID_METHOD_BY_DIG_ID:
257 			switch (digital->dig_bcast_system) {
258 			case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_BS:
259 			case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_T:
260 				if (arib->transport_id == info->tsid &&
261 				    arib->service_id == info->sid &&
262 				    arib->orig_network_id == info->onid)
263 					return i;
264 				break;
265 			case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_SAT:
266 			case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_T:
267 				if (atsc->transport_id == info->tsid &&
268 				    atsc->program_number == info->sid)
269 					return i;
270 				break;
271 			case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_S2:
272 			case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_T:
273 				if (dvb->transport_id == info->tsid &&
274 				    dvb->service_id == info->sid &&
275 				    dvb->orig_network_id == info->onid)
276 					return i;
277 				break;
278 			}
279 			break;
280 		case CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL:
281 			switch (channel->channel_number_fmt) {
282 			case CEC_OP_CHANNEL_NUMBER_FMT_1_PART:
283 				if (channel->minor == info->minor)
284 					return i;
285 				break;
286 			case CEC_OP_CHANNEL_NUMBER_FMT_2_PART:
287 				if (channel->major == info->major &&
288 				    channel->minor == info->minor)
289 					return i;
290 				break;
291 			}
292 			break;
293 		default:
294 			break;
295 		}
296 	}
297 	return -1;
298 }
299 
digital_get_service_idx(struct cec_op_digital_service_id * digital)300 static int digital_get_service_idx(struct cec_op_digital_service_id *digital)
301 {
302 	const struct service_info *info;
303 	bool is_terrestrial = false;
304 	unsigned offset;
305 	int idx;
306 
307 	switch (digital->dig_bcast_system) {
308 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_T:
309 		is_terrestrial = true;
310 		fallthrough;
311 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_BS:
312 		info = &digital_arib_data[is_terrestrial][0];
313 		offset = is_terrestrial * NUM_DIGITAL_CHANS;
314 		break;
315 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_T:
316 		is_terrestrial = true;
317 		fallthrough;
318 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_SAT:
319 		info = &digital_atsc_data[is_terrestrial][0];
320 		offset = (2 + is_terrestrial) * NUM_DIGITAL_CHANS;
321 		break;
322 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_T:
323 		is_terrestrial = true;
324 		fallthrough;
325 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_S2:
326 		info = &digital_dvb_data[is_terrestrial][0];
327 		offset = (4 + is_terrestrial) * NUM_DIGITAL_CHANS;
328 		break;
329 	default:
330 		return -1;
331 	}
332 	idx = digital_get_service_offset(info, digital);
333 	return idx >= 0 ? idx + offset : -1;
334 }
335 
digital_update_tuner_dev_info(struct node * node,int idx,struct cec_msg * msg)336 static bool digital_update_tuner_dev_info(struct node *node, int idx,
337 					  struct cec_msg *msg)
338 {
339 	if (idx < 0)
340 		return false;
341 
342 	struct cec_op_tuner_device_info *info = &node->state.tuner_dev_info;
343 	struct cec_op_digital_service_id *digital = &info->digital;
344 	struct cec_op_arib_data *arib = &digital->arib;
345 	struct cec_op_dvb_data *dvb = &digital->dvb;
346 	struct cec_op_atsc_data *atsc = &digital->atsc;
347 	struct cec_op_channel_data *channel = &digital->channel;
348 	unsigned int tbl = idx / (NUM_DIGITAL_CHANS * 2);
349 	unsigned int sys = (idx % (NUM_DIGITAL_CHANS * 2)) / NUM_DIGITAL_CHANS;
350 	unsigned int offset = idx % NUM_DIGITAL_CHANS;
351 
352 	info->tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_DIGITAL;
353 	info->is_analog = false;
354 	digital->service_id_method = node->state.service_by_dig_id ?
355 		CEC_OP_SERVICE_ID_METHOD_BY_DIG_ID :
356 		CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL;
357 
358 	switch (tbl) {
359 	case 0: {
360 		if (sys)
361 			digital->dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_T;
362 		else
363 			digital->dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_BS;
364 		if (node->state.service_by_dig_id) {
365 			arib->transport_id = digital_arib_data[sys][offset].tsid;
366 			arib->orig_network_id = digital_arib_data[sys][offset].onid;
367 			arib->service_id = digital_arib_data[sys][offset].sid;
368 		} else {
369 			channel->channel_number_fmt = digital_arib_data[sys][offset].fmt;
370 			channel->major = digital_arib_data[sys][offset].major;
371 			channel->minor = digital_arib_data[sys][offset].minor;
372 		}
373 		break;
374 	}
375 	case 1: {
376 		if (sys)
377 			digital->dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_T;
378 		else
379 			digital->dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_SAT;
380 		if (node->state.service_by_dig_id) {
381 			atsc->transport_id = digital_atsc_data[sys][offset].tsid;
382 			atsc->program_number = digital_atsc_data[sys][offset].sid;
383 		} else {
384 			channel->channel_number_fmt = digital_atsc_data[sys][offset].fmt;
385 			channel->major = digital_atsc_data[sys][offset].major;
386 			channel->minor = digital_atsc_data[sys][offset].minor;
387 		}
388 		break;
389 	}
390 	case 2: {
391 		if (sys)
392 			digital->dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_T;
393 		else
394 			digital->dig_bcast_system = CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_S2;
395 		if (node->state.service_by_dig_id) {
396 			dvb->transport_id = digital_dvb_data[sys][offset].tsid;
397 			dvb->orig_network_id = digital_dvb_data[sys][offset].onid;
398 			dvb->service_id = digital_dvb_data[sys][offset].sid;
399 		} else {
400 			channel->channel_number_fmt = digital_dvb_data[sys][offset].fmt;
401 			channel->major = digital_dvb_data[sys][offset].major;
402 			channel->minor = digital_dvb_data[sys][offset].minor;
403 		}
404 		break;
405 	}
406 	default:
407 		break;
408 	}
409 	if (node->state.service_idx != static_cast<unsigned>(idx) && node->state.tuner_report_changes) {
410 		cec_msg_set_reply_to(msg, msg);
411 		cec_msg_tuner_device_status(msg, &node->state.tuner_dev_info);
412 		transmit(node, msg);
413 	}
414 	node->state.service_idx = idx;
415 	return true;
416 }
417 
digital_set_tuner_dev_info(struct node * node,struct cec_msg * msg)418 static bool digital_set_tuner_dev_info(struct node *node, struct cec_msg *msg)
419 {
420 	struct cec_op_digital_service_id digital = {};
421 
422 	cec_ops_select_digital_service(msg, &digital);
423 	return digital_update_tuner_dev_info(node, digital_get_service_idx(&digital), msg);
424 }
425 
analog_get_nearest_service_idx(__u8 ana_bcast_type,__u8 ana_bcast_system,int ana_freq_khz)426 static unsigned int analog_get_nearest_service_idx(__u8 ana_bcast_type, __u8 ana_bcast_system,
427 						   int ana_freq_khz)
428 {
429 	int nearest = analog_freqs_khz[ana_bcast_type][ana_bcast_system][0];
430 	unsigned int offset = 0;
431 
432 	for (int i = 0; i < NUM_ANALOG_FREQS; i++) {
433 		int freq = analog_freqs_khz[ana_bcast_type][ana_bcast_system][i];
434 
435 		if (abs(ana_freq_khz - freq) < abs(ana_freq_khz - nearest)) {
436 			nearest = freq;
437 			offset = i;
438 		}
439 	}
440 	return NUM_ANALOG_FREQS * ((ana_bcast_type * 9) + ana_bcast_system) +
441 		offset + TOT_DIGITAL_CHANS;
442 }
443 
analog_update_tuner_dev_info(struct node * node,unsigned int idx,struct cec_msg * msg)444 static void analog_update_tuner_dev_info(struct node *node, unsigned int idx,
445 					 struct cec_msg *msg)
446 {
447 	struct cec_op_tuner_device_info *info = &node->state.tuner_dev_info;
448 	unsigned int sys_freqs = NUM_ANALOG_FREQS * 9;
449 	unsigned int offset;
450 	unsigned int freq_khz;
451 
452 	info->tuner_display_info = CEC_OP_TUNER_DISPLAY_INFO_ANALOGUE;
453 	info->is_analog = true;
454 	info->analog.ana_bcast_type = (idx - TOT_DIGITAL_CHANS) / sys_freqs;
455 	info->analog.bcast_system =
456 		(idx - (sys_freqs * info->analog.ana_bcast_type + TOT_DIGITAL_CHANS)) / NUM_ANALOG_FREQS;
457 	offset = idx % NUM_ANALOG_FREQS;
458 	freq_khz = analog_freqs_khz[info->analog.ana_bcast_type][info->analog.bcast_system][offset];
459 	info->analog.ana_freq = (freq_khz * 10) / 625;
460 	if (node->state.service_idx != idx && node->state.tuner_report_changes) {
461 		cec_msg_set_reply_to(msg, msg);
462 		cec_msg_tuner_device_status(msg, &node->state.tuner_dev_info);
463 		transmit(node, msg);
464 	}
465 	node->state.service_idx = idx;
466 }
467 
analog_set_tuner_dev_info(struct node * node,struct cec_msg * msg)468 static bool analog_set_tuner_dev_info(struct node *node, struct cec_msg *msg)
469 {
470 	__u8 type;
471 	__u16 freq;
472 	__u8 system;
473 	unsigned int idx;
474 
475 	cec_ops_select_analogue_service(msg, &type, &freq, &system);
476 	if (type < 3 && system < 9) {
477 		int freq_khz = (freq * 625) / 10;
478 
479 		idx = analog_get_nearest_service_idx(type, system, freq_khz);
480 		analog_update_tuner_dev_info(node, idx, msg);
481 		return true;
482 	}
483 	return false;
484 }
485 
digital_operand_invalid(const struct cec_op_record_src & rec_src)486 static bool digital_operand_invalid(const struct cec_op_record_src &rec_src)
487 {
488 	switch (rec_src.digital.dig_bcast_system) {
489 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_GEN:
490 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_GEN:
491 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_GEN:
492 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_BS:
493 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_CS:
494 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_T:
495 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_CABLE:
496 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_SAT:
497 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_T:
498 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_C:
499 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_S:
500 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_S2:
501 	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_T:
502 		break;
503 	default:
504 		return true;
505 	}
506 
507 	if (rec_src.digital.service_id_method == CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL) {
508 		if (rec_src.digital.channel.channel_number_fmt < CEC_OP_CHANNEL_NUMBER_FMT_1_PART ||
509 		    rec_src.digital.channel.channel_number_fmt > CEC_OP_CHANNEL_NUMBER_FMT_2_PART)
510 		    return true;
511 	}
512 
513 	return false;
514 }
515 
analog_operand_invalid(const cec_op_record_src & rec_src)516 static bool analog_operand_invalid(const cec_op_record_src &rec_src)
517 {
518 	if (rec_src.analog.ana_bcast_type > CEC_OP_ANA_BCAST_TYPE_TERRESTRIAL)
519 		return true;
520 
521 	if (rec_src.analog.bcast_system > CEC_OP_BCAST_SYSTEM_PAL_DK &&
522 	    rec_src.analog.bcast_system != CEC_OP_BCAST_SYSTEM_OTHER)
523 		return true;
524 
525 	if (rec_src.analog.ana_freq == 0 || rec_src.analog.ana_freq == 0xffff)
526 		return true;
527 
528 	return false;
529 }
530 
analog_channel_is_available(const cec_op_record_src & rec_src)531 static bool analog_channel_is_available(const cec_op_record_src &rec_src)
532 {
533 	__u8 bcast_type = rec_src.analog.ana_bcast_type;
534 	unsigned freq = (rec_src.analog.ana_freq * 625) / 10;
535 	__u8 bcast_system = rec_src.analog.bcast_system;
536 
537 	for (unsigned i = 0; i < NUM_ANALOG_FREQS; i++) {
538 		if (freq == analog_freqs_khz[bcast_type][bcast_system][i])
539 			return true;
540 	}
541 
542 	return false;
543 }
544 
process_tuner_msgs(struct node * node,struct cec_msg & msg,unsigned me,__u8 type)545 void process_tuner_msgs(struct node *node, struct cec_msg &msg, unsigned me, __u8 type)
546 {
547 	bool is_bcast = cec_msg_is_broadcast(&msg);
548 
549 	/* Tuner Control */
550 	switch (msg.msg[1]) {
551 	case CEC_MSG_GIVE_TUNER_DEVICE_STATUS: {
552 		__u8 status_req;
553 
554 		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
555 			break;
556 
557 		cec_ops_give_tuner_device_status(&msg, &status_req);
558 		if (status_req < CEC_OP_STATUS_REQ_ON ||
559 		    status_req > CEC_OP_STATUS_REQ_ONCE) {
560 			reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
561 			return;
562 		}
563 		if (status_req != CEC_OP_STATUS_REQ_ONCE)
564 			node->state.tuner_report_changes =
565 				status_req == CEC_OP_STATUS_REQ_ON;
566 		if (status_req == CEC_OP_STATUS_REQ_OFF)
567 			return;
568 
569 		cec_msg_set_reply_to(&msg, &msg);
570 		cec_msg_tuner_device_status(&msg, &node->state.tuner_dev_info);
571 		transmit(node, &msg);
572 		return;
573 	}
574 
575 	case CEC_MSG_TUNER_DEVICE_STATUS:
576 		return;
577 
578 	case CEC_MSG_SELECT_ANALOGUE_SERVICE:
579 		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
580 			break;
581 
582 		if (node->state.tuner_dev_info.rec_flag == CEC_OP_REC_FLAG_USED) {
583 			reply_feature_abort(node, &msg, CEC_OP_ABORT_REFUSED);
584 			return;
585 		}
586 		if (!analog_set_tuner_dev_info(node, &msg)) {
587 			reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
588 			return;
589 		}
590 		return;
591 
592 	case CEC_MSG_SELECT_DIGITAL_SERVICE:
593 		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
594 			break;
595 
596 		if (node->state.tuner_dev_info.rec_flag == CEC_OP_REC_FLAG_USED) {
597 			reply_feature_abort(node, &msg, CEC_OP_ABORT_REFUSED);
598 			return;
599 		}
600 		if (!digital_set_tuner_dev_info(node, &msg)) {
601 			reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
602 			return;
603 		}
604 		return;
605 
606 	case CEC_MSG_TUNER_STEP_DECREMENT: {
607 		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
608 			break;
609 
610 		if (node->state.service_idx == 0)
611 			node->state.service_idx =
612 				TOT_DIGITAL_CHANS + TOT_ANALOG_FREQS - 1;
613 		else
614 			node->state.service_idx--;
615 		if (node->state.service_idx < TOT_DIGITAL_CHANS)
616 			digital_update_tuner_dev_info(node, node->state.service_idx, &msg);
617 		else
618 			analog_update_tuner_dev_info(node, node->state.service_idx, &msg);
619 		return;
620 	}
621 
622 	case CEC_MSG_TUNER_STEP_INCREMENT: {
623 		if (!cec_has_tuner(1 << me) && !cec_has_tv(1 << me))
624 			break;
625 
626 		if (node->state.service_idx ==
627 				TOT_DIGITAL_CHANS + TOT_ANALOG_FREQS - 1)
628 			node->state.service_idx = 0;
629 		else
630 			node->state.service_idx++;
631 		if (node->state.service_idx < TOT_DIGITAL_CHANS)
632 			digital_update_tuner_dev_info(node, node->state.service_idx, &msg);
633 		else
634 			analog_update_tuner_dev_info(node, node->state.service_idx, &msg);
635 		return;
636 	}
637 	default:
638 		break;
639 	}
640 
641 	if (is_bcast)
642 		return;
643 
644 	reply_feature_abort(node, &msg);
645 }
646 
process_record_msgs(struct node * node,struct cec_msg & msg,unsigned me,__u8 type)647 void process_record_msgs(struct node *node, struct cec_msg &msg, unsigned me, __u8 type)
648 {
649 	__u8 from = cec_msg_initiator(&msg);
650 	bool is_bcast = cec_msg_is_broadcast(&msg);
651 
652 	/* One Touch Record */
653 	switch (msg.msg[1]) {
654 	case CEC_MSG_RECORD_TV_SCREEN: {
655 		if (!node->has_rec_tv)
656 			break;
657 
658 		/* Ignore if initiator is not a recording device */
659 		if (!cec_has_record(1 << from) && node->remote_prim_devtype[from] != CEC_OP_PRIM_DEVTYPE_RECORD)
660 			return;
661 
662 		struct cec_op_record_src rec_src = {};
663 
664 		rec_src.type = CEC_OP_RECORD_SRC_OWN;
665 		cec_msg_set_reply_to(&msg, &msg);
666 		cec_msg_record_on(&msg, false, &rec_src);
667 		transmit(node, &msg);
668 		return;
669 	}
670 	case CEC_MSG_RECORD_ON: {
671 		if (type != CEC_LOG_ADDR_TYPE_RECORD)
672 			break;
673 
674 		__u8 rec_status;
675 		bool feature_abort = false;
676 		struct cec_op_record_src rec_src = {};
677 
678 		cec_ops_record_on(&msg, &rec_src);
679 		switch (rec_src.type) {
680 		case CEC_OP_RECORD_SRC_OWN:
681 			rec_status = CEC_OP_RECORD_STATUS_CUR_SRC;
682 			break;
683 		case CEC_OP_RECORD_SRC_DIGITAL:
684 			if (digital_operand_invalid(rec_src)) {
685 				feature_abort = true;
686 				break;
687 			}
688 			if (digital_get_service_idx(&rec_src.digital) >= 0)
689 				rec_status = CEC_OP_RECORD_STATUS_DIG_SERVICE;
690 			else
691 				rec_status = CEC_OP_RECORD_STATUS_NO_DIG_SERVICE;
692 			break;
693 		case CEC_OP_RECORD_SRC_ANALOG:
694 			if (analog_operand_invalid(rec_src)) {
695 				feature_abort = true;
696 				break;
697 			}
698 			if (analog_channel_is_available(rec_src))
699 				rec_status = CEC_OP_RECORD_STATUS_ANA_SERVICE;
700 			else
701 				rec_status = CEC_OP_RECORD_STATUS_NO_ANA_SERVICE;
702 			break;
703 		case CEC_OP_RECORD_SRC_EXT_PLUG:
704 			if (rec_src.ext_plug.plug == 0)
705 				feature_abort = true;
706 			/* Plug number range is 1-255 in spec, but a realistic range of connectors is 6. */
707 			else if (rec_src.ext_plug.plug > 6)
708 				rec_status = CEC_OP_RECORD_STATUS_INVALID_EXT_PLUG;
709 			else
710 				rec_status = CEC_OP_RECORD_STATUS_EXT_INPUT;
711 			break;
712 		case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
713 			rec_status = CEC_OP_RECORD_STATUS_INVALID_EXT_PHYS_ADDR;
714 			break;
715 		default:
716 			feature_abort = true;
717 			break;
718 		}
719 		if (feature_abort) {
720 			reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
721 			return;
722 		}
723 		if (node->state.one_touch_record_on)
724 			rec_status = CEC_OP_RECORD_STATUS_ALREADY_RECORDING;
725 		cec_msg_set_reply_to(&msg, &msg);
726 		cec_msg_record_status(&msg, rec_status);
727 		transmit(node, &msg);
728 		node->state.one_touch_record_on = true;
729 		return;
730 	}
731 	case CEC_MSG_RECORD_OFF:
732 		if (type != CEC_LOG_ADDR_TYPE_RECORD)
733 			break;
734 
735 		cec_msg_set_reply_to(&msg, &msg);
736 		cec_msg_record_status(&msg, CEC_OP_RECORD_STATUS_TERMINATED_OK);
737 		transmit(node, &msg);
738 		node->state.one_touch_record_on = false;
739 
740 		/* Delete any currently active recording timer or it may restart itself in first minute. */
741 		if (node->state.recording_controlled_by_timer) {
742 			node->state.recording_controlled_by_timer = false;
743 			programmed_timers.erase(programmed_timers.begin());
744 			if (show_info)
745 				printf("Deleted manually stopped timer.\n");
746 			print_timers(node);
747 		}
748 		/*
749 		 * If standby was received during recording, enter standby when the
750 		 * recording is finished unless recording device is the active source.
751 		 */
752 		if (node->state.record_received_standby) {
753 			if (node->phys_addr != node->state.active_source_pa)
754 				enter_standby(node);
755 			node->state.record_received_standby = false;
756 		}
757 		return;
758 	case CEC_MSG_RECORD_STATUS:
759 		return;
760 	default:
761 		break;
762 	}
763 
764 	if (is_bcast)
765 		return;
766 
767 	reply_feature_abort(node, &msg);
768 }
769 
get_timer_from_message(const struct cec_msg & msg)770 static struct Timer get_timer_from_message(const struct cec_msg &msg)
771 {
772 	struct Timer timer = {};
773 
774 	__u8 day = 0;
775 	__u8 month = 0;
776 	__u8 start_hr = 0;
777 	__u8 start_min = 0;
778 	__u8 duration_hr = 0;
779 	__u8 duration_min = 0;
780 	__u8 ext_src_spec = 0;
781 	__u8 plug = 0;
782 	__u16 phys_addr = 0;
783 
784 	switch (msg.msg[1]) {
785 	case CEC_MSG_CLEAR_ANALOGUE_TIMER:
786 	case CEC_MSG_SET_ANALOGUE_TIMER:
787 		timer.src.type = CEC_OP_RECORD_SRC_ANALOG;
788 		cec_ops_set_analogue_timer(&msg, &day, &month, &start_hr, &start_min,
789 		                           &duration_hr, &duration_min, &timer.recording_seq,
790 		                           &timer.src.analog.ana_bcast_type, &timer.src.analog.ana_freq,
791 		                           &timer.src.analog.bcast_system);
792 		break;
793 	case CEC_MSG_CLEAR_DIGITAL_TIMER:
794 	case CEC_MSG_SET_DIGITAL_TIMER: {
795 		struct cec_op_digital_service_id digital = {};
796 		timer.src.type = CEC_OP_RECORD_SRC_DIGITAL;
797 		timer.src.digital = digital;
798 		cec_ops_set_digital_timer(&msg, &day, &month, &start_hr, &start_min,
799 		                          &duration_hr, &duration_min, &timer.recording_seq,
800 		                          &timer.src.digital);
801 		break;
802 	}
803 	case CEC_MSG_CLEAR_EXT_TIMER:
804 	case CEC_MSG_SET_EXT_TIMER: {
805 		cec_ops_set_ext_timer(&msg, &day, &month, &start_hr, &start_min,
806 		                      &duration_hr, &duration_min, &timer.recording_seq, &ext_src_spec,
807 		                      &plug, &phys_addr);
808 		if (ext_src_spec == CEC_OP_EXT_SRC_PLUG) {
809 			timer.src.type = CEC_OP_RECORD_SRC_EXT_PLUG;
810 			timer.src.ext_plug.plug = plug;
811 		}
812 		if (ext_src_spec == CEC_OP_EXT_SRC_PHYS_ADDR) {
813 			timer.src.type = CEC_OP_RECORD_SRC_EXT_PHYS_ADDR;
814 			timer.src.ext_phys_addr.phys_addr = phys_addr;
815 		}
816 		break;
817 	}
818 	default:
819 		break;
820 	}
821 
822 	timer.duration = ((duration_hr * 60) + duration_min) * 60; /* In seconds. */
823 
824 	/* Use current time in the timer when it is not available from message e.g. year. */
825 	time_t current_time = time(nullptr);
826 	struct tm *temp = localtime(&current_time);
827 	temp->tm_mday = day;
828 	temp->tm_mon = month - 1; /* CEC months are 1-12 but struct tm range is 0-11. */
829 	temp->tm_hour = start_hr;
830 	temp->tm_min = start_min;
831 	/*
832 	 * Timer precision is only to the minute. Set sec to 0 so that differences in seconds
833 	 * do not affect timer comparisons.
834 	 */
835 	temp->tm_sec = 0;
836 	temp->tm_isdst = -1;
837 	timer.start_time = mktime(temp);
838 
839 	return timer;
840 }
841 
timer_date_out_of_range(const struct cec_msg & msg,const struct Timer & timer)842 static bool timer_date_out_of_range(const struct cec_msg &msg, const struct Timer &timer)
843 {
844 	__u8 day = msg.msg[2];
845 	__u8 month = msg.msg[3];
846 	/* Hours and minutes are in BCD format */
847 	__u8 start_hr = (msg.msg[4] >> 4) * 10 + (msg.msg[4] & 0xf);
848 	__u8 start_min = (msg.msg[5] >> 4) * 10 + (msg.msg[5] & 0xf);
849 	__u8 duration_hr = (msg.msg[6] >> 4) * 10 + (msg.msg[6] & 0xf);
850 	__u8 duration_min = (msg.msg[7] >> 4) * 10 + (msg.msg[7] & 0xf);
851 
852 	if (start_min > 59 || start_hr > 23 || month > 12 || month == 0 || day > 31 || day == 0 ||
853 	    duration_min > 59 || (duration_hr == 0 && duration_min == 0))
854 		return true;
855 
856 	switch (month) {
857 	case Apr: case Jun: case Sep: case Nov:
858 		if (day > 30)
859 			return true;
860 		break;
861 	case Feb: {
862 		struct tm *tp = localtime(&timer.start_time);
863 
864 		if (!(tp->tm_year % 4) && ((tp->tm_year % 100) || !(tp->tm_year % 400))) {
865 			if (day > 29)
866 				return true;
867 		} else {
868 			if (day > 28)
869 				return true;
870 		}
871 		break;
872 	}
873 	default:
874 		break;
875 	}
876 
877 	return false;
878 }
879 
timer_overlap(const struct Timer & new_timer)880 static bool timer_overlap(const struct Timer &new_timer)
881 {
882 	if (programmed_timers.size() == 1)
883 		return false;
884 
885 	time_t new_timer_end = new_timer.start_time + new_timer.duration;
886 	for (auto &t : programmed_timers) {
887 
888 		if (new_timer == t)
889 			continue; /* Timer doesn't overlap itself. */
890 
891 		time_t existing_timer_end = t.start_time + t.duration;
892 
893 		if ((t.start_time < new_timer.start_time && new_timer.start_time < existing_timer_end) ||
894 		    (t.start_time < new_timer_end && new_timer_end < existing_timer_end) ||
895 		    (t.start_time == new_timer.start_time || existing_timer_end == new_timer_end) ||
896 		    (new_timer.start_time < t.start_time && existing_timer_end < new_timer_end))
897 		    return true;
898 	}
899 
900 	return false;
901 }
902 
process_timer_msgs(struct node * node,struct cec_msg & msg,unsigned me,__u8 type)903 void process_timer_msgs(struct node *node, struct cec_msg &msg, unsigned me, __u8 type)
904 {
905 	bool is_bcast = cec_msg_is_broadcast(&msg);
906 
907 	/* Timer Programming */
908 	switch (msg.msg[1]) {
909 	case CEC_MSG_SET_ANALOGUE_TIMER:
910 	case CEC_MSG_SET_DIGITAL_TIMER:
911 	case CEC_MSG_SET_EXT_TIMER: {
912 		if (type != CEC_LOG_ADDR_TYPE_RECORD)
913 			break;
914 
915 		__u8 prog_error = 0;
916 		__u8 prog_info = 0;
917 		__u8 timer_overlap_warning = CEC_OP_TIMER_OVERLAP_WARNING_NO_OVERLAP;
918 		__u8 available_space_hr = 0;
919 		__u8 available_space_min = 0;
920 		struct Timer timer = get_timer_from_message(msg);
921 
922 		/* If timer starts in the past, increment the year so that timers can be set across year-end. */
923 		if (time(nullptr) > timer.start_time) {
924 			struct tm *temp = localtime(&timer.start_time);
925 			temp->tm_year++;
926 			temp->tm_isdst = -1;
927 			timer.start_time = mktime(temp);
928 		}
929 
930 		if (timer_date_out_of_range(msg, timer))
931 			prog_error = CEC_OP_PROG_ERROR_DATE_OUT_OF_RANGE;
932 
933 		if (timer.recording_seq > 0x7f)
934 			prog_error = CEC_OP_PROG_ERROR_REC_SEQ_ERROR;
935 
936 		if (programmed_timers.find(timer) != programmed_timers.end())
937 			prog_error = CEC_OP_PROG_ERROR_DUPLICATE;
938 
939 		if (!prog_error) {
940 			programmed_timers.insert(timer);
941 
942 			if (timer_overlap(timer))
943 				timer_overlap_warning = CEC_OP_TIMER_OVERLAP_WARNING_OVERLAP;
944 
945 			if (node->state.media_space_available <= 0 ||
946 			    timer.duration > node->state.media_space_available) {
947 				prog_info = CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE;
948 			} else {
949 				int space_that_may_be_needed = 0;
950 				for (auto &t : programmed_timers) {
951 					space_that_may_be_needed += t.duration;
952 					if (t == timer) /* Only count the space up to and including the new timer. */
953 						break;
954 				}
955 				if ((node->state.media_space_available - space_that_may_be_needed) >= 0)
956 					prog_info = CEC_OP_PROG_INFO_ENOUGH_SPACE;
957 				else
958 					prog_info = CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE;
959 			}
960 			print_timers(node);
961 		}
962 
963 		if (prog_info == CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE ||
964 		    prog_info == CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE ||
965 		    prog_error == CEC_OP_PROG_ERROR_DUPLICATE) {
966 			available_space_hr = node->state.media_space_available / 3600; /* 3600 MB/hour */
967 			available_space_min = (node->state.media_space_available % 3600) / 60; /* 60 MB/min */
968 		}
969 		cec_msg_set_reply_to(&msg, &msg);
970 		cec_msg_timer_status(&msg, timer_overlap_warning, CEC_OP_MEDIA_INFO_UNPROT_MEDIA,
971 		                     prog_info, prog_error, available_space_hr, available_space_min);
972 		transmit(node, &msg);
973 		return;
974 	}
975 	case CEC_MSG_CLEAR_ANALOGUE_TIMER:
976 	case CEC_MSG_CLEAR_DIGITAL_TIMER:
977 	case CEC_MSG_CLEAR_EXT_TIMER: {
978 		if (type != CEC_LOG_ADDR_TYPE_RECORD)
979 			break;
980 
981 		__u8 timer_cleared_status = CEC_OP_TIMER_CLR_STAT_NO_MATCHING;
982 
983 		/* Look for timer in the previous year which have persisted across year-end. */
984 		struct Timer timer_in_previous_year = get_timer_from_message(msg);
985 		struct tm *temp = localtime(&timer_in_previous_year.start_time);
986 		temp->tm_year--;
987 		temp->tm_isdst = -1;
988 		timer_in_previous_year.start_time = mktime(temp);
989 		auto it_previous_year = programmed_timers.find(timer_in_previous_year);
990 
991 		if (it_previous_year != programmed_timers.end()) {
992 			if (node->state.recording_controlled_by_timer && it_previous_year == programmed_timers.begin()) {
993 				timer_cleared_status = CEC_OP_TIMER_CLR_STAT_RECORDING;
994 				node->state.one_touch_record_on = false;
995 				node->state.recording_controlled_by_timer = false;
996 			} else {
997 				timer_cleared_status = CEC_OP_TIMER_CLR_STAT_CLEARED;
998 			}
999 			programmed_timers.erase(timer_in_previous_year);
1000 			print_timers(node);
1001 		}
1002 
1003 		/* Look for timer in the current year. */
1004 		struct Timer timer_in_current_year = get_timer_from_message(msg);
1005 		auto it_current_year = programmed_timers.find(timer_in_current_year);
1006 
1007 		if (it_current_year != programmed_timers.end()) {
1008 			if (node->state.recording_controlled_by_timer && it_current_year == programmed_timers.begin()) {
1009 				timer_cleared_status = CEC_OP_TIMER_CLR_STAT_RECORDING;
1010 				node->state.one_touch_record_on = false;
1011 				node->state.recording_controlled_by_timer = false;
1012 			} else {
1013 				/* Do not overwrite status if already set. */
1014 				if (timer_cleared_status == CEC_OP_TIMER_CLR_STAT_NO_MATCHING)
1015 					timer_cleared_status = CEC_OP_TIMER_CLR_STAT_CLEARED;
1016 			}
1017 			programmed_timers.erase(timer_in_current_year);
1018 			print_timers(node);
1019 		}
1020 
1021 		/* Look for timer in the next year. */
1022 		struct Timer timer_in_next_year = get_timer_from_message(msg);
1023 		temp = localtime(&timer_in_next_year.start_time);
1024 		temp->tm_year++;
1025 		temp->tm_isdst = -1;
1026 		timer_in_next_year.start_time = mktime(temp);
1027 		if (programmed_timers.find(timer_in_next_year) != programmed_timers.end()) {
1028 			/* Do not overwrite status if already set. */
1029 			if (timer_cleared_status == CEC_OP_TIMER_CLR_STAT_NO_MATCHING)
1030 				timer_cleared_status = CEC_OP_TIMER_CLR_STAT_CLEARED;
1031 			programmed_timers.erase(timer_in_next_year);
1032 			print_timers(node);
1033 		}
1034 		cec_msg_set_reply_to(&msg, &msg);
1035 		cec_msg_timer_cleared_status(&msg, timer_cleared_status);
1036 		transmit(node, &msg);
1037 		/*
1038 		 * If the cleared timer was recording, and standby was received during recording,
1039 		 * enter standby when the recording stops unless recording device is the active source.
1040 		 */
1041 		if (timer_cleared_status == CEC_OP_TIMER_CLR_STAT_RECORDING) {
1042 			if (node->state.record_received_standby) {
1043 				if (node->phys_addr != node->state.active_source_pa)
1044 					enter_standby(node);
1045 				node->state.record_received_standby = false;
1046 			}
1047 		}
1048 		return;
1049 	}
1050 	case CEC_MSG_SET_TIMER_PROGRAM_TITLE:
1051 		if (type != CEC_LOG_ADDR_TYPE_RECORD)
1052 			break;
1053 		return;
1054 	case CEC_MSG_TIMER_CLEARED_STATUS:
1055 	case CEC_MSG_TIMER_STATUS:
1056 		return;
1057 	default:
1058 		break;
1059 	}
1060 
1061 	if (is_bcast)
1062 		return;
1063 
1064 	reply_feature_abort(node, &msg);
1065 }
1066