-
-
- -- Does this switch support PVIDs?
- if has_ptpvid then
-
- -- Spawn a "virtual" section. We just attach it to the global
- -- switch section here, the overrides below take care of writing
- -- the actual values to the correct uci sections.
- s = m:section(TypedSection, "switch",
- translatef("Port PVIDs on %q", switch_name),
- translate("Port <abbr title=\"Primary VLAN IDs\">PVIDs</abbr> specify " ..
- "the default VLAN ID added to received untagged frames."))
-
- s.template = "cbi/tblsection"
- s.addremove = false
- s.anonymous = true
-
- -- Filter by switch
- function s.filter(self, section)
- return (m:get(section, "name") == switch_name)
- end
-
- -- Build port list, store pointers to the option objects in the
- -- pvid_opts array so that other callbacks can repopulate their
- -- choice lists.
- local pt
- for pt = 0, num_ports - 1 do
- local po = s:option(ListValue, tostring(pt),
- (pt == cpu_port) and translate("CPU") or translatef("Port %d", (pt + 1)))
-
- -- When cbi queries the current config value for this post,
- -- lookup the associated switch_port section (if any) and
- -- return its "pvid" or "vlan" option value.
- po.cfgvalue = function(self, section)
- local val
- m.uci:foreach("network", "switch_port",
- function(s)
- if s.port == self.option then
- val = s[has_ptpvid]
- return false
- end
- end)
- return val
- end
-
- -- On write, find the actual switch_port section associated
- -- to this port and set the value there. Create a new
- -- switch_port section for this port if there is none yet.
- po.write = function(self, section, value)
- local found = false
-
- m.uci:foreach("network", "switch_port",
- function(s)
- if s.port == self.option then
- m.uci:set("network", s['.name'], has_ptpvid, value)
- found = true
- return false
- end
- end)
-
- if not found then
- m.uci:section("network", "switch_port", nil, {
- ["port"] = self.option,
- [has_ptpvid] = value
- })
- end
- end
-
- -- If the user cleared the PVID value on this port, find
- -- the associated switch_port section and clear it.
- -- If the section does not contain any other unrelated
- -- options (like led or blinkrate) then remove it completely,
- -- else just clear out the "pvid" option.
- po.remove = function(self, section)
- m.uci:foreach("network", "switch_port",
- function(s)
- if s.port == self.option then
- local k, found
- local empty = true
-
- for k, _ in pairs(s) do
- if k:sub(1,1) ~= "." and k ~= "port" and k ~= has_ptpvid then
- empty = false
- break
- end
- end
-
- if empty then
- m.uci:delete("network", s['.name'])
- else
- m.uci:delete("network", s['.name'], has_ptpvid)
- end
-
- return false
- end
- end)
- end
-
- -- The referenced VLAN might just have been removed, simply
- -- return "" (none) in this case to avoid triggering a
- -- validation error.
- po.validate = function(...)
- return ListValue.validate(...) or ""
- end
-
- pvid_opts[#pvid_opts+1] = po
- end
-
- populate_pvids()
- end