210-ffho-txpower-fix 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/lua
  2. site = require("gluon.site_config")
  3. uci = require('luci.model.uci').cursor()
  4. --- wrapper for calling systemcommands
  5. function cmd(_command)
  6. local f = io.popen(_command)
  7. local l = f:read("*a")
  8. f:close()
  9. return l
  10. end
  11. --- first of all, get 2.4GHz wifi interface
  12. local interface24 = false
  13. if uci:get('wireless', 'radio0', 'hwmode') then
  14. hwmode = uci:get('wireless', 'radio0', 'hwmode')
  15. if hwmode == '11g' then
  16. interface24 = 'radio0'
  17. end
  18. end
  19. --- try with radio1 if radio0 seems not the correct interface
  20. if not interface24 and uci:get('wireless', 'radio1', 'hwmode') then
  21. hwmode = uci:get('wireless', 'radio1', 'hwmode')
  22. if hwmode == '11g' then
  23. interface24 = 'radio1'
  24. end
  25. end
  26. if not interface24 then
  27. os.exit(0) -- something went wrong
  28. end
  29. --- check if txpower is already set. if so, we have nothing to do
  30. if uci:get('wireless', interface24, 'txpower') then
  31. os.exit(0)
  32. end
  33. --- get maximum available power and step
  34. t = cmd('iwinfo ' .. interface24 .. ' txpowerlist | tail -n 1 | awk \'{print $1}\'')
  35. maximumTxPowerDb = string.gsub(t, "\n", "")
  36. maximumTxPowerDb = tonumber(maximumTxPowerDb)
  37. if maximumTxPowerDb < 20 then
  38. t = cmd('iwinfo ' .. interface24 .. ' txpowerlist | wc -l')
  39. maximumTxPower = string.gsub(t, "\n", "")
  40. maximumTxPower = tonumber(maximumTxPower)-1
  41. else
  42. t = cmd('iwinfo ' .. interface24 .. ' txpowerlist | grep -n "20 dBm" | cut -f1 -d\':\'')
  43. maximumTxPower = string.gsub(t, "\n", "")
  44. maximumTxPower = tonumber(maximumTxPower)-1
  45. end
  46. --- set values
  47. uci:set('wireless', interface24, 'country', '00')
  48. uci:set('wireless', interface24, 'txpower', maximumTxPower)
  49. uci:save('wireless')
  50. uci:commit('wireless')