ath9k-blackout-workaround 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/lua
  2. local fs = require 'nixio.fs'
  3. local uci = require('simple-uci').cursor()
  4. local iwinfo = require 'iwinfo'
  5. local site = require 'gluon.site'
  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('wireless', iface, 'device')
  15. if uci:get_bool('wireless', iface, 'disabled') or uci:get_bool('wireless', radio, 'disabled') then
  16. return null
  17. end
  18. if uci:get('wireless', radio, 'hwmode') ~= '11g' then
  19. return null
  20. end
  21. local ifname = uci:get('wireless', iface, 'ifname')
  22. local wifitype = iwinfo.type(radio)
  23. local assoclist = iwinfo[wifitype].assoclist(ifname)
  24. if next(assoclist) then
  25. return true
  26. end
  27. return false
  28. end
  29. function check_wifi()
  30. local ok = false
  31. uci:foreach('wireless', 'wifi-iface',
  32. function(s)
  33. ok = ok or devOk(s['.name'])
  34. end
  35. )
  36. return ok
  37. end
  38. if not fs.readfile(fileReset) then
  39. time2file(fileReset)
  40. end
  41. if check_wifi() or not fs.readfile(fileOk) then
  42. time2file(fileOk)
  43. os.exit(0)
  44. end
  45. local blackout_wait_secs = site.ath9k_workaround.blackout_wait() * 60
  46. local reset_wait_secs = site.ath9k_workaround.reset_wait() * 60
  47. if os.difftime(os.time(), tonumber(fs.readfile(fileReset))) <= reset_wait_secs then
  48. os.exit(0)
  49. end
  50. if os.difftime(os.time(), tonumber(fs.readfile(fileOk))) <= blackout_wait_secs then
  51. os.exit(0)
  52. end
  53. io.popen('logger -s -t ath9k-blackout-workaround -p local0.info "blackout detected, resart wifi"')
  54. time2file(fileReset)
  55. os.execute('wifi')