From fe07bf5bfb8f5f9421aa1d404b25ec3e647fc5f0 Mon Sep 17 00:00:00 2001
From: j1nx
Date: Thu, 28 May 2020 09:12:54 +0200
Subject: [PATCH] MycroftOS: Refactoring wifi setup system
---
.../network/{wifi.network.ap => ap0.network} | 0
.../{wifi.network.client => wlan0.network} | 0
.../opt/mycroft/wifisetup/app.py | 23 ++++++------
buildroot-external/package/wifi-ap/wifi-ap.mk | 37 +++----------------
.../package/wifi-ap/wifi-setup.service | 13 +++----
.../package/wifi-ap/wireless-mode-ap.service | 16 --------
.../wifi-ap/wireless-mode-client.service | 15 --------
.../package/wifi-ap/wpa_supplicant-ap0.conf | 2 +-
.../wifi-ap/wpa_supplicant-ap0.service | 18 ---------
.../wifi-ap/wpa_supplicant@ap0.service | 18 +++++++++
.../systemd/system-preset/10-mycroftos.preset | 5 ++-
11 files changed, 45 insertions(+), 102 deletions(-)
rename buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/{wifi.network.ap => ap0.network} (100%)
rename buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/{wifi.network.client => wlan0.network} (100%)
delete mode 100644 buildroot-external/package/wifi-ap/wireless-mode-ap.service
delete mode 100644 buildroot-external/package/wifi-ap/wireless-mode-client.service
delete mode 100644 buildroot-external/package/wifi-ap/wpa_supplicant-ap0.service
create mode 100644 buildroot-external/package/wifi-ap/wpa_supplicant@ap0.service
diff --git a/buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/wifi.network.ap b/buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/ap0.network
similarity index 100%
rename from buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/wifi.network.ap
rename to buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/ap0.network
diff --git a/buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/wifi.network.client b/buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/wlan0.network
similarity index 100%
rename from buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/wifi.network.client
rename to buildroot-external/board/raspberrypi/rootfs-overlay/etc/systemd/network/wlan0.network
diff --git a/buildroot-external/board/raspberrypi/rootfs-overlay/opt/mycroft/wifisetup/app.py b/buildroot-external/board/raspberrypi/rootfs-overlay/opt/mycroft/wifisetup/app.py
index bee3d5a7..ec1a803a 100644
--- a/buildroot-external/board/raspberrypi/rootfs-overlay/opt/mycroft/wifisetup/app.py
+++ b/buildroot-external/board/raspberrypi/rootfs-overlay/opt/mycroft/wifisetup/app.py
@@ -28,12 +28,12 @@ def save_credentials():
create_wpa_supplicant(ssid, wifi_key)
- # Call reboot_device() in a thread otherwise the reboot will prevent
+ # Call reconfigure_device() in a thread otherwise the reconfigure will prevent
# the response from getting to the browser
- def sleep_and_reboot():
+ def sleep_and_reconfigure():
time.sleep(2)
- reboot_device()
- t = Thread(target=sleep_and_reboot)
+ reconfigure_device()
+ t = Thread(target=sleep_and_reconfigure)
t.start()
return render_template('save_credentials.html', ssid = ssid)
@@ -43,12 +43,12 @@ def skip_wifi():
empty_wpa_supplicant()
- # Call reboot_device() in a thread otherwise the reboot will prevent
+ # Call reconfigure_device() in a thread otherwise the reconfigure will prevent
# the response from getting to the browser
- def sleep_and_reboot():
+ def sleep_and_reconfigure():
time.sleep(2)
- reboot_device()
- t = Thread(target=sleep_and_reboot)
+ reconfigure_device()
+ t = Thread(target=sleep_and_reconfigure)
t.start()
return render_template('cancelled_wifi.html')
@@ -104,9 +104,10 @@ def empty_wpa_supplicant():
os.system('mv wpa_supplicant-wlan0.conf.tmp /etc/wpa_supplicant/wpa_supplicant-wlan0.conf')
-def reboot_device():
- os.system('reboot')
+def reconfigure_device():
+ os.system('systemctl disable wpa_supplicant@ap0.service')
+ os.system('systemctl enable wpa_supplicant@wlan0.service')
+ os.system('systemctl start wpa_supplicant@wlan0.service')
if __name__ == '__main__':
- os.system("fbv -f -d 1 /opt/mycroft/wifisetup/static/images/wifi.png > /dev/null 2>&1")
app.run(host = '0.0.0.0', port = '88')
diff --git a/buildroot-external/package/wifi-ap/wifi-ap.mk b/buildroot-external/package/wifi-ap/wifi-ap.mk
index f2bd9bf0..daf3423f 100644
--- a/buildroot-external/package/wifi-ap/wifi-ap.mk
+++ b/buildroot-external/package/wifi-ap/wifi-ap.mk
@@ -15,45 +15,20 @@ define WIFI_AP_INSTALL_TARGET_CMDS
$(INSTALL) -m 644 -D $(@D)/wpa_supplicant-ap0.conf \
$(TARGET_DIR)/etc/wpa_supplicant/wpa_supplicant-ap0.conf
- $(INSTALL) -D -m 644 $(@D)/wpa_supplicant-ap0.service \
- $(TARGET_DIR)/usr/lib/systemd/system/wpa_supplicant-ap0.service
- mkdir -p $(TARGET_DIR)/etc/systemd/system/sys-subsystem-net-devices-ap0.device.wants
- ln -fs ../../../../usr/lib/systemd/system/wpa_supplicant-ap0.service \
- $(TARGET_DIR)/etc/systemd/system/sys-subsystem-net-devices-ap0.device.wants/wpa_supplicant-ap0.service
-
+ $(INSTALL) -D -m 644 $(@D)/wpa_supplicant@ap0.service \
+ $(TARGET_DIR)/usr/lib/systemd/system/wpa_supplicant@ap0.service
+
$(INSTALL) -D -m 644 $(@D)/wifi-setup.service \
$(TARGET_DIR)/usr/lib/systemd/system/wifi-setup.service
- ln -fs ../../../../usr/lib/systemd/system/wifi-setup.service \
- $(TARGET_DIR)/etc/systemd/system/sys-subsystem-net-devices-ap0.device.wants/wifi-setup.service
-
+
$(INSTALL) -D -m 644 $(@D)/dnsmasq.service \
$(TARGET_DIR)/usr/lib/systemd/system/dnsmasq.service
- ln -fs ../../../../usr/lib/systemd/system/dnsmasq.service \
- $(TARGET_DIR)/etc/systemd/system/sys-subsystem-net-devices-ap0.device.wants/dnsmasq.service
-
- $(INSTALL) -D -m 644 $(@D)/wireless-mode-ap.service \
- $(TARGET_DIR)/usr/lib/systemd/system/wireless-mode-ap.service
- mkdir -p $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
- ln -fs ../../../../usr/lib/systemd/system/wireless-mode-ap.service \
- $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/wireless-mode-ap.service
-
- $(INSTALL) -D -m 644 $(@D)/wireless-mode-client.service \
- $(TARGET_DIR)/usr/lib/systemd/system/wireless-mode-client.service
- mkdir -p $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
- ln -fs ../../../../usr/lib/systemd/system/wireless-mode-client.service \
- $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/wireless-mode-client.service
-
+
$(INSTALL) -D -m 644 $(@D)/nginx.service \
$(TARGET_DIR)/usr/lib/systemd/system/nginx.service
- mkdir -p $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
- ln -fs ../../../../usr/lib/systemd/system/nginx.service \
- $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/nginx.service
-
+
$(INSTALL) -D -m 644 $(@D)/iptables.service \
$(TARGET_DIR)/usr/lib/systemd/system/iptables.service
- mkdir -p $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
- ln -fs ../../../../usr/lib/systemd/system/iptables.service \
- $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/iptables.service
endef
$(eval $(generic-package))
diff --git a/buildroot-external/package/wifi-ap/wifi-setup.service b/buildroot-external/package/wifi-ap/wifi-setup.service
index 64ae57d8..d4d7fc5c 100644
--- a/buildroot-external/package/wifi-ap/wifi-setup.service
+++ b/buildroot-external/package/wifi-ap/wifi-setup.service
@@ -1,18 +1,15 @@
[Unit]
-Description=MycroftOS-WiFiSetup webbased configurator
-BindsTo=sys-subsystem-net-devices-ap0.device
-After=sys-subsystem-net-devices-ap0.device
+Description=MycroftOS WiFi Setup - webbased configurator
+After=wpa_supplicant@ap0.service
+Wants=wpa_supplicant@ap0.service
+Conflicts=wpa_supplicant@wlan0.service
Before=network.target
Wants=network.target
-# check existence of configuration file
-ConditionPathExists= /etc/wpa_supplicant/wpa_supplicant-ap0.conf
-ConditionPathExists=!/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
-
[Service]
Type=idle
WorkingDirectory=/opt/mycroft/wifisetup
ExecStart=/usr/bin/python3 app.py > /dev/null 2>&1
[Install]
-WantedBy=sys-subsystem-net-devices-ap0.device
+WantedBy=multi-user.target
diff --git a/buildroot-external/package/wifi-ap/wireless-mode-ap.service b/buildroot-external/package/wifi-ap/wireless-mode-ap.service
deleted file mode 100644
index 189324de..00000000
--- a/buildroot-external/package/wifi-ap/wireless-mode-ap.service
+++ /dev/null
@@ -1,16 +0,0 @@
-[Unit]
-Description=Wireless mode set AP
-Before=wpa_supplicant@.service
-Before=systemd-networkd.service
-Wants=network.target
-
-# check existence of configuration file
-ConditionPathExists= /etc/wpa_supplicant/wpa_supplicant-ap0.conf
-ConditionPathExists=!/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
-
-[Service]
-Type=oneshot
-ExecStart=/bin/ln -sf wifi.network.ap /etc/systemd/network/wifi.network
-
-[Install]
-WantedBy=multi-user.target
diff --git a/buildroot-external/package/wifi-ap/wireless-mode-client.service b/buildroot-external/package/wifi-ap/wireless-mode-client.service
deleted file mode 100644
index 0cd1df09..00000000
--- a/buildroot-external/package/wifi-ap/wireless-mode-client.service
+++ /dev/null
@@ -1,15 +0,0 @@
-[Unit]
-Description=Wireless mode set client
-Before=wpa_supplicant@.service
-Before=systemd-networkd.service
-Wants=network.target
-
-# check existence of configuration file
-ConditionPathExists=/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
-
-[Service]
-Type=oneshot
-ExecStart=/bin/ln -sf wifi.network.client /etc/systemd/network/wifi.network
-
-[Install]
-WantedBy=multi-user.target
diff --git a/buildroot-external/package/wifi-ap/wpa_supplicant-ap0.conf b/buildroot-external/package/wifi-ap/wpa_supplicant-ap0.conf
index 54700df9..23279643 100644
--- a/buildroot-external/package/wifi-ap/wpa_supplicant-ap0.conf
+++ b/buildroot-external/package/wifi-ap/wpa_supplicant-ap0.conf
@@ -2,7 +2,7 @@ ctrl_interface=/var/run/wpa_supplicant
update_config=1
network={
- ssid="MycroftOS-WiFiSetup"
+ ssid="MYCROFT"
mode=2
key_mgmt=NONE
}
diff --git a/buildroot-external/package/wifi-ap/wpa_supplicant-ap0.service b/buildroot-external/package/wifi-ap/wpa_supplicant-ap0.service
deleted file mode 100644
index 3b181de6..00000000
--- a/buildroot-external/package/wifi-ap/wpa_supplicant-ap0.service
+++ /dev/null
@@ -1,18 +0,0 @@
-[Unit]
-Description=WPA supplicant daemon (interface-specific version)
-Requires=sys-subsystem-net-devices-ap0.device
-After=sys-subsystem-net-devices-ap0.device
-Before=network.target
-Wants=network.target
-
-# check existence of configuration file
-ConditionPathExists=!/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
-
-# NetworkManager users will probably want the dbus version instead.
-
-[Service]
-Type=simple
-ExecStart=/usr/sbin/wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant-ap0.conf -iap0
-
-[Install]
-WantedBy=sys-subsystem-net-devices-ap0.device
diff --git a/buildroot-external/package/wifi-ap/wpa_supplicant@ap0.service b/buildroot-external/package/wifi-ap/wpa_supplicant@ap0.service
new file mode 100644
index 00000000..cdb3e5db
--- /dev/null
+++ b/buildroot-external/package/wifi-ap/wpa_supplicant@ap0.service
@@ -0,0 +1,18 @@
+[Unit]
+Description=WPA supplicant daemon (interface-specific version)
+Requires=sys-subsystem-net-devices-wlan0.device
+After=sys-subsystem-net-devices-wlan0.device
+Conflicts=wpa_supplicant@wlan0.service
+Before=network.target
+Wants=network.target
+
+# NetworkManager users will probably want the dbus version instead.
+
+[Service]
+Type=simple
+ExecStartPre=/usr/sbin/iw dev wlan0 interface add ap0 type __ap
+ExecStart=/usr/sbin/wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant-ap0.conf -Dnl80211,wext -iap0
+ExecStopPost=/usr/sbin/iw dev ap0 del
+
+[Install]
+Alias=multi-user.target.wants/wpa_supplicant@%i.service
diff --git a/buildroot-external/rootfs-overlay/usr/lib/systemd/system-preset/10-mycroftos.preset b/buildroot-external/rootfs-overlay/usr/lib/systemd/system-preset/10-mycroftos.preset
index 72017c9f..aafd16b3 100644
--- a/buildroot-external/rootfs-overlay/usr/lib/systemd/system-preset/10-mycroftos.preset
+++ b/buildroot-external/rootfs-overlay/usr/lib/systemd/system-preset/10-mycroftos.preset
@@ -1,4 +1,4 @@
-enable wpa_supplicant@wlan0.service
+enable wpa_supplicant@ap0.service
enable firstboot.service
enable prepare_system.service
@@ -20,8 +20,8 @@ enable mycroft-audio.service
enable mycroft-skills.service
enable mycroft-enclosure.service
enable mycroft-gui.service
+enable mycroft-splash-quit.service
-disable mycroft-splash-quit.service
disable getty@.service
disable mosquitto.service
disable motion.service
@@ -31,3 +31,4 @@ disable snapclient.service
disable snapserver.service
disable spotifyd.service
+disable wpa_supplicant@wlan0.service