wifi-config.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. local fs = require 'nixio.fs'
  2. local iwinfo = require 'iwinfo'
  3. local uci = require("simple-uci").cursor()
  4. local util = require 'gluon.util'
  5. local function txpower_list(phy)
  6. local list = iwinfo.nl80211.txpwrlist(phy) or { }
  7. local off = tonumber(iwinfo.nl80211.txpower_offset(phy)) or 0
  8. local new = { }
  9. local prev = -1
  10. local _, val
  11. for _, val in ipairs(list) do
  12. local dbm = val.dbm + off
  13. local mw = math.floor(10 ^ (dbm / 10))
  14. if mw ~= prev then
  15. prev = mw
  16. table.insert(new, {
  17. display_dbm = dbm,
  18. display_mw = mw,
  19. driver_dbm = val.dbm,
  20. })
  21. end
  22. end
  23. return new
  24. end
  25. local f = Form(translate("WLAN"))
  26. f:section(Section, nil, translate(
  27. "You can enable or disable your node's client and mesh network "
  28. .. "SSIDs here. Please don't disable the mesh network without "
  29. .. "a good reason, so other nodes can mesh with yours.<br /><br />"
  30. .. "It is also possible to configure the WLAN adapters transmission power "
  31. .. "here. Please note that the transmission power values include the antenna gain "
  32. .. "where available, but there are many devices for which the gain is unavailable or inaccurate."
  33. ))
  34. uci:foreach('wireless', 'wifi-device', function(config)
  35. local radio = config['.name']
  36. local title
  37. if config.hwmode == '11g' or config.hwmode == '11ng' then
  38. title = translate("2.4GHz WLAN")
  39. elseif config.hwmode == '11a' or config.hwmode == '11na' then
  40. title = translate("5GHz WLAN")
  41. else
  42. return
  43. end
  44. local p = f:section(Section, title)
  45. local function vif_option(t, msg)
  46. if not uci:get('wireless', t .. '_' .. radio) then
  47. return
  48. end
  49. local o = p:option(Flag, radio .. '_' .. t .. '_enabled', msg)
  50. o.default = not uci:get_bool('wireless', t .. '_' .. radio, 'disabled')
  51. function o:write(data)
  52. uci:set('wireless', t .. '_' .. radio, 'disabled', not data)
  53. end
  54. end
  55. vif_option('client', translate('Enable client network (access point)'))
  56. vif_option('mesh', translate("Enable mesh network (802.11s)"))
  57. vif_option('ibss', translate("Enable mesh network (IBSS)"))
  58. local phy = util.find_phy(config)
  59. if not phy then
  60. return
  61. end
  62. local txpowers = txpower_list(phy)
  63. if #txpowers <= 1 then
  64. return
  65. end
  66. local tp = p:option(ListValue, radio .. '_txpower', translate("Transmission power"))
  67. tp.default = uci:get('wireless', radio, 'txpower') or 'default'
  68. tp:value('default', translate("(default)"))
  69. table.sort(txpowers, function(a, b) return a.driver_dbm > b.driver_dbm end)
  70. for _, entry in ipairs(txpowers) do
  71. tp:value(entry.driver_dbm, string.format("%i dBm (%i mW)", entry.display_dbm, entry.display_mw))
  72. end
  73. function tp:write(data)
  74. if data == 'default' then
  75. data = nil
  76. end
  77. uci:set('wireless', radio, 'txpower', data)
  78. end
  79. end)
  80. function f:write()
  81. uci:commit('wireless')
  82. end
  83. return f