Debian Codegraph @

utils/cec-compliance/cec-test-adapter.cpp

raw
1 // SPDX-License-Identifier: GPL-2.0-only
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 <fcntl.h>
11 #include <sys/ioctl.h>
12 #include <sys/select.h>
13 #include <unistd.h>
14
15 #include "cec-compliance.h"
16
17 static constexpr __u8 tx_ok_retry_mask = CEC_TX_STATUS_OK | CEC_TX_STATUS_MAX_RETRIES;
18
19 // Flush any pending messages
20 static int flush_pending_msgs(struct node *node)
21 {
22 int res;
23
24 do {
25 struct cec_msg msg;
26
27 memset(&msg, 0xff, sizeof(msg));
28 msg.timeout = 1;
29 res = doioctl(node, CEC_RECEIVE, &msg);
30 fail_on_test(res && res != ETIMEDOUT);
31 } while (res == 0);
32 return 0;
33 }
34
35 static int testCap(struct node *node)
36 {
37 struct cec_caps caps;
38
39 memset(&caps, 0xff, sizeof(caps));
40 fail_on_test(doioctl(node, CEC_ADAP_G_CAPS, nullptr) != EFAULT);
41 fail_on_test(doioctl(node, CEC_ADAP_G_CAPS, &caps));
42 fail_on_test(caps.available_log_addrs == 0 ||
43 caps.available_log_addrs > CEC_MAX_LOG_ADDRS);
44 fail_on_test((caps.capabilities & CEC_CAP_PASSTHROUGH) &&
45 !(caps.capabilities & CEC_CAP_TRANSMIT));
46 return 0;
47 }
48
49 static int testInvalidIoctls(struct node *node)
50 {
51 const char type = 'a';
52 unsigned ioc = _IOC(_IOC_NONE, type, 0xff, 0);
53 unsigned char buf[0x4000] = {};
54
55 fail_on_test(doioctl(node, ioc, nullptr) != ENOTTY);
56 ioc = _IOC(_IOC_NONE, type, 0, 0x3fff);
57 fail_on_test(doioctl(node, ioc, nullptr) != ENOTTY);
58 ioc = _IOC(_IOC_READ, type, 0, 0x3fff);
59 fail_on_test(doioctl(node, ioc, buf) != ENOTTY);
60 fail_on_test(check_0(buf, sizeof(buf)));
61 ioc = _IOC(_IOC_WRITE, type, 0, 0x3fff);
62 fail_on_test(doioctl(node, ioc, buf) != ENOTTY);
63 fail_on_test(check_0(buf, sizeof(buf)));
64 ioc = _IOC(_IOC_READ | _IOC_WRITE, type, 0, 0x3fff);
65 fail_on_test(doioctl(node, ioc, buf) != ENOTTY);
66 fail_on_test(check_0(buf, sizeof(buf)));
67 return 0;
68 }
69
70 static int testDQEvent(struct node *node)
71 {
72 struct cec_event ev;
73
74 memset(&ev, 0xff, sizeof(ev));
75 fail_on_test(doioctl(node, CEC_DQEVENT, &ev));
76 fail_on_test(!(ev.flags & CEC_EVENT_FL_INITIAL_STATE));
77 fail_on_test(ev.flags & ~CEC_EVENT_FL_INITIAL_STATE);
78 fail_on_test(ev.ts == 0 || ev.ts == ~0ULL);
79 fail_on_test(ev.event != CEC_EVENT_STATE_CHANGE);
80 fail_on_test(ev.state_change.log_addr_mask == 0xffff);
81 memset(&ev.state_change, 0, sizeof(ev.state_change));
82 fail_on_test(check_0(ev.raw, sizeof(ev.raw)));
83 return 0;
84 }
85
86 static int testAdapPhysAddr(struct node *node)
87 {
88 __u16 old_pa = 0xefff;
89 __u16 pa = 0x1000;
90
91 fail_on_test(doioctl(node, CEC_ADAP_G_PHYS_ADDR, &old_pa));
92 fail_on_test(old_pa == 0xefff);
93 if (node->caps & CEC_CAP_PHYS_ADDR) {
94 fail_on_test(doioctl(node, CEC_ADAP_S_PHYS_ADDR, &pa));
95 fail_on_test(doioctl(node, CEC_ADAP_G_PHYS_ADDR, &pa));
96 fail_on_test(pa != 0x1000);
97
98 fail_on_test(doioctl(node, CEC_ADAP_S_PHYS_ADDR, &old_pa));
99 fail_on_test(doioctl(node, CEC_ADAP_G_PHYS_ADDR, &pa));
100 fail_on_test(pa != old_pa);
101 } else {
102 fail_on_test(doioctl(node, CEC_ADAP_S_PHYS_ADDR, &pa) != ENOTTY);
103 }
104 return 0;
105 }
106
107 static int testAdapLogAddrs(struct node *node)
108 {
109 static constexpr __u8 la_types[] = {
110 CEC_LOG_ADDR_TYPE_TV,
111 CEC_LOG_ADDR_TYPE_RECORD,
112 CEC_LOG_ADDR_TYPE_TUNER,
113 CEC_LOG_ADDR_TYPE_AUDIOSYSTEM
114 };
115 static constexpr __u8 prim_dev_types[] = {
116 CEC_OP_PRIM_DEVTYPE_TV,
117 CEC_OP_PRIM_DEVTYPE_RECORD,
118 CEC_OP_PRIM_DEVTYPE_TUNER,
119 CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM
120 };
121 static constexpr __u8 all_dev_types[2] = {
122 CEC_OP_ALL_DEVTYPE_TV | CEC_OP_ALL_DEVTYPE_RECORD |
123 CEC_OP_ALL_DEVTYPE_AUDIOSYSTEM,
124 CEC_OP_ALL_DEVTYPE_RECORD | CEC_OP_ALL_DEVTYPE_AUDIOSYSTEM,
125 };
126 static constexpr __u8 features[12] = {
127 0x90, 0x00, 0x8e, 0x00,
128 0xff, 0xff, 0xff, 0xff,
129 0xff, 0xff, 0xff, 0xff
130 };
131 struct cec_log_addrs clear = { };
132 struct cec_log_addrs laddrs;
133 struct cec_event ev;
134 int res;
135
136 memset(&laddrs, 0xff, sizeof(laddrs));
137 fail_on_test(doioctl(node, CEC_ADAP_G_LOG_ADDRS, &laddrs));
138 fail_on_test(laddrs.vendor_id != CEC_VENDOR_ID_NONE &&
139 (laddrs.vendor_id & 0xff000000));
140 fail_on_test(laddrs.cec_version != CEC_OP_CEC_VERSION_1_4 &&
141 laddrs.cec_version != CEC_OP_CEC_VERSION_2_0);
142 fail_on_test(laddrs.num_log_addrs > CEC_MAX_LOG_ADDRS);
143
144 if (node->phys_addr == CEC_PHYS_ADDR_INVALID || laddrs.num_log_addrs == 0)
145 fail_on_test(laddrs.log_addr_mask);
146 else
147 fail_on_test(!laddrs.log_addr_mask);
148
149 if (!(node->caps & CEC_CAP_LOG_ADDRS)) {
150 fail_on_test(!laddrs.num_log_addrs);
151 fail_on_test(doioctl(node, CEC_ADAP_S_LOG_ADDRS, &laddrs) != ENOTTY);
152 return 0;
153 }
154
155 fail_on_test(doioctl(node, CEC_ADAP_S_LOG_ADDRS, &clear));
156 fail_on_test(clear.num_log_addrs != 0);
157 fail_on_test(clear.log_addr_mask != 0);
158 fail_on_test(doioctl(node, CEC_ADAP_G_LOG_ADDRS, &laddrs));
159 fail_on_test(laddrs.num_log_addrs != 0);
160 fail_on_test(laddrs.log_addr_mask != 0);
161
162 __u16 pa;
163
164 fail_on_test(doioctl(node, CEC_ADAP_G_PHYS_ADDR, &pa));
165 fail_on_test(pa != node->phys_addr);
166 unsigned skip_tv = pa ? 1 : 0;
167 unsigned available_log_addrs = node->available_log_addrs;
168
169 if (skip_tv && available_log_addrs == CEC_MAX_LOG_ADDRS)
170 available_log_addrs--;
171 memset(&laddrs, 0, sizeof(laddrs));
172 strcpy(laddrs.osd_name, "Compliance");
173 laddrs.num_log_addrs = available_log_addrs;
174 laddrs.cec_version = laddrs.num_log_addrs > 2 ?
175 CEC_OP_CEC_VERSION_1_4: CEC_OP_CEC_VERSION_2_0;
176
177 for (unsigned i = 0; i < CEC_MAX_LOG_ADDRS - skip_tv; i++) {
178 laddrs.log_addr_type[i] = la_types[i + skip_tv];
179 laddrs.primary_device_type[i] = prim_dev_types[i + skip_tv];
180 laddrs.all_device_types[i] = all_dev_types[skip_tv];
181 memcpy(laddrs.features[i], features, sizeof(features));
182 }
183
184 fail_on_test(doioctl(node, CEC_ADAP_S_LOG_ADDRS, &laddrs));
185 fail_on_test(laddrs.num_log_addrs != available_log_addrs);
186 fail_on_test(laddrs.log_addr_mask == 0);
187 for (unsigned i = 0; i < laddrs.num_log_addrs; i++) {
188 fail_on_test(laddrs.log_addr[i] == CEC_LOG_ADDR_INVALID);
189 fail_on_test(memcmp(laddrs.features[i], features, 4));
190 fail_on_test(check_0(laddrs.features[i] + 4, 8));
191 }
192 for (unsigned i = laddrs.num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
193 fail_on_test(laddrs.log_addr_type[i]);
194 fail_on_test(laddrs.primary_device_type[i]);
195 fail_on_test(laddrs.all_device_types[i]);
196 fail_on_test(check_0(laddrs.features[i], sizeof(laddrs.features[i])));
197 }
198 fail_on_test(doioctl(node, CEC_ADAP_S_LOG_ADDRS, &laddrs) != EBUSY);
199
200 fcntl(node->fd, F_SETFL, fcntl(node->fd, F_GETFL) | O_NONBLOCK);
201 do {
202 res = doioctl(node, CEC_DQEVENT, &ev);
203 fail_on_test(res && res != EAGAIN);
204 if (!res) {
205 struct timeval tv = { 0, 10000 }; // 10 ms
206
207 switch (ev.event) {
208 case CEC_EVENT_STATE_CHANGE:
209 fail_on_test(ev.flags & CEC_EVENT_FL_INITIAL_STATE);
210 break;
211 case CEC_EVENT_PIN_CEC_LOW:
212 case CEC_EVENT_PIN_CEC_HIGH:
213 case CEC_EVENT_PIN_HPD_LOW:
214 case CEC_EVENT_PIN_HPD_HIGH:
215 case CEC_EVENT_PIN_5V_LOW:
216 case CEC_EVENT_PIN_5V_HIGH:
217 fail_on_test(!(ev.flags & CEC_EVENT_FL_INITIAL_STATE));
218 break;
219 case CEC_EVENT_LOST_MSGS:
220 fail("Unexpected event %d\n", ev.event);
221 break;
222 default:
223 fail("Unknown event %d\n", ev.event);
224 break;
225 }
226 select(0, nullptr, nullptr, nullptr, &tv);
227 }
228 } while (!res);
229 fail_on_test(doioctl(node, CEC_ADAP_S_LOG_ADDRS, &clear));
230 do {
231 struct timeval tv = { 0, 10000 }; // 10 ms
232
233 res = doioctl(node, CEC_DQEVENT, &ev);
234 fail_on_test(res && res != EAGAIN);
235 if (res)
236 select(0, nullptr, nullptr, nullptr, &tv);
237 } while (res);
238 fail_on_test(ev.flags & CEC_EVENT_FL_INITIAL_STATE);
239 fail_on_test(ev.ts == 0);
240 fail_on_test(ev.event != CEC_EVENT_STATE_CHANGE);
241 fail_on_test(ev.state_change.phys_addr != node->phys_addr);
242 fail_on_test(ev.state_change.log_addr_mask);
243
244 fail_on_test(doioctl(node, CEC_ADAP_S_LOG_ADDRS, &laddrs));
245 do {
246 struct timeval tv = { 0, 10000 }; // 10 ms
247
248 res = doioctl(node, CEC_DQEVENT, &ev);
249 fail_on_test(res && res != EAGAIN);
250 if (res)
251 select(0, nullptr, nullptr, nullptr, &tv);
252 } while (res);
253 fail_on_test(ev.flags & CEC_EVENT_FL_INITIAL_STATE);
254 fail_on_test(ev.ts == 0);
255 fail_on_test(ev.event != CEC_EVENT_STATE_CHANGE);
256 fail_on_test(ev.state_change.phys_addr != node->phys_addr);
257 fail_on_test(ev.state_change.log_addr_mask == 0);
258 return 0;
259 }
260
261 static int testTransmit(struct node *node)
262 {
263 struct cec_msg msg;
264 unsigned i, la = node->log_addr[0];
265 unsigned valid_la = 15, invalid_la = 15;
266 bool tested_self = false;
267 bool tested_valid_la = false;
268 bool tested_invalid_la = false;
269 bool has_reply_vendor_id = node->caps & CEC_CAP_REPLY_VENDOR_ID;
270
271 if (!(node->caps & CEC_CAP_TRANSMIT)) {
272 cec_msg_init(&msg, la, 0);
273 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg) != ENOTTY);
274 return OK_NOT_SUPPORTED;
275 }
276
277 /* Check invalid messages */
278 cec_msg_init(&msg, la, 0xf);
279 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg) != EINVAL);
280
281 cec_msg_init(&msg, la, 0);
282 msg.timeout = 1000;
283 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg) != EINVAL);
284
285 cec_msg_init(&msg, la, 0);
286 msg.len = 0;
287 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg) != EINVAL);
288
289 cec_msg_init(&msg, la, 0);
290 msg.len = CEC_MAX_MSG_SIZE + 1;
291 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg) != EINVAL);
292
293 cec_msg_init(&msg, la, 0);
294 msg.reply = CEC_MSG_CEC_VERSION;
295 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg) != EINVAL);
296
297 if (has_reply_vendor_id) {
298 // Test that CEC_MSG_FL_REPLY_VENDOR_ID requires a message
299 // size of at least 6 by constructing a message of length 5
300 // and verifying that that fails with EINVAL.
301 cec_msg_init(&msg, la, 0);
302 __u8 cmd = 0;
303 cec_msg_vendor_command_with_id(&msg, node->vendor_id, 0, &cmd);
304 msg.flags = CEC_MSG_FL_REPLY_VENDOR_ID;
305 msg.reply = cmd + 1;
306 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg) != EINVAL);
307 fail_on_test(!(msg.flags & CEC_MSG_FL_REPLY_VENDOR_ID));
308 }
309
310 for (i = 0; i < 15; i++) {
311 if (tested_self && (node->adap_la_mask & (1 << i)))
312 continue;
313
314 memset(&msg, 0xff, sizeof(msg));
315 msg.msg[0] = 0xf0 | i;
316 msg.len = 1;
317 msg.timeout = 0;
318 msg.reply = 0;
319 msg.flags &= ~node->msg_fl_mask;
320
321 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
322
323 fail_on_test(msg.len != 1);
324 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
325 fail_on_test(msg.timeout);
326 fail_on_test(msg.reply);
327 fail_on_test(msg.flags);
328 fail_on_test(msg.rx_status);
329 fail_on_test(msg.rx_ts);
330 fail_on_test(msg.tx_status == 0);
331 fail_on_test(msg.tx_ts == 0);
332 fail_on_test(msg.tx_ts == ~0ULL);
333 fail_on_test(msg.msg[0] != (0xf0 | i));
334 fail_on_test(msg.sequence == 0);
335 fail_on_test(msg.sequence == ~0U);
336 fail_on_test(msg.tx_status & ~0x3f);
337 fail_on_test((msg.tx_status & tx_ok_retry_mask) == tx_ok_retry_mask);
338 fail_on_test(!(msg.tx_status & tx_ok_retry_mask));
339
340 if (node->adap_la_mask & (1 << i)) {
341 // Send message to yourself
342 fail_on_test(msg.tx_status !=
343 (CEC_TX_STATUS_NACK | CEC_TX_STATUS_MAX_RETRIES));
344 fail_on_test(msg.tx_nack_cnt != 1);
345 fail_on_test(msg.tx_arb_lost_cnt);
346 fail_on_test(msg.tx_low_drive_cnt);
347 fail_on_test(msg.tx_error_cnt);
348
349 cec_msg_init(&msg, i, i);
350 cec_msg_give_physical_addr(&msg, true);
351 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg) != EINVAL);
352 } else if (msg.tx_status & CEC_TX_STATUS_OK) {
353 if (tested_valid_la)
354 continue;
355 tested_valid_la = true;
356 valid_la = i;
357 // Send message to a remote LA
358 memset(&msg, 0xff, sizeof(msg));
359 msg.msg[0] = (la << 4) | i;
360 msg.timeout = 1001;
361 msg.flags &= ~node->msg_fl_mask;
362 cec_msg_give_physical_addr(&msg, true);
363 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
364 fail_on_test(!(msg.tx_status & CEC_TX_STATUS_OK));
365 fail_on_test(msg.rx_status & CEC_RX_STATUS_TIMEOUT);
366 fail_on_test(!(msg.rx_status & CEC_RX_STATUS_OK));
367 fail_on_test(msg.len != 5);
368 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
369 fail_on_test(msg.timeout != 1001);
370 fail_on_test(msg.sequence == 0 || msg.sequence == ~0U);
371 fail_on_test(msg.reply != CEC_MSG_REPORT_PHYSICAL_ADDR);
372 fail_on_test(msg.rx_ts == 0);
373 fail_on_test(msg.rx_ts == ~0ULL);
374 fail_on_test(msg.flags);
375 fail_on_test(msg.rx_status == 0);
376 fail_on_test(msg.rx_status & ~0x07);
377 fail_on_test(msg.tx_ts == 0);
378 fail_on_test(msg.tx_ts == ~0ULL);
379 fail_on_test(msg.rx_ts <= msg.tx_ts);
380 fail_on_test((msg.tx_status & tx_ok_retry_mask) == tx_ok_retry_mask);
381 fail_on_test(msg.tx_nack_cnt == 0xff);
382 fail_on_test(msg.tx_arb_lost_cnt == 0xff);
383 fail_on_test(msg.tx_low_drive_cnt == 0xff);
384 fail_on_test(msg.tx_error_cnt == 0xff);
385
386 memset(&msg, 0xff, sizeof(msg));
387 msg.msg[0] = (la << 4) | i;
388 msg.timeout = 0;
389 msg.flags &= ~node->msg_fl_mask;
390 cec_msg_give_physical_addr(&msg, false);
391 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
392 fail_on_test(msg.timeout);
393 fail_on_test(msg.sequence == 0 || msg.sequence == ~0U);
394 fail_on_test(msg.reply);
395 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
396 fail_on_test(msg.rx_status);
397 fail_on_test(msg.rx_ts);
398 fail_on_test(msg.flags);
399 fail_on_test(msg.tx_ts == 0);
400 fail_on_test(msg.tx_ts == ~0ULL);
401 fail_on_test(!(msg.tx_status & CEC_TX_STATUS_OK));
402 fail_on_test((msg.tx_status & tx_ok_retry_mask) == tx_ok_retry_mask);
403 fail_on_test(msg.tx_nack_cnt == 0xff);
404 fail_on_test(msg.tx_arb_lost_cnt == 0xff);
405 fail_on_test(msg.tx_low_drive_cnt == 0xff);
406 fail_on_test(msg.tx_error_cnt == 0xff);
407
408 // CEC_MSG_FL_REPLY_VENDOR_ID tests, only valid for use with
409 // the vivid driver since that has support for this.
410 //
411 // The vivid driver will Feature Abort the vendor message if
412 // it has a payload size != 1.
413 //
414 // It will ignore messages with an even payload byte, and
415 // it will reply to messages with an odd payload byte with
416 // that payload byte incremented by 1.
417 if (node->is_vivid && has_reply_vendor_id) {
418 __u32 vendor_id;
419 __u8 size;
420 const __u8 *vendor_data;
421 __u8 vendor_cmd = 0x11;
422
423 // Test that a broadcast of an invalid vendor ID is ignored
424 cec_msg_init(&msg, la, CEC_LOG_ADDR_BROADCAST);
425 cec_msg_vendor_command_with_id(&msg, node->vendor_id + 1, 1, &vendor_cmd);
426 msg.flags = CEC_MSG_FL_REPLY_VENDOR_ID;
427 msg.reply = vendor_cmd + 2;
428 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
429 fail_on_test(!(msg.rx_status & CEC_RX_STATUS_TIMEOUT));
430 fail_on_test(!(msg.flags & CEC_MSG_FL_REPLY_VENDOR_ID));
431
432 // Test that an invalid vendor ID is feature aborted
433 cec_msg_init(&msg, la, i);
434 cec_msg_vendor_command_with_id(&msg, node->vendor_id + 1, 1, &vendor_cmd);
435 msg.flags = CEC_MSG_FL_REPLY_VENDOR_ID;
436 msg.reply = vendor_cmd + 2;
437 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
438 fail_on_test(!(msg.rx_status & CEC_RX_STATUS_FEATURE_ABORT));
439 fail_on_test(!(msg.flags & CEC_MSG_FL_REPLY_VENDOR_ID));
440
441 // The vivid driver will reply with value vendor_cmd + 1, so
442 // waiting for different reply must time out
443 cec_msg_init(&msg, la, i);
444 cec_msg_vendor_command_with_id(&msg, node->vendor_id, 1, &vendor_cmd);
445 msg.flags = CEC_MSG_FL_REPLY_VENDOR_ID;
446 msg.reply = vendor_cmd + 2;
447 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
448 fail_on_test(!(msg.rx_status & CEC_RX_STATUS_TIMEOUT));
449 fail_on_test(!(msg.flags & CEC_MSG_FL_REPLY_VENDOR_ID));
450
451 // This should work
452 cec_msg_init(&msg, la, i);
453 cec_msg_vendor_command_with_id(&msg, node->vendor_id, 1, &vendor_cmd);
454 msg.flags = CEC_MSG_FL_REPLY_VENDOR_ID;
455 msg.reply = vendor_cmd + 1;
456 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
457 fail_on_test(!(msg.rx_status & CEC_RX_STATUS_OK));
458 fail_on_test(!(msg.flags & CEC_MSG_FL_REPLY_VENDOR_ID));
459 cec_ops_vendor_command_with_id(&msg, &vendor_id, &size, &vendor_data);
460 fail_on_test(vendor_id != node->vendor_id);
461 fail_on_test(size != 1);
462 fail_on_test(vendor_data[0] != vendor_cmd + 1);
463
464 // This too: here the reply is 0 (0xff + 1 % 256)
465 cec_msg_init(&msg, la, i);
466 vendor_cmd = 0xff;
467 cec_msg_vendor_command_with_id(&msg, node->vendor_id, 1, &vendor_cmd);
468 msg.flags = CEC_MSG_FL_REPLY_VENDOR_ID;
469 msg.reply = 0;
470 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
471 fail_on_test(!(msg.rx_status & CEC_RX_STATUS_OK));
472 fail_on_test(!(msg.flags & CEC_MSG_FL_REPLY_VENDOR_ID));
473 cec_ops_vendor_command_with_id(&msg, &vendor_id, &size, &vendor_data);
474 fail_on_test(vendor_id != node->vendor_id);
475 fail_on_test(size != 1);
476 fail_on_test(vendor_data[0]);
477
478 // A size != 1 should result in a feature abort
479 cec_msg_init(&msg, la, i);
480 vendor_cmd = 0xff;
481 cec_msg_vendor_command_with_id(&msg, node->vendor_id, 1, &vendor_cmd);
482 msg.len++;
483 msg.flags = CEC_MSG_FL_REPLY_VENDOR_ID;
484 msg.reply = 0;
485 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
486 fail_on_test(!(msg.rx_status & CEC_RX_STATUS_FEATURE_ABORT));
487 fail_on_test(msg.msg[3] != CEC_OP_ABORT_INVALID_OP);
488 }
489 } else {
490 if (tested_invalid_la)
491 continue;
492 tested_invalid_la = true;
493 invalid_la = i;
494 // Send message to a remote non-existent LA
495 memset(&msg, 0xff, sizeof(msg));
496 msg.msg[0] = (la << 4) | i;
497 msg.timeout = 1002;
498 msg.flags &= ~node->msg_fl_mask;
499 cec_msg_give_physical_addr(&msg, true);
500 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
501 fail_on_test(msg.timeout != 1002);
502 fail_on_test(msg.sequence == 0);
503 fail_on_test(msg.sequence == ~0U);
504 fail_on_test(msg.len != 2);
505 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
506 fail_on_test(msg.flags);
507 fail_on_test(msg.reply != CEC_MSG_REPORT_PHYSICAL_ADDR);
508 fail_on_test(!(msg.tx_status & CEC_TX_STATUS_MAX_RETRIES));
509 fail_on_test((msg.tx_status & tx_ok_retry_mask) == tx_ok_retry_mask);
510 fail_on_test(msg.rx_status);
511 fail_on_test(msg.rx_ts);
512 fail_on_test(msg.tx_ts == 0);
513 fail_on_test(msg.tx_ts == ~0ULL);
514 fail_on_test(msg.tx_nack_cnt == 0xff);
515 fail_on_test(msg.tx_arb_lost_cnt == 0xff);
516 fail_on_test(msg.tx_low_drive_cnt == 0xff);
517 fail_on_test(msg.tx_error_cnt == 0xff);
518
519 memset(&msg, 0xff, sizeof(msg));
520 msg.msg[0] = (la << 4) | i;
521 msg.timeout = 0;
522 msg.flags &= ~node->msg_fl_mask;
523 cec_msg_give_physical_addr(&msg, false);
524 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
525 fail_on_test(msg.timeout);
526 fail_on_test(msg.sequence == 0);
527 fail_on_test(msg.sequence == ~0U);
528 fail_on_test(msg.reply);
529 fail_on_test(msg.len != 2);
530 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
531 fail_on_test(msg.rx_status);
532 fail_on_test(msg.rx_ts);
533 fail_on_test(msg.flags);
534 fail_on_test(msg.tx_ts == 0);
535 fail_on_test(msg.tx_ts == ~0ULL);
536 fail_on_test(!(msg.tx_status & CEC_TX_STATUS_MAX_RETRIES));
537 fail_on_test((msg.tx_status & tx_ok_retry_mask) == tx_ok_retry_mask);
538 fail_on_test(msg.tx_nack_cnt == 0xff);
539 fail_on_test(msg.tx_arb_lost_cnt == 0xff);
540 fail_on_test(msg.tx_low_drive_cnt == 0xff);
541 fail_on_test(msg.tx_error_cnt == 0xff);
542 }
543 }
544
545 if (tested_valid_la) {
546 time_t cur_t = time(nullptr), t;
547 time_t last_t = cur_t + 7;
548 unsigned max_cnt = 0;
549 unsigned cnt = 0;
550
551 do {
552 t = time(nullptr);
553 if (t != cur_t) {
554 if (cnt > max_cnt)
555 max_cnt = cnt;
556 cnt = 0;
557 cur_t = t;
558 }
559 cec_msg_init(&msg, la, valid_la);
560 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
561 cnt++;
562 } while (t < last_t);
563 // A ping can take 10 * 2.4 + 4.5 + 7 * 2.4 = 45.3 ms (SFT)
564 if (max_cnt < 21)
565 warn("Could only do %u pings per second to a valid LA, expected at least 21\n",
566 max_cnt);
567 }
568
569 if (tested_invalid_la) {
570 time_t cur_t = time(nullptr), t;
571 time_t last_t = cur_t + 7;
572 unsigned max_cnt = 0;
573 unsigned cnt = 0;
574
575 do {
576 t = time(nullptr);
577 if (t != cur_t) {
578 if (cnt > max_cnt)
579 max_cnt = cnt;
580 cnt = 0;
581 cur_t = t;
582 }
583 cec_msg_init(&msg, la, invalid_la);
584 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
585 cnt++;
586 } while (t < last_t);
587 // A ping to an invalid LA can take 2 * (10 * 2.4 + 4.5) + (3 + 7) * 2.4 = 81 ms (SFT)
588 if (max_cnt < 12)
589 warn("Could only do %u pings per second to an invalid LA, expected at least 12\n",
590 max_cnt);
591 }
592
593 return 0;
594 }
595
596 static int testReceive(struct node *node)
597 {
598 unsigned la = node->log_addr[0], remote_la = 0;
599 struct cec_msg msg;
600
601 if (!(node->caps & CEC_CAP_TRANSMIT)) {
602 fail_on_test(doioctl(node, CEC_RECEIVE, &msg) != ENOTTY);
603 return OK_NOT_SUPPORTED;
604 }
605
606 for (unsigned i = 0; i < 15; i++) {
607 if (node->remote_la_mask & (1 << i))
608 break;
609 remote_la++;
610 }
611
612 fail_on_test(flush_pending_msgs(node));
613
614 if (remote_la == 15)
615 return OK_PRESUMED;
616
617 cec_msg_init(&msg, la, remote_la);
618 cec_msg_give_physical_addr(&msg, false);
619 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
620
621 msg.timeout = 1500;
622 fail_on_test(doioctl(node, CEC_RECEIVE, &msg) != ETIMEDOUT);
623 fail_on_test(msg.timeout != 1500);
624
625 __u32 mode = CEC_MODE_INITIATOR | CEC_MODE_FOLLOWER;
626
627 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
628
629 cec_msg_init(&msg, la, remote_la);
630 cec_msg_give_physical_addr(&msg, false);
631 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
632
633 memset(&msg, 0xff, sizeof(msg));
634 msg.timeout = 1500;
635 fail_on_test(doioctl(node, CEC_RECEIVE, &msg));
636
637 fail_on_test(msg.tx_ts);
638 fail_on_test(msg.tx_status);
639 fail_on_test(msg.tx_arb_lost_cnt);
640 fail_on_test(msg.tx_nack_cnt);
641 fail_on_test(msg.tx_low_drive_cnt);
642 fail_on_test(msg.tx_error_cnt);
643 fail_on_test(msg.timeout != 1500);
644 fail_on_test(msg.len != 5);
645 fail_on_test(msg.msg[1] != CEC_MSG_REPORT_PHYSICAL_ADDR);
646 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
647 fail_on_test(msg.msg[0] != ((remote_la << 4) | 0xf));
648 fail_on_test(msg.sequence);
649 fail_on_test(msg.rx_ts == 0);
650 fail_on_test(msg.flags);
651 fail_on_test(msg.reply);
652 fail_on_test(msg.rx_status != CEC_RX_STATUS_OK);
653
654 return 0;
655 }
656
657 static int testNonBlocking(struct node *node)
658 {
659 unsigned la = node->log_addr[0], remote_la = 0, invalid_remote = 0xf;
660 struct cec_msg msg;
661
662 if (!(node->caps & CEC_CAP_TRANSMIT))
663 return OK_NOT_SUPPORTED;
664
665 for (unsigned i = 0; i < 15; i++) {
666 if (node->remote_la_mask & (1 << i))
667 break;
668 if (invalid_remote == 0xf && !(node->adap_la_mask & (1 << i)))
669 invalid_remote = i;
670 remote_la++;
671 }
672
673 fail_on_test(flush_pending_msgs(node));
674
675 fcntl(node->fd, F_SETFL, fcntl(node->fd, F_GETFL) | O_NONBLOCK);
676
677 fail_on_test(doioctl(node, CEC_RECEIVE, &msg) != EAGAIN);
678
679 cec_msg_init(&msg, la, la);
680 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
681 fail_on_test(msg.tx_status != (CEC_TX_STATUS_NACK | CEC_TX_STATUS_MAX_RETRIES));
682
683 if (invalid_remote < 15) {
684 __u32 seq;
685
686 // Send non-blocking non-reply message to invalid remote LA
687 memset(&msg, 0xff, sizeof(msg));
688 msg.msg[0] = (la << 4) | invalid_remote;
689 msg.timeout = 0;
690 msg.flags &= ~node->msg_fl_mask;
691 cec_msg_give_physical_addr(&msg, false);
692 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
693 fail_on_test(msg.len != 2);
694 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
695 fail_on_test(msg.msg[0] != ((la << 4) | invalid_remote));
696 fail_on_test(msg.msg[1] != CEC_MSG_GIVE_PHYSICAL_ADDR);
697 fail_on_test(msg.timeout);
698 fail_on_test(msg.flags);
699 fail_on_test(msg.sequence == 0);
700 fail_on_test(msg.sequence == ~0U);
701 fail_on_test(msg.reply);
702 fail_on_test(msg.tx_status);
703 fail_on_test(msg.rx_ts);
704 fail_on_test(msg.rx_status);
705 fail_on_test(msg.tx_ts);
706 fail_on_test(msg.tx_nack_cnt);
707 fail_on_test(msg.tx_arb_lost_cnt);
708 fail_on_test(msg.tx_low_drive_cnt);
709 fail_on_test(msg.tx_error_cnt);
710 seq = msg.sequence;
711
712 sleep(1);
713 while (true) {
714 memset(&msg, 0xff, sizeof(msg));
715 msg.timeout = 1500;
716 fail_on_test(doioctl(node, CEC_RECEIVE, &msg));
717 if (msg.sequence == 0)
718 continue;
719 fail_on_test(msg.sequence != seq);
720 fail_on_test(msg.len != 2);
721 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
722 fail_on_test(msg.msg[0] != ((la << 4) | invalid_remote));
723 fail_on_test(msg.msg[1] != CEC_MSG_GIVE_PHYSICAL_ADDR);
724 fail_on_test(msg.timeout != 1500);
725 fail_on_test(msg.flags);
726 fail_on_test(msg.reply);
727 fail_on_test(!(msg.tx_status & CEC_TX_STATUS_MAX_RETRIES));
728 fail_on_test((msg.tx_status & tx_ok_retry_mask) == tx_ok_retry_mask);
729 fail_on_test(msg.tx_nack_cnt == 0xff);
730 fail_on_test(msg.tx_arb_lost_cnt == 0xff);
731 fail_on_test(msg.tx_low_drive_cnt == 0xff);
732 fail_on_test(msg.tx_error_cnt == 0xff);
733 fail_on_test(msg.rx_ts);
734 fail_on_test(msg.rx_status);
735 break;
736 }
737
738 // Send non-blocking reply message to invalid remote LA
739 memset(&msg, 0xff, sizeof(msg));
740 msg.msg[0] = (la << 4) | invalid_remote;
741 msg.timeout = 0;
742 msg.flags &= ~node->msg_fl_mask;
743 cec_msg_give_physical_addr(&msg, true);
744 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
745 fail_on_test(msg.len != 2);
746 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
747 fail_on_test(msg.msg[0] != ((la << 4) | invalid_remote));
748 fail_on_test(msg.msg[1] != CEC_MSG_GIVE_PHYSICAL_ADDR);
749 fail_on_test(msg.timeout != 1000);
750 fail_on_test(msg.flags);
751 fail_on_test(msg.sequence == 0);
752 fail_on_test(msg.sequence == ~0U);
753 fail_on_test(msg.reply != CEC_MSG_REPORT_PHYSICAL_ADDR);
754 fail_on_test(msg.tx_status);
755 fail_on_test(msg.rx_ts);
756 fail_on_test(msg.rx_status);
757 fail_on_test(msg.tx_ts);
758 fail_on_test(msg.tx_nack_cnt);
759 fail_on_test(msg.tx_arb_lost_cnt);
760 fail_on_test(msg.tx_low_drive_cnt);
761 fail_on_test(msg.tx_error_cnt);
762 seq = msg.sequence;
763
764 sleep(1);
765 while (true) {
766 memset(&msg, 0xff, sizeof(msg));
767 msg.timeout = 1500;
768 fail_on_test(doioctl(node, CEC_RECEIVE, &msg));
769 if (msg.sequence == 0)
770 continue;
771 fail_on_test(msg.sequence != seq);
772 fail_on_test(msg.len != 2);
773 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
774 fail_on_test(msg.msg[0] != ((la << 4) | invalid_remote));
775 fail_on_test(msg.msg[1] != CEC_MSG_GIVE_PHYSICAL_ADDR);
776 fail_on_test(msg.timeout != 1500);
777 fail_on_test(msg.flags);
778 fail_on_test(msg.reply != CEC_MSG_REPORT_PHYSICAL_ADDR);
779 fail_on_test(!(msg.tx_status & CEC_TX_STATUS_MAX_RETRIES));
780 fail_on_test((msg.tx_status & tx_ok_retry_mask) == tx_ok_retry_mask);
781 fail_on_test(msg.tx_nack_cnt == 0xff);
782 fail_on_test(msg.tx_arb_lost_cnt == 0xff);
783 fail_on_test(msg.tx_low_drive_cnt == 0xff);
784 fail_on_test(msg.tx_error_cnt == 0xff);
785 fail_on_test(msg.rx_ts);
786 fail_on_test(msg.rx_status);
787 break;
788 }
789
790 __u32 mode = CEC_MODE_INITIATOR | CEC_MODE_FOLLOWER;
791
792 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
793 }
794 if (remote_la < 15) {
795 __u32 seq;
796
797 // Send non-blocking non-reply message to valid remote LA
798 memset(&msg, 0xff, sizeof(msg));
799 msg.msg[0] = (la << 4) | remote_la;
800 msg.timeout = 0;
801 msg.flags &= ~node->msg_fl_mask;
802 cec_msg_give_physical_addr(&msg, false);
803 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
804 fail_on_test(msg.len != 2);
805 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
806 fail_on_test(msg.msg[0] != ((la << 4) | remote_la));
807 fail_on_test(msg.msg[1] != CEC_MSG_GIVE_PHYSICAL_ADDR);
808 fail_on_test(msg.timeout);
809 fail_on_test(msg.flags);
810 fail_on_test(msg.sequence == 0);
811 fail_on_test(msg.sequence == ~0U);
812 fail_on_test(msg.reply);
813 fail_on_test(msg.tx_status);
814 fail_on_test(msg.rx_ts);
815 fail_on_test(msg.rx_status);
816 fail_on_test(msg.tx_ts);
817 fail_on_test(msg.tx_nack_cnt);
818 fail_on_test(msg.tx_arb_lost_cnt);
819 fail_on_test(msg.tx_low_drive_cnt);
820 fail_on_test(msg.tx_error_cnt);
821 seq = msg.sequence;
822
823 sleep(1);
824 while (true) {
825 memset(&msg, 0xff, sizeof(msg));
826 msg.timeout = 1500;
827 fail_on_test(doioctl(node, CEC_RECEIVE, &msg));
828 if (msg.sequence == 0)
829 continue;
830 fail_on_test(msg.sequence != seq);
831 fail_on_test(msg.len != 2);
832 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
833 fail_on_test(msg.msg[0] != ((la << 4) | remote_la));
834 fail_on_test(msg.msg[1] != CEC_MSG_GIVE_PHYSICAL_ADDR);
835 fail_on_test(msg.timeout != 1500);
836 fail_on_test(msg.flags);
837 fail_on_test(msg.reply);
838 fail_on_test(!(msg.tx_status & CEC_TX_STATUS_OK));
839 fail_on_test((msg.tx_status & tx_ok_retry_mask) == tx_ok_retry_mask);
840 fail_on_test(msg.tx_nack_cnt == 0xff);
841 fail_on_test(msg.tx_arb_lost_cnt == 0xff);
842 fail_on_test(msg.tx_low_drive_cnt == 0xff);
843 fail_on_test(msg.tx_error_cnt == 0xff);
844 fail_on_test(msg.rx_ts || msg.rx_status);
845 break;
846 }
847
848 // Send non-blocking reply message to valid remote LA
849 memset(&msg, 0xff, sizeof(msg));
850 msg.msg[0] = (la << 4) | remote_la;
851 msg.timeout = 0;
852 msg.flags &= ~node->msg_fl_mask;
853 cec_msg_give_physical_addr(&msg, true);
854 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
855 fail_on_test(msg.len != 2);
856 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
857 fail_on_test(msg.msg[0] != ((la << 4) | remote_la));
858 fail_on_test(msg.msg[1] != CEC_MSG_GIVE_PHYSICAL_ADDR);
859 fail_on_test(msg.timeout != 1000);
860 fail_on_test(msg.flags);
861 fail_on_test(msg.sequence == 0);
862 fail_on_test(msg.sequence == ~0U);
863 fail_on_test(msg.reply != CEC_MSG_REPORT_PHYSICAL_ADDR);
864 fail_on_test(msg.tx_status);
865 fail_on_test(msg.rx_ts);
866 fail_on_test(msg.rx_status);
867 fail_on_test(msg.tx_ts);
868 fail_on_test(msg.tx_nack_cnt);
869 fail_on_test(msg.tx_arb_lost_cnt);
870 fail_on_test(msg.tx_low_drive_cnt);
871 fail_on_test(msg.tx_error_cnt);
872 seq = msg.sequence;
873
874 sleep(1);
875 while (true) {
876 memset(&msg, 0xff, sizeof(msg));
877 msg.timeout = 1500;
878 fail_on_test(doioctl(node, CEC_RECEIVE, &msg));
879 if (msg.sequence == 0)
880 continue;
881 fail_on_test(msg.sequence != seq);
882 fail_on_test(msg.len != 5);
883 fail_on_test(check_0(msg.msg + msg.len, sizeof(msg.msg) - msg.len));
884 fail_on_test(msg.msg[0] != ((remote_la << 4) | 0xf));
885 fail_on_test(msg.msg[1] != CEC_MSG_REPORT_PHYSICAL_ADDR);
886 fail_on_test(msg.timeout != 1500);
887 fail_on_test(msg.flags);
888 fail_on_test(msg.reply != CEC_MSG_REPORT_PHYSICAL_ADDR);
889 fail_on_test(!(msg.tx_status & CEC_TX_STATUS_OK));
890 fail_on_test((msg.tx_status & tx_ok_retry_mask) == tx_ok_retry_mask);
891 fail_on_test(msg.tx_nack_cnt == 0xff);
892 fail_on_test(msg.tx_arb_lost_cnt == 0xff);
893 fail_on_test(msg.tx_low_drive_cnt == 0xff);
894 fail_on_test(msg.tx_error_cnt == 0xff);
895 fail_on_test(msg.rx_ts == 0);
896 fail_on_test(msg.rx_ts == ~0ULL);
897 fail_on_test(msg.rx_status != CEC_RX_STATUS_OK);
898 fail_on_test(msg.rx_ts < msg.tx_ts);
899 break;
900 }
901
902 __u32 mode = CEC_MODE_INITIATOR | CEC_MODE_FOLLOWER;
903
904 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
905 }
906 if (remote_la == 15 || invalid_remote == 15)
907 return OK_PRESUMED;
908 return 0;
909 }
910
911 static int testModes(struct node *node, struct node *node2)
912 {
913 struct cec_msg msg;
914 __u8 me = node->log_addr[0];
915 __u8 remote = CEC_LOG_ADDR_INVALID;
916 __u32 mode = 0, m;
917
918 for (unsigned i = 0; i < 15; i++) {
919 if (node->remote_la_mask & (1 << i)) {
920 remote = i;
921 break;
922 }
923 }
924
925 cec_msg_init(&msg, me, 0);
926 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
927 fail_on_test(doioctl(node2, CEC_TRANSMIT, &msg));
928
929 fail_on_test(doioctl(node, CEC_G_MODE, &m));
930 fail_on_test(m != CEC_MODE_INITIATOR);
931 fail_on_test(doioctl(node2, CEC_G_MODE, &m));
932 fail_on_test(m != CEC_MODE_INITIATOR);
933
934 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
935 fail_on_test(doioctl(node, CEC_G_MODE, &m));
936 fail_on_test(m != mode);
937 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg) != EBUSY);
938 fail_on_test(doioctl(node2, CEC_TRANSMIT, &msg));
939
940 mode = CEC_MODE_INITIATOR;
941 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
942 fail_on_test(doioctl(node, CEC_G_MODE, &m));
943 fail_on_test(m != mode);
944 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
945
946 mode = CEC_MODE_EXCL_INITIATOR;
947 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
948 fail_on_test(doioctl(node, CEC_G_MODE, &m));
949 fail_on_test(m != mode);
950 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
951 fail_on_test(doioctl(node2, CEC_TRANSMIT, &msg) != EBUSY);
952
953 mode = CEC_MODE_INITIATOR;
954 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
955 fail_on_test(doioctl(node, CEC_G_MODE, &m));
956 fail_on_test(m != mode);
957
958 if (remote == CEC_LOG_ADDR_INVALID)
959 return 0;
960
961 fail_on_test(flush_pending_msgs(node));
962 cec_msg_init(&msg, me, remote);
963 cec_msg_give_physical_addr(&msg, false);
964 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
965 msg.timeout = 1200;
966 fail_on_test(doioctl(node, CEC_RECEIVE, &msg) != ETIMEDOUT);
967
968 mode = CEC_MODE_INITIATOR | CEC_MODE_FOLLOWER;
969 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
970 fail_on_test(doioctl(node, CEC_G_MODE, &m));
971 fail_on_test(m != mode);
972
973 fail_on_test(flush_pending_msgs(node));
974 cec_msg_init(&msg, me, remote);
975 cec_msg_give_physical_addr(&msg, false);
976 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
977 msg.timeout = 1200;
978 fail_on_test(doioctl(node, CEC_RECEIVE, &msg));
979 fail_on_test(msg.msg[1] != CEC_MSG_REPORT_PHYSICAL_ADDR);
980
981 mode = CEC_MODE_INITIATOR | CEC_MODE_FOLLOWER;
982 fail_on_test(doioctl(node2, CEC_S_MODE, &mode));
983 fail_on_test(doioctl(node2, CEC_G_MODE, &m));
984 fail_on_test(m != mode);
985
986 fail_on_test(flush_pending_msgs(node));
987 fail_on_test(flush_pending_msgs(node2));
988 cec_msg_init(&msg, me, remote);
989 cec_msg_give_physical_addr(&msg, false);
990 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
991 msg.timeout = 1200;
992 fail_on_test(doioctl(node, CEC_RECEIVE, &msg));
993 fail_on_test(msg.msg[1] != CEC_MSG_REPORT_PHYSICAL_ADDR);
994 msg.timeout = 1;
995 fail_on_test(doioctl(node2, CEC_RECEIVE, &msg));
996 fail_on_test(msg.msg[1] != CEC_MSG_REPORT_PHYSICAL_ADDR);
997
998 mode = CEC_MODE_INITIATOR | CEC_MODE_EXCL_FOLLOWER;
999 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
1000 fail_on_test(doioctl(node, CEC_G_MODE, &m));
1001 fail_on_test(m != mode);
1002
1003 fail_on_test(flush_pending_msgs(node));
1004 fail_on_test(flush_pending_msgs(node2));
1005 cec_msg_init(&msg, me, remote);
1006 cec_msg_give_physical_addr(&msg, false);
1007 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
1008 msg.timeout = 1200;
1009 fail_on_test(doioctl(node, CEC_RECEIVE, &msg));
1010 fail_on_test(msg.msg[1] != CEC_MSG_REPORT_PHYSICAL_ADDR);
1011 msg.timeout = 1;
1012 fail_on_test(doioctl(node2, CEC_RECEIVE, &msg) != ETIMEDOUT);
1013
1014 mode = CEC_MODE_INITIATOR | CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
1015 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
1016 fail_on_test(doioctl(node, CEC_G_MODE, &m));
1017 fail_on_test(m != mode);
1018
1019 // Note: this test is the same as for CEC_MODE_EXCL_FOLLOWER.
1020 // Ideally the remote device would send us a passthrough message,
1021 // but there is no way to trigger that.
1022 fail_on_test(flush_pending_msgs(node));
1023 fail_on_test(flush_pending_msgs(node2));
1024 cec_msg_init(&msg, me, remote);
1025 cec_msg_give_physical_addr(&msg, false);
1026 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
1027 msg.timeout = 1200;
1028 fail_on_test(doioctl(node, CEC_RECEIVE, &msg));
1029 fail_on_test(msg.msg[1] != CEC_MSG_REPORT_PHYSICAL_ADDR);
1030 msg.timeout = 1;
1031 fail_on_test(doioctl(node2, CEC_RECEIVE, &msg) != ETIMEDOUT);
1032
1033 mode = CEC_MODE_INITIATOR;
1034 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
1035 fail_on_test(doioctl(node, CEC_G_MODE, &m));
1036 fail_on_test(m != mode);
1037
1038 mode = CEC_MODE_INITIATOR | CEC_MODE_MONITOR;
1039 fail_on_test(doioctl(node2, CEC_S_MODE, &mode) != EINVAL);
1040 fail_on_test(doioctl(node2, CEC_G_MODE, &m));
1041 fail_on_test(m != (CEC_MODE_INITIATOR | CEC_MODE_FOLLOWER));
1042
1043 bool is_root = geteuid() == 0;
1044
1045 mode = CEC_MODE_MONITOR;
1046 fail_on_test(doioctl(node2, CEC_S_MODE, &mode) != (is_root ? 0 : EPERM));
1047
1048 if (is_root) {
1049 fail_on_test(doioctl(node2, CEC_G_MODE, &m));
1050 fail_on_test(m != mode);
1051 fail_on_test(flush_pending_msgs(node2));
1052 cec_msg_init(&msg, me, remote);
1053 cec_msg_give_physical_addr(&msg, false);
1054 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
1055 msg.timeout = 1200;
1056 fail_on_test(doioctl(node2, CEC_RECEIVE, &msg));
1057 fail_on_test(msg.msg[1] != CEC_MSG_GIVE_PHYSICAL_ADDR);
1058 while (true) {
1059 fail_on_test(doioctl(node2, CEC_RECEIVE, &msg));
1060 if (msg.msg[1] == CEC_MSG_REPORT_PHYSICAL_ADDR)
1061 break;
1062 fail_on_test(!cec_msg_is_broadcast(&msg) &&
1063 !(node2->adap_la_mask & (1 << cec_msg_destination(&msg))));
1064 }
1065 }
1066
1067 mode = CEC_MODE_MONITOR_ALL;
1068 int res = doioctl(node2, CEC_S_MODE, &mode);
1069 if (node2->caps & CEC_CAP_MONITOR_ALL)
1070 fail_on_test(res != (is_root ? 0 : EPERM));
1071 else
1072 fail_on_test(res != EINVAL);
1073
1074 if (is_root && (node2->caps & CEC_CAP_MONITOR_ALL)) {
1075 fail_on_test(doioctl(node2, CEC_G_MODE, &m));
1076 fail_on_test(m != mode);
1077 fail_on_test(flush_pending_msgs(node2));
1078 cec_msg_init(&msg, me, remote);
1079 cec_msg_give_physical_addr(&msg, false);
1080 fail_on_test(doioctl(node, CEC_TRANSMIT, &msg));
1081 msg.timeout = 1200;
1082 fail_on_test(doioctl(node2, CEC_RECEIVE, &msg));
1083 fail_on_test(msg.msg[1] != CEC_MSG_GIVE_PHYSICAL_ADDR);
1084 while (true) {
1085 fail_on_test(doioctl(node2, CEC_RECEIVE, &msg));
1086 if (msg.msg[1] == CEC_MSG_REPORT_PHYSICAL_ADDR)
1087 break;
1088 }
1089 }
1090 return 0;
1091 }
1092
1093 static void print_sfts(unsigned sft[12], const char *descr)
1094 {
1095 std::string s;
1096 char num[20];
1097
1098 for (unsigned i = 0; i < 12; i++) {
1099 if (sft[i] == 0)
1100 continue;
1101 if (!s.empty())
1102 s += ", ";
1103 sprintf(num, "%u: %d", i, sft[i]);
1104 s += num;
1105 }
1106 if (!s.empty())
1107 printf("\t\t%s: %s\n", descr, s.c_str());
1108 }
1109
1110 // testLostMsgs() checks if the CEC_EVENT_LOST_MSGS event works. But it
1111 // actually tests a lot more: it also checks Signal Free Time behavior
1112 // and if the speed of the CEC bus is as expected.
1113
1114 // Some defines dealing with SFTs (from include/media/cec.h):
1115 // Correct Signal Free times are:
1116 #define CEC_SIGNAL_FREE_TIME_RETRY 3
1117 #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
1118 #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
1119 // but for measuring we support up to 11:
1120 #define CEC_SIGNAL_FREE_TIME_MAX 11
1121
1122 // Two defines (copied from include/media/cec.h) that give the maximum
1123 // number of CEC messages that can be queued up in the transmit and
1124 // receive queues (this is per filehandle):
1125 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
1126 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
1127
1128 static int testLostMsgs(struct node *node)
1129 {
1130 struct cec_msg msg;
1131 struct cec_event ev;
1132 __u8 me = node->log_addr[0];
1133 __u8 remote = CEC_LOG_ADDR_INVALID;
1134 __u32 mode = CEC_MODE_INITIATOR | CEC_MODE_FOLLOWER;
1135 // Store counts of detected SFTs: the first dimension
1136 // is whether it is a repeating initiator (1) or not (0).
1137 // The second dimension is whether it is a transmit (1)
1138 // or not (0).
1139 // The third dimension is a count of the corresponding SFT
1140 // (0-CEC_SIGNAL_FREE_TIME_MAX).
1141 unsigned sft[2][2][CEC_SIGNAL_FREE_TIME_MAX + 1] = {};
1142
1143 // Find the first available remote LA to use for this test
1144 for (unsigned i = 0; i < 15; i++) {
1145 if (node->remote_la_mask & (1 << i)) {
1146 remote = i;
1147 break;
1148 }
1149 }
1150
1151 fail_on_test(flush_pending_msgs(node));
1152 // We now switch to non-blocking mode.
1153 // This will cause CEC_TRANSMIT to return at once, and when the
1154 // transmit is done it will appear in the receive queue.
1155 fcntl(node->fd, F_SETFL, fcntl(node->fd, F_GETFL) | O_NONBLOCK);
1156
1157 // Become follower for this test, otherwise all the replies to
1158 // the GET_CEC_VERSION message would have been 'Feature Abort'ed,
1159 // unless some other follower was running. By becoming a follower
1160 // all the CEC_VERSION replies are just ignored.
1161 fail_on_test(doioctl(node, CEC_S_MODE, &mode));
1162 cec_msg_init(&msg, me, remote);
1163 cec_msg_get_cec_version(&msg, false);
1164
1165 // flush pending events
1166 while (doioctl(node, CEC_DQEVENT, &ev) == 0) { }
1167
1168 bool got_busy = false;
1169 unsigned xfer_cnt = 0;
1170 unsigned tx_queue_depth = 0;
1171 unsigned tx_first_seq = 0;
1172 unsigned rx_first_seq = 0;
1173
1174 // Transmit <Get CEC Version> until an event was queued up.
1175 // We never dequeue received messages, so once the receive
1176 // queue is full the LOST_MSGS event is queued by the CEC
1177 // core framework, at which point we stop.
1178 do {
1179 int res;
1180
1181 do {
1182 res = doioctl(node, CEC_TRANSMIT, &msg);
1183
1184 fail_on_test(res && res != EBUSY);
1185 if (!res) {
1186 if (!xfer_cnt)
1187 tx_first_seq = msg.sequence;
1188 xfer_cnt++;
1189 }
1190
1191 if (res == EBUSY) {
1192 // If the CEC device was busy (i.e. the transmit
1193 // queue was full), then wait 10 ms and try again.
1194 struct timeval tv = { 0, 10000 }; // 10 ms
1195
1196 select(0, nullptr, nullptr, nullptr, &tv);
1197 // Mark that we got a busy error
1198 got_busy = true;
1199 } else if (!got_busy) {
1200 // If we didn't get a EBUSY yet, then increment this
1201 // variable to detect the TX queue depth.
1202 tx_queue_depth++;
1203 }
1204 // Keep retrying as long as the transmit failed with EBUSY.
1205 } while (res == EBUSY);
1206 // Keep transmitting until an event was queued
1207 } while (doioctl(node, CEC_DQEVENT, &ev));
1208 // We expect the LOST_MSGS event due to a full RX queue.
1209 fail_on_test(ev.event != CEC_EVENT_LOST_MSGS);
1210 // Since the transmit queue is one-third of the receive queue,
1211 // it would be very weird if we never got an EBUSY error.
1212 fail_on_test(!got_busy);
1213
1214 /*
1215 * No more than max 18 transmits can be queued, but one message
1216 * might finish transmitting before the queue fills up, so
1217 * check for 19 instead.
1218 */
1219 fail_on_test(tx_queue_depth == 0 || tx_queue_depth > CEC_MAX_MSG_TX_QUEUE_SZ + 1);
1220
1221 if (show_info)
1222 printf("\n\t\tFinished transmitting\n\n");
1223
1224 unsigned pending_msgs = 0;
1225 unsigned pending_quick_msgs = 0;
1226 unsigned pending_tx_ok_msgs = 0;
1227 unsigned pending_tx_timed_out_msgs = 0;
1228 unsigned pending_tx_arb_lost_msgs = 0;
1229 unsigned pending_tx_nack_msgs = 0;
1230 unsigned pending_tx_low_drive_msgs = 0;
1231 unsigned pending_tx_error_msgs = 0;
1232 unsigned pending_tx_aborted_msgs = 0;
1233 unsigned pending_tx_rx_msgs = 0;
1234 unsigned pending_rx_msgs = 0;
1235 unsigned pending_rx_cec_version_msgs = 0;
1236 time_t start = time(nullptr);
1237 __u8 last_init = 0xff;
1238 __u64 last_ts = 0;
1239 unsigned tx_repeats = 0;
1240
1241 // Now we start reading from the receive queue and analyze the
1242 // timestamps. Remember that the receive queue contains both
1243 // the finished transmit messages from the local LA ('me') and
1244 // the received replies from the remote LA.
1245 //
1246 // We do the while-loop twice, the first time (i == 0) the file handle
1247 // is still in non-blocking mode and so we effectively just drain the
1248 // receive queue quickly. Then we switch to blocking mode (i == 1) and
1249 // keep receiving messages (waiting up to 3 seconds) until no more
1250 // messages are received.
1251 for (unsigned i = 0; i < 2; i++) {
1252 // note: timeout is ignored in non-blocking mode (i == 0)
1253 msg.timeout = 3000;
1254
1255 while (!doioctl(node, CEC_RECEIVE, &msg)) {
1256 __u64 ts = msg.tx_ts ? : msg.rx_ts;
1257 __u8 initiator = cec_msg_initiator(&msg);
1258 bool ok_or_nack = (msg.tx_status & (CEC_TX_STATUS_OK | CEC_TX_STATUS_NACK)) ||
1259 (msg.rx_status & CEC_RX_STATUS_OK);
1260
1261 if (last_ts && ok_or_nack) {
1262 __u64 delta = last_ts ? ts - last_ts : 0;
1263
1264 // The timestamp ts is when the transmit was done
1265 // or the message was received.
1266 // To get the time when the message started transmitting
1267 // we subtract the time spent transmitting the message.
1268 delta -= msg.len * 24000000ULL + 4500000ULL;
1269 //unsigned sft_real = (delta + 1200000ULL) / 2400000ULL;
1270 unsigned sft_real = delta / 2400000ULL;
1271
1272 // Count the detected SFT in the sft array.
1273 if (last_ts && sft_real <= CEC_SIGNAL_FREE_TIME_MAX)
1274 sft[last_init == initiator][initiator == me][sft_real]++;
1275
1276 if (!i && last_ts) {
1277 if (last_init == initiator && initiator == me) {
1278 tx_repeats++;
1279 } else {
1280 // Too many repeated transmits means that
1281 // the transmitter is hogging the bus, preventing
1282 // the remote LA to transmit the reply.
1283 if (tx_repeats > 2)
1284 warn("Too many transmits (%d) without receives\n",
1285 tx_repeats);
1286 tx_repeats = 0;
1287 }
1288 }
1289 }
1290 if (ok_or_nack) {
1291 last_ts = ts;
1292 last_init = initiator;
1293 }
1294
1295 // Count total number of received messages (both non-blocking
1296 // transmit results and actual received replies).
1297 pending_msgs++;
1298 // Count number of received msgs when quickly draining the
1299 // receive queue in the non-blocking phase.
1300 if (i == 0) {
1301 // The result of the first transmit will also be the
1302 // message that was lost since it was the oldest: check
1303 // that that's actually the case.
1304 if (!rx_first_seq && msg.sequence)
1305 rx_first_seq = msg.sequence;
1306 pending_quick_msgs++;
1307 }
1308 if (!msg.sequence) {
1309 // Count remote messages and the number of
1310 // received <CEC Version> messages.
1311 pending_rx_msgs++;
1312 if (msg.len == 3 && msg.msg[1] == CEC_MSG_CEC_VERSION)
1313 pending_rx_cec_version_msgs++;
1314 }
1315 else if (msg.tx_status & CEC_TX_STATUS_TIMEOUT)
1316 pending_tx_timed_out_msgs++;
1317 else if (msg.tx_status & CEC_TX_STATUS_ABORTED)
1318 pending_tx_aborted_msgs++;
1319 else if (msg.tx_status & CEC_TX_STATUS_OK) {
1320 pending_tx_ok_msgs++;
1321 if (msg.rx_status)
1322 pending_tx_rx_msgs++;
1323 }
1324 if (msg.tx_status & CEC_TX_STATUS_NACK)
1325 pending_tx_nack_msgs += msg.tx_nack_cnt;
1326 if (msg.tx_status & CEC_TX_STATUS_ARB_LOST)
1327 pending_tx_arb_lost_msgs += msg.tx_arb_lost_cnt;
1328 if (msg.tx_status & CEC_TX_STATUS_LOW_DRIVE)
1329 pending_tx_low_drive_msgs += msg.tx_low_drive_cnt;
1330 if (msg.tx_status & CEC_TX_STATUS_ERROR)
1331 pending_tx_error_msgs += msg.tx_error_cnt;
1332 }
1333 // Go back to blocking mode after draining the receive queue.
1334 fcntl(node->fd, F_SETFL, fcntl(node->fd, F_GETFL) & ~O_NONBLOCK);
1335 if (!i && show_info)
1336 printf("\n\t\tDrained receive queue\n\n");
1337 }
1338
1339 /*
1340 * Should be at least the size of the internal message queue and
1341 * close to the number of transmitted messages. There should also be
1342 * no timed out or aborted transmits.
1343 */
1344
1345 bool fail_msg = pending_tx_error_msgs || pending_tx_timed_out_msgs || pending_tx_aborted_msgs ||
1346 pending_tx_rx_msgs || pending_quick_msgs < CEC_MAX_MSG_RX_QUEUE_SZ ||
1347 pending_rx_cec_version_msgs > xfer_cnt;
1348 bool warn_msg = pending_rx_cec_version_msgs < xfer_cnt - 2;
1349
1350 if (fail_msg || warn_msg || show_info) {
1351 if (show_info)
1352 printf("\n");
1353 if (pending_tx_ok_msgs) {
1354 printf("\t\tSuccessful transmits: %d\n", pending_tx_ok_msgs);
1355 if (pending_tx_rx_msgs)
1356 printf("\t\t\tUnexpected replies: %d\n", pending_tx_rx_msgs);
1357 }
1358 if (pending_tx_nack_msgs)
1359 printf("\t\tNACKed transmits: %d\n", pending_tx_nack_msgs);
1360 if (pending_tx_timed_out_msgs)
1361 printf("\t\tTimed out transmits: %d\n", pending_tx_timed_out_msgs);
1362 if (pending_tx_aborted_msgs)
1363 printf("\t\tAborted transmits: %d\n", pending_tx_aborted_msgs);
1364 if (pending_tx_arb_lost_msgs)
1365 printf("\t\tArbitration Lost transmits: %d\n", pending_tx_arb_lost_msgs);
1366 if (pending_tx_low_drive_msgs)
1367 printf("\t\tLow Drive transmits: %d\n", pending_tx_low_drive_msgs);
1368 if (pending_tx_error_msgs)
1369 printf("\t\tError transmits: %d\n", pending_tx_error_msgs);
1370 if (pending_rx_msgs)
1371 printf("\t\tReceived messages: %d of which %d were CEC_MSG_CEC_VERSION\n",
1372 pending_rx_msgs, pending_rx_cec_version_msgs);
1373 if (pending_quick_msgs < pending_msgs)
1374 printf("\t\tReceived %d messages immediately, and %d over %llu seconds\n",
1375 pending_quick_msgs, pending_msgs - pending_quick_msgs,
1376 (__u64)time(nullptr) - start);
1377 }
1378 print_sfts(sft[1][1], "SFTs for repeating messages (>= 7)");
1379 print_sfts(sft[1][0], "SFTs for repeating remote messages (>= 7)");
1380 print_sfts(sft[0][1], "SFTs for newly transmitted messages (>= 5)");
1381 print_sfts(sft[0][0], "SFTs for newly transmitted remote messages (>= 5)");
1382 if (fail_msg)
1383 return fail("There were %d messages in the receive queue for %d transmits\n",
1384 pending_msgs, xfer_cnt);
1385 if (warn_msg)
1386 warn("There were %d CEC_GET_VERSION transmits but only %d CEC_VERSION receives\n",
1387 xfer_cnt, pending_rx_cec_version_msgs);
1388
1389 // Final check if everything else is OK: check that only the oldest
1390 // message (the result of the first transmit) was dropped in the
1391 // receive queue.
1392 if (!fail_msg && !warn_msg)
1393 warn_on_test(rx_first_seq != tx_first_seq + 1);
1394 return 0;
1395 }
1396
1397 void testAdapter(struct node &node, struct cec_log_addrs &laddrs,
1398 const char *device)
1399 {
1400 /* Required ioctls */
1401
1402 printf("\nCEC API:\n");
1403 printf("\tCEC_ADAP_G_CAPS: %s\n", ok(testCap(&node)));
1404 printf("\tInvalid ioctls: %s\n", ok(testInvalidIoctls(&node)));
1405 printf("\tCEC_DQEVENT: %s\n", ok(testDQEvent(&node)));
1406 printf("\tCEC_ADAP_G/S_PHYS_ADDR: %s\n", ok(testAdapPhysAddr(&node)));
1407 if (node.caps & CEC_CAP_PHYS_ADDR)
1408 doioctl(&node, CEC_ADAP_S_PHYS_ADDR, &node.phys_addr);
1409 if (node.phys_addr == CEC_PHYS_ADDR_INVALID) {
1410 fprintf(stderr, "FAIL: without a valid physical address this test cannot proceed.\n");
1411 fprintf(stderr, "Make sure that this CEC adapter is connected to another HDMI sink or source.\n");
1412 std::exit(EXIT_FAILURE);
1413 }
1414 printf("\tCEC_ADAP_G/S_LOG_ADDRS: %s\n", ok(testAdapLogAddrs(&node)));
1415 fcntl(node.fd, F_SETFL, fcntl(node.fd, F_GETFL) & ~O_NONBLOCK);
1416 sleep(1);
1417 if (node.caps & CEC_CAP_LOG_ADDRS) {
1418 struct cec_log_addrs clear = { };
1419
1420 doioctl(&node, CEC_ADAP_S_LOG_ADDRS, &clear);
1421 doioctl(&node, CEC_ADAP_S_LOG_ADDRS, &laddrs);
1422 }
1423 doioctl(&node, CEC_ADAP_G_LOG_ADDRS, &laddrs);
1424 if (laddrs.log_addr_mask != node.adap_la_mask)
1425 printf("\tNew Logical Address Mask : 0x%04x\n", laddrs.log_addr_mask);
1426 // The LAs may have changed after these tests, so update these node fields
1427 node.num_log_addrs = laddrs.num_log_addrs;
1428 memcpy(node.log_addr, laddrs.log_addr, laddrs.num_log_addrs);
1429 node.adap_la_mask = laddrs.log_addr_mask;
1430
1431 printf("\tCEC_TRANSMIT: %s\n", ok(testTransmit(&node)));
1432 printf("\tCEC_RECEIVE: %s\n", ok(testReceive(&node)));
1433 __u32 mode = CEC_MODE_INITIATOR;
1434 doioctl(&node, CEC_S_MODE, &mode);
1435 printf("\tCEC_TRANSMIT/RECEIVE (non-blocking): %s\n", ok(testNonBlocking(&node)));
1436 fcntl(node.fd, F_SETFL, fcntl(node.fd, F_GETFL) & ~O_NONBLOCK);
1437 doioctl(&node, CEC_S_MODE, &mode);
1438
1439 struct node node2 = node;
1440
1441 if ((node2.fd = open(device, O_RDWR)) < 0) {
1442 fprintf(stderr, "Failed to open %s: %s\n", device,
1443 strerror(errno));
1444 std::exit(EXIT_FAILURE);
1445 }
1446
1447 printf("\tCEC_G/S_MODE: %s\n", ok(testModes(&node, &node2)));
1448 close(node2.fd);
1449 doioctl(&node, CEC_S_MODE, &mode);
1450
1451 printf("\tCEC_EVENT_LOST_MSGS: %s\n", ok(testLostMsgs(&node)));
1452 fcntl(node.fd, F_SETFL, fcntl(node.fd, F_GETFL) & ~O_NONBLOCK);
1453 doioctl(&node, CEC_S_MODE, &mode);
1454 }