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