diff --git a/components/bt/bluedroid/api/esp_gap_ble_api.c b/components/bt/bluedroid/api/esp_gap_ble_api.c index 37e4f295c..72b3e1c26 100644 --- a/components/bt/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/bluedroid/api/esp_gap_ble_api.c @@ -70,12 +70,24 @@ esp_err_t esp_ble_gap_start_scanning(uint32_t duration) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GAP_BLE; - msg.act = BTC_GAP_BLE_ACT_SET_SCAN_PARAM; + msg.act = BTC_GAP_BLE_ACT_START_SCAN; arg.duration = duration; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } + +esp_err_t esp_ble_gap_stop_scanning(void) +{ + btc_msg_t msg; + btc_ble_gap_args_t arg; + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GAP_BLE; + msg.act = BTC_GAP_BLE_ACT_STOP_SCAN; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + esp_err_t esp_ble_gap_start_advertising(esp_ble_adv_params_t *adv_params) { btc_msg_t msg; @@ -173,3 +185,35 @@ esp_err_t esp_ble_gap_set_device_name(char *name) return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } + +/******************************************************************************* +** +** Function esp_ble_resolve_adv_data +** +** Description This function is called to get ADV data for a specific type. +** +** Parameters p_adv - pointer of ADV data +** type - finding ADV data type +** p_length - return the length of ADV data not including type +** +** Returns pointer of ADV data +** +*******************************************************************************/ +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)) + { + LOG_ERROR("the eir type not define, type = %x\n", type); + return NULL; + } + + if (p_adv == NULL) + { + LOG_ERROR("Invalid p_eir data.\n"); + return NULL; + } + + return (BTM_CheckAdvData( p_adv, type, p_length)); +} + diff --git a/components/bt/bluedroid/api/include/esp_gap_ble_api.h b/components/bt/bluedroid/api/include/esp_gap_ble_api.h index 7a4f8413f..af3469574 100644 --- a/components/bt/bluedroid/api/include/esp_gap_ble_api.h +++ b/components/bt/bluedroid/api/include/esp_gap_ble_api.h @@ -12,6 +12,36 @@ #define ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT 2 #define ESP_GAP_BLE_SCAN_RESULT_EVT 3 +#define ESP_BLE_ADV_DATA_LEN_MAX 31 + +/****************** define the adv type macro***************************************/ +#define ESP_BLE_AD_TYPE_FLAG 0x01 +#define ESP_BLE_AD_TYPE_16SRV_PART 0x02 +#define ESP_BLE_AD_TYPE_16SRV_CMPL 0x03 +#define ESP_BLE_AD_TYPE_32SRV_PART 0x04 +#define ESP_BLE_AD_TYPE_32SRV_CMPL 0x05 +#define ESP_BLE_AD_TYPE_128SRV_PART 0x06 +#define ESP_BLE_AD_TYPE_128SRV_CMPL 0x07 +#define ESP_BLE_AD_TYPE_NAME_SHORT 0x08 +#define ESP_BLE_AD_TYPE_NAME_CMPL 0x09 +#define ESP_BLE_AD_TYPE_TX_PWR 0x0A +#define ESP_BLE_AD_TYPE_DEV_CLASS 0x0D +#define ESP_BLE_AD_TYPE_SM_TK 0x10 +#define ESP_BLE_AD_TYPE_SM_OOB_FLAG 0x11 +#define ESP_BLE_AD_TYPE_INT_RANGE 0x12 +#define ESP_BLE_AD_TYPE_SOL_SRV_UUID 0x14 +#define ESP_BLE_AD_TYPE_128SOL_SRV_UUID 0x15 +#define ESP_BLE_AD_TYPE_SERVICE_DATA 0x16 +#define ESP_BLE_AD_TYPE_PUBLIC_TARGET 0x17 +#define ESP_BLE_AD_TYPE_RANDOM_TARGET 0x18 +#define ESP_BLE_AD_TYPE_APPEARANCE 0x19 +#define ESP_BLE_AD_TYPE_ADV_INT 0x1A +#define ESP_BLE_AD_TYPE_32SOL_SRV_UUID 0x1B +#define ESP_BLE_AD_TYPE_32SERVICE_DATA 0x1C +#define ESP_BLE_AD_TYPE_128SERVICE_DATA 0x1D +#define ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE 0xFF + + typedef uint32_t esp_gap_ble_event_t; /// Advertising mode @@ -70,6 +100,24 @@ typedef struct { uint8_t flag; } esp_ble_adv_data_t; +/// Own BD address source of the device +typedef enum +{ + /// Public Address + ESP_PUBLIC_ADDR, + /// Provided random address + ESP_PROVIDED_RND_ADDR, + /// Provided static random address + ESP_GEN_STATIC_RND_ADDR, + /// Generated resolvable private random address + ESP_GEN_RSLV_ADDR, + /// Generated non-resolvable private random address + ESP_GEN_NON_RSLV_ADDR, + /// Provided Reconnection address + ESP_PROVIDED_RECON_ADDR, +}esp_ble_own_addr_src_t; + + typedef enum { BLE_SCAN_TYPE_PASSIVE = 0x0, BLE_SCAN_TYPE_ACTIVE = 0x1, @@ -132,6 +180,7 @@ typedef union { esp_bt_dev_type_t dev_type; esp_ble_addr_type_t ble_addr_type; int rssi; + uint8_t ble_adv[ESP_BLE_ADV_DATA_LEN_MAX]; /* received EIR */ int flag; int num_resps; } scan_rst; @@ -199,6 +248,16 @@ esp_err_t esp_ble_gap_set_scan_params(esp_ble_scan_params_t *scan_params); esp_err_t esp_ble_gap_start_scanning(uint32_t duration); +/******************************************************************************* +** +** @function esp_ble_gap_stop_scanning +** +** @brief This function call to stop the device scanning the peer device whith advertising on the air +** @param void +** @return ESP_OK - success, other - failed +** +*******************************************************************************/ +esp_err_t esp_ble_gap_stop_scanning(void); /******************************************************************************* ** @@ -300,4 +359,20 @@ esp_err_t esp_ble_gap_config_local_privacy (bool privacy_enable); *******************************************************************************/ esp_err_t esp_ble_gap_set_device_name(char *name); + +/******************************************************************************* +** +** @function esp_ble_resolve_adv_data +** +** @brief This function is called to get ADV data for a specific type. +** +** @param[in] p_adv - pointer of ADV data whitch to be resolved +** @param[in] type - finding ADV data type +** @param[out] p_length - return the length of ADV data not including type +** +** @return pointer of ADV data +** +*******************************************************************************/ +uint8_t *esp_ble_resolve_adv_data( uint8_t *p_adv, uint8_t type, uint8_t *p_length ); + #endif /* __ESP_GAP_BLE_API_H__ */ diff --git a/components/bt/bluedroid/api/include/esp_gatt_defs.h b/components/bt/bluedroid/api/include/esp_gatt_defs.h index 1e9d5a783..216285599 100644 --- a/components/bt/bluedroid/api/include/esp_gatt_defs.h +++ b/components/bt/bluedroid/api/include/esp_gatt_defs.h @@ -115,6 +115,7 @@ typedef enum { } esp_gatt_char_prop_t; #define ESP_GATT_MAX_ATTR_LEN 600 //as same as GATT_MAX_ATTR_LEN + typedef struct { uint8_t value[ESP_GATT_MAX_ATTR_LEN]; uint16_t handle; diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c index c7546404e..6c131197a 100755 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c @@ -191,10 +191,10 @@ void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data) tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES; - APPL_TRACE_DEBUG("bta_gattc_register state %d",p_cb->state); + APPL_TRACE_DEBUG("bta_gattc_register state %d\n",p_cb->state); memset(&cb_data, 0, sizeof(cb_data)); cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES; - + /* check if GATTC module is already enabled . Else enable */ if (p_cb->state == BTA_GATTC_STATE_DISABLED) { @@ -207,7 +207,7 @@ void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data) { if ((p_app_uuid == NULL) || (p_cb->cl_rcb[i].client_if = GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0) { - APPL_TRACE_ERROR("Register with GATT stack failed."); + APPL_TRACE_ERROR("Register with GATT stack failed.\n"); status = BTA_GATT_ERROR; } else @@ -223,7 +223,7 @@ void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data) { p_buf->hdr.event = BTA_GATTC_INT_START_IF_EVT; p_buf->client_if = p_cb->cl_rcb[i].client_if; - + APPL_TRACE_DEBUG("GATTC getbuf sucess.\n"); bta_sys_sendmsg(p_buf); status = BTA_GATT_OK; } @@ -243,8 +243,7 @@ void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data) if (p_data->api_reg.p_cback) { if (p_app_uuid != NULL) - memcpy(&(cb_data.reg_oper.app_uuid),p_app_uuid,sizeof(tBT_UUID)); - + memcpy(&(cb_data.reg_oper.app_uuid),p_app_uuid,sizeof(tBT_UUID)); cb_data.reg_oper.status = status; (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT, (tBTA_GATTC *)&cb_data); } diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/bluedroid/btc/core/btc_task.c index ea00a8ec4..90d8c398f 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/bluedroid/btc/core/btc_task.c @@ -24,6 +24,8 @@ #include "btc_gattc.h" #include "btc_gap_ble.h" #include "btc_blufi_prf.h" +#include "bta_gatt_api.h" + static xTaskHandle xBtcTaskHandle = NULL; static xQueueHandle xBtcQueue = 0; diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c index d9539182d..d6c88cec4 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c @@ -14,6 +14,7 @@ #include +#include "bt_types.h" #include "bta_api.h" #include "btc_task.h" #include "btc_manage.h" @@ -428,7 +429,6 @@ void btc_ble_start_advertising (esp_ble_adv_params_t *ble_adv_params) ble_adv_params->adv_filter_policy, &peer_addr); - /*set connectable,discoverable, pairable and paired only modes of local device*/ BTA_DmSetVisibility(disc_mode, conn_mode, (UINT8)BTA_DM_NON_PAIRABLE, (UINT8)BTA_DM_CONN_ALL); } @@ -474,12 +474,11 @@ static void btc_search_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data { esp_ble_gap_cb_param_t param; btc_msg_t msg; - uint8_t len; - + msg.sig = BTC_SIG_API_CB; msg.pid = BTC_PID_GAP_BLE; msg.act = ESP_GAP_BLE_SCAN_RESULT_EVT; - + param.scan_rst.search_evt = event; switch (event) { case BTA_DM_INQ_RES_EVT: { @@ -488,15 +487,32 @@ static void btc_search_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data param.scan_rst.rssi = p_data->inq_res.rssi; param.scan_rst.ble_addr_type = p_data->inq_res.ble_addr_type; param.scan_rst.flag = p_data->inq_res.flag; + memcpy(param.scan_rst.ble_adv, p_data->inq_res.p_eir, + ESP_BLE_ADV_DATA_LEN_MAX); break; } case BTA_DM_INQ_CMPL_EVT: { param.scan_rst.num_resps = p_data->inq_cmpl.num_resps; - LOG_ERROR("%s BLE observe complete. Num Resp %d", __FUNCTION__,p_data->inq_cmpl.num_resps); + LOG_ERROR("%s BLE observe complete. Num Resp %d\n", __FUNCTION__,p_data->inq_cmpl.num_resps); break; } + case BTA_DM_DISC_RES_EVT: + LOG_ERROR("BTA_DM_DISC_RES_EVT\n"); + break; + case BTA_DM_DISC_BLE_RES_EVT: + LOG_ERROR("BTA_DM_DISC_BLE_RES_EVT\n"); + break; + case BTA_DM_DISC_CMPL_EVT: + LOG_ERROR("BTA_DM_DISC_CMPL_EVT\n"); + break; + case BTA_DM_DI_DISC_CMPL_EVT: + LOG_ERROR("BTA_DM_DI_DISC_CMPL_EVT\n"); + break; + case BTA_DM_SEARCH_CANCEL_CMPL_EVT: + LOG_ERROR("BTA_DM_SEARCH_CANCEL_CMPL_EVT\n"); + break; default: - LOG_ERROR("%s : Unknown event 0x%x", __FUNCTION__, event); + LOG_ERROR("%s : Unknown event 0x%x\n", __FUNCTION__, event); return; } btc_transfer_context(&msg, ¶m, sizeof(esp_ble_gap_cb_param_t), NULL); @@ -514,6 +530,12 @@ static void btc_ble_start_scanning(uint8_t duration, tBTA_DM_SEARCH_CBACK *resul } } +static void btc_ble_stop_scanning(void) +{ + uint8_t duration = 0; + BTA_DmBleObserve(false, duration, NULL); +} + static void btc_ble_stop_advertising(void) { @@ -663,6 +685,7 @@ void btc_gap_ble_call_handler(btc_msg_t *msg) btc_ble_start_scanning(arg->duration, btc_search_callback); break; case BTC_GAP_BLE_ACT_STOP_SCAN: + btc_ble_stop_scanning(); break; case BTC_GAP_BLE_ACT_START_ADV: btc_ble_start_advertising(&arg->adv_params); diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c index c42659d6e..674416f84 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -54,8 +54,8 @@ 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; - - ret = btc_transfer_context(&msg, &p_data, sizeof(tBTA_GATTC), btc_gattc_copy_req_data); + 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) LOG_ERROR("%s transfer failed\n", __func__); @@ -425,7 +425,7 @@ void btc_gattc_call_handler(btc_msg_t *msg) btc_gattc_unreg_for_notify(arg); break; default: - LOG_ERROR("%s: Unhandled event (%d)!", __FUNCTION__, msg->act); + LOG_ERROR("%s: Unhandled event (%d)!\n", __FUNCTION__, msg->act); break; } @@ -436,7 +436,7 @@ void btc_gattc_cb_handler(btc_msg_t *msg) tBTA_GATTC *arg = (tBTA_GATTC *)(msg->arg); esp_ble_gattc_cb_param_t param; - memset(¶m, 0, sizeof(esp_ble_gattc_cb_param_t)); + memset(¶m, 0, sizeof(esp_ble_gattc_cb_param_t)); switch (msg->act) { case BTA_GATTC_REG_EVT: { diff --git a/components/bt/bluedroid/btc/profile/std/include/dis_api.h b/components/bt/bluedroid/btc/profile/std/include/dis_api.h index 95bf24ac5..5c8d5243f 100644 --- a/components/bt/bluedroid/btc/profile/std/include/dis_api.h +++ b/components/bt/bluedroid/btc/profile/std/include/dis_api.h @@ -28,7 +28,7 @@ #include "bt_target.h" #include "gatt_api.h" #include "gattdefs.h" -#include "esp_gatt_api.h" +#include "esp_gatts_api.h" #define DIS_SUCCESS GATT_SUCCESS #define DIS_ILLEGAL_PARAM GATT_ILLEGAL_PARAMETER diff --git a/components/bt/bluedroid/include/bt_target.h b/components/bt/bluedroid/include/bt_target.h index bb9d59860..c427f8f49 100755 --- a/components/bt/bluedroid/include/bt_target.h +++ b/components/bt/bluedroid/include/bt_target.h @@ -795,7 +795,7 @@ * resolution, local address rotation etc. */ #ifndef BLE_PRIVACY_SPT -#define BLE_PRIVACY_SPT TRUE +#define BLE_PRIVACY_SPT FALSE ///TRUE #endif /* diff --git a/components/bt/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/bluedroid/stack/btm/btm_ble_gap.c index fee484221..33785a296 100755 --- a/components/bt/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/bluedroid/stack/btm/btm_ble_gap.c @@ -415,7 +415,7 @@ tBTM_STATUS BTM_BleObserve(BOOLEAN start, UINT8 duration, } else { - BTM_TRACE_ERROR("%s Observe not active", __func__); + BTM_TRACE_ERROR("%s Observe not active\n", __func__); } return status; @@ -2869,13 +2869,14 @@ void btm_ble_process_adv_pkt (UINT8 *p_data) STREAM_TO_UINT8 (evt_type, p); STREAM_TO_UINT8 (addr_type, p); STREAM_TO_BDADDR (bda, p); - + //BTM_TRACE_ERROR("btm_ble_process_adv_pkt:bda= %0x:%0x:%0x:%0x:%0x:%0x\n", + // bda[0],bda[1],bda[2],bda[3],bda[4],bda[5]); #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE) /* map address to security record */ match = btm_identity_addr_to_random_pseudo(bda, &addr_type, FALSE); - BTM_TRACE_DEBUG("btm_ble_process_adv_pkt:bda= %0x:%0x:%0x:%0x:%0x:%0x", - bda[0],bda[1],bda[2],bda[3],bda[4],bda[5]); + // BTM_TRACE_ERROR("btm_ble_process_adv_pkt:bda= %0x:%0x:%0x:%0x:%0x:%0x\n", + // bda[0],bda[1],bda[2],bda[3],bda[4],bda[5]); /* always do RRA resolution on host */ if (!match && BTM_BLE_IS_RESOLVE_BDA(bda)) { @@ -2993,7 +2994,7 @@ static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, UINT8 addr_type, UINT8 evt btm_send_sel_conn_callback(bda, evt_type, p, addr_type); else { - BTM_TRACE_DEBUG("None LE device, can not initiate selective connection"); + BTM_TRACE_DEBUG("None LE device, can not initiate selective connection\n"); } } else diff --git a/components/bt/bluedroid/stack/btu/btu_hcif.c b/components/bt/bluedroid/stack/btu/btu_hcif.c index ccbc90a9d..3e4f75bbf 100755 --- a/components/bt/bluedroid/stack/btu/btu_hcif.c +++ b/components/bt/bluedroid/stack/btu/btu_hcif.c @@ -1687,7 +1687,7 @@ static void btu_hcif_encryption_key_refresh_cmpl_evt (UINT8 *p) static void btu_ble_process_adv_pkt (UINT8 *p) { - HCI_TRACE_EVENT("btu_ble_process_adv_pkt"); + HCI_TRACE_DEBUG("btu_ble_process_adv_pkt\n"); btm_ble_process_adv_pkt(p); } diff --git a/examples/06_bluedroid_demos/components/bluedroid_demos/app_client_profiles/battery_c/battery_c.c b/examples/06_bluedroid_demos/components/bluedroid_demos/app_client_profiles/battery_c/battery_c.c index 9500f7709..2f146e967 100644 --- a/examples/06_bluedroid_demos/components/bluedroid_demos/app_client_profiles/battery_c/battery_c.c +++ b/examples/06_bluedroid_demos/components/bluedroid_demos/app_client_profiles/battery_c/battery_c.c @@ -31,7 +31,7 @@ #include "btm_api.h" #include "bt_types.h" #include "gattc_profile.h" -#include "esp_gatt_api.h" +#include "esp_gatts_api.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] diff --git a/examples/06_bluedroid_demos/components/bluedroid_demos/app_project/SampleClientProject.c b/examples/06_bluedroid_demos/components/bluedroid_demos/app_project/SampleClientProject.c index 6f3be9a0f..5eb616a36 100644 --- a/examples/06_bluedroid_demos/components/bluedroid_demos/app_project/SampleClientProject.c +++ b/examples/06_bluedroid_demos/components/bluedroid_demos/app_project/SampleClientProject.c @@ -184,7 +184,7 @@ static void bta_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH* p char dev_name[32]; tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE; //char obj_name[] = "Find Me"; - char obj_name[] = "SimpleBLEPeripheral"; + char obj_name[] = "SimpleBLEClient"; uint8_t dev_name_len; switch (event) @@ -224,7 +224,7 @@ static void bta_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH* p 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*/ + /* scan complete, start connect*/ BTA_GATTC_Open(client_if, obj_addr, true, transport); } break; diff --git a/examples/10_gatt_client/Makefile b/examples/10_gatt_client/Makefile new file mode 100644 index 000000000..1e91bbb89 --- /dev/null +++ b/examples/10_gatt_client/Makefile @@ -0,0 +1,11 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := bluedroid_demos + +COMPONENT_ADD_INCLUDEDIRS := components/include + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/10_gatt_client/README.rst b/examples/10_gatt_client/README.rst new file mode 100644 index 000000000..3b074c72d --- /dev/null +++ b/examples/10_gatt_client/README.rst @@ -0,0 +1,10 @@ +ESP-IDF Blufi 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.. + diff --git a/examples/10_gatt_client/components/bluedroid_demos/app_core/bt_app_core.c b/examples/10_gatt_client/components/bluedroid_demos/app_core/bt_app_core.c new file mode 100644 index 000000000..71854cfba --- /dev/null +++ b/examples/10_gatt_client/components/bluedroid_demos/app_core/bt_app_core.c @@ -0,0 +1,418 @@ +// 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 +#include +#include + +#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); +} + diff --git a/examples/10_gatt_client/components/bluedroid_demos/app_project/Arch_SimpleClientProject.c b/examples/10_gatt_client/components/bluedroid_demos/app_project/Arch_SimpleClientProject.c new file mode 100644 index 000000000..1d1eec6a0 --- /dev/null +++ b/examples/10_gatt_client/components/bluedroid_demos/app_project/Arch_SimpleClientProject.c @@ -0,0 +1,447 @@ +// 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 +#include +#include +#include +#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_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\n"); + 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(); +} diff --git a/examples/10_gatt_client/components/bluedroid_demos/component.mk b/examples/10_gatt_client/components/bluedroid_demos/component.mk new file mode 100644 index 000000000..81ba2de72 --- /dev/null +++ b/examples/10_gatt_client/components/bluedroid_demos/component.mk @@ -0,0 +1,17 @@ +# +# 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 diff --git a/examples/10_gatt_client/components/bluedroid_demos/include/bt_app_common.h b/examples/10_gatt_client/components/bluedroid_demos/include/bt_app_common.h new file mode 100644 index 000000000..501bfccc9 --- /dev/null +++ b/examples/10_gatt_client/components/bluedroid_demos/include/bt_app_common.h @@ -0,0 +1,30 @@ +#ifndef __BT_APP_COMMON_H__ +#define __BT_APP_COMMON_H__ + +#include +#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__ */ diff --git a/examples/10_gatt_client/main/component.mk b/examples/10_gatt_client/main/component.mk new file mode 100644 index 000000000..24356f23e --- /dev/null +++ b/examples/10_gatt_client/main/component.mk @@ -0,0 +1,10 @@ +# +# 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. +# + +include $(IDF_PATH)/make/component_common.mk diff --git a/examples/10_gatt_client/main/demo_main.c b/examples/10_gatt_client/main/demo_main.c new file mode 100644 index 000000000..a128c1f06 --- /dev/null +++ b/examples/10_gatt_client/main/demo_main.c @@ -0,0 +1,43 @@ +// 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 +#include +#include +#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); +} +