test_lirc_mode2_user.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0
  2. // test ir decoder
  3. //
  4. // Copyright (C) 2018 Sean Young <sean@mess.org>
  5. // A lirc chardev is a device representing a consumer IR (cir) device which
  6. // can receive infrared signals from remote control and/or transmit IR.
  7. //
  8. // IR is sent as a series of pulses and space somewhat like morse code. The
  9. // BPF program can decode this into scancodes so that rc-core can translate
  10. // this into input key codes using the rc keymap.
  11. //
  12. // This test works by sending IR over rc-loopback, so the IR is processed by
  13. // BPF and then decoded into scancodes. The lirc chardev must be the one
  14. // associated with rc-loopback, see the output of ir-keytable(1).
  15. //
  16. // The following CONFIG options must be enabled for the test to succeed:
  17. // CONFIG_RC_CORE=y
  18. // CONFIG_BPF_RAWIR_EVENT=y
  19. // CONFIG_RC_LOOPBACK=y
  20. // Steps:
  21. // 1. Open the /dev/lircN device for rc-loopback (given on command line)
  22. // 2. Attach bpf_lirc_mode2 program which decodes some IR.
  23. // 3. Send some IR to the same IR device; since it is loopback, this will
  24. // end up in the bpf program
  25. // 4. bpf program should decode IR and report keycode
  26. // 5. We can read keycode from same /dev/lirc device
  27. #include <linux/bpf.h>
  28. #include <linux/lirc.h>
  29. #include <errno.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include <poll.h>
  35. #include <sys/types.h>
  36. #include <sys/ioctl.h>
  37. #include <sys/stat.h>
  38. #include <fcntl.h>
  39. #include "bpf_util.h"
  40. #include <bpf/bpf.h>
  41. #include <bpf/libbpf.h>
  42. int main(int argc, char **argv)
  43. {
  44. struct bpf_object *obj;
  45. int ret, lircfd, progfd, mode;
  46. int testir = 0x1dead;
  47. u32 prog_ids[10], prog_flags[10], prog_cnt;
  48. if (argc != 2) {
  49. printf("Usage: %s /dev/lircN\n", argv[0]);
  50. return 2;
  51. }
  52. ret = bpf_prog_load("test_lirc_mode2_kern.o",
  53. BPF_PROG_TYPE_LIRC_MODE2, &obj, &progfd);
  54. if (ret) {
  55. printf("Failed to load bpf program\n");
  56. return 1;
  57. }
  58. lircfd = open(argv[1], O_RDWR | O_NONBLOCK);
  59. if (lircfd == -1) {
  60. printf("failed to open lirc device %s: %m\n", argv[1]);
  61. return 1;
  62. }
  63. /* Let's try detach it before it was ever attached */
  64. ret = bpf_prog_detach2(progfd, lircfd, BPF_LIRC_MODE2);
  65. if (ret != -1 || errno != ENOENT) {
  66. printf("bpf_prog_detach2 not attached should fail: %m\n");
  67. return 1;
  68. }
  69. mode = LIRC_MODE_SCANCODE;
  70. if (ioctl(lircfd, LIRC_SET_REC_MODE, &mode)) {
  71. printf("failed to set rec mode: %m\n");
  72. return 1;
  73. }
  74. prog_cnt = 10;
  75. ret = bpf_prog_query(lircfd, BPF_LIRC_MODE2, 0, prog_flags, prog_ids,
  76. &prog_cnt);
  77. if (ret) {
  78. printf("Failed to query bpf programs on lirc device: %m\n");
  79. return 1;
  80. }
  81. if (prog_cnt != 0) {
  82. printf("Expected nothing to be attached\n");
  83. return 1;
  84. }
  85. ret = bpf_prog_attach(progfd, lircfd, BPF_LIRC_MODE2, 0);
  86. if (ret) {
  87. printf("Failed to attach bpf to lirc device: %m\n");
  88. return 1;
  89. }
  90. /* Write raw IR */
  91. ret = write(lircfd, &testir, sizeof(testir));
  92. if (ret != sizeof(testir)) {
  93. printf("Failed to send test IR message: %m\n");
  94. return 1;
  95. }
  96. struct pollfd pfd = { .fd = lircfd, .events = POLLIN };
  97. struct lirc_scancode lsc;
  98. poll(&pfd, 1, 100);
  99. /* Read decoded IR */
  100. ret = read(lircfd, &lsc, sizeof(lsc));
  101. if (ret != sizeof(lsc)) {
  102. printf("Failed to read decoded IR: %m\n");
  103. return 1;
  104. }
  105. if (lsc.scancode != 0xdead || lsc.rc_proto != 64) {
  106. printf("Incorrect scancode decoded\n");
  107. return 1;
  108. }
  109. prog_cnt = 10;
  110. ret = bpf_prog_query(lircfd, BPF_LIRC_MODE2, 0, prog_flags, prog_ids,
  111. &prog_cnt);
  112. if (ret) {
  113. printf("Failed to query bpf programs on lirc device: %m\n");
  114. return 1;
  115. }
  116. if (prog_cnt != 1) {
  117. printf("Expected one program to be attached\n");
  118. return 1;
  119. }
  120. /* Let's try detaching it now it is actually attached */
  121. ret = bpf_prog_detach2(progfd, lircfd, BPF_LIRC_MODE2);
  122. if (ret) {
  123. printf("bpf_prog_detach2: returned %m\n");
  124. return 1;
  125. }
  126. return 0;
  127. }