xref: /aosp_15_r20/system/chre/platform/shared/chre_api_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 #include "chre_api/chre/ble.h"
18 
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/util/macros.h"
21 #include "chre/util/system/napp_permissions.h"
22 
23 using chre::EventLoopManager;
24 using chre::EventLoopManagerSingleton;
25 using chre::Nanoapp;
26 using chre::NanoappPermissions;
27 
chreBleGetCapabilities()28 DLL_EXPORT uint32_t chreBleGetCapabilities() {
29 #ifdef CHRE_BLE_SUPPORT_ENABLED
30   return EventLoopManagerSingleton::get()
31       ->getBleRequestManager()
32       .getCapabilities();
33 #else
34   return CHRE_BLE_CAPABILITIES_NONE;
35 #endif  // CHRE_BLE_SUPPORT_ENABLED
36 }
37 
chreBleGetFilterCapabilities()38 DLL_EXPORT uint32_t chreBleGetFilterCapabilities() {
39 #ifdef CHRE_BLE_SUPPORT_ENABLED
40   return EventLoopManagerSingleton::get()
41       ->getBleRequestManager()
42       .getFilterCapabilities();
43 #else
44   return CHRE_BLE_FILTER_CAPABILITIES_NONE;
45 #endif  // CHRE_BLE_SUPPORT_ENABLED
46 }
47 
chreBleFlushAsync(const void * cookie)48 DLL_EXPORT bool chreBleFlushAsync(const void *cookie) {
49 #ifdef CHRE_BLE_SUPPORT_ENABLED
50   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
51   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_BLE) &&
52          EventLoopManagerSingleton::get()->getBleRequestManager().flushAsync(
53              nanoapp, cookie);
54 #else
55   UNUSED_VAR(cookie);
56   return false;
57 #endif  // CHRE_BLE_SUPPORT_ENABLED
58 }
59 
chreBleStartScanAsyncV1_9(chreBleScanMode mode,uint32_t reportDelayMs,const struct chreBleScanFilterV1_9 * filter,const void * cookie)60 DLL_EXPORT bool chreBleStartScanAsyncV1_9(
61     chreBleScanMode mode, uint32_t reportDelayMs,
62     const struct chreBleScanFilterV1_9 *filter, const void *cookie) {
63 #ifdef CHRE_BLE_SUPPORT_ENABLED
64   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
65   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_BLE) &&
66          EventLoopManagerSingleton::get()
67              ->getBleRequestManager()
68              .startScanAsync(nanoapp, mode, reportDelayMs, filter, cookie);
69 #else
70   UNUSED_VAR(mode);
71   UNUSED_VAR(reportDelayMs);
72   UNUSED_VAR(filter);
73   UNUSED_VAR(cookie);
74   return false;
75 #endif  // CHRE_BLE_SUPPORT_ENABLED
76 }
77 
chreBleStartScanAsync(chreBleScanMode mode,uint32_t reportDelayMs,const struct chreBleScanFilter * filter)78 DLL_EXPORT bool chreBleStartScanAsync(chreBleScanMode mode,
79                                       uint32_t reportDelayMs,
80                                       const struct chreBleScanFilter *filter) {
81   if (filter == nullptr) {
82     return chreBleStartScanAsyncV1_9(mode, reportDelayMs, nullptr /* filter */,
83                                      nullptr /* cookie */);
84   }
85   chreBleScanFilterV1_9 filterV1_9 = {
86       filter->rssiThreshold, filter->scanFilterCount, filter->scanFilters,
87       0 /* broadcasterAddressFilterCount */,
88       nullptr /* broadcasterAddressFilters */};
89   return chreBleStartScanAsyncV1_9(mode, reportDelayMs, &filterV1_9,
90                                    nullptr /* cookie */);
91 }
92 
chreBleStopScanAsyncV1_9(const void * cookie)93 DLL_EXPORT bool chreBleStopScanAsyncV1_9(const void *cookie) {
94 #ifdef CHRE_BLE_SUPPORT_ENABLED
95   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
96   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_BLE) &&
97          EventLoopManagerSingleton::get()->getBleRequestManager().stopScanAsync(
98              nanoapp, cookie);
99 #else
100   UNUSED_VAR(cookie);
101   return false;
102 #endif  // CHRE_BLE_SUPPORT_ENABLED
103 }
104 
chreBleStopScanAsync()105 DLL_EXPORT bool chreBleStopScanAsync() {
106   return chreBleStopScanAsyncV1_9(nullptr /* cookie */);
107 }
108 
chreBleReadRssiAsync(uint16_t connectionHandle,const void * cookie)109 DLL_EXPORT bool chreBleReadRssiAsync(uint16_t connectionHandle,
110                                      const void *cookie) {
111 #ifdef CHRE_BLE_SUPPORT_ENABLED
112   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
113   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_BLE) &&
114          EventLoopManagerSingleton::get()->getBleRequestManager().readRssiAsync(
115              nanoapp, connectionHandle, cookie);
116 #else
117   UNUSED_VAR(connectionHandle);
118   UNUSED_VAR(cookie);
119   return false;
120 #endif  // CHRE_BLE_SUPPORT_ENABLED
121 }
122 
chreBleGetScanStatus(struct chreBleScanStatus * status)123 DLL_EXPORT bool chreBleGetScanStatus(struct chreBleScanStatus *status) {
124 #ifdef CHRE_BLE_SUPPORT_ENABLED
125   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
126   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_BLE) &&
127          EventLoopManagerSingleton::get()->getBleRequestManager().getScanStatus(
128              status);
129 #else
130   UNUSED_VAR(status);
131   return false;
132 #endif  // CHRE_BLE_SUPPORT_ENABLED
133 }