Browse Source

Search for any open freifunk network, not only for our current network

Michael Schwarz 8 years ago
parent
commit
7e1c61d9d5

+ 7 - 7
ffho/ffho-autoupdater-wifi-fallback/files/usr/lib/lua/autoupdater-wifi-fallback/util.lua

@@ -5,19 +5,19 @@ local site = require 'gluon.site_config'
 local json = require 'luci.jsonc'
 local util = require("luci.util")
 
-
-function is_site_ssid_available()
-  local found = false
+function get_available_wifi_networks()
   local interfaces = util.split(util.trim(util.exec("iw dev | grep Interface | cut -d' ' -f2")))
   for _, ifname in ipairs(interfaces) do
     for line in io.popen(string.format("iw dev %s scan 2>/dev/null", ifname)):lines() do
-        if line:match("SSID: " .. site.wifi24.ap.ssid) then
-          found = true
-        end
+      net = line:match(".+.freifunk.net.*")
+      if net then
+	netname = net:sub(8)
+	return netname
+      end
     end
   end
 
-  return found
+  return false
 end
 
 function get_site_macs()

+ 21 - 21
ffho/ffho-autoupdater-wifi-fallback/files/usr/sbin/autoupdater-wifi-fallback

@@ -72,10 +72,11 @@ end
 
 local function run_autoupdater()
   -- TODO:should be called with -f !
-  os.execute("/usr/sbin/autoupdater")
+  os.execute("/usr/sbin/autoupdater -f")
 end
 
-local function switch_to_fallback_mode()
+local function switch_to_fallback_mode(wifiNetwork)
+  print("I will connect to: " .. wifiNetwork)
   local disabled_radios = {'fallback'}
   uci:foreach('wireless', 'wifi-iface',
         function(s)
@@ -89,28 +90,21 @@ local function switch_to_fallback_mode()
   if disabled_radios ~= nil then
     uci:set_list(configname, 'settings', 'disabled_radios', disabled_radios)
   end
-  
+ 
+  uci:set('wireless', 'fallback', 'ssid', wifiNetwork) 
   uci:set('wireless', 'fallback', 'disabled', 0)
   uci:save('wireless')
   uci:commit('wireless')
-  
-  local maclist = get_site_macs()
-  local mac = table.remove(maclist, math.random(#maclist))
-  os.remove("/usr/sbin/wpa_supplicant")  
-  local f = io.open("/usr/sbin/wpa_supplicant", "w")
-  local freq = 2412 + (site.wifi24.channel -1) * 5
-  f:write("iw dev fallback connect -w " .. site.wifi24.ap.ssid .. " " .. freq .. " " .. mac)
-  f:close()
-  fs.chmod("/usr/sbin/wpa_supplicant", "775")
-  
-  os.execute("/etc/init.d/wireless restart")
-  -- wait for dhcp
-  os.execute("sleep 30")
-  
-  run_autoupdater()
+
+  io.popen("wifi")
+  os.execute("sleep 5")
+  os.execute("iw dev fallback connect -w " .. wifiNetwork)
+  os.execute("echo \"2\" > /proc/sys/net/ipv6/conf/fallback/accept_ra")
+  os.execute("sleep 20")
 end
 
 local function revert_to_standard_mode(restart_network)
+  print("Going back to standard config")
   local disabled_radios = uci:get_list(configname, 'settings', 'disabled_radios')
   uci:foreach('wireless', 'wifi-iface',
     function(s)
@@ -160,9 +154,15 @@ if not check_connectivity() then
   uci:save(configname)
   uci:commit(configname)
   
-  if tonumber(unreachable_since) + offset < os.time() then
-    if is_site_ssid_available() then
-      switch_to_fallback_mode()
+  if tonumber(unreachable_since) + offset < os.time() or force then
+    wifiNetwork = get_available_wifi_networks()
+    if wifiNetwork then
+      switch_to_fallback_mode(wifiNetwork)
+
+      run_autoupdater()
+      
+      -- this is only reached if no updated happened
+      revert_to_standard_mode(true)
     end
   end
 else