wifi-config.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. if uci:get('wireless', 'client_' .. radio) then
  66. o = p:option(Flag, radio .. '_client_enabled', translate("Enable client network (access point)"))
  67. o.default = uci:get_bool('wireless', 'client_' .. radio, "disabled") and o.disabled or o.enabled
  68. o.rmempty = false
  69. end
  70. if uci:get('wireless', 'mesh_' .. radio) then
  71. o = p:option(Flag, radio .. '_mesh_enabled', translate("Enable mesh network (802.11s)"))
  72. o.default = uci:get_bool('wireless', 'mesh_' .. radio, "disabled") and o.disabled or o.enabled
  73. o.rmempty = false
  74. end
  75. if uci:get('wireless', 'ibss_' .. radio) then
  76. o = p:option(Flag, radio .. '_ibss_enabled', translate("Enable mesh network (IBSS)"))
  77. o.default = uci:get_bool('wireless', 'ibss_' .. radio, "disabled") and o.disabled or o.enabled
  78. o.rmempty = false
  79. end
  80. local phy
  81. if config.path then
  82. phy = find_phy_by_path(config.path)
  83. elseif config.macaddr then
  84. phy = find_phy_by_macaddr(config.macaddr)
  85. end
  86. if phy then
  87. local txpowers = txpower_list(phy)
  88. if #txpowers > 1 then
  89. local tp = p:option(ListValue, radio .. '_txpower', translate("Transmission power"))
  90. tp.rmempty = true
  91. tp.default = uci:get('wireless', radio, 'txpower') or 'default'
  92. tp:value('default', translate("(default)"))
  93. table.sort(txpowers, function(a, b) return a.driver_dbm > b.driver_dbm end)
  94. for _, entry in ipairs(txpowers) do
  95. tp:value(entry.driver_dbm, "%i dBm (%i mW)" % {entry.display_dbm, entry.display_mw})
  96. end
  97. end
  98. end
  99. end
  100. end
  101. --when the save-button is pushed
  102. function f.handle(self, state, data)
  103. if state == FORM_VALID then
  104. for _, radio in ipairs(radios) do
  105. if uci:get('wireless', 'client_' .. radio) then
  106. local disabled = 0
  107. if data[radio .. '_client_enabled'] == '0' then
  108. disabled = 1
  109. end
  110. uci:set('wireless', 'client_' .. radio, "disabled", disabled)
  111. end
  112. if uci:get('wireless', 'mesh_' .. radio) then
  113. local disabled = 0
  114. if data[radio .. '_mesh_enabled'] == '0' then
  115. disabled = 1
  116. end
  117. uci:set('wireless', 'mesh_' .. radio, "disabled", disabled)
  118. end
  119. if uci:get('wireless', 'ibss_' .. radio) then
  120. local disabled = 0
  121. if data[radio .. '_ibss_enabled'] == '0' then
  122. disabled = 1
  123. end
  124. uci:set('wireless', 'ibss_' .. radio, "disabled", disabled)
  125. end
  126. if data[radio .. '_txpower'] then
  127. if data[radio .. '_txpower'] == 'default' then
  128. uci:delete('wireless', radio, 'txpower')
  129. else
  130. uci:set('wireless', radio, 'txpower', data[radio .. '_txpower'])
  131. end
  132. end
  133. end
  134. uci:save('wireless')
  135. uci:commit('wireless')
  136. end
  137. end
  138. return f