Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // test ir decoder
0003 //
0004 // Copyright (C) 2018 Sean Young <sean@mess.org>
0005 
0006 // When sending LIRC_MODE_SCANCODE, the IR will be encoded. rc-loopback
0007 // will send this IR to the receiver side, where we try to read the decoded
0008 // IR. Decoding happens in a separate kernel thread, so we will need to
0009 // wait until that is scheduled, hence we use poll to check for read
0010 // readiness.
0011 
0012 #include <linux/lirc.h>
0013 #include <errno.h>
0014 #include <stdio.h>
0015 #include <stdlib.h>
0016 #include <stdbool.h>
0017 #include <string.h>
0018 #include <unistd.h>
0019 #include <poll.h>
0020 #include <time.h>
0021 #include <sys/types.h>
0022 #include <sys/ioctl.h>
0023 #include <dirent.h>
0024 #include <sys/stat.h>
0025 #include <fcntl.h>
0026 #include "../kselftest.h"
0027 
0028 #define TEST_SCANCODES  10
0029 #define SYSFS_PATH_MAX 256
0030 #define DNAME_PATH_MAX 256
0031 
0032 /*
0033  * Support ancient lirc.h which does not have these values. Can be removed
0034  * once RHEL 8 is no longer a relevant testing platform.
0035  */
0036 #if RC_PROTO_MAX < 26
0037 #define RC_PROTO_RCMM12 24
0038 #define RC_PROTO_RCMM24 25
0039 #define RC_PROTO_RCMM32 26
0040 #endif
0041 
0042 static const struct {
0043     enum rc_proto proto;
0044     const char *name;
0045     unsigned int mask;
0046     const char *decoder;
0047 } protocols[] = {
0048     { RC_PROTO_RC5, "rc-5", 0x1f7f, "rc-5" },
0049     { RC_PROTO_RC5X_20, "rc-5x-20", 0x1f7f3f, "rc-5" },
0050     { RC_PROTO_RC5_SZ, "rc-5-sz", 0x2fff, "rc-5-sz" },
0051     { RC_PROTO_JVC, "jvc", 0xffff, "jvc" },
0052     { RC_PROTO_SONY12, "sony-12", 0x1f007f, "sony" },
0053     { RC_PROTO_SONY15, "sony-15", 0xff007f, "sony" },
0054     { RC_PROTO_SONY20, "sony-20", 0x1fff7f, "sony" },
0055     { RC_PROTO_NEC, "nec", 0xffff, "nec" },
0056     { RC_PROTO_NECX, "nec-x", 0xffffff, "nec" },
0057     { RC_PROTO_NEC32, "nec-32", 0xffffffff, "nec" },
0058     { RC_PROTO_SANYO, "sanyo", 0x1fffff, "sanyo" },
0059     { RC_PROTO_RC6_0, "rc-6-0", 0xffff, "rc-6" },
0060     { RC_PROTO_RC6_6A_20, "rc-6-6a-20", 0xfffff, "rc-6" },
0061     { RC_PROTO_RC6_6A_24, "rc-6-6a-24", 0xffffff, "rc-6" },
0062     { RC_PROTO_RC6_6A_32, "rc-6-6a-32", 0xffffffff, "rc-6" },
0063     { RC_PROTO_RC6_MCE, "rc-6-mce", 0x00007fff, "rc-6" },
0064     { RC_PROTO_SHARP, "sharp", 0x1fff, "sharp" },
0065     { RC_PROTO_IMON, "imon", 0x7fffffff, "imon" },
0066     { RC_PROTO_RCMM12, "rcmm-12", 0x00000fff, "rc-mm" },
0067     { RC_PROTO_RCMM24, "rcmm-24", 0x00ffffff, "rc-mm" },
0068     { RC_PROTO_RCMM32, "rcmm-32", 0xffffffff, "rc-mm" },
0069 };
0070 
0071 int lirc_open(const char *rc)
0072 {
0073     struct dirent *dent;
0074     char buf[SYSFS_PATH_MAX + DNAME_PATH_MAX];
0075     DIR *d;
0076     int fd;
0077 
0078     snprintf(buf, sizeof(buf), "/sys/class/rc/%s", rc);
0079 
0080     d = opendir(buf);
0081     if (!d)
0082         ksft_exit_fail_msg("cannot open %s: %m\n", buf);
0083 
0084     while ((dent = readdir(d)) != NULL) {
0085         if (!strncmp(dent->d_name, "lirc", 4)) {
0086             snprintf(buf, sizeof(buf), "/dev/%s", dent->d_name);
0087             break;
0088         }
0089     }
0090 
0091     if (!dent)
0092         ksft_exit_skip("cannot find lirc device for %s\n", rc);
0093 
0094     closedir(d);
0095 
0096     fd = open(buf, O_RDWR | O_NONBLOCK);
0097     if (fd == -1)
0098         ksft_exit_fail_msg("cannot open: %s: %m\n", buf);
0099 
0100     return fd;
0101 }
0102 
0103 int main(int argc, char **argv)
0104 {
0105     unsigned int mode;
0106     char buf[100];
0107     int rlircfd, wlircfd, protocolfd, i, n;
0108 
0109     srand(time(NULL));
0110 
0111     if (argc != 3)
0112         ksft_exit_fail_msg("Usage: %s <write rcN> <read rcN>\n",
0113                    argv[0]);
0114 
0115     rlircfd = lirc_open(argv[2]);
0116     mode = LIRC_MODE_SCANCODE;
0117     if (ioctl(rlircfd, LIRC_SET_REC_MODE, &mode))
0118         ksft_exit_fail_msg("failed to set scancode rec mode %s: %m\n",
0119                    argv[2]);
0120 
0121     wlircfd = lirc_open(argv[1]);
0122     if (ioctl(wlircfd, LIRC_SET_SEND_MODE, &mode))
0123         ksft_exit_fail_msg("failed to set scancode send mode %s: %m\n",
0124                    argv[1]);
0125 
0126     snprintf(buf, sizeof(buf), "/sys/class/rc/%s/protocols", argv[2]);
0127     protocolfd = open(buf, O_WRONLY);
0128     if (protocolfd == -1)
0129         ksft_exit_fail_msg("failed to open %s: %m\n", buf);
0130 
0131     printf("Sending IR on %s and receiving IR on %s.\n", argv[1], argv[2]);
0132 
0133     for (i = 0; i < ARRAY_SIZE(protocols); i++) {
0134         if (write(protocolfd, protocols[i].decoder,
0135               strlen(protocols[i].decoder)) == -1)
0136             ksft_exit_fail_msg("failed to set write decoder\n");
0137 
0138         printf("Testing protocol %s for decoder %s (%d/%d)...\n",
0139                protocols[i].name, protocols[i].decoder,
0140                i + 1, (int)ARRAY_SIZE(protocols));
0141 
0142         for (n = 0; n < TEST_SCANCODES; n++) {
0143             unsigned int scancode = rand() & protocols[i].mask;
0144             unsigned int rc_proto = protocols[i].proto;
0145 
0146             if (rc_proto == RC_PROTO_RC6_MCE)
0147                 scancode |= 0x800f0000;
0148 
0149             if (rc_proto == RC_PROTO_NECX &&
0150                 (((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
0151                 continue;
0152 
0153             if (rc_proto == RC_PROTO_NEC32 &&
0154                 (((scancode >> 8) ^ ~scancode) & 0xff) == 0)
0155                 continue;
0156 
0157             if (rc_proto == RC_PROTO_RCMM32 &&
0158                 (scancode & 0x000c0000) != 0x000c0000 &&
0159                 scancode & 0x00008000)
0160                 continue;
0161 
0162             struct lirc_scancode lsc = {
0163                 .rc_proto = rc_proto,
0164                 .scancode = scancode
0165             };
0166 
0167             printf("Testing scancode:%x\n", scancode);
0168 
0169             while (write(wlircfd, &lsc, sizeof(lsc)) < 0) {
0170                 if (errno == EINTR)
0171                     continue;
0172 
0173                 ksft_exit_fail_msg("failed to send ir: %m\n");
0174             }
0175 
0176             struct pollfd pfd = { .fd = rlircfd, .events = POLLIN };
0177             struct lirc_scancode lsc2;
0178 
0179             poll(&pfd, 1, 1000);
0180 
0181             bool decoded = true;
0182 
0183             while (read(rlircfd, &lsc2, sizeof(lsc2)) < 0) {
0184                 if (errno == EINTR)
0185                     continue;
0186 
0187                 ksft_test_result_error("no scancode decoded: %m\n");
0188                 decoded = false;
0189                 break;
0190             }
0191 
0192             if (!decoded)
0193                 continue;
0194 
0195             if (lsc.rc_proto != lsc2.rc_proto)
0196                 ksft_test_result_error("decoded protocol is different: %d\n",
0197                                lsc2.rc_proto);
0198 
0199             else if (lsc.scancode != lsc2.scancode)
0200                 ksft_test_result_error("decoded scancode is different: %llx\n",
0201                                lsc2.scancode);
0202             else
0203                 ksft_inc_pass_cnt();
0204         }
0205 
0206         printf("OK\n");
0207     }
0208 
0209     close(rlircfd);
0210     close(wlircfd);
0211     close(protocolfd);
0212 
0213     if (ksft_get_fail_cnt() > 0)
0214         ksft_exit_fail();
0215     else
0216         ksft_exit_pass();
0217 
0218     return 0;
0219 }