NCDRfkillMonitor.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <base/BLog.h>
  29. #include <ncd/NCDRfkillMonitor.h>
  30. #include <generated/blog_channel_NCDRfkillMonitor.h>
  31. #define RFKILL_DEVICE_NODE "/dev/rfkill"
  32. static void rfkill_fd_handler (NCDRfkillMonitor *o, int events);
  33. void rfkill_fd_handler (NCDRfkillMonitor *o, int events)
  34. {
  35. DebugObject_Access(&o->d_obj);
  36. // read from netlink fd
  37. struct rfkill_event event;
  38. int len = read(o->rfkill_fd, &event, sizeof(event));
  39. if (len < 0) {
  40. BLog(BLOG_ERROR, "read failed");
  41. return;
  42. }
  43. if (len != sizeof(event)) {
  44. BLog(BLOG_ERROR, "read returned wrong length");
  45. return;
  46. }
  47. // call handler
  48. o->handler(o->user, event);
  49. return;
  50. }
  51. int NCDRfkillMonitor_Init (NCDRfkillMonitor *o, BReactor *reactor, NCDRfkillMonitor_handler handler, void *user)
  52. {
  53. // init arguments
  54. o->reactor = reactor;
  55. o->handler = handler;
  56. o->user = user;
  57. // open rfkill
  58. if ((o->rfkill_fd = open(RFKILL_DEVICE_NODE, O_RDONLY)) < 0) {
  59. BLog(BLOG_ERROR, "open failed");
  60. goto fail0;
  61. }
  62. // set fd non-blocking
  63. if (!badvpn_set_nonblocking(o->rfkill_fd)) {
  64. BLog(BLOG_ERROR, "badvpn_set_nonblocking failed");
  65. goto fail1;
  66. }
  67. // init BFileDescriptor
  68. BFileDescriptor_Init(&o->bfd, o->rfkill_fd, (BFileDescriptor_handler)rfkill_fd_handler, o);
  69. if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
  70. BLog(BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  71. goto fail1;
  72. }
  73. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  74. DebugObject_Init(&o->d_obj);
  75. return 1;
  76. fail1:
  77. ASSERT_FORCE(close(o->rfkill_fd) == 0)
  78. fail0:
  79. return 0;
  80. }
  81. void NCDRfkillMonitor_Free (NCDRfkillMonitor *o)
  82. {
  83. DebugObject_Free(&o->d_obj);
  84. // free BFileDescriptor
  85. BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
  86. // close rfkill
  87. ASSERT_FORCE(close(o->rfkill_fd) == 0)
  88. }