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