util.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. -- Writes all lines from the file input to the file output except those starting with prefix
  2. -- Doesn't close the output file, but returns the file object
  3. local function do_filter_prefix(input, output, prefix)
  4. local f = io.open(output, 'w+')
  5. local l = prefix:len()
  6. for line in io.lines(input) do
  7. if line:sub(1, l) ~= prefix then
  8. f:write(line, '\n')
  9. end
  10. end
  11. return f
  12. end
  13. local function escape_args(ret, arg0, ...)
  14. if not arg0 then
  15. return ret
  16. end
  17. return escape_args(ret .. "'" .. string.gsub(arg0, "'", "'\\''") .. "' ", ...)
  18. end
  19. local os = os
  20. local string = string
  21. local tonumber = tonumber
  22. local ipairs = ipairs
  23. local table = table
  24. local nixio = require 'nixio'
  25. local sysconfig = require 'gluon.sysconfig'
  26. local site = require 'gluon.site_config'
  27. local uci = require('luci.model.uci').cursor()
  28. module 'gluon.util'
  29. function exec(...)
  30. return os.execute(escape_args('', 'exec', ...))
  31. end
  32. -- Removes all lines starting with a prefix from a file, optionally adding a new one
  33. function replace_prefix(file, prefix, add)
  34. local tmp = file .. '.tmp'
  35. local f = do_filter_prefix(file, tmp, prefix)
  36. if add then
  37. f:write(add)
  38. end
  39. f:close()
  40. os.rename(tmp, file)
  41. end
  42. function readline(fd)
  43. local line = fd:read('*l')
  44. fd:close()
  45. return line
  46. end
  47. function lock(file)
  48. exec('lock', file)
  49. end
  50. function unlock(file)
  51. exec('lock', '-u', file)
  52. end
  53. function node_id()
  54. return string.gsub(sysconfig.primary_mac, ':', '')
  55. end
  56. -- Generates a (hopefully) unique MAC address
  57. -- The first parameter defines the function and the second
  58. -- parameter an ID to add to the MAC address
  59. -- Functions and IDs defined so far:
  60. -- (1, 0): WAN (for mesh-on-WAN)
  61. -- (1, 1): LAN (for mesh-on-LAN)
  62. -- (2, n): client interface for the n'th radio
  63. -- (3, n): adhoc interface for n'th radio
  64. -- (4, 0): mesh VPN
  65. -- (5, n): mesh interface for n'th radio (802.11s)
  66. function generate_mac(f, i)
  67. local m1, m2, m3, m4, m5, m6 = string.match(sysconfig.primary_mac, '(%x%x):(%x%x):(%x%x):(%x%x):(%x%x):(%x%x)')
  68. m1 = nixio.bit.bor(tonumber(m1, 16), 0x02)
  69. m2 = (tonumber(m2, 16)+f) % 0x100
  70. m3 = (tonumber(m3, 16)+i) % 0x100
  71. return string.format('%02x:%02x:%02x:%s:%s:%s', m1, m2, m3, m4, m5, m6)
  72. end
  73. -- Iterate over all radios defined in UCI calling
  74. -- f(radio, index, site.wifiX) for each radio found while passing
  75. -- site.wifi24 for 2.4 GHz devices and site.wifi5 for 5 GHz ones.
  76. function iterate_radios(f)
  77. local radios = {}
  78. uci:foreach('wireless', 'wifi-device',
  79. function(s)
  80. table.insert(radios, s['.name'])
  81. end
  82. )
  83. for index, radio in ipairs(radios) do
  84. local hwmode = uci:get('wireless', radio, 'hwmode')
  85. if hwmode == '11g' or hwmode == '11ng' then
  86. f(radio, index, site.wifi24)
  87. elseif hwmode == '11a' or hwmode == '11na' then
  88. f(radio, index, site.wifi5)
  89. end
  90. end
  91. end