Skip to content

Commit 6aa7dc2

Browse files
committed
gn-build: Add bluetooth-service target
This patch makes the Bluetooth system service (service/) buildable using GN: 1. Added new BUILD.gn file for service/ 2. Added conditional compilation for global config paths, with TODOs for generalizing them later. 3. Added a shim for loading the Bluetooth library that calls hw_get_module on Android and explicitly calls dlopen on OS_GENERIC. 4. Fixed compile warnings and errors. 5. Did some minor clean up in gatt_server.cpp for better readability. Bug: 22124644 Change-Id: I3226537a3a5211a6762651a35707638df29956b0
1 parent 445e3be commit 6aa7dc2

File tree

22 files changed

+398
-107
lines changed

22 files changed

+398
-107
lines changed

BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#
2+
#
23
# Copyright (C) 2015 Google, Inc.
34
#
45
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,5 +25,7 @@
2425
group("bluetooth") {
2526
deps = [
2627
"//main:bluetooth.default",
28+
"//service:bluetoothtbd",
29+
"//vendor_libs:vendor-libs"
2730
]
2831
}

system/btcore/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ LOCAL_SRC_FILES := \
3232
src/bdaddr.c \
3333
src/counter.c \
3434
src/device_class.c \
35+
src/hal_util.c \
3536
src/module.c \
3637
src/osi_module.c \
3738
src/property.c \

