From 18b4fcb415f74088e46dd46d2c0c3aaebae27bb9 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Wed, 21 Mar 2018 01:53:58 -0700 Subject: [PATCH] Added access-point and disabled Wifi configuration. --- CHANGELOG.md | 7 +- scripts/config-wifi | 114 ++++++++++++--------- src/jade/templates/admin-network-view.jade | 30 +++++- src/js/admin-network-view.js | 11 +- src/py/bbctrl/Web.py | 26 +++-- src/stylus/style.styl | 9 ++ 6 files changed, 132 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6104b1..ceba328 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,10 @@ Buildbotics CNC Controller Firmware Change Log - Fixed disappearing GCode in Web. - More efficient GCode scrolling with very large files. - Fully functional soft-limited jogging. - - Added basic Wifi configuration to admin tab. - - Fixed broken hostname change redirect. - - Split admin page. + - Added client and access-point Wifi configuration. + - Fixed broken hostname Web redirect after change. + - Split admin page -> General & Network. + - Improved calculation of junction velocity limits. ## v0.3.19 - Fixed stopping problems. #127 diff --git a/scripts/config-wifi b/scripts/config-wifi index 4b6f9a4..d5ab71e 100755 --- a/scripts/config-wifi +++ b/scripts/config-wifi @@ -1,6 +1,7 @@ #!/bin/bash -e AP=false +DISABLE=false SSID= PASS= CHANNEL=7 @@ -13,14 +14,31 @@ DHCPCD_CFG=/etc/dhcpcd.conf WPA_CFG=/etc/wpa_supplicant/wpa_supplicant.conf -function query_ssid() { +function query_config() { if [ -e $WLAN0_CFG ]; then - grep wpa-ssid $WLAN0_CFG | sed 's/^[[:space:]]*wpa-ssid "\([^"]*\)"/\1/' + SSID=$(grep wpa-ssid $WLAN0_CFG | + sed 's/^[[:space:]]*wpa-ssid "\([^"]*\)"/\1/') + echo "{\"ssid\": \"$SSID\", \"mode\": \"client\"}" + else - if [ -e $HOSTAPD_CFG ]; then - grep ^ssid $HOSTAPD_CFG | sed 's/^ssid="\(.*\)$"/\1/' + if [ -e $HOSTAPD_CFG -a -e /etc/default/hostapd ]; then + SSID=$(grep ^ssid= $HOSTAPD_CFG | sed 's/^ssid=\(.*\)$/\1/') + CHANNEL=$(grep ^channel= $HOSTAPD_CFG | + sed 's/^channel=\(.*\)$/\1/') + + echo -n "{\"ssid\": \"$SSID\", " + echo "\"channel\": $CHANNEL, \"mode\": \"ap\"}" + + else + echo "{\"mode\": \"disabled\"}" fi fi + +} + + +function disable_wifi() { + rm -f $WLAN0_CFG $HOSTAPD_CFG /etc/default/hostapd } @@ -76,14 +94,10 @@ function configure_dhcpcd() { function configure_wifi() { - # Disable AP - service hostapd stop || true - rm -f /etc/default/hostapd - - configure_wlan0 > $WLAN0_CFG - configure_wpa > $WPA_CFG - configure_dhcpcd > $DHCPCD_CFG - service dhcpcd restart + disable_wifi + configure_wlan0 > $WLAN0_CFG + configure_wpa > $WPA_CFG + configure_dhcpcd > $DHCPCD_CFG } @@ -121,8 +135,7 @@ function is_installed() { function configure_ap() { - # Disable wifi config - rm -f $WLAN0_CFG + disable_wifi # Install packages ( @@ -147,13 +160,9 @@ function configure_ap() { echo 1 > /proc/sys/net/ipv4/ip_forward # Enable IP masquerading - iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE + iptables -t nat -F + iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE iptables-save > /etc/iptables/rules.v4 - - # Start services - service dhcpcd restart - service dnsmasq restart - service hostapd restart } @@ -165,11 +174,12 @@ function usage() { echo "OPTIONS:" echo echo " -a Configure access point." + echo " -d Disable wifi." echo " -r Reboot when done." echo " -s Set SSID." echo " -p Set password." echo " -c Set wifi channel." - echo " -g Report SSID and exit." + echo " -j Report wifi config as JSON data." echo } @@ -178,11 +188,12 @@ function usage() { while [ $# -ne 0 ]; do case "$1" in -a) AP=true ;; + -d) DISABLE=true ;; -r) REBOOT=true; ;; -s) SSID="$2"; shift ;; -p) PASS="$2"; shift ;; -c) CHANNEL="$2"; shift ;; - -g) query_ssid; exit 0 ;; + -j) query_config; exit 0 ;; -h) usage @@ -199,41 +210,46 @@ while [ $# -ne 0 ]; do done -# Check args -function clean_str() { - echo "$1" | tr -d '\n\r"' -} +if $DISABLE; then + disable_wifi -SSID=$(clean_str "$SSID") -PASS=$(clean_str "$PASS") +else + # Check args + function clean_str() { + echo "$1" | tr -d '\n\r"' + } -LANG=C LC_ALL=C # For correct string byte length + SSID=$(clean_str "$SSID") + PASS=$(clean_str "$PASS") -if [ ${#SSID} -eq 0 -o 32 -lt ${#SSID} ]; then - echo "Invalid or missing SSID '$SSID'" - exit 1 -fi + LANG=C LC_ALL=C # For correct string byte length -if [ ${#PASS} -lt 8 -o 128 -lt ${#PASS} ]; then - echo "Invalid passsword" - exit 1 -fi + if [ ${#SSID} -eq 0 -o 32 -lt ${#SSID} ]; then + echo "Invalid or missing SSID '$SSID'" + exit 1 + fi -echo "$CHANNEL" | grep '^[0-9]\{1,2\}' > /dev/null -if [ $? -ne 0 ]; then - echo "Invalid channel '$CHANNEL'" - exit 1 -fi + if [ ${#PASS} -lt 8 -o 128 -lt ${#PASS} ]; then + echo "Invalid passsword" + exit 1 + fi + echo "$CHANNEL" | grep '^[0-9]\{1,2\}' > /dev/null + if [ $? -ne 0 ]; then + echo "Invalid channel '$CHANNEL'" + exit 1 + fi -# Execute -if $AP; then - echo "Configuring Wifi access point" - configure_ap -else - echo "Configuring Wifi" - configure_wifi + # Execute + if $AP; then + echo "Configuring Wifi access point" + configure_ap + + else + echo "Configuring Wifi" + configure_wifi + fi fi diff --git a/src/jade/templates/admin-network-view.jade b/src/jade/templates/admin-network-view.jade index e31cb3a..1ff0b4b 100644 --- a/src/jade/templates/admin-network-view.jade +++ b/src/jade/templates/admin-network-view.jade @@ -72,14 +72,28 @@ script#admin-network-view-template(type="text/x-template") h2 Wifi Setup .pure-form.pure-form-aligned .pure-control-group - label(for="ssid") SSID + label(for="wifi_mode") Mode + select(name="wifi_mode", v-model="wifi_mode", + title="Select client or access point mode") + option(value="disabled") Disabled + option(value="client") Client + option(value="ap") Access Point + button.pure-button.pure-button-primary(@click="wifiConfirm = true", + v-if="wifi_mode == 'disabled'") Set + .pure-control-group(v-if="wifi_mode == 'ap'") + label(for="wifi_ch") Channel + select(name="wifi_ch", v-model="wifi_ch") + each ch in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] + option(value="#{ch}")= ch + .pure-control-group(v-if="wifi_mode != 'disabled'") + label(for="ssid") Network (SSID) input(name="ssid", v-model="wifi_ssid") - .pure-control-group + .pure-control-group(v-if="wifi_mode != 'disabled'") label(for="wifi_pass") Password input(name="wifi_pass", v-model="wifi_pass", type="password") button.pure-button.pure-button-primary(@click="wifiConfirm = true") Set - message(:show.sync="wifiConfirm") + message.wifi-confirm(:show.sync="wifiConfirm") h3(slot="header") Configure Wifi and reboot? div(slot="body") p @@ -87,11 +101,17 @@ script#admin-network-view-template(type="text/x-template") | automatically reboot. table tr + th Mode + td  {{wifi_mode}} + tr(v-if="wifi_mode == 'ap'") + th Channel + td  {{wifi_ch}} + tr(v-if="wifi_mode != 'disabled'") th SSID td  {{wifi_ssid}} - tr + tr(v-if="wifi_mode != 'disabled'") th Auth - td  {{wifi_pass ? 'Password' : 'Open'}} + td  {{wifi_pass ? 'WPA2' : 'Open'}} div(slot="footer") button.pure-button(@click="wifiConfirm = false") Cancel diff --git a/src/js/admin-network-view.js b/src/js/admin-network-view.js index 143b157..467d6b7 100644 --- a/src/js/admin-network-view.js +++ b/src/js/admin-network-view.js @@ -47,7 +47,9 @@ module.exports = { current: '', password: '', password2: '', + wifi_mode: 'client', wifi_ssid: '', + wifi_ch: undefined, wifi_pass: '', wifiConfirm: false, rebooting: false @@ -65,7 +67,9 @@ module.exports = { }.bind(this)); api.get('wifi').done(function (config) { + this.wifi_mode = config.mode; this.wifi_ssid = config.ssid; + this.wifi_ch = config.channel; }.bind(this)); }, @@ -134,11 +138,14 @@ module.exports = { this.wifiConfirm = false; this.rebooting = true; - api.put('wifi', { + var config = { + mode: this.wifi_mode, + channel: this.wifi_ch, ssid: this.wifi_ssid, pass: this.wifi_pass + } - }).fail(function (error) { + api.put('wifi', config).fail(function (error) { alert('Failed to configure WiFi: ' + JSON.stringify(error)); }) } diff --git a/src/py/bbctrl/Web.py b/src/py/bbctrl/Web.py index a6d32ff..d9a1332 100644 --- a/src/py/bbctrl/Web.py +++ b/src/py/bbctrl/Web.py @@ -117,16 +117,30 @@ class HostnameHandler(bbctrl.APIHandler): class WifiHandler(bbctrl.APIHandler): def get(self): - ssid = '' + data = {'ssid': '', 'channel': 0} try: - ssid = call_get_output(['config-wifi', '-g']) + data = json.loads(call_get_output(['config-wifi', '-j'])) except: pass - self.write_json({'ssid': ssid}) + self.write_json(data) def put(self): - if 'ssid' in self.json and 'pass' in self.json: - if subprocess.call(['config-wifi', '-s', self.json['ssid'], - '-p', self.json['pass']]) == 0: + if 'mode' in self.json: + cmd = ['config-wifi', '-r'] + mode = self.json['mode'] + + if mode == 'disabled': cmd += ['-d'] + elif 'ssid' in self.json: + cmd += ['-s', self.json['ssid']] + + if mode == 'ap': + cmd += ['-a'] + if 'channel' in self.json: + cmd += ['-c', self.json['channel']] + + if 'pass' in self.json: + cmd += ['-p', self.json['pass']] + + if subprocess.call(cmd) == 0: self.write_json('ok') return diff --git a/src/stylus/style.styl b/src/stylus/style.styl index f8ab2dd..ab213bc 100644 --- a/src/stylus/style.styl +++ b/src/stylus/style.styl @@ -615,6 +615,15 @@ label.file-upload margin-bottom 1em clear both + +.wifi-confirm table + th + text-align right + + th, td + padding 0 4px + + .cheat-sheet table border-collapse collapse -- 2.27.0