86ec3431ea7d27a45f6a50cbae266bdf3f1e2023
[openwrt/staging/jow.git] / package / network / config / wifi-scripts / files / usr / share / hostap / wifi-detect.uc
1 #!/usr/bin/env ucode
2 'use strict';
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");
6
7 let board_file = "/etc/board.json";
8 let prev_board_data = json(readfile(board_file));
9 let board_data = json(readfile(board_file));
10
11 function phy_idx(name) {
12 return +rtrim(readfile(`/sys/class/ieee80211/${name}/index`));
13 }
14
15 function phy_path(name) {
16 let devpath = realpath(`/sys/class/ieee80211/${name}/device`);
17
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));
23
24 let ofs = index(dev_phys, name);
25 if (ofs > 0)
26 devpath += `+${ofs}`;
27
28 return devpath;
29 }
30
31 function cleanup() {
32 let wlan = board_data.wlan;
33
34 for (let name in wlan)
35 if (substr(name, 0, 3) == "phy")
36 delete wlan[name];
37 else
38 delete wlan[name].info;
39 }
40
41 function wiphy_get_entry(phy, path) {
42 let wlan = board_data.wlan;
43
44 for (let name in wlan)
45 if (wlan[name].path == path)
46 return wlan[name];
47
48 wlan[phy] = {
49 path: path
50 };
51
52 return wlan[phy];
53 }
54
55 function wiphy_detect() {
56 let phys = nl.request(nl.const.NL80211_CMD_GET_WIPHY, nl.const.NLM_F_DUMP, { split_wiphy_dump: true });
57 if (!phys)
58 return;
59
60 for (let phy in phys) {
61 let name = phy.wiphy_name;
62 let path = phy_path(name);
63 let info = {
64 antenna_rx: phy.wiphy_antenna_avail_rx,
65 antenna_tx: phy.wiphy_antenna_avail_tx,
66 bands: {},
67 };
68
69 let bands = info.bands;
70 for (let band in phy.wiphy_bands) {
71 if (!band || !band.freqs)
72 continue;
73 let freq = band.freqs[0].freq;
74 let band_info = {};
75 let band_name;
76 if (freq > 50000)
77 band_name = "60G";
78 else if (freq > 5900)
79 band_name = "6G";
80 else if (freq > 4000)
81 band_name = "5G";
82 else
83 band_name = "2G";
84 bands[band_name] = band_info;
85 if (band.ht_capa > 0)
86 band_info.ht = true;
87 if (band.vht_capa > 0)
88 band_info.vht = true;
89 let he_phy_cap = 0;
90
91 for (let ift in band.iftype_data) {
92 if (!ift.he_cap_phy)
93 continue;
94
95 band_info.he = true;
96 he_phy_cap |= ift.he_cap_phy[0];
97 /* TODO: EHT */
98 }
99
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;
108 else
109 band_info.max_width = 20;
110
111 let modes = band_info.modes = [ "NOHT" ];
112 if (band_info.ht)
113 push(modes, "HT20");
114 if (band_info.vht)
115 push(modes, "VHT20");
116 if (band_info.he)
117 push(modes, "HE20");
118 if (band.ht_capa & 0x2) {
119 push(modes, "HT40");
120 if (band_info.vht)
121 push(modes, "VHT40")
122 }
123 if (he_phy_cap & 0x2)
124 push(modes, "HE40");
125
126 if (band_name == "2G")
127 continue;
128 if (band_info.vht)
129 push(modes, "VHT80");
130 if (he_phy_cap & 4)
131 push(modes, "HE80");
132 if ((band.vht_capa >> 2) & 0x3)
133 push(modes, "VHT160");
134 if (he_phy_cap & 0x18)
135 push(modes, "HE160");
136 }
137
138 let entry = wiphy_get_entry(name, path);
139 entry.info = info;
140 }
141 }
142
143 cleanup();
144 wiphy_detect();
145 if (!is_equal(prev_board_data, board_data)) {
146 let new_file = board_file + ".new";
147 unlink(new_file);
148 let f = open(new_file, "wx");
149 if (!f)
150 exit(1);
151 f.write(sprintf("%.J\n", board_data));
152 f.close();
153 rename(new_file, board_file);
154 }