NCDRfkillMonitor.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file NCDRfkillMonitor.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <unistd.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <misc/debug.h>
  34. #include <misc/nonblocking.h>
  35. #include <base/BLog.h>
  36. #include <ncd/NCDRfkillMonitor.h>
  37. #include <generated/blog_channel_NCDRfkillMonitor.h>
  38. #define RFKILL_DEVICE_NODE "/dev/rfkill"
  39. static void rfkill_fd_handler (NCDRfkillMonitor *o, int events);
  40. void rfkill_fd_handler (NCDRfkillMonitor *o, int events)
  41. {
  42. DebugObject_Access(&o->d_obj);
  43. // read from netlink fd
  44. struct rfkill_event event;
  45. int len = read(o->rfkill_fd, &event, sizeof(event));
  46. if (len < 0) {
  47. BLog(BLOG_ERROR, "read failed");
  48. return;
  49. }
  50. if (len != sizeof(event)) {
  51. BLog(BLOG_ERROR, "read returned wrong length");
  52. return;
  53. }
  54. // call handler
  55. o->handler(o->user, event);
  56. return;
  57. }
  58. int NCDRfkillMonitor_Init (NCDRfkillMonitor *o, BReactor *reactor, NCDRfkillMonitor_handler handler, void *user)
  59. {
  60. // init arguments
  61. o->reactor = reactor;
  62. o->handler = handler;
  63. o->user = user;
  64. // open rfkill
  65. if ((o->rfkill_fd = open(RFKILL_DEVICE_NODE, O_RDONLY)) < 0) {
  66. BLog(BLOG_ERROR, "open failed");
  67. goto fail0;
  68. }
  69. // set fd non-blocking
  70. if (!badvpn_set_nonblocking(o->rfkill_fd)) {
  71. BLog(BLOG_ERROR, "badvpn_set_nonblocking failed");
  72. goto fail1;
  73. }
  74. // init BFileDescriptor
  75. BFileDescriptor_Init(&o->bfd, o->rfkill_fd, (BFileDescriptor_handler)rfkill_fd_handler, o);
  76. if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
  77. BLog(BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  78. goto fail1;
  79. }
  80. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  81. DebugObject_Init(&o->d_obj);
  82. return 1;
  83. fail1:
  84. if (close(o->rfkill_fd) < 0) {
  85. BLog(BLOG_ERROR, "close failed");
  86. }
  87. fail0:
  88. return 0;
  89. }
  90. void NCDRfkillMonitor_Free (NCDRfkillMonitor *o)
  91. {
  92. DebugObject_Free(&o->d_obj);
  93. // free BFileDescriptor
  94. BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
  95. // close rfkill
  96. if (close(o->rfkill_fd) < 0) {
  97. BLog(BLOG_ERROR, "close failed");
  98. }
  99. }