Skip to content

Commit

Permalink
The hostapd will now be edited according to the user settings specifi…
Browse files Browse the repository at this point in the history
…ed in the raspiwifi.conf file
  • Loading branch information
jasbur committed Jan 23, 2019
1 parent 796f6b5 commit 952b34d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
12 changes: 8 additions & 4 deletions libs/reset_device/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
serial_last_four = subprocess.check_output(['cat', '/proc/cpuinfo'])[-5:-1].decode('utf-8')
config_hash = reset_lib.config_file_hash()
ssid_prefix = config_hash['ssid_prefix'] + " "
hostapd_reset_required = reset_lib.hostapd_reset_check(ssid_prefix)
reboot_required = False


if hostapd_reset_required == True:
reset_lib.update_hostapd(ssid_prefix, serial_last_four)
os.system('reboot')
reboot_required = reset_lib.wpa_check_activate(config_hash['wpa_enabled'], config_hash['wpa_key'])

if reset_lib.ssid_is_correct(ssid_prefix) == False:
reboot_required = reset_lib.update_hostapd(ssid_prefix, serial_last_four)

if reboot_required == True:
print('REBOOT')

# This is the main logic loop waiting for a button to be pressed on GPIO 18 for 10 seconds.
# If that happens the device will reset to its AP Host mode allowing for reconfiguration on a new network.
Expand Down
33 changes: 29 additions & 4 deletions libs/reset_device/reset_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def config_file_hash():

return config_hash

def hostapd_reset_check(ssid_prefix):
def ssid_is_correct(ssid_prefix):
hostapd_conf = open('/etc/hostapd/hostapd.conf', 'r')
reset_required = True
ssid_correct = False

for line in hostapd_conf:
if ssid_prefix in line:
reset_required = False
ssid_correct = True

return reset_required
return ssid_correct

def update_wpa_key(wpa_key):
with fileinput.FileInput('/etc/hostapd/hostapd.conf', inplace=True) as hostapd_conf:
Expand All @@ -31,12 +31,37 @@ def update_wpa_key(wpa_key):
else:
print(line, end = '')

def wpa_check_activate(wpa_enabled, wpa_key):
wpa_active = False
reboot_required = False

with open('/etc/hostapd/hostapd.conf') as hostapd_conf:
for line in hostapd_conf:
if 'wpa_passphrase' in line:
wpa_active = True

if wpa_enabled == '1' and wpa_active == False:
reboot_required = True
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/hostapd.conf.wpa /etc/hostapd/hostapd.conf')
update_wpa_key(wpa_key)

if wpa_enabled == '0' and wpa_active == True:
reboot_required = True
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/hostapd.conf.nowpa /etc/hostapd/hostapd.conf')

return reboot_required

def update_hostapd(ssid_prefix, serial_last_four):
reboot_required = False

with fileinput.FileInput("/etc/hostapd/hostapd.conf", inplace=True) as file:
for line in file:
reboot_required = True
print(line.replace("temp-ssid", ssid_prefix + serial_last_four), end='')
file.close

return reboot_required

def is_wifi_active():
iwconfig_out = subprocess.check_output(['iwconfig']).decode('utf-8')
wifi_active = True
Expand Down
8 changes: 0 additions & 8 deletions libs/reset_device/test.py

This file was deleted.

0 comments on commit 952b34d

Please sign in to comment.