component/bt: add bluedroid 1st version

1. add bluedroid 1st version
2. alarm adapter
3. task semaphore lock
4. other bugs resolved
This commit is contained in:
Tian Hao 2016-09-26 21:37:39 +08:00
parent 4480ab6c8c
commit b80325604d
254 changed files with 169362 additions and 6 deletions

View file

@ -0,0 +1,185 @@
/***************************************************************
* *
* * This file is for client to execute battery-related operation
* *
***************************************************************/
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "bta_api.h"
#include "bta_gatt_api.h"
#include "controller.h"
#include "gatt_int.h"
#include "bt_trace.h"
#include "btm_api.h"
#include "bt_types.h"
#include "gattc_profile.h"
#define BT_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
tBTA_GATTC_IF client_if;
tBT_UUID bas_uuid = {LEN_UUID_16, {UUID_SERVCLASS_BATTERY}};
uint16_t get_uuid16(tBT_UUID* p_uuid)
{
if(p_uuid->len == LEN_UUID_16)
{
return p_uuid->uu.uuid16;
}
else if(p_uuid->len == LEN_UUID_128)
{
UINT16 u16;
UINT8 *p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4];
STREAM_TO_UINT16(u16, p);
return u16;
}
else
{
return (UINT16)p_uuid->uu.uuid32;
}
}
/*fill a GATT ID structure*/
void bta_le_fill_16bits_gatt_id(UINT8 inst_id, UINT16 uuid, tBTA_GATT_ID* p_output)
{
p_output->inst_id = inst_id;
p_output->uuid.len = LEN_UUID_16;
p_output->uuid.uu.uuid16 = uuid;
}
/*fill a service ID structure with a 16 bits service UUID*/
void bta_le_fill_16bits_srvc_id(bool is_pri, UINT8 inst_id, UINT16 srvc_uuid, tBTA_GATT_SRVC_ID* p_output)
{
memset((void *)p_output, 0, sizeof(tBTA_GATT_SRVC_ID));
p_output->is_primary = is_pri;
bta_le_fill_16bits_gatt_id(inst_id, srvc_uuid, &p_output->id);
}
/*fill a char ID structure with a 16 bits char UUID*/
void bta_le_fill_16bits_char_id(UINT8 inst_id, UINT16 char_uuid, tBTA_GATT_ID* p_output)
{
memset((void *)p_output, 0, sizeof(tBTA_GATT_ID));
bta_le_fill_16bits_gatt_id(inst_id, char_uuid, p_output);
}
/******************************************************************************
** Function bas_gattc_callback
**
** Description battery service register callback function
*******************************************************************************/
static void bas_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data)
{
switch (event)
{
case BTA_GATTC_REG_EVT:
{
tBTA_GATT_STATUS status = p_data->reg_oper.status;
client_if = p_data->reg_oper.client_if;
LOG_ERROR("BAS register completed: event=%d, status=%d, client_if=%d\n",
event, status, client_if);
}
break;
/*connect callback*/
case BTA_GATTC_OPEN_EVT:
{
LOG_ERROR("\n%s:device is connected "BT_BD_ADDR_STR", client_if=%d, status=%d, connect_id=%d\n",
__FUNCTION__, BT_BD_ADDR_HEX(p_data->open.remote_bda), p_data->open.client_if,
p_data->open.status, p_data->open.conn_id);
/*return whether the remote device is currently connected*/
int is_connected = BTA_DmGetConnectionState(p_data->open.remote_bda);
LOG_ERROR("is_connected=%d\n",is_connected);
/*get the energy info of the controller*/
/*read battery level*/
int conn_id = p_data->open.conn_id;
/*discover service*/
BTA_GATTC_ServiceSearchRequest(conn_id, NULL);
}
break;
case BTA_GATTC_SEARCH_RES_EVT:
{
// tBTA_GATTC_SRVC_RES service_result;
LOG_ERROR("find the service,uuid=0x%x, is_primary=%d\n",
get_uuid16(&p_data->srvc_res.service_uuid.id.uuid),
p_data->srvc_res.service_uuid.is_primary);
}
break;
case BTA_GATTC_SEARCH_CMPL_EVT:
{
LOG_ERROR("search service complete, conn_id=%d,status=%d\n", p_data->search_cmpl.conn_id,
p_data->search_cmpl.status);
/*get first characteristic of battey service*/
LOG_ERROR("get first characteristic of battery service\n");
tBTA_GATT_STATUS status;
tBTA_GATT_SRVC_ID battery_srvc_id;
tBTA_GATTC_CHAR_ID out_char_id;
tGATT_CHAR_PROP out_char_prop;
bta_le_fill_16bits_srvc_id(TRUE, 0, UUID_SERVCLASS_BATTERY, &battery_srvc_id);
status = BTA_GATTC_GetFirstChar(p_data->search_cmpl.conn_id, &battery_srvc_id, NULL,
&out_char_id, &out_char_prop);
if(status == 0)
{
LOG_ERROR("the first char:srvc_id=0x%x,char_id=0x%x, property = %d\n",
get_uuid16(&out_char_id.srvc_id.id.uuid), get_uuid16(&out_char_id.char_id.uuid),
out_char_prop);
/*read battery level*/
tBTA_GATTC_CHAR_ID battery_char_id;
bta_le_fill_16bits_srvc_id(TRUE, 0, UUID_SERVCLASS_BATTERY, &battery_char_id.srvc_id);
bta_le_fill_16bits_char_id(0, GATT_UUID_BATTERY_LEVEL, &battery_char_id.char_id);
BTA_GATTC_ReadCharacteristic(p_data->search_cmpl.conn_id, &battery_char_id,
BTA_GATT_AUTH_REQ_NONE);
}
}
break;
case BTA_GATTC_READ_CHAR_EVT:
{
LOG_ERROR("\nread characteristic:connect_id=%d, status=%d\n",
p_data->read.conn_id, p_data->read.status);
LOG_ERROR("srvc_id=0x%x,char_id=0x%x,descr_type=0x%x\n",
get_uuid16(&p_data->read.srvc_id.id.uuid),
get_uuid16(&p_data->read.char_id.uuid),
get_uuid16(&p_data->read.descr_type.uuid));
if(get_uuid16(&p_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
&& p_data->read.p_value->unformat.len > 0
&& p_data->read.p_value->unformat.p_value != NULL)
{
LOG_ERROR("read the value: len=%d, value=%d\n", p_data->read.p_value->unformat.len,
*(p_data->read.p_value->unformat.p_value));
}
}
break;
default:
LOG_ERROR("unsettled event: %d\n", event);
}
}
/***************************************************************
**
** Function bac_register
**
** Description register app for battery service
**
****************************************************************/
void bac_register(void)
{
BTA_GATTC_AppRegister(&bas_uuid, bas_gattc_callback);
}

View file

@ -0,0 +1,186 @@
/**
****************************************************************************************
*
* @file bt_app.c
*
* @brief Application entry point
*
* Copyright (C) Espressif 2016
* Created by Yulong at 2016/9/9
*
*
****************************************************************************************
*/
#include "bt_app_defs.h"
/*******************************************************************************
**
** Function ESP_AppConfigadvData
**
** Description This function is called to override the BTA default ADV parameters.
**
** adv_data: Pointer to User defined ADV data structure. This
** memory space can not be freed until p_adv_data_cback
** is received.
** p_adv_data_cback: set adv data complete callback.
**
** Returns None
**
*******************************************************************************/
void ESP_AppBleConfigadvData(tESP_BLE_ADV_DATA *adv_data,
tBTA_SET_ADV_DATA_CMPL_CBACK *p_adv_data_cback)
{
tBTA_BLE_AD_MASK data_mask = 0;
if(adv_data->adv_name != NULL)
{
data_mask |= BTM_BLE_AD_BIT_DEV_NAME;
BTA_DmSetDeviceName(adv_data->adv_name);
}
if(adv_data->ble_adv_data.int_range.low != 0 ||
adv_data->ble_adv_data.int_range.hi != 0)
data_mask |= BTM_BLE_AD_BIT_INT_RANGE;
if(adv_data->ble_adv_data.p_manu != NULL)
{
data_mask |= BTM_BLE_AD_BIT_MANU;
}
if(adv_data->ble_adv_data.p_services != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE;
}
if(adv_data->ble_adv_data.p_service_32b != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_32;
}
if(adv_data->ble_adv_data.p_services_128b != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_128;
}
if(adv_data->ble_adv_data.p_sol_services != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_SOL;
}
if(adv_data->ble_adv_data.p_sol_service_32b != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_32SOL;
}
if(adv_data->ble_adv_data.p_sol_service_128b != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_128SOL;
}
if(adv_data->ble_adv_data.p_service_data != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_DATA;
}
if(adv_data->ble_adv_data.appearance != 0)
{
data_mask |= BTM_BLE_AD_BIT_APPEARANCE;
}
if(adv_data->ble_adv_data.p_proprietary != NULL)
{
data_mask |= BTM_BLE_AD_BIT_PROPRIETARY;
}
if(adv_data->ble_adv_data.tx_power != 0)
{
data_mask |= BTM_BLE_AD_BIT_TX_PWR;
}
BTA_DmBleSetAdvConfig(data_mask, &(adv_data->ble_adv_data), p_adv_data_cback);
}
/*******************************************************************************
**
** Function ESP_BleSetScanRsp
**
** Description This function is called to override the app scan response.
**
** Parameters Pointer to User defined ADV data structure
**
** Returns None
**
*******************************************************************************/
void ESP_AppBleSetScanRsp(tESP_BLE_ADV_DATA *scan_rsp_data,
tBTA_SET_ADV_DATA_CMPL_CBACK *p_scan_rsp_data_cback)
{
tBTA_BLE_AD_MASK data_mask = 0;
if(scan_rsp_data->adv_name != NULL)
{
data_mask |= BTM_BLE_AD_BIT_DEV_NAME;
BTA_DmSetDeviceName(scan_rsp_data->adv_name);
}
if(scan_rsp_data->ble_adv_data.int_range.low != 0 ||
scan_rsp_data->ble_adv_data.int_range.hi != 0)
data_mask |= BTM_BLE_AD_BIT_INT_RANGE;
if(scan_rsp_data->ble_adv_data.p_manu != NULL)
{
data_mask |= BTM_BLE_AD_BIT_MANU;
}
if(scan_rsp_data->ble_adv_data.p_services != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE;
}
if(scan_rsp_data->ble_adv_data.p_service_32b != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_32;
}
if(scan_rsp_data->ble_adv_data.p_services_128b != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_128;
}
if(scan_rsp_data->ble_adv_data.p_sol_services != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_SOL;
}
if(scan_rsp_data->ble_adv_data.p_sol_service_32b != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_32SOL;
}
if(scan_rsp_data->ble_adv_data.p_sol_service_128b != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_128SOL;
}
if(scan_rsp_data->ble_adv_data.p_service_data != NULL)
{
data_mask |= BTM_BLE_AD_BIT_SERVICE_DATA;
}
if(scan_rsp_data->ble_adv_data.appearance != 0)
{
data_mask |= BTM_BLE_AD_BIT_APPEARANCE;
}
if(scan_rsp_data->ble_adv_data.p_proprietary != NULL)
{
data_mask |= BTM_BLE_AD_BIT_PROPRIETARY;
}
if(scan_rsp_data->ble_adv_data.tx_power != 0)
{
data_mask |= BTM_BLE_AD_BIT_TX_PWR;
}
BTA_DmBleSetScanRsp(data_mask, &(scan_rsp_data->ble_adv_data), p_scan_rsp_data_cback);
}

View file

@ -0,0 +1,384 @@
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "fixed_queue.h"
#include "gki.h"
#include "bt_defs.h"
#include "bt_trace.h"
#include "bt_types.h"
#include "allocator.h"
#include "bta_api.h"
#include "bta_gatt_api.h"
#include "bt_app_common.h"
#include "controller.h"
#include "hash_map.h"
#include "hash_functions.h"
#include "alarm.h"
#include "app_button.h"
#include "button_pro.h"
#include "thread.h"
#include "bt_app_common.h"
#include "gatt_profile.h"
#include "gattc_profile.h"
#include "smp_int.h"
#include "smp_api.h"
static fixed_queue_t *bta_app1_msg_queue;
fixed_queue_t *bt_app1_general_alarm_queue;
hash_map_t *bt_app1_general_alarm_hash_map;
pthread_mutex_t bt_app1_general_alarm_lock;
static const size_t BT_APP1_GENERAL_ALARM_HASH_MAP_SIZE = 10;
xQueueHandle xBtaApp1Queue;
xTaskHandle xBtaApp1TaskHandle;
#define BT_APP1_TTYPE_MAIN_ENTRY (1)
static TIMER_LIST_ENT main_boot_tle;
tSMP_CB smp_cmd;
static void bt_app1_context_switched(void *p_msg);
static void bt_app1_send_msg(void *p_msg);
static void bt_app1_task_handler(void *arg);
static void bta_app1_msg_ready(fixed_queue_t *queue);
static void bt_app1_task_shut_down(void);
static void bt_app1_general_alarm_ready(fixed_queue_t *queue);
static void bt_app1_general_alarm_process(TIMER_LIST_ENT *p_tle);
void bt_app1_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec);
//extern void ble_test_conn(void);
//extern void bt_test_start_inquiry(void);
extern void ble_server_test(void);
static void bt_app1_task_handler(void *arg)
{
TaskEvt_t *e;
UINT8 button_msg[2] = {0x01,0x00};
for (;;) {
if (pdTRUE == xQueueReceive(xBtaApp1Queue, &e, (portTickType)portMAX_DELAY)) {
if (e->sig == 0xff) {
fixed_queue_process(bta_app1_msg_queue);
fixed_queue_process(bt_app1_general_alarm_queue);
}else if(e->sig == BUTTON_PRESS_EVT){
LOG_ERROR("button_press_event come in,button_value=%x\n",e->par);
button_msg[1] = e->par;
button_msg_notify(2,button_msg);
}
}
osi_free(e);
}
}
static void bt_app1_task_post(void)
{
TaskEvt_t *evt = (TaskEvt_t *)osi_malloc(sizeof(TaskEvt_t));
if (evt == NULL)
return;
evt->sig = 0xff;
evt->par = 0;
if (xQueueSend(xBtaApp1Queue, &evt, 10/portTICK_RATE_MS) != pdTRUE) {
ets_printf("btdm_post failed\n");
}
}
static void bta_app1_msg_ready(fixed_queue_t *queue) {
BT_HDR *p_msg;
while (!fixed_queue_is_empty(queue)) {
p_msg = (BT_HDR *)fixed_queue_dequeue(queue);
LOG_ERROR("bta_app1_msg_ready, evt: %d\n", p_msg->event);
switch (p_msg->event) {
case BT_EVT_APP1_CONTEXT_SWITCH:
bt_app1_context_switched(p_msg);
break;
default:
LOG_ERROR("unhandled BT_APP1 event (%d)\n", p_msg->event & BT_EVT_MASK);
break;
}
GKI_freebuf(p_msg);
}
}
static void bt_app1_context_switched(void *p_msg)
{
tBTAPP1_CONTEXT_SWITCH_CBACK *p = (tBTAPP1_CONTEXT_SWITCH_CBACK *) p_msg;
if (p->p_cb)
p->p_cb(p->event, p->p_param);
}
static void bt_app1_send_msg(void *p_msg)
{
if (bta_app1_msg_queue) {
fixed_queue_enqueue(bta_app1_msg_queue, p_msg);
//ke_event_set(KE_EVENT_BT_APP1_TASK);
bt_app1_task_post();
}
}
bt_status_t bt_app1_transfer_context (tBTAPP1_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTAPP1_COPY_CBACK *p_copy_cback)
{
tBTAPP1_CONTEXT_SWITCH_CBACK *p_msg;
LOG_ERROR("btapp1_transfer_context evt %d, len %d", event, param_len);
/* allocate and send message that will be executed in btif context */
if ((p_msg = (tBTAPP1_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTAPP1_CONTEXT_SWITCH_CBACK) + param_len)) != NULL)
{
p_msg->hdr.event = BT_EVT_APP1_CONTEXT_SWITCH; /* internal event */
p_msg->p_cb = p_cback;
p_msg->event = event; /* callback event */
/* check if caller has provided a copy callback to do the deep copy */
if (p_copy_cback)
{
p_copy_cback(event, p_msg->p_param, p_params);
}
else if (p_params)
{
memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
}
bt_app1_send_msg(p_msg);
return BT_STATUS_SUCCESS;
}
else
{
/* let caller deal with a failed allocation */
return BT_STATUS_NOMEM;
}
}
void bt_app1_task_start_up(void)
{
bta_app1_msg_queue = fixed_queue_new(SIZE_MAX);
if (bta_app1_msg_queue == NULL)
goto error_exit;
//ke_event_callback_set(KE_EVENT_BT_APP1_TASK, &bt_app1_task_handler);
xBtaApp1Queue = xQueueCreate(3, sizeof(void *));
xTaskCreate(bt_app1_task_handler, "BtaApp1T", 8192, NULL, configMAX_PRIORITIES - 3, xBtaApp1TaskHandle);
fixed_queue_register_dequeue(bta_app1_msg_queue, bta_app1_msg_ready);
bt_app1_general_alarm_hash_map = hash_map_new(BT_APP1_GENERAL_ALARM_HASH_MAP_SIZE,
hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
if (bt_app1_general_alarm_hash_map == NULL)
goto error_exit;
pthread_mutex_init(&bt_app1_general_alarm_lock, NULL);
bt_app1_general_alarm_queue = fixed_queue_new(SIZE_MAX);
if (bt_app1_general_alarm_queue == NULL)
goto error_exit;
fixed_queue_register_dequeue(bt_app1_general_alarm_queue, bt_app1_general_alarm_ready);
memset(&main_boot_tle, 0, sizeof(TIMER_LIST_ENT));
return;
error_exit:
LOG_ERROR("%s Unable to allocate resources for bt_app1", __func__);
bt_app1_task_shut_down();
}
static void bt_app1_task_shut_down(void)
{
fixed_queue_unregister_dequeue(bta_app1_msg_queue);
fixed_queue_free(bta_app1_msg_queue, NULL);
bta_app1_msg_queue = NULL;
// todo: hash map, pthread_mutex...
fixed_queue_unregister_dequeue(bt_app1_general_alarm_queue);
vTaskDelete(xBtaApp1TaskHandle);
vTaskDelete(xBtaApp1Queue);
}
static void bt_app1_dm_data_copy(uint16_t event, char *dst, char *src)
{
tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
if (!src_dm_sec)
return;
assert(dst_dm_sec);
memcpy(dst_dm_sec, src_dm_sec, sizeof(tBTA_DM_SEC));
if (event == BTA_DM_BLE_KEY_EVT)
{
dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
assert(src_dm_sec->ble_key.p_key_value);
assert(dst_dm_sec->ble_key.p_key_value);
memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
}
}
static void bt_app1_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
{
if (event == BTA_DM_BLE_KEY_EVT)
osi_free(dm_sec->ble_key.p_key_value);
}
static void bt_app1_dm_upstreams_evt(UINT16 event, char *p_param)
{
tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
switch (event) {
case BTA_DM_ENABLE_EVT: {
// BTA_DmSetDeviceName("ijiazu");
/*set connectable,discoverable, pairable and paired only modes of local device*/
tBTA_DM_DISC disc_mode = BTA_DM_GENERAL_DISC | BTA_DM_BLE_GENERAL_DISCOVERABLE;
tBTA_DM_CONN conn_mode = BTA_DM_CONN | BTA_DM_BLE_CONNECTABLE;
BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
/* Enable local privacy */
BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
do {
const controller_t *controller = controller_get_interface();
char bdstr[18];
bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr));
LOG_ERROR("BDA is: %s\n", bdstr);
} while (0);
#endif
}
break;
case BTA_DM_BLE_SEC_REQ_EVT:
smp_cmd.local_io_capability = 0x03; //no input no output
smp_cmd.loc_oob_flag = 0x00; //oob data not present
smp_cmd.loc_auth_req = 0x05;
smp_cmd.loc_enc_size = 0x10;
smp_cmd.local_i_key = 0x07;
smp_cmd.local_r_key = 0x07;
memcpy(smp_cmd.pairing_bda,p_data->ble_req.bd_addr,0x06);
smp_send_cmd(SMP_OPCODE_PAIRING_RSP,&smp_cmd);
smp_set_state(SMP_STATE_WAIT_CONFIRM);
//BTA_DmConfirm(p_data->ble_req.bd_addr,true);
break;
default:
break;
}
bt_app1_dm_data_free(event, p_data);
}
static void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data)
{
LOG_ERROR("bte_dm_evt: %d\n", (uint16_t)event);
bt_app1_transfer_context(bt_app1_dm_upstreams_evt, (uint16_t)event,
(void *)p_data, sizeof(tBTA_DM_SEC), bt_app1_dm_data_copy);
}
void bt_app1_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param)
{
BTA_EnableBluetooth(bte_dm_evt);
vTaskDelay(1000 / portTICK_PERIOD_MS);
bt_app1_start_timer(&main_boot_tle, BT_APP1_TTYPE_MAIN_ENTRY, 8);
}
/* Alarm timer */
static void bt_app1_general_alarm_cb(void *data) {
assert(data != NULL);
TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
fixed_queue_enqueue(bt_app1_general_alarm_queue, p_tle);
//ke_event_set(KE_EVENT_BT_APP1_TASK);
bt_app1_task_post();
}
void bt_app1_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
osi_alarm_t *alarm = NULL;
assert(p_tle != NULL);
// Get the alarm for the timer list entry.
pthread_mutex_lock(&bt_app1_general_alarm_lock);
if (!hash_map_has_key(bt_app1_general_alarm_hash_map, p_tle)) {
alarm = osi_alarm_new("bt_app1", bt_app1_general_alarm_cb, (void *)p_tle, 0);
hash_map_set(bt_app1_general_alarm_hash_map, p_tle, alarm);
}
pthread_mutex_unlock(&bt_app1_general_alarm_lock);
pthread_mutex_lock(&bt_app1_general_alarm_lock);
alarm = hash_map_get(bt_app1_general_alarm_hash_map, p_tle);
pthread_mutex_unlock(&bt_app1_general_alarm_lock);
if (alarm == NULL) {
LOG_ERROR("%s Unable to create alarm", __func__);
return;
}
osi_alarm_cancel(alarm);
p_tle->event = type;
// NOTE: This value is in seconds but stored in a ticks field.
p_tle->ticks = timeout_sec;
p_tle->in_use = TRUE;
osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
}
void bt_app1_stop_timer(TIMER_LIST_ENT *p_tle)
{
assert(p_tle != NULL);
if (p_tle->in_use == FALSE)
return;
p_tle->in_use = FALSE;
// Get the alarm for the timer list entry.
osi_alarm_t *alarm = hash_map_get(bt_app1_general_alarm_hash_map, p_tle);
if (alarm == NULL) {
LOG_WARN("%s Unable to find expected alarm in hashmap", __func__);
return;
}
osi_alarm_cancel(alarm);
}
static void bt_app1_general_alarm_process(TIMER_LIST_ENT *p_tle)
{
assert(p_tle != NULL);
LOG_ERROR("general_alarm_process\n");
switch (p_tle->event) {
case BT_APP1_TTYPE_MAIN_ENTRY:
LOG_ERROR("BT_APP1 main boot**********\n");
// ble_test_conn();
// ble_server_test();
// bt_test_start_inquiry();
gatts_server_test();
//gattc_client_test();
break;
}
}
static void bt_app1_general_alarm_ready(fixed_queue_t *queue)
{
TIMER_LIST_ENT *p_tle;
while (!fixed_queue_is_empty(queue)) {
p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue);
bt_app1_general_alarm_process(p_tle);
}
}

View file

@ -0,0 +1,225 @@
/**
*******************************************************************************
*********
*
* @file app_button.c
*
* @brief button Service Application entry point
*
* Copyright (C) ESPRESSSIF 2016
* Created by Yulong at 2016/08/24
*
*
*******************************************************************************
*********
*/
#if 0
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "rom/gpio.h"
#include "app_button.h"
#include "bt_trace.h"
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "freertos/task.h"
#include "freertos/xtensa_api.h"
#include "thread.h"
#include "allocator.h"
#include "button_pro.h"
#define GPIO_INUM 8
#define TABLE_ELEMENT_CNT(table) ((sizeof(table))/(sizeof(table[0])));
app_key_env key_press;
uint8_t gpio_test_table[]={0,2,4,5,12,13,14,15,16,17,18,19,20,21,22,23,25,26,27,33,34,35,36,37,38,39};
struct gpio_test_info{
uint8_t *gpio_test_table;
uint8_t gpio_test_num;
void *time_s;
};
static void gpio_irq_init(uint64_t gpio_num);
void gpio_check_register(enum_gpio_num_t gpio_num)
{
if(gpio_num>=GPIO_PIN_COUNT||0==GPIO_PIN_MUX_REG[gpio_num]){
ets_printf("io_num=%d not exits\n",gpio_num);
return;
}
LOG_ERROR("---------gpio_num %d reg----------\n",gpio_num);
LOG_ERROR("GPIO_IOMUX_%d=0x%08x\n",gpio_num,READ_PERI_REG(GPIO_PIN_MUX_REG[gpio_num]));
LOG_ERROR("GPIO_PIN%d_ADDR=0x%08x\n",gpio_num,READ_PERI_REG(GPIO_PIN_ADDR(gpio_num)));
LOG_ERROR("GPIO_OUT_REG=0x%08x\n",READ_PERI_REG(GPIO_OUT_REG));
LOG_ERROR("GPIO_OUT1_REG=0x%08x\n",READ_PERI_REG(GPIO_OUT1_REG));
LOG_ERROR("GPIO_ENABLE_REG=0x%08x\n",READ_PERI_REG(GPIO_ENABLE_REG));
LOG_ERROR("GPIO_ENABLE1_REG=0x%08x\n",READ_PERI_REG(GPIO_ENABLE1_REG));
LOG_ERROR("GPIO_IN_REG=0x%08x\n",READ_PERI_REG(GPIO_IN_REG));
LOG_ERROR("GPIO_IN1_REG=0x%08x\n",READ_PERI_REG(GPIO_IN1_REG));
LOG_ERROR("GPIO_STATUS_REG=0x%08x\n",READ_PERI_REG(GPIO_STATUS_REG));
LOG_ERROR("GPIO_STATUS1_REG=0x%08x\n",READ_PERI_REG(GPIO_STATUS1_REG));
}
void t1_callback(void *arg)
{
static uint8_t level=0;
static uint8_t cnt=0;
uint8_t err_flag=0;
struct gpio_test_info *gpio_test=(struct gpio_test_info*)(arg);
uint8_t i=0;
while(1){
gpio_check_register(35);
vTaskDelay(2*1000);
/*
level=~level;
LOG_ERROR("Test cnt %u, level %u\n",cnt+1,level&0x01);
for(i=0;i<gpio_test->gpio_test_num;i++){
gpio_set_output_level(gpio_test->gpio_test_table[i],level&0x01);
if(gpio_get_input_level(gpio_test->gpio_test_table[i])!=(level&0x01))
{
err_flag=1;
LOG_ERROR("[ERR] GPIO%u set_level %u get_level %u\n",gpio_test->gpio_test_table[i],level&0x01,gpio_get_input_level(gpio_test->gpio_test_table[i]));
}
else{
LOG_ERROR("GPIO%u OK\n",gpio_test->gpio_test_table[i]);
}
}
cnt++;
if(err_flag==0){
LOG_ERROR("cnt %u test ok\n",cnt);
}
err_flag=0;
if(cnt>=10){
LOG_ERROR("Gpio input and output test end\n");
vTaskDelete(NULL);*/
// }
//vTaskDelay(2*1000);
}
}
void app_button_init(void)
{
uint64_t gpio_num = GPIO_Pin_27|GPIO_Pin_35|GPIO_Pin_34|GPIO_Pin_36|GPIO_Pin_39;
// Reset environment
memset(&key_press, 0, sizeof(key_press));
gpio_irq_init(gpio_num);
static struct gpio_test_info gpio_test_infor;
LOG_ERROR("app_button_init.");
/* TimerHandle_t t1=NULL;
t1=xTimerCreate("t1_time",(1000/portTICK_PERIOD_MS),pdTRUE,&gpio_test_infor,t1_callback);
do{
gpio_test_infor.gpio_test_table=gpio_test_table;
gpio_test_infor.gpio_test_num=TABLE_ELEMENT_CNT(gpio_test_table);
gpio_test_infor.time_s=t1;
}while(0);*/
// xTaskCreate(t1_callback,"t1_callback",1024,&gpio_test_infor,30,NULL);
LOG_ERROR("gpio_input_output_demo\n");
return;
}
static void gpio_irq_init(uint64_t gpio_num)
{
gpio_config_t gpio_config_prot;
memset(&gpio_config_prot,0,sizeof(gpio_config_prot));
gpio_config_prot.GPIO_Pin= gpio_num;
gpio_config_prot.GPIO_Mode=GPIO_Mode_Input;
gpio_config_prot.GPIO_IntrType=GPIO_PIN_INTR_NEGEDGE;
gpio_config_prot.GPIO_Pullup=GPIO_PullUp_EN;
gpio_config_prot.GPIO_Pulldown=GPIO_PullDown_DIS;
gpio_config(&gpio_config_prot);
//Register gpio handler
gpio_intr_handler_register(GPIO_isr_callback,NULL);
//Enable gpio intr
xt_ints_on(1<<GPIO_INUM);
//ETS_GPIO_INTR_ENABLE(); //Enable intr
}
void GPIO_isr_callback(void* arg)
{
/*GPIO interrupt process*/
uint32_t gpio_intr_status = 0;
uint32_t gpio_intr_status_h = 0;
uint32_t gpio_num =0;
//disable the pending interrupt
xt_ints_off(1<<GPIO_INUM);
gpio_intr_status = READ_PERI_REG(GPIO_STATUS_REG);
gpio_intr_status_h = READ_PERI_REG(GPIO_STATUS1_REG);
LOG_ERROR("the interrupt come in,gpio_intr_staus = %d,gpio_intr_status_h=%d\n",gpio_intr_status,gpio_intr_status_h);
//clear intr for gpio0-gpio31
WRITE_PERI_REG((GPIO_STATUS_W1TC_REG),(READ_PERI_REG(GPIO_STATUS_W1TC_REG)|(gpio_intr_status)));
//clear intr for gpio32-39
WRITE_PERI_REG((GPIO_STATUS1_W1TC_REG),(READ_PERI_REG(GPIO_STATUS1_W1TC_REG)|(gpio_intr_status_h)));
do{
if(gpio_num < 32){
if(gpio_intr_status&BIT(gpio_num))
{
LOG_ERROR("Intr GPIO= %d\n",gpio_num);
break;
}
}
else{
if(gpio_intr_status_h&BIT(gpio_num - 32)){
LOG_ERROR("Intr GPIO = %d\n",gpio_num);
break;
}
}
}while(++gpio_num<GPIO_PIN_COUNT);
TaskEvt_t *evt = (TaskEvt_t *)osi_malloc(sizeof(TaskEvt_t));
if(evt == NULL)
{
return;
}
switch(gpio_num)
{
case GPIO_NUM_35:
evt->par = Button_Voice;
break;
case GPIO_NUM_34:
evt->par = Button_OK;
break;
case GPIO_NUM_39:
evt->par = Button_Down;
break;
case GPIO_NUM_36:
evt->par = Button_Up;
break;
default:
evt->par = Button_Back;
break;
}
evt->sig = BUTTON_PRESS_EVT;
if(xQueueSend(xBtaApp1Queue,&evt,10/portTICK_RATE_MS)!=pdTRUE){
LOG_ERROR("btdm_post_failed\n");
}
//enable the interrupt
xt_ints_on(1<<GPIO_INUM);
LOG_ERROR("the interrupt come in arg = %s\n",arg);
}
uint8_t check_sum(uint8_t *check_array,uint8_t len)
{
uint8_t i,sum = 0;
for(i = 0;i < len; i++)
{
sum ^= check_array[i];
}
sum += 1;
return sum;
}
#endif

View file

@ -0,0 +1,247 @@
/***************************************************************
*
* This file is for gatt server device. It instantiates BATTERY
* sevice. It can be scanned and connected by central device,
* and the client will get the BAS value. It calls the API bta
* layer provides.
*
****************************************************************/
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "bta_api.h"
#include "bta_gatt_api.h"
#include "controller.h"
#include "gatt_int.h"
#include "bt_trace.h"
#include "btm_api.h"
#include "bt_types.h"
#include "gatt_profile.h"
#include "bt_app_common.h"
//#include "app_button.h"
//#include "button_pro.h"
#include "hid_le_prf.h"
#include "prf_defs.h"
#include "hcimsgs.h"
#include "bt_app_defs.h"
#define BT_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
tBTA_GATTS_IF server_if;
static unsigned char DIS_UUID[16] = {
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
0x00, 0x10, 0x00, 0x00, 0x0a, 0x18, 0x00, 0x00
};
static unsigned char BASE_UUID[16] = {
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
UINT16 ijiazu_uuid = 0xffff;
tBTA_BLE_SERVICE ijiazu_service = {
0x01, //only one service in the ijiazu button profile
false,
&ijiazu_uuid
}; /* 16 bits services */
UINT8 beacon_manu[25] = {0x4c, 0x00,0x02, 0x15, 0xfd, 0xa5, 0x06, 0x93, 0xa4, 0xe2,
0x4f, 0xb1, 0xaf, 0xcf, 0xc6, 0xeb, 0x07, 0x64, 0x78, 0x25,
0x27, 0x32, 0xe6, 0x08, 0xc5};
//UINT8 ijiazu_manu[17] = {0xff,0x20,0x14,0x07,0x22,0x00,0x02,0x5B,0x00,0x33,0x49,0x31,0x30,0x4a,0x30,0x30,0x31};
UINT8 ijiazu_manu[17] = {0xff,0x20,0x14,0x07,0x22,0x00,0x02,0x5B,0x00,0x33,0x49,0x31,0x30,0x4a,0x30,0x30,0x31};
tBTA_BLE_MANU p_ijiazu_manu = {sizeof(ijiazu_manu),ijiazu_manu}; /* manufacturer data */
BD_ADDR rand_ijiazu_addr = {0x00,0x02,0x5B,0x00,0x32,0x55};
tESP_BLE_ADV_DATA ijiazu_adv_data[ADV_SCAN_IDX_MAX] =
{
[BLE_ADV_DATA_IDX] = {
.adv_name = "esp_server",
{
{0,0},
NULL, //no manufature data to be setting in the ijiazu adervetisiing datas
&ijiazu_service,
NULL, //the 128 bits service uuid set to null(not used)
NULL, //the 32 bits Service UUID set to null(not used)
NULL, //16 bits services Solicitation UUIDs set to null(not used)
NULL, //List of 32 bit Service Solicitation UUIDs set to null(not used)
NULL, //List of 128 bit Service Solicitation UUIDs set to null(not used)
NULL, //proprietary data set to null(not used)
NULL, //service data set not null(no service data to be sent)
0x0200, //device type : generic display
BTA_DM_GENERAL_DISC, // General discoverable.
0xFE //the tx power value,defult value is 0
},
},
[BLE_SCAN_RSP_DATA_IDX] = {
.adv_name = NULL,
{
{0,0},
&p_ijiazu_manu,
NULL,
NULL, //the 128 bits service uuid set to null(not used)
NULL, //the 32 bits Service UUID set to null(not used)
NULL, //16 bits services Solicitation UUIDs set to null(not used)
NULL, //List of 32 bit Service Solicitation UUIDs set to null(not used)
NULL, //List of 128 bit Service Solicitation UUIDs set to null(not used)
NULL, //proprietary data set to null(not used)
NULL, //service data set not null(no service data to be sent)
0x0000, //device type : generic display
0x00, // General discoverable.
0x00}, //the tx power value,defult value is 0
}
};
typedef struct {
uint8_t uu[16];
} bt_uuid_t;
int uuidType(unsigned char* p_uuid)
{
int i = 0;
int match = 0;
int all_zero = 1;
for(i = 0; i != 16; ++i)
{
if (i == 12 || i == 13)
continue;
if (p_uuid[i] == BASE_UUID[i])
++match;
if (p_uuid[i] != 0)
all_zero = 0;
}
if (all_zero)
return 0;
if (match == 12)
return LEN_UUID_32;
if (match == 14)
return LEN_UUID_16;
return LEN_UUID_128;
}
/*16-bits uuid to the structure of holding any type of UUID*/
void btif_to_bta_uuid(tBT_UUID *p_dest, bt_uuid_t *p_src)
{
char *p_byte = (char*)p_src;
int i = 0;
p_dest->len = uuidType(p_src->uu);
switch (p_dest->len)
{
case LEN_UUID_16:
p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12];
break;
case LEN_UUID_32:
p_dest->uu.uuid32 = (p_src->uu[13] << 8) + p_src->uu[12];
p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16);
break;
case LEN_UUID_128:
for(i = 0; i != 16; ++i)
p_dest->uu.uuid128[i] = p_byte[i];
break;
default:
LOG_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_dest->len);
break;
}
}
/*set advertising config callback*/
static void bta_gatts_set_adv_data_cback(tBTA_STATUS call_status)
{
LOG_ERROR("set advertising config:status=%d\n", call_status);
/*dis init*/
/* tDIS_ATTR_MASK dis_attr_mask;
dis_attr_mask = DIS_ATTR_SYS_ID_BIT | DIS_ATTR_MODEL_NUM_BIT | DIS_ATTR_SERIAL_NUM_BIT |
DIS_ATTR_FW_NUM_BIT | DIS_ATTR_HW_NUM_BIT | DIS_ATTR_SW_NUM_BIT | DIS_ATTR_MANU_NAME_BIT |
DIS_ATTR_IEEE_DATA_BIT | DIS_ATTR_PNP_ID_BIT;
DIS_SrInit(dis_attr_mask);
*/
/*instantiate a battery service*/
bas_register();
/*instantiate the driver for button profile*/
//app_button_init();
/*instantiate a button service*/
//button_init();
/*instantiate a hid device service*/
hidd_le_init();
/*start advetising*/
// BTA_GATTS_Listen(server_if, true, NULL);
}
/*register callback*/
void bta_gatts_callback(tBTA_GATTS_EVT event, tBTA_GATTS* p_data)
{
switch (event)
{
case BTA_GATTS_REG_EVT:
{
tBTA_GATT_STATUS status = p_data->reg_oper.status;
server_if = p_data->reg_oper.server_if;
LOG_ERROR("register complete: event=%d, status=%d, server_if=%d\n",
event, status, server_if);
LOG_ERROR("set advertising parameters\n");
//set the advertising data to the btm layer
ESP_AppBleConfigadvData(&ijiazu_adv_data[BLE_ADV_DATA_IDX],
bta_gatts_set_adv_data_cback);
//set the adversting data to the btm layer
ESP_AppBleSetScanRsp(&ijiazu_adv_data[BLE_SCAN_RSP_DATA_IDX],NULL);
}
break;
/*connect callback*/
case BTA_GATTS_CONNECT_EVT:
{
LOG_ERROR("\ndevice is connected "BT_BD_ADDR_STR", server_if=%d,reason=0x%x,connect_id=%d\n",
BT_BD_ADDR_HEX(p_data->conn.remote_bda), p_data->conn.server_if,
p_data->conn.reason, p_data->conn.conn_id);
/*return whether the remote device is currently connected*/
int is_connected = BTA_DmGetConnectionState(p_data->conn.remote_bda);
LOG_ERROR("is_connected=%d\n",is_connected);
}
break;
default:
LOG_ERROR("unsettled event: %d\n", event);
}
}
static void ble_server_appRegister(void)
{
bt_uuid_t uuid;
tBT_UUID t_uuid;
memcpy(&uuid, BASE_UUID, sizeof(bt_uuid_t));
//memcpy(&uuid, DIS_UUID, sizeof(bt_uuid_t));
btif_to_bta_uuid(&t_uuid, &uuid);
LOG_ERROR("register gatts application\n");
BTA_GATTS_AppRegister(&t_uuid, bta_gatts_callback);
}
void gatts_server_test(void)
{
BTM_SetTraceLevel(BT_TRACE_LEVEL_DEBUG);
ble_server_appRegister();
}

View file

@ -0,0 +1,381 @@
/****************************************************************************
*
* This file is for gatt client. It can scan ble device, connect one device,
*
****************************************************************************/
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "bta_api.h"
#include "bta_gatt_api.h"
#include "controller.h"
#include "bt_trace.h"
#include "btm_api.h"
#include "bt_types.h"
#include "gattc_profile.h"
#define BT_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
tBTA_GATTC_IF client_if;
BD_ADDR obj_addr;
static unsigned char BASE_UUID[16] = {
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
typedef struct {
uint8_t uu[16];
} bt_uuid_t;
int uuidType(unsigned char* p_uuid)
{
int i = 0;
int match = 0;
int all_zero = 1;
for(i = 0; i != 16; ++i)
{
if (i == 12 || i == 13)
continue;
if (p_uuid[i] == BASE_UUID[i])
++match;
if (p_uuid[i] != 0)
all_zero = 0;
}
if (all_zero)
return 0;
if (match == 12)
return LEN_UUID_32;
if (match == 14)
return LEN_UUID_16;
return LEN_UUID_128;
}
static void btif_to_bta_uuid(tBT_UUID *p_dest, bt_uuid_t *p_src)
{
char *p_byte = (char*)p_src;
int i = 0;
p_dest->len = uuidType(p_src->uu);
switch (p_dest->len)
{
case LEN_UUID_16:
p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12];
break;
case LEN_UUID_32:
p_dest->uu.uuid32 = (p_src->uu[13] << 8) + p_src->uu[12];
p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16);
break;
case LEN_UUID_128:
for(i = 0; i != 16; ++i)
p_dest->uu.uuid128[i] = p_byte[i];
break;
default:
LOG_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_dest->len);
break;
}
}
/*
uint16_t get_uuid16(tBT_UUID* p_uuid)
{
if(p_uuid->len == LEN_UUID_16)
{
return p_uuid->uu.uuid16;
}
else if(p_uuid->len == LEN_UUID_128)
{
UINT16 u16;
UINT8 *p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4];
STREAM_TO_UINT16(u16, p);
return u16;
}
else
{
return (UINT16)p_uuid->uu.uuid32;
}
}
//fill a GATT ID structure
void bta_le_fill_16bits_gatt_id(UINT8 inst_id, UINT16 uuid, tBTA_GATT_ID* p_output)
{
p_output->inst_id = inst_id;
p_output->uuid.len = LEN_UUID_16;
p_output->uuid.uu.uuid16 = uuid;
}
//fill a service ID structure with a 16 bits service UUID
void bta_le_fill_16bits_srvc_id(bool is_pri, UINT8 inst_id, UINT16 srvc_uuid, tBTA_GATT_SRVC_ID* p_output)
{
memset((void *)p_output, 0, sizeof(tBTA_GATT_SRVC_ID));
p_output->is_primary = is_pri;
bta_le_fill_16bits_gatt_id(inst_id, srvc_uuid, &p_output->id);
}
//fill a char ID structure with a 16 bits char UUID
void bta_le_fill_16bits_char_id(UINT8 inst_id, UINT16 char_uuid, tBTA_GATT_ID* p_output)
{
memset((void *)p_output, 0, sizeof(tBTA_GATT_ID));
bta_le_fill_16bits_gatt_id(inst_id, char_uuid, p_output);
}
*/
/*get remote name*/
static bool check_remote_name(tBTA_DM_INQ_RES* result, uint8_t* rmt_name, uint8_t* rmt_name_len)
{
uint8_t *p_rmt_name = NULL;
uint8_t remote_name_len = 0;
if (result->p_eir) {
p_rmt_name = BTM_CheckEirData(result->p_eir,
BTM_EIR_COMPLETE_LOCAL_NAME_TYPE,
&remote_name_len);
if (!p_rmt_name)
p_rmt_name = BTM_CheckEirData(result->p_eir,
BTM_EIR_SHORTENED_LOCAL_NAME_TYPE,
&remote_name_len);
if (p_rmt_name) {
if (remote_name_len > BD_NAME_LEN)
remote_name_len = BD_NAME_LEN;
if (rmt_name && rmt_name_len) {
memcpy(rmt_name, p_rmt_name, remote_name_len);
*(rmt_name + remote_name_len) = 0;
*rmt_name_len = remote_name_len;
}
return true;
}
}
return false;
}
/************************************************************************************
* * Function bta_scan_recult_callback
* *
* * Description scan result.it will be called when device scaned a peer device
* *
* * Return NULL
**************************************************************************************/
static void bta_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH* p_data)
{
uint8_t len;
BD_ADDR bd_addr;
char dev_name[32];
tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
//char obj_name[] = "Find Me";
char obj_name[] = "SimpleBLEPeripheral";
uint8_t dev_name_len;
switch (event)
{
case BTA_DM_INQ_RES_EVT:
{
LOG_ERROR("scan result: event=%d, "BT_BD_ADDR_STR", device_type=%d\n",
event, BT_BD_ADDR_HEX(p_data->inq_res.bd_addr), p_data->inq_res.device_type);
bdcpy(bd_addr, p_data->inq_res.bd_addr);
if (p_data->inq_res.p_eir)
{
if (BTM_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &len))
{
p_data->inq_res.remt_name_not_required = TRUE;
}
}
if(check_remote_name(&(p_data->inq_res), dev_name, &dev_name_len))
{
LOG_ERROR("scan device name len=%d, name = %s\n", dev_name_len, dev_name);
}
if(strcmp(dev_name, obj_name) == 0)
{
bdcpy(obj_addr, bd_addr);
LOG_ERROR("find the device, obj_addr="BT_BD_ADDR_STR"\n", BT_BD_ADDR_HEX(obj_addr));
// BTA_GATTC_Open(client_if, obj_addr, true, transport);
}
}
break;
case BTA_DM_INQ_CMPL_EVT:
{
LOG_ERROR("%s-BLE observe complete. Num Resp %d\n", __FUNCTION__, p_data->inq_cmpl.num_resps);
LOG_ERROR("connect the device "BT_BD_ADDR_STR", client_if=%d\n",
BT_BD_ADDR_HEX(obj_addr), client_if);
/* scan complete, start connect*/
BTA_GATTC_Open(client_if, obj_addr, true, transport);
}
break;
default:
LOG_ERROR("%s : unknown event 0x%x", __FUNCTION__, event);
}
}
/************************************************************************************
* * Function bta_scan_param_setup_cback
* *
* * Description set scan param callback.it will be called after setting scan parameter
* *
* * Return NULL
**************************************************************************************/
static void bta_scan_param_setup_cback(tGATT_IF c_client_if, tBTM_STATUS status)
{
client_if = c_client_if;
LOG_ERROR("\nset scan params complete: status=%d, client_if=%d\n", status, client_if);
/*start scan*/
BTA_DmBleObserve(true, 8, bta_scan_result_callback);
}
/************************************************************************************
* * Function bta_gattc_callback
* *
* * Description app register callback
* *
* * Return NULL
**************************************************************************************/
static void bta_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data)
{
switch (event)
{
case BTA_GATTC_REG_EVT:
{
tBTA_GATT_STATUS status = p_data->reg_oper.status;
client_if = p_data->reg_oper.client_if;
LOG_ERROR("%s:register complete: event=%d, status=%d, client_if=%d\n", __FUNCTION__, event, status, client_if);
UINT8 scan_interval = 0x50;
UINT8 scan_window = 0x30;
tBLE_SCAN_MODE scan_mode = BTM_BLE_SCAN_MODE_ACTI;
bac_register();
/*register complete,set scan parameter*/
BTA_DmSetBleScanParams(client_if, scan_interval, scan_window, scan_mode,
bta_scan_param_setup_cback);
}
break;
/*connect callback*/
case BTA_GATTC_OPEN_EVT:
{
LOG_ERROR("\n%s:device is connected "BT_BD_ADDR_STR", client_if=%d, status=%d, connect_id=%d\n",
__FUNCTION__, BT_BD_ADDR_HEX(p_data->open.remote_bda), p_data->open.client_if,
p_data->open.status, p_data->open.conn_id);
/*return whether the remote device is currently connected*/
int is_connected = BTA_DmGetConnectionState(p_data->open.remote_bda);
LOG_ERROR("is_connected=%d\n",is_connected);
/*get the energy info of the controller*/
/*read battery level*/
int conn_id = p_data->open.conn_id;
/*discover service*/
// BTA_GATTC_ServiceSearchRequest(conn_id, NULL);
}
break;
/*
case BTA_GATTC_SEARCH_RES_EVT:
{
// tBTA_GATTC_SRVC_RES service_result;
LOG_ERROR("find the service,uuid=0x%x, is_primary=%d\n",
get_uuid16(&p_data->srvc_res.service_uuid.id.uuid),
p_data->srvc_res.service_uuid.is_primary);
}
break;
case BTA_GATTC_SEARCH_CMPL_EVT:
{
LOG_ERROR("search service complete, conn_id=%d,status=%d\n", p_data->search_cmpl.conn_id,
p_data->search_cmpl.status);
//get first characteristic of battey service
LOG_ERROR("get first characteristic of battery service\n");
tBTA_GATT_STATUS status;
tBTA_GATT_SRVC_ID battery_srvc_id;
tBTA_GATTC_CHAR_ID out_char_id;
tGATT_CHAR_PROP out_char_prop;
bta_le_fill_16bits_srvc_id(TRUE, 0, UUID_SERVCLASS_BATTERY, &battery_srvc_id);
status = BTA_GATTC_GetFirstChar(p_data->search_cmpl.conn_id, &battery_srvc_id, NULL,
&out_char_id, &out_char_prop);
if(status == 0)
{
LOG_ERROR("the first char:srvc_id=0x%x,char_id=0x%x, property = %d\n",
get_uuid16(&out_char_id.srvc_id.id.uuid), get_uuid16(&out_char_id.char_id.uuid),
out_char_prop);
//read battery level
tBTA_GATTC_CHAR_ID battery_char_id;
bta_le_fill_16bits_srvc_id(TRUE, 0, UUID_SERVCLASS_BATTERY, &battery_char_id.srvc_id);
bta_le_fill_16bits_char_id(0, GATT_UUID_BATTERY_LEVEL, &battery_char_id.char_id);
BTA_GATTC_ReadCharacteristic(p_data->search_cmpl.conn_id, &battery_char_id,
BTA_GATT_AUTH_REQ_NONE);
}
}
break;
case BTA_GATTC_READ_CHAR_EVT:
{
LOG_ERROR("\nread characteristic:connect_id=%d, status=%d\n",
p_data->read.conn_id, p_data->read.status);
LOG_ERROR("srvc_id=0x%x,char_id=0x%x,descr_type=0x%x\n",
get_uuid16(&p_data->read.srvc_id.id.uuid),
get_uuid16(&p_data->read.char_id.uuid),
get_uuid16(&p_data->read.descr_type.uuid));
if(get_uuid16(&p_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
&& p_data->read.p_value->unformat.len > 0
&& p_data->read.p_value->unformat.p_value != NULL)
{
LOG_ERROR("read the value: len=%d, value=%d\n", p_data->read.p_value->unformat.len,
*(p_data->read.p_value->unformat.p_value));
}
}
break;
*/
default:
LOG_ERROR("%s:unknown event: %d\n", __FUNCTION__, event);
}
}
/************************************************************************************
* * Function ble_client_appRegister
* *
* * Description app register function
* *
* * Return NULL
**************************************************************************************/
void ble_client_appRegister(void)
{
bt_uuid_t uuid;
tBT_UUID t_uuid;
memcpy(&uuid, BASE_UUID, sizeof(bt_uuid_t));
btif_to_bta_uuid(&t_uuid, &uuid);
LOG_ERROR("register application\n");
BTA_GATTC_AppRegister(&t_uuid, bta_gattc_callback);
/*battery service register*/
// bac_register();
}
void gattc_client_test(void)
{
BTM_SetTraceLevel(BT_TRACE_LEVEL_DEBUG);
ble_client_appRegister();
}

View file

@ -0,0 +1,62 @@
/**
****************************************************************************************
*
* @file app_button.h
*
* @brief button Service Application entry point
*
* Copyright (C) ESPRESSIF 2016
* Created by Yulong at 2016/02/24
*
*
****************************************************************************************
*/
/*
* DEFINES
****************************************************************************************
*/
#define BUTTON_HEAD (0x01)
#define BUTTON_PRESS_EVT (0x10)
//the key value enum
enum
{
Button_Up = 0x01,
Button_Voice = 0x02,
Button_OK = 0x04,
Button_Down = 0x08,
Button_Back = 0x10,
};
typedef struct {
uint8_t key_val; //button val
uint8_t head; //the head of the frame
}key_frame;
typedef struct
{
bool button_press;
key_frame key_msg;
}app_key_env;
extern app_key_env key_press;
/*
* FUNCTIONS DECLARATION
****************************************************************************************
*/
void app_button_init(void);
void GPIO_isr_callback(void* arg);
uint8_t check_sum(uint8_t *check_array,uint8_t len);

View file

@ -0,0 +1,46 @@
#ifndef __BT_APP_COMMON_H__
#define __BT_APP_COMMON_H__
#include <stdint.h>
#include "osi.h"
/** Bluetooth Error Status */
/** originally defined in bluetooth.h */
typedef enum {
BT_STATUS_SUCCESS,
BT_STATUS_FAIL,
BT_STATUS_NOT_READY,
BT_STATUS_NOMEM,
BT_STATUS_BUSY,
BT_STATUS_DONE,
BT_STATUS_UNSUPPORTED,
BT_STATUS_PARAM_INVALID,
BT_STATUS_UNHANDLED,
BT_STATUS_AUTH_FAILURE,
BT_STATUS_RMT_DEV_DOWN
} bt_status_t;
/* BT APP1 Events */
#define BT_EVT_APP1 (0xB000)
#define BT_EVT_APP1_CONTEXT_SWITCH (0x0001 | BT_EVT_APP1)
typedef void (tBTAPP1_CBACK) (UINT16 event, char *p_param);
typedef void (tBTAPP1_COPY_CBACK) (UINT16 event, char *p_dest, char *p_src);
typedef struct
{
BT_HDR hdr;
tBTAPP1_CBACK* p_cb; /* context switch callback */
/* parameters passed to callback */
UINT16 event; /* message event id */
char p_param[0]; /* parameter area needs to be last */
} tBTAPP1_CONTEXT_SWITCH_CBACK;
bt_status_t bt_app1_transfer_context (tBTAPP1_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTAPP1_COPY_CBACK *p_copy_cback);
void bt_app1_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
void bt_app1_task_start_up(void);
#endif /* __BT_APP_COMMON_H__ */

View file

@ -0,0 +1,23 @@
#include "bta_api.h"
#include "btm_ble_api.h"
enum
{
BLE_ADV_DATA_IDX,
BLE_SCAN_RSP_DATA_IDX,
ADV_SCAN_IDX_MAX
};
typedef struct
{
char *adv_name; //set the device name to be sent on the advertising
tBTA_BLE_ADV_DATA ble_adv_data;
}tESP_BLE_ADV_DATA;
extern void ESP_AppBleConfigadvData(tESP_BLE_ADV_DATA *adv_data,
tBTA_SET_ADV_DATA_CMPL_CBACK *p_adv_data_cback);
extern void ESP_AppBleSetScanRsp(tESP_BLE_ADV_DATA *scan_rsp_data,
tBTA_SET_ADV_DATA_CMPL_CBACK *p_scan_rsp_data_cback);

View file

@ -0,0 +1,46 @@
/******************************************************************************
*
* Copyright (C) 1999-2013 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/*******************************************************************************
**
** Header file for gatt client.
**
********************************************************************************/
#include "bt_target.h"
#include "gatt_api.h"
#include "gattdefs.h"
/*******************************************************************************
** BATTERY CLIENT API
*******************************************************************************/
/***************************************************************
**
** Function bac_register
**
** Description register app for battery service
**
****************************************************************/
extern void bac_register(void);
extern void gattc_client_test(void);
#ifdef __cplusplus
}
#endif

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,587 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains compile-time configurable constants for the device
* manager.
*
******************************************************************************/
#include <stddef.h>
#include "bt_target.h"
#include "bta_sys.h"
#include "bta_api.h"
#include "bta_dm_int.h"
// #include "bta_jv_api.h"
#ifndef BTA_DM_LINK_POLICY_SETTINGS
#define BTA_DM_LINK_POLICY_SETTINGS (HCI_ENABLE_MASTER_SLAVE_SWITCH | HCI_ENABLE_HOLD_MODE | HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)
#endif
/* page timeout in 625uS */
#ifndef BTA_DM_PAGE_TIMEOUT
#define BTA_DM_PAGE_TIMEOUT 8192
#endif
/* link supervision timeout in 625uS (5 secs) */
#ifndef BTA_DM_LINK_TIMEOUT
#define BTA_DM_LINK_TIMEOUT 8000
#endif
/* TRUE to avoid scatternet when av is streaming (be the master) */
#ifndef BTA_DM_AVOID_SCATTER_A2DP
#define BTA_DM_AVOID_SCATTER_A2DP TRUE
#endif
/* For Insight, PM cfg lookup tables are runtime configurable (to allow tweaking of params for power consumption measurements) */
#ifndef BTE_SIM_APP
#define tBTA_DM_PM_TYPE_QUALIFIER const
#else
#define tBTA_DM_PM_TYPE_QUALIFIER
#endif
const tBTA_DM_CFG bta_dm_cfg =
{
/* mobile phone COD */
BTA_DM_COD,
/* link policy settings */
BTA_DM_LINK_POLICY_SETTINGS,
/* page timeout in 625uS */
BTA_DM_PAGE_TIMEOUT,
/* link supervision timeout in 625uS*/
BTA_DM_LINK_TIMEOUT,
/* TRUE to avoid scatternet when av is streaming (be the master) */
BTA_DM_AVOID_SCATTER_A2DP
};
#ifndef BTA_DM_SCATTERNET
/* By default, allow partial scatternet */
#define BTA_DM_SCATTERNET BTA_DM_PARTIAL_SCATTERNET
#endif
#ifndef BTA_HH_ROLE
/* By default, do not specify HH role (backward compatibility) */
#define BTA_HH_ROLE BTA_ANY_ROLE
#endif
#ifndef BTA_AV_ROLE
/* By default, AV role (backward BTA_MASTER_ROLE_PREF) */
#define BTA_AV_ROLE BTA_MASTER_ROLE_PREF
#endif
#ifndef BTA_PANU_ROLE
/* By default, AV role (backward BTA_MASTER_ROLE_PREF) */
#define BTA_PANU_ROLE BTA_SLAVE_ROLE_ONLY
#endif
#define BTA_DM_NUM_RM_ENTRY 6
/* appids for PAN used by insight sample application
these have to be same as defined in btui_int.h */
#define BTUI_PAN_ID_PANU 0
#define BTUI_PAN_ID_NAP 1
#define BTUI_PAN_ID_GN 2
/* First element is always for SYS:
app_id = # of entries table, cfg is
device scatternet support */
const tBTA_DM_RM bta_dm_rm_cfg[] =
{
{BTA_ID_SYS, BTA_DM_NUM_RM_ENTRY, BTA_DM_SCATTERNET},
{BTA_ID_PAN, BTUI_PAN_ID_NAP, BTA_ANY_ROLE},
{BTA_ID_PAN, BTUI_PAN_ID_GN, BTA_ANY_ROLE},
{BTA_ID_PAN, BTA_APP_ID_PAN_MULTI, BTA_MASTER_ROLE_ONLY},
{BTA_ID_PAN, BTUI_PAN_ID_PANU, BTA_PANU_ROLE},
{BTA_ID_HH, BTA_ALL_APP_ID, BTA_HH_ROLE},
{BTA_ID_AV, BTA_ALL_APP_ID, BTA_AV_ROLE}
};
tBTA_DM_CFG *p_bta_dm_cfg = (tBTA_DM_CFG *)&bta_dm_cfg;
tBTA_DM_RM *p_bta_dm_rm_cfg = (tBTA_DM_RM *)&bta_dm_rm_cfg;
#if BLE_INCLUDED == TRUE
# define BTA_DM_NUM_PM_ENTRY 21 /* number of entries in bta_dm_pm_cfg except the first */
# define BTA_DM_NUM_PM_SPEC 15 /* number of entries in bta_dm_pm_spec */
#else
# define BTA_DM_NUM_PM_ENTRY 19 /* number of entries in bta_dm_pm_cfg except the first */
# define BTA_DM_NUM_PM_SPEC 13 /* number of entries in bta_dm_pm_spec */
#endif
tBTA_DM_PM_TYPE_QUALIFIER tBTA_DM_PM_CFG bta_dm_pm_cfg[BTA_DM_NUM_PM_ENTRY + 1] =
{
{BTA_ID_SYS, BTA_DM_NUM_PM_ENTRY, 0}, /* reserved: specifies length of this table. */
{BTA_ID_AG, BTA_ALL_APP_ID, 0}, /* ag uses first spec table for app id 0 */
{BTA_ID_CT, 1, 1}, /* ct (BTA_ID_CT,APP ID=1) spec table */
{BTA_ID_CG, BTA_ALL_APP_ID, 1}, /* cg resue ct spec table */
{BTA_ID_DG, BTA_ALL_APP_ID, 2}, /* dg spec table */
{BTA_ID_AV, BTA_ALL_APP_ID, 4}, /* av spec table */
{BTA_ID_AVK, BTA_ALL_APP_ID, 12}, /* avk spec table */
{BTA_ID_FTC, BTA_ALL_APP_ID, 6}, /* ftc spec table */
{BTA_ID_FTS, BTA_ALL_APP_ID, 7}, /* fts spec table */
{BTA_ID_HD, BTA_ALL_APP_ID, 3}, /* hd spec table */
{BTA_ID_HH, BTA_ALL_APP_ID, 5}, /* hh spec table */
{BTA_ID_PBC, BTA_ALL_APP_ID, 2}, /* reuse dg spec table */
{BTA_ID_PBS, BTA_ALL_APP_ID, 7}, /* reuse fts spec table */
{BTA_ID_OPC, BTA_ALL_APP_ID, 6}, /* reuse ftc spec table */
{BTA_ID_OPS, BTA_ALL_APP_ID, 7}, /* reuse fts spec table */
{BTA_ID_MSE, BTA_ALL_APP_ID, 7}, /* reuse fts spec table */
// {BTA_ID_JV, BTA_JV_PM_ID_1, 6}, /* app BTA_JV_PM_ID_1, reuse ftc spec table */
// {BTA_ID_JV, BTA_ALL_APP_ID, 7}, /* reuse fts spec table */
{BTA_ID_HL, BTA_ALL_APP_ID, 8}, /* reuse fts spec table */
{BTA_ID_PAN, BTUI_PAN_ID_PANU, 9}, /* PANU spec table */
{BTA_ID_PAN, BTUI_PAN_ID_NAP, 10}, /* NAP spec table */
{BTA_ID_HS, BTA_ALL_APP_ID, 11} /* HS spec table */
#if BLE_INCLUDED == TRUE
,{BTA_ID_GATTC, BTA_ALL_APP_ID, 13} /* gattc spec table */
,{BTA_ID_GATTS, BTA_ALL_APP_ID, 14} /* gatts spec table */
#endif
};
tBTA_DM_PM_TYPE_QUALIFIER tBTA_DM_PM_SPEC bta_dm_pm_spec[BTA_DM_NUM_PM_SPEC] =
{
/* AG : 0 */
{
(BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_SNIFF_A2DP_IDX, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_SNIFF_SCO_OPEN_IDX, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open, active */
{{BTA_DM_PM_SNIFF_A2DP_IDX, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close sniff */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_RETRY, 7000}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* CT, CG : 1 */
{
(BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_PARK, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open park */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_SNIFF_A2DP_IDX, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open sniff */
{{BTA_DM_PM_PARK, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close park */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_RETRY, 5000}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* DG, PBC : 2 */
{
(BTA_DM_PM_ACTIVE), /* no power saving mode allowed */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_SNIFF, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_SNIFF, 1000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* HD : 3 */
{
(BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR3), /* the SSR entry */
#endif
{
{{BTA_DM_PM_SNIFF_HD_ACTIVE_IDX, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_SNIFF_HD_IDLE_IDX, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_SNIFF_HD_ACTIVE_IDX, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* AV : 4 */
{
(BTA_DM_PM_SNIFF), /* allow sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_SNIFF_A2DP_IDX, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_SNIFF_A2DP_IDX, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* HH : 5 */
{
(BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR1), /* the SSR entry */
#endif
{
{{BTA_DM_PM_SNIFF_HH_OPEN_IDX, BTA_DM_PM_HH_OPEN_DELAY},{BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close, used for HH suspend */
{{BTA_DM_PM_SNIFF_HH_IDLE_IDX, BTA_DM_PM_HH_IDLE_DELAY}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_SNIFF_HH_ACTIVE_IDX, BTA_DM_PM_HH_ACTIVE_DELAY}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* FTC, OPC, JV : 6 */
{
(BTA_DM_PM_SNIFF), /* allow sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_SNIFF_A2DP_IDX, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* FTS, PBS, OPS, MSE, BTA_JV_PM_ID_1 : 7 */
{
(BTA_DM_PM_SNIFF), /* allow sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_SNIFF_A2DP_IDX, BTA_FTS_OPS_IDLE_TO_SNIFF_DELAY_MS}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* HL : 8 */
{
(BTA_DM_PM_SNIFF), /* allow sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_SNIFF_A2DP_IDX, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open, active */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close sniff */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* PANU : 9 */
{
(BTA_DM_PM_SNIFF), /* allow sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_SNIFF_A2DP_IDX, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* NAP : 10 */
{
(BTA_DM_PM_SNIFF), /* allow sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_SNIFF_A2DP_IDX, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* HS : 11 */
{
(BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_SNIFF, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_SNIFF3, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open, active */
{{BTA_DM_PM_SNIFF, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close sniff */
{{BTA_DM_PM_SNIFF, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_RETRY, 7000}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
},
/* AVK : 12 */
{
(BTA_DM_PM_SNIFF), /* allow sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_SNIFF, 3000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_SNIFF4, 3000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
}
#if BLE_INCLUDED == TRUE
/* GATTC : 13 */
,{
(BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_SNIFF_A2DP_IDX, 10000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_SNIFF_A2DP_IDX, 10000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
#if defined(AMP_INCLUDED) && (AMP_INCLUDED == TRUE)
{{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* amp */
#endif
{{BTA_DM_PM_RETRY, 5000}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
}
/* GATTS : 14 */
,{
(BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */
#if (BTM_SSR_INCLUDED == TRUE)
(BTA_DM_PM_SSR2), /* the SSR entry */
#endif
{
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */
#if defined(AMP_INCLUDED) && (AMP_INCLUDED == TRUE)
{{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* amp */
#endif
{{BTA_DM_PM_RETRY, 5000}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */
}
}
#endif
#ifdef BTE_SIM_APP /* For Insight builds only */
/* Entries at the end of the pm_spec table are user-defined (runtime configurable),
for power consumption experiments.
Insight finds the first user-defined entry by looking for the first BTA_DM_PM_NO_PREF.
The number of user_defined specs is defined by BTA_SWRAP_UD_PM_SPEC_COUNT */
,
{BTA_DM_PM_NO_PREF}, /* pm_spec USER_DEFINED_0 */
{BTA_DM_PM_NO_PREF} /* pm_spec USER_DEFINED_1 */
#endif /* BTE_SIM_APP */
};
/* Please refer to the SNIFF table definitions in bta_api.h.
*
* Adding to or Modifying the Table
* Additional sniff parameter entries can be added for BTA_DM_PM_SNIFF5 - BTA_DM_PM_SNIFF7.
* Overrides of additional table entries can be specified in bdroid_buildcfg.h. If additional
* sniff parameter entries are added or an override of an existing entry is specified in
* bdroid_buildcfg.h then the BTA_DM_PM_*_IDX defines in bta_api.h will need to be match the new
* ordering.
*
* Table Ordering
* Sniff Table entries must be ordered from highest latency (biggest interval) to lowest latency.
* If there is a conflict among the connected services the setting with the lowest latency will
* be selected.
*/
tBTA_DM_PM_TYPE_QUALIFIER tBTM_PM_PWR_MD bta_dm_pm_md[] =
{
/*
* More sniff parameter entries can be added for
* BTA_DM_PM_SNIFF3 - BTA_DM_PM_SNIFF7, if needed. When entries are added or
* removed, BTA_DM_PM_PARK_IDX needs to be updated to reflect the actual index
* BTA_DM_PM_PARK_IDX is defined in bta_api.h and can be override by the
* bdroid_buildcfg.h settings.
* The SNIFF table entries must be in the order from highest latency (biggest
* interval) to lowest latency. If there's a conflict among the connected
* services, the setting with lowest latency wins.
*/
/* sniff modes: max interval, min interval, attempt, timeout */
{BTA_DM_PM_SNIFF_MAX, BTA_DM_PM_SNIFF_MIN, BTA_DM_PM_SNIFF_ATTEMPT, BTA_DM_PM_SNIFF_TIMEOUT, BTM_PM_MD_SNIFF}, /* for BTA_DM_PM_SNIFF - A2DP */
{BTA_DM_PM_SNIFF1_MAX, BTA_DM_PM_SNIFF1_MIN, BTA_DM_PM_SNIFF1_ATTEMPT, BTA_DM_PM_SNIFF1_TIMEOUT, BTM_PM_MD_SNIFF}, /* for BTA_DM_PM_SNIFF1 */
{BTA_DM_PM_SNIFF2_MAX, BTA_DM_PM_SNIFF2_MIN, BTA_DM_PM_SNIFF2_ATTEMPT, BTA_DM_PM_SNIFF2_TIMEOUT, BTM_PM_MD_SNIFF}, /* for BTA_DM_PM_SNIFF2- HD idle */
{BTA_DM_PM_SNIFF3_MAX, BTA_DM_PM_SNIFF3_MIN, BTA_DM_PM_SNIFF3_ATTEMPT, BTA_DM_PM_SNIFF3_TIMEOUT, BTM_PM_MD_SNIFF}, /* for BTA_DM_PM_SNIFF3- SCO open */
{BTA_DM_PM_SNIFF4_MAX, BTA_DM_PM_SNIFF4_MIN, BTA_DM_PM_SNIFF4_ATTEMPT, BTA_DM_PM_SNIFF4_TIMEOUT, BTM_PM_MD_SNIFF}, /* for BTA_DM_PM_SNIFF4- HD active */
{BTA_DM_PM_SNIFF5_MAX, BTA_DM_PM_SNIFF5_MIN, BTA_DM_PM_SNIFF5_ATTEMPT, BTA_DM_PM_SNIFF5_TIMEOUT, BTM_PM_MD_SNIFF}, /* for BTA_DM_PM_SNIFF5- HD active */
{BTA_DM_PM_PARK_MAX, BTA_DM_PM_PARK_MIN, BTA_DM_PM_PARK_ATTEMPT, BTA_DM_PM_PARK_TIMEOUT, BTM_PM_MD_PARK}
#ifdef BTE_SIM_APP /* For Insight builds only */
/* Entries at the end of the bta_dm_pm_md table are user-defined (runtime configurable),
for power consumption experiments.
Insight finds the first user-defined entry by looking for the first 'max=0'.
The number of user_defined specs is defined by BTA_SWRAP_UD_PM_DM_COUNT */
,
{0}, /* CONN_OPEN/SCO_CLOSE power mode settings for pm_spec USER_DEFINED_0 */
{0}, /* SCO_OPEN power mode settings for pm_spec USER_DEFINED_0 */
{0}, /* CONN_OPEN/SCO_CLOSE power mode settings for pm_spec USER_DEFINED_1 */
{0} /* SCO_OPEN power mode settings for pm_spec USER_DEFINED_1 */
#endif /* BTE_SIM_APP */
};
/* 0=max_lat -> no SSR */
/* the smaller of the SSR max latency wins.
* the entries in this table must be from highest latency (biggest interval) to lowest latency */
#if (BTM_SSR_INCLUDED == TRUE)
tBTA_DM_SSR_SPEC bta_dm_ssr_spec[] =
{
/*max_lat, min_rmt_to, min_loc_to*/
{0, 0, 0}, /* BTA_DM_PM_SSR0 - do not use SSR */
{0, 0, 2}, /* BTA_DM_PM_SSR1 - HH, can NOT share entry with any other profile,
seting default max latency and min remote timeout as 0,
and always read individual device preference from HH module */
{1200, 2, 2}, /* BTA_DM_PM_SSR2 - others (as long as sniff is allowed)*/
{360, 160, 2} /* BTA_DM_PM_SSR3 - HD */
};
tBTA_DM_SSR_SPEC *p_bta_dm_ssr_spec = (tBTA_DM_SSR_SPEC *)&bta_dm_ssr_spec;
#endif
tBTA_DM_PM_CFG *p_bta_dm_pm_cfg = (tBTA_DM_PM_CFG *)&bta_dm_pm_cfg;
tBTA_DM_PM_SPEC *p_bta_dm_pm_spec = (tBTA_DM_PM_SPEC *)&bta_dm_pm_spec;
tBTM_PM_PWR_MD *p_bta_dm_pm_md = (tBTM_PM_PWR_MD *)&bta_dm_pm_md;
/* The performance impact of EIR packet size
**
** When BTM_EIR_DEFAULT_FEC_REQUIRED is TRUE,
** 1 to 17 bytes, DM1 is used and most robust.
** 18 to 121 bytes, DM3 is used but impacts inquiry scan time with large number
** of devices.(almost double with 150 users)
** 122 to 224 bytes, DM5 is used but cause quite big performance loss even with
** small number of users. so it is not recommended.
** 225 to 240 bytes, DH5 is used without FEC but it not recommended.
** (same reason of DM5)
**
** When BTM_EIR_DEFAULT_FEC_REQUIRED is FALSE,
** 1 to 27 bytes, DH1 is used but only robust at short range.
** 28 to 183 bytes, DH3 is used but only robust at short range and impacts inquiry
** scan time with large number of devices.
** 184 to 240 bytes, DH5 is used but it not recommended.
*/
#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
/* for example */
const UINT8 bta_dm_eir_uuid16_list[] = { 0x08, 0x11, /* Headset */
0x1E, 0x11, /* Handsfree */
0x0E, 0x11, /* AV Remote Control */
0x0B, 0x11, /* Audio Sink */
};
#endif // BTA_EIR_CANNED_UUID_LIST
/* Extended Inquiry Response */
const tBTA_DM_EIR_CONF bta_dm_eir_cfg =
{
50, /* minimum length of local name when it is shortened */
/* if length of local name is longer than this and EIR has not enough */
/* room for all UUID list then local name is shortened to this length */
#if (BTA_EIR_CANNED_UUID_LIST == TRUE)
8,
(UINT8 *)bta_dm_eir_uuid16_list,
#else // BTA_EIR_CANNED_UUID_LIST
{ /* mask of UUID list in EIR */
0xFFFFFFFF, /* LSB is the first UUID of the first 32 UUIDs in BTM_EIR_UUID_LKUP_TBL */
0xFFFFFFFF /* LSB is the first UUID of the next 32 UUIDs in BTM_EIR_UUID_LKUP_TBL */
/* BTM_EIR_UUID_LKUP_TBL can be overrided */
},
#endif // BTA_EIR_CANNED_UUID_LIST
NULL, /* Inquiry TX power */
0, /* length of flags in bytes */
NULL, /* flags for EIR */
0, /* length of manufacturer specific in bytes */
NULL, /* manufacturer specific */
0, /* length of additional data in bytes */
NULL /* additional data */
};
tBTA_DM_EIR_CONF *p_bta_dm_eir_cfg = (tBTA_DM_EIR_CONF*)&bta_dm_eir_cfg;

View file

@ -0,0 +1,117 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the API implementation file for the BTA device manager.
*
******************************************************************************/
#include "gki.h"
#include "bta_sys.h"
#include "bta_api.h"
#include "bta_dm_int.h"
#include <string.h>
#include "bta_dm_ci.h"
#if (BTM_OOB_INCLUDED == TRUE)
/*******************************************************************************
**
** Function bta_dm_ci_io_req
**
** Description This function must be called in response to function
** bta_dm_co_io_req(), if *p_oob_data to BTA_OOB_UNKNOWN
** by bta_dm_co_io_req().
**
** Returns void
**
*******************************************************************************/
void bta_dm_ci_io_req(BD_ADDR bd_addr, tBTA_IO_CAP io_cap, tBTA_OOB_DATA oob_data,
tBTA_AUTH_REQ auth_req)
{
tBTA_DM_CI_IO_REQ *p_msg;
if ((p_msg = (tBTA_DM_CI_IO_REQ *) GKI_getbuf(sizeof(tBTA_DM_CI_IO_REQ))) != NULL)
{
p_msg->hdr.event = BTA_DM_CI_IO_REQ_EVT;
bdcpy(p_msg->bd_addr, bd_addr);
p_msg->io_cap = io_cap;
p_msg->oob_data = oob_data;
p_msg->auth_req = auth_req;
bta_sys_sendmsg(p_msg);
}
}
/*******************************************************************************
**
** Function bta_dm_ci_rmt_oob
**
** Description This function must be called in response to function
** bta_dm_co_rmt_oob() to provide the OOB data associated
** with the remote device.
**
** Returns void
**
*******************************************************************************/
void bta_dm_ci_rmt_oob(BOOLEAN accept, BD_ADDR bd_addr, BT_OCTET16 c, BT_OCTET16 r)
{
tBTA_DM_CI_RMT_OOB *p_msg;
if ((p_msg = (tBTA_DM_CI_RMT_OOB *) GKI_getbuf(sizeof(tBTA_DM_CI_RMT_OOB))) != NULL)
{
p_msg->hdr.event = BTA_DM_CI_RMT_OOB_EVT;
bdcpy(p_msg->bd_addr, bd_addr);
p_msg->accept = accept;
memcpy(p_msg->c, c, BT_OCTET16_LEN);
memcpy(p_msg->r, r, BT_OCTET16_LEN);
bta_sys_sendmsg(p_msg);
}
}
#endif /* BTM_OOB_INCLUDED */
#if (BTM_SCO_HCI_INCLUDED == TRUE)
/*******************************************************************************
**
** Function bta_dm_sco_ci_data_ready
**
** Description This function sends an event to indicating that the phone
** has SCO data ready.
**
** Parameters event: is obtained from bta_dm_sco_co_open() function, which
** is the BTA event we want to send back to BTA module
** when there is encoded data ready.
** sco_handle: is the BTA sco handle which indicate a specific
** SCO connection.
** Returns void
**
*******************************************************************************/
void bta_dm_sco_ci_data_ready(UINT16 event, UINT16 sco_handle)
{
BT_HDR *p_buf;
if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
{
p_buf->event = event;
p_buf->layer_specific = sco_handle;
bta_sys_sendmsg(p_buf);
}
}
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,376 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the main implementation file for the BTA device manager.
*
******************************************************************************/
#include "bta_api.h"
#include "bta_sys.h"
#include "bta_dm_int.h"
/*****************************************************************************
** Constants and types
*****************************************************************************/
#if BTA_DYNAMIC_MEMORY == FALSE
tBTA_DM_CB bta_dm_cb;
tBTA_DM_SEARCH_CB bta_dm_search_cb;
tBTA_DM_DI_CB bta_dm_di_cb;
#endif
#define BTA_DM_NUM_ACTIONS (BTA_DM_MAX_EVT & 0x00ff)
/* type for action functions */
typedef void (*tBTA_DM_ACTION)(tBTA_DM_MSG *p_data);
/* action function list */
const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] =
{
/* device manager local device API events */
bta_dm_enable, /* 0 BTA_DM_API_ENABLE_EVT */
bta_dm_disable, /* 1 BTA_DM_API_DISABLE_EVT */
bta_dm_set_dev_name, /* 2 BTA_DM_API_SET_NAME_EVT */
bta_dm_set_visibility, /* 3 BTA_DM_API_SET_VISIBILITY_EVT */
bta_dm_acl_change, /* 8 BTA_DM_ACL_CHANGE_EVT */
bta_dm_add_device, /* 9 BTA_DM_API_ADD_DEVICE_EVT */
bta_dm_close_acl, /* 10 BTA_DM_API_ADD_DEVICE_EVT */
/* security API events */
bta_dm_bond, /* 11 BTA_DM_API_BOND_EVT */
bta_dm_bond_cancel, /* 12 BTA_DM_API_BOND_CANCEL_EVT */
bta_dm_pin_reply, /* 13 BTA_DM_API_PIN_REPLY_EVT */
/* power manger events */
bta_dm_pm_btm_status, /* 16 BTA_DM_PM_BTM_STATUS_EVT */
bta_dm_pm_timer, /* 17 BTA_DM_PM_TIMER_EVT*/
/* simple pairing events */
bta_dm_confirm, /* 18 BTA_DM_API_CONFIRM_EVT */
bta_dm_set_encryption, /* BTA_DM_API_SET_ENCRYPTION_EVT */
#if (BTM_OOB_INCLUDED == TRUE)
bta_dm_loc_oob, /* 20 BTA_DM_API_LOC_OOB_EVT */
bta_dm_ci_io_req_act, /* 21 BTA_DM_CI_IO_REQ_EVT */
bta_dm_ci_rmt_oob_act, /* 22 BTA_DM_CI_RMT_OOB_EVT */
#endif /* BTM_OOB_INCLUDED */
#if BLE_INCLUDED == TRUE
bta_dm_add_blekey, /* BTA_DM_API_ADD_BLEKEY_EVT */
bta_dm_add_ble_device, /* BTA_DM_API_ADD_BLEDEVICE_EVT */
bta_dm_ble_passkey_reply, /* BTA_DM_API_BLE_PASSKEY_REPLY_EVT */
bta_dm_ble_confirm_reply, /* BTA_DM_API_BLE_CONFIRM_REPLY_EVT */
bta_dm_security_grant,
bta_dm_ble_set_bg_conn_type,
bta_dm_ble_set_conn_params, /* BTA_DM_API_BLE_CONN_PARAM_EVT */
bta_dm_ble_set_conn_scan_params, /* BTA_DM_API_BLE_CONN_SCAN_PARAM_EVT */
bta_dm_ble_set_scan_params, /* BTA_DM_API_BLE_SCAN_PARAM_EVT */
bta_dm_ble_observe,
bta_dm_ble_update_conn_params, /* BTA_DM_API_UPDATE_CONN_PARAM_EVT */
/*******This handler function added by Yulong at 2016/9/9 to
support the random address setting for the APP******/
bta_dm_ble_set_rand_address, /*BTA_DM_API_SET_RAND_ADDR_EVT*/
#if BLE_PRIVACY_SPT == TRUE
bta_dm_ble_config_local_privacy, /* BTA_DM_API_LOCAL_PRIVACY_EVT */
#endif
bta_dm_ble_set_adv_params, /* BTA_DM_API_BLE_ADV_PARAM_EVT */
bta_dm_ble_set_adv_config, /* BTA_DM_API_BLE_SET_ADV_CONFIG_EVT */
bta_dm_ble_set_scan_rsp, /* BTA_DM_API_BLE_SET_SCAN_RSPT */
bta_dm_ble_broadcast, /* BTA_DM_API_BLE_BROADCAST_EVT */
bta_dm_ble_set_data_length, /* BTA_DM_API_SET_DATA_LENGTH_EVT */
#if BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE
bta_dm_cfg_filter_cond, /* BTA_DM_API_CFG_FILTER_COND_EVT */
bta_dm_scan_filter_param_setup, /* BTA_DM_API_SCAN_FILTER_SETUP_EVT */
bta_dm_enable_scan_filter, /* BTA_DM_API_SCAN_FILTER_ENABLE_EVT */
#endif
bta_dm_ble_multi_adv_enb, /* BTA_DM_API_BLE_MULTI_ADV_ENB_EVT*/
bta_dm_ble_multi_adv_upd_param, /* BTA_DM_API_BLE_MULTI_ADV_PARAM_UPD_EVT */
bta_dm_ble_multi_adv_data, /* BTA_DM_API_BLE_MULTI_ADV_DATA_EVT */
btm_dm_ble_multi_adv_disable, /* BTA_DM_API_BLE_MULTI_ADV_DISABLE_EVT */
bta_dm_ble_setup_storage, /* BTA_DM_API_BLE_SETUP_STORAGE_EVT */
bta_dm_ble_enable_batch_scan, /* BTA_DM_API_BLE_ENABLE_BATCH_SCAN_EVT */
bta_dm_ble_disable_batch_scan, /* BTA_DM_API_BLE_DISABLE_BATCH_SCAN_EVT */
bta_dm_ble_read_scan_reports, /* BTA_DM_API_BLE_READ_SCAN_REPORTS_EVT */
bta_dm_ble_track_advertiser, /* BTA_DM_API_BLE_TRACK_ADVERTISER_EVT */
bta_dm_ble_get_energy_info, /* BTA_DM_API_BLE_ENERGY_INFO_EVT */
#endif
bta_dm_enable_test_mode, /* BTA_DM_API_ENABLE_TEST_MODE_EVT */
bta_dm_disable_test_mode, /* BTA_DM_API_DISABLE_TEST_MODE_EVT */
bta_dm_execute_callback, /* BTA_DM_API_EXECUTE_CBACK_EVT */
bta_dm_remove_all_acl, /* BTA_DM_API_REMOVE_ALL_ACL_EVT */
bta_dm_remove_device, /* BTA_DM_API_REMOVE_DEVICE_EVT */
};
/* state machine action enumeration list */
enum
{
BTA_DM_API_SEARCH, /* 0 bta_dm_search_start */
BTA_DM_API_SEARCH_CANCEL, /* 1 bta_dm_search_cancel */
BTA_DM_API_DISCOVER, /* 2 bta_dm_discover */
BTA_DM_INQUIRY_CMPL, /* 3 bta_dm_inq_cmpl */
BTA_DM_REMT_NAME, /* 4 bta_dm_rmt_name */
BTA_DM_SDP_RESULT, /* 5 bta_dm_sdp_result */
BTA_DM_SEARCH_CMPL, /* 6 bta_dm_search_cmpl*/
BTA_DM_FREE_SDP_DB, /* 7 bta_dm_free_sdp_db */
BTA_DM_DISC_RESULT, /* 8 bta_dm_disc_result */
BTA_DM_SEARCH_RESULT, /* 9 bta_dm_search_result */
BTA_DM_QUEUE_SEARCH, /* 10 bta_dm_queue_search */
BTA_DM_QUEUE_DISC, /* 11 bta_dm_queue_disc */
BTA_DM_SEARCH_CLEAR_QUEUE, /* 12 bta_dm_search_clear_queue */
BTA_DM_SEARCH_CANCEL_CMPL, /* 13 bta_dm_search_cancel_cmpl */
BTA_DM_SEARCH_CANCEL_NOTIFY, /* 14 bta_dm_search_cancel_notify */
BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, /* 15 bta_dm_search_cancel_transac_cmpl */
BTA_DM_DISC_RMT_NAME, /* 16 bta_dm_disc_rmt_name */
BTA_DM_API_DI_DISCOVER, /* 17 bta_dm_di_disc */
#if BLE_INCLUDED == TRUE
BTA_DM_CLOSE_GATT_CONN, /* 18 bta_dm_close_gatt_conn */
#endif
BTA_DM_SEARCH_NUM_ACTIONS /* 19 */
};
/* action function list */
const tBTA_DM_ACTION bta_dm_search_action[] =
{
bta_dm_search_start, /* 0 BTA_DM_API_SEARCH */
bta_dm_search_cancel, /* 1 BTA_DM_API_SEARCH_CANCEL */
bta_dm_discover, /* 2 BTA_DM_API_DISCOVER */
bta_dm_inq_cmpl, /* 3 BTA_DM_INQUIRY_CMPL */
bta_dm_rmt_name, /* 4 BTA_DM_REMT_NAME */
bta_dm_sdp_result, /* 5 BTA_DM_SDP_RESULT */
bta_dm_search_cmpl, /* 6 BTA_DM_SEARCH_CMPL */
bta_dm_free_sdp_db, /* 7 BTA_DM_FREE_SDP_DB */
bta_dm_disc_result, /* 8 BTA_DM_DISC_RESULT */
bta_dm_search_result, /* 9 BTA_DM_SEARCH_RESULT */
bta_dm_queue_search, /* 10 BTA_DM_QUEUE_SEARCH */
bta_dm_queue_disc, /* 11 BTA_DM_QUEUE_DISC */
bta_dm_search_clear_queue, /* 12 BTA_DM_SEARCH_CLEAR_QUEUE */
bta_dm_search_cancel_cmpl, /* 13 BTA_DM_SEARCH_CANCEL_CMPL */
bta_dm_search_cancel_notify, /* 14 BTA_DM_SEARCH_CANCEL_NOTIFY */
bta_dm_search_cancel_transac_cmpl, /* 15 BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL */
bta_dm_disc_rmt_name, /* 16 BTA_DM_DISC_RMT_NAME */
bta_dm_di_disc /* 17 BTA_DM_API_DI_DISCOVER */
#if BLE_INCLUDED == TRUE
,bta_dm_close_gatt_conn
#endif
};
#define BTA_DM_SEARCH_IGNORE BTA_DM_SEARCH_NUM_ACTIONS
/* state table information */
#define BTA_DM_SEARCH_ACTIONS 2 /* number of actions */
#define BTA_DM_SEARCH_NEXT_STATE 2 /* position of next state */
#define BTA_DM_SEARCH_NUM_COLS 3 /* number of columns in state tables */
/* state table for listen state */
const UINT8 bta_dm_search_idle_st_table[][BTA_DM_SEARCH_NUM_COLS] =
{
/* Event Action 1 Action 2 Next State */
/* API_SEARCH */ {BTA_DM_API_SEARCH, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE},
/* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE},
/* API_SEARCH_DISC */ {BTA_DM_API_DISCOVER, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE},
/* INQUIRY_CMPL */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE},
/* REMT_NAME_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE},
/* SDP_RESULT_EVT */ {BTA_DM_FREE_SDP_DB, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE},
/* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE},
/* DISCV_RES_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE},
/* API_DI_DISCOVER_EVT */ {BTA_DM_API_DI_DISCOVER, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}
#if BLE_INCLUDED == TRUE
/* DISC_CLOSE_TOUT_EVT */ ,{BTA_DM_CLOSE_GATT_CONN, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}
#endif
};
const UINT8 bta_dm_search_search_active_st_table[][BTA_DM_SEARCH_NUM_COLS] =
{
/* Event Action 1 Action 2 Next State */
/* API_SEARCH */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE},
/* API_SEARCH_CANCEL */ {BTA_DM_API_SEARCH_CANCEL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING},
/* API_SEARCH_DISC */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE},
/* INQUIRY_CMPL */ {BTA_DM_INQUIRY_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE},
/* REMT_NAME_EVT */ {BTA_DM_REMT_NAME, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE},
/* SDP_RESULT_EVT */ {BTA_DM_SDP_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE},
/* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE},
/* DISCV_RES_EVT */ {BTA_DM_SEARCH_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE},
/* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}
#if BLE_INCLUDED == TRUE
/* DISC_CLOSE_TOUT_EVT */ ,{BTA_DM_CLOSE_GATT_CONN, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}
#endif
};
const UINT8 bta_dm_search_search_cancelling_st_table[][BTA_DM_SEARCH_NUM_COLS] =
{
/* Event Action 1 Action 2 Next State */
/* API_SEARCH */ {BTA_DM_QUEUE_SEARCH, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING},
/* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CLEAR_QUEUE, BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_CANCELLING},
/* API_SEARCH_DISC */ {BTA_DM_QUEUE_DISC, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING},
/* INQUIRY_CMPL */ {BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE},
/* REMT_NAME_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE},
/* SDP_RESULT_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE},
/* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE},
/* DISCV_RES_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE},
/* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}
#if BLE_INCLUDED == TRUE
/* DISC_CLOSE_TOUT_EVT */ ,{BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}
#endif
};
const UINT8 bta_dm_search_disc_active_st_table[][BTA_DM_SEARCH_NUM_COLS] =
{
/* Event Action 1 Action 2 Next State */
/* API_SEARCH */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE},
/* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING},
/* API_SEARCH_DISC */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE},
/* INQUIRY_CMPL */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE},
/* REMT_NAME_EVT */ {BTA_DM_DISC_RMT_NAME, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE},
/* SDP_RESULT_EVT */ {BTA_DM_SDP_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE},
/* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE},
/* DISCV_RES_EVT */ {BTA_DM_DISC_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE},
/* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}
#if BLE_INCLUDED == TRUE
/* DISC_CLOSE_TOUT_EVT */ ,{BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}
#endif
};
typedef const UINT8 (*tBTA_DM_ST_TBL)[BTA_DM_SEARCH_NUM_COLS];
/* state table */
const tBTA_DM_ST_TBL bta_dm_search_st_tbl[] = {
bta_dm_search_idle_st_table,
bta_dm_search_search_active_st_table,
bta_dm_search_search_cancelling_st_table,
bta_dm_search_disc_active_st_table
};
/*******************************************************************************
**
** Function bta_dm_sm_disable
**
** Description unregister BTA DM
**
**
** Returns void
**
*******************************************************************************/
void bta_dm_sm_disable( )
{
bta_sys_deregister( BTA_ID_DM );
}
/*******************************************************************************
**
** Function bta_dm_sm_execute
**
** Description State machine event handling function for DM
**
**
** Returns void
**
*******************************************************************************/
BOOLEAN bta_dm_sm_execute(BT_HDR *p_msg)
{
UINT16 event = p_msg->event & 0x00ff;
APPL_TRACE_EVENT("bta_dm_sm_execute event:0x%x", event);
/* execute action functions */
if(event < BTA_DM_NUM_ACTIONS)
{
(*bta_dm_action[event])( (tBTA_DM_MSG*) p_msg);
}
return TRUE;
}
/*******************************************************************************
**
** Function bta_dm_sm_search_disable
**
** Description unregister BTA SEARCH DM
**
**
** Returns void
**
*******************************************************************************/
void bta_dm_search_sm_disable( )
{
bta_sys_deregister( BTA_ID_DM_SEARCH );
}
/*******************************************************************************
**
** Function bta_dm_search_sm_execute
**
** Description State machine event handling function for DM
**
**
** Returns void
**
*******************************************************************************/
BOOLEAN bta_dm_search_sm_execute(BT_HDR *p_msg)
{
tBTA_DM_ST_TBL state_table;
UINT8 action;
int i;
APPL_TRACE_EVENT("bta_dm_search_sm_execute state:%d, event:0x%x",
bta_dm_search_cb.state, p_msg->event);
/* look up the state table for the current state */
state_table = bta_dm_search_st_tbl[bta_dm_search_cb.state];
bta_dm_search_cb.state = state_table[p_msg->event & 0x00ff][BTA_DM_SEARCH_NEXT_STATE];
/* execute action functions */
for (i = 0; i < BTA_DM_SEARCH_ACTIONS; i++)
{
if ((action = state_table[p_msg->event & 0x00ff][i]) != BTA_DM_SEARCH_IGNORE)
{
(*bta_dm_search_action[action])( (tBTA_DM_MSG*) p_msg);
}
else
{
break;
}
}
return TRUE;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,695 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the down sampling utility to convert PCM samples in
* 16k/32k/48k/44.1k/22050/11025 sampling rate into 8K/16bits samples
* required for SCO channel format. One API function isprovided and only
* possible to be used when transmitting SCO data is sent via HCI
* interface.
*
******************************************************************************/
#include <string.h>
#include "bta_api.h"
#include "bta_sys.h"
#if (BTM_SCO_HCI_INCLUDED == TRUE)
#ifndef BTA_DM_SCO_DEBUG
#define BTA_DM_SCO_DEBUG FALSE
#endif
/*****************************************************************************
** Constants
*****************************************************************************/
#define BTA_DM_PCM_OVERLAP_SIZE 48
#define BTA_DM_PCM_SMPL_RATE_44100 44100
#define BTA_DM_PCM_SMPL_RATE_22050 22050
#define BTA_DM_PCM_SMPL_RATE_11025 11025
/*****************************************************************************
** Data types for PCM Resampling utility
*****************************************************************************/
typedef INT32 (*PCONVERT_TO_BT_FILTERED) (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples,
UINT32 dwSrcSps,INT32 *pLastCurPos, UINT8 *pOverlapArea);
typedef INT32 (*PCONVERT_TO_BT_NOFILTER) (void *pSrc, void *pDst, UINT32 dwSrcSamples,
UINT32 dwSrcSps);
typedef struct
{
UINT8 overlap_area[BTA_DM_PCM_OVERLAP_SIZE * 4];
UINT32 cur_pos; /* current position */
UINT32 src_sps; /* samples per second (source audio data) */
PCONVERT_TO_BT_FILTERED filter; /* the action function to do the
conversion 44100, 22050, 11025*/
PCONVERT_TO_BT_NOFILTER nofilter; /* the action function to do
the conversion 48000, 32000, 16000*/
UINT32 bits; /* number of bits per pcm sample */
UINT32 n_channels; /* number of channels (i.e. mono(1), stereo(2)...) */
UINT32 sample_size;
UINT32 can_be_filtered;
UINT32 divisor;
} tBTA_DM_PCM_RESAMPLE_CB;
tBTA_DM_PCM_RESAMPLE_CB bta_dm_pcm_cb;
/*****************************************************************************
** Macro Definition
*****************************************************************************/
#define CHECK_SATURATION16(x) \
if (x > 32767) \
x = 32767; \
else if (x < -32768) \
x = -32768;
////////////////////////////////////////////////////////////////////////////////////////////////////
//
#define CONVERT_44100_TO_BLUETOOTH(pStart, pEnd) \
{ \
INT32 out1, out2, out3, out4, out5; \
SRC_TYPE *pS = (SRC_TYPE *)pStart; \
SRC_TYPE *pSEnd = (SRC_TYPE *)pEnd; \
\
while (pS < pSEnd) \
{ \
CurrentPos -= 8000; \
\
if (CurrentPos >= 0) \
{ \
pS += SRC_CHANNELS; \
continue; \
} \
CurrentPos += dwSrcSps; \
\
out1 = (SRC_SAMPLE(0) * 1587) \
+ ((SRC_SAMPLE(1) + SRC_SAMPLE(-1)) * 1522) \
+ ((SRC_SAMPLE(2) + SRC_SAMPLE(-2)) * 1337) \
+ ((SRC_SAMPLE(3) + SRC_SAMPLE(-3)) * 1058); \
\
out1 = out1 / 30000; \
\
out2 = ((SRC_SAMPLE(4) + SRC_SAMPLE(-4)) * 725) \
+ ((SRC_SAMPLE(5) + SRC_SAMPLE(-5)) * 384) \
+ ((SRC_SAMPLE(6) + SRC_SAMPLE(-6)) * 79); \
\
out2 = out2 / 30000; \
\
out3 = ((SRC_SAMPLE(7) + SRC_SAMPLE(-7)) * 156) \
+ ((SRC_SAMPLE(8) + SRC_SAMPLE(-8)) * 298) \
+ ((SRC_SAMPLE(9) + SRC_SAMPLE(-9)) * 345); \
\
out3 = out3 / 30000; \
\
out4 = ((SRC_SAMPLE(10) + SRC_SAMPLE(-10)) * 306) \
+ ((SRC_SAMPLE(11) + SRC_SAMPLE(-11)) * 207) \
+ ((SRC_SAMPLE(12) + SRC_SAMPLE(-12)) * 78); \
\
out4 = out4 / 30000; \
\
out5 = out1 + out2 - out3 - out4; \
\
CHECK_SATURATION16(out5); \
*psBtOut++ = (INT16)out5; \
\
pS += SRC_CHANNELS; \
} \
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
#define CONVERT_22050_TO_BLUETOOTH(pStart, pEnd) \
{ \
INT32 out1, out2, out3, out4, out5; \
SRC_TYPE *pS = (SRC_TYPE *)pStart; \
SRC_TYPE *pSEnd = (SRC_TYPE *)pEnd; \
\
while (pS < pSEnd) \
{ \
CurrentPos -= 8000; \
\
if (CurrentPos >= 0) \
{ \
pS += SRC_CHANNELS; \
continue; \
} \
CurrentPos += dwSrcSps; \
\
out1 = (SRC_SAMPLE(0) * 2993) \
+ ((SRC_SAMPLE(1) + SRC_SAMPLE(-1)) * 2568) \
+ ((SRC_SAMPLE(2) + SRC_SAMPLE(-2)) * 1509) \
+ ((SRC_SAMPLE(3) + SRC_SAMPLE(-3)) * 331); \
\
out1 = out1 / 30000; \
\
out2 = ((SRC_SAMPLE(4) + SRC_SAMPLE(-4)) * 454) \
+ ((SRC_SAMPLE(5) + SRC_SAMPLE(-5)) * 620) \
+ ((SRC_SAMPLE(6) + SRC_SAMPLE(-6)) * 305); \
\
out2 = out2 / 30000; \
\
out3 = ((SRC_SAMPLE(7) + SRC_SAMPLE(-7)) * 127) \
+ ((SRC_SAMPLE(8) + SRC_SAMPLE(-8)) * 350) \
+ ((SRC_SAMPLE(9) + SRC_SAMPLE(-9)) * 265) \
+ ((SRC_SAMPLE(10) + SRC_SAMPLE(-10)) * 6); \
\
out3 = out3 / 30000; \
\
out4 = ((SRC_SAMPLE(11) + SRC_SAMPLE(-11)) * 201); \
\
out4 = out4 / 30000; \
\
out5 = out1 - out2 + out3 - out4; \
\
CHECK_SATURATION16(out5); \
*psBtOut++ = (INT16)out5; \
\
pS += SRC_CHANNELS; \
} \
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
#define CONVERT_11025_TO_BLUETOOTH(pStart, pEnd) \
{ \
INT32 out1; \
SRC_TYPE *pS = (SRC_TYPE *)pStart; \
SRC_TYPE *pSEnd = (SRC_TYPE *)pEnd; \
\
while (pS < pSEnd) \
{ \
CurrentPos -= 8000; \
\
if (CurrentPos >= 0) \
{ \
pS += SRC_CHANNELS; \
continue; \
} \
CurrentPos += dwSrcSps; \
\
out1 = (SRC_SAMPLE(0) * 6349) \
+ ((SRC_SAMPLE(1) + SRC_SAMPLE(-1)) * 2874) \
- ((SRC_SAMPLE(2) + SRC_SAMPLE(-2)) * 1148) \
- ((SRC_SAMPLE(3) + SRC_SAMPLE(-3)) * 287) \
+ ((SRC_SAMPLE(4) + SRC_SAMPLE(-4)) * 675) \
- ((SRC_SAMPLE(5) + SRC_SAMPLE(-5)) * 258) \
- ((SRC_SAMPLE(6) + SRC_SAMPLE(-6)) * 206) \
+ ((SRC_SAMPLE(7) + SRC_SAMPLE(-7)) * 266); \
\
out1 = out1 / 30000; \
\
CHECK_SATURATION16(out1); \
*psBtOut++ = (INT16)out1; \
\
pS += SRC_CHANNELS; \
} \
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
#undef SRC_CHANNELS
#undef SRC_SAMPLE
#undef SRC_TYPE
#define SRC_TYPE UINT8
#define SRC_CHANNELS 1
#define SRC_SAMPLE(x) ((pS[x] - 0x80) << 8)
/*****************************************************************************
** Local Function
*****************************************************************************/
INT32 Convert_8M_ToBT_Filtered (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples,
UINT32 dwSrcSps, INT32 *pLastCurPos, UINT8 *pOverlapArea)
{
INT32 CurrentPos = *pLastCurPos;
SRC_TYPE *pIn, *pInEnd;
SRC_TYPE *pOv, *pOvEnd;
INT16 *psBtOut = (INT16 *)pDst;
#if BTA_DM_SCO_DEBUG
APPL_TRACE_DEBUG("Convert_8M_ToBT_Filtered, CurrentPos %d\n", CurrentPos);
#endif
memcpy (pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 2), pSrc, BTA_DM_PCM_OVERLAP_SIZE * 2);
pOv = (SRC_TYPE *)(pOverlapArea + BTA_DM_PCM_OVERLAP_SIZE);
pOvEnd = (SRC_TYPE *)(pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 3));
pIn = (SRC_TYPE *)(pSrc + BTA_DM_PCM_OVERLAP_SIZE);
pInEnd = (SRC_TYPE *)(pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \
BTA_DM_PCM_OVERLAP_SIZE);
if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_44100)
{
CONVERT_44100_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_44100_TO_BLUETOOTH(pIn, pInEnd);
}
else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_22050)
{
CONVERT_22050_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_22050_TO_BLUETOOTH(pIn, pInEnd);
}
else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_11025)
{
CONVERT_11025_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_11025_TO_BLUETOOTH(pIn, pInEnd);
}
memcpy (pOverlapArea, pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \
(BTA_DM_PCM_OVERLAP_SIZE * 2), BTA_DM_PCM_OVERLAP_SIZE * 2);
*pLastCurPos = CurrentPos;
return (psBtOut - (INT16 *)pDst);
}
INT32 Convert_8M_ToBT_NoFilter (void *pSrc, void *pDst, UINT32 dwSrcSamples, UINT32 dwSrcSps)
{
INT32 CurrentPos;
UINT8 *pbSrc = (UINT8 *)pSrc;
INT16 *psDst = (INT16 *)pDst;
INT16 sWorker;
// start at dwSpsSrc / 2, decrement by 8000
//
CurrentPos = (dwSrcSps >> 1);
while (dwSrcSamples--)
{
CurrentPos -= 8000;
if (CurrentPos >= 0)
pbSrc++;
else
{
sWorker = *pbSrc++;
sWorker -= 0x80;
sWorker <<= 8;
*psDst++ = sWorker;
CurrentPos += dwSrcSps;
}
}
return (psDst - (INT16 *)pDst);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
#undef SRC_CHANNELS
#undef SRC_SAMPLE
#undef SRC_TYPE
#define SRC_TYPE INT16
#define SRC_CHANNELS 1
#define SRC_SAMPLE(x) pS[x]
INT32 Convert_16M_ToBT_Filtered (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples,
UINT32 dwSrcSps, INT32 *pLastCurPos, UINT8 *pOverlapArea)
{
INT32 CurrentPos = *pLastCurPos;
SRC_TYPE *pIn, *pInEnd;
SRC_TYPE *pOv, *pOvEnd;
INT16 *psBtOut = (INT16 *)pDst;
memcpy (pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 2), pSrc, BTA_DM_PCM_OVERLAP_SIZE * 2);
pOv = (SRC_TYPE *)(pOverlapArea + BTA_DM_PCM_OVERLAP_SIZE);
pOvEnd = (SRC_TYPE *)(pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 3));
pIn = (SRC_TYPE *)(pSrc + BTA_DM_PCM_OVERLAP_SIZE);
pInEnd = (SRC_TYPE *)(pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - BTA_DM_PCM_OVERLAP_SIZE);
if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_44100)
{
CONVERT_44100_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_44100_TO_BLUETOOTH(pIn, pInEnd);
}
else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_22050)
{
CONVERT_22050_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_22050_TO_BLUETOOTH(pIn, pInEnd);
}
else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_11025)
{
CONVERT_11025_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_11025_TO_BLUETOOTH(pIn, pInEnd);
}
memcpy (pOverlapArea, pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \
(BTA_DM_PCM_OVERLAP_SIZE * 2), BTA_DM_PCM_OVERLAP_SIZE * 2);
*pLastCurPos = CurrentPos;
return (psBtOut - (INT16 *)pDst);
}
INT32 Convert_16M_ToBT_NoFilter (void *pSrc, void *pDst, UINT32 dwSrcSamples, UINT32 dwSrcSps)
{
INT32 CurrentPos;
INT16 *psSrc = (INT16 *)pSrc;
INT16 *psDst = (INT16 *)pDst;
// start at dwSpsSrc / 2, decrement by 8000
//
CurrentPos = (dwSrcSps >> 1);
while (dwSrcSamples--)
{
CurrentPos -= 8000;
if (CurrentPos >= 0)
psSrc++;
else
{
*psDst++ = *psSrc++;
CurrentPos += dwSrcSps;
}
}
return (psDst - (INT16 *)pDst);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
#undef SRC_CHANNELS
#undef SRC_SAMPLE
#undef SRC_TYPE
#define SRC_TYPE UINT8
#define SRC_CHANNELS 2
#define SRC_SAMPLE(x) ((((pS[x * 2] - 0x80) << 8) + ((pS[(x * 2) + 1] - 0x80) << 8)) >> 1)
INT32 Convert_8S_ToBT_Filtered (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples,
UINT32 dwSrcSps, INT32 *pLastCurPos, UINT8 *pOverlapArea)
{
INT32 CurrentPos = *pLastCurPos;
SRC_TYPE *pIn, *pInEnd;
SRC_TYPE *pOv, *pOvEnd;
INT16 *psBtOut = (INT16 *)pDst;
#if BTA_DM_SCO_DEBUG
APPL_TRACE_DEBUG("Convert_8S_ToBT_Filtered CurrentPos %d, SRC_TYPE %d, SRC_CHANNELS %d, \
dwSrcSamples %d, dwSrcSps %d", CurrentPos, sizeof (SRC_TYPE), SRC_CHANNELS, \
dwSrcSamples, dwSrcSps);
#endif
memcpy (pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 2), pSrc, BTA_DM_PCM_OVERLAP_SIZE * 2);
pOv = (SRC_TYPE *)(pOverlapArea + BTA_DM_PCM_OVERLAP_SIZE);
pOvEnd = (SRC_TYPE *)(pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 3));
pIn = (SRC_TYPE *)(pSrc + BTA_DM_PCM_OVERLAP_SIZE);
pInEnd = (SRC_TYPE *)(pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - BTA_DM_PCM_OVERLAP_SIZE);
if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_44100)
{
CONVERT_44100_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_44100_TO_BLUETOOTH(pIn, pInEnd);
}
else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_22050)
{
CONVERT_22050_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_22050_TO_BLUETOOTH(pIn, pInEnd);
}
else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_11025)
{
CONVERT_11025_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_11025_TO_BLUETOOTH(pIn, pInEnd);
}
memcpy (pOverlapArea, pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \
(BTA_DM_PCM_OVERLAP_SIZE * 2), BTA_DM_PCM_OVERLAP_SIZE * 2);
*pLastCurPos = CurrentPos;
return (psBtOut - (INT16 *)pDst);
}
INT32 Convert_8S_ToBT_NoFilter (void *pSrc, void *pDst, UINT32 dwSrcSamples, UINT32 dwSrcSps)
{
INT32 CurrentPos;
UINT8 *pbSrc = (UINT8 *)pSrc;
INT16 *psDst = (INT16 *)pDst;
INT16 sWorker, sWorker2;
// start at dwSpsSrc / 2, decrement by 8000
//
CurrentPos = (dwSrcSps >> 1);
while (dwSrcSamples--)
{
CurrentPos -= 8000;
if (CurrentPos >= 0)
pbSrc += 2;
else
{
sWorker = *(unsigned char *)pbSrc;
sWorker -= 0x80;
sWorker <<= 8;
pbSrc++;
sWorker2 = *(unsigned char *)pbSrc;
sWorker2 -= 0x80;
sWorker2 <<= 8;
pbSrc++;
sWorker += sWorker2;
sWorker >>= 1;
*psDst++ = sWorker;
CurrentPos += dwSrcSps;
}
}
return (psDst - (INT16 *)pDst);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
#undef SRC_CHANNELS
#undef SRC_SAMPLE
#undef SRC_TYPE
#define SRC_TYPE INT16
#define SRC_CHANNELS 2
#define SRC_SAMPLE(x) ((pS[x * 2] + pS[(x * 2) + 1]) >> 1)
INT32 Convert_16S_ToBT_Filtered (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples,
UINT32 dwSrcSps, INT32 *pLastCurPos, UINT8 *pOverlapArea)
{
INT32 CurrentPos = *pLastCurPos;
SRC_TYPE *pIn, *pInEnd;
SRC_TYPE *pOv, *pOvEnd;
INT16 *psBtOut = (INT16 *)pDst;
memcpy (pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 2), pSrc, BTA_DM_PCM_OVERLAP_SIZE * 2);
pOv = (SRC_TYPE *)(pOverlapArea + BTA_DM_PCM_OVERLAP_SIZE);
pOvEnd = (SRC_TYPE *)(pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 3));
pIn = (SRC_TYPE *)(pSrc + BTA_DM_PCM_OVERLAP_SIZE);
pInEnd = (SRC_TYPE *)(pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - BTA_DM_PCM_OVERLAP_SIZE);
if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_44100)
{
CONVERT_44100_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_44100_TO_BLUETOOTH(pIn, pInEnd);
}
else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_22050)
{
CONVERT_22050_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_22050_TO_BLUETOOTH(pIn, pInEnd);
}
else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_11025)
{
CONVERT_11025_TO_BLUETOOTH(pOv, pOvEnd);
CONVERT_11025_TO_BLUETOOTH(pIn, pInEnd);
}
memcpy (pOverlapArea, pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \
(BTA_DM_PCM_OVERLAP_SIZE * 2), BTA_DM_PCM_OVERLAP_SIZE * 2);
*pLastCurPos = CurrentPos;
return (psBtOut - (INT16 *)pDst);
}
INT32 Convert_16S_ToBT_NoFilter (void *pSrc, void *pDst, UINT32 dwSrcSamples, UINT32 dwSrcSps)
{
INT32 CurrentPos;
INT16 *psSrc = (INT16 *)pSrc;
INT16 *psDst = (INT16 *)pDst;
INT16 sWorker;
// start at dwSpsSrc / 2, decrement by 8000
//
CurrentPos = (dwSrcSps >> 1);
while (dwSrcSamples--)
{
CurrentPos -= 8000;
if (CurrentPos >= 0)
psSrc += 2;
else
{
/* CR 82894, to avoid overflow, divide before add */
sWorker = ((*psSrc) >> 1 );
psSrc++;
sWorker += ((*psSrc) >> 1 );
psSrc++;
*psDst++ = sWorker;
CurrentPos += dwSrcSps;
}
}
return (psDst - (INT16 *)pDst);
}
/*******************************************************************************
**
** Function BTA_DmPcmInitSamples
**
** Description initialize the down sample converter.
**
** src_sps: original samples per second (source audio data)
** (ex. 44100, 48000)
** bits: number of bits per pcm sample (16)
** n_channels: number of channels (i.e. mono(1), stereo(2)...)
**
** Returns none
**
*******************************************************************************/
void BTA_DmPcmInitSamples (UINT32 src_sps, UINT32 bits, UINT32 n_channels)
{
tBTA_DM_PCM_RESAMPLE_CB *p_cb = &bta_dm_pcm_cb;
p_cb->cur_pos = src_sps / 2;
p_cb->src_sps = src_sps;
p_cb->bits = bits;
p_cb->n_channels = n_channels;
p_cb->sample_size = 2;
p_cb->divisor = 2;
memset(p_cb->overlap_area, 0, sizeof(p_cb->overlap_area) );
if ((src_sps == BTA_DM_PCM_SMPL_RATE_44100) ||
(src_sps == BTA_DM_PCM_SMPL_RATE_22050) ||
(src_sps == BTA_DM_PCM_SMPL_RATE_11025))
p_cb->can_be_filtered = 1;
else
p_cb->can_be_filtered = 0;
#if BTA_DM_SCO_DEBUG
APPL_TRACE_DEBUG("bta_dm_pcm_init_samples: n_channels = %d bits = %d", n_channels, bits);
#endif
if(n_channels == 1)
{
/* mono */
if(bits == 8)
{
p_cb->filter = (PCONVERT_TO_BT_FILTERED) Convert_8M_ToBT_Filtered;
p_cb->nofilter = (PCONVERT_TO_BT_NOFILTER) Convert_8M_ToBT_NoFilter;
p_cb->divisor = 1;
}
else
{
p_cb->filter = (PCONVERT_TO_BT_FILTERED) Convert_16M_ToBT_Filtered;
p_cb->nofilter = (PCONVERT_TO_BT_NOFILTER) Convert_16M_ToBT_NoFilter;
}
}
else
{
/* stereo */
if(bits == 8)
{
p_cb->filter = (PCONVERT_TO_BT_FILTERED) Convert_8S_ToBT_Filtered;
p_cb->nofilter = (PCONVERT_TO_BT_NOFILTER) Convert_8S_ToBT_NoFilter;
}
else
{
p_cb->filter = (PCONVERT_TO_BT_FILTERED) Convert_16S_ToBT_Filtered;
p_cb->nofilter = (PCONVERT_TO_BT_NOFILTER) Convert_16S_ToBT_NoFilter;
p_cb->divisor = 4;
}
}
#if BTA_DM_SCO_DEBUG
APPL_TRACE_DEBUG("bta_pcm_init_dwn_sample: cur_pos %d, src_sps %d", \
p_cb->cur_pos, p_cb->src_sps);
APPL_TRACE_DEBUG("bta_pcm_init_dwn_sample: bits %d, n_channels %d, sample_size %d, ", \
p_cb->bits, p_cb->n_channels, p_cb->sample_size);
APPL_TRACE_DEBUG("bta_pcm_init_dwn_sample: can_be_filtered %d, n_channels: %d, \
divisor %d", p_cb->can_be_filtered, p_cb->n_channels, p_cb->divisor);
#endif
}
/**************************************************************************************
** Function BTA_DmPcmResample
**
** Description Down sampling utility to convert higher sampling rate into 8K/16bits
** PCM samples.
**
** Parameters p_src: pointer to the buffer where the original sampling PCM
** are stored.
** in_bytes: Length of the input PCM sample buffer in byte.
** p_dst: pointer to the buffer which is to be used to store
** the converted PCM samples.
**
**
** Returns INT32: number of samples converted.
**
**************************************************************************************/
INT32 BTA_DmPcmResample (void *p_src, UINT32 in_bytes, void *p_dst)
{
UINT32 out_sample;
#if BTA_DM_SCO_DEBUG
APPL_TRACE_DEBUG("bta_pcm_resample : insamples %d", (in_bytes / bta_dm_pcm_cb.divisor));
#endif
if(bta_dm_pcm_cb.can_be_filtered)
{
out_sample = (*bta_dm_pcm_cb.filter) (p_src, p_dst, (in_bytes / bta_dm_pcm_cb.divisor),
bta_dm_pcm_cb.src_sps, (INT32 *) &bta_dm_pcm_cb.cur_pos, bta_dm_pcm_cb.overlap_area);
}
else
{
out_sample = (*bta_dm_pcm_cb.nofilter) (p_src, p_dst,
(in_bytes / bta_dm_pcm_cb.divisor), bta_dm_pcm_cb.src_sps);
}
#if BTA_DM_SCO_DEBUG
APPL_TRACE_DEBUG("bta_pcm_resample : outsamples %d", out_sample);
#endif
return (out_sample * bta_dm_pcm_cb.sample_size);
}
#endif

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,140 @@
/******************************************************************************
*
* Copyright (C) 2010-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the implementation file for the GATT call-in functions.
*
******************************************************************************/
#include "bt_target.h"
#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
#include <string.h>
#include "bta_api.h"
#include "bta_sys.h"
#include "bta_gattc_ci.h"
#include "gki.h"
#include "utl.h"
/*******************************************************************************
**
** Function bta_gattc_ci_cache_open
**
** Description This function sends an event to indicate server cache open
** completed.
**
** Parameters server_bda - server BDA of this cache.
** status - BTA_GATT_OK if full buffer of data,
** BTA_GATT_FAIL if an error has occurred.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_ci_cache_open(BD_ADDR server_bda, UINT16 evt, tBTA_GATT_STATUS status,
UINT16 conn_id)
{
tBTA_GATTC_CI_EVT *p_evt;
UNUSED(server_bda);
if ((p_evt = (tBTA_GATTC_CI_EVT *) GKI_getbuf(sizeof(tBTA_GATTC_CI_EVT))) != NULL)
{
p_evt->hdr.event = evt;
p_evt->hdr.layer_specific = conn_id;
p_evt->status = status;
bta_sys_sendmsg(p_evt);
}
}
/*******************************************************************************
**
** Function bta_gattc_ci_cache_load
**
** Description This function sends an event to BTA indicating the phone has
** load the servere cache and ready to send it to the stack.
**
** Parameters server_bda - server BDA of this cache.
** num_bytes_read - number of bytes read into the buffer
** specified in the read callout-function.
** status - BTA_GATT_OK if full buffer of data,
** BTA_GATT_FAIL if an error has occurred.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_ci_cache_load(BD_ADDR server_bda, UINT16 evt, UINT16 num_attr,
tBTA_GATTC_NV_ATTR *p_attr, tBTA_GATT_STATUS status,
UINT16 conn_id)
{
tBTA_GATTC_CI_LOAD *p_evt;
UNUSED(server_bda);
if ((p_evt = (tBTA_GATTC_CI_LOAD *) GKI_getbuf(sizeof(tBTA_GATTC_CI_LOAD))) != NULL)
{
memset(p_evt, 0, sizeof(tBTA_GATTC_CI_LOAD));
p_evt->hdr.event = evt;
p_evt->hdr.layer_specific = conn_id;
p_evt->status = status;
p_evt->num_attr = (num_attr > BTA_GATTC_NV_LOAD_MAX) ? BTA_GATTC_NV_LOAD_MAX : num_attr;
if (p_evt->num_attr > 0 && p_attr != NULL)
{
memcpy(p_evt->attr, p_attr, p_evt->num_attr * sizeof(tBTA_GATTC_NV_ATTR));
}
bta_sys_sendmsg(p_evt);
}
}
/*******************************************************************************
**
** Function bta_gattc_ci_cache_save
**
** Description This function sends an event to BTA indicating the phone has
** save the servere cache.
**
** Parameters server_bda - server BDA of this cache.
** evt - callin event code.
** status - BTA_GATT_OK if full buffer of data,
** BTA_GATT_ERROR if an error has occurred.
*8 conn_id - for this NV operation for.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_ci_cache_save(BD_ADDR server_bda, UINT16 evt, tBTA_GATT_STATUS status,
UINT16 conn_id)
{
tBTA_GATTC_CI_EVT *p_evt;
UNUSED(server_bda);
if ((p_evt = (tBTA_GATTC_CI_EVT *) GKI_getbuf(sizeof(tBTA_GATTC_CI_EVT))) != NULL)
{
p_evt->hdr.event = evt;
p_evt->hdr.layer_specific = conn_id;
p_evt->status = status;
bta_sys_sendmsg(p_evt);
}
}
#endif /* BTA_GATT_INCLUDED */

View file

@ -0,0 +1,542 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the GATT client main functions and state machine.
*
******************************************************************************/
#include "bt_target.h"
#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
#include <string.h>
#include "bta_gattc_int.h"
#include "gki.h"
/*****************************************************************************
** Constants and types
*****************************************************************************/
/* state machine action enumeration list */
enum
{
BTA_GATTC_OPEN,
BTA_GATTC_OPEN_FAIL,
BTA_GATTC_OPEN_ERROR,
BTA_GATTC_CANCEL_OPEN,
BTA_GATTC_CANCEL_OPEN_OK,
BTA_GATTC_CANCEL_OPEN_ERROR,
BTA_GATTC_CONN,
BTA_GATTC_START_DISCOVER,
BTA_GATTC_DISC_CMPL,
BTA_GATTC_Q_CMD,
BTA_GATTC_CLOSE,
BTA_GATTC_CLOSE_FAIL,
BTA_GATTC_READ,
BTA_GATTC_WRITE,
BTA_GATTC_OP_CMPL,
BTA_GATTC_SEARCH,
BTA_GATTC_FAIL,
BTA_GATTC_CONFIRM,
BTA_GATTC_EXEC,
BTA_GATTC_READ_MULTI,
BTA_GATTC_CI_OPEN,
BTA_GATTC_CI_LOAD,
BTA_GATTC_CI_SAVE,
BTA_GATTC_CACHE_OPEN,
BTA_GATTC_IGNORE_OP_CMPL,
BTA_GATTC_DISC_CLOSE,
BTA_GATTC_RESTART_DISCOVER,
BTA_GATTC_CFG_MTU,
BTA_GATTC_IGNORE
};
/* type for action functions */
typedef void (*tBTA_GATTC_ACTION)(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
/* action function list */
const tBTA_GATTC_ACTION bta_gattc_action[] =
{
bta_gattc_open,
bta_gattc_open_fail,
bta_gattc_open_error,
bta_gattc_cancel_open,
bta_gattc_cancel_open_ok,
bta_gattc_cancel_open_error,
bta_gattc_conn,
bta_gattc_start_discover,
bta_gattc_disc_cmpl,
bta_gattc_q_cmd,
bta_gattc_close,
bta_gattc_close_fail,
bta_gattc_read,
bta_gattc_write,
bta_gattc_op_cmpl,
bta_gattc_search,
bta_gattc_fail,
bta_gattc_confirm,
bta_gattc_execute,
bta_gattc_read_multi,
bta_gattc_ci_open,
bta_gattc_ci_load,
bta_gattc_ci_save,
bta_gattc_cache_open,
bta_gattc_ignore_op_cmpl,
bta_gattc_disc_close,
bta_gattc_restart_discover,
bta_gattc_cfg_mtu
};
/* state table information */
#define BTA_GATTC_ACTIONS 1 /* number of actions */
#define BTA_GATTC_NEXT_STATE 1 /* position of next state */
#define BTA_GATTC_NUM_COLS 2 /* number of columns in state tables */
/* state table for idle state */
static const UINT8 bta_gattc_st_idle[][BTA_GATTC_NUM_COLS] =
{
/* Event Action 1 Next state */
/* BTA_GATTC_API_OPEN_EVT */ {BTA_GATTC_OPEN, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_INT_OPEN_FAIL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_CANCEL_OPEN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_READ_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_WRITE_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_EXEC_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_CFG_MTU_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_CLOSE_EVT */ {BTA_GATTC_CLOSE_FAIL, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_SEARCH_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_CONFIRM_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_READ_MULTI_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_REFRESH_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_INT_CONN_EVT */ {BTA_GATTC_CONN, BTA_GATTC_CONN_ST},
/* BTA_GATTC_INT_DISCOVER_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_DISCOVER_CMPL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* ===> for cache loading, saving */
/* BTA_GATTC_START_CACHE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_CI_CACHE_OPEN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_CI_CACHE_LOAD_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_CI_CACHE_SAVE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST}
};
/* state table for wait for open state */
static const UINT8 bta_gattc_st_w4_conn[][BTA_GATTC_NUM_COLS] =
{
/* Event Action 1 Next state */
/* BTA_GATTC_API_OPEN_EVT */ {BTA_GATTC_OPEN, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_INT_OPEN_FAIL_EVT */ {BTA_GATTC_OPEN_FAIL, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_CANCEL_OPEN_EVT */ {BTA_GATTC_CANCEL_OPEN, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */ {BTA_GATTC_CANCEL_OPEN_OK, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_READ_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_API_WRITE_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_API_EXEC_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_API_CFG_MTU_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_API_CLOSE_EVT */ {BTA_GATTC_CANCEL_OPEN, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_API_SEARCH_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_API_CONFIRM_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_API_READ_MULTI_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_API_REFRESH_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_INT_CONN_EVT */ {BTA_GATTC_CONN, BTA_GATTC_CONN_ST},
/* BTA_GATTC_INT_DISCOVER_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_DISCOVER_CMPL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_OPEN_FAIL, BTA_GATTC_IDLE_ST},
/* ===> for cache loading, saving */
/* BTA_GATTC_START_CACHE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_CI_CACHE_OPEN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_CI_CACHE_LOAD_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST},
/* BTA_GATTC_CI_CACHE_SAVE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_W4_CONN_ST}
};
/* state table for open state */
static const UINT8 bta_gattc_st_connected[][BTA_GATTC_NUM_COLS] =
{
/* Event Action 1 Next state */
/* BTA_GATTC_API_OPEN_EVT */ {BTA_GATTC_OPEN, BTA_GATTC_CONN_ST},
/* BTA_GATTC_INT_OPEN_FAIL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST},
/* BTA_GATTC_API_CANCEL_OPEN_EVT */ {BTA_GATTC_CANCEL_OPEN_ERROR, BTA_GATTC_CONN_ST},
/* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST},
/* BTA_GATTC_API_READ_EVT */ {BTA_GATTC_READ, BTA_GATTC_CONN_ST},
/* BTA_GATTC_API_WRITE_EVT */ {BTA_GATTC_WRITE, BTA_GATTC_CONN_ST},
/* BTA_GATTC_API_EXEC_EVT */ {BTA_GATTC_EXEC, BTA_GATTC_CONN_ST},
/* BTA_GATTC_API_CFG_MTU_EVT */ {BTA_GATTC_CFG_MTU, BTA_GATTC_CONN_ST},
/* BTA_GATTC_API_CLOSE_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST},
/* BTA_GATTC_API_SEARCH_EVT */ {BTA_GATTC_SEARCH, BTA_GATTC_CONN_ST},
/* BTA_GATTC_API_CONFIRM_EVT */ {BTA_GATTC_CONFIRM, BTA_GATTC_CONN_ST},
/* BTA_GATTC_API_READ_MULTI_EVT */ {BTA_GATTC_READ_MULTI, BTA_GATTC_CONN_ST},
/* BTA_GATTC_API_REFRESH_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST},
/* BTA_GATTC_INT_CONN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST},
/* BTA_GATTC_INT_DISCOVER_EVT */ {BTA_GATTC_START_DISCOVER, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_DISCOVER_CMPL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST},
/* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_OP_CMPL, BTA_GATTC_CONN_ST},
/* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST},
/* ===> for cache loading, saving */
/* BTA_GATTC_START_CACHE_EVT */ {BTA_GATTC_CACHE_OPEN, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_CI_CACHE_OPEN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST},
/* BTA_GATTC_CI_CACHE_LOAD_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST},
/* BTA_GATTC_CI_CACHE_SAVE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_CONN_ST}
};
/* state table for discover state */
static const UINT8 bta_gattc_st_discover[][BTA_GATTC_NUM_COLS] =
{
/* Event Action 1 Next state */
/* BTA_GATTC_API_OPEN_EVT */ {BTA_GATTC_OPEN, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_INT_OPEN_FAIL_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_CANCEL_OPEN_EVT */ {BTA_GATTC_CANCEL_OPEN_ERROR, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_READ_EVT */ {BTA_GATTC_Q_CMD, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_WRITE_EVT */ {BTA_GATTC_Q_CMD, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_EXEC_EVT */ {BTA_GATTC_Q_CMD, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_CFG_MTU_EVT */ {BTA_GATTC_Q_CMD, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_CLOSE_EVT */ {BTA_GATTC_DISC_CLOSE, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_SEARCH_EVT */ {BTA_GATTC_Q_CMD, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_CONFIRM_EVT */ {BTA_GATTC_CONFIRM, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_READ_MULTI_EVT */ {BTA_GATTC_Q_CMD, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_API_REFRESH_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_INT_CONN_EVT */ {BTA_GATTC_CONN, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_INT_DISCOVER_EVT */ {BTA_GATTC_RESTART_DISCOVER, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_DISCOVER_CMPL_EVT */ {BTA_GATTC_DISC_CMPL, BTA_GATTC_CONN_ST},
/* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE_OP_CMPL, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST},
/* ===> for cache loading, saving */
/* BTA_GATTC_START_CACHE_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_CI_CACHE_OPEN_EVT */ {BTA_GATTC_CI_OPEN, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_CI_CACHE_LOAD_EVT */ {BTA_GATTC_CI_LOAD, BTA_GATTC_DISCOVER_ST},
/* BTA_GATTC_CI_CACHE_SAVE_EVT */ {BTA_GATTC_CI_SAVE, BTA_GATTC_DISCOVER_ST}
};
/* type for state table */
typedef const UINT8 (*tBTA_GATTC_ST_TBL)[BTA_GATTC_NUM_COLS];
/* state table */
const tBTA_GATTC_ST_TBL bta_gattc_st_tbl[] =
{
bta_gattc_st_idle,
bta_gattc_st_w4_conn,
bta_gattc_st_connected,
bta_gattc_st_discover
};
/*****************************************************************************
** Global data
*****************************************************************************/
/* GATTC control block */
#if BTA_DYNAMIC_MEMORY == FALSE
tBTA_GATTC_CB bta_gattc_cb;
#endif
#if BTA_GATT_DEBUG == TRUE
static char *gattc_evt_code(tBTA_GATTC_INT_EVT evt_code);
static char *gattc_state_code(tBTA_GATTC_STATE state_code);
#endif
/*******************************************************************************
**
** Function bta_gattc_sm_execute
**
** Description State machine event handling function for GATTC
**
**
** Returns BOOLEAN : TRUE if queued client request buffer can be immediately released
** else FALSE
**
*******************************************************************************/
BOOLEAN bta_gattc_sm_execute(tBTA_GATTC_CLCB *p_clcb, UINT16 event, tBTA_GATTC_DATA *p_data)
{
tBTA_GATTC_ST_TBL state_table;
UINT8 action;
int i;
BOOLEAN rt = TRUE;
#if BTA_GATT_DEBUG == TRUE
tBTA_GATTC_STATE in_state = p_clcb->state;
UINT16 in_event = event;
APPL_TRACE_DEBUG("bta_gattc_sm_execute: State 0x%02x [%s], Event 0x%x[%s]", in_state,
gattc_state_code(in_state),
in_event,
gattc_evt_code(in_event));
#endif
/* look up the state table for the current state */
state_table = bta_gattc_st_tbl[p_clcb->state];
event &= 0x00FF;
/* set next state */
p_clcb->state = state_table[event][BTA_GATTC_NEXT_STATE];
/* execute action functions */
for (i = 0; i < BTA_GATTC_ACTIONS; i++)
{
if ((action = state_table[event][i]) != BTA_GATTC_IGNORE)
{
(*bta_gattc_action[action])(p_clcb, p_data);
if (p_clcb->p_q_cmd == p_data) {
/* buffer is queued, don't free in the bta dispatcher.
* we free it ourselves when a completion event is received.
*/
rt = FALSE;
}
}
else
{
break;
}
}
#if BTA_GATT_DEBUG == TRUE
if (in_state != p_clcb->state)
{
APPL_TRACE_DEBUG("GATTC State Change: [%s] -> [%s] after Event [%s]",
gattc_state_code(in_state),
gattc_state_code(p_clcb->state),
gattc_evt_code(in_event));
}
#endif
return rt;
}
/*******************************************************************************
**
** Function bta_gattc_hdl_event
**
** Description GATT client main event handling function.
**
**
** Returns BOOLEAN
**
*******************************************************************************/
BOOLEAN bta_gattc_hdl_event(BT_HDR *p_msg)
{
tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
tBTA_GATTC_CLCB *p_clcb = NULL;
tBTA_GATTC_RCB *p_clreg;
BOOLEAN rt = TRUE;
#if BTA_GATT_DEBUG == TRUE
APPL_TRACE_DEBUG("bta_gattc_hdl_event: Event [%s]", gattc_evt_code(p_msg->event));
#endif
switch (p_msg->event)
{
case BTA_GATTC_API_DISABLE_EVT:
bta_gattc_disable(p_cb);
break;
case BTA_GATTC_API_REG_EVT:
bta_gattc_register(p_cb, (tBTA_GATTC_DATA *) p_msg);
break;
case BTA_GATTC_INT_START_IF_EVT:
bta_gattc_start_if(p_cb, (tBTA_GATTC_DATA *) p_msg);
break;
case BTA_GATTC_API_DEREG_EVT:
p_clreg = bta_gattc_cl_get_regcb(((tBTA_GATTC_DATA *)p_msg)->api_dereg.client_if);
bta_gattc_deregister(p_cb, p_clreg);
break;
case BTA_GATTC_API_OPEN_EVT:
bta_gattc_process_api_open(p_cb, (tBTA_GATTC_DATA *) p_msg);
break;
case BTA_GATTC_API_CANCEL_OPEN_EVT:
bta_gattc_process_api_open_cancel(p_cb, (tBTA_GATTC_DATA *) p_msg);
break;
case BTA_GATTC_API_REFRESH_EVT:
bta_gattc_process_api_refresh(p_cb, (tBTA_GATTC_DATA *) p_msg);
break;
#if BLE_INCLUDED == TRUE
case BTA_GATTC_API_LISTEN_EVT:
bta_gattc_listen(p_cb, (tBTA_GATTC_DATA *) p_msg);
break;
case BTA_GATTC_API_BROADCAST_EVT:
bta_gattc_broadcast(p_cb, (tBTA_GATTC_DATA *) p_msg);
break;
#endif
case BTA_GATTC_ENC_CMPL_EVT:
bta_gattc_process_enc_cmpl(p_cb, (tBTA_GATTC_DATA *) p_msg);
break;
default:
if (p_msg->event == BTA_GATTC_INT_CONN_EVT)
p_clcb = bta_gattc_find_int_conn_clcb((tBTA_GATTC_DATA *) p_msg);
else if (p_msg->event == BTA_GATTC_INT_DISCONN_EVT)
p_clcb = bta_gattc_find_int_disconn_clcb((tBTA_GATTC_DATA *) p_msg);
else
p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->layer_specific);
if (p_clcb != NULL)
{
rt = bta_gattc_sm_execute(p_clcb, p_msg->event, (tBTA_GATTC_DATA *) p_msg);
}
else
{
APPL_TRACE_DEBUG("Ignore unknown conn ID: %d", p_msg->layer_specific);
}
break;
}
return rt;
}
/*****************************************************************************
** Debug Functions
*****************************************************************************/
#if BTA_GATT_DEBUG == TRUE
/*******************************************************************************
**
** Function gattc_evt_code
**
** Description
**
** Returns void
**
*******************************************************************************/
static char *gattc_evt_code(tBTA_GATTC_INT_EVT evt_code)
{
switch (evt_code)
{
case BTA_GATTC_API_OPEN_EVT:
return "BTA_GATTC_API_OPEN_EVT";
case BTA_GATTC_INT_OPEN_FAIL_EVT:
return "BTA_GATTC_INT_OPEN_FAIL_EVT";
case BTA_GATTC_API_CANCEL_OPEN_EVT:
return "BTA_GATTC_API_CANCEL_OPEN_EVT";
case BTA_GATTC_INT_CANCEL_OPEN_OK_EVT:
return "BTA_GATTC_INT_CANCEL_OPEN_OK_EVT";
case BTA_GATTC_API_READ_EVT:
return "BTA_GATTC_API_READ_EVT";
case BTA_GATTC_API_WRITE_EVT:
return "BTA_GATTC_API_WRITE_EVT";
case BTA_GATTC_API_EXEC_EVT:
return "BTA_GATTC_API_EXEC_EVT";
case BTA_GATTC_API_CLOSE_EVT:
return "BTA_GATTC_API_CLOSE_EVT";
case BTA_GATTC_API_SEARCH_EVT:
return "BTA_GATTC_API_SEARCH_EVT";
case BTA_GATTC_API_CONFIRM_EVT:
return "BTA_GATTC_API_CONFIRM_EVT";
case BTA_GATTC_API_READ_MULTI_EVT:
return "BTA_GATTC_API_READ_MULTI_EVT";
case BTA_GATTC_INT_CONN_EVT:
return "BTA_GATTC_INT_CONN_EVT";
case BTA_GATTC_INT_DISCOVER_EVT:
return "BTA_GATTC_INT_DISCOVER_EVT";
case BTA_GATTC_DISCOVER_CMPL_EVT:
return "BTA_GATTC_DISCOVER_CMPL_EVT";
case BTA_GATTC_OP_CMPL_EVT:
return "BTA_GATTC_OP_CMPL_EVT";
case BTA_GATTC_INT_DISCONN_EVT:
return "BTA_GATTC_INT_DISCONN_EVT";
case BTA_GATTC_START_CACHE_EVT:
return "BTA_GATTC_START_CACHE_EVT";
case BTA_GATTC_CI_CACHE_OPEN_EVT:
return "BTA_GATTC_CI_CACHE_OPEN_EVT";
case BTA_GATTC_CI_CACHE_LOAD_EVT:
return "BTA_GATTC_CI_CACHE_LOAD_EVT";
case BTA_GATTC_CI_CACHE_SAVE_EVT:
return "BTA_GATTC_CI_CACHE_SAVE_EVT";
case BTA_GATTC_INT_START_IF_EVT:
return "BTA_GATTC_INT_START_IF_EVT";
case BTA_GATTC_API_REG_EVT:
return "BTA_GATTC_API_REG_EVT";
case BTA_GATTC_API_DEREG_EVT:
return "BTA_GATTC_API_DEREG_EVT";
case BTA_GATTC_API_REFRESH_EVT:
return "BTA_GATTC_API_REFRESH_EVT";
case BTA_GATTC_API_LISTEN_EVT:
return "BTA_GATTC_API_LISTEN_EVT";
case BTA_GATTC_API_DISABLE_EVT:
return "BTA_GATTC_API_DISABLE_EVT";
case BTA_GATTC_API_CFG_MTU_EVT:
return "BTA_GATTC_API_CFG_MTU_EVT";
default:
return "unknown GATTC event code";
}
}
/*******************************************************************************
**
** Function gattc_state_code
**
** Description
**
** Returns void
**
*******************************************************************************/
static char *gattc_state_code(tBTA_GATTC_STATE state_code)
{
switch (state_code)
{
case BTA_GATTC_IDLE_ST:
return "GATTC_IDLE_ST";
case BTA_GATTC_W4_CONN_ST:
return "GATTC_W4_CONN_ST";
case BTA_GATTC_CONN_ST:
return "GATTC_CONN_ST";
case BTA_GATTC_DISCOVER_ST:
return "GATTC_DISCOVER_ST";
default:
return "unknown GATTC state code";
}
}
#endif /* Debug Functions */
#endif /* BTA_GATT_INCLUDED */

View file

@ -0,0 +1,973 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the GATT client utility function.
*
******************************************************************************/
#include "bt_target.h"
#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
#include <string.h>
#include "bdaddr.h"
// #include "btif/include/btif_util.h"
#include "gki.h"
#include "utl.h"
#include "bta_sys.h"
#include "bta_gattc_int.h"
#include "l2c_api.h"
#define LOG_TAG "bt_bta_gattc"
/*****************************************************************************
** Constants
*****************************************************************************/
static const UINT8 base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static const BD_ADDR dummy_bda = {0,0,0,0,0,0};
/*******************************************************************************
**
** Function bta_gatt_convert_uuid16_to_uuid128
**
** Description Convert a 16 bits UUID to be an standard 128 bits one.
**
** Returns TRUE if two uuid match; FALSE otherwise.
**
*******************************************************************************/
void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
{
UINT8 *p = &uuid_128[LEN_UUID_128 - 4];
memcpy (uuid_128, base_uuid, LEN_UUID_128);
UINT16_TO_STREAM(p, uuid_16);
}
/*******************************************************************************
**
** Function bta_gattc_uuid_compare
**
** Description Compare two UUID to see if they are the same.
**
** Returns TRUE if two uuid match; FALSE otherwise.
**
*******************************************************************************/
BOOLEAN bta_gattc_uuid_compare (tBT_UUID *p_src, tBT_UUID *p_tar, BOOLEAN is_precise)
{
UINT8 su[LEN_UUID_128], tu[LEN_UUID_128];
UINT8 *ps, *pt;
/* any of the UUID is unspecified */
if (p_src == 0 || p_tar == 0)
{
if (is_precise)
return FALSE;
else
return TRUE;
}
/* If both are 16-bit, we can do a simple compare */
if (p_src->len == 2 && p_tar->len == 2)
{
return p_src->uu.uuid16 == p_tar->uu.uuid16;
}
/* One or both of the UUIDs is 128-bit */
if (p_src->len == LEN_UUID_16)
{
/* convert a 16 bits UUID to 128 bits value */
bta_gatt_convert_uuid16_to_uuid128(su, p_src->uu.uuid16);
ps = su;
}
else
ps = p_src->uu.uuid128;
if (p_tar->len == LEN_UUID_16)
{
/* convert a 16 bits UUID to 128 bits value */
bta_gatt_convert_uuid16_to_uuid128(tu, p_tar->uu.uuid16);
pt = tu;
}
else
pt = p_tar->uu.uuid128;
return(memcmp(ps, pt, LEN_UUID_128) == 0);
}
/*******************************************************************************
**
** Function bta_gattc_cl_get_regcb
**
** Description get registration control block by client interface.
**
** Returns pointer to the regcb
**
*******************************************************************************/
tBTA_GATTC_RCB * bta_gattc_cl_get_regcb(UINT8 client_if)
{
UINT8 i = 0;
tBTA_GATTC_RCB *p_clrcb = &bta_gattc_cb.cl_rcb[0];
for (i = 0; i < BTA_GATTC_CL_MAX; i ++, p_clrcb ++)
{
if (p_clrcb->in_use &&
p_clrcb->client_if == client_if)
return p_clrcb;
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gattc_num_reg_app
**
** Description find the number of registered application.
**
** Returns pointer to the regcb
**
*******************************************************************************/
UINT8 bta_gattc_num_reg_app(void)
{
UINT8 i = 0, j = 0;
for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
{
if (bta_gattc_cb.cl_rcb[i].in_use)
j ++;
}
return j;
}
/*******************************************************************************
**
** Function bta_gattc_find_clcb_by_cif
**
** Description get clcb by client interface and remote bd adddress
**
** Returns pointer to the clcb
**
*******************************************************************************/
tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda,
tBTA_TRANSPORT transport)
{
tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
UINT8 i;
for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
{
if (p_clcb->in_use &&
p_clcb->p_rcb->client_if == client_if &&
p_clcb->transport == transport &&
bdcmp(p_clcb->bda, remote_bda) == 0)
return p_clcb;
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gattc_find_clcb_by_conn_id
**
** Description get clcb by connection ID
**
** Returns pointer to the clcb
**
*******************************************************************************/
tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_conn_id (UINT16 conn_id)
{
tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
UINT8 i;
for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
{
if (p_clcb->in_use &&
p_clcb->bta_conn_id == conn_id)
return p_clcb;
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gattc_clcb_alloc
**
** Description allocate CLCB
**
** Returns pointer to the clcb
**
*******************************************************************************/
tBTA_GATTC_CLCB * bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
tBTA_TRANSPORT transport)
{
UINT8 i_clcb = 0;
tBTA_GATTC_CLCB *p_clcb = NULL;
for (i_clcb = 0; i_clcb < BTA_GATTC_CLCB_MAX; i_clcb++)
{
if (!bta_gattc_cb.clcb[i_clcb].in_use)
{
#if BTA_GATT_DEBUG == TRUE
APPL_TRACE_DEBUG("bta_gattc_clcb_alloc: found clcb[%d] available",i_clcb);
#endif
p_clcb = &bta_gattc_cb.clcb[i_clcb];
p_clcb->in_use = TRUE;
p_clcb->status = BTA_GATT_OK;
p_clcb->transport = transport;
bdcpy(p_clcb->bda, remote_bda);
p_clcb->p_rcb = bta_gattc_cl_get_regcb(client_if);
if ((p_clcb->p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
p_clcb->p_srcb = bta_gattc_srcb_alloc(remote_bda);
if (p_clcb->p_rcb != NULL && p_clcb->p_srcb != NULL)
{
p_clcb->p_srcb->num_clcb ++;
p_clcb->p_rcb->num_clcb ++;
}
else
{
/* release this clcb if clcb or srcb allocation failed */
p_clcb->in_use = FALSE;
p_clcb = NULL;
}
break;
}
}
return p_clcb;
}
/*******************************************************************************
**
** Function bta_gattc_find_alloc_clcb
**
** Description find or allocate CLCB if not found.
**
** Returns pointer to the clcb
**
*******************************************************************************/
tBTA_GATTC_CLCB *bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
tBTA_TRANSPORT transport)
{
tBTA_GATTC_CLCB *p_clcb ;
if ((p_clcb = bta_gattc_find_clcb_by_cif(client_if, remote_bda, transport)) == NULL)
{
p_clcb = bta_gattc_clcb_alloc(client_if, remote_bda, transport);
}
return p_clcb;
}
/*******************************************************************************
**
** Function bta_gattc_clcb_dealloc
**
** Description Deallocte a clcb
**
** Returns pointer to the clcb
**
*******************************************************************************/
void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB *p_clcb)
{
tBTA_GATTC_SERV *p_srcb = NULL;
if (p_clcb)
{
p_srcb = p_clcb->p_srcb;
if (p_srcb->num_clcb)
p_srcb->num_clcb --;
if (p_clcb->p_rcb->num_clcb)
p_clcb->p_rcb->num_clcb --;
/* if the srcb is no longer needed, reset the state */
if ( p_srcb->num_clcb == 0)
{
p_srcb->connected = FALSE;
p_srcb->state = BTA_GATTC_SERV_IDLE;
p_srcb->mtu = 0;
}
utl_freebuf((void **)&p_clcb->p_q_cmd);
memset(p_clcb, 0, sizeof(tBTA_GATTC_CLCB));
}
else
{
APPL_TRACE_ERROR("bta_gattc_clcb_dealloc p_clcb=NULL");
}
}
/*******************************************************************************
**
** Function bta_gattc_find_srcb
**
** Description find server cache by remote bd address currently in use
**
** Returns pointer to the server cache.
**
*******************************************************************************/
tBTA_GATTC_SERV * bta_gattc_find_srcb(BD_ADDR bda)
{
tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
UINT8 i;
for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++)
{
if (p_srcb->in_use && bdcmp(p_srcb->server_bda, bda) == 0)
return p_srcb;
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gattc_find_srvr_cache
**
** Description find server cache by remote bd address
**
** Returns pointer to the server cache.
**
*******************************************************************************/
tBTA_GATTC_SERV * bta_gattc_find_srvr_cache(BD_ADDR bda)
{
tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
UINT8 i;
for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++)
{
if (bdcmp(p_srcb->server_bda, bda) == 0)
return p_srcb;
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gattc_find_scb_by_cid
**
** Description find server control block by connection ID
**
** Returns pointer to the server cache.
**
*******************************************************************************/
tBTA_GATTC_SERV * bta_gattc_find_scb_by_cid (UINT16 conn_id)
{
tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
if (p_clcb)
return p_clcb->p_srcb;
else
return NULL;
}
/*******************************************************************************
**
** Function bta_gattc_srcb_alloc
**
** Description allocate server cache control block
**
** Returns pointer to the server cache.
**
*******************************************************************************/
tBTA_GATTC_SERV * bta_gattc_srcb_alloc(BD_ADDR bda)
{
tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0],
*p_recycle = NULL;
BOOLEAN found = FALSE;
UINT8 i;
for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_tcb ++)
{
if (!p_tcb->in_use)
{
found = TRUE;
break;
}
else if (!p_tcb->connected)
{
p_recycle = p_tcb;
}
}
/* if not found, try to recycle one known device */
if (!found && !p_recycle)
p_tcb = NULL;
else if (!found && p_recycle)
p_tcb = p_recycle;
if (p_tcb != NULL)
{
while (!GKI_queue_is_empty(&p_tcb->cache_buffer))
GKI_freebuf (GKI_dequeue (&p_tcb->cache_buffer));
utl_freebuf((void **)&p_tcb->p_srvc_list);
memset(p_tcb, 0 , sizeof(tBTA_GATTC_SERV));
p_tcb->in_use = TRUE;
bdcpy(p_tcb->server_bda, bda);
}
return p_tcb;
}
/*******************************************************************************
**
** Function bta_gattc_enqueue
**
** Description enqueue a client request in clcb.
**
** Returns success or failure.
**
*******************************************************************************/
BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
if (p_clcb->p_q_cmd == NULL)
{
p_clcb->p_q_cmd = p_data;
}
else
{
APPL_TRACE_ERROR("already has a pending command!!");
/* skip the callback now. ----- need to send callback ? */
}
return (p_clcb->p_q_cmd != NULL) ? TRUE : FALSE;
}
/*******************************************************************************
**
** Function bta_gattc_pack_attr_uuid
**
** Description pack UUID into a stream.
**
** Returns
**
*******************************************************************************/
void bta_gattc_pack_attr_uuid(tBTA_GATTC_CACHE_ATTR *p_attr, tBT_UUID *p_uuid)
{
UINT8 *pp = (UINT8 *)p_attr->p_uuid;
memset(p_uuid, 0, sizeof(tBT_UUID));
p_uuid->len = p_attr->uuid_len;
if (p_attr->uuid_len == LEN_UUID_16)
{
STREAM_TO_UINT16(p_uuid->uu.uuid16, pp);
}
else
{
memcpy(p_uuid->uu.uuid128, pp, LEN_UUID_128);
}
return;
}
/*******************************************************************************
**
** Function bta_gattc_cpygattid
**
** Description copy two tBTA_GATT_ID value
**
** Returns
**
*******************************************************************************/
void bta_gattc_cpygattid(tBTA_GATT_ID *p_des, tBTA_GATT_ID *p_src)
{
memset ((void *)p_des, 0, sizeof(tBTA_GATT_ID));
p_des->inst_id = p_src->inst_id;
p_des->uuid.len = p_src->uuid.len;
if (p_des->uuid.len == LEN_UUID_16)
{
p_des->uuid.uu.uuid16 = p_src->uuid.uu.uuid16;
}
else if (p_des->uuid.len == LEN_UUID_128)
{
memcpy(p_des->uuid.uu.uuid128, p_src->uuid.uu.uuid128, LEN_UUID_128);
}
}
/*******************************************************************************
**
** Function bta_gattc_gattid_compare
**
** Description compare two tBTA_GATT_ID type of pointer
**
** Returns
**
*******************************************************************************/
BOOLEAN bta_gattc_gattid_compare(tBTA_GATT_ID *p_src, tBTA_GATT_ID *p_tar)
{
if (p_src->inst_id == p_tar->inst_id &&
bta_gattc_uuid_compare (&p_src->uuid, &p_tar->uuid, TRUE ))
return TRUE;
else
return FALSE;
}
/*******************************************************************************
**
** Function bta_gattc_srvcid_compare
**
** Description compare two tBTA_GATT_SRVC_ID type of pointer
**
** Returns
**
*******************************************************************************/
BOOLEAN bta_gattc_srvcid_compare(tBTA_GATT_SRVC_ID *p_src, tBTA_GATT_SRVC_ID *p_tar)
{
if (p_src->is_primary == p_tar->is_primary &&
bta_gattc_gattid_compare (&p_src->id, &p_tar->id))
return TRUE;
else
return FALSE;
}
/*******************************************************************************
**
** Function bta_gattc_charid_compare
**
** Description compare two tBTA_GATTC_CHAR_ID type of pointer
**
** Returns
**
*******************************************************************************/
BOOLEAN bta_gattc_charid_compare(tBTA_GATTC_CHAR_ID *p_src, tBTA_GATTC_CHAR_ID *p_tar)
{
if (bta_gattc_gattid_compare (&p_src->char_id, &p_tar->char_id) &&
bta_gattc_srvcid_compare (&p_src->srvc_id, &p_tar->srvc_id))
return TRUE;
else
return FALSE;
}
/*******************************************************************************
**
** Function bta_gattc_check_notif_registry
**
** Description check if the service notificaition has been registered.
**
** Returns
**
*******************************************************************************/
BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB *p_clreg, tBTA_GATTC_SERV *p_srcb,
tBTA_GATTC_NOTIFY *p_notify)
{
UINT8 i;
for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
{
if (p_clreg->notif_reg[i].in_use &&
bdcmp(p_clreg->notif_reg[i].remote_bda, p_srcb->server_bda) == 0 &&
bta_gattc_charid_compare (&p_clreg->notif_reg[i].char_id, &p_notify->char_id))
{
APPL_TRACE_DEBUG("Notification registered!");
return TRUE;
}
}
return FALSE;
}
/*******************************************************************************
**
** Function bta_gattc_clear_notif_registration
**
** Description clear up the notification registration information by BD_ADDR.
**
** Returns None.
**
*******************************************************************************/
void bta_gattc_clear_notif_registration(UINT16 conn_id)
{
BD_ADDR remote_bda;
tBTA_GATTC_IF gatt_if;
tBTA_GATTC_RCB *p_clrcb ;
UINT8 i;
tGATT_TRANSPORT transport;
if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport))
{
if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) != NULL)
{
for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
{
if (p_clrcb->notif_reg[i].in_use &&
!bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda))
memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
}
}
}
else
{
APPL_TRACE_ERROR("can not clear indication/notif registration for unknown app");
}
return;
}
/*******************************************************************************
**
** Function bta_gattc_pack_cb_data
**
** Description pack the data from read response into callback data structure.
**
** Returns
**
*******************************************************************************/
tBTA_GATT_STATUS bta_gattc_pack_read_cb_data(tBTA_GATTC_SERV *p_srcb,
tBT_UUID *p_descr_uuid,
tGATT_VALUE *p_attr,
tBTA_GATT_READ_VAL *p_value)
{
UINT8 i = 0, *pp = p_attr->value;
tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_CHAR_AGG_FORMAT}};
UINT16 handle;
tBTA_GATT_STATUS status = BTA_GATT_OK;
/* GATT_UUID_CHAR_AGG_FORMAT */
if (bta_gattc_uuid_compare (&uuid, p_descr_uuid, TRUE))
{
while (p_attr->len >= 2 && i < BTA_GATTC_MULTI_MAX)
{
STREAM_TO_UINT16(handle, pp);
if (bta_gattc_handle2id(p_srcb,
handle,
&p_value->aggre_value.pre_format[i].char_id.srvc_id,
&p_value->aggre_value.pre_format[i].char_id.char_id,
&p_value->aggre_value.pre_format[i].descr_id) == FALSE)
{
status = BTA_GATT_INTERNAL_ERROR;
APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x", handle);
break;
}
i ++;
p_attr->len -= 2;
}
p_value->aggre_value.num_pres_fmt = i;
}
else
{
/* all others, take as raw format */
p_value->unformat.len = p_attr->len;
p_value->unformat.p_value = p_attr->value;
}
return status;
}
/*******************************************************************************
**
** Function bta_gattc_mark_bg_conn
**
** Description mark background connection status when a bg connection is initiated
** or terminated.
**
** Returns TRUE if success; FALSE otherwise.
**
*******************************************************************************/
BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR_PTR remote_bda_ptr,
BOOLEAN add, BOOLEAN is_listen)
{
tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0];
UINT8 i = 0;
tBTA_GATTC_CIF_MASK *p_cif_mask;
for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++)
{
if (p_bg_tck->in_use &&
((remote_bda_ptr != NULL && bdcmp(p_bg_tck->remote_bda, remote_bda_ptr) == 0) ||
(remote_bda_ptr == NULL && bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0)))
{
p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
if (add)
/* mask on the cif bit */
*p_cif_mask |= (1 <<(client_if - 1));
else
{
if (client_if != 0)
*p_cif_mask &= (~(1 <<(client_if - 1)));
else
*p_cif_mask = 0;
}
/* no BG connection for this device, make it available */
if (p_bg_tck->cif_mask == 0 && p_bg_tck->cif_adv_mask == 0)
{
memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK));
}
return TRUE;
}
}
if (!add)
{
if (remote_bda_ptr)
{
// bdstr_t bdstr = {0};
char bdstr[18] = {0};
APPL_TRACE_ERROR("%s unable to find the bg connection mask for: %s", __func__,
bdaddr_to_string((bt_bdaddr_t *)remote_bda_ptr, bdstr, sizeof(bdstr)));
}
return FALSE;
}
else /* adding a new device mask */
{
for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0];
i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++)
{
if (!p_bg_tck->in_use)
{
p_bg_tck->in_use = TRUE;
if (remote_bda_ptr)
bdcpy(p_bg_tck->remote_bda, remote_bda_ptr);
else
bdcpy(p_bg_tck->remote_bda, dummy_bda);
p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
*p_cif_mask = (1 <<(client_if - 1));
return TRUE;
}
}
APPL_TRACE_ERROR("no available space to mark the bg connection status");
return FALSE;
}
}
/*******************************************************************************
**
** Function bta_gattc_check_bg_conn
**
** Description check if this is a background connection background connection.
**
** Returns TRUE if success; FALSE otherwise.
**
*******************************************************************************/
BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR remote_bda, UINT8 role)
{
tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0];
UINT8 i = 0;
BOOLEAN is_bg_conn = FALSE;
for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX && !is_bg_conn; i ++, p_bg_tck ++)
{
if (p_bg_tck->in_use &&
(bdcmp(p_bg_tck->remote_bda, remote_bda) == 0 ||
bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0))
{
if (((p_bg_tck->cif_mask &(1 <<(client_if - 1))) != 0) &&
role == HCI_ROLE_MASTER)
is_bg_conn = TRUE;
if (((p_bg_tck->cif_adv_mask &(1 <<(client_if - 1))) != 0) &&
role == HCI_ROLE_SLAVE)
is_bg_conn = TRUE;
}
}
return is_bg_conn;
}
/*******************************************************************************
**
** Function bta_gattc_send_open_cback
**
** Description send open callback
**
** Returns
**
*******************************************************************************/
void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
BD_ADDR remote_bda, UINT16 conn_id,
tBTA_TRANSPORT transport, UINT16 mtu)
{
tBTA_GATTC cb_data;
if (p_clreg->p_cback)
{
memset(&cb_data, 0, sizeof(tBTA_GATTC));
cb_data.open.status = status;
cb_data.open.client_if = p_clreg->client_if;
cb_data.open.conn_id = conn_id;
cb_data.open.mtu = mtu;
cb_data.open.transport = transport;
bdcpy(cb_data.open.remote_bda, remote_bda);
(*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data);
}
}
/*******************************************************************************
**
** Function bta_gattc_conn_alloc
**
** Description allocate connection tracking spot
**
** Returns pointer to the clcb
**
*******************************************************************************/
tBTA_GATTC_CONN * bta_gattc_conn_alloc(BD_ADDR remote_bda)
{
UINT8 i_conn = 0;
tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
{
if (!p_conn->in_use)
{
#if BTA_GATT_DEBUG == TRUE
APPL_TRACE_DEBUG("bta_gattc_conn_alloc: found conn_track[%d] available",i_conn);
#endif
p_conn->in_use = TRUE;
bdcpy(p_conn->remote_bda, remote_bda);
return p_conn;
}
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gattc_conn_find
**
** Description allocate connection tracking spot
**
** Returns pointer to the clcb
**
*******************************************************************************/
tBTA_GATTC_CONN * bta_gattc_conn_find(BD_ADDR remote_bda)
{
UINT8 i_conn = 0;
tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
{
if (p_conn->in_use && bdcmp(remote_bda, p_conn->remote_bda) == 0)
{
#if BTA_GATT_DEBUG == TRUE
APPL_TRACE_DEBUG("bta_gattc_conn_find: found conn_track[%d] matched",i_conn);
#endif
return p_conn;
}
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gattc_conn_find_alloc
**
** Description find or allocate connection tracking spot
**
** Returns pointer to the clcb
**
*******************************************************************************/
tBTA_GATTC_CONN * bta_gattc_conn_find_alloc(BD_ADDR remote_bda)
{
tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda);
if (p_conn == NULL)
{
p_conn = bta_gattc_conn_alloc(remote_bda);
}
return p_conn;
}
/*******************************************************************************
**
** Function bta_gattc_conn_dealloc
**
** Description de-allocate connection tracking spot
**
** Returns pointer to the clcb
**
*******************************************************************************/
BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda)
{
tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda);
if (p_conn != NULL)
{
p_conn->in_use = FALSE;
memset(p_conn->remote_bda, 0, BD_ADDR_LEN);
return TRUE;
}
return FALSE;
}
/*******************************************************************************
**
** Function bta_gattc_find_int_conn_clcb
**
** Description try to locate a clcb when an internal connecion event arrives.
**
** Returns pointer to the clcb
**
*******************************************************************************/
tBTA_GATTC_CLCB * bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA *p_msg)
{
tBTA_GATTC_CLCB *p_clcb = NULL;
if (p_msg->int_conn.role == HCI_ROLE_SLAVE)
bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda);
/* try to locate a logic channel */
if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
p_msg->int_conn.remote_bda,
p_msg->int_conn.transport)) == NULL)
{
/* for a background connection or listening connection */
if (/*p_msg->int_conn.role == HCI_ROLE_SLAVE || */
bta_gattc_check_bg_conn(p_msg->int_conn.client_if,
p_msg->int_conn.remote_bda,
p_msg->int_conn.role))
{
/* allocate a new channel */
p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if,
p_msg->int_conn.remote_bda,
p_msg->int_conn.transport);
}
}
return p_clcb;
}
/*******************************************************************************
**
** Function bta_gattc_find_int_disconn_clcb
**
** Description try to locate a clcb when an internal disconnect callback arrives.
**
** Returns pointer to the clcb
**
*******************************************************************************/
tBTA_GATTC_CLCB * bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA *p_msg)
{
tBTA_GATTC_CLCB *p_clcb = NULL;
bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda);
if ((p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific)) == NULL)
{
/* connection attempt failed, send connection callback event */
p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
p_msg->int_conn.remote_bda,
p_msg->int_conn.transport);
}
if (p_clcb == NULL)
{
APPL_TRACE_DEBUG(" disconnection ID: [%d] not used by BTA",
p_msg->int_conn.hdr.layer_specific);
}
return p_clcb;
}
#endif /* BTA_GATT_INCLUDED */

View file

@ -0,0 +1,992 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the GATT Server action functions for the state
* machine.
*
******************************************************************************/
#include "bt_target.h"
#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
#include "utl.h"
#include "gki.h"
#include "bta_sys.h"
#include "bta_gatts_int.h"
#include "bta_gatts_co.h"
#include "btm_ble_api.h"
// #include "btif/include/btif_debug_conn.h"
#include <string.h>
static void bta_gatts_nv_save_cback(BOOLEAN is_saved, tGATTS_HNDL_RANGE *p_hndl_range);
static BOOLEAN bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd, tGATTS_SRV_CHG_REQ *p_req,
tGATTS_SRV_CHG_RSP *p_rsp);
static void bta_gatts_conn_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
BOOLEAN connected, tGATT_DISCONN_REASON reason,
tGATT_TRANSPORT transport);
static void bta_gatts_send_request_cback (UINT16 conn_id,
UINT32 trans_id,
tGATTS_REQ_TYPE req_type, tGATTS_DATA *p_data);
static void bta_gatts_cong_cback (UINT16 conn_id, BOOLEAN congested);
static tGATT_CBACK bta_gatts_cback =
{
bta_gatts_conn_cback,
NULL,
NULL,
NULL,
bta_gatts_send_request_cback,
NULL,
bta_gatts_cong_cback
};
tGATT_APPL_INFO bta_gatts_nv_cback =
{
bta_gatts_nv_save_cback,
bta_gatts_nv_srv_chg_cback
};
/*******************************************************************************
**
** Function bta_gatts_nv_save_cback
**
** Description NV save callback function.
**
** Parameter is_add: true is to add a handle range; otherwise is to delete.
** Returns none.
**
*******************************************************************************/
static void bta_gatts_nv_save_cback(BOOLEAN is_add, tGATTS_HNDL_RANGE *p_hndl_range)
{
bta_gatts_co_update_handle_range(is_add, (tBTA_GATTS_HNDL_RANGE *)p_hndl_range);
}
/*******************************************************************************
**
** Function bta_gatts_nv_srv_chg_cback
**
** Description NV save callback function.
**
** Parameter is_add: true is to add a handle range; otherwise is to delete.
** Returns none.
**
*******************************************************************************/
static BOOLEAN bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd,
tGATTS_SRV_CHG_REQ *p_req, tGATTS_SRV_CHG_RSP *p_rsp)
{
return bta_gatts_co_srv_chg((tBTA_GATTS_SRV_CHG_CMD) cmd,
(tBTA_GATTS_SRV_CHG_REQ *) p_req,
(tBTA_GATTS_SRV_CHG_RSP *) p_rsp);
}
/*******************************************************************************
**
** Function bta_gatts_enable
**
** Description enable BTA GATTS module.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_enable(tBTA_GATTS_CB *p_cb)
{
UINT8 index=0;
tBTA_GATTS_HNDL_RANGE handle_range;
if (p_cb->enabled)
{
APPL_TRACE_DEBUG("GATTS already enabled.");
}
else
{
memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
p_cb->enabled = TRUE;
while ( bta_gatts_co_load_handle_range(index, &handle_range))
{
GATTS_AddHandleRange((tGATTS_HNDL_RANGE *)&handle_range);
memset(&handle_range, 0, sizeof(tGATTS_HNDL_RANGE));
index++;
}
APPL_TRACE_DEBUG("bta_gatts_enable: num of handle range added=%d", index);
if (!GATTS_NVRegister(&bta_gatts_nv_cback))
{
APPL_TRACE_ERROR("BTA GATTS NV register failed.");
}
}
}
/*******************************************************************************
**
** Function bta_gatts_api_disable
**
** Description disable BTA GATTS module.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_api_disable(tBTA_GATTS_CB *p_cb)
{
UINT8 i;
if (p_cb->enabled)
{
for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++)
{
if (p_cb->rcb[i].in_use)
{
GATT_Deregister(p_cb->rcb[i].gatt_if);
}
}
memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
}
else
{
APPL_TRACE_ERROR("GATTS not enabled");
}
}
/*******************************************************************************
**
** Function bta_gatts_register
**
** Description register an application.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_register(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
{
tBTA_GATTS_INT_START_IF *p_buf;
tBTA_GATTS cb_data;
tBTA_GATT_STATUS status = BTA_GATT_OK;
UINT8 i, first_unuse = 0xff;
if (p_cb->enabled == FALSE)
{
bta_gatts_enable(p_cb);
}
for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++)
{
if (p_cb->rcb[i].in_use)
{
if (bta_gatts_uuid_compare(p_cb->rcb[i].app_uuid, p_msg->api_reg.app_uuid))
{
APPL_TRACE_ERROR("application already registered.\n");
status = BTA_GATT_DUP_REG;
break;
}
}
}
if (status == BTA_GATT_OK)
{
for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++)
{
if (first_unuse == 0xff && !p_cb->rcb[i].in_use)
{
first_unuse = i;
break;
}
}
cb_data.reg_oper.server_if = BTA_GATTS_INVALID_IF;
// btla-specific ++
memcpy(&cb_data.reg_oper.uuid, &p_msg->api_reg.app_uuid, sizeof(tBT_UUID));
// btla-specific --
if (first_unuse != 0xff)
{
APPL_TRACE_ERROR("register application first_unuse rcb_idx = %d", first_unuse);
p_cb->rcb[first_unuse].in_use = TRUE;
p_cb->rcb[first_unuse].p_cback = p_msg->api_reg.p_cback;
memcpy(&p_cb->rcb[first_unuse].app_uuid, &p_msg->api_reg.app_uuid, sizeof(tBT_UUID));
cb_data.reg_oper.server_if =
p_cb->rcb[first_unuse].gatt_if =
GATT_Register(&p_msg->api_reg.app_uuid, &bta_gatts_cback);
if ( !p_cb->rcb[first_unuse].gatt_if)
{
status = BTA_GATT_NO_RESOURCES;
}
else
{
if ((p_buf =
(tBTA_GATTS_INT_START_IF *) GKI_getbuf(sizeof(tBTA_GATTS_INT_START_IF))) != NULL)
{
p_buf->hdr.event = BTA_GATTS_INT_START_IF_EVT;
p_buf->server_if = p_cb->rcb[first_unuse].gatt_if;
bta_sys_sendmsg(p_buf);
}
else
{
status = BTA_GATT_NO_RESOURCES;
memset( &p_cb->rcb[first_unuse], 0 , sizeof(tBTA_GATTS_RCB));
}
}
}
else
{
status = BTA_GATT_NO_RESOURCES;
}
}
cb_data.reg_oper.status = status;
if (p_msg->api_reg.p_cback)
(*p_msg->api_reg.p_cback)(BTA_GATTS_REG_EVT, &cb_data);
LOG_ERROR("status=%x\n",status);
}
/*******************************************************************************
**
** Function bta_gatts_start_if
**
** Description start an application interface.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_start_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
{
UNUSED(p_cb);
if (bta_gatts_find_app_rcb_by_app_if(p_msg->int_start_if.server_if))
{
GATT_StartIf(p_msg->int_start_if.server_if);
}
else
{
APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",
p_msg->int_start_if.server_if );
}
}
/*******************************************************************************
**
** Function bta_gatts_deregister
**
** Description deregister an application.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_deregister(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
{
tBTA_GATT_STATUS status = BTA_GATT_ERROR;
tBTA_GATTS_CBACK *p_cback = NULL;
UINT8 i;
tBTA_GATTS cb_data;
cb_data.reg_oper.server_if = p_msg->api_dereg.server_if;
cb_data.reg_oper.status = status;
for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++)
{
if (p_cb->rcb[i].in_use && p_cb->rcb[i].gatt_if == p_msg->api_dereg.server_if)
{
p_cback = p_cb->rcb[i].p_cback;
status = BTA_GATT_OK;
/* deregister the app */
GATT_Deregister(p_cb->rcb[i].gatt_if);
/* reset cb */
memset(&p_cb->rcb[i], 0, sizeof(tBTA_GATTS_RCB));
cb_data.reg_oper.status = status;
break;
}
}
if (p_cback)
{
(*p_cback)(BTA_GATTS_DEREG_EVT, &cb_data);
}
else
{
APPL_TRACE_ERROR("application not registered.");
}
}
/*******************************************************************************
**
** Function bta_gatts_create_srvc
**
** Description action function to create a service.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_create_srvc(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg)
{
UINT8 rcb_idx;
tBTA_GATTS cb_data;
UINT8 srvc_idx;
UINT16 service_id = 0;
cb_data.create.status = BTA_GATT_ERROR;
rcb_idx = bta_gatts_find_app_rcb_idx_by_app_if(p_cb, p_msg->api_create_svc.server_if);
APPL_TRACE_ERROR("create service rcb_idx = %d", rcb_idx);
if (rcb_idx != BTA_GATTS_INVALID_APP)
{
if ((srvc_idx = bta_gatts_alloc_srvc_cb(p_cb, rcb_idx)) != BTA_GATTS_INVALID_APP)
{
/* create the service now */
service_id = GATTS_CreateService (p_cb->rcb[rcb_idx].gatt_if,
&p_msg->api_create_svc.service_uuid,
p_msg->api_create_svc.inst,
p_msg->api_create_svc.num_handle,
p_msg->api_create_svc.is_pri);
if (service_id != 0)
{
memcpy(&p_cb->srvc_cb[srvc_idx].service_uuid,
&p_msg->api_create_svc.service_uuid, sizeof(tBT_UUID));
p_cb->srvc_cb[srvc_idx].service_id = service_id;
p_cb->srvc_cb[srvc_idx].inst_num = p_msg->api_create_svc.inst;
p_cb->srvc_cb[srvc_idx].idx = srvc_idx;
cb_data.create.status = BTA_GATT_OK;
cb_data.create.service_id = service_id;
// btla-specific ++
cb_data.create.is_primary = p_msg->api_create_svc.is_pri;
// btla-specific --
cb_data.create.server_if = p_cb->rcb[rcb_idx].gatt_if;
}
else
{
cb_data.status = BTA_GATT_ERROR;
memset(&p_cb->srvc_cb[srvc_idx], 0, sizeof(tBTA_GATTS_SRVC_CB));
APPL_TRACE_ERROR("service creation failed.");
}
// btla-specific ++
memcpy(&cb_data.create.uuid, &p_msg->api_create_svc.service_uuid, sizeof(tBT_UUID));
cb_data.create.svc_instance= p_msg->api_create_svc.inst;
// btla-specific --
}
if (p_cb->rcb[rcb_idx].p_cback)
(* p_cb->rcb[rcb_idx].p_cback)(BTA_GATTS_CREATE_EVT, &cb_data);
}
else /* application not registered */
{
APPL_TRACE_ERROR("Application not registered");
}
}
/*******************************************************************************
**
** Function bta_gatts_add_include_srvc
**
** Description action function to add an included service.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_add_include_srvc(tBTA_GATTS_SRVC_CB *p_srvc_cb,tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
UINT16 attr_id = 0;
tBTA_GATTS cb_data;
attr_id = GATTS_AddIncludeService(p_msg->api_add_incl_srvc.hdr.layer_specific,
p_msg->api_add_incl_srvc.included_service_id);
cb_data.add_result.server_if = p_rcb->gatt_if;
cb_data.add_result.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
cb_data.add_result.attr_id = attr_id;
if (attr_id)
{
cb_data.add_result.status = BTA_GATT_OK;
}
else
{
cb_data.add_result.status = BTA_GATT_ERROR;
}
if (p_rcb->p_cback)
(*p_rcb->p_cback)(BTA_GATTS_ADD_INCL_SRVC_EVT, &cb_data);
}
/*******************************************************************************
**
** Function bta_gatts_add_char
**
** Description action function to add characteristic.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_add_char(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
UINT16 attr_id = 0;
tBTA_GATTS cb_data;
attr_id = GATTS_AddCharacteristic(p_msg->api_add_char.hdr.layer_specific,
&p_msg->api_add_char.char_uuid,
p_msg->api_add_char.perm,
p_msg->api_add_char.property);
cb_data.add_result.server_if = p_rcb->gatt_if;
cb_data.add_result.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
cb_data.add_result.attr_id = attr_id;
// btla-specific ++
memcpy(&cb_data.add_result.char_uuid, &p_msg->api_add_char.char_uuid, sizeof(tBT_UUID));
// btla-specific --
if (attr_id)
{
cb_data.add_result.status = BTA_GATT_OK;
}
else
{
cb_data.add_result.status = BTA_GATT_ERROR;
}
if (p_rcb->p_cback)
(*p_rcb->p_cback)(BTA_GATTS_ADD_CHAR_EVT, &cb_data);
}
/*******************************************************************************
**
** Function bta_gatts_add_char_descr
**
** Description action function to add characteristic descriptor.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_add_char_descr(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
UINT16 attr_id = 0;
tBTA_GATTS cb_data;
attr_id = GATTS_AddCharDescriptor(p_msg->api_add_char_descr.hdr.layer_specific,
p_msg->api_add_char_descr.perm,
&p_msg->api_add_char_descr.descr_uuid);
cb_data.add_result.server_if = p_rcb->gatt_if;
cb_data.add_result.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
cb_data.add_result.attr_id = attr_id;
// btla-specific ++
memcpy(&cb_data.add_result.char_uuid, &p_msg->api_add_char_descr.descr_uuid, sizeof(tBT_UUID));
// btla-specific --
if (attr_id)
{
cb_data.add_result.status = BTA_GATT_OK;
}
else
{
cb_data.add_result.status = BTA_GATT_ERROR;
}
if (p_rcb->p_cback)
(*p_rcb->p_cback)(BTA_GATTS_ADD_CHAR_DESCR_EVT, &cb_data);
}
/*******************************************************************************
**
** Function bta_gatts_delete_service
**
** Description action function to delete a service.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_delete_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
tBTA_GATTS cb_data;
cb_data.srvc_oper.server_if = p_rcb->gatt_if;
cb_data.srvc_oper.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
if (GATTS_DeleteService(p_rcb->gatt_if,
&p_srvc_cb->service_uuid,
p_srvc_cb->inst_num))
{
cb_data.srvc_oper.status = BTA_GATT_OK;
memset(p_srvc_cb, 0, sizeof(tBTA_GATTS_SRVC_CB));
}
else
{
cb_data.srvc_oper.status = BTA_GATT_ERROR;
}
if (p_rcb->p_cback)
(*p_rcb->p_cback)(BTA_GATTS_DELELTE_EVT, &cb_data);
}
/*******************************************************************************
**
** Function bta_gatts_start_service
**
** Description action function to start a service.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_start_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
tBTA_GATTS cb_data;
cb_data.srvc_oper.server_if = p_rcb->gatt_if;
cb_data.srvc_oper.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
if (GATTS_StartService(p_rcb->gatt_if,
p_srvc_cb->service_id,
p_msg->api_start.transport) == GATT_SUCCESS)
{
APPL_TRACE_DEBUG("bta_gatts_start_service service_id= %d", p_srvc_cb->service_id);
cb_data.srvc_oper.status = BTA_GATT_OK;
}
else
{
cb_data.srvc_oper.status = BTA_GATT_ERROR;
}
if (p_rcb->p_cback)
(*p_rcb->p_cback)(BTA_GATTS_START_EVT, &cb_data);
}
/*******************************************************************************
**
** Function bta_gatts_stop_service
**
** Description action function to stop a service.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_stop_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
tBTA_GATTS cb_data;
UNUSED(p_msg);
GATTS_StopService(p_srvc_cb->service_id);
cb_data.srvc_oper.server_if = p_rcb->gatt_if;
cb_data.srvc_oper.service_id = p_srvc_cb->service_id;
cb_data.srvc_oper.status = BTA_GATT_OK;
APPL_TRACE_ERROR("bta_gatts_stop_service service_id= %d", p_srvc_cb->service_id);
if (p_rcb->p_cback)
(*p_rcb->p_cback)(BTA_GATTS_STOP_EVT, &cb_data);
}
/*******************************************************************************
**
** Function bta_gatts_send_rsp
**
** Description GATTS send response.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_send_rsp (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg)
{
UNUSED(p_cb);
if (GATTS_SendRsp (p_msg->api_rsp.hdr.layer_specific,
p_msg->api_rsp.trans_id,
p_msg->api_rsp.status,
(tGATTS_RSP *)p_msg->api_rsp.p_rsp) != GATT_SUCCESS)
{
APPL_TRACE_ERROR("Sending response failed");
}
}
/*******************************************************************************
**
** Function bta_gatts_indicate_handle
**
** Description GATTS send handle value indication or notification.
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_indicate_handle (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_SRVC_CB *p_srvc_cb;
tBTA_GATTS_RCB *p_rcb = NULL;
tBTA_GATT_STATUS status = BTA_GATT_ILLEGAL_PARAMETER;
tGATT_IF gatt_if;
BD_ADDR remote_bda;
tBTA_TRANSPORT transport;
tBTA_GATTS cb_data;
p_srvc_cb = bta_gatts_find_srvc_cb_by_attr_id (p_cb, p_msg->api_indicate.attr_id);
if (p_srvc_cb )
{
if (GATT_GetConnectionInfor(p_msg->api_indicate.hdr.layer_specific,
&gatt_if, remote_bda, &transport))
{
p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
if (p_msg->api_indicate.need_confirm)
status = GATTS_HandleValueIndication (p_msg->api_indicate.hdr.layer_specific,
p_msg->api_indicate.attr_id,
p_msg->api_indicate.len,
p_msg->api_indicate.value);
else
status = GATTS_HandleValueNotification (p_msg->api_indicate.hdr.layer_specific,
p_msg->api_indicate.attr_id,
p_msg->api_indicate.len,
p_msg->api_indicate.value);
/* if over BR_EDR, inform PM for mode change */
if (transport == BTA_TRANSPORT_BR_EDR)
{
bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
}
}
else
{
APPL_TRACE_ERROR("Unknown connection ID: %d fail sending notification",
p_msg->api_indicate.hdr.layer_specific);
}
if ((status != GATT_SUCCESS || !p_msg->api_indicate.need_confirm) &&
p_rcb && p_cb->rcb[p_srvc_cb->rcb_idx].p_cback)
{
cb_data.req_data.status = status;
cb_data.req_data.conn_id = p_msg->api_indicate.hdr.layer_specific;
(*p_rcb->p_cback)(BTA_GATTS_CONF_EVT, &cb_data);
}
}
else
{
APPL_TRACE_ERROR("Not an registered servce attribute ID: 0x%04x",
p_msg->api_indicate.attr_id);
}
}
/*******************************************************************************
**
** Function bta_gatts_open
**
** Description
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb=NULL;
tBTA_GATT_STATUS status= BTA_GATT_ERROR;
UINT16 conn_id;
UNUSED(p_cb);
if ((p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_open.server_if)) != NULL)
{
/* should always get the connection ID */
if (GATT_Connect(p_rcb->gatt_if, p_msg->api_open.remote_bda,
p_msg->api_open.is_direct, p_msg->api_open.transport))
{
status = BTA_GATT_OK;
if (GATT_GetConnIdIfConnected(p_rcb->gatt_if, p_msg->api_open.remote_bda,
&conn_id, p_msg->api_open.transport))
{
status = BTA_GATT_ALREADY_OPEN;
}
}
}
else
{
APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_open.server_if);
}
if (p_rcb && p_rcb->p_cback)
(*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT, (tBTA_GATTS *)&status);
}
/*******************************************************************************
**
** Function bta_gatts_cancel_open
**
** Description
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_cancel_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb;
tBTA_GATT_STATUS status= BTA_GATT_ERROR;
UNUSED(p_cb);
if ((p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_cancel_open.server_if)) != NULL)
{
if (!GATT_CancelConnect(p_rcb->gatt_if, p_msg->api_cancel_open.remote_bda,
p_msg->api_cancel_open.is_direct))
{
APPL_TRACE_ERROR("bta_gatts_cancel_open failed for open request");
}
else
{
status= BTA_GATT_OK;
}
}
else
{
APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_cancel_open.server_if);
}
if (p_rcb && p_rcb->p_cback)
(*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT, (tBTA_GATTS *)&status);
}
/*******************************************************************************
**
** Function bta_gatts_close
**
** Description
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_close (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb;
tBTA_GATT_STATUS status= BTA_GATT_ERROR;
tGATT_IF gatt_if;
BD_ADDR remote_bda;
tBTA_GATT_TRANSPORT transport;
UNUSED(p_cb);
if (GATT_GetConnectionInfor(p_msg->hdr.layer_specific, &gatt_if, remote_bda, &transport))
{
if (GATT_Disconnect(p_msg->hdr.layer_specific) != GATT_SUCCESS)
{
APPL_TRACE_ERROR("bta_gatts_close fail conn_id=%d", p_msg->hdr.layer_specific);
}
else
{
status= BTA_GATT_OK;
}
p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
if (p_rcb && p_rcb->p_cback)
{
if (transport == BTA_TRANSPORT_BR_EDR)
bta_sys_conn_close( BTA_ID_GATTS ,BTA_ALL_APP_ID, remote_bda);
(*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT, (tBTA_GATTS *)&status);
}
}
else
{
APPL_TRACE_ERROR("Unknown connection ID: %d", p_msg->hdr.layer_specific);
}
}
/*******************************************************************************
**
** Function bta_gatts_listen
**
** Description Start or stop listening for LE connection on a GATT server
**
** Returns none.
**
*******************************************************************************/
void bta_gatts_listen(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg)
{
tBTA_GATTS_RCB *p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_listen.server_if);
tBTA_GATTS cb_data;
UNUSED(p_cb);
cb_data.reg_oper.status = BTA_GATT_OK;
cb_data.reg_oper.server_if = p_msg->api_listen.server_if;
if (p_rcb == NULL)
{
APPL_TRACE_ERROR("Unknown GATTS application");
return;
}
if (!GATT_Listen(p_msg->api_listen.server_if,
p_msg->api_listen.start,
p_msg->api_listen.remote_bda))
{
cb_data.status = BTA_GATT_ERROR;
APPL_TRACE_ERROR("bta_gatts_listen Listen failed");
}
if (p_rcb->p_cback)
(*p_rcb->p_cback)(BTA_GATTS_LISTEN_EVT, &cb_data);
}
/*******************************************************************************
**
** Function bta_gatts_request_cback
**
** Description GATTS attribute request callback.
**
** Returns none.
**
*******************************************************************************/
static void bta_gatts_send_request_cback (UINT16 conn_id,
UINT32 trans_id,
tGATTS_REQ_TYPE req_type, tGATTS_DATA *p_data)
{
tBTA_GATTS cb_data;
tBTA_GATTS_RCB *p_rcb;
tGATT_IF gatt_if;
tBTA_GATT_TRANSPORT transport;
memset(&cb_data, 0 , sizeof(tBTA_GATTS));
if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda, &transport))
{
p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
APPL_TRACE_DEBUG ("bta_gatts_send_request_cback conn_id=%d trans_id=%d req_type=%d",
conn_id, trans_id, req_type);
if (p_rcb && p_rcb->p_cback)
{
/* if over BR_EDR, inform PM for mode change */
if (transport == BTA_TRANSPORT_BR_EDR)
{
bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
}
cb_data.req_data.conn_id = conn_id;
cb_data.req_data.trans_id = trans_id;
cb_data.req_data.p_data = (tBTA_GATTS_REQ_DATA *)p_data;
(*p_rcb->p_cback)(req_type, &cb_data);
}
else
{
APPL_TRACE_ERROR("connection request on gatt_if[%d] is not interested", gatt_if);
}
}
else
{
APPL_TRACE_ERROR("request received on unknown connectino ID: %d", conn_id);
}
}
/*******************************************************************************
**
** Function bta_gatts_conn_cback
**
** Description connection callback.
**
** Returns none.
**
*******************************************************************************/
static void bta_gatts_conn_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
BOOLEAN connected, tGATT_DISCONN_REASON reason,
tGATT_TRANSPORT transport)
{
tBTA_GATTS cb_data;
UINT8 evt = connected ? BTA_GATTS_CONNECT_EVT: BTA_GATTS_DISCONNECT_EVT;
tBTA_GATTS_RCB *p_reg;
APPL_TRACE_DEBUG ("bta_gatts_conn_cback gatt_if=%d conn_id=%d connected=%d reason = 0x%04d",
gatt_if, conn_id, connected, reason);
APPL_TRACE_DEBUG("bta_gatts_conn_cback bda :%02x-%02x-%02x-%02x-%02x-%02x ",
bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
bt_bdaddr_t bdaddr;
bdcpy(bdaddr.address, bda);
/*
if (connected)
btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
else
btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
*/
p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
if (p_reg && p_reg->p_cback)
{
/* there is no RM for GATT */
if (transport == BTA_TRANSPORT_BR_EDR)
{
if (connected)
bta_sys_conn_open(BTA_ID_GATTS, BTA_ALL_APP_ID, bda);
else
bta_sys_conn_close( BTA_ID_GATTS ,BTA_ALL_APP_ID, bda);
}
cb_data.conn.conn_id = conn_id;
cb_data.conn.server_if = gatt_if;
cb_data.conn.reason = reason;
cb_data.conn.transport = transport;
memcpy(cb_data.conn.remote_bda, bda, BD_ADDR_LEN);
(*p_reg->p_cback)(evt, &cb_data);
}
else
{
APPL_TRACE_ERROR("bta_gatts_conn_cback server_if=%d not found",gatt_if);
}
}
/*******************************************************************************
**
** Function bta_gatts_cong_cback
**
** Description congestion callback.
**
** Returns none.
**
*******************************************************************************/
static void bta_gatts_cong_cback (UINT16 conn_id, BOOLEAN congested)
{
tBTA_GATTS_RCB *p_rcb;
tGATT_IF gatt_if;
tBTA_GATT_TRANSPORT transport;
tBTA_GATTS cb_data;
if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda, &transport))
{
p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
if (p_rcb && p_rcb->p_cback)
{
cb_data.congest.conn_id = conn_id;
cb_data.congest.congested = congested;
(*p_rcb->p_cback)(BTA_GATTS_CONGEST_EVT, &cb_data);
}
}
}
#endif /* BTA_GATT_INCLUDED */

View file

@ -0,0 +1,581 @@
/******************************************************************************
*
* Copyright (C) 2010-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the implementation of the API for GATT server of BTA.
*
******************************************************************************/
#include "bt_target.h"
#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
#include <string.h>
#include "gki.h"
#include "bta_sys.h"
#include "bta_gatt_api.h"
#include "bta_gatts_int.h"
/*****************************************************************************
** Constants
*****************************************************************************/
static const tBTA_SYS_REG bta_gatts_reg =
{
bta_gatts_hdl_event,
BTA_GATTS_Disable
};
/*******************************************************************************
**
** Function BTA_GATTS_Disable
**
** Description This function is called to disable GATTS module
**
** Parameters None.
**
** Returns None
**
*******************************************************************************/
void BTA_GATTS_Disable(void)
{
BT_HDR *p_buf;
if (bta_sys_is_register(BTA_ID_GATTS) == FALSE)
{
APPL_TRACE_WARNING("GATTS Module not enabled/already disabled");
return;
}
if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
{
p_buf->event = BTA_GATTS_API_DISABLE_EVT;
bta_sys_sendmsg(p_buf);
}
bta_sys_deregister(BTA_ID_GATTS);
}
/*******************************************************************************
**
** Function BTA_GATTS_AppRegister
**
** Description This function is called to register application callbacks
** with BTA GATTS module.
**
** Parameters p_app_uuid - applicaiton UUID
** p_cback - pointer to the application callback function.
**
** Returns None
**
*******************************************************************************/
void BTA_GATTS_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTS_CBACK *p_cback)
{
tBTA_GATTS_API_REG *p_buf;
/* register with BTA system manager */
if (bta_sys_is_register(BTA_ID_GATTS) == FALSE)
{
bta_sys_register(BTA_ID_GATTS, &bta_gatts_reg);
}
if ((p_buf = (tBTA_GATTS_API_REG *) GKI_getbuf(sizeof(tBTA_GATTS_API_REG))) != NULL)
{
p_buf->hdr.event = BTA_GATTS_API_REG_EVT;
if (p_app_uuid != NULL)
memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
p_buf->p_cback = p_cback;
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_AppDeregister
**
** Description De-register with GATT Server.
**
** Parameters app_id: applicatino ID.
**
** Returns void
**
*******************************************************************************/
void BTA_GATTS_AppDeregister(tBTA_GATTS_IF server_if)
{
tBTA_GATTS_API_DEREG *p_buf;
if ((p_buf = (tBTA_GATTS_API_DEREG *) GKI_getbuf(sizeof(tBTA_GATTS_API_DEREG))) != NULL)
{
p_buf->hdr.event = BTA_GATTS_API_DEREG_EVT;
p_buf->server_if = server_if;
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_CreateService
**
** Description Create a service. When service creation is done, a callback
** event BTA_GATTS_CREATE_SRVC_EVT is called to report status
** and service ID to the profile. The service ID obtained in
** the callback function needs to be used when adding included
** service and characteristics/descriptors into the service.
**
** Parameters app_id: Profile ID this service is belonged to.
** p_service_uuid: service UUID.
** inst: instance ID number of this service.
** num_handle: numble of handle requessted for this service.
** is_primary: is this service a primary one or not.
**
** Returns void
**
*******************************************************************************/
void BTA_GATTS_CreateService(tBTA_GATTS_IF server_if, tBT_UUID *p_service_uuid, UINT8 inst,
UINT16 num_handle, BOOLEAN is_primary)
{
tBTA_GATTS_API_CREATE_SRVC *p_buf;
if ((p_buf = (tBTA_GATTS_API_CREATE_SRVC *) GKI_getbuf(sizeof(tBTA_GATTS_API_CREATE_SRVC))) != NULL)
{
p_buf->hdr.event = BTA_GATTS_API_CREATE_SRVC_EVT;
p_buf->server_if = server_if;
p_buf->inst = inst;
memcpy(&p_buf->service_uuid, p_service_uuid, sizeof(tBT_UUID));
p_buf->num_handle = num_handle;
p_buf->is_pri = is_primary;
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_AddIncludeService
**
** Description This function is called to add an included service. After included
** service is included, a callback event BTA_GATTS_ADD_INCL_SRVC_EVT
** is reported the included service ID.
**
** Parameters service_id: service ID to which this included service is to
** be added.
** included_service_id: the service ID to be included.
**
** Returns void
**
*******************************************************************************/
void BTA_GATTS_AddIncludeService(UINT16 service_id, UINT16 included_service_id)
{
tBTA_GATTS_API_ADD_INCL_SRVC *p_buf;
if ((p_buf =
(tBTA_GATTS_API_ADD_INCL_SRVC *) GKI_getbuf(sizeof(tBTA_GATTS_API_ADD_INCL_SRVC)))
!= NULL)
{
p_buf->hdr.event = BTA_GATTS_API_ADD_INCL_SRVC_EVT;
p_buf->hdr.layer_specific = service_id;
p_buf->included_service_id = included_service_id;
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_AddCharacteristic
**
** Description This function is called to add a characteristic into a service.
**
** Parameters service_id: service ID to which this included service is to
** be added.
** p_char_uuid : Characteristic UUID.
** perm : Characteristic value declaration attribute permission.
** property : Characteristic Properties
**
** Returns None
**
*******************************************************************************/
void BTA_GATTS_AddCharacteristic (UINT16 service_id, tBT_UUID *p_char_uuid,
tBTA_GATT_PERM perm, tBTA_GATT_CHAR_PROP property)
{
tBTA_GATTS_API_ADD_CHAR *p_buf;
if ((p_buf = (tBTA_GATTS_API_ADD_CHAR *) GKI_getbuf(sizeof(tBTA_GATTS_API_ADD_CHAR))) != NULL)
{
memset(p_buf, 0, sizeof(tBTA_GATTS_API_ADD_CHAR));
p_buf->hdr.event = BTA_GATTS_API_ADD_CHAR_EVT;
p_buf->hdr.layer_specific = service_id;
p_buf->perm = perm;
p_buf->property = property;
if (p_char_uuid)
{
memcpy(&p_buf->char_uuid, p_char_uuid, sizeof(tBT_UUID));
}
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_AddCharDescriptor
**
** Description This function is called to add characteristic descriptor. When
** it's done, a callback event BTA_GATTS_ADD_DESCR_EVT is called
** to report the status and an ID number for this descriptor.
**
** Parameters service_id: service ID to which this charatceristic descriptor is to
** be added.
** perm: descriptor access permission.
** p_descr_uuid: descriptor UUID.
**
** Returns returns status.
**
*******************************************************************************/
void BTA_GATTS_AddCharDescriptor (UINT16 service_id,
tBTA_GATT_PERM perm,
tBT_UUID * p_descr_uuid)
{
tBTA_GATTS_API_ADD_DESCR *p_buf;
UINT16 len = sizeof(tBTA_GATTS_API_ADD_DESCR);
if ((p_buf = (tBTA_GATTS_API_ADD_DESCR *) GKI_getbuf(len)) != NULL)
{
memset(p_buf, 0, len);
p_buf->hdr.event = BTA_GATTS_API_ADD_DESCR_EVT;
p_buf->hdr.layer_specific = service_id;
p_buf->perm = perm;
if (p_descr_uuid)
{
memcpy(&p_buf->descr_uuid, p_descr_uuid, sizeof(tBT_UUID));
}
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_DeleteService
**
** Description This function is called to delete a service. When this is done,
** a callback event BTA_GATTS_DELETE_EVT is report with the status.
**
** Parameters service_id: service_id to be deleted.
**
** Returns returns none.
**
*******************************************************************************/
void BTA_GATTS_DeleteService(UINT16 service_id)
{
BT_HDR *p_buf;
if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
{
p_buf->event = BTA_GATTS_API_DEL_SRVC_EVT;
p_buf->layer_specific = service_id;
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_StartService
**
** Description This function is called to start a service.
**
** Parameters service_id: the service ID to be started.
** sup_transport: supported trasnport.
**
** Returns None.
**
*******************************************************************************/
void BTA_GATTS_StartService(UINT16 service_id, tBTA_GATT_TRANSPORT sup_transport)
{
tBTA_GATTS_API_START *p_buf;
if ((p_buf = (tBTA_GATTS_API_START *) GKI_getbuf(sizeof(tBTA_GATTS_API_START))) != NULL)
{
p_buf->hdr.event = BTA_GATTS_API_START_SRVC_EVT;
p_buf->hdr.layer_specific = service_id;
p_buf->transport = sup_transport;
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_StopService
**
** Description This function is called to stop a service.
**
** Parameters service_id - service to be topped.
**
** Returns None
**
*******************************************************************************/
void BTA_GATTS_StopService(UINT16 service_id)
{
BT_HDR *p_buf;
if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
{
p_buf->event = BTA_GATTS_API_STOP_SRVC_EVT;
p_buf->layer_specific = service_id;
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_HandleValueIndication
**
** Description This function is called to read a characteristics descriptor.
**
** Parameters bda - remote device bd address to indicate.
** attr_id - attribute ID to indicate.
** data_len - indicate data length.
** p_data: data to indicate.
** need_confirm - if this indication expects a confirmation or not.
**
** Returns None
**
*******************************************************************************/
void BTA_GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_id, UINT16 data_len,
UINT8 *p_data, BOOLEAN need_confirm)
{
tBTA_GATTS_API_INDICATION *p_buf;
UINT16 len = sizeof(tBTA_GATTS_API_INDICATION);
if ((p_buf = (tBTA_GATTS_API_INDICATION *) GKI_getbuf(len)) != NULL)
{
memset(p_buf, 0, len);
p_buf->hdr.event = BTA_GATTS_API_INDICATION_EVT;
p_buf->hdr.layer_specific = conn_id;
p_buf->attr_id = attr_id;
p_buf->need_confirm = need_confirm;
if (data_len > 0 && p_data != NULL)
{
p_buf->len = data_len;
memcpy(p_buf->value, p_data, data_len);
}
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_SendRsp
**
** Description This function is called to send a response to a request.
**
** Parameters conn_id - connection identifier.
** trans_id - transaction ID.
** status - response status
** p_msg - response data.
**
** Returns None
**
*******************************************************************************/
void BTA_GATTS_SendRsp (UINT16 conn_id, UINT32 trans_id,
tBTA_GATT_STATUS status, tBTA_GATTS_RSP *p_msg)
{
tBTA_GATTS_API_RSP *p_buf;
UINT16 len = sizeof(tBTA_GATTS_API_RSP) + sizeof(tBTA_GATTS_RSP);
if ((p_buf = (tBTA_GATTS_API_RSP *) GKI_getbuf(len)) != NULL)
{
memset(p_buf, 0, len);
p_buf->hdr.event = BTA_GATTS_API_RSP_EVT;
p_buf->hdr.layer_specific = conn_id;
p_buf->trans_id = trans_id;
p_buf->status = status;
if (p_msg != NULL)
{
p_buf->p_rsp = (tBTA_GATTS_RSP *)(p_buf + 1);
memcpy(p_buf->p_rsp, p_msg, sizeof(tBTA_GATTS_RSP));
}
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_Open
**
** Description Open a direct open connection or add a background auto connection
** bd address
**
** Parameters server_if: server interface.
** remote_bda: remote device BD address.
** is_direct: direct connection or background auto connection
** transport : Transport on which GATT connection to be opened (BR/EDR or LE)
**
** Returns void
**
*******************************************************************************/
void BTA_GATTS_Open(tBTA_GATTS_IF server_if, BD_ADDR remote_bda, BOOLEAN is_direct,
tBTA_GATT_TRANSPORT transport)
{
tBTA_GATTS_API_OPEN *p_buf;
if ((p_buf = (tBTA_GATTS_API_OPEN *) GKI_getbuf(sizeof(tBTA_GATTS_API_OPEN))) != NULL)
{
p_buf->hdr.event = BTA_GATTS_API_OPEN_EVT;
p_buf->server_if = server_if;
p_buf->is_direct = is_direct;
p_buf->transport = transport;
memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_CancelOpen
**
** Description Cancel a direct open connection or remove a background auto connection
** bd address
**
** Parameters server_if: server interface.
** remote_bda: remote device BD address.
** is_direct: direct connection or background auto connection
**
** Returns void
**
*******************************************************************************/
void BTA_GATTS_CancelOpen(tBTA_GATTS_IF server_if, BD_ADDR remote_bda, BOOLEAN is_direct)
{
tBTA_GATTS_API_CANCEL_OPEN *p_buf;
if ((p_buf = (tBTA_GATTS_API_CANCEL_OPEN *) GKI_getbuf(sizeof(tBTA_GATTS_API_CANCEL_OPEN))) != NULL)
{
p_buf->hdr.event = BTA_GATTS_API_CANCEL_OPEN_EVT;
p_buf->server_if = server_if;
p_buf->is_direct = is_direct;
memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_Close
**
** Description Close a connection a remote device.
**
** Parameters conn_id: connectino ID to be closed.
**
** Returns void
**
*******************************************************************************/
void BTA_GATTS_Close(UINT16 conn_id)
{
BT_HDR *p_buf;
if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
{
p_buf->event = BTA_GATTS_API_CLOSE_EVT;
p_buf->layer_specific = conn_id;
bta_sys_sendmsg(p_buf);
}
return;
}
/*******************************************************************************
**
** Function BTA_GATTS_Listen
**
** Description Start advertisement to listen for connection request for a
** GATT server
**
** Parameters server_if: server interface.
** start: to start or stop listening for connection
** remote_bda: remote device BD address, if listen to all device
** use NULL.
**
** Returns void
**
*******************************************************************************/
void BTA_GATTS_Listen(tBTA_GATTS_IF server_if, BOOLEAN start, BD_ADDR_PTR target_bda)
{
tBTA_GATTS_API_LISTEN *p_buf;
if ((p_buf = (tBTA_GATTS_API_LISTEN *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTS_API_LISTEN) + BD_ADDR_LEN))) != NULL)
{
p_buf->hdr.event = BTA_GATTS_API_LISTEN_EVT;
p_buf->server_if = server_if;
p_buf->start = start;
if (target_bda)
{
p_buf->remote_bda = (UINT8*)(p_buf + 1);
memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
}
else
p_buf->remote_bda = NULL;
bta_sys_sendmsg(p_buf);
}
return;
}
#endif /* BTA_GATT_INCLUDED */

View file

@ -0,0 +1,144 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*****************************************************************************/
/******************************************************************************
*
* This file contains the GATT server main functions and state machine.
*
******************************************************************************/
#include "bt_target.h"
#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
#include <string.h>
#include "bta_gatts_int.h"
#include "gki.h"
/* type for service building action functions */
typedef void (*tBTA_GATTS_SRVC_ACT)(tBTA_GATTS_SRVC_CB *p_rcb, tBTA_GATTS_DATA *p_data);
/* service building action function list */
const tBTA_GATTS_SRVC_ACT bta_gatts_srvc_build_act[] =
{
bta_gatts_add_include_srvc,
bta_gatts_add_char,
bta_gatts_add_char_descr,
bta_gatts_delete_service,
bta_gatts_start_service,
bta_gatts_stop_service,
};
/* GATTS control block */
#if BTA_DYNAMIC_MEMORY == FALSE
tBTA_GATTS_CB bta_gatts_cb;
#endif
/*******************************************************************************
**
** Function bta_gatts_hdl_event
**
** Description BTA GATT server main event handling function.
**
**
** Returns void
**
*******************************************************************************/
BOOLEAN bta_gatts_hdl_event(BT_HDR *p_msg)
{
tBTA_GATTS_CB *p_cb = &bta_gatts_cb;
tBTA_GATTS_SRVC_CB *p_srvc_cb = NULL;
switch (p_msg->event)
{
case BTA_GATTS_API_DISABLE_EVT:
bta_gatts_api_disable(p_cb);
break;
case BTA_GATTS_API_REG_EVT:
LOG_ERROR("bta_gatts_register\n");
bta_gatts_register(p_cb, (tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_INT_START_IF_EVT:
bta_gatts_start_if(p_cb, (tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_API_DEREG_EVT:
bta_gatts_deregister(p_cb, (tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_API_CREATE_SRVC_EVT:
bta_gatts_create_srvc(p_cb, (tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_API_INDICATION_EVT:
bta_gatts_indicate_handle(p_cb,(tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_API_OPEN_EVT:
bta_gatts_open(p_cb,(tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_API_CANCEL_OPEN_EVT:
bta_gatts_cancel_open(p_cb,(tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_API_CLOSE_EVT:
bta_gatts_close(p_cb,(tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_API_RSP_EVT:
bta_gatts_send_rsp(p_cb,(tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_API_LISTEN_EVT:
bta_gatts_listen(p_cb,(tBTA_GATTS_DATA *) p_msg);
break;
case BTA_GATTS_API_ADD_INCL_SRVC_EVT:
case BTA_GATTS_API_ADD_CHAR_EVT:
case BTA_GATTS_API_ADD_DESCR_EVT:
case BTA_GATTS_API_DEL_SRVC_EVT:
case BTA_GATTS_API_START_SRVC_EVT:
case BTA_GATTS_API_STOP_SRVC_EVT:
p_srvc_cb = bta_gatts_find_srvc_cb_by_srvc_id(p_cb,
((tBTA_GATTS_DATA *)p_msg)->api_add_incl_srvc.hdr.layer_specific);
if (p_srvc_cb != NULL)
{
bta_gatts_srvc_build_act[p_msg->event - BTA_GATTS_API_ADD_INCL_SRVC_EVT](p_srvc_cb, (tBTA_GATTS_DATA *) p_msg);
}
else
{
APPL_TRACE_ERROR("service not created");
}
break;
default:
break;
}
return (TRUE);
}
#endif /* BTA_GATT_INCLUDED */

View file

@ -0,0 +1,234 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the GATT client utility function.
*
******************************************************************************/
#include "bt_target.h"
#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
#include <string.h>
#include "utl.h"
#include "gki.h"
#include "bta_sys.h"
#include "bta_gatts_int.h"
static const UINT8 base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/*******************************************************************************
**
** Function bta_gatt_convert_uuid16_to_uuid128
**
** Description Convert a 16 bits UUID to be an standard 128 bits one.
**
** Returns TRUE if two uuid match; FALSE otherwise.
**
*******************************************************************************/
static void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
{
UINT8 *p = &uuid_128[LEN_UUID_128 - 4];
memcpy (uuid_128, base_uuid, LEN_UUID_128);
UINT16_TO_STREAM(p, uuid_16);
}
/*******************************************************************************
**
** Function bta_gatts_alloc_srvc_cb
**
** Description allocate a service control block.
**
** Returns pointer to the control block, or otherwise NULL when failed.
**
*******************************************************************************/
UINT8 bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB *p_cb, UINT8 rcb_idx)
{
UINT8 i;
for (i = 0; i < BTA_GATTS_MAX_SRVC_NUM; i ++)
{
if (!p_cb->srvc_cb[i].in_use)
{
p_cb->srvc_cb[i].in_use = TRUE;
p_cb->srvc_cb[i].rcb_idx = rcb_idx;
return i;
}
}
return BTA_GATTS_INVALID_APP;
}
/*******************************************************************************
**
** Function bta_gatts_find_app_rcb_by_app_if
**
** Description find the index of the application control block by app ID.
**
** Returns pointer to the control block if success, otherwise NULL
**
*******************************************************************************/
tBTA_GATTS_RCB *bta_gatts_find_app_rcb_by_app_if(tBTA_GATTS_IF server_if)
{
UINT8 i;
tBTA_GATTS_RCB *p_reg;
for (i = 0, p_reg = bta_gatts_cb.rcb; i < BTA_GATTS_MAX_APP_NUM; i ++, p_reg++)
{
if (p_reg->in_use && p_reg->gatt_if == server_if)
return p_reg;
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gatts_find_app_rcb_idx_by_app_if
**
** Description find the index of the application control block by app ID.
**
** Returns index of the control block, or BTA_GATTS_INVALID_APP if failed.
**
*******************************************************************************/
UINT8 bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_IF server_if)
{
UINT8 i;
for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++)
{
if (p_cb->rcb[i].in_use && p_cb->rcb[i].gatt_if == server_if)
return i;
}
return BTA_GATTS_INVALID_APP;
}
/*******************************************************************************
**
** Function bta_gatts_find_srvc_cb_by_srvc_id
**
** Description find the service control block by service ID.
**
** Returns pointer to the rcb.
**
*******************************************************************************/
tBTA_GATTS_SRVC_CB * bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB *p_cb, UINT16 service_id)
{
UINT8 i;
APPL_TRACE_DEBUG("bta_gatts_find_srvc_cb_by_srvc_id service_id=%d", service_id);
for (i = 0; i < BTA_GATTS_MAX_SRVC_NUM; i ++)
{
if (p_cb->srvc_cb[i].in_use &&
p_cb->srvc_cb[i].service_id == service_id)
{
APPL_TRACE_DEBUG("bta_gatts_find_srvc_cb_by_srvc_id found service cb index =%d", i);
return &p_cb->srvc_cb[i];
}
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gatts_find_srvc_cb_by_attr_id
**
** Description find the service control block by attribute ID.
**
** Returns pointer to the rcb.
**
*******************************************************************************/
tBTA_GATTS_SRVC_CB * bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB *p_cb, UINT16 attr_id)
{
UINT8 i;
for (i = 0; i < (BTA_GATTS_MAX_SRVC_NUM); i ++)
{
if (/* middle service */
(i < (BTA_GATTS_MAX_SRVC_NUM - 1) &&
p_cb->srvc_cb[i].in_use &&
p_cb->srvc_cb[i + 1].in_use &&
attr_id >= p_cb->srvc_cb[i].service_id &&
attr_id < p_cb->srvc_cb[i + 1].service_id) ||
/* last active service */
(i < (BTA_GATTS_MAX_SRVC_NUM - 1) &&
p_cb->srvc_cb[i].in_use &&
!p_cb->srvc_cb[i + 1].in_use &&
attr_id >= p_cb->srvc_cb[i].service_id) ||
/* last service incb */
(i == (BTA_GATTS_MAX_SRVC_NUM - 1) &&
attr_id >= p_cb->srvc_cb[i].service_id)
)
{
return &p_cb->srvc_cb[i];
}
}
return NULL;
}
/*******************************************************************************
**
** Function bta_gatts_uuid_compare
**
** Description Compare two UUID to see if they are the same.
**
** Returns TRUE if two uuid match; FALSE otherwise.
**
*******************************************************************************/
BOOLEAN bta_gatts_uuid_compare(tBT_UUID tar, tBT_UUID src)
{
UINT8 su[LEN_UUID_128], tu[LEN_UUID_128];
UINT8 *ps, *pt;
/* any of the UUID is unspecified */
if (src.len == 0 || tar.len == 0)
{
return TRUE;
}
/* If both are 16-bit, we can do a simple compare */
if (src.len == 2 && tar.len == 2)
{
return src.uu.uuid16 == tar.uu.uuid16;
}
/* One or both of the UUIDs is 128-bit */
if (src.len == LEN_UUID_16)
{
/* convert a 16 bits UUID to 128 bits value */
bta_gatt_convert_uuid16_to_uuid128(su, src.uu.uuid16);
ps = su;
}
else
ps = src.uu.uuid128;
if (tar.len == LEN_UUID_16)
{
/* convert a 16 bits UUID to 128 bits value */
bta_gatt_convert_uuid16_to_uuid128(tu, tar.uu.uuid16);
pt = tu;
}
else
pt = tar.uu.uuid128;
return(memcmp(ps, pt, LEN_UUID_128) == 0);
}
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,496 @@
/******************************************************************************
*
* Copyright (C) 2005-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the HID HOST API in the subsystem of BTA.
*
******************************************************************************/
#include "bt_target.h"
#if defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "bta_hh_api.h"
#include "bta_hh_int.h"
#include "l2c_api.h"
#include "utl.h"
#define LOG_TAG "bt_bta_hh"
#include "osi/include/log.h"
/*****************************************************************************
** Constants
*****************************************************************************/
static const tBTA_SYS_REG bta_hh_reg =
{
bta_hh_hdl_event,
BTA_HhDisable
};
/*******************************************************************************
**
** Function BTA_HhEnable
**
** Description Enable the HID host. This function must be called before
** any other functions in the HID host API are called. When the
** enable operation is complete the callback function will be
** called with BTA_HH_ENABLE_EVT.
**
**
** Returns void
**
*******************************************************************************/
void BTA_HhEnable(tBTA_SEC sec_mask, tBTA_HH_CBACK *p_cback)
{
tBTA_HH_API_ENABLE *p_buf;
/* register with BTA system manager */
bta_sys_register(BTA_ID_HH, &bta_hh_reg);
LOG_INFO("%s sec_mask:0x%x p_cback:%p", __func__, sec_mask, p_cback);
p_buf = (tBTA_HH_API_ENABLE *)GKI_getbuf((UINT16)sizeof(tBTA_HH_API_ENABLE));
if (p_buf != NULL)
{
memset(p_buf, 0, sizeof(tBTA_HH_API_ENABLE));
p_buf->hdr.event = BTA_HH_API_ENABLE_EVT;
p_buf->p_cback = p_cback;
p_buf->sec_mask = sec_mask;
bta_sys_sendmsg(p_buf);
}
}
/*******************************************************************************
**
** Function BTA_HhDisable
**
** Description Disable the HID host. If the server is currently
** connected, the connection will be closed.
**
** Returns void
**
*******************************************************************************/
void BTA_HhDisable(void)
{
BT_HDR *p_buf;
bta_sys_deregister(BTA_ID_HH);
if ((p_buf = (BT_HDR *)GKI_getbuf(sizeof(BT_HDR))) != NULL)
{
p_buf->event = BTA_HH_API_DISABLE_EVT;
bta_sys_sendmsg(p_buf);
}
}
/*******************************************************************************
**
** Function BTA_HhClose
**
** Description Disconnect a connection.
**
** Returns void
**
*******************************************************************************/
void BTA_HhClose(UINT8 dev_handle)
{
BT_HDR *p_buf;
if ((p_buf = (BT_HDR *)GKI_getbuf((UINT16)sizeof(BT_HDR))) != NULL)
{
memset(p_buf, 0, sizeof(BT_HDR));
p_buf->event = BTA_HH_API_CLOSE_EVT;
p_buf->layer_specific = (UINT16) dev_handle;
bta_sys_sendmsg(p_buf);
}
}
/*******************************************************************************
**
** Function BTA_HhOpen
**
** Description Connect to a device of specified BD address in specified
** protocol mode and security level.
**
** Returns void
**
*******************************************************************************/
void BTA_HhOpen(BD_ADDR dev_bda, tBTA_HH_PROTO_MODE mode, tBTA_SEC sec_mask)
{
tBTA_HH_API_CONN *p_buf;
p_buf = (tBTA_HH_API_CONN *)GKI_getbuf((UINT16)sizeof(tBTA_HH_API_CONN));
if (p_buf!= NULL)
{
memset((void *)p_buf, 0, sizeof(tBTA_HH_API_CONN));
p_buf->hdr.event = BTA_HH_API_OPEN_EVT;
p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
p_buf->sec_mask = sec_mask;
p_buf->mode = mode;
bdcpy(p_buf->bd_addr, dev_bda);
bta_sys_sendmsg((void *)p_buf);
}
else
{
APPL_TRACE_ERROR("No resource to send HID host Connect request.");
}
}
/*******************************************************************************
**
** Function bta_hh_snd_write_dev
**
*******************************************************************************/
static void bta_hh_snd_write_dev(UINT8 dev_handle, UINT8 t_type, UINT8 param,
UINT16 data, UINT8 rpt_id, BT_HDR *p_data)
{
tBTA_HH_CMD_DATA *p_buf;
UINT16 len = (UINT16) (sizeof(tBTA_HH_CMD_DATA) );
if ((p_buf = (tBTA_HH_CMD_DATA *)GKI_getbuf(len))!= NULL)
{
memset(p_buf, 0, sizeof(tBTA_HH_CMD_DATA));
p_buf->hdr.event = BTA_HH_API_WRITE_DEV_EVT;
p_buf->hdr.layer_specific = (UINT16) dev_handle;
p_buf->t_type = t_type;
p_buf->data = data;
p_buf->param = param;
p_buf->p_data = p_data;
p_buf->rpt_id = rpt_id;
bta_sys_sendmsg(p_buf);
}
}
/*******************************************************************************
**
** Function BTA_HhSetReport
**
** Description send SET_REPORT to device.
**
** Parameter dev_handle: device handle
** r_type: report type, could be BTA_HH_RPTT_OUTPUT or
** BTA_HH_RPTT_FEATURE.
** Returns void
**
*******************************************************************************/
void BTA_HhSetReport(UINT8 dev_handle, tBTA_HH_RPT_TYPE r_type, BT_HDR *p_data)
{
bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_REPORT, r_type, 0, 0, p_data);
}
/*******************************************************************************
**
** Function BTA_HhGetReport
**
** Description Send a GET_REPORT to HID device.
**
** Returns void
**
*******************************************************************************/
void BTA_HhGetReport(UINT8 dev_handle, tBTA_HH_RPT_TYPE r_type, UINT8 rpt_id, UINT16 buf_size)
{
UINT8 param = (buf_size) ? (r_type | 0x08) : r_type;
bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_REPORT, param,
buf_size, rpt_id, NULL);
}
/*******************************************************************************
**
** Function BTA_HhSetProtoMode
**
** Description This function set the protocol mode at specified HID handle
**
** Returns void
**
*******************************************************************************/
void BTA_HhSetProtoMode(UINT8 dev_handle, tBTA_HH_PROTO_MODE p_type)
{
bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_PROTOCOL, (UINT8)p_type,
0, 0, NULL);
}
/*******************************************************************************
**
** Function BTA_HhGetProtoMode
**
** Description This function get protocol mode information.
**
** Returns void
**
*******************************************************************************/
void BTA_HhGetProtoMode(UINT8 dev_handle)
{
bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_PROTOCOL, 0, 0, 0, NULL);
}
/*******************************************************************************
**
** Function BTA_HhSetIdle
**
** Description send SET_IDLE to device.
**
** Returns void
**
*******************************************************************************/
void BTA_HhSetIdle(UINT8 dev_handle, UINT16 idle_rate)
{
bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_IDLE, 0, idle_rate, 0, NULL);
}
/*******************************************************************************
**
** Function BTA_HhGetIdle
**
** Description Send a GET_IDLE from HID device.
**
** Returns void
**
*******************************************************************************/
void BTA_HhGetIdle(UINT8 dev_handle)
{
bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_IDLE, 0, 0, 0, NULL);
}
/*******************************************************************************
**
** Function BTA_HhSendCtrl
**
** Description Send a control command to HID device.
**
** Returns void
**
*******************************************************************************/
void BTA_HhSendCtrl(UINT8 dev_handle, tBTA_HH_TRANS_CTRL_TYPE c_type)
{
bta_hh_snd_write_dev(dev_handle, HID_TRANS_CONTROL, (UINT8)c_type, 0, 0, NULL);
}
/*******************************************************************************
**
** Function BTA_HhSendData
**
** Description This function send DATA transaction to HID device.
**
** Parameter dev_handle: device handle
** dev_bda: remote device address
** p_data: data to be sent in the DATA transaction; or
** the data to be write into the Output Report of a LE HID
** device. The report is identified the report ID which is
** the value of the byte (UINT8 *)(p_buf + 1) + p_buf->offset.
** p_data->layer_specific needs to be set to the report type,
** it can be OUTPUT report, or FEATURE report.
**
** Returns void
**
*******************************************************************************/
void BTA_HhSendData(UINT8 dev_handle, BD_ADDR dev_bda, BT_HDR *p_data)
{
UNUSED(dev_bda);
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
if (p_data->layer_specific != BTA_HH_RPTT_OUTPUT)
{
APPL_TRACE_ERROR("ERROR! Wrong report type! Write Command only valid for output report!");
return;
}
#endif
bta_hh_snd_write_dev(dev_handle, HID_TRANS_DATA, (UINT8)p_data->layer_specific, 0, 0, p_data);
}
/*******************************************************************************
**
** Function BTA_HhGetDscpInfo
**
** Description Get HID device report descriptor
**
** Returns void
**
*******************************************************************************/
void BTA_HhGetDscpInfo(UINT8 dev_handle)
{
BT_HDR *p_buf;
if ((p_buf = (BT_HDR *)GKI_getbuf((UINT16)sizeof(BT_HDR))) != NULL)
{
memset(p_buf, 0, sizeof(BT_HDR));
p_buf->event = BTA_HH_API_GET_DSCP_EVT;
p_buf->layer_specific = (UINT16) dev_handle;
bta_sys_sendmsg(p_buf);
}
}
/*******************************************************************************
**
** Function BTA_HhAddDev
**
** Description Add a virtually cabled device into HID-Host device list
** to manage and assign a device handle for future API call,
** host applciation call this API at start-up to initialize its
** virtually cabled devices.
**
** Returns void
**
*******************************************************************************/
void BTA_HhAddDev(BD_ADDR bda, tBTA_HH_ATTR_MASK attr_mask, UINT8 sub_class,
UINT8 app_id, tBTA_HH_DEV_DSCP_INFO dscp_info)
{
tBTA_HH_MAINT_DEV *p_buf;
UINT16 len = sizeof(tBTA_HH_MAINT_DEV) + dscp_info.descriptor.dl_len;
p_buf = (tBTA_HH_MAINT_DEV *)GKI_getbuf(len);
if (p_buf != NULL)
{
memset(p_buf, 0, sizeof(tBTA_HH_MAINT_DEV));
p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
p_buf->sub_event = BTA_HH_ADD_DEV_EVT;
p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
p_buf->attr_mask = (UINT16) attr_mask;
p_buf->sub_class = sub_class;
p_buf->app_id = app_id;
bdcpy(p_buf->bda, bda);
memcpy(&p_buf->dscp_info, &dscp_info, sizeof(tBTA_HH_DEV_DSCP_INFO));
if ( dscp_info.descriptor.dl_len != 0 && dscp_info.descriptor.dsc_list)
{
p_buf->dscp_info.descriptor.dl_len = dscp_info.descriptor.dl_len;
p_buf->dscp_info.descriptor.dsc_list = (UINT8 *)(p_buf + 1);
memcpy(p_buf->dscp_info.descriptor.dsc_list, dscp_info.descriptor.dsc_list, dscp_info.descriptor.dl_len);
}
else
{
p_buf->dscp_info.descriptor.dsc_list = NULL;
p_buf->dscp_info.descriptor.dl_len = 0;
}
bta_sys_sendmsg(p_buf);
}
}
/*******************************************************************************
**
** Function BTA_HhRemoveDev
**
** Description Remove a device from the HID host devices list.
**
** Returns void
**
*******************************************************************************/
void BTA_HhRemoveDev(UINT8 dev_handle )
{
tBTA_HH_MAINT_DEV *p_buf;
p_buf = (tBTA_HH_MAINT_DEV *)GKI_getbuf((UINT16)sizeof(tBTA_HH_MAINT_DEV));
if (p_buf != NULL)
{
memset(p_buf, 0, sizeof(tBTA_HH_MAINT_DEV));
p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
p_buf->sub_event = BTA_HH_RMV_DEV_EVT;
p_buf->hdr.layer_specific = (UINT16) dev_handle;
bta_sys_sendmsg(p_buf);
}
}
#if BTA_HH_LE_INCLUDED == TRUE
/*******************************************************************************
**
** Function BTA_HhUpdateLeScanParam
**
** Description Update the scan paramteters if connected to a LE hid device as
** report host.
**
** Returns void
**
*******************************************************************************/
void BTA_HhUpdateLeScanParam(UINT8 dev_handle, UINT16 scan_int, UINT16 scan_win)
{
tBTA_HH_SCPP_UPDATE *p_buf;
p_buf = (tBTA_HH_SCPP_UPDATE *)GKI_getbuf((UINT16)sizeof(tBTA_HH_SCPP_UPDATE));
if (p_buf != NULL)
{
memset(p_buf, 0, sizeof(tBTA_HH_SCPP_UPDATE));
p_buf->hdr.event = BTA_HH_API_SCPP_UPDATE_EVT;
p_buf->hdr.layer_specific = (UINT16) dev_handle;
p_buf->scan_int = scan_int;
p_buf->scan_win = scan_win;
bta_sys_sendmsg(p_buf);
}
}
#endif
/*******************************************************************************/
/* Utility Function */
/*******************************************************************************/
/*******************************************************************************
**
** Function BTA_HhParseBootRpt
**
** Description This utility function parse a boot mode report.
** For keyboard report, report data will carry the keycode max
** up to 6 key press in one report. Application need to convert
** the keycode into keypress character according to keyboard
** language.
**
** Returns void
**
*******************************************************************************/
void BTA_HhParseBootRpt(tBTA_HH_BOOT_RPT *p_data, UINT8 *p_report,
UINT16 report_len)
{
p_data->dev_type = BTA_HH_DEVT_UNKNOWN;
if (p_report)
{
/* first byte is report ID */
switch (p_report[0])
{
case BTA_HH_KEYBD_RPT_ID: /* key board report ID */
p_data->dev_type = p_report[0];
bta_hh_parse_keybd_rpt(p_data, p_report + 1, (UINT16)(report_len -1));
break;
case BTA_HH_MOUSE_RPT_ID: /* mouse report ID */
p_data->dev_type = p_report[0];
bta_hh_parse_mice_rpt(p_data, p_report + 1, (UINT16)(report_len - 1));
break;
default:
APPL_TRACE_DEBUG("Unknown boot report: %d", p_report[0]);;
break;
}
}
return;
}
#endif /* BTA_HH_INCLUDED */

View file

@ -0,0 +1,62 @@
/******************************************************************************
*
* Copyright (C) 2005-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains compile-time configurable constants for the BTA Hid
* Host.
*
******************************************************************************/
#include "bt_target.h"
#include "bta_hh_api.h"
/* max number of device types supported by BTA */
#define BTA_HH_MAX_DEVT_SPT 9
/* size of database for service discovery */
#ifndef BTA_HH_DISC_BUF_SIZE
#define BTA_HH_DISC_BUF_SIZE GKI_MAX_BUF_SIZE
#endif
/* The type of devices supported by BTA HH and corresponding application ID */
tBTA_HH_SPT_TOD p_devt_list[BTA_HH_MAX_DEVT_SPT] =
{
{BTA_HH_DEVT_MIC, BTA_HH_APP_ID_MI},
{BTA_HH_DEVT_KBD, BTA_HH_APP_ID_KB},
{BTA_HH_DEVT_KBD|BTA_HH_DEVT_MIC, BTA_HH_APP_ID_KB},
{BTA_HH_DEVT_RMC, BTA_HH_APP_ID_RMC},
{BTA_HH_DEVT_RMC | BTA_HH_DEVT_KBD, BTA_HH_APP_ID_RMC},
{BTA_HH_DEVT_MIC | BTA_HH_DEVT_DGT, BTA_HH_APP_ID_MI},
{BTA_HH_DEVT_JOS, BTA_HH_APP_ID_JOY},
{BTA_HH_DEVT_GPD, BTA_HH_APP_ID_GPAD},
{BTA_HH_DEVT_UNKNOWN, BTA_HH_APP_ID_3DSG}
};
const tBTA_HH_CFG bta_hh_cfg =
{
BTA_HH_MAX_DEVT_SPT, /* number of supported type of devices */
p_devt_list, /* ToD & AppID list */
BTA_HH_DISC_BUF_SIZE /* HH SDP discovery database size */
};
tBTA_HH_CFG *p_bta_hh_cfg = (tBTA_HH_CFG *)&bta_hh_cfg;

View file

@ -0,0 +1,415 @@
/******************************************************************************
*
* Copyright (C) 2005-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains BTA HID Host internal definitions
*
******************************************************************************/
#ifndef BTA_HH_INT_H
#define BTA_HH_INT_H
#include "bta_sys.h"
#include "utl.h"
#include "bta_hh_api.h"
#if BTA_HH_LE_INCLUDED == TRUE
xxx
#include "bta_gatt_api.h"
#endif
/* can be moved to bta_api.h */
#define BTA_HH_MAX_RPT_CHARS 8
#if (BTA_GATT_INCLUDED == FALSE || BLE_INCLUDED == FALSE)
#undef BTA_HH_LE_INCLUDED
#define BTA_HH_LE_INCLUDED FALSE
#endif
/* state machine events, these events are handled by the state machine */
enum
{
BTA_HH_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_HH),
BTA_HH_API_CLOSE_EVT,
BTA_HH_INT_OPEN_EVT,
BTA_HH_INT_CLOSE_EVT,
BTA_HH_INT_DATA_EVT,
BTA_HH_INT_CTRL_DATA,
BTA_HH_INT_HANDSK_EVT,
BTA_HH_SDP_CMPL_EVT,
BTA_HH_API_WRITE_DEV_EVT,
BTA_HH_API_GET_DSCP_EVT,
BTA_HH_API_MAINT_DEV_EVT,
BTA_HH_OPEN_CMPL_EVT,
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
BTA_HH_GATT_CLOSE_EVT,
BTA_HH_GATT_OPEN_EVT,
BTA_HH_START_ENC_EVT,
BTA_HH_ENC_CMPL_EVT,
BTA_HH_GATT_READ_CHAR_CMPL_EVT,
BTA_HH_GATT_WRITE_CHAR_CMPL_EVT,
BTA_HH_GATT_READ_DESCR_CMPL_EVT,
BTA_HH_GATT_WRITE_DESCR_CMPL_EVT,
BTA_HH_API_SCPP_UPDATE_EVT,
BTA_HH_GATT_ENC_CMPL_EVT,
#endif
/* not handled by execute state machine */
BTA_HH_API_ENABLE_EVT,
BTA_HH_API_DISABLE_EVT,
BTA_HH_DISC_CMPL_EVT
};
typedef UINT16 tBTA_HH_INT_EVT; /* HID host internal events */
#define BTA_HH_INVALID_EVT (BTA_HH_DISC_CMPL_EVT + 1)
/* event used to map between BTE event and BTA event */
#define BTA_HH_FST_TRANS_CB_EVT BTA_HH_GET_RPT_EVT
#define BTA_HH_FST_BTE_TRANS_EVT HID_TRANS_GET_REPORT
/* sub event code used for device maintainence API call */
#define BTA_HH_ADD_DEV 0
#define BTA_HH_REMOVE_DEV 1
/* state machine states */
enum
{
BTA_HH_NULL_ST,
BTA_HH_IDLE_ST,
BTA_HH_W4_CONN_ST,
BTA_HH_CONN_ST
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
,BTA_HH_W4_SEC
#endif
,BTA_HH_INVALID_ST /* Used to check invalid states before executing SM function */
};
typedef UINT8 tBTA_HH_STATE;
/* data structure used to send a command/data to HID device */
typedef struct
{
BT_HDR hdr;
UINT8 t_type;
UINT8 param;
UINT8 rpt_id;
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
UINT8 srvc_id;
#endif
UINT16 data;
BT_HDR *p_data;
}tBTA_HH_CMD_DATA;
/* data type for BTA_HH_API_ENABLE_EVT */
typedef struct
{
BT_HDR hdr;
UINT8 sec_mask;
UINT8 service_name[BTA_SERVICE_NAME_LEN+1];
tBTA_HH_CBACK *p_cback;
} tBTA_HH_API_ENABLE;
typedef struct
{
BT_HDR hdr;
BD_ADDR bd_addr;
UINT8 sec_mask;
tBTA_HH_PROTO_MODE mode;
}tBTA_HH_API_CONN;
/* internal event data from BTE HID callback */
typedef struct
{
BT_HDR hdr;
BD_ADDR addr;
UINT32 data;
BT_HDR *p_data;
}tBTA_HH_CBACK_DATA;
typedef struct
{
BT_HDR hdr;
BD_ADDR bda;
UINT16 attr_mask;
UINT16 sub_event;
UINT8 sub_class;
UINT8 app_id;
tBTA_HH_DEV_DSCP_INFO dscp_info;
}tBTA_HH_MAINT_DEV;
#if BTA_HH_LE_INCLUDED == TRUE
typedef struct
{
BT_HDR hdr;
UINT16 conn_id;
tBTA_GATT_REASON reason; /* disconnect reason code, not useful when connect event is reported */
}tBTA_HH_LE_CLOSE;
typedef struct
{
BT_HDR hdr;
UINT16 scan_int;
UINT16 scan_win;
}tBTA_HH_SCPP_UPDATE;
#endif
/* union of all event data types */
typedef union
{
BT_HDR hdr;
tBTA_HH_API_ENABLE api_enable;
tBTA_HH_API_CONN api_conn;
tBTA_HH_CMD_DATA api_sndcmd;
tBTA_HH_CBACK_DATA hid_cback;
tBTA_HH_STATUS status;
tBTA_HH_MAINT_DEV api_maintdev;
#if BTA_HH_LE_INCLUDED == TRUE
tBTA_HH_LE_CLOSE le_close;
tBTA_GATTC_OPEN le_open;
tBTA_HH_SCPP_UPDATE le_scpp_update;
tBTA_GATTC_ENC_CMPL_CB le_enc_cmpl;
#endif
} tBTA_HH_DATA;
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
typedef struct
{
UINT8 index;
BOOLEAN in_use;
UINT8 inst_id; /* share service instance ID and report instance ID, as
hi 4 for service instance ID, low 4 as charatceristic instance ID */
tBTA_HH_RPT_TYPE rpt_type;
UINT16 uuid;
UINT8 prop;
UINT8 rpt_id;
BOOLEAN client_cfg_exist;
UINT16 client_cfg_value;
}tBTA_HH_LE_RPT;
#ifndef BTA_HH_LE_RPT_MAX
#define BTA_HH_LE_RPT_MAX 20
#endif
typedef struct
{
BOOLEAN in_use;
tBTA_HH_LE_RPT report[BTA_HH_LE_RPT_MAX];
#define BTA_HH_LE_PROTO_MODE_BIT 0x01
#define BTA_HH_LE_CP_BIT 0x02
UINT8 option_char; /* control point char exisit or not */
BOOLEAN expl_incl_srvc;
UINT8 incl_srvc_inst; /* assuming only one included service : battery service */
UINT8 cur_expl_char_idx; /* currently discovering service index */
UINT8 *rpt_map;
UINT16 ext_rpt_ref;
tBTA_HH_DEV_DESCR descriptor;
}tBTA_HH_LE_HID_SRVC;
#ifndef BTA_HH_LE_HID_SRVC_MAX
#define BTA_HH_LE_HID_SRVC_MAX 1
#endif
/* convert a HID handle to the LE CB index */
#define BTA_HH_GET_LE_CB_IDX(x) (((x) >> 4) - 1)
/* convert a GATT connection ID to HID device handle, it is the hi 4 bits of a UINT8 */
#define BTA_HH_GET_LE_DEV_HDL(x) (UINT8)(((x) + 1) << 4)
/* check to see if th edevice handle is a LE device handle */
#define BTA_HH_IS_LE_DEV_HDL(x) ((x) & 0xf0)
#define BTA_HH_IS_LE_DEV_HDL_VALID(x) (((x)>>4) <= BTA_HH_LE_MAX_KNOWN)
#endif
/* device control block */
typedef struct
{
tBTA_HH_DEV_DSCP_INFO dscp_info; /* report descriptor and DI information */
BD_ADDR addr; /* BD-Addr of the HID device */
UINT16 attr_mask; /* attribute mask */
UINT16 w4_evt; /* W4_handshake event name */
UINT8 index; /* index number referenced to handle index */
UINT8 sub_class; /* Cod sub class */
UINT8 sec_mask; /* security mask */
UINT8 app_id; /* application ID for this connection */
UINT8 hid_handle; /* device handle : low 4 bits for regular HID: HID_HOST_MAX_DEVICES can not exceed 15;
high 4 bits for LE HID: GATT_MAX_PHY_CHANNEL can not exceed 15 */
BOOLEAN vp; /* virtually unplug flag */
BOOLEAN in_use; /* control block currently in use */
BOOLEAN incoming_conn; /* is incoming connection? */
UINT8 incoming_hid_handle; /* temporary handle for incoming connection? */
BOOLEAN opened; /* TRUE if device successfully opened HID connection */
tBTA_HH_PROTO_MODE mode; /* protocol mode */
tBTA_HH_STATE state; /* CB state */
#if (BTA_HH_LE_INCLUDED == TRUE)
#define BTA_HH_LE_DISC_NONE 0x00
#define BTA_HH_LE_DISC_HIDS 0x01
#define BTA_HH_LE_DISC_DIS 0x02
#define BTA_HH_LE_DISC_SCPS 0x04
UINT8 disc_active;
tBTA_HH_STATUS status;
tBTA_GATT_REASON reason;
BOOLEAN is_le_device;
tBTA_HH_LE_HID_SRVC hid_srvc[BTA_HH_LE_HID_SRVC_MAX];
UINT16 conn_id;
BOOLEAN in_bg_conn;
UINT8 total_srvc;
UINT8 clt_cfg_idx;
UINT8 cur_srvc_index; /* currently discovering service index */
BOOLEAN scps_supported;
#define BTA_HH_LE_SCPS_NOTIFY_NONE 0
#define BTA_HH_LE_SCPS_NOTIFY_SPT 0x01
#define BTA_HH_LE_SCPS_NOTIFY_ENB 0x02
UINT8 scps_notify; /* scan refresh supported/notification enabled */
#endif
BOOLEAN security_pending;
} tBTA_HH_DEV_CB;
/* key board parsing control block */
typedef struct
{
BOOLEAN mod_key[4]; /* ctrl, shift(upper), Alt, GUI */
BOOLEAN num_lock;
BOOLEAN caps_lock;
UINT8 last_report[BTA_HH_MAX_RPT_CHARS];
} tBTA_HH_KB_CB;
/******************************************************************************
** Main Control Block
*******************************************************************************/
typedef struct
{
tBTA_HH_KB_CB kb_cb; /* key board control block,
suppose BTA will connect
to only one keyboard at
the same time */
tBTA_HH_DEV_CB kdev[BTA_HH_MAX_DEVICE]; /* device control block */
tBTA_HH_DEV_CB* p_cur; /* current device control
block idx, used in sdp */
UINT8 cb_index[BTA_HH_MAX_KNOWN]; /* maintain a CB index
map to dev handle */
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
UINT8 le_cb_index[BTA_HH_MAX_DEVICE]; /* maintain a CB index map to LE dev handle */
tBTA_GATTC_IF gatt_if;
#endif
tBTA_HH_CBACK *p_cback; /* Application callbacks */
tSDP_DISCOVERY_DB* p_disc_db;
UINT8 trace_level; /* tracing level */
UINT8 cnt_num; /* connected device number */
BOOLEAN w4_disable; /* w4 disable flag */
}
tBTA_HH_CB;
#if BTA_DYNAMIC_MEMORY == FALSE
extern tBTA_HH_CB bta_hh_cb;
#else
extern tBTA_HH_CB *bta_hh_cb_ptr;
#define bta_hh_cb (*bta_hh_cb_ptr)
#endif
/* from bta_hh_cfg.c */
extern tBTA_HH_CFG *p_bta_hh_cfg;
/*****************************************************************************
** Function prototypes
*****************************************************************************/
extern BOOLEAN bta_hh_hdl_event(BT_HDR *p_msg);
extern void bta_hh_sm_execute(tBTA_HH_DEV_CB *p_cb, UINT16 event,
tBTA_HH_DATA *p_data);
/* action functions */
extern void bta_hh_api_disc_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_open_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_close_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_data_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA * p_data);
extern void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA * p_data);
extern void bta_hh_start_sdp(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_sdp_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_write_dev_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_get_dscp_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_handsk_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_maint_dev_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_open_failure(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
/* utility functions */
extern UINT8 bta_hh_find_cb(BD_ADDR bda);
extern void bta_hh_parse_keybd_rpt(tBTA_HH_BOOT_RPT *p_kb_data,
UINT8 *p_report, UINT16 report_len);
extern void bta_hh_parse_mice_rpt(tBTA_HH_BOOT_RPT *p_kb_data,
UINT8 *p_report, UINT16 report_len);
extern BOOLEAN bta_hh_tod_spt(tBTA_HH_DEV_CB *p_cb,UINT8 sub_class);
extern void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB *p_cb);
extern void bta_hh_add_device_to_list(tBTA_HH_DEV_CB *p_cb, UINT8 handle,
UINT16 attr_mask,
tHID_DEV_DSCP_INFO *p_dscp_info,
UINT8 sub_class, UINT16 max_latency, UINT16 min_tout, UINT8 app_id);
extern void bta_hh_update_di_info(tBTA_HH_DEV_CB *p_cb, UINT16 vendor_id, UINT16 product_id,
UINT16 version, UINT8 flag);
extern void bta_hh_cleanup_disable(tBTA_HH_STATUS status);
extern UINT8 bta_hh_dev_handle_to_cb_idx(UINT8 dev_handle);
/* action functions used outside state machine */
extern void bta_hh_api_enable(tBTA_HH_DATA *p_data);
extern void bta_hh_api_disable(void);
extern void bta_hh_disc_cmpl(void);
extern tBTA_HH_STATUS bta_hh_read_ssr_param(BD_ADDR bd_addr, UINT16 *p_max_ssr_lat, UINT16 *p_min_ssr_tout);
/* functions for LE HID */
extern void bta_hh_le_enable(void);
extern BOOLEAN bta_hh_le_is_hh_gatt_if(tBTA_GATTC_IF client_if);
extern void bta_hh_le_deregister(void);
extern BOOLEAN bta_hh_is_le_device(tBTA_HH_DEV_CB *p_cb, BD_ADDR remote_bda);
extern void bta_hh_le_open_conn(tBTA_HH_DEV_CB *p_cb, BD_ADDR remote_bda);
extern void bta_hh_le_api_disc_act(tBTA_HH_DEV_CB *p_cb);
extern void bta_hh_le_get_dscp_act(tBTA_HH_DEV_CB *p_cb);
extern void bta_hh_le_write_dev_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern UINT8 bta_hh_le_add_device(tBTA_HH_DEV_CB *p_cb, tBTA_HH_MAINT_DEV *p_dev_info);
extern void bta_hh_le_remove_dev_bg_conn(tBTA_HH_DEV_CB *p_cb);
extern void bta_hh_le_open_fail(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_gatt_open(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_gatt_close(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_start_security(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_start_srvc_discovery(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_w4_le_read_char_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_le_read_char_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_w4_le_read_descr_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_le_read_descr_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_w4_le_write_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_le_write_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_le_write_char_descr_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_start_security(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_security_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_le_update_scpp(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
extern void bta_hh_le_notify_enc_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
extern void bta_hh_ci_load_rpt (tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_buf);
#if BTA_HH_DEBUG
extern void bta_hh_trace_dev_db(void);
#endif
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,593 @@
/******************************************************************************
*
* Copyright (C) 2005-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the HID host main functions and state machine.
*
******************************************************************************/
#include "bt_target.h"
#if defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)
#include <string.h>
#include "bta_hh_api.h"
#include "bta_hh_int.h"
#include "gki.h"
/*****************************************************************************
** Constants and types
*****************************************************************************/
/* state machine action enumeration list */
enum
{
BTA_HH_API_DISC_ACT, /* HID host process API close action */
BTA_HH_OPEN_ACT, /* HID host process BTA_HH_EVT_OPEN */
BTA_HH_CLOSE_ACT, /* HID host process BTA_HH_EVT_CLOSE */
BTA_HH_DATA_ACT, /* HID host receive data report */
BTA_HH_CTRL_DAT_ACT,
BTA_HH_HANDSK_ACT,
BTA_HH_START_SDP, /* HID host inquery */
BTA_HH_SDP_CMPL,
BTA_HH_WRITE_DEV_ACT,
BTA_HH_GET_DSCP_ACT,
BTA_HH_MAINT_DEV_ACT,
BTA_HH_OPEN_CMPL_ACT,
BTA_HH_OPEN_FAILURE,
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
BTA_HH_GATT_CLOSE,
BTA_HH_LE_OPEN_FAIL,
BTA_HH_GATT_OPEN,
BTA_HH_W4_LE_READ_CHAR,
BTA_HH_LE_READ_CHAR,
BTA_HH_W4_LE_READ_DESCR,
BTA_HH_LE_READ_DESCR,
BTA_HH_W4_LE_WRITE,
BTA_HH_LE_WRITE,
BTA_HH_WRITE_DESCR,
BTA_HH_START_SEC,
BTA_HH_SEC_CMPL,
BTA_HH_LE_UPDATE_SCPP,
BTA_HH_GATT_ENC_CMPL,
#endif
BTA_HH_NUM_ACTIONS
};
#define BTA_HH_IGNORE BTA_HH_NUM_ACTIONS
/* type for action functions */
typedef void (*tBTA_HH_ACTION)(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data);
/* action functions */
const tBTA_HH_ACTION bta_hh_action[] =
{
bta_hh_api_disc_act,
bta_hh_open_act,
bta_hh_close_act,
bta_hh_data_act,
bta_hh_ctrl_dat_act,
bta_hh_handsk_act,
bta_hh_start_sdp,
bta_hh_sdp_cmpl,
bta_hh_write_dev_act,
bta_hh_get_dscp_act,
bta_hh_maint_dev_act,
bta_hh_open_cmpl_act,
bta_hh_open_failure
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
,bta_hh_gatt_close
,bta_hh_le_open_fail
,bta_hh_gatt_open
,bta_hh_w4_le_read_char_cmpl
,bta_hh_le_read_char_cmpl
,bta_hh_w4_le_read_descr_cmpl
,bta_hh_le_read_descr_cmpl
,bta_hh_w4_le_write_cmpl
,bta_hh_le_write_cmpl
,bta_hh_le_write_char_descr_cmpl
,bta_hh_start_security
,bta_hh_security_cmpl
,bta_hh_le_update_scpp
,bta_hh_le_notify_enc_cmpl
#endif
};
/* state table information */
#define BTA_HH_ACTION 0 /* position of action */
#define BTA_HH_NEXT_STATE 1 /* position of next state */
#define BTA_HH_NUM_COLS 2 /* number of columns */
/* state table for idle state */
const UINT8 bta_hh_st_idle[][BTA_HH_NUM_COLS] =
{
/* Event Action Next state */
/* BTA_HH_API_OPEN_EVT */ {BTA_HH_START_SDP, BTA_HH_W4_CONN_ST },
/* BTA_HH_API_CLOSE_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST },
/* BTA_HH_INT_OPEN_EVT */ {BTA_HH_OPEN_ACT, BTA_HH_W4_CONN_ST },
/* BTA_HH_INT_CLOSE_EVT */ {BTA_HH_CLOSE_ACT, BTA_HH_IDLE_ST },
/* BTA_HH_INT_DATA_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST },
/* BTA_HH_INT_CTRL_DATA */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST },
/* BTA_HH_INT_HANDSK_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST },
/* BTA_HH_SDP_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST },
/* BTA_HH_API_WRITE_DEV_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST },
/* BTA_HH_API_GET_DSCP_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST },
/* BTA_HH_API_MAINT_DEV_EVT */ {BTA_HH_MAINT_DEV_ACT, BTA_HH_IDLE_ST },
/* BTA_HH_OPEN_CMPL_EVT */ {BTA_HH_OPEN_CMPL_ACT, BTA_HH_CONN_ST }
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
/* BTA_HH_GATT_CLOSE_EVT */ ,{BTA_HH_IGNORE, BTA_HH_IDLE_ST }
/* BTA_HH_GATT_OPEN_EVT */ ,{BTA_HH_GATT_OPEN, BTA_HH_W4_CONN_ST }
/* BTA_HH_START_ENC_EVT */ ,{BTA_HH_IGNORE, BTA_HH_IDLE_ST }
/* BTA_HH_ENC_CMPL_EVT */ ,{BTA_HH_IGNORE, BTA_HH_IDLE_ST }
/* READ_CHAR_CMPL_EVT */ ,{BTA_HH_IGNORE, BTA_HH_IDLE_ST }
/* BTA_HH_GATT_WRITE_CMPL_EVT*/ ,{BTA_HH_IGNORE, BTA_HH_IDLE_ST }
/* READ_DESCR_CMPL_EVT */ ,{BTA_HH_IGNORE, BTA_HH_IDLE_ST }
/* WRITE_DESCR_CMPL_EVT */ ,{BTA_HH_IGNORE, BTA_HH_IDLE_ST }
/* SCPP_UPDATE_EVT */ ,{BTA_HH_IGNORE, BTA_HH_IDLE_ST }
/* BTA_HH_GATT_ENC_CMPL_EVT */ ,{BTA_HH_IGNORE, BTA_HH_IDLE_ST }
#endif
};
const UINT8 bta_hh_st_w4_conn[][BTA_HH_NUM_COLS] =
{
/* Event Action Next state */
/* BTA_HH_API_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST },
/* BTA_HH_API_CLOSE_EVT */ {BTA_HH_IGNORE, BTA_HH_IDLE_ST },
/* BTA_HH_INT_OPEN_EVT */ {BTA_HH_OPEN_ACT, BTA_HH_W4_CONN_ST },
/* BTA_HH_INT_CLOSE_EVT */ {BTA_HH_OPEN_FAILURE, BTA_HH_IDLE_ST },
/* BTA_HH_INT_DATA_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST },
/* BTA_HH_INT_CTRL_DATA */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST },
/* BTA_HH_INT_HANDSK_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST },
/* BTA_HH_SDP_CMPL_EVT */ {BTA_HH_SDP_CMPL, BTA_HH_W4_CONN_ST },
/* BTA_HH_API_WRITE_DEV_EVT */ {BTA_HH_WRITE_DEV_ACT, BTA_HH_W4_CONN_ST },
/* BTA_HH_API_GET_DSCP_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_CONN_ST },
/* BTA_HH_API_MAINT_DEV_EVT */ {BTA_HH_MAINT_DEV_ACT, BTA_HH_IDLE_ST },
/* BTA_HH_OPEN_CMPL_EVT */ {BTA_HH_OPEN_CMPL_ACT, BTA_HH_CONN_ST }
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
/* BTA_HH_GATT_CLOSE_EVT */ ,{BTA_HH_LE_OPEN_FAIL, BTA_HH_IDLE_ST }
/* BTA_HH_GATT_OPEN_EVT */ ,{BTA_HH_GATT_OPEN, BTA_HH_W4_CONN_ST }
/* BTA_HH_START_ENC_EVT */ ,{BTA_HH_START_SEC, BTA_HH_W4_SEC }
/* BTA_HH_ENC_CMPL_EVT */ ,{BTA_HH_IGNORE, BTA_HH_W4_CONN_ST }
/* READ_CHAR_CMPL_EVT */ ,{BTA_HH_W4_LE_READ_CHAR, BTA_HH_W4_CONN_ST }
/* BTA_HH_GATT_WRITE_CMPL_EVT*/ ,{BTA_HH_W4_LE_WRITE, BTA_HH_W4_CONN_ST }
/* READ_DESCR_CMPL_EVT */ ,{BTA_HH_W4_LE_READ_DESCR, BTA_HH_W4_CONN_ST }
/* WRITE_DESCR_CMPL_EVT */ ,{BTA_HH_WRITE_DESCR, BTA_HH_W4_CONN_ST }
/* SCPP_UPDATE_EVT */ ,{BTA_HH_IGNORE, BTA_HH_W4_CONN_ST }
/* BTA_HH_GATT_ENC_CMPL_EVT */ ,{BTA_HH_IGNORE, BTA_HH_W4_CONN_ST }
#endif
};
const UINT8 bta_hh_st_connected[][BTA_HH_NUM_COLS] =
{
/* Event Action Next state */
/* BTA_HH_API_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_CONN_ST },
/* BTA_HH_API_CLOSE_EVT */ {BTA_HH_API_DISC_ACT, BTA_HH_CONN_ST },
/* BTA_HH_INT_OPEN_EVT */ {BTA_HH_OPEN_ACT, BTA_HH_CONN_ST },
/* BTA_HH_INT_CLOSE_EVT */ {BTA_HH_CLOSE_ACT, BTA_HH_IDLE_ST },
/* BTA_HH_INT_DATA_EVT */ {BTA_HH_DATA_ACT, BTA_HH_CONN_ST },
/* BTA_HH_INT_CTRL_DATA */ {BTA_HH_CTRL_DAT_ACT, BTA_HH_CONN_ST },
/* BTA_HH_INT_HANDSK_EVT */ {BTA_HH_HANDSK_ACT, BTA_HH_CONN_ST },
/* BTA_HH_SDP_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_CONN_ST },
/* BTA_HH_API_WRITE_DEV_EVT */ {BTA_HH_WRITE_DEV_ACT, BTA_HH_CONN_ST },
/* BTA_HH_API_GET_DSCP_EVT */ {BTA_HH_GET_DSCP_ACT, BTA_HH_CONN_ST },
/* BTA_HH_API_MAINT_DEV_EVT */ {BTA_HH_MAINT_DEV_ACT, BTA_HH_CONN_ST },
/* BTA_HH_OPEN_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_CONN_ST }
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
/* BTA_HH_GATT_CLOSE_EVT */ ,{BTA_HH_GATT_CLOSE, BTA_HH_IDLE_ST }
/* BTA_HH_GATT_OPEN_EVT */ ,{BTA_HH_IGNORE, BTA_HH_CONN_ST }
/* BTA_HH_START_ENC_EVT */ ,{BTA_HH_IGNORE, BTA_HH_CONN_ST }
/* BTA_HH_ENC_CMPL_EVT */ ,{BTA_HH_IGNORE, BTA_HH_CONN_ST }
/* READ_CHAR_CMPL_EVT */ ,{BTA_HH_LE_READ_CHAR, BTA_HH_CONN_ST }
/* WRITE_CHAR_CMPL_EVT*/ ,{BTA_HH_LE_WRITE, BTA_HH_CONN_ST }
/* READ_DESCR_CMPL_EVT */ ,{BTA_HH_LE_READ_DESCR, BTA_HH_CONN_ST } /* do not currently read any descr when connection up */
/* WRITE_DESCR_CMPL_EVT */ ,{BTA_HH_WRITE_DESCR, BTA_HH_CONN_ST } /* do not currently write any descr when connection up */
/* SCPP_UPDATE_EVT */ ,{BTA_HH_LE_UPDATE_SCPP, BTA_HH_CONN_ST }
/* BTA_HH_GATT_ENC_CMPL_EVT */ ,{BTA_HH_IGNORE, BTA_HH_CONN_ST }
#endif
};
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
const UINT8 bta_hh_st_w4_sec[][BTA_HH_NUM_COLS] =
{
/* Event Action Next state */
/* BTA_HH_API_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_API_CLOSE_EVT */ {BTA_HH_API_DISC_ACT, BTA_HH_W4_SEC },
/* BTA_HH_INT_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_INT_CLOSE_EVT */ {BTA_HH_OPEN_FAILURE, BTA_HH_IDLE_ST },
/* BTA_HH_INT_DATA_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_INT_CTRL_DATA */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_INT_HANDSK_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_SDP_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_API_WRITE_DEV_EVT */ {BTA_HH_IGNORE , BTA_HH_W4_SEC },
/* BTA_HH_API_GET_DSCP_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_API_MAINT_DEV_EVT */ {BTA_HH_MAINT_DEV_ACT, BTA_HH_W4_SEC },
/* BTA_HH_OPEN_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_GATT_CLOSE_EVT */ {BTA_HH_LE_OPEN_FAIL, BTA_HH_IDLE_ST },
/* BTA_HH_GATT_OPEN_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_START_ENC_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_ENC_CMPL_EVT */ {BTA_HH_SEC_CMPL, BTA_HH_W4_CONN_ST },
/* READ_CHAR_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* BTA_HH_GATT_WRITE_CMPL_EVT*/ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* READ_DESCR_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC },
/* WRITE_DESCR_CMPL_EVT */ {BTA_HH_IGNORE, BTA_HH_W4_SEC }
/* SCPP_UPDATE_EVT */ ,{BTA_HH_IGNORE, BTA_HH_W4_SEC }
/* BTA_HH_GATT_ENC_CMPL_EVT */ ,{BTA_HH_GATT_ENC_CMPL, BTA_HH_W4_SEC }
};
#endif
/* type for state table */
typedef const UINT8 (*tBTA_HH_ST_TBL)[BTA_HH_NUM_COLS];
/* state table */
const tBTA_HH_ST_TBL bta_hh_st_tbl[] =
{
bta_hh_st_idle,
bta_hh_st_w4_conn,
bta_hh_st_connected
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
,bta_hh_st_w4_sec
#endif
};
/*****************************************************************************
** Global data
*****************************************************************************/
#if BTA_DYNAMIC_MEMORY == FALSE
tBTA_HH_CB bta_hh_cb;
#endif
/*****************************************************************************
** Static functions
*****************************************************************************/
#if BTA_HH_DEBUG == TRUE
static char *bta_hh_evt_code(tBTA_HH_INT_EVT evt_code);
static char *bta_hh_state_code(tBTA_HH_STATE state_code);
#endif
/*******************************************************************************
**
** Function bta_hh_sm_execute
**
** Description State machine event handling function for HID Host
**
**
** Returns void
**
*******************************************************************************/
void bta_hh_sm_execute(tBTA_HH_DEV_CB *p_cb, UINT16 event, tBTA_HH_DATA * p_data)
{
tBTA_HH_ST_TBL state_table;
UINT8 action;
tBTA_HH cback_data;
tBTA_HH_EVT cback_event = 0;
#if BTA_HH_DEBUG == TRUE
tBTA_HH_STATE in_state ;
UINT16 debug_event = event;
#endif
memset(&cback_data, 0, sizeof(tBTA_HH));
/* handle exception, no valid control block was found */
if (!p_cb)
{
/* BTA HH enabled already? otherwise ignore the event although it's bad*/
if (bta_hh_cb.p_cback != NULL)
{
switch (event)
{
/* no control block available for new connection */
case BTA_HH_API_OPEN_EVT:
cback_event = BTA_HH_OPEN_EVT;
/* build cback data */
bdcpy(cback_data.conn.bda, ((tBTA_HH_API_CONN *)p_data)->bd_addr);
cback_data.conn.status = BTA_HH_ERR_DB_FULL;
cback_data.conn.handle = BTA_HH_INVALID_HANDLE;
break;
/* DB full, BTA_HhAddDev */
case BTA_HH_API_MAINT_DEV_EVT:
cback_event = p_data->api_maintdev.sub_event;
if (p_data->api_maintdev.sub_event == BTA_HH_ADD_DEV_EVT)
{
bdcpy(cback_data.dev_info.bda, p_data->api_maintdev.bda);
cback_data.dev_info.status = BTA_HH_ERR_DB_FULL;
cback_data.dev_info.handle = BTA_HH_INVALID_HANDLE;
}
else
{
cback_data.dev_info.status = BTA_HH_ERR_HDL;
cback_data.dev_info.handle = (UINT8)p_data->api_maintdev.hdr.layer_specific;
}
break;
case BTA_HH_API_WRITE_DEV_EVT:
cback_event = (p_data->api_sndcmd.t_type - BTA_HH_FST_BTE_TRANS_EVT) +
BTA_HH_FST_TRANS_CB_EVT;
if (p_data->api_sndcmd.p_data != NULL)
{
GKI_freebuf(p_data->api_sndcmd.p_data);
}
if (p_data->api_sndcmd.t_type == HID_TRANS_SET_PROTOCOL ||
p_data->api_sndcmd.t_type == HID_TRANS_SET_REPORT ||
p_data->api_sndcmd.t_type == HID_TRANS_SET_IDLE)
{
cback_data.dev_status.status = BTA_HH_ERR_HDL;
cback_data.dev_status.handle = (UINT8)p_data->api_sndcmd.hdr.layer_specific;
}
else if (p_data->api_sndcmd.t_type != HID_TRANS_DATA &&
p_data->api_sndcmd.t_type != HID_TRANS_CONTROL)
{
cback_data.hs_data.handle = (UINT8)p_data->api_sndcmd.hdr.layer_specific;
cback_data.hs_data.status = BTA_HH_ERR_HDL;
/* hs_data.rsp_data will be all zero, which is not valid value */
}
else if (p_data->api_sndcmd.t_type == HID_TRANS_CONTROL &&
p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
{
cback_data.status = BTA_HH_ERR_HDL;
cback_event = BTA_HH_VC_UNPLUG_EVT;
}
else
cback_event = 0;
break;
case BTA_HH_API_CLOSE_EVT:
cback_event = BTA_HH_CLOSE_EVT;
cback_data.dev_status.status = BTA_HH_ERR_HDL;
cback_data.dev_status.handle = (UINT8)p_data->api_sndcmd.hdr.layer_specific;
break;
default:
/* invalid handle, call bad API event */
APPL_TRACE_ERROR("wrong device handle: [%d]", p_data->hdr.layer_specific);
/* Free the callback buffer now */
if (p_data != NULL && p_data->hid_cback.p_data != NULL)
{
GKI_freebuf(p_data->hid_cback.p_data);
p_data->hid_cback.p_data = NULL;
}
break;
}
if (cback_event)
(* bta_hh_cb.p_cback)(cback_event, &cback_data);
}
}
/* corresponding CB is found, go to state machine */
else
{
#if BTA_HH_DEBUG == TRUE
in_state = p_cb->state;
APPL_TRACE_EVENT("bta_hh_sm_execute: State 0x%02x [%s], Event [%s]",
in_state, bta_hh_state_code(in_state),
bta_hh_evt_code(debug_event));
#endif
if ((p_cb->state == BTA_HH_NULL_ST) || (p_cb->state >= BTA_HH_INVALID_ST))
{
APPL_TRACE_ERROR("bta_hh_sm_execute: Invalid state State = 0x%x, Event = %d",
p_cb->state,event);
return;
}
state_table = bta_hh_st_tbl[p_cb->state - 1];
event &= 0xff;
p_cb->state = state_table[event][BTA_HH_NEXT_STATE] ;
if ((action = state_table[event][BTA_HH_ACTION]) != BTA_HH_IGNORE)
{
(*bta_hh_action[action])(p_cb, p_data);
}
#if BTA_HH_DEBUG == TRUE
if (in_state != p_cb->state)
{
APPL_TRACE_DEBUG("HH State Change: [%s] -> [%s] after Event [%s]",
bta_hh_state_code(in_state),
bta_hh_state_code(p_cb->state),
bta_hh_evt_code(debug_event));
}
#endif
}
return;
}
/*******************************************************************************
**
** Function bta_hh_hdl_event
**
** Description HID host main event handling function.
**
**
** Returns void
**
*******************************************************************************/
BOOLEAN bta_hh_hdl_event(BT_HDR *p_msg)
{
UINT8 index = BTA_HH_IDX_INVALID;
tBTA_HH_DEV_CB *p_cb = NULL;
switch (p_msg->event)
{
case BTA_HH_API_ENABLE_EVT:
bta_hh_api_enable((tBTA_HH_DATA *) p_msg);
break;
case BTA_HH_API_DISABLE_EVT:
bta_hh_api_disable();
break;
case BTA_HH_DISC_CMPL_EVT: /* disable complete */
bta_hh_disc_cmpl();
break;
default:
/* all events processed in state machine need to find corresponding
CB before proceed */
if (p_msg->event == BTA_HH_API_OPEN_EVT)
{
index = bta_hh_find_cb(((tBTA_HH_API_CONN *)p_msg)->bd_addr);
}
else if (p_msg->event == BTA_HH_API_MAINT_DEV_EVT)
{
/* if add device */
if (((tBTA_HH_MAINT_DEV *)p_msg)->sub_event == BTA_HH_ADD_DEV_EVT)
{
index = bta_hh_find_cb(((tBTA_HH_MAINT_DEV *)p_msg)->bda);
}
else /* else remove device by handle */
{
index = bta_hh_dev_handle_to_cb_idx((UINT8)p_msg->layer_specific);
// btla-specific ++
/* If BT disable is done while the HID device is connected and Link_Key uses unauthenticated combination
* then we can get into a situation where remove_bonding is called with the index set to 0 (without getting
* cleaned up). Only when VIRTUAL_UNPLUG is called do we cleanup the index and make it MAX_KNOWN.
* So if REMOVE_DEVICE is called and in_use is FALSE then we should treat this as a NULL p_cb. Hence we
* force the index to be IDX_INVALID
*/
if ((index != BTA_HH_IDX_INVALID) &&
(bta_hh_cb.kdev[index].in_use == FALSE)) {
index = BTA_HH_IDX_INVALID;
}
// btla-specific --
}
}
else if (p_msg->event == BTA_HH_INT_OPEN_EVT)
{
index = bta_hh_find_cb(((tBTA_HH_CBACK_DATA *)p_msg)->addr);
}
else
index = bta_hh_dev_handle_to_cb_idx((UINT8)p_msg->layer_specific);
if (index != BTA_HH_IDX_INVALID)
p_cb = &bta_hh_cb.kdev[index];
#if BTA_HH_DEBUG
APPL_TRACE_DEBUG("bta_hh_hdl_event:: handle = %d dev_cb[%d] ", p_msg->layer_specific, index);
#endif
bta_hh_sm_execute(p_cb, p_msg->event, (tBTA_HH_DATA *) p_msg);
}
return (TRUE);
}
/*****************************************************************************
** Debug Functions
*****************************************************************************/
#if BTA_HH_DEBUG
/*******************************************************************************
**
** Function bta_hh_evt_code
**
** Description
**
** Returns void
**
*******************************************************************************/
static char *bta_hh_evt_code(tBTA_HH_INT_EVT evt_code)
{
switch(evt_code)
{
case BTA_HH_API_DISABLE_EVT:
return "BTA_HH_API_DISABLE_EVT";
case BTA_HH_API_ENABLE_EVT:
return "BTA_HH_API_ENABLE_EVT";
case BTA_HH_API_OPEN_EVT:
return "BTA_HH_API_OPEN_EVT";
case BTA_HH_API_CLOSE_EVT:
return "BTA_HH_API_CLOSE_EVT";
case BTA_HH_INT_OPEN_EVT:
return "BTA_HH_INT_OPEN_EVT";
case BTA_HH_INT_CLOSE_EVT:
return "BTA_HH_INT_CLOSE_EVT";
case BTA_HH_INT_HANDSK_EVT:
return "BTA_HH_INT_HANDSK_EVT";
case BTA_HH_INT_DATA_EVT:
return "BTA_HH_INT_DATA_EVT";
case BTA_HH_INT_CTRL_DATA:
return "BTA_HH_INT_CTRL_DATA";
case BTA_HH_API_WRITE_DEV_EVT:
return "BTA_HH_API_WRITE_DEV_EVT";
case BTA_HH_SDP_CMPL_EVT:
return "BTA_HH_SDP_CMPL_EVT";
case BTA_HH_DISC_CMPL_EVT:
return "BTA_HH_DISC_CMPL_EVT";
case BTA_HH_API_MAINT_DEV_EVT:
return "BTA_HH_API_MAINT_DEV_EVT";
case BTA_HH_API_GET_DSCP_EVT:
return "BTA_HH_API_GET_DSCP_EVT";
case BTA_HH_OPEN_CMPL_EVT:
return "BTA_HH_OPEN_CMPL_EVT";
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
case BTA_HH_GATT_CLOSE_EVT:
return "BTA_HH_GATT_CLOSE_EVT";
case BTA_HH_GATT_OPEN_EVT:
return "BTA_HH_GATT_OPEN_EVT";
case BTA_HH_START_ENC_EVT:
return "BTA_HH_START_ENC_EVT";
case BTA_HH_ENC_CMPL_EVT:
return "BTA_HH_ENC_CMPL_EVT";
case BTA_HH_GATT_READ_CHAR_CMPL_EVT:
return "BTA_HH_GATT_READ_CHAR_CMPL_EVT";
case BTA_HH_GATT_WRITE_CHAR_CMPL_EVT:
return "BTA_HH_GATT_WRITE_CHAR_CMPL_EVT";
case BTA_HH_GATT_READ_DESCR_CMPL_EVT:
return "BTA_HH_GATT_READ_DESCR_CMPL_EVT";
case BTA_HH_GATT_WRITE_DESCR_CMPL_EVT:
return "BTA_HH_GATT_WRITE_DESCR_CMPL_EVT";
#endif
default:
return "unknown HID Host event code";
}
}
/*******************************************************************************
**
** Function bta_hh_state_code
**
** Description get string representation of HID host state code.
**
** Returns void
**
*******************************************************************************/
static char *bta_hh_state_code(tBTA_HH_STATE state_code)
{
switch (state_code)
{
case BTA_HH_NULL_ST:
return"BTA_HH_NULL_ST";
case BTA_HH_IDLE_ST:
return "BTA_HH_IDLE_ST";
case BTA_HH_W4_CONN_ST:
return "BTA_HH_W4_CONN_ST";
case BTA_HH_CONN_ST:
return "BTA_HH_CONN_ST";
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
case BTA_HH_W4_SEC:
return "BTA_HH_W4_SEC";
#endif
default:
return "unknown HID Host state";
}
}
#endif /* Debug Functions */
#endif /* BTA_HH_INCLUDED */

View file

@ -0,0 +1,539 @@
/******************************************************************************
*
* Copyright (C) 2005-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <string.h>
#include "bt_target.h"
#if defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)
#include "bta_hh_int.h"
/* if SSR max latency is not defined by remote device, set the default value
as half of the link supervision timeout */
#define BTA_HH_GET_DEF_SSR_MAX_LAT(x) ((x)>> 1)
/*****************************************************************************
** Constants
*****************************************************************************/
#define BTA_HH_KB_CTRL_MASK 0x11
#define BTA_HH_KB_SHIFT_MASK 0x22
#define BTA_HH_KB_ALT_MASK 0x44
#define BTA_HH_KB_GUI_MASK 0x88
#define BTA_HH_KB_CAPS_LOCK 0x39 /* caps lock */
#define BTA_HH_KB_NUM_LOCK 0x53 /* num lock */
#define BTA_HH_MAX_RPT_CHARS 8
static const UINT8 bta_hh_mod_key_mask[BTA_HH_MOD_MAX_KEY] =
{
BTA_HH_KB_CTRL_MASK,
BTA_HH_KB_SHIFT_MASK,
BTA_HH_KB_ALT_MASK,
BTA_HH_KB_GUI_MASK
};
/*******************************************************************************
**
** Function bta_hh_find_cb
**
** Description Find best available control block according to BD address.
**
**
** Returns void
**
*******************************************************************************/
UINT8 bta_hh_find_cb(BD_ADDR bda)
{
UINT8 xx;
/* See how many active devices there are. */
for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++)
{
/* check if any active/known devices is a match */
if ((!bdcmp (bda, bta_hh_cb.kdev[xx].addr) &&
bdcmp(bda, bd_addr_null) != 0) )
{
#if BTA_HH_DEBUG
APPL_TRACE_DEBUG("found kdev_cb[%d] hid_handle = %d ", xx,
bta_hh_cb.kdev[xx].hid_handle)
#endif
return xx;
}
#if BTA_HH_DEBUG
else
APPL_TRACE_DEBUG("in_use ? [%d] kdev[%d].hid_handle = %d state = [%d]",
bta_hh_cb.kdev[xx].in_use, xx,
bta_hh_cb.kdev[xx].hid_handle,
bta_hh_cb.kdev[xx].state);
#endif
}
/* if no active device match, find a spot for it */
for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++)
{
if (!bta_hh_cb.kdev[xx].in_use)
{
bdcpy(bta_hh_cb.kdev[xx].addr, bda);
break;
}
}
/* If device list full, report BTA_HH_IDX_INVALID */
#if BTA_HH_DEBUG
APPL_TRACE_DEBUG("bta_hh_find_cb:: index = %d while max = %d",
xx, BTA_HH_MAX_DEVICE);
#endif
if (xx == BTA_HH_MAX_DEVICE)
xx = BTA_HH_IDX_INVALID;
return xx;
}
/*******************************************************************************
**
** Function bta_hh_clean_up_kdev
**
** Description Clean up device control block when device is removed from
** manitainace list, and update control block index map.
**
** Returns void
**
*******************************************************************************/
void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB *p_cb)
{
UINT8 index;
if (p_cb->hid_handle != BTA_HH_INVALID_HANDLE )
{
#if BTA_HH_LE_INCLUDED == TRUE
if (p_cb->is_le_device)
bta_hh_cb.le_cb_index[BTA_HH_GET_LE_CB_IDX(p_cb->hid_handle)] = BTA_HH_IDX_INVALID;
else
#endif
bta_hh_cb.cb_index[p_cb->hid_handle] = BTA_HH_IDX_INVALID;
}
/* reset device control block */
index = p_cb->index; /* Preserve index for this control block */
/* Free buffer for report descriptor info */
utl_freebuf((void **)&p_cb->dscp_info.descriptor.dsc_list);
memset(p_cb, 0, sizeof (tBTA_HH_DEV_CB)); /* Reset control block */
p_cb->index = index; /* Restore index for this control block */
p_cb->state = BTA_HH_IDLE_ST;
p_cb->hid_handle = BTA_HH_INVALID_HANDLE;
}
/*******************************************************************************
**
** Function bta_hh_update_di_info
**
** Description Maintain a known device list for BTA HH.
**
** Returns void
**
*******************************************************************************/
void bta_hh_update_di_info(tBTA_HH_DEV_CB *p_cb, UINT16 vendor_id, UINT16 product_id,
UINT16 version, UINT8 flag)
{
#if BTA_HH_DEBUG
APPL_TRACE_DEBUG("vendor_id = 0x%2x product_id = 0x%2x version = 0x%2x",
vendor_id, product_id, version);
#endif
p_cb->dscp_info.vendor_id = vendor_id;
p_cb->dscp_info.product_id = product_id;
p_cb->dscp_info.version = version;
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
p_cb->dscp_info.flag = flag;
#else
UNUSED(flag);
#endif
}
/*******************************************************************************
**
** Function bta_hh_add_device_to_list
**
** Description Maintain a known device list for BTA HH.
**
** Returns void
**
*******************************************************************************/
void bta_hh_add_device_to_list(tBTA_HH_DEV_CB *p_cb, UINT8 handle,
UINT16 attr_mask,
tHID_DEV_DSCP_INFO *p_dscp_info,
UINT8 sub_class,
UINT16 ssr_max_latency,
UINT16 ssr_min_tout,
UINT8 app_id)
{
#if BTA_HH_DEBUG
APPL_TRACE_DEBUG("subclass = 0x%2x", sub_class);
#endif
p_cb->hid_handle = handle;
p_cb->in_use = TRUE;
p_cb->attr_mask = attr_mask;
p_cb->sub_class = sub_class;
p_cb->app_id = app_id;
p_cb->dscp_info.ssr_max_latency = ssr_max_latency;
p_cb->dscp_info.ssr_min_tout = ssr_min_tout;
/* store report descriptor info */
if ( p_dscp_info)
{
utl_freebuf((void **)&p_cb->dscp_info.descriptor.dsc_list);
if (p_dscp_info->dl_len &&
(p_cb->dscp_info.descriptor.dsc_list =
(UINT8 *)GKI_getbuf(p_dscp_info->dl_len)) != NULL)
{
p_cb->dscp_info.descriptor.dl_len = p_dscp_info->dl_len;
memcpy(p_cb->dscp_info.descriptor.dsc_list, p_dscp_info->dsc_list,
p_dscp_info->dl_len);
}
}
return;
}
/*******************************************************************************
**
** Function bta_hh_tod_spt
**
** Description Check to see if this type of device is supported
**
** Returns
**
*******************************************************************************/
BOOLEAN bta_hh_tod_spt(tBTA_HH_DEV_CB *p_cb,UINT8 sub_class)
{
UINT8 xx;
UINT8 cod = (sub_class >> 2); /* lower two bits are reserved */
for (xx = 0 ; xx < p_bta_hh_cfg->max_devt_spt; xx ++)
{
if (cod == (UINT8) p_bta_hh_cfg->p_devt_list[xx].tod)
{
p_cb->app_id = p_bta_hh_cfg->p_devt_list[xx].app_id;
#if BTA_HH_DEBUG
APPL_TRACE_EVENT("bta_hh_tod_spt sub_class:0x%x supported", sub_class);
#endif
return TRUE;
}
}
#if BTA_HH_DEBUG
APPL_TRACE_EVENT("bta_hh_tod_spt sub_class:0x%x NOT supported", sub_class);
#endif
return FALSE;
}
/*******************************************************************************
**
** Function bta_hh_parse_keybd_rpt
**
** Description This utility function parse a boot mode keyboard report.
**
** Returns void
**
*******************************************************************************/
void bta_hh_parse_keybd_rpt(tBTA_HH_BOOT_RPT *p_kb_data, UINT8 *p_report,
UINT16 report_len)
{
tBTA_HH_KB_CB *p_kb = &bta_hh_cb.kb_cb;
tBTA_HH_KEYBD_RPT *p_data = &p_kb_data->data_rpt.keybd_rpt;
UINT8 this_char, ctl_shift;
UINT16 xx, yy, key_idx = 0;
UINT8 this_report[BTA_HH_MAX_RPT_CHARS];
#if BTA_HH_DEBUG
APPL_TRACE_DEBUG("bta_hh_parse_keybd_rpt: (report=%p, report_len=%d) called",
p_report, report_len);
#endif
if (report_len < 2)
return;
ctl_shift = *p_report++;
report_len--;
if (report_len > BTA_HH_MAX_RPT_CHARS)
report_len = BTA_HH_MAX_RPT_CHARS;
memset (this_report, 0, BTA_HH_MAX_RPT_CHARS);
memset (p_data, 0, sizeof(tBTA_HH_KEYBD_RPT));
memcpy (this_report, p_report, report_len);
/* Take care of shift, control, GUI and alt, modifier keys */
for (xx = 0; xx < BTA_HH_MOD_MAX_KEY; xx ++ )
{
if (ctl_shift & bta_hh_mod_key_mask[xx])
{
APPL_TRACE_DEBUG("Mod Key[%02x] pressed", bta_hh_mod_key_mask[xx] );
p_kb->mod_key[xx] = TRUE;
}
else if (p_kb->mod_key[xx])
{
p_kb->mod_key[xx] = FALSE;
}
/* control key flag is set */
p_data->mod_key[xx] = p_kb->mod_key[xx];
}
/***************************************************************************/
/* First step is to remove all characters we saw in the last report */
/***************************************************************************/
for (xx = 0; xx < report_len; xx++)
{
for (yy = 0; yy < BTA_HH_MAX_RPT_CHARS; yy++)
{
if (this_report[xx] == p_kb->last_report[yy])
{
this_report[xx] = 0;
}
}
}
/***************************************************************************/
/* Now, process all the characters in the report, up to 6 keycodes */
/***************************************************************************/
for (xx = 0; xx < report_len; xx++)
{
#if BTA_HH_DEBUG
APPL_TRACE_DEBUG("this_char = %02x", this_report[xx]);
#endif
if ((this_char = this_report[xx]) == 0)
continue;
/* take the key code as the report data */
if (this_report[xx] == BTA_HH_KB_CAPS_LOCK)
p_kb->caps_lock = p_kb->caps_lock ? FALSE : TRUE;
else if (this_report[xx] == BTA_HH_KB_NUM_LOCK)
p_kb->num_lock = p_kb->num_lock ? FALSE : TRUE;
else
p_data->this_char[key_idx ++] = this_char;
#if BTA_HH_DEBUG
APPL_TRACE_DEBUG("found keycode %02x ", this_report[xx]);
#endif
p_data->caps_lock = p_kb->caps_lock;
p_data->num_lock = p_kb->num_lock;
}
memset (p_kb->last_report, 0, BTA_HH_MAX_RPT_CHARS);
memcpy (p_kb->last_report, p_report, report_len);
return;
}
/*******************************************************************************
**
** Function bta_hh_parse_mice_rpt
**
** Description This utility function parse a boot mode mouse report.
**
** Returns void
**
*******************************************************************************/
void bta_hh_parse_mice_rpt(tBTA_HH_BOOT_RPT *p_mice_data, UINT8 *p_report,
UINT16 report_len)
{
tBTA_HH_MICE_RPT *p_data = &p_mice_data->data_rpt.mice_rpt;
#if BTA_HH_DEBUG
UINT8 xx;
APPL_TRACE_DEBUG("bta_hh_parse_mice_rpt: bta_keybd_rpt_rcvd(report=%p, \
report_len=%d) called", p_report, report_len);
#endif
if (report_len < 3)
return;
if (report_len > BTA_HH_MAX_RPT_CHARS)
report_len = BTA_HH_MAX_RPT_CHARS;
#if BTA_HH_DEBUG
for (xx = 0; xx < report_len; xx++)
{
APPL_TRACE_DEBUG("this_char = %02x", p_report[xx]);
}
#endif
/* only first bytes lower 3 bits valid */
p_data->mouse_button = (p_report[0] & 0x07);
/* x displacement */
p_data->delta_x = p_report[1];
/* y displacement */
p_data->delta_y = p_report[2];
#if BTA_HH_DEBUG
APPL_TRACE_DEBUG("mice button: 0x%2x", p_data->mouse_button);
APPL_TRACE_DEBUG("mice move: x = %d y = %d", p_data->delta_x,
p_data->delta_y );
#endif
return;
}
/*******************************************************************************
**
** Function bta_hh_read_ssr_param
**
** Description Read the SSR Parameter for the remote device
**
** Returns tBTA_HH_STATUS operation status
**
*******************************************************************************/
tBTA_HH_STATUS bta_hh_read_ssr_param(BD_ADDR bd_addr, UINT16 *p_max_ssr_lat, UINT16 *p_min_ssr_tout)
{
tBTA_HH_STATUS status = BTA_HH_ERR;
tBTA_HH_CB *p_cb = &bta_hh_cb;
UINT8 i;
UINT16 ssr_max_latency;
for (i = 0; i < BTA_HH_MAX_KNOWN; i ++)
{
if (memcmp(p_cb->kdev[i].addr, bd_addr, BD_ADDR_LEN) == 0)
{
/* if remote device does not have HIDSSRHostMaxLatency attribute in SDP,
set SSR max latency default value here. */
if (p_cb->kdev[i].dscp_info.ssr_max_latency == HID_SSR_PARAM_INVALID)
{
/* The default is calculated as half of link supervision timeout.*/
BTM_GetLinkSuperTout(p_cb->kdev[i].addr, &ssr_max_latency) ;
ssr_max_latency = BTA_HH_GET_DEF_SSR_MAX_LAT(ssr_max_latency);
/* per 1.1 spec, if the newly calculated max latency is greater than
BTA_HH_SSR_MAX_LATENCY_DEF which is 500ms, use BTA_HH_SSR_MAX_LATENCY_DEF */
if (ssr_max_latency > BTA_HH_SSR_MAX_LATENCY_DEF)
ssr_max_latency = BTA_HH_SSR_MAX_LATENCY_DEF;
* p_max_ssr_lat = ssr_max_latency;
}
else
* p_max_ssr_lat = p_cb->kdev[i].dscp_info.ssr_max_latency;
if (p_cb->kdev[i].dscp_info.ssr_min_tout == HID_SSR_PARAM_INVALID)
* p_min_ssr_tout = BTA_HH_SSR_MIN_TOUT_DEF;
else
* p_min_ssr_tout = p_cb->kdev[i].dscp_info.ssr_min_tout;
status = BTA_HH_OK;
break;
}
}
return status;
}
/*******************************************************************************
**
** Function bta_hh_cleanup_disable
**
** Description when disable finished, cleanup control block and send callback
**
**
** Returns void
**
*******************************************************************************/
void bta_hh_cleanup_disable(tBTA_HH_STATUS status)
{
UINT8 xx;
/* free buffer in CB holding report descriptors */
for(xx = 0; xx < BTA_HH_MAX_DEVICE; xx ++)
{
utl_freebuf((void **)&bta_hh_cb.kdev[xx].dscp_info.descriptor.dsc_list);
}
utl_freebuf((void **)&bta_hh_cb.p_disc_db);
(* bta_hh_cb.p_cback)(BTA_HH_DISABLE_EVT, (tBTA_HH *)&status);
/* all connections are down, no waiting for diconnect */
memset(&bta_hh_cb, 0, sizeof(tBTA_HH_CB));
}
/*******************************************************************************
**
** Function bta_hh_dev_handle_to_cb_idx
**
** Description convert a HID device handle to the device control block index.
**
**
** Returns UINT8: index of the device control block.
**
*******************************************************************************/
UINT8 bta_hh_dev_handle_to_cb_idx(UINT8 dev_handle)
{
UINT8 index = BTA_HH_IDX_INVALID;
#if BTA_HH_LE_INCLUDED == TRUE
if (BTA_HH_IS_LE_DEV_HDL(dev_handle))
{
if (BTA_HH_IS_LE_DEV_HDL_VALID(dev_handle))
index = bta_hh_cb.le_cb_index[BTA_HH_GET_LE_CB_IDX(dev_handle)];
#if BTA_HH_DEBUG == TRUE
APPL_TRACE_DEBUG("bta_hh_dev_handle_to_cb_idx dev_handle = %d index = %d", dev_handle, index);
#endif
}
else
#endif
/* regular HID device checking */
if (dev_handle < BTA_HH_MAX_KNOWN )
index = bta_hh_cb.cb_index[dev_handle];
return index;
}
#if BTA_HH_DEBUG
/*******************************************************************************
**
** Function bta_hh_trace_dev_db
**
** Description Check to see if this type of device is supported
**
** Returns
**
*******************************************************************************/
void bta_hh_trace_dev_db(void)
{
UINT8 xx;
APPL_TRACE_DEBUG("bta_hh_trace_dev_db:: Device DB list********************");
for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++)
{
APPL_TRACE_DEBUG("kdev[%d] in_use[%d] handle[%d] ",xx,
bta_hh_cb.kdev[xx].in_use, bta_hh_cb.kdev[xx].hid_handle);
APPL_TRACE_DEBUG("\t\t\t attr_mask[%04x] state [%d] sub_class[%02x] index = %d",
bta_hh_cb.kdev[xx].attr_mask, bta_hh_cb.kdev[xx].state,
bta_hh_cb.kdev[xx].sub_class, bta_hh_cb.kdev[xx].index);
}
APPL_TRACE_DEBUG("*********************************************************");
}
#endif
#endif /* HL_INCLUDED */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,80 @@
/******************************************************************************
*
* Copyright (C) 2006-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the interface file for device mananger call-in functions.
*
******************************************************************************/
#ifndef BTA_DM_CI_H
#define BTA_DM_CI_H
#include "bta_api.h"
/*****************************************************************************
** Function Declarations
*****************************************************************************/
#ifdef __cplusplus
extern "C"
{
#endif
/*******************************************************************************
**
** Function bta_dm_ci_io_req
**
** Description This function must be called in response to function
** bta_dm_co_io_req(), if *p_oob_data is set to BTA_OOB_UNKNOWN
** by bta_dm_co_io_req().
**
** Returns void
**
*******************************************************************************/
extern void bta_dm_ci_io_req(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req);
/*******************************************************************************
**
** Function bta_dm_ci_rmt_oob
**
** Description This function must be called in response to function
** bta_dm_co_rmt_oob() to provide the OOB data associated
** with the remote device.
**
** Returns void
**
*******************************************************************************/
extern void bta_dm_ci_rmt_oob(BOOLEAN accept, BD_ADDR bd_addr,
BT_OCTET16 c, BT_OCTET16 r);
/*******************************************************************************
**
** Function bta_dm_sco_ci_data_ready
**
** Description This function sends an event to indicating that the phone
** has SCO data ready..
**
** Returns void
**
*******************************************************************************/
extern void bta_dm_sco_ci_data_ready(UINT16 event, UINT16 sco_handle);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,274 @@
/******************************************************************************
*
* Copyright (C) 2006-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the interface file for device mananger callout functions.
*
******************************************************************************/
#ifndef BTA_DM_CO_H
#define BTA_DM_CO_H
#include "bta_sys.h"
#ifndef BTA_SCO_OUT_PKT_SIZE
#define BTA_SCO_OUT_PKT_SIZE BTM_SCO_DATA_SIZE_MAX
#endif
#define BTA_SCO_CODEC_PCM 0 /* used for regular SCO */
#define BTA_SCO_CODEC_SBC 1 /* used for WBS */
typedef UINT8 tBTA_SCO_CODEC_TYPE;
#define BTA_DM_SCO_SAMP_RATE_8K 8000
#define BTA_DM_SCO_SAMP_RATE_16K 16000
/* SCO codec information */
typedef struct
{
tBTA_SCO_CODEC_TYPE codec_type;
}tBTA_CODEC_INFO;
#define BTA_DM_SCO_ROUTE_PCM BTM_SCO_ROUTE_PCM
#define BTA_DM_SCO_ROUTE_HCI BTM_SCO_ROUTE_HCI
typedef tBTM_SCO_ROUTE_TYPE tBTA_DM_SCO_ROUTE_TYPE;
/*****************************************************************************
** Function Declarations
*****************************************************************************/
/*******************************************************************************
**
** Function bta_dm_co_io_req
**
** Description This callout function is executed by DM to get IO capabilities
** of the local device for the Simple Pairing process
**
** Parameters bd_addr - The peer device
** *p_io_cap - The local Input/Output capabilities
** *p_oob_data - TRUE, if OOB data is available for the peer device.
** *p_auth_req - TRUE, if MITM protection is required.
**
** Returns void.
**
*******************************************************************************/
extern void bta_dm_co_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap,
tBTA_OOB_DATA *p_oob_data, tBTA_AUTH_REQ *p_auth_req,
BOOLEAN is_orig);
/*******************************************************************************
**
** Function bta_dm_co_io_rsp
**
** Description This callout function is executed by DM to report IO capabilities
** of the peer device for the Simple Pairing process
**
** Parameters bd_addr - The peer device
** io_cap - The remote Input/Output capabilities
** oob_data - TRUE, if OOB data is available for the peer device.
** auth_req - TRUE, if MITM protection is required.
**
** Returns void.
**
*******************************************************************************/
extern void bta_dm_co_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req);
/*******************************************************************************
**
** Function bta_dm_co_lk_upgrade
**
** Description This callout function is executed by DM to check if the
** platform wants allow link key upgrade
**
** Parameters bd_addr - The peer device
** *p_upgrade - TRUE, if link key upgrade is desired.
**
** Returns void.
**
*******************************************************************************/
extern void bta_dm_co_lk_upgrade(BD_ADDR bd_addr, BOOLEAN *p_upgrade );
/*******************************************************************************
**
** Function bta_dm_co_loc_oob
**
** Description This callout function is executed by DM to report the OOB
** data of the local device for the Simple Pairing process
**
** Parameters valid - TRUE, if the local OOB data is retrieved from LM
** c - Simple Pairing Hash C
** r - Simple Pairing Randomnizer R
**
** Returns void.
**
*******************************************************************************/
extern void bta_dm_co_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r);
/*******************************************************************************
**
** Function bta_dm_co_rmt_oob
**
** Description This callout function is executed by DM to request the OOB
** data for the remote device for the Simple Pairing process
**
** Parameters bd_addr - The peer device
**
** Returns void.
**
*******************************************************************************/
extern void bta_dm_co_rmt_oob(BD_ADDR bd_addr);
/*****************************************************************************
** SCO over HCI Function Declarations
*****************************************************************************/
/*******************************************************************************
**
** Function bta_dm_sco_co_init
**
** Description This function can be used by the phone to initialize audio
** codec or for other initialization purposes before SCO connection
** is opened.
**
**
** Returns Void.
**
*******************************************************************************/
extern tBTA_DM_SCO_ROUTE_TYPE bta_dm_sco_co_init(UINT32 rx_bw, UINT32 tx_bw,
tBTA_CODEC_INFO *p_codec_info, UINT8 app_id);
/*******************************************************************************
**
** Function bta_dm_sco_co_open
**
** Description This function is executed when a SCO connection is open.
**
**
** Returns void
**
*******************************************************************************/
extern void bta_dm_sco_co_open(UINT16 handle, UINT8 pkt_size, UINT16 event);
/*******************************************************************************
**
** Function bta_dm_sco_co_close
**
** Description This function is called when a SCO connection is closed
**
**
** Returns void
**
*******************************************************************************/
extern void bta_dm_sco_co_close(void);
/*******************************************************************************
**
** Function bta_dm_sco_co_out_data
**
** Description This function is called to send SCO data over HCI.
**
** Returns void
**
*******************************************************************************/
extern void bta_dm_sco_co_out_data(BT_HDR **p_buf);
/*******************************************************************************
**
** Function bta_dm_sco_co_in_data
**
** Description This function is called to send incoming SCO data to application.
**
** Returns void
**
*******************************************************************************/
extern void bta_dm_sco_co_in_data(BT_HDR *p_buf, tBTM_SCO_DATA_FLAG status);
/*******************************************************************************
**
** Function bta_dm_co_ble_io_req
**
** Description This callout function is executed by DM to get BLE IO capabilities
** before SMP pairing gets going.
**
** Parameters bd_addr - The peer device
** *p_io_cap - The local Input/Output capabilities
** *p_oob_data - TRUE, if OOB data is available for the peer device.
** *p_auth_req - Auth request setting (Bonding and MITM required or not)
** *p_max_key_size - max key size local device supported.
** *p_init_key - initiator keys.
** *p_resp_key - responder keys.
**
** Returns void.
**
*******************************************************************************/
extern void bta_dm_co_ble_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap,
tBTA_OOB_DATA *p_oob_data,
tBTA_LE_AUTH_REQ *p_auth_req,
UINT8 *p_max_key_size,
tBTA_LE_KEY_TYPE *p_init_key,
tBTA_LE_KEY_TYPE *p_resp_key );
/*******************************************************************************
**
** Function bta_dm_co_ble_local_key_reload
**
** Description This callout function is to load the local BLE keys if available
** on the device.
**
** Parameters none
**
** Returns void.
**
*******************************************************************************/
extern void bta_dm_co_ble_load_local_keys (tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
tBTA_BLE_LOCAL_ID_KEYS *p_id_keys);
// btla-specific ++
/*******************************************************************************
**
** Function bta_dm_co_ble_io_req
**
** Description This callout function is executed by DM to get BLE IO capabilities
** before SMP pairing gets going.
**
** Parameters bd_addr - The peer device
** *p_io_cap - The local Input/Output capabilities
** *p_oob_data - TRUE, if OOB data is available for the peer device.
** *p_auth_req - Auth request setting (Bonding and MITM required or not)
** *p_max_key_size - max key size local device supported.
** *p_init_key - initiator keys.
** *p_resp_key - responder keys.
**
** Returns void.
**
*******************************************************************************/
extern void bta_dm_co_ble_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap,
tBTA_OOB_DATA *p_oob_data,
tBTA_LE_AUTH_REQ *p_auth_req,
UINT8 *p_max_key_size,
tBTA_LE_KEY_TYPE *p_init_key,
tBTA_LE_KEY_TYPE *p_resp_key );
// btla-specific --
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,119 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the interface file for GATT call-in functions.
*
******************************************************************************/
#ifndef BTA_GATTC_CI_H
#define BTA_GATTC_CI_H
#include "bta_gatt_api.h"
/*****************************************************************************
** Constants and data types
*****************************************************************************/
/* Open Complete Event */
typedef struct
{
BT_HDR hdr;
tBTA_GATT_STATUS status;
} tBTA_GATTC_CI_EVT;
#define BTA_GATTC_NV_LOAD_MAX 10
/* Read Ready Event */
typedef struct
{
BT_HDR hdr;
tBTA_GATT_STATUS status;
UINT16 num_attr;
tBTA_GATTC_NV_ATTR attr[BTA_GATTC_NV_LOAD_MAX];
} tBTA_GATTC_CI_LOAD;
/*****************************************************************************
** Function Declarations
*****************************************************************************/
#ifdef __cplusplus
extern "C"
{
#endif
/*******************************************************************************
**
** Function bta_gattc_ci_cache_open
**
** Description This function sends an event to indicate server cache open
** completed.
**
** Parameters server_bda - server BDA of this cache.
** status - BTA_GATT_OK if full buffer of data,
** BTA_GATT_FAIL if an error has occurred.
**
** Returns void
**
*******************************************************************************/
extern void bta_gattc_ci_cache_open(BD_ADDR server_bda, UINT16 evt,
tBTA_GATT_STATUS status, UINT16 conn_id);
/*******************************************************************************
**
** Function bta_gattc_ci_cache_load
**
** Description This function sends an event to BTA indicating the phone has
** load the servere cache and ready to send it to the stack.
**
** Parameters server_bda - server BDA of this cache.
** num_bytes_read - number of bytes read into the buffer
** specified in the read callout-function.
** status - BTA_GATT_OK if full buffer of data,
** BTA_GATT_FAIL if an error has occurred.
**
** Returns void
**
*******************************************************************************/
extern void bta_gattc_ci_cache_load(BD_ADDR server_bda, UINT16 evt,
UINT16 num_attr, tBTA_GATTC_NV_ATTR *p_atrr,
tBTA_GATT_STATUS status, UINT16 conn_id);
/*******************************************************************************
**
** Function bta_gattc_ci_save
**
** Description This function sends an event to BTA indicating the phone has
** save the server cache.
**
** Parameters server_bda - server BDA of this cache.
** status - BTA_GATT_OK if full buffer of data,
** BTA_GATT_FAIL if an error has occurred.
**
** Returns void
**
*******************************************************************************/
extern void bta_gattc_ci_cache_save(BD_ADDR server_bda, UINT16 evt,
tBTA_GATT_STATUS status, UINT16 conn_id);
#ifdef __cplusplus
}
#endif
#endif /* BTA_GATTC_CI_H */

View file

@ -0,0 +1,114 @@
/******************************************************************************
*
* Copyright (C) 2009-2013 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the interface file for BTA GATT client call-out functions.
*
******************************************************************************/
#ifndef BTA_GATTC_CO_H
#define BTA_GATTC_CO_H
#include "bta_gatt_api.h"
/*******************************************************************************
**
** Function bta_gattc_co_cache_open
**
** Description This callout function is executed by GATTC when a GATT server
** cache is ready to be sent.
**
** Parameter server_bda: server bd address of this cache belongs to
** evt: call in event to be passed in when cache open is done.
** conn_id: connection ID of this cache operation attach to.
** to_save: open cache to save or to load.
**
** Returns void.
**
*******************************************************************************/
extern void bta_gattc_co_cache_open(BD_ADDR server_bda, UINT16 evt,
UINT16 conn_id, BOOLEAN to_save);
/*******************************************************************************
**
** Function bta_gattc_co_cache_close
**
** Description This callout function is executed by GATTC when a GATT server
** cache is written completely.
**
** Parameter server_bda: server bd address of this cache belongs to
** conn_id: connection ID of this cache operation attach to.
**
** Returns void.
**
*******************************************************************************/
extern void bta_gattc_co_cache_close(BD_ADDR server_bda, UINT16 conn_id);
/*******************************************************************************
**
** Function bta_gattc_co_cache_save
**
** Description This callout function is executed by GATT when a server cache
** is available to save.
**
** Parameter server_bda: server bd address of this cache belongs to
** evt: call in event to be passed in when cache save is done.
** num_attr: number of attribute to be save.
** p_attr: pointer to the list of attributes to save.
** attr_index: starting attribute index of the save operation.
** conn_id: connection ID of this cache operation attach to.
** Returns
**
*******************************************************************************/
extern void bta_gattc_co_cache_save(BD_ADDR server_bda, UINT16 evt,
UINT16 num_attr, tBTA_GATTC_NV_ATTR *p_attr,
UINT16 attr_index, UINT16 conn_id);
/*******************************************************************************
**
** Function bta_gattc_co_cache_load
**
** Description This callout function is executed by GATT when server cache
** is required to load.
**
** Parameter server_bda: server bd address of this cache belongs to
** evt: call in event to be passed in when cache save is done.
** num_attr: number of attribute to be save.
** attr_index: starting attribute index of the save operation.
** conn_id: connection ID of this cache operation attach to.
** Returns
**
*******************************************************************************/
extern void bta_gattc_co_cache_load(BD_ADDR server_bda, UINT16 evt,
UINT16 start_index, UINT16 conn_id);
/*******************************************************************************
**
** Function bta_gattc_co_cache_reset
**
** Description This callout function is executed by GATTC to reset cache in
** application
**
** Parameter server_bda: server bd address of this cache belongs to
**
** Returns void.
**
*******************************************************************************/
extern void bta_gattc_co_cache_reset(BD_ADDR server_bda);
#endif /* BTA_GATT_CO_H */

View file

@ -0,0 +1,554 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the private file for the file transfer client (FTC).
*
******************************************************************************/
#ifndef BTA_GATTC_INT_H
#define BTA_GATTC_INT_H
#include "bt_target.h"
#include "bta_sys.h"
#include "bta_gatt_api.h"
#include "bta_gattc_ci.h"
#include "bta_gattc_co.h"
#include "gki.h"
/*****************************************************************************
** Constants and data types
*****************************************************************************/
enum
{
BTA_GATTC_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_GATTC),
BTA_GATTC_INT_OPEN_FAIL_EVT,
BTA_GATTC_API_CANCEL_OPEN_EVT,
BTA_GATTC_INT_CANCEL_OPEN_OK_EVT,
BTA_GATTC_API_READ_EVT,
BTA_GATTC_API_WRITE_EVT,
BTA_GATTC_API_EXEC_EVT,
BTA_GATTC_API_CFG_MTU_EVT,
BTA_GATTC_API_CLOSE_EVT,
BTA_GATTC_API_SEARCH_EVT,
BTA_GATTC_API_CONFIRM_EVT,
BTA_GATTC_API_READ_MULTI_EVT,
BTA_GATTC_API_REFRESH_EVT,
BTA_GATTC_INT_CONN_EVT,
BTA_GATTC_INT_DISCOVER_EVT,
BTA_GATTC_DISCOVER_CMPL_EVT,
BTA_GATTC_OP_CMPL_EVT,
BTA_GATTC_INT_DISCONN_EVT,
/* for cache loading/saving */
BTA_GATTC_START_CACHE_EVT,
BTA_GATTC_CI_CACHE_OPEN_EVT,
BTA_GATTC_CI_CACHE_LOAD_EVT,
BTA_GATTC_CI_CACHE_SAVE_EVT,
BTA_GATTC_INT_START_IF_EVT,
BTA_GATTC_API_REG_EVT,
BTA_GATTC_API_DEREG_EVT,
BTA_GATTC_API_LISTEN_EVT,
BTA_GATTC_API_BROADCAST_EVT,
BTA_GATTC_API_DISABLE_EVT,
BTA_GATTC_ENC_CMPL_EVT
};
typedef UINT16 tBTA_GATTC_INT_EVT;
/* max client application GATTC can support */
#ifndef BTA_GATTC_CL_MAX
#define BTA_GATTC_CL_MAX 8 // 32
#endif
/* max known devices GATTC can support */
#ifndef BTA_GATTC_KNOWN_SR_MAX
#define BTA_GATTC_KNOWN_SR_MAX 5 // 10
#endif
#define BTA_GATTC_CONN_MAX GATT_MAX_PHY_CHANNEL
#ifndef BTA_GATTC_CLCB_MAX
#define BTA_GATTC_CLCB_MAX GATT_CL_MAX_LCB
#endif
#define BTA_GATTC_WRITE_PREPARE GATT_WRITE_PREPARE
/* internal strucutre for GATTC register API */
typedef struct
{
BT_HDR hdr;
tBT_UUID app_uuid;
tBTA_GATTC_CBACK *p_cback;
}tBTA_GATTC_API_REG;
typedef struct
{
BT_HDR hdr;
tBTA_GATTC_IF client_if;
}tBTA_GATTC_INT_START_IF;
typedef tBTA_GATTC_INT_START_IF tBTA_GATTC_API_DEREG;
typedef tBTA_GATTC_INT_START_IF tBTA_GATTC_INT_DEREG;
typedef struct
{
BT_HDR hdr;
BD_ADDR remote_bda;
tBTA_GATTC_IF client_if;
BOOLEAN is_direct;
tBTA_TRANSPORT transport;
} tBTA_GATTC_API_OPEN;
typedef tBTA_GATTC_API_OPEN tBTA_GATTC_API_CANCEL_OPEN;
typedef struct
{
BT_HDR hdr;
tBTA_GATT_AUTH_REQ auth_req;
tBTA_GATT_SRVC_ID srvc_id;
tBTA_GATT_ID char_id;
tBTA_GATT_ID *p_descr_type;
} tBTA_GATTC_API_READ;
typedef struct
{
BT_HDR hdr;
tBTA_GATT_AUTH_REQ auth_req;
tBTA_GATT_SRVC_ID srvc_id;
tBTA_GATT_ID char_id;
tBTA_GATT_ID *p_descr_type;
tBTA_GATTC_WRITE_TYPE write_type;
UINT16 offset;
UINT16 len;
UINT8 *p_value;
}tBTA_GATTC_API_WRITE;
typedef struct
{
BT_HDR hdr;
BOOLEAN is_execute;
}tBTA_GATTC_API_EXEC;
typedef struct
{
BT_HDR hdr;
tBTA_GATT_SRVC_ID srvc_id;
tBTA_GATT_ID char_id;
} tBTA_GATTC_API_CONFIRM;
typedef tGATT_CL_COMPLETE tBTA_GATTC_CMPL;
typedef struct
{
BT_HDR hdr;
UINT8 op_code;
tGATT_STATUS status;
tBTA_GATTC_CMPL *p_cmpl;
}tBTA_GATTC_OP_CMPL;
typedef struct
{
BT_HDR hdr;
tBT_UUID *p_srvc_uuid;
}tBTA_GATTC_API_SEARCH;
typedef struct
{
BT_HDR hdr;
tBTA_GATT_AUTH_REQ auth_req;
UINT8 num_attr;
tBTA_GATTC_ATTR_ID *p_id_list;
}tBTA_GATTC_API_READ_MULTI;
typedef struct
{
BT_HDR hdr;
BD_ADDR_PTR remote_bda;
tBTA_GATTC_IF client_if;
BOOLEAN start;
} tBTA_GATTC_API_LISTEN;
typedef struct
{
BT_HDR hdr;
UINT16 mtu;
}tBTA_GATTC_API_CFG_MTU;
typedef struct
{
BT_HDR hdr;
BD_ADDR remote_bda;
tBTA_GATTC_IF client_if;
UINT8 role;
tBT_TRANSPORT transport;
tGATT_DISCONN_REASON reason;
}tBTA_GATTC_INT_CONN;
typedef struct
{
BT_HDR hdr;
BD_ADDR remote_bda;
tBTA_GATTC_IF client_if;
}tBTA_GATTC_ENC_CMPL;
typedef union
{
BT_HDR hdr;
tBTA_GATTC_API_REG api_reg;
tBTA_GATTC_API_DEREG api_dereg;
tBTA_GATTC_API_OPEN api_conn;
tBTA_GATTC_API_CANCEL_OPEN api_cancel_conn;
tBTA_GATTC_API_READ api_read;
tBTA_GATTC_API_SEARCH api_search;
tBTA_GATTC_API_WRITE api_write;
tBTA_GATTC_API_CONFIRM api_confirm;
tBTA_GATTC_API_EXEC api_exec;
tBTA_GATTC_API_READ_MULTI api_read_multi;
tBTA_GATTC_API_CFG_MTU api_mtu;
tBTA_GATTC_OP_CMPL op_cmpl;
tBTA_GATTC_CI_EVT ci_open;
tBTA_GATTC_CI_EVT ci_save;
tBTA_GATTC_CI_LOAD ci_load;
tBTA_GATTC_INT_CONN int_conn;
tBTA_GATTC_ENC_CMPL enc_cmpl;
tBTA_GATTC_INT_START_IF int_start_if;
tBTA_GATTC_INT_DEREG int_dereg;
/* if peripheral role is supported */
tBTA_GATTC_API_LISTEN api_listen;
} tBTA_GATTC_DATA;
/* GATT server cache on the client */
typedef union
{
UINT8 uuid128[LEN_UUID_128];
UINT16 uuid16;
}tBTA_GATTC_UUID;
typedef struct gattc_attr_cache
{
tBTA_GATTC_UUID *p_uuid;
struct gattc_attr_cache *p_next;
UINT16 uuid_len;
UINT16 attr_handle;
UINT8 inst_id;
tBTA_GATT_CHAR_PROP property; /* if characteristic, it is char property;
if included service, flag primary,
if descriptor, not used */
tBTA_GATTC_ATTR_TYPE attr_type;
// btla-specific ++
} __attribute__((packed)) tBTA_GATTC_CACHE_ATTR;
// btla-specific --
typedef struct gattc_svc_cache
{
tBTA_GATT_SRVC_ID service_uuid;
tBTA_GATTC_CACHE_ATTR *p_attr;
tBTA_GATTC_CACHE_ATTR *p_last_attr;
UINT16 s_handle;
UINT16 e_handle;
struct gattc_svc_cache *p_next;
tBTA_GATTC_CACHE_ATTR *p_cur_char;
// btla-specific ++
} __attribute__((packed)) tBTA_GATTC_CACHE;
// btla-specific --
typedef struct
{
tBT_UUID uuid;
UINT16 s_handle;
UINT16 e_handle;
BOOLEAN is_primary;
UINT8 srvc_inst_id;
tBTA_GATT_CHAR_PROP property;
}tBTA_GATTC_ATTR_REC;
#define BTA_GATTC_MAX_CACHE_CHAR 40
#define BTA_GATTC_ATTR_LIST_SIZE (BTA_GATTC_MAX_CACHE_CHAR * sizeof(tBTA_GATTC_ATTR_REC))
#ifndef BTA_GATTC_CACHE_SRVR_SIZE
#define BTA_GATTC_CACHE_SRVR_SIZE 600
#endif
enum
{
BTA_GATTC_IDLE_ST = 0, /* Idle */
BTA_GATTC_W4_CONN_ST, /* Wait for connection - (optional) */
BTA_GATTC_CONN_ST, /* connected state */
BTA_GATTC_DISCOVER_ST /* discover is in progress */
};
typedef UINT8 tBTA_GATTC_STATE;
typedef struct
{
BOOLEAN in_use;
BD_ADDR server_bda;
BOOLEAN connected;
#define BTA_GATTC_SERV_IDLE 0
#define BTA_GATTC_SERV_LOAD 1
#define BTA_GATTC_SERV_SAVE 2
#define BTA_GATTC_SERV_DISC 3
#define BTA_GATTC_SERV_DISC_ACT 4
UINT8 state;
tBTA_GATTC_CACHE *p_srvc_cache;
tBTA_GATTC_CACHE *p_cur_srvc;
BUFFER_Q cache_buffer; /* buffer queue used for storing the cache data */
UINT8 *p_free; /* starting point to next available byte */
UINT16 free_byte; /* number of available bytes in server cache buffer */
UINT8 update_count; /* indication received */
UINT8 num_clcb; /* number of associated CLCB */
tBTA_GATTC_ATTR_REC *p_srvc_list;
UINT8 cur_srvc_idx;
UINT8 cur_char_idx;
UINT8 next_avail_idx;
UINT8 total_srvc;
UINT8 total_char;
UINT8 srvc_hdl_chg; /* service handle change indication pending */
UINT16 attr_index; /* cahce NV saving/loading attribute index */
UINT16 mtu;
} tBTA_GATTC_SERV;
#ifndef BTA_GATTC_NOTIF_REG_MAX
#define BTA_GATTC_NOTIF_REG_MAX 15
#endif
typedef struct
{
BOOLEAN in_use;
BD_ADDR remote_bda;
tBTA_GATTC_CHAR_ID char_id;
}tBTA_GATTC_NOTIF_REG;
typedef struct
{
tBTA_GATTC_CBACK *p_cback;
BOOLEAN in_use;
tBTA_GATTC_IF client_if; /* client interface with BTE stack for this application */
UINT8 num_clcb; /* number of associated CLCB */
BOOLEAN dereg_pending;
tBT_UUID app_uuid;
tBTA_GATTC_NOTIF_REG notif_reg[BTA_GATTC_NOTIF_REG_MAX];
}tBTA_GATTC_RCB;
/* client channel is a mapping between a BTA client(cl_id) and a remote BD address */
typedef struct
{
UINT16 bta_conn_id; /* client channel ID, unique for clcb */
BD_ADDR bda;
tBTA_TRANSPORT transport; /* channel transport */
tBTA_GATTC_RCB *p_rcb; /* pointer to the registration CB */
tBTA_GATTC_SERV *p_srcb; /* server cache CB */
tBTA_GATTC_DATA *p_q_cmd; /* command in queue waiting for execution */
#define BTA_GATTC_NO_SCHEDULE 0
#define BTA_GATTC_DISC_WAITING 0x01
#define BTA_GATTC_REQ_WAITING 0x10
UINT8 auto_update; /* auto update is waiting */
BOOLEAN disc_active;
BOOLEAN in_use;
tBTA_GATTC_STATE state;
tBTA_GATT_STATUS status;
UINT16 reason;
} tBTA_GATTC_CLCB;
/* back ground connection tracking information */
#if GATT_MAX_APPS <= 8
typedef UINT8 tBTA_GATTC_CIF_MASK ;
#elif GATT_MAX_APPS <= 16
typedef UINT16 tBTA_GATTC_CIF_MASK;
#elif GATT_MAX_APPS <= 32
typedef UINT32 tBTA_GATTC_CIF_MASK;
#endif
typedef struct
{
BOOLEAN in_use;
BD_ADDR remote_bda;
tBTA_GATTC_CIF_MASK cif_mask;
tBTA_GATTC_CIF_MASK cif_adv_mask;
}tBTA_GATTC_BG_TCK;
typedef struct
{
BOOLEAN in_use;
BD_ADDR remote_bda;
}tBTA_GATTC_CONN;
enum
{
BTA_GATTC_STATE_DISABLED,
BTA_GATTC_STATE_ENABLING,
BTA_GATTC_STATE_ENABLED,
BTA_GATTC_STATE_DISABLING
};
typedef struct
{
UINT8 state;
tBTA_GATTC_CONN conn_track[BTA_GATTC_CONN_MAX];
tBTA_GATTC_BG_TCK bg_track[BTA_GATTC_KNOWN_SR_MAX];
tBTA_GATTC_RCB cl_rcb[BTA_GATTC_CL_MAX];
tBTA_GATTC_CLCB clcb[BTA_GATTC_CLCB_MAX];
tBTA_GATTC_SERV known_server[BTA_GATTC_KNOWN_SR_MAX];
tSDP_DISCOVERY_DB *p_sdp_db;
UINT16 sdp_conn_id;
}tBTA_GATTC_CB;
/*****************************************************************************
** Global data
*****************************************************************************/
/* GATTC control block */
#if BTA_DYNAMIC_MEMORY == FALSE
extern tBTA_GATTC_CB bta_gattc_cb;
#else
extern tBTA_GATTC_CB *bta_gattc_cb_ptr;
#define bta_gattc_cb (*bta_gattc_cb_ptr)
#endif
/*****************************************************************************
** Function prototypes
*****************************************************************************/
extern BOOLEAN bta_gattc_hdl_event(BT_HDR *p_msg);
extern BOOLEAN bta_gattc_sm_execute(tBTA_GATTC_CLCB *p_clcb, UINT16 event, tBTA_GATTC_DATA *p_data);
/* function processed outside SM */
extern void bta_gattc_disable(tBTA_GATTC_CB *p_cb);
extern void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
extern void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
extern void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB *p_clreg);
extern void bta_gattc_process_enc_cmpl(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg);
/* function within state machine */
extern void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_ci_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA * p_msg);
extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg);
extern void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data);
extern void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
BD_ADDR remote_bda, UINT16 conn_id, tBTA_TRANSPORT transport, UINT16 mtu);
extern void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
extern void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
#if BLE_INCLUDED == TRUE
extern void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
extern void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
#endif
/* utility functions */
extern tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda, tBTA_TRANSPORT transport);
extern tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_conn_id (UINT16 conn_id);
extern tBTA_GATTC_CLCB * bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, tBTA_TRANSPORT transport);
extern void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB *p_clcb);
extern tBTA_GATTC_CLCB * bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, tBTA_TRANSPORT transport);
extern tBTA_GATTC_RCB * bta_gattc_cl_get_regcb(UINT8 client_if);
extern tBTA_GATTC_SERV * bta_gattc_find_srcb(BD_ADDR bda);
extern tBTA_GATTC_SERV * bta_gattc_srcb_alloc(BD_ADDR bda);
extern tBTA_GATTC_SERV * bta_gattc_find_scb_by_cid (UINT16 conn_id);
extern tBTA_GATTC_CLCB * bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA *p_msg);
extern tBTA_GATTC_CLCB * bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA *p_msg);
extern BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
extern UINT16 bta_gattc_id2handle(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_SRVC_ID *p_service_id, tBTA_GATT_ID *p_char_id, tBTA_GATT_ID *p_descr_uuid);
extern BOOLEAN bta_gattc_handle2id(tBTA_GATTC_SERV *p_srcb, UINT16 handle, tBTA_GATT_SRVC_ID *service_id, tBTA_GATT_ID *char_id, tBTA_GATT_ID *p_type);
extern BOOLEAN bta_gattc_uuid_compare (tBT_UUID *p_src, tBT_UUID *p_tar, BOOLEAN is_precise);
extern void bta_gattc_pack_attr_uuid(tBTA_GATTC_CACHE_ATTR *p_attr, tBT_UUID *p_uuid);
extern BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB *p_clreg, tBTA_GATTC_SERV *p_srcb, tBTA_GATTC_NOTIFY *p_notify);
extern tBTA_GATT_STATUS bta_gattc_pack_read_cb_data(tBTA_GATTC_SERV *p_srcb, tBT_UUID *p_descr_uuid, tGATT_VALUE *p_attr, tBTA_GATT_READ_VAL *p_value);
extern BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR_PTR remote_bda, BOOLEAN add, BOOLEAN is_listen);
extern BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR remote_bda, UINT8 role);
extern UINT8 bta_gattc_num_reg_app(void);
extern void bta_gattc_clear_notif_registration(UINT16 conn_id);
extern tBTA_GATTC_SERV * bta_gattc_find_srvr_cache(BD_ADDR bda);
extern BOOLEAN bta_gattc_charid_compare(tBTA_GATTC_CHAR_ID *p_src, tBTA_GATTC_CHAR_ID *p_tar);
extern BOOLEAN bta_gattc_srvcid_compare(tBTA_GATT_SRVC_ID *p_src, tBTA_GATT_SRVC_ID *p_tar);
extern void bta_gattc_cpygattid(tBTA_GATT_ID *p_des, tBTA_GATT_ID *p_src);
/* discovery functions */
extern void bta_gattc_disc_res_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_DISC_RES *p_data);
extern void bta_gattc_disc_cmpl_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status);
extern tBTA_GATT_STATUS bta_gattc_discover_procedure(UINT16 conn_id, tBTA_GATTC_SERV *p_server_cb, UINT8 disc_type);
extern tBTA_GATT_STATUS bta_gattc_discover_pri_service(UINT16 conn_id, tBTA_GATTC_SERV *p_server_cb, UINT8 disc_type);
extern void bta_gattc_search_service(tBTA_GATTC_CLCB *p_clcb, tBT_UUID *p_uuid);
extern tBTA_GATT_STATUS bta_gattc_query_cache(UINT16 conn_id, UINT8 query_type, tBTA_GATT_SRVC_ID *p_srvc_id,
tBTA_GATT_ID *p_start_rec,tBT_UUID *p_uuid_cond,
tBTA_GATT_ID *p_output, void *p_param);
extern tBTA_GATT_STATUS bta_gattc_init_cache(tBTA_GATTC_SERV *p_srvc_cb);
extern void bta_gattc_rebuild_cache(tBTA_GATTC_SERV *p_srcv, UINT16 num_attr, tBTA_GATTC_NV_ATTR *p_attr, UINT16 attr_index);
extern BOOLEAN bta_gattc_cache_save(tBTA_GATTC_SERV *p_srvc_cb, UINT16 conn_id);
extern tBTA_GATTC_CONN * bta_gattc_conn_alloc(BD_ADDR remote_bda);
extern tBTA_GATTC_CONN * bta_gattc_conn_find(BD_ADDR remote_bda);
extern tBTA_GATTC_CONN * bta_gattc_conn_find_alloc(BD_ADDR remote_bda);
extern BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda);
#endif /* BTA_GATTC_INT_H */

View file

@ -0,0 +1,81 @@
/******************************************************************************
*
* Copyright (C) 2010-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the interface file for BTA GATT server call-out functions.
*
******************************************************************************/
#ifndef BTA_GATTS_CO_H
#define BTA_GATTS_CO_H
#include "bta_gatt_api.h"
/*******************************************************************************
**
** Function bta_gatts_co_update_handle_range
**
** Description This callout function is executed by GATTS when a GATT server
** handle range ios to be added or removed.
**
** Parameter is_add: true is to add a handle range; otherwise is to delete.
** p_hndl_range: handle range.
**
** Returns void.
**
*******************************************************************************/
extern void bta_gatts_co_update_handle_range(BOOLEAN is_add, tBTA_GATTS_HNDL_RANGE *p_hndl_range);
/*******************************************************************************
**
** Function bta_gatts_co_srv_chg
**
** Description This call-out is to read/write/remove service change related
** informaiton. The request consists of the cmd and p_req and the
** response is returned in p_rsp
**
** Parameter cmd - request command
** p_req - request paramters
** p_rsp - response data for the request
**
** Returns TRUE - if the request is processed successfully and
** the response is returned in p_rsp.
** FASLE - if the request can not be processed
**
*******************************************************************************/
extern BOOLEAN bta_gatts_co_srv_chg(tBTA_GATTS_SRV_CHG_CMD cmd,
tBTA_GATTS_SRV_CHG_REQ *p_req,
tBTA_GATTS_SRV_CHG_RSP *p_rsp);
/*******************************************************************************
**
** Function bta_gatts_co_load_handle_range
**
** Description This callout function is executed by GATTS when a GATT server
** handle range is requested to be loaded from NV.
**
** Parameter
**
** Returns void.
**
*******************************************************************************/
extern BOOLEAN bta_gatts_co_load_handle_range(UINT8 index,
tBTA_GATTS_HNDL_RANGE *p_handle);
#endif /* BTA_GATTS_CO_H */

View file

@ -0,0 +1,260 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the private file for the BTA GATT server.
*
******************************************************************************/
#ifndef BTA_GATTS_INT_H
#define BTA_GATTS_INT_H
#include "bt_target.h"
#include "bta_sys.h"
#include "bta_gatt_api.h"
#include "gatt_api.h"
#include "gki.h"
/*****************************************************************************
** Constants and data types
*****************************************************************************/
enum
{
BTA_GATTS_API_REG_EVT = BTA_SYS_EVT_START(BTA_ID_GATTS),
BTA_GATTS_INT_START_IF_EVT,
BTA_GATTS_API_DEREG_EVT,
BTA_GATTS_API_CREATE_SRVC_EVT,
BTA_GATTS_API_INDICATION_EVT,
BTA_GATTS_API_ADD_INCL_SRVC_EVT,
BTA_GATTS_API_ADD_CHAR_EVT,
BTA_GATTS_API_ADD_DESCR_EVT,
BTA_GATTS_API_DEL_SRVC_EVT,
BTA_GATTS_API_START_SRVC_EVT,
BTA_GATTS_API_STOP_SRVC_EVT,
BTA_GATTS_API_RSP_EVT,
BTA_GATTS_API_OPEN_EVT,
BTA_GATTS_API_CANCEL_OPEN_EVT,
BTA_GATTS_API_CLOSE_EVT,
BTA_GATTS_API_LISTEN_EVT,
BTA_GATTS_API_DISABLE_EVT
};
typedef UINT16 tBTA_GATTS_INT_EVT;
/* max number of application allowed on device */
#define BTA_GATTS_MAX_APP_NUM GATT_MAX_SR_PROFILES
/* max number of services allowed in the device */
#define BTA_GATTS_MAX_SRVC_NUM GATT_MAX_SR_PROFILES
/* internal strucutre for GATTC register API */
typedef struct
{
BT_HDR hdr;
tBT_UUID app_uuid;
tBTA_GATTS_CBACK *p_cback;
}tBTA_GATTS_API_REG;
typedef struct
{
BT_HDR hdr;
tBTA_GATTS_IF server_if;
}tBTA_GATTS_INT_START_IF;
typedef tBTA_GATTS_INT_START_IF tBTA_GATTS_API_DEREG;
typedef struct
{
BT_HDR hdr;
tBTA_GATTS_IF server_if;
tBT_UUID service_uuid;
UINT16 num_handle;
UINT8 inst;
BOOLEAN is_pri;
} tBTA_GATTS_API_CREATE_SRVC;
typedef struct
{
BT_HDR hdr;
tBT_UUID char_uuid;
tBTA_GATT_PERM perm;
tBTA_GATT_CHAR_PROP property;
}tBTA_GATTS_API_ADD_CHAR;
typedef struct
{
BT_HDR hdr;
UINT16 included_service_id;
}tBTA_GATTS_API_ADD_INCL_SRVC;
typedef struct
{
BT_HDR hdr;
tBT_UUID descr_uuid;
tBTA_GATT_PERM perm;
}tBTA_GATTS_API_ADD_DESCR;
typedef struct
{
BT_HDR hdr;
UINT16 attr_id;
UINT16 len;
BOOLEAN need_confirm;
UINT8 value[BTA_GATT_MAX_ATTR_LEN];
}tBTA_GATTS_API_INDICATION;
typedef struct
{
BT_HDR hdr;
UINT32 trans_id;
tBTA_GATT_STATUS status;
tBTA_GATTS_RSP *p_rsp;
}tBTA_GATTS_API_RSP;
typedef struct
{
BT_HDR hdr;
tBTA_GATT_TRANSPORT transport;
}tBTA_GATTS_API_START;
typedef struct
{
BT_HDR hdr;
BD_ADDR remote_bda;
tBTA_GATTS_IF server_if;
BOOLEAN is_direct;
tBTA_GATT_TRANSPORT transport;
}tBTA_GATTS_API_OPEN;
typedef tBTA_GATTS_API_OPEN tBTA_GATTS_API_CANCEL_OPEN;
typedef struct
{
BT_HDR hdr;
BD_ADDR_PTR remote_bda;
tBTA_GATTS_IF server_if;
BOOLEAN start;
} tBTA_GATTS_API_LISTEN;
typedef union
{
BT_HDR hdr;
tBTA_GATTS_API_REG api_reg;
tBTA_GATTS_API_DEREG api_dereg;
tBTA_GATTS_API_CREATE_SRVC api_create_svc;
tBTA_GATTS_API_ADD_INCL_SRVC api_add_incl_srvc;
tBTA_GATTS_API_ADD_CHAR api_add_char;
tBTA_GATTS_API_ADD_DESCR api_add_char_descr;
tBTA_GATTS_API_START api_start;
tBTA_GATTS_API_INDICATION api_indicate;
tBTA_GATTS_API_RSP api_rsp;
tBTA_GATTS_API_OPEN api_open;
tBTA_GATTS_API_CANCEL_OPEN api_cancel_open;
tBTA_GATTS_INT_START_IF int_start_if;
/* if peripheral role is supported */
tBTA_GATTS_API_LISTEN api_listen;
} tBTA_GATTS_DATA;
/* application registration control block */
typedef struct
{
BOOLEAN in_use;
tBT_UUID app_uuid;
tBTA_GATTS_CBACK *p_cback;
tBTA_GATTS_IF gatt_if;
}tBTA_GATTS_RCB;
/* service registration control block */
typedef struct
{
tBT_UUID service_uuid; /* service UUID */
UINT16 service_id; /* service handle */
UINT8 inst_num; /* instance ID */
UINT8 rcb_idx;
UINT8 idx; /* self index of serviec CB */
BOOLEAN in_use;
}tBTA_GATTS_SRVC_CB;
/* GATT server control block */
typedef struct
{
BOOLEAN enabled;
tBTA_GATTS_RCB rcb[BTA_GATTS_MAX_APP_NUM];
tBTA_GATTS_SRVC_CB srvc_cb[BTA_GATTS_MAX_SRVC_NUM];
}tBTA_GATTS_CB;
/*****************************************************************************
** Global data
*****************************************************************************/
/* GATTC control block */
#if BTA_DYNAMIC_MEMORY == FALSE
extern tBTA_GATTS_CB bta_gatts_cb;
#else
extern tBTA_GATTS_CB *bta_gatts_cb_ptr;
#define bta_gatts_cb (*bta_gatts_cb_ptr)
#endif
/*****************************************************************************
** Function prototypes
*****************************************************************************/
extern BOOLEAN bta_gatts_hdl_event(BT_HDR *p_msg);
extern void bta_gatts_api_disable(tBTA_GATTS_CB *p_cb);
extern void bta_gatts_api_enable(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_data);
extern void bta_gatts_register(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
extern void bta_gatts_start_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
extern void bta_gatts_deregister(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
extern void bta_gatts_create_srvc(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_add_include_srvc(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_add_char(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_add_char_descr(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_delete_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_start_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_stop_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_send_rsp(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_indicate_handle (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_cancel_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_close (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg);
extern void bta_gatts_listen(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg);
extern BOOLEAN bta_gatts_uuid_compare(tBT_UUID tar, tBT_UUID src);
extern tBTA_GATTS_RCB *bta_gatts_find_app_rcb_by_app_if(tBTA_GATTS_IF server_if);
extern UINT8 bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_IF server_if);
extern UINT8 bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB *p_cb, UINT8 rcb_idx);
extern tBTA_GATTS_SRVC_CB * bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB *p_cb, UINT16 service_id);
extern tBTA_GATTS_SRVC_CB * bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB *p_cb, UINT16 attr_id);
#endif /* BTA_GATTS_INT_H */

View file

@ -0,0 +1,558 @@
/******************************************************************************
*
* Copyright (C) 2002-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef BTA_HH_API_H
#define BTA_HH_API_H
#include "bta_api.h"
#include "hidh_api.h"
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
#include "gatt_api.h"
#endif
/*****************************************************************************
** Constants and Type Definitions
*****************************************************************************/
#ifndef BTA_HH_DEBUG
#define BTA_HH_DEBUG TRUE
#endif
#ifndef BTA_HH_SSR_MAX_LATENCY_DEF
#define BTA_HH_SSR_MAX_LATENCY_DEF 800 /* 500 ms*/
#endif
#ifndef BTA_HH_SSR_MIN_TOUT_DEF
#define BTA_HH_SSR_MIN_TOUT_DEF 2
#endif
/* BTA HID Host callback events */
#define BTA_HH_ENABLE_EVT 0 /* HH enabled */
#define BTA_HH_DISABLE_EVT 1 /* HH disabled */
#define BTA_HH_OPEN_EVT 2 /* connection opened */
#define BTA_HH_CLOSE_EVT 3 /* connection closed */
#define BTA_HH_GET_RPT_EVT 4 /* BTA_HhGetReport callback */
#define BTA_HH_SET_RPT_EVT 5 /* BTA_HhSetReport callback */
#define BTA_HH_GET_PROTO_EVT 6 /* BTA_GetProtoMode callback */
#define BTA_HH_SET_PROTO_EVT 7 /* BTA_HhSetProtoMode callback */
#define BTA_HH_GET_IDLE_EVT 8 /* BTA_HhGetIdle comes callback */
#define BTA_HH_SET_IDLE_EVT 9 /* BTA_HhSetIdle finish callback */
#define BTA_HH_GET_DSCP_EVT 10 /* Get report descriptor */
#define BTA_HH_ADD_DEV_EVT 11 /* Add Device callback */
#define BTA_HH_RMV_DEV_EVT 12 /* remove device finished */
#define BTA_HH_VC_UNPLUG_EVT 13 /* virtually unplugged */
#define BTA_HH_DATA_EVT 15
#define BTA_HH_API_ERR_EVT 16 /* API error is caught */
#define BTA_HH_UPDATE_SCPP_EVT 17 /* update scan paramter complete */
typedef UINT16 tBTA_HH_EVT;
/* application ID(none-zero) for each type of device */
#define BTA_HH_APP_ID_MI 1
#define BTA_HH_APP_ID_KB 2
#define BTA_HH_APP_ID_RMC 3
#define BTA_HH_APP_ID_3DSG 4
#define BTA_HH_APP_ID_JOY 5
#define BTA_HH_APP_ID_GPAD 6
#define BTA_HH_APP_ID_LE 0xff
/* defined the minimum offset */
#define BTA_HH_MIN_OFFSET L2CAP_MIN_OFFSET+1
/* HID_HOST_MAX_DEVICES can not exceed 15 for th design of BTA HH */
#define BTA_HH_IDX_INVALID 0xff
#define BTA_HH_MAX_KNOWN HID_HOST_MAX_DEVICES
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
/* GATT_MAX_PHY_CHANNEL can not exceed 14 for the design of BTA HH */
#define BTA_HH_LE_MAX_KNOWN GATT_MAX_PHY_CHANNEL
#define BTA_HH_MAX_DEVICE (HID_HOST_MAX_DEVICES + GATT_MAX_PHY_CHANNEL)
#else
#define BTA_HH_MAX_DEVICE HID_HOST_MAX_DEVICES
#endif
/* invalid device handle */
#define BTA_HH_INVALID_HANDLE 0xff
/* type of protocol mode */
#define BTA_HH_PROTO_RPT_MODE (0x00)
#define BTA_HH_PROTO_BOOT_MODE (0x01)
#define BTA_HH_PROTO_UNKNOWN (0xff)
typedef UINT8 tBTA_HH_PROTO_MODE;
enum
{
BTA_HH_KEYBD_RPT_ID = 1,
BTA_HH_MOUSE_RPT_ID
};
typedef UINT8 tBTA_HH_BOOT_RPT_ID;
/* type of devices, bit mask */
#define BTA_HH_DEVT_UNKNOWN 0x00
#define BTA_HH_DEVT_JOS 0x01 /* joy stick */
#define BTA_HH_DEVT_GPD 0x02 /* game pad */
#define BTA_HH_DEVT_RMC 0x03 /* remote control */
#define BTA_HH_DEVT_SED 0x04 /* sensing device */
#define BTA_HH_DEVT_DGT 0x05 /* Digitizer tablet */
#define BTA_HH_DEVT_CDR 0x06 /* card reader */
#define BTA_HH_DEVT_KBD 0x10 /* keyboard */
#define BTA_HH_DEVT_MIC 0x20 /* pointing device */
#define BTA_HH_DEVT_COM 0x30 /* Combo keyboard/pointing */
#define BTA_HH_DEVT_OTHER 0x80
typedef UINT8 tBTA_HH_DEVT;
enum
{
BTA_HH_OK,
BTA_HH_HS_HID_NOT_READY, /* handshake error : device not ready */
BTA_HH_HS_INVALID_RPT_ID, /* handshake error : invalid report ID */
BTA_HH_HS_TRANS_NOT_SPT, /* handshake error : transaction not spt */
BTA_HH_HS_INVALID_PARAM, /* handshake error : invalid paremter */
BTA_HH_HS_ERROR, /* handshake error : unspecified HS error */
BTA_HH_ERR, /* general BTA HH error */
BTA_HH_ERR_SDP, /* SDP error */
BTA_HH_ERR_PROTO, /* SET_Protocol error,
only used in BTA_HH_OPEN_EVT callback */
BTA_HH_ERR_DB_FULL, /* device database full error, used in
BTA_HH_OPEN_EVT/BTA_HH_ADD_DEV_EVT */
BTA_HH_ERR_TOD_UNSPT, /* type of device not supported */
BTA_HH_ERR_NO_RES, /* out of system resources */
BTA_HH_ERR_AUTH_FAILED, /* authentication fail */
BTA_HH_ERR_HDL,
BTA_HH_ERR_SEC
};
typedef UINT8 tBTA_HH_STATUS;
#define BTA_HH_VIRTUAL_CABLE HID_VIRTUAL_CABLE
#define BTA_HH_NORMALLY_CONNECTABLE HID_NORMALLY_CONNECTABLE
#define BTA_HH_RECONN_INIT HID_RECONN_INIT
#define BTA_HH_SDP_DISABLE HID_SDP_DISABLE
#define BTA_HH_BATTERY_POWER HID_BATTERY_POWER
#define BTA_HH_REMOTE_WAKE HID_REMOTE_WAKE
#define BTA_HH_SUP_TOUT_AVLBL HID_SUP_TOUT_AVLBL
#define BTA_HH_SEC_REQUIRED HID_SEC_REQUIRED
typedef UINT16 tBTA_HH_ATTR_MASK;
/* supported type of device and corresponding application ID */
typedef struct
{
tBTA_HH_DEVT tod; /* type of device */
UINT8 app_id; /* corresponding application ID */
}tBTA_HH_SPT_TOD;
/* configuration struct */
typedef struct
{
UINT8 max_devt_spt; /* max number of types of devices spt */
tBTA_HH_SPT_TOD *p_devt_list; /* supported types of device list */
UINT16 sdp_db_size;
}tBTA_HH_CFG;
enum
{
BTA_HH_RPTT_RESRV, /* reserved */
BTA_HH_RPTT_INPUT, /* input report */
BTA_HH_RPTT_OUTPUT, /* output report */
BTA_HH_RPTT_FEATURE /* feature report */
};
typedef UINT8 tBTA_HH_RPT_TYPE;
/* HID_CONTROL operation code used in BTA_HhSendCtrl()
*/
enum
{
BTA_HH_CTRL_NOP = 0 + HID_PAR_CONTROL_NOP ,/* mapping from BTE */
BTA_HH_CTRL_HARD_RESET, /* hard reset */
BTA_HH_CTRL_SOFT_RESET, /* soft reset */
BTA_HH_CTRL_SUSPEND, /* enter suspend */
BTA_HH_CTRL_EXIT_SUSPEND, /* exit suspend */
BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG /* virtual unplug */
};
typedef UINT8 tBTA_HH_TRANS_CTRL_TYPE;
typedef tHID_DEV_DSCP_INFO tBTA_HH_DEV_DESCR;
#define BTA_HH_SSR_PARAM_INVALID HID_SSR_PARAM_INVALID
/* id DI is not existing in remote device, vendor_id in tBTA_HH_DEV_DSCP_INFO will be set to 0xffff */
#define BTA_HH_VENDOR_ID_INVALID 0xffff
/* report descriptor information */
typedef struct
{
UINT16 vendor_id; /* vendor ID */
UINT16 product_id; /* product ID */
UINT16 version; /* version */
UINT16 ssr_max_latency; /* SSR max latency, BTA_HH_SSR_PARAM_INVALID if unknown */
UINT16 ssr_min_tout; /* SSR min timeout, BTA_HH_SSR_PARAM_INVALID if unknown */
UINT8 ctry_code; /*Country Code.*/
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
#define BTA_HH_LE_REMOTE_WAKE 0x01
#define BTA_HH_LE_NORMAL_CONN 0x02
UINT8 flag;
#endif
tBTA_HH_DEV_DESCR descriptor;
}tBTA_HH_DEV_DSCP_INFO;
/* callback event data for BTA_HH_OPEN_EVT */
typedef struct
{
BD_ADDR bda; /* HID device bd address */
tBTA_HH_STATUS status; /* operation status */
UINT8 handle; /* device handle */
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
BOOLEAN le_hid; /* is LE devices? */
BOOLEAN scps_supported; /* scan parameter service supported */
#endif
} tBTA_HH_CONN;
typedef tBTA_HH_CONN tBTA_HH_DEV_INFO;
/* callback event data */
typedef struct
{
tBTA_HH_STATUS status; /* operation status */
UINT8 handle; /* device handle */
} tBTA_HH_CBDATA;
enum
{
BTA_HH_MOD_CTRL_KEY,
BTA_HH_MOD_SHFT_KEY,
BTA_HH_MOD_ALT_KEY,
BTA_HH_MOD_GUI_KEY,
BTA_HH_MOD_MAX_KEY
};
/* parsed boot mode keyboard report */
typedef struct
{
UINT8 this_char[6]; /* virtual key code */
BOOLEAN mod_key[BTA_HH_MOD_MAX_KEY];
/* ctrl, shift, Alt, GUI */
/* modifier key: is Shift key pressed */
/* modifier key: is Ctrl key pressed */
/* modifier key: is Alt key pressed */
/* modifier key: GUI up/down */
BOOLEAN caps_lock; /* is caps locked */
BOOLEAN num_lock; /* is Num key pressed */
} tBTA_HH_KEYBD_RPT;
/* parsed boot mode mouse report */
typedef struct
{
UINT8 mouse_button; /* mouse button is clicked */
INT8 delta_x; /* displacement x */
INT8 delta_y; /* displacement y */
}tBTA_HH_MICE_RPT;
/* parsed Boot report */
typedef struct
{
tBTA_HH_BOOT_RPT_ID dev_type; /* type of device report */
union
{
tBTA_HH_KEYBD_RPT keybd_rpt; /* keyboard report */
tBTA_HH_MICE_RPT mice_rpt; /* mouse report */
} data_rpt;
} tBTA_HH_BOOT_RPT;
/* handshake data */
typedef struct
{
tBTA_HH_STATUS status; /* handshake status */
UINT8 handle; /* device handle */
union
{
tBTA_HH_PROTO_MODE proto_mode; /* GET_PROTO_EVT :protocol mode */
BT_HDR *p_rpt_data; /* GET_RPT_EVT : report data */
UINT8 idle_rate; /* GET_IDLE_EVT : idle rate */
} rsp_data;
}tBTA_HH_HSDATA;
/* union of data associated with HD callback */
typedef union
{
tBTA_HH_DEV_INFO dev_info; /* BTA_HH_ADD_DEV_EVT, BTA_HH_RMV_DEV_EVT */
tBTA_HH_CONN conn; /* BTA_HH_OPEN_EVT */
tBTA_HH_CBDATA dev_status; /* BTA_HH_CLOSE_EVT,
BTA_HH_SET_PROTO_EVT
BTA_HH_SET_RPT_EVT
BTA_HH_SET_IDLE_EVT
BTA_HH_UPDATE_SCPP_EVT */
tBTA_HH_STATUS status; /* BTA_HH_ENABLE_EVT */
tBTA_HH_DEV_DSCP_INFO dscp_info; /* BTA_HH_GET_DSCP_EVT */
tBTA_HH_HSDATA hs_data; /* GET_ transaction callback
BTA_HH_GET_RPT_EVT
BTA_HH_GET_PROTO_EVT
BTA_HH_GET_IDLE_EVT */
} tBTA_HH;
/* BTA HH callback function */
typedef void (tBTA_HH_CBACK) (tBTA_HH_EVT event, tBTA_HH *p_data);
/*****************************************************************************
** External Function Declarations
*****************************************************************************/
#ifdef __cplusplus
extern "C"
{
#endif
/*******************************************************************************
**
** Function BTA_HhRegister
**
** Description This function enable HID host and registers HID-Host with
** lower layers.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhEnable(tBTA_SEC sec_mask, tBTA_HH_CBACK *p_cback);
/*******************************************************************************
**
** Function BTA_HhDeregister
**
** Description This function is called when the host is about power down.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhDisable(void);
/*******************************************************************************
**
** Function BTA_HhOpen
**
** Description This function is called to start an inquiry and read SDP
** record of responding devices; connect to a device if only
** one active HID device is found.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhOpen (BD_ADDR dev_bda, tBTA_HH_PROTO_MODE mode,
tBTA_SEC sec_mask);
/*******************************************************************************
**
** Function BTA_HhClose
**
** Description This function disconnects the device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhClose(UINT8 dev_handle);
/*******************************************************************************
**
** Function BTA_HhSetProtoMode
**
** Description This function set the protocol mode at specified HID handle
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhSetProtoMode(UINT8 handle, tBTA_HH_PROTO_MODE t_type);
/*******************************************************************************
**
** Function BTA_HhGetProtoMode
**
** Description This function get the protocol mode of a specified HID device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhGetProtoMode(UINT8 dev_handle);
/*******************************************************************************
**
** Function BTA_HhSetReport
**
** Description send SET_REPORT to device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhSetReport(UINT8 dev_handle, tBTA_HH_RPT_TYPE r_type,
BT_HDR *p_data);
/*******************************************************************************
**
** Function BTA_HhGetReport
**
** Description Send a GET_REPORT to HID device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhGetReport(UINT8 dev_handle, tBTA_HH_RPT_TYPE r_type,
UINT8 rpt_id, UINT16 buf_size);
/*******************************************************************************
**
** Function BTA_HhSetIdle
**
** Description send SET_IDLE to device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhSetIdle(UINT8 dev_handle, UINT16 idle_rate);
/*******************************************************************************
**
** Function BTA_HhGetIdle
**
** Description Send a GET_IDLE to HID device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhGetIdle(UINT8 dev_handle);
/*******************************************************************************
**
** Function BTA_HhSendCtrl
**
** Description Send HID_CONTROL request to a HID device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhSendCtrl(UINT8 dev_handle,
tBTA_HH_TRANS_CTRL_TYPE c_type);
/*******************************************************************************
**
** Function BTA_HhSetIdle
**
** Description send SET_IDLE to device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhSetIdle(UINT8 dev_handle, UINT16 idle_rate);
/*******************************************************************************
**
** Function BTA_HhGetIdle
**
** Description Send a GET_IDLE from HID device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhGetIdle(UINT8 dev_handle);
/*******************************************************************************
**
** Function BTA_HhSendData
**
** Description Send DATA transaction to a HID device.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhSendData(UINT8 dev_handle, BD_ADDR dev_bda, BT_HDR *p_buf);
/*******************************************************************************
**
** Function BTA_HhGetDscpInfo
**
** Description Get report descriptor of the device
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhGetDscpInfo(UINT8 dev_handle);
/*******************************************************************************
** Function BTA_HhAddDev
**
** Description Add a virtually cabled device into HID-Host device list
** to manage and assign a device handle for future API call,
** host applciation call this API at start-up to initialize its
** virtually cabled devices.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhAddDev(BD_ADDR bda, tBTA_HH_ATTR_MASK attr_mask,
UINT8 sub_class, UINT8 app_id,
tBTA_HH_DEV_DSCP_INFO dscp_info);
/*******************************************************************************
**
** Function BTA_HhRemoveDev
**
** Description Remove a device from the HID host devices list.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhRemoveDev(UINT8 dev_handle );
/*******************************************************************************
**
** Parsing Utility Functions
**
*******************************************************************************/
/*******************************************************************************
**
** Function BTA_HhParseBootRpt
**
** Description This utility function parse a boot mode report.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhParseBootRpt(tBTA_HH_BOOT_RPT *p_data, UINT8 *p_report,
UINT16 report_len);
#if BTA_HH_LE_INCLUDED == TRUE
/*******************************************************************************
**
** Function BTA_HhUpdateLeScanParam
**
** Description Update the scan paramteters if connected to a LE hid device as
** report host.
**
** Returns void
**
*******************************************************************************/
extern void BTA_HhUpdateLeScanParam(UINT8 dev_handle, UINT16 scan_int, UINT16 scan_win);
#endif
/* test commands */
extern void bta_hh_le_hid_read_rpt_clt_cfg(BD_ADDR bd_addr, UINT8 rpt_id);
#ifdef __cplusplus
}
#endif
#endif /* BTA_HH_API_H */

View file

@ -0,0 +1,133 @@
/******************************************************************************
*
* Copyright (C) 2005-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the interface file for hid host call-out functions.
*
******************************************************************************/
#ifndef BTA_HH_CO_H
#define BTA_HH_CO_H
#include "bta_hh_api.h"
typedef struct
{
UINT16 rpt_uuid;
UINT8 rpt_id;
tBTA_HH_RPT_TYPE rpt_type;
UINT8 inst_id;
UINT8 prop;
}tBTA_HH_RPT_CACHE_ENTRY;
/*******************************************************************************
**
** Function bta_hh_co_data
**
** Description This callout function is executed by HH when data is received
** in interupt channel.
**
**
** Returns void.
**
*******************************************************************************/
extern void bta_hh_co_data(UINT8 dev_handle, UINT8 *p_rpt, UINT16 len,
tBTA_HH_PROTO_MODE mode, UINT8 sub_class,
UINT8 ctry_code, BD_ADDR peer_addr, UINT8 app_id);
/*******************************************************************************
**
** Function bta_hh_co_open
**
** Description This callout function is executed by HH when connection is
** opened, and application may do some device specific
** initialization.
**
** Returns void.
**
*******************************************************************************/
extern void bta_hh_co_open(UINT8 dev_handle, UINT8 sub_class,
UINT16 attr_mask, UINT8 app_id);
/*******************************************************************************
**
** Function bta_hh_co_close
**
** Description This callout function is executed by HH when connection is
** closed, and device specific finalizatio nmay be needed.
**
** Returns void.
**
*******************************************************************************/
extern void bta_hh_co_close(UINT8 dev_handle, UINT8 app_id);
#if (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE)
/*******************************************************************************
**
** Function bta_hh_le_co_rpt_info
**
** Description This callout function is to convey the report information on
** a HOGP device to the application. Application can save this
** information in NV if device is bonded and load it back when
** stack reboot.
**
** Parameters remote_bda - remote device address
** p_entry - report entry pointer
** app_id - application id
**
** Returns void.
**
*******************************************************************************/
extern void bta_hh_le_co_rpt_info(BD_ADDR remote_bda,
tBTA_HH_RPT_CACHE_ENTRY *p_entry,
UINT8 app_id);
/*******************************************************************************
**
** Function bta_hh_le_co_cache_load
**
** Description This callout function is to request the application to load the
** cached HOGP report if there is any. When cache reading is completed,
** bta_hh_le_ci_cache_load() is called by the application.
**
** Parameters remote_bda - remote device address
** p_num_rpt: number of cached report
** app_id - application id
**
** Returns the acched report array
**
*******************************************************************************/
extern tBTA_HH_RPT_CACHE_ENTRY *bta_hh_le_co_cache_load (BD_ADDR remote_bda,
UINT8 *p_num_rpt,
UINT8 app_id);
/*******************************************************************************
**
** Function bta_hh_le_co_reset_rpt_cache
**
** Description This callout function is to reset the HOGP device cache.
**
** Parameters remote_bda - remote device address
**
** Returns none
**
*******************************************************************************/
extern void bta_hh_le_co_reset_rpt_cache (BD_ADDR remote_bda, UINT8 app_id);
#endif /* #if (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE) */
#endif /* BTA_HH_CO_H */

View file

@ -0,0 +1,287 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the public interface file for the BTA system manager.
*
******************************************************************************/
#ifndef BTA_SYS_H
#define BTA_SYS_H
#include "bt_target.h"
#include "gki.h"
/*****************************************************************************
** Constants and data types
*****************************************************************************/
/* vendor specific event handler function type */
typedef BOOLEAN (tBTA_SYS_VS_EVT_HDLR)(UINT16 evt, void *p);
/* event handler function type */
typedef BOOLEAN (tBTA_SYS_EVT_HDLR)(BT_HDR *p_msg);
/* disable function type */
typedef void (tBTA_SYS_DISABLE)(void);
/* HW modules */
enum
{
BTA_SYS_HW_BLUETOOTH,
BTA_SYS_HW_RT,
BTA_SYS_MAX_HW_MODULES
};
typedef UINT16 tBTA_SYS_HW_MODULE;
#ifndef BTA_DM_NUM_JV_ID
#define BTA_DM_NUM_JV_ID 2
#endif
/* SW sub-systems */
#define BTA_ID_SYS 0 /* system manager */
/* BLUETOOTH PART - from 0 to BTA_ID_BLUETOOTH_MAX */
#define BTA_ID_DM 1 /* device manager */
#define BTA_ID_DM_SEARCH 2 /* device manager search */
#define BTA_ID_DM_SEC 3 /* device manager security */
#define BTA_ID_DG 4 /* data gateway */
#define BTA_ID_AG 5 /* audio gateway */
#define BTA_ID_OPC 6 /* object push client */
#define BTA_ID_OPS 7 /* object push server */
#define BTA_ID_FTS 8 /* file transfer server */
#define BTA_ID_CT 9 /* cordless telephony terminal */
#define BTA_ID_FTC 10 /* file transfer client */
#define BTA_ID_SS 11 /* synchronization server */
#define BTA_ID_PR 12 /* Printer client */
#define BTA_ID_BIC 13 /* Basic Imaging Client */
#define BTA_ID_PAN 14 /* Personal Area Networking */
#define BTA_ID_BIS 15 /* Basic Imaging Server */
#define BTA_ID_ACC 16 /* Advanced Camera Client */
#define BTA_ID_SC 17 /* SIM Card Access server */
#define BTA_ID_AV 18 /* Advanced audio/video */
#define BTA_ID_AVK 19 /* Audio/video sink */
#define BTA_ID_HD 20 /* HID Device */
#define BTA_ID_CG 21 /* Cordless Gateway */
#define BTA_ID_BP 22 /* Basic Printing Client */
#define BTA_ID_HH 23 /* Human Interface Device Host */
#define BTA_ID_PBS 24 /* Phone Book Access Server */
#define BTA_ID_PBC 25 /* Phone Book Access Client */
#define BTA_ID_JV 26 /* Java */
#define BTA_ID_HS 27 /* Headset */
#define BTA_ID_MSE 28 /* Message Server Equipment */
#define BTA_ID_MCE 29 /* Message Client Equipment */
#define BTA_ID_HL 30 /* Health Device Profile*/
#define BTA_ID_GATTC 31 /* GATT Client */
#define BTA_ID_GATTS 32 /* GATT Client */
#define BTA_ID_SDP 33 /* SDP Client */
#define BTA_ID_BLUETOOTH_MAX 34 /* last BT profile */
/* GENERIC */
#define BTA_ID_PRM 38
#define BTA_ID_SYSTEM 39 /* platform-specific */
#define BTA_ID_SWRAP 40 /* Insight script wrapper */
#define BTA_ID_MIP 41 /* Multicase Individual Polling */
#define BTA_ID_RT 42 /* Audio Routing module: This module is always on. */
/* JV */
#define BTA_ID_JV1 44 /* JV1 */
#define BTA_ID_JV2 45 /* JV2 */
#define BTA_ID_MAX (44 + BTA_DM_NUM_JV_ID)
typedef UINT8 tBTA_SYS_ID;
#define BTA_SYS_CONN_OPEN 0x00
#define BTA_SYS_CONN_CLOSE 0x01
#define BTA_SYS_APP_OPEN 0x02
#define BTA_SYS_APP_CLOSE 0x03
#define BTA_SYS_SCO_OPEN 0x04
#define BTA_SYS_SCO_CLOSE 0x05
#define BTA_SYS_CONN_IDLE 0x06
#define BTA_SYS_CONN_BUSY 0x07
/* for link policy */
#define BTA_SYS_PLCY_SET 0x10 /* set the link policy to the given addr */
#define BTA_SYS_PLCY_CLR 0x11 /* clear the link policy to the given addr */
#define BTA_SYS_PLCY_DEF_SET 0x12 /* set the default link policy */
#define BTA_SYS_PLCY_DEF_CLR 0x13 /* clear the default link policy */
#define BTA_SYS_ROLE_CHANGE 0x14 /* role change */
typedef UINT8 tBTA_SYS_CONN_STATUS;
/* Bitmask of sys features */
#define BTA_SYS_FEAT_PCM2 0x0001
#define BTA_SYS_FEAT_PCM2_MASTER 0x0002
/* tBTA_PREF_ROLES */
typedef UINT8 tBTA_SYS_PREF_ROLES;
/* conn callback for role / low power manager*/
typedef void (tBTA_SYS_CONN_CBACK)(tBTA_SYS_CONN_STATUS status,UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
/* conn callback for role / low power manager*/
typedef void (tBTA_SYS_SSR_CFG_CBACK)(UINT8 id, UINT8 app_id, UINT16 latency, UINT16 tout);
#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
/* eir callback for adding/removeing UUID */
typedef void (tBTA_SYS_EIR_CBACK)(UINT16 uuid16, BOOLEAN adding);
#endif
/* registration structure */
typedef struct
{
tBTA_SYS_EVT_HDLR *evt_hdlr;
tBTA_SYS_DISABLE *disable;
} tBTA_SYS_REG;
/* data type to send events to BTA SYS HW manager */
typedef struct
{
BT_HDR hdr;
tBTA_SYS_HW_MODULE hw_module;
} tBTA_SYS_HW_MSG;
/*****************************************************************************
** Global data
*****************************************************************************/
/* trace level */
extern UINT8 appl_trace_level;
/*****************************************************************************
** Macros
*****************************************************************************/
/* Calculate start of event enumeration; id is top 8 bits of event */
#define BTA_SYS_EVT_START(id) ((id) << 8)
/*****************************************************************************
** events for BTA SYS HW manager
*****************************************************************************/
/* events sent to SYS HW manager - must be kept synchronized with tables in bta_sys_main.c */
enum
{
/* device manager local device API events */
BTA_SYS_API_ENABLE_EVT = BTA_SYS_EVT_START(BTA_ID_SYS),
BTA_SYS_EVT_ENABLED_EVT,
BTA_SYS_EVT_STACK_ENABLED_EVT,
BTA_SYS_API_DISABLE_EVT,
BTA_SYS_EVT_DISABLED_EVT,
BTA_SYS_ERROR_EVT,
BTA_SYS_MAX_EVT
};
/* SYS HW status events - returned by SYS HW manager to other modules. */
enum
{
BTA_SYS_HW_OFF_EVT,
BTA_SYS_HW_ON_EVT,
BTA_SYS_HW_STARTING_EVT,
BTA_SYS_HW_STOPPING_EVT,
BTA_SYS_HW_ERROR_EVT
};
typedef UINT8 tBTA_SYS_HW_EVT;
/* HW enable callback type */
typedef void (tBTA_SYS_HW_CBACK)(tBTA_SYS_HW_EVT status);
/*****************************************************************************
** Function declarations
*****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
extern void bta_sys_init(void);
extern void bta_sys_free(void);
extern void bta_sys_event(BT_HDR *p_msg);
extern void bta_sys_set_trace_level(UINT8 level);
extern void bta_sys_register(UINT8 id, const tBTA_SYS_REG *p_reg);
extern void bta_sys_deregister(UINT8 id);
extern BOOLEAN bta_sys_is_register(UINT8 id);
extern UINT16 bta_sys_get_sys_features(void);
extern void bta_sys_sendmsg(void *p_msg);
extern void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout_ms);
extern void bta_sys_stop_timer(TIMER_LIST_ENT *p_tle);
extern void bta_sys_disable(tBTA_SYS_HW_MODULE module);
extern UINT32 bta_sys_get_remaining_ticks(TIMER_LIST_ENT *p_target_tle);
extern void bta_sys_hw_register( tBTA_SYS_HW_MODULE module, tBTA_SYS_HW_CBACK *cback);
extern void bta_sys_hw_unregister( tBTA_SYS_HW_MODULE module );
extern void bta_sys_rm_register(tBTA_SYS_CONN_CBACK * p_cback);
extern void bta_sys_pm_register(tBTA_SYS_CONN_CBACK * p_cback);
extern void bta_sys_policy_register(tBTA_SYS_CONN_CBACK * p_cback);
extern void bta_sys_sco_register(tBTA_SYS_CONN_CBACK * p_cback);
extern void bta_sys_conn_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
extern void bta_sys_conn_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
extern void bta_sys_app_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
extern void bta_sys_app_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
extern void bta_sys_sco_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
extern void bta_sys_sco_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
extern void bta_sys_sco_use(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
extern void bta_sys_sco_unuse(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
extern void bta_sys_idle(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
extern void bta_sys_busy(UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
#if (BTM_SSR_INCLUDED == TRUE)
extern void bta_sys_ssr_cfg_register(tBTA_SYS_SSR_CFG_CBACK * p_cback);
extern void bta_sys_chg_ssr_config (UINT8 id, UINT8 app_id, UINT16 max_latency, UINT16 min_tout);
#endif
extern void bta_sys_role_chg_register(tBTA_SYS_CONN_CBACK * p_cback);
extern void bta_sys_notify_role_chg(BD_ADDR_PTR p_bda, UINT8 new_role, UINT8 hci_status);
extern void bta_sys_collision_register(UINT8 bta_id, tBTA_SYS_CONN_CBACK *p_cback);
extern void bta_sys_notify_collision (BD_ADDR_PTR p_bda);
#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
extern void bta_sys_eir_register(tBTA_SYS_EIR_CBACK * p_cback);
extern void bta_sys_add_uuid(UINT16 uuid16);
extern void bta_sys_remove_uuid(UINT16 uuid16);
#else
#define bta_sys_eir_register(ut)
#define bta_sys_add_uuid(ut)
#define bta_sys_remove_uuid(ut)
#endif
extern void bta_sys_set_policy (UINT8 id, UINT8 policy, BD_ADDR peer_addr);
extern void bta_sys_clear_policy (UINT8 id, UINT8 policy, BD_ADDR peer_addr);
extern void bta_sys_set_default_policy (UINT8 id, UINT8 policy);
extern void bta_sys_clear_default_policy (UINT8 id, UINT8 policy);
extern BOOLEAN bta_sys_vs_hdl(UINT16 evt, void *p);
#ifdef __cplusplus
}
#endif
#endif /* BTA_SYS_H */

View file

@ -0,0 +1,170 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* Basic utility functions.
*
******************************************************************************/
#ifndef UTL_H
#define UTL_H
#include "bt_types.h"
// #include "bt_utils.h"
/*****************************************************************************
** Constants
*****************************************************************************/
/*** class of device settings ***/
#define BTA_UTL_SET_COD_MAJOR_MINOR 0x01
#define BTA_UTL_SET_COD_SERVICE_CLASS 0x02 /* only set the bits in the input */
#define BTA_UTL_CLR_COD_SERVICE_CLASS 0x04
#define BTA_UTL_SET_COD_ALL 0x08 /* take service class as the input (may clear some set bits!!) */
#define BTA_UTL_INIT_COD 0x0a
/*****************************************************************************
** Type Definitions
*****************************************************************************/
/** for utl_set_device_class() **/
typedef struct
{
UINT8 minor;
UINT8 major;
UINT16 service;
} tBTA_UTL_COD;
#ifdef __cplusplus
extern "C"
{
#endif
/*****************************************************************************
** External Function Declarations
*****************************************************************************/
/*******************************************************************************
**
** Function utl_str2int
**
** Description This utility function converts a character string to an
** integer. Acceptable values in string are 0-9. If invalid
** string or string value too large, -1 is returned.
**
**
** Returns Integer value or -1 on error.
**
*******************************************************************************/
extern INT16 utl_str2int(const char *p_s);
/*******************************************************************************
**
** Function utl_strucmp
**
** Description This utility function compares two strings in uppercase.
** String p_s must be uppercase. String p_t is converted to
** uppercase if lowercase. If p_s ends first, the substring
** match is counted as a match.
**
**
** Returns 0 if strings match, nonzero otherwise.
**
*******************************************************************************/
extern int utl_strucmp(const char *p_s, const char *p_t);
/*******************************************************************************
**
** Function utl_itoa
**
** Description This utility function converts a UINT16 to a string. The
** string is NULL-terminated. The length of the string is
** returned.
**
**
** Returns Length of string.
**
*******************************************************************************/
extern UINT8 utl_itoa(UINT16 i, char *p_s);
/*******************************************************************************
**
** Function utl_freebuf
**
** Description This function calls GKI_freebuf to free the buffer passed
** in, if buffer pointer is not NULL, and also initializes
** buffer pointer to NULL.
**
**
** Returns Nothing.
**
*******************************************************************************/
extern void utl_freebuf(void **p);
/*******************************************************************************
**
** Function utl_set_device_class
**
** Description This function updates the local Device Class.
**
** Parameters:
** p_cod - Pointer to the device class to set to
**
** cmd - the fields of the device class to update.
** BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major, minor class
** BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in the input
** BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in the input
** BTA_UTL_SET_COD_ALL - overwrite major, minor, set the bits in service class
** BTA_UTL_INIT_COD - overwrite major, minor, and service class
**
** Returns TRUE if successful, Otherwise FALSE
**
*******************************************************************************/
extern BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd);
/*******************************************************************************
**
** Function utl_isintstr
**
** Description This utility function checks if the given string is an
** integer string or not
**
**
** Returns TRUE if successful, Otherwise FALSE
**
*******************************************************************************/
extern BOOLEAN utl_isintstr(const char *p_s);
/*******************************************************************************
**
** Function utl_isdialstr
**
** Description This utility function checks if the given string contains
** only dial digits or not
**
**
** Returns TRUE if successful, Otherwise FALSE
**
*******************************************************************************/
extern BOOLEAN utl_isdialstr(const char *p_s);
#ifdef __cplusplus
}
#endif
#endif /* UTL_H */

View file

@ -0,0 +1,598 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* Routes connection status callbacks from various sub systems to DM
*
******************************************************************************/
#include <stddef.h>
#include "bta_api.h"
#include "bta_sys.h"
#include "bta_sys_int.h"
#include "gki.h"
#include "utl.h"
/*******************************************************************************
**
** Function bta_sys_rm_register
**
** Description Called by BTA DM to register role management callbacks
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_rm_register(tBTA_SYS_CONN_CBACK * p_cback)
{
bta_sys_cb.prm_cb = p_cback;
}
/*******************************************************************************
**
** Function bta_sys_policy_register
**
** Description Called by BTA DM to register link policy change callbacks
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_policy_register(tBTA_SYS_CONN_CBACK * p_cback)
{
bta_sys_cb.p_policy_cb = p_cback;
}
/*******************************************************************************
**
** Function bta_sys_role_chg_register
**
** Description Called by BTA AV to register role change callbacks
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_role_chg_register(tBTA_SYS_CONN_CBACK * p_cback)
{
bta_sys_cb.p_role_cb = p_cback;
}
/*******************************************************************************
**
** Function bta_sys_ssr_cfg_register
**
** Description Called by BTA DM to register SSR configuration callback
**
**
** Returns void
**
*******************************************************************************/
#if (BTM_SSR_INCLUDED == TRUE)
void bta_sys_ssr_cfg_register(tBTA_SYS_SSR_CFG_CBACK * p_cback)
{
bta_sys_cb.p_ssr_cb = p_cback;
}
#endif
/*******************************************************************************
**
** Function bta_sys_role_chg_register
**
** Description Called by BTA AV to register role change callbacks
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_notify_role_chg(BD_ADDR_PTR p_bda, UINT8 new_role, UINT8 hci_status)
{
if (bta_sys_cb.p_role_cb)
{
bta_sys_cb.p_role_cb(BTA_SYS_ROLE_CHANGE, new_role, hci_status, p_bda);
}
}
/*******************************************************************************
**
** Function bta_sys_collision_register
**
** Description Called by any BTA module to register for collision event.
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_collision_register(UINT8 bta_id, tBTA_SYS_CONN_CBACK *p_cback)
{
UINT8 index;
for (index = 0; index < MAX_COLLISION_REG; index++)
{
if ((bta_sys_cb.colli_reg.id[index] == bta_id) ||
(bta_sys_cb.colli_reg.id[index] == 0))
{
bta_sys_cb.colli_reg.id[index] = bta_id;
bta_sys_cb.colli_reg.p_coll_cback[index] = p_cback;
return;
}
}
}
/*******************************************************************************
**
** Function bta_sys_notify_collision
**
** Description Called by BTA DM to notify collision event.
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_notify_collision (BD_ADDR_PTR p_bda)
{
UINT8 index;
for (index = 0; index < MAX_COLLISION_REG; index++)
{
if ((bta_sys_cb.colli_reg.id[index] != 0) &&
(bta_sys_cb.colli_reg.p_coll_cback[index] != NULL))
{
bta_sys_cb.colli_reg.p_coll_cback[index] (0, BTA_ID_SYS, 0, p_bda);
}
}
}
/*******************************************************************************
**
** Function bta_sys_sco_register
**
** Description Called by BTA AV to register sco connection change callbacks
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_sco_register(tBTA_SYS_CONN_CBACK * p_cback)
{
bta_sys_cb.p_sco_cb = p_cback;
}
/*******************************************************************************
**
** Function bta_sys_pm_register
**
** Description Called by BTA DM to register power management callbacks
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_pm_register(tBTA_SYS_CONN_CBACK * p_cback)
{
bta_sys_cb.ppm_cb = p_cback;
}
/*******************************************************************************
**
** Function bta_sys_conn_open
**
** Description Called by BTA subsystems when a connection is made to
** the service
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_conn_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
if(bta_sys_cb.prm_cb)
{
bta_sys_cb.prm_cb(BTA_SYS_CONN_OPEN, id, app_id, peer_addr);
}
if(bta_sys_cb.ppm_cb)
{
bta_sys_cb.ppm_cb(BTA_SYS_CONN_OPEN, id, app_id, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_conn_close
**
** Description Called by BTA subsystems when a connection to the service
** is closed
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_conn_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
if(bta_sys_cb.prm_cb)
{
bta_sys_cb.prm_cb(BTA_SYS_CONN_CLOSE, id, app_id, peer_addr);
}
if(bta_sys_cb.ppm_cb)
{
bta_sys_cb.ppm_cb(BTA_SYS_CONN_CLOSE, id, app_id, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_app_open
**
** Description Called by BTA subsystems when application initiates connection
** to a peer device
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_app_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
if(bta_sys_cb.ppm_cb)
{
bta_sys_cb.ppm_cb(BTA_SYS_APP_OPEN, id, app_id, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_app_close
**
** Description Called by BTA subsystems when application initiates close
** of connection to peer device
**
** Returns void
**
*******************************************************************************/
void bta_sys_app_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
if(bta_sys_cb.ppm_cb)
{
bta_sys_cb.ppm_cb(BTA_SYS_APP_CLOSE, id, app_id, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_sco_open
**
** Description Called by BTA subsystems when sco connection for that service
** is open
**
** Returns void
**
*******************************************************************************/
void bta_sys_sco_open(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
/* AG triggers p_sco_cb by bta_sys_sco_use. */
if((id != BTA_ID_AG) && (bta_sys_cb.p_sco_cb))
{
/* without querying BTM_GetNumScoLinks() */
bta_sys_cb.p_sco_cb(BTA_SYS_SCO_OPEN, 1, app_id, peer_addr);
}
if(bta_sys_cb.ppm_cb)
{
bta_sys_cb.ppm_cb(BTA_SYS_SCO_OPEN, id, app_id, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_sco_close
**
** Description Called by BTA subsystems when sco connection for that service
** is closed
**
** Returns void
**
*******************************************************************************/
void bta_sys_sco_close(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
UINT8 num_sco_links;
if((id != BTA_ID_AG) && (bta_sys_cb.p_sco_cb))
{
num_sco_links = BTM_GetNumScoLinks();
bta_sys_cb.p_sco_cb(BTA_SYS_SCO_CLOSE, num_sco_links, app_id, peer_addr);
}
if(bta_sys_cb.ppm_cb)
{
bta_sys_cb.ppm_cb(BTA_SYS_SCO_CLOSE, id, app_id, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_sco_use
**
** Description Called by BTA subsystems when that service needs to use sco.
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_sco_use(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
UNUSED(id);
/* AV streaming need to be suspended before SCO is connected. */
if(bta_sys_cb.p_sco_cb)
{
/* without querying BTM_GetNumScoLinks() */
bta_sys_cb.p_sco_cb(BTA_SYS_SCO_OPEN, 1, app_id, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_sco_unuse
**
** Description Called by BTA subsystems when sco connection for that service
** is no longer needed.
**
** Returns void
**
*******************************************************************************/
void bta_sys_sco_unuse(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
UINT8 num_sco_links;
UNUSED(id);
if((bta_sys_cb.p_sco_cb))
{
num_sco_links = BTM_GetNumScoLinks();
bta_sys_cb.p_sco_cb(BTA_SYS_SCO_CLOSE, num_sco_links, app_id, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_chg_ssr_config
**
** Description Called by BTA subsystems to indicate that the given app SSR setting
** need to be changed.
**
** Returns void
**
*******************************************************************************/
#if (BTM_SSR_INCLUDED == TRUE)
void bta_sys_chg_ssr_config (UINT8 id, UINT8 app_id, UINT16 max_latency, UINT16 min_tout)
{
if(bta_sys_cb.p_ssr_cb)
{
bta_sys_cb.p_ssr_cb(id, app_id, max_latency, min_tout);
}
}
#endif
/*******************************************************************************
**
** Function bta_sys_set_policy
**
** Description Called by BTA subsystems to indicate that the given link
** policy to peer device should be set
**
** Returns void
**
*******************************************************************************/
void bta_sys_set_policy (UINT8 id, UINT8 policy, BD_ADDR peer_addr)
{
if(bta_sys_cb.p_policy_cb)
{
bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_SET, id, policy, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_clear_policy
**
** Description Called by BTA subsystems to indicate that the given link
** policy to peer device should be clear
**
** Returns void
**
*******************************************************************************/
void bta_sys_clear_policy (UINT8 id, UINT8 policy, BD_ADDR peer_addr)
{
if(bta_sys_cb.p_policy_cb)
{
bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_CLR, id, policy, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_set_default_policy
**
** Description Called by BTA subsystems to indicate that the given default
** link policy should be set
**
** Returns void
**
*******************************************************************************/
void bta_sys_set_default_policy (UINT8 id, UINT8 policy)
{
if(bta_sys_cb.p_policy_cb)
{
bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_DEF_SET, id, policy, NULL);
}
}
/*******************************************************************************
**
** Function bta_sys_clear_default_policy
**
** Description Called by BTA subsystems to indicate that the given default
** link policy should be clear
**
** Returns void
**
*******************************************************************************/
void bta_sys_clear_default_policy (UINT8 id, UINT8 policy)
{
if(bta_sys_cb.p_policy_cb)
{
bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_DEF_CLR, id, policy, NULL);
}
}
/*******************************************************************************
**
** Function bta_sys_idle
**
** Description Called by BTA subsystems to indicate that the connection to
** peer device is idle
**
** Returns void
**
*******************************************************************************/
void bta_sys_idle(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
if(bta_sys_cb.prm_cb)
{
bta_sys_cb.prm_cb(BTA_SYS_CONN_IDLE, id, app_id, peer_addr);
}
if(bta_sys_cb.ppm_cb)
{
bta_sys_cb.ppm_cb(BTA_SYS_CONN_IDLE, id, app_id, peer_addr);
}
}
/*******************************************************************************
**
** Function bta_sys_busy
**
** Description Called by BTA subsystems to indicate that the connection to
** peer device is busy
**
** Returns void
**
*******************************************************************************/
void bta_sys_busy(UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{
if(bta_sys_cb.prm_cb)
{
bta_sys_cb.prm_cb(BTA_SYS_CONN_BUSY, id, app_id, peer_addr);
}
if(bta_sys_cb.ppm_cb)
{
bta_sys_cb.ppm_cb(BTA_SYS_CONN_BUSY, id, app_id, peer_addr);
}
}
#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
/*******************************************************************************
**
** Function bta_sys_eir_register
**
** Description Called by BTA DM to register EIR utility function that can be
** used by the other BTA modules to add/remove UUID.
**
** Returns void
**
*******************************************************************************/
void bta_sys_eir_register(tBTA_SYS_EIR_CBACK * p_cback)
{
bta_sys_cb.eir_cb = p_cback;
}
/*******************************************************************************
**
** Function bta_sys_add_uuid
**
** Description Called by BTA subsystems to indicate to DM that new service
** class UUID is added.
**
** Returns void
**
*******************************************************************************/
void bta_sys_add_uuid(UINT16 uuid16)
{
if(bta_sys_cb.eir_cb)
{
bta_sys_cb.eir_cb(uuid16, TRUE );
}
}
/*******************************************************************************
**
** Function bta_sys_remove_uuid
**
** Description Called by BTA subsystems to indicate to DM that the service
** class UUID is removed.
**
** Returns void
**
*******************************************************************************/
void bta_sys_remove_uuid(UINT16 uuid16)
{
if(bta_sys_cb.eir_cb)
{
bta_sys_cb.eir_cb(uuid16, FALSE);
}
}
#endif
/*******************************************************************************
**
** Function bta_sys_vs_hdl
**
** Description Called by BTA subsystems to execute a VS event handler function
**
** Returns void
**
*******************************************************************************/
BOOLEAN bta_sys_vs_hdl(UINT16 evt, void *p)
{
if (bta_sys_cb.p_vs_evt_hdlr)
return (*bta_sys_cb.p_vs_evt_hdlr)(evt, p);
return FALSE;
}

View file

@ -0,0 +1,754 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the main implementation file for the BTA system manager.
*
******************************************************************************/
#define LOG_TAG "bt_bta_sys_main"
// #include <assert.h>
#include <string.h>
#include "alarm.h"
#include "thread.h"
#include "btm_api.h"
#include "bta_api.h"
#include "bta_sys.h"
#include "bta_sys_int.h"
#include "fixed_queue.h"
#include "gki.h"
#include "hash_map.h"
#include "osi.h"
#include "hash_functions.h"
// #include "osi/include/log.h"
// #include "osi/include/thread.h"
#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
#include "bta_ar_api.h"
#endif
#include "utl.h"
/* system manager control block definition */
#if BTA_DYNAMIC_MEMORY == FALSE
tBTA_SYS_CB bta_sys_cb;
#endif
fixed_queue_t *btu_bta_alarm_queue;
static hash_map_t *bta_alarm_hash_map;
static const size_t BTA_ALARM_HASH_MAP_SIZE = 17;
static pthread_mutex_t bta_alarm_lock;
// extern thread_t *bt_workqueue_thread;
/* trace level */
/* TODO Bluedroid - Hard-coded trace levels - Needs to be configurable */
UINT8 appl_trace_level = BT_TRACE_LEVEL_WARNING; //APPL_INITIAL_TRACE_LEVEL;
UINT8 btif_trace_level = BT_TRACE_LEVEL_WARNING;
// Communication queue between btu_task and bta.
extern fixed_queue_t *btu_bta_msg_queue;
void btu_bta_alarm_ready(fixed_queue_t *queue);
static const tBTA_SYS_REG bta_sys_hw_reg =
{
bta_sys_sm_execute,
NULL
};
/* type for action functions */
typedef void (*tBTA_SYS_ACTION)(tBTA_SYS_HW_MSG *p_data);
/* action function list */
const tBTA_SYS_ACTION bta_sys_action[] =
{
/* device manager local device API events - cf bta_sys.h for events */
bta_sys_hw_api_enable, /* 0 BTA_SYS_HW_API_ENABLE_EVT */
bta_sys_hw_evt_enabled, /* 1 BTA_SYS_HW_EVT_ENABLED_EVT */
bta_sys_hw_evt_stack_enabled, /* 2 BTA_SYS_HW_EVT_STACK_ENABLED_EVT */
bta_sys_hw_api_disable, /* 3 BTA_SYS_HW_API_DISABLE_EVT */
bta_sys_hw_evt_disabled, /* 4 BTA_SYS_HW_EVT_DISABLED_EVT */
bta_sys_hw_error /* 5 BTA_SYS_HW_ERROR_EVT */
};
/* state machine action enumeration list */
enum
{
/* device manager local device API events */
BTA_SYS_HW_API_ENABLE,
BTA_SYS_HW_EVT_ENABLED,
BTA_SYS_HW_EVT_STACK_ENABLED,
BTA_SYS_HW_API_DISABLE,
BTA_SYS_HW_EVT_DISABLED,
BTA_SYS_HW_ERROR
};
#define BTA_SYS_NUM_ACTIONS (BTA_SYS_MAX_EVT & 0x00ff)
#define BTA_SYS_IGNORE BTA_SYS_NUM_ACTIONS
/* state table information */
#define BTA_SYS_ACTIONS 2 /* number of actions */
#define BTA_SYS_NEXT_STATE 2 /* position of next state */
#define BTA_SYS_NUM_COLS 3 /* number of columns in state tables */
/* state table for OFF state */
const UINT8 bta_sys_hw_off[][BTA_SYS_NUM_COLS] =
{
/* Event Action 1 Action 2 Next State */
/* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
/* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
/* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
/* API_DISABLE */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
/* EVT_DISABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
/* EVT_ERROR */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF}
};
const UINT8 bta_sys_hw_starting[][BTA_SYS_NUM_COLS] =
{
/* Event Action 1 Action 2 Next State */
/* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING}, /* wait for completion event */
/* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
/* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
/* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}, /* successive disable/enable: change state wait for completion to disable */
/* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_HW_API_ENABLE, BTA_SYS_HW_STARTING}, /* successive enable/disable: notify, then restart HW */
/* EVT_ERROR */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}
};
const UINT8 bta_sys_hw_on[][BTA_SYS_NUM_COLS] =
{
/* Event Action 1 Action 2 Next State */
/* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
/* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
/* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
/* API_DISABLE */ {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON}, /* don't change the state here, as some other modules might be active */
/* EVT_DISABLED */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
/* EVT_ERROR */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}
};
const UINT8 bta_sys_hw_stopping[][BTA_SYS_NUM_COLS] =
{
/* Event Action 1 Action 2 Next State */
/* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING}, /* change state, and wait for completion event to enable */
/* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}, /* successive enable/disable: finish the enable before disabling */
/* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_HW_API_DISABLE, BTA_SYS_HW_STOPPING}, /* successive enable/disable: notify, then stop */
/* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}, /* wait for completion event */
/* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
/* EVT_ERROR */ {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}
};
typedef const UINT8 (*tBTA_SYS_ST_TBL)[BTA_SYS_NUM_COLS];
/* state table */
const tBTA_SYS_ST_TBL bta_sys_st_tbl[] = {
bta_sys_hw_off,
bta_sys_hw_starting,
bta_sys_hw_on,
bta_sys_hw_stopping
};
/*******************************************************************************
**
** Function bta_sys_init
**
** Description BTA initialization; called from task initialization.
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_init(void)
{
memset(&bta_sys_cb, 0, sizeof(tBTA_SYS_CB));
pthread_mutex_init(&bta_alarm_lock, NULL);
bta_alarm_hash_map = hash_map_new(BTA_ALARM_HASH_MAP_SIZE,
hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
btu_bta_alarm_queue = fixed_queue_new(SIZE_MAX);
fixed_queue_register_dequeue(btu_bta_alarm_queue,
btu_bta_alarm_ready);
appl_trace_level = APPL_INITIAL_TRACE_LEVEL;
/* register BTA SYS message handler */
bta_sys_register( BTA_ID_SYS, &bta_sys_hw_reg);
/* register for BTM notifications */
BTM_RegisterForDeviceStatusNotif ((tBTM_DEV_STATUS_CB*)&bta_sys_hw_btm_cback );
#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
bta_ar_init();
#endif
}
void bta_sys_free(void) {
fixed_queue_free(btu_bta_alarm_queue, NULL);
hash_map_free(bta_alarm_hash_map);
pthread_mutex_destroy(&bta_alarm_lock);
}
/*******************************************************************************
**
** Function bta_dm_sm_execute
**
** Description State machine event handling function for DM
**
**
** Returns void
**
*******************************************************************************/
BOOLEAN bta_sys_sm_execute(BT_HDR *p_msg)
{
BOOLEAN freebuf = TRUE;
tBTA_SYS_ST_TBL state_table;
UINT8 action;
int i;
APPL_TRACE_EVENT("bta_sys_sm_execute state:%d, event:0x%x", bta_sys_cb.state, p_msg->event);
/* look up the state table for the current state */
state_table = bta_sys_st_tbl[bta_sys_cb.state];
/* update state */
bta_sys_cb.state = state_table[p_msg->event & 0x00ff][BTA_SYS_NEXT_STATE];
/* execute action functions */
for (i = 0; i < BTA_SYS_ACTIONS; i++)
{
if ((action = state_table[p_msg->event & 0x00ff][i]) != BTA_SYS_IGNORE)
{
(*bta_sys_action[action])( (tBTA_SYS_HW_MSG*) p_msg);
}
else
{
break;
}
}
return freebuf;
}
void bta_sys_hw_register( tBTA_SYS_HW_MODULE module, tBTA_SYS_HW_CBACK *cback)
{
bta_sys_cb.sys_hw_cback[module]=cback;
}
void bta_sys_hw_unregister( tBTA_SYS_HW_MODULE module )
{
bta_sys_cb.sys_hw_cback[module]=NULL;
}
/*******************************************************************************
**
** Function bta_sys_hw_btm_cback
**
** Description This function is registered by BTA SYS to BTM in order to get status notifications
**
**
** Returns
**
*******************************************************************************/
void bta_sys_hw_btm_cback( tBTM_DEV_STATUS status )
{
tBTA_SYS_HW_MSG *sys_event;
APPL_TRACE_DEBUG(" bta_sys_hw_btm_cback was called with parameter: %i" , status );
/* send a message to BTA SYS */
if ((sys_event = (tBTA_SYS_HW_MSG *) GKI_getbuf(sizeof(tBTA_SYS_HW_MSG))) != NULL)
{
if (status == BTM_DEV_STATUS_UP)
sys_event->hdr.event = BTA_SYS_EVT_STACK_ENABLED_EVT;
else if (status == BTM_DEV_STATUS_DOWN)
sys_event->hdr.event = BTA_SYS_ERROR_EVT;
else
{
/* BTM_DEV_STATUS_CMD_TOUT is ignored for now. */
GKI_freebuf (sys_event);
sys_event = NULL;
}
if (sys_event)
{
bta_sys_sendmsg(sys_event);
}
}
else
{
APPL_TRACE_DEBUG("ERROR bta_sys_hw_btm_cback couldn't send msg" );
}
}
/*******************************************************************************
**
** Function bta_sys_hw_error
**
** Description In case the HW device stops answering... Try to turn it off, then re-enable all
** previously active SW modules.
**
** Returns success or failure
**
*******************************************************************************/
void bta_sys_hw_error(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
UINT8 module_index;
UNUSED(p_sys_hw_msg);
APPL_TRACE_DEBUG("%s", __FUNCTION__);
for (module_index = 0; module_index < BTA_SYS_MAX_HW_MODULES; module_index++)
{
if( bta_sys_cb.sys_hw_module_active & ((UINT32)1 << module_index )) {
switch( module_index)
{
case BTA_SYS_HW_BLUETOOTH:
/* Send BTA_SYS_HW_ERROR_EVT to DM */
if (bta_sys_cb.sys_hw_cback[module_index] != NULL)
bta_sys_cb.sys_hw_cback[module_index] (BTA_SYS_HW_ERROR_EVT);
break;
default:
/* not yet supported */
break;
}
}
}
}
/*******************************************************************************
**
** Function bta_sys_hw_enable
**
** Description this function is called after API enable and HW has been turned on
**
**
** Returns success or failure
**
*******************************************************************************/
void bta_sys_hw_api_enable( tBTA_SYS_HW_MSG *p_sys_hw_msg )
{
if ((!bta_sys_cb.sys_hw_module_active) && (bta_sys_cb.state != BTA_SYS_HW_ON))
{
/* register which HW module was turned on */
bta_sys_cb.sys_hw_module_active |= ((UINT32)1 << p_sys_hw_msg->hw_module );
tBTA_SYS_HW_MSG *p_msg;
if ((p_msg = (tBTA_SYS_HW_MSG *) GKI_getbuf(sizeof(tBTA_SYS_HW_MSG))) != NULL)
{
p_msg->hdr.event = BTA_SYS_EVT_ENABLED_EVT;
p_msg->hw_module = p_sys_hw_msg->hw_module;
bta_sys_sendmsg(p_msg);
}
}
else
{
/* register which HW module was turned on */
bta_sys_cb.sys_hw_module_active |= ((UINT32)1 << p_sys_hw_msg->hw_module );
/* HW already in use, so directly notify the caller */
if (bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]!= NULL )
bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]( BTA_SYS_HW_ON_EVT );
}
APPL_TRACE_EVENT ("bta_sys_hw_api_enable for %d, active modules 0x%04X",
p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active);
}
/*******************************************************************************
**
** Function bta_sys_hw_disable
**
** Description if no other module is using the HW, this function will call ( if defined ) a user-macro to turn off the HW
**
**
** Returns success or failure
**
*******************************************************************************/
void bta_sys_hw_api_disable(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
APPL_TRACE_DEBUG("bta_sys_hw_api_disable for %d, active modules: 0x%04X",
p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active );
/* make sure the related SW blocks were stopped */
bta_sys_disable( p_sys_hw_msg->hw_module );
/* register which module we turn off */
bta_sys_cb.sys_hw_module_active &= ~((UINT32)1 << p_sys_hw_msg->hw_module );
/* if there are still some SW modules using the HW, just provide an answer to the calling */
if( bta_sys_cb.sys_hw_module_active != 0 )
{
/* if there are still some SW modules using the HW, directly notify the caller */
if( bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]!= NULL )
bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]( BTA_SYS_HW_OFF_EVT );
}
else
{
/* manually update the state of our system */
bta_sys_cb.state = BTA_SYS_HW_STOPPING;
tBTA_SYS_HW_MSG *p_msg;
if ((p_msg = (tBTA_SYS_HW_MSG *) GKI_getbuf(sizeof(tBTA_SYS_HW_MSG))) != NULL)
{
p_msg->hdr.event = BTA_SYS_EVT_DISABLED_EVT;
p_msg->hw_module = p_sys_hw_msg->hw_module;
bta_sys_sendmsg(p_msg);
}
}
}
/*******************************************************************************
**
** Function bta_sys_hw_event_enabled
**
** Description
**
**
** Returns success or failure
**
*******************************************************************************/
void bta_sys_hw_evt_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
APPL_TRACE_EVENT("bta_sys_hw_evt_enabled for %i", p_sys_hw_msg->hw_module);
BTM_DeviceReset( NULL );
}
/*******************************************************************************
**
** Function bta_sys_hw_event_disabled
**
** Description
**
**
** Returns success or failure
**
*******************************************************************************/
void bta_sys_hw_evt_disabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
UINT8 hw_module_index;
APPL_TRACE_DEBUG("bta_sys_hw_evt_disabled - module 0x%X", p_sys_hw_msg->hw_module);
for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES; hw_module_index++)
{
if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL)
bta_sys_cb.sys_hw_cback[hw_module_index] (BTA_SYS_HW_OFF_EVT);
}
}
/*******************************************************************************
**
** Function bta_sys_hw_event_stack_enabled
**
** Description we receive this event once the SW side is ready ( stack, FW download,... ),
** i.e. we can really start using the device. So notify the app.
**
** Returns success or failure
**
*******************************************************************************/
void bta_sys_hw_evt_stack_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
UINT8 hw_module_index;
UNUSED(p_sys_hw_msg);
APPL_TRACE_DEBUG(" bta_sys_hw_evt_stack_enabled!notify the callers");
for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES; hw_module_index++ )
{
if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL)
bta_sys_cb.sys_hw_cback[hw_module_index] (BTA_SYS_HW_ON_EVT);
}
}
/*******************************************************************************
**
** Function bta_sys_event
**
** Description BTA event handler; called from task event handler.
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_event(BT_HDR *p_msg)
{
UINT8 id;
BOOLEAN freebuf = TRUE;
APPL_TRACE_EVENT("BTA got event 0x%x", p_msg->event);
/* get subsystem id from event */
id = (UINT8) (p_msg->event >> 8);
/* verify id and call subsystem event handler */
if ((id < BTA_ID_MAX) && (bta_sys_cb.reg[id] != NULL))
{
freebuf = (*bta_sys_cb.reg[id]->evt_hdlr)(p_msg);
}
else
{
APPL_TRACE_WARNING("BTA got unregistered event id %d", id);
}
if (freebuf)
{
GKI_freebuf(p_msg);
}
}
/*******************************************************************************
**
** Function bta_sys_register
**
** Description Called by other BTA subsystems to register their event
** handler.
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_register(UINT8 id, const tBTA_SYS_REG *p_reg)
{
bta_sys_cb.reg[id] = (tBTA_SYS_REG *) p_reg;
bta_sys_cb.is_reg[id] = TRUE;
}
/*******************************************************************************
**
** Function bta_sys_deregister
**
** Description Called by other BTA subsystems to de-register
** handler.
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_deregister(UINT8 id)
{
bta_sys_cb.is_reg[id] = FALSE;
}
/*******************************************************************************
**
** Function bta_sys_is_register
**
** Description Called by other BTA subsystems to get registeration
** status.
**
**
** Returns void
**
*******************************************************************************/
BOOLEAN bta_sys_is_register(UINT8 id)
{
return bta_sys_cb.is_reg[id];
}
/*******************************************************************************
**
** Function bta_sys_sendmsg
**
** Description Send a GKI message to BTA. This function is designed to
** optimize sending of messages to BTA. It is called by BTA
** API functions and call-in functions.
**
**
** Returns void
**
*******************************************************************************/
void bta_sys_sendmsg(void *p_msg)
{
// There is a race condition that occurs if the stack is shut down while
// there is a procedure in progress that can schedule a task via this
// message queue. This causes |btu_bta_msg_queue| to get cleaned up before
// it gets used here; hence we check for NULL before using it.
if (btu_bta_msg_queue) {
fixed_queue_enqueue(btu_bta_msg_queue, p_msg);
//ke_event_set(KE_EVENT_BTU_TASK_THREAD);
btu_task_post();
}
}
/*******************************************************************************
**
** Function bta_sys_start_timer
**
** Description Start a protocol timer for the specified amount
** of time in milliseconds.
**
** Returns void
**
*******************************************************************************/
void bta_alarm_cb(void *data) {
assert(data != NULL);
TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
fixed_queue_enqueue(btu_bta_alarm_queue, p_tle);
}
void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout_ms) {
assert(p_tle != NULL);
// Get the alarm for this p_tle.
pthread_mutex_lock(&bta_alarm_lock);
if (!hash_map_has_key(bta_alarm_hash_map, p_tle)) {
hash_map_set(bta_alarm_hash_map, p_tle, osi_alarm_new("bta_sys", bta_alarm_cb, p_tle, 0));
}
pthread_mutex_unlock(&bta_alarm_lock);
osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
if (alarm == NULL) {
LOG_ERROR("%s unable to create alarm.", __func__);
return;
}
p_tle->event = type;
p_tle->ticks = timeout_ms;
//osi_alarm_set(alarm, (period_ms_t)timeout_ms, bta_alarm_cb, p_tle);
osi_alarm_set(alarm, (period_ms_t)timeout_ms);
}
bool hash_iter_ro_cb(hash_map_entry_t *hash_map_entry, void *context)
{
osi_alarm_t *alarm = (osi_alarm_t *)hash_map_entry->data;
period_ms_t *p_remaining_ms = (period_ms_t*)context;
*p_remaining_ms +=osi_alarm_get_remaining_ms(alarm);
return true;
}
UINT32 bta_sys_get_remaining_ticks(TIMER_LIST_ENT *p_target_tle)
{
period_ms_t remaining_ms = 0;
pthread_mutex_lock(&bta_alarm_lock);
// Get the alarm for this p_tle
hash_map_foreach(bta_alarm_hash_map, hash_iter_ro_cb, &remaining_ms);
pthread_mutex_unlock(&bta_alarm_lock);
return remaining_ms;
}
/*******************************************************************************
**
** Function bta_sys_stop_timer
**
** Description Stop a BTA timer.
**
** Returns void
**
*******************************************************************************/
void bta_sys_stop_timer(TIMER_LIST_ENT *p_tle) {
assert(p_tle != NULL);
osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
if (alarm == NULL) {
LOG_DEBUG("%s expected alarm was not in bta alarm hash map.", __func__);
return;
}
osi_alarm_cancel(alarm);
}
/*******************************************************************************
**
** Function bta_sys_disable
**
** Description For each registered subsystem execute its disable function.
**
** Returns void
**
*******************************************************************************/
void bta_sys_disable(tBTA_SYS_HW_MODULE module)
{
int bta_id = 0;
int bta_id_max = 0;
APPL_TRACE_DEBUG("bta_sys_disable: module %i", module);
switch( module )
{
case BTA_SYS_HW_BLUETOOTH:
bta_id = BTA_ID_DM;
bta_id_max = BTA_ID_BLUETOOTH_MAX;
break;
default:
APPL_TRACE_WARNING("bta_sys_disable: unkown module");
return;
}
for ( ; bta_id <= bta_id_max; bta_id++)
{
if (bta_sys_cb.reg[bta_id] != NULL)
{
if (bta_sys_cb.is_reg[bta_id] == TRUE && bta_sys_cb.reg[bta_id]->disable != NULL)
{
(*bta_sys_cb.reg[bta_id]->disable)();
}
}
}
}
/*******************************************************************************
**
** Function bta_sys_set_trace_level
**
** Description Set trace level for BTA
**
** Returns void
**
*******************************************************************************/
void bta_sys_set_trace_level(UINT8 level)
{
appl_trace_level = level;
}
/*******************************************************************************
**
** Function bta_sys_get_sys_features
**
** Description Returns sys_features to other BTA modules.
**
** Returns sys_features
**
*******************************************************************************/
UINT16 bta_sys_get_sys_features (void)
{
return bta_sys_cb.sys_features;
}

View file

@ -0,0 +1,104 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the private interface file for the BTA system manager.
*
******************************************************************************/
#ifndef BTA_SYS_INT_H
#define BTA_SYS_INT_H
/*****************************************************************************
** Constants and data types
*****************************************************************************/
/*****************************************************************************
** state table
*****************************************************************************/
/* SYS HW state */
enum
{
BTA_SYS_HW_OFF,
BTA_SYS_HW_STARTING,
BTA_SYS_HW_ON,
BTA_SYS_HW_STOPPING
};
typedef UINT8 tBTA_SYS_HW_STATE;
/* Collision callback */
#define MAX_COLLISION_REG 5
typedef struct
{
UINT8 id[MAX_COLLISION_REG];
tBTA_SYS_CONN_CBACK *p_coll_cback[MAX_COLLISION_REG];
} tBTA_SYS_COLLISION;
/* system manager control block */
typedef struct
{
tBTA_SYS_REG *reg[BTA_ID_MAX]; /* registration structures */
BOOLEAN is_reg[BTA_ID_MAX]; /* registration structures */
tBTA_SYS_HW_STATE state;
tBTA_SYS_HW_CBACK *sys_hw_cback[BTA_SYS_MAX_HW_MODULES]; /* enable callback for each HW modules */
UINT32 sys_hw_module_active; /* bitmask of all active modules */
UINT16 sys_features; /* Bitmask of sys features */
tBTA_SYS_CONN_CBACK *prm_cb; /* role management callback registered by DM */
tBTA_SYS_CONN_CBACK *ppm_cb; /* low power management callback registered by DM */
tBTA_SYS_CONN_CBACK *p_policy_cb; /* link policy change callback registered by DM */
tBTA_SYS_CONN_CBACK *p_sco_cb; /* SCO connection change callback registered by AV */
tBTA_SYS_CONN_CBACK *p_role_cb; /* role change callback registered by AV */
tBTA_SYS_COLLISION colli_reg; /* collision handling module */
#if (BTA_EIR_CANNED_UUID_LIST != TRUE)
tBTA_SYS_EIR_CBACK *eir_cb; /* add/remove UUID into EIR */
#endif
#if (BTM_SSR_INCLUDED == TRUE)
tBTA_SYS_SSR_CFG_CBACK *p_ssr_cb;
#endif
/* VS event handler */
tBTA_SYS_VS_EVT_HDLR *p_vs_evt_hdlr;
} tBTA_SYS_CB;
/*****************************************************************************
** Global variables
*****************************************************************************/
/* system manager control block */
#if BTA_DYNAMIC_MEMORY == FALSE
extern tBTA_SYS_CB bta_sys_cb;
#else
extern tBTA_SYS_CB *bta_sys_cb_ptr;
#define bta_sys_cb (*bta_sys_cb_ptr)
#endif
/* functions used for BTA SYS HW state machine */
void bta_sys_hw_btm_cback( tBTM_DEV_STATUS status );
void bta_sys_hw_error(tBTA_SYS_HW_MSG *p_sys_hw_msg);
void bta_sys_hw_api_enable( tBTA_SYS_HW_MSG *p_sys_hw_msg );
void bta_sys_hw_api_disable(tBTA_SYS_HW_MSG *p_sys_hw_msg);
void bta_sys_hw_evt_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg);
void bta_sys_hw_evt_disabled(tBTA_SYS_HW_MSG *p_sys_hw_msg);
void bta_sys_hw_evt_stack_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg);
BOOLEAN bta_sys_sm_execute(BT_HDR *p_msg);
#endif /* BTA_SYS_INT_H */

View file

@ -0,0 +1,299 @@
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains utility functions.
*
******************************************************************************/
#include <stddef.h>
#include "utl.h"
#include "gki.h"
#include "btm_api.h"
/*******************************************************************************
**
** Function utl_str2int
**
** Description This utility function converts a character string to an
** integer. Acceptable values in string are 0-9. If invalid
** string or string value too large, -1 is returned. Leading
** spaces are skipped.
**
**
** Returns Integer value or -1 on error.
**
*******************************************************************************/
INT16 utl_str2int(const char *p_s)
{
INT32 val = 0;
for (;*p_s == ' ' && *p_s != 0; p_s++);
if (*p_s == 0) return -1;
for (;;)
{
if ((*p_s < '0') || (*p_s > '9')) return -1;
val += (INT32) (*p_s++ - '0');
if (val > 32767) return -1;
if (*p_s == 0)
{
return (INT16) val;
}
else
{
val *= 10;
}
}
}
/*******************************************************************************
**
** Function utl_strucmp
**
** Description This utility function compares two strings in uppercase.
** String p_s must be uppercase. String p_t is converted to
** uppercase if lowercase. If p_s ends first, the substring
** match is counted as a match.
**
**
** Returns 0 if strings match, nonzero otherwise.
**
*******************************************************************************/
int utl_strucmp(const char *p_s, const char *p_t)
{
char c;
while (*p_s && *p_t)
{
c = *p_t++;
if (c >= 'a' && c <= 'z')
{
c -= 0x20;
}
if (*p_s++ != c)
{
return -1;
}
}
/* if p_t hit null first, no match */
if (*p_t == 0 && *p_s != 0)
{
return 1;
}
/* else p_s hit null first, count as match */
else
{
return 0;
}
}
/*******************************************************************************
**
** Function utl_itoa
**
** Description This utility function converts a UINT16 to a string. The
** string is NULL-terminated. The length of the string is
** returned;
**
**
** Returns Length of string.
**
*******************************************************************************/
UINT8 utl_itoa(UINT16 i, char *p_s)
{
UINT16 j, k;
char *p = p_s;
BOOLEAN fill = FALSE;
if (i == 0)
{
/* take care of zero case */
*p++ = '0';
}
else
{
for(j = 10000; j > 0; j /= 10)
{
k = i / j;
i %= j;
if (k > 0 || fill)
{
*p++ = k + '0';
fill = TRUE;
}
}
}
*p = 0;
return (UINT8) (p - p_s);
}
/*******************************************************************************
**
** Function utl_freebuf
**
** Description This function calls GKI_freebuf to free the buffer passed
** in, if buffer pointer is not NULL, and also initializes
** buffer pointer to NULL.
**
**
** Returns Nothing.
**
*******************************************************************************/
void utl_freebuf(void **p)
{
if (*p != NULL)
{
GKI_freebuf(*p);
*p = NULL;
}
}
/*******************************************************************************
**
** Function utl_set_device_class
**
** Description This function updates the local Device Class.
**
** Parameters:
** p_cod - Pointer to the device class to set to
**
** cmd - the fields of the device class to update.
** BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major, minor class
** BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in the input
** BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in the input
** BTA_UTL_SET_COD_ALL - overwrite major, minor, set the bits in service class
** BTA_UTL_INIT_COD - overwrite major, minor, and service class
**
** Returns TRUE if successful, Otherwise FALSE
**
*******************************************************************************/
BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd)
{
UINT8 *dev;
UINT16 service;
UINT8 minor, major;
DEV_CLASS dev_class;
dev = BTM_ReadDeviceClass();
BTM_COD_SERVICE_CLASS( service, dev );
BTM_COD_MINOR_CLASS(minor, dev );
BTM_COD_MAJOR_CLASS(major, dev );
switch(cmd)
{
case BTA_UTL_SET_COD_MAJOR_MINOR:
minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
break;
case BTA_UTL_SET_COD_SERVICE_CLASS:
/* clear out the bits that is not SERVICE_CLASS bits */
p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
service = service | p_cod->service;
break;
case BTA_UTL_CLR_COD_SERVICE_CLASS:
p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
service = service & (~p_cod->service);
break;
case BTA_UTL_SET_COD_ALL:
minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
service = service | p_cod->service;
break;
case BTA_UTL_INIT_COD:
minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK;
break;
default:
return FALSE;
}
/* convert the fields into the device class type */
FIELDS_TO_COD(dev_class, minor, major, service);
if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS)
return TRUE;
return FALSE;
}
/*******************************************************************************
**
** Function utl_isintstr
**
** Description This utility function checks if the given string is an
** integer string or not
**
**
** Returns TRUE if successful, Otherwise FALSE
**
*******************************************************************************/
BOOLEAN utl_isintstr(const char *p_s)
{
UINT16 i = 0;
for(i=0; p_s[i] != 0; i++)
{
if(((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';'))
return FALSE;
}
return TRUE;
}
/*******************************************************************************
**
** Function utl_isdialstr
**
** Description This utility function checks if the given string contains
** only dial digits or not
**
**
** Returns TRUE if successful, Otherwise FALSE
**
*******************************************************************************/
BOOLEAN utl_isdialstr(const char *p_s)
{
UINT16 i = 0;
for(i=0; p_s[i] != 0; i++)
{
if(!(((p_s[i] >= '0') && (p_s[i] <= '9'))
|| (p_s[i] == '*') || (p_s[i] == '+') || (p_s[i] == '#') || (p_s[i] == ';')
|| ((p_s[i] >= 'A') && (p_s[i] <= 'C'))
|| ((p_s[i] == 'p') || (p_s[i] == 'P')
|| (p_s[i] == 'w') || (p_s[i] == 'W'))))
return FALSE;
}
return TRUE;
}

View file

@ -0,0 +1,107 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include "bt_trace.h"
#include "bdaddr.h"
static inline bool ets_isxdigit(char c) {
if ((c >= '0') && (c <= '9'))
return true;
if ((c >= 'a') && (c <= 'f'))
return true;
return ((c >= 'A') && (c <= 'F'));
}
bool bdaddr_is_empty(const bt_bdaddr_t *addr) {
assert(addr != NULL);
uint8_t zero[sizeof(bt_bdaddr_t)] = { 0 };
return memcmp(addr, &zero, sizeof(bt_bdaddr_t)) == 0;
}
bool bdaddr_equals(const bt_bdaddr_t *first, const bt_bdaddr_t *second) {
assert(first != NULL);
assert(second != NULL);
return memcmp(first, second, sizeof(bt_bdaddr_t)) == 0;
}
bt_bdaddr_t *bdaddr_copy(bt_bdaddr_t *dest, const bt_bdaddr_t *src) {
assert(dest != NULL);
assert(src != NULL);
return (bt_bdaddr_t *)memcpy(dest, src, sizeof(bt_bdaddr_t));
}
const char *bdaddr_to_string(const bt_bdaddr_t *addr, char *string, size_t size) {
assert(addr != NULL);
assert(string != NULL);
if (size < 18)
return NULL;
const uint8_t *ptr = addr->address;
sprintf(string, "%02x:%02x:%02x:%02x:%02x:%02x",
ptr[0], ptr[1], ptr[2],
ptr[3], ptr[4], ptr[5]);
return string;
}
bool string_is_bdaddr(const char *string) {
assert(string != NULL);
size_t len = strlen(string);
if (len != 17)
return false;
for (size_t i = 0; i < len; ++i) {
// Every 3rd char must be ':'.
if (((i + 1) % 3) == 0 && string[i] != ':')
return false;
// All other chars must be a hex digit.
if (((i + 1) % 3) != 0 && !ets_isxdigit(string[i]))
return false;
}
return true;
}
bool string_to_bdaddr(const char *string, bt_bdaddr_t *addr) {
assert(string != NULL);
assert(addr != NULL);
bt_bdaddr_t new_addr;
uint8_t *ptr = new_addr.address;
bool ret = sscanf(string, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
&ptr[0], &ptr[1], &ptr[2], &ptr[3], &ptr[4], &ptr[5]) == 6;
if (ret)
memcpy(addr, &new_addr, sizeof(bt_bdaddr_t));
return ret;
}
hash_index_t hash_function_bdaddr(const void *key) {
hash_index_t hash = 5381;
const char *bytes = (const char *)key;
for (size_t i = 0; i < sizeof(bt_bdaddr_t); ++i)
hash = ((hash << 5) + hash) + bytes[i];
return hash;
}

View file

@ -0,0 +1,63 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _BDADDR_H_
#define _BDADDR_H_
#include <stdbool.h>
#include <stddef.h>
#include "bt_defs.h"
#include "hash_map.h"
// Note: the string representation of a bdaddr is expected to have the format
// xx:xx:xx:xx:xx:xx
// where each 'x' is a hex digit. The API presented in this header will accept
// both uppercase and lowercase digits but will only ever produce lowercase
// digits.
// Returns true if |addr| is the empty address (00:00:00:00:00:00).
// |addr| may not be NULL.
bool bdaddr_is_empty(const bt_bdaddr_t *addr);
// Returns true if |first| and |second| refer to the same address. Neither
// may be NULL.
bool bdaddr_equals(const bt_bdaddr_t *first, const bt_bdaddr_t *second);
// Returns destination bdaddr |dest| after copying |src| to |dest|.
// |dest| and |src| must not be NULL.
bt_bdaddr_t *bdaddr_copy(bt_bdaddr_t *dest, const bt_bdaddr_t *src);
// Makes a string representation of |addr| and places it into |string|. |size|
// refers to the size of |string|'s buffer and must be >= 18. On success, this
// function returns |string|, otherwise it returns NULL. Neither |addr| nor |string|
// may be NULL.
const char *bdaddr_to_string(const bt_bdaddr_t *addr, char *string, size_t size);
// Returns true if |string| represents a Bluetooth address. |string| may not be NULL.
bool string_is_bdaddr(const char *string);
// Converts |string| to bt_bdaddr_t and places it in |addr|. If |string| does not
// represent a Bluetooth address, |addr| is not modified and this function returns
// false. Otherwise, it returns true. Neither |string| nor |addr| may be NULL.
bool string_to_bdaddr(const char *string, bt_bdaddr_t *addr);
// A hash function tailored for bdaddrs.
hash_index_t hash_function_bdaddr(const void *key);
#endif

View file

@ -0,0 +1,29 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _DEVICE_FEATURES_H_
#define _DEVICE_FEATURES_H_
#include <stdint.h>
// Represents a page of device feature enabled/disabled bits returned
// by the local controller. See the bluetooth spec for bit indexes.
typedef struct {
uint8_t as_array[8];
} bt_device_features_t;
#endif /*_DEVICE_FEATURES_H_*/

View file

@ -0,0 +1,30 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _EVENT_MASK_H_
#define _EVENT_MASK_H_
#include <stdint.h>
// Represents a mask which can be used to tell the controller which
// HCI events the stack wishes to be informed about. See the bluetooth
// spec for more information on what each bit means.
typedef struct {
uint8_t as_array[8];
} bt_event_mask_t;
#endif /*_EVENT_MASK_H_*/

View file

@ -0,0 +1,31 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _VERSION_H_
#define _VERSION_H_
#include <stdint.h>
typedef struct {
uint8_t hci_version;
uint16_t hci_revision;
uint8_t lmp_version;
uint16_t manufacturer;
uint16_t lmp_subversion;
} bt_version_t;
#endif /*_VERSION_H_*/

View file

@ -0,0 +1,464 @@
/******************************************************************************
*
* Copyright (C) 2009-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "bta_api.h"
#include "bta_sys.h"
#include "bta_dm_co.h"
#include "bta_dm_ci.h"
#if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
#include "bt_utils.h"
#if (BTM_OOB_INCLUDED == TRUE)
#include "btif_dm.h"
#endif
#endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
#include "bte_appl.h"
tBTE_APPL_CFG bte_appl_cfg =
{
#if SMP_INCLUDED == TRUE
BTA_LE_AUTH_REQ_SC_MITM_BOND, // Authentication requirements
#else
BTM_AUTH_SPGB_YES, // Authentication requirements
#endif
BTM_LOCAL_IO_CAPS_BLE,
BTM_BLE_INITIATOR_KEY_SIZE,
BTM_BLE_RESPONDER_KEY_SIZE,
BTM_BLE_MAX_KEY_SIZE
};
#endif
/*******************************************************************************
**
** Function bta_dm_co_get_compress_memory
**
** Description This callout function is executed by DM to get memory for compression
** Parameters id - BTA SYS ID
** memory_p - memory return by callout
** memory_size - memory size
**
** Returns TRUE for success, FALSE for fail.
**
*******************************************************************************/
BOOLEAN bta_dm_co_get_compress_memory(tBTA_SYS_ID id, UINT8 **memory_p, UINT32 *memory_size)
{
UNUSED(id);
UNUSED(memory_p);
UNUSED(memory_size);
return TRUE;
}
/*******************************************************************************
**
** Function bta_dm_co_io_req
**
** Description This callout function is executed by DM to get IO capabilities
** of the local device for the Simple Pairing process
**
** Parameters bd_addr - The peer device
** *p_io_cap - The local Input/Output capabilities
** *p_oob_data - TRUE, if OOB data is available for the peer device.
** *p_auth_req - TRUE, if MITM protection is required.
**
** Returns void.
**
*******************************************************************************/
void bta_dm_co_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
{
UNUSED(bd_addr);
#if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
#if (BTM_OOB_INCLUDED == TRUE)
btif_dm_set_oob_for_io_req(p_oob_data);
#endif
btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig);
#else
LOG_ERROR("bta_dm_co_io_req: func not ported\n");
#endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data);
BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap);
BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req);
BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig);
}
/*******************************************************************************
**
** Function bta_dm_co_io_rsp
**
** Description This callout function is executed by DM to report IO capabilities
** of the peer device for the Simple Pairing process
**
** Parameters bd_addr - The peer device
** io_cap - The remote Input/Output capabilities
** oob_data - TRUE, if OOB data is available for the peer device.
** auth_req - TRUE, if MITM protection is required.
**
** Returns void.
**
*******************************************************************************/
void bta_dm_co_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
{
#if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req);
#else
LOG_ERROR("bta_dm_co_io_rsp: func not ported\n");
#endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
}
/*******************************************************************************
**
** Function bta_dm_co_lk_upgrade
**
** Description This callout function is executed by DM to check if the
** platform wants allow link key upgrade
**
** Parameters bd_addr - The peer device
** *p_upgrade - TRUE, if link key upgrade is desired.
**
** Returns void.
**
*******************************************************************************/
void bta_dm_co_lk_upgrade(BD_ADDR bd_addr, BOOLEAN *p_upgrade )
{
UNUSED(bd_addr);
UNUSED(p_upgrade);
}
#if (BTM_OOB_INCLUDED == TRUE)
/*******************************************************************************
**
** Function bta_dm_co_loc_oob
**
** Description This callout function is executed by DM to report the OOB
** data of the local device for the Simple Pairing process
**
** Parameters valid - TRUE, if the local OOB data is retrieved from LM
** c - Simple Pairing Hash C
** r - Simple Pairing Randomnizer R
**
** Returns void.
**
*******************************************************************************/
void bta_dm_co_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
{
#if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid);
#ifdef BTIF_DM_OOB_TEST
btif_dm_proc_loc_oob(valid, c, r);
#endif
#else
LOG_ERROR("bta_dm_co_loc_oob: func not ported\n");
#endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
}
/*******************************************************************************
**
** Function bta_dm_co_rmt_oob
**
** Description This callout function is executed by DM to request the OOB
** data for the remote device for the Simple Pairing process
** Need to call bta_dm_ci_rmt_oob() in response
**
** Parameters bd_addr - The peer device
**
** Returns void.
**
*******************************************************************************/
void bta_dm_co_rmt_oob(BD_ADDR bd_addr)
{
BT_OCTET16 p_c;
BT_OCTET16 p_r;
BOOLEAN result = FALSE;
#ifdef BTIF_DM_OOB_TEST
#if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
result = btif_dm_proc_rmt_oob(bd_addr, p_c, p_r);
#else
LOG_ERROR("bta_dm_rmt_oob: func not ported\n");
#endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
#endif
BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d",result);
bta_dm_ci_rmt_oob(result, bd_addr, p_c, p_r);
}
#endif /* BTM_OOB_INCLUDED */
// REMOVE FOR BLUEDROID ?
#if (BTM_SCO_HCI_INCLUDED == TRUE ) && (BTM_SCO_INCLUDED == TRUE)
#if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
/*******************************************************************************
**
** Function btui_sco_codec_callback
**
** Description Callback for btui codec.
**
**
** Returns void
**
*******************************************************************************/
static void btui_sco_codec_callback(UINT16 event, UINT16 sco_handle)
{
bta_dm_sco_ci_data_ready(event, sco_handle);
}
/*******************************************************************************
**
** Function bta_dm_sco_co_init
**
** Description This function can be used by the phone to initialize audio
** codec or for other initialization purposes before SCO connection
** is opened.
**
**
** Returns tBTA_DM_SCO_ROUTE_TYPE: SCO routing configuration type.
**
*******************************************************************************/
tBTA_DM_SCO_ROUTE_TYPE bta_dm_sco_co_init(UINT32 rx_bw, UINT32 tx_bw,
tBTA_CODEC_INFO * p_codec_type, UINT8 app_id)
{
tBTM_SCO_ROUTE_TYPE route = BTA_DM_SCO_ROUTE_PCM;
BTIF_TRACE_DEBUG("bta_dm_sco_co_init");
/* set up SCO routing configuration if SCO over HCI app ID is used and run time
configuration is set to SCO over HCI */
/* HS invoke this call-out */
if (
#if (BTA_HS_INCLUDED == TRUE ) && (BTA_HS_INCLUDED == TRUE)
(app_id == BTUI_DM_SCO_4_HS_APP_ID && btui_cfg.hs_sco_over_hci) ||
#endif
/* AG invoke this call-out */
(app_id != BTUI_DM_SCO_4_HS_APP_ID && btui_cfg.ag_sco_over_hci ))
{
route = btui_cb.sco_hci = BTA_DM_SCO_ROUTE_HCI;
}
/* no codec is is used for the SCO data */
if (p_codec_type->codec_type == BTA_SCO_CODEC_PCM && route == BTA_DM_SCO_ROUTE_HCI)
{
/* initialize SCO codec */
if (!btui_sco_codec_init(rx_bw, tx_bw))
{
BTIF_TRACE_ERROR("codec initialization exception!");
}
}
return route;
}
/*******************************************************************************
**
** Function bta_dm_sco_co_open
**
** Description This function is executed when a SCO connection is open.
**
**
** Returns void
**
*******************************************************************************/
void bta_dm_sco_co_open(UINT16 handle, UINT8 pkt_size, UINT16 event)
{
tBTUI_SCO_CODEC_CFG cfg;
if (btui_cb.sco_hci)
{
BTIF_TRACE_DEBUG("bta_dm_sco_co_open handle:%d pkt_size:%d", handle, pkt_size);
/* use dedicated SCO buffer pool for SCO TX data */
cfg.pool_id = HCI_SCO_POOL_ID;
cfg.p_cback = btui_sco_codec_callback;
cfg.pkt_size = pkt_size;
cfg.cb_event = event;
/* open and start the codec */
btui_sco_codec_open(&cfg);
btui_sco_codec_start(handle);
}
}
/*******************************************************************************
**
** Function bta_dm_sco_co_close
**
** Description This function is called when a SCO connection is closed
**
**
** Returns void
**
*******************************************************************************/
void bta_dm_sco_co_close(void)
{
if (btui_cb.sco_hci)
{
BTIF_TRACE_DEBUG("bta_dm_sco_co_close close codec");
/* close sco codec */
btui_sco_codec_close();
btui_cb.sco_hci = FALSE;
}
}
/*******************************************************************************
**
** Function bta_dm_sco_co_in_data
**
** Description This function is called to send incoming SCO data to application.
**
** Returns void
**
*******************************************************************************/
void bta_dm_sco_co_in_data(BT_HDR *p_buf)
{
if (btui_cfg.sco_use_mic)
btui_sco_codec_inqdata (p_buf);
else
GKI_freebuf(p_buf);
}
/*******************************************************************************
**
** Function bta_dm_sco_co_out_data
**
** Description This function is called to send SCO data over HCI.
**
** Returns void
**
*******************************************************************************/
void bta_dm_sco_co_out_data(BT_HDR **p_buf)
{
btui_sco_codec_readbuf(p_buf);
}
#endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
#endif /* #if (BTM_SCO_HCI_INCLUDED == TRUE ) && (BTM_SCO_INCLUDED == TRUE)*/
#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
/*******************************************************************************
**
** Function bta_dm_co_le_io_key_req
**
** Description This callout function is executed by DM to get BLE key information
** before SMP pairing gets going.
**
** Parameters bd_addr - The peer device
** *p_max_key_size - max key size local device supported.
** *p_init_key - initiator keys.
** *p_resp_key - responder keys.
**
** Returns void.
**
*******************************************************************************/
void bta_dm_co_le_io_key_req(BD_ADDR bd_addr, UINT8 *p_max_key_size,
tBTA_LE_KEY_TYPE *p_init_key,
tBTA_LE_KEY_TYPE *p_resp_key )
{
UNUSED(bd_addr);
BTIF_TRACE_ERROR("##################################");
BTIF_TRACE_ERROR("bta_dm_co_le_io_key_req: only setting max size to 16");
BTIF_TRACE_ERROR("##################################");
*p_max_key_size = 16;
*p_init_key = *p_resp_key =
(BTA_LE_KEY_PENC|BTA_LE_KEY_PID|BTA_LE_KEY_PCSRK|BTA_LE_KEY_LENC|BTA_LE_KEY_LID|BTA_LE_KEY_LCSRK);
}
/*******************************************************************************
**
** Function bta_dm_co_ble_local_key_reload
**
** Description This callout function is to load the local BLE keys if available
** on the device.
**
** Parameters none
**
** Returns void.
**
*******************************************************************************/
void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
{
#if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
BTIF_TRACE_DEBUG("##################################");
BTIF_TRACE_DEBUG("bta_dm_co_ble_load_local_keys: Load local keys if any are persisted");
BTIF_TRACE_DEBUG("##################################");
btif_dm_get_ble_local_keys( p_key_mask, er, p_id_keys);
#else
LOG_ERROR("bta_dm_co_ble_load_local_keys: func not ported\n");
#endif
}
/*******************************************************************************
**
** Function bta_dm_co_ble_io_req
**
** Description This callout function is executed by DM to get BLE IO capabilities
** before SMP pairing gets going.
**
** Parameters bd_addr - The peer device
** *p_io_cap - The local Input/Output capabilities
** *p_oob_data - TRUE, if OOB data is available for the peer device.
** *p_auth_req - Auth request setting (Bonding and MITM required or not)
** *p_max_key_size - max key size local device supported.
** *p_init_key - initiator keys.
** *p_resp_key - responder keys.
**
** Returns void.
**
*******************************************************************************/
void bta_dm_co_ble_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap,
tBTA_OOB_DATA *p_oob_data,
tBTA_LE_AUTH_REQ *p_auth_req,
UINT8 *p_max_key_size,
tBTA_LE_KEY_TYPE *p_init_key,
tBTA_LE_KEY_TYPE *p_resp_key )
{
UNUSED(bd_addr);
/* if OOB is not supported, this call-out function does not need to do anything
* otherwise, look for the OOB data associated with the address and set *p_oob_data accordingly
* If the answer can not be obtained right away,
* set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the answer is available */
*p_oob_data = FALSE;
/* *p_auth_req by default is FALSE for devices with NoInputNoOutput; TRUE for other devices. */
if (bte_appl_cfg.ble_auth_req)
*p_auth_req = bte_appl_cfg.ble_auth_req | (bte_appl_cfg.ble_auth_req & 0x04) | ((*p_auth_req) & 0x04);
if (bte_appl_cfg.ble_io_cap <=4)
*p_io_cap = bte_appl_cfg.ble_io_cap;
if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE)
*p_init_key = bte_appl_cfg.ble_init_key;
if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE)
*p_resp_key = bte_appl_cfg.ble_resp_key;
if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16)
*p_max_key_size = bte_appl_cfg.ble_max_key_size;
}
#endif

View file

@ -0,0 +1,217 @@
/******************************************************************************
*
* Copyright (C) 2009-2013 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifdef BT_SUPPORT_NVM
#include <stdio.h>
#include <unistd.h>
#endif /* BT_SUPPORT_NVM */
#include "gki.h"
#include "bta_gattc_co.h"
#include "bta_gattc_ci.h"
// #include "btif_util.h"
#include "btm_int.h"
#if( defined BLE_INCLUDED ) && (BLE_INCLUDED == TRUE)
#if( defined BTA_GATT_INCLUDED ) && (BTA_GATT_INCLUDED == TRUE)
#define GATT_CACHE_PREFIX "/data/misc/bluedroid/gatt_cache_"
#ifdef BT_SUPPORT_NVM
static FILE* sCacheFD = 0;
static void getFilename(char *buffer, BD_ADDR bda)
{
sprintf(buffer, "%s%02x%02x%02x%02x%02x%02x", GATT_CACHE_PREFIX
, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
}
static void cacheClose()
{
if (sCacheFD != 0)
{
fclose(sCacheFD);
sCacheFD = 0;
}
}
static bool cacheOpen(BD_ADDR bda, bool to_save)
{
char fname[255] = {0};
getFilename(fname, bda);
cacheClose();
sCacheFD = fopen(fname, to_save ? "w" : "r");
return (sCacheFD != 0);
}
static void cacheReset(BD_ADDR bda)
{
char fname[255] = {0};
getFilename(fname, bda);
unlink(fname);
}
#endif /* BT_SUPPORT_NVM */
/*****************************************************************************
** Function Declarations
*****************************************************************************/
/*******************************************************************************
**
** Function bta_gattc_co_cache_open
**
** Description This callout function is executed by GATTC when a GATT server
** cache is ready to be sent.
**
** Parameter server_bda: server bd address of this cache belongs to
** evt: call in event to be passed in when cache open is done.
** conn_id: connection ID of this cache operation attach to.
** to_save: open cache to save or to load.
**
** Returns void.
**
*******************************************************************************/
void bta_gattc_co_cache_open(BD_ADDR server_bda, UINT16 evt, UINT16 conn_id, BOOLEAN to_save)
{
#ifdef BT_SUPPORT_NVM
/* open NV cache and send call in */
tBTA_GATT_STATUS status = BTA_GATT_OK;
if (!btm_sec_is_a_bonded_dev(server_bda) || !cacheOpen(server_bda, to_save))
status = BTA_GATT_ERROR;
BTIF_TRACE_DEBUG("%s() - status=%d", __FUNCTION__, status);
bta_gattc_ci_cache_open(server_bda, evt, status, conn_id);
#else
bta_gattc_ci_cache_open(server_bda, evt, BTA_GATT_ERROR, conn_id);
#endif /* BT_SUPPORT_NVM */
}
/*******************************************************************************
**
** Function bta_gattc_co_cache_load
**
** Description This callout function is executed by GATT when server cache
** is required to load.
**
** Parameter server_bda: server bd address of this cache belongs to
** evt: call in event to be passed in when cache save is done.
** num_attr: number of attribute to be save.
** attr_index: starting attribute index of the save operation.
** conn_id: connection ID of this cache operation attach to.
** Returns
**
*******************************************************************************/
void bta_gattc_co_cache_load(BD_ADDR server_bda, UINT16 evt, UINT16 start_index, UINT16 conn_id)
{
UINT16 num_attr = 0;
tBTA_GATTC_NV_ATTR attr[BTA_GATTC_NV_LOAD_MAX];
tBTA_GATT_STATUS status = BTA_GATT_ERROR;
#ifdef BT_SUPPORT_NVM
if (sCacheFD && (0 == fseek(sCacheFD, start_index * sizeof(tBTA_GATTC_NV_ATTR), SEEK_SET)))
{
num_attr = fread(attr, sizeof(tBTA_GATTC_NV_ATTR), BTA_GATTC_NV_LOAD_MAX, sCacheFD);
status = (num_attr < BTA_GATTC_NV_LOAD_MAX ? BTA_GATT_OK : BTA_GATT_MORE);
}
BTIF_TRACE_DEBUG("%s() - sCacheFD=%p, start_index=%d, read=%d, status=%d",
__FUNCTION__, sCacheFD, start_index, num_attr, status);
#endif /* BT_SUPPORT_NVM */
bta_gattc_ci_cache_load(server_bda, evt, num_attr, attr, status, conn_id);
}
/*******************************************************************************
**
** Function bta_gattc_co_cache_save
**
** Description This callout function is executed by GATT when a server cache
** is available to save.
**
** Parameter server_bda: server bd address of this cache belongs to
** evt: call in event to be passed in when cache save is done.
** num_attr: number of attribute to be save.
** p_attr: pointer to the list of attributes to save.
** attr_index: starting attribute index of the save operation.
** conn_id: connection ID of this cache operation attach to.
** Returns
**
*******************************************************************************/
void bta_gattc_co_cache_save (BD_ADDR server_bda, UINT16 evt, UINT16 num_attr,
tBTA_GATTC_NV_ATTR *p_attr_list, UINT16 attr_index, UINT16 conn_id)
{
tBTA_GATT_STATUS status = BTA_GATT_OK;
UNUSED(attr_index);
#ifdef BT_SUPPORT_NVM
if (sCacheFD != 0)
{
int num = fwrite(p_attr_list, sizeof(tBTA_GATTC_NV_ATTR), num_attr, sCacheFD);
BTIF_TRACE_DEBUG("%s() wrote %d", __FUNCTION__, num);
}
#endif /* BT_SUPPORT_NVM */
bta_gattc_ci_cache_save(server_bda, evt, status, conn_id);
}
/*******************************************************************************
**
** Function bta_gattc_co_cache_close
**
** Description This callout function is executed by GATTC when a GATT server
** cache is written completely.
**
** Parameter server_bda: server bd address of this cache belongs to
** conn_id: connection ID of this cache operation attach to.
**
** Returns void.
**
*******************************************************************************/
void bta_gattc_co_cache_close(BD_ADDR server_bda, UINT16 conn_id)
{
UNUSED(server_bda);
UNUSED(conn_id);
#ifdef BT_SUPPORT_NVM
cacheClose();
#endif /* BT_SUPPORT_NVM */
/* close NV when server cache is done saving or loading,
does not need to do anything for now on Insight */
BTIF_TRACE_DEBUG("%s()", __FUNCTION__);
}
/*******************************************************************************
**
** Function bta_gattc_co_cache_reset
**
** Description This callout function is executed by GATTC to reset cache in
** application
**
** Parameter server_bda: server bd address of this cache belongs to
**
** Returns void.
**
*******************************************************************************/
void bta_gattc_co_cache_reset(BD_ADDR server_bda)
{
BTIF_TRACE_DEBUG("%s()", __FUNCTION__);
#ifdef BT_SUPPORT_NVM
cacheReset(server_bda);
#endif /* BT_SUPPORT_NVM */
}
#endif /* #if( defined BLE_INCLUDED ) && (BLE_INCLUDED == TRUE) */
#endif /* #if( defined BTA_GATT_INCLUDED ) && (BTA_GATT_INCLUDED == TRUE) */

View file

@ -0,0 +1,170 @@
/******************************************************************************
*
* Copyright (C) 2009-2013 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include "bta_api.h"
#if( defined BLE_INCLUDED ) && (BLE_INCLUDED == TRUE)
#if( defined BTA_GATT_INCLUDED ) && (BTA_GATT_INCLUDED == TRUE)
#include <stdlib.h>
#include <string.h>
#include "gki.h"
#include "bta_gatts_co.h"
// #include "btif_util.h"
#if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
/*****************************************************************************
** Local type definitions
*****************************************************************************/
#define BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE 50
typedef struct
{
BOOLEAN enable;
UINT8 num_clients;
tBTA_GATTS_SRV_CHG srv_chg[BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE];
} __attribute__((packed)) btif_gatts_srv_chg_cb_t;
/*****************************************************************************
** Static variables
*****************************************************************************/
static btif_gatts_srv_chg_cb_t btif_gatts_srv_chg_cb;
/*****************************************************************************
** Static functions
*****************************************************************************/
static void btif_gatts_check_init(void)
{
btif_gatts_srv_chg_cb_t *p_cb= &btif_gatts_srv_chg_cb;
if (!p_cb->enable)
{
memset(p_cb, 0, sizeof(btif_gatts_srv_chg_cb_t));
p_cb->enable = TRUE;
}
}
/*****************************************************************************
** Externally called functions
*****************************************************************************/
void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda)
{
btif_gatts_srv_chg_cb_t *p_cb= &btif_gatts_srv_chg_cb;
BOOLEAN found = FALSE;
UINT8 i;
btif_gatts_check_init();
for (i=0; i != p_cb->num_clients; ++i)
{
if (!memcmp(p_cb->srv_chg[i].bda, bda, sizeof(BD_ADDR)))
{
found = TRUE;
break;
}
}
if (!found)
{
if (p_cb->num_clients < BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE)
{
bdcpy(p_cb->srv_chg[p_cb->num_clients].bda, bda);
p_cb->srv_chg[p_cb->num_clients].srv_changed = FALSE;
p_cb->num_clients++;
}
}
}
#endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
/*****************************************************************************
** Call-out functions
*****************************************************************************/
/*******************************************************************************
**
** Function bta_gatts_co_update_handle_range
**
** Description This callout function is executed by GATTS when a GATT server
** handle range ios to be added or removed.
**
** Parameter is_add: true is to add a handle range; otherwise is to delete.
** p_hndl_range: handle range.
**
** Returns void.
**
*******************************************************************************/
void bta_gatts_co_update_handle_range(BOOLEAN is_add, tBTA_GATTS_HNDL_RANGE *p_hndl_range)
{
UNUSED(is_add);
UNUSED(p_hndl_range);
}
/*******************************************************************************
**
** Function bta_gatts_co_srv_chg
**
** Description This call-out is to read/write/remove service change related
** informaiton. The request consists of the cmd and p_req and the
** response is returned in p_rsp
**
** Parameter cmd - request command
** p_req - request paramters
** p_rsp - response data for the request
**
** Returns TRUE - if the request is processed successfully and
** the response is returned in p_rsp.
** FASLE - if the request can not be processed
**
*******************************************************************************/
BOOLEAN bta_gatts_co_srv_chg(tBTA_GATTS_SRV_CHG_CMD cmd,
tBTA_GATTS_SRV_CHG_REQ *p_req,
tBTA_GATTS_SRV_CHG_RSP *p_rsp)
{
UNUSED(cmd);
UNUSED(p_req);
UNUSED(p_rsp);
return FALSE;
}
/*******************************************************************************
**
** Function bta_gatts_co_load_handle_range
**
** Description This callout function is executed by GATTS when a GATT server
** handle range is requested to be loaded from NV.
**
** Parameter
**
** Returns void.
**
*******************************************************************************/
BOOLEAN bta_gatts_co_load_handle_range(UINT8 index,
tBTA_GATTS_HNDL_RANGE *p_handle_range)
{
UNUSED(index);
UNUSED(p_handle_range);
return FALSE;
}
#endif
#endif

View file

@ -0,0 +1,589 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <stdbool.h>
#include "bt_trace.h"
#include "bdaddr.h"
#include "bt_types.h"
#include "controller.h"
#include "event_mask.h"
#include "hcimsgs.h"
#include "hci_layer.h"
#include "hci_packet_factory.h"
#include "hci_packet_parser.h"
#include "btm_ble_api.h"
#include "version.h"
//#include "bluedroid_test.h" /*FOr Test Case*/
const bt_event_mask_t BLE_EVENT_MASK = { "\x00\x00\x00\x00\x00\x00\x06\x7f" };
#if (BLE_INCLUDED)
const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_DUMO_EVENT_MASK_EXT };
#else
const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_LISBON_EVENT_MASK_EXT };
#endif
// TODO(zachoverflow): factor out into common module
const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
#define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
#define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
#define BLE_SUPPORTED_STATES_SIZE 8
#define BLE_SUPPORTED_FEATURES_SIZE 8
static const hci_t *hci;
static const hci_packet_factory_t *packet_factory;
static const hci_packet_parser_t *packet_parser;
static bt_bdaddr_t address;
static bt_version_t bt_version;
static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
static uint8_t last_features_classic_page_index;
static uint16_t acl_data_size_classic;
static uint16_t acl_data_size_ble;
static uint16_t acl_buffer_count_classic;
static uint8_t acl_buffer_count_ble;
static uint8_t ble_white_list_size;
static uint8_t ble_resolving_list_max_size;
static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
static bt_device_features_t features_ble;
static uint16_t ble_suggested_default_data_length;
static bool readable;
static bool ble_supported;
static bool simple_pairing_supported;
static bool secure_connections_supported;
devctl_reset_callback reset_cb;
static uint8_t page_number = 0;
static void devctl_hdl_cmd_complete(BT_HDR *response, void *context) {
BT_HDR *command = NULL;
command_opcode_t opcode;
uint8_t *stream = response->data + response->offset;
STREAM_SKIP_UINT16(stream); //skip event_code and total length field
STREAM_SKIP_UINT8(stream); //skip command_credits field
STREAM_TO_UINT16(opcode, stream);
switch (opcode) {
case HCI_RESET:
packet_parser->parse_generic_command_complete(response);
command = packet_factory->make_read_buffer_size();
break;
case HCI_READ_BUFFER_SIZE:
packet_parser->parse_read_buffer_size_response(
response, &acl_data_size_classic, &acl_buffer_count_classic);
command = packet_factory->make_host_buffer_size(
L2CAP_MTU_SIZE, SCO_HOST_BUFFER_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10);
break;
case HCI_HOST_BUFFER_SIZE:
packet_parser->parse_generic_command_complete(response);
command = packet_factory->make_read_local_version_info();
break;
case HCI_READ_LOCAL_VERSION_INFO:
packet_parser->parse_read_local_version_info_response(response, &bt_version);
command = packet_factory->make_read_bd_addr();
break;
case HCI_READ_BD_ADDR:
packet_parser->parse_read_bd_addr_response(response, &address);
command = packet_factory->make_read_local_supported_commands();
break;
case HCI_READ_LOCAL_SUPPORTED_CMDS:
packet_parser->parse_read_local_supported_commands_response(
response, supported_commands, HCI_SUPPORTED_COMMANDS_ARRAY_SIZE);
page_number = 0;
command = packet_factory->make_read_local_extended_features(page_number);
break;
case HCI_READ_LOCAL_EXT_FEATURES:
if (response) {
packet_parser->parse_read_local_extended_features_response(
response, &page_number,&last_features_classic_page_index,
features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
response = NULL;
page_number++;
}
if (1 == page_number) {
simple_pairing_supported = HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array);
if (simple_pairing_supported) {
command = packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED);
break;
}
// BLOCK_BEGIN
#if (BLE_INCLUDED == TRUE)
if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
uint8_t simultaneous_le_host = HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array) ? BTM_BLE_SIMULTANEOUS_HOST : 0;
command = packet_factory->make_ble_write_host_support(BTM_BLE_HOST_SUPPORT, simultaneous_le_host);
break;
}
#endif
}
if (page_number <= last_features_classic_page_index &&
page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
command = packet_factory->make_read_local_extended_features(page_number);
break;
} else {
#if (SC_MODE_INCLUDED == TRUE)
secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
if (secure_connections_supported) {
command = packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED);
break;
}
#endif
#if (BLE_INCLUDED == TRUE)
ble_supported = last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
if (ble_supported) {
// Request the ble white list size next
command = packet_factory->make_ble_read_white_list_size();
break;
}
#endif
if (simple_pairing_supported) {
command = packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK);
break;
}
}
// BLOCK_END
case HCI_WRITE_SIMPLE_PAIRING_MODE:
if (response) {
packet_parser->parse_generic_command_complete(response);
response = NULL;
}
// BLOCK_BEGIN
#if (BLE_INCLUDED == TRUE)
if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
uint8_t simultaneous_le_host = HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array) ? BTM_BLE_SIMULTANEOUS_HOST : 0;
command = packet_factory->make_ble_write_host_support(BTM_BLE_HOST_SUPPORT, simultaneous_le_host);
break;
}
#endif
if (page_number <= last_features_classic_page_index &&
page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
command = packet_factory->make_read_local_extended_features(page_number);
break;
} else {
#if (SC_MODE_INCLUDED == TRUE)
secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
if (secure_connections_supported) {
command = packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED);
break;
}
#endif
#if (BLE_INCLUDED == TRUE)
ble_supported = last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
if (ble_supported) {
// Request the ble white list size next
command = packet_factory->make_ble_read_white_list_size();
break;
}
#endif
if (simple_pairing_supported) {
command = packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK);
break;
}
}
#if (SC_MODE_INCLUDED == TRUE)
case HCI_WRITE_SECURE_CONNS_SUPPORT:
if (response) {
packet_parser->parse_generic_command_complete(response);
response = NULL;
}
#if (BLE_INCLUDED == TRUE)
ble_supported = last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
if (ble_supported) {
// Request the ble white list size next
command = packet_factory->make_ble_read_white_list_size();
break;
}
#endif /* (BLE_INCLUDED == TRUE) */
if (simple_pairing_supported) {
command = packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK);
break;
}
#endif /* (SC_MODE_INCLUDED == TRUE) */
#if (BLE_INCLUDED == TRUE)
case HCI_WRITE_LE_HOST_SUPPORT:
if (response) {
packet_parser->parse_generic_command_complete(response);
response = NULL;
}
if (page_number <= last_features_classic_page_index &&
page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
command = packet_factory->make_read_local_extended_features(page_number);
break;
} else {
#if (SC_MODE_INCLUDED == TRUE)
secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
if (secure_connections_supported) {
command = packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED);
break;
}
#endif
ble_supported = last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
if (ble_supported) {
// Request the ble white list size next
command = packet_factory->make_ble_read_white_list_size();
break;
}
if (simple_pairing_supported) {
command = packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK);
break;
}
}
case HCI_BLE_READ_WHITE_LIST_SIZE:
if (response) {
packet_parser->parse_ble_read_white_list_size_response(response, &ble_white_list_size);
response = NULL;
}
if (ble_supported) {
// Request the ble buffer size next
command = packet_factory->make_ble_read_buffer_size();
break;
}
// Fall Through if no next command generated
case HCI_BLE_READ_BUFFER_SIZE:
if (response) {
packet_parser->parse_ble_read_buffer_size_response(
response, &acl_data_size_ble, &acl_buffer_count_ble);
response = NULL;
}
// Response of 0 indicates ble has the same buffer size as classic
if (acl_data_size_ble == 0)
acl_data_size_ble = acl_data_size_classic;
if (ble_supported) {
// Request the ble supported states next
command = packet_factory->make_ble_read_supported_states();
break;
}
// Fall Through if no next command generated
case HCI_BLE_READ_SUPPORTED_STATES:
if (response) {
packet_parser->parse_ble_read_supported_states_response(
response, ble_supported_states, sizeof(ble_supported_states));
response = NULL;
}
if (ble_supported) {
// Request the ble supported features next
command = packet_factory->make_ble_read_local_supported_features();
break;
}
// Fall Through if no next command generated
case HCI_BLE_READ_LOCAL_SPT_FEAT:
if (response) {
packet_parser->parse_ble_read_local_supported_features_response(
response, &features_ble);
response = NULL;
}
if (ble_supported &&
HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
command = packet_factory->make_ble_read_resolving_list_size();
break;
}
case HCI_BLE_READ_RESOLVING_LIST_SIZE:
if (response) {
packet_parser->parse_ble_read_resolving_list_size_response(
response, &ble_resolving_list_max_size);
response = NULL;
}
if (ble_supported &&
HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
command = packet_factory->make_ble_read_suggested_default_data_length();
break;
}
case HCI_BLE_READ_DEFAULT_DATA_LENGTH:
if (response) {
packet_parser->parse_ble_read_suggested_default_data_length_response(
response, &ble_suggested_default_data_length);
response = NULL;
}
if (ble_supported) {
// Set the ble event mask next
command = packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK);
break;
}
case HCI_BLE_SET_EVENT_MASK:
if (response) {
packet_parser->parse_generic_command_complete(response);
response = NULL;
}
if (simple_pairing_supported) {
command = packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK);
break;
}
#endif
case HCI_SET_EVENT_MASK:
if (response) {
packet_parser->parse_generic_command_complete(response);
response = command = NULL;
}
//At this point, Reset Thread should be completed well.
readable = true;
page_number = 0;
if (reset_cb)
reset_cb();
break;
default:
LOG_ERROR("%s: No available opcode matched.", __func__);
break;
}
if (command)
hci->transmit_command(command, devctl_hdl_cmd_complete, NULL, NULL);
}
// Interface functions
static void devctl_reset(devctl_reset_callback reset_callback) {
reset_cb = reset_callback;
BT_HDR *command = packet_factory->make_read_buffer_size();
//BT_HDR *command = packet_factory->make_reset();
LOG_ERROR("Device Control Send Device Read Buffer Size Command\n");
page_number = 0;
if (command)
hci->transmit_command(command, devctl_hdl_cmd_complete, NULL, NULL);
}
static void devctl_shutdown(void) {
reset_cb = NULL;
readable = false;
}
static bool get_is_ready(void) {
return readable;
}
static const bt_bdaddr_t *get_address(void) {
assert(readable);
return &address;
}
static const bt_version_t *get_bt_version(void) {
assert(readable);
return &bt_version;
}
// TODO(zachoverflow): hide inside, move decoder inside too
static const bt_device_features_t *get_features_classic(int index) {
assert(readable);
assert(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
return &features_classic[index];
}
static uint8_t get_last_features_classic_index(void) {
assert(readable);
return last_features_classic_page_index;
}
static const bt_device_features_t *get_features_ble(void) {
assert(readable);
assert(ble_supported);
return &features_ble;
}
static const uint8_t *get_ble_supported_states(void) {
assert(readable);
assert(ble_supported);
return ble_supported_states;
}
static bool supports_simple_pairing(void) {
assert(readable);
return simple_pairing_supported;
}
static bool supports_secure_connections(void) {
assert(readable);
return secure_connections_supported;
}
static bool supports_simultaneous_le_bredr(void) {
assert(readable);
return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
}
static bool supports_reading_remote_extended_features(void) {
assert(readable);
return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
}
static bool supports_interlaced_inquiry_scan(void) {
assert(readable);
return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
}
static bool supports_rssi_with_inquiry_results(void) {
assert(readable);
return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
}
static bool supports_extended_inquiry_response(void) {
assert(readable);
return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
}
static bool supports_master_slave_role_switch(void) {
assert(readable);
return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
}
static bool supports_ble(void) {
assert(readable);
return ble_supported;
}
static bool supports_ble_privacy(void) {
assert(readable);
assert(ble_supported);
return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
}
static bool supports_ble_packet_extension(void) {
assert(readable);
assert(ble_supported);
return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
}
static bool supports_ble_connection_parameters_request(void) {
assert(readable);
assert(ble_supported);
return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
}
static uint16_t get_acl_data_size_classic(void) {
assert(readable);
return acl_data_size_classic;
}
static uint16_t get_acl_data_size_ble(void) {
assert(readable);
assert(ble_supported);
return acl_data_size_ble;
}
static uint16_t get_acl_packet_size_classic(void) {
assert(readable);
return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
}
static uint16_t get_acl_packet_size_ble(void) {
assert(readable);
return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
}
static uint16_t get_ble_suggested_default_data_length(void) {
assert(readable);
assert(ble_supported);
return ble_suggested_default_data_length;
}
static uint16_t get_acl_buffer_count_classic(void) {
assert(readable);
return acl_buffer_count_classic;
}
static uint8_t get_acl_buffer_count_ble(void) {
assert(readable);
assert(ble_supported);
return acl_buffer_count_ble;
}
static uint8_t get_ble_white_list_size(void) {
assert(readable);
assert(ble_supported);
return ble_white_list_size;
}
static uint8_t get_ble_resolving_list_max_size(void) {
assert(readable);
assert(ble_supported);
return ble_resolving_list_max_size;
}
static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
assert(readable);
assert(ble_supported);
ble_resolving_list_max_size = resolving_list_max_size;
}
static const controller_t interface = {
devctl_reset,
devctl_shutdown,
get_is_ready,
get_address,
get_bt_version,
get_features_classic,
get_last_features_classic_index,
get_features_ble,
get_ble_supported_states,
supports_simple_pairing,
supports_secure_connections,
supports_simultaneous_le_bredr,
supports_reading_remote_extended_features,
supports_interlaced_inquiry_scan,
supports_rssi_with_inquiry_results,
supports_extended_inquiry_response,
supports_master_slave_role_switch,
supports_ble,
supports_ble_packet_extension,
supports_ble_connection_parameters_request,
supports_ble_privacy,
get_acl_data_size_classic,
get_acl_data_size_ble,
get_acl_packet_size_classic,
get_acl_packet_size_ble,
get_ble_suggested_default_data_length,
get_acl_buffer_count_classic,
get_acl_buffer_count_ble,
get_ble_white_list_size,
get_ble_resolving_list_max_size,
set_ble_resolving_list_max_size
};
const controller_t *controller_get_interface() {
static bool loaded = false;
if (!loaded) {
loaded = true;
hci = hci_layer_get_interface();
packet_factory = hci_packet_factory_get_interface();
packet_parser = hci_packet_parser_get_interface();
}
return &interface;
}

View file

@ -0,0 +1,87 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _CONTROLLER_H_
#define _CONTROLLER_H_
#include <stdbool.h>
#include <stdint.h>
#include "bt_target.h"
#include "bdaddr.h"
#include "device_features.h"
#include "hci_layer.h"
#include "hci_packet_factory.h"
#include "hci_packet_parser.h"
typedef void (*devctl_reset_callback)(void);
typedef struct controller_t {
void (*devctl_reset)(devctl_reset_callback reset_callback);
void (*devctl_shutdown)(void);
bool (*get_is_ready)(void);
const bt_bdaddr_t *(*get_address)(void);
const bt_version_t *(*get_bt_version)(void);
const bt_device_features_t *(*get_features_classic)(int index);
uint8_t (*get_last_features_classic_index)(void);
const bt_device_features_t *(*get_features_ble)(void);
const uint8_t *(*get_ble_supported_states)(void);
bool (*supports_simple_pairing)(void);
bool (*supports_secure_connections)(void);
bool (*supports_simultaneous_le_bredr)(void);
bool (*supports_reading_remote_extended_features)(void);
bool (*supports_interlaced_inquiry_scan)(void);
bool (*supports_rssi_with_inquiry_results)(void);
bool (*supports_extended_inquiry_response)(void);
bool (*supports_master_slave_role_switch)(void);
bool (*supports_ble)(void);
bool (*supports_ble_packet_extension)(void);
bool (*supports_ble_connection_parameters_request)(void);
bool (*supports_ble_privacy)(void);
// Get the cached acl data sizes for the controller.
uint16_t (*get_acl_data_size_classic)(void);
uint16_t (*get_acl_data_size_ble)(void);
// Get the cached acl packet sizes for the controller.
// This is a convenience function for the respective
// acl data size + size of the acl header.
uint16_t (*get_acl_packet_size_classic)(void);
uint16_t (*get_acl_packet_size_ble)(void);
uint16_t (*get_ble_default_data_packet_length)(void);
// Get the number of acl packets the controller can buffer.
uint16_t (*get_acl_buffer_count_classic)(void);
uint8_t (*get_acl_buffer_count_ble)(void);
uint8_t (*get_ble_white_list_size)(void);
uint8_t (*get_ble_resolving_list_max_size)(void);
void (*set_ble_resolving_list_max_size)(int resolving_list_max_size);
} controller_t;
const controller_t *controller_get_interface();
#endif /*_CONTROLLER_H_*/

View file

@ -0,0 +1,44 @@
/******************************************************************************
*
* Copyright (C) 2015 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _INTEROP_H_
#define _INTEROP_H_
#include <stdbool.h>
#include "bt_defs.h"
typedef enum {
// Disable secure connections
// This is for pre BT 4.1/2 devices that do not handle secure mode
// very well.
INTEROP_DISABLE_LE_SECURE_CONNECTIONS,
// Some devices have proven problematic during the pairing process, often
// requiring multiple retries to complete pairing. To avoid degrading the user
// experience for those devices, automatically re-try pairing if page
// timeouts are received during pairing.
INTEROP_AUTO_RETRY_PAIRING
} interop_feature_t;
// Check if a given |addr| matches a known interoperability workaround as identified
// by the |interop_feature_t| enum. This API is used for simple address based lookups
// where more information is not available. No look-ups or random address resolution
// is performed on |addr|.
bool interop_match(const interop_feature_t feature, const bt_bdaddr_t *addr);
#endif /*_INTEROP_H_*/

View file

@ -0,0 +1,50 @@
/******************************************************************************
*
* Copyright (C) 2015 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _INTEROP_DATABASE_H_
#define _INTEROP_DATABASE_H_
#include "interop.h"
typedef struct {
bt_bdaddr_t addr;
uint8_t len;
interop_feature_t feature;
} interop_entry_t;
static const interop_entry_t interop_database[] = {
// Nexus Remote (Spike)
// Note: May affect other Asus brand devices
{{0x08, 0x62, 0x66, 0,0,0}, 3, INTEROP_DISABLE_LE_SECURE_CONNECTIONS},
{{0x38, 0x2c, 0x4a, 0xc9, 0,0}, 4, INTEROP_DISABLE_LE_SECURE_CONNECTIONS},
{{0x38, 0x2c, 0x4a, 0xe6, 0,0}, 4, INTEROP_DISABLE_LE_SECURE_CONNECTIONS},
{{0x54, 0xa0, 0x50, 0xd9, 0,0}, 4, INTEROP_DISABLE_LE_SECURE_CONNECTIONS},
{{0xac, 0x9e, 0x17, 0,0,0}, 3, INTEROP_DISABLE_LE_SECURE_CONNECTIONS},
{{0xf0, 0x79, 0x59, 0,0,0}, 3, INTEROP_DISABLE_LE_SECURE_CONNECTIONS},
// Motorola Key Link
{{0x1c, 0x96, 0x5a, 0,0,0}, 3, INTEROP_DISABLE_LE_SECURE_CONNECTIONS},
// Flic smart button
{{0x80, 0xe4, 0xda, 0x70, 0,0}, 4, INTEROP_DISABLE_LE_SECURE_CONNECTIONS},
// BMW car kits (Harman/Becker)
{{0x9c, 0xdf, 0x03, 0,0,0}, 3, INTEROP_AUTO_RETRY_PAIRING}
};
#endif /*_INTEROP_DATABASE_H_*/

View file

@ -0,0 +1,56 @@
/******************************************************************************
*
* Copyright (C) 2015 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/*
#define LOG_TAG "bt_device_interop"
*/
#include <string.h> // For memcmp
#include "bt_trace.h"
#include "bdaddr.h"
#include "interop.h"
#include "interop_database.h"
#define CASE_RETURN_STR(const) case const: return #const;
static const char* interop_feature_string(const interop_feature_t feature) {
switch (feature) {
CASE_RETURN_STR(INTEROP_DISABLE_LE_SECURE_CONNECTIONS)
CASE_RETURN_STR(INTEROP_AUTO_RETRY_PAIRING)
}
return "UNKNOWN";
}
// Interface functions
bool interop_match(const interop_feature_t feature, const bt_bdaddr_t *addr) {
assert(addr);
const size_t db_size = sizeof(interop_database) / sizeof(interop_entry_t);
for (size_t i = 0; i != db_size; ++i) {
if (feature == interop_database[i].feature &&
memcmp(addr, &interop_database[i].addr, interop_database[i].len) == 0) {
char bdstr[20] = {0};
LOG_WARN("%s() Device %s is a match for interop workaround %s", __func__,
bdaddr_to_string(addr, bdstr, sizeof(bdstr)), interop_feature_string(feature));
return true;
}
}
return false;
}

View file

@ -0,0 +1,563 @@
/******************************************************************************
*
* Copyright (C) 1999-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include "bt_trace.h"
#include "allocator.h"
#include "gki_int.h"
#define ALIGN_POOL(pl_size) ( (((pl_size) + 3) / sizeof(UINT32)) * sizeof(UINT32))
#define BUFFER_HDR_SIZE (sizeof(BUFFER_HDR_T)) /* Offset past header */
#define BUFFER_PADDING_SIZE (sizeof(BUFFER_HDR_T) + sizeof(UINT32)) /* Header + Magic Number */
#define MAGIC_NO 0xDDBADDBA
#define BUF_STATUS_FREE 0
#define BUF_STATUS_UNLINKED 1
#define BUF_STATUS_QUEUED 2
/*******************************************************************************
**
** Function gki_init_free_queue
**
** Description Internal function called at startup to initialize a free
** queue. It is called once for each free queue.
**
** Returns void
**
*******************************************************************************/
static void gki_init_free_queue (UINT8 id, UINT16 size, UINT16 total, void *p_mem)
{
UINT16 i;
UINT16 act_size;
BUFFER_HDR_T *hdr;
BUFFER_HDR_T *hdr1 = NULL;
UINT32 *magic;
INT32 tempsize = size;
tGKI_COM_CB *p_cb = &gki_cb.com;
/* Ensure an even number of longwords */
tempsize = (INT32)ALIGN_POOL(size);
act_size = (UINT16)(tempsize + BUFFER_PADDING_SIZE);
/* Remember pool start and end addresses */
if(p_mem)
{
p_cb->pool_start[id] = (UINT8 *)p_mem;
p_cb->pool_end[id] = (UINT8 *)p_mem + (act_size * total);
}
p_cb->pool_size[id] = act_size;
p_cb->freeq[id].size = (UINT16) tempsize;
p_cb->freeq[id].total = total;
p_cb->freeq[id].cur_cnt = 0;
p_cb->freeq[id].max_cnt = 0;
/* Initialize index table */
if(p_mem)
{
hdr = (BUFFER_HDR_T *)p_mem;
p_cb->freeq[id]._p_first = hdr;
for (i = 0; i < total; i++)
{
hdr->q_id = id;
hdr->status = BUF_STATUS_FREE;
magic = (UINT32 *)((UINT8 *)hdr + BUFFER_HDR_SIZE + tempsize);
*magic = MAGIC_NO;
hdr1 = hdr;
hdr = (BUFFER_HDR_T *)((UINT8 *)hdr + act_size);
hdr1->p_next = hdr;
}
hdr1->p_next = NULL;
p_cb->freeq[id]._p_last = hdr1;
}
}
void gki_buffer_cleanup(void)
{
UINT8 i;
tGKI_COM_CB *p_cb = &gki_cb.com;
for (i=0; i < GKI_NUM_FIXED_BUF_POOLS; i++)
{
if ( 0 < p_cb->freeq[i].max_cnt )
{
osi_free(p_cb->pool_start[i]);
p_cb->freeq[i].cur_cnt = 0;
p_cb->freeq[i].max_cnt = 0;
p_cb->freeq[i]._p_first = NULL;
p_cb->freeq[i]._p_last = NULL;
p_cb->pool_start[i] = NULL;
p_cb->pool_end[i] = NULL;
p_cb->pool_size[i] = 0;
}
}
}
/*******************************************************************************
**
** Function gki_buffer_init
**
** Description Called once internally by GKI at startup to initialize all
** buffers and free buffer pools.
**
** Returns void
**
*******************************************************************************/
void gki_buffer_init(void)
{
static const struct {
uint16_t size;
uint16_t count;
} buffer_info[GKI_NUM_FIXED_BUF_POOLS] = {
{ GKI_BUF0_SIZE, GKI_BUF0_MAX },
{ GKI_BUF1_SIZE, GKI_BUF1_MAX },
{ GKI_BUF2_SIZE, GKI_BUF2_MAX },
{ GKI_BUF3_SIZE, GKI_BUF3_MAX },
{ GKI_BUF4_SIZE, GKI_BUF4_MAX },
{ GKI_BUF5_SIZE, GKI_BUF5_MAX },
{ GKI_BUF6_SIZE, GKI_BUF6_MAX },
{ GKI_BUF7_SIZE, GKI_BUF7_MAX },
{ GKI_BUF8_SIZE, GKI_BUF8_MAX },
{ GKI_BUF9_SIZE, GKI_BUF9_MAX },
};
tGKI_COM_CB *p_cb = &gki_cb.com;
for (int i = 0; i < GKI_NUM_TOTAL_BUF_POOLS; i++)
{
p_cb->pool_start[i] = NULL;
p_cb->pool_end[i] = NULL;
p_cb->pool_size[i] = 0;
p_cb->freeq[i]._p_first = 0;
p_cb->freeq[i]._p_last = 0;
p_cb->freeq[i].size = 0;
p_cb->freeq[i].total = 0;
p_cb->freeq[i].cur_cnt = 0;
p_cb->freeq[i].max_cnt = 0;
}
/* Use default from target.h */
p_cb->pool_access_mask = GKI_DEF_BUFPOOL_PERM_MASK;
for (int i = 0; i < GKI_NUM_FIXED_BUF_POOLS; ++i) {
gki_init_free_queue(i, buffer_info[i].size, buffer_info[i].count, NULL);
}
}
/*******************************************************************************
**
** Function GKI_init_q
**
** Description Called by an application to initialize a buffer queue.
**
** Returns void
**
*******************************************************************************/
void GKI_init_q (BUFFER_Q *p_q)
{
p_q->_p_first = p_q->_p_last = NULL;
p_q->_count = 0;
}
/*******************************************************************************
**
** Function GKI_getbuf
**
** Description Called by an application to get a free buffer which
** is of size greater or equal to the requested size.
**
** Note: This routine only takes buffers from public pools.
** It will not use any buffers from pools
** marked GKI_RESTRICTED_POOL.
**
** Parameters size - (input) number of bytes needed.
**
** Returns A pointer to the buffer, or NULL if none available
**
*******************************************************************************/
void *GKI_getbuf (UINT16 size)
{
BUFFER_HDR_T *header = osi_malloc(size + BUFFER_HDR_SIZE);
header->status = BUF_STATUS_UNLINKED;
header->p_next = NULL;
header->Type = 0;
header->size = size;
return header + 1;
}
/*******************************************************************************
**
** Function GKI_getpoolbuf
**
** Description Called by an application to get a free buffer from
** a specific buffer pool.
**
** Note: If there are no more buffers available from the pool,
** the public buffers are searched for an available buffer.
**
** Parameters pool_id - (input) pool ID to get a buffer out of.
**
** Returns A pointer to the buffer, or NULL if none available
**
*******************************************************************************/
void *GKI_getpoolbuf (UINT8 pool_id)
{
return GKI_getbuf(gki_cb.com.pool_size[pool_id]);
}
/*******************************************************************************
**
** Function GKI_freebuf
**
** Description Called by an application to return a buffer to the free pool.
**
** Parameters p_buf - (input) address of the beginning of a buffer.
**
** Returns void
**
*******************************************************************************/
void GKI_freebuf (void *p_buf)
{
osi_free((BUFFER_HDR_T *)p_buf - 1);
}
/*******************************************************************************
**
** Function GKI_get_buf_size
**
** Description Called by an application to get the size of a buffer.
**
** Parameters p_buf - (input) address of the beginning of a buffer.
**
** Returns the size of the buffer
**
*******************************************************************************/
UINT16 GKI_get_buf_size (void *p_buf)
{
BUFFER_HDR_T *header = (BUFFER_HDR_T *)p_buf - 1;
return header->size;
}
/*******************************************************************************
**
** Function GKI_enqueue
**
** Description Enqueue a buffer at the tail of the queue
**
** Parameters: p_q - (input) pointer to a queue.
** p_buf - (input) address of the buffer to enqueue
**
** Returns void
**
*******************************************************************************/
void GKI_enqueue (BUFFER_Q *p_q, void *p_buf)
{
BUFFER_HDR_T *p_hdr = (BUFFER_HDR_T *) ((UINT8 *) p_buf - BUFFER_HDR_SIZE);
assert(p_hdr->status == BUF_STATUS_UNLINKED);
GKI_disable();
/* Since the queue is exposed (C vs C++), keep the pointers in exposed format */
if (p_q->_p_last)
{
BUFFER_HDR_T *_p_last_hdr = (BUFFER_HDR_T *)((UINT8 *)p_q->_p_last - BUFFER_HDR_SIZE);
_p_last_hdr->p_next = p_hdr;
}
else
p_q->_p_first = p_buf;
p_q->_p_last = p_buf;
p_q->_count++;
p_hdr->p_next = NULL;
p_hdr->status = BUF_STATUS_QUEUED;
GKI_enable();
}
/*******************************************************************************
**
** Function GKI_dequeue
**
** Description Dequeues a buffer from the head of a queue
**
** Parameters: p_q - (input) pointer to a queue.
**
** Returns NULL if queue is empty, else buffer
**
*******************************************************************************/
void *GKI_dequeue (BUFFER_Q *p_q)
{
BUFFER_HDR_T *p_hdr;
GKI_disable();
if (!p_q || !p_q->_count)
{
GKI_enable();
return (NULL);
}
p_hdr = (BUFFER_HDR_T *)((UINT8 *)p_q->_p_first - BUFFER_HDR_SIZE);
/* Keep buffers such that GKI header is invisible
*/
if (p_hdr->p_next)
p_q->_p_first = ((UINT8 *)p_hdr->p_next + BUFFER_HDR_SIZE);
else
{
p_q->_p_first = NULL;
p_q->_p_last = NULL;
}
p_q->_count--;
p_hdr->p_next = NULL;
p_hdr->status = BUF_STATUS_UNLINKED;
GKI_enable();
return ((UINT8 *)p_hdr + BUFFER_HDR_SIZE);
}
/*******************************************************************************
**
** Function GKI_remove_from_queue
**
** Description Dequeue a buffer from the middle of the queue
**
** Parameters: p_q - (input) pointer to a queue.
** p_buf - (input) address of the buffer to enqueue
**
** Returns NULL if queue is empty, else buffer
**
*******************************************************************************/
void *GKI_remove_from_queue (BUFFER_Q *p_q, void *p_buf)
{
BUFFER_HDR_T *p_prev;
BUFFER_HDR_T *p_buf_hdr;
GKI_disable();
if (p_buf == p_q->_p_first)
{
GKI_enable();
return (GKI_dequeue (p_q));
}
p_buf_hdr = (BUFFER_HDR_T *)((UINT8 *)p_buf - BUFFER_HDR_SIZE);
p_prev = (BUFFER_HDR_T *)((UINT8 *)p_q->_p_first - BUFFER_HDR_SIZE);
for ( ; p_prev; p_prev = p_prev->p_next)
{
/* If the previous points to this one, move the pointers around */
if (p_prev->p_next == p_buf_hdr)
{
p_prev->p_next = p_buf_hdr->p_next;
/* If we are removing the last guy in the queue, update _p_last */
if (p_buf == p_q->_p_last)
p_q->_p_last = p_prev + 1;
/* One less in the queue */
p_q->_count--;
/* The buffer is now unlinked */
p_buf_hdr->p_next = NULL;
p_buf_hdr->status = BUF_STATUS_UNLINKED;
GKI_enable();
return (p_buf);
}
}
GKI_enable();
return (NULL);
}
/*******************************************************************************
**
** Function GKI_getfirst
**
** Description Return a pointer to the first buffer in a queue
**
** Parameters: p_q - (input) pointer to a queue.
**
** Returns NULL if queue is empty, else buffer address
**
*******************************************************************************/
void *GKI_getfirst (BUFFER_Q *p_q)
{
return (p_q->_p_first);
}
/*******************************************************************************
**
** Function GKI_getlast
**
** Description Return a pointer to the last buffer in a queue
**
** Parameters: p_q - (input) pointer to a queue.
**
** Returns NULL if queue is empty, else buffer address
**
*******************************************************************************/
void *GKI_getlast (BUFFER_Q *p_q)
{
return (p_q->_p_last);
}
/*******************************************************************************
**
** Function GKI_getnext
**
** Description Return a pointer to the next buffer in a queue
**
** Parameters: p_buf - (input) pointer to the buffer to find the next one from.
**
** Returns NULL if no more buffers in the queue, else next buffer address
**
*******************************************************************************/
void *GKI_getnext (void *p_buf)
{
BUFFER_HDR_T *p_hdr;
p_hdr = (BUFFER_HDR_T *) ((UINT8 *) p_buf - BUFFER_HDR_SIZE);
if (p_hdr->p_next)
return ((UINT8 *)p_hdr->p_next + BUFFER_HDR_SIZE);
else
return (NULL);
}
/*******************************************************************************
**
** Function GKI_queue_is_empty
**
** Description Check the status of a queue.
**
** Parameters: p_q - (input) pointer to a queue.
**
** Returns TRUE if queue is empty, else FALSE
**
*******************************************************************************/
BOOLEAN GKI_queue_is_empty(BUFFER_Q *p_q)
{
return ((BOOLEAN) (p_q->_count == 0));
}
UINT16 GKI_queue_length(BUFFER_Q *p_q)
{
return p_q->_count;
}
/*******************************************************************************
**
** Function GKI_poolcount
**
** Description Called by an application to get the total number of buffers
** in the specified buffer pool.
**
** Parameters pool_id - (input) pool ID to get the free count of.
**
** Returns the total number of buffers in the pool
**
*******************************************************************************/
UINT16 GKI_poolcount (UINT8 pool_id)
{
if (pool_id >= GKI_NUM_TOTAL_BUF_POOLS)
return (0);
return (gki_cb.com.freeq[pool_id].total);
}
/*******************************************************************************
**
** Function GKI_poolfreecount
**
** Description Called by an application to get the number of free buffers
** in the specified buffer pool.
**
** Parameters pool_id - (input) pool ID to get the free count of.
**
** Returns the number of free buffers in the pool
**
*******************************************************************************/
UINT16 GKI_poolfreecount (UINT8 pool_id)
{
FREE_QUEUE_T *Q;
if (pool_id >= GKI_NUM_TOTAL_BUF_POOLS)
return (0);
Q = &gki_cb.com.freeq[pool_id];
return ((UINT16)(Q->total - Q->cur_cnt));
}
/*******************************************************************************
**
** Function GKI_get_pool_bufsize
**
** Description Called by an application to get the size of buffers in a pool
**
** Parameters Pool ID.
**
** Returns the size of buffers in the pool
**
*******************************************************************************/
UINT16 GKI_get_pool_bufsize (UINT8 pool_id)
{
if (pool_id < GKI_NUM_TOTAL_BUF_POOLS)
return (gki_cb.com.freeq[pool_id].size);
return (0);
}
/*******************************************************************************
**
** Function GKI_poolutilization
**
** Description Called by an application to get the buffer utilization
** in the specified buffer pool.
**
** Parameters pool_id - (input) pool ID to get the free count of.
**
** Returns % of buffers used from 0 to 100
**
*******************************************************************************/
UINT16 GKI_poolutilization (UINT8 pool_id)
{
FREE_QUEUE_T *Q;
if (pool_id >= GKI_NUM_TOTAL_BUF_POOLS)
return (100);
Q = &gki_cb.com.freeq[pool_id];
if (Q->total == 0)
return (100);
return ((Q->cur_cnt * 100) / Q->total);
}

View file

@ -0,0 +1,62 @@
/******************************************************************************
*
* Copyright (C) 2009-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <string.h>
#include "bt_trace.h"
#include "gki_int.h"
#include "osi.h"
#include "osi_arch.h"
#include "alarm.h"
#include "bt_defs.h"
tGKI_CB gki_cb;
int gki_init(void) {
memset(&gki_cb, 0, sizeof(gki_cb));
//pthread_mutexattr_t attr;
//pthread_mutexattr_init(&attr);
//pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&gki_cb.lock, NULL);//&attr);
gki_buffer_init();
return 0;
}
void gki_clean_up(void) {
gki_buffer_cleanup();
pthread_mutex_destroy(&gki_cb.lock);
}
UINT32 GKI_get_os_tick_count(void) {
return osi_alarm_now();
}
// Sleep the calling thread unconditionally for |timeout_ms| milliseconds.
void GKI_delay(UINT32 timeout_ms) {
osi_delay_ms(timeout_ms);
/*TODO:*/
}
void GKI_enable(void) {
pthread_mutex_unlock(&gki_cb.lock);
}
void GKI_disable(void) {
pthread_mutex_lock(&gki_cb.lock);
}

View file

@ -0,0 +1,101 @@
/******************************************************************************
*
* Copyright (C) 1999-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _GKI_H_
#define _GKI_H_
#include "bt_target.h"
#include "bt_types.h"
//static const char GKI_MODULE[] = "gki_module";
/* Timer list entry callback type
*/
typedef void (TIMER_CBACK)(void *p_tle);
#ifndef TIMER_PARAM_TYPE
#define TIMER_PARAM_TYPE UINT32
#endif
/* Define a timer list entry
*/
typedef struct _tle
{
struct _tle *p_next;
struct _tle *p_prev;
TIMER_CBACK *p_cback;
INT32 ticks;
INT32 ticks_initial;
TIMER_PARAM_TYPE param;
TIMER_PARAM_TYPE data;
UINT16 event;
UINT8 in_use;
} TIMER_LIST_ENT;
/***********************************************************************
** This queue is a general purpose buffer queue, for application use.
*/
typedef struct
{
void *_p_first;
void *_p_last;
UINT16 _count;
} BUFFER_Q;
#define GKI_PUBLIC_POOL 0 /* General pool accessible to GKI_getbuf() */
#define GKI_RESTRICTED_POOL 1 /* Inaccessible pool to GKI_getbuf() */
/***********************************************************************
** Function prototypes
*/
/* To get and release buffers, change owner and get size
*/
void GKI_freebuf (void *);
void *GKI_getbuf (UINT16);
UINT16 GKI_get_buf_size (void *);
void *GKI_getpoolbuf (UINT8);
UINT16 GKI_poolcount (UINT8);
UINT16 GKI_poolfreecount (UINT8);
UINT16 GKI_poolutilization (UINT8);
/* User buffer queue management
*/
void *GKI_dequeue (BUFFER_Q *);
void GKI_enqueue (BUFFER_Q *, void *);
void *GKI_getfirst (BUFFER_Q *);
void *GKI_getlast (BUFFER_Q *);
void *GKI_getnext (void *);
void GKI_init_q (BUFFER_Q *);
UINT16 GKI_queue_length(BUFFER_Q *);
BOOLEAN GKI_queue_is_empty(BUFFER_Q *);
void *GKI_remove_from_queue (BUFFER_Q *, void *);
UINT16 GKI_get_pool_bufsize (UINT8);
/* Timer management
*/
void GKI_delay(UINT32);
/* Disable Interrupts, Enable Interrupts
*/
void GKI_enable(void);
void GKI_disable(void);
/* os timer operation */
UINT32 GKI_get_os_tick_count(void);
#endif /*_GKI_H_*/

View file

@ -0,0 +1,68 @@
/******************************************************************************
*
* Copyright (C) 1999-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _GKI_COMMON_H_
#define _GKI_COMMON_H_
#include "gki.h"
typedef struct _buffer_hdr
{
struct _buffer_hdr *p_next; /* next buffer in the queue */
UINT8 q_id; /* id of the queue */
UINT8 status; /* FREE, UNLINKED or QUEUED */
UINT8 Type;
UINT16 size;
} BUFFER_HDR_T;
typedef struct _free_queue
{
BUFFER_HDR_T *_p_first; /* first buffer in the queue */
BUFFER_HDR_T *_p_last; /* last buffer in the queue */
UINT16 size; /* size of the buffers in the pool */
UINT16 total; /* toatal number of buffers */
UINT16 cur_cnt; /* number of buffers currently allocated */
UINT16 max_cnt; /* maximum number of buffers allocated at any time */
} FREE_QUEUE_T;
/* Put all GKI variables into one control block
*/
typedef struct
{
/* Define the buffer pool management variables
*/
FREE_QUEUE_T freeq[GKI_NUM_TOTAL_BUF_POOLS];
UINT16 pool_buf_size[GKI_NUM_TOTAL_BUF_POOLS];
/* Define the buffer pool start addresses
*/
UINT8 *pool_start[GKI_NUM_TOTAL_BUF_POOLS]; /* array of pointers to the start of each buffer pool */
UINT8 *pool_end[GKI_NUM_TOTAL_BUF_POOLS]; /* array of pointers to the end of each buffer pool */
UINT16 pool_size[GKI_NUM_TOTAL_BUF_POOLS]; /* actual size of the buffers in a pool */
/* Define the buffer pool access control variables */
UINT16 pool_access_mask; /* Bits are set if the corresponding buffer pool is a restricted pool */
} tGKI_COM_CB;
/* Internal GKI function prototypes
*/
void gki_buffer_init(void);
void gki_buffer_cleanup(void);
#endif /*_GKI_COMMON_H_*/

View file

@ -0,0 +1,35 @@
/******************************************************************************
*
* Copyright (C) 1999-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _GKI_INT_H_
#define _GKI_INT_H_
//#include <pthread.h>
#include "bt_defs.h"
#include "gki_common.h"
typedef struct
{
pthread_mutex_t lock;
tGKI_COM_CB com;
} tGKI_CB;
extern tGKI_CB gki_cb;
#endif /*_GKI_INT_H_*/

View file

@ -0,0 +1,33 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include "buffer_allocator.h"
#include "gki.h"
// TODO(zachoverflow): move the assertion into GKI_getbuf in the future
static void *buffer_alloc(size_t size) {
return GKI_getbuf((uint16_t)size);
}
static const allocator_t interface = {
buffer_alloc,
GKI_freebuf
};
const allocator_t *buffer_allocator_get_interface() {
return &interface;
}

View file

@ -0,0 +1,288 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <string.h>
#include "bt_defs.h"
#include "bt_trace.h"
#include "bt_types.h"
#include "buffer_allocator.h"
#include "fixed_queue.h"
#include "hci_hal.h"
#include "hci_internals.h"
#include "hci_layer.h"
#include "thread.h"
#define HCI_HAL_SERIAL_BUFFER_SIZE 1026
#define HCI_BLE_EVENT 0x3e
#define PACKET_TYPE_TO_INBOUND_INDEX(type) ((type) - 2)
#define PACKET_TYPE_TO_INDEX(type) ((type) - 1)
static const uint8_t preamble_sizes[] = {
HCI_COMMAND_PREAMBLE_SIZE,
HCI_ACL_PREAMBLE_SIZE,
HCI_SCO_PREAMBLE_SIZE,
HCI_EVENT_PREAMBLE_SIZE
};
static const uint16_t outbound_event_types[] = {
MSG_HC_TO_STACK_HCI_ERR,
MSG_HC_TO_STACK_HCI_ACL,
MSG_HC_TO_STACK_HCI_SCO,
MSG_HC_TO_STACK_HCI_EVT
};
typedef struct {
const allocator_t *allocator;
size_t buffer_size;
fixed_queue_t *rx_q;
} hci_hal_env_t;
static hci_hal_env_t hci_hal_env;
static const hci_hal_t interface;
static const hci_hal_callbacks_t *callbacks;
static const vhci_host_callback_t vhci_host_cb;
static xTaskHandle xHciH4TaskHandle;
static xQueueHandle xHciH4Queue;
static void host_send_pkt_available_cb(void);
static int host_recv_pkt_cb(uint8_t *data, uint16_t len);
static void hci_hal_h4_rx_handler(void *arg);
static void event_uart_has_bytes(fixed_queue_t *queue);
static void hci_hal_env_init(
size_t buffer_size,
size_t max_buffer_count) {
assert(buffer_size > 0);
assert(max_buffer_count > 0);
hci_hal_env.allocator = buffer_allocator_get_interface();
hci_hal_env.buffer_size = buffer_size;
hci_hal_env.rx_q = fixed_queue_new(max_buffer_count);
if (hci_hal_env.rx_q)
fixed_queue_register_dequeue(hci_hal_env.rx_q, event_uart_has_bytes);
else
LOG_ERROR("%s unable to create rx queue.", __func__);
return;
}
static void hci_hal_env_deinit(void) {
fixed_queue_free(hci_hal_env.rx_q, hci_hal_env.allocator->free);
}
static bool hal_open(const hci_hal_callbacks_t *upper_callbacks) {
assert(upper_callbacks != NULL);
callbacks = upper_callbacks;
hci_hal_env_init(HCI_HAL_SERIAL_BUFFER_SIZE, SIZE_MAX);
xHciH4Queue = xQueueCreate(3, sizeof(void *));
xTaskCreate(hci_hal_h4_rx_handler, "HciH4T", 4096+2048, NULL, configMAX_PRIORITIES - 3, &xHciH4TaskHandle);
//register vhci host cb
API_vhci_host_register_callback(&vhci_host_cb);
return true;
error:
interface.close();
return false;
}
static void hal_close() {
hci_hal_env_deinit();
/* delete task and queue */
vTaskDelete(xHciH4TaskHandle);
vQueueDelete(xHciH4Queue);
}
/**
* Function: transmit_data -TX data to low-layer
* It is ported from Bluedroid source code, so it is not
* needed to use write() to send data.
* TODO: Just use firmware API to send data.
*/
static uint16_t transmit_data(serial_data_type_t type,
uint8_t *data, uint16_t length)
{
uint8_t previous_byte;
assert(data != NULL);
assert(length > 0);
if (type < DATA_TYPE_COMMAND || type > DATA_TYPE_SCO) {
LOG_ERROR("%s invalid data type: %d", __func__, type);
return 0;
}
// Write the signal byte right before the data
--data;
previous_byte = *data;
*(data) = type;
++length;
BTTRC_DUMP_BUFFER("Transmit Pkt", data, length);
// TX Data to target
API_vhci_host_send_packet(data, length);
// Be nice and restore the old value of that byte
*(data) = previous_byte;
return length - 1;
}
// Internal functions
static void hci_hal_h4_rx_handler(void *arg) {
TaskEvt_t *e;
for (;;) {
if (pdTRUE == xQueueReceive(xHciH4Queue, &e, (portTickType)portMAX_DELAY)) {
if (e->sig == 0xff) {
fixed_queue_process(hci_hal_env.rx_q);
}
osi_free(e);
}
}
}
void hci_hal_h4_task_post(void)
{
TaskEvt_t *evt = (TaskEvt_t *)osi_malloc(sizeof(TaskEvt_t));
if (evt == NULL)
return;
evt->sig = 0xff;
evt->par = 0;
if (xQueueSend(xHciH4Queue, &evt, 10/portTICK_RATE_MS) != pdTRUE) {
ets_printf("xHciH4Queue failed\n");
}
}
static void hci_hal_h4_hdl_rx_packet(BT_HDR *packet) {
uint8_t type, hdr_size;
uint16_t length;
uint8_t *stream = packet->data + packet->offset;
if (!packet)
return;
STREAM_TO_UINT8(type, stream);
packet->offset++;
packet->len--;
if (type == HCI_BLE_EVENT) {
uint8_t len;
STREAM_TO_UINT8(len, stream);
LOG_ERROR("Workround stream corrupted during LE SCAN: pkt_len=%d ble_event_len=%d",
packet->len, len);
hci_hal_env.allocator->free(packet);
return;
}
if (type < DATA_TYPE_ACL || type > DATA_TYPE_EVENT) {
LOG_ERROR("%d Unknown HCI message type. Dropping this byte 0x%x,"
" min %x, max %x", __func__, type,
DATA_TYPE_ACL, DATA_TYPE_EVENT);
hci_hal_env.allocator->free(packet);
return;
}
hdr_size = preamble_sizes[type - 1];
if (packet->len < hdr_size) {
LOG_ERROR("Wrong packet length type=%s pkt_len=%d hdr_len=%d",
type, packet->len, hdr_size);
hci_hal_env.allocator->free(packet);
return;
}
if (type == DATA_TYPE_ACL) {
stream += hdr_size - 2;
STREAM_TO_UINT16(length, stream);
} else {
stream += hdr_size - 1;
STREAM_TO_UINT8(length, stream);
}
if ((length + hdr_size) != packet->len) {
LOG_ERROR("Wrong packet length type=%d hdr_len=%d pd_len=%d "
"pkt_len=%d", type, hdr_size, length, packet->len);
hci_hal_env.allocator->free(packet);
return;
}
packet->event = outbound_event_types[PACKET_TYPE_TO_INDEX(type)];
callbacks->packet_ready(packet);
}
static void event_uart_has_bytes(fixed_queue_t *queue) {
BT_HDR *packet;
while (!fixed_queue_is_empty(queue)) {
packet = fixed_queue_dequeue(queue);
hci_hal_h4_hdl_rx_packet(packet);
}
}
static void host_send_pkt_available_cb(void) {
//Controller rx cache buffer is ready for receiving new host packet
//Just Call Host main thread task to process pending packets.
hci_host_task_post();
}
static int host_recv_pkt_cb(uint8_t *data, uint16_t len) {
//Target has packet to host, malloc new buffer for packet
BT_HDR *pkt;
size_t pkt_size;
pkt_size = BT_HDR_SIZE + len;
pkt = (BT_HDR *)hci_hal_env.allocator->alloc(pkt_size);
if (!pkt) {
LOG_ERROR("%s couldn't aquire memory for inbound data buffer.", __func__);
return -1;
}
pkt->offset = 0;
pkt->len = len;
pkt->layer_specific = 0;
memcpy(pkt->data, data, len);
fixed_queue_enqueue(hci_hal_env.rx_q, pkt);
hci_hal_h4_task_post();
BTTRC_DUMP_BUFFER("Recv Pkt", pkt->data, len);
return 0;
}
static const vhci_host_callback_t vhci_host_cb = {
.notify_host_send_available = host_send_pkt_available_cb,
.notify_host_recv = host_recv_pkt_cb,
};
static const hci_hal_t interface = {
hal_open,
hal_close,
transmit_data,
};
const hci_hal_t *hci_hal_h4_get_interface() {
return &interface;
}

View file

@ -0,0 +1,574 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <string.h>
#include "bt_defs.h"
#include "bt_trace.h"
#include "hcidefs.h"
#include "hcimsgs.h"
#include "bt_vendor_lib.h"
#include "hci_internals.h"
#include "hci_hal.h"
#include "hci_layer.h"
#include "allocator.h"
#include "packet_fragmenter.h"
#include "buffer_allocator.h"
#include "list.h"
#include "alarm.h"
#include "thread.h"
typedef struct {
uint16_t opcode;
command_complete_cb complete_callback;
command_status_cb status_callback;
void *context;
uint32_t sent_time;
BT_HDR *command;
} waiting_command_t;
typedef struct {
bool timer_is_set;
osi_alarm_t *command_response_timer;
list_t *commands_pending_response;
pthread_mutex_t commands_pending_response_lock;
} command_waiting_response_t;
typedef struct {
int command_credits;
fixed_queue_t *command_queue;
fixed_queue_t *packet_queue;
// The hand-off point for data going to a higher layer, set by the higher layer
fixed_queue_t *upwards_data_queue;
command_waiting_response_t cmd_waiting_q;
/*
non_repeating_timer_t *command_response_timer;
list_t *commands_pending_response;
pthread_mutex_t commands_pending_response_lock;
*/
} hci_host_env_t;
// Using a define here, because it can be stringified for the property lookup
static const uint32_t COMMAND_PENDING_TIMEOUT = 8000;
// Our interface
static bool interface_created;
static hci_t interface;
static hci_host_env_t hci_host_env;
static xTaskHandle xHciHostTaskHandle;
static xQueueHandle xHciHostQueue;
static bool hci_host_startup_flag;
// Modules we import and callbacks we export
static const allocator_t *buffer_allocator;
static const hci_hal_t *hal;
static const hci_hal_callbacks_t hal_callbacks;
static const packet_fragmenter_t *packet_fragmenter;
static const packet_fragmenter_callbacks_t packet_fragmenter_callbacks;
static int hci_layer_init_env(void);
static void hci_layer_deinit_env(void);
static void hci_host_thread_handler(void *arg);
static int hci_send_async_command(bt_vendor_opcode_t opcode, void *param);
static void event_finish_startup(void *context);
static void firmware_config_callback(bool success);
static void event_postload(void);
static void sco_config_callback(bool success);
static void event_command_ready(fixed_queue_t *queue);
static void event_packet_ready(fixed_queue_t *queue);
static void restart_comamnd_waiting_response_timer(
command_waiting_response_t *cmd_wait_q,
bool tigger_by_sending_command);
static void command_timed_out(void *context);
static void hal_says_packet_ready(BT_HDR *packet);
static bool filter_incoming_event(BT_HDR *packet);
static serial_data_type_t event_to_data_type(uint16_t event);
static waiting_command_t *get_waiting_command(command_opcode_t opcode);
static void dispatch_reassembled(BT_HDR *packet);
// Module lifecycle functions
int hci_start_up(void) {
if (hci_layer_init_env())
goto error;
xHciHostQueue = xQueueCreate(3, sizeof(void *));
xTaskCreate(hci_host_thread_handler, "HciHostT", (4096+2048), NULL, configMAX_PRIORITIES - 3, &xHciHostTaskHandle);
packet_fragmenter->init(&packet_fragmenter_callbacks);
hal->open(&hal_callbacks);
hci_host_startup_flag = true;
return 0;
error:
hci_shut_down();
return -1;
}
void hci_shut_down(void) {
hci_host_startup_flag = false;
hci_layer_deinit_env();
packet_fragmenter->cleanup();
//low_power_manager->cleanup();
hal->close();
vTaskDelete(xHciHostTaskHandle);
vQueueDelete(xHciHostQueue);
}
void hci_host_task_post(void)
{
if (hci_host_startup_flag == false)
return;
TaskEvt_t *evt = (TaskEvt_t *)osi_malloc(sizeof(TaskEvt_t));
if (evt == NULL)
return;
evt->sig = 0xff;
evt->par = 0;
if (xQueueSend(xHciHostQueue, &evt, 10/portTICK_RATE_MS) != pdTRUE) {
ets_printf("xHciHostQueue failed\n");
}
}
static int hci_layer_init_env(void)
{
command_waiting_response_t *cmd_wait_q;
// The host is only allowed to send at most one command initially,
// as per the Bluetooth spec, Volume 2, Part E, 4.4 (Command Flow Control)
// This value can change when you get a command complete or command status event.
hci_host_env.command_credits = 1;
hci_host_env.command_queue = fixed_queue_new(SIZE_MAX);
if (hci_host_env.command_queue)
fixed_queue_register_dequeue(hci_host_env.command_queue, event_command_ready);
else {
LOG_ERROR("%s unable to create pending command queue.", __func__);
return -1;
}
hci_host_env.packet_queue = fixed_queue_new(SIZE_MAX);
if (hci_host_env.packet_queue)
fixed_queue_register_dequeue(hci_host_env.packet_queue, event_packet_ready);
else {
LOG_ERROR("%s unable to create pending packet queue.", __func__);
return -1;
}
// Init Commands waiting response list and timer
cmd_wait_q = &hci_host_env.cmd_waiting_q;
cmd_wait_q->timer_is_set = false;
cmd_wait_q->commands_pending_response = list_new(NULL);
if (!cmd_wait_q->commands_pending_response) {
LOG_ERROR("%s unable to create list for commands pending response.", __func__);
return -1;
}
pthread_mutex_init(&cmd_wait_q->commands_pending_response_lock, NULL);
cmd_wait_q->command_response_timer = osi_alarm_new("cmd_rsp_to", command_timed_out, cmd_wait_q, COMMAND_PENDING_TIMEOUT);
if (!cmd_wait_q->command_response_timer) {
LOG_ERROR("%s unable to create command response timer.", __func__);
return -1;
}
return 0;
}
static void hci_layer_deinit_env(void)
{
command_waiting_response_t *cmd_wait_q;
if (hci_host_env.command_queue)
fixed_queue_free(hci_host_env.command_queue, osi_free);
if (hci_host_env.packet_queue)
fixed_queue_free(hci_host_env.packet_queue, buffer_allocator->free);
cmd_wait_q = &hci_host_env.cmd_waiting_q;
list_free(cmd_wait_q->commands_pending_response);
pthread_mutex_destroy(&cmd_wait_q->commands_pending_response_lock);
osi_alarm_free(cmd_wait_q->command_response_timer);
cmd_wait_q->command_response_timer = NULL;
}
static void hci_host_thread_handler(void *arg)
{
/*
* Previous task handles RX queue and two TX Queues, Since there is
* a RX Thread Task in H4 layer which receives packet from driver layer.
* Now HCI Host Task has been optimized to only process TX Queue
* including command and data queue. And command queue has high priority,
* All packets will be directly copied to single queue in driver layer with
* H4 type header added (1 byte).
*/
TaskEvt_t *e;
for (;;) {
if (pdTRUE == xQueueReceive(xHciHostQueue, &e, (portTickType)portMAX_DELAY)) {
if (e->sig == 0xff) {
if (API_vhci_host_check_send_available()) {
/*Now Target only allowed one packet per TX*/
BT_HDR *pkt = packet_fragmenter->fragment_current_packet();
if (pkt != NULL) {
packet_fragmenter->fragment_and_dispatch(pkt);
} else {
if (!fixed_queue_is_empty(hci_host_env.command_queue) &&
hci_host_env.command_credits > 0)
fixed_queue_process(hci_host_env.command_queue);
else if (!fixed_queue_is_empty(hci_host_env.packet_queue))
fixed_queue_process(hci_host_env.packet_queue);
}
}
}
}
osi_free(e);
}
}
static void set_data_queue(fixed_queue_t *queue) {
hci_host_env.upwards_data_queue = queue;
}
static void transmit_command(
BT_HDR *command,
command_complete_cb complete_callback,
command_status_cb status_callback,
void *context) {
uint8_t *stream;
waiting_command_t *wait_entry = osi_calloc(sizeof(waiting_command_t));
if (!wait_entry) {
LOG_ERROR("%s couldn't allocate space for wait entry.", __func__);
return;
}
stream = command->data + command->offset;
STREAM_TO_UINT16(wait_entry->opcode, stream);
wait_entry->complete_callback = complete_callback;
wait_entry->status_callback = status_callback;
wait_entry->command = command;
wait_entry->context = context;
// Store the command message type in the event field
// in case the upper layer didn't already
command->event = MSG_STACK_TO_HC_HCI_CMD;
LOG_DEBUG("HCI Enqueue Comamnd opcode=0x%x\n", wait_entry->opcode);
BTTRC_DUMP_BUFFER(NULL, command->data + command->offset, command->len);
fixed_queue_enqueue(hci_host_env.command_queue, wait_entry);
hci_host_task_post();
}
static void transmit_downward(uint16_t type, void *data) {
BT_HDR *tmp = (BT_HDR *)data;
if (type == MSG_STACK_TO_HC_HCI_CMD) {
transmit_command((BT_HDR *)data, NULL, NULL, NULL);
LOG_WARN("%s legacy transmit of command. Use transmit_command instead.\n", __func__);
} else {
fixed_queue_enqueue(hci_host_env.packet_queue, data);
}
//ke_event_set(KE_EVENT_HCI_HOST_THREAD);
hci_host_task_post();
}
// Postload functions
static void event_postload(void) {
if (hci_send_async_command(BT_VND_OP_SCO_CFG, NULL) == -1) {
// If couldn't configure sco, we won't get the sco configuration callback
// so go pretend to do it now
sco_config_callback(false);
}
}
static void sco_config_callback(UNUSED_ATTR bool success) {
LOG_INFO("%s postload finished.", __func__);
}
// Command/packet transmitting functions
static void event_command_ready(fixed_queue_t *queue) {
waiting_command_t *wait_entry = NULL;
command_waiting_response_t *cmd_wait_q = &hci_host_env.cmd_waiting_q;
wait_entry = fixed_queue_dequeue(queue);
hci_host_env.command_credits--;
// Move it to the list of commands awaiting response
pthread_mutex_lock(&cmd_wait_q->commands_pending_response_lock);
//ets_printf("%s\n", __func__);
list_append(cmd_wait_q->commands_pending_response, wait_entry);
pthread_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
// Send it off
packet_fragmenter->fragment_and_dispatch(wait_entry->command);
wait_entry->sent_time = osi_alarm_now();
restart_comamnd_waiting_response_timer(cmd_wait_q, true);
}
static void event_packet_ready(fixed_queue_t *queue) {
BT_HDR *packet = (BT_HDR *)fixed_queue_dequeue(queue);
// The queue may be the command queue or the packet queue, we don't care
packet_fragmenter->fragment_and_dispatch(packet);
}
// Callback for the fragmenter to send a fragment
static void transmit_fragment(BT_HDR *packet, bool send_transmit_finished) {
uint16_t event = packet->event & MSG_EVT_MASK;
serial_data_type_t type = event_to_data_type(event);
hal->transmit_data(type, packet->data + packet->offset, packet->len);
if (event != MSG_STACK_TO_HC_HCI_CMD && send_transmit_finished)
buffer_allocator->free(packet);
}
static void fragmenter_transmit_finished(BT_HDR *packet, bool all_fragments_sent) {
if (all_fragments_sent) {
buffer_allocator->free(packet);
} else {
// This is kind of a weird case, since we're dispatching a partially sent packet
// up to a higher layer.
// TODO(zachoverflow): rework upper layer so this isn't necessary.
dispatch_reassembled(packet);
//data_dispatcher_dispatch(interface.event_dispatcher, packet->event & MSG_EVT_MASK, packet);
}
}
static void restart_comamnd_waiting_response_timer(
command_waiting_response_t *cmd_wait_q,
bool tigger_by_sending_command)
{
uint32_t timeout;
waiting_command_t *wait_entry;
if (!cmd_wait_q)
return;
if (cmd_wait_q->timer_is_set) {
if (tigger_by_sending_command)
return;
//Cancel Previous command timeout timer setted when sending command
osi_alarm_cancel(cmd_wait_q->command_response_timer);
cmd_wait_q->timer_is_set = false;
}
pthread_mutex_lock(&cmd_wait_q->commands_pending_response_lock);
wait_entry = (list_is_empty(cmd_wait_q->commands_pending_response) ?
NULL : list_front(cmd_wait_q->commands_pending_response));
pthread_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
if (wait_entry == NULL)
return;
timeout =osi_alarm_time_diff(osi_alarm_now(), wait_entry->sent_time);
timeout =osi_alarm_time_diff(COMMAND_PENDING_TIMEOUT, timeout);
timeout = (timeout <= COMMAND_PENDING_TIMEOUT) ? timeout : COMMAND_PENDING_TIMEOUT;
osi_alarm_set(cmd_wait_q->command_response_timer, timeout);
cmd_wait_q->timer_is_set = true;
}
static void command_timed_out(void *context) {
command_waiting_response_t *cmd_wait_q = (command_waiting_response_t *)context;
waiting_command_t *wait_entry;
pthread_mutex_lock(&cmd_wait_q->commands_pending_response_lock);
wait_entry = (list_is_empty(cmd_wait_q->commands_pending_response) ?
NULL : list_front(cmd_wait_q->commands_pending_response));
pthread_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
if (wait_entry == NULL)
LOG_ERROR("%s with no commands pending response", __func__);
else
// We shouldn't try to recover the stack from this command timeout.
// If it's caused by a software bug, fix it. If it's a hardware bug, fix it.
LOG_ERROR("%s hci layer timeout waiting for response to a command. opcode: 0x%x", __func__, wait_entry->opcode);
}
// Event/packet receiving functions
static void hal_says_packet_ready(BT_HDR *packet) {
if (packet->event != MSG_HC_TO_STACK_HCI_EVT) {
packet_fragmenter->reassemble_and_dispatch(packet);
} else if (!filter_incoming_event(packet)) {
dispatch_reassembled(packet);
}
}
// Returns true if the event was intercepted and should not proceed to
// higher layers. Also inspects an incoming event for interesting
// information, like how many commands are now able to be sent.
static bool filter_incoming_event(BT_HDR *packet) {
waiting_command_t *wait_entry = NULL;
uint8_t *stream = packet->data + packet->offset;
uint8_t event_code;
command_opcode_t opcode;
STREAM_TO_UINT8(event_code, stream);
STREAM_SKIP_UINT8(stream); // Skip the parameter total length field
LOG_DEBUG("Receive packet event_code=0x%x\n", event_code);
if (event_code == HCI_COMMAND_COMPLETE_EVT) {
STREAM_TO_UINT8(hci_host_env.command_credits, stream);
STREAM_TO_UINT16(opcode, stream);
wait_entry = get_waiting_command(opcode);
if (!wait_entry)
LOG_WARN("%s command complete event with no matching command. opcode: 0x%x.", __func__, opcode);
else if (wait_entry->complete_callback)
wait_entry->complete_callback(packet, wait_entry->context);
goto intercepted;
} else if (event_code == HCI_COMMAND_STATUS_EVT) {
uint8_t status;
STREAM_TO_UINT8(status, stream);
STREAM_TO_UINT8(hci_host_env.command_credits, stream);
STREAM_TO_UINT16(opcode, stream);
// If a command generates a command status event, it won't be getting a command complete event
wait_entry = get_waiting_command(opcode);
if (!wait_entry)
LOG_WARN("%s command status event with no matching command. opcode: 0x%x", __func__, opcode);
else if (wait_entry->status_callback)
wait_entry->status_callback(status, wait_entry->command, wait_entry->context);
goto intercepted;
}
return false;
intercepted:
/*Tell HCI Host Task to continue TX Pending commands*/
if (hci_host_env.command_credits &&
!fixed_queue_is_empty(hci_host_env.command_queue))
hci_host_task_post();
//ke_event_set(KE_EVENT_HCI_HOST_THREAD);
restart_comamnd_waiting_response_timer(&hci_host_env.cmd_waiting_q, false);
if (wait_entry) {
// If it has a callback, it's responsible for freeing the packet
if (event_code == HCI_COMMAND_STATUS_EVT ||
!wait_entry->complete_callback)
buffer_allocator->free(packet);
// If it has a callback, it's responsible for freeing the command
if (event_code == HCI_COMMAND_COMPLETE_EVT || !wait_entry->status_callback)
buffer_allocator->free(wait_entry->command);
osi_free(wait_entry);
} else {
buffer_allocator->free(packet);
}
return true;
}
// Callback for the fragmenter to dispatch up a completely reassembled packet
static void dispatch_reassembled(BT_HDR *packet) {
// Events should already have been dispatched before this point
if (hci_host_env.upwards_data_queue) {
fixed_queue_enqueue(hci_host_env.upwards_data_queue, packet);
btu_task_post();
//Tell Up-layer received packet.
} else {
LOG_DEBUG("%s had no queue to place upwards data packet in. Dropping it on the floor.", __func__);
buffer_allocator->free(packet);
}
}
// Misc internal functions
// TODO(zachoverflow): we seem to do this a couple places, like the HCI inject module. #centralize
static serial_data_type_t event_to_data_type(uint16_t event) {
if (event == MSG_STACK_TO_HC_HCI_ACL)
return DATA_TYPE_ACL;
else if (event == MSG_STACK_TO_HC_HCI_SCO)
return DATA_TYPE_SCO;
else if (event == MSG_STACK_TO_HC_HCI_CMD)
return DATA_TYPE_COMMAND;
else
LOG_ERROR("%s invalid event type, could not translate 0x%x", __func__, event);
return 0;
}
static waiting_command_t *get_waiting_command(command_opcode_t opcode) {
command_waiting_response_t *cmd_wait_q = &hci_host_env.cmd_waiting_q;
pthread_mutex_lock(&cmd_wait_q->commands_pending_response_lock);
for (const list_node_t *node = list_begin(cmd_wait_q->commands_pending_response);
node != list_end(cmd_wait_q->commands_pending_response);
node = list_next(node)) {
waiting_command_t *wait_entry = list_node(node);
//ets_printf("wait_entry %08x\n", wait_entry);
if (!wait_entry || wait_entry->opcode != opcode)
continue;
list_remove(cmd_wait_q->commands_pending_response, wait_entry);
pthread_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
return wait_entry;
}
pthread_mutex_unlock(&cmd_wait_q->commands_pending_response_lock);
return NULL;
}
static int hci_send_async_command(bt_vendor_opcode_t opcode, void *param)
{
}
static void init_layer_interface() {
if (!interface_created) {
interface.set_data_queue = set_data_queue;
interface.transmit_command = transmit_command;
interface.transmit_downward = transmit_downward;
interface_created = true;
}
}
static const hci_hal_callbacks_t hal_callbacks = {
hal_says_packet_ready
};
static const packet_fragmenter_callbacks_t packet_fragmenter_callbacks = {
transmit_fragment,
dispatch_reassembled,
fragmenter_transmit_finished
};
const hci_t *hci_layer_get_interface() {
buffer_allocator = buffer_allocator_get_interface();
hal = hci_hal_h4_get_interface();
packet_fragmenter = packet_fragmenter_get_interface();
init_layer_interface();
return &interface;
}

View file

@ -0,0 +1,202 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include "bt_defs.h"
#include "allocator.h"
#include "bt_types.h"
#include "buffer_allocator.h"
#include "hcidefs.h"
#include "hcimsgs.h"
#include "hci_internals.h"
#include "hci_layer.h"
#include "hci_packet_factory.h"
static const allocator_t *buffer_allocator;
static BT_HDR *make_packet(size_t data_size);
static BT_HDR *make_command_no_params(uint16_t opcode);
static BT_HDR *make_command(uint16_t opcode, size_t parameter_size, uint8_t **stream_out);
// Interface functions
static BT_HDR *make_reset(void) {
return make_command_no_params(HCI_RESET);
}
static BT_HDR *make_read_buffer_size(void) {
return make_command_no_params(HCI_READ_BUFFER_SIZE);
}
static BT_HDR *make_host_buffer_size(uint16_t acl_size, uint8_t sco_size, uint16_t acl_count, uint16_t sco_count) {
uint8_t *stream;
const uint8_t parameter_size = 2 + 1 + 2 + 2; // from each of the parameters
BT_HDR *packet = make_command(HCI_HOST_BUFFER_SIZE, parameter_size, &stream);
UINT16_TO_STREAM(stream, acl_size);
UINT8_TO_STREAM(stream, sco_size);
UINT16_TO_STREAM(stream, acl_count);
UINT16_TO_STREAM(stream, sco_count);
return packet;
}
static BT_HDR *make_read_local_version_info(void) {
return make_command_no_params(HCI_READ_LOCAL_VERSION_INFO);
}
static BT_HDR *make_read_bd_addr(void) {
return make_command_no_params(HCI_READ_BD_ADDR);
}
static BT_HDR *make_read_local_supported_commands(void) {
return make_command_no_params(HCI_READ_LOCAL_SUPPORTED_CMDS);
}
static BT_HDR *make_read_local_extended_features(uint8_t page_number) {
uint8_t *stream;
const uint8_t parameter_size = 1;
BT_HDR *packet = make_command(HCI_READ_LOCAL_EXT_FEATURES, parameter_size, &stream);
UINT8_TO_STREAM(stream, page_number);
return packet;
}
static BT_HDR *make_write_simple_pairing_mode(uint8_t mode) {
uint8_t *stream;
const uint8_t parameter_size = 1;
BT_HDR *packet = make_command(HCI_WRITE_SIMPLE_PAIRING_MODE, parameter_size, &stream);
UINT8_TO_STREAM(stream, mode);
return packet;
}
static BT_HDR *make_write_secure_connections_host_support(uint8_t mode) {
uint8_t *stream;
const uint8_t parameter_size = 1;
BT_HDR *packet = make_command(HCI_WRITE_SECURE_CONNS_SUPPORT, parameter_size, &stream);
UINT8_TO_STREAM(stream, mode);
return packet;
}
static BT_HDR *make_set_event_mask(const bt_event_mask_t *event_mask) {
uint8_t *stream;
uint8_t parameter_size = sizeof(bt_event_mask_t);
BT_HDR *packet = make_command(HCI_SET_EVENT_MASK, parameter_size, &stream);
ARRAY8_TO_STREAM(stream, event_mask->as_array);
return packet;
}
static BT_HDR *make_ble_write_host_support(uint8_t supported_host, uint8_t simultaneous_host) {
uint8_t *stream;
const uint8_t parameter_size = 1 + 1;
BT_HDR *packet = make_command(HCI_WRITE_LE_HOST_SUPPORT, parameter_size, &stream);
UINT8_TO_STREAM(stream, supported_host);
UINT8_TO_STREAM(stream, simultaneous_host);
return packet;
}
static BT_HDR *make_ble_read_white_list_size(void) {
return make_command_no_params(HCI_BLE_READ_WHITE_LIST_SIZE);
}
static BT_HDR *make_ble_read_buffer_size(void) {
return make_command_no_params(HCI_BLE_READ_BUFFER_SIZE);
}
static BT_HDR *make_ble_read_supported_states(void) {
return make_command_no_params(HCI_BLE_READ_SUPPORTED_STATES);
}
static BT_HDR *make_ble_read_local_supported_features(void) {
return make_command_no_params(HCI_BLE_READ_LOCAL_SPT_FEAT);
}
static BT_HDR *make_ble_read_resolving_list_size(void) {
return make_command_no_params(HCI_BLE_READ_RESOLVING_LIST_SIZE);
}
static BT_HDR *make_ble_read_suggested_default_data_length(void) {
return make_command_no_params(HCI_BLE_READ_DEFAULT_DATA_LENGTH);
}
static BT_HDR *make_ble_set_event_mask(const bt_event_mask_t *event_mask) {
uint8_t *stream;
uint8_t parameter_size = sizeof(bt_event_mask_t);
BT_HDR *packet = make_command(HCI_BLE_SET_EVENT_MASK, parameter_size, &stream);
ARRAY8_TO_STREAM(stream, event_mask->as_array);
return packet;
}
// Internal functions
static BT_HDR *make_command_no_params(uint16_t opcode) {
return make_command(opcode, 0, NULL);
}
static BT_HDR *make_command(uint16_t opcode, size_t parameter_size, uint8_t **stream_out) {
BT_HDR *packet = make_packet(HCI_COMMAND_PREAMBLE_SIZE + parameter_size);
uint8_t *stream = packet->data;
UINT16_TO_STREAM(stream, opcode);
UINT8_TO_STREAM(stream, parameter_size);
if (stream_out != NULL)
*stream_out = stream;
return packet;
}
static BT_HDR *make_packet(size_t data_size) {
BT_HDR *ret = (BT_HDR *)buffer_allocator->alloc(sizeof(BT_HDR) + data_size);
assert(ret);
ret->event = 0;
ret->offset = 0;
ret->layer_specific = 0;
ret->len = data_size;
return ret;
}
static const hci_packet_factory_t interface = {
make_reset,
make_read_buffer_size,
make_host_buffer_size,
make_read_local_version_info,
make_read_bd_addr,
make_read_local_supported_commands,
make_read_local_extended_features,
make_write_simple_pairing_mode,
make_write_secure_connections_host_support,
make_set_event_mask,
make_ble_write_host_support,
make_ble_read_white_list_size,
make_ble_read_buffer_size,
make_ble_read_supported_states,
make_ble_read_local_supported_features,
make_ble_read_resolving_list_size,
make_ble_read_suggested_default_data_length,
make_ble_set_event_mask
};
const hci_packet_factory_t *hci_packet_factory_get_interface() {
buffer_allocator = buffer_allocator_get_interface();
return &interface;
}

View file

@ -0,0 +1,243 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include "bt_defs.h"
#include "buffer_allocator.h"
#include "bt_types.h"
#include "hcimsgs.h"
#include "hci_layer.h"
#include "hci_packet_parser.h"
static const command_opcode_t NO_OPCODE_CHECKING = 0;
static const allocator_t *buffer_allocator;
static uint8_t *read_command_complete_header(
BT_HDR *response,
command_opcode_t expected_opcode,
size_t minimum_bytes_after);
static void parse_generic_command_complete(BT_HDR *response) {
read_command_complete_header(response, NO_OPCODE_CHECKING, 0 /* bytes after */);
buffer_allocator->free(response);
}
static void parse_read_buffer_size_response(
BT_HDR *response,
uint16_t *data_size_ptr,
uint16_t *acl_buffer_count_ptr) {
uint8_t *stream = read_command_complete_header(response, HCI_READ_BUFFER_SIZE, 5 /* bytes after */);
assert(stream != NULL);
STREAM_TO_UINT16(*data_size_ptr, stream);
STREAM_SKIP_UINT8(stream); // skip the sco packet length
STREAM_TO_UINT16(*acl_buffer_count_ptr, stream);
buffer_allocator->free(response);
}
static void parse_read_local_version_info_response(
BT_HDR *response,
bt_version_t *bt_version) {
uint8_t *stream = read_command_complete_header(response, HCI_READ_LOCAL_VERSION_INFO, 8 /* bytes after */);
assert(stream != NULL);
STREAM_TO_UINT8(bt_version->hci_version, stream);
STREAM_TO_UINT16(bt_version->hci_revision, stream);
STREAM_TO_UINT8(bt_version->lmp_version, stream);
STREAM_TO_UINT16(bt_version->manufacturer, stream);
STREAM_TO_UINT16(bt_version->lmp_subversion, stream);
buffer_allocator->free(response);
}
static void parse_read_bd_addr_response(
BT_HDR *response,
bt_bdaddr_t *address_ptr) {
uint8_t *stream = read_command_complete_header(response, HCI_READ_BD_ADDR, sizeof(bt_bdaddr_t) /* bytes after */);
assert(stream != NULL);
STREAM_TO_BDADDR(address_ptr->address, stream);
buffer_allocator->free(response);
}
static void parse_read_local_supported_commands_response(
BT_HDR *response,
uint8_t *supported_commands_ptr,
size_t supported_commands_length) {
uint8_t *stream = read_command_complete_header(response, HCI_READ_LOCAL_SUPPORTED_CMDS, supported_commands_length /* bytes after */);
assert(stream != NULL);
STREAM_TO_ARRAY(supported_commands_ptr, stream, (int)supported_commands_length);
buffer_allocator->free(response);
}
static void parse_read_local_extended_features_response(
BT_HDR *response,
uint8_t *page_number_ptr,
uint8_t *max_page_number_ptr,
bt_device_features_t *feature_pages,
size_t feature_pages_count) {
uint8_t *stream = read_command_complete_header(response, HCI_READ_LOCAL_EXT_FEATURES, 2 + sizeof(bt_device_features_t) /* bytes after */);
if (stream != NULL) {
STREAM_TO_UINT8(*page_number_ptr, stream);
STREAM_TO_UINT8(*max_page_number_ptr, stream);
assert(*page_number_ptr < feature_pages_count);
STREAM_TO_ARRAY(feature_pages[*page_number_ptr].as_array, stream, (int)sizeof(bt_device_features_t));
} else {
LOG_ERROR("%s() - WARNING: READING EXTENDED FEATURES FAILED. "
"THIS MAY INDICATE A FIRMWARE/CONTROLLER ISSUE.", __func__);
}
buffer_allocator->free(response);
}
static void parse_ble_read_white_list_size_response(
BT_HDR *response,
uint8_t *white_list_size_ptr) {
uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_WHITE_LIST_SIZE, 1 /* byte after */);
assert(stream != NULL);
STREAM_TO_UINT8(*white_list_size_ptr, stream);
buffer_allocator->free(response);
}
static void parse_ble_read_buffer_size_response(
BT_HDR *response,
uint16_t *data_size_ptr,
uint8_t *acl_buffer_count_ptr) {
uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_BUFFER_SIZE, 3 /* bytes after */);
assert(stream != NULL);
STREAM_TO_UINT16(*data_size_ptr, stream);
STREAM_TO_UINT8(*acl_buffer_count_ptr, stream);
buffer_allocator->free(response);
}
static void parse_ble_read_supported_states_response(
BT_HDR *response,
uint8_t *supported_states,
size_t supported_states_size) {
uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_SUPPORTED_STATES, supported_states_size /* bytes after */);
assert(stream != NULL);
STREAM_TO_ARRAY(supported_states, stream, (int)supported_states_size);
buffer_allocator->free(response);
}
static void parse_ble_read_local_supported_features_response(
BT_HDR *response,
bt_device_features_t *supported_features) {
uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_LOCAL_SPT_FEAT, sizeof(bt_device_features_t) /* bytes after */);
assert(stream != NULL);
STREAM_TO_ARRAY(supported_features->as_array, stream, (int)sizeof(bt_device_features_t));
buffer_allocator->free(response);
}
static void parse_ble_read_resolving_list_size_response(
BT_HDR *response,
uint8_t *resolving_list_size_ptr) {
uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_RESOLVING_LIST_SIZE, 1 /* bytes after */);
STREAM_TO_UINT8(*resolving_list_size_ptr, stream);
buffer_allocator->free(response);
}
static void parse_ble_read_suggested_default_data_length_response(
BT_HDR *response,
uint16_t *ble_default_packet_length_ptr) {
uint8_t *stream = read_command_complete_header(response, HCI_BLE_READ_DEFAULT_DATA_LENGTH, 2 /* bytes after */);
STREAM_TO_UINT8(*ble_default_packet_length_ptr, stream);
buffer_allocator->free(response);
}
// Internal functions
static uint8_t *read_command_complete_header(
BT_HDR *response,
command_opcode_t expected_opcode,
size_t minimum_bytes_after) {
uint8_t *stream = response->data + response->offset;
// Read the event header
uint8_t event_code;
uint8_t parameter_length;
STREAM_TO_UINT8(event_code, stream);
STREAM_TO_UINT8(parameter_length, stream);
const size_t parameter_bytes_we_read_here = 4;
// Check the event header values against what we expect
assert(event_code == HCI_COMMAND_COMPLETE_EVT);
assert(parameter_length >= (parameter_bytes_we_read_here + minimum_bytes_after));
// Read the command complete header
command_opcode_t opcode;
uint8_t status;
STREAM_SKIP_UINT8(stream); // skip the number of hci command packets field
STREAM_TO_UINT16(opcode, stream);
// Check the command complete header values against what we expect
if (expected_opcode != NO_OPCODE_CHECKING) {
assert(opcode == expected_opcode);
}
// Assume the next field is the status field
STREAM_TO_UINT8(status, stream);
if (status != HCI_SUCCESS)
return NULL;
return stream;
}
static const hci_packet_parser_t interface = {
parse_generic_command_complete,
parse_read_buffer_size_response,
parse_read_local_version_info_response,
parse_read_bd_addr_response,
parse_read_local_supported_commands_response,
parse_read_local_extended_features_response,
parse_ble_read_white_list_size_response,
parse_ble_read_buffer_size_response,
parse_ble_read_supported_states_response,
parse_ble_read_local_supported_features_response,
parse_ble_read_resolving_list_size_response,
parse_ble_read_suggested_default_data_length_response
};
const hci_packet_parser_t *hci_packet_parser_get_interface() {
buffer_allocator = buffer_allocator_get_interface();
return &interface;
}

View file

@ -0,0 +1,362 @@
/******************************************************************************
*
* Copyright (C) 2009-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef BT_VENDOR_LIB_H
#define BT_VENDOR_LIB_H
#include <stdint.h>
//#include <sys/cdefs.h>
//#include <sys/types.h>
/** Struct types */
/** Typedefs and defines */
/** Vendor specific operations OPCODE */
typedef enum {
/* [operation]
* Power on or off the BT Controller.
* [input param]
* A pointer to int type with content of bt_vendor_power_state_t.
* Typecasting conversion: (int *) param.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_POWER_CTRL,
/* [operation]
* Perform any vendor specific initialization or configuration
* on the BT Controller. This is called before stack initialization.
* [input param]
* None.
* [return]
* 0 - default, don't care.
* [callback]
* Must call fwcfg_cb to notify the stack of the completion of vendor
* specific initialization once it has been done.
*/
BT_VND_OP_FW_CFG,
/* [operation]
* Perform any vendor specific SCO/PCM configuration on the BT Controller.
* This is called after stack initialization.
* [input param]
* None.
* [return]
* 0 - default, don't care.
* [callback]
* Must call scocfg_cb to notify the stack of the completion of vendor
* specific SCO configuration once it has been done.
*/
BT_VND_OP_SCO_CFG,
/* [operation]
* Open UART port on where the BT Controller is attached.
* This is called before stack initialization.
* [input param]
* A pointer to int array type for open file descriptors.
* The mapping of HCI channel to fd slot in the int array is given in
* bt_vendor_hci_channels_t.
* And, it requires the vendor lib to fill up the content before returning
* the call.
* Typecasting conversion: (int (*)[]) param.
* [return]
* Numbers of opened file descriptors.
* Valid number:
* 1 - CMD/EVT/ACL-In/ACL-Out via the same fd (e.g. UART)
* 2 - CMD/EVT on one fd, and ACL-In/ACL-Out on the other fd
* 4 - CMD, EVT, ACL-In, ACL-Out are on their individual fd
* [callback]
* None.
*/
BT_VND_OP_USERIAL_OPEN,
/* [operation]
* Close the previously opened UART port.
* [input param]
* None.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_USERIAL_CLOSE,
/* [operation]
* Get the LPM idle timeout in milliseconds.
* The stack uses this information to launch a timer delay before it
* attempts to de-assert LPM WAKE signal once downstream HCI packet
* has been delivered.
* [input param]
* A pointer to uint32_t type which is passed in by the stack. And, it
* requires the vendor lib to fill up the content before returning
* the call.
* Typecasting conversion: (uint32_t *) param.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_GET_LPM_IDLE_TIMEOUT,
/* [operation]
* Enable or disable LPM mode on BT Controller.
* [input param]
* A pointer to uint8_t type with content of bt_vendor_lpm_mode_t.
* Typecasting conversion: (uint8_t *) param.
* [return]
* 0 - default, don't care.
* [callback]
* Must call lpm_cb to notify the stack of the completion of LPM
* disable/enable process once it has been done.
*/
BT_VND_OP_LPM_SET_MODE,
/* [operation]
* Assert or Deassert LPM WAKE on BT Controller.
* [input param]
* A pointer to uint8_t type with content of bt_vendor_lpm_wake_state_t.
* Typecasting conversion: (uint8_t *) param.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_LPM_WAKE_SET_STATE,
/* [operation]
* Perform any vendor specific commands related to audio state changes.
* [input param]
* a pointer to bt_vendor_op_audio_state_t indicating what audio state is
* set.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_SET_AUDIO_STATE,
/* [operation]
* The epilog call to the vendor module so that it can perform any
* vendor-specific processes (e.g. send a HCI_RESET to BT Controller)
* before the caller calls for cleanup().
* [input param]
* None.
* [return]
* 0 - default, don't care.
* [callback]
* Must call epilog_cb to notify the stack of the completion of vendor
* specific epilog process once it has been done.
*/
BT_VND_OP_EPILOG,
} bt_vendor_opcode_t;
/** Power on/off control states */
typedef enum {
BT_VND_PWR_OFF,
BT_VND_PWR_ON,
} bt_vendor_power_state_t;
/** Define HCI channel identifier in the file descriptors array
used in BT_VND_OP_USERIAL_OPEN operation.
*/
typedef enum {
CH_CMD, // HCI Command channel
CH_EVT, // HCI Event channel
CH_ACL_OUT, // HCI ACL downstream channel
CH_ACL_IN, // HCI ACL upstream channel
CH_MAX // Total channels
} bt_vendor_hci_channels_t;
/** LPM disable/enable request */
typedef enum {
BT_VND_LPM_DISABLE,
BT_VND_LPM_ENABLE,
} bt_vendor_lpm_mode_t;
/** LPM WAKE set state request */
typedef enum {
BT_VND_LPM_WAKE_ASSERT,
BT_VND_LPM_WAKE_DEASSERT,
} bt_vendor_lpm_wake_state_t;
/** Callback result values */
typedef enum {
BT_VND_OP_RESULT_SUCCESS,
BT_VND_OP_RESULT_FAIL,
} bt_vendor_op_result_t;
/** audio (SCO) state changes triggering VS commands for configuration */
typedef struct {
uint16_t handle;
uint16_t peer_codec;
uint16_t state;
} bt_vendor_op_audio_state_t;
/*
* Bluetooth Host/Controller Vendor callback structure.
*/
/* vendor initialization/configuration callback */
typedef void (*cfg_result_cb)(bt_vendor_op_result_t result);
/* datapath buffer allocation callback (callout)
*
* Vendor lib needs to request a buffer through the alloc callout function
* from HCI lib if the buffer is for constructing a HCI Command packet which
* will be sent through xmit_cb to BT Controller.
*
* For each buffer allocation, the requested size needs to be big enough to
* accommodate the below header plus a complete HCI packet --
* typedef struct
* {
* uint16_t event;
* uint16_t len;
* uint16_t offset;
* uint16_t layer_specific;
* } HC_BT_HDR;
*
* HCI lib returns a pointer to the buffer where Vendor lib should use to
* construct a HCI command packet as below format:
*
* --------------------------------------------
* | HC_BT_HDR | HCI command |
* --------------------------------------------
* where
* HC_BT_HDR.event = 0x2000;
* HC_BT_HDR.len = Length of HCI command;
* HC_BT_HDR.offset = 0;
* HC_BT_HDR.layer_specific = 0;
*
* For example, a HCI_RESET Command will be formed as
* ------------------------
* | HC_BT_HDR |03|0c|00|
* ------------------------
* with
* HC_BT_HDR.event = 0x2000;
* HC_BT_HDR.len = 3;
* HC_BT_HDR.offset = 0;
* HC_BT_HDR.layer_specific = 0;
*/
typedef void* (*malloc_cb)(int size);
/* datapath buffer deallocation callback (callout) */
typedef void (*mdealloc_cb)(void *p_buf);
/* define callback of the cmd_xmit_cb
*
* The callback function which HCI lib will call with the return of command
* complete packet. Vendor lib is responsible for releasing the buffer passed
* in at the p_mem parameter by calling dealloc callout function.
*/
typedef void (*tINT_CMD_CBACK)(void *p_mem);
/* hci command packet transmit callback (callout)
*
* Vendor lib calls xmit_cb callout function in order to send a HCI Command
* packet to BT Controller. The buffer carrying HCI Command packet content
* needs to be first allocated through the alloc callout function.
* HCI lib will release the buffer for Vendor lib once it has delivered the
* packet content to BT Controller.
*
* Vendor lib needs also provide a callback function (p_cback) which HCI lib
* will call with the return of command complete packet.
*
* The opcode parameter gives the HCI OpCode (combination of OGF and OCF) of
* HCI Command packet. For example, opcode = 0x0c03 for the HCI_RESET command
* packet.
*/
typedef uint8_t (*cmd_xmit_cb)(uint16_t opcode, void *p_buf, tINT_CMD_CBACK p_cback);
typedef struct {
/** set to sizeof(bt_vendor_callbacks_t) */
size_t size;
/*
* Callback and callout functions have implemented in HCI libray
* (libbt-hci.so).
*/
/* notifies caller result of firmware configuration request */
cfg_result_cb fwcfg_cb;
/* notifies caller result of sco configuration request */
cfg_result_cb scocfg_cb;
/* notifies caller result of lpm enable/disable */
cfg_result_cb lpm_cb;
/* notifies the result of codec setting */
cfg_result_cb audio_state_cb;
/* buffer allocation request */
malloc_cb alloc;
/* buffer deallocation request */
mdealloc_cb dealloc;
/* hci command packet transmit request */
cmd_xmit_cb xmit_cb;
/* notifies caller completion of epilog process */
cfg_result_cb epilog_cb;
} bt_vendor_callbacks_t;
/*
* Bluetooth Host/Controller VENDOR Interface
*/
typedef struct {
/** Set to sizeof(bt_vndor_interface_t) */
size_t size;
/*
* Functions need to be implemented in Vendor libray (libbt-vendor.so).
*/
/**
* Caller will open the interface and pass in the callback routines
* to the implemenation of this interface.
*/
int (*init)(const bt_vendor_callbacks_t* p_cb, unsigned char *local_bdaddr);
/** Vendor specific operations */
int (*op)(bt_vendor_opcode_t opcode, void *param);
/** Closes the interface */
void (*cleanup)(void);
} bt_vendor_interface_t;
/*
* External shared lib functions/data
*/
/* Entry point of DLib --
* Vendor library needs to implement the body of bt_vendor_interface_t
* structure and uses the below name as the variable name. HCI library
* will use this symbol name to get address of the object through the
* dlsym call.
*/
//extern const bt_vendor_interface_t BLUETOOTH_VENDOR_LIB_INTERFACE;
#endif /* BT_VENDOR_LIB_H */

View file

@ -0,0 +1,25 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _BUFFER_ALLOCATOR_H_
#include "allocator.h"
const allocator_t *buffer_allocator_get_interface();
#endif /*_BUFFER_ALLOCATOR_H_*/

View file

@ -0,0 +1,85 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _HCI_HAL_H_
#define _HCI_HAL_H_
#include <stdbool.h>
#include <stdint.h>
#include "bt_types.h"
typedef enum {
DATA_TYPE_COMMAND = 1,
DATA_TYPE_ACL = 2,
DATA_TYPE_SCO = 3,
DATA_TYPE_EVENT = 4
} serial_data_type_t;
typedef void (*packet_ready_cb)(BT_HDR *packet);
typedef struct {
// Called when the HAL detects inbound data.
// Data |type| may be ACL, SCO, or EVENT.
// Executes in the context of the thread supplied to |init|.
packet_ready_cb packet_ready;
/*
// Called when the HAL detects inbound astronauts named Dave.
// HAL will deny all requests to open the pod bay doors after this.
dave_ready_cb dave_ready;
*/
} hci_hal_callbacks_t;
typedef struct hci_hal_t {
// Initialize the HAL, with |upper_callbacks| and |upper_thread| to run in the context of.
//bool (*init)(const hci_hal_callbacks_t *upper_callbacks);
// Connect to the underlying hardware, and let data start flowing.
bool (*open)(const hci_hal_callbacks_t *upper_callbacks);
// Disconnect from the underlying hardware, and close the HAL.
// "Daisy, Daisy..."
void (*close)(void);
// Retrieve up to |max_size| bytes for ACL, SCO, or EVENT data packets into
// |buffer|, blocking until max_size bytes read if |block| is true.
// Only guaranteed to be correct in the context of a data_ready callback
// of the corresponding type.
//size_t (*read_data)(serial_data_type_t type, uint8_t *buffer, size_t max_size);
// The upper layer must call this to notify the HAL that it has finished
// reading a packet of the specified |type|. Underlying implementations that
// use shared channels for multiple data types depend on this to know when
// to reinterpret the data stream.
//void (*packet_finished)(serial_data_type_t type);
// Transmit COMMAND, ACL, or SCO data packets.
// |data| may not be NULL. |length| must be greater than zero.
//
// IMPORTANT NOTE:
// Depending on the underlying implementation, the byte right
// before the beginning of |data| may be borrowed during this call
// and then restored to its original value.
// This is safe in the bluetooth context, because there is always a buffer
// header that prefixes data you're sending.
uint16_t (*transmit_data)(serial_data_type_t type, uint8_t *data, uint16_t length);
} hci_hal_t;
// Gets the correct hal implementation, as compiled for.
const hci_hal_t *hci_hal_h4_get_interface(void);
#endif /* _HCI_HAL_H */

View file

@ -0,0 +1,31 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _HCI_INTERNALS_H_
#define _HCI_INTERNALS_H_
// 2 bytes for opcode, 1 byte for parameter length (Volume 2, Part E, 5.4.1)
#define HCI_COMMAND_PREAMBLE_SIZE 3
// 2 bytes for handle, 2 bytes for data length (Volume 2, Part E, 5.4.2)
#define HCI_ACL_PREAMBLE_SIZE 4
// 2 bytes for handle, 1 byte for data length (Volume 2, Part E, 5.4.3)
#define HCI_SCO_PREAMBLE_SIZE 3
// 1 byte for event code, 1 byte for parameter length (Volume 2, Part E, 5.4.4)
#define HCI_EVENT_PREAMBLE_SIZE 2
#endif /* _HCI_INTERNALS_H_ */

View file

@ -0,0 +1,101 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _HCI_LAYER_H_
#define _HCI_LAYER_H_
#include "bt_types.h"
#include "allocator.h"
#include "fixed_queue.h"
#include "osi.h"
///// LEGACY DEFINITIONS /////
/* Message event mask across Host/Controller lib and stack */
#define MSG_EVT_MASK 0xFF00 /* eq. BT_EVT_MASK */
#define MSG_SUB_EVT_MASK 0x00FF /* eq. BT_SUB_EVT_MASK */
/* Message event ID passed from Host/Controller lib to stack */
#define MSG_HC_TO_STACK_HCI_ERR 0x1300 /* eq. BT_EVT_TO_BTU_HCIT_ERR */
#define MSG_HC_TO_STACK_HCI_ACL 0x1100 /* eq. BT_EVT_TO_BTU_HCI_ACL */
#define MSG_HC_TO_STACK_HCI_SCO 0x1200 /* eq. BT_EVT_TO_BTU_HCI_SCO */
#define MSG_HC_TO_STACK_HCI_EVT 0x1000 /* eq. BT_EVT_TO_BTU_HCI_EVT */
#define MSG_HC_TO_STACK_L2C_SEG_XMIT 0x1900 /* eq. BT_EVT_TO_BTU_L2C_SEG_XMIT */
/* Message event ID passed from stack to vendor lib */
#define MSG_STACK_TO_HC_HCI_ACL 0x2100 /* eq. BT_EVT_TO_LM_HCI_ACL */
#define MSG_STACK_TO_HC_HCI_SCO 0x2200 /* eq. BT_EVT_TO_LM_HCI_SCO */
#define MSG_STACK_TO_HC_HCI_CMD 0x2000 /* eq. BT_EVT_TO_LM_HCI_CMD */
/* Local Bluetooth Controller ID for BR/EDR */
#define LOCAL_BR_EDR_CONTROLLER_ID 0
///// END LEGACY DEFINITIONS /////
typedef struct hci_hal_t hci_hal_t;
//typedef struct btsnoop_t btsnoop_t;
typedef struct controller_t controller_t;
//typedef struct hci_inject_t hci_inject_t;
typedef struct packet_fragmenter_t packet_fragmenter_t;
//typedef struct vendor_t vendor_t;
//typedef struct low_power_manager_t low_power_manager_t;
//typedef unsigned char * bdaddr_t;
typedef uint16_t command_opcode_t;
/*
typedef enum {
LPM_DISABLE,
LPM_ENABLE,
LPM_WAKE_ASSERT,
LPM_WAKE_DEASSERT
} low_power_command_t;
*/
typedef void (*command_complete_cb)(BT_HDR *response, void *context);
typedef void (*command_status_cb)(uint8_t status, BT_HDR *command, void *context);
typedef struct hci_t {
// Send a low power command, if supported and the low power manager is enabled.
//void (*send_low_power_command)(low_power_command_t command);
// Do the postload sequence (call after the rest of the BT stack initializes).
void (*do_postload)(void);
// Set the queue to receive ACL data in
void (*set_data_queue)(fixed_queue_t *queue);
// Send a command through the HCI layer
void (*transmit_command)(
BT_HDR *command,
command_complete_cb complete_callback,
command_status_cb status_cb,
void *context
);
// Send some data downward through the HCI layer
void (*transmit_downward)(uint16_t type, void *data);
} hci_t;
const hci_t *hci_layer_get_interface();
int hci_start_up(void);
void hci_shut_down(void);
#endif /* _HCI_LAYER_H_ */

View file

@ -0,0 +1,48 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _HCI_PACKET_FACTORY_H_
#define _HCI_PACKET_FACTORY_H_
#include "bt_types.h"
#include "event_mask.h"
typedef struct {
BT_HDR *(*make_reset)(void);
BT_HDR *(*make_read_buffer_size)(void);
BT_HDR *(*make_host_buffer_size)(uint16_t acl_size, uint8_t sco_size, uint16_t acl_count, uint16_t sco_count);
BT_HDR *(*make_read_local_version_info)(void);
BT_HDR *(*make_read_bd_addr)(void);
BT_HDR *(*make_read_local_supported_commands)(void);
BT_HDR *(*make_read_local_extended_features)(uint8_t page_number);
BT_HDR *(*make_write_simple_pairing_mode)(uint8_t mode);
BT_HDR *(*make_write_secure_connections_host_support)(uint8_t mode);
BT_HDR *(*make_set_event_mask)(const bt_event_mask_t *event_mask);
BT_HDR *(*make_ble_write_host_support)(uint8_t supported_host, uint8_t simultaneous_host);
BT_HDR *(*make_ble_read_white_list_size)(void);
BT_HDR *(*make_ble_read_buffer_size)(void);
BT_HDR *(*make_ble_read_supported_states)(void);
BT_HDR *(*make_ble_read_local_supported_features)(void);
BT_HDR *(*make_ble_read_resolving_list_size)(void);
BT_HDR *(*make_ble_read_suggested_default_data_length)(void);
BT_HDR *(*make_ble_set_event_mask)(const bt_event_mask_t *event_mask);
} hci_packet_factory_t;
const hci_packet_factory_t *hci_packet_factory_get_interface();
#endif /*_HCI_PACKET_FACTORY_H_*/

View file

@ -0,0 +1,99 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _HCI_PACKET_PARSER_H_
#define _HCI_PACKET_PARSER_H_
#include <stdint.h>
#include "allocator.h"
#include "bdaddr.h"
#include "bt_types.h"
#include "device_features.h"
//#include "features.h"
#include "version.h"
typedef struct {
void (*parse_generic_command_complete)(BT_HDR *response);
void (*parse_read_buffer_size_response)(
BT_HDR *response,
uint16_t *data_size_ptr,
uint16_t *acl_buffer_count_ptr
);
void (*parse_read_local_version_info_response)(
BT_HDR *response,
bt_version_t *bt_version_ptr
);
void (*parse_read_bd_addr_response)(
BT_HDR *response,
bt_bdaddr_t *address_ptr
);
void (*parse_read_local_supported_commands_response)(
BT_HDR *response,
uint8_t *supported_commands_ptr,
size_t supported_commands_length
);
void (*parse_read_local_extended_features_response)(
BT_HDR *response,
uint8_t *page_number_ptr,
uint8_t *max_page_number_ptr,
bt_device_features_t *feature_pages,
size_t feature_pages_count
);
void (*parse_ble_read_white_list_size_response)(
BT_HDR *response,
uint8_t *white_list_size_ptr
);
void (*parse_ble_read_buffer_size_response)(
BT_HDR *response,
uint16_t *data_size_ptr,
uint8_t *acl_buffer_count_ptr
);
void (*parse_ble_read_supported_states_response)(
BT_HDR *response,
uint8_t *supported_states,
size_t supported_states_size
);
void (*parse_ble_read_local_supported_features_response)(
BT_HDR *response,
bt_device_features_t *supported_features
);
void (*parse_ble_read_resolving_list_size_response) (
BT_HDR *response,
uint8_t *resolving_list_size_ptr
);
void (*parse_ble_read_suggested_default_data_length_response)(
BT_HDR *response,
uint16_t *ble_default_packet_length_ptr
);
} hci_packet_parser_t;
const hci_packet_parser_t *hci_packet_parser_get_interface();
#endif /*_HCI_PACKET_PARSER_H_*/

View file

@ -0,0 +1,62 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _PACKET_FRAGMENTER_H_
#define _PACKET_FRAGMENTER_H_
#include "allocator.h"
#include "bt_types.h"
#include "hci_layer.h"
typedef void (*transmit_finished_cb)(BT_HDR *packet, bool all_fragments_sent);
typedef void (*packet_reassembled_cb)(BT_HDR *packet);
typedef void (*packet_fragmented_cb)(BT_HDR *packet, bool send_transmit_finished);
typedef struct {
// Called for every packet fragment.
packet_fragmented_cb fragmented;
// Called for every completely reassembled packet.
packet_reassembled_cb reassembled;
// Called when the fragmenter finishes sending all requested fragments,
// but the packet has not been entirely sent.
transmit_finished_cb transmit_finished;
} packet_fragmenter_callbacks_t;
typedef struct packet_fragmenter_t {
// Initialize the fragmenter, specifying the |result_callbacks|.
void (*init)(const packet_fragmenter_callbacks_t *result_callbacks);
// Release all resources associated with the fragmenter.
void (*cleanup)(void);
// CHeck if Current fragmenter is ongoing
BT_HDR *(*fragment_current_packet)(void);
// Fragments |packet| if necessary and hands off everything to the fragmented callback.
void (*fragment_and_dispatch)(BT_HDR *packet);
// If |packet| is a complete packet, forwards to the reassembled callback. Otherwise
// holds onto it until all fragments arrive, at which point the reassembled callback is called
// with the reassembled data.
void (*reassemble_and_dispatch)(BT_HDR *packet);
} packet_fragmenter_t;
const packet_fragmenter_t *packet_fragmenter_get_interface();
#endif /* _PACKET_FRAGMENTER_H_ */

View file

@ -0,0 +1,225 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <string.h>
#include "bt_trace.h"
#include "bt_defs.h"
#include "controller.h"
#include "buffer_allocator.h"
#include "hci_internals.h"
#include "hci_layer.h"
#include "packet_fragmenter.h"
#include "hash_map.h"
#include "hash_functions.h"
#define APPLY_CONTINUATION_FLAG(handle) (((handle) & 0xCFFF) | 0x1000)
#define APPLY_START_FLAG(handle) (((handle) & 0xCFFF) | 0x2000)
#define SUB_EVENT(event) ((event) & MSG_SUB_EVT_MASK)
#define GET_BOUNDARY_FLAG(handle) (((handle) >> 12) & 0x0003)
#define HANDLE_MASK 0x0FFF
#define START_PACKET_BOUNDARY 2
#define CONTINUATION_PACKET_BOUNDARY 1
#define L2CAP_HEADER_SIZE 4
// TODO(zachoverflow): find good value for this
#define NUMBER_OF_BUCKETS 42
// Our interface and callbacks
static const packet_fragmenter_t interface;
static const allocator_t *buffer_allocator;
static const controller_t *controller;
static const packet_fragmenter_callbacks_t *callbacks;
static hash_map_t *partial_packets;
static BT_HDR *current_fragment_packet;
static void init(const packet_fragmenter_callbacks_t *result_callbacks) {
current_fragment_packet = NULL;
callbacks = result_callbacks;
partial_packets = hash_map_new(NUMBER_OF_BUCKETS, hash_function_naive, NULL, NULL, NULL);
}
static void cleanup() {
if (partial_packets)
hash_map_free(partial_packets);
}
static BT_HDR *fragment_get_current_packet() {
return current_fragment_packet;
}
static void fragment_and_dispatch(BT_HDR *packet) {
uint16_t continuation_handle, current_pkt_size;
uint16_t max_data_size, max_packet_size, remaining_length;
uint16_t event = packet->event & MSG_EVT_MASK;
uint8_t *stream = packet->data + packet->offset;
assert(packet != NULL);
// We only fragment ACL packets
if (event != MSG_STACK_TO_HC_HCI_ACL) {
callbacks->fragmented(packet, true);
return;
}
max_data_size =
SUB_EVENT(packet->event) == LOCAL_BR_EDR_CONTROLLER_ID ?
controller->get_acl_data_size_classic() :
controller->get_acl_data_size_ble();
max_packet_size = max_data_size + HCI_ACL_PREAMBLE_SIZE;
remaining_length = packet->len;
STREAM_TO_UINT16(continuation_handle, stream);
continuation_handle = APPLY_CONTINUATION_FLAG(continuation_handle);
if (remaining_length > max_packet_size) {
current_fragment_packet = packet;
UINT16_TO_STREAM(stream, max_packet_size);
packet->len = max_packet_size;
callbacks->fragmented(packet, false);
packet->offset += max_data_size;
remaining_length -= max_data_size;
packet->len = remaining_length;
// Write the ACL header for the next fragment
stream = packet->data + packet->offset;
UINT16_TO_STREAM(stream, continuation_handle);
UINT16_TO_STREAM(stream, remaining_length - HCI_ACL_PREAMBLE_SIZE);
// Apparently L2CAP can set layer_specific to a max number of segments to transmit
if (packet->layer_specific) {
packet->layer_specific--;
if (packet->layer_specific == 0) {
packet->event = MSG_HC_TO_STACK_L2C_SEG_XMIT;
callbacks->transmit_finished(packet, false);
return;
}
}
} else {
current_fragment_packet = NULL;
callbacks->fragmented(packet, true);
}
}
static void reassemble_and_dispatch(BT_HDR *packet) {
if ((packet->event & MSG_EVT_MASK) == MSG_HC_TO_STACK_HCI_ACL) {
uint8_t *stream = packet->data + packet->offset;
uint16_t handle;
uint16_t l2cap_length;
uint16_t acl_length;
uint8_t boundary_flag;
BT_HDR *partial_packet;
STREAM_TO_UINT16(handle, stream);
STREAM_TO_UINT16(acl_length, stream);
STREAM_TO_UINT16(l2cap_length, stream);
assert(acl_length == packet->len - HCI_ACL_PREAMBLE_SIZE);
boundary_flag = GET_BOUNDARY_FLAG(handle);
handle = handle & HANDLE_MASK;
partial_packet = (BT_HDR *)hash_map_get(partial_packets, (void *)(uintptr_t)handle);
if (boundary_flag == START_PACKET_BOUNDARY) {
uint16_t full_length;
if (partial_packet) {
LOG_WARN("%s found unfinished packet for handle with start packet. Dropping old.", __func__);
hash_map_erase(partial_packets, (void *)(uintptr_t)handle);
buffer_allocator->free(partial_packet);
}
full_length = l2cap_length + L2CAP_HEADER_SIZE + HCI_ACL_PREAMBLE_SIZE;
if (full_length <= packet->len) {
if (full_length < packet->len)
LOG_WARN("%s found l2cap full length %d less than the hci length %d.", __func__, l2cap_length, packet->len);
callbacks->reassembled(packet);
return;
}
partial_packet = (BT_HDR *)buffer_allocator->alloc(full_length + sizeof(BT_HDR));
partial_packet->event = packet->event;
partial_packet->len = full_length;
partial_packet->offset = packet->len;
memcpy(partial_packet->data, packet->data, packet->len);
// Update the ACL data size to indicate the full expected length
stream = partial_packet->data;
STREAM_SKIP_UINT16(stream); // skip the handle
UINT16_TO_STREAM(stream, full_length - HCI_ACL_PREAMBLE_SIZE);
hash_map_set(partial_packets, (void *)(uintptr_t)handle, partial_packet);
// Free the old packet buffer, since we don't need it anymore
buffer_allocator->free(packet);
} else {
uint16_t projected_offset;
if (!partial_packet) {
LOG_WARN("%s got continuation for unknown packet. Dropping it.", __func__);
buffer_allocator->free(packet);
return;
}
packet->offset = HCI_ACL_PREAMBLE_SIZE;
projected_offset = partial_packet->offset + (packet->len - HCI_ACL_PREAMBLE_SIZE);
if (projected_offset > partial_packet->len) { // len stores the expected length
LOG_WARN("%s got packet which would exceed expected length of %d. Truncating.", __func__, partial_packet->len);
packet->len = partial_packet->len - partial_packet->offset;
projected_offset = partial_packet->len;
}
memcpy(
partial_packet->data + partial_packet->offset,
packet->data + packet->offset,
packet->len - packet->offset
);
// Free the old packet buffer, since we don't need it anymore
buffer_allocator->free(packet);
partial_packet->offset = projected_offset;
if (partial_packet->offset == partial_packet->len) {
hash_map_erase(partial_packets, (void *)(uintptr_t)handle);
partial_packet->offset = 0;
callbacks->reassembled(partial_packet);
}
}
} else {
callbacks->reassembled(packet);
}
}
static const packet_fragmenter_t interface = {
init,
cleanup,
fragment_get_current_packet,
fragment_and_dispatch,
reassemble_and_dispatch
};
const packet_fragmenter_t *packet_fragmenter_get_interface() {
controller = controller_get_interface();
buffer_allocator = buffer_allocator_get_interface();
return &interface;
}

View file

@ -0,0 +1,31 @@
#ifndef _BT_COMMON_TYPES_H_
#define _BT_COMMON_TYPES_H_
#include "bt_defs.h"
#include "thread.h"
extern xQueueHandle xBtaApp1Queue;
extern xTaskHandle xBtaApp1TaskHandle;
typedef struct {
uint8_t client_if;
uint8_t filt_index;
uint8_t advertiser_state;
uint8_t advertiser_info_present;
uint8_t addr_type;
uint8_t tx_power;
int8_t rssi_value;
uint16_t time_stamp;
bt_bdaddr_t bd_addr;
uint8_t adv_pkt_len;
uint8_t *p_adv_pkt_data;
uint8_t scan_rsp_len;
uint8_t *p_scan_rsp_data;
} btgatt_track_adv_info_t;
#endif

View file

@ -0,0 +1,90 @@
/**
* bt_defs.h Defines useful API for whole Bluedroid
*
*/
#ifndef _BT_DEFS_H_
#define _BT_DEFS_H_
#include <stdlib.h>
#include <stdint.h>
#include "bt_trace.h"
#include "osi_arch.h"
#define UNUSED(x) (void)(x)
#ifndef SIZE_MAX
#define SIZE_MAX 128
#endif
/*Timer Related Defination*/
#define alarm_timer_t uint32_t
#define alarm_timer_setfn(timer, cb, data) \
do { \
} while (0)
#define alarm_timer_arm(timer, to, periodic) \
do { \
} while (0)
#define alarm_timer_disarm(timer) \
do { \
} while (0)
#define alarm_timer_now() (0)
/*Thread and locker related defination*/
#define RTOS_SUPPORT
#ifdef RTOS_SUPPORT
#define pthread_mutex_t osi_mutex_t
#define pthread_mutex_init(mutex, a) osi_mutex_new(mutex)
#define pthread_mutex_destroy(mutex) osi_mutex_free(mutex)
#define pthread_mutex_lock osi_mutex_lock
#define pthread_mutex_unlock osi_mutex_unlock
#else
#define pthread_mutex_t uint8_t
#define pthread_mutex_init(x1, x2)
#define pthread_mutex_destroy(mutex)
#define pthread_mutex_lock(mutex)
#define pthread_mutex_unlock(mutex)
#endif
/*Bluetooth Address*/
typedef struct {
uint8_t address[6];
} __attribute__ ((__packed__)) bt_bdaddr_t;
#ifndef CPU_LITTLE_ENDIAN
#define CPU_LITTLE_ENDIAN
#endif
inline uint16_t swap_byte_16(uint16_t x) {
return (((x & 0x00ffU) << 8) |
((x & 0xff00U) >> 8));
}
inline uint32_t swap_byte_32(uint32_t x) {
return (((x & 0x000000ffUL) << 24) |
((x & 0x0000ff00UL) << 8) |
((x & 0x00ff0000UL) >> 8) |
((x & 0xff000000UL) >> 24));
}
inline uint16_t ntohs(uint16_t x) {
#ifdef CPU_LITTLE_ENDIAN
return swap_byte_16(x);
#else
return x;
#endif
}
typedef struct vhci_host_callback {
void (*notify_host_send_available)(void);
int (*notify_host_recv)(uint8_t *data, uint16_t len);
} vhci_host_callback_t;
bool API_vhci_host_check_send_available(void);
void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
void API_vhci_host_register_callback(const vhci_host_callback_t *callback);
#endif /* _BT_DEFS_H_ */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,462 @@
/******************************************************************************
*
* Copyright (C) 1999-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _BT_TRACE_H_
#define _BT_TRACE_H_
#include "bt_types.h"
#include "rom/ets_sys.h"
//extern void ets_printf(const char *fmt, ...);
#define assert(x) do { if (!(x)) ets_printf("bt host error %s %u\n", __FILE__, __LINE__); } while (0)
inline void trc_dump_buffer(uint8_t *prefix, uint8_t *data, uint16_t len)
{
uint16_t i;
if (!data || !len)
return;
if (prefix)
ets_printf("%s:\t", prefix);
for (i = 0; i < len; i++) {
ets_printf(" %02x", *(data + i));
if (!((i + 1) & 0xf))
ets_printf("\n");
}
ets_printf("\n");
}
#ifdef BTTRC_DUMP_BUFFER
#define BTTRC_DUMP_BUFFER(_prefix, _data, _len) trc_dump_buffer(_data, _len)
#else
#define BTTRC_DUMP_BUFFER(_prefix, _data, _len)
#endif
//static const char BTE_LOGMSG_MODULE[] = "bte_logmsg_module";
/* BTrgs);E tracing IDs for debug purposes */
/* LayerIDs for stack */
#define BTTRC_ID_STK_GKI 1
#define BTTRC_ID_STK_BTU 2
#define BTTRC_ID_STK_HCI 3
#define BTTRC_ID_STK_L2CAP 4
#define BTTRC_ID_STK_RFCM_MX 5
#define BTTRC_ID_STK_RFCM_PRT 6
#define BTTRC_ID_STK_OBEX_C 7
#define BTTRC_ID_STK_OBEX_S 8
#define BTTRC_ID_STK_AVCT 9
#define BTTRC_ID_STK_AVDT 10
#define BTTRC_ID_STK_AVRC 11
#define BTTRC_ID_STK_BIC 12
#define BTTRC_ID_STK_BIS 13
#define BTTRC_ID_STK_BNEP 14
#define BTTRC_ID_STK_BPP 15
#define BTTRC_ID_STK_BTM_ACL 16
#define BTTRC_ID_STK_BTM_PM 17
#define BTTRC_ID_STK_BTM_DEV_CTRL 18
#define BTTRC_ID_STK_BTM_SVC_DSC 19
#define BTTRC_ID_STK_BTM_INQ 20
#define BTTRC_ID_STK_BTM_SCO 21
#define BTTRC_ID_STK_BTM_SEC 22
#define BTTRC_ID_STK_HID 24
#define BTTRC_ID_STK_HSP2 25
#define BTTRC_ID_STK_CTP 26
#define BTTRC_ID_STK_FTC 27
#define BTTRC_ID_STK_FTS 28
#define BTTRC_ID_STK_GAP 29
#define BTTRC_ID_STK_HCRP 31
#define BTTRC_ID_STK_ICP 32
#define BTTRC_ID_STK_OPC 33
#define BTTRC_ID_STK_OPS 34
#define BTTRC_ID_STK_PAN 35
#define BTTRC_ID_STK_SAP 36
#define BTTRC_ID_STK_SDP 37
#define BTTRC_ID_STK_SLIP 38
#define BTTRC_ID_STK_SPP 39
#define BTTRC_ID_STK_TCS 40
#define BTTRC_ID_STK_VDP 41
#define BTTRC_ID_STK_MCAP 42
#define BTTRC_ID_STK_GATT 43
#define BTTRC_ID_STK_SMP 44
#define BTTRC_ID_STK_NFC 45
#define BTTRC_ID_STK_NCI 46
#define BTTRC_ID_STK_IDEP 47
#define BTTRC_ID_STK_NDEP 48
#define BTTRC_ID_STK_LLCP 49
#define BTTRC_ID_STK_RW 50
#define BTTRC_ID_STK_CE 51
#define BTTRC_ID_STK_SNEP 52
#define BTTRC_ID_STK_NDEF 53
/* LayerIDs for BTA */
#define BTTRC_ID_BTA_ACC 55 /* Advanced Camera Client */
#define BTTRC_ID_BTA_AG 56 /* audio gateway */
#define BTTRC_ID_BTA_AV 57 /* Advanced audio */
#define BTTRC_ID_BTA_BIC 58 /* Basic Imaging Client */
#define BTTRC_ID_BTA_BIS 59 /* Basic Imaging Server */
#define BTTRC_ID_BTA_BP 60 /* Basic Printing Client */
#define BTTRC_ID_BTA_CG 61
#define BTTRC_ID_BTA_CT 62 /* cordless telephony terminal */
#define BTTRC_ID_BTA_DG 63 /* data gateway */
#define BTTRC_ID_BTA_DM 64 /* device manager */
#define BTTRC_ID_BTA_DM_SRCH 65 /* device manager search */
#define BTTRC_ID_BTA_DM_SEC 66 /* device manager security */
#define BTTRC_ID_BTA_FM 67
#define BTTRC_ID_BTA_FTC 68 /* file transfer client */
#define BTTRC_ID_BTA_FTS 69 /* file transfer server */
#define BTTRC_ID_BTA_HIDH 70
#define BTTRC_ID_BTA_HIDD 71
#define BTTRC_ID_BTA_JV 72
#define BTTRC_ID_BTA_OPC 73 /* object push client */
#define BTTRC_ID_BTA_OPS 74 /* object push server */
#define BTTRC_ID_BTA_PAN 75 /* Personal Area Networking */
#define BTTRC_ID_BTA_PR 76 /* Printer client */
#define BTTRC_ID_BTA_SC 77 /* SIM Card Access server */
#define BTTRC_ID_BTA_SS 78 /* synchronization server */
#define BTTRC_ID_BTA_SYS 79 /* system manager */
#define BTTRC_ID_AVDT_SCB 80 /* avdt scb */
#define BTTRC_ID_AVDT_CCB 81 /* avdt ccb */
// btla-specific ++
/* LayerIDs added for BTL-A. Probably should modify bte_logmsg.c in future. */
#define BTTRC_ID_STK_RFCOMM 82
#define BTTRC_ID_STK_RFCOMM_DATA 83
#define BTTRC_ID_STK_OBEX 84
#define BTTRC_ID_STK_A2D 85
#define BTTRC_ID_STK_BIP 86
/* LayerIDs for BT APP */
#define BTTRC_ID_BTAPP 87
#define BTTRC_ID_BT_PROTOCOL 88 /* this is a temporary solution to allow dynamic
enable/disable of BT_PROTOCOL_TRACE */
#define BTTRC_ID_MAX_ID BTTRC_ID_BT_PROTOCOL
// btla-specific --
#define BTTRC_ID_ALL_LAYERS 0xFF /* all trace layers */
/* Parameter datatypes used in Trace APIs */
#define BTTRC_PARAM_UINT8 1
#define BTTRC_PARAM_UINT16 2
#define BTTRC_PARAM_UINT32 3
/* Enables or disables verbose trace information. */
#ifndef BT_TRACE_VERBOSE
#define BT_TRACE_VERBOSE FALSE
#endif
/* Enables or disables all trace messages. */
#ifndef BT_USE_TRACES
#define BT_USE_TRACES TRUE
#endif
/******************************************************************************
**
** Trace Levels
**
** The following values may be used for different levels:
** BT_TRACE_LEVEL_NONE 0 * No trace messages to be generated
** BT_TRACE_LEVEL_ERROR 1 * Error condition trace messages
** BT_TRACE_LEVEL_WARNING 2 * Warning condition trace messages
** BT_TRACE_LEVEL_API 3 * API traces
** BT_TRACE_LEVEL_EVENT 4 * Debug messages for events
** BT_TRACE_LEVEL_DEBUG 5 * Debug messages (general)
******************************************************************************/
// btla-specific ++
/* Core Stack default trace levels */
#ifndef HCI_INITIAL_TRACE_LEVEL
#define HCI_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef BTM_INITIAL_TRACE_LEVEL
#define BTM_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef L2CAP_INITIAL_TRACE_LEVEL
#define L2CAP_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef RFCOMM_INITIAL_TRACE_LEVEL
#define RFCOMM_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef SDP_INITIAL_TRACE_LEVEL
#define SDP_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef GAP_INITIAL_TRACE_LEVEL
#define GAP_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef BNEP_INITIAL_TRACE_LEVEL
#define BNEP_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef PAN_INITIAL_TRACE_LEVEL
#define PAN_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef A2D_INITIAL_TRACE_LEVEL
#define A2D_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef AVDT_INITIAL_TRACE_LEVEL
#define AVDT_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef AVCT_INITIAL_TRACE_LEVEL
#define AVCT_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef AVRC_INITIAL_TRACE_LEVEL
#define AVRC_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef MCA_INITIAL_TRACE_LEVEL
#define MCA_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef HID_INITIAL_TRACE_LEVEL
#define HID_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef APPL_INITIAL_TRACE_LEVEL
#define APPL_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef BT_TRACE_APPL
#define BT_TRACE_APPL BT_USE_TRACES
#endif
#ifndef GATT_INITIAL_TRACE_LEVEL
#define GATT_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
#ifndef SMP_INITIAL_TRACE_LEVEL
#define SMP_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
#endif
// btla-specific --
/* Define common tracing for all */
#define LOG_LEVEL_ERROR 1
#define LOG_LEVEL_WARN 2
#define LOG_LEVEL_INFO 3
#define LOG_LEVEL_DEBUG 4
#define LOG_LEVEL_VERBOSE 5
#ifndef LOG_LEVEL
#define LOG_LEVEL LOG_LEVEL_DEBUG
#endif
#define LOG_ERROR(fmt, args...) do {if (LOG_LEVEL >= LOG_LEVEL_ERROR) ets_printf(fmt,## args);} while(0)
#define LOG_WARN(fmt, args...) do {if (LOG_LEVEL >= LOG_LEVEL_WARN) ets_printf(fmt,## args);} while(0)
#define LOG_INFO(fmt, args...) do {if (LOG_LEVEL >= LOG_LEVEL_INFO) ets_printf(fmt,## args);} while(0)
#define LOG_DEBUG(fmt, args...) do {if (LOG_LEVEL >= LOG_LEVEL_DEBUG) ets_printf(fmt,## args);} while(0)
#define LOG_VERBOSE(fmt, args...) do {if (LOG_LEVEL >= LOG_LEVEL_VERBOSE) ets_printf(fmt,## args);} while(0)
/* Define tracing for the HCI unit
*/
#define HCI_TRACE_ERROR(fmt, args...) {if (btu_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt,## args);}
#define HCI_TRACE_WARNING(fmt, args...) {if (btu_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt,## args);}
#define HCI_TRACE_EVENT(fmt, args...) {if (btu_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt,## args);}
#define HCI_TRACE_DEBUG(fmt, args...) {if (btu_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt,## args);}
/* Define tracing for BTM
*/
#define BTM_TRACE_ERROR(fmt, args...) {if (btm_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define BTM_TRACE_WARNING(fmt, args...) {if (btm_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define BTM_TRACE_API(fmt, args...) {if (btm_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define BTM_TRACE_EVENT(fmt, args...) {if (btm_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define BTM_TRACE_DEBUG(fmt, args...) {if (btm_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
/* Define tracing for the L2CAP unit
*/
#define L2CAP_TRACE_ERROR(fmt, args...) {if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define L2CAP_TRACE_WARNING(fmt, args...) {if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define L2CAP_TRACE_API(fmt, args...) {if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define L2CAP_TRACE_EVENT(fmt, args...) {if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define L2CAP_TRACE_DEBUG(fmt, args...) {if (l2cb.l2cap_trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
/* Define tracing for the SDP unit
*/
#define SDP_TRACE_ERROR(fmt, args...) {if (sdp_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define SDP_TRACE_WARNING(fmt, args...) {if (sdp_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define SDP_TRACE_API(fmt, args...) {if (sdp_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define SDP_TRACE_EVENT(fmt, args...) {if (sdp_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define SDP_TRACE_DEBUG(fmt, args...) {if (sdp_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
/* Define tracing for the RFCOMM unit
*/
#define RFCOMM_TRACE_ERROR(fmt, args...) {if (rfc_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define RFCOMM_TRACE_WARNING(fmt, args...) {if (rfc_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define RFCOMM_TRACE_API(fmt, args...) {if (rfc_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define RFCOMM_TRACE_EVENT(fmt, args...) {if (rfc_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define RFCOMM_TRACE_DEBUG(fmt, args...) {if (rfc_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
/* Generic Access Profile traces */
#define GAP_TRACE_ERROR(fmt, args...) {if (gap_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define GAP_TRACE_EVENT(fmt, args...) {if (gap_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define GAP_TRACE_API(fmt, args...) {if (gap_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define GAP_TRACE_WARNING(fmt, args...) {if (gap_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
/* define traces for HID Host */
#define HIDH_TRACE_ERROR(fmt, args...) {if (hh_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define HIDH_TRACE_WARNING(fmt, args...) {if (hh_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define HIDH_TRACE_API(fmt, args...) {if (hh_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define HIDH_TRACE_EVENT(fmt, args...) {if (hh_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define HIDH_TRACE_DEBUG(fmt, args...) {if (hh_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
/* define traces for BNEP */
#define BNEP_TRACE_ERROR(fmt, args...) {if (bnep_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define BNEP_TRACE_WARNING(fmt, args...) {if (bnep_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define BNEP_TRACE_API(fmt, args...) {if (bnep_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define BNEP_TRACE_EVENT(fmt, args...) {if (bnep_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define BNEP_TRACE_DEBUG(fmt, args...) {if (bnep_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
/* define traces for PAN */
#define PAN_TRACE_ERROR(fmt, args...) {if (pan_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define PAN_TRACE_WARNING(fmt, args...) {if (pan_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define PAN_TRACE_API(fmt, args...) {if (pan_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define PAN_TRACE_EVENT(fmt, args...) {if (pan_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define PAN_TRACE_DEBUG(fmt, args...) {if (pan_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
/* Define tracing for the A2DP profile
*/
#define A2D_TRACE_ERROR(fmt, args...) {if (a2d_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define A2D_TRACE_WARNING(fmt, args...) {if (a2d_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define A2D_TRACE_EVENT(fmt, args...) {if (a2d_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define A2D_TRACE_DEBUG(fmt, args...) {if (a2d_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
#define A2D_TRACE_API(fmt, args...) {if (a2d_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
/* AVDTP
*/
#define AVDT_TRACE_ERROR(fmt, args...) {if (avdt_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define AVDT_TRACE_WARNING(fmt, args...) {if (avdt_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define AVDT_TRACE_EVENT(fmt, args...) {if (avdt_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define AVDT_TRACE_DEBUG(fmt, args...) {if (avdt_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
#define AVDT_TRACE_API(fmt, args...) {if (avdt_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
/* Define tracing for the AVCTP protocol
*/
#define AVCT_TRACE_ERROR(fmt, args...) {if (avct_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define AVCT_TRACE_WARNING(fmt, args...) {if (avct_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define AVCT_TRACE_EVENT(fmt, args...) {if (avct_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define AVCT_TRACE_DEBUG(fmt, args...) {if (avct_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
#define AVCT_TRACE_API(fmt, args...) {if (avct_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
/* Define tracing for the AVRCP profile
*/
#define AVRC_TRACE_ERROR(fmt, args...) {if (avrc_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define AVRC_TRACE_WARNING(fmt, args...) {if (avrc_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define AVRC_TRACE_EVENT(fmt, args...) {if (avrc_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define AVRC_TRACE_DEBUG(fmt, args...) {if (avrc_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
#define AVRC_TRACE_API(fmt, args...) {if (avrc_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
/* MCAP
*/
#define MCA_TRACE_ERROR(fmt, args...) {if (mca_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define MCA_TRACE_WARNING(fmt, args...) {if (mca_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define MCA_TRACE_EVENT(fmt, args...) {if (mca_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define MCA_TRACE_DEBUG(fmt, args...) {if (mca_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
#define MCA_TRACE_API(fmt, args...) {if (mca_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
/* Define tracing for the ATT/GATT unit
*/
#define GATT_TRACE_ERROR(fmt, args...) {if (gatt_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define GATT_TRACE_WARNING(fmt, args...) {if (gatt_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define GATT_TRACE_API(fmt, args...) {if (gatt_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define GATT_TRACE_EVENT(fmt, args...) {if (gatt_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define GATT_TRACE_DEBUG(fmt, args...) {if (gatt_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
/* Define tracing for the SMP unit
*/
#define SMP_TRACE_ERROR(fmt, args...) {if (smp_cb.trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define SMP_TRACE_WARNING(fmt, args...) {if (smp_cb.trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define SMP_TRACE_API(fmt, args...) {if (smp_cb.trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define SMP_TRACE_EVENT(fmt, args...) {if (smp_cb.trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define SMP_TRACE_DEBUG(fmt, args...) {if (smp_cb.trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
extern UINT8 btif_trace_level;
// define traces for application
#define BTIF_TRACE_ERROR(fmt, args...) {if (btif_trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define BTIF_TRACE_WARNING(fmt, args...) {if (btif_trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define BTIF_TRACE_API(fmt, args...) {if (btif_trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define BTIF_TRACE_EVENT(fmt, args...) {if (btif_trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define BTIF_TRACE_DEBUG(fmt, args...) {if (btif_trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
#define BTIF_TRACE_VERBOSE(fmt, args...) {if (btif_trace_level >= BT_TRACE_LEVEL_VERBOSE)ets_printf(fmt, ## args);}
/* define traces for application */
#define APPL_TRACE_ERROR(fmt, args...) {if (appl_trace_level >= BT_TRACE_LEVEL_ERROR) ets_printf(fmt, ## args);}
#define APPL_TRACE_WARNING(fmt, args...) {if (appl_trace_level >= BT_TRACE_LEVEL_WARNING) ets_printf(fmt, ## args);}
#define APPL_TRACE_API(fmt, args...) {if (appl_trace_level >= BT_TRACE_LEVEL_API) ets_printf(fmt, ## args);}
#define APPL_TRACE_EVENT(fmt, args...) {if (appl_trace_level >= BT_TRACE_LEVEL_EVENT) ets_printf(fmt, ## args);}
#define APPL_TRACE_DEBUG(fmt, args...) {if (appl_trace_level >= BT_TRACE_LEVEL_DEBUG) ets_printf(fmt, ## args);}
#define APPL_TRACE_VERBOSE(fmt, args...) {if (appl_trace_level >= BT_TRACE_LEVEL_VERBOSE) ets_printf(fmt, ## args);}
/* Simplified Trace Helper Macro
*/
#define bdld(fmt, args...) \
do{\
if((MY_LOG_LEVEL) >= BT_TRACE_LEVEL_DEBUG) \
ets_printf(fmt, ## args); \
}while(0)
#define bdlw(fmt, args...) \
do{\
if((MY_LOG_LEVEL) >= BT_TRACE_LEVEL_DEBUG) \
ets_printf(fmt, ## args); \
}while(0)
#define bdle(fmt, args...) \
do{\
if((MY_LOG_LEVEL) >= BT_TRACE_LEVEL_DEBUG) \
ets_printf(fmt, ## args); \
}while(0)
#define bdla(assert_if) \
do{\
if(((MY_LOG_LEVEL) >= BT_TRACE_LEVEL_ERROR) && !(assert_if)) \
ets_printf("%s: assert failed\n", #assert_if); \
}while(0)
typedef UINT8 tBTTRC_PARAM_TYPE;
typedef UINT8 tBTTRC_LAYER_ID;
typedef UINT8 tBTTRC_TYPE;
typedef struct {
tBTTRC_LAYER_ID layer_id;
tBTTRC_TYPE type; /* TODO: use tBTTRC_TYPE instead of "classical level 0-5" */
} tBTTRC_LEVEL;
typedef UINT8 (tBTTRC_SET_TRACE_LEVEL)( UINT8 );
typedef struct {
const tBTTRC_LAYER_ID layer_id_start;
const tBTTRC_LAYER_ID layer_id_end;
tBTTRC_SET_TRACE_LEVEL *p_f;
const char *trc_name;
UINT8 trace_level;
} tBTTRC_FUNC_MAP;
/* External declaration for appl_trace_level here to avoid to add the declaration in all the files using APPL_TRACExxx macros */
extern UINT8 appl_trace_level;
#endif /*_BT_TRACE_H_*/

View file

@ -0,0 +1,362 @@
/******************************************************************************
*
* Copyright (C) 2009-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef BT_VENDOR_LIB_H
#define BT_VENDOR_LIB_H
#include <stdint.h>
//#include <sys/cdefs.h>
//#include <sys/types.h>
/** Struct types */
/** Typedefs and defines */
/** Vendor specific operations OPCODE */
typedef enum {
/* [operation]
* Power on or off the BT Controller.
* [input param]
* A pointer to int type with content of bt_vendor_power_state_t.
* Typecasting conversion: (int *) param.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_POWER_CTRL,
/* [operation]
* Perform any vendor specific initialization or configuration
* on the BT Controller. This is called before stack initialization.
* [input param]
* None.
* [return]
* 0 - default, don't care.
* [callback]
* Must call fwcfg_cb to notify the stack of the completion of vendor
* specific initialization once it has been done.
*/
BT_VND_OP_FW_CFG,
/* [operation]
* Perform any vendor specific SCO/PCM configuration on the BT Controller.
* This is called after stack initialization.
* [input param]
* None.
* [return]
* 0 - default, don't care.
* [callback]
* Must call scocfg_cb to notify the stack of the completion of vendor
* specific SCO configuration once it has been done.
*/
BT_VND_OP_SCO_CFG,
/* [operation]
* Open UART port on where the BT Controller is attached.
* This is called before stack initialization.
* [input param]
* A pointer to int array type for open file descriptors.
* The mapping of HCI channel to fd slot in the int array is given in
* bt_vendor_hci_channels_t.
* And, it requires the vendor lib to fill up the content before returning
* the call.
* Typecasting conversion: (int (*)[]) param.
* [return]
* Numbers of opened file descriptors.
* Valid number:
* 1 - CMD/EVT/ACL-In/ACL-Out via the same fd (e.g. UART)
* 2 - CMD/EVT on one fd, and ACL-In/ACL-Out on the other fd
* 4 - CMD, EVT, ACL-In, ACL-Out are on their individual fd
* [callback]
* None.
*/
BT_VND_OP_USERIAL_OPEN,
/* [operation]
* Close the previously opened UART port.
* [input param]
* None.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_USERIAL_CLOSE,
/* [operation]
* Get the LPM idle timeout in milliseconds.
* The stack uses this information to launch a timer delay before it
* attempts to de-assert LPM WAKE signal once downstream HCI packet
* has been delivered.
* [input param]
* A pointer to uint32_t type which is passed in by the stack. And, it
* requires the vendor lib to fill up the content before returning
* the call.
* Typecasting conversion: (uint32_t *) param.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_GET_LPM_IDLE_TIMEOUT,
/* [operation]
* Enable or disable LPM mode on BT Controller.
* [input param]
* A pointer to uint8_t type with content of bt_vendor_lpm_mode_t.
* Typecasting conversion: (uint8_t *) param.
* [return]
* 0 - default, don't care.
* [callback]
* Must call lpm_cb to notify the stack of the completion of LPM
* disable/enable process once it has been done.
*/
BT_VND_OP_LPM_SET_MODE,
/* [operation]
* Assert or Deassert LPM WAKE on BT Controller.
* [input param]
* A pointer to uint8_t type with content of bt_vendor_lpm_wake_state_t.
* Typecasting conversion: (uint8_t *) param.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_LPM_WAKE_SET_STATE,
/* [operation]
* Perform any vendor specific commands related to audio state changes.
* [input param]
* a pointer to bt_vendor_op_audio_state_t indicating what audio state is
* set.
* [return]
* 0 - default, don't care.
* [callback]
* None.
*/
BT_VND_OP_SET_AUDIO_STATE,
/* [operation]
* The epilog call to the vendor module so that it can perform any
* vendor-specific processes (e.g. send a HCI_RESET to BT Controller)
* before the caller calls for cleanup().
* [input param]
* None.
* [return]
* 0 - default, don't care.
* [callback]
* Must call epilog_cb to notify the stack of the completion of vendor
* specific epilog process once it has been done.
*/
BT_VND_OP_EPILOG,
} bt_vendor_opcode_t;
/** Power on/off control states */
typedef enum {
BT_VND_PWR_OFF,
BT_VND_PWR_ON,
} bt_vendor_power_state_t;
/** Define HCI channel identifier in the file descriptors array
used in BT_VND_OP_USERIAL_OPEN operation.
*/
typedef enum {
CH_CMD, // HCI Command channel
CH_EVT, // HCI Event channel
CH_ACL_OUT, // HCI ACL downstream channel
CH_ACL_IN, // HCI ACL upstream channel
CH_MAX // Total channels
} bt_vendor_hci_channels_t;
/** LPM disable/enable request */
typedef enum {
BT_VND_LPM_DISABLE,
BT_VND_LPM_ENABLE,
} bt_vendor_lpm_mode_t;
/** LPM WAKE set state request */
typedef enum {
BT_VND_LPM_WAKE_ASSERT,
BT_VND_LPM_WAKE_DEASSERT,
} bt_vendor_lpm_wake_state_t;
/** Callback result values */
typedef enum {
BT_VND_OP_RESULT_SUCCESS,
BT_VND_OP_RESULT_FAIL,
} bt_vendor_op_result_t;
/** audio (SCO) state changes triggering VS commands for configuration */
typedef struct {
uint16_t handle;
uint16_t peer_codec;
uint16_t state;
} bt_vendor_op_audio_state_t;
/*
* Bluetooth Host/Controller Vendor callback structure.
*/
/* vendor initialization/configuration callback */
typedef void (*cfg_result_cb)(bt_vendor_op_result_t result);
/* datapath buffer allocation callback (callout)
*
* Vendor lib needs to request a buffer through the alloc callout function
* from HCI lib if the buffer is for constructing a HCI Command packet which
* will be sent through xmit_cb to BT Controller.
*
* For each buffer allocation, the requested size needs to be big enough to
* accommodate the below header plus a complete HCI packet --
* typedef struct
* {
* uint16_t event;
* uint16_t len;
* uint16_t offset;
* uint16_t layer_specific;
* } HC_BT_HDR;
*
* HCI lib returns a pointer to the buffer where Vendor lib should use to
* construct a HCI command packet as below format:
*
* --------------------------------------------
* | HC_BT_HDR | HCI command |
* --------------------------------------------
* where
* HC_BT_HDR.event = 0x2000;
* HC_BT_HDR.len = Length of HCI command;
* HC_BT_HDR.offset = 0;
* HC_BT_HDR.layer_specific = 0;
*
* For example, a HCI_RESET Command will be formed as
* ------------------------
* | HC_BT_HDR |03|0c|00|
* ------------------------
* with
* HC_BT_HDR.event = 0x2000;
* HC_BT_HDR.len = 3;
* HC_BT_HDR.offset = 0;
* HC_BT_HDR.layer_specific = 0;
*/
typedef void* (*malloc_cb)(int size);
/* datapath buffer deallocation callback (callout) */
typedef void (*mdealloc_cb)(void *p_buf);
/* define callback of the cmd_xmit_cb
*
* The callback function which HCI lib will call with the return of command
* complete packet. Vendor lib is responsible for releasing the buffer passed
* in at the p_mem parameter by calling dealloc callout function.
*/
typedef void (*tINT_CMD_CBACK)(void *p_mem);
/* hci command packet transmit callback (callout)
*
* Vendor lib calls xmit_cb callout function in order to send a HCI Command
* packet to BT Controller. The buffer carrying HCI Command packet content
* needs to be first allocated through the alloc callout function.
* HCI lib will release the buffer for Vendor lib once it has delivered the
* packet content to BT Controller.
*
* Vendor lib needs also provide a callback function (p_cback) which HCI lib
* will call with the return of command complete packet.
*
* The opcode parameter gives the HCI OpCode (combination of OGF and OCF) of
* HCI Command packet. For example, opcode = 0x0c03 for the HCI_RESET command
* packet.
*/
typedef uint8_t (*cmd_xmit_cb)(uint16_t opcode, void *p_buf, tINT_CMD_CBACK p_cback);
typedef struct {
/** set to sizeof(bt_vendor_callbacks_t) */
size_t size;
/*
* Callback and callout functions have implemented in HCI libray
* (libbt-hci.so).
*/
/* notifies caller result of firmware configuration request */
cfg_result_cb fwcfg_cb;
/* notifies caller result of sco configuration request */
cfg_result_cb scocfg_cb;
/* notifies caller result of lpm enable/disable */
cfg_result_cb lpm_cb;
/* notifies the result of codec setting */
cfg_result_cb audio_state_cb;
/* buffer allocation request */
malloc_cb alloc;
/* buffer deallocation request */
mdealloc_cb dealloc;
/* hci command packet transmit request */
cmd_xmit_cb xmit_cb;
/* notifies caller completion of epilog process */
cfg_result_cb epilog_cb;
} bt_vendor_callbacks_t;
/*
* Bluetooth Host/Controller VENDOR Interface
*/
typedef struct {
/** Set to sizeof(bt_vndor_interface_t) */
size_t size;
/*
* Functions need to be implemented in Vendor libray (libbt-vendor.so).
*/
/**
* Caller will open the interface and pass in the callback routines
* to the implemenation of this interface.
*/
int (*init)(const bt_vendor_callbacks_t* p_cb, unsigned char *local_bdaddr);
/** Vendor specific operations */
int (*op)(bt_vendor_opcode_t opcode, void *param);
/** Closes the interface */
void (*cleanup)(void);
} bt_vendor_interface_t;
/*
* External shared lib functions/data
*/
/* Entry point of DLib --
* Vendor library needs to implement the body of bt_vendor_interface_t
* structure and uses the below name as the variable name. HCI library
* will use this symbol name to get address of the object through the
* dlsym call.
*/
//extern const bt_vendor_interface_t BLUETOOTH_VENDOR_LIB_INTERFACE;
#endif /* BT_VENDOR_LIB_H */

View file

@ -0,0 +1,118 @@
/******************************************************************************
*
* Copyright (C) 2001-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* this file contains constants and definitions for the bte project
*
******************************************************************************/
#ifndef BTE_H
#define BTE_H
//#include <semaphore.h>
//#include <signal.h>
//#include <pthread.h>
#include "bt_target.h"
/* by default on shutdown, baudrate is reset 115kbits. this should NOT be need for platforms
* that kill BTE driver and remove/reset BT chip
*/
#ifndef BTE_RESET_BAUD_ON_BT_DISABLE
#define BTE_RESET_BAUD_ON_BT_DISABLE TRUE
#endif
/* Target Modes (based on jumper settings on hardware [see user manual]) */
enum
{
/* BTE BBY */
/* J3 J4 SW3-3 SW3-2 SW3-1 */
/* -------------------------------------------- */
BTE_MODE_SERIAL_APP, /* OUT OUT OFF OFF OFF Sample serial port application */
BTE_MODE_APPL, /* IN OUT OFF OFF ON Target used with Tester through RPC */
BTE_MODE_RESERVED, /* OUT IN OFF ON OFF Reserved */
BTE_MODE_SAMPLE_APPS, /* IN IN OFF ON ON Sample applications (ICP/HSP) */
BTE_MODE_DONGLE, /* not yet supported ON OFF OFF Dongle mode */
BTE_MODE_APPL_PROTOCOL_TRACE, /* this is a fake mode do allow protocol tracing in application without rpc */
BTE_MODE_INVALID
};
extern volatile UINT8 bte_target_mode; /* indicates the mode that the board is running in */
/* Startup options */
extern UINT32 bte_startup_options; /* Switch and jumper settings at startup */
void bte_get_startup_options(UINT32 *p_options); /* Platform specific function for getting startup options */
#define BTE_OPTIONS_TARGET_MODE_MASK 0x00000007 /* bits 2-0 indicate target mode (QuickConnect: jp3 & jp4, BBY: SW3-1 & SW3-2)*/
/****************************************************************************
* Definitions to define which type of application gets built
****************************************************************************/
#define BUILD_HCITOOL FALSE
#define BUILD_L2PING FALSE
#define LINUX_FM_DRIVER_INCLUDED FALSE
/* hcisu userial operations. should probably go into bt_types to avoid collisions! */
#define BT_EVT_TO_HCISU_USERIAL_OP (0x0080 | BT_EVT_HCISU)
/* operation for above hcisu event */
#define BT_HCISU_USERIAL_OPEN (0) /* open serial port calling USERIAL_Open() */
#define BT_HCISU_USERIAL_CLOSE (1) /* close userial port */
/* options associated with close op */
#define BT_HCISU_USERIAL_CL_NO_DIS_BT 0 /* do not touch bt_wake and power gpio */
#define BT_HCISU_USERIAL_CL_DIS_BT 1 /* put power and bt_wake into defined off state to preserve
power */
/* status codes for callback */
/*
#define BTE_HCISU_USERIAL_FAIL 0
#define BTE_HCISU_USERIAL_OK 1
typedef void (tUSERIAL_MSG_CBACK) (int status);
typedef struct tHCISU_USERIAL_MSG_tag {
BT_HDR hdr;
tUSERIAL_MSG_CBACK *p_cback;
UINT8 port; // port number
UINT8 op;
UINT8 option; // option for operation. depends on operation
} tHCISU_USERIAL_MSG;
extern void bte_hcisu_userial_oper( tUSERIAL_MSG_CBACK *p_cback, UINT8 port, UINT8 op, UINT8 option );
*/
/* Pointer to function for sending HCI commands and data to the HCI tranport */
extern int (*p_bte_hci_send)(UINT16 port, BT_HDR *p_msg);
/* Protocol trace mask */
extern UINT32 bte_proto_trace_mask;
typedef struct tBAUD_REG_tag {
UINT8 DHBR;
UINT8 DLBR;
UINT8 ExplicitBaudRate0;
UINT8 ExplicitBaudRate1;
UINT8 ExplicitBaudRate2;
UINT8 ExplicitBaudRate3;
} tBAUD_REG;
#include "gki.h"
extern const tBAUD_REG baud_rate_regs[];
#endif /* BTE_H */

View file

@ -0,0 +1,37 @@
/******************************************************************************
*
* Copyright (C) 2002-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This is the interface file for the bte application task
*
******************************************************************************/
#pragma once
typedef struct {
#if ((BLE_INCLUDED == TRUE) && (SMP_INCLUDED == TRUE))
UINT8 ble_auth_req;
UINT8 ble_io_cap;
UINT8 ble_init_key;
UINT8 ble_resp_key;
UINT8 ble_max_key_size;
#endif
} tBTE_APPL_CFG;
extern tBTE_APPL_CFG bte_appl_cfg;

View file

@ -0,0 +1,244 @@
/******************************************************************************
*
* Copyright (C) 1999-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#ifndef _GKI_TARGET_H_
#define _GKI_TARGET_H_
/******************************************************************************
**
** Buffer configuration
**
******************************************************************************/
/* The size of the buffers in pool 0. */
#ifndef GKI_BUF0_SIZE
#define GKI_BUF0_SIZE 64
#endif
/* The number of buffers in buffer pool 0. */
#ifndef GKI_BUF0_MAX
#define GKI_BUF0_MAX 96
#endif
/* The ID of buffer pool 0. */
#ifndef GKI_POOL_ID_0
#define GKI_POOL_ID_0 0
#endif
/* The size of the buffers in pool 1. */
#ifndef GKI_BUF1_SIZE
#define GKI_BUF1_SIZE 288
#endif
/* The number of buffers in buffer pool 1. */
#ifndef GKI_BUF1_MAX
#define GKI_BUF1_MAX 52
#endif
/* The ID of buffer pool 1. */
#ifndef GKI_POOL_ID_1
#define GKI_POOL_ID_1 1
#endif
/* The size of the buffers in pool 2. */
#ifndef GKI_BUF2_SIZE
#define GKI_BUF2_SIZE 660
#endif
/* The number of buffers in buffer pool 2. */
#ifndef GKI_BUF2_MAX
#define GKI_BUF2_MAX 90
#endif
/* The ID of buffer pool 2. */
#ifndef GKI_POOL_ID_2
#define GKI_POOL_ID_2 2
#endif
/* The size of the buffers in pool 3. */
#ifndef GKI_BUF3_SIZE
#define GKI_BUF3_SIZE (4096+16)
#endif
/* The number of buffers in buffer pool 3. */
#ifndef GKI_BUF3_MAX
#define GKI_BUF3_MAX 400
#endif
/* The ID of buffer pool 3. */
#ifndef GKI_POOL_ID_3
#define GKI_POOL_ID_3 3
#endif
/* The size of the largest PUBLIC fixed buffer in system. */
#ifndef GKI_MAX_BUF_SIZE
#define GKI_MAX_BUF_SIZE GKI_BUF3_SIZE
#endif
/* The pool ID of the largest PUBLIC fixed buffer in system. */
#ifndef GKI_MAX_BUF_SIZE_POOL_ID
#define GKI_MAX_BUF_SIZE_POOL_ID GKI_POOL_ID_3
#endif
/* Pool 4 is used for BluetoothSocket L2CAP connections */
/* The size of the buffers in pool 4. */
#ifndef GKI_BUF4_SIZE
#define GKI_BUF4_SIZE (8080+26)
#endif
/* The number of buffers in buffer pool 4. */
#ifndef GKI_BUF4_MAX
#define GKI_BUF4_MAX (OBX_NUM_SERVERS + OBX_NUM_CLIENTS)
#endif
/* The ID of buffer pool 4. */
#ifndef GKI_POOL_ID_4
#define GKI_POOL_ID_4 4
#endif
/* The number of fixed GKI buffer pools.
eL2CAP requires Pool ID 5
If BTM_SCO_HCI_INCLUDED is FALSE, Pool ID 6 is unnecessary, otherwise set to 7
If BTA_HL_INCLUDED is FALSE then Pool ID 7 is uncessary and set the following to 7, otherwise set to 8
If BLE_INCLUDED is FALSE then Pool ID 8 is uncessary and set the following to 8, otherwise set to 9
POOL_ID 9 is a public pool meant for large buffer needs such as SDP_DB
*/
#ifndef GKI_NUM_FIXED_BUF_POOLS
#define GKI_NUM_FIXED_BUF_POOLS 10
#endif
/* The buffer pool usage mask. */
#ifndef GKI_DEF_BUFPOOL_PERM_MASK
/* Setting POOL_ID 9 as a public pool meant for large buffers such as SDP_DB */
#define GKI_DEF_BUFPOOL_PERM_MASK 0xfdf0
#endif
/* The following is intended to be a reserved pool for L2CAP
Flow control and retransmissions and intentionally kept out
of order */
/* The number of buffers in buffer pool 5. */
#ifndef GKI_BUF5_MAX
#define GKI_BUF5_MAX 64
#endif
/* The ID of buffer pool 5. */
#ifndef GKI_POOL_ID_5
#define GKI_POOL_ID_5 5
#endif
/* The size of the buffers in pool 5
** Special pool used by L2CAP retransmissions only. This size based on segment
** that will fit into both DH5 and 2-DH3 packet types after accounting for GKI
** header. 13 bytes of max headers allows us a 339 payload max. (in btui_app.txt)
** Note: 748 used for insight scriptwrapper with CAT-2 scripts.
*/
#ifndef GKI_BUF5_SIZE
#define GKI_BUF5_SIZE 748
#endif
/* The following is intended to be a reserved pool for SCO
over HCI data and intentionally kept out of order */
/* The ID of buffer pool 6. */
#ifndef GKI_POOL_ID_6
#define GKI_POOL_ID_6 6
#endif
/* The size of the buffers in pool 6,
BUF_SIZE = max SCO data 255 + sizeof(BT_HDR) = 8 + SCO packet header 3 + padding 2 = 268 */
#ifndef GKI_BUF6_SIZE
#define GKI_BUF6_SIZE 268
#endif
/* The number of buffers in buffer pool 6. */
#ifndef GKI_BUF6_MAX
#define GKI_BUF6_MAX 60
#endif
/* The following pool is a dedicated pool for HDP
If a shared pool is more desirable then
1. set BTA_HL_LRG_DATA_POOL_ID to the desired Gki Pool ID
2. make sure that the shared pool size is larger than 9472
3. adjust GKI_NUM_FIXED_BUF_POOLS accordingly since
POOL ID 7 is not needed
*/
/* The ID of buffer pool 7. */
#ifndef GKI_POOL_ID_7
#define GKI_POOL_ID_7 7
#endif
/* The size of the buffers in pool 7 */
#ifndef GKI_BUF7_SIZE
#define GKI_BUF7_SIZE (10240 + 24)
#endif
/* The number of buffers in buffer pool 7. */
#ifndef GKI_BUF7_MAX
#define GKI_BUF7_MAX 2
#endif
/* The following pool is a dedicated pool for GATT
If a shared pool is more desirable then
1. set GATT_DB_POOL_ID to the desired Gki Pool ID
2. make sure that the shared pool size fit a common GATT database needs
3. adjust GKI_NUM_FIXED_BUF_POOLS accordingly since
POOL ID 8 is not needed
*/
/* The ID of buffer pool 8. */
#ifndef GKI_POOL_ID_8
#define GKI_POOL_ID_8 8
#endif
/* The size of the buffers in pool 8 */
#ifndef GKI_BUF8_SIZE
#define GKI_BUF8_SIZE 128
#endif
/* The number of buffers in buffer pool 8. */
#ifndef GKI_BUF8_MAX
#define GKI_BUF8_MAX 30
#endif
/* The following pool is meant for large allocations such as SDP_DB */
#ifndef GKI_POOL_ID_9
#define GKI_POOL_ID_9 9
#endif
#ifndef GKI_BUF9_SIZE
#define GKI_BUF9_SIZE 8192
#endif
#ifndef GKI_BUF9_MAX
#define GKI_BUF9_MAX 5
#endif
/* The number of fixed and dynamic buffer pools */
#ifndef GKI_NUM_TOTAL_BUF_POOLS
#define GKI_NUM_TOTAL_BUF_POOLS 10
#endif
int gki_init(void);
void gki_clean_up(void);
//void LogMsg (UINT32 trace_set_mask, const char *fmt_str, ...);
#endif /*_GKI_TARGET_H_*/

View file

@ -0,0 +1,216 @@
/******************************************************************************
*
* Copyright (C) 2000-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This module contains the routines that initialize the stack components.
* It must be called before the BTU task is started.
*
******************************************************************************/
#include "bt_target.h"
#include <string.h>
/* Stack Configuation Related Init Definaton
* TODO: Now Just Unmask these defination until stack layer is OK
*/
#ifndef BTA_INCLUDED
#define BTA_INCLUDED FALSE
#endif
#if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
// Include initialization functions definitions
#include "port_api.h"
#endif
#if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
#include "bnep_api.h"
#endif
#if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
#include "gap_api.h"
#endif
#if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
#include "pan_api.h"
#endif
#if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
#include "avrc_api.h"
#endif
#if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
#include "a2d_api.h"
#endif
#if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
#include "hidh_api.h"
#endif
#if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
#include "mca_api.h"
#endif
#if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
#include "gatt_api.h"
#if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
#include "smp_api.h"
#endif
#endif
//BTA Modules
#if BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE
#include "bta_api.h"
#include "bta_sys.h"
#include "bta_ag_int.h"
#if BTA_HS_INCLUDED == TRUE
#include "bta_hs_int.h"
#endif
#include "bta_dm_int.h"
#if BTA_AR_INCLUDED==TRUE
#include "bta_ar_int.h"
#endif
#if BTA_AV_INCLUDED==TRUE
#include "bta_av_int.h"
#endif
#if BTA_HH_INCLUDED==TRUE
#include "bta_hh_int.h"
#endif
#if BTA_JV_INCLUDED==TRUE
#include "bta_jv_int.h"
tBTA_JV_CB *bta_jv_cb_ptr = NULL;
#endif
#if BTA_HL_INCLUDED == TRUE
#include "bta_hl_int.h"
#endif
#if BTA_GATT_INCLUDED == TRUE
#include "bta_gattc_int.h"
#include "bta_gatts_int.h"
#endif
#if BTA_PAN_INCLUDED==TRUE
#include "bta_pan_int.h"
#endif
#include "bta_sys_int.h"
// control block for patch ram downloading
#include "bta_prm_int.h"
#endif // BTA_INCLUDED
/*****************************************************************************
** F U N C T I O N S *
******************************************************************************/
/*****************************************************************************
**
** Function BTE_InitStack
**
** Description Initialize control block memory for each component.
**
** Note: The core stack components must be called
** before creating the BTU Task. The rest of the
** components can be initialized at a later time if desired
** as long as the component's init function is called
** before accessing any of its functions.
**
** Returns void
**
******************************************************************************/
void BTE_InitStack(void)
{
#if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
//Initialize the optional stack components
RFCOMM_Init();
#endif
//BNEP and its profiles
#if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
BNEP_Init();
#if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
PAN_Init();
#endif // PAN
#endif // BNEP Included
//AVDT and its profiles
#if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
A2D_Init();
#endif // AADP
#if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
AVRC_Init();
#endif
#if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
GAP_Init();
#endif
#if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
HID_HostInit();
#endif
#if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
MCA_Init();
#endif
//BTA Modules
#if (BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE)
memset((void*)bta_sys_cb_ptr, 0, sizeof(tBTA_SYS_CB));
memset((void*)bta_dm_cb_ptr, 0, sizeof(tBTA_DM_CB));
memset((void*)bta_dm_search_cb_ptr, 0, sizeof(tBTA_DM_SEARCH_CB));
memset((void*)bta_dm_di_cb_ptr, 0, sizeof(tBTA_DM_DI_CB));
memset((void*)bta_prm_cb_ptr, 0, sizeof(tBTA_PRM_CB));
memset((void*)bta_ag_cb_ptr, 0, sizeof(tBTA_AG_CB));
#if BTA_HS_INCLUDED == TRUE
memset((void*)bta_hs_cb_ptr, 0, sizeof(tBTA_HS_CB));
#endif
#if BTA_AR_INCLUDED==TRUE
memset((void *)bta_ar_cb_ptr, 0, sizeof(tBTA_AR_CB));
#endif
#if BTA_AV_INCLUDED==TRUE
memset((void *)bta_av_cb_ptr, 0, sizeof(tBTA_AV_CB));
#endif
#if BTA_HH_INCLUDED==TRUE
memset((void *)bta_hh_cb_ptr, 0, sizeof(tBTA_HH_CB));
#endif
#if BTA_HL_INCLUDED==TRUE
memset((void *)bta_hl_cb_ptr, 0, sizeof(tBTA_HL_CB));
#endif
#if BTA_GATT_INCLUDED==TRUE
memset((void *)bta_gattc_cb_ptr, 0, sizeof(tBTA_GATTC_CB));
memset((void *)bta_gatts_cb_ptr, 0, sizeof(tBTA_GATTS_CB));
#endif
#if BTA_PAN_INCLUDED==TRUE
memset((void *)bta_pan_cb_ptr, 0, sizeof(tBTA_PAN_CB));
#endif
#endif // BTA_INCLUDED == TRUE
}

View file

@ -0,0 +1,324 @@
/******************************************************************************
*
* Copyright (C) 2009-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* Filename: bte_main.c
*
* Description: Contains BTE core stack initialization and shutdown code
*
******************************************************************************/
#include "bt_defs.h"
#include "bte.h"
#include "btu.h"
#include "bt_trace.h"
#include "osi.h"
#include "alarm.h"
#include "fixed_queue.h"
#include "hash_map.h"
#include "hash_functions.h"
#include "controller.h"
#include "hci_layer.h"
#include "bt_app_common.h"
//#include "bluedroid_test.h"
/*
#define LOG_TAG "bt_main"
#include <cutils/properties.h>
#include <fcntl.h>
#include <hardware/bluetooth.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include "osi/include/alarm.h"
#include "bta_api.h"
#include "bt_hci_bdroid.h"
#include "bte.h"
#include "btif_common.h"
#include "btu.h"
#include "btsnoop.h"
#include "bt_utils.h"
#include "btcore/include/counter.h"
#include "btcore/include/module.h"
#include "osi/include/fixed_queue.h"
#include "osi/include/future.h"
#include "gki.h"
#include "osi/include/hash_functions.h"
#include "osi/include/hash_map.h"
#include "hci_layer.h"
#include "osi/include/osi.h"
#include "osi/include/log.h"
#include "stack_config.h"
#include "osi/include/thread.h"
*/
/*******************************************************************************
** Constants & Macros
*******************************************************************************/
/* Run-time configuration file for BLE*/
/*
#ifndef BTE_BLE_STACK_CONF_FILE
#define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
#endif
*/
/******************************************************************************
** Variables
******************************************************************************/
/*******************************************************************************
** Static variables
*******************************************************************************/
static const hci_t *hci;
static void bte_main_enable(void);
/*******************************************************************************
** Static functions
*******************************************************************************/
/*******************************************************************************
** Externs
*******************************************************************************/
//extern void bte_load_ble_conf(const char *p_path);
fixed_queue_t *btu_hci_msg_queue;
/******************************************************************************
**
** Function bte_main_boot_entry
**
** Description BTE MAIN API - Entry point for BTE chip/stack initialization
**
** Returns None
**
******************************************************************************/
void bte_main_boot_entry(void)
{
if (gki_init())
LOG_ERROR("%s: Init GKI Module Failure.\n", __func__);
hci = hci_layer_get_interface();
if (!hci)
LOG_ERROR("%s could not get hci layer interface.\n", __func__);
btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
if (btu_hci_msg_queue == NULL) {
LOG_ERROR("%s unable to allocate hci message queue.\n", __func__);
return;
}
//Caution: No event dispatcher defined now in hci layer
//data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue);
hci->set_data_queue(btu_hci_msg_queue);
osi_alarm_init();
#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
//bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
#endif
//TODO: STACK CONFIG Module init
//module_init(get_module(STACK_CONFIG_MODULE));
// set up bt application task
#if (defined(BT_APP_DEMO) && BT_APP_DEMO == TRUE)
bt_app1_task_start_up();
// bt_app_start_up();
#endif
//Enbale HCI
bte_main_enable();
// LOG_ERROR("Test HCI Command\n");
// controller_get_interface()->devctl_reset(NULL);
//LOG_ERROR("Test bta_enable_bt\n");
//bt_test_bta_enable_bt();
//LOG_ERROR("Test ble_advertise\n");
//bt_test_ble_advertise();
}
/******************************************************************************
**
** Function bte_main_shutdown
**
** Description BTE MAIN API - Shutdown code for BTE chip/stack
**
** Returns None
**
******************************************************************************/
void bte_main_shutdown()
{
//data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL);
hci->set_data_queue(NULL);
fixed_queue_free(btu_hci_msg_queue, NULL);
btu_hci_msg_queue = NULL;
/*
module_clean_up(get_module(STACK_CONFIG_MODULE));
module_clean_up(get_module(COUNTER_MODULE));
module_clean_up(get_module(GKI_MODULE));
*/
gki_clean_up();
}
/******************************************************************************
**
** Function bte_main_enable
**
** Description BTE MAIN API - Creates all the BTE tasks. Should be called
** part of the Bluetooth stack enable sequence
**
** Returns None
**
******************************************************************************/
static void bte_main_enable(void)
{
LOG_DEBUG("Enable HCI\n");
if (hci_start_up()) {
LOG_ERROR("Start HCI Host Layer Failure\n");
return;
}
//Now Test Case Not Supported BTU
LOG_ERROR("Startup BTU\n");
BTU_StartUp();
}
/******************************************************************************
**
** Function bte_main_disable
**
** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
** part of the Bluetooth stack disable sequence
**
** Returns None
**
******************************************************************************/
void bte_main_disable(void)
{
/*
APPL_TRACE_DEBUG("%s", __FUNCTION__);
module_shut_down(get_module(HCI_MODULE));
module_shut_down(get_module(BTSNOOP_MODULE));
*/
hci_shut_down();
BTU_ShutDown();
}
/******************************************************************************
**
** Function bte_main_postload_cfg
**
** Description BTE MAIN API - Stack postload configuration
**
** Returns None
**
******************************************************************************/
void bte_main_postload_cfg(void)
{
hci->do_postload();
}
#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
/******************************************************************************
**
** Function bte_main_enable_lpm
**
** Description BTE MAIN API - Enable/Disable low power mode operation
**
** Returns None
**
******************************************************************************/
void bte_main_enable_lpm(BOOLEAN enable)
{
/*Enable Low power ?*/
//hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
}
/******************************************************************************
**
** Function bte_main_lpm_allow_bt_device_sleep
**
** Description BTE MAIN API - Allow BT controller goest to sleep
**
** Returns None
**
******************************************************************************/
void bte_main_lpm_allow_bt_device_sleep()
{
/**/
//hci->send_low_power_command(LPM_WAKE_DEASSERT);
}
/******************************************************************************
**
** Function bte_main_lpm_wake_bt_device
**
** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
**
** Returns None
**
******************************************************************************/
void bte_main_lpm_wake_bt_device()
{
//hci->send_low_power_command(LPM_WAKE_ASSERT);
}
#endif // HCILP_INCLUDED
/******************************************************************************
**
** Function bte_main_hci_send
**
** Description BTE MAIN API - This function is called by the upper stack to
** send an HCI message. The function displays a protocol trace
** message (if enabled), and then calls the 'transmit' function
** associated with the currently selected HCI transport
**
** Returns None
**
******************************************************************************/
void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
{
UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
p_msg->event = event;
//counter_add("main.tx.packets", 1);
//counter_add("main.tx.bytes", p_msg->len);
if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
(sub_event == LOCAL_BLE_CONTROLLER_ID))
{
hci->transmit_downward(event, p_msg);
}
else
{
//APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
GKI_freebuf(p_msg);
}
}

12
components/bt/bluedroid/mk.sh Executable file
View file

@ -0,0 +1,12 @@
#!/bin/bash
FIND_F=component.mk
MV_F=Makefile.backup
for mkf in `find . -name "$FIND_F"`
do
echo $mkf
mkf_n=`echo $mkf | sed -n "s/$FIND_F/$MV_F/p"`
echo $mkf_n
mv $mkf $mkf_n
done

View file

@ -0,0 +1,184 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "bt_defs.h"
#include "bt_trace.h"
#include "alarm.h"
#include "allocator.h"
#include "list.h"
#include "thread.h"
#include "freertos/FreeRTOSConfig.h"
#include "freertos/xtensa_api.h"
#include "rom/ets_sys.h"
#define RTC_TIMER_TICKS_TO_MS(ticks) ((ticks/625)<<1 + (ticks-(ticks/625)*625)/312)
#define BT_ALARM_START_WAIT_TICKS 100
#define BT_ALARM_STOP_WAIT_TICKS 100
#define BT_ALARM_FREE_WAIT_TICKS 100
#define BT_ALARM_CHG_PERIOD_WAIT_TICKS 100
static struct alarm_t alarm_cbs[ALARM_CBS_NUM];
void osi_alarm_init(void)
{
memset(&alarm_cbs[0], 0x00, sizeof(alarm_cbs));
}
static struct alarm_t *alarm_cbs_lookfor_available(void)
{
int i;
for (i = 0; i < ALARM_CBS_NUM; i++) {
if (alarm_cbs[i].alarm_hdl == NULL) { //available
ets_printf(">>>> %d %08x<<<<\n", i, &alarm_cbs[i]);
return &alarm_cbs[i];
}
}
return NULL;
}
static void alarm_cb_handler(TimerHandle_t xTimer)
{
struct alarm_t *alarm;
ets_printf("*********************************************************\n");
if (!xTimer) {
ets_printf("TimerName: NULL\n");
return;
}
alarm = pvTimerGetTimerID(xTimer);
ets_printf("TimerID %08x, Name %s\n", alarm, pcTimerGetTimerName(xTimer));
if (alarm->cb) {
alarm->cb(alarm->cb_data);
}
}
osi_alarm_t *osi_alarm_new(char *alarm_name, osi_alarm_callback_t callback, void *data, period_ms_t timer_expire)
{
struct alarm_t *timer_id;
TimerHandle_t t;
if (timer_expire == 0)
timer_expire = 1000;
/* TODO mutex lock */
timer_id = alarm_cbs_lookfor_available();
if (!timer_id) {
ets_printf("%s full\n", __func__);
return NULL;
}
t = xTimerCreate(alarm_name, timer_expire / portTICK_PERIOD_MS, pdFALSE, timer_id, alarm_cb_handler);
if (!t) {
ets_printf("%s error\n", __func__);
return NULL;
}
timer_id->alarm_hdl = t;
timer_id->cb = callback;
timer_id->cb_data = data;
/* TODO mutex unlock */
return timer_id;
}
int osi_alarm_free(osi_alarm_t *alarm)
{
if (!alarm) {
ets_printf("%s null\n", __func__);
return -1;
}
if (xTimerDelete(alarm->alarm_hdl, BT_ALARM_FREE_WAIT_TICKS) != pdPASS) {
ets_printf("%s error\n", __func__);
return -2;
}
/* TODO mutex lock */
memset(alarm, 0x00, sizeof(osi_alarm_t));
/* TODO mutex unlock */
return 0;
}
int osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout) {
if (!alarm) {
ets_printf("%s null\n", __func__);
return -1;
}
if (xTimerChangePeriod(alarm->alarm_hdl, timeout / portTICK_PERIOD_MS, BT_ALARM_CHG_PERIOD_WAIT_TICKS) != pdPASS) {
ets_printf("%s chg period error\n", __func__);
return -2;
}
if (xTimerStart(alarm->alarm_hdl, BT_ALARM_START_WAIT_TICKS) != pdPASS) {
ets_printf("%s start error\n", __func__);
return -3;
}
return 0;
}
int osi_alarm_cancel(osi_alarm_t *alarm) {
if (!alarm) {
ets_printf("%s null\n", __func__);
return -1;
}
if (xTimerStop(alarm->alarm_hdl, BT_ALARM_STOP_WAIT_TICKS) != pdPASS) {
ets_printf("%s error\n", __func__);
return -2;
}
return 0;
}
static uint32_t alarm_current_tick(void) {
return xTaskGetTickCount();
}
// todo: this is not accurate
// max return value: 0xffffffff / 312 = 13765920 = 0xD20D20
period_ms_t osi_alarm_now(void) {
return RTC_TIMER_TICKS_TO_MS((alarm_current_tick()));
}
period_ms_t osi_alarm_get_remaining_ms(const osi_alarm_t *alarm)
{
/* TODO: use FreeRTOS timer.c implement ??? */
return 0xffffffff;
}
// pre-condition: 0 <= t1, t2 <= 0xD20D20
// return value: 0<= ret <=0XD20D20
period_ms_t osi_alarm_time_diff(period_ms_t t1, period_ms_t t2) {
#define MAX_ALARM_TIME_MS (0xD20D20)
int32_t diff = (int32_t)(t1) - (int32_t)(t2);
if (diff < 0)
diff += MAX_ALARM_TIME_MS;
return (period_ms_t)diff;
}

View file

@ -0,0 +1,58 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "bt_defs.h"
#include "allocator.h"
extern void *pvPortZalloc(size_t size);
extern void vPortFree(void *pv);
char *osi_strdup(const char *str) {
size_t size = strlen(str) + 1; // + 1 for the null terminator
char *new_string = (char *)calloc(1, size);
if (!new_string)
return NULL;
memcpy(new_string, str, size);
return new_string;
}
void *osi_malloc(size_t size) {
return calloc(1, size);
}
void *osi_calloc(size_t size) {
return calloc(1, size);
}
void osi_free(void *ptr) {
free(ptr);
}
const allocator_t allocator_malloc = {
osi_malloc,
osi_free
};
const allocator_t allocator_calloc = {
osi_calloc,
osi_free
};

View file

@ -0,0 +1,96 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <stdint.h>
#include "bt_trace.h"
#include "allocator.h"
#include "buffer.h"
#include "bt_defs.h"
#include "bt_trace.h"
struct buffer_t {
buffer_t *root;
size_t refcount;
size_t length;
uint8_t data[];
};
buffer_t *buffer_new(size_t size) {
assert(size > 0);
buffer_t *buffer = osi_malloc(sizeof(buffer_t) + size);
if (!buffer) {
LOG_ERROR("%s unable to allocate buffer of %zu bytes.", __func__, size);
return NULL;
}
buffer->root = buffer;
buffer->refcount = 1;
buffer->length = size;
return buffer;
}
buffer_t *buffer_new_ref(const buffer_t *buf) {
assert(buf != NULL);
return buffer_new_slice(buf, buf->length);
}
buffer_t *buffer_new_slice(const buffer_t *buf, size_t slice_size) {
assert(buf != NULL);
assert(slice_size > 0);
assert(slice_size <= buf->length);
buffer_t *ret = osi_calloc(sizeof(buffer_t));
if (!ret) {
LOG_ERROR("%s unable to allocate new buffer for slice of length %zu.", __func__, slice_size);
return NULL;
}
ret->root = buf->root;
ret->refcount = SIZE_MAX;
ret->length = slice_size;
++buf->root->refcount;
return ret;
}
void buffer_free(buffer_t *buffer) {
if (!buffer)
return;
if (buffer->root != buffer) {
// We're a leaf node. Delete the root node if we're the last referent.
if (--buffer->root->refcount == 0)
osi_free(buffer->root);
osi_free(buffer);
} else if (--buffer->refcount == 0) {
// We're a root node. Roots are only deleted when their refcount goes to 0.
osi_free(buffer);
}
}
void *buffer_ptr(const buffer_t *buf) {
assert(buf != NULL);
return buf->root->data + buf->root->length - buf->length;
}
size_t buffer_length(const buffer_t *buf) {
assert(buf != NULL);
return buf->length;
}

View file

@ -0,0 +1,187 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include "bt_defs.h"
#include "allocator.h"
#include "fixed_queue.h"
#include "list.h"
#include "osi.h"
#include "bt_trace.h"
typedef struct fixed_queue_t {
list_t *list;
//semaphore_t *enqueue_sem;
//semaphore_t *dequeue_sem;
pthread_mutex_t lock;
size_t capacity;
fixed_queue_cb dequeue_ready;
/*
reactor_object_t *dequeue_object;
fixed_queue_cb dequeue_ready;
void *dequeue_context;
*/
} fixed_queue_t;
//static void internal_dequeue_ready(void *context);
fixed_queue_t *fixed_queue_new(size_t capacity) {
fixed_queue_t *ret = osi_calloc(sizeof(fixed_queue_t));
if (!ret)
goto error;
pthread_mutex_init(&ret->lock, NULL);
ret->capacity = capacity;
ret->list = list_new(NULL);
if (!ret->list)
goto error;
/*
ret->enqueue_sem = semaphore_new(capacity);
if (!ret->enqueue_sem)
goto error;
ret->dequeue_sem = semaphore_new(0);
if (!ret->dequeue_sem)
goto error;
*/
return ret;
error:;
fixed_queue_free(ret, NULL);
return NULL;
}
void fixed_queue_free(fixed_queue_t *queue, fixed_queue_free_cb free_cb) {
const list_node_t *node;
if (!queue)
return;
// fixed_queue_unregister_dequeue(queue);
if (free_cb)
for (node = list_begin(queue->list); node != list_end(queue->list); node = list_next(node))
free_cb(list_node(node));
list_free(queue->list);
// semaphore_free(queue->enqueue_sem);
// semaphore_free(queue->dequeue_sem);
pthread_mutex_destroy(&queue->lock);
osi_free(queue);
}
bool fixed_queue_is_empty(fixed_queue_t *queue) {
bool is_empty = false;
assert(queue != NULL);
pthread_mutex_lock(&queue->lock);
is_empty = list_is_empty(queue->list);
pthread_mutex_unlock(&queue->lock);
return is_empty;
}
size_t fixed_queue_capacity(fixed_queue_t *queue) {
assert(queue != NULL);
return queue->capacity;
}
void fixed_queue_enqueue(fixed_queue_t *queue, void *data) {
assert(queue != NULL);
assert(data != NULL);
// semaphore_wait(queue->enqueue_sem);
pthread_mutex_lock(&queue->lock);
list_append(queue->list, data);
pthread_mutex_unlock(&queue->lock);
// semaphore_post(queue->dequeue_sem);
}
void *fixed_queue_dequeue(fixed_queue_t *queue) {
void *ret = NULL;
assert(queue != NULL);
// semaphore_wait(queue->dequeue_sem);
pthread_mutex_lock(&queue->lock);
ret = list_front(queue->list);
list_remove(queue->list, ret);
pthread_mutex_unlock(&queue->lock);
// semaphore_post(queue->enqueue_sem);
return ret;
}
/*
void *fixed_queue_try_dequeue(fixed_queue_t *queue) {
void *ret = NULL;
assert(queue != NULL);
if (!semaphore_try_wait(queue->dequeue_sem))
return NULL;
pthread_mutex_lock(&queue->lock);
ret = list_front(queue->list);
list_remove(queue->list, ret);
pthread_mutex_unlock(&queue->lock);
semaphore_post(queue->enqueue_sem);
return ret;
}
int fixed_queue_get_dequeue_fd(const fixed_queue_t *queue) {
assert(queue != NULL);
return semaphore_get_fd(queue->dequeue_sem);
}
*/
void fixed_queue_register_dequeue(fixed_queue_t *queue, fixed_queue_cb ready_cb) {
assert(queue != NULL);
assert(ready_cb != NULL);
queue->dequeue_ready = ready_cb;
}
void fixed_queue_unregister_dequeue(fixed_queue_t *queue) {
assert(queue != NULL);
queue->dequeue_ready = NULL;
}
void fixed_queue_process(fixed_queue_t *queue) {
if (queue == NULL)
return;
if (queue->dequeue_ready)
queue->dequeue_ready(queue);
}
/*
static void internal_dequeue_ready(void *context) {
assert(context != NULL);
fixed_queue_t *queue = context;
queue->dequeue_ready(queue, queue->dequeue_context);
}
*/

View file

@ -0,0 +1,42 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <string.h>
#include "hash_functions.h"
hash_index_t hash_function_naive(const void *key) {
return (hash_index_t)key;
}
hash_index_t hash_function_integer(const void *key) {
return ((hash_index_t)key) * 2654435761;
}
hash_index_t hash_function_pointer(const void *key) {
return ((hash_index_t)key) * 2654435761;
}
hash_index_t hash_function_string(const void *key) {
hash_index_t hash = 5381;
const char *name = (const char *)key;
size_t string_len = strlen(name);
for (size_t i = 0; i < string_len; ++i)
hash = ((hash << 5) + hash ) + name[i];
return hash;
}

Some files were not shown because too many files have changed in this diff Show more