1 /******************************************************************************
2  *
3  *  Copyright 2009-2013 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /*******************************************************************************
20  *
21  *  Filename:      btif_gatt.c
22  *
23  *  Description:   GATT Profile Bluetooth Interface
24  *
25  ******************************************************************************/
26 
27 #define LOG_TAG "bt_btif_gatt"
28 
29 #include "btif_gatt.h"
30 
31 #include <com_android_bluetooth_flags.h>
32 #include <hardware/bluetooth.h>
33 #include <hardware/bt_gatt.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "bta/include/bta_gatt_api.h"
38 #include "btif/include/btif_common.h"
39 #include "main/shim/distance_measurement_manager.h"
40 #include "main/shim/le_advertising_manager.h"
41 
42 // TODO(b/369381361) Enfore -Wmissing-prototypes
43 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
44 
45 const btgatt_callbacks_t* bt_gatt_callbacks = NULL;
46 
47 /*******************************************************************************
48  *
49  * Function         btif_gatt_init
50  *
51  * Description      Initializes the GATT interface
52  *
53  * Returns          bt_status_t
54  *
55  ******************************************************************************/
btif_gatt_init(const btgatt_callbacks_t * callbacks)56 static bt_status_t btif_gatt_init(const btgatt_callbacks_t* callbacks) {
57   bt_gatt_callbacks = callbacks;
58   BTA_GATTS_InitBonded();
59   return BT_STATUS_SUCCESS;
60 }
61 
62 /*******************************************************************************
63  *
64  * Function         btif_gatt_cleanup
65  *
66  * Description      Closes the GATT interface
67  *
68  * Returns          void
69  *
70  ******************************************************************************/
btif_gatt_cleanup(void)71 static void btif_gatt_cleanup(void) {
72   if (bt_gatt_callbacks) {
73     bt_gatt_callbacks = NULL;
74   }
75 
76   BTA_GATTC_Disable();
77   BTA_GATTS_Disable();
78 }
79 
80 static btgatt_interface_t btgattInterface = {
81         .size = sizeof(btgattInterface),
82 
83         .init = btif_gatt_init,
84         .cleanup = btif_gatt_cleanup,
85 
86         .client = &btgattClientInterface,
87         .server = &btgattServerInterface,
88         .scanner = nullptr,    // filled in btif_gatt_get_interface
89         .advertiser = nullptr  // filled in btif_gatt_get_interface
90 };
91 
92 /*******************************************************************************
93  *
94  * Function         btif_gatt_get_interface
95  *
96  * Description      Get the gatt callback interface
97  *
98  * Returns          btgatt_interface_t
99  *
100  ******************************************************************************/
btif_gatt_get_interface()101 const btgatt_interface_t* btif_gatt_get_interface() {
102   // TODO(jpawlowski) right now initializing advertiser field in static
103   // structure cause explosion of dependencies. It must be initialized here
104   // until those dependencies are properly abstracted for tests.
105   btgattInterface.scanner = get_ble_scanner_instance();
106   btgattInterface.advertiser = bluetooth::shim::get_ble_advertiser_instance();
107   btgattInterface.distance_measurement_manager =
108           bluetooth::shim::get_distance_measurement_instance();
109   return &btgattInterface;
110 }
111