system/btcore/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static_library("btcore") {
1919
"src/bdaddr.c",
2020
"src/counter.c",
2121
"src/device_class.c",
22+
"src/hal_util.c",
2223
"src/module.c",
2324
"src/property.c",
2425
"src/uuid.c",

system/btcore/include/hal_util.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Copyright (C) 2015 Google, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at:
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#pragma once
18+
19+
struct hw_module_t;
20+
21+
// Loads the Bluetooth library. If OS_GENERIC is defined, this function looks
22+
// explicitly for libbluetooth.default.so and loads it. On Android, this calls
23+
// the hw_get_module routine with the Bluetooth stack module id.
24+
int hal_util_load_bt_library(const struct hw_module_t **module);

system/btcore/src/hal_util.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//
2+
// Copyright (C) 2015 Google, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at:
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#define LOG_TAG "hal_util"
18+
19+
#include <hardware/bluetooth.h>
20+
#include <hardware/hardware.h>
21+
22+
#include <dlfcn.h>
23+
#include <errno.h>
24+
#include <string.h>
25+
26+
#include "btcore/include/hal_util.h"
27+
#include "osi/include/log.h"
28+
29+
#if defined(OS_GENERIC)
30+
31+
// TODO(armansito): All logging macros should include __func__ by default (see
32+
// Bug: 22671731)
33+
#define HULOGERR(fmt, args...) \
34+
LOG_ERROR(LOG_TAG, "[%s] failed to load the Bluetooth library: " fmt, \
35+
__func__, ## args)
36+
37+
// TODO(armansito): It might be better to pass the library name in a more
38+
// generic manner as opposed to hard-coding it here.
39+
static const char kBluetoothLibraryName[] = "libbluetooth.default.so";
40+
41+
static int load_bt_library(const struct hw_module_t **module) {
42+
const char *id = BT_STACK_MODULE_ID;
43+
44+
// Always try to load the default Bluetooth stack on GN builds.
45+
void *handle = dlopen(kBluetoothLibraryName, RTLD_NOW);
46+
if (!handle) {
47+
char const *err_str = dlerror();
48+
HULOGERR("%s", err_str ? err_str : "error unknown");
49+
goto error;
50+
}
51+
52+
// Get the address of the struct hal_module_info.
53+
const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
54+
struct hw_module_t *hmi = (struct hw_module_t *)dlsym(handle, sym);
55+
if (!hmi) {
56+
HULOGERR("%s", sym);
57+
goto error;
58+
}
59+
60+
// Check that the id matches.
61+
if (strcmp(id, hmi->id) != 0) {
62+
HULOGERR("id=%s does not match HAL module ID: %s", id, hmi->id);
63+
goto error;
64+
}
65+
66+
hmi->dso = handle;
67+
68+
// Success.
69+
LOG_INFO(
70+
LOG_TAG, "[%s] loaded HAL id=%s path=%s hmi=%p handle=%p",
71+
__func__, id, kBluetoothLibraryName, hmi, handle);
72+
73+
*module = hmi;
74+
return 0;
75+
76+
error:
77+
*module = NULL;
78+
if (handle)
79+
dlclose(handle);
80+
81+
return -EINVAL;
82+
}
83+
84+
#endif // defined(OS_GENERIC)
85+
86+
int hal_util_load_bt_library(const struct hw_module_t **module) {
87+
#if defined(OS_GENERIC)
88+
return load_bt_library(module);
89+
#else // !defined(OS_GENERIC)
90+
return hw_get_module(BT_STACK_MODULE_ID, module);
91+
#endif // defined(OS_GENERIC)
92+
}

system/btif/src/btif_config.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838
#include "osi/include/log.h"
3939
#include "osi/include/osi.h"
4040

41+
// TODO(armansito): Find a better way than searching by a hardcoded path.
42+
#if defined(OS_GENERIC)
43+
static const char *CONFIG_FILE_PATH = "bt_config.conf";
44+
#else // !defined(OS_GENERIC)
4145
static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf";
46+
#endif // defined(OS_GENERIC)
4247
static const char *LEGACY_CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.xml";
4348
static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000;
4449

system/btif/src/btif_core.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@
7575
************************************************************************************/
7676

7777
#ifndef BTE_DID_CONF_FILE
78+
// TODO(armansito): Find a better way than searching by a hardcoded path.
79+
#if defined(OS_GENERIC)
80+
#define BTE_DID_CONF_FILE "bt_did.conf"
81+
#else // !defined(OS_GENERIC)
7882
#define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
79-
#endif
83+
#endif // defined(OS_GENERIC)
84+
#endif // BTE_DID_CONF_FILE
8085

8186
/************************************************************************************
8287
** Local type definitions

system/btif/src/btif_storage.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
** Constants & Macros
5858
************************************************************************************/
5959

60+
// TODO(armansito): Find a better way than using a hardcoded path.
6061
#define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
6162

6263
//#define BTIF_STORAGE_PATH_ADAPTER_INFO "adapter_info"
@@ -78,7 +79,12 @@
7879
#define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "DiscoveryTimeout"
7980

8081

81-
#define BTIF_AUTO_PAIR_CONF_FILE "/etc/bluetooth/auto_pair_devlist.conf"
82+
#if defined(OS_GENERIC)
83+
// TODO(armansito): Find a better way than searching by a hardcoded path.
84+
#define BTIF_AUTO_PAIR_CONF_FILE "auto_pair_devlist.conf"
85+
#else // !defined(OS_GENERIC)
86+
#define BTIF_AUTO_PAIR_CONF_FILE "/etc/bluetooth/auto_pair_devlist.conf"
87+
#endif // defined(OS_GENERIC)
8288
#define BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST "AutoPairBlacklist"
8389
#define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR "AddressBlacklist"
8490
#define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME "ExactNameBlacklist"

system/build/secondary/third_party/libchrome/BUILD.gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,5 @@ static_library("base") {
224224
"-Wno-sign-promo",
225225
]
226226

227-
libs = [ "-levent", "-levent_core" ]
227+
libs = [ "-levent", "-levent_core", "-lpthread" ]
228228
}

system/main/BUILD.gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ shared_library("bluetooth.default") {
7474
"//utils",
7575
]
7676

77-
libs = [ "-lpthread", "-lrt", "-ldl" ]
77+
libs = [ "-ldl", "-lpthread", "-lresolv", "-lrt", "-lz" ]
7878
}

0 commit comments

Comments
 (0)