luci-app-mwan3: fix legacy uci api usage
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / interface.lua
index 920dc6a..8564231 100644 (file)
@@ -3,42 +3,43 @@
 -- Licensed to the public under the GNU General Public License v2.
 
 dsp = require "luci.dispatcher"
+uci = require "uci"
 
 
-function interfaceWarnings(overview, count)
+function interfaceWarnings(overview, count, iface_max)
        local warnings = ""
-       if count <= 250 then
-               warnings = string.format("<strong>%s</strong></br>",
-                       translatef("There are currently %d of 250 supported interfaces configured", count)
+       if count <= iface_max then
+               warnings = string.format("<strong>%s</strong><br />",
+                       translatef("There are currently %d of %d supported interfaces configured", count, iface_max)
                        )
        else
-               warnings = string.format("<strong>%s</strong></br>",
-                       translatef("WARNING: %d interfaces are configured exceeding the maximum of 250!", count)
+               warnings = string.format("<strong>%s</strong><br />",
+                       translatef("WARNING: %d interfaces are configured exceeding the maximum of %d!", count, iface_max)
                        )
        end
 
        for i, k in pairs(overview) do
                if overview[i]["network"] == false then
-                       warnings = warnings .. string.format("<strong>%s</strong></br>",
+                       warnings = warnings .. string.format("<strong>%s</strong><br />",
                                        translatef("WARNING: Interface %s are not found in /etc/config/network", i)
                                        )
                end
 
                if overview[i]["default_route"] == false then
-                       warnings = warnings .. string.format("<strong>%s</strong></br>",
+                       warnings = warnings .. string.format("<strong>%s</strong><br />",
                                translatef("WARNING: Interface %s has no default route in the main routing table", i)
                                )
                end
 
                if overview[i]["reliability"] == false then
-                       warnings = warnings .. string.format("<strong>%s</strong></br>",
+                       warnings = warnings .. string.format("<strong>%s</strong><br />",
                                translatef("WARNING: Interface %s has a higher reliability " ..
                                "requirement than tracking hosts (%d)", i, overview[i]["tracking"])
                                )
                end
 
                if overview[i]["duplicate_metric"] == true then
-                       warnings = warnings .. string.format("<strong>%s</strong></br>",
+                       warnings = warnings .. string.format("<strong>%s</strong><br />",
                                translatef("WARNING: Interface %s has a duplicate metric %s configured", i, overview[i]["metric"])
                                )
                end
@@ -93,7 +94,7 @@ function configCheck()
 
                        local trackingNumber = uci:get("mwan3", iface, "track_ip")
                        overview[iface]["tracking"] = 0
-                       if #trackingNumber > 0 then
+                       if trackingNumber and #trackingNumber > 0 then
                                overview[iface]["tracking"] = #trackingNumber
                                overview[iface]["reliability"] = false
                                local reliabilityNumber = tonumber(uci:get("mwan3", iface, "reliability"))
@@ -103,7 +104,34 @@ function configCheck()
                        end
                end
        )
-       return overview, count
+
+       -- calculate iface_max usage from firewall mmx_mask
+       function bit(p)
+               return 2 ^ (p - 1)
+       end
+       function hasbit(x, p)
+               return x % (p + p) >= p
+       end
+       function setbit(x, p)
+               return hasbit(x, p) and x or x + p
+       end
+
+       local uci = require("uci").cursor(nil, "/var/state")
+       local mmx_mask = uci:get("mwan3", "globals", "mmx_mask") or "0x3F00"
+       local number = tonumber(mmx_mask, 16)
+       local bits = 0
+       local iface_max = 0
+       for i=1,16 do
+               if hasbit(number, bit(i)) then
+                       bits = bits + 1
+                       iface_max = setbit( iface_max, bit(bits))
+               end
+       end
+
+       -- subtract blackhole, unreachable and default table from iface_max
+       iface_max = iface_max - 3
+
+       return overview, count, iface_max
 end
 
 m5 = Map("mwan3", translate("MWAN - Interfaces"),