1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 The Android Open Source Project
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  *  This is the implementation of the API for SDP search subsystem
22  *
23  ******************************************************************************/
24 
25 #include "common/bt_target.h"
26 #include "bta/bta_api.h"
27 #include "bta/bta_sys.h"
28 #include "bta/bta_sdp_api.h"
29 #include "bta_sdp_int.h"
30 #include <string.h>
31 #include "osi/allocator.h"
32 #include "stack/sdp_api.h"
33 
34 #if defined(BTA_SDP_INCLUDED) && (BTA_SDP_INCLUDED == TRUE)
35 /*****************************************************************************
36 **  Constants
37 *****************************************************************************/
38 
39 static const tBTA_SYS_REG bta_sdp_reg = {
40     bta_sdp_sm_execute,
41     NULL
42 };
43 
44 /*******************************************************************************
45 **
46 ** Function         BTA_SdpEnable
47 **
48 ** Description      Enable the SDP search I/F service. When the enable
49 **                  operation is complete the callback function will be
50 **                  called with a BTA_SDP_ENABLE_EVT. This function must
51 **                  be called before other functions in the SDP search API are
52 **                  called.
53 **
54 ** Returns          BTA_SDP_SUCCESS if successful.
55 **                  BTA_SDP_FAIL if internal failure.
56 **
57 *******************************************************************************/
BTA_SdpEnable(tBTA_SDP_DM_CBACK * p_cback)58 tBTA_SDP_STATUS BTA_SdpEnable(tBTA_SDP_DM_CBACK *p_cback)
59 {
60     tBTA_SDP_STATUS status = BTA_SDP_FAILURE;
61     tBTA_SDP_API_ENABLE  *p_buf;
62 
63     APPL_TRACE_API("%s\n", __FUNCTION__);
64 
65 #if BTA_DYNAMIC_MEMORY == TRUE
66     /* Malloc buffer for SDP configuration structure */
67     p_bta_sdp_cfg->p_sdp_db = (tSDP_DISCOVERY_DB *)osi_malloc(p_bta_sdp_cfg->sdp_db_size);
68     p_bta_sdp_cfg->p_sdp_raw_data = (UINT8 *)osi_malloc(p_bta_sdp_cfg->sdp_raw_size);
69     if (p_bta_sdp_cfg->p_sdp_db == NULL || p_bta_sdp_cfg->p_sdp_raw_data == NULL) {
70         BTA_SdpCleanup();
71         return BTA_SDP_FAILURE;
72     }
73 #endif
74 
75     if (p_cback && FALSE == bta_sys_is_register(BTA_ID_SDP)) {
76         memset(&bta_sdp_cb, 0, sizeof(tBTA_SDP_CB));
77 
78         /* register with BTA system manager */
79         bta_sys_register(BTA_ID_SDP, &bta_sdp_reg);
80 
81         if (p_cback &&
82                 (p_buf = (tBTA_SDP_API_ENABLE *) osi_malloc(sizeof(tBTA_SDP_API_ENABLE))) != NULL) {
83             p_buf->hdr.event = BTA_SDP_API_ENABLE_EVT;
84             p_buf->p_cback = p_cback;
85             bta_sys_sendmsg(p_buf);
86             status = BTA_SDP_SUCCESS;
87         }
88     }
89     return (status);
90 }
91 
92 
93 /*******************************************************************************
94 **
95 ** Function         BTA_SdpDisable
96 **
97 ** Description      Disable the SDP search I/F service.
98 **                  Free buffer for SDP configuration structure.
99 **
100 ** Returns          BTA_SDP_SUCCESS if successful.
101 **                  BTA_SDP_FAIL if internal failure.
102 **
103 *******************************************************************************/
BTA_SdpDisable(void)104 tBTA_SDP_STATUS BTA_SdpDisable(void)
105 {
106     BT_HDR  *p_buf = NULL;
107     tBTA_SDP_STATUS status = BTA_SDP_SUCCESS;
108 
109     if ((p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR))) != NULL) {
110         p_buf->event = BTA_SDP_API_DISABLE_EVT;
111         bta_sys_sendmsg(p_buf);
112         status = BTA_SDP_FAILURE;
113     }
114 
115     return status;
116 }
117 
BTA_SdpCleanup(void)118 tBTA_SDP_STATUS BTA_SdpCleanup(void)
119 {
120     bta_sys_deregister(BTA_ID_SDP);
121 #if BTA_DYNAMIC_MEMORY == TRUE
122     /* Free buffer for SDP configuration structure */
123     if (p_bta_sdp_cfg->p_sdp_db) {
124         osi_free(p_bta_sdp_cfg->p_sdp_db);
125         p_bta_sdp_cfg->p_sdp_db = NULL;
126     }
127 
128     if (p_bta_sdp_cfg->p_sdp_raw_data) {
129         osi_free(p_bta_sdp_cfg->p_sdp_raw_data);
130         p_bta_sdp_cfg->p_sdp_raw_data = NULL;
131     }
132 #endif
133     return BTA_SDP_SUCCESS;
134 }
135 
136 /*******************************************************************************
137 **
138 ** Function         BTA_SdpSearch
139 **
140 ** Description      This function performs service discovery for a specific service
141 **                  on given peer device. When the operation is completed
142 **                  the tBTA_SDP_DM_CBACK callback function will be  called with
143 **                  a BTA_SDP_SEARCH_COMPLETE_EVT.
144 **
145 ** Returns          BTA_SDP_SUCCESS, if the request is being processed.
146 **                  BTA_SDP_FAILURE, otherwise.
147 **
148 *******************************************************************************/
BTA_SdpSearch(BD_ADDR bd_addr,tSDP_UUID * uuid)149 tBTA_SDP_STATUS BTA_SdpSearch(BD_ADDR bd_addr, tSDP_UUID *uuid)
150 {
151     tBTA_SDP_STATUS ret = BTA_SDP_FAILURE;
152     tBTA_SDP_API_SEARCH *p_msg;
153 
154     APPL_TRACE_API("%s\n", __FUNCTION__);
155     if ((p_msg = (tBTA_SDP_API_SEARCH *)osi_malloc(sizeof(tBTA_SDP_API_SEARCH))) != NULL) {
156         p_msg->hdr.event = BTA_SDP_API_SEARCH_EVT;
157         bdcpy(p_msg->bd_addr, bd_addr);
158         //p_msg->uuid = uuid;
159         memcpy(&(p_msg->uuid), uuid, sizeof(tSDP_UUID));
160         bta_sys_sendmsg(p_msg);
161         ret = BTA_SDP_SUCCESS;
162     }
163 
164     return (ret);
165 }
166 
167 /*******************************************************************************
168 **
169 ** Function         BTA_SdpCreateRecordByUser
170 **
171 ** Description      This function is used to request a callback to create a SDP
172 **                  record. The registered callback will be called with event
173 **                  BTA_SDP_CREATE_RECORD_USER_EVT.
174 **
175 ** Returns          BTA_SDP_SUCCESS, if the request is being processed.
176 **                  BTA_SDP_FAILURE, otherwise.
177 **
178 *******************************************************************************/
BTA_SdpCreateRecordByUser(void * user_data)179 tBTA_SDP_STATUS BTA_SdpCreateRecordByUser(void *user_data)
180 {
181     tBTA_SDP_STATUS ret = BTA_SDP_FAILURE;
182     tBTA_SDP_API_RECORD_USER *p_msg;
183 
184     APPL_TRACE_API("%s\n", __FUNCTION__);
185     if ((p_msg = (tBTA_SDP_API_RECORD_USER *)osi_malloc(sizeof(tBTA_SDP_API_RECORD_USER))) != NULL) {
186         p_msg->hdr.event = BTA_SDP_API_CREATE_RECORD_USER_EVT;
187         p_msg->user_data = user_data;
188         bta_sys_sendmsg(p_msg);
189         ret = BTA_SDP_SUCCESS;
190     }
191 
192     return (ret);
193 }
194 
195 /*******************************************************************************
196 **
197 ** Function         BTA_SdpRemoveRecordByUser
198 **
199 ** Description      This function is used to request a callback to remove a SDP
200 **                  record. The registered callback will be called with event
201 **                  BTA_SDP_REMOVE_RECORD_USER_EVT.
202 **
203 ** Returns          BTA_SDP_SUCCESS, if the request is being processed.
204 **                  BTA_SDP_FAILURE, otherwise.
205 **
206 *******************************************************************************/
BTA_SdpRemoveRecordByUser(void * user_data)207 tBTA_SDP_STATUS BTA_SdpRemoveRecordByUser(void *user_data)
208 {
209     tBTA_SDP_STATUS ret = BTA_SDP_FAILURE;
210     tBTA_SDP_API_RECORD_USER *p_msg;
211 
212     APPL_TRACE_API("%s\n", __FUNCTION__);
213     if ((p_msg = (tBTA_SDP_API_RECORD_USER *)osi_malloc(sizeof(tBTA_SDP_API_RECORD_USER))) != NULL) {
214         p_msg->hdr.event = BTA_SDP_API_REMOVE_RECORD_USER_EVT;
215         p_msg->user_data = user_data;
216         bta_sys_sendmsg(p_msg);
217         ret = BTA_SDP_SUCCESS;
218     }
219 
220     return (ret);
221 }
222 
223 
224 #endif /* #if defined(BTA_SDP_INCLUDED) && (BTA_SDP_INCLUDED == TRUE) */
225