From b241373640e570302dc13c18194479ea88fa8d89 Mon Sep 17 00:00:00 2001
From: Matthias Schiffer <mschiffer@universe-factory.net>
Date: Wed, 27 Jul 2016 03:46:04 +0200
Subject: [PATCH] gluon-luci-private-wifi: set MAC address if the driver
 provides one

---
 .../lib/lua/luci/model/cbi/admin/privatewifi.lua  | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/package/gluon-luci-private-wifi/luasrc/usr/lib/lua/luci/model/cbi/admin/privatewifi.lua b/package/gluon-luci-private-wifi/luasrc/usr/lib/lua/luci/model/cbi/admin/privatewifi.lua
index 788ff4303..0e04b67df 100644
--- a/package/gluon-luci-private-wifi/luasrc/usr/lib/lua/luci/model/cbi/admin/privatewifi.lua
+++ b/package/gluon-luci-private-wifi/luasrc/usr/lib/lua/luci/model/cbi/admin/privatewifi.lua
@@ -1,5 +1,7 @@
-local f, s, o, ssid
 local uci = luci.model.uci.cursor()
+local util = require 'gluon.util'
+
+local f, s, o, ssid
 local config = 'wireless'
 
 -- where to read the configuration from
@@ -33,19 +35,24 @@ function f.handle(self, state, data)
   if state == FORM_VALID then
     uci:foreach(config, "wifi-device",
       function(s)
-        local device = s['.name']
-        local name   = "wan_" .. device
+        local radio = s['.name']
+        local name   = "wan_" .. radio
 
         if data.enabled == '1' then
+          -- get_wlan_mac_from_driver will return nil (and thus leave the
+          -- MAC address unset) if the driver doesn't provide enough addresses
+          local macaddr = util.get_wlan_mac_from_driver(radio, 4)
+
           -- set up WAN wifi-iface
           uci:section(config, "wifi-iface", name,
                       {
-                        device     = device,
+                        device     = radio,
                         network    = "wan",
                         mode       = 'ap',
                         encryption = 'psk2',
                         ssid       = data.ssid,
                         key        = data.key,
+                        macaddr    = macaddr,
                         disabled   = 0,
                       }
           )
-- 
GitLab