Added access-point and disabled Wifi configuration.
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Wed, 21 Mar 2018 08:53:58 +0000 (01:53 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Wed, 21 Mar 2018 08:53:58 +0000 (01:53 -0700)
CHANGELOG.md
scripts/config-wifi
src/jade/templates/admin-network-view.jade
src/js/admin-network-view.js
src/py/bbctrl/Web.py
src/stylus/style.styl

index c6104b13d4db0ccb8d76fa697fd14716140ef564..ceba3288175466cb92d7361ddee6113f3a22420d 100644 (file)
@@ -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
index 4b6f9a48a6a039802603bc7dcd891cd80d1d7f3f..d5ab71eed403705aef6ea5d82e3d6e4e275f42d0 100755 (executable)
@@ -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 <SSID>      Set SSID."
     echo "  -p <PASS>      Set password."
     echo "  -c <CHANNEL>   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
 
 
index e31cb3ad76ee63aea6a29158dc7a855827c3b957..1ff0b4b84e11ef106f80f023c58213af7118381a 100644 (file)
@@ -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 &nbsp;{{wifi_mode}}
+          tr(v-if="wifi_mode == 'ap'")
+            th Channel
+            td &nbsp;{{wifi_ch}}
+          tr(v-if="wifi_mode != 'disabled'")
             th SSID
             td &nbsp;{{wifi_ssid}}
-          tr
+          tr(v-if="wifi_mode != 'disabled'")
             th Auth
-            td &nbsp;{{wifi_pass ? 'Password' : 'Open'}}
+            td &nbsp;{{wifi_pass ? 'WPA2' : 'Open'}}
 
       div(slot="footer")
         button.pure-button(@click="wifiConfirm = false") Cancel
index 143b1572e8e2c2feef232d690774fbf849206983..467d6b7d709982ae8c8878eb905198d25d999788 100644 (file)
@@ -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));
       })
     }
index a6d32ff0e18738d596ddcd7affbae26b55bbab97..d9a1332952ad49d987e176caf1935ca90bc641d0 100644 (file)
@@ -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
 
index f8ab2dd6a89b307b3ca763383ba71e2ea6eecf6d..ab213bc4de85f6830949e243684d9f3b0615d8a2 100644 (file)
@@ -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