summaryrefslogtreecommitdiff
path: root/cethcan/main.c
blob: b9c6daff3cc49875387c4ff33bd88de2cde30140 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "cethcan.h"

struct event_base *ev_base;

int verbosity = 0;

int main(int argc, char **argv)
{
	int optch = 0;
	const char *cfgfile = "cethcan.json";
	json_error_t je;
	json_t *config;
	struct sigaction sa;

	do {
		optch = getopt(argc, argv, "vc:");
		switch (optch) {
		case 'c':
			cfgfile = optarg;
			break;
		case 'v':
			verbosity++;
			break;
		case -1:
			break;
		}
	} while (optch != -1);

	if (optind < argc) {
		fprintf(stderr, "leftover arguments\n");
		return 1;
	}

	config = json_load_file(cfgfile, JSON_REJECT_DUPLICATES, &je);
	if (!config) {
		fprintf(stderr, "failed to load config:\n%s:%d:%d %s\n",
			je.source, je.line, je.column, je.text);
		return 1;
	}
	if (!json_is_object(config)) {
		fprintf(stderr, "config must be object/dictionary\n");
		return 1;
	}

	ev_base = event_base_new();

	osc_init();

	can_init();

	json_t *ethercfg = json_object_get(config, "ethernet");
	for (size_t i = 0; i < json_array_size(ethercfg); i++) {
		json_t *c = json_array_get(ethercfg, i);
		if (ether_init(c))
			return 1;
	}

	json_t *lightcfg = json_object_get(config, "lights");
	for (size_t i = 0; i < json_array_size(lightcfg); i++) {
		json_t *c = json_array_get(lightcfg, i);
		if (light_init_conf(c))
			return 1;
	}

	json_t *beancfg = json_object_get(config, "beans");
	for (size_t i = 0; i < json_array_size(beancfg); i++) {
		json_t *c = json_array_get(beancfg, i);
		if (bean_init_conf(c))
			return 1;
	}

	json_t *socancfg = json_object_get(config, "socketcan");
	for (size_t i = 0; i < json_array_size(socancfg); i++) {
		json_t *c = json_array_get(socancfg, i);
		if (socan_init(c))
			return 1;
	}

	json_t *espcfg = json_object_get(config, "espnet");
	for (size_t i = 0; i < json_array_size(espcfg); i++) {
		json_t *c = json_array_get(espcfg, i);
		if (espnet_init_conf(c))
			return 1;
	}

	json_t *dmxcfg = json_object_get(config, "ttydmx");
	for (size_t i = 0; i < json_array_size(dmxcfg); i++) {
		json_t *c = json_array_get(dmxcfg, i);
		if (ttydmx_init_conf(c))
			return 1;
	}

	http_init();

	json_decref(config);

	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = SIG_IGN;
	sigaction(SIGPIPE, &sa, NULL);
	sigaction(SIGHUP, &sa, NULL);
	sigaction(SIGUSR1, &sa, NULL);
	sigaction(SIGUSR2, &sa, NULL);

	event_base_loop(ev_base, 0);
	return 0;
}