Path: blob/master/tools/testing/selftests/ir/ir_loopback.c
26285 views
// SPDX-License-Identifier: GPL-2.01// test ir decoder2//3// Copyright (C) 2018 Sean Young <[email protected]>45// When sending LIRC_MODE_SCANCODE, the IR will be encoded. rc-loopback6// will send this IR to the receiver side, where we try to read the decoded7// IR. Decoding happens in a separate kernel thread, so we will need to8// wait until that is scheduled, hence we use poll to check for read9// readiness.1011#include <linux/lirc.h>12#include <errno.h>13#include <stdio.h>14#include <stdlib.h>15#include <stdbool.h>16#include <string.h>17#include <unistd.h>18#include <poll.h>19#include <time.h>20#include <sys/types.h>21#include <sys/ioctl.h>22#include <dirent.h>23#include <sys/stat.h>24#include <fcntl.h>25#include "../kselftest.h"2627#define TEST_SCANCODES 1028#define SYSFS_PATH_MAX 25629#define DNAME_PATH_MAX 2563031/*32* Support ancient lirc.h which does not have these values. Can be removed33* once RHEL 8 is no longer a relevant testing platform.34*/35#if RC_PROTO_MAX < 2636#define RC_PROTO_RCMM12 2437#define RC_PROTO_RCMM24 2538#define RC_PROTO_RCMM32 2639#endif4041static const struct {42enum rc_proto proto;43const char *name;44unsigned int mask;45const char *decoder;46} protocols[] = {47{ RC_PROTO_RC5, "rc-5", 0x1f7f, "rc-5" },48{ RC_PROTO_RC5X_20, "rc-5x-20", 0x1f7f3f, "rc-5" },49{ RC_PROTO_RC5_SZ, "rc-5-sz", 0x2fff, "rc-5-sz" },50{ RC_PROTO_JVC, "jvc", 0xffff, "jvc" },51{ RC_PROTO_SONY12, "sony-12", 0x1f007f, "sony" },52{ RC_PROTO_SONY15, "sony-15", 0xff007f, "sony" },53{ RC_PROTO_SONY20, "sony-20", 0x1fff7f, "sony" },54{ RC_PROTO_NEC, "nec", 0xffff, "nec" },55{ RC_PROTO_NECX, "nec-x", 0xffffff, "nec" },56{ RC_PROTO_NEC32, "nec-32", 0xffffffff, "nec" },57{ RC_PROTO_SANYO, "sanyo", 0x1fffff, "sanyo" },58{ RC_PROTO_RC6_0, "rc-6-0", 0xffff, "rc-6" },59{ RC_PROTO_RC6_6A_20, "rc-6-6a-20", 0xfffff, "rc-6" },60{ RC_PROTO_RC6_6A_24, "rc-6-6a-24", 0xffffff, "rc-6" },61{ RC_PROTO_RC6_6A_32, "rc-6-6a-32", 0xffffffff, "rc-6" },62{ RC_PROTO_RC6_MCE, "rc-6-mce", 0x00007fff, "rc-6" },63{ RC_PROTO_SHARP, "sharp", 0x1fff, "sharp" },64{ RC_PROTO_IMON, "imon", 0x7fffffff, "imon" },65{ RC_PROTO_RCMM12, "rcmm-12", 0x00000fff, "rc-mm" },66{ RC_PROTO_RCMM24, "rcmm-24", 0x00ffffff, "rc-mm" },67{ RC_PROTO_RCMM32, "rcmm-32", 0xffffffff, "rc-mm" },68};6970int lirc_open(const char *rc)71{72struct dirent *dent;73char buf[SYSFS_PATH_MAX + DNAME_PATH_MAX];74DIR *d;75int fd;7677snprintf(buf, sizeof(buf), "/sys/class/rc/%s", rc);7879d = opendir(buf);80if (!d)81ksft_exit_fail_msg("cannot open %s: %m\n", buf);8283while ((dent = readdir(d)) != NULL) {84if (!strncmp(dent->d_name, "lirc", 4)) {85snprintf(buf, sizeof(buf), "/dev/%s", dent->d_name);86break;87}88}8990if (!dent)91ksft_exit_skip("cannot find lirc device for %s\n", rc);9293closedir(d);9495fd = open(buf, O_RDWR | O_NONBLOCK);96if (fd == -1)97ksft_exit_fail_msg("cannot open: %s: %m\n", buf);9899return fd;100}101102int main(int argc, char **argv)103{104unsigned int mode;105char buf[100];106int rlircfd, wlircfd, protocolfd, i, n;107108srand(time(NULL));109110if (argc != 3)111ksft_exit_fail_msg("Usage: %s <write rcN> <read rcN>\n",112argv[0]);113114rlircfd = lirc_open(argv[2]);115mode = LIRC_MODE_SCANCODE;116if (ioctl(rlircfd, LIRC_SET_REC_MODE, &mode))117ksft_exit_fail_msg("failed to set scancode rec mode %s: %m\n",118argv[2]);119120wlircfd = lirc_open(argv[1]);121if (ioctl(wlircfd, LIRC_SET_SEND_MODE, &mode))122ksft_exit_fail_msg("failed to set scancode send mode %s: %m\n",123argv[1]);124125snprintf(buf, sizeof(buf), "/sys/class/rc/%s/protocols", argv[2]);126protocolfd = open(buf, O_WRONLY);127if (protocolfd == -1)128ksft_exit_fail_msg("failed to open %s: %m\n", buf);129130printf("Sending IR on %s and receiving IR on %s.\n", argv[1], argv[2]);131132for (i = 0; i < ARRAY_SIZE(protocols); i++) {133if (write(protocolfd, protocols[i].decoder,134strlen(protocols[i].decoder)) == -1)135ksft_exit_fail_msg("failed to set write decoder\n");136137printf("Testing protocol %s for decoder %s (%d/%d)...\n",138protocols[i].name, protocols[i].decoder,139i + 1, (int)ARRAY_SIZE(protocols));140141for (n = 0; n < TEST_SCANCODES; n++) {142unsigned int scancode = rand() & protocols[i].mask;143unsigned int rc_proto = protocols[i].proto;144145if (rc_proto == RC_PROTO_RC6_MCE)146scancode |= 0x800f0000;147148if (rc_proto == RC_PROTO_NECX &&149(((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)150continue;151152if (rc_proto == RC_PROTO_NEC32 &&153(((scancode >> 8) ^ ~scancode) & 0xff) == 0)154continue;155156if (rc_proto == RC_PROTO_RCMM32 &&157(scancode & 0x000c0000) != 0x000c0000 &&158scancode & 0x00008000)159continue;160161struct lirc_scancode lsc = {162.rc_proto = rc_proto,163.scancode = scancode164};165166printf("Testing scancode:%x\n", scancode);167168while (write(wlircfd, &lsc, sizeof(lsc)) < 0) {169if (errno == EINTR)170continue;171172ksft_exit_fail_msg("failed to send ir: %m\n");173}174175struct pollfd pfd = { .fd = rlircfd, .events = POLLIN };176struct lirc_scancode lsc2;177178poll(&pfd, 1, 1000);179180bool decoded = true;181182while (read(rlircfd, &lsc2, sizeof(lsc2)) < 0) {183if (errno == EINTR)184continue;185186ksft_test_result_error("no scancode decoded: %m\n");187decoded = false;188break;189}190191if (!decoded)192continue;193194if (lsc.rc_proto != lsc2.rc_proto)195ksft_test_result_error("decoded protocol is different: %d\n",196lsc2.rc_proto);197198else if (lsc.scancode != lsc2.scancode)199ksft_test_result_error("decoded scancode is different: %llx\n",200lsc2.scancode);201else202ksft_inc_pass_cnt();203}204205printf("OK\n");206}207208close(rlircfd);209close(wlircfd);210close(protocolfd);211212if (ksft_get_fail_cnt() > 0)213ksft_exit_fail();214else215ksft_exit_pass();216217return 0;218}219220221