3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2017-2019 Intel Corporation. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
32 #include "lib/bluetooth.h"
35 #include "mesh/mesh.h"
36 #include "mesh/crypto.h"
37 #include "mesh/dbus.h"
38 #include "mesh/mesh-io.h"
40 static const char *config_dir;
41 static const char *mesh_conf_fname;
42 static enum mesh_io_type io_type;
44 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
48 static const struct option main_options[] = {
49 { "io", required_argument, NULL, 'i' },
50 { "config", optional_argument, NULL, 'c' },
51 { "nodetach", no_argument, NULL, 'n' },
52 { "debug", no_argument, NULL, 'd' },
53 { "dbus-debug", no_argument, NULL, 'b' },
54 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
55 { "use_raw", no_argument, NULL, 'r' },
57 { "help", no_argument, NULL, 'h' },
61 static void usage(void)
65 "\tbluetooth-meshd [options]\n");
68 "\t--io <io> Use specified io (default: generic)\n"
69 "\t--config Configuration directory\n"
70 "\t--nodetach Run in foreground\n"
71 "\t--debug Enable debug output\n"
72 "\t--dbus-debug Enable D-Bus debugging\n"
73 "\t--help Show %s information\n", __func__);
76 "\t([hci]<index> | generic[:[hci]<index>])\n"
77 "\t\tUse generic HCI io on interface hci<index>, or the first\n"
78 "\t\tavailable one\n");
81 static void do_debug(const char *str, void *user_data)
83 const char *prefix = user_data;
85 l_info("%s%s", prefix, str);
88 static void mesh_ready_callback(void *user_data, bool success)
90 struct l_dbus *dbus = user_data;
93 l_error("Failed to start mesh");
98 if (!dbus_init(dbus)) {
99 l_error("Failed to initialize mesh D-Bus resources");
104 static void request_name_callback(struct l_dbus *dbus, bool success,
105 bool queued, void *user_data)
107 l_info("Request name %s",
108 success ? "success": "failed");
115 if (!mesh_init(config_dir, mesh_conf_fname, io_type, io_opts,
116 mesh_ready_callback, dbus)) {
117 l_error("Failed to initialize mesh");
122 static void ready_callback(void *user_data)
124 struct l_dbus *dbus = user_data;
126 l_info("D-Bus ready");
127 l_dbus_name_acquire(dbus, BLUEZ_MESH_NAME, false, false, false,
128 request_name_callback, NULL);
131 static void disconnect_callback(void *user_data)
136 static void signal_handler(uint32_t signo, void *user_data)
138 static bool terminated;
143 l_info("Terminating");
148 static bool parse_io(const char *optarg, enum mesh_io_type *type, void **opts)
150 if (strstr(optarg, "generic") == optarg) {
151 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
152 struct mesh_io_opts *m_io_opts = l_new(struct mesh_io_opts, 1);
153 int *index = &(m_io_opts->index);
154 m_io_opts->use_raw = use_raw;
157 int *index = l_new(int, 1);
160 *type = MESH_IO_TYPE_GENERIC;
162 optarg += strlen("generic");
164 *index = MGMT_INDEX_NONE;
173 if (sscanf(optarg, "hci%d", index) == 1)
176 if (sscanf(optarg, "%d", index) == 1)
185 int main(int argc, char *argv[])
188 bool detached = true;
189 bool dbus_debug = false;
190 struct l_dbus *dbus = NULL;
199 if (!mesh_crypto_check_avail()) {
200 l_error("Mesh Crypto functions unavailable");
201 status = l_main_run_with_signal(signal_handler, NULL);
208 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
209 opt = getopt_long(argc, argv, "i:c:f:ndbhr", main_options, NULL);
211 opt = getopt_long(argc, argv, "i:c:f:ndbh", main_options, NULL);
218 if (sscanf(optarg, "hci%d", &hci_index) == 1 ||
219 sscanf(optarg, "%d", &hci_index) == 1)
220 io = l_strdup_printf("generic:%s", optarg);
222 io = l_strdup(optarg);
234 mesh_conf_fname = optarg;
239 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
246 status = EXIT_SUCCESS;
250 status = EXIT_FAILURE;
256 io = l_strdup_printf("generic");
258 if (!parse_io(io, &io_type, &io_opts)) {
259 l_error("Invalid io: %s", io);
260 status = EXIT_FAILURE;
270 dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
272 l_error("unable to connect to D-Bus");
273 status = EXIT_FAILURE;
278 l_dbus_set_debug(dbus, do_debug, "[DBUS] ", NULL);
279 l_dbus_set_ready_handler(dbus, ready_callback, dbus, NULL);
280 l_dbus_set_disconnect_handler(dbus, disconnect_callback, NULL, NULL);
282 if (!l_dbus_object_manager_enable(dbus, "/")) {
283 l_error("Failed to enable Object Manager");
284 status = EXIT_FAILURE;
288 status = l_main_run_with_signal(signal_handler, NULL);
298 l_dbus_destroy(dbus);