ath9k-blackout-workaround 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 data = uci:get_all('wireless', iface)
  15. if data.disabled then
  16. return null
  17. end
  18. local radio = uci:get_all('wireless', data.radio)
  19. if not radio.hwmode == '11g' or radio.disabled then
  20. return null
  21. end
  22. local wifitype = iwinfo.type(data.radio)
  23. local iw = iwinfo[wifitype]
  24. if iw.assoclist(iface) then
  25. return true
  26. end
  27. return false
  28. end
  29. function check_wifi()
  30. local ifaces = {}
  31. uci:foreach('wireless', 'wifi-iface',
  32. function(s)
  33. table.insert(ifaces, s['.name'])
  34. end
  35. )
  36. local blackout = false
  37. for _, iface in ipairs(ifaces) do
  38. local tmp = devOk(iface)
  39. if tmp then
  40. return true
  41. elseif tmp == false then
  42. blackout = true
  43. end
  44. end
  45. return not blackout
  46. end
  47. if not fs.readfile(fileReset) then
  48. time2file(fileReset)
  49. end
  50. if check_wifi() or not fs.readfile(fileOk) then
  51. time2file(fileOk)
  52. os.exit(0)
  53. end
  54. local blackout_wait_secs = site.ath9k-workaround.blackout_wait *60
  55. local reset_wait_secs = site.ath9k-workaround.reset_wait *60
  56. if os.difftime(os.time(), tonumber(fs.readfile(fileReset))) <= reset_wait_secs then
  57. os.exit(0)
  58. end
  59. if os.difftime(os.time(), tonumber(fs.readfile(fileOk))) <= blackout_wait_secs then
  60. os.exit(0)
  61. end
  62. io.popen('logger -s -t ath9k-blackout-workaround -p local0.info "blackout detected, resart wifi"')
  63. time2file(fileReset)
  64. os.execute("wifi")