1 /*
2 * Copyright (C) 2021 The Android Open Source Project
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 #include "chre/platform/platform_ble.h"
18
19 #include <cinttypes>
20
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/platform/log.h"
23 #include "chre/platform/shared/pal_system_api.h"
24 #include "chre_api/chre/ble.h"
25
26 namespace chre {
27
28 const chrePalBleCallbacks PlatformBleBase::sBleCallbacks = {
29 PlatformBleBase::requestStateResync,
30 PlatformBleBase::scanStatusChangeCallback,
31 PlatformBleBase::advertisingEventCallback,
32 };
33
~PlatformBle()34 PlatformBle::~PlatformBle() {
35 if (mBleApi != nullptr) {
36 LOGD("Platform BLE closing");
37 prePalApiCall(PalType::BLE);
38 mBleApi->close();
39 LOGD("Platform BLE closed");
40 }
41 }
42
init()43 void PlatformBle::init() {
44 prePalApiCall(PalType::BLE);
45 mBleApi = chrePalBleGetApi(CHRE_PAL_BLE_API_CURRENT_VERSION);
46 if (mBleApi != nullptr) {
47 if (!mBleApi->open(&gChrePalSystemApi, &sBleCallbacks)) {
48 LOGE("BLE PAL open returned false");
49 mBleApi = nullptr;
50 } else {
51 LOGD("Opened BLE PAL version 0x%08" PRIx32, mBleApi->moduleVersion);
52 }
53 } else {
54 LOGW("Requested BLE PAL (version 0x%08" PRIx32 ") not found",
55 CHRE_PAL_BLE_API_CURRENT_VERSION);
56 }
57 }
58
getCapabilities()59 uint32_t PlatformBle::getCapabilities() {
60 if (mBleApi != nullptr) {
61 prePalApiCall(PalType::BLE);
62 return mBleApi->getCapabilities();
63 } else {
64 return CHRE_BLE_CAPABILITIES_NONE;
65 }
66 }
67
getFilterCapabilities()68 uint32_t PlatformBle::getFilterCapabilities() {
69 if (mBleApi != nullptr) {
70 prePalApiCall(PalType::BLE);
71 return mBleApi->getFilterCapabilities();
72 } else {
73 return CHRE_BLE_FILTER_CAPABILITIES_NONE;
74 }
75 }
76
startScanAsync(chreBleScanMode mode,uint32_t reportDelayMs,const struct chreBleScanFilter * filter)77 bool PlatformBle::startScanAsync(chreBleScanMode mode, uint32_t reportDelayMs,
78 const struct chreBleScanFilter *filter) {
79 if (mBleApi != nullptr) {
80 prePalApiCall(PalType::BLE);
81 return mBleApi->startScan(mode, reportDelayMs, filter);
82 } else {
83 return false;
84 }
85 }
86
stopScanAsync()87 bool PlatformBle::stopScanAsync() {
88 if (mBleApi != nullptr) {
89 prePalApiCall(PalType::BLE);
90 return mBleApi->stopScan();
91 } else {
92 return false;
93 }
94 }
95
releaseAdvertisingEvent(struct chreBleAdvertisementEvent * event)96 void PlatformBle::releaseAdvertisingEvent(
97 struct chreBleAdvertisementEvent *event) {
98 prePalApiCall(PalType::BLE);
99 mBleApi->releaseAdvertisingEvent(event);
100 }
101
requestStateResync()102 void PlatformBleBase::requestStateResync() {
103 EventLoopManagerSingleton::get()
104 ->getBleRequestManager()
105 .handleRequestStateResyncCallback();
106 }
107
scanStatusChangeCallback(bool enabled,uint8_t errorCode)108 void PlatformBleBase::scanStatusChangeCallback(bool enabled,
109 uint8_t errorCode) {
110 EventLoopManagerSingleton::get()->getBleRequestManager().handlePlatformChange(
111 enabled, errorCode);
112 }
113
advertisingEventCallback(struct chreBleAdvertisementEvent * event)114 void PlatformBleBase::advertisingEventCallback(
115 struct chreBleAdvertisementEvent *event) {
116 EventLoopManagerSingleton::get()
117 ->getBleRequestManager()
118 .handleAdvertisementEvent(event);
119 }
120
121 } // namespace chre
122