Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright 2014 The Android Open Source Project
      4  *  Copyright 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 "btif_common.h"
     41 #include "btif_util.h"
     42 
     43 /*****************************************************************************
     44  *  Static variables
     45  *****************************************************************************/
     46 
     47 static btmce_callbacks_t* bt_mce_callbacks = NULL;
     48 
     49 static void btif_mce_mas_discovery_comp_evt(uint16_t event, char* p_param) {
     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   int i;
     53 
     54   BTIF_TRACE_EVENT("%s:  event = %d", __func__, event);
     55 
     56   if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT) return;
     57 
     58   for (i = 0; i < evt_data->num_mas; i++) {
     59     insts[i].id = evt_data->mas[i].instance_id;
     60     insts[i].scn = evt_data->mas[i].scn;
     61     insts[i].msg_types = evt_data->mas[i].msg_type;
     62     insts[i].p_name = evt_data->mas[i].p_srv_name;
     63   }
     64 
     65   RawAddress addr = evt_data->remote_addr;
     66   HAL_CBACK(bt_mce_callbacks, remote_mas_instances_cb,
     67             (bt_status_t)evt_data->status, &addr, evt_data->num_mas, insts);
     68 }
     69 
     70 static void mas_discovery_comp_copy_cb(uint16_t event, char* p_dest,
     71                                        char* p_src) {
     72   tBTA_MCE_MAS_DISCOVERY_COMP* p_dest_data =
     73       (tBTA_MCE_MAS_DISCOVERY_COMP*)p_dest;
     74   tBTA_MCE_MAS_DISCOVERY_COMP* p_src_data = (tBTA_MCE_MAS_DISCOVERY_COMP*)p_src;
     75   char* p_dest_str;
     76   int i;
     77 
     78   if (!p_src) return;
     79 
     80   if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT) return;
     81 
     82   maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
     83 
     84   p_dest_str = p_dest + sizeof(tBTA_MCE_MAS_DISCOVERY_COMP);
     85 
     86   for (i = 0; i < p_src_data->num_mas; i++) {
     87     p_dest_data->mas[i].p_srv_name = p_dest_str;
     88     memcpy(p_dest_str, p_src_data->mas[i].p_srv_name,
     89            p_src_data->mas[i].srv_name_len);
     90     p_dest_str += p_src_data->mas[i].srv_name_len;
     91     *(p_dest_str++) = '\0';
     92   }
     93 }
     94 
     95 static void mce_dm_cback(tBTA_MCE_EVT event, tBTA_MCE* p_data,
     96                          void* user_data) {
     97   switch (event) {
     98     case BTA_MCE_MAS_DISCOVERY_COMP_EVT: {
     99       int i;
    100       uint16_t param_len = sizeof(tBTA_MCE);
    101 
    102       /* include space for all p_srv_name copies including null-termination */
    103       for (i = 0; i < p_data->mas_disc_comp.num_mas; i++)
    104         param_len += (p_data->mas_disc_comp.mas[i].srv_name_len + 1);
    105 
    106       /* need to deepy copy p_srv_name and null-terminate */
    107       btif_transfer_context(btif_mce_mas_discovery_comp_evt, event,
    108                             (char*)p_data, param_len,
    109                             mas_discovery_comp_copy_cb);
    110 
    111       break;
    112     }
    113   }
    114 }
    115 
    116 static bt_status_t init(btmce_callbacks_t* callbacks) {
    117   BTIF_TRACE_EVENT("%s", __func__);
    118 
    119   bt_mce_callbacks = callbacks;
    120 
    121   btif_enable_service(BTA_MAP_SERVICE_ID);
    122 
    123   return BT_STATUS_SUCCESS;
    124 }
    125 
    126 static bt_status_t get_remote_mas_instances(RawAddress* bd_addr) {
    127   VLOG(2) << __func__ << ": remote_addr=" << bd_addr;
    128 
    129   BTA_MceGetRemoteMasInstances(*bd_addr);
    130 
    131   return BT_STATUS_SUCCESS;
    132 }
    133 
    134 static const btmce_interface_t mce_if = {
    135     sizeof(btmce_interface_t), init, get_remote_mas_instances,
    136 };
    137 
    138 const btmce_interface_t* btif_mce_get_interface(void) {
    139   BTIF_TRACE_EVENT("%s", __func__);
    140   return &mce_if;
    141 }
    142 
    143 /*******************************************************************************
    144  *
    145  * Function         btif_mce_execute_service
    146  *
    147  * Description      Initializes/Shuts down the service
    148  *
    149  * Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
    150  *
    151  ******************************************************************************/
    152 bt_status_t btif_mce_execute_service(bool b_enable) {
    153   BTIF_TRACE_EVENT("%s enable:%d", __func__, b_enable);
    154 
    155   if (b_enable) {
    156     BTA_MceEnable(mce_dm_cback);
    157   } else {
    158     /* This is called on BT disable so no need to extra cleanup */
    159   }
    160   return BT_STATUS_SUCCESS;
    161 }
    162