]> git.ipfire.org Git - thirdparty/openwrt.git/commitdiff
wifi-scripts: ucode: add support for iwinfo nl80211 phyname
authorFelix Fietkau <nbd@nbd.name>
Wed, 16 Jul 2025 11:09:03 +0000 (13:09 +0200)
committerFelix Fietkau <nbd@nbd.name>
Fri, 18 Jul 2025 10:33:14 +0000 (12:33 +0200)
It is needed for compatibility with scripts that rely on this
existing iwinfo behavior

Signed-off-by: Felix Fietkau <nbd@nbd.name>
package/network/config/wifi-scripts/files-ucode/lib/netifd/wireless/mac80211.sh
package/network/config/wifi-scripts/files-ucode/usr/bin/iwinfo
package/network/config/wifi-scripts/files-ucode/usr/share/ucode/wifi/utils.uc [new file with mode: 0644]

index 96cd0d0fc3730f318de3e6cae67fddb7f9df3725..249c7a4596dc4f31f5d8339447d3e81b14580c79 100755 (executable)
@@ -8,6 +8,7 @@ import * as supplicant from 'wifi.supplicant';
 import * as hostapd from 'wifi.hostapd';
 import * as netifd from 'wifi.netifd';
 import * as iface from 'wifi.iface';
+import { find_phy } from 'wifi.utils';
 import * as nl80211 from 'nl80211';
 import * as fs from 'fs';
 
@@ -41,101 +42,6 @@ function reset_config(phy, radio) {
        system(`ucode /usr/share/hostap/wdev.uc ${name} set_config '{}'`);
 }
 
