1 /******************************************************************************
2  *
3  *  Copyright (C) 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 <errno.h>
30 #include <hardware/bluetooth.h>
31 #include <hardware/bt_gatt.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "btif_common.h"
37 #include "btif_util.h"
38 
39 #include "bta_api.h"
40 #include "bta_gatt_api.h"
41 #include "btif_gatt.h"
42 #include "btif_gatt_util.h"
43 #include "btif_storage.h"
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   return BT_STATUS_SUCCESS;
59 }
60 
61 /*******************************************************************************
62  *
63  * Function         btif_gatt_cleanup
64  *
65  * Description      Closes the GATT interface
66  *
67  * Returns          void
68  *
69  ******************************************************************************/
btif_gatt_cleanup(void)70 static void btif_gatt_cleanup(void) {
71   if (bt_gatt_callbacks) bt_gatt_callbacks = NULL;
72 
73   BTA_GATTC_Disable();
74   BTA_GATTS_Disable();
75 }
76 
77 static btgatt_interface_t btgattInterface = {
78     sizeof(btgattInterface),
79 
80     btif_gatt_init,
81     btif_gatt_cleanup,
82 
83     &btgattClientInterface,
84     &btgattServerInterface,
85     nullptr,  // filled in btif_gatt_get_interface
86     nullptr   // filled in btif_gatt_get_interface
87 };
88 
89 /*******************************************************************************
90  *
91  * Function         btif_gatt_get_interface
92  *
93  * Description      Get the gatt callback interface
94  *
95  * Returns          btgatt_interface_t
96  *
97  ******************************************************************************/
btif_gatt_get_interface()98 const btgatt_interface_t* btif_gatt_get_interface() {
99   // TODO(jpawlowski) right now initializing advertiser field in static
100   // structure cause explosion of dependencies. It must be initialized here
101   // until those dependencies are properly abstracted for tests.
102   btgattInterface.scanner = get_ble_scanner_instance();
103   btgattInterface.advertiser = get_ble_advertiser_instance();
104   return &btgattInterface;
105 }
106