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