-function phy_filename(phy, name) {
-       return `/sys/class/ieee80211/${phy}/${name}`;
-}
-
-function phy_file(phy, name) {
-       return fs.readfile(phy_filename(phy, name));
-}
-
-function phy_index(phy) {
-       return +phy_file(phy, "index");
-}
-
-function phy_path_match(phy, path) {
-       let phy_path = fs.realpath(phy_filename(phy, "device"));
-       return substr(phy_path, -length(path)) == path;
-}
-
-function __find_phy_by_path(phys, path) {
-       if (!path)
-               return null;
-
-       path = split(path, "+");
-       phys = filter(phys, (phy) => phy_path_match(phy, path[0]));
-       phys = sort(phys, (a, b) => phy_index(a) - phy_index(b));
-
-       return phys[+path[1]];
-}
-
-function find_phy_by_macaddr(phys, macaddr) {
-       macaddr = lc(macaddr);
-       return filter(phys, (phy) => phy_file(phy, "macaddr") == macaddr)[0];
-}
-
-function rename_phy_by_name(phys, name) {
-       let data = json(fs.readfile("/etc/board.json")).wlan;
-       if (!data)
-               return;
-
-       data = data[name];
-       if (!data)
-               return;
-
-       let prev_name = __find_phy_by_path(phys, data.path);
-       if (!prev_name)
-               return;
-
-       let idx = phy_index(prev_name);
-       nl80211.request(nl80211.const.NL80211_CMD_SET_WIPHY, 0, {
-               wiphy: idx,
-               wiphy_name: name
-       });
-       return true;
-}
-
-function find_phy_by_path(phys, path) {
-       let name = __find_phy_by_path(phys, path);
-       if (!name)
-               return;
-
-       let data = json(fs.readfile("/etc/board.json")).wlan;
-       if (!data || data[name])
-               return name;
-
-       for (let cur_name, cur_data in data) {
-               if (!phy_path_match(name, cur_data.path))
-                       continue;
-
-               let idx = phy_index(name);
-               nl80211.request(nl80211.const.NL80211_CMD_SET_WIPHY, 0, {
-                       wiphy: idx,
-                       wiphy_name: cur_name
-               });
-
-               return cur_name;
-       }
-
-       return name;
-}
-
-function find_phy_by_name(phys, name) {
-       if (index(phys, name) >= 0)
-               return name;
-
-       rename_phy_by_name(phys, name);
-       return index(phys, name) < 0 ? null : name;
-}
-
-function find_phy(config) {
-       let phys = fs.lsdir("/sys/class/ieee80211");
-
-       return find_phy_by_path(phys, config.path) ??
-              find_phy_by_macaddr(phys, config.macaddr) ??
-              find_phy_by_name(phys, config.phy);
-}
-
 function get_channel_frequency(band, channel) {
        if (channel < 1)
                return null;
@@ -254,7 +160,7 @@ function config_add_mesh_params(config, data) {
 function setup() {
        let data = json(ARGV[3]);
 
-       data.phy = find_phy(data.config);
+       data.phy = find_phy(data.config, true);
        if (!data.phy) {
                log('Bug: PHY is undefined for device');
                netifd.set_retry(false);
index 3b274f838c01c36002a401cbeed48dcdba44ec9b..d51c3ad6faf09c124a12b9aa70ee96ae4d6e1fcd 100755 (executable)
@@ -2,6 +2,8 @@
 
 'use strict';
 
+import { find_phy } from 'wifi.utils';
+import * as uci from 'uci';
 import * as iwinfo from 'iwinfo';
 
 function print_assoclist(stations) {
@@ -123,6 +125,24 @@ const commands = {
        txpowerlist: [ iwinfo.txpowerlist, print_txpowerlist ],
 };
 
+if (ARGV[0] == 'nl80211' && ARGV[1] == 'phyname') {
+       let sec_name = ARGV[2];
+       let sec = uci.cursor(null, null, null, { strict: false }).get_all('wireless', sec_name);
+       if (!sec || sec['.type'] != 'wifi-device') {
+               warn(`Config section ${sec_name} not found\n`);
+               return 1;
+       }
+
+       let name = find_phy(sec);
+       if (!name) {
+               warn('Phy not found\n');
+               return 1;
+       }
+
+       print(name + '\n');
+       return 0;
+}
+
 if (length(ARGV) == 2 && iwinfo.ifaces[ARGV[0]])
        for (let cmd, cb in commands)
                if (substr(cmd, 0, length(ARGV[1])) == ARGV[1]) {
@@ -145,7 +165,7 @@ case 'iface':
        return 0;
 }
 
-printf('Usage:\n' +
+warn('Usage:\n' +
        '\tiwinfo <device> info\n' +
        '\tiwinfo <device> scan\n' +
        '\tiwinfo <device> txpowerlist\n' +
@@ -153,4 +173,5 @@ printf('Usage:\n' +
        '\tiwinfo <device> assoclist\n' +
        '\tiwinfo <device> countrylist\n' +
        '\tiwinfo <device> htmodelist\n' +
-       '\tiwinfo <backend> phyname <section>\n');
+       '\tiwinfo nl80211 phyname <section>\n');
+return 1;
diff --git a/package/network/config/wifi-scripts/files-ucode/usr/share/ucode/wifi/utils.uc b/package/network/config/wifi-scripts/files-ucode/usr/share/ucode/wifi/utils.uc
new file mode 100644 (file)
index 0000000..0ec8505
--- /dev/null
@@ -0,0 +1,107 @@
+'use strict';
+
+import { readfile, realpath, lsdir } from "fs";
+import * as nl80211 from "nl80211";
+
+function phy_filename(phy, name) {
+       return `/sys/class/ieee80211/${phy}/${name}`;
+}
+
+function phy_file(phy, name) {
+       return readfile(phy_filename(phy, name));
+}
+
+function phy_index(phy) {
+       return +phy_file(phy, "index");
+}
+
+function phy_path_match(phy, path) {
+       let phy_path = realpath(phy_filename(phy, "device"));
+       return substr(phy_path, -length(path)) == path;
+}
+
+function __find_phy_by_path(phys, path) {
+       if (!path)
+               return null;
+
+       path = split(path, "+");
+       phys = filter(phys, (phy) => phy_path_match(phy, path[0]));
+       phys = sort(phys, (a, b) => phy_index(a) - phy_index(b));
+
+       return phys[+path[1]];
+}
+
+function find_phy_by_macaddr(phys, macaddr) {
+       macaddr = lc(macaddr);
+       return filter(phys, (phy) => phy_file(phy, "macaddr") == macaddr)[0];
+}
+
+function rename_phy_by_name(phys, name, rename) {
+       let data = json(readfile("/etc/board.json")).wlan;
+       if (!data)
+               return;
+
+       data = data[name];
+       if (!data)
+               return;
+
+       let prev_name = __find_phy_by_path(phys, data.path);
+       if (!prev_name)
+               return;
+
+       if (!rename)
+               return true;
+
+       let idx = phy_index(prev_name);
+       nl80211.request(nl80211.const.NL80211_CMD_SET_WIPHY, 0, {
+               wiphy: idx,
+               wiphy_name: name
+       });
+       return true;
+}
+
+function find_phy_by_path(phys, path) {
+       let name = __find_phy_by_path(phys, path);
+       if (!name)
+               return;
+
+       let data = json(readfile("/etc/board.json")).wlan;
+       if (!data || data[name])
+               return name;
+
+       for (let cur_name, cur_data in data) {
+               if (!phy_path_match(name, cur_data.path))
+                       continue;
+
+               let idx = phy_index(name);
+               nl80211.request(nl80211.const.NL80211_CMD_SET_WIPHY, 0, {
+                       wiphy: idx,
+                       wiphy_name: cur_name
+               });
+
+               return cur_name;
+       }
+
+       return name;
+}
+
+function find_phy_by_name(phys, name, rename) {
+       if (index(phys, name) >= 0)
+               return name;
+
+       if (!rename_phy_by_name(phys, name, rename))
+               return;
+
+       if (!rename)
+               return name;
+
+       return index(phys, name) < 0 ? null : name;
+}
+
+export function find_phy(config, rename) {
+       let phys = lsdir("/sys/class/ieee80211");
+
+       return find_phy_by_path(phys, config.path) ??
+              find_phy_by_macaddr(phys, config.macaddr) ??
+              find_phy_by_name(phys, config.phy, rename);
+};