ath9k-blackout-workaround 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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('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 wifitype = iwinfo.type(radio)
  22. local assoclist = iwinfo[wifitype].assoclist(iface)
  23. if assoclist and #assoclist > 0 then
  24. return true
  25. end
  26. return false
  27. end
  28. function check_wifi()
  29. local ok = false
  30. uci:foreach('wireless', 'wifi-iface',
  31. function(s)
  32. ok = ok or devOk(s['.name'])
  33. end
  34. )
  35. return ok
  36. end
  37. if not fs.readfile(fileReset) then
  38. time2file(fileReset)
  39. end
  40. if check_wifi() or not fs.readfile(fileOk) then
  41. time2file(fileOk)
  42. os.exit(0)
  43. end
  44. local blackout_wait_secs = site.ath9k_workaround.blackout_wait * 60
  45. local reset_wait_secs = site.ath9k_workaround.reset_wait * 60
  46. if os.difftime(os.time(), tonumber(fs.readfile(fileReset))) <= reset_wait_secs then
  47. os.exit(0)
  48. end
  49. if os.difftime(os.time(), tonumber(fs.readfile(fileOk))) <= blackout_wait_secs then
  50. os.exit(0)
  51. end
  52. io.popen('logger -s -t ath9k-blackout-workaround -p local0.info "blackout detected, resart wifi"')
  53. time2file(fileReset)
  54. os.execute('wifi')