Debian Codegraph @

utils/cec-follower/cec-processing.cpp

raw
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 <cerrno>
7 #include <ctime>
8 #include <string>
9
10 #include <sys/ioctl.h>
11 #include <sys/time.h>
12
13 #include "cec-follower.h"
14 #include "compiler.h"
15
16 #define VOLUME_MAX 0x64
17 #define VOLUME_MIN 0
18
19 /* States for the RC handling */
20 #define NOPRESS 0
21 #define PRESS 1
22 #define PRESS_HOLD 2
23
24 /* The follower safety timeout as defined in the spec */
25 #define FOLLOWER_SAFETY_TIMEOUT 450
26 #define MIN_INITIATOR_REP_TIME 200
27 #define MAX_INITIATOR_REP_TIME 500
28
29 /* Time between each polling message sent to a device */
30 #define POLL_PERIOD 15000
31
32 /* The maximum interval in nanoseconds between audio rate messages as defined in the spec */
33 #define MAX_AUD_RATE_MSG_INTERVAL_NS (2 * 1000000000ULL)
34
35 /* The maximum interval in nanoseconds to allow a deck to skip forward/reverse */
36 #define MAX_DECK_SKIP_NS (2 * 1000000000ULL)
37
38 struct cec_enum_values {
39 const char *type_name;
40 __u8 value;
41 };
42
43 struct la_info la_info[15];
44
45 static struct timespec start_monotonic;
46 static struct timeval start_timeofday;
47 static constexpr time_t time_to_transient = 1;
48 static constexpr time_t time_to_stable = 8;
49
50 static const char *get_ui_cmd_string(__u8 ui_cmd)
51 {
52 return cec_log_ui_cmd_string(ui_cmd) ? : "Unknown";
53 }
54
55 static std::string ts2s(__u64 ts, bool wallclock)
56 {
57 std::string s;
58 struct timeval sub;
59 struct timeval res;
60 __u64 diff;
61 char buf[64];
62 time_t t;
63
64 if (!wallclock) {
65 sprintf(buf, "%llu.%03llus", ts / 1000000000, (ts % 1000000000) / 1000000);
66 return buf;
67 }
68 diff = ts - start_monotonic.tv_sec * 1000000000ULL - start_monotonic.tv_nsec;
69 sub.tv_sec = diff / 1000000000ULL;
70 sub.tv_usec = (diff % 1000000000ULL) / 1000;
71 timeradd(&start_timeofday, &sub, &res);
72 t = res.tv_sec;
73 s = ctime(&t);
74 s = s.substr(0, s.length() - 6);
75 sprintf(buf, "%03llu", (__u64)res.tv_usec / 1000);
76 return s + "." + buf;
77 }
78
79 static void log_event(struct cec_event &ev, bool wallclock)
80 {
81 __u16 pa;
82
83 if (ev.flags & CEC_EVENT_FL_DROPPED_EVENTS)
84 printf("(Note: events were lost)\n");
85 if (ev.flags & CEC_EVENT_FL_INITIAL_STATE)
86 printf("Initial ");
87 switch (ev.event) {
88 case CEC_EVENT_STATE_CHANGE:
89 pa = ev.state_change.phys_addr;
90 printf("Event: State Change: PA: %x.%x.%x.%x, LA mask: 0x%04x\n",
91 cec_phys_addr_exp(pa),
92 ev.state_change.log_addr_mask);
93 break;
94 case CEC_EVENT_LOST_MSGS:
95 printf("Event: Lost Messages\n");
96 break;
97 case CEC_EVENT_PIN_HPD_LOW:
98 case CEC_EVENT_PIN_HPD_HIGH:
99 printf("Event: HPD Pin %s\n",
100 ev.event == CEC_EVENT_PIN_HPD_HIGH ? "High" : "Low");
101 break;
102 case CEC_EVENT_PIN_5V_LOW:
103 case CEC_EVENT_PIN_5V_HIGH:
104 printf("Event: 5V Pin %s\n",
105 ev.event == CEC_EVENT_PIN_5V_HIGH ? "High" : "Low");
106 break;
107 case CEC_EVENT_PIN_CEC_LOW:
108 case CEC_EVENT_PIN_CEC_HIGH:
109 printf("Event: CEC Pin %s\n",
110 ev.event == CEC_EVENT_PIN_CEC_HIGH ? "High" : "Low");
111 break;
112 default:
113 printf("Event: Unknown (0x%x)\n", ev.event);
114 break;
115 }
116 if (show_info)
117 printf("\tTimestamp: %s\n", ts2s(ev.ts, wallclock).c_str());
118 }
119
120 void reply_feature_abort(struct node *node, struct cec_msg *msg, __u8 reason)
121 {
122 unsigned la = cec_msg_initiator(msg);
123 __u8 opcode = cec_msg_opcode(msg);
124 __u64 ts_now = get_ts();
125
126 if (cec_msg_is_broadcast(msg) || cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
127 return;
128 if (reason == CEC_OP_ABORT_UNRECOGNIZED_OP) {
129 la_info[la].feature_aborted[opcode].count++;
130 if (la_info[la].feature_aborted[opcode].count == 2) {
131 /* If the Abort Reason was "Unrecognized opcode", the Initiator should not send
132 the same message to the same Follower again at that time to avoid saturating
133 the bus. */
134 warn("Received message %s from LA %d (%s) shortly after\n",
135 opcode2s(msg).c_str(), la, cec_la2s(la));
136 warn("replying Feature Abort [Unrecognized Opcode] to the same message.\n");
137 }
138 }
139 else if (la_info[la].feature_aborted[opcode].count) {
140 warn("Replying Feature Abort with abort reason different than [Unrecognized Opcode]\n");
141 warn("to message that has previously been replied Feature Abort to with [Unrecognized Opcode].\n");
142 }
143 else
144 la_info[la].feature_aborted[opcode].ts = ts_now;
145
146 cec_msg_reply_feature_abort(msg, reason);
147 transmit(node, msg);
148 }
149
150 static bool exit_standby(struct node *node)
151 {
152 /* Cancel any standby request that was pending. */
153 node->state.record_received_standby = false;
154
155 if (node->state.power_status == CEC_OP_POWER_STATUS_STANDBY ||
156 node->state.power_status == CEC_OP_POWER_STATUS_TO_STANDBY) {
157 node->state.old_power_status = node->state.power_status;
158 node->state.power_status = CEC_OP_POWER_STATUS_ON;
159 node->state.power_status_changed_time = time(nullptr);
160 dev_info("Changing state to on\n");
161 return true;
162 }
163 return false;
164 }
165
166 bool enter_standby(struct node *node)
167 {
168 if (node->state.power_status == CEC_OP_POWER_STATUS_ON ||
169 node->state.power_status == CEC_OP_POWER_STATUS_TO_ON) {
170 /*
171 * Standby should not interrupt a recording in progress, but
172 * remember to go to standby once the recording is finished.
173 */
174 if (node->state.one_touch_record_on) {
175 node->state.record_received_standby = true;
176 return false;
177 }
178 node->state.old_power_status = node->state.power_status;
179 node->state.power_status = CEC_OP_POWER_STATUS_STANDBY;
180 node->state.power_status_changed_time = time(nullptr);
181 node->state.deck_skip_start = 0;
182 node->state.record_received_standby = false;
183 dev_info("Changing state to standby\n");
184 return true;
185 }
186 return false;
187 }
188
189 static unsigned get_duration_ms(__u64 ts_a, __u64 ts_b)
190 {
191 return (ts_a - ts_b) / 1000000;
192 }
193
194 static void rc_press_hold_stop(const struct state *state)
195 {
196 unsigned mean_duration = state->rc_duration_sum / state->rc_press_hold_count;
197
198 dev_info("Stop Press and Hold. Mean duration between User Control Pressed messages: %dms\n",
199 mean_duration);
200 if (mean_duration < MIN_INITIATOR_REP_TIME) {
201 warn("The mean duration between User Control Pressed messages is lower\n");
202 warn("than the Minimum Initiator Repetition Time (200ms).\n");
203 }
204 if (mean_duration > MAX_INITIATOR_REP_TIME) {
205 warn("The mean duration between User Control Pressed messages is higher\n");
206 warn("than the Maximum Initiator Repetition Time (500ms).\n");
207 }
208 }
209
210 static __u16 pa_common_mask(__u16 pa1, __u16 pa2)
211 {
212 __u16 mask = 0xf000;
213
214 for (int i = 0; i < 3; i++) {
215 if ((pa1 & mask) != (pa2 & mask))
216 break;
217 mask = (mask >> 4) | 0xf000;
218 }
219 return mask << 4;
220 }
221 static bool pa_are_adjacent(__u16 pa1, __u16 pa2)
222 {
223 const __u16 mask = pa_common_mask(pa1, pa2);
224 const __u16 trail_mask = ((~mask) & 0xffff) >> 4;
225
226 if (pa1 == CEC_PHYS_ADDR_INVALID || pa2 == CEC_PHYS_ADDR_INVALID || pa1 == pa2)
227 return false;
228 if ((pa1 & trail_mask) || (pa2 & trail_mask))
229 return false;
230 return !((pa1 & ~mask) && (pa2 & ~mask));
231 }
232
233 static bool pa_is_upstream_from(__u16 pa1, __u16 pa2)
234 {
235 const __u16 mask = pa_common_mask(pa1, pa2);
236
237 if (pa1 == CEC_PHYS_ADDR_INVALID || pa2 == CEC_PHYS_ADDR_INVALID)
238 return false;
239 return !(pa1 & ~mask) && (pa2 & ~mask);
240 }
241
242 static __u8 current_power_state(struct node *node)
243 {
244 time_t t = time(nullptr);
245
246 if (t - node->state.power_status_changed_time <= time_to_transient)
247 return node->state.old_power_status;
248 if (t - node->state.power_status_changed_time >= time_to_stable)
249 return node->state.power_status;
250 if (node->state.power_status == CEC_OP_POWER_STATUS_ON)
251 return CEC_OP_POWER_STATUS_TO_ON;
252 return CEC_OP_POWER_STATUS_TO_STANDBY;
253 }
254
255 static void aud_rate_msg_interval_check(struct node *node, __u64 ts_new)
256 {
257 /*
258 * The interval since the last audio rate message is only relevant
259 * if the Source is currently in audio rate controlled mode
260 * (i.e. state.last_aud_rate_rx_ts != 0).
261 */
262 __u64 ts_old = node->state.last_aud_rate_rx_ts;
263
264 if (ts_old) {
265 __u64 interval = ts_new - ts_old;
266
267 if (interval > MAX_AUD_RATE_MSG_INTERVAL_NS) {
268 warn("The interval since the last Audio Rate Control message was > 2s.\n");
269 node->state.last_aud_rate_rx_ts = 0;
270 }
271 }
272 }
273
274 static void update_deck_state(struct node *node, unsigned me, __u8 deck_state_new)
275 {
276 if (node->state.deck_state != deck_state_new) {
277 node->state.deck_state = deck_state_new;
278
279 if (node->state.deck_report_changes) {
280 struct cec_msg msg;
281
282 cec_msg_init(&msg, me, node->state.deck_report_changes_to);
283 cec_msg_deck_status(&msg, node->state.deck_state);
284 transmit(node, &msg);
285 }
286 }
287 }
288
289 static void processMsg(struct node *node, struct cec_msg &msg, unsigned me, __u8 type)
290 {
291 __u8 to = cec_msg_destination(&msg);
292 __u8 from = cec_msg_initiator(&msg);
293 bool is_bcast = cec_msg_is_broadcast(&msg);
294 __u16 remote_pa = (from < 15) ? node->remote_phys_addr[from] : CEC_PHYS_ADDR_INVALID;
295
296 switch (msg.msg[1]) {
297
298 /* OSD Display */
299
300 case CEC_MSG_SET_OSD_STRING: {
301 __u8 disp_ctl;
302 char osd[14];
303
304 if (to != 0 && to != 14)
305 break;
306 if (!node->has_osd_string)
307 break;
308
309 cec_ops_set_osd_string(&msg, &disp_ctl, osd);
310 switch (disp_ctl) {
311 case CEC_OP_DISP_CTL_DEFAULT:
312 case CEC_OP_DISP_CTL_UNTIL_CLEARED:
313 case CEC_OP_DISP_CTL_CLEAR:
314 return;
315 }
316 cec_msg_reply_feature_abort(&msg, CEC_OP_ABORT_INVALID_OP);
317 transmit(node, &msg);
318 return;
319 }
320
321
322 /* Give Device Power Status */
323
324 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
325 cec_msg_set_reply_to(&msg, &msg);
326 cec_msg_report_power_status(&msg, current_power_state(node));
327 transmit(node, &msg);
328 return;
329
330 case CEC_MSG_REPORT_POWER_STATUS:
331 // Nothing to do here for now.
332 return;
333
334
335 /* Standby */
336
337 case CEC_MSG_STANDBY:
338 if (!node->ignore_standby || (++node->standby_cnt % node->ignore_standby))
339 enter_standby(node);
340 return;
341
342
343 /* One Touch Play and Routing Control */
344
345 case CEC_MSG_ACTIVE_SOURCE: {
346 __u16 phys_addr;
347
348 cec_ops_active_source(&msg, &phys_addr);
349 node->state.active_source_pa = phys_addr;
350 dev_info("New active source: %x.%x.%x.%x\n", cec_phys_addr_exp(phys_addr));
351 return;
352 }
353 case CEC_MSG_INACTIVE_SOURCE: {
354 __u16 phys_addr;
355
356 if (node->phys_addr)
357 break;
358
359 cec_ops_active_source(&msg, &phys_addr);
360 if (node->state.active_source_pa != phys_addr)
361 break;
362 node->state.active_source_pa = 0;
363 cec_msg_init(&msg, me, CEC_LOG_ADDR_BROADCAST);
364 cec_msg_active_source(&msg, node->phys_addr);
365 transmit(node, &msg);
366 dev_info("New active source: 0.0.0.0\n");
367 return;
368 }
369 case CEC_MSG_IMAGE_VIEW_ON:
370 case CEC_MSG_TEXT_VIEW_ON:
371 if (!cec_has_tv(1 << me))
372 break;
373 if (!node->ignore_view_on || (++node->view_on_cnt % node->ignore_view_on))
374 exit_standby(node);
375 return;
376 case CEC_MSG_SET_STREAM_PATH: {
377 __u16 phys_addr;
378
379 cec_ops_set_stream_path(&msg, &phys_addr);
380 if (phys_addr != node->phys_addr)
381 return;
382
383 if (!cec_has_tv(1 << me))
384 exit_standby(node);
385
386 cec_msg_init(&msg, me, CEC_LOG_ADDR_BROADCAST);
387 cec_msg_active_source(&msg, node->phys_addr);
388 transmit(node, &msg);
389 dev_info("Stream Path is directed to this device\n");
390 return;
391 }
392 case CEC_MSG_ROUTING_INFORMATION: {
393 __u8 la = cec_msg_initiator(&msg);
394
395 if (cec_has_tv(1 << la) && la_info[la].phys_addr == 0)
396 warn("TV (0) at 0.0.0.0 sent Routing Information.");
397 return;
398 }
399
400 /* System Information */
401
402 case CEC_MSG_GET_MENU_LANGUAGE:
403 if (!cec_has_tv(1 << me))
404 break;
405 cec_msg_set_reply_to(&msg, &msg);
406 cec_msg_set_menu_language(&msg, node->state.menu_language);
407 transmit(node, &msg);
408 return;
409 case CEC_MSG_CEC_VERSION:
410 return;
411 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
412 __u16 phys_addr;
413 __u8 prim_dev_type;
414
415 cec_ops_report_physical_addr(&msg, &phys_addr, &prim_dev_type);
416 if (from < 15) {
417 node->remote_phys_addr[from] = phys_addr;
418 node->remote_prim_devtype[from] = prim_dev_type;
419 }
420 return;
421 }
422
423
424 /* Remote Control Passthrough
425 (System Audio Control, Device Menu Control) */
426
427 case CEC_MSG_USER_CONTROL_PRESSED: {
428 struct cec_op_ui_command rc_press;
429 unsigned new_state;
430 unsigned duration;
431
432 cec_ops_user_control_pressed(&msg, &rc_press);
433 duration = get_duration_ms(msg.rx_ts, node->state.rc_press_rx_ts);
434
435 new_state = PRESS;
436 if (node->state.rc_state == NOPRESS)
437 dev_info("Button press: %s\n", get_ui_cmd_string(rc_press.ui_cmd));
438 else if (rc_press.ui_cmd != node->state.rc_ui_cmd) {
439 /* We have not yet received User Control Released, but have received
440 another User Control Pressed with a different UI Command. */
441 if (node->state.rc_state == PRESS_HOLD)
442 rc_press_hold_stop(&node->state);
443 dev_info("Button press (no User Control Released between): %s\n",
444 get_ui_cmd_string(rc_press.ui_cmd));
445
446 /* The device shall send User Control Released if the time between
447 two messages is longer than the maximum Initiator Repetition Time. */
448 if (duration > MAX_INITIATOR_REP_TIME)
449 warn("Device waited more than the maximum Initiatior Repetition Time and should have sent a User Control Released message.");
450 } else if (duration < FOLLOWER_SAFETY_TIMEOUT) {
451 /* We have not yet received a User Control Released, but received
452 another User Control Pressed, with the same UI Command as the
453 previous, which means that the Press and Hold behavior should
454 be invoked. */
455 new_state = PRESS_HOLD;
456 if (node->state.rc_state != PRESS_HOLD) {
457 dev_info("Start Press and Hold with button %s\n",
458 get_ui_cmd_string(rc_press.ui_cmd));
459 node->state.rc_duration_sum = 0;
460 node->state.rc_press_hold_count = 0;
461 }
462 node->state.rc_duration_sum += duration;
463 node->state.rc_press_hold_count++;
464 }
465
466 node->state.rc_state = new_state;
467 node->state.rc_ui_cmd = rc_press.ui_cmd;
468 node->state.rc_press_rx_ts = msg.rx_ts;
469
470 switch (rc_press.ui_cmd) {
471 case CEC_OP_UI_CMD_VOLUME_UP:
472 if (node->state.volume < VOLUME_MAX)
473 node->state.volume++;
474 break;
475 case CEC_OP_UI_CMD_VOLUME_DOWN:
476 if (node->state.volume > VOLUME_MIN)
477 node->state.volume--;
478 break;
479 case CEC_OP_UI_CMD_MUTE:
480 node->state.mute = !node->state.mute;
481 break;
482 case CEC_OP_UI_CMD_MUTE_FUNCTION:
483 node->state.mute = true;
484 break;
485 case CEC_OP_UI_CMD_RESTORE_VOLUME_FUNCTION:
486 node->state.mute = false;
487 break;
488 case CEC_OP_UI_CMD_POWER_TOGGLE_FUNCTION:
489 if (!enter_standby(node))
490 exit_standby(node);
491 break;
492 case CEC_OP_UI_CMD_POWER_OFF_FUNCTION:
493 enter_standby(node);
494 break;
495 case CEC_OP_UI_CMD_POWER_ON_FUNCTION:
496 exit_standby(node);
497 break;
498 }
499 if (rc_press.ui_cmd >= CEC_OP_UI_CMD_VOLUME_UP && rc_press.ui_cmd <= CEC_OP_UI_CMD_MUTE) {
500 dev_info("Volume: %d%s\n", node->state.volume, node->state.mute ? ", muted" : "");
501 cec_msg_set_reply_to(&msg, &msg);
502 cec_msg_report_audio_status(&msg, node->state.mute ? 1 : 0, node->state.volume);
503 transmit(node, &msg);
504 }
505
506 return;
507 }
508 case CEC_MSG_USER_CONTROL_RELEASED:
509 if (node->state.rc_state == PRESS_HOLD)
510 rc_press_hold_stop(&node->state);
511
512 if (node->state.rc_state == NOPRESS)
513 dev_info("Button release (unexpected, %ums after last press)\n",
514 ts_to_ms(msg.rx_ts - node->state.rc_press_rx_ts));
515 else if (get_duration_ms(msg.rx_ts, node->state.rc_press_rx_ts) > FOLLOWER_SAFETY_TIMEOUT)
516 dev_info("Button release: %s (after timeout, %ums after last press)\n",
517 get_ui_cmd_string(node->state.rc_ui_cmd),
518 ts_to_ms(msg.rx_ts - node->state.rc_press_rx_ts));
519 else
520 dev_info("Button release: %s (%ums after last press)\n",
521 get_ui_cmd_string(node->state.rc_ui_cmd),
522 ts_to_ms(msg.rx_ts - node->state.rc_press_rx_ts));
523 node->state.rc_state = NOPRESS;
524 return;
525
526
527 /* Device Menu Control */
528
529 case CEC_MSG_MENU_REQUEST: {
530 if (node->cec_version < CEC_OP_CEC_VERSION_2_0 &&
531 !cec_has_tv(1 << me)) {
532 cec_msg_set_reply_to(&msg, &msg);
533 cec_msg_menu_status(&msg, CEC_OP_MENU_STATE_ACTIVATED);
534 transmit(node, &msg);
535 return;
536 }
537 break;
538 }
539 case CEC_MSG_MENU_STATUS:
540 if (node->cec_version < CEC_OP_CEC_VERSION_2_0 &&
541 cec_has_tv(1 << me))
542 return;
543 break;
544
545
546 /* Deck Control */
547
548 case CEC_MSG_GIVE_DECK_STATUS:
549 if (!node->has_deck_ctl)
550 break;
551
552 __u8 status_req;
553 cec_ops_give_deck_status(&msg, &status_req);
554
555 switch (status_req) {
556 case CEC_OP_STATUS_REQ_ON:
557 node->state.deck_report_changes = true;
558 node->state.deck_report_changes_to = cec_msg_initiator(&msg);
559 fallthrough;
560 case CEC_OP_STATUS_REQ_ONCE:
561 cec_msg_set_reply_to(&msg, &msg);
562 cec_msg_deck_status(&msg, node->state.deck_state);
563 transmit(node, &msg);
564 return;
565 case CEC_OP_STATUS_REQ_OFF:
566 node->state.deck_report_changes = false;
567 return;
568 default:
569 reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
570 return;
571 }
572 case CEC_MSG_PLAY:
573 if (!node->has_deck_ctl)
574 break;
575
576 __u8 deck_state;
577 __u8 play_mode;
578
579 cec_ops_play(&msg, &play_mode);
580
581 switch (play_mode) {
582 case CEC_OP_PLAY_MODE_PLAY_FWD:
583 exit_standby(node);
584 deck_state = CEC_OP_DECK_INFO_PLAY;
585 break;
586 case CEC_OP_PLAY_MODE_PLAY_REV:
587 deck_state = CEC_OP_DECK_INFO_PLAY_REV;
588 break;
589 case CEC_OP_PLAY_MODE_PLAY_STILL:
590 deck_state = CEC_OP_DECK_INFO_STILL;
591 break;
592 case CEC_OP_PLAY_MODE_PLAY_FAST_FWD_MIN:
593 case CEC_OP_PLAY_MODE_PLAY_FAST_FWD_MED:
594 case CEC_OP_PLAY_MODE_PLAY_FAST_FWD_MAX:
595 deck_state = CEC_OP_DECK_INFO_FAST_FWD;
596 break;
597 case CEC_OP_PLAY_MODE_PLAY_FAST_REV_MIN:
598 case CEC_OP_PLAY_MODE_PLAY_FAST_REV_MED:
599 case CEC_OP_PLAY_MODE_PLAY_FAST_REV_MAX:
600 deck_state = CEC_OP_DECK_INFO_FAST_REV;
601 break;
602 case CEC_OP_PLAY_MODE_PLAY_SLOW_FWD_MIN:
603 case CEC_OP_PLAY_MODE_PLAY_SLOW_FWD_MED:
604 case CEC_OP_PLAY_MODE_PLAY_SLOW_FWD_MAX:
605 deck_state = CEC_OP_DECK_INFO_SLOW;
606 break;
607 case CEC_OP_PLAY_MODE_PLAY_SLOW_REV_MIN:
608 case CEC_OP_PLAY_MODE_PLAY_SLOW_REV_MED:
609 case CEC_OP_PLAY_MODE_PLAY_SLOW_REV_MAX:
610 deck_state = CEC_OP_DECK_INFO_SLOW_REV;
611 break;
612 default:
613 cec_msg_reply_feature_abort(&msg, CEC_OP_ABORT_INVALID_OP);
614 transmit(node, &msg);
615 return;
616 }
617 /* Only Play Forward and Play Still will close tray if open. */
618 if (play_mode != CEC_OP_PLAY_MODE_PLAY_FWD &&
619 play_mode != CEC_OP_PLAY_MODE_PLAY_STILL &&
620 node->state.deck_state == CEC_OP_DECK_INFO_NO_MEDIA) {
621 reply_feature_abort(node, &msg, CEC_OP_ABORT_INCORRECT_MODE);
622 return;
623 }
624 node->state.deck_skip_start = 0;
625 update_deck_state(node, me, deck_state);
626 return;
627 case CEC_MSG_DECK_CONTROL:
628 if (!node->has_deck_ctl)
629 break;
630
631 __u8 deck_control_mode;
632
633 cec_ops_deck_control(&msg, &deck_control_mode);
634
635 switch (deck_control_mode) {
636 case CEC_OP_DECK_CTL_MODE_STOP:
637 deck_state = CEC_OP_DECK_INFO_STOP;
638 node->state.deck_skip_start = 0;
639 break;
640 case CEC_OP_DECK_CTL_MODE_EJECT:
641 exit_standby(node);
642 deck_state = CEC_OP_DECK_INFO_NO_MEDIA;
643 node->state.deck_skip_start = 0;
644 break;
645 case CEC_OP_DECK_CTL_MODE_SKIP_FWD:
646 /* Skip Forward will not retract the deck tray. */
647 if (node->state.deck_state == CEC_OP_DECK_INFO_NO_MEDIA) {
648 reply_feature_abort(node, &msg, CEC_OP_ABORT_INCORRECT_MODE);
649 return;
650 }
651 deck_state = CEC_OP_DECK_INFO_SKIP_FWD;
652 node->state.deck_skip_start = msg.rx_ts;
653 break;
654 case CEC_OP_DECK_CTL_MODE_SKIP_REV:
655 /* Skip Reverse will not retract the deck tray. */
656 if (node->state.deck_state == CEC_OP_DECK_INFO_NO_MEDIA) {
657 reply_feature_abort(node, &msg, CEC_OP_ABORT_INCORRECT_MODE);
658 return;
659 }
660 deck_state = CEC_OP_DECK_INFO_SKIP_REV;
661 node->state.deck_skip_start = msg.rx_ts;
662 break;
663 default:
664 cec_msg_reply_feature_abort(&msg, CEC_OP_ABORT_INVALID_OP);
665 transmit(node, &msg);
666 return;
667 }
668 update_deck_state(node, me, deck_state);
669 return;
670 case CEC_MSG_DECK_STATUS:
671 return;
672
673 /* Tuner/Record/Timer Messages */
674
675 case CEC_MSG_GIVE_TUNER_DEVICE_STATUS:
676 case CEC_MSG_TUNER_DEVICE_STATUS:
677 case CEC_MSG_SELECT_ANALOGUE_SERVICE:
678 case CEC_MSG_SELECT_DIGITAL_SERVICE:
679 case CEC_MSG_TUNER_STEP_DECREMENT:
680 case CEC_MSG_TUNER_STEP_INCREMENT:
681 process_tuner_msgs(node, msg, me, type);
682 return;
683 case CEC_MSG_RECORD_TV_SCREEN:
684 case CEC_MSG_RECORD_ON:
685 case CEC_MSG_RECORD_OFF:
686 case CEC_MSG_RECORD_STATUS:
687 process_record_msgs(node, msg, me, type);
688 return;
689 case CEC_MSG_SET_ANALOGUE_TIMER:
690 case CEC_MSG_SET_DIGITAL_TIMER:
691 case CEC_MSG_SET_EXT_TIMER:
692 case CEC_MSG_CLEAR_ANALOGUE_TIMER:
693 case CEC_MSG_CLEAR_DIGITAL_TIMER:
694 case CEC_MSG_CLEAR_EXT_TIMER:
695 case CEC_MSG_SET_TIMER_PROGRAM_TITLE:
696 case CEC_MSG_TIMER_CLEARED_STATUS:
697 case CEC_MSG_TIMER_STATUS:
698 process_timer_msgs(node, msg, me, type);
699 return;
700
701 /* Dynamic Auto Lipsync */
702
703 case CEC_MSG_REQUEST_CURRENT_LATENCY: {
704 __u16 phys_addr;
705
706 cec_ops_request_current_latency(&msg, &phys_addr);
707 if (phys_addr == node->phys_addr) {
708 cec_msg_init(&msg, me, from);
709 cec_msg_report_current_latency(&msg, phys_addr,
710 node->state.video_latency,
711 node->state.low_latency_mode,
712 node->state.audio_out_compensated,
713 node->state.audio_out_delay);
714 transmit(node, &msg);
715 }
716 return;
717 }
718
719
720 /* Audio Return Channel Control */
721
722 case CEC_MSG_INITIATE_ARC:
723 if (node->sink_has_arc_tx) {
724 if (!pa_is_upstream_from(node->phys_addr, remote_pa) ||
725 !pa_are_adjacent(node->phys_addr, remote_pa)) {
726 cec_msg_reply_feature_abort(&msg, CEC_OP_ABORT_REFUSED);
727 transmit(node, &msg);
728 return;
729 }
730 cec_msg_set_reply_to(&msg, &msg);
731 cec_msg_report_arc_initiated(&msg);
732 transmit(node, &msg);
733 node->state.arc_active = true;
734 dev_info("ARC is initiated");
735 return;
736 }
737 break;
738 case CEC_MSG_TERMINATE_ARC:
739 if (node->sink_has_arc_tx) {
740 if (!pa_is_upstream_from(node->phys_addr, remote_pa) ||
741 !pa_are_adjacent(node->phys_addr, remote_pa)) {
742 cec_msg_reply_feature_abort(&msg, CEC_OP_ABORT_REFUSED);
743 transmit(node, &msg);
744 return;
745 }
746 cec_msg_set_reply_to(&msg, &msg);
747 cec_msg_report_arc_terminated(&msg);
748 transmit(node, &msg);
749 node->state.arc_active = false;
750 dev_info("ARC is terminated\n");
751 return;
752 }
753 break;
754 case CEC_MSG_REQUEST_ARC_INITIATION:
755 if (node->source_has_arc_rx) {
756 if (pa_is_upstream_from(node->phys_addr, remote_pa) ||
757 !pa_are_adjacent(node->phys_addr, remote_pa)) {
758 cec_msg_reply_feature_abort(&msg, CEC_OP_ABORT_REFUSED);
759 transmit(node, &msg);
760 return;
761 }
762 cec_msg_set_reply_to(&msg, &msg);
763 cec_msg_initiate_arc(&msg, false);
764 transmit(node, &msg);
765 dev_info("ARC initiation has been requested.");
766 return;
767 }
768 break;
769 case CEC_MSG_REQUEST_ARC_TERMINATION:
770 if (node->source_has_arc_rx) {
771 if (pa_is_upstream_from(node->phys_addr, remote_pa) ||
772 !pa_are_adjacent(node->phys_addr, remote_pa)) {
773 cec_msg_reply_feature_abort(&msg, CEC_OP_ABORT_REFUSED);
774 transmit(node, &msg);
775 return;
776 }
777 cec_msg_set_reply_to(&msg, &msg);
778 cec_msg_terminate_arc(&msg, false);
779 transmit(node, &msg);
780 dev_info("ARC initiation has been requested.");
781 return;
782 }
783 break;
784 case CEC_MSG_REPORT_ARC_INITIATED:
785 node->state.arc_active = true;
786 dev_info("ARC is initiated\n");
787 return;
788 case CEC_MSG_REPORT_ARC_TERMINATED:
789 node->state.arc_active = false;
790 dev_info("ARC is terminated\n");
791 return;
792
793
794 /* System Audio Control */
795
796 case CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST: {
797 if (!cec_has_audiosystem(1 << me))
798 break;
799
800 __u16 phys_addr;
801
802 cec_ops_system_audio_mode_request(&msg, &phys_addr);
803 cec_msg_init(&msg, me, CEC_LOG_ADDR_BROADCAST);
804 if (phys_addr != CEC_PHYS_ADDR_INVALID) {
805 cec_msg_set_system_audio_mode(&msg, CEC_OP_SYS_AUD_STATUS_ON);
806 transmit(node, &msg);
807 node->state.sac_active = true;
808 }
809 else {
810 cec_msg_set_system_audio_mode(&msg, CEC_OP_SYS_AUD_STATUS_OFF);
811 transmit(node, &msg);
812 node->state.sac_active = false;
813 }
814 dev_info("System Audio Mode: %s\n", node->state.sac_active ? "on" : "off");
815 return;
816 }
817 case CEC_MSG_SET_SYSTEM_AUDIO_MODE: {
818 __u8 system_audio_status;
819
820 if (!cec_msg_is_broadcast(&msg)) {
821 /* Directly addressed Set System Audio Mode is used to see
822 if the TV supports the feature. If we time out, we
823 signalize that we support SAC. */
824 if (cec_has_tv(1 << me))
825 return;
826
827 break;
828 }
829 cec_ops_set_system_audio_mode(&msg, &system_audio_status);
830 if (system_audio_status == CEC_OP_SYS_AUD_STATUS_ON)
831 node->state.sac_active = true;
832 else if (system_audio_status == CEC_OP_SYS_AUD_STATUS_OFF)
833 node->state.sac_active = false;
834 dev_info("System Audio Mode: %s\n", node->state.sac_active ? "on" : "off");
835 return;
836 }
837 case CEC_MSG_REPORT_AUDIO_STATUS:
838 return;
839 case CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS:
840 if (!cec_has_audiosystem(1 << me))
841 break;
842 cec_msg_set_reply_to(&msg, &msg);
843 cec_msg_system_audio_mode_status(&msg, node->state.sac_active ? CEC_OP_SYS_AUD_STATUS_ON :
844 CEC_OP_SYS_AUD_STATUS_OFF);
845 transmit(node, &msg);
846 fallthrough;
847 case CEC_MSG_GIVE_AUDIO_STATUS:
848 if (!cec_has_audiosystem(1 << me))
849 break;
850 cec_msg_set_reply_to(&msg, &msg);
851 cec_msg_report_audio_status(&msg, node->state.mute ? 1 : 0, node->state.volume);
852 transmit(node, &msg);
853 return;
854 case CEC_MSG_SYSTEM_AUDIO_MODE_STATUS:
855 return;
856 case CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR: {
857 if (!cec_has_audiosystem(1 << me))
858 break;
859
860 /* The list of formats that the follower 'supports' */
861 const struct short_audio_desc supported_formats[] = {
862 { 2, SAD_FMT_CODE_AC3,
863 SAD_SAMPLE_FREQ_MASK_32 | SAD_SAMPLE_FREQ_MASK_44_1,
864 { 64 }, 0, 0 },
865 { 4, SAD_FMT_CODE_AC3,
866 SAD_SAMPLE_FREQ_MASK_32,
867 { 32 }, 0, 0 },
868 { 4, SAD_FMT_CODE_ONE_BIT_AUDIO,
869 SAD_SAMPLE_FREQ_MASK_48 | SAD_SAMPLE_FREQ_MASK_192,
870 { 123 }, 0, 0 },
871 { 8, SAD_FMT_CODE_EXTENDED,
872 SAD_SAMPLE_FREQ_MASK_96,
873 { 0 }, 0, SAD_EXT_TYPE_DRA },
874 { 2, SAD_FMT_CODE_EXTENDED,
875 SAD_SAMPLE_FREQ_MASK_176_4,
876 { SAD_FRAME_LENGTH_MASK_960 | SAD_FRAME_LENGTH_MASK_1024 }, 1, SAD_EXT_TYPE_MPEG4_HE_AAC_SURROUND },
877 { 2, SAD_FMT_CODE_EXTENDED,
878 SAD_SAMPLE_FREQ_MASK_44_1,
879 { SAD_BIT_DEPTH_MASK_16 | SAD_BIT_DEPTH_MASK_24 }, 0, SAD_EXT_TYPE_LPCM_3D_AUDIO },
880 };
881
882 __u8 num_descriptors, audio_format_id[4], audio_format_code[4];
883 __u32 descriptors[4];
884 std::string format_list;
885
886 cec_ops_request_short_audio_descriptor(&msg, &num_descriptors, audio_format_id, audio_format_code);
887 if (num_descriptors == 0) {
888 warn("Got Request Short Audio Descriptor with no operands");
889 reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
890 return;
891 }
892
893 unsigned found_descs = 0;
894
895 for (int i = 0; i < num_descriptors; i++)
896 format_list += audio_format_id_code2s(audio_format_id[i], audio_format_code[i]) + ",";
897 format_list.erase(format_list.end() - 1);
898 dev_info("Requested descriptors: %s\n", format_list.c_str());
899 for (unsigned i = 0; i < num_descriptors; i++) {
900 for (const auto &supported_format : supported_formats) {
901 if (found_descs >= 4)
902 break;
903 if ((audio_format_id[i] == 0 &&
904 audio_format_code[i] == supported_format.format_code) ||
905 (audio_format_id[i] == 1 &&
906 audio_format_code[i] == supported_format.extension_type_code))
907 sad_encode(&supported_format, &descriptors[found_descs++]);
908 }
909 }
910
911 if (found_descs > 0) {
912 cec_msg_set_reply_to(&msg, &msg);
913 cec_msg_report_short_audio_descriptor(&msg, found_descs, descriptors);
914 transmit(node, &msg);
915 }
916 else
917 reply_feature_abort(node, &msg, CEC_OP_ABORT_INVALID_OP);
918 return;
919 }
920
921
922 /* Device OSD Transfer */
923
924 case CEC_MSG_SET_OSD_NAME:
925 return;
926
927
928 /* Audio Rate Control */
929
930 case CEC_MSG_SET_AUDIO_RATE:
931 if (!node->has_aud_rate)
932 break;
933
934 switch (msg.msg[2]) {
935 case CEC_OP_AUD_RATE_OFF:
936 aud_rate_msg_interval_check(node, msg.rx_ts);
937 node->state.last_aud_rate_rx_ts = 0;
938 return;
939 case CEC_OP_AUD_RATE_WIDE_STD:
940 case CEC_OP_AUD_RATE_WIDE_FAST:
941 case CEC_OP_AUD_RATE_WIDE_SLOW:
942 case CEC_OP_AUD_RATE_NARROW_STD:
943 case CEC_OP_AUD_RATE_NARROW_FAST:
944 case CEC_OP_AUD_RATE_NARROW_SLOW:
945 aud_rate_msg_interval_check(node, msg.rx_ts);
946 node->state.last_aud_rate_rx_ts = msg.rx_ts;
947 return;
948 default:
949 cec_msg_reply_feature_abort(&msg, CEC_OP_ABORT_INVALID_OP);
950 transmit(node, &msg);
951 break;
952 }
953 break;
954
955
956 /* CDC */
957
958 case CEC_MSG_CDC_MESSAGE: {
959 switch (msg.msg[4]) {
960 case CEC_MSG_CDC_HEC_DISCOVER:
961 __u16 phys_addr;
962
963 cec_msg_init(&msg, me, CEC_LOG_ADDR_BROADCAST);
964 cec_ops_cdc_hec_discover(&msg, &phys_addr);
965 cec_msg_cdc_hec_report_state(&msg, phys_addr,
966 CEC_OP_HEC_FUNC_STATE_NOT_SUPPORTED,
967 CEC_OP_HOST_FUNC_STATE_NOT_SUPPORTED,
968 CEC_OP_ENC_FUNC_STATE_EXT_CON_NOT_SUPPORTED,
969 CEC_OP_CDC_ERROR_CODE_NONE,
970 1, 0); // We do not support HEC on any HDMI connections
971 transmit(node, &msg);
972 return;
973 }
974 }
975
976
977 /* Core */
978
979 case CEC_MSG_FEATURE_ABORT:
980 return;
981 default:
982 break;
983 }
984
985 if (is_bcast)
986 return;
987
988 reply_feature_abort(node, &msg);
989 }
990
991 static void poll_remote_devs(struct node *node, unsigned me)
992 {
993 node->remote_la_mask = 0;
994 for (unsigned short & i : node->remote_phys_addr)
995 i = CEC_PHYS_ADDR_INVALID;
996
997 if (!(node->caps & CEC_CAP_TRANSMIT))
998 return;
999
1000 for (unsigned i = 0; i < 15; i++) {
1001 struct cec_msg msg;
1002
1003 cec_msg_init(&msg, me, i);
1004
1005 doioctl(node, CEC_TRANSMIT, &msg);
1006 if (msg.tx_status & CEC_TX_STATUS_OK) {
1007 node->remote_la_mask |= 1 << i;
1008 cec_msg_init(&msg, me, i);
1009 cec_msg_give_physical_addr(&msg, true);
1010 doioctl(node, CEC_TRANSMIT, &msg);
1011 if (cec_msg_status_is_ok(&msg))
1012 node->remote_phys_addr[i] = (msg.msg[2] << 8) | msg.msg[3];
1013 }
1014 }
1015 }
1016
1017 static void update_programmed_timers(struct node *node)
1018 {
1019 auto it = programmed_timers.begin();
1020 /* Use the current minute because timers do not have second precision. */
1021 time_t current_minute = time(nullptr) / 60;
1022 time_t timer_start_minute = it->start_time / 60;
1023 time_t timer_end_minute = (it->start_time + it->duration) / 60;
1024
1025 /* Start the timed recording only if the deck is not already recording. */
1026 if (timer_start_minute == current_minute && !node->state.one_touch_record_on) {
1027 node->state.one_touch_record_on = true;
1028 node->state.recording_controlled_by_timer = true;
1029 print_timers(node);
1030 }
1031
1032 /* Delete an overlapped timer. Recording will be at best incomplete. */
1033 if (timer_start_minute < current_minute &&
1034 (!node->state.recording_controlled_by_timer || !node->state.one_touch_record_on)) {
1035 programmed_timers.erase(*it);
1036 if (show_info)
1037 printf("Deleted overlapped timer.\n");
1038 print_timers(node);
1039 }
1040
1041 if (timer_end_minute != current_minute || !node->state.recording_controlled_by_timer)
1042 return;
1043
1044 /* Delete finished timers. */
1045 node->state.one_touch_record_on = false;
1046 node->state.recording_controlled_by_timer = false;
1047 node->state.media_space_available -= it->duration; /* 1 MB per second */
1048 /*
1049 * TODO: We are only ever decreasing the amount of space available,
1050 * there is no heuristic that reclaims the space.
1051 */
1052
1053 if (it->recording_seq) {
1054 struct tm *last_start_time = localtime(&(it->start_time));
1055 int next_wday = (last_start_time->tm_wday + 1) % 7;
1056 int days_to_move_ahead = 1;
1057
1058 while ((it->recording_seq & (1 << next_wday)) == 0) {
1059 days_to_move_ahead++;
1060 next_wday = (next_wday + 1) % 7;
1061 }
1062 struct Timer next_timer = {};
1063 next_timer = *it;
1064 last_start_time->tm_mday += days_to_move_ahead;
1065 last_start_time->tm_isdst = -1;
1066 next_timer.start_time = mktime(last_start_time);
1067 programmed_timers.insert(next_timer);
1068 }
1069 programmed_timers.erase(*it);
1070 if (show_info)
1071 printf("Deleted finished timer.\n");
1072 print_timers(node);
1073 /*
1074 * If the finished timer was recording, and standby was received during recording,
1075 * enter standby when the recording stops unless recording device is the active source.
1076 */
1077 if (node->state.record_received_standby) {
1078 if (node->phys_addr != node->state.active_source_pa)
1079 enter_standby(node);
1080 node->state.record_received_standby = false;
1081 }
1082 }
1083
1084 void testProcessing(struct node *node, bool exclusive, bool wallclock)
1085 {
1086 struct cec_log_addrs laddrs;
1087 fd_set rd_fds;
1088 fd_set ex_fds;
1089 int fd = node->fd;
1090 __u32 mode = CEC_MODE_INITIATOR |
1091 (exclusive ? CEC_MODE_EXCL_FOLLOWER : CEC_MODE_FOLLOWER);
1092 unsigned me;
1093 unsigned last_poll_la = 15;
1094 __u8 last_pwr_state = current_power_state(node);
1095 time_t last_pwr_status_toggle = time(nullptr);
1096
1097 clock_gettime(CLOCK_MONOTONIC, &start_monotonic);
1098 gettimeofday(&start_timeofday, nullptr);
1099
1100 doioctl(node, CEC_S_MODE, &mode);
1101 doioctl(node, CEC_ADAP_G_LOG_ADDRS, &laddrs);
1102 me = laddrs.log_addr[0];
1103 __u8 type = laddrs.log_addr_type[0];
1104
1105 poll_remote_devs(node, me);
1106
1107 while (true) {
1108 int res;
1109 struct timeval timeval = {};
1110
1111 fflush(stdout);
1112 timeval.tv_sec = 1;
1113 FD_ZERO(&rd_fds);
1114 FD_ZERO(&ex_fds);
1115 FD_SET(fd, &rd_fds);
1116 FD_SET(fd, &ex_fds);
1117 res = select(fd + 1, &rd_fds, nullptr, &ex_fds, &timeval);
1118 if (res < 0)
1119 break;
1120 if (FD_ISSET(fd, &ex_fds)) {
1121 struct cec_event ev;
1122
1123 res = doioctl(node, CEC_DQEVENT, &ev);
1124 if (res == ENODEV) {
1125 printf("Device was disconnected.\n");
1126 break;
1127 }
1128 if (res)
1129 continue;
1130 log_event(ev, wallclock);
1131 if (ev.event == CEC_EVENT_STATE_CHANGE) {
1132 dev_info("CEC adapter state change.\n");
1133 node->phys_addr = ev.state_change.phys_addr;
1134 node->adap_la_mask = ev.state_change.log_addr_mask;
1135 if (node->adap_la_mask) {
1136 doioctl(node, CEC_ADAP_G_LOG_ADDRS, &laddrs);
1137 me = laddrs.log_addr[0];
1138 } else {
1139 node->state.active_source_pa = CEC_PHYS_ADDR_INVALID;
1140 me = CEC_LOG_ADDR_INVALID;
1141 }
1142 memset(la_info, 0, sizeof(la_info));
1143 }
1144 }
1145 if (FD_ISSET(fd, &rd_fds)) {
1146 struct cec_msg msg = { };
1147
1148 res = doioctl(node, CEC_RECEIVE, &msg);
1149 if (res == ENODEV) {
1150 printf("Device was disconnected.\n");
1151 break;
1152 }
1153 if (res)
1154 continue;
1155
1156 __u8 from = cec_msg_initiator(&msg);
1157 __u8 to = cec_msg_destination(&msg);
1158 __u8 opcode = cec_msg_opcode(&msg);
1159
1160 if (node->ignore_la[from])
1161 continue;
1162 if (node->ignore_opcode[msg.msg[1]] & (1 << from))
1163 continue;
1164
1165 if (from != CEC_LOG_ADDR_UNREGISTERED &&
1166 la_info[from].feature_aborted[opcode].ts &&
1167 ts_to_ms(get_ts() - la_info[from].feature_aborted[opcode].ts) < 200) {
1168 warn("Received message %s from LA %d (%s) less than 200 ms after\n",
1169 opcode2s(&msg).c_str(), from, cec_la2s(from));
1170 warn("replying Feature Abort (not [Unrecognized Opcode]) to the same message.\n");
1171 }
1172 if (from != CEC_LOG_ADDR_UNREGISTERED && !la_info[from].ts)
1173 dev_info("Logical address %d (%s) discovered.\n", from, cec_la2s(from));
1174 if (show_msgs) {
1175 printf(" %s to %s (%d to %d): ",
1176 cec_la2s(from), to == 0xf ? "all" : cec_la2s(to), from, to);
1177 cec_log_msg(&msg);
1178 if (show_info)
1179 printf("\tSequence: %u Rx Timestamp: %s\n",
1180 msg.sequence, ts2s(msg.rx_ts, wallclock).c_str());
1181 }
1182 if (node->adap_la_mask)
1183 processMsg(node, msg, me, type);
1184 }
1185
1186 __u8 pwr_state = current_power_state(node);
1187 if (node->cec_version >= CEC_OP_CEC_VERSION_2_0 &&
1188 last_pwr_state != pwr_state &&
1189 (time_to_stable > 2 || pwr_state < CEC_OP_POWER_STATUS_TO_ON)) {
1190 struct cec_msg msg;
1191
1192 cec_msg_init(&msg, me, CEC_LOG_ADDR_BROADCAST);
1193 cec_msg_report_power_status(&msg, pwr_state);
1194 transmit(node, &msg);
1195 last_pwr_state = pwr_state;
1196 }
1197
1198 if (node->state.toggle_power_status && cec_has_tv(1 << me) &&
1199 (time(nullptr) - last_pwr_status_toggle > node->state.toggle_power_status)) {
1200 last_pwr_status_toggle = time(nullptr);
1201 if (pwr_state & 1) // standby or to-standby
1202 exit_standby(node);
1203 else
1204 enter_standby(node);
1205 }
1206
1207 __u64 ts_now = get_ts();
1208 unsigned poll_la = ts_to_s(ts_now) % 16;
1209
1210 if (poll_la != me &&
1211 poll_la != last_poll_la && poll_la < 15 && la_info[poll_la].ts &&
1212 ts_to_ms(ts_now - la_info[poll_la].ts) > POLL_PERIOD) {
1213 struct cec_msg msg;
1214
1215 cec_msg_init(&msg, me, poll_la);
1216 transmit(node, &msg);
1217 if (msg.tx_status & CEC_TX_STATUS_NACK) {
1218 dev_info("Logical address %d stopped responding to polling message.\n", poll_la);
1219 memset(&la_info[poll_la], 0, sizeof(la_info[poll_la]));
1220 node->remote_la_mask &= ~(1 << poll_la);
1221 node->remote_phys_addr[poll_la] = CEC_PHYS_ADDR_INVALID;
1222 }
1223 }
1224 last_poll_la = poll_la;
1225
1226 unsigned ms_since_press = ts_to_ms(ts_now - node->state.rc_press_rx_ts);
1227
1228 if (ms_since_press > FOLLOWER_SAFETY_TIMEOUT) {
1229 if (node->state.rc_state == PRESS_HOLD)
1230 rc_press_hold_stop(&node->state);
1231 else if (node->state.rc_state == PRESS) {
1232 dev_info("Button timeout: %s\n", get_ui_cmd_string(node->state.rc_ui_cmd));
1233 node->state.rc_state = NOPRESS;
1234 }
1235 }
1236
1237 if (node->has_aud_rate)
1238 aud_rate_msg_interval_check(node, ts_now);
1239
1240 if (node->state.deck_skip_start && ts_now - node->state.deck_skip_start > MAX_DECK_SKIP_NS) {
1241 node->state.deck_skip_start = 0;
1242 update_deck_state(node, me, CEC_OP_DECK_INFO_PLAY);
1243 }
1244
1245 if (!programmed_timers.empty())
1246 update_programmed_timers(node);
1247 }
1248 mode = CEC_MODE_INITIATOR;
1249 doioctl(node, CEC_S_MODE, &mode);
1250 }