iface.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python3
  2. # vim: set fileencoding=utf-8
  3. """Implement Interface for manipulating wifi devies."""
  4. import platform
  5. import logging
  6. if platform.system().lower() == 'windows':
  7. from . import _wifiutil_win as wifiutil
  8. elif platform.system().lower() == 'linux':
  9. from . import _wifiutil_linux as wifiutil
  10. else:
  11. raise NotImplementedError
  12. class Interface:
  13. """Interface provides methods for manipulating wifi devices."""
  14. """
  15. For encapsulating OS dependent behavior, we declare _raw_obj here for
  16. storing some common attribute (e.g. name) and os attributes (e.g. dbus
  17. objects for linux)
  18. """
  19. _raw_obj = {}
  20. _wifi_ctrl = {}
  21. _logger = None
  22. def __init__(self, raw_obj):
  23. self._raw_obj = raw_obj
  24. self._wifi_ctrl = wifiutil.WifiUtil()
  25. self._logger = logging.getLogger('pywifi')
  26. def name(self):
  27. """"Get the name of the wifi interfacce."""
  28. return self._raw_obj['name']
  29. def scan(self):
  30. """Trigger the wifi interface to scan."""
  31. self._logger.info("iface '%s' scans", self.name())
  32. self._wifi_ctrl.scan(self._raw_obj)
  33. def scan_results(self):
  34. """Return the scan result."""
  35. bsses = self._wifi_ctrl.scan_results(self._raw_obj)
  36. if self._logger.isEnabledFor(logging.INFO):
  37. for bss in bsses:
  38. self._logger.info("Find bss:")
  39. self._logger.info("\tbssid: %s", bss.bssid)
  40. self._logger.info("\tssid: %s", bss.ssid)
  41. self._logger.info("\tfreq: %d", bss.freq)
  42. self._logger.info("\tauth: %s", bss.auth)
  43. self._logger.info("\takm: %s", bss.akm)
  44. self._logger.info("\tsignal: %d", bss.signal)
  45. return bsses
  46. def add_network_profile(self, params):
  47. """Add the info of the AP for connecting afterward."""
  48. return self._wifi_ctrl.add_network_profile(self._raw_obj, params)
  49. def remove_network_profile(self, params):
  50. """Remove the specified AP settings."""
  51. self._wifi_ctrl.remove_network_profile(self._raw_obj, params)
  52. def remove_all_network_profiles(self):
  53. """Remove all the AP settings."""
  54. self._wifi_ctrl.remove_all_network_profiles(self._raw_obj)
  55. def network_profiles(self):
  56. """Get all the AP profiles."""
  57. profiles = self._wifi_ctrl.network_profiles(self._raw_obj)
  58. if self._logger.isEnabledFor(logging.INFO):
  59. for profile in profiles:
  60. self._logger.info("Get profile:")
  61. self._logger.info("\tssid: %s", profile.ssid)
  62. self._logger.info("\tauth: %s", profile.auth)
  63. self._logger.info("\takm: %s", profile.akm)
  64. self._logger.info("\tcipher: %s", profile.cipher)
  65. return profiles
  66. def connect(self, params):
  67. """Connect to the specified AP."""
  68. self._logger.info("iface '%s' connects to AP: '%s'",
  69. self.name(), params.ssid)
  70. self._wifi_ctrl.connect(self._raw_obj, params)
  71. def disconnect(self):
  72. """Disconnect from the specified AP."""
  73. self._logger.info("iface '%s' disconnects", self.name())
  74. self._wifi_ctrl.disconnect(self._raw_obj)
  75. def status(self):
  76. """Get the status of the wifi interface."""
  77. return self._wifi_ctrl.status(self._raw_obj)