1 /******************************************************************************
2 *
3 * Copyright (C) 2014 The Android Open Source Project
4 * Copyright (C) 2003-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 /******************************************************************************
21 *
22 * This file contains action functions for MCE.
23 *
24 ******************************************************************************/
25
26 #include <arpa/inet.h>
27 #include <hardware/bluetooth.h>
28
29 #include <string.h>
30 #include "bt_common.h"
31 #include "bt_types.h"
32 #include "bta_api.h"
33 #include "bta_mce_api.h"
34 #include "bta_mce_int.h"
35 #include "bta_sys.h"
36 #include "btm_api.h"
37 #include "btm_int.h"
38 #include "sdp_api.h"
39 #include "utl.h"
40
41 /*****************************************************************************
42 * Constants
43 ****************************************************************************/
44
45 static const tBT_UUID bta_mce_mas_uuid = {
46 .len = 2, .uu.uuid16 = UUID_SERVCLASS_MESSAGE_ACCESS};
47
48 /*******************************************************************************
49 *
50 * Function bta_mce_search_cback
51 *
52 * Description Callback from btm after search is completed
53 *
54 * Returns void
55 *
56 ******************************************************************************/
57
bta_mce_search_cback(uint16_t result,void * user_data)58 static void bta_mce_search_cback(uint16_t result, void* user_data) {
59 tSDP_DISC_REC* p_rec = NULL;
60 tBTA_MCE_MAS_DISCOVERY_COMP evt_data;
61 int found = 0;
62
63 APPL_TRACE_DEBUG("bta_mce_start_discovery_cback res: 0x%x", result);
64
65 bta_mce_cb.sdp_active = BTA_MCE_SDP_ACT_NONE;
66
67 if (bta_mce_cb.p_dm_cback == NULL) return;
68
69 evt_data.status = BTA_MCE_FAILURE;
70 bdcpy(evt_data.remote_addr, bta_mce_cb.remote_addr);
71 evt_data.num_mas = 0;
72
73 if (result == SDP_SUCCESS || result == SDP_DB_FULL) {
74 do {
75 tSDP_DISC_ATTR* p_attr;
76 tSDP_PROTOCOL_ELEM pe;
77
78 p_rec = SDP_FindServiceUUIDInDb(p_bta_mce_cfg->p_sdp_db,
79 (tBT_UUID*)&bta_mce_mas_uuid, p_rec);
80
81 APPL_TRACE_DEBUG("p_rec:%p", p_rec);
82
83 if (p_rec == NULL) break;
84
85 if (!SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe))
86 continue;
87
88 evt_data.mas[found].scn = pe.params[0];
89
90 p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_NAME);
91 if (p_attr == NULL) continue;
92
93 evt_data.mas[found].p_srv_name = (char*)p_attr->attr_value.v.array;
94 evt_data.mas[found].srv_name_len =
95 SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
96
97 p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_MAS_INSTANCE_ID);
98 if (p_attr == NULL) break;
99
100 evt_data.mas[found].instance_id = p_attr->attr_value.v.u8;
101
102 p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_MSG_TYPE);
103 if (p_attr == NULL) break;
104
105 evt_data.mas[found].msg_type = p_attr->attr_value.v.u8;
106
107 found++;
108 } while (p_rec != NULL && found < BTA_MCE_MAX_MAS_INSTANCES);
109
110 evt_data.num_mas = found;
111 evt_data.status = BTA_MCE_SUCCESS;
112 }
113
114 bta_mce_cb.p_dm_cback(BTA_MCE_MAS_DISCOVERY_COMP_EVT, (tBTA_MCE*)&evt_data,
115 user_data);
116 }
117
118 /*******************************************************************************
119 *
120 * Function bta_mce_enable
121 *
122 * Description Initializes the MCE I/F
123 *
124 * Returns void
125 *
126 ******************************************************************************/
bta_mce_enable(tBTA_MCE_MSG * p_data)127 void bta_mce_enable(tBTA_MCE_MSG* p_data) {
128 tBTA_MCE_STATUS status = BTA_MCE_SUCCESS;
129 bta_mce_cb.p_dm_cback = p_data->enable.p_cback;
130 bta_mce_cb.p_dm_cback(BTA_MCE_ENABLE_EVT, (tBTA_MCE*)&status, NULL);
131 }
132
133 /*******************************************************************************
134 *
135 * Function bta_mce_get_remote_mas_instances
136 *
137 * Description Discovers MAS instances on remote device
138 *
139 * Returns void
140 *
141 ******************************************************************************/
bta_mce_get_remote_mas_instances(tBTA_MCE_MSG * p_data)142 void bta_mce_get_remote_mas_instances(tBTA_MCE_MSG* p_data) {
143 if (p_data == NULL) {
144 APPL_TRACE_DEBUG("MCE control block handle is null");
145 return;
146 }
147 tBTA_MCE_STATUS status = BTA_MCE_FAILURE;
148
149 APPL_TRACE_DEBUG("%s in, sdp_active:%d", __func__, bta_mce_cb.sdp_active);
150
151 if (bta_mce_cb.sdp_active != BTA_MCE_SDP_ACT_NONE) {
152 /* SDP is still in progress */
153 status = BTA_MCE_BUSY;
154 if (bta_mce_cb.p_dm_cback)
155 bta_mce_cb.p_dm_cback(BTA_MCE_MAS_DISCOVERY_COMP_EVT, (tBTA_MCE*)&status,
156 NULL);
157
158 return;
159 }
160
161 bta_mce_cb.sdp_active = BTA_MCE_SDP_ACT_YES;
162 bdcpy(bta_mce_cb.remote_addr, p_data->get_rmt_mas.bd_addr);
163
164 SDP_InitDiscoveryDb(p_bta_mce_cfg->p_sdp_db, p_bta_mce_cfg->sdp_db_size, 1,
165 (tBT_UUID*)&bta_mce_mas_uuid, 0, NULL);
166
167 if (!SDP_ServiceSearchAttributeRequest2(p_data->get_rmt_mas.bd_addr,
168 p_bta_mce_cfg->p_sdp_db,
169 bta_mce_search_cback, NULL)) {
170 bta_mce_cb.sdp_active = BTA_MCE_SDP_ACT_NONE;
171
172 /* failed to start SDP. report the failure right away */
173 if (bta_mce_cb.p_dm_cback)
174 bta_mce_cb.p_dm_cback(BTA_MCE_MAS_DISCOVERY_COMP_EVT, (tBTA_MCE*)&status,
175 NULL);
176 }
177 /*
178 else report the result when the cback is called
179 */
180 }
181