Skip to content
This repository has been archived by the owner on Jun 9, 2018. It is now read-only.

Commit

Permalink
Corewlan scan (#39)
Browse files Browse the repository at this point in the history
* chore: Move snippet code

These aren't true tests and shouldn't be labeled as such.

* test: Add wireless scanning snippets

* fix: Scan for wireless networks via CoreWLAN

Fix #37

* style: Fix typos

* fix: Use the scan results

No need to retrieve from the cache.
  • Loading branch information
clburlison authored Mar 13, 2018
1 parent 99191a5 commit ce8bd9b
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 19 deletions.
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions examples/snippets/wireless_scan_objc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/python
import objc

bundle_path = '/System/Library/Frameworks/CoreWLAN.framework'
objc.loadBundle('CoreWLAN',
bundle_path=bundle_path,
module_globals=globals())


def wireless_scan():
"""Scan available wireless networks.
Returns:
Sorted array of dictionaries with rssi, bssid, and ssid_str from
strongest rssi value to weakest.
"""
iface = CWInterface.interface()
results, error = iface.scanForNetworksWithName_includeHidden_error_(
None, True, None)
if error:
return error
values = []
for i in results:
if i.ssid() is None:
continue
wifi_stats = {'RSSI': i.rssiValue(),
'BSSID': i.bssid(),
'SSID_STR': i.ssid()
}
values.append(wifi_stats)
return sorted(values, key=lambda k: k['RSSI'], reverse=True)


print wireless_scan()
30 changes: 30 additions & 0 deletions examples/snippets/wireless_scan_subprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/python
import subprocess
import plistlib


def wireless_scan():
"""Scan available wireless networks.
Returns:
Sorted array of dictionaries with rssi, bssid, and ssid_str from
strongest rssi value to weakest.
"""
ssid_scan = subprocess.check_output(
['/System/Library/PrivateFrameworks/'
'Apple80211.framework/Versions/A/'
'Resources/airport', '-s', '--xml']
)
ssid_scan = plistlib.readPlistFromString(ssid_scan)
values = []
for i, val in enumerate(ssid_scan):
wifi_stats = {'RSSI': val.get('RSSI'),
'BSSID': val.get('BSSID'),
'SSID_STR': val.get('SSID_STR')
}
values.append(wifi_stats)
return sorted(values, key=lambda k: k['RSSI'], reverse=True)


print wireless_scan()
Empty file.
Empty file.
35 changes: 16 additions & 19 deletions pkgroot/Library/Application Support/pinpoint/bin/pinpoint
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ class wireless(object):
objc.loadBundle('CoreWLAN',
bundle_path=bundle_path,
module_globals=globals())
iface = CWInterface.interface()

def get_wireless_interface(self):
"""Get the current wireless interface.
Expand Down Expand Up @@ -649,32 +650,29 @@ class wireless(object):
return wifi_power

def wireless_scan(self):
"""Scan aviable wireless networks.
"""Scan available wireless networks.
Returns:
Sorted array of dictionaries with rssi, bssid, and ssid_str from
strongest rssi value to weakest.
"""
logging.info("Scanning for wireless networks...")
ssid_scan = subprocess.check_output(
['/System/Library/PrivateFrameworks/'
'Apple80211.framework/Versions/A/'
'Resources/airport', '-s', '--xml']
)
# TODO (clburlison): This can error with
# FoundationPlist.FoundationPlist.NSPropertyListSerializationException: Encountered unexpected EOF # noqa
ssid_scan = FoundationPlist.readPlistFromString(ssid_scan)
logging.info("Scanning complete")
values = []
for i, val in enumerate(ssid_scan):
wifi_stats = {'RSSI': val.get('RSSI'),
'BSSID': val.get('BSSID'),
'SSID_STR': val.get('SSID_STR')
logging.info("Scanning for wireless networks...")
results, error = self.iface.\
scanForNetworksWithName_includeHidden_error_(None, True, None)
if error:
logging.debug("Error scanning for wireless networks: {}"
"".format(str(error)))
return values
for i in results:
if i.ssid() is None:
continue
wifi_stats = {'RSSI': i.rssiValue(),
'BSSID': i.bssid(),
'SSID_STR': i.ssid()
}
values.append(wifi_stats)

# Sort our values by RSSI to increase accuracy of location
sorted_list = sorted(values, key=lambda k: k['RSSI'], reverse=True)
logging.debug("Local wireless networks: \n%s", sorted_list)
return sorted_list
Expand All @@ -690,7 +688,6 @@ class wireless(object):
"""
wifi_power = self.wireless_status()
iface = CWInterface.interface()
value = value.lower()
if value == "on":
setPower = True
Expand All @@ -699,7 +696,7 @@ class wireless(object):
if (wifi_power == 'Off' and value == "on") or (
wifi_power == 'On' and value == "off"
):
iface.setPower_error_(setPower, None)
self.iface.setPower_error_(setPower, None)
logging.debug("Wireless adapter has been turned %s.", value)
sleep(5)
return True
Expand Down

0 comments on commit ce8bd9b

Please sign in to comment.