Debian Codegraph @

utils/cec-compliance/cec-compliance.h

raw
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * CEC API compliance test tool.
4 *
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 */
7
8 #ifndef _CEC_COMPLIANCE_H_
9 #define _CEC_COMPLIANCE_H_
10
11 #include <linux/cec-funcs.h>
12 #include "cec-htng-funcs.h"
13
14 #include <cec-info.h>
15
16 #include <vector>
17
18 #define TAG_AUDIO_RATE_CONTROL 1
19 #define TAG_ARC_CONTROL (1 << 1)
20 #define TAG_CAP_DISCOVERY_CONTROL (1 << 2)
21 #define TAG_DECK_CONTROL (1 << 3)
22 #define TAG_DEVICE_MENU_CONTROL (1 << 4)
23 #define TAG_DEVICE_OSD_TRANSFER (1 << 5)
24 #define TAG_DYNAMIC_AUTO_LIPSYNC (1 << 6)
25 #define TAG_OSD_DISPLAY (1 << 7)
26 #define TAG_ONE_TOUCH_PLAY (1 << 8)
27 #define TAG_ONE_TOUCH_RECORD (1 << 9)
28 #define TAG_POWER_STATUS (1 << 10)
29 #define TAG_REMOTE_CONTROL_PASSTHROUGH (1 << 11)
30 #define TAG_ROUTING_CONTROL (1 << 12)
31 #define TAG_SYSTEM_AUDIO_CONTROL (1 << 13)
32 #define TAG_SYSTEM_INFORMATION (1 << 14)
33 #define TAG_TIMER_PROGRAMMING (1 << 15)
34 #define TAG_TUNER_CONTROL (1 << 16)
35 #define TAG_VENDOR_SPECIFIC_COMMANDS (1 << 17)
36 #define TAG_STANDBY_RESUME (1 << 18)
37
38 #define TAG_INTERACTIVE (1 << 19)
39 #define TAG_CORE 0
40 #define TAG_ALL (~TAG_INTERACTIVE)
41
42 #define NUM_TAGS 21
43
44 struct short_audio_desc {
45 /* Byte 1 */
46 __u8 num_channels;
47 __u8 format_code;
48
49 /* Byte 2 */
50 __u8 sample_freq_mask;
51
52 /* Byte 3 */
53 union {
54 __u8 bit_depth_mask; // LPCM
55 __u8 max_bitrate; // Format codes 2-8
56 __u8 format_dependent; // Format codes 9-13
57 __u8 wma_profile; // WMA Pro
58 __u8 frame_length_mask; // Extension type codes 4-6, 8-10
59 };
60 __u8 mps; // Format codes 8-10
61 __u8 extension_type_code;
62 };
63
64 #define SAD_FMT_CODE_LPCM 1
65 #define SAD_FMT_CODE_AC3 2
66 #define SAD_FMT_CODE_MPEG1 3
67 #define SAD_FMT_CODE_MP3 4
68 #define SAD_FMT_CODE_MPEG2 5
69 #define SAD_FMT_CODE_AAC_LC 6
70 #define SAD_FMT_CODE_DTS 7
71 #define SAD_FMT_CODE_ATRAC 8
72 #define SAD_FMT_CODE_ONE_BIT_AUDIO 9
73 #define SAD_FMT_CODE_ENHANCED_AC3 10
74 #define SAD_FMT_CODE_DTS_HD 11
75 #define SAD_FMT_CODE_MAT 12
76 #define SAD_FMT_CODE_DST 13
77 #define SAD_FMT_CODE_WMA_PRO 14
78 #define SAD_FMT_CODE_EXTENDED 15
79
80 #define SAD_BIT_DEPTH_MASK_16 1
81 #define SAD_BIT_DEPTH_MASK_20 (1 << 1)
82 #define SAD_BIT_DEPTH_MASK_24 (1 << 2)
83
84 #define SAD_SAMPLE_FREQ_MASK_32 1
85 #define SAD_SAMPLE_FREQ_MASK_44_1 (1 << 1)
86 #define SAD_SAMPLE_FREQ_MASK_48 (1 << 2)
87 #define SAD_SAMPLE_FREQ_MASK_88_2 (1 << 3)
88 #define SAD_SAMPLE_FREQ_MASK_96 (1 << 4)
89 #define SAD_SAMPLE_FREQ_MASK_176_4 (1 << 5)
90 #define SAD_SAMPLE_FREQ_MASK_192 (1 << 6)
91
92 #define SAD_FRAME_LENGTH_MASK_960 1
93 #define SAD_FRAME_LENGTH_MASK_1024 (1 << 1)
94
95 #define SAD_EXT_TYPE_MPEG4_HE_AAC 4
96 #define SAD_EXT_TYPE_MPEG4_HE_AACv2 5
97 #define SAD_EXT_TYPE_MPEG4_AAC_LC 6
98 #define SAD_EXT_TYPE_DRA 7
99 #define SAD_EXT_TYPE_MPEG4_HE_AAC_SURROUND 8
100 #define SAD_EXT_TYPE_MPEG4_AAC_LC_SURROUND 10
101 #define SAD_EXT_TYPE_MPEG_H_3D_AUDIO 11
102 #define SAD_EXT_TYPE_AC_4 12
103 #define SAD_EXT_TYPE_LPCM_3D_AUDIO 13
104
105 extern bool show_info;
106 extern bool show_colors;
107 extern bool show_warnings;
108 extern bool exit_on_fail;
109 extern bool exit_on_warn;
110 extern unsigned warnings;
111 extern unsigned reply_threshold;
112 extern time_t long_timeout;
113
114 struct remote {
115 bool recognized_op[256];
116 bool unrecognized_op[256];
117 unsigned prim_type;
118 __u16 phys_addr;
119 char osd_name[15];
120 char language[4];
121 __u8 cec_version;
122 __u8 rc_profile;
123 __u8 dev_features;
124 __u8 all_device_types;
125 __u32 vendor_id;
126 bool has_osd;
127 bool has_power_status;
128 bool has_image_view_on;
129 bool has_text_view_on;
130 bool in_standby;
131 bool has_remote_control_passthrough;
132 bool source_has_arc_rx;
133 bool sink_has_arc_tx;
134 bool arc_initiated;
135 bool has_sys_audio_mode_req;
136 bool has_set_sys_audio_mode;
137 bool has_sad;
138 __u8 supp_format_id;
139 __u8 supp_format_code;
140 __u8 volume;
141 __u8 mute;
142 bool has_aud_rate;
143 bool has_deck_ctl;
144 bool has_analogue_timer;
145 __u8 bcast_sys;
146 __u8 dig_bcast_sys;
147 bool has_rec_tv;
148 bool has_cdc;
149 };
150
151 struct node {
152 int fd;
153 const char *device;
154 bool is_vivid;
155 bool has_cec20;
156 unsigned caps;
157 unsigned available_log_addrs;
158 unsigned num_log_addrs;
159 unsigned adap_la_mask;
160 __u8 log_addr[CEC_MAX_LOG_ADDRS];
161 __u32 msg_fl_mask;
162 unsigned remote_la_mask;
163 struct remote remote[16];
164 __u16 phys_addr;
165 __u32 vendor_id;
166 bool in_standby;
167 __u8 prim_devtype;
168 time_t current_time;
169 };
170
171 struct remote_subtest {
172 const char *name;
173 const __u16 la_mask;
174 int (*const test_fn)(struct node *node, unsigned me, unsigned la, bool interactive);
175 bool in_standby;
176 bool for_cec20;
177 };
178
179 using vec_remote_subtests = std::vector<remote_subtest>;
180
181 #ifndef __FILE_NAME__
182 #define __FILE_NAME__ __FILE__
183 #endif
184
185 #define OK 0
186 #define FAIL 1
187 #define OK_PRESUMED 2
188 #define OK_NOT_SUPPORTED 3
189 #define OK_REFUSED 4
190 #define OK_UNEXPECTED 5
191 #define OK_EXPECTED_FAIL 6
192 #define NOTAPPLICABLE 7
193 #define FAIL_CRITICAL 8
194 #define DONT_CARE 255
195
196 #define CEC_LOG_ADDR_MASK_ALL 0xffff
197
198 #define COLOR_GREEN(s) "\033[32m" s "\033[0m"
199 #define COLOR_RED(s) "\033[1;31m" s "\033[0m"
200 #define COLOR_BOLD(s) "\033[1m" s "\033[0m"
201
202 #define info(fmt, args...) \
203 do { \
204 if (show_info) { \
205 printf("\t\tinfo: " fmt, ##args); \
206 fflush(stdout); \
207 } \
208 } while (0)
209
210 #define announce(fmt, args...) \
211 do { \
212 printf("\t\t>>> " fmt "\n", ##args); \
213 fflush(stdout); \
214 } while (0)
215
216 #define interactive_info(block, fmt, args...) \
217 do { \
218 if (interactive) { \
219 printf("\t\t>>> " fmt "\n", ##args); \
220 if (block) { \
221 printf("\t\t>>> Press ENTER to proceed.\n"); \
222 getchar(); \
223 } \
224 } \
225 } while(0)
226
227 #define warn(fmt, args...) \
228 ({ \
229 warnings++; \
230 if (show_warnings) { \
231 printf("\t\%s: %s(%d): " fmt, \
232 show_colors ? COLOR_BOLD("warn") : "warn", \
233 __FILE_NAME__, __LINE__, ##args); \
234 fflush(stdout); \
235 } \
236 if (exit_on_warn) \
237 std::exit(EXIT_FAILURE); \
238 0; \
239 })
240
241 #define warn_once(fmt, args...) \
242 do { \
243 static bool show; \
244 \
245 if (!show) { \
246 show = true; \
247 warn(fmt, ##args); \
248 } \
249 } while (0)
250
251 #define warn_on_test(test) \
252 do { \
253 if (test) \
254 warn("%s\n", #test); \
255 } while (0)
256
257 #define warn_once_on_test(test) \
258 do { \
259 if (test) \
260 warn_once("%s\n", #test); \
261 } while (0)
262
263 #define fail(fmt, args...) \
264 ({ \
265 printf("\t\t%s: %s(%d): " fmt, show_colors ? \
266 COLOR_RED("fail") : "fail", __FILE_NAME__, __LINE__, ##args); \
267 fflush(stdout); \
268 if (exit_on_fail) \
269 std::exit(EXIT_FAILURE); \
270 FAIL; \
271 })
272
273 #define fail_once(fmt, args...) \
274 do { \
275 static bool show; \
276 \
277 if (!show) { \
278 show = true; \
279 fail(fmt, ##args); \
280 } \
281 } while (0)
282
283 #define fail_or_warn(node, fmt, args...) \
284 ({ \
285 if ((node)->in_standby) \
286 warn(fmt, ##args); \
287 else \
288 fail(fmt, ##args); \
289 (node)->in_standby ? 0 : FAIL; \
290 })
291
292 #define fail_on_test(test) \
293 do { \
294 if (test) \
295 return fail("%s\n", #test); \
296 } while (0)
297
298 #define fail_on_test_v2(version, test) fail_on_test(version >= CEC_OP_CEC_VERSION_2_0 && (test))
299
300 #define fail_on_test_v2_warn(version, test) \
301 do { \
302 if (test) { \
303 if (version >= CEC_OP_CEC_VERSION_2_0) \
304 return fail("%s\n", #test); \
305 else \
306 warn("fails in CEC 2.0: %s\n", #test); \
307 } \
308 } while(0)
309
310 static inline char get_yn()
311 {
312 char c;
313
314 while ((c = tolower(getchar())) != 'y' && c != 'n');
315 getchar();
316 return c;
317 }
318
319 static inline bool question(const char* prompt)
320 {
321 printf("\t\t>>> %s (y/n) ", prompt);
322 return get_yn() == 'y';
323 }
324
325 int cec_named_ioctl(struct node *node, const char *name,
326 unsigned long int request, void *parm);
327
328 #define doioctl(n, r, p) cec_named_ioctl(n, #r, r, p)
329
330 const char *opcode2s(__u8 opcode);
331 std::string opcode2s(const struct cec_msg *msg);
332
333 static inline bool is_tv(unsigned la, unsigned prim_type)
334 {
335 return cec_has_tv(1 << la) ||
336 (cec_has_specific(1 << la) && prim_type == CEC_OP_PRIM_DEVTYPE_TV);
337 }
338
339 static inline bool is_playback_or_rec(unsigned la)
340 {
341 return cec_has_playback(1 << la) || cec_has_record(1 << la);
342 }
343
344 static inline bool cec_msg_status_is_abort(const struct cec_msg *msg)
345 {
346 return msg->rx_status & CEC_RX_STATUS_FEATURE_ABORT;
347 }
348
349 static inline __u8 abort_reason(const struct cec_msg *msg)
350 {
351 return msg->msg[3];
352 }
353
354 static inline bool unrecognized_op(const struct cec_msg *msg)
355 {
356 if (!cec_msg_status_is_abort(msg))
357 return false;
358 if (abort_reason(msg) == CEC_OP_ABORT_UNRECOGNIZED_OP)
359 return true;
360 if (abort_reason(msg) == CEC_OP_ABORT_UNDETERMINED) {
361 warn("Opcode %x was undetermined and is treated as not supported.\n", msg->msg[2]);
362 return true;
363 }
364 return false;
365 }
366
367 static inline bool refused(const struct cec_msg *msg)
368 {
369 return cec_msg_status_is_abort(msg) && abort_reason(msg) == CEC_OP_ABORT_REFUSED;
370 }
371
372 static inline bool incorrect_mode(const struct cec_msg *msg)
373 {
374 return cec_msg_status_is_abort(msg) && abort_reason(msg) == CEC_OP_ABORT_INCORRECT_MODE;
375 }
376
377 static inline bool timed_out(const struct cec_msg *msg)
378 {
379 return msg->rx_status & CEC_RX_STATUS_TIMEOUT;
380 }
381
382 static inline bool timed_out_or_abort(const struct cec_msg *msg)
383 {
384 return timed_out(msg) || cec_msg_status_is_abort(msg);
385 }
386
387 static inline unsigned response_time_ms(const struct cec_msg *msg)
388 {
389 unsigned ms = (msg->rx_ts - msg->tx_ts) / 1000000;
390
391 // Compensate for the time it took (approx.) to receive the
392 // message.
393 if (ms >= msg->len * 24)
394 return ms - msg->len * 24;
395 return 0;
396 }
397
398 bool transmit_timeout(struct node *node, struct cec_msg *msg,
399 unsigned timeout = 2000);
400
401 static inline bool transmit(struct node *node, struct cec_msg *msg)
402 {
403 return transmit_timeout(node, msg, 0);
404 }
405
406 static inline void mode_set_follower(struct node *node)
407 {
408 __u32 mode = CEC_MODE_INITIATOR | CEC_MODE_FOLLOWER;
409
410 doioctl(node, CEC_S_MODE, &mode);
411 }
412
413 static inline void mode_set_initiator(struct node *node)
414 {
415 __u32 mode = CEC_MODE_INITIATOR;
416
417 doioctl(node, CEC_S_MODE, &mode);
418 }
419
420 static inline unsigned get_ts_ms()
421 {
422 struct timespec timespec;
423
424 clock_gettime(CLOCK_MONOTONIC, &timespec);
425 return timespec.tv_sec * 1000ull + timespec.tv_nsec / 1000000;
426 }
427
428 const char *result_name(int res, bool show_colors);
429 const char *ok(int res);
430 const char *power_status2s(__u8 power_status);
431 const char *bcast_system2s(__u8 bcast_system);
432 const char *dig_bcast_system2s(__u8 bcast_system);
433 int check_0(const void *p, int len);
434 int util_receive(struct node *node, unsigned la, unsigned timeout,
435 struct cec_msg *msg, __u8 sent_msg,
436 __u8 reply1, __u8 reply2 = 0);
437 std::string safename(const char *name);
438 std::string current_ts();
439
440 // CEC adapter tests
441 void testAdapter(struct node &node, struct cec_log_addrs &laddrs,
442 const char *device);
443
444 // CEC fuzzing test
445 int testFuzzing(struct node &node, unsigned me, unsigned la);
446
447 // CEC core tests
448 int testCore(struct node *node);
449 int core_unknown(struct node *node, unsigned me, unsigned la, bool interactive);
450 int core_abort(struct node *node, unsigned me, unsigned la, bool interactive);
451 int system_info_polling(struct node *node, unsigned me, unsigned la, bool interactive);
452 int system_info_phys_addr(struct node *node, unsigned me, unsigned la, bool interactive);
453 int system_info_version(struct node *node, unsigned me, unsigned la, bool interactive);
454 int system_info_get_menu_lang(struct node *node, unsigned me, unsigned la, bool interactive);
455 int system_info_give_features(struct node *node, unsigned me, unsigned la, bool interactive);
456 int vendor_specific_commands_id(struct node *node, unsigned me, unsigned la, bool interactive);
457 int device_osd_transfer_give(struct node *node, unsigned me, unsigned la, bool interactive);
458
459 // CEC processing
460 int testProcessing(struct node *node, unsigned me);
461
462 // CEC testing
463 void collectTests(void);
464 void listTests(void);
465 int setExpectedResult(char *optarg, bool no_warnings);
466 void testRemote(struct node *node, unsigned me, unsigned la, unsigned test_tags,
467 bool interactive, bool show_ts);
468
469 // cec-test-tuner-record-timer.cpp
470 extern const vec_remote_subtests tuner_ctl_subtests;
471 extern const vec_remote_subtests one_touch_rec_subtests;
472 extern const vec_remote_subtests timer_prog_subtests;
473
474 // cec-test-audio.cpp
475 extern const vec_remote_subtests sac_subtests;
476 extern const vec_remote_subtests dal_subtests;
477 extern const vec_remote_subtests arc_subtests;
478 extern const vec_remote_subtests audio_rate_ctl_subtests;
479
480 // cec-test-power.cpp
481 bool util_interactive_ensure_power_state(struct node *node, unsigned me, unsigned la, bool interactive,
482 __u8 target_pwr);
483 int standby_resume_wakeup(struct node *node, unsigned me, unsigned la, bool interactive);
484 extern const vec_remote_subtests standby_subtests;
485 extern const vec_remote_subtests one_touch_play_subtests;
486 extern const vec_remote_subtests power_status_subtests;
487 extern const vec_remote_subtests standby_resume_subtests;
488
489 #endif