1 // SPDX-License-Identifier: GPL-2.0 2 // test ir decoder 3 // 4 // Copyright (C) 2018 Sean Young <sean@mess.org> 5 6 // When sending LIRC_MODE_SCANCODE, the IR will be encoded. rc-loopback 7 // will send this IR to the receiver side, where we try to read the decoded 8 // IR. Decoding happens in a separate kernel thread, so we will need to 9 // wait until that is scheduled, hence we use poll to check for read 10 // readiness. 11 12 #include <linux/lirc.h> 13 #include <errno.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <stdbool.h> 17 #include <string.h> 18 #include <unistd.h> 19 #include <poll.h> 20 #include <time.h> 21 #include <sys/types.h> 22 #include <sys/ioctl.h> 23 #include <dirent.h> 24 #include <sys/stat.h> 25 #include <fcntl.h> 26 #include "../kselftest.h" 27 28 #define TEST_SCANCODES 10 29 #define SYSFS_PATH_MAX 256 30 #define DNAME_PATH_MAX 256 31 32 static const struct { 33 enum rc_proto proto; 34 const char *name; 35 unsigned int mask; 36 const char *decoder; 37 } protocols[] = { 38 { RC_PROTO_RC5, "rc-5", 0x1f7f, "rc-5" }, 39 { RC_PROTO_RC5X_20, "rc-5x-20", 0x1f7f3f, "rc-5" }, 40 { RC_PROTO_RC5_SZ, "rc-5-sz", 0x2fff, "rc-5-sz" }, 41 { RC_PROTO_JVC, "jvc", 0xffff, "jvc" }, 42 { RC_PROTO_SONY12, "sony-12", 0x1f007f, "sony" }, 43 { RC_PROTO_SONY15, "sony-15", 0xff007f, "sony" }, 44 { RC_PROTO_SONY20, "sony-20", 0x1fff7f, "sony" }, 45 { RC_PROTO_NEC, "nec", 0xffff, "nec" }, 46 { RC_PROTO_NECX, "nec-x", 0xffffff, "nec" }, 47 { RC_PROTO_NEC32, "nec-32", 0xffffffff, "nec" }, 48 { RC_PROTO_SANYO, "sanyo", 0x1fffff, "sanyo" }, 49 { RC_PROTO_RC6_0, "rc-6-0", 0xffff, "rc-6" }, 50 { RC_PROTO_RC6_6A_20, "rc-6-6a-20", 0xfffff, "rc-6" }, 51 { RC_PROTO_RC6_6A_24, "rc-6-6a-24", 0xffffff, "rc-6" }, 52 { RC_PROTO_RC6_6A_32, "rc-6-6a-32", 0xffffffff, "rc-6" }, 53 { RC_PROTO_RC6_MCE, "rc-6-mce", 0x00007fff, "rc-6" }, 54 { RC_PROTO_SHARP, "sharp", 0x1fff, "sharp" }, 55 { RC_PROTO_IMON, "imon", 0x7fffffff, "imon" }, 56 { RC_PROTO_RCMM12, "rcmm-12", 0x00000fff, "rc-mm" }, 57 { RC_PROTO_RCMM24, "rcmm-24", 0x00ffffff, "rc-mm" }, 58 { RC_PROTO_RCMM32, "rcmm-32", 0xffffffff, "rc-mm" }, 59 }; 60 61 int lirc_open(const char *rc) 62 { 63 struct dirent *dent; 64 char buf[SYSFS_PATH_MAX + DNAME_PATH_MAX]; 65 DIR *d; 66 int fd; 67 68 snprintf(buf, sizeof(buf), "/sys/class/rc/%s", rc); 69 70 d = opendir(buf); 71 if (!d) 72 ksft_exit_fail_msg("cannot open %s: %m\n", buf); 73 74 while ((dent = readdir(d)) != NULL) { 75 if (!strncmp(dent->d_name, "lirc", 4)) { 76 snprintf(buf, sizeof(buf), "/dev/%s", dent->d_name); 77 break; 78 } 79 } 80 81 if (!dent) 82 ksft_exit_skip("cannot find lirc device for %s\n", rc); 83 84 closedir(d); 85 86 fd = open(buf, O_RDWR | O_NONBLOCK); 87 if (fd == -1) 88 ksft_exit_fail_msg("cannot open: %s: %m\n", buf); 89 90 return fd; 91 } 92 93 int main(int argc, char **argv) 94 { 95 unsigned int mode; 96 char buf[100]; 97 int rlircfd, wlircfd, protocolfd, i, n; 98 99 srand(time(NULL)); 100 101 if (argc != 3) 102 ksft_exit_fail_msg("Usage: %s <write rcN> <read rcN>\n", 103 argv[0]); 104 105 rlircfd = lirc_open(argv[2]); 106 mode = LIRC_MODE_SCANCODE; 107 if (ioctl(rlircfd, LIRC_SET_REC_MODE, &mode)) 108 ksft_exit_fail_msg("failed to set scancode rec mode %s: %m\n", 109 argv[2]); 110 111 wlircfd = lirc_open(argv[1]); 112 if (ioctl(wlircfd, LIRC_SET_SEND_MODE, &mode)) 113 ksft_exit_fail_msg("failed to set scancode send mode %s: %m\n", 114 argv[1]); 115 116 snprintf(buf, sizeof(buf), "/sys/class/rc/%s/protocols", argv[2]); 117 protocolfd = open(buf, O_WRONLY); 118 if (protocolfd == -1) 119 ksft_exit_fail_msg("failed to open %s: %m\n", buf); 120 121 printf("Sending IR on %s and receiving IR on %s.\n", argv[1], argv[2]); 122 123 for (i = 0; i < ARRAY_SIZE(protocols); i++) { 124 if (write(protocolfd, protocols[i].decoder, 125 strlen(protocols[i].decoder)) == -1) 126 ksft_exit_fail_msg("failed to set write decoder\n"); 127 128 printf("Testing protocol %s for decoder %s (%d/%d)...\n", 129 protocols[i].name, protocols[i].decoder, 130 i + 1, (int)ARRAY_SIZE(protocols)); 131 132 for (n = 0; n < TEST_SCANCODES; n++) { 133 unsigned int scancode = rand() & protocols[i].mask; 134 unsigned int rc_proto = protocols[i].proto; 135 136 if (rc_proto == RC_PROTO_RC6_MCE) 137 scancode |= 0x800f0000; 138 139 if (rc_proto == RC_PROTO_NECX && 140 (((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0) 141 continue; 142 143 if (rc_proto == RC_PROTO_NEC32 && 144 (((scancode >> 8) ^ ~scancode) & 0xff) == 0) 145 continue; 146 147 if (rc_proto == RC_PROTO_RCMM32 && 148 (scancode & 0x000c0000) != 0x000c0000 && 149 scancode & 0x00008000) 150 continue; 151 152 struct lirc_scancode lsc = { 153 .rc_proto = rc_proto, 154 .scancode = scancode 155 }; 156 157 printf("Testing scancode:%x\n", scancode); 158 159 while (write(wlircfd, &lsc, sizeof(lsc)) < 0) { 160 if (errno == EINTR) 161 continue; 162 163 ksft_exit_fail_msg("failed to send ir: %m\n"); 164 } 165 166 struct pollfd pfd = { .fd = rlircfd, .events = POLLIN }; 167 struct lirc_scancode lsc2; 168 169 poll(&pfd, 1, 1000); 170 171 bool decoded = true; 172 173 while (read(rlircfd, &lsc2, sizeof(lsc2)) < 0) { 174 if (errno == EINTR) 175 continue; 176 177 ksft_test_result_error("no scancode decoded: %m\n"); 178 decoded = false; 179 break; 180 } 181 182 if (!decoded) 183 continue; 184 185 if (lsc.rc_proto != lsc2.rc_proto) 186 ksft_test_result_error("decoded protocol is different: %d\n", 187 lsc2.rc_proto); 188 189 else if (lsc.scancode != lsc2.scancode) 190 ksft_test_result_error("decoded scancode is different: %llx\n", 191 lsc2.scancode); 192 else 193 ksft_inc_pass_cnt(); 194 } 195 196 printf("OK\n"); 197 } 198 199 close(rlircfd); 200 close(wlircfd); 201 close(protocolfd); 202 203 if (ksft_get_fail_cnt() > 0) 204 ksft_exit_fail(); 205 else 206 ksft_exit_pass(); 207 208 return 0; 209 } 210