1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hdi_wrapper.h"
17
18 #include <dlfcn.h>
19
20 #include "log.h"
21 #include "platform/include/allocator.h"
22
23 #ifdef __x86_64__
24 #define HDI_LIB "libbluetooth_hdi_adapter.so"
25 #else
26 #define HDI_LIB "libbluetooth_hdi_adapter.z.so"
27 #endif
28
LoadHdiLib()29 HDILib *LoadHdiLib()
30 {
31 HDILib *lib = MEM_CALLOC.alloc(sizeof(HDILib));
32 if (lib != NULL) {
33 do {
34 lib->lib = dlopen(HDI_LIB, RTLD_LAZY | RTLD_NODELETE);
35 if (lib->lib == NULL) {
36 LOG_ERROR("Load libbluetooth_hdi_adapter.so failed, %{public}s", dlerror());
37 break;
38 }
39
40 lib->hdiInit = dlsym(lib->lib, "HdiInit");
41 if (lib->hdiInit == NULL) {
42 LOG_ERROR("Load symbol HdiInit failed");
43 }
44
45 lib->hdiSendHciPacket = dlsym(lib->lib, "HdiSendHciPacket");
46 if (lib->hdiSendHciPacket == NULL) {
47 LOG_ERROR("Load symbol HdiSendHciPacket failed");
48 }
49
50 lib->hdiClose = dlsym(lib->lib, "HdiClose");
51 if (lib->hdiClose == NULL) {
52 LOG_ERROR("Load symbol HdiClose failed");
53 }
54 } while (0);
55
56 if ((lib->lib == NULL) ||
57 (lib->hdiInit == NULL) ||
58 (lib->hdiSendHciPacket == NULL) ||
59 (lib->hdiClose == NULL)) {
60 MEM_CALLOC.free(lib);
61 lib = NULL;
62 }
63 }
64 return lib;
65 }
66
UnloadHdiLib(HDILib * lib)67 void UnloadHdiLib(HDILib *lib)
68 {
69 if (lib != NULL) {
70 lib->hdiInit = NULL;
71 lib->hdiSendHciPacket = NULL;
72 lib->hdiClose = NULL;
73 if (lib->lib != NULL) {
74 dlclose(lib->lib);
75 lib->lib = NULL;
76 }
77 MEM_CALLOC.free(lib);
78 }
79 }