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
49static btmce_callbacks_t *bt_mce_callbacks = NULL;
50
51static void btif_mce_mas_discovery_comp_evt(UINT16 event, char *p_param)
52{
53    tBTA_MCE_MAS_DISCOVERY_COMP *evt_data = (tBTA_MCE_MAS_DISCOVERY_COMP*) p_param;
54    btmce_mas_instance_t insts[BTA_MCE_MAX_MAS_INSTANCES];
55    bt_bdaddr_t addr;
56    int i;
57
58    BTIF_TRACE_EVENT("%s:  event = %d", __FUNCTION__, event);
59
60    if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT)
61        return;
62
63    for (i = 0; i < evt_data->num_mas; i++)
64    {
65        insts[i].id = evt_data->mas[i].instance_id;
66        insts[i].scn = evt_data->mas[i].scn;
67        insts[i].msg_types = evt_data->mas[i].msg_type;
68        insts[i].p_name = evt_data->mas[i].p_srv_name;
69    }
70
71    bdcpy(addr.address, evt_data->remote_addr);
72
73    HAL_CBACK(bt_mce_callbacks, remote_mas_instances_cb, evt_data->status, &addr, evt_data->num_mas, insts);
74}
75
76static void mas_discovery_comp_copy_cb(UINT16 event, char *p_dest, char *p_src)
77{
78    tBTA_MCE_MAS_DISCOVERY_COMP *p_dest_data =  (tBTA_MCE_MAS_DISCOVERY_COMP *) p_dest;
79    tBTA_MCE_MAS_DISCOVERY_COMP *p_src_data =  (tBTA_MCE_MAS_DISCOVERY_COMP *) p_src;
80    char *p_dest_str;
81    int i;
82
83    if (!p_src)
84        return;
85
86    if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT)
87        return;
88
89    maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
90
91    p_dest_str = p_dest + sizeof(tBTA_MCE_MAS_DISCOVERY_COMP);
92
93    for (i = 0; i < p_src_data->num_mas; i++)
94    {
95        p_dest_data->mas[i].p_srv_name = p_dest_str;
96        memcpy(p_dest_str, p_src_data->mas[i].p_srv_name, p_src_data->mas[i].srv_name_len);
97        p_dest_str += p_src_data->mas[i].srv_name_len;
98        *(p_dest_str++) = '\0';
99    }
100}
101
102static void mce_dm_cback(tBTA_MCE_EVT event, tBTA_MCE *p_data, void *user_data)
103{
104    switch (event)
105    {
106        case BTA_MCE_MAS_DISCOVERY_COMP_EVT:
107            {
108                int i;
109                UINT16 param_len = sizeof(tBTA_MCE);
110
111                /* include space for all p_srv_name copies including null-termination */
112                for (i = 0; i < p_data->mas_disc_comp.num_mas; i++)
113                    param_len += (p_data->mas_disc_comp.mas[i].srv_name_len + 1);
114
115                /* need to deepy copy p_srv_name and null-terminate */
116                btif_transfer_context(btif_mce_mas_discovery_comp_evt, event,
117                                        (char*)p_data, param_len, mas_discovery_comp_copy_cb);
118
119                break;
120            }
121    }
122}
123
124static bt_status_t init(btmce_callbacks_t* callbacks)
125{
126    BTIF_TRACE_EVENT("%s", __FUNCTION__);
127
128    bt_mce_callbacks = callbacks;
129
130    btif_enable_service(BTA_MAP_SERVICE_ID);
131
132    return BT_STATUS_SUCCESS;
133}
134
135static bt_status_t get_remote_mas_instances(bt_bdaddr_t *bd_addr)
136{
137    bdstr_t bdstr;
138
139    BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
140
141    BTA_MceGetRemoteMasInstances(bd_addr->address);
142
143    return BT_STATUS_SUCCESS;
144}
145
146static const btmce_interface_t mce_if = {
147    sizeof(btmce_interface_t),
148    init,
149    get_remote_mas_instances,
150};
151
152const btmce_interface_t *btif_mce_get_interface(void)
153{
154    BTIF_TRACE_EVENT("%s", __FUNCTION__);
155    return &mce_if;
156}
157
158/*******************************************************************************
159**
160** Function         btif_mce_execute_service
161**
162** Description      Initializes/Shuts down the service
163**
164** Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
165**
166*******************************************************************************/
167bt_status_t btif_mce_execute_service(BOOLEAN b_enable)
168{
169    BTIF_TRACE_EVENT("%s enable:%d", __FUNCTION__, b_enable);
170
171     if (b_enable)
172     {
173         BTA_MceEnable(mce_dm_cback);
174     }
175     else
176     {
177         /* This is called on BT disable so no need to extra cleanup */
178     }
179     return BT_STATUS_SUCCESS;
180}
181