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