summaryrefslogtreecommitdiff
path: root/mesh/main.c
blob: 145bcfa98d8592fd861d2542972430ef6ca6516e (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2017-2019  Intel Corporation. All rights reserved.
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>

#include <sys/prctl.h>
#include <sys/stat.h>
#include <ell/ell.h>

#include "lib/bluetooth.h"
#include "lib/mgmt.h"

#include "mesh/mesh.h"
#include "mesh/crypto.h"
#include "mesh/dbus.h"
#include "mesh/mesh-io.h"
#include "mesh/util.h"

static const char *storage_dir;
static const char *mesh_conf_fname;
static enum mesh_io_type io_type;
static void *io_opts;

static const struct option main_options[] = {
	{ "io",		required_argument,	NULL, 'i' },
	{ "storage",	required_argument,	NULL, 's' },
	{ "config",	required_argument,	NULL, 'c' },
	{ "nodetach",	no_argument,		NULL, 'n' },
	{ "debug",	no_argument,		NULL, 'd' },
	{ "dbus-debug",	no_argument,		NULL, 'b' },
	{ "help",	no_argument,		NULL, 'h' },
	{ }
};

static const char *io_usage =
	       "\t(auto | generic:[hci]<index> | unit:<fd_path>)\n"
	       "\t\tauto - Use first available controller (MGMT or raw HCI)\n"
	       "\t\tgeneric - Use raw HCI io on interface hci<index>\n"
	       "\t\tunit - Use test IO (for automatic testing only)\n";

static void usage(void)
{
	fprintf(stderr,
		"Usage:\n"
	       "\tbluetooth-meshd [options]\n");
	fprintf(stderr,
		"Options:\n"
	       "\t--io <io>         Use specified io (default: auto)\n"
	       "\t--config          Daemon configuration directory\n"
	       "\t--storage         Mesh node(s) configuration directory\n"
	       "\t--nodetach        Run in foreground\n"
	       "\t--debug           Enable debug output\n"
	       "\t--dbus-debug      Enable D-Bus debugging\n"
	       "\t--help            Show %s information\n", __func__);
	fprintf(stderr, "\n\t io: %s", io_usage);
}

static void do_debug(const char *str, void *user_data)
{
	const char *prefix = user_data;

	l_info("%s%s", prefix, str);
}

static void mesh_ready_callback(void *user_data, bool success)
{
	struct l_dbus *dbus = user_data;

	l_info("mesh_ready_callback");
	if (!success) {
		l_error("Failed to start mesh");
		l_main_quit();
		return;
	}

	if (!dbus_init(dbus)) {
		l_error("Failed to initialize mesh D-Bus resources");
		l_main_quit();
	}
}

static void request_name_callback(struct l_dbus *dbus, bool success,
					bool queued, void *user_data)
{
	if (!success && io_type != MESH_IO_TYPE_UNIT_TEST) {
		l_info("Request name failed");
		l_main_quit();
		return;
	}

	if (!mesh_init(storage_dir, mesh_conf_fname, io_type, io_opts,
					mesh_ready_callback, dbus)) {
		l_error("Failed to initialize mesh");
		l_main_quit();
	}
}

static void ready_callback(void *user_data)
{
	struct l_dbus *dbus = user_data;

	l_info("D-Bus ready");
	l_dbus_name_acquire(dbus, BLUEZ_MESH_NAME, false, false, false,
						request_name_callback, NULL);
}

static void disconnect_callback(void *user_data)
{
	l_main_quit();
}

static void kill_to(struct l_timeout *timeout, void *user_data)
{
	l_timeout_remove(timeout);
	l_main_quit();
}

static void signal_handler(uint32_t signo, void *user_data)
{
	static bool terminated;

	if (terminated)
		return;

	l_info("Terminating");

	mesh_cleanup(true);

	if (io_type != MESH_IO_TYPE_UNIT_TEST)
		l_timeout_create(1, kill_to, NULL, NULL);
	else
		l_main_quit();

	terminated = true;
}

static bool parse_io(const char *optarg, enum mesh_io_type *type, void **opts)
{
	if (strstr(optarg, "auto") == optarg) {
		int *index = l_new(int, 1);

		*type = MESH_IO_TYPE_AUTO;
		*opts = index;

		optarg += strlen("auto");
		*index = MGMT_INDEX_NONE;
		return true;

		return false;
	} else if (strstr(optarg, "generic") == optarg) {
		int *index = l_new(int, 1);

		*type = MESH_IO_TYPE_GENERIC;
		*opts = index;

		optarg += strlen("generic");
		if (!*optarg || *optarg != ':')
			return false;

		optarg++;

		if (sscanf(optarg, "hci%d", index) == 1)
			return true;

		if (sscanf(optarg, "%d", index) == 1)
			return true;

		return false;

	} else if (strstr(optarg, "unit") == optarg) {
		char *test_path;

		*type = MESH_IO_TYPE_UNIT_TEST;

		optarg += strlen("unit");
		if (*optarg != ':')
			return false;

		optarg++;
		test_path = strdup(optarg);

		*opts = test_path;
		return true;
	}

	return false;
}

int main(int argc, char *argv[])
{
	int status;
	bool detached = true;
	bool dbus_debug = false;
	struct l_dbus *dbus = NULL;
	char *io = NULL;
	int hci_index;

	if (!l_main_init())
		return -1;

	l_log_set_stderr();

	if (!mesh_crypto_check_avail()) {
		l_error("Mesh Crypto functions unavailable");
		status = l_main_run_with_signal(signal_handler, NULL);
		goto done;
	}

	for (;;) {
		int opt;

		opt = getopt_long(argc, argv, "u:i:s:c:ndbh", main_options,
									NULL);
		if (opt < 0)
			break;

		switch (opt) {
		case 'u':
			if (sscanf(optarg, "%d", &hci_index) == 1 ||
					sscanf(optarg, "%d", &hci_index) == 1)
				io = l_strdup_printf("unit:%d", hci_index);
			else
				io = l_strdup(optarg);
			break;
		case 'i':
			if (sscanf(optarg, "hci%d", &hci_index) == 1 ||
					sscanf(optarg, "%d", &hci_index) == 1)
				io = l_strdup_printf("generic:%s", optarg);
			else
				io = l_strdup(optarg);
			break;
		case 'n':
			detached = false;
			break;
		case 'd':
			enable_debug();
			break;
		case 's':
			storage_dir = optarg;
			break;
		case 'c':
			mesh_conf_fname = optarg;
			break;
		case 'b':
			dbus_debug = true;
			break;
		case 'h':
			usage();
			status = EXIT_SUCCESS;
			goto done;
		default:
			usage();
			status = EXIT_FAILURE;
			goto done;
		}
	}

	if (!io)
		io = l_strdup_printf("auto");

	if (!parse_io(io, &io_type, &io_opts)) {
		l_error("Invalid io: %s\n%s", io, io_usage);
		status = EXIT_FAILURE;
		goto done;
	}

	l_free(io);
	io = NULL;

	if (!detached)
		umask(0077);

	if (io_type != MESH_IO_TYPE_UNIT_TEST)
		dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
	else {
		dbus = l_dbus_new_default(L_DBUS_SESSION_BUS);
		prctl(PR_SET_PDEATHSIG, SIGSEGV);
	}

	if (!dbus) {
		l_error("unable to connect to D-Bus");
		status = EXIT_FAILURE;
		goto done;
	}

	if (dbus_debug)
		l_dbus_set_debug(dbus, do_debug, "[DBUS] ", NULL);
	l_dbus_set_ready_handler(dbus, ready_callback, dbus, NULL);
	l_dbus_set_disconnect_handler(dbus, disconnect_callback, NULL, NULL);

	if (!l_dbus_object_manager_enable(dbus, "/")) {
		l_error("Failed to enable Object Manager");
		status = EXIT_FAILURE;
		goto done;
	}

	status = l_main_run_with_signal(signal_handler, NULL);

done:
	l_free(io);
	l_free(io_opts);

	mesh_cleanup(false);
	l_dbus_destroy(dbus);
	l_main_exit();

	return status;
}