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