example: fix ble hid device demo include

This commit is contained in:
Tian Hao 2018-04-26 20:02:14 +08:00
parent adada3f67e
commit b9855df44e
5 changed files with 35 additions and 43 deletions

View File

@ -24,7 +24,6 @@
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "bt_trace.h"
#include "driver/gpio.h"
#include "hid_dev.h"
@ -45,7 +44,7 @@
*
*/
#define GATTS_TAG "HID_DEMO"
#define HID_DEMO_TAG "HID_DEMO"
#define GPIO_OUTPUT_IO_0 18
@ -110,7 +109,7 @@ void gpio_task_example(void* arg)
uint32_t io_num;
for(;;) {
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
ESP_LOGI(GATTS_TAG, "GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
ESP_LOGI(HID_DEMO_TAG, "GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
if(i == 0) {
++i;
}
@ -191,13 +190,13 @@ static void hidd_event_callback(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *
}
case ESP_HIDD_EVENT_BLE_DISCONNECT: {
sec_conn = false;
LOG_ERROR("%s(), ESP_HIDD_EVENT_BLE_DISCONNECT", __func__);
ESP_LOGE(HID_DEMO_TAG, "%s(), ESP_HIDD_EVENT_BLE_DISCONNECT", __func__);
esp_ble_gap_start_advertising(&hidd_adv_params);
break;
}
case ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT: {
LOG_ERROR("%s, ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT", __func__);
ESP_LOG_BUFFER_HEX(GATTS_TAG, param->vendor_write.data, param->vendor_write.length);
ESP_LOGE(HID_DEMO_TAG, "%s, ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT", __func__);
ESP_LOG_BUFFER_HEX(HID_DEMO_TAG, param->vendor_write.data, param->vendor_write.length);
}
default:
break;
@ -213,13 +212,13 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
break;
case ESP_GAP_BLE_SEC_REQ_EVT:
for(int i = 0; i < ESP_BD_ADDR_LEN; i++) {
LOG_DEBUG("%x:",param->ble_security.ble_req.bd_addr[i]);
ESP_LOGD(HID_DEMO_TAG, "%x:",param->ble_security.ble_req.bd_addr[i]);
}
esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
break;
case ESP_GAP_BLE_AUTH_CMPL_EVT:
sec_conn = true;
LOG_ERROR("staus =%s, ESP_GAP_BLE_AUTH_CMPL_EVT",param->ble_security.auth_cmpl.success ? "success" : "fail");
ESP_LOGE(HID_DEMO_TAG, "staus =%s, ESP_GAP_BLE_AUTH_CMPL_EVT",param->ble_security.auth_cmpl.success ? "success" : "fail");
break;
default:
break;
@ -232,7 +231,7 @@ void hid_demo_task(void *pvParameters)
while(1) {
vTaskDelay(2000 / portTICK_PERIOD_MS);
if (sec_conn) {
LOG_ERROR("Send the volume");
ESP_LOGE(HID_DEMO_TAG, "Send the volume");
send_volum_up = true;
//uint8_t key_vaule = {HID_KEY_A};
//esp_hidd_send_keyboard_value(hid_conn_id, 0, &key_vaule, 1);
@ -263,30 +262,30 @@ void app_main()
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s initialize controller failed\n", __func__);
ESP_LOGE(HID_DEMO_TAG, "%s initialize controller failed\n", __func__);
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s enable controller failed\n", __func__);
ESP_LOGE(HID_DEMO_TAG, "%s enable controller failed\n", __func__);
return;
}
ret = esp_bluedroid_init();
if (ret) {
LOG_ERROR("%s init bluedroid failed\n", __func__);
ESP_LOGE(HID_DEMO_TAG, "%s init bluedroid failed\n", __func__);
return;
}
ret = esp_bluedroid_enable();
if (ret) {
LOG_ERROR("%s init bluedroid failed\n", __func__);
ESP_LOGE(HID_DEMO_TAG, "%s init bluedroid failed\n", __func__);
return;
}
if((ret = esp_hidd_profile_init()) != ESP_OK) {
LOG_ERROR("%s init bluedroid failed\n", __func__);
ESP_LOGE(HID_DEMO_TAG, "%s init bluedroid failed\n", __func__);
}
///register the callback function to the gap module

View File

@ -17,7 +17,7 @@
#include "hid_dev.h"
#include <stdlib.h>
#include <string.h>
#include "bt_trace.h"
#include "esp_log.h"
// HID keyboard input report length
#define HID_KEYBOARD_IN_RPT_LEN 8
@ -31,7 +31,6 @@
// HID consumer control input report length
#define HID_CC_IN_RPT_LEN 2
esp_err_t esp_hidd_register_callbacks(esp_hidd_event_cb_t callbacks)
{
esp_err_t hidd_status;
@ -58,7 +57,7 @@ esp_err_t esp_hidd_register_callbacks(esp_hidd_event_cb_t callbacks)
esp_err_t esp_hidd_profile_init(void)
{
if (hidd_le_env.enabled) {
LOG_ERROR("HID device profile already initialized");
ESP_LOGE(HID_LE_PRF_TAG, "HID device profile already initialized");
return ESP_FAIL;
}
// Reset the hid device target environment
@ -71,7 +70,7 @@ esp_err_t esp_hidd_profile_deinit(void)
{
uint16_t hidd_svc_hdl = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_SVC];
if (!hidd_le_env.enabled) {
LOG_ERROR("HID device profile already initialized");
ESP_LOGE(HID_LE_PRF_TAG, "HID device profile already initialized");
return ESP_OK;
}
@ -97,10 +96,10 @@ void esp_hidd_send_consumer_value(uint16_t conn_id, uint8_t key_cmd, bool key_pr
{
uint8_t buffer[HID_CC_IN_RPT_LEN] = {0, 0};
if (key_pressed) {
LOG_DEBUG("hid_consumer_build_report");
ESP_LOGD(HID_LE_PRF_TAG, "hid_consumer_build_report");
hid_consumer_build_report(buffer, key_cmd);
}
LOG_DEBUG("buffer[0] = %x, buffer[1] = %x", buffer[0], buffer[1]);
ESP_LOGD(HID_LE_PRF_TAG, "buffer[0] = %x, buffer[1] = %x", buffer[0], buffer[1]);
hid_dev_send_report(hidd_le_env.gatt_if, conn_id,
HID_RPT_ID_CC_IN, HID_REPORT_TYPE_INPUT, HID_CC_IN_RPT_LEN, buffer);
return;
@ -109,7 +108,7 @@ void esp_hidd_send_consumer_value(uint16_t conn_id, uint8_t key_cmd, bool key_pr
void esp_hidd_send_keyboard_value(uint16_t conn_id, key_mask_t special_key_mask, uint8_t *keyboard_cmd, uint8_t num_key)
{
if (num_key > HID_KEYBOARD_IN_RPT_LEN - 2) {
LOG_ERROR("%s(), the number key should not be more than %d", __func__, HID_KEYBOARD_IN_RPT_LEN);
ESP_LOGE(HID_LE_PRF_TAG, "%s(), the number key should not be more than %d", __func__, HID_KEYBOARD_IN_RPT_LEN);
return;
}
@ -121,7 +120,7 @@ void esp_hidd_send_keyboard_value(uint16_t conn_id, key_mask_t special_key_mask,
buffer[i+2] = keyboard_cmd[i];
}
LOG_DEBUG("the key vaule = %d,%d,%d, %d, %d, %d,%d, %d", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7]);
ESP_LOGD(HID_LE_PRF_TAG, "the key vaule = %d,%d,%d, %d, %d, %d,%d, %d", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7]);
hid_dev_send_report(hidd_le_env.gatt_if, conn_id,
HID_RPT_ID_KEY_IN, HID_REPORT_TYPE_INPUT, HID_KEYBOARD_IN_RPT_LEN, buffer);
return;

View File

@ -13,16 +13,11 @@
// limitations under the License.
#include "hid_dev.h"
#include "bt_trace.h"
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "esp_log.h"
static hid_report_map_t *hid_dev_rpt_tbl;
static uint8_t hid_dev_rpt_tbl_Len;
@ -55,7 +50,7 @@ void hid_dev_send_report(esp_gatt_if_t gatts_if, uint16_t conn_id,
// get att handle for report
if ((p_rpt = hid_dev_rpt_by_id(id, type)) != NULL) {
// if notifications are enabled
LOG_DEBUG("%s(), send the report, handle = %d", __func__, p_rpt->handle);
ESP_LOGD(HID_LE_PRF_TAG, "%s(), send the report, handle = %d", __func__, p_rpt->handle);
esp_ble_gatts_send_indicate(gatts_if, conn_id, p_rpt->handle, length, data, false);
}
@ -65,7 +60,7 @@ void hid_dev_send_report(esp_gatt_if_t gatts_if, uint16_t conn_id,
void hid_consumer_build_report(uint8_t *buffer, consumer_cmd_t cmd)
{
if (!buffer) {
LOG_ERROR("%s(), the buffer is NULL, hid build report failed.", __func__);
ESP_LOGE(HID_LE_PRF_TAG, "%s(), the buffer is NULL, hid build report failed.", __func__);
return;
}

View File

@ -14,10 +14,7 @@
#include "hidd_le_prf_int.h"
#include <string.h>
#include "bt_types.h"
#include "bt_trace.h"
//#include "esp_gap_ble_api.h"
#include "esp_log.h"
/// characteristic presentation information
struct prf_char_pres_fmt
@ -558,7 +555,7 @@ void esp_hidd_prf_cb_hdl(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
break;
case ESP_GATTS_CONNECT_EVT: {
esp_hidd_cb_param_t cb_param = {0};
LOG_ERROR("the connection establish, conn_id = %x",param->connect.conn_id);
ESP_LOGE(HID_LE_PRF_TAG, "the connection establish, conn_id = %x",param->connect.conn_id);
memcpy(cb_param.connect.remote_bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
cb_param.connect.conn_id = param->connect.conn_id;
hidd_clcb_alloc(param->connect.conn_id, param->connect.remote_bda);
@ -595,7 +592,7 @@ void esp_hidd_prf_cb_hdl(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
param->add_attr_tab.status == ESP_GATT_OK) {
incl_svc.start_hdl = param->add_attr_tab.handles[BAS_IDX_SVC];
incl_svc.end_hdl = incl_svc.start_hdl + BAS_IDX_NB -1;
LOG_ERROR("%s(), start added the hid service to the stack database. incl_handle = %d",
ESP_LOGE(HID_LE_PRF_TAG, "%s(), start added the hid service to the stack database. incl_handle = %d",
__func__, incl_svc.start_hdl);
esp_ble_gatts_create_attr_tab(hidd_le_gatt_db, gatts_if, HIDD_LE_IDX_NB, 0);
}
@ -603,7 +600,7 @@ void esp_hidd_prf_cb_hdl(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
param->add_attr_tab.status == ESP_GATT_OK) {
memcpy(hidd_le_env.hidd_inst.att_tbl, param->add_attr_tab.handles,
HIDD_LE_IDX_NB*sizeof(uint16_t));
LOG_ERROR("hid svc handle = %x",hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_SVC]);
ESP_LOGE(HID_LE_PRF_TAG, "hid svc handle = %x",hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_SVC]);
hid_add_id_tbl();
esp_ble_gatts_start_service(hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_SVC]);
} else {
@ -639,9 +636,9 @@ void hidd_clcb_alloc (uint16_t conn_id, esp_bd_addr_t bda)
for (i_clcb = 0, p_clcb= hidd_le_env.hidd_clcb; i_clcb < HID_MAX_APPS; i_clcb++, p_clcb++) {
if (!p_clcb->in_use) {
p_clcb->in_use = TRUE;
p_clcb->in_use = true;
p_clcb->conn_id = conn_id;
p_clcb->connected = TRUE;
p_clcb->connected = true;
memcpy (p_clcb->remote_bda, bda, ESP_BD_ADDR_LEN);
break;
}
@ -656,10 +653,10 @@ bool hidd_clcb_dealloc (uint16_t conn_id)
for (i_clcb = 0, p_clcb= hidd_le_env.hidd_clcb; i_clcb < HID_MAX_APPS; i_clcb++, p_clcb++) {
memset(p_clcb, 0, sizeof(hidd_clcb_t));
return TRUE;
return true;
}
return FALSE;
return false;
}
esp_err_t hidd_register_cb(void)
@ -676,7 +673,7 @@ void hidd_set_attr_value(uint16_t handle, uint16_t val_len, const uint8_t *value
hidd_inst->att_tbl[HIDD_LE_IDX_REPORT_REP_REF] >= handle) {
esp_ble_gatts_set_attr_value(handle, val_len, value);
} else {
LOG_ERROR("%s error:Invalid handle value.",__func__);
ESP_LOGE(HID_LE_PRF_TAG, "%s error:Invalid handle value.",__func__);
}
return;
}
@ -688,7 +685,7 @@ void hidd_get_attr_value(uint16_t handle, uint16_t *length, uint8_t **value)
hidd_inst->att_tbl[HIDD_LE_IDX_REPORT_REP_REF] >= handle){
esp_ble_gatts_get_attr_value(handle, length, (const uint8_t **)value);
} else {
LOG_ERROR("%s error:Invalid handle value.", __func__);
ESP_LOGE(HID_LE_PRF_TAG, "%s error:Invalid handle value.", __func__);
}
return;

View File

@ -22,6 +22,8 @@
#include "esp_gap_ble_api.h"
#include "hid_dev.h"
//HID BLE profile log tag
#define HID_LE_PRF_TAG "HID_LE_PRF"
/// Maximal number of HIDS that can be added in the DB
#ifndef USE_ONE_HIDS_INSTANCE