xref: /aosp_15_r20/system/chre/platform/shared/platform_ble.cc (revision 84e339476a462649f82315436d70fd732297a399)
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 #ifdef CHRE_BLE_SUPPORT_ENABLED
18 
19 #include "chre/platform/platform_ble.h"
20 
21 #include <cinttypes>
22 
23 #include "chre/core/event_loop_manager.h"
24 #include "chre/platform/log.h"
25 #include "chre/platform/shared/bt_snoop_log.h"
26 #include "chre/platform/shared/pal_system_api.h"
27 #include "chre_api/chre/ble.h"
28 
29 namespace chre {
30 
31 const chrePalBleCallbacks PlatformBleBase::sBleCallbacks = {
32     PlatformBleBase::requestStateResync,
33     PlatformBleBase::scanStatusChangeCallback,
34     PlatformBleBase::advertisingEventCallback,
35     PlatformBleBase::readRssiCallback,
36     PlatformBleBase::flushCallback,
37     PlatformBleBase::handleBtSnoopLog,
38 };
39 
~PlatformBle()40 PlatformBle::~PlatformBle() {
41   if (mBleApi != nullptr) {
42     LOGD("Platform BLE closing");
43     prePalApiCall(PalType::BLE);
44     mBleApi->close();
45     LOGD("Platform BLE closed");
46   }
47 }
48 
init()49 void PlatformBle::init() {
50   prePalApiCall(PalType::BLE);
51   mBleApi = chrePalBleGetApi(CHRE_PAL_BLE_API_CURRENT_VERSION);
52   if (mBleApi != nullptr) {
53     if (!mBleApi->open(&gChrePalSystemApi, &sBleCallbacks)) {
54       LOGE("BLE PAL open returned false");
55 
56 #ifdef CHRE_TELEMETRY_SUPPORT_ENABLED
57       EventLoopManagerSingleton::get()->getTelemetryManager().onPalOpenFailure(
58           TelemetryManager::PalType::BLE);
59 #endif  // CHRE_TELEMETRY_SUPPORT_ENABLED
60 
61       mBleApi = nullptr;
62     } else {
63       LOGD("Opened BLE PAL version 0x%08" PRIx32, mBleApi->moduleVersion);
64     }
65   } else {
66     LOGW("Requested BLE PAL (version 0x%08" PRIx32 ") not found",
67          CHRE_PAL_BLE_API_CURRENT_VERSION);
68   }
69 }
70 
getCapabilities()71 uint32_t PlatformBle::getCapabilities() {
72   if (mBleApi != nullptr) {
73     prePalApiCall(PalType::BLE);
74     return mBleApi->getCapabilities();
75   } else {
76     return CHRE_BLE_CAPABILITIES_NONE;
77   }
78 }
79 
getFilterCapabilities()80 uint32_t PlatformBle::getFilterCapabilities() {
81   if (mBleApi != nullptr) {
82     prePalApiCall(PalType::BLE);
83     return mBleApi->getFilterCapabilities();
84   } else {
85     return CHRE_BLE_FILTER_CAPABILITIES_NONE;
86   }
87 }
88 
startScanAsync(chreBleScanMode mode,uint32_t reportDelayMs,const struct chreBleScanFilterV1_9 * filter)89 bool PlatformBle::startScanAsync(chreBleScanMode mode, uint32_t reportDelayMs,
90                                  const struct chreBleScanFilterV1_9 *filter) {
91   if (mBleApi != nullptr) {
92     prePalApiCall(PalType::BLE);
93     return mBleApi->startScan(mode, reportDelayMs, filter);
94   } else {
95     return false;
96   }
97 }
98 
stopScanAsync()99 bool PlatformBle::stopScanAsync() {
100   if (mBleApi != nullptr) {
101     prePalApiCall(PalType::BLE);
102     return mBleApi->stopScan();
103   } else {
104     return false;
105   }
106 }
107 
releaseAdvertisingEvent(struct chreBleAdvertisementEvent * event)108 void PlatformBle::releaseAdvertisingEvent(
109     struct chreBleAdvertisementEvent *event) {
110   prePalApiCall(PalType::BLE);
111   mBleApi->releaseAdvertisingEvent(event);
112 }
113 
requestStateResync()114 void PlatformBleBase::requestStateResync() {
115   EventLoopManagerSingleton::get()
116       ->getBleRequestManager()
117       .handleRequestStateResyncCallback();
118 }
119 
scanStatusChangeCallback(bool enabled,uint8_t errorCode)120 void PlatformBleBase::scanStatusChangeCallback(bool enabled,
121                                                uint8_t errorCode) {
122   EventLoopManagerSingleton::get()->getBleRequestManager().handlePlatformChange(
123       enabled, errorCode);
124 }
125 
advertisingEventCallback(struct chreBleAdvertisementEvent * event)126 void PlatformBleBase::advertisingEventCallback(
127     struct chreBleAdvertisementEvent *event) {
128   EventLoopManagerSingleton::get()
129       ->getBleRequestManager()
130       .handleAdvertisementEvent(event);
131 }
132 
readRssiAsync(uint16_t connectionHandle)133 bool PlatformBle::readRssiAsync(uint16_t connectionHandle) {
134   if (mBleApi != nullptr) {
135     prePalApiCall(PalType::BLE);
136     return mBleApi->readRssi(connectionHandle);
137   } else {
138     return false;
139   }
140 }
141 
readRssiCallback(uint8_t errorCode,uint16_t connectionHandle,int8_t rssi)142 void PlatformBleBase::readRssiCallback(uint8_t errorCode,
143                                        uint16_t connectionHandle, int8_t rssi) {
144   EventLoopManagerSingleton::get()->getBleRequestManager().handleReadRssi(
145       errorCode, connectionHandle, rssi);
146 }
147 
flushAsync()148 bool PlatformBle::flushAsync() {
149   if (mBleApi != nullptr) {
150     prePalApiCall(PalType::BLE);
151     return mBleApi->flush();
152   } else {
153     return false;
154   }
155 }
156 
flushCallback(uint8_t errorCode)157 void PlatformBleBase::flushCallback(uint8_t errorCode) {
158   EventLoopManagerSingleton::get()->getBleRequestManager().handleFlushComplete(
159       errorCode);
160 }
161 
handleBtSnoopLog(bool isTxToBtController,const uint8_t * buffer,size_t size)162 void PlatformBleBase::handleBtSnoopLog(bool isTxToBtController,
163                                        const uint8_t *buffer, size_t size) {
164   BtSnoopDirection direction =
165       isTxToBtController ? BtSnoopDirection::OUTGOING_TO_ARBITER
166                          : BtSnoopDirection::INCOMING_FROM_BT_CONTROLLER;
167   chrePlatformBtSnoopLog(direction, buffer, size);
168 }
169 
170 }  // namespace chre
171 
172 #endif  // CHRE_BLE_SUPPORT_ENABLED
173