component/bt : clean up the demos

1. cleanup gatt client demo
2. cleanup gatt server demo
3. cleanup blufi demo
4. use correct log level
pull/111/merge
Tian Hao 2016-11-22 20:32:45 +08:00
rodzic 190b6d629b
commit db007fbb2e
20 zmienionych plików z 233 dodań i 1011 usunięć

Wyświetl plik

@ -203,7 +203,7 @@ esp_err_t esp_ble_gap_set_device_name(char *name)
** Returns pointer of ADV data
**
*******************************************************************************/
uint8_t *esp_ble_resolve_adv_data( uint8_t *p_adv, uint8_t type, uint8_t *p_length )
uint8_t *esp_ble_resolve_adv_data( uint8_t *p_adv, uint8_t type, uint8_t *p_length)
{
if (((type < ESP_BLE_AD_TYPE_FLAG) || (type > ESP_BLE_AD_TYPE_128SERVICE_DATA)) &&
(type != ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE))

Wyświetl plik

@ -3377,7 +3377,7 @@ void bta_dm_acl_change(tBTA_DM_MSG *p_data)
/* both local and remote devices support SSR */
bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_USE_SSR;
}
APPL_TRACE_WARNING("%s info: 0x%x", __func__, bta_dm_cb.device_list.peer_device[i].info);
APPL_TRACE_DEBUG("%s info: 0x%x", __func__, bta_dm_cb.device_list.peer_device[i].info);
if (bta_dm_cb.p_sec_cback)
bta_dm_cb.p_sec_cback(BTA_DM_LINK_UP_EVT, (tBTA_DM_SEC *)&conn);

Wyświetl plik

@ -601,7 +601,7 @@ static void bta_gattc_explore_srvc(UINT16 conn_id, tBTA_GATTC_SERV *p_srvc_cb)
}
}
/* no service found at all, the end of server discovery*/
LOG_WARN("%s no more services found", __func__);
LOG_DEBUG("%s no more services found", __func__);
#if (defined BTA_GATT_DEBUG && BTA_GATT_DEBUG == TRUE)
bta_gattc_display_cache_server(p_srvc_cb->p_srvc_cache);

Wyświetl plik

@ -54,7 +54,6 @@ static void btc_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_GATTC;
msg.act = (uint8_t) event;
LOG_ERROR("the gattc event = %x\n",event);
ret = btc_transfer_context(&msg, p_data, sizeof(tBTA_GATTC), btc_gattc_copy_req_data);
if (ret)

Wyświetl plik

@ -404,7 +404,7 @@ void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OC
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");
LOG_WARN("bta_dm_co_ble_load_local_keys: func not ported\n");
#endif
}

Wyświetl plik

@ -268,7 +268,7 @@ inline void trc_dump_buffer(uint8_t *prefix, uint8_t *data, uint16_t len)
#define LOG_LEVEL_DEBUG 4
#define LOG_LEVEL_VERBOSE 5
#ifndef LOG_LEVEL
#define LOG_LEVEL LOG_LEVEL_ERROR
#define LOG_LEVEL LOG_LEVEL_INFO
#endif
#define LOG_ERROR(fmt, args...) do {if (LOG_LEVEL >= LOG_LEVEL_ERROR) printf(fmt,## args);} while(0)
#define LOG_WARN(fmt, args...) do {if (LOG_LEVEL >= LOG_LEVEL_WARN) BT_PRINTF(fmt,## args);} while(0)

Wyświetl plik

