2016-11-04 18:48:16 +00:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2016-09-26 13:37:39 +00:00
|
|
|
/***************************************************************
|
|
|
|
* *
|
|
|
|
* * 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"
|
2016-11-17 11:37:56 +00:00
|
|
|
#include "esp_gatts_api.h"
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
#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]
|
2016-11-02 11:04:11 +00:00
|
|
|
esp_gattc_if_t client_if;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-02 11:04:11 +00:00
|
|
|
esp_bt_uuid_t bas_uuid = {LEN_UUID_16, {UUID_SERVCLASS_BATTERY}};
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-02 11:04:11 +00:00
|
|
|
uint16_t get_uuid16(esp_bt_uuid_t* p_uuid)
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
|
|
|
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*/
|
2016-11-02 11:04:11 +00:00
|
|
|
void bta_le_fill_16bits_srvc_id(bool is_pri, UINT8 inst_id, UINT16 srvc_uuid, esp_gatt_srvc_id_t* p_output)
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
2016-11-02 11:04:11 +00:00
|
|
|
memset((void *)p_output, 0, sizeof(esp_gatt_srvc_id_t));
|
2016-09-26 13:37:39 +00:00
|
|
|
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*/
|
2016-11-02 11:04:11 +00:00
|
|
|
void bta_le_fill_16bits_char_id(UINT8 inst_id, UINT16 char_uuid, esp_gatt_id_t* p_output)
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
2016-11-02 11:04:11 +00:00
|
|
|
memset((void *)p_output, 0, sizeof(esp_gatt_id_t));
|
2016-09-26 13:37:39 +00:00
|
|
|
bta_le_fill_16bits_gatt_id(inst_id, char_uuid, p_output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
** Function bas_gattc_callback
|
|
|
|
**
|
|
|
|
** Description battery service register callback function
|
|
|
|
*******************************************************************************/
|
2016-11-02 11:04:11 +00:00
|
|
|
static void bas_gattc_callback(esp_gattc_evt_t event, esp_gattc_t* p_data)
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
|
|
|
switch (event)
|
|
|
|
{
|
2016-11-02 11:04:11 +00:00
|
|
|
case ESP_GATTC_REG_EVT:
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
2016-11-02 11:04:11 +00:00
|
|
|
esp_gatt_status_t status = p_data->reg_oper.status;
|
2016-09-26 13:37:39 +00:00
|
|
|
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*/
|
2016-11-02 11:04:11 +00:00
|
|
|
case ESP_GATTC_OPEN_EVT:
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
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*/
|
2016-11-02 11:04:11 +00:00
|
|
|
esp_ble_gattc_svc_search_req(conn_id, NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-11-02 11:04:11 +00:00
|
|
|
case ESP_GATTC_SEARCH_RES_EVT:
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
2016-11-02 11:04:11 +00:00
|
|
|
case ESP_GATTC_SEARCH_CMPL_EVT:
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
|
|
|
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);
|
2016-11-02 11:04:11 +00:00
|
|
|
status = esp_ble_gattc_get_first_char(p_data->search_cmpl.conn_id, &battery_srvc_id, NULL,
|
2016-09-26 13:37:39 +00:00
|
|
|
&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);
|
|
|
|
|
2016-11-02 11:04:11 +00:00
|
|
|
esp_ble_gattc_read_char(p_data->search_cmpl.conn_id, &battery_char_id,
|
2016-09-26 13:37:39 +00:00
|
|
|
BTA_GATT_AUTH_REQ_NONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-11-02 11:04:11 +00:00
|
|
|
case ESP_GATTC_READ_CHAR_EVT:
|
2016-09-26 13:37:39 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-11-02 11:04:11 +00:00
|
|
|
esp_ble_gattc_app_register(&bas_uuid, bas_gattc_callback);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|