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