rfkill.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python
  2. #
  3. # rfkill control code
  4. #
  5. # Copyright (c) 2015 Intel Corporation
  6. #
  7. # Author: Johannes Berg <johannes.berg@intel.com>
  8. #
  9. # This software may be distributed under the terms of the BSD license.
  10. # See README for more details.
  11. import struct
  12. import fcntl
  13. import os
  14. (TYPE_ALL,
  15. TYPE_WLAN,
  16. TYPE_BLUETOOTH,
  17. TYPE_UWB,
  18. TYPE_WIMAX,
  19. TYPE_WWAN,
  20. TYPE_GPS,
  21. TYPE_FM,
  22. TYPE_NFC) = range(9)
  23. (_OP_ADD,
  24. _OP_DEL,
  25. _OP_CHANGE,
  26. _OP_CHANGE_ALL) = range(4)
  27. _type_names = {
  28. TYPE_ALL: "all",
  29. TYPE_WLAN: "Wireless LAN",
  30. TYPE_BLUETOOTH: "Bluetooth",
  31. TYPE_UWB: "Ultra-Wideband",
  32. TYPE_WIMAX: "WiMAX",
  33. TYPE_WWAN: "Wireless WAN",
  34. TYPE_GPS: "GPS",
  35. TYPE_FM: "FM",
  36. TYPE_NFC: "NFC",
  37. }
  38. # idx, type, op, soft, hard
  39. _event_struct = '@IBBBB'
  40. _event_sz = struct.calcsize(_event_struct)
  41. class RFKillException(Exception):
  42. pass
  43. class RFKill(object):
  44. def __init__(self, idx):
  45. self._idx = idx
  46. self._type = None
  47. @property
  48. def idx(self):
  49. return self._idx
  50. @property
  51. def name(self):
  52. return open('/sys/class/rfkill/rfkill%d/name' % self._idx, 'r').read().rstrip()
  53. @property
  54. def type(self):
  55. if not self._type:
  56. for r, s, h in RFKill.list():
  57. if r.idx == self.idx:
  58. self._type = r._type
  59. break
  60. return self._type
  61. @property
  62. def type_name(self):
  63. return _type_names.get(self._type, "unknown")
  64. @property
  65. def blocked(self):
  66. l = RFKill.list()
  67. for r, s, h in l:
  68. if r.idx == self.idx:
  69. return (s, h)
  70. raise RFKillException("RFKill instance no longer exists")
  71. @property
  72. def soft_blocked(self):
  73. return self.blocked[0]
  74. @soft_blocked.setter
  75. def soft_blocked(self, block):
  76. if block:
  77. self.block()
  78. else:
  79. self.unblock()
  80. @property
  81. def hard_blocked(self):
  82. return self.blocked[1]
  83. def block(self):
  84. rfk = open('/dev/rfkill', 'w')
  85. s = struct.pack(_event_struct, self.idx, TYPE_ALL, _OP_CHANGE, 1, 0)
  86. rfk.write(s)
  87. rfk.close()
  88. def unblock(self):
  89. rfk = open('/dev/rfkill', 'w')
  90. s = struct.pack(_event_struct, self.idx, TYPE_ALL, _OP_CHANGE, 0, 0)
  91. rfk.write(s)
  92. rfk.close()
  93. @classmethod
  94. def block_all(cls, t=TYPE_ALL):
  95. rfk = open('/dev/rfkill', 'w')
  96. print rfk
  97. s = struct.pack(_event_struct, 0, t, _OP_CHANGE_ALL, 1, 0)
  98. rfk.write(s)
  99. rfk.close()
  100. @classmethod
  101. def unblock_all(cls, t=TYPE_ALL):
  102. rfk = open('/dev/rfkill', 'w')
  103. s = struct.pack(_event_struct, 0, t, _OP_CHANGE_ALL, 0, 0)
  104. rfk.write(s)
  105. rfk.close()
  106. @classmethod
  107. def list(cls):
  108. res = []
  109. rfk = open('/dev/rfkill', 'r')
  110. fd = rfk.fileno()
  111. flgs = fcntl.fcntl(fd, fcntl.F_GETFL)
  112. fcntl.fcntl(fd, fcntl.F_SETFL, flgs | os.O_NONBLOCK)
  113. while True:
  114. try:
  115. d = rfk.read(_event_sz)
  116. _idx, _t, _op, _s, _h = struct.unpack(_event_struct, d)
  117. if _op != _OP_ADD:
  118. continue
  119. r = RFKill(_idx)
  120. r._type = _t
  121. res.append((r, _s, _h))
  122. except IOError:
  123. break
  124. return res
  125. if __name__ == "__main__":
  126. for r, s, h in RFKill.list():
  127. print "%d: %s: %s" % (r.idx, r.name, r.type_name)
  128. print "\tSoft blocked: %s" % ("yes" if s else "no")
  129. print "\tHard blocked: %s" % ("yes" if h else "no")