1 let libubus = require("ubus");
2 import { open, readfile } from "fs";
3 import { wdev_create, wdev_remove, is_equal, vlist_new } from "common";
5 let ubus = libubus.connect();
8 wpas.data.iface_phy = {};
10 function iface_stop(iface)
12 let ifname = iface.config.iface;
17 delete wpas.data.iface_phy[ifname];
18 wpas.remove_iface(ifname);
20 iface.running = false;
23 function iface_start(phy, iface)
28 let ifname = iface.config.iface;
30 wpas.data.iface_phy[ifname] = phy;
32 let ret = wdev_create(phy, ifname, iface.config);
34 wpas.printf(`Failed to create device ${ifname}: ${ret}`);
35 wpas.add_iface(iface.config);
39 function iface_cb(new_if, old_if)
41 if (old_if && new_if && is_equal(old_if.config, new_if.config)) {
42 new_if.running = old_if.running;
47 wpas.printf(`Update configuration for interface ${old_if.config.iface}`);
49 wpas.printf(`Remove interface ${old_if.config.iface}`);
55 function prepare_config(config)
57 config.config_data = readfile(config.config);
59 return { config: config };
62 function set_config(phy_name, config_list)
64 let phy = wpas.data.config[phy_name];
67 phy = vlist_new(iface_cb, false);
68 wpas.data.config[phy_name] = phy;
72 for (let config in config_list)
73 push(values, [ config.iface, prepare_config(config) ]);
78 function start_pending(phy_name)
80 let phy = wpas.data.config[phy_name];
82 for (let ifname in phy.data)
83 iface_start(phy_name, phy.data[ifname]);
93 if (!req.args.phy || req.args.stop == null)
94 return libubus.STATUS_INVALID_ARGUMENT;
96 let phy = wpas.data.config[req.args.phy];
98 return libubus.STATUS_NOT_FOUND;
102 for (let ifname in phy.data)
103 iface_stop(phy.data[ifname]);
105 start_pending(req.args.phy);
108 wpas.printf(`Error chaging state: ${e}\n${e.stacktrace[0].context}`);
109 return libubus.STATUS_INVALID_ARGUMENT;
118 call: function(req) {
120 return libubus.STATUS_INVALID_ARGUMENT;
122 let phy = wpas.data.config[req.args.phy];
124 return libubus.STATUS_NOT_FOUND;
126 for (let ifname in phy.data) {
128 let iface = wpas.interfaces[ifname];
132 let status = iface.status();
136 if (status.state == "INTERFACE_DISABLED")
139 status.ifname = ifname;
146 return libubus.STATUS_NOT_FOUND;
155 call: function(req) {
157 return libubus.STATUS_INVALID_ARGUMENT;
159 wpas.printf(`Set new config for phy ${req.args.phy}`);
162 set_config(req.args.phy, req.args.config);
165 start_pending(req.args.phy);
167 wpas.printf(`Error loading config: ${e}\n${e.stacktrace[0].context}`);
168 return libubus.STATUS_INVALID_ARGUMENT;
185 call: function(req) {
186 if (!req.args.iface || !req.args.config)
187 return libubus.STATUS_INVALID_ARGUMENT;
189 if (wpas.add_iface(req.args) < 0)
190 return libubus.STATUS_INVALID_ARGUMENT;
201 call: function(req) {
203 return libubus.STATUS_INVALID_ARGUMENT;
205 wpas.remove_iface(req.args.iface);
211 wpas.data.ubus = ubus;
212 wpas.data.obj = ubus.publish("wpa_supplicant", main_obj);
214 function iface_event(type, name, data) {
215 let ubus = wpas.data.ubus;
219 wpas.data.obj.notify(`iface.${type}`, data, null, null, null, -1);
220 ubus.call("service", "event", { type: `wpa_supplicant.${name}.${type}`, data: {} });
223 function iface_hostapd_notify(phy, ifname, iface, state)
225 let ubus = wpas.data.ubus;
226 let status = iface.status();
227 let msg = { phy: phy };
231 case "AUTHENTICATING":
235 case "INTERFACE_DISABLED":
241 msg.frequency = status.frequency;
242 msg.sec_chan_offset = status.sec_chan_offset;
248 ubus.call("hostapd", "apsta_state", msg);
251 function iface_channel_switch(phy, ifname, iface, info)
257 csa_count: info.csa_count ? info.csa_count - 1 : 0,
258 frequency: info.frequency,
259 sec_chan_offset: info.sec_chan_offset,
261 ubus.call("hostapd", "apsta_state", msg);
265 shutdown: function() {
266 for (let phy in wpas.data.config)
268 wpas.ubus.disconnect();
270 iface_add: function(name, obj) {
271 iface_event("add", name);
273 iface_remove: function(name, obj) {
274 iface_event("remove", name);
276 state: function(ifname, iface, state) {
277 let phy = wpas.data.iface_phy[ifname];
279 wpas.printf(`no PHY for ifname ${ifname}`);
283 iface_hostapd_notify(phy, ifname, iface, state);
285 event: function(ifname, iface, ev, info) {
286 let phy = wpas.data.iface_phy[ifname];
288 wpas.printf(`no PHY for ifname ${ifname}`);
292 if (ev == "CH_SWITCH_STARTED")
293 iface_channel_switch(phy, ifname, iface, info);