]> granicus.if.org Git - esp-idf/blob - examples/08_bt_sdp/components/bluedroid_demos/btif/btif_sdp.c
component/bt: merge SDP client and SDP server example to a single demo
[esp-idf] / examples / 08_bt_sdp / components / bluedroid_demos / btif / btif_sdp.c
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 functionality.
24  *                 References btif_sdp_server.c for SDP record creation.
25  *
26  ***********************************************************************************/
27
28 #include "btif_sdp.h"
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define LOG_TAG "BTIF_SDP"
33 #include "btif_common.h"
34 #include "btif_util.h"
35 #include "bta_api.h"
36
37 #include "bt_sdp_api.h"
38
39 /*****************************************************************************
40 **  Functions implemented in sdp_server.c
41 ******************************************************************************/
42 bt_status_t sdp_server_init();
43 void sdp_server_cleanup();
44 void on_create_record_event(int handle);
45 void on_remove_record_event(int handle);
46
47 // Utility functions:
48 int get_sdp_records_size(bluetooth_sdp_record* in_record, int count);
49 void copy_sdp_records(bluetooth_sdp_record* in_records,
50         bluetooth_sdp_record* out_records, int count);
51
52
53 /*****************************************************************************
54 **  Static variables
55 ******************************************************************************/
56
57 static btsdp_callbacks_t *bt_sdp_callbacks = NULL;
58
59 static void btif_sdp_search_comp_evt(UINT16 event, char *p_param)
60 {
61     bt_sdp_search_comp_t *evt_data = (bt_sdp_search_comp_t *) p_param;
62     bt_bdaddr_t addr;
63     BTIF_TRACE_DEBUG("%s:  event = %d\n", __FUNCTION__, event);
64
65     if (event != BT_SDP_SEARCH_COMP_EVT)
66         return;
67
68     bdcpy(addr.address, evt_data->remote_addr);
69
70     HAL_CBACK(bt_sdp_callbacks, sdp_search_cb, evt_data->status,
71             &addr, (uint8_t*)(evt_data->uuid.uu.uuid128),
72             evt_data->record_count, evt_data->records);
73 }
74
75 static void sdp_search_comp_copy_cb(UINT16 event, char *p_dest, char *p_src)
76 {
77     bt_sdp_search_comp_t *p_dest_data = (bt_sdp_search_comp_t *) p_dest;
78     bt_sdp_search_comp_t *p_src_data = (bt_sdp_search_comp_t *) p_src;
79     if (!p_src)
80         return;
81
82     if (event != BT_SDP_SEARCH_COMP_EVT)
83         return;
84
85     memcpy(p_dest_data, p_src_data, sizeof(bt_sdp_search_comp_t));
86
87     copy_sdp_records(p_src_data->records, p_dest_data->records, p_src_data->record_count);
88 }
89
90
91 static void sdp_dm_cback(bt_sdp_evt_t event, bt_sdp_t *p_data, void * user_data)
92 {
93     switch (event)
94     {
95         case BT_SDP_SEARCH_COMP_EVT:
96         {
97             int size = sizeof(bt_sdp_t);
98             size += get_sdp_records_size(p_data->sdp_search_comp.records,
99                     p_data->sdp_search_comp.record_count);
100
101             BTIF_TRACE_DEBUG("%s:  stat %d, record_cnt = %d\n", __FUNCTION__, p_data->sdp_search_comp.status, p_data->sdp_search_comp.record_count);
102             /* need to deep copy the record content */
103             btif_transfer_context(btif_sdp_search_comp_evt, event,
104                                     (char*)p_data, size, sdp_search_comp_copy_cb);
105             break;
106         }
107         case BT_SDP_CREATE_RECORD_USER_EVT:
108         {
109             on_create_record_event((int)user_data);
110             break;
111         }
112         case BT_SDP_REMOVE_RECORD_USER_EVT:
113         {
114             on_remove_record_event((int)user_data);
115             break;
116         }
117         default:
118             break;
119     }
120 }
121
122 bt_status_t BTIF_SdpInit(btsdp_callbacks_t *callbacks)
123 {
124     BTIF_TRACE_DEBUG("Sdp Search %s\n", __FUNCTION__);
125
126     bt_sdp_callbacks = callbacks;
127     sdp_server_init();
128
129     btif_enable_service(BTA_SDP_SERVICE_ID);
130
131     return BT_STATUS_SUCCESS;
132 }
133
134 bt_status_t BTIF_SdpDeinit(void)
135 {
136     BTIF_TRACE_DEBUG("Sdp Search %s\n", __FUNCTION__);
137
138     bt_sdp_callbacks = NULL;
139     sdp_server_cleanup();
140     btif_disable_service(BTA_SDP_SERVICE_ID);
141
142     return BT_STATUS_SUCCESS;
143 }
144
145 bt_status_t BTIF_SdpSearch(bt_bdaddr_t *bd_addr,  const uint8_t* uuid)
146 {
147     esp_bt_uuid_t sdp_uuid;
148     sdp_uuid.len = 16;
149     memcpy(sdp_uuid.uu.uuid128, uuid, sizeof(sdp_uuid.uu.uuid128));
150
151     esp_bt_sdp_search(bd_addr->address, &sdp_uuid);
152     return BT_STATUS_SUCCESS;
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 *******************************************************************************/
164 bt_status_t btif_sdp_execute_service(BOOLEAN b_enable)
165 {
166     BTIF_TRACE_DEBUG("%s enable:%d\n", __FUNCTION__, b_enable);
167
168     if (b_enable) {
169         esp_bt_sdp_enable(sdp_dm_cback);
170     } else {
171         /* This is called on BT disable so no need to extra cleanup */
172     }
173     return BT_STATUS_SUCCESS;
174 }
175