3 import { readfile, writefile, realpath, glob, basename, unlink, open, rename } from "fs";
4 import { is_equal } from "/usr/share/hostap/common.uc";
5 let nl = require("nl80211");
7 let board_file = "/etc/board.json";
8 let prev_board_data = json(readfile(board_file));
9 let board_data = json(readfile(board_file));
11 function phy_idx(name) {
12 return +rtrim(readfile(`/sys/class/ieee80211/${name}/index`));
15 function phy_path(name) {
16 let devpath = realpath(`/sys/class/ieee80211/${name}/device`);
18 devpath = replace(devpath, /^\/sys\/devices\//, "");
19 if (match(devpath, /^platform\/.*\/pci/))
20 devpath = replace(devpath, /^platform\//, "");
21 let dev_phys = map(glob(`/sys/class/ieee80211/${name}/device/ieee80211/*`), basename);
22 sort(dev_phys, (a, b) => phy_idx(a) - phy_idx(b));
24 let ofs = index(dev_phys, name);
32 let wlan = board_data.wlan;
34 for (let name in wlan)
35 if (substr(name, 0, 3) == "phy")
38 delete wlan[name].info;
41 function wiphy_get_entry(phy, path) {
42 board_data.wlan ??= {};
44 let wlan = board_data.wlan;
45 for (let name in wlan)
46 if (wlan[name].path == path)
56 function wiphy_detect() {
57 let phys = nl.request(nl.const.NL80211_CMD_GET_WIPHY, nl.const.NLM_F_DUMP, { split_wiphy_dump: true });
61 for (let phy in phys) {
62 let name = phy.wiphy_name;
63 let path = phy_path(name);
65 antenna_rx: phy.wiphy_antenna_avail_rx,
66 antenna_tx: phy.wiphy_antenna_avail_tx,
70 let bands = info.bands;
71 for (let band in phy.wiphy_bands) {
72 if (!band || !band.freqs)
74 let freq = band.freqs[0].freq;
85 bands[band_name] = band_info;
88 if (band.vht_capa > 0)
92 for (let ift in band.iftype_data) {
97 he_phy_cap |= ift.he_cap_phy[0];
101 if (band_name != "2G" &&
102 (he_phy_cap & 0x18) || ((band.vht_capa >> 2) & 0x3))
103 band_info.max_width = 160;
104 else if (band_name != "2G" &&
105 (he_phy_cap & 4) || band.vht_capa > 0)
106 band_info.max_width = 80;
107 else if ((band.ht_capa & 0x2) || (he_phy_cap & 0x2))
108 band_info.max_width = 40;
110 band_info.max_width = 20;
112 let modes = band_info.modes = [ "NOHT" ];
116 push(modes, "VHT20");
119 if (band.ht_capa & 0x2) {
124 if (he_phy_cap & 0x2)
127 if (band_name == "2G")
130 push(modes, "VHT80");
133 if ((band.vht_capa >> 2) & 0x3)
134 push(modes, "VHT160");
135 if (he_phy_cap & 0x18)
136 push(modes, "HE160");
139 let entry = wiphy_get_entry(name, path);
146 if (!is_equal(prev_board_data, board_data)) {
147 let new_file = board_file + ".new";
149 let f = open(new_file, "wx");
152 f.write(sprintf("%.J\n", board_data));
154 rename(new_file, board_file);