wifi-config.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. local uci = luci.model.uci.cursor()
  2. local fs = require 'nixio.fs'
  3. local iwinfo = require 'iwinfo'
  4. local function find_phy_by_path(path)
  5. for phy in fs.glob("/sys/devices/" .. path .. "/ieee80211/phy*") do
  6. return phy:match("([^/]+)$")
  7. end
  8. end
  9. local function find_phy_by_macaddr(macaddr)
  10. local addr = macaddr:lower()
  11. for file in fs.glob("/sys/class/ieee80211/*/macaddress") do
  12. if luci.util.trim(fs.readfile(file)) == addr then
  13. return file:match("([^/]+)/macaddress$")
  14. end
  15. end
  16. end
  17. local function txpower_list(phy)
  18. local list = iwinfo.nl80211.txpwrlist(phy) or { }
  19. local off = tonumber(iwinfo.nl80211.txpower_offset(phy)) or 0
  20. local new = { }
  21. local prev = -1
  22. local _, val
  23. for _, val in ipairs(list) do
  24. local dbm = val.dbm + off
  25. local mw = math.floor(10 ^ (dbm / 10))
  26. if mw ~= prev then
  27. prev = mw
  28. table.insert(new, {
  29. display_dbm = dbm,
  30. display_mw = mw,
  31. driver_dbm = val.dbm,
  32. })
  33. end
  34. end
  35. return new
  36. end
  37. local f = SimpleForm("wifi", translate("WLAN"))
  38. f.template = "admin/expertmode"
  39. local s = f:section(SimpleSection, nil, translate(
  40. "You can enable or disable your node's client and mesh network "
  41. .. "SSIDs here. Please don't disable the mesh network without "
  42. .. "a good reason, so other nodes can mesh with yours.<br /><br />"
  43. .. "It is also possible to configure the WLAN adapters transmission power "
  44. .. "here. Please note that the transmission power values include the antenna gain "
  45. .. "where available, but there are many devices for which the gain is unavailable or inaccurate."
  46. ))
  47. local radios = {}
  48. -- look for wifi interfaces and add them to the array
  49. uci:foreach('wireless', 'wifi-device',
  50. function(s)
  51. table.insert(radios, s['.name'])
  52. end
  53. )
  54. -- add a client and mesh checkbox for each interface
  55. for _, radio in ipairs(radios) do
  56. local config = uci:get_all('wireless', radio)
  57. local p
  58. if config.hwmode == '11g' or config.hwmode == '11ng' then
  59. p = f:section(SimpleSection, translate("2.4GHz WLAN"))
  60. elseif config.hwmode == '11a' or config.hwmode == '11na' then
  61. p = f:section(SimpleSection, translate("5GHz WLAN"))
  62. end
  63. if p then
  64. local o
  65. --box for the client network
  66. o = p:option(Flag, radio .. '_client_enabled', translate("Enable client network"))
  67. o.default = uci:get_bool('wireless', 'client_' .. radio, "disabled") and o.disabled or o.enabled
  68. o.rmempty = false
  69. --box for the mesh network
  70. o = p:option(Flag, radio .. '_mesh_enabled', translate("Enable mesh network"))
  71. o.default = uci:get_bool('wireless', 'mesh_' .. radio, "disabled") and o.disabled or o.enabled
  72. o.rmempty = false
  73. local phy
  74. if config.path then
  75. phy = find_phy_by_path(config.path)
  76. elseif config.macaddr then
  77. phy = find_phy_by_path(config.macaddr)
  78. end
  79. if phy then
  80. local txpowers = txpower_list(phy)
  81. if #txpowers > 1 then
  82. local tp = p:option(ListValue, radio .. '_txpower', translate("Transmission power"))
  83. tp.rmempty = true
  84. tp.default = uci:get('wireless', radio, 'txpower') or 'default'
  85. tp:value('default', translate("(default)"))
  86. table.sort(txpowers, function(a, b) return a.driver_dbm > b.driver_dbm end)
  87. for _, entry in ipairs(txpowers) do
  88. tp:value(entry.driver_dbm, "%i dBm (%i mW)" % {entry.display_dbm, entry.display_mw})
  89. end
  90. end
  91. end
  92. end
  93. end
  94. --when the save-button is pushed
  95. function f.handle(self, state, data)
  96. if state == FORM_VALID then
  97. for _, radio in ipairs(radios) do
  98. local clientdisabled = 0
  99. if data[radio .. '_client_enabled'] == '0' then
  100. clientdisabled = 1
  101. end
  102. uci:set('wireless', 'client_' .. radio, "disabled", clientdisabled)
  103. local meshdisabled = 0
  104. if data[radio .. '_mesh_enabled'] == '0' then
  105. meshdisabled = 1
  106. end
  107. uci:set('wireless', 'mesh_' .. radio, "disabled", meshdisabled)
  108. if data[radio .. '_txpower'] then
  109. if data[radio .. '_txpower'] == 'default' then
  110. uci:delete('wireless', radio, 'txpower')
  111. else
  112. uci:set('wireless', radio, 'txpower', data[radio .. '_txpower'])
  113. end
  114. end
  115. end
  116. uci:save('wireless')
  117. uci:commit('wireless')
  118. end
  119. end
  120. return f