1 import * as nl80211 from "nl80211";
2 import * as rtnl from "rtnl";
3 import { readfile } from "fs";
6 ap: nl80211.const.NL80211_IFTYPE_AP,
7 mesh: nl80211.const.NL80211_IFTYPE_MESH_POINT,
8 sta: nl80211.const.NL80211_IFTYPE_STATION,
9 adhoc: nl80211.const.NL80211_IFTYPE_ADHOC,
10 monitor: nl80211.const.NL80211_IFTYPE_MONITOR,
13 function wdev_remove(name)
15 nl80211.request(nl80211.const.NL80211_CMD_DEL_INTERFACE, 0, { dev: name });
18 function __phy_is_fullmac(phyidx)
20 let data = nl80211.request(nl80211.const.NL80211_CMD_GET_WIPHY, 0, { wiphy: phyidx });
22 return !data.software_iftypes.ap_vlan;
25 function phy_is_fullmac(phy)
27 let phyidx = int(trim(readfile(`/sys/class/ieee80211/${phy}/index`)));
29 return __phy_is_fullmac(phyidx);
32 function find_reusable_wdev(phyidx)
34 if (!__phy_is_fullmac(phyidx))
37 let data = nl80211.request(
38 nl80211.const.NL80211_CMD_GET_INTERFACE,
39 nl80211.const.NLM_F_DUMP,
42 if (trim(readfile(`/sys/class/net/${res.ifname}/operstate`)) == "down")
47 function wdev_create(phy, name, data)
49 let phyidx = int(readfile(`/sys/class/ieee80211/${phy}/index`));
53 if (!iftypes[data.mode])
54 return `Invalid mode: ${data.mode}`;
59 iftype: iftypes[data.mode],
63 req["4addr"] = data["4addr"];
65 req.mac = data.macaddr;
69 let reuse_ifname = find_reusable_wdev(phyidx);
71 (reuse_ifname == name ||
72 rtnl.request(rtnl.const.RTM_SETLINK, 0, { dev: reuse_ifname, ifname: name}) != false))
74 nl80211.const.NL80211_CMD_SET_INTERFACE, 0, {
77 iftype: iftypes[data.mode],
81 nl80211.const.NL80211_CMD_NEW_INTERFACE,
82 nl80211.const.NLM_F_CREATE,
85 let error = nl80211.error();
89 if (data.powersave != null) {
90 nl80211.request(nl80211.const.NL80211_CMD_SET_POWER_SAVE, 0,
91 { dev: name, ps_state: data.powersave ? 1 : 0});
98 update: function(values, arg) {
105 this.data = new_data;
107 if (type(values) == "object") {
108 for (let key in values) {
109 old_data[key] = data[key];
110 new_data[key] = values[key];
114 for (let val in values) {
115 let cur_key = val[0];
116 let cur_obj = val[1];
118 old_data[cur_key] = data[cur_key];
119 new_data[cur_key] = val[1];
120 delete data[cur_key];
124 for (let key in data) {
125 cb(null, data[key], arg);
128 for (let key in new_data)
129 cb(new_data[key], old_data[key], arg);
133 function is_equal(val1, val2) {
136 if (t1 != type(val2))
140 if (length(val1) != length(val2))
143 for (let i = 0; i < length(val1); i++)
144 if (!is_equal(val1[i], val2[i]))
148 } else if (t1 == "object") {
149 for (let key in val1)
150 if (!is_equal(val1[key], val2[key]))
152 for (let key in val2)
161 function vlist_new(cb) {
168 export { wdev_remove, wdev_create, is_equal, vlist_new, phy_is_fullmac };