[RFC PATCH v3 1/8] fff-hoods: add initial hood configuration package

Jan Kraus mayosemmel at googlemail.com
So Jun 12 14:17:20 CEST 2016


From: Tim Niemeyer <tim.niemeyer at mastersword.de>

Signed-off-by: Tim Niemeyer <tim.niemeyer at mastersword.de>

Rebased to current master
Fixed some Errors
removed dhcp configuration from fff-hoods because no dhcp server is in use
moved IPv6-Hood configuration from configurehood to configurenetwork
added check if IPv6 prefix is set in configurenetwork

Signed-off-by: Jan Kraus <mayosemmel at gmail.com>
---
 bsp/default/root_file_system/etc/rc.local          |   1 +
 src/packages/fff/fff-hoods/Makefile                |  39 ++++++
 .../fff/fff-hoods/files/etc/hoods/fuerth.hood      |  18 +++
 .../fff/fff-hoods/files/etc/hoods/nuernberg.hood   |  18 +++
 .../fff/fff-hoods/files/etc/hoods/test.hood        |  18 +++
 .../fff-hoods/files/etc/hoods/trainstation.hood    |  17 +++
 .../fff/fff-hoods/files/usr/sbin/configurehood     | 139 +++++++++++++++++++++
 .../fff-network/files/usr/sbin/configurenetwork    |   8 +-
 src/packages/fff/fff/Makefile                      |   3 +-
 9 files changed, 256 insertions(+), 5 deletions(-)
 create mode 100644 src/packages/fff/fff-hoods/Makefile
 create mode 100644 src/packages/fff/fff-hoods/files/etc/hoods/fuerth.hood
 create mode 100644 src/packages/fff/fff-hoods/files/etc/hoods/nuernberg.hood
 create mode 100644 src/packages/fff/fff-hoods/files/etc/hoods/test.hood
 create mode 100644 src/packages/fff/fff-hoods/files/etc/hoods/trainstation.hood
 create mode 100755 src/packages/fff/fff-hoods/files/usr/sbin/configurehood

diff --git a/bsp/default/root_file_system/etc/rc.local b/bsp/default/root_file_system/etc/rc.local
index bd972fb..c980d8e 100755
--- a/bsp/default/root_file_system/etc/rc.local
+++ b/bsp/default/root_file_system/etc/rc.local
@@ -2,6 +2,7 @@
 # Put your custom commands here that should be executed once
 # the system init finished. By default this file does nothing.
 
+/usr/sbin/configurehood
 /usr/sbin/configurenetwork
 
 touch /tmp/started
