Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions pywifi/_wifiutil_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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 = """<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>{profile_name}</name>
<name><![CDATA[{profile_name}]]></name>
<SSIDConfig>
<SSID>
<name>{ssid}</name>
<hex>{ssid}</hex>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>manual</connectionMode>
"""
if params.bsstype == BSS_TYPE_ADHOC:
xml += """ <nonBroadcast>false</nonBroadcast>"""
else:
xml += """ <nonBroadcast>true</nonBroadcast>"""

xml += """ </SSIDConfig>"""
if params.bsstype == BSS_TYPE_ADHOC:
xml += """<connectionType>IBSS</connectionType>"""
else:
xml += """<connectionType>ESS</connectionType>"""

xml += """<connectionMode>manual</connectionMode>
<MSM>
<security>
<authEncryption>
Expand All @@ -367,11 +389,19 @@ def add_network_profile(self, obj, params):
"""

if AKM_TYPE_NONE not in params.akm:
xml += """<sharedKey>
<keyType>passPhrase</keyType>
<protected>{protected}</protected>
<keyMaterial>{key}</keyMaterial>
xml += """<sharedKey>"""
if params.cipher == CIPHER_TYPE_WEP or len(params.key) == 64:
xml += """<keyType>networkKey</keyType>"""
else:
xml += """<keyType>passPhrase</keyType>"""

xml += """<protected>{protected}</protected>
<keyMaterial><![CDATA[{key}]]></keyMaterial>
</sharedKey>"""

if params.cipher == CIPHER_TYPE_WEP:
profile_data['key_index'] = params.keyindex - 1
xml += """ <keyIndex>{key_index}</keyIndex>"""

xml += """
</security>
Expand All @@ -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."""
Expand Down
4 changes: 4 additions & 0 deletions pywifi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@

KEY_TYPE_NETWORKKEY = 0
KEY_TYPE_PASSPHRASE = 1

# Define Bss Type
BSS_TYPE_INFRA = 1
BSS_TYPE_ADHOC = 2
7 changes: 6 additions & 1 deletion pywifi/iface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
2 changes: 2 additions & 0 deletions pywifi/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
9 changes: 9 additions & 0 deletions pywifi/wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)