wifi-config.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. local uci = require("simple-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. local s = f:section(SimpleSection, nil, translate(
  39. "You can enable or disable your node's client and mesh network "
  40. .. "SSIDs here. Please don't disable the mesh network without "
  41. .. "a good reason, so other nodes can mesh with yours.<br /><br />"
  42. .. "It is also possible to configure the WLAN adapters transmission power "
  43. .. "here. Please note that the transmission power values include the antenna gain "
  44. .. "where available, but there are many devices for which the gain is unavailable or inaccurate."
  45. ))
  46. local radios = {}
  47. -- look for wifi interfaces and add them to the array
  48. uci:foreach('wireless', 'wifi-device',
  49. function(s)
  50. table.insert(radios, s['.name'])
  51. end
  52. )
  53. -- add a client and mesh checkbox for each interface
  54. for _, radio in ipairs(radios) do
  55. local config = uci:get_all('wireless', radio)
  56. local p
  57. if config.hwmode == '11g' or config.hwmode == '11ng' then
  58. p = f:section(SimpleSection, translate("2.4GHz WLAN"))
  59. elseif config.hwmode == '11a' or config.hwmode == '11na' then
  60. p = f:section(SimpleSection, translate("5GHz WLAN"))
  61. end
  62. if p then
  63. local o
  64. if uci:get('wireless', 'client_' .. radio) then
  65. o = p:option(Flag, radio .. '_client_enabled', translate("Enable client network (access point)"))
  66. o.default = uci:get_bool('wireless', 'client_' .. radio, "disabled") and o.disabled or o.enabled
  67. o.rmempty = false
  68. end
  69. if uci:get('wireless', 'mesh_' .. radio) then
  70. o = p:option(Flag, radio .. '_mesh_enabled', translate("Enable mesh network (802.11s)"))
  71. o.default = uci:get_bool('wireless', 'mesh_' .. radio, "disabled") and o.disabled or o.enabled
  72. o.rmempty = false
  73. end
  74. if uci:get('wireless', 'ibss_' .. radio) then
  75. o = p:option(Flag, radio .. '_ibss_enabled', translate("Enable mesh network (IBSS)"))
  76. o.default = uci:get_bool('wireless', 'ibss_' .. radio, "disabled") and o.disabled or o.enabled
  77. o.rmempty = false
  78. end
  79. local phy
  80. if config.path then
  81. phy = find_phy_by_path(config.path)
  82. elseif config.macaddr then
  83. phy = find_phy_by_macaddr(config.macaddr)
  84. end
  85. if phy then
  86. local txpowers = txpower_list(phy)
  87. if #txpowers > 1 then
  88. local tp = p:option(ListValue, radio .. '_txpower', translate("Transmission power"))
  89. tp.rmempty = true
  90. tp.default = uci:get('wireless', radio, 'txpower') or 'default'
  91. tp:value('default', translate("(default)"))
  92. table.sort(txpowers, function(a, b) return a.driver_dbm > b.driver_dbm end)
  93. for _, entry in ipairs(txpowers) do
  94. tp:value(entry.driver_dbm, "%i dBm (%i mW)" % {entry.display_dbm, entry.display_mw})
  95. end
  96. end
  97. end
  98. end
  99. end
  100. --when the save-button is pushed
  101. function f.handle(self, state, data)
  102. if state == FORM_VALID then
  103. for _, radio in ipairs(radios) do
  104. if uci:get('wireless', 'client_' .. radio) then
  105. local disabled = 0
  106. if data[radio .. '_client_enabled'] == '0' then
  107. disabled = 1
  108. end
  109. uci:set('wireless', 'client_' .. radio, "disabled", disabled)
  110. end
  111. if uci:get('wireless', 'mesh_' .. radio) then
  112. local disabled = 0
  113. if data[radio .. '_mesh_enabled'] == '0' then
  114. disabled = 1
  115. end
  116. uci:set('wireless', 'mesh_' .. radio, "disabled", disabled)
  117. end
  118. if uci:get('wireless', 'ibss_' .. radio) then
  119. local disabled = 0
  120. if data[radio .. '_ibss_enabled'] == '0' then
  121. disabled = 1
  122. end
  123. uci:set('wireless', 'ibss_' .. radio, "disabled", disabled)
  124. end
  125. if data[radio .. '_txpower'] then
  126. if data[radio .. '_txpower'] == 'default' then
  127. uci:delete('wireless', radio, 'txpower')
  128. else
  129. uci:set('wireless', radio, 'txpower', data[radio .. '_txpower'])
  130. end
  131. end
  132. end
  133. uci:save('wireless')
  134. uci:commit('wireless')
  135. end
  136. end
  137. return f