1 let libubus = require("ubus");
2 import { open, readfile } from "fs";
3 import { wdev_create, wdev_remove, is_equal, vlist_new, phy_open } from "common";
5 let ubus = libubus.connect();
8 wpas.data.iface_phy = {};
9 wpas.data.macaddr_list = {};
11 function iface_stop(iface)
13 let ifname = iface.config.iface;
18 delete wpas.data.iface_phy[ifname];
19 wpas.remove_iface(ifname);
21 iface.running = false;
24 function iface_start(phydev, iface, macaddr_list)
26 let phy = phydev.name;
31 let ifname = iface.config.iface;
33 for (let field in iface.config)
34 wdev_config[field] = iface.config[field];
35 if (!wdev_config.macaddr)
36 wdev_config.macaddr = phydev.macaddr_next();
38 wpas.data.iface_phy[ifname] = phy;
40 let ret = wdev_create(phy, ifname, wdev_config);
42 wpas.printf(`Failed to create device ${ifname}: ${ret}`);
43 wpas.add_iface(iface.config);
47 function iface_cb(new_if, old_if)
49 if (old_if && new_if && is_equal(old_if.config, new_if.config)) {
50 new_if.running = old_if.running;
55 wpas.printf(`Update configuration for interface ${old_if.config.iface}`);
57 wpas.printf(`Remove interface ${old_if.config.iface}`);
63 function prepare_config(config)
65 config.config_data = readfile(config.config);
67 return { config: config };
70 function set_config(phy_name, config_list)
72 let phy = wpas.data.config[phy_name];
75 phy = vlist_new(iface_cb, false);
76 wpas.data.config[phy_name] = phy;
80 for (let config in config_list)
81 push(values, [ config.iface, prepare_config(config) ]);
86 function start_pending(phy_name)
88 let phy = wpas.data.config[phy_name];
89 let ubus = wpas.data.ubus;
91 if (!phy || !phy.data)
94 let phydev = phy_open(phy_name);
96 wpas.printf(`Could not open phy ${phy_name}`);
100 let macaddr_list = wpas.data.macaddr_list[phy_name];
101 phydev.macaddr_init(macaddr_list);
103 for (let ifname in phy.data)
104 iface_start(phydev, phy.data[ifname]);
113 call: function(req) {
114 if (!req.args.phy || req.args.stop == null)
115 return libubus.STATUS_INVALID_ARGUMENT;
117 let phy = wpas.data.config[req.args.phy];
119 return libubus.STATUS_NOT_FOUND;
123 for (let ifname in phy.data)
124 iface_stop(phy.data[ifname]);
126 start_pending(req.args.phy);
129 wpas.printf(`Error chaging state: ${e}\n${e.stacktrace[0].context}`);
130 return libubus.STATUS_INVALID_ARGUMENT;
135 phy_set_macaddr_list: {
140 call: function(req) {
141 let phy = req.args.phy;
143 return libubus.STATUS_INVALID_ARGUMENT;
145 wpas.data.macaddr_list[phy] = req.args.macaddr;
153 call: function(req) {
155 return libubus.STATUS_INVALID_ARGUMENT;
157 let phy = wpas.data.config[req.args.phy];
159 return libubus.STATUS_NOT_FOUND;
161 for (let ifname in phy.data) {
163 let iface = wpas.interfaces[ifname];
167 let status = iface.status();
171 if (status.state == "INTERFACE_DISABLED")
174 status.ifname = ifname;
181 return libubus.STATUS_NOT_FOUND;
190 call: function(req) {
192 return libubus.STATUS_INVALID_ARGUMENT;
194 wpas.printf(`Set new config for phy ${req.args.phy}`);
197 set_config(req.args.phy, req.args.config);
200 start_pending(req.args.phy);
202 wpas.printf(`Error loading config: ${e}\n${e.stacktrace[0].context}`);
203 return libubus.STATUS_INVALID_ARGUMENT;
220 call: function(req) {
221 if (!req.args.iface || !req.args.config)
222 return libubus.STATUS_INVALID_ARGUMENT;
224 if (wpas.add_iface(req.args) < 0)
225 return libubus.STATUS_INVALID_ARGUMENT;
236 call: function(req) {
238 return libubus.STATUS_INVALID_ARGUMENT;
240 wpas.remove_iface(req.args.iface);
246 wpas.data.ubus = ubus;
247 wpas.data.obj = ubus.publish("wpa_supplicant", main_obj);
249 function iface_event(type, name, data) {
250 let ubus = wpas.data.ubus;
254 wpas.data.obj.notify(`iface.${type}`, data, null, null, null, -1);
255 ubus.call("service", "event", { type: `wpa_supplicant.${name}.${type}`, data: {} });
258 function iface_hostapd_notify(phy, ifname, iface, state)
260 let ubus = wpas.data.ubus;
261 let status = iface.status();
262 let msg = { phy: phy };
266 case "AUTHENTICATING":
270 case "INTERFACE_DISABLED":
276 msg.frequency = status.frequency;
277 msg.sec_chan_offset = status.sec_chan_offset;
283 ubus.call("hostapd", "apsta_state", msg);
286 function iface_channel_switch(phy, ifname, iface, info)
292 csa_count: info.csa_count ? info.csa_count - 1 : 0,
293 frequency: info.frequency,
294 sec_chan_offset: info.sec_chan_offset,
296 ubus.call("hostapd", "apsta_state", msg);
300 shutdown: function() {
301 for (let phy in wpas.data.config)
303 wpas.ubus.disconnect();
305 iface_add: function(name, obj) {
306 iface_event("add", name);
308 iface_remove: function(name, obj) {
309 iface_event("remove", name);
311 state: function(ifname, iface, state) {
312 let phy = wpas.data.iface_phy[ifname];
314 wpas.printf(`no PHY for ifname ${ifname}`);
318 iface_hostapd_notify(phy, ifname, iface, state);
320 event: function(ifname, iface, ev, info) {
321 let phy = wpas.data.iface_phy[ifname];
323 wpas.printf(`no PHY for ifname ${ifname}`);
327 if (ev == "CH_SWITCH_STARTED")
328 iface_channel_switch(phy, ifname, iface, info);