NCDRfkillMonitor.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @file NCDRfkillMonitor.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <misc/debug.h>
  27. #include <misc/nonblocking.h>
  28. #include <ncd/NCDRfkillMonitor.h>
  29. #define RFKILL_DEVICE_NODE "/dev/rfkill"
  30. static void rfkill_fd_handler (NCDRfkillMonitor *o, int events);
  31. void rfkill_fd_handler (NCDRfkillMonitor *o, int events)
  32. {
  33. DebugObject_Access(&o->d_obj);
  34. // read from netlink fd
  35. struct rfkill_event event;
  36. int len = read(o->rfkill_fd, &event, sizeof(event));
  37. if (len < 0) {
  38. DEBUG("read failed");
  39. return;
  40. }
  41. if (len != sizeof(event)) {
  42. DEBUG("read returned wrong length");
  43. return;
  44. }
  45. // call handler
  46. o->handler(o->user, event);
  47. return;
  48. }
  49. int NCDRfkillMonitor_Init (NCDRfkillMonitor *o, BReactor *reactor, NCDRfkillMonitor_handler handler, void *user)
  50. {
  51. // init arguments
  52. o->reactor = reactor;
  53. o->handler = handler;
  54. o->user = user;
  55. // open rfkill
  56. if ((o->rfkill_fd = open(RFKILL_DEVICE_NODE, O_RDONLY)) < 0) {
  57. DEBUG("open failed");
  58. goto fail0;
  59. }
  60. // set fd non-blocking
  61. if (!badvpn_set_nonblocking(o->rfkill_fd)) {
  62. DEBUG("badvpn_set_nonblocking failed");
  63. goto fail1;
  64. }
  65. // init BFileDescriptor
  66. BFileDescriptor_Init(&o->bfd, o->rfkill_fd, (BFileDescriptor_handler)rfkill_fd_handler, o);
  67. if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
  68. DEBUG("BReactor_AddFileDescriptor failed");
  69. goto fail1;
  70. }
  71. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  72. DebugObject_Init(&o->d_obj);
  73. return 1;
  74. fail1:
  75. ASSERT_FORCE(close(o->rfkill_fd) == 0)
  76. fail0:
  77. return 0;
  78. }
  79. void NCDRfkillMonitor_Free (NCDRfkillMonitor *o)
  80. {
  81. DebugObject_Free(&o->d_obj);
  82. // free BFileDescriptor
  83. BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
  84. // close rfkill
  85. ASSERT_FORCE(close(o->rfkill_fd) == 0)
  86. }