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 * Static variables
50 *****************************************************************************/
51
52 static btsdp_callbacks_t* bt_sdp_callbacks = NULL;
53
btif_sdp_search_comp_evt(uint16_t event,char * p_param)54 static void btif_sdp_search_comp_evt(uint16_t event, char* p_param) {
55 tBTA_SDP_SEARCH_COMP* evt_data = (tBTA_SDP_SEARCH_COMP*)p_param;
56 log::verbose("event = {}", event);
57
58 if (event != BTA_SDP_SEARCH_COMP_EVT) {
59 return;
60 }
61
62 HAL_CBACK(bt_sdp_callbacks, sdp_search_cb, (bt_status_t)evt_data->status, evt_data->remote_addr,
63 evt_data->uuid, evt_data->record_count, evt_data->records);
64 }
65
sdp_search_comp_copy_cb(uint16_t event,char * p_dest,const char * p_src)66 static void sdp_search_comp_copy_cb(uint16_t event, char* p_dest, const char* p_src) {
67 tBTA_SDP_SEARCH_COMP* p_dest_data = (tBTA_SDP_SEARCH_COMP*)p_dest;
68 tBTA_SDP_SEARCH_COMP* p_src_data = (tBTA_SDP_SEARCH_COMP*)p_src;
69
70 if (!p_src) {
71 return;
72 }
73
74 if (event != BTA_SDP_SEARCH_COMP_EVT) {
75 return;
76 }
77
78 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
79
80 copy_sdp_records(p_src_data->records, p_dest_data->records, p_src_data->record_count);
81 }
82
sdp_dm_cback(tBTA_SDP_EVT event,tBTA_SDP * p_data,void * user_data)83 static void sdp_dm_cback(tBTA_SDP_EVT event, tBTA_SDP* p_data, void* user_data) {
84 switch (event) {
85 case BTA_SDP_SEARCH_COMP_EVT: {
86 int size = sizeof(tBTA_SDP);
87 size += get_sdp_records_size(p_data->sdp_search_comp.records,
88 p_data->sdp_search_comp.record_count);
89
90 /* need to deep copy the record content */
91 btif_transfer_context(btif_sdp_search_comp_evt, event, (char*)p_data, size,
92 sdp_search_comp_copy_cb);
93 break;
94 }
95 case BTA_SDP_CREATE_RECORD_USER_EVT: {
96 on_create_record_event(PTR_TO_INT(user_data));
97 break;
98 }
99 case BTA_SDP_REMOVE_RECORD_USER_EVT: {
100 on_remove_record_event(PTR_TO_INT(user_data));
101 break;
102 }
103 default:
104 break;
105 }
106 }
107
init(btsdp_callbacks_t * callbacks)108 static bt_status_t init(btsdp_callbacks_t* callbacks) {
109 log::verbose("Sdp Search Init");
110
111 bt_sdp_callbacks = callbacks;
112 sdp_server_init();
113
114 btif_enable_service(BTA_SDP_SERVICE_ID);
115
116 return BT_STATUS_SUCCESS;
117 }
118
deinit()119 static bt_status_t deinit() {
120 log::verbose("Sdp Search Deinit");
121
122 bt_sdp_callbacks = NULL;
123 sdp_server_cleanup();
124 btif_disable_service(BTA_SDP_SERVICE_ID);
125
126 return BT_STATUS_SUCCESS;
127 }
128
search(RawAddress * bd_addr,const Uuid & uuid)129 static bt_status_t search(RawAddress* bd_addr, const Uuid& uuid) {
130 BTA_SdpSearch(*bd_addr, uuid);
131 return BT_STATUS_SUCCESS;
132 }
133
134 static const btsdp_interface_t sdp_if = {
135 sizeof(btsdp_interface_t), init, deinit, search, create_sdp_record, remove_sdp_record};
136
btif_sdp_get_interface(void)137 const btsdp_interface_t* btif_sdp_get_interface(void) {
138 log::verbose("");
139 return &sdp_if;
140 }
141
142 /*******************************************************************************
143 *
144 * Function btif_sdp_execute_service
145 *
146 * Description Initializes/Shuts down the service
147 *
148 * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
149 *
150 ******************************************************************************/
btif_sdp_execute_service(bool b_enable)151 bt_status_t btif_sdp_execute_service(bool b_enable) {
152 log::verbose("enable:{}", b_enable);
153
154 if (b_enable) {
155 BTA_SdpEnable(sdp_dm_cback);
156 } else {
157 /* This is called on BT disable so no need to extra cleanup */
158 }
159 return BT_STATUS_SUCCESS;
160 }
161