mirror of
https://github.com/espressif/esp-idf
synced 2025-03-10 01:29:21 -04:00
component/bt: modify bluetooth API
1. VHCI api and doxygen 2. Controller api and doxygen 3. bluedroid init/enable api and doxygen 4. cleanup demo codes
This commit is contained in:
parent
d6fcec73b2
commit
2e7748d625
@ -21,7 +21,7 @@
|
||||
static bool esp_already_enable = false;
|
||||
static bool esp_already_init = false;
|
||||
|
||||
esp_err_t esp_enable_bluetooth(void)
|
||||
esp_err_t esp_bluedroid_enable(void)
|
||||
{
|
||||
btc_msg_t msg;
|
||||
future_t **future_p;
|
||||
@ -53,7 +53,7 @@ esp_err_t esp_enable_bluetooth(void)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_disable_bluetooth(void)
|
||||
esp_err_t esp_bluedroid_disable(void)
|
||||
{
|
||||
btc_msg_t msg;
|
||||
future_t **future_p;
|
||||
@ -85,7 +85,7 @@ esp_err_t esp_disable_bluetooth(void)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_init_bluetooth(void)
|
||||
esp_err_t esp_bluedroid_init(void)
|
||||
{
|
||||
btc_msg_t msg;
|
||||
future_t **future_p;
|
||||
@ -120,7 +120,7 @@ esp_err_t esp_init_bluetooth(void)
|
||||
}
|
||||
|
||||
|
||||
esp_err_t esp_deinit_bluetooth(void)
|
||||
esp_err_t esp_bluedroid_deinit(void)
|
||||
{
|
||||
btc_msg_t msg;
|
||||
future_t **future_p;
|
||||
|
@ -22,22 +22,22 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Enable bluetooth, must after esp_init_bluetooth()
|
||||
* @brief Enable bluetooth, must after esp_bluedroid_init()
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK : Succeed
|
||||
* - Other : Failed
|
||||
*/
|
||||
esp_err_t esp_enable_bluetooth(void);
|
||||
esp_err_t esp_bluedroid_enable(void);
|
||||
|
||||
/**
|
||||
* @brief Disable bluetooth, must prior to esp_deinit_bluetooth()
|
||||
* @brief Disable bluetooth, must prior to esp_bluedroid_deinit()
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK : Succeed
|
||||
* - Other : Failed
|
||||
*/
|
||||
esp_err_t esp_disable_bluetooth(void);
|
||||
esp_err_t esp_bluedroid_disable(void);
|
||||
|
||||
/**
|
||||
* @brief Init and alloc the resource for bluetooth, must be prior to every bluetooth stuff
|
||||
@ -46,7 +46,7 @@ esp_err_t esp_disable_bluetooth(void);
|
||||
* - ESP_OK : Succeed
|
||||
* - Other : Failed
|
||||
*/
|
||||
esp_err_t esp_init_bluetooth(void);
|
||||
esp_err_t esp_bluedroid_init(void);
|
||||
|
||||
/**
|
||||
* @brief Deinit and free the resource for bluetooth, must be after every bluetooth stuff
|
||||
@ -55,7 +55,7 @@ esp_err_t esp_init_bluetooth(void);
|
||||
* - ESP_OK : Succeed
|
||||
* - Other : Failed
|
||||
*/
|
||||
esp_err_t esp_deinit_bluetooth(void);
|
||||
esp_err_t esp_bluedroid_deinit(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ typedef struct {
|
||||
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 const esp_vhci_host_callback_t vhci_host_cb;
|
||||
|
||||
static xTaskHandle xHciH4TaskHandle;
|
||||
static xQueueHandle xHciH4Queue;
|
||||
@ -105,7 +105,7 @@ static bool hal_open(const hci_hal_callbacks_t *upper_callbacks)
|
||||
xTaskCreate(hci_hal_h4_rx_handler, HCI_H4_TASK_NAME, HCI_H4_TASK_STACK_SIZE, NULL, HCI_H4_TASK_PRIO, &xHciH4TaskHandle);
|
||||
|
||||
//register vhci host cb
|
||||
API_vhci_host_register_callback(&vhci_host_cb);
|
||||
esp_vhci_host_register_callback(&vhci_host_cb);
|
||||
|
||||
|
||||
return true;
|
||||
@ -148,7 +148,7 @@ static uint16_t transmit_data(serial_data_type_t type,
|
||||
BTTRC_DUMP_BUFFER("Transmit Pkt", data, length);
|
||||
|
||||
// TX Data to target
|
||||
API_vhci_host_send_packet(data, length);
|
||||
esp_vhci_host_send_packet(data, length);
|
||||
|
||||
// Be nice and restore the old value of that byte
|
||||
*(data) = previous_byte;
|
||||
@ -278,7 +278,7 @@ static int host_recv_pkt_cb(uint8_t *data, uint16_t len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const vhci_host_callback_t vhci_host_cb = {
|
||||
static const esp_vhci_host_callback_t vhci_host_cb = {
|
||||
.notify_host_send_available = host_send_pkt_available_cb,
|
||||
.notify_host_recv = host_recv_pkt_cb,
|
||||
};
|
||||
|
@ -228,7 +228,7 @@ static void hci_host_thread_handler(void *arg)
|
||||
if (pdTRUE == xQueueReceive(xHciHostQueue, &e, (portTickType)portMAX_DELAY)) {
|
||||
|
||||
if (e.sig == 0xff) {
|
||||
if (API_vhci_host_check_send_available()) {
|
||||
if (esp_vhci_host_check_send_available()) {
|
||||
/*Now Target only allowed one packet per TX*/
|
||||
BT_HDR *pkt = packet_fragmenter->fragment_current_packet();
|
||||
if (pkt != NULL) {
|
||||
|
@ -35,6 +35,15 @@
|
||||
extern void btdm_osi_funcs_register(void *osi_funcs);
|
||||
extern void btdm_controller_init(void);
|
||||
|
||||
/* VHCI function interface */
|
||||
typedef struct vhci_host_callback {
|
||||
void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
|
||||
int (*notify_host_recv)(uint8_t *data, uint16_t len); /*!< callback used to notify that the controller has a packet to send to the host*/
|
||||
} vhci_host_callback_t;
|
||||
|
||||
extern bool API_vhci_host_check_send_available(void);
|
||||
extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
|
||||
extern void API_vhci_host_register_callback(const vhci_host_callback_t *callback);
|
||||
|
||||
#define BT_DEBUG(...)
|
||||
#define BT_API_CALL_CHECK(info, api_call, ret) \
|
||||
@ -118,13 +127,28 @@ static struct osi_funcs_t osi_funcs = {
|
||||
._read_efuse_mac = esp_efuse_read_mac,
|
||||
};
|
||||
|
||||
bool esp_vhci_host_check_send_available(void)
|
||||
{
|
||||
return API_vhci_host_check_send_available();
|
||||
}
|
||||
|
||||
void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
|
||||
{
|
||||
API_vhci_host_send_packet(data, len);
|
||||
}
|
||||
|
||||
void esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
|
||||
{
|
||||
API_vhci_host_register_callback((const vhci_host_callback_t *)callback);
|
||||
}
|
||||
|
||||
static void bt_controller_task(void *pvParam)
|
||||
{
|
||||
btdm_osi_funcs_register(&osi_funcs);
|
||||
btdm_controller_init();
|
||||
}
|
||||
|
||||
void bt_controller_init()
|
||||
void esp_bt_controller_init()
|
||||
{
|
||||
xTaskCreatePinnedToCore(bt_controller_task, "btController",
|
||||
ESP_TASK_BT_CONTROLLER_STACK, NULL,
|
||||
|
@ -29,35 +29,35 @@ extern "C" {
|
||||
*
|
||||
* This function should be called only once, before any other BT functions are called.
|
||||
*/
|
||||
void bt_controller_init(void);
|
||||
void esp_bt_controller_init(void);
|
||||
|
||||
/** @brief vhci_host_callback
|
||||
/** @brief esp_vhci_host_callback
|
||||
* used for vhci call host function to notify what host need to do
|
||||
*/
|
||||
typedef struct vhci_host_callback {
|
||||
typedef struct esp_vhci_host_callback {
|
||||
void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
|
||||
int (*notify_host_recv)(uint8_t *data, uint16_t len); /*!< callback used to notify that the controller has a packet to send to the host*/
|
||||
} vhci_host_callback_t;
|
||||
} esp_vhci_host_callback_t;
|
||||
|
||||
/** @brief API_vhci_host_check_send_available
|
||||
/** @brief esp_vhci_host_check_send_available
|
||||
* used for check actively if the host can send packet to controller or not.
|
||||
* @return true for ready to send, false means cannot send packet
|
||||
*/
|
||||
bool API_vhci_host_check_send_available(void);
|
||||
bool esp_vhci_host_check_send_available(void);
|
||||
|
||||
/** @brief API_vhci_host_send_packet
|
||||
/** @brief esp_vhci_host_send_packet
|
||||
* host send packet to controller
|
||||
* @param data the packet point
|
||||
*,@param len the packet length
|
||||
*/
|
||||
void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
|
||||
void esp_vhci_host_send_packet(uint8_t *data, uint16_t len);
|
||||
|
||||
/** @brief API_vhci_host_register_callback
|
||||
/** @brief esp_vhci_host_register_callback
|
||||
* register the vhci referece callback, the call back
|
||||
* struct defined by vhci_host_callback structure.
|
||||
* @param callback vhci_host_callback type variable
|
||||
* @param callback esp_vhci_host_callback type variable
|
||||
*/
|
||||
void API_vhci_host_register_callback(const vhci_host_callback_t *callback);
|
||||
void esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -229,7 +229,7 @@ esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_da
|
||||
*
|
||||
* Applications which don't need to enable PHY on every start up should
|
||||
* disable this menuconfig option and call esp_phy_init before calling
|
||||
* esp_wifi_init or bt_controller_init. See do_phy_init function in
|
||||
* esp_wifi_init or esp_bt_controller_init. See do_phy_init function in
|
||||
* cpu_start.c for an example of using this function.
|
||||
*
|
||||
* @param init_data PHY parameters. Default set of parameters can
|
||||
|
@ -4,7 +4,7 @@ Bluetooth
|
||||
.. toctree::
|
||||
:caption: Bluetooth APIs
|
||||
|
||||
Bluetooth VHCI <vhci>
|
||||
Bluetooth Controller && VHCI <controller_vhci>
|
||||
Bluetooth Common <bt_common>
|
||||
Bluetooth Classic
|
||||
Bluetooth LE <bt_le>
|
||||
|
@ -1,5 +1,5 @@
|
||||
VHCI
|
||||
====
|
||||
Controller && VHCI
|
||||
==================
|
||||
|
||||
Overview
|
||||
--------
|
||||
@ -30,12 +30,24 @@ Header Files
|
||||
Type Definitions
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
.. doxygenstruct:: vhci_host_callback
|
||||
.. doxygentypedef:: esp_vhci_host_callback_t
|
||||
|
||||
Enumerations
|
||||
^^^^^^^^^^^^
|
||||
|
||||
|
||||
Structures
|
||||
^^^^^^^^^^
|
||||
|
||||
.. doxygenstruct:: esp_vhci_host_callback
|
||||
:members:
|
||||
|
||||
|
||||
Functions
|
||||
^^^^^^^^^
|
||||
|
||||
.. doxygenfunction:: API_vhci_host_check_send_available
|
||||
.. doxygenfunction:: API_vhci_host_register_callback
|
||||
.. doxygenfunction:: API_vhci_host_send_packet
|
||||
.. doxygenfunction:: bt_controller_init
|
||||
.. doxygenfunction:: esp_bt_controller_init
|
||||
.. doxygenfunction:: esp_vhci_host_check_send_available
|
||||
.. doxygenfunction:: esp_vhci_host_send_packet
|
||||
.. doxygenfunction:: esp_vhci_host_register_callback
|
||||
|
@ -42,8 +42,8 @@ Structures
|
||||
Functions
|
||||
^^^^^^^^^
|
||||
|
||||
.. doxygenfunction:: esp_enable_bluetooth
|
||||
.. doxygenfunction:: esp_disable_bluetooth
|
||||
.. doxygenfunction:: esp_init_bluetooth
|
||||
.. doxygenfunction:: esp_deinit_bluetooth
|
||||
.. doxygenfunction:: esp_bluedroid_enable
|
||||
.. doxygenfunction:: esp_bluedroid_disable
|
||||
.. doxygenfunction:: esp_bluedroid_init
|
||||
.. doxygenfunction:: esp_bluedroid_deinit
|
||||
|
||||
|
@ -73,7 +73,7 @@ static int host_rcv_pkt(uint8_t *data, uint16_t len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static vhci_host_callback_t vhci_host_cb = {
|
||||
static esp_vhci_host_callback_t vhci_host_cb = {
|
||||
controller_rcv_pkt_ready,
|
||||
host_rcv_pkt
|
||||
};
|
||||
@ -139,13 +139,13 @@ static uint16_t make_cmd_ble_set_adv_data(uint8_t *buf, uint8_t data_len, uint8_
|
||||
static void hci_cmd_send_reset(void)
|
||||
{
|
||||
uint16_t sz = make_cmd_reset (hci_cmd_buf);
|
||||
API_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
esp_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
}
|
||||
|
||||
static void hci_cmd_send_ble_adv_start(void)
|
||||
{
|
||||
uint16_t sz = make_cmd_ble_set_adv_enable (hci_cmd_buf, 1);
|
||||
API_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
esp_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
}
|
||||
|
||||
static void hci_cmd_send_ble_set_adv_param(void)
|
||||
@ -168,7 +168,7 @@ static void hci_cmd_send_ble_set_adv_param(void)
|
||||
peer_addr,
|
||||
adv_chn_map,
|
||||
adv_filter_policy);
|
||||
API_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
esp_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
}
|
||||
|
||||
static void hci_cmd_send_ble_set_adv_data(void)
|
||||
@ -185,7 +185,7 @@ static void hci_cmd_send_ble_set_adv_data(void)
|
||||
adv_data_len = 5 + name_len;
|
||||
|
||||
uint16_t sz = make_cmd_ble_set_adv_data(hci_cmd_buf, adv_data_len, (uint8_t *)adv_data);
|
||||
API_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
esp_vhci_host_send_packet(hci_cmd_buf, sz);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -195,11 +195,11 @@ void bleAdvtTask(void *pvParameters)
|
||||
{
|
||||
int cmd_cnt = 0;
|
||||
bool send_avail = false;
|
||||
API_vhci_host_register_callback(&vhci_host_cb);
|
||||
esp_vhci_host_register_callback(&vhci_host_cb);
|
||||
printf("BLE advt task start\n");
|
||||
while (1) {
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
send_avail = API_vhci_host_check_send_available();
|
||||
send_avail = esp_vhci_host_check_send_available();
|
||||
if (send_avail) {
|
||||
switch (cmd_cnt) {
|
||||
case 0: hci_cmd_send_reset(); ++cmd_cnt; break;
|
||||
@ -214,7 +214,7 @@ void bleAdvtTask(void *pvParameters)
|
||||
|
||||
void app_main()
|
||||
{
|
||||
bt_controller_init();
|
||||
esp_bt_controller_init();
|
||||
xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0);
|
||||
}
|
||||
|
||||
|
@ -107,7 +107,7 @@ esp_err_t blufi_enable(void *arg)
|
||||
{
|
||||
esp_err_t err;
|
||||
|
||||
err = esp_enable_bluetooth();
|
||||
err = esp_bluedroid_enable();
|
||||
if (err) {
|
||||
LOG_ERROR("%s failed\n", __func__);
|
||||
return err;
|
||||
@ -122,7 +122,7 @@ esp_err_t blufi_disable(void *arg)
|
||||
{
|
||||
esp_err_t err;
|
||||
|
||||
err = esp_disable_bluetooth();
|
||||
err = esp_bluedroid_disable();
|
||||
|
||||
if (arg) {
|
||||
((void (*)(void))arg)();
|
||||
|
@ -68,8 +68,8 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||
esp_blufi_send_config_state(ESP_BLUFI_CONFIG_OK);
|
||||
esp_disable_bluetooth(); //close bluetooth function
|
||||
//esp_deinit_bluetooth(); //free bluetooth resource
|
||||
esp_bluedroid_disable(); //close bluetooth function
|
||||
//esp_bluedroid_deinit(); //free bluetooth resource
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
/* This is a workaround as ESP32 WiFi libs don't currently
|
||||
@ -131,11 +131,11 @@ void app_main()
|
||||
|
||||
//vTaskDelay(3000 / portTICK_PERIOD_MS);
|
||||
|
||||
bt_controller_init();
|
||||
esp_bt_controller_init();
|
||||
xTaskCreatePinnedToCore(&wifiTestTask, "wifiTestTask", 2048, NULL, 20, NULL, 0);
|
||||
|
||||
LOG_ERROR("%s init bluetooth\n", __func__);
|
||||
ret = esp_init_bluetooth();
|
||||
ret = esp_bluedroid_init();
|
||||
if (ret) {
|
||||
LOG_ERROR("%s init bluetooth failed\n", __func__);
|
||||
return;
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// 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
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
@ -30,6 +30,8 @@
|
||||
#include "esp_bt_main.h"
|
||||
#include "esp_bt_main.h"
|
||||
|
||||
#define GATTS_TAG "GATTS_DEMO"
|
||||
|
||||
///Declare the static function
|
||||
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
|
||||
static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
|
||||
@ -115,8 +117,6 @@ static struct gatts_profile_inst gl_profile_tab[PROFILE_NUM] = {
|
||||
|
||||
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
||||
{
|
||||
LOG_ERROR("GAP_EVT, event %d\n", event);
|
||||
|
||||
switch (event) {
|
||||
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
|
||||
esp_ble_gap_start_advertising(&test_adv_params);
|
||||
@ -129,23 +129,19 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
|
||||
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
|
||||
switch (event) {
|
||||
case ESP_GATTS_REG_EVT:
|
||||
LOG_INFO("REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
|
||||
ESP_LOGI(GATTS_TAG, "REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.is_primary = true;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.inst_id = 0x00;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST_A;
|
||||
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gap_config_adv_data(&test_adv_data);
|
||||
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_A);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
break;
|
||||
case ESP_GATTS_READ_EVT: {
|
||||
LOG_INFO("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
|
||||
ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
|
||||
esp_gatt_rsp_t rsp;
|
||||
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
|
||||
rsp.attr_value.handle = param->read.handle;
|
||||
@ -159,8 +155,8 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
break;
|
||||
}
|
||||
case ESP_GATTS_WRITE_EVT: {
|
||||
LOG_INFO("GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
|
||||
LOG_INFO("GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
|
||||
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
|
||||
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
|
||||
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
|
||||
break;
|
||||
}
|
||||
@ -170,7 +166,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
case ESP_GATTS_UNREG_EVT:
|
||||
break;
|
||||
case ESP_GATTS_CREATE_EVT:
|
||||
LOG_INFO("CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
|
||||
ESP_LOGI(GATTS_TAG, "CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_A;
|
||||
@ -184,7 +180,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
case ESP_GATTS_ADD_INCL_SRVC_EVT:
|
||||
break;
|
||||
case ESP_GATTS_ADD_CHAR_EVT:
|
||||
LOG_INFO("ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
ESP_LOGI(GATTS_TAG, "ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
|
||||
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_handle = param->add_char.attr_handle;
|
||||
@ -194,19 +190,19 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
|
||||
break;
|
||||
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
|
||||
LOG_INFO("ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
ESP_LOGI(GATTS_TAG, "ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
|
||||
break;
|
||||
case ESP_GATTS_DELETE_EVT:
|
||||
break;
|
||||
case ESP_GATTS_START_EVT:
|
||||
LOG_INFO("SERVICE_START_EVT, status %d, service_handle %d\n",
|
||||
ESP_LOGI(GATTS_TAG, "SERVICE_START_EVT, status %d, service_handle %d\n",
|
||||
param->start.status, param->start.service_handle);
|
||||
break;
|
||||
case ESP_GATTS_STOP_EVT:
|
||||
break;
|
||||
case ESP_GATTS_CONNECT_EVT:
|
||||
LOG_INFO("SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
|
||||
ESP_LOGI(GATTS_TAG, "SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
|
||||
param->connect.conn_id,
|
||||
param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
|
||||
param->connect.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5],
|
||||
@ -227,23 +223,19 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
|
||||
switch (event) {
|
||||
case ESP_GATTS_REG_EVT:
|
||||
LOG_INFO("REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
|
||||
ESP_LOGI(GATTS_TAG, "REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.is_primary = true;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.inst_id = 0x00;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_id.id.uuid.uuid.uuid16 = GATTS_SERVICE_UUID_TEST_B;
|
||||
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gap_config_adv_data(&test_adv_data);
|
||||
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_A_APP_ID].service_id, GATTS_NUM_HANDLE_TEST_B);
|
||||
LOG_INFO("%s %d\n", __func__, __LINE__);
|
||||
break;
|
||||
case ESP_GATTS_READ_EVT: {
|
||||
LOG_INFO("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
|
||||
ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
|
||||
esp_gatt_rsp_t rsp;
|
||||
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
|
||||
rsp.attr_value.handle = param->read.handle;
|
||||
@ -257,8 +249,8 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
break;
|
||||
}
|
||||
case ESP_GATTS_WRITE_EVT: {
|
||||
LOG_INFO("GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
|
||||
LOG_INFO("GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
|
||||
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, conn_id %d, trans_id %d, handle %d\n", param->write.conn_id, param->write.trans_id, param->write.handle);
|
||||
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value %08x\n", param->write.len, *(uint32_t *)param->write.value);
|
||||
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
|
||||
break;
|
||||
}
|
||||
@ -268,7 +260,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
case ESP_GATTS_UNREG_EVT:
|
||||
break;
|
||||
case ESP_GATTS_CREATE_EVT:
|
||||
LOG_INFO("CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
|
||||
ESP_LOGI(GATTS_TAG, "CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle);
|
||||
gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_B;
|
||||
@ -282,7 +274,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
case ESP_GATTS_ADD_INCL_SRVC_EVT:
|
||||
break;
|
||||
case ESP_GATTS_ADD_CHAR_EVT:
|
||||
LOG_INFO("ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
ESP_LOGI(GATTS_TAG, "ADD_CHAR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
|
||||
|
||||
gl_profile_tab[PROFILE_A_APP_ID].char_handle = param->add_char.attr_handle;
|
||||
@ -292,19 +284,19 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
|
||||
break;
|
||||
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
|
||||
LOG_INFO("ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
ESP_LOGI(GATTS_TAG, "ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n",
|
||||
param->add_char.status, param->add_char.attr_handle, param->add_char.service_handle);
|
||||
break;
|
||||
case ESP_GATTS_DELETE_EVT:
|
||||
break;
|
||||
case ESP_GATTS_START_EVT:
|
||||
LOG_INFO("SERVICE_START_EVT, status %d, service_handle %d\n",
|
||||
ESP_LOGI(GATTS_TAG, "SERVICE_START_EVT, status %d, service_handle %d\n",
|
||||
param->start.status, param->start.service_handle);
|
||||
break;
|
||||
case ESP_GATTS_STOP_EVT:
|
||||
break;
|
||||
case ESP_GATTS_CONNECT_EVT:
|
||||
LOG_INFO("SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
|
||||
ESP_LOGI(GATTS_TAG, "SERVICE_START_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
|
||||
param->connect.conn_id,
|
||||
param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
|
||||
param->connect.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5],
|
||||
@ -324,14 +316,12 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
|
||||
|
||||
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
|
||||
{
|
||||
LOG_INFO("EVT %d, gatts if %d\n", event, gatts_if);
|
||||
|
||||
/* If event is register event, store the gatts_if for each profile */
|
||||
if (event == ESP_GATTS_REG_EVT) {
|
||||
if (param->reg.status == ESP_GATT_OK) {
|
||||
gl_profile_tab[param->reg.app_id].gatts_if = gatts_if;
|
||||
} else {
|
||||
LOG_INFO("Reg app failed, app_id %04x, status %d\n",
|
||||
ESP_LOGI(GATTS_TAG, "Reg app failed, app_id %04x, status %d\n",
|
||||
param->reg.app_id,
|
||||
param->reg.status);
|
||||
return;
|
||||
@ -357,16 +347,16 @@ void app_main()
|
||||
{
|
||||
esp_err_t ret;
|
||||
|
||||
bt_controller_init();
|
||||
LOG_INFO("%s init bluetooth\n", __func__);
|
||||
ret = esp_init_bluetooth();
|
||||
esp_bt_controller_init();
|
||||
|
||||
ret = esp_bluedroid_init();
|
||||
if (ret) {
|
||||
LOG_ERROR("%s init bluetooth failed\n", __func__);
|
||||
ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__);
|
||||
return;
|
||||
}
|
||||
ret = esp_enable_bluetooth();
|
||||
ret = esp_bluedroid_enable();
|
||||
if (ret) {
|
||||
LOG_ERROR("%s enable bluetooth failed\n", __func__);
|
||||
ESP_LOGE(GATTS_TAG, "%s enable bluetooth failed\n", __func__);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -37,6 +37,8 @@
|
||||
#include "esp_gatt_defs.h"
|
||||
#include "esp_bt_main.h"
|
||||
|
||||
#define GATTC_TAG "GATTC_DEMO"
|
||||
|
||||
///Declare static functions
|
||||
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
||||
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
|
||||
@ -108,16 +110,16 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
|
||||
switch (event) {
|
||||
case ESP_GATTC_REG_EVT:
|
||||
LOG_INFO("REG_EVT\n");
|
||||
ESP_LOGI(GATTC_TAG, "REG_EVT\n");
|
||||
esp_ble_gap_set_scan_params(&ble_scan_params);
|
||||
break;
|
||||
case ESP_GATTC_OPEN_EVT:
|
||||
conn_id = p_data->open.conn_id;
|
||||
|
||||
memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
|
||||
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
|
||||
ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
|
||||
|
||||
LOG_INFO("REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
ESP_LOGI(GATTC_TAG, "REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[1],
|
||||
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[3],
|
||||
gl_profile_tab[PROFILE_A_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_A_APP_ID].remote_bda[5]
|
||||
@ -128,37 +130,37 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
case ESP_GATTC_SEARCH_RES_EVT: {
|
||||
esp_gatt_srvc_id_t *srvc_id = &p_data->search_res.srvc_id;
|
||||
conn_id = p_data->search_res.conn_id;
|
||||
LOG_INFO("SEARCH RES: conn_id = %x\n", conn_id);
|
||||
ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x\n", conn_id);
|
||||
if (srvc_id->id.uuid.len == ESP_UUID_LEN_16) {
|
||||
LOG_INFO("UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
|
||||
ESP_LOGI(GATTC_TAG, "UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
|
||||
LOG_INFO("UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
|
||||
ESP_LOGI(GATTC_TAG, "UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
|
||||
LOG_INFO("UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", srvc_id->id.uuid.uuid.uuid128[0],
|
||||
ESP_LOGI(GATTC_TAG, "UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", srvc_id->id.uuid.uuid.uuid128[0],
|
||||
srvc_id->id.uuid.uuid.uuid128[1], srvc_id->id.uuid.uuid.uuid128[2], srvc_id->id.uuid.uuid.uuid128[3],
|
||||
srvc_id->id.uuid.uuid.uuid128[4], srvc_id->id.uuid.uuid.uuid128[5], srvc_id->id.uuid.uuid.uuid128[6],
|
||||
srvc_id->id.uuid.uuid.uuid128[7], srvc_id->id.uuid.uuid.uuid128[8], srvc_id->id.uuid.uuid.uuid128[9],
|
||||
srvc_id->id.uuid.uuid.uuid128[10], srvc_id->id.uuid.uuid.uuid128[11], srvc_id->id.uuid.uuid.uuid128[12],
|
||||
srvc_id->id.uuid.uuid.uuid128[13], srvc_id->id.uuid.uuid.uuid128[14], srvc_id->id.uuid.uuid.uuid128[15]);
|
||||
} else {
|
||||
LOG_ERROR("UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
|
||||
ESP_LOGE(GATTC_TAG, "UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ESP_GATTC_SEARCH_CMPL_EVT:
|
||||
conn_id = p_data->search_cmpl.conn_id;
|
||||
LOG_INFO("SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
|
||||
ESP_LOGI(GATTC_TAG, "SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
|
||||
esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, NULL);
|
||||
break;
|
||||
case ESP_GATTC_GET_CHAR_EVT:
|
||||
if (p_data->get_char.status != ESP_GATT_OK) {
|
||||
break;
|
||||
}
|
||||
LOG_INFO("GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
|
||||
LOG_INFO("GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
|
||||
ESP_LOGI(GATTC_TAG, "GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
|
||||
ESP_LOGI(GATTC_TAG, "GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
|
||||
|
||||
if (p_data->get_char.char_id.uuid.uuid.uuid16 == 0x2a46) {
|
||||
LOG_INFO("register notify\n");
|
||||
ESP_LOGI(GATTC_TAG, "register notify\n");
|
||||
esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, &alert_service_id, &p_data->get_char.char_id);
|
||||
}
|
||||
|
||||
@ -166,8 +168,8 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
break;
|
||||
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
|
||||
uint16_t notify_en = 1;
|
||||
LOG_INFO("REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
|
||||
LOG_INFO("REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
|
||||
ESP_LOGI(GATTC_TAG, "REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
|
||||
ESP_LOGI(GATTC_TAG, "REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
|
||||
|
||||
esp_ble_gattc_write_char_descr(
|
||||
gattc_if,
|
||||
@ -182,10 +184,10 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
break;
|
||||
}
|
||||
case ESP_GATTC_NOTIFY_EVT:
|
||||
LOG_INFO("NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
|
||||
ESP_LOGI(GATTC_TAG, "NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
|
||||
break;
|
||||
case ESP_GATTC_WRITE_DESCR_EVT:
|
||||
LOG_INFO("WRITE: status %d\n", p_data->write.status);
|
||||
ESP_LOGI(GATTC_TAG, "WRITE: status %d\n", p_data->write.status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -199,16 +201,15 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
|
||||
switch (event) {
|
||||
case ESP_GATTC_REG_EVT:
|
||||
LOG_INFO("REG_EVT\n");
|
||||
//esp_ble_gap_set_scan_params(&ble_scan_params);
|
||||
ESP_LOGI(GATTC_TAG, "REG_EVT\n");
|
||||
break;
|
||||
case ESP_GATTC_OPEN_EVT:
|
||||
conn_id = p_data->open.conn_id;
|
||||
|
||||
memcpy(gl_profile_tab[PROFILE_B_APP_ID].remote_bda, p_data->open.remote_bda, sizeof(esp_bd_addr_t));
|
||||
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
|
||||
ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d, mtu %d\n", conn_id, gattc_if, p_data->open.status, p_data->open.mtu);
|
||||
|
||||
LOG_INFO("REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
ESP_LOGI(GATTC_TAG, "REMOTE BDA %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[0], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[1],
|
||||
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[2], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[3],
|
||||
gl_profile_tab[PROFILE_B_APP_ID].remote_bda[4], gl_profile_tab[PROFILE_B_APP_ID].remote_bda[5]
|
||||
@ -219,37 +220,37 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
case ESP_GATTC_SEARCH_RES_EVT: {
|
||||
esp_gatt_srvc_id_t *srvc_id = &p_data->search_res.srvc_id;
|
||||
conn_id = p_data->search_res.conn_id;
|
||||
LOG_INFO("SEARCH RES: conn_id = %x\n", conn_id);
|
||||
ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x\n", conn_id);
|
||||
if (srvc_id->id.uuid.len == ESP_UUID_LEN_16) {
|
||||
LOG_INFO("UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
|
||||
ESP_LOGI(GATTC_TAG, "UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
|
||||
LOG_INFO("UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
|
||||
ESP_LOGI(GATTC_TAG, "UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
|
||||
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
|
||||
LOG_INFO("UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", srvc_id->id.uuid.uuid.uuid128[0],
|
||||
ESP_LOGI(GATTC_TAG, "UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n", srvc_id->id.uuid.uuid.uuid128[0],
|
||||
srvc_id->id.uuid.uuid.uuid128[1], srvc_id->id.uuid.uuid.uuid128[2], srvc_id->id.uuid.uuid.uuid128[3],
|
||||
srvc_id->id.uuid.uuid.uuid128[4], srvc_id->id.uuid.uuid.uuid128[5], srvc_id->id.uuid.uuid.uuid128[6],
|
||||
srvc_id->id.uuid.uuid.uuid128[7], srvc_id->id.uuid.uuid.uuid128[8], srvc_id->id.uuid.uuid.uuid128[9],
|
||||
srvc_id->id.uuid.uuid.uuid128[10], srvc_id->id.uuid.uuid.uuid128[11], srvc_id->id.uuid.uuid.uuid128[12],
|
||||
srvc_id->id.uuid.uuid.uuid128[13], srvc_id->id.uuid.uuid.uuid128[14], srvc_id->id.uuid.uuid.uuid128[15]);
|
||||
} else {
|
||||
LOG_ERROR("UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
|
||||
ESP_LOGE(GATTC_TAG, "UNKNOWN LEN %d\n", srvc_id->id.uuid.len);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ESP_GATTC_SEARCH_CMPL_EVT:
|
||||
conn_id = p_data->search_cmpl.conn_id;
|
||||
LOG_INFO("SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
|
||||
ESP_LOGI(GATTC_TAG, "SEARCH_CMPL: conn_id = %x, status %d\n", conn_id, p_data->search_cmpl.status);
|
||||
esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, NULL);
|
||||
break;
|
||||
case ESP_GATTC_GET_CHAR_EVT:
|
||||
if (p_data->get_char.status != ESP_GATT_OK) {
|
||||
break;
|
||||
}
|
||||
LOG_INFO("GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
|
||||
LOG_INFO("GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
|
||||
ESP_LOGI(GATTC_TAG, "GET CHAR: conn_id = %x, status %d\n", p_data->get_char.conn_id, p_data->get_char.status);
|
||||
ESP_LOGI(GATTC_TAG, "GET CHAR: srvc_id = %04x, char_id = %04x\n", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
|
||||
|
||||
if (p_data->get_char.char_id.uuid.uuid.uuid16 == 0x2a46) {
|
||||
LOG_INFO("register notify\n");
|
||||
ESP_LOGI(GATTC_TAG, "register notify\n");
|
||||
esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_B_APP_ID].remote_bda, &alert_service_id, &p_data->get_char.char_id);
|
||||
}
|
||||
|
||||
@ -257,8 +258,8 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
break;
|
||||
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
|
||||
uint16_t notify_en = 1;
|
||||
LOG_INFO("REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
|
||||
LOG_INFO("REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
|
||||
ESP_LOGI(GATTC_TAG, "REG FOR NOTIFY: status %d\n", p_data->reg_for_notify.status);
|
||||
ESP_LOGI(GATTC_TAG, "REG FOR_NOTIFY: srvc_id = %04x, char_id = %04x\n", p_data->reg_for_notify.srvc_id.id.uuid.uuid.uuid16, p_data->reg_for_notify.char_id.uuid.uuid.uuid16);
|
||||
|
||||
esp_ble_gattc_write_char_descr(
|
||||
gattc_if,
|
||||
@ -273,10 +274,10 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
|
||||
break;
|
||||
}
|
||||
case ESP_GATTC_NOTIFY_EVT:
|
||||
LOG_INFO("NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
|
||||
ESP_LOGI(GATTC_TAG, "NOTIFY: len %d, value %08x\n", p_data->notify.value_len, *(uint32_t *)p_data->notify.value);
|
||||
break;
|
||||
case ESP_GATTC_WRITE_DESCR_EVT:
|
||||
LOG_INFO("WRITE: status %d\n", p_data->write.status);
|
||||
ESP_LOGI(GATTC_TAG, "WRITE: status %d\n", p_data->write.status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -299,27 +300,22 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
|
||||
switch (scan_result->scan_rst.search_evt) {
|
||||
case ESP_GAP_SEARCH_INQ_RES_EVT:
|
||||
for (int i = 0; i < 6; i++) {
|
||||
LOG_INFO("%x:", scan_result->scan_rst.bda[i]);
|
||||
ESP_LOGI(GATTC_TAG, "%x:", scan_result->scan_rst.bda[i]);
|
||||
}
|
||||
LOG_INFO("\n");
|
||||
ESP_LOGI(GATTC_TAG, "\n");
|
||||
adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,
|
||||
ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
|
||||
LOG_INFO("adv_name_len=%x\n", adv_name_len);
|
||||
ESP_LOGI(GATTC_TAG, "Searched Device Name Len %d\n", adv_name_len);
|
||||
for (int j = 0; j < adv_name_len; j++) {
|
||||
LOG_INFO("%c", adv_name[j]);
|
||||
ESP_LOGI(GATTC_TAG, "%c", adv_name[j]);
|
||||
}
|
||||
LOG_INFO("\n");
|
||||
for (int j = 0; j < adv_name_len; j++) {
|
||||
LOG_INFO("%c", device_name[j]);
|
||||
}
|
||||
LOG_INFO("\n");
|
||||
|
||||
if (adv_name != NULL) {
|
||||
if (strcmp((char *)adv_name, device_name) == 0) {
|
||||
LOG_INFO("the name equal to Heart Rate\n");
|
||||
ESP_LOGI(GATTC_TAG, "Searched device %s\n", device_name);
|
||||
if (connect == false) {
|
||||
connect = true;
|
||||
LOG_INFO("Connect to the remote device.\n");
|
||||
ESP_LOGI(GATTC_TAG, "Connect to the remote device.\n");
|
||||
esp_ble_gap_stop_scanning();
|
||||
esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, true);
|
||||
esp_ble_gattc_open(gl_profile_tab[PROFILE_B_APP_ID].gattc_if, scan_result->scan_rst.bda, true);
|
||||
@ -341,14 +337,14 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
|
||||
|
||||
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
|
||||
{
|
||||
LOG_INFO("EVT %d, gattc if %d\n", event, gattc_if);
|
||||
ESP_LOGI(GATTC_TAG, "EVT %d, gattc if %d\n", event, gattc_if);
|
||||
|
||||
/* If event is register event, store the gattc_if for each profile */
|
||||
if (event == ESP_GATTC_REG_EVT) {
|
||||
if (param->reg.status == ESP_GATT_OK) {
|
||||
gl_profile_tab[param->reg.app_id].gattc_if = gattc_if;
|
||||
} else {
|
||||
LOG_INFO("Reg app failed, app_id %04x, status %d\n",
|
||||
ESP_LOGI(GATTC_TAG, "Reg app failed, app_id %04x, status %d\n",
|
||||
param->reg.app_id,
|
||||
param->reg.status);
|
||||
return;
|
||||
@ -374,17 +370,17 @@ void ble_client_appRegister(void)
|
||||
{
|
||||
esp_err_t status;
|
||||
|
||||
LOG_INFO("register callback\n");
|
||||
ESP_LOGI(GATTC_TAG, "register callback\n");
|
||||
|
||||
//register the scan callback function to the gap moudule
|
||||
if ((status = esp_ble_gap_register_callback(esp_gap_cb)) != ESP_OK) {
|
||||
LOG_ERROR("gap register error, error code = %x\n", status);
|
||||
ESP_LOGE(GATTC_TAG, "gap register error, error code = %x\n", status);
|
||||
return;
|
||||
}
|
||||
|
||||
//register the callback function to the gattc module
|
||||
if ((status = esp_ble_gattc_register_callback(esp_gattc_cb)) != ESP_OK) {
|
||||
LOG_ERROR("gattc register error, error code = %x\n", status);
|
||||
ESP_LOGE(GATTC_TAG, "gattc register error, error code = %x\n", status);
|
||||
return;
|
||||
}
|
||||
esp_ble_gattc_app_register(PROFILE_A_APP_ID);
|
||||
@ -393,14 +389,14 @@ void ble_client_appRegister(void)
|
||||
|
||||
void gattc_client_test(void)
|
||||
{
|
||||
esp_init_bluetooth();
|
||||
esp_enable_bluetooth();
|
||||
esp_bluedroid_init();
|
||||
esp_bluedroid_enable();
|
||||
ble_client_appRegister();
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
bt_controller_init();
|
||||
esp_bt_controller_init();
|
||||
gattc_client_test();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user