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