summaryrefslogtreecommitdiffstats
path: root/examples/main.uc
blob: 6c2e38fd6a0e50fdcaa3eb41db14b50baeb57334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import * as uci from "uci";
import * as uloop from "uloop";
import * as libubus from "ubus";
import { access, dirname } from "fs";

function ex_handler(e)
{
	netifd.log(netifd.L_WARNING, `Exception: ${e}\n${e.stacktrace[0].context}\n`);
}

uloop.guard(ex_handler);
libubus.guard(ex_handler);

let ubus = netifd.ubus = libubus.connect();
let wireless;

function uci_ctx()
{
	let savedir = netifd.dummy_mode ? "./tmp" : null;
	let ctx = uci.cursor(netifd.config_path, savedir, null, {
		strict: false
	});
	return ctx;
}

function config_init()
{
	let ctx = uci_ctx();

	if (wireless)
		wireless.config_init(ctx);
}

function config_start()
{
	if (wireless)
		wireless.config_start();
}

function check_interfaces()
{
	if (wireless)
		wireless.check_interfaces();
}

function hotplug(name, add)
{
	if (wireless)
		wireless.hotplug(name, add);
}

function ex_wrap(cb)
{
	let fn = cb;
	return (...args) => {
		try {
			return fn(...args);
		} catch (e) {
			netifd.log(netifd.L_WARNING, `${e}\n${e.stacktrace[0].context}`);
		}
	};
}

netifd.cb = {
	hotplug: ex_wrap(hotplug),
	config_init: ex_wrap(config_init),
	config_start: ex_wrap(config_start),
	check_interfaces: ex_wrap(check_interfaces),
};

const wireless_module = dirname(sourcepath()) + "/wireless.uc";
if (access(wireless_module, "r")) {
	try {
		wireless = loadfile(wireless_module)();
	} catch (e) {
		netifd.log(netifd.L_WARNING, `Error loading wireless module: ${e}\n${e.stacktrace[0].context}\n`);
	}
} else {
	netifd.log(netifd.L_WARNING, `Wireless module not found\n`);
}