@ -236,7 +236,7 @@ BOOLEAN BTM_SecRegister(tBTM_APPL_INFO *p_cb_info)
BTM_TRACE_EVENT("%s application registered\n", __func__);
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
LOG_INFO("%s p_cb_info->p_le_callback == 0x%p\n", __func__, p_cb_info->p_le_callback);
LOG_DEBUG("%s p_cb_info->p_le_callback == 0x%p\n", __func__, p_cb_info->p_le_callback);
if (p_cb_info->p_le_callback)
{
#if SMP_INCLUDED == TRUE
@ -257,7 +257,7 @@ BOOLEAN BTM_SecRegister(tBTM_APPL_INFO *p_cb_info)
btm_cb.api = *p_cb_info;
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
LOG_INFO("%s btm_cb.api.p_le_callback = 0x%p\n", __func__, btm_cb.api.p_le_callback);
LOG_DEBUG("%s btm_cb.api.p_le_callback = 0x%p\n", __func__, btm_cb.api.p_le_callback);
#endif
BTM_TRACE_EVENT("%s application registered\n", __func__);
return(TRUE);

Wyświetl plik

@ -247,7 +247,7 @@ static void smp_br_connect_callback(UINT16 channel, BD_ADDR bd_addr, BOOLEAN con
if (transport != BT_TRANSPORT_BR_EDR)
{
SMP_TRACE_WARNING("%s is called on unexpected transport %d",
SMP_TRACE_WARNING("%s is called on unexpected transport %d\n",
__func__, transport);
return;
}
@ -255,7 +255,7 @@ static void smp_br_connect_callback(UINT16 channel, BD_ADDR bd_addr, BOOLEAN con
if (!(memcmp(bd_addr, p_cb->pairing_bda, BD_ADDR_LEN) == 0))
return;
SMP_TRACE_EVENT ("%s for pairing BDA: %08x%04x Event: %s",
SMP_TRACE_EVENT ("%s for pairing BDA: %08x%04x Event: %s\n",
__func__,
(bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8) + bd_addr[3],
(bd_addr[4]<<8)+bd_addr[5],

Wyświetl plik

@ -3,7 +3,7 @@
# project subdirectory.
#
PROJECT_NAME := bluedroid_demos
PROJECT_NAME := blufi_demo
COMPONENT_ADD_INCLUDEDIRS := components/include

Wyświetl plik

@ -3,7 +3,7 @@
# project subdirectory.
#
PROJECT_NAME := bluedroid_demos
PROJECT_NAME := gatt_server_demos
COMPONENT_ADD_INCLUDEDIRS := components/include

Wyświetl plik

@ -1,5 +1,5 @@
ESP-IDF GATT SERVER demo
=======================
========================
This is the demo for user to use ESP_APIs to create a GATT Server.

Wyświetl plik

@ -108,7 +108,7 @@ static void gatts_event_handler(uint32_t event, void *param)
switch (event) {
case ESP_GATTS_REG_EVT:
LOG_ERROR("REGISTER_APP_EVT, status %d, gatt_if %d, app_id %d\n", p->reg.status, p->reg.gatt_if, p->reg.app_id);
LOG_INFO("REGISTER_APP_EVT, status %d, gatt_if %d, app_id %d\n", p->reg.status, p->reg.gatt_if, p->reg.app_id);
gl_test.gatt_if = p->reg.gatt_if;
gl_test.service_id.is_primary = true;
gl_test.service_id.id.inst_id = 0x00;
@ -121,7 +121,7 @@ static void gatts_event_handler(uint32_t event, void *param)
esp_ble_gatts_create_service(gl_test.gatt_if, &gl_test.service_id, GATTS_NUM_HANDLE_TEST);
break;
case ESP_GATTS_READ_EVT: {
LOG_ERROR("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", p->read.conn_id, p->read.trans_id, p->read.handle);
LOG_INFO("GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", p->read.conn_id, p->read.trans_id, p->read.handle);
esp_gatt_rsp_t rsp;
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
rsp.attr_value.handle = p->read.handle;
@ -141,7 +141,7 @@ static void gatts_event_handler(uint32_t event, void *param)
case ESP_GATTS_UNREG_EVT:
break;
case ESP_GATTS_CREATE_EVT:
LOG_ERROR("CREATE_SERVICE_EVT, status %d, gatt_if %d, service_handle %d\n", p->create.status, p->create.gatt_if, p->create.service_handle);
LOG_INFO("CREATE_SERVICE_EVT, status %d, gatt_if %d, service_handle %d\n", p->create.status, p->create.gatt_if, p->create.service_handle);
gl_test.service_handle = p->create.service_handle;
gl_test.char_uuid.len = ESP_UUID_LEN_16;
gl_test.char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST;
@ -155,7 +155,7 @@ static void gatts_event_handler(uint32_t event, void *param)
case ESP_GATTS_ADD_INCL_SRVC_EVT:
break;
case ESP_GATTS_ADD_CHAR_EVT:
LOG_ERROR("ADD_CHAR_EVT, status %d, gatt_if %d, attr_handle %d, service_handle %d\n",
LOG_INFO("ADD_CHAR_EVT, status %d, gatt_if %d, attr_handle %d, service_handle %d\n",
p->add_char.status, p->add_char.gatt_if, p->add_char.attr_handle, p->add_char.service_handle);
gl_test.char_handle = p->add_char.attr_handle;
@ -165,19 +165,19 @@ static void gatts_event_handler(uint32_t event, void *param)
ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE);
break;
case ESP_GATTS_ADD_CHAR_DESCR_EVT:
LOG_ERROR("ADD_DESCR_EVT, status %d, gatt_if %d, attr_handle %d, service_handle %d\n",
LOG_INFO("ADD_DESCR_EVT, status %d, gatt_if %d, attr_handle %d, service_handle %d\n",
p->add_char.status, p->add_char.gatt_if, p->add_char.attr_handle, p->add_char.service_handle);
break;
case ESP_GATTS_DELELTE_EVT:
break;
case ESP_GATTS_START_EVT:
LOG_ERROR("SERVICE_START_EVT, status %d, gatt_if %d, service_handle %d\n",
LOG_INFO("SERVICE_START_EVT, status %d, gatt_if %d, service_handle %d\n",
p->start.status, p->start.gatt_if, p->start.service_handle);
break;
case ESP_GATTS_STOP_EVT:
break;
case ESP_GATTS_CONNECT_EVT:
LOG_ERROR("SERVICE_START_EVT, conn_id %d, gatt_if %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
LOG_INFO("SERVICE_START_EVT, conn_id %d, gatt_if %d, remote %02x:%02x:%02x:%02x:%02x:%02x:, is_conn %d\n",
p->connect.conn_id, p->connect.gatt_if,
p->connect.remote_bda[0], p->connect.remote_bda[1], p->connect.remote_bda[2],
p->connect.remote_bda[3], p->connect.remote_bda[4], p->connect.remote_bda[5],
@ -200,7 +200,7 @@ void app_main()
esp_err_t ret;
bt_controller_init();
LOG_ERROR("%s init bluetooth\n", __func__);
LOG_INFO("%s init bluetooth\n", __func__);
ret = esp_init_bluetooth();
if (ret) {
LOG_ERROR("%s init bluetooth failed\n", __func__);
@ -215,7 +215,6 @@ void app_main()
esp_ble_gatts_register_callback(gatts_event_handler);
esp_ble_gap_register_callback(gap_event_handler);
esp_ble_gatts_app_register(GATTS_SERVICE_UUID_TEST);
//esp_ble_gatts_app_register(0x18);
return;
}

Wyświetl plik

@ -3,7 +3,7 @@
# project subdirectory.
#
PROJECT_NAME := bluedroid_demos
PROJECT_NAME := gatt_client_demo
COMPONENT_ADD_INCLUDEDIRS := components/include

Wyświetl plik

@ -1,10 +1,5 @@
ESP-IDF Blufi demo
=======================
ESP-IDF GATT CLIENT demo
========================
This is the demo for bluetooth config wifi connection to ap.
attentions:
1. Please use the BLEDEMO APK
2. As the GATTServer start a litte slowly, so Please Wait for a period, then use apk scan the apk util find a random address devices named Espressif_008
3. Just a unstable version..
This is the demo for user to use ESP_APIs to create a GATT Client.

Wyświetl plik

@ -1,418 +0,0 @@
// 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.
#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 "prf_defs.h"
#include "hash_map.h"
#include "hash_functions.h"
#include "alarm.h"
#include "thread.h"
#include "bt_app_common.h"
//#include "gattc_profile.h"
#include "smp_int.h"
#include "smp_api.h"
extern void gattc_client_test(void);
static fixed_queue_t *bta_app_msg_queue;
fixed_queue_t *bt_app_general_alarm_queue;
hash_map_t *bt_app_general_alarm_hash_map;
pthread_mutex_t bt_app_general_alarm_lock;
static const size_t BT_APP_GENERAL_ALARM_HASH_MAP_SIZE = 10;
xQueueHandle xBtaApp1Queue;
xTaskHandle xBtaApp1TaskHandle;
#define BT_APP_TTYPE_MAIN_ENTRY (1)
static TIMER_LIST_ENT main_boot_tle;
tSMP_CB smp_cmd;
static void bt_app_context_switched(void *p_msg);
static void bt_app_send_msg(void *p_msg);
static void bt_app_task_handler(void *arg);
static void bta_app_msg_ready(fixed_queue_t *queue);
static void bt_app_task_shut_down(void);
static void bt_app_general_alarm_ready(fixed_queue_t *queue);
static void bt_app_general_alarm_process(TIMER_LIST_ENT *p_tle);
void bt_app_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_app_task_handler(void *arg)
{
BtTaskEvt_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_app_msg_queue);
fixed_queue_process(bt_app_general_alarm_queue);
}
#if (BUT_PROFILE_CFG)
// 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);
//}
#endif ///BUT_PROFILE_CFG
}
}
}
static void bt_app_task_post(void)
{
BtTaskEvt_t evt;
evt.sig = 0xff;
evt.par = 0;
if (xQueueSend(xBtaApp1Queue, &evt, 10/portTICK_RATE_MS) != pdTRUE) {
ets_printf("btdm_post failed\n");
}
}
static void bta_app_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_app_msg_ready, evt: %d\n", p_msg->event);
switch (p_msg->event) {
case BT_EVT_APP_CONTEXT_SWITCH:
bt_app_context_switched(p_msg);
break;
default:
LOG_ERROR("unhandled BT_APP event (%d)\n", p_msg->event & BT_EVT_MASK);
break;
}
GKI_freebuf(p_msg);
}
}
static void bt_app_context_switched(void *p_msg)
{
tBTAPP_CONTEXT_SWITCH_CBACK *p = (tBTAPP_CONTEXT_SWITCH_CBACK *) p_msg;
if (p->p_cb)
p->p_cb(p->event, p->p_param);
}
static void bt_app_send_msg(void *p_msg)
{
if (bta_app_msg_queue) {
fixed_queue_enqueue(bta_app_msg_queue, p_msg);
//ke_event_set(KE_EVENT_BT_APP_TASK);
bt_app_task_post();
}
}
bt_status_t bt_app_transfer_context (tBTAPP_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTAPP_COPY_CBACK *p_copy_cback)
{
tBTAPP_CONTEXT_SWITCH_CBACK *p_msg;
LOG_ERROR("btapp_transfer_context evt %d, len %d", event, param_len);
/* allocate and send message that will be executed in btif context */
if ((p_msg = (tBTAPP_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTAPP_CONTEXT_SWITCH_CBACK) + param_len)) != NULL)
{
p_msg->hdr.event = BT_EVT_APP_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_app_send_msg(p_msg);
return BT_STATUS_SUCCESS;
}
else
{
/* let caller deal with a failed allocation */
return BT_STATUS_NOMEM;
}
}
void bt_app_task_start_up(void)
{
bta_app_msg_queue = fixed_queue_new(SIZE_MAX);
if (bta_app_msg_queue == NULL)
goto error_exit;
//ke_event_callback_set(KE_EVENT_BT_APP_TASK, &bt_app_task_handler);
xBtaApp1Queue = xQueueCreate(3, sizeof(BtTaskEvt_t));
xTaskCreate(bt_app_task_handler, "BtaApp1T", 8192, NULL, configMAX_PRIORITIES - 3, xBtaApp1TaskHandle);
fixed_queue_register_dequeue(bta_app_msg_queue, bta_app_msg_ready);
bt_app_general_alarm_hash_map = hash_map_new(BT_APP_GENERAL_ALARM_HASH_MAP_SIZE,
hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
if (bt_app_general_alarm_hash_map == NULL)
goto error_exit;
pthread_mutex_init(&bt_app_general_alarm_lock, NULL);
bt_app_general_alarm_queue = fixed_queue_new(SIZE_MAX);
if (bt_app_general_alarm_queue == NULL)
goto error_exit;
fixed_queue_register_dequeue(bt_app_general_alarm_queue, bt_app_general_alarm_ready);
memset(&main_boot_tle, 0, sizeof(TIMER_LIST_ENT));
return;
error_exit:
LOG_ERROR("%s Unable to allocate resources for bt_app\n", __func__);
bt_app_task_shut_down();
}
static void bt_app_task_shut_down(void)
{
fixed_queue_unregister_dequeue(bta_app_msg_queue);
fixed_queue_free(bta_app_msg_queue, NULL);
bta_app_msg_queue = NULL;
// todo: hash map, pthread_mutex...
fixed_queue_unregister_dequeue(bt_app_general_alarm_queue);
vTaskDelete(xBtaApp1TaskHandle);
vQueueDelete(xBtaApp1Queue);
}
static void bt_app_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_app_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_app_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_BLE_GENERAL_DISCOVERABLE;
tBTA_DM_CONN conn_mode = BTA_DM_BLE_CONNECTABLE;
//BTA_DmSetVisibility(disc_mode, conn_mode, (UINT8)BTA_DM_NON_PAIRABLE, (UINT8)BTA_DM_CONN_ALL);
#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_cb.local_io_capability = 0x03; //no input no output
smp_cb.loc_oob_flag = 0x00; //oob data not present
smp_cb.loc_auth_req = 0x01;
smp_cb.loc_enc_size = 0x10;
smp_cb.local_i_key = 0x01;
smp_cb.local_r_key = 0x01; //1101
//memcpy(smp_cb.pairing_bda,p_data->ble_req.bd_addr,0x06);
smp_sm_event(&smp_cb,SMP_PAIRING_REQ_EVT,NULL);
//smp_send_cmd(SMP_OPCODE_PAIRING_RSP,&smp_cb);
//smp_generate_srand_mrand_confirm(&smp_cb,NULL);
//smp_set_state(SMP_STATE_PAIR_REQ_RSP,SMP_BR_PAIRING_REQ_EVT);
//BTA_DmConfirm(p_data->ble_req.bd_addr,true);
break;
case BTA_DM_BLE_KEY_EVT:
if(p_data->ble_key.key_type == BTM_LE_KEY_PENC)
{
smp_set_state(SMP_STATE_IDLE);
}
break;
default:
break;
}
bt_app_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_app_transfer_context(bt_app_dm_upstreams_evt, (uint16_t)event,
(void *)p_data, sizeof(tBTA_DM_SEC), bt_app_dm_data_copy);
}
void bt_app_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param)
{
//BTA_EnableBluetooth(bte_dm_evt);
vTaskDelay(1000 / portTICK_PERIOD_MS);
bt_app_start_timer(&main_boot_tle, BT_APP_TTYPE_MAIN_ENTRY, 8);
}
/* Alarm timer */
static void bt_app_general_alarm_cb(void *data) {
assert(data != NULL);
TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
fixed_queue_enqueue(bt_app_general_alarm_queue, p_tle);
//ke_event_set(KE_EVENT_BT_APP_TASK);
bt_app_task_post();
}
void bt_app_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_app_general_alarm_lock);
if (!hash_map_has_key(bt_app_general_alarm_hash_map, p_tle)) {
alarm = osi_alarm_new("bt_app", bt_app_general_alarm_cb, (void *)p_tle, 0);
hash_map_set(bt_app_general_alarm_hash_map, p_tle, alarm);
}
pthread_mutex_unlock(&bt_app_general_alarm_lock);
pthread_mutex_lock(&bt_app_general_alarm_lock);
alarm = hash_map_get(bt_app_general_alarm_hash_map, p_tle);
pthread_mutex_unlock(&bt_app_general_alarm_lock);
if (alarm == NULL) {
LOG_ERROR("%s Unable to create alarm\n", __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_app_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_app_general_alarm_hash_map, p_tle);
if (alarm == NULL) {
LOG_WARN("%s Unable to find expected alarm in hashmap\n", __func__);
return;
}
osi_alarm_cancel(alarm);
}
static void bt_app_general_alarm_process(TIMER_LIST_ENT *p_tle)
{
assert(p_tle != NULL);
LOG_ERROR("general_alarm_process\n");
switch (p_tle->event) {
case BT_APP_TTYPE_MAIN_ENTRY:
LOG_ERROR("BT_APP main boot**********\n");
// ble_test_conn();
// ble_server_test();
// bt_test_start_inquiry();
/*set connectable,discoverable, pairable and paired only modes of local device*/
// tBTA_DM_DISC disc_mode = BTA_DM_BLE_GENERAL_DISCOVERABLE;
// tBTA_DM_CONN conn_mode = BTA_DM_BLE_CONNECTABLE;
// BTA_DmSetVisibility(disc_mode, conn_mode, (UINT8)BTA_DM_NON_PAIRABLE, (UINT8)BTA_DM_CONN_ALL);
//gatts_server_test();
gattc_client_test();
break;
}
}
static void bt_app_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_app_general_alarm_process(p_tle);
}
}
void bt_app_core_start(void) {
bt_app_transfer_context(bt_app_init_ok, 0, NULL, 0, NULL);
}

