main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <stdbool.h>
  15. #include <signal.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <sys/signalfd.h>
  24. #if defined(ANDROID)
  25. #include <sys/prctl.h>
  26. #include <sys/capability.h>
  27. #endif
  28. #include <glib.h>
  29. #include "lib/bluetooth.h"
  30. #include "lib/sdp.h"
  31. #include "src/log.h"
  32. #include "src/sdpd.h"
  33. #include "src/shared/util.h"
  34. #include "ipc-common.h"
  35. #include "ipc.h"
  36. #include "bluetooth.h"
  37. #include "socket.h"
  38. #include "hidhost.h"
  39. #include "hal-msg.h"
  40. #include "a2dp.h"
  41. #include "pan.h"
  42. #include "avrcp.h"
  43. #include "handsfree.h"
  44. #include "gatt.h"
  45. #include "health.h"
  46. #include "handsfree-client.h"
  47. #include "map-client.h"
  48. #include "utils.h"
  49. #define DEFAULT_VENDOR "BlueZ"
  50. #define DEFAULT_MODEL "BlueZ for Android"
  51. #define DEFAULT_NAME "BlueZ for Android"
  52. #define STARTUP_GRACE_SECONDS 5
  53. #define SHUTDOWN_GRACE_SECONDS 5
  54. static char *config_vendor = NULL;
  55. static char *config_model = NULL;
  56. static char *config_name = NULL;
  57. static char *config_serial = NULL;
  58. static char *config_fw_rev = NULL;
  59. static char *config_hw_rev = NULL;
  60. static uint64_t config_system_id = 0;
  61. static uint16_t config_pnp_source = 0x0002; /* USB */
  62. static uint16_t config_pnp_vendor = 0x1d6b; /* Linux Foundation */
  63. static uint16_t config_pnp_product = 0x0247; /* BlueZ for Android */
  64. static uint16_t config_pnp_version = 0x0000;
  65. static guint quit_timeout = 0;
  66. static bdaddr_t adapter_bdaddr;
  67. static GMainLoop *event_loop;
  68. static struct ipc *hal_ipc = NULL;
  69. static bool services[HAL_SERVICE_ID_MAX + 1] = { false };
  70. const char *bt_config_get_vendor(void)
  71. {
  72. if (config_vendor)
  73. return config_vendor;
  74. return DEFAULT_VENDOR;
  75. }
  76. const char *bt_config_get_name(void)
  77. {
  78. if (config_name)
  79. return config_name;
  80. return DEFAULT_NAME;
  81. }
  82. const char *bt_config_get_model(void)
  83. {
  84. if (config_model)
  85. return config_model;
  86. return DEFAULT_MODEL;
  87. }
  88. const char *bt_config_get_serial(void)
  89. {
  90. return config_serial;
  91. }
  92. const char *bt_config_get_fw_rev(void)
  93. {
  94. return config_fw_rev;
  95. }
  96. const char *bt_config_get_hw_rev(void)
  97. {
  98. return config_hw_rev;
  99. }
  100. uint64_t bt_config_get_system_id(void)
  101. {
  102. return config_system_id;
  103. }
  104. uint16_t bt_config_get_pnp_source(void)
  105. {
  106. return config_pnp_source;
  107. }
  108. uint16_t bt_config_get_pnp_vendor(void)
  109. {
  110. return config_pnp_vendor;
  111. }
  112. uint16_t bt_config_get_pnp_product(void)
  113. {
  114. return config_pnp_product;
  115. }
  116. uint16_t bt_config_get_pnp_version(void)
  117. {
  118. return config_pnp_version;
  119. }
  120. static void service_register(const void *buf, uint16_t len)
  121. {
  122. const struct hal_cmd_register_module *m = buf;
  123. uint8_t status;
  124. if (m->service_id > HAL_SERVICE_ID_MAX || services[m->service_id]) {
  125. status = HAL_STATUS_FAILED;
  126. goto failed;
  127. }
  128. switch (m->service_id) {
  129. case HAL_SERVICE_ID_BLUETOOTH:
  130. if (!bt_bluetooth_register(hal_ipc, m->mode)) {
  131. status = HAL_STATUS_FAILED;
  132. goto failed;
  133. }
  134. break;
  135. case HAL_SERVICE_ID_SOCKET:
  136. bt_socket_register(hal_ipc, &adapter_bdaddr, m->mode);
  137. break;
  138. case HAL_SERVICE_ID_HIDHOST:
  139. if (!bt_hid_register(hal_ipc, &adapter_bdaddr, m->mode)) {
  140. status = HAL_STATUS_FAILED;
  141. goto failed;
  142. }
  143. break;
  144. case HAL_SERVICE_ID_A2DP:
  145. if (!bt_a2dp_register(hal_ipc, &adapter_bdaddr, m->mode)) {
  146. status = HAL_STATUS_FAILED;
  147. goto failed;
  148. }
  149. break;
  150. case HAL_SERVICE_ID_PAN:
  151. if (!bt_pan_register(hal_ipc, &adapter_bdaddr, m->mode)) {
  152. status = HAL_STATUS_FAILED;
  153. goto failed;
  154. }
  155. break;
  156. case HAL_SERVICE_ID_AVRCP:
  157. if (!bt_avrcp_register(hal_ipc, &adapter_bdaddr, m->mode)) {
  158. status = HAL_STATUS_FAILED;
  159. goto failed;
  160. }
  161. break;
  162. case HAL_SERVICE_ID_HANDSFREE:
  163. if (!bt_handsfree_register(hal_ipc, &adapter_bdaddr, m->mode,
  164. m->max_clients)) {
  165. status = HAL_STATUS_FAILED;
  166. goto failed;
  167. }
  168. break;
  169. case HAL_SERVICE_ID_GATT:
  170. if (!bt_gatt_register(hal_ipc, &adapter_bdaddr)) {
  171. status = HAL_STATUS_FAILED;
  172. goto failed;
  173. }
  174. break;
  175. case HAL_SERVICE_ID_HEALTH:
  176. if (!bt_health_register(hal_ipc, &adapter_bdaddr, m->mode)) {
  177. status = HAL_STATUS_FAILED;
  178. goto failed;
  179. }
  180. break;
  181. case HAL_SERVICE_ID_HANDSFREE_CLIENT:
  182. if (!bt_hf_client_register(hal_ipc, &adapter_bdaddr)) {
  183. status = HAL_STATUS_FAILED;
  184. goto failed;
  185. }
  186. break;
  187. case HAL_SERVICE_ID_MAP_CLIENT:
  188. if (!bt_map_client_register(hal_ipc, &adapter_bdaddr,
  189. m->mode)) {
  190. status = HAL_STATUS_FAILED;
  191. goto failed;
  192. }
  193. break;
  194. default:
  195. DBG("service %u not supported", m->service_id);
  196. status = HAL_STATUS_FAILED;
  197. goto failed;
  198. }
  199. services[m->service_id] = true;
  200. status = HAL_STATUS_SUCCESS;
  201. info("Service ID=%u registered", m->service_id);
  202. failed:
  203. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
  204. status);
  205. }
  206. static bool unregister_service(uint8_t id)
  207. {
  208. if (id > HAL_SERVICE_ID_MAX || !services[id])
  209. return false;
  210. switch (id) {
  211. case HAL_SERVICE_ID_BLUETOOTH:
  212. bt_bluetooth_unregister();
  213. break;
  214. case HAL_SERVICE_ID_SOCKET:
  215. bt_socket_unregister();
  216. break;
  217. case HAL_SERVICE_ID_HIDHOST:
  218. bt_hid_unregister();
  219. break;
  220. case HAL_SERVICE_ID_A2DP:
  221. bt_a2dp_unregister();
  222. break;
  223. case HAL_SERVICE_ID_PAN:
  224. bt_pan_unregister();
  225. break;
  226. case HAL_SERVICE_ID_AVRCP:
  227. bt_avrcp_unregister();
  228. break;
  229. case HAL_SERVICE_ID_HANDSFREE:
  230. bt_handsfree_unregister();
  231. break;
  232. case HAL_SERVICE_ID_GATT:
  233. bt_gatt_unregister();
  234. break;
  235. case HAL_SERVICE_ID_HEALTH:
  236. bt_health_unregister();
  237. break;
  238. case HAL_SERVICE_ID_HANDSFREE_CLIENT:
  239. bt_hf_client_unregister();
  240. break;
  241. case HAL_SERVICE_ID_MAP_CLIENT:
  242. bt_map_client_unregister();
  243. break;
  244. default:
  245. DBG("service %u not supported", id);
  246. return false;
  247. }
  248. services[id] = false;
  249. return true;
  250. }
  251. static void service_unregister(const void *buf, uint16_t len)
  252. {
  253. const struct hal_cmd_unregister_module *m = buf;
  254. uint8_t status;
  255. if (!unregister_service(m->service_id)) {
  256. status = HAL_STATUS_FAILED;
  257. goto failed;
  258. }
  259. status = HAL_STATUS_SUCCESS;
  260. info("Service ID=%u unregistered", m->service_id);
  261. failed:
  262. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
  263. status);
  264. }
  265. static char *get_prop(char *prop, uint16_t len, const uint8_t *val)
  266. {
  267. /* TODO should fail if set more than once ? */
  268. free(prop);
  269. prop = malloc0(len);
  270. if (!prop)
  271. return NULL;
  272. memcpy(prop, val, len);
  273. prop[len - 1] = '\0';
  274. return prop;
  275. }
  276. static void parse_pnp_id(uint16_t len, const uint8_t *val)
  277. {
  278. int result;
  279. uint16_t vendor, product, version , source;
  280. char *pnp;
  281. /* version is optional */
  282. version = config_pnp_version;
  283. pnp = get_prop(NULL, len, val);
  284. if (!pnp)
  285. return;
  286. DBG("pnp_id %s", pnp);
  287. result = sscanf(pnp, "bluetooth:%4hx:%4hx:%4hx",
  288. &vendor, &product, &version);
  289. if (result != EOF && result >= 2) {
  290. source = 0x0001;
  291. goto done;
  292. }
  293. result = sscanf(pnp, "usb:%4hx:%4hx:%4hx", &vendor, &product, &version);
  294. if (result != EOF && result >= 2) {
  295. source = 0x0002;
  296. goto done;
  297. }
  298. free(pnp);
  299. return;
  300. done:
  301. free(pnp);
  302. config_pnp_source = source;
  303. config_pnp_vendor = vendor;
  304. config_pnp_product = product;
  305. config_pnp_version = version;
  306. }
  307. static void parse_system_id(uint16_t len, const uint8_t *val)
  308. {
  309. uint64_t res;
  310. char *id;
  311. id = get_prop(NULL, len, val);
  312. if (!id)
  313. return;
  314. res = strtoull(id, NULL, 16);
  315. if (res == ULLONG_MAX && errno == ERANGE)
  316. goto done;
  317. config_system_id = res;
  318. done:
  319. free(id);
  320. }
  321. static void configuration(const void *buf, uint16_t len)
  322. {
  323. const struct hal_cmd_configuration *cmd = buf;
  324. const struct hal_config_prop *prop;
  325. unsigned int i;
  326. buf += sizeof(*cmd);
  327. len -= sizeof(*cmd);
  328. for (i = 0; i < cmd->num; i++) {
  329. prop = buf;
  330. if (len < sizeof(*prop) || len < sizeof(*prop) + prop->len) {
  331. error("Invalid configuration command, terminating");
  332. raise(SIGTERM);
  333. return;
  334. }
  335. switch (prop->type) {
  336. case HAL_CONFIG_VENDOR:
  337. config_vendor = get_prop(config_vendor, prop->len,
  338. prop->val);
  339. DBG("vendor %s", config_vendor);
  340. break;
  341. case HAL_CONFIG_NAME:
  342. config_name = get_prop(config_name, prop->len,
  343. prop->val);
  344. DBG("name %s", config_name);
  345. break;
  346. case HAL_CONFIG_MODEL:
  347. config_model = get_prop(config_model, prop->len,
  348. prop->val);
  349. DBG("model %s", config_model);
  350. break;
  351. case HAL_CONFIG_SERIAL_NUMBER:
  352. config_serial = get_prop(config_serial, prop->len,
  353. prop->val);
  354. DBG("serial %s", config_serial);
  355. break;
  356. case HAL_CONFIG_SYSTEM_ID:
  357. parse_system_id(prop->len, prop->val);
  358. break;
  359. case HAL_CONFIG_PNP_ID:
  360. parse_pnp_id(prop->len, prop->val);
  361. break;
  362. case HAL_CONFIG_FW_REV:
  363. config_fw_rev = get_prop(config_fw_rev, prop->len,
  364. prop->val);
  365. DBG("fw_rev %s", config_fw_rev);
  366. break;
  367. case HAL_CONFIG_HW_REV:
  368. config_hw_rev = get_prop(config_hw_rev, prop->len,
  369. prop->val);
  370. DBG("hw_rev %s", config_hw_rev);
  371. break;
  372. default:
  373. error("Invalid configuration option (%u), terminating",
  374. prop->type);
  375. raise(SIGTERM);
  376. return;
  377. }
  378. buf += sizeof(*prop) + prop->len;
  379. len -= sizeof(*prop) + prop->len;
  380. }
  381. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_CORE, HAL_OP_CONFIGURATION,
  382. HAL_STATUS_SUCCESS);
  383. }
  384. static const struct ipc_handler cmd_handlers[] = {
  385. /* HAL_OP_REGISTER_MODULE */
  386. { service_register, false, sizeof(struct hal_cmd_register_module) },
  387. /* HAL_OP_UNREGISTER_MODULE */
  388. { service_unregister, false, sizeof(struct hal_cmd_unregister_module) },
  389. /* HAL_OP_CONFIGURATION */
  390. { configuration, true, sizeof(struct hal_cmd_configuration) },
  391. };
  392. static void bluetooth_stopped(void)
  393. {
  394. g_main_loop_quit(event_loop);
  395. }
  396. static gboolean quit_eventloop(gpointer user_data)
  397. {
  398. g_main_loop_quit(event_loop);
  399. quit_timeout = 0;
  400. return FALSE;
  401. }
  402. static void stop_bluetooth(void)
  403. {
  404. static bool __stop = false;
  405. if (__stop)
  406. return;
  407. __stop = true;
  408. if (!bt_bluetooth_stop(bluetooth_stopped)) {
  409. g_main_loop_quit(event_loop);
  410. return;
  411. }
  412. quit_timeout = g_timeout_add_seconds(SHUTDOWN_GRACE_SECONDS,
  413. quit_eventloop, NULL);
  414. }
  415. static void ipc_disconnected(void *data)
  416. {
  417. stop_bluetooth();
  418. }
  419. static void adapter_ready(int err, const bdaddr_t *addr)
  420. {
  421. if (err < 0) {
  422. error("Adapter initialization failed: %s", strerror(-err));
  423. exit(EXIT_FAILURE);
  424. }
  425. bacpy(&adapter_bdaddr, addr);
  426. if (quit_timeout > 0) {
  427. g_source_remove(quit_timeout);
  428. quit_timeout = 0;
  429. }
  430. info("Adapter initialized");
  431. hal_ipc = ipc_init(BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH),
  432. HAL_SERVICE_ID_MAX, true,
  433. ipc_disconnected, NULL);
  434. if (!hal_ipc) {
  435. error("Failed to initialize IPC");
  436. exit(EXIT_FAILURE);
  437. }
  438. ipc_register(hal_ipc, HAL_SERVICE_ID_CORE, cmd_handlers,
  439. G_N_ELEMENTS(cmd_handlers));
  440. }
  441. static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
  442. gpointer user_data)
  443. {
  444. static bool __terminated = false;
  445. struct signalfd_siginfo si;
  446. ssize_t result;
  447. int fd;
  448. if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
  449. return FALSE;
  450. fd = g_io_channel_unix_get_fd(channel);
  451. result = read(fd, &si, sizeof(si));
  452. if (result != sizeof(si))
  453. return FALSE;
  454. switch (si.ssi_signo) {
  455. case SIGINT:
  456. case SIGTERM:
  457. if (!__terminated) {
  458. info("Terminating");
  459. stop_bluetooth();
  460. }
  461. __terminated = true;
  462. break;
  463. }
  464. return TRUE;
  465. }
  466. static guint setup_signalfd(void)
  467. {
  468. GIOChannel *channel;
  469. guint source;
  470. sigset_t mask;
  471. int fd;
  472. sigemptyset(&mask);
  473. sigaddset(&mask, SIGINT);
  474. sigaddset(&mask, SIGTERM);
  475. if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
  476. perror("Failed to set signal mask");
  477. return 0;
  478. }
  479. fd = signalfd(-1, &mask, 0);
  480. if (fd < 0) {
  481. perror("Failed to create signal descriptor");
  482. return 0;
  483. }
  484. channel = g_io_channel_unix_new(fd);
  485. g_io_channel_set_close_on_unref(channel, TRUE);
  486. g_io_channel_set_encoding(channel, NULL, NULL);
  487. g_io_channel_set_buffered(channel, FALSE);
  488. source = g_io_add_watch(channel,
  489. G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  490. signal_handler, NULL);
  491. g_io_channel_unref(channel);
  492. return source;
  493. }
  494. static gboolean option_version = FALSE;
  495. static gint option_index = -1;
  496. static gboolean option_dbg = FALSE;
  497. static gboolean option_mgmt_dbg = FALSE;
  498. static GOptionEntry options[] = {
  499. { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
  500. "Show version information and exit", NULL },
  501. { "index", 'i', 0, G_OPTION_ARG_INT, &option_index,
  502. "Use specified controller", "INDEX"},
  503. { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_dbg,
  504. "Enable debug logs", NULL},
  505. { "mgmt-debug", 0, 0, G_OPTION_ARG_NONE, &option_mgmt_dbg,
  506. "Enable mgmt debug logs", NULL},
  507. { NULL }
  508. };
  509. static void cleanup_services(void)
  510. {
  511. int i;
  512. DBG("");
  513. for (i = HAL_SERVICE_ID_MAX; i > HAL_SERVICE_ID_CORE; i--)
  514. unregister_service(i);
  515. }
  516. static bool set_capabilities(void)
  517. {
  518. #if defined(ANDROID)
  519. struct __user_cap_header_struct header;
  520. struct __user_cap_data_struct cap;
  521. header.version = _LINUX_CAPABILITY_VERSION;
  522. header.pid = 0;
  523. /*
  524. * CAP_NET_ADMIN: Allow use of MGMT interface
  525. * CAP_NET_BIND_SERVICE: Allow use of privileged PSM
  526. * CAP_NET_RAW: Allow use of bnep ioctl calls
  527. */
  528. cap.effective = cap.permitted =
  529. CAP_TO_MASK(CAP_NET_RAW) |
  530. CAP_TO_MASK(CAP_NET_ADMIN) |
  531. CAP_TO_MASK(CAP_NET_BIND_SERVICE);
  532. cap.inheritable = 0;
  533. /* don't clear capabilities when dropping root */
  534. if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
  535. error("%s: prctl(): %s", __func__, strerror(errno));
  536. return false;
  537. }
  538. /* Android bluetooth user UID=1002 */
  539. if (setuid(1002) < 0) {
  540. error("%s: setuid(): %s", __func__, strerror(errno));
  541. return false;
  542. }
  543. /* TODO: Move to cap_set_proc once bionic support it */
  544. if (capset(&header, &cap) < 0) {
  545. error("%s: capset(): %s", __func__, strerror(errno));
  546. return false;
  547. }
  548. /* TODO: Move to cap_get_proc once bionic support it */
  549. if (capget(&header, &cap) < 0) {
  550. error("%s: capget(): %s", __func__, strerror(errno));
  551. return false;
  552. }
  553. DBG("Caps: eff: 0x%x, perm: 0x%x, inh: 0x%x", cap.effective,
  554. cap.permitted, cap.inheritable);
  555. #endif
  556. return true;
  557. }
  558. static void set_version(void)
  559. {
  560. uint8_t major, minor;
  561. if (sscanf(VERSION, "%hhu.%hhu", &major, &minor) != 2)
  562. return;
  563. config_pnp_version = major << 8 | minor;
  564. }
  565. int main(int argc, char *argv[])
  566. {
  567. GOptionContext *context;
  568. GError *err = NULL;
  569. guint signal;
  570. set_version();
  571. context = g_option_context_new(NULL);
  572. g_option_context_add_main_entries(context, options, NULL);
  573. if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
  574. if (err != NULL) {
  575. g_printerr("%s\n", err->message);
  576. g_error_free(err);
  577. } else
  578. g_printerr("An unknown error occurred\n");
  579. exit(EXIT_FAILURE);
  580. }
  581. g_option_context_free(context);
  582. if (option_version == TRUE) {
  583. printf("%s\n", VERSION);
  584. exit(EXIT_SUCCESS);
  585. }
  586. signal = setup_signalfd();
  587. if (!signal)
  588. return EXIT_FAILURE;
  589. if (option_dbg || option_mgmt_dbg)
  590. __btd_log_init("*", 0);
  591. else
  592. __btd_log_init(NULL, 0);
  593. if (!set_capabilities()) {
  594. __btd_log_cleanup();
  595. g_source_remove(signal);
  596. return EXIT_FAILURE;
  597. }
  598. quit_timeout = g_timeout_add_seconds(STARTUP_GRACE_SECONDS,
  599. quit_eventloop, NULL);
  600. if (quit_timeout == 0) {
  601. error("Failed to init startup timeout");
  602. __btd_log_cleanup();
  603. g_source_remove(signal);
  604. return EXIT_FAILURE;
  605. }
  606. if (!bt_bluetooth_start(option_index, option_mgmt_dbg, adapter_ready)) {
  607. __btd_log_cleanup();
  608. g_source_remove(quit_timeout);
  609. g_source_remove(signal);
  610. return EXIT_FAILURE;
  611. }
  612. /* Use params: mtu = 0, flags = 0 */
  613. start_sdp_server(0, 0);
  614. DBG("Entering main loop");
  615. event_loop = g_main_loop_new(NULL, FALSE);
  616. g_main_loop_run(event_loop);
  617. g_source_remove(signal);
  618. if (quit_timeout > 0)
  619. g_source_remove(quit_timeout);
  620. cleanup_services();
  621. stop_sdp_server();
  622. bt_bluetooth_cleanup();
  623. g_main_loop_unref(event_loop);
  624. /* If no adapter was initialized, hal_ipc is NULL */
  625. if (hal_ipc) {
  626. ipc_unregister(hal_ipc, HAL_SERVICE_ID_CORE);
  627. ipc_cleanup(hal_ipc);
  628. }
  629. info("Exit");
  630. __btd_log_cleanup();
  631. free(config_vendor);
  632. free(config_model);
  633. free(config_name);
  634. free(config_serial);
  635. free(config_fw_rev);
  636. free(config_hw_rev);
  637. return EXIT_SUCCESS;
  638. }