def __init__(self): wlan_manager_base.WlanManagerBase.__init__(self) self.scan_lock = threading.Lock() self.init_succeeded = False self.scan_complete = False try: self.client_handle = wintypes.HANDLE() self.ifaces = None # Open wlan handle negotiated_version = wintypes.DWORD() ret = wlan_api.WlanOpenHandle(1, None, byref(negotiated_version), byref(self.client_handle)) if ret != wlan_error_code.ERROR_SUCCESS: logging.error(FormatError(ret)) raise Exception("WlanOpenHandle", FormatError(ret)) # Get/Enumerate network interface pInterfaceList = pointer(wlan_api.WLAN_INTERFACE_INFO_LIST()) ret = wlan_api.WlanEnumInterfaces(self.client_handle, None, byref(pInterfaceList)) if ret != wlan_error_code.ERROR_SUCCESS: logging.error(FormatError(ret)) raise Exception("WlanOpenHandle", FormatError(ret)) self.ifaces = wlan_api.customresize(pInterfaceList.contents.InterfaceInfo, pInterfaceList.contents.NumberOfItems) self.init_succeeded = True except Exception as current_exception: logging.error(current_exception) finally: pass
def scan_ap(self): # Scan ap now self.scan_lock.acquire() self.ap_list = {} if self.init_succeeded == False: logging.error("The class does not be initialized successfully") return self.ap_list.copy() try: pAvailableNetworkList = None for iface in self.ifaces: # Register notification call back pre_notify_type = wintypes.DWORD(0) wlan_notification = wlan_api.WLAN_NOTIFICATION_CALLBACK(wlan_notification_callback) callback_context = py_object(self) ret = wlan_api.WlanRegisterNotification(self.client_handle, wlan_api.WLAN_NOTIFICATION_SOURCE_ACM, 1, wlan_notification, callback_context, None, byref(pre_notify_type)) if ret != wlan_error_code.ERROR_SUCCESS: logging.error(FormatError(ret)) raise Exception("WlanRegisterNotification", FormatError(ret)) ret = wlan_api.WlanScan(self.client_handle, byref(iface.InterfaceGuid), None, None, None) if ret != wlan_error_code.ERROR_SUCCESS: logging.error(FormatError(ret)) raise Exception("WlanScan", FormatError(ret)) # Wait for scan completed while True: if not self.scan_complete: time.sleep(1) else: self.scan_complete = False break # Unregister notification function wlan_api.WlanRegisterNotification(self.client_handle, wlan_api.WLAN_NOTIFICATION_SOURCE_NONE, 1, wlan_notification, None, None, byref(pre_notify_type)) if ret != wlan_error_code.ERROR_SUCCESS: logging.error(FormatError(ret)) raise Exception("WlanUnRegisterNotification", FormatError(ret)) pAvailableNetworkList = pointer(wlan_api.WLAN_AVAILABLE_NETWORK_LIST()) ret = wlan_api.WlanGetAvailableNetworkList(self.client_handle, byref(iface.InterfaceGuid), 0, None, byref(pAvailableNetworkList)) if ret != wlan_error_code.ERROR_SUCCESS: logging.error(FormatError(ret)) raise Exception("WlanGetAvailableNetworkList", FormatError(ret)) avail_net_list = pAvailableNetworkList.contents networks = wlan_api.customresize(avail_net_list.Network, avail_net_list.NumberOfItems) for network in networks: net_essid = network.dot11Ssid.SSID[:network.dot11Ssid.SSIDLength] # The ap should be begin with EasyConnect_ if not net_essid.upper().startswith("EASYCONNECT_"): continue # One AP with different encrypt algorithm will get more than once if self.ap_list.has_key(net_essid) == True: if self.ap_list[net_essid]['is_connected'] == True: continue net_signal = network.wlanSignalQuality net_auth = WlanManagerWindows.auth[network.dot11DefaultAuthAlgorithm] net_cifr = WlanManagerWindows.cifr[network.dot11DefaultCipherAlgorithm] net_security = network.SecurityEnabled is_connected = False if network.Flags == 3: is_connected = True self.ap_list[net_essid] = {'net_essid':net_essid, 'net_signal':net_signal, 'net_auth':net_auth, 'net_cifr':net_cifr, 'net_security':net_security, 'is_connected':is_connected} logging.info(self.ap_list[net_essid]) except Exception as current_exception: logging.error(current_exception) finally: if pAvailableNetworkList != None: wlan_api.WlanFreeMemory(pAvailableNetworkList) self.scan_lock.release() return self.ap_list.copy()