Wyświetl plik

@ -1,472 +0,0 @@
// 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.
/****************************************************************************
*
* 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 "controller.h"
#include "bt_trace.h"
#include "bt_types.h"
#include "btm_api.h"
#include "bta_api.h"
#include "bta_gatt_api.h"
#include "esp_gap_ble_api.h"
#include "esp_gattc_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.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]
esp_gatt_if_t client_if;
esp_gatt_status_t status = ESP_GATT_ERROR;
BOOLEAN connet = FALSE;
BD_ADDR obj_addr;
uint16_t simpleClient_id = 0xEE;
char device_name[] = "Heart Rate";
static unsigned char BASE_UUID[16] = {
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static esp_ble_scan_params_t ble_scan_params =
{
.scan_type = BLE_SCAN_TYPE_ACTIVE,
.own_addr_type = ESP_PUBLIC_ADDR,
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
.scan_interval = 0x50,
.scan_window = 0x30
};
static void esp_scan_result_cb(uint32_t event, void *scan_param);
static void esp_gattc_result_cb(uint32_t event, void *gattc_param);
int arch_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 = arch_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;
}
}
static void esp_scan_result_cb(uint32_t event, void *param)
{
uint8_t *adv_name = NULL;
uint8_t adv_name_len = 0;
switch(event)
{
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT:
{
//the unit of the duration is second
uint32_t duration = 10;
esp_ble_gap_start_scanning(duration);
break;
}
case ESP_GAP_BLE_SCAN_RESULT_EVT:
{
esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
switch(scan_result->scan_rst.search_evt)
{
case ESP_GAP_SEARCH_INQ_RES_EVT:
for (int i = 0; i < 6; i++)
{
LOG_ERROR("%x:", scan_result->scan_rst.bda[i]);
}
LOG_ERROR("\n");
adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,
ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
LOG_ERROR("adv_name_len=%x\n",adv_name_len);
for(int j = 0; j < adv_name_len; j++)
{
LOG_ERROR("%c",adv_name[j]);
}
LOG_ERROR("\n");
for(int j = 0; j < adv_name_len; j++)
{
LOG_ERROR("%c",device_name[j]);
}
LOG_ERROR("\n");
if (adv_name != NULL)
{
if(strcmp(adv_name, device_name) == 0)
{
LOG_ERROR("the name eque to Heart Rate.\n");
if (status == ESP_GATT_OK && connet == FALSE)
{
connet = TRUE;
LOG_ERROR("Connet to the remote device.\n");
esp_ble_gap_stop_scanning();
esp_ble_gattc_open(client_if, scan_result->scan_rst.bda, TRUE);
}
}
}
break;
case ESP_GAP_SEARCH_INQ_CMPL_EVT:
break;
default:
break;
// LOG_ERROR("ESP_GAP_SEARCH_DISC_BLE_RES_EVT\n");
// break;
}
break;
}
//case :
// break;
default:
break;
}
}
static void esp_gattc_result_cb(uint32_t event, void *gattc_param)
{
uint16_t conidx = 0;
esp_ble_gattc_cb_param_t *gattc_data = (esp_ble_gattc_cb_param_t *)gattc_param;
LOG_ERROR("esp_gattc_result_cb, event = %x\n", event);
switch (event)
{
case ESP_GATTC_REG_EVT:
status = gattc_data->reg.status;
client_if = gattc_data->reg.gatt_if;
LOG_ERROR("status = %x, client_if = %x\n", status, client_if);
break;
case ESP_GATTC_OPEN_EVT:
conidx = gattc_data->open.conn_id;
LOG_ERROR("conidx = %x, if = %x\n",conidx, gattc_data->open.gatt_if);
esp_ble_gattc_search_service(conidx, NULL);
LOG_ERROR("ESP_GATTC_OPEN_EVT status %d\n", gattc_data->open.status);
break;
case ESP_GATTC_SEARCH_RES_EVT: {
esp_gatt_srvc_id_t *srvc_id = &gattc_data->search_res.service_id;
conidx = gattc_data->open.conn_id;
LOG_ERROR("SEARCH RES: conidx = %x\n", conidx);
if (srvc_id->id.uuid.len == ESP_UUID_LEN_16) {
LOG_ERROR("UUID16: %x\n", srvc_id->id.uuid.uuid.uuid16);
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
LOG_ERROR("UUID32: %x\n", srvc_id->id.uuid.uuid.uuid32);
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_128) {
LOG_ERROR("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);
}
break;
}
case ESP_GATTC_SEARCH_CMPL_EVT:
conidx = gattc_data->search_cmpl.conn_id;
LOG_ERROR("SEARCH_CMPL: conidx = %x, status %d\n",conidx, gattc_data->search_cmpl.status);
break;
default:
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);
}
*/
/************************************************************************************
* * Function bta_scan_recult_callback
* *
* * Description scan result.it will be called when device scaned a peer device
* *
* * Return NULL
**************************************************************************************/
#if 0
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[] = "SimpleBLEClient";
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);
}
}
#endif ///if 0
/************************************************************************************
* * 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);
BTA_DmBleObserve(true, 8, bta_scan_result_callback);
}*/
/************************************************************************************
* * Function bta_gattc_callback
* *
* * Description app register callback
* *
* * Return NULL
**************************************************************************************/
#if 0
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;
}
break;
default:
LOG_ERROR("%s:unknown event: %d\n", __FUNCTION__, event);
}
}
#endif
/************************************************************************************
* * 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);
esp_err_t status;
LOG_ERROR("register application\n");
//BTA_GATTC_AppRegister(&t_uuid, bta_gattc_callback);
//register the scan callback function to the gap moudule
if((status = esp_ble_gap_register_callback(esp_scan_result_cb)) == ESP_OK){
esp_ble_gap_set_scan_params(&ble_scan_params);
}else{
LOG_ERROR("gap register error, error code = %x\n",status);
}
//register the callback function to the gattc module
if ((status = esp_ble_gattc_register_callback(esp_gattc_result_cb)) != ESP_OK){
LOG_ERROR("gattc register error, error code = %x\n",status);
}else{
esp_ble_gattc_app_register(simpleClient_id);
}
}
void gattc_client_test(void)
{
BTM_SetTraceLevel(BT_TRACE_LEVEL_DEBUG);
esp_init_bluetooth();
esp_enable_bluetooth();
ble_client_appRegister();
}

Wyświetl plik

@ -1,17 +0,0 @@
#
# Main Makefile. This is basically the same as a component makefile.
#
# This Makefile should, at the very least, just include $(SDK_PATH)/make/component_common.mk. By default,
# this will take the sources in the src/ directory, compile them and link them into
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#
COMPONENT_SRCDIRS := \
app_core \
app_project \
CFLAGS += -Wno-error=unused-label -Wno-error=return-type -Wno-error=missing-braces -Wno-error=pointer-sign -Wno-error=parentheses -I./include
include $(IDF_PATH)/make/component_common.mk

Wyświetl plik

@ -1,30 +0,0 @@
#ifndef __BT_APP_COMMON_H__
#define __BT_APP_COMMON_H__
#include <stdint.h>
#include "osi.h"
#include "bt_common_types.h"
#include "bt_defs.h"
/* BT APP Events */
#define BT_EVT_APP (0xB000)
#define BT_EVT_APP_CONTEXT_SWITCH (0x0001 | BT_EVT_APP)
typedef void (tBTAPP_CBACK) (uint16_t event, char *p_param);
typedef void (tBTAPP_COPY_CBACK) (uint16_t event, char *p_dest, char *p_src);
typedef struct
{
BT_HDR hdr;
tBTAPP_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 */
} tBTAPP_CONTEXT_SWITCH_CBACK;
bt_status_t bt_app_transfer_context (tBTAPP_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTAPP_COPY_CBACK *p_copy_cback);
void bt_app_task_start_up(void);
#endif /* __BT_APP_COMMON_H__ */