diff --git a/src/packages/fff/fff-hoods/Makefile b/src/packages/fff/fff-hoods/Makefile
new file mode 100644
index 0000000..ac48760
--- /dev/null
+++ b/src/packages/fff/fff-hoods/Makefile
@@ -0,0 +1,39 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=fff-hoods
+PKG_VERSION:=0.0.1
+PKG_RELEASE:=1
+
+PKG_BUILD_DIR:=$(BUILD_DIR)/fff-hoods
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/fff-hoods
+    SECTION:=base
+    CATEGORY:=Freifunk
+    TITLE:= Freifunk-Franken hoods
+    URL:=http://www.freifunk-franken.de
+    DEPENDS:=+mdns +fff-network
+endef
+
+define Package/fff-hoods/description
+    This package load and configures the current hood
+endef
+
+define Build/Prepare
+	echo "all: " > $(PKG_BUILD_DIR)/Makefile
+endef
+
+define Build/Configure
+	# nothing
+endef
+
+define Build/Compile
+	# nothing
+endef
+
+define Package/fff-hoods/install
+	$(CP) ./files/* $(1)/
+endef
+
+$(eval $(call BuildPackage,fff-hoods))
diff --git a/src/packages/fff/fff-hoods/files/etc/hoods/fuerth.hood b/src/packages/fff/fff-hoods/files/etc/hoods/fuerth.hood
new file mode 100644
index 0000000..fd5c9ea
--- /dev/null
+++ b/src/packages/fff/fff-hoods/files/etc/hoods/fuerth.hood
@@ -0,0 +1,18 @@
+{
+ "version": 1,
+ "hood": {
+  "name": "fuerth",
+  "bssid": "ca:ff:ee:ba:be:02",
+  "protocol": "batman-adv-v14",
+  "channel2": 1,
+  "mode2": "ht20",
+  "type2": "adhoc",
+  "channel5": 40,
+  "mode5": "ht40+",
+  "type5": "adhoc",
+  "location": { "lat": 49.478330, "lon": 10.990270 },
+ },
+ "network": {
+  "ula_prefix": "fdff:2::/64"
+ }
+}
diff --git a/src/packages/fff/fff-hoods/files/etc/hoods/nuernberg.hood b/src/packages/fff/fff-hoods/files/etc/hoods/nuernberg.hood
new file mode 100644
index 0000000..6c26ab3
--- /dev/null
+++ b/src/packages/fff/fff-hoods/files/etc/hoods/nuernberg.hood
@@ -0,0 +1,18 @@
+{
+ "version": 1,
+ "hood": {
+  "name": "nuernberg",
+  "bssid": "ca:ff:ee:ba:be:03",
+  "protocol": "batman-adv-v14",
+  "channel2": 1,
+  "mode2": "HT20",
+  "type": "adhoc",
+  "channel5": 40,
+  "mode5": "HT40+",
+  "type": "adhoc",
+  "location": { "lat": 49.448856, "lon": 11.082108 },
+ },
+ "network": {
+  "ula_prefix": "fdff:3::/64"
+ }
+}
diff --git a/src/packages/fff/fff-hoods/files/etc/hoods/test.hood b/src/packages/fff/fff-hoods/files/etc/hoods/test.hood
new file mode 100644
index 0000000..18a93f1
--- /dev/null
+++ b/src/packages/fff/fff-hoods/files/etc/hoods/test.hood
@@ -0,0 +1,18 @@
+{
+ "version": 0,
+ "hood": {
+  "name": "test",
+  "bssid": "ca:ff:ee:ba:be:ff",
+  "protocol": "batman-adv-v14",
+  "channel2": 13,
+  "mode2": "ht20",
+  "type2": "adhoc",
+  "channel5": 40,
+  "mode5": "ht40+",
+  "type5": "adhoc",
+  "location": { "lat": 49.46654, "lon": 10.992 },
+ },
+ "network": {
+  "ula_prefix": "fdff:ff::/64"
+ }
+}
diff --git a/src/packages/fff/fff-hoods/files/etc/hoods/trainstation.hood b/src/packages/fff/fff-hoods/files/etc/hoods/trainstation.hood
new file mode 100644
index 0000000..41ac75e
--- /dev/null
+++ b/src/packages/fff/fff-hoods/files/etc/hoods/trainstation.hood
@@ -0,0 +1,17 @@
+{
+ "version": 1,
+ "hood": {
+  "name": "trainstation",
+  "bssid": "ca:ff:ee:ba:be:00",
+  "protocol": "batman-adv-v14",
+  "channel2": 1,
+  "mode2": "ht20",
+  "type2": "adhoc",
+  "channel5": 40,
+  "mode5": "ht40+",
+  "type5": "adhoc",
+ },
+ "network": {
+  "ula_prefix": "fdff:0::/64"
+ }
+}
diff --git a/src/packages/fff/fff-hoods/files/usr/sbin/configurehood b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
new file mode 100755
index 0000000..a1b5a6a
--- /dev/null
+++ b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+HOODDIR=/etc/hoods
+
+. /usr/share/libubox/jshn.sh
+
+myLat=$(uci get 'system. at system[0].latitude')
+myLon=$(uci get 'system. at system[0].longitude')
+
+if [ -z "$myLat" ] || [ -z "$myLon" ]; then
+  nearestHood="/etc/hoods/trainstation.hood"
+else
+  for filename in $HOODDIR/*.hood; do
+    hood=$(basename "$filename")
+    echo "Parsing $hood"
+    json_load "$(cat $filename)"
+    json_get_var ret version
+    if [ "$ret" -ne "1" ]; then
+      echo "Wrong version for $hood"
+      continue
+    fi
+
+    json_select hood
+    json_get_var protocol protocol
+    if [ "$protocol" != "batman-adv-v14" ]; then
+      echo "Unknown protocol in $hood"
+      continue
+    fi
+    json_get_var type2 type2
+    json_get_var type5 type5
+    if [ "$type2" != "adhoc" -o "$type5" != "adhoc" ]; then
+      echo "Unknown mesh type in $hood"
+      continue
+    fi
+    json_get_var hoodname name
+    if ! json_select location; then
+        echo "No location for $hood"
+        continue
+    fi
+    json_get_var lat lat
+    json_get_var lon lon
+
+    d=$(echo "$lat $lon $myLat $myLon" | awk '
+    function rad(deg) { # degrees to radians
+        return deg * (3.1415926 / 180.)
+    }
+    function distance(lat1,lon1,lat2,lon2) {
+        dlat = rad(lat2-lat1)
+        dlon = rad(lon2-lon1)
+        lat1 = rad(lat1)
+        lat2 = rad(lat2)
+        a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
+        return 6371 * 2 * atan2(sqrt(a),sqrt(1-a)) * 1000
+    }
+    {
+        printf("%d", distance($1, $2, $3, $4))
+    }')
+    if [ -z "$nearestDistance" ]; then
+      nearestDistance=$d
+      nearestHood=$filename
+    else
+      if [ "$d" -lt "$nearestDistance" ]; then
+        nearestDistance=$d
+        nearestHood=$filename
+      fi
+    fi
+  done
+fi
+
+echo "Found Hood: $nearestHood in $nearestDistance m"
+
+json_load "$(cat $nearestHood)"
+json_select hood
+
+json_get_var hood name
+json_get_var bssid bssid
+json_get_var channel2 channel2
+json_get_var mode2 mode2
+json_get_var protocol protocol
+json_get_var type2 type2
+
+echo "Setting hood name: $hood"
+echo "Setting channel 2.4 Ghz: $channel2"
+echo "Setting mode 2.4 GHz: $mode2"
+echo "Setting mesh 2.4 GHz type: $type2"
+
+echo "Shutting down wifi"
+wifi down
+
+uci set wireless.radio0=wifi-device
+uci set wireless.radio0.type=mac80211
+uci set wireless.radio0.channel=$channel2
+uci set wireless.radio0.phy=phy0
+uci set wireless.radio0.hwmode=11g
+uci set wireless.radio0.htmode=$mode2
+uci set wireless.radio0.country=DE
+uci set wireless. at wifi-iface[0]=wifi-iface
+uci set wireless. at wifi-iface[0].device=radio0
+uci set wireless. at wifi-iface[0].network=w2mesh
+uci set wireless. at wifi-iface[0].ifname=w2mesh
+uci set wireless. at wifi-iface[0].mode=$type2
+uci set wireless. at wifi-iface[0].bssid=$bssid
+uci set wireless. at wifi-iface[0].ssid=${protocol}.${hood}.freifunk.net
+uci set wireless. at wifi-iface[0].mcast_rate=6000
+uci set wireless. at wifi-iface[0].encryption=none
+uci set wireless. at wifi-iface[1]=wifi-iface
+uci set wireless. at wifi-iface[1].device=radio0
+uci set wireless. at wifi-iface[1].network=mesh
+uci set wireless. at wifi-iface[1].ifname=w2ap
+uci set wireless. at wifi-iface[1].mode=ap
+uci set wireless. at wifi-iface[1].ssid=${hood}.freifunk.net
+uci set wireless. at wifi-iface[1].encryption=none
+uci commit
+
+echo "Loading wifi"
+wifi
+
+json_select ..
+json_select network
+
+json_get_var prefix ula_prefix
+echo "Setting prefix: $prefix"
+suffix=$(awk -F: '{ print $1$2":"$3$4":"$5$6 }' /sys/class/net/br-mesh/address)
+addr=$(echo $prefix | sed -e 's/\//'$suffix'\//')
+
+uci -q batch <<-EOF >/dev/null
+    del network.globals
+    set network.globals=globals
+    set network.globals.ula_prefix="$prefix"
+    set network.mesh.ip6addr="$addr"
+    commit network
+EOF
+
+. /usr/sbin/configurenetwork
+
+# ==TODO==
+#echo "Finding fastd Gateways, if online. Configure fastd."
+# =======
+echo "Done"
diff --git a/src/packages/fff/fff-network/files/usr/sbin/configurenetwork b/src/packages/fff/fff-network/files/usr/sbin/configurenetwork
index 3b21411..1bbc6fb 100755
--- a/src/packages/fff/fff-network/files/usr/sbin/configurenetwork
+++ b/src/packages/fff/fff-network/files/usr/sbin/configurenetwork
@@ -111,18 +111,16 @@ if [[ -n "$ETH0MAC" ]]; then
         /etc/init.d/network restart
 fi
 
-if uci get network.mesh.ip6addr
+if uci get network.globals.ula_prefix
 then
-    echo "IPv6 for mesh is set already"
-else
     # Some time needed :(
     sleep 5
+    prefix=$(uci get network.globals.ula_prefix)
 
     for ip in $(ip -6 addr show br-mesh | awk '/fdff/{ print $2 }'); do
         ip -6 addr del $ip dev br-mesh
     done
 
-    prefix="fdff:0::/64"
     # Set $prefix::MAC as IP
     suffix=$(awk -F: '{ print $1$2":"$3$4":"$5$6 }' /sys/class/net/br-mesh/address)
     addr=$(echo $prefix | sed -e 's/\//'$suffix'\//')
@@ -149,4 +147,6 @@ else
     uci -q commit network
 
     /etc/init.d/fff-uradvd restart
+else
+    echo "IPv6 Prefix is not set"
 fi
diff --git a/src/packages/fff/fff/Makefile b/src/packages/fff/fff/Makefile
index 11fb4a1..e8dfb67 100644
--- a/src/packages/fff/fff/Makefile
+++ b/src/packages/fff/fff/Makefile
@@ -25,7 +25,8 @@ define Package/fff-base
              +fff-network \
              +fff-sysupgrade \
              +fff-wireless \
-             +fff-timeserver
+             +fff-timeserver \
+             +fff-hoods
 endef
 
 define Package/fff-base/description
-- 
2.1.4



Mehr Informationen über die Mailingliste franken-dev