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 let wlan = board_data.wlan;
44 for (let name in wlan)
45 if (wlan[name].path == path)
55 function wiphy_detect() {
56 let phys = nl.request(nl.const.NL80211_CMD_GET_WIPHY, nl.const.NLM_F_DUMP, { split_wiphy_dump: true });
60 for (let phy in phys) {
61 let name = phy.wiphy_name;
62 let path = phy_path(name);
64 antenna_rx: phy.wiphy_antenna_avail_rx,
65 antenna_tx: phy.wiphy_antenna_avail_tx,
69 let bands = info.bands;
70 for (let band in phy.wiphy_bands) {
71 if (!band || !band.freqs)
73 let freq = band.freqs[0].freq;
84 bands[band_name] = band_info;
87 if (band.vht_capa > 0)
91 for (let ift in band.iftype_data) {
96 he_phy_cap |= ift.he_cap_phy[0];
100 if (band_name != "2G" &&
101 (he_phy_cap & 0x18) || ((band.vht_capa >> 2) & 0x3))
102 band_info.max_width = 160;
103 else if (band_name != "2G" &&
104 (he_phy_cap & 4) || band.vht_capa > 0)
105 band_info.max_width = 80;
106 else if ((band.ht_capa & 0x2) || (he_phy_cap & 0x2))
107 band_info.max_width = 40;
109 band_info.max_width = 20;
111 let modes = band_info.modes = [ "NOHT" ];
115 push(modes, "VHT20");
118 if (band.ht_capa & 0x2) {
123 if (he_phy_cap & 0x2)
126 if (band_name == "2G")
129 push(modes, "VHT80");
132 if ((band.vht_capa >> 2) & 0x3)
133 push(modes, "VHT160");
134 if (he_phy_cap & 0x18)
135 push(modes, "HE160");
138 let entry = wiphy_get_entry(name, path);
145 if (!is_equal(prev_board_data, board_data)) {
146 let new_file = board_file + ".new";
148 let f = open(new_file, "wx");
151 f.write(sprintf("%.J\n", board_data));
153 rename(new_file, board_file);