Wyświetl plik

@ -1,43 +0,0 @@
// 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.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "bt.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "string.h"
//#include "bt_app_common.h"
extern void bte_main_boot_entry(void *);
extern void bt_app_task_start_up(void);
extern void bt_app_core_start(void);
void pingTask(void *pvParameters)
{
while (1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
//printf("ping\n");
}
}
void app_main()
{
bt_controller_init();
xTaskCreatePinnedToCore(&pingTask, "pingTask", 2048, NULL, 5, NULL, 0);
bt_app_task_start_up();
bte_main_boot_entry(bt_app_core_start);
}

Wyświetl plik

@ -0,0 +1,209 @@
// 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.
/****************************************************************************
*
* 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 "controller.h"
#include "bt.h"
#include "bt_trace.h"
#include "bt_types.h"
#include "btm_api.h"
#include "bta_api.h"
#include "bta_gatt_api.h"
#include "esp_gap_ble_api.h"
#include "esp_gattc_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.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]
esp_gatt_if_t client_if;
esp_gatt_status_t status = ESP_GATT_ERROR;
bool connet = false;
uint16_t simpleClient_id = 0xEE;
const char device_name[] = "Heart Rate";
static esp_ble_scan_params_t ble_scan_params =
{
.scan_type = BLE_SCAN_TYPE_ACTIVE,
.own_addr_type = ESP_PUBLIC_ADDR,
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
.scan_interval = 0x50,
.scan_window = 0x30
};
static void esp_gap_cb(uint32_t event, void *param);
static void esp_gattc_cb(uint32_t event, void *param);
static void esp_gap_cb(uint32_t event, void *param)
{
uint8_t *adv_name = NULL;
uint8_t adv_name_len = 0;
switch(event)
{
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT:
{
//the unit of the duration is second
uint32_t duration = 10;
esp_ble_gap_start_scanning(duration);
break;
}
case ESP_GAP_BLE_SCAN_RESULT_EVT:
{
esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
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]);
}
LOG_INFO("\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);
for(int j = 0; j < adv_name_len; j++)
{
LOG_INFO("%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 eque to Heart Rate.\n");
if (status == ESP_GATT_OK && connet == false)
{
connet = true;
LOG_INFO("Connet to the remote device.\n");
esp_ble_gap_stop_scanning();
esp_ble_gattc_open(client_if, scan_result->scan_rst.bda, true);
}
}
}
break;
case ESP_GAP_SEARCH_INQ_CMPL_EVT:
break;
default:
break;
}
break;
}
default:
break;
}
}
static void esp_gattc_cb(uint32_t event, void *param)
{
uint16_t conn_id = 0;
esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param;
LOG_INFO("esp_gattc_cb, event = %x\n", event);
switch (event)
{
case ESP_GATTC_REG_EVT:
status = p_data->reg.status;
client_if = p_data->reg.gatt_if;
LOG_INFO("status = %x, client_if = %x\n", status, client_if);
break;
case ESP_GATTC_OPEN_EVT:
conn_id = p_data->open.conn_id;
LOG_INFO("ESP_GATTC_OPEN_EVT conn_id %d, if %d, status %d\n", conn_id, p_data->open.gatt_if, p_data->open.status);
esp_ble_gattc_search_service(conn_id, NULL);
break;
case ESP_GATTC_SEARCH_RES_EVT: {
esp_gatt_srvc_id_t *srvc_id = &p_data->search_res.service_id;
conn_id = p_data->open.conn_id;
LOG_INFO("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);
} else if (srvc_id->id.uuid.len == ESP_UUID_LEN_32) {
LOG_INFO("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],
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);
}
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);
break;
default:
break;
}
}
void ble_client_appRegister(void)
{
LOG_INFO("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);
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);
return;
}
esp_ble_gattc_app_register(simpleClient_id);
esp_ble_gap_set_scan_params(&ble_scan_params);
}
void gattc_client_test(void)
{
esp_init_bluetooth();
esp_enable_bluetooth();
ble_client_appRegister();
}
void app_main()
{
bt_controller_init();
gattc_client_test();
}