ath9k-blackout-workaround 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/lua
  2. local fs = require('nixio.fs')
  3. local uci = require('luci.model.uci').cursor()
  4. local iwinfo = require "iwinfo"
  5. local site = require 'gluon.site_config'
  6. local fileOk="/tmp/ath9k-ok"
  7. local fileReset="/tmp/ath9k-reset"
  8. function time2file (file)
  9. local f = io.open(file, 'w')
  10. f:write(string.format('%i', os.time()))
  11. f:close()
  12. end
  13. function devOk (iface)
  14. local radio = uci:get_all('wireless', iface, 'radio')
  15. local disabled = uci:get_bool('wireless', iface, 'disabled') or uci:get_bool('wireless', radio, 'disabled')
  16. if disabled then
  17. return null
  18. end
  19. local hwmode = uci:get_all('wireless', radio, 'hwmode')
  20. if not hwmode == '11g' then
  21. return null
  22. end
  23. local wifitype = iwinfo.type(radio)
  24. local assoclist = iwinfo[wifitype].assoclist(iface)
  25. if assoclist and #assoclist > 0 then
  26. return true
  27. end
  28. return false
  29. end
  30. function check_wifi()
  31. local ifaces = {}
  32. uci:foreach('wireless', 'wifi-iface',
  33. function(s)
  34. table.insert(ifaces, s['.name'])
  35. end
  36. )
  37. local blackout = false
  38. for _, iface in ipairs(ifaces) do
  39. local tmp = devOk(iface)
  40. if tmp then
  41. return true
  42. elseif tmp == false then
  43. blackout = true
  44. end
  45. end
  46. return not blackout
  47. end
  48. if not fs.readfile(fileReset) then
  49. time2file(fileReset)
  50. end
  51. if check_wifi() or not fs.readfile(fileOk) then
  52. time2file(fileOk)
  53. os.exit(0)
  54. end
  55. local blackout_wait_secs = site.ath9k-workaround.blackout_wait *60
  56. local reset_wait_secs = site.ath9k-workaround.reset_wait *60
  57. if os.difftime(os.time(), tonumber(fs.readfile(fileReset))) <= reset_wait_secs then
  58. os.exit(0)
  59. end
  60. if os.difftime(os.time(), tonumber(fs.readfile(fileOk))) <= blackout_wait_secs then
  61. os.exit(0)
  62. end
  63. io.popen('logger -s -t ath9k-blackout-workaround -p local0.info "blackout detected, resart wifi"')
  64. time2file(fileReset)
  65. os.execute("wifi")