ath9k-blackout-workaround 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_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 ok = false
  38. for _, iface in ipairs(ifaces) do
  39. ok = ok or devOk(iface)
  40. end
  41. return ok
  42. end
  43. if not fs.readfile(fileReset) then
  44. time2file(fileReset)
  45. end
  46. if check_wifi() or not fs.readfile(fileOk) then
  47. time2file(fileOk)
  48. os.exit(0)
  49. end
  50. local blackout_wait_secs = site.ath9k-workaround.blackout_wait *60
  51. local reset_wait_secs = site.ath9k-workaround.reset_wait *60
  52. if os.difftime(os.time(), tonumber(fs.readfile(fileReset))) <= reset_wait_secs then
  53. os.exit(0)
  54. end
  55. if os.difftime(os.time(), tonumber(fs.readfile(fileOk))) <= blackout_wait_secs then
  56. os.exit(0)
  57. end
  58. io.popen('logger -s -t ath9k-blackout-workaround -p local0.info "blackout detected, resart wifi"')
  59. time2file(fileReset)
  60. os.execute('wifi')