diff --git a/pywifi/_wifiutil_win.py b/pywifi/_wifiutil_win.py index d0adb11..5514df7 100755 --- a/pywifi/_wifiutil_win.py +++ b/pywifi/_wifiutil_win.py @@ -6,11 +6,14 @@ import re import platform import time +import binascii import logging from ctypes import * from ctypes.wintypes import * from comtypes import GUID +import chardet + from .const import * from .profile import Profile @@ -313,7 +316,7 @@ def connect(self, obj, params): connect_params = WLAN_CONNECTION_PARAMETERS() connect_params.wlanConnectionMode = 0 # Profile - connect_params.dot11BssType = 1 # infra + connect_params.dot11BssType = params.bsstype # infra profile_name = create_unicode_buffer(params.ssid) connect_params.strProfile = profile_name.value @@ -333,30 +336,49 @@ def add_network_profile(self, obj, params): params.process_akm() profile_data = {} - profile_data['ssid'] = params.ssid + profile_data['ssid'] = binascii.b2a_hex(params.ssid) if AKM_TYPE_NONE in params.akm: profile_data['auth'] = auth_value_to_str_dict[params.auth] profile_data['encrypt'] = "none" + elif AKM_TYPE_UNKNOWN in params.akm: + profile_data['auth'] = auth_value_to_str_dict[params.auth] + profile_data['encrypt'] = cipher_value_to_str_dict[params.cipher] else: profile_data['auth'] = akm_value_to_str_dict[params.akm[-1]] profile_data['encrypt'] = cipher_value_to_str_dict[params.cipher] profile_data['key'] = params.key - profile_data['protected'] = 'false' - profile_data['profile_name'] = params.ssid + + if params.bsstype == BSS_TYPE_ADHOC and 1 == CLIENT_VERSION: + profile_name = '%s-adhoc' % params.ssid + else: + profile_name = params.ssid + if profile_name and (not isinstance(profile_name, unicode)): + profile_name = profile_name.decode(chardet.detect(profile_name)['encoding']) + profile_data['profile_name'] = profile_name xml = """ - {profile_name} + - {ssid} + {ssid} - - ESS - manual + """ + if params.bsstype == BSS_TYPE_ADHOC: + xml += """ false""" + else: + xml += """ true""" + + xml += """ """ + if params.bsstype == BSS_TYPE_ADHOC: + xml += """IBSS""" + else: + xml += """ESS""" + + xml += """manual @@ -367,11 +389,19 @@ def add_network_profile(self, obj, params): """ if AKM_TYPE_NONE not in params.akm: - xml += """ - passPhrase - {protected} - {key} + xml += """""" + if params.cipher == CIPHER_TYPE_WEP or len(params.key) == 64: + xml += """networkKey""" + else: + xml += """passPhrase""" + + xml += """{protected} + """ + + if params.cipher == CIPHER_TYPE_WEP: + profile_data['key_index'] = params.keyindex - 1 + xml += """ {key_index}""" xml += """ @@ -394,7 +424,7 @@ def add_network_profile(self, obj, params): buf = create_unicode_buffer(64) self._wlan_reason_code_to_str(reason_code, buf_size, buf) - return params + return (status, buf) def network_profile_name_list(self, obj): """Get AP profile names.""" diff --git a/pywifi/const.py b/pywifi/const.py index 66b73dc..e554502 100755 --- a/pywifi/const.py +++ b/pywifi/const.py @@ -31,3 +31,7 @@ KEY_TYPE_NETWORKKEY = 0 KEY_TYPE_PASSPHRASE = 1 + +# Define Bss Type +BSS_TYPE_INFRA = 1 +BSS_TYPE_ADHOC = 2 diff --git a/pywifi/iface.py b/pywifi/iface.py index 400978b..37b646d 100755 --- a/pywifi/iface.py +++ b/pywifi/iface.py @@ -71,11 +71,16 @@ def remove_network_profile(self, params): """Remove the specified AP settings.""" self._wifi_ctrl.remove_network_profile(self._raw_obj, params) - + def remove_all_network_profiles(self): """Remove all the AP settings.""" self._wifi_ctrl.remove_all_network_profiles(self._raw_obj) + + def network_profile_name_list(self): + """Get all the AP profile names.""" + + return self._wifi_ctrl.network_profile_name_list(self._raw_obj) def network_profiles(self): """Get all the AP profiles.""" diff --git a/pywifi/profile.py b/pywifi/profile.py index 85959e9..a21aded 100755 --- a/pywifi/profile.py +++ b/pywifi/profile.py @@ -17,6 +17,8 @@ def __init__(self): self.ssid = None self.bssid = None self.key = None + self.keyindex = None + self.bsstype = BSS_TYPE_INFRA # 1 - Infra, 2 - Adhoc def process_akm(self): diff --git a/pywifi/wifi.py b/pywifi/wifi.py index 270163a..90e48ba 100755 --- a/pywifi/wifi.py +++ b/pywifi/wifi.py @@ -48,3 +48,12 @@ def interfaces(self): self._logger.error("Can't get wifi interface") return self._ifaces + + def interface(self, guid): + wifi_ctrl = wifiutil.WifiUtil() + for interface in wifi_ctrl.interfaces(): + if str(interface['guid']) == str(guid): + return Interface(interface) + else: + self._logger.error("Can't get wifi interface with GUID: %s" % guid) + raise RuntimeError("Can't get wifi interface with GUID: %s" % guid)