mac80211.uc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env ucode
  2. import { readfile } from "fs";
  3. import * as uci from 'uci';
  4. const bands_order = [ "6G", "5G", "2G" ];
  5. const htmode_order = [ "EHT", "HE", "VHT", "HT" ];
  6. let board = json(readfile("/etc/board.json"));
  7. if (!board.wlan)
  8. exit(0);
  9. let idx = 0;
  10. let commit;
  11. let config = uci.cursor().get_all("wireless") ?? {};
  12. function radio_exists(path, macaddr, phy, radio) {
  13. for (let name, s in config) {
  14. if (s[".type"] != "wifi-device")
  15. continue;
  16. if (radio != null && int(s.radio) != radio)
  17. continue;
  18. if (s.macaddr && lc(s.macaddr) == lc(macaddr))
  19. return true;
  20. if (s.phy == phy)
  21. return true;
  22. if (!s.path || !path)
  23. continue;
  24. if (substr(s.path, -length(path)) == path)
  25. return true;
  26. }
  27. }
  28. for (let phy_name, phy in board.wlan) {
  29. let info = phy.info;
  30. if (!info || !length(info.bands))
  31. continue;
  32. let radios = length(info.radios) > 0 ? info.radios : [{ bands: info.bands }];
  33. for (let radio in radios) {
  34. while (config[`radio${idx}`])
  35. idx++;
  36. let name = "radio" + idx;
  37. let s = "wireless." + name;
  38. let si = "wireless.default_" + name;
  39. let band_name = filter(bands_order, (b) => radio.bands[b])[0];
  40. if (!band_name)
  41. continue;
  42. let band = info.bands[band_name];
  43. let rband = radio.bands[band_name];
  44. let channel = rband.default_channel ?? "auto";
  45. let width = band.max_width;
  46. if (band_name == "2G")
  47. width = 20;
  48. else if (width > 80)
  49. width = 80;
  50. let htmode = filter(htmode_order, (m) => band[lc(m)])[0];
  51. if (htmode)
  52. htmode += width;
  53. else
  54. htmode = "NOHT";
  55. if (!phy.path)
  56. continue;
  57. let macaddr = trim(readfile(`/sys/class/ieee80211/${phy_name}/macaddress`));
  58. if (radio_exists(phy.path, macaddr, phy_name, radio.index))
  59. continue;
  60. let id = `phy='${phy_name}'`;
  61. if (match(phy_name, /^phy[0-9]/))
  62. id = `path='${phy.path}'`;
  63. band_name = lc(band_name);
  64. let country, encryption, defaults, num_global_macaddr;
  65. if (band_name == '6g') {
  66. country = '00';
  67. encryption = 'owe';
  68. } else {
  69. encryption = 'open';
  70. }
  71. if (board.wlan.defaults) {
  72. defaults = board.wlan.defaults.ssids?.[band_name]?.ssid ? board.wlan.defaults.ssids?.[band_name] : board.wlan.defaults.ssids?.all;
  73. country = board.wlan.defaults.country;
  74. if (!country && band_name != '2g')
  75. defaults = null;
  76. num_global_macaddr = board.wlan.defaults.ssids?.[band_name]?.mac_count;
  77. }
  78. if (length(info.radios) > 0)
  79. id += `\nset ${s}.radio='${radio.index}'`;
  80. print(`set ${s}=wifi-device
  81. set ${s}.type='mac80211'
  82. set ${s}.${id}
  83. set ${s}.band='${band_name}'
  84. set ${s}.channel='${channel}'
  85. set ${s}.htmode='${htmode}'
  86. set ${s}.country='${country || ''}'
  87. set ${s}.num_global_macaddr='${num_global_macaddr || ''}'
  88. set ${si}=wifi-iface
  89. set ${si}.device='${name}'
  90. set ${si}.network='lan'
  91. set ${si}.mode='ap'
  92. set ${si}.ssid='${defaults?.ssid || "OpenWrt"}'
  93. set ${si}.encryption='${defaults?.encryption || encryption}'
  94. set ${si}.key='${defaults?.key || ""}'
  95. set ${si}.disabled='${defaults ? 0 : 1}'
  96. `);
  97. config[name] = {};
  98. commit = true;
  99. }
  100. }
  101. if (commit)
  102. print("commit wireless\n");