From eff95f3799a992d9a91eef0b2fd4079bcdcc6a4f Mon Sep 17 00:00:00 2001 From: island Date: Tue, 18 Jul 2017 20:42:22 +0800 Subject: [PATCH 1/4] component/bt: add ibeacon demo --- examples/bluetooth/ble_ibeacon/Makefile | 10 + examples/bluetooth/ble_ibeacon/README.rst | 5 + .../bluetooth/ble_ibeacon/main/component.mk | 4 + .../bluetooth/ble_ibeacon/main/ibeacon_demo.c | 254 ++++++++++++++++++ .../bluetooth/ble_ibeacon/sdkconfig.defaults | 4 + 5 files changed, 277 insertions(+) create mode 100644 examples/bluetooth/ble_ibeacon/Makefile create mode 100644 examples/bluetooth/ble_ibeacon/README.rst create mode 100644 examples/bluetooth/ble_ibeacon/main/component.mk create mode 100644 examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c create mode 100644 examples/bluetooth/ble_ibeacon/sdkconfig.defaults diff --git a/examples/bluetooth/ble_ibeacon/Makefile b/examples/bluetooth/ble_ibeacon/Makefile new file mode 100644 index 0000000000..a104adccb1 --- /dev/null +++ b/examples/bluetooth/ble_ibeacon/Makefile @@ -0,0 +1,10 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := ble_ibeacon_demo + +COMPONENT_ADD_INCLUDEDIRS := components/include + +include $(IDF_PATH)/make/project.mk diff --git a/examples/bluetooth/ble_ibeacon/README.rst b/examples/bluetooth/ble_ibeacon/README.rst new file mode 100644 index 0000000000..c7c4d490b7 --- /dev/null +++ b/examples/bluetooth/ble_ibeacon/README.rst @@ -0,0 +1,5 @@ +ESP-IDF GATT CLIENT demo +======================== + +This is the demo for user to use ESP_APIs to create a GATT Client. + diff --git a/examples/bluetooth/ble_ibeacon/main/component.mk b/examples/bluetooth/ble_ibeacon/main/component.mk new file mode 100644 index 0000000000..a98f634eae --- /dev/null +++ b/examples/bluetooth/ble_ibeacon/main/component.mk @@ -0,0 +1,4 @@ +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) diff --git a/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c b/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c new file mode 100644 index 0000000000..7665bae847 --- /dev/null +++ b/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c @@ -0,0 +1,254 @@ +// 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.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 IBEACON_TAG "IBEACON_DEMO" + +/* Because current ESP IDF version doesn't support scan and adv simultaneously, + * so Ibeacon sender and receiver should not run simultaneously */ +#define IBEACON_SENDER 0 +#define IBEACON_RECEIVER 1 +//#define IBEACON_MODE IBEACON_SENDER //IBEACON_RECEIVER +#define IBEACON_MODE IBEACON_RECEIVER + +///Declare static functions +static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); + +#if (IBEACON_MODE == IBEACON_RECEIVER) +static esp_ble_scan_params_t ble_scan_params = { + .scan_type = BLE_SCAN_TYPE_ACTIVE, + .own_addr_type = BLE_ADDR_TYPE_PUBLIC, + .scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL, + .scan_interval = 0x50, + .scan_window = 0x30 +}; + +#elif (IBEACON_MODE == IBEACON_SENDER) +static esp_ble_adv_params_t ble_adv_params = { + .adv_int_min = 0x20, + .adv_int_max = 0x40, + .adv_type = ADV_TYPE_NONCONN_IND, + .own_addr_type = BLE_ADDR_TYPE_PUBLIC, + .channel_map = ADV_CHNL_ALL, + .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, +}; +#endif + + +typedef struct { + uint8_t flags[3]; + uint8_t length; + uint8_t type; + uint16_t company_id; + uint16_t beacon_type; +}__attribute__((packed)) esp_ble_ibeacon_head_t; + +typedef struct { + uint8_t proximity_uuid[16]; + uint16_t major; + uint16_t minor; + uint8_t tx_power; +}__attribute__((packed)) esp_ble_ibeacon_vendor_t; + + +typedef struct { + esp_ble_ibeacon_head_t ibeacon_head; + esp_ble_ibeacon_vendor_t ibeacon_vendor; +}__attribute__((packed)) esp_ble_ibeacon_t; + +const uint8_t uuid_zeros[ESP_UUID_LEN_128] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* For Ibeacon packet format, please refer to Apple "Proximity Beacon Specification" doc */ +const esp_ble_ibeacon_head_t ibeacon_common_head = { + .flags = {0x02, 0x01, 0x06}, + .length = 0x1A, + .type = 0xFF, + .company_id = 0x004C, + .beacon_type = 0x1502 +}; + +bool esp_ble_is_ibeacon_packet (uint8_t *adv_data, uint8_t adv_data_len){ + bool result = FALSE; + + if ((adv_data != NULL) && (adv_data_len == 0x1E)){ + if (!memcmp(adv_data, (uint8_t*)&ibeacon_common_head, sizeof(ibeacon_common_head))){ + result = TRUE; + } + } + + return result; +} + +esp_err_t esp_ble_config_ibeacon_data (esp_ble_ibeacon_vendor_t *vendor_config, esp_ble_ibeacon_t *ibeacon_adv_data){ + if ((vendor_config == NULL) || (ibeacon_adv_data == NULL) || (!memcmp(vendor_config->proximity_uuid, uuid_zeros, sizeof(uuid_zeros)))){ + return ESP_ERR_INVALID_ARG; + } + + memcpy(&(ibeacon_adv_data->ibeacon_head), (uint8_t*)&ibeacon_common_head, sizeof(ibeacon_common_head)); + memcpy(&(ibeacon_adv_data->ibeacon_vendor), vendor_config, sizeof(esp_ble_ibeacon_vendor_t)); + + return ESP_OK; +} + + +static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) +{ + switch (event) { + case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:{ +#if (IBEACON_MODE == IBEACON_SENDER) + esp_ble_gap_start_advertising(&ble_adv_params); +#endif + break; + } + case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: { +#if (IBEACON_MODE == IBEACON_RECEIVER) + //the unit of the duration is second, 0 means scan permanently + uint32_t duration = 0; + esp_ble_gap_start_scanning(duration); +#endif + break; + } + case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT: + //scan start complete event to indicate scan start successfully or failed + if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { + ESP_LOGE(IBEACON_TAG, "Scan start failed"); + } + break; + case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: + //adv start complete event to indicate adv start successfully or failed + if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { + ESP_LOGE(IBEACON_TAG, "Adv start failed"); + } + 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: + /* Search for BLE Ibeacon Packet */ + if (esp_ble_is_ibeacon_packet(scan_result->scan_rst.ble_adv, scan_result->scan_rst.adv_data_len)){ + esp_ble_ibeacon_t *ibeacon_data = (esp_ble_ibeacon_t*)(scan_result->scan_rst.ble_adv); + ESP_LOGI(IBEACON_TAG, "----------Ibeacon Found----------"); + esp_log_buffer_hex("IBEACON_DEMO: Device address:", scan_result->scan_rst.bda, BD_ADDR_LEN ); + esp_log_buffer_hex("IBEACON_DEMO: Proximity UUID:", ibeacon_data->ibeacon_vendor.proximity_uuid, ESP_UUID_LEN_128); + + ESP_LOGI(IBEACON_TAG, "Major: 0x%04x", ibeacon_data->ibeacon_vendor.major); + ESP_LOGI(IBEACON_TAG, "Minor: 0x%04x", ibeacon_data->ibeacon_vendor.minor); + ESP_LOGI(IBEACON_TAG, "Tx Power:0x%02x", ibeacon_data->ibeacon_vendor.tx_power); + } + break; + default: + break; + } + break; + } + + case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT: + if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){ + ESP_LOGE(IBEACON_TAG, "Scan stop failed"); + } + else { + ESP_LOGI(IBEACON_TAG, "Stop scan successfully"); + } + break; + + case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: + if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){ + ESP_LOGE(IBEACON_TAG, "Adv stop failed"); + } + else { + ESP_LOGI(IBEACON_TAG, "Stop adv successfully"); + } + break; + + default: + break; + } +} + + +void ble_client_appRegister(void) +{ + esp_err_t status; + + ESP_LOGI(IBEACON_TAG, "register callback"); + + //register the scan callback function to the gap module + if ((status = esp_ble_gap_register_callback(esp_gap_cb)) != ESP_OK) { + ESP_LOGE(IBEACON_TAG, "gap register error, error code = %x", status); + return; + } + +} + +void gattc_client_test(void) +{ + esp_bluedroid_init(); + esp_bluedroid_enable(); + ble_client_appRegister(); +} + +void app_main() +{ + esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); + esp_bt_controller_init(&bt_cfg); + esp_bt_controller_enable(ESP_BT_MODE_BTDM); + + gattc_client_test(); + + /* set scan parameters */ +#if (IBEACON_MODE == IBEACON_RECEIVER) + esp_ble_gap_set_scan_params(&ble_scan_params); + +#elif (IBEACON_MODE == IBEACON_SENDER) + esp_ble_ibeacon_t ibeacon_adv_data; + esp_ble_ibeacon_vendor_t vendor_config = { + .proximity_uuid = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0XFF}, + .major = 0x0001, + .minor = 0x0010, + .tx_power = 0xC5 + }; + esp_err_t status = esp_ble_config_ibeacon_data (&vendor_config, &ibeacon_adv_data); + if (status == ESP_OK){ + esp_ble_gap_config_adv_data_raw((uint8_t*)&ibeacon_adv_data, sizeof(ibeacon_adv_data)); + } + else { + ESP_LOGE(IBEACON_TAG, "Config Ibeacon data failed, status =0x%x\n", status); + } +#endif +} + diff --git a/examples/bluetooth/ble_ibeacon/sdkconfig.defaults b/examples/bluetooth/ble_ibeacon/sdkconfig.defaults new file mode 100644 index 0000000000..9d51df5ee5 --- /dev/null +++ b/examples/bluetooth/ble_ibeacon/sdkconfig.defaults @@ -0,0 +1,4 @@ +# Override some defaults so BT stack is enabled +# and WiFi disabled by default in this example +CONFIG_BT_ENABLED=y +CONFIG_WIFI_ENABLED=n From 2f9111c13fb07e78f4325ca83559fbd1e249f909 Mon Sep 17 00:00:00 2001 From: island Date: Tue, 25 Jul 2017 14:36:00 +0800 Subject: [PATCH 2/4] component/bt: Optimize iBeacon demo - Add endian change for major and minor part - Change measured power format - Optimize comments in example --- examples/bluetooth/ble_ibeacon/README.rst | 8 +- .../bluetooth/ble_ibeacon/main/ibeacon_demo.c | 112 ++++++++++-------- 2 files changed, 70 insertions(+), 50 deletions(-) diff --git a/examples/bluetooth/ble_ibeacon/README.rst b/examples/bluetooth/ble_ibeacon/README.rst index c7c4d490b7..eb9a97b294 100644 --- a/examples/bluetooth/ble_ibeacon/README.rst +++ b/examples/bluetooth/ble_ibeacon/README.rst @@ -1,5 +1,9 @@ -ESP-IDF GATT CLIENT demo +ESP-IDF iBeacon demo ======================== +This example demonstrates iBeacon-compatible BLE advertising, and scanning of iBeacons. +- IBEACON_SENDER: demo to send iBeacon-compatible advertising data. +- IBEACON_RECEIVER: demo to receive and resolve iBeacon advertising data. -This is the demo for user to use ESP_APIs to create a GATT Client. +iBeacon is a trademark of Apple Inc. +Before building devices which use iBeacon technology, visit https://developer.apple.com/ibeacon/ to obtain a license. diff --git a/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c b/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c index 7665bae847..59fc2a5d1e 100644 --- a/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c +++ b/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c @@ -1,4 +1,4 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2017 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. @@ -16,7 +16,11 @@ /**************************************************************************** * -* This file is for gatt client. It can scan ble device, connect one device, +* This file is for iBeacon demo. It supports both iBeacon sender and receiver +* which is distinguished by macros IBEACON_SENDER and IBEACON_RECEIVER, +* +* iBeacon is a trademark of Apple Inc. Before building devices which use iBeacon technology, +* visit https://developer.apple.com/ibeacon/ to obtain a license. * ****************************************************************************/ @@ -25,6 +29,7 @@ #include #include #include "controller.h" +#include "nvs_flash.h" #include "bt.h" #include "bt_trace.h" @@ -37,15 +42,20 @@ #include "esp_gatt_defs.h" #include "esp_bt_main.h" -#define IBEACON_TAG "IBEACON_DEMO" /* Because current ESP IDF version doesn't support scan and adv simultaneously, - * so Ibeacon sender and receiver should not run simultaneously */ + * so iBeacon sender and receiver should not run simultaneously */ #define IBEACON_SENDER 0 #define IBEACON_RECEIVER 1 -//#define IBEACON_MODE IBEACON_SENDER //IBEACON_RECEIVER #define IBEACON_MODE IBEACON_RECEIVER +static const char* DEMO_TAG = "IBEACON_DEMO"; + +/* Major and Minor part are stored in big endian mode in iBeacon packet, + * need to use this macro to transfer while creating or processing + * iBeacon data */ +#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8)) + ///Declare static functions static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); @@ -82,7 +92,7 @@ typedef struct { uint8_t proximity_uuid[16]; uint16_t major; uint16_t minor; - uint8_t tx_power; + int8_t measured_power; }__attribute__((packed)) esp_ble_ibeacon_vendor_t; @@ -93,7 +103,8 @@ typedef struct { const uint8_t uuid_zeros[ESP_UUID_LEN_128] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; -/* For Ibeacon packet format, please refer to Apple "Proximity Beacon Specification" doc */ +/* For iBeacon packet format, please refer to Apple "Proximity Beacon Specification" doc */ +/* Constant part of iBeacon data */ const esp_ble_ibeacon_head_t ibeacon_common_head = { .flags = {0x02, 0x01, 0x06}, .length = 0x1A, @@ -102,9 +113,18 @@ const esp_ble_ibeacon_head_t ibeacon_common_head = { .beacon_type = 0x1502 }; -bool esp_ble_is_ibeacon_packet (uint8_t *adv_data, uint8_t adv_data_len){ - bool result = FALSE; - +/* Vendor part of iBeacon data*/ +esp_ble_ibeacon_vendor_t vendor_config = { + .proximity_uuid = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0XFF}, + .major = ENDIAN_CHANGE_U16(0x0001), //Major=0x0001 + .minor = ENDIAN_CHANGE_U16(0x0010), //Minor=0x0010 + .measured_power = 0xC5 +}; + +BOOLEAN esp_ble_is_ibeacon_packet (uint8_t *adv_data, uint8_t adv_data_len){ + BOOLEAN result = FALSE; + false; + if ((adv_data != NULL) && (adv_data_len == 0x1E)){ if (!memcmp(adv_data, (uint8_t*)&ibeacon_common_head, sizeof(ibeacon_common_head))){ result = TRUE; @@ -119,8 +139,8 @@ esp_err_t esp_ble_config_ibeacon_data (esp_ble_ibeacon_vendor_t *vendor_config, return ESP_ERR_INVALID_ARG; } - memcpy(&(ibeacon_adv_data->ibeacon_head), (uint8_t*)&ibeacon_common_head, sizeof(ibeacon_common_head)); - memcpy(&(ibeacon_adv_data->ibeacon_vendor), vendor_config, sizeof(esp_ble_ibeacon_vendor_t)); + memcpy(&ibeacon_adv_data->ibeacon_head, &ibeacon_common_head, sizeof(esp_ble_ibeacon_head_t)); + memcpy(&ibeacon_adv_data->ibeacon_vendor, vendor_config, sizeof(esp_ble_ibeacon_vendor_t)); return ESP_OK; } @@ -131,10 +151,10 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par switch (event) { case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:{ #if (IBEACON_MODE == IBEACON_SENDER) - esp_ble_gap_start_advertising(&ble_adv_params); + esp_ble_gap_start_advertising(&ble_adv_params); #endif - break; - } + break; + } case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: { #if (IBEACON_MODE == IBEACON_RECEIVER) //the unit of the duration is second, 0 means scan permanently @@ -146,29 +166,30 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT: //scan start complete event to indicate scan start successfully or failed if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { - ESP_LOGE(IBEACON_TAG, "Scan start failed"); + ESP_LOGE(DEMO_TAG, "Scan start failed"); } break; - case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: + case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: //adv start complete event to indicate adv start successfully or failed - if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { - ESP_LOGE(IBEACON_TAG, "Adv start failed"); - } - break; + if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { + ESP_LOGE(DEMO_TAG, "Adv start failed"); + } + 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: - /* Search for BLE Ibeacon Packet */ + /* Search for BLE iBeacon Packet */ if (esp_ble_is_ibeacon_packet(scan_result->scan_rst.ble_adv, scan_result->scan_rst.adv_data_len)){ esp_ble_ibeacon_t *ibeacon_data = (esp_ble_ibeacon_t*)(scan_result->scan_rst.ble_adv); - ESP_LOGI(IBEACON_TAG, "----------Ibeacon Found----------"); + ESP_LOGI(DEMO_TAG, "----------iBeacon Found----------"); esp_log_buffer_hex("IBEACON_DEMO: Device address:", scan_result->scan_rst.bda, BD_ADDR_LEN ); esp_log_buffer_hex("IBEACON_DEMO: Proximity UUID:", ibeacon_data->ibeacon_vendor.proximity_uuid, ESP_UUID_LEN_128); - ESP_LOGI(IBEACON_TAG, "Major: 0x%04x", ibeacon_data->ibeacon_vendor.major); - ESP_LOGI(IBEACON_TAG, "Minor: 0x%04x", ibeacon_data->ibeacon_vendor.minor); - ESP_LOGI(IBEACON_TAG, "Tx Power:0x%02x", ibeacon_data->ibeacon_vendor.tx_power); + ESP_LOGI(DEMO_TAG, "Major: 0x%04x", ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.major)); + ESP_LOGI(DEMO_TAG, "Minor: 0x%04x", ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.minor)); + ESP_LOGI(DEMO_TAG, "Measured power (RSSI at a 1m distance):%d dbm", ibeacon_data->ibeacon_vendor.measured_power); + ESP_LOGI(DEMO_TAG, "RSSI of packet:%d dbm", scan_result->scan_rst.rssi); } break; default: @@ -179,19 +200,19 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT: if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){ - ESP_LOGE(IBEACON_TAG, "Scan stop failed"); + ESP_LOGE(DEMO_TAG, "Scan stop failed"); } else { - ESP_LOGI(IBEACON_TAG, "Stop scan successfully"); + ESP_LOGI(DEMO_TAG, "Stop scan successfully"); } break; case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){ - ESP_LOGE(IBEACON_TAG, "Adv stop failed"); + ESP_LOGE(DEMO_TAG, "Adv stop failed"); } else { - ESP_LOGI(IBEACON_TAG, "Stop adv successfully"); + ESP_LOGI(DEMO_TAG, "Stop adv successfully"); } break; @@ -201,54 +222,49 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par } -void ble_client_appRegister(void) +void ble_ibeacon_appRegister(void) { esp_err_t status; - ESP_LOGI(IBEACON_TAG, "register callback"); + ESP_LOGI(DEMO_TAG, "register callback"); //register the scan callback function to the gap module if ((status = esp_ble_gap_register_callback(esp_gap_cb)) != ESP_OK) { - ESP_LOGE(IBEACON_TAG, "gap register error, error code = %x", status); + ESP_LOGE(DEMO_TAG, "gap register error, error code = %x", status); return; } } -void gattc_client_test(void) +void ble_ibeacon_init(void) { esp_bluedroid_init(); esp_bluedroid_enable(); - ble_client_appRegister(); + ble_ibeacon_appRegister(); } void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); esp_bt_controller_init(&bt_cfg); esp_bt_controller_enable(ESP_BT_MODE_BTDM); - gattc_client_test(); + ble_ibeacon_init(); /* set scan parameters */ #if (IBEACON_MODE == IBEACON_RECEIVER) esp_ble_gap_set_scan_params(&ble_scan_params); #elif (IBEACON_MODE == IBEACON_SENDER) - esp_ble_ibeacon_t ibeacon_adv_data; - esp_ble_ibeacon_vendor_t vendor_config = { - .proximity_uuid = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0XFF}, - .major = 0x0001, - .minor = 0x0010, - .tx_power = 0xC5 - }; + esp_ble_ibeacon_t ibeacon_adv_data; esp_err_t status = esp_ble_config_ibeacon_data (&vendor_config, &ibeacon_adv_data); if (status == ESP_OK){ - esp_ble_gap_config_adv_data_raw((uint8_t*)&ibeacon_adv_data, sizeof(ibeacon_adv_data)); - } - else { - ESP_LOGE(IBEACON_TAG, "Config Ibeacon data failed, status =0x%x\n", status); - } + esp_ble_gap_config_adv_data_raw((uint8_t*)&ibeacon_adv_data, sizeof(ibeacon_adv_data)); + } + else { + ESP_LOGE(DEMO_TAG, "Config iBeacon data failed, status =0x%x\n", status); + } #endif } From 941001959e197a5b17ae02b35c0bf743b9ef1813 Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Wed, 26 Jul 2017 19:50:17 +0800 Subject: [PATCH 3/4] Component/bt: delete the useless .h files --- examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c b/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c index 59fc2a5d1e..2ad02990be 100644 --- a/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c +++ b/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c @@ -32,11 +32,6 @@ #include "nvs_flash.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" From 9e27c24e54ce6f5c2385d87e412a53cf5c2af85d Mon Sep 17 00:00:00 2001 From: island Date: Mon, 4 Sep 2017 21:17:09 +0800 Subject: [PATCH 4/4] component/bt: add esp_ibeacon_api file --- .../ble_ibeacon/main/esp_ibeacon_api.c | 78 +++++++++++++++ .../ble_ibeacon/main/esp_ibeacon_api.h | 84 ++++++++++++++++ .../bluetooth/ble_ibeacon/main/ibeacon_demo.c | 95 ++----------------- 3 files changed, 169 insertions(+), 88 deletions(-) create mode 100644 examples/bluetooth/ble_ibeacon/main/esp_ibeacon_api.c create mode 100644 examples/bluetooth/ble_ibeacon/main/esp_ibeacon_api.h diff --git a/examples/bluetooth/ble_ibeacon/main/esp_ibeacon_api.c b/examples/bluetooth/ble_ibeacon/main/esp_ibeacon_api.c new file mode 100644 index 0000000000..96d602a24a --- /dev/null +++ b/examples/bluetooth/ble_ibeacon/main/esp_ibeacon_api.c @@ -0,0 +1,78 @@ +// Copyright 2015-2017 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 iBeacon APIs. It supports both iBeacon encode and decode. +* +* iBeacon is a trademark of Apple Inc. Before building devices which use iBeacon technology, +* visit https://developer.apple.com/ibeacon/ to obtain a license. +* +****************************************************************************/ + +#include +#include +#include +#include + +#include "esp_gap_ble_api.h" +#include "esp_ibeacon_api.h" + + +const uint8_t uuid_zeros[ESP_UUID_LEN_128] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +/* For iBeacon packet format, please refer to Apple "Proximity Beacon Specification" doc */ +/* Constant part of iBeacon data */ +esp_ble_ibeacon_head_t ibeacon_common_head = { + .flags = {0x02, 0x01, 0x06}, + .length = 0x1A, + .type = 0xFF, + .company_id = 0x004C, + .beacon_type = 0x1502 +}; + +/* Vendor part of iBeacon data*/ +esp_ble_ibeacon_vendor_t vendor_config = { + .proximity_uuid = ESP_UUID, + .major = ENDIAN_CHANGE_U16(ESP_MAJOR), //Major=ESP_MAJOR + .minor = ENDIAN_CHANGE_U16(ESP_MINOR), //Minor=ESP_MINOR + .measured_power = 0xC5 +}; + +BOOLEAN esp_ble_is_ibeacon_packet (uint8_t *adv_data, uint8_t adv_data_len){ + BOOLEAN result = FALSE; + + if ((adv_data != NULL) && (adv_data_len == 0x1E)){ + if (!memcmp(adv_data, (uint8_t*)&ibeacon_common_head, sizeof(ibeacon_common_head))){ + result = TRUE; + } + } + + return result; +} + +esp_err_t esp_ble_config_ibeacon_data (esp_ble_ibeacon_vendor_t *vendor_config, esp_ble_ibeacon_t *ibeacon_adv_data){ + if ((vendor_config == NULL) || (ibeacon_adv_data == NULL) || (!memcmp(vendor_config->proximity_uuid, uuid_zeros, sizeof(uuid_zeros)))){ + return ESP_ERR_INVALID_ARG; + } + + memcpy(&ibeacon_adv_data->ibeacon_head, &ibeacon_common_head, sizeof(esp_ble_ibeacon_head_t)); + memcpy(&ibeacon_adv_data->ibeacon_vendor, vendor_config, sizeof(esp_ble_ibeacon_vendor_t)); + + return ESP_OK; +} + + diff --git a/examples/bluetooth/ble_ibeacon/main/esp_ibeacon_api.h b/examples/bluetooth/ble_ibeacon/main/esp_ibeacon_api.h new file mode 100644 index 0000000000..b91cd646be --- /dev/null +++ b/examples/bluetooth/ble_ibeacon/main/esp_ibeacon_api.h @@ -0,0 +1,84 @@ +// Copyright 2015-2017 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 iBeacon definitions. It supports both iBeacon sender and receiver +* which is distinguished by macros IBEACON_SENDER and IBEACON_RECEIVER, +* +* iBeacon is a trademark of Apple Inc. Before building devices which use iBeacon technology, +* visit https://developer.apple.com/ibeacon/ to obtain a license. +* +****************************************************************************/ + +#include +#include +#include +#include +#include "controller.h" + +#include "esp_gap_ble_api.h" +#include "esp_gattc_api.h" + + +/* Because current ESP IDF version doesn't support scan and adv simultaneously, + * so iBeacon sender and receiver should not run simultaneously */ +#define IBEACON_SENDER 0 +#define IBEACON_RECEIVER 1 +#define IBEACON_MODE IBEACON_SENDER + +/* Major and Minor part are stored in big endian mode in iBeacon packet, + * need to use this macro to transfer while creating or processing + * iBeacon data */ +#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8)) + +/* Espressif WeChat official account can be found using WeChat "Yao Yi Yao Zhou Bian", + * if device advertises using ESP defined UUID. + * Please refer to http://zb.weixin.qq.com for further information. */ +#define ESP_UUID {0xFD, 0xA5, 0x06, 0x93, 0xA4, 0xE2, 0x4F, 0xB1, 0xAF, 0xCF, 0xC6, 0xEB, 0x07, 0x64, 0x78, 0x25} +#define ESP_MAJOR 10167 +#define ESP_MINOR 61958 + + +typedef struct { + uint8_t flags[3]; + uint8_t length; + uint8_t type; + uint16_t company_id; + uint16_t beacon_type; +}__attribute__((packed)) esp_ble_ibeacon_head_t; + +typedef struct { + uint8_t proximity_uuid[16]; + uint16_t major; + uint16_t minor; + int8_t measured_power; +}__attribute__((packed)) esp_ble_ibeacon_vendor_t; + + +typedef struct { + esp_ble_ibeacon_head_t ibeacon_head; + esp_ble_ibeacon_vendor_t ibeacon_vendor; +}__attribute__((packed)) esp_ble_ibeacon_t; + + +/* For iBeacon packet format, please refer to Apple "Proximity Beacon Specification" doc */ +/* Constant part of iBeacon data */ +extern esp_ble_ibeacon_head_t ibeacon_common_head; + +BOOLEAN esp_ble_is_ibeacon_packet (uint8_t *adv_data, uint8_t adv_data_len); + +esp_err_t esp_ble_config_ibeacon_data (esp_ble_ibeacon_vendor_t *vendor_config, esp_ble_ibeacon_t *ibeacon_adv_data); diff --git a/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c b/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c index 2ad02990be..63884a9fc9 100644 --- a/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c +++ b/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c @@ -1,12 +1,4 @@ -// Copyright 2015-2017 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 +// Copyright 2015-2017 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 @@ -36,20 +28,11 @@ #include "esp_gattc_api.h" #include "esp_gatt_defs.h" #include "esp_bt_main.h" +#include "esp_ibeacon_api.h" -/* Because current ESP IDF version doesn't support scan and adv simultaneously, - * so iBeacon sender and receiver should not run simultaneously */ -#define IBEACON_SENDER 0 -#define IBEACON_RECEIVER 1 -#define IBEACON_MODE IBEACON_RECEIVER - static const char* DEMO_TAG = "IBEACON_DEMO"; - -/* Major and Minor part are stored in big endian mode in iBeacon packet, - * need to use this macro to transfer while creating or processing - * iBeacon data */ -#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8)) +extern esp_ble_ibeacon_vendor_t vendor_config; ///Declare static functions static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); @@ -75,72 +58,6 @@ static esp_ble_adv_params_t ble_adv_params = { #endif -typedef struct { - uint8_t flags[3]; - uint8_t length; - uint8_t type; - uint16_t company_id; - uint16_t beacon_type; -}__attribute__((packed)) esp_ble_ibeacon_head_t; - -typedef struct { - uint8_t proximity_uuid[16]; - uint16_t major; - uint16_t minor; - int8_t measured_power; -}__attribute__((packed)) esp_ble_ibeacon_vendor_t; - - -typedef struct { - esp_ble_ibeacon_head_t ibeacon_head; - esp_ble_ibeacon_vendor_t ibeacon_vendor; -}__attribute__((packed)) esp_ble_ibeacon_t; - -const uint8_t uuid_zeros[ESP_UUID_LEN_128] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - -/* For iBeacon packet format, please refer to Apple "Proximity Beacon Specification" doc */ -/* Constant part of iBeacon data */ -const esp_ble_ibeacon_head_t ibeacon_common_head = { - .flags = {0x02, 0x01, 0x06}, - .length = 0x1A, - .type = 0xFF, - .company_id = 0x004C, - .beacon_type = 0x1502 -}; - -/* Vendor part of iBeacon data*/ -esp_ble_ibeacon_vendor_t vendor_config = { - .proximity_uuid = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0XFF}, - .major = ENDIAN_CHANGE_U16(0x0001), //Major=0x0001 - .minor = ENDIAN_CHANGE_U16(0x0010), //Minor=0x0010 - .measured_power = 0xC5 -}; - -BOOLEAN esp_ble_is_ibeacon_packet (uint8_t *adv_data, uint8_t adv_data_len){ - BOOLEAN result = FALSE; - false; - - if ((adv_data != NULL) && (adv_data_len == 0x1E)){ - if (!memcmp(adv_data, (uint8_t*)&ibeacon_common_head, sizeof(ibeacon_common_head))){ - result = TRUE; - } - } - - return result; -} - -esp_err_t esp_ble_config_ibeacon_data (esp_ble_ibeacon_vendor_t *vendor_config, esp_ble_ibeacon_t *ibeacon_adv_data){ - if ((vendor_config == NULL) || (ibeacon_adv_data == NULL) || (!memcmp(vendor_config->proximity_uuid, uuid_zeros, sizeof(uuid_zeros)))){ - return ESP_ERR_INVALID_ARG; - } - - memcpy(&ibeacon_adv_data->ibeacon_head, &ibeacon_common_head, sizeof(esp_ble_ibeacon_head_t)); - memcpy(&ibeacon_adv_data->ibeacon_vendor, vendor_config, sizeof(esp_ble_ibeacon_vendor_t)); - - return ESP_OK; -} - - static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { switch (event) { @@ -181,8 +98,10 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par esp_log_buffer_hex("IBEACON_DEMO: Device address:", scan_result->scan_rst.bda, BD_ADDR_LEN ); esp_log_buffer_hex("IBEACON_DEMO: Proximity UUID:", ibeacon_data->ibeacon_vendor.proximity_uuid, ESP_UUID_LEN_128); - ESP_LOGI(DEMO_TAG, "Major: 0x%04x", ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.major)); - ESP_LOGI(DEMO_TAG, "Minor: 0x%04x", ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.minor)); + uint16_t major = ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.major); + uint16_t minor = ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.minor); + ESP_LOGI(DEMO_TAG, "Major: 0x%04x (%d)", major, major); + ESP_LOGI(DEMO_TAG, "Minor: 0x%04x (%d)", minor, minor); ESP_LOGI(DEMO_TAG, "Measured power (RSSI at a 1m distance):%d dbm", ibeacon_data->ibeacon_vendor.measured_power); ESP_LOGI(DEMO_TAG, "RSSI of packet:%d dbm", scan_result->scan_rst.rssi); }