1 /******************************************************************************
2  *
3  * Copyright 2014 Samsung System LSI
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_sdp.c
22  *  Description:   SDP Bluetooth Interface.
23  *                 Implements the generic message handling and search
24  *                 functionality.
25  *                 References btif_sdp_server.c for SDP record creation.
26  *
27  ******************************************************************************/
28 
29 #define LOG_TAG "bt_btif_sdp"
30 
31 #include "btif/include/btif_sdp.h"
32 
33 #include <bluetooth/log.h>
34 #include <hardware/bluetooth.h>
35 #include <hardware/bt_sdp.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "bta/include/bta_sdp_api.h"
40 #include "bta_api.h"
41 #include "btif_common.h"
42 #include "types/bluetooth/uuid.h"
43 #include "types/raw_address.h"
44 
45 using bluetooth::Uuid;
46 using namespace bluetooth;
47 
48 /*****************************************************************************
49  *  Functions implemented in sdp_server.c
50  *****************************************************************************/
51 bt_status_t sdp_server_init();
52 void sdp_server_cleanup();
53 bt_status_t create_sdp_record(bluetooth_sdp_record* records, int* record_handles);
54 bt_status_t remove_sdp_record(int record_handle);
55 void on_create_record_event(int handle);
56 void on_remove_record_event(int handle);
57 
58 // Utility functions:
59 int get_sdp_records_size(bluetooth_sdp_record* in_record, int count);
60 void copy_sdp_records(bluetooth_sdp_record* in_records, bluetooth_sdp_record* out_records,
61                       int count);
62 
63 /*****************************************************************************
64  *  Static variables
65  *****************************************************************************/
66 
67 static btsdp_callbacks_t* bt_sdp_callbacks = NULL;
68 
btif_sdp_search_comp_evt(uint16_t event,char * p_param)69 static void btif_sdp_search_comp_evt(uint16_t event, char* p_param) {
70   tBTA_SDP_SEARCH_COMP* evt_data = (tBTA_SDP_SEARCH_COMP*)p_param;
71   log::verbose("event = {}", event);
72 
73   if (event != BTA_SDP_SEARCH_COMP_EVT) {
74     return;
75   }
76 
77   HAL_CBACK(bt_sdp_callbacks, sdp_search_cb, (bt_status_t)evt_data->status, evt_data->remote_addr,
78             evt_data->uuid, evt_data->record_count, evt_data->records);
79 }
80 
sdp_search_comp_copy_cb(uint16_t event,char * p_dest,const char * p_src)81 static void sdp_search_comp_copy_cb(uint16_t event, char* p_dest, const char* p_src) {
82   tBTA_SDP_SEARCH_COMP* p_dest_data = (tBTA_SDP_SEARCH_COMP*)p_dest;
83   tBTA_SDP_SEARCH_COMP* p_src_data = (tBTA_SDP_SEARCH_COMP*)p_src;
84 
85   if (!p_src) {
86     return;
87   }
88 
89   if (event != BTA_SDP_SEARCH_COMP_EVT) {
90     return;
91   }
92 
93   maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
94 
95   copy_sdp_records(p_src_data->records, p_dest_data->records, p_src_data->record_count);
96 }
97 
sdp_dm_cback(tBTA_SDP_EVT event,tBTA_SDP * p_data,void * user_data)98 static void sdp_dm_cback(tBTA_SDP_EVT event, tBTA_SDP* p_data, void* user_data) {
99   switch (event) {
100     case BTA_SDP_SEARCH_COMP_EVT: {
101       int size = sizeof(tBTA_SDP);
102       size += get_sdp_records_size(p_data->sdp_search_comp.records,
103                                    p_data->sdp_search_comp.record_count);
104 
105       /* need to deep copy the record content */
106       btif_transfer_context(btif_sdp_search_comp_evt, event, (char*)p_data, size,
107                             sdp_search_comp_copy_cb);
108       break;
109     }
110     case BTA_SDP_CREATE_RECORD_USER_EVT: {
111       on_create_record_event(PTR_TO_INT(user_data));
112       break;
113     }
114     case BTA_SDP_REMOVE_RECORD_USER_EVT: {
115       on_remove_record_event(PTR_TO_INT(user_data));
116       break;
117     }
118     default:
119       break;
120   }
121 }
122 
init(btsdp_callbacks_t * callbacks)123 static bt_status_t init(btsdp_callbacks_t* callbacks) {
124   log::verbose("Sdp Search Init");
125 
126   bt_sdp_callbacks = callbacks;
127   sdp_server_init();
128 
129   btif_enable_service(BTA_SDP_SERVICE_ID);
130 
131   return BT_STATUS_SUCCESS;
132 }
133 
deinit()134 static bt_status_t deinit() {
135   log::verbose("Sdp Search Deinit");
136 
137   bt_sdp_callbacks = NULL;
138   sdp_server_cleanup();
139   btif_disable_service(BTA_SDP_SERVICE_ID);
140 
141   return BT_STATUS_SUCCESS;
142 }
143 
search(RawAddress * bd_addr,const Uuid & uuid)144 static bt_status_t search(RawAddress* bd_addr, const Uuid& uuid) {
145   BTA_SdpSearch(*bd_addr, uuid);
146   return BT_STATUS_SUCCESS;
147 }
148 
149 static const btsdp_interface_t sdp_if = {
150         sizeof(btsdp_interface_t), init, deinit, search, create_sdp_record, remove_sdp_record};
151 
btif_sdp_get_interface(void)152 const btsdp_interface_t* btif_sdp_get_interface(void) {
153   log::verbose("");
154   return &sdp_if;
155 }
156 
157 /*******************************************************************************
158  *
159  * Function         btif_sdp_execute_service
160  *
161  * Description      Initializes/Shuts down the service
162  *
163  * Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
164  *
165  ******************************************************************************/
btif_sdp_execute_service(bool b_enable)166 bt_status_t btif_sdp_execute_service(bool b_enable) {
167   log::verbose("enable:{}", b_enable);
168 
169   if (b_enable) {
170     BTA_SdpEnable(sdp_dm_cback);
171   } else {
172     /* This is called on BT disable so no need to extra cleanup */
173   }
174   return BT_STATUS_SUCCESS;
175 }
176