From e20d5b0a3e43a4d51a6d8a192315f87f766fad12 Mon Sep 17 00:00:00 2001
From: Martin Weinelt <martin@darmstadt.freifunk.net>
Date: Thu, 1 Mar 2018 02:21:04 +0100
Subject: [PATCH] gluon-core: allow presetting the outdoor mode for new
 installations

---
 docs/user/site.rst                            |  4 +++
 package/gluon-core/check_site.lua             |  1 +
 .../luasrc/lib/gluon/upgrade/180-outdoors     | 35 +++++++++++++++++++
 .../luasrc/usr/lib/lua/gluon/platform.lua     | 20 +++++++++++
 4 files changed, 60 insertions(+)
 create mode 100755 package/gluon-core/luasrc/lib/gluon/upgrade/180-outdoors

diff --git a/docs/user/site.rst b/docs/user/site.rst
index 88859a299..b1549b23b 100644
--- a/docs/user/site.rst
+++ b/docs/user/site.rst
@@ -172,6 +172,10 @@ wifi5 \: optional
     When set this offers the outdoor mode flag for 5 GHz radios in the config mode which
     reconfigures the AP to select its channel from outdoor chanlist, while respecting
     regulatory specifications, and  disables mesh on that radio.
+    The ``outdoors`` option in turn allows to configure when outdoor mode will be enabled.
+    When set to ``true`` all 5 GHz radios will use outdoor channels, while on ``false``
+    the outdoor mode will be completely disabled. The default setting is ``'preset'``,
+    which will enable outdoor mode automatically on outdoor-capable devices.
     ::
 
       wifi5 = {
diff --git a/package/gluon-core/check_site.lua b/package/gluon-core/check_site.lua
index b14825083..74a6c3f9a 100644
--- a/package/gluon-core/check_site.lua
+++ b/package/gluon-core/check_site.lua
@@ -44,6 +44,7 @@ for _, config in ipairs({'wifi24', 'wifi5'}) do
 				149, 151, 153, 155, 157, 159, 161, 165, 169, 173 }
 			need_one_of({config, 'channel'}, channels)
 			need_chanlist({config, 'outdoor_chanlist'}, channels, false)
+			need_one_of({config, 'outdoors'}, {true, false, 'preset'}, false)
 		end
 
 		obsolete({config, 'supported_rates'}, '802.11b rates are disabled by default.')
diff --git a/package/gluon-core/luasrc/lib/gluon/upgrade/180-outdoors b/package/gluon-core/luasrc/lib/gluon/upgrade/180-outdoors
new file mode 100755
index 000000000..3acaa63c9
--- /dev/null
+++ b/package/gluon-core/luasrc/lib/gluon/upgrade/180-outdoors
@@ -0,0 +1,35 @@
+#!/usr/bin/lua
+
+-- This script needs to be sorted before 200-wireless as it affects
+-- wireless channel selection and wireless mesh configuration.
+
+local uci = require('simple-uci').cursor()
+local site = require 'gluon.site'
+
+if uci:get('gluon', 'wireless', 'outdoor') ~= nil then
+	-- don't overwrite existing configuration
+	os.exit(0)
+end
+
+local sysconfig = require 'gluon.sysconfig'
+local platform_info = require 'platform_info'
+
+local config = site.wifi5.outdoor_preset('preset')
+local outdoor = false
+
+if sysconfig.gluon_version then
+	-- don't enable the outdoor mode after an upgrade
+	outdoor = false
+elseif config == 'preset' then
+	-- enable outdoor mode through presets on new installs
+	outdoor = platform_info.is_outdoor_device()
+else
+	-- enable/disable outdoor mode unconditionally on new installs
+	outdoor = config
+end
+
+uci:section('gluon', 'wireless', 'wireless', {
+	outdoor = outdoor
+})
+
+uci:save('gluon')
diff --git a/package/gluon-core/luasrc/usr/lib/lua/gluon/platform.lua b/package/gluon-core/luasrc/usr/lib/lua/gluon/platform.lua
index 5b4f559a6..17d881f62 100644
--- a/package/gluon-core/luasrc/usr/lib/lua/gluon/platform.lua
+++ b/package/gluon-core/luasrc/usr/lib/lua/gluon/platform.lua
@@ -27,3 +27,23 @@ function match(target, subtarget, boards)
 
    return true
 end
+
+function is_outdoor_device()
+   if match('ar71xx', 'generic', {
+      'cpe510-520-v1',
+      'ubnt-nano-m',
+      'ubnt-nano-m-xw',
+      }) then
+      return true
+
+   elseif match('ar71xx', 'generic', {'unifiac-lite'}) and
+	   get_model() == 'Ubiquiti UniFi-AC-MESH' then
+      return true
+
+   elseif match('ar71xx', 'generic', {'unifiac-pro'}) and
+	   get_model() == 'Ubiquiti UniFi-AC-MESH-PRO' then
+      return true
+   end
+
+   return false
+end
-- 
GitLab