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 <hardware/bluetooth.h>
32 #include <hardware/bt_gatt.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "bta_gatt_api.h"
37 #include "main/shim/distance_measurement_manager.h"
38 #include "main/shim/le_advertising_manager.h"
39 
40 const btgatt_callbacks_t* bt_gatt_callbacks = NULL;
41 
42 /*******************************************************************************
43  *
44  * Function         btif_gatt_init
45  *
46  * Description      Initializes the GATT interface
47  *
48  * Returns          bt_status_t
49  *
50  ******************************************************************************/
btif_gatt_init(const btgatt_callbacks_t * callbacks)51 static bt_status_t btif_gatt_init(const btgatt_callbacks_t* callbacks) {
52   bt_gatt_callbacks = callbacks;
53   BTA_GATTS_InitBonded();
54   return BT_STATUS_SUCCESS;
55 }
56 
57 /*******************************************************************************
58  *
59  * Function         btif_gatt_cleanup
60  *
61  * Description      Closes the GATT interface
62  *
63  * Returns          void
64  *
65  ******************************************************************************/
btif_gatt_cleanup(void)66 static void btif_gatt_cleanup(void) {
67   if (bt_gatt_callbacks) bt_gatt_callbacks = NULL;
68 
69   BTA_GATTC_Disable();
70   BTA_GATTS_Disable();
71 }
72 
73 static btgatt_interface_t btgattInterface = {
74     .size = sizeof(btgattInterface),
75 
76     .init = btif_gatt_init,
77     .cleanup = btif_gatt_cleanup,
78 
79     .client = &btgattClientInterface,
80     .server = &btgattServerInterface,
81     .scanner = nullptr,    // filled in btif_gatt_get_interface
82     .advertiser = nullptr  // filled in btif_gatt_get_interface
83 };
84 
85 /*******************************************************************************
86  *
87  * Function         btif_gatt_get_interface
88  *
89  * Description      Get the gatt callback interface
90  *
91  * Returns          btgatt_interface_t
92  *
93  ******************************************************************************/
btif_gatt_get_interface()94 const btgatt_interface_t* btif_gatt_get_interface() {
95   // TODO(jpawlowski) right now initializing advertiser field in static
96   // structure cause explosion of dependencies. It must be initialized here
97   // until those dependencies are properly abstracted for tests.
98   btgattInterface.scanner = get_ble_scanner_instance();
99   btgattInterface.advertiser = bluetooth::shim::get_ble_advertiser_instance();
100   btgattInterface.distance_measurement_manager =
101       bluetooth::shim::get_distance_measurement_instance();
102   return &btgattInterface;
103 }
104