|
44 | 44 | netconfdir = confdir+"networks/" |
45 | 45 | sysconfdir = "/appconfigs/" |
46 | 46 | datadir = "/mnt/SDCARD/App/Wifi/data/" |
| 47 | +autoconnect_enabled = False |
47 | 48 |
|
48 | 49 | surface = pygame.display.set_mode((320,240)) |
49 | 50 | selected_key = '' |
@@ -144,6 +145,30 @@ def enableiface(iface): |
144 | 145 | mac_addresses[iface] = getmac(iface) |
145 | 146 | return True |
146 | 147 |
|
| 148 | +def autoconnect(iface): |
| 149 | + check = checkinterfacestatus(iface) |
| 150 | + if check: |
| 151 | + return False |
| 152 | + |
| 153 | + modal("Autoconnecting...") |
| 154 | + drawinterfacestatus() |
| 155 | + pygame.display.update() |
| 156 | + |
| 157 | + SU.Popen(['/bin/sed', '-i', "s/\"wifi\":\s*[01]/\"wifi\": 1/", '/appconfigs/system.json'], close_fds=True).wait() |
| 158 | + SU.Popen(['/customer/app/axp_test', 'wifion'], close_fds=True).wait() |
| 159 | + SU.Popen(['sleep', '2'], close_fds=True).wait() |
| 160 | + SU.Popen(['pkill', '-9', 'wpa_supplicant'], close_fds=True).wait() |
| 161 | + SU.Popen(['pkill', '-9', 'udhcpc'], close_fds=True).wait() |
| 162 | + SU.Popen(['pkill', '-9', 'hostapd'], close_fds=True).wait() |
| 163 | + SU.Popen(['pkill', '-9', 'dnsmasq'], close_fds=True).wait() |
| 164 | + while True: |
| 165 | + if SU.Popen(['/sbin/ifconfig', iface, 'up'], close_fds=True).wait() == 0: |
| 166 | + break |
| 167 | + time.sleep(0.1); |
| 168 | + SU.Popen(['/mnt/SDCARD/Koriki/bin/wpa_supplicant', '-B', '-D', 'nl80211', '-i', iface, '-c', '/appconfigs/wpa_supplicant.conf'], close_fds=True).wait() |
| 169 | + mac_addresses[iface] = getmac(iface) |
| 170 | + return True |
| 171 | + |
147 | 172 | def disableiface(iface): |
148 | 173 | SU.Popen(['pkill', '-9', 'wpa_supplicant'], close_fds=True).wait() |
149 | 174 | SU.Popen(['pkill', '-9', 'udhcpc'], close_fds=True).wait() |
@@ -455,33 +480,38 @@ def drawstatusbar(): # Set up the status bar |
455 | 480 | wlan_text.topleft = (2, 225) |
456 | 481 | surface.blit(wlantext, wlan_text) |
457 | 482 |
|
458 | | -def drawinterfacestatus(): # Interface status badge |
459 | | - global colors |
460 | | - wlanstatus = checkinterfacestatus(wlan) |
461 | | - if not wlanstatus: |
462 | | - wlanstatus = wlan+" is off." |
463 | | - else: |
464 | | - wlanstatus = getcurrentssid(wlan) |
465 | | - |
466 | | - wlantext = font_mono_small.render(wlanstatus, True, colors['white'], colors['lightbg']) |
467 | | - wlan_text = wlantext.get_rect() |
468 | | - wlan_text.topleft = (2, 225) |
469 | | - surface.blit(wlantext, wlan_text) |
470 | | - |
471 | | - # Note that the leading space here is intentional, to more cleanly overdraw any overly-long |
472 | | - # strings written to the screen beneath it (i.e. a very long ESSID) |
473 | | - if checkinterfacestatus(wlan): |
474 | | - text = font_mono_small.render(" "+getip(wlan), True, colors['white'], colors['lightbg']) |
475 | | - interfacestatus_text = text.get_rect() |
476 | | - interfacestatus_text.topright = (317, 225) |
477 | | - surface.blit(text, interfacestatus_text) |
478 | | - else: |
479 | | - mac = mac_addresses.get(wlan) # grabbed by enableiface() |
480 | | - if mac is not None: |
481 | | - text = font_mono_small.render(" "+mac, True, colors['white'], colors['lightbg']) |
482 | | - interfacestatus_text = text.get_rect() |
483 | | - interfacestatus_text.topright = (317, 225) |
484 | | - surface.blit(text, interfacestatus_text) |
| 483 | +def drawinterfacestatus(): # Interface status badge |
| 484 | + global colors |
| 485 | + wlanstatus = checkinterfacestatus(wlan) |
| 486 | + if not wlanstatus: |
| 487 | + wlanstatus = wlan+" is off." |
| 488 | + else: |
| 489 | + wlanstatus = getcurrentssid(wlan) |
| 490 | + |
| 491 | + wlantext = font_mono_small.render(wlanstatus, True, colors['white'], colors['lightbg']) |
| 492 | + wlan_text = wlantext.get_rect() |
| 493 | + wlan_text.topleft = (2, 225) |
| 494 | + surface.blit(wlantext, wlan_text) |
| 495 | + |
| 496 | + # Note that the leading space here is intentional, to more cleanly overdraw any overly-long |
| 497 | + # strings written to the screen beneath it (i.e. a very long ESSID) |
| 498 | + if checkinterfacestatus(wlan): |
| 499 | + ip_address = getip(wlan) |
| 500 | + if ip_address is None: # Handle case where no IP is assigned |
| 501 | + ip_address = " " # Display alternative message if no IP is available |
| 502 | + text = font_mono_small.render(" " + ip_address, True, colors['white'], colors['lightbg']) |
| 503 | + interfacestatus_text = text.get_rect() |
| 504 | + interfacestatus_text.topright = (317, 225) |
| 505 | + surface.blit(text, interfacestatus_text) |
| 506 | + else: |
| 507 | + mac = mac_addresses.get(wlan) # grabbed by enableiface() |
| 508 | + if mac is not None: |
| 509 | + text = font_mono_small.render(" " + mac, True, colors['white'], colors['lightbg']) |
| 510 | + else: |
| 511 | + text = font_mono_small.render(" ", True, colors['white'], colors['lightbg']) # Handle no MAC case |
| 512 | + interfacestatus_text = text.get_rect() |
| 513 | + interfacestatus_text.topright = (317, 225) |
| 514 | + surface.blit(text, interfacestatus_text) |
485 | 515 |
|
486 | 516 | def redraw(): |
487 | 517 | global colors |
@@ -596,6 +626,44 @@ def writeconfig(): # Write wireless configuration to disk |
596 | 626 | f2.write('}\n') |
597 | 627 | f2.close() |
598 | 628 |
|
| 629 | +def save_autoconnect_state(): |
| 630 | + config_file = "/mnt/SDCARD/App/Wifi/autoconnect_state.txt" |
| 631 | + with open(config_file, 'w') as f: |
| 632 | + f.write("autoconnect_enabled={}\n".format(autoconnect_enabled)) |
| 633 | + |
| 634 | +def load_autoconnect_state(): |
| 635 | + global autoconnect_enabled |
| 636 | + config_file = "/mnt/SDCARD/App/Wifi/autoconnect_state.txt" |
| 637 | + if os.path.exists(config_file): |
| 638 | + with open(config_file, 'r') as f: |
| 639 | + for line in f: |
| 640 | + key, value = line.strip().split("=") |
| 641 | + if key == "autoconnect_enabled": |
| 642 | + autoconnect_enabled = value == "True" |
| 643 | + |
| 644 | +def toggle_autoconnect(): |
| 645 | + global autoconnect_enabled |
| 646 | + if autoconnect_enabled: |
| 647 | + confirm = modal("Disable Autoconnect?", query=True) |
| 648 | + if confirm: |
| 649 | + autoconnect_enabled = False |
| 650 | + modal("Autoconnect is OFF", wait=True) |
| 651 | + redraw() |
| 652 | + else: |
| 653 | + active_menu = to_menu("main") |
| 654 | + redraw() |
| 655 | + else: |
| 656 | + confirm = modal("Enable Autoconnect?", query=True) |
| 657 | + if confirm: |
| 658 | + autoconnect_enabled = True |
| 659 | + modal("Autoconnect is ON", wait=True) |
| 660 | + redraw() |
| 661 | + else: |
| 662 | + active_menu = to_menu("main") |
| 663 | + redraw() |
| 664 | + |
| 665 | + save_autoconnect_state() |
| 666 | + |
599 | 667 | ## HostAP |
600 | 668 | def startap(): |
601 | 669 | global wlan |
@@ -1320,11 +1388,11 @@ def mainmenu(): |
1320 | 1388 | if mac == ap: |
1321 | 1389 | elems = ['AP info'] + elems |
1322 | 1390 | except: |
1323 | | - elems = ['Create ADHOC'] + elems |
| 1391 | + pass |
1324 | 1392 |
|
1325 | | - elems = ["Saved Networks", 'Scan for APs', "Manual Setup"] + elems |
| 1393 | + elems = ["Saved Networks", 'Scan for APs', "Manual Setup", 'Autoconnect'] + elems |
1326 | 1394 |
|
1327 | | - if checkinterfacestatus(wlan): |
| 1395 | + if checkinterfacestatus(wlan) and getcurrentssid(wlan) is not None: |
1328 | 1396 | elems = ['Disconnect'] + elems |
1329 | 1397 |
|
1330 | 1398 | menu.init(elems, surface) |
@@ -1478,6 +1546,19 @@ def convert_file_names(): |
1478 | 1546 | logoBar = LogoBar() |
1479 | 1547 |
|
1480 | 1548 | redraw() |
| 1549 | + |
| 1550 | + load_autoconnect_state() |
| 1551 | + |
| 1552 | + if autoconnect_enabled: |
| 1553 | + modal("Autoconnecting...") |
| 1554 | + autoconnect(wlan) |
| 1555 | + if not udhcpc_timeout(wlan, 30): |
| 1556 | + modal('Autoconnect failed!', wait=True) |
| 1557 | + else: |
| 1558 | + modal('Autoconnect successful!') |
| 1559 | + time.sleep(2) |
| 1560 | + sys.exit() |
| 1561 | + |
1481 | 1562 | while True: |
1482 | 1563 | time.sleep(0.01) |
1483 | 1564 | for event in pygame.event.get(): |
@@ -1648,6 +1729,9 @@ def convert_file_names(): |
1648 | 1729 |
|
1649 | 1730 | elif menu.get_selected() == 'Create ADHOC': |
1650 | 1731 | startap() |
| 1732 | + |
| 1733 | + elif menu.get_selected() == 'Autoconnect': |
| 1734 | + toggle_autoconnect() |
1651 | 1735 |
|
1652 | 1736 | elif menu.get_selected() == 'AP info': |
1653 | 1737 | apinfo() |
|
0 commit comments