1 /******************************************************************************
2 *
3 * Copyright (C) 2014 The Android Open Source Project
4 * Copyright (C) 2009-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 * Filename: btif_mce.c
23 *
24 * Description: Message Access Profile (MCE role) Bluetooth Interface
25 *
26 *
27 ******************************************************************************/
28
29 #define LOG_TAG "bt_btif_mce"
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <hardware/bluetooth.h>
35 #include <hardware/bt_mce.h>
36
37 #include "bt_types.h"
38 #include "bta_api.h"
39 #include "bta_mce_api.h"
40 #include "btcore/include/bdaddr.h"
41 #include "btif_common.h"
42 #include "btif_profile_queue.h"
43 #include "btif_util.h"
44
45 /*****************************************************************************
46 * Static variables
47 *****************************************************************************/
48
49 static btmce_callbacks_t* bt_mce_callbacks = NULL;
50
btif_mce_mas_discovery_comp_evt(uint16_t event,char * p_param)51 static void btif_mce_mas_discovery_comp_evt(uint16_t event, char* p_param) {
52 tBTA_MCE_MAS_DISCOVERY_COMP* evt_data = (tBTA_MCE_MAS_DISCOVERY_COMP*)p_param;
53 btmce_mas_instance_t insts[BTA_MCE_MAX_MAS_INSTANCES];
54 bt_bdaddr_t addr;
55 int i;
56
57 BTIF_TRACE_EVENT("%s: event = %d", __func__, event);
58
59 if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT) return;
60
61 for (i = 0; i < evt_data->num_mas; i++) {
62 insts[i].id = evt_data->mas[i].instance_id;
63 insts[i].scn = evt_data->mas[i].scn;
64 insts[i].msg_types = evt_data->mas[i].msg_type;
65 insts[i].p_name = evt_data->mas[i].p_srv_name;
66 }
67
68 bdcpy(addr.address, evt_data->remote_addr);
69
70 HAL_CBACK(bt_mce_callbacks, remote_mas_instances_cb,
71 (bt_status_t)evt_data->status, &addr, evt_data->num_mas, insts);
72 }
73
mas_discovery_comp_copy_cb(uint16_t event,char * p_dest,char * p_src)74 static void mas_discovery_comp_copy_cb(uint16_t event, char* p_dest,
75 char* p_src) {
76 tBTA_MCE_MAS_DISCOVERY_COMP* p_dest_data =
77 (tBTA_MCE_MAS_DISCOVERY_COMP*)p_dest;
78 tBTA_MCE_MAS_DISCOVERY_COMP* p_src_data = (tBTA_MCE_MAS_DISCOVERY_COMP*)p_src;
79 char* p_dest_str;
80 int i;
81
82 if (!p_src) return;
83
84 if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT) return;
85
86 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
87
88 p_dest_str = p_dest + sizeof(tBTA_MCE_MAS_DISCOVERY_COMP);
89
90 for (i = 0; i < p_src_data->num_mas; i++) {
91 p_dest_data->mas[i].p_srv_name = p_dest_str;
92 memcpy(p_dest_str, p_src_data->mas[i].p_srv_name,
93 p_src_data->mas[i].srv_name_len);
94 p_dest_str += p_src_data->mas[i].srv_name_len;
95 *(p_dest_str++) = '\0';
96 }
97 }
98
mce_dm_cback(tBTA_MCE_EVT event,tBTA_MCE * p_data,void * user_data)99 static void mce_dm_cback(tBTA_MCE_EVT event, tBTA_MCE* p_data,
100 void* user_data) {
101 switch (event) {
102 case BTA_MCE_MAS_DISCOVERY_COMP_EVT: {
103 int i;
104 uint16_t param_len = sizeof(tBTA_MCE);
105
106 /* include space for all p_srv_name copies including null-termination */
107 for (i = 0; i < p_data->mas_disc_comp.num_mas; i++)
108 param_len += (p_data->mas_disc_comp.mas[i].srv_name_len + 1);
109
110 /* need to deepy copy p_srv_name and null-terminate */
111 btif_transfer_context(btif_mce_mas_discovery_comp_evt, event,
112 (char*)p_data, param_len,
113 mas_discovery_comp_copy_cb);
114
115 break;
116 }
117 }
118 }
119
init(btmce_callbacks_t * callbacks)120 static bt_status_t init(btmce_callbacks_t* callbacks) {
121 BTIF_TRACE_EVENT("%s", __func__);
122
123 bt_mce_callbacks = callbacks;
124
125 btif_enable_service(BTA_MAP_SERVICE_ID);
126
127 return BT_STATUS_SUCCESS;
128 }
129
get_remote_mas_instances(bt_bdaddr_t * bd_addr)130 static bt_status_t get_remote_mas_instances(bt_bdaddr_t* bd_addr) {
131 bdstr_t bdstr;
132
133 BTIF_TRACE_EVENT("%s: remote_addr=%s", __func__,
134 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
135
136 BTA_MceGetRemoteMasInstances(bd_addr->address);
137
138 return BT_STATUS_SUCCESS;
139 }
140
141 static const btmce_interface_t mce_if = {
142 sizeof(btmce_interface_t), init, get_remote_mas_instances,
143 };
144
btif_mce_get_interface(void)145 const btmce_interface_t* btif_mce_get_interface(void) {
146 BTIF_TRACE_EVENT("%s", __func__);
147 return &mce_if;
148 }
149
150 /*******************************************************************************
151 *
152 * Function btif_mce_execute_service
153 *
154 * Description Initializes/Shuts down the service
155 *
156 * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
157 *
158 ******************************************************************************/
btif_mce_execute_service(bool b_enable)159 bt_status_t btif_mce_execute_service(bool b_enable) {
160 BTIF_TRACE_EVENT("%s enable:%d", __func__, b_enable);
161
162 if (b_enable) {
163 BTA_MceEnable(mce_dm_cback);
164 } else {
165 /* This is called on BT disable so no need to extra cleanup */
166 }
167 return BT_STATUS_SUCCESS;
168 }
169