mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
NimBLE: Added health thermometer profile
This commit is contained in:
parent
3fc6eb9000
commit
71f5dc4973
@ -522,6 +522,7 @@ if(CONFIG_BT_ENABLED)
|
||||
host/nimble/nimble/nimble/host/services/dis/include
|
||||
host/nimble/nimble/nimble/host/services/gap/include
|
||||
host/nimble/nimble/nimble/host/services/gatt/include
|
||||
host/nimble/nimble/nimble/host/services/htp/include
|
||||
host/nimble/nimble/nimble/host/services/ias/include
|
||||
host/nimble/nimble/nimble/host/services/ipss/include
|
||||
host/nimble/nimble/nimble/host/services/lls/include
|
||||
@ -537,6 +538,7 @@ if(CONFIG_BT_ENABLED)
|
||||
"host/nimble/nimble/nimble/host/services/ias/src/ble_svc_ias.c"
|
||||
"host/nimble/nimble/nimble/host/services/ipss/src/ble_svc_ipss.c"
|
||||
"host/nimble/nimble/nimble/host/services/ans/src/ble_svc_ans.c"
|
||||
"host/nimble/nimble/nimble/host/services/htp/src/ble_svc_htp.c"
|
||||
"host/nimble/nimble/nimble/host/services/gap/src/ble_svc_gap.c"
|
||||
"host/nimble/nimble/nimble/host/services/bas/src/ble_svc_bas.c"
|
||||
"host/nimble/nimble/nimble/host/services/dis/src/ble_svc_dis.c"
|
||||
|
@ -93,6 +93,12 @@ examples/bluetooth/nimble:
|
||||
|
||||
reason: not tested yet
|
||||
|
||||
examples/bluetooth/nimble/ble_htp:
|
||||
enable:
|
||||
- if: IDF_TARGET in ["esp32", "esp32c2", "esp32c3", "esp32c6", "esp32h2", "esp32h4", "esp32s3"]
|
||||
temporary: true
|
||||
reason: the other targets are not tested yet
|
||||
|
||||
examples/bluetooth/nimble/ble_l2cap_coc:
|
||||
enable:
|
||||
- if: IDF_TARGET in ["esp32", "esp32c2", "esp32c3", "esp32s3" ]
|
||||
|
@ -0,0 +1,8 @@
|
||||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/bluetooth/nimble/common/nimble_central_utils)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(htp_cent)
|
152
examples/bluetooth/nimble/ble_htp/htp_cent/README.md
Normal file
152
examples/bluetooth/nimble/ble_htp/htp_cent/README.md
Normal file
@ -0,0 +1,152 @@
|
||||
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S3 |
|
||||
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- |
|
||||
|
||||
# BLE HTP Cent Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example creates GATT client and performs passive scan, it then connects to peripheral device if the device advertises connectability and the device advertises support for the Health Thermometer service (0x1809) as primary service UUID.
|
||||
|
||||
After connection it enables bonding and link encryprion if the `Enable Link Encryption` flag is set in the example config.
|
||||
|
||||
It performs four GATT operations against the specified peer:
|
||||
|
||||
* Reads the HTP temperature type characteristic.
|
||||
|
||||
* After the read operation is completed, writes the HTP measurement interval characteristic.
|
||||
|
||||
* After the write operation is completed, subscribes to indications for the HTP temperature measurement characteristic.
|
||||
|
||||
* After the subscribe operation is completed, it subscribes to notifications for the HTP intermediate temperature characteristic.
|
||||
|
||||
If the peer does not support a required service, characteristic, or descriptor, then the peer lied when it claimed support for the Health Thermometer service! When this happens, or if a GATT procedure fails, this function immediately terminates the connection.
|
||||
|
||||
It uses ESP32's Bluetooth controller and NimBLE stack based BLE host.
|
||||
|
||||
This example aims at understanding BLE service discovery, connection, encryption and characteristic operations.
|
||||
|
||||
To test this demo, use any BLE GATT server app that advertises support for the Health Thermometer service (0x1809) and includes it in the GATT database.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
Before project configuration and build, be sure to set the correct chip target using:
|
||||
|
||||
```bash
|
||||
idf.py set-target <chip_name>
|
||||
```
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with ESP32/ESP32-C2/ESP32-C3/ESP32-S3 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
|
||||
* A USB cable for Power supply and programming
|
||||
|
||||
See [Development Boards](https://www.espressif.com/en/products/devkits) for more information about it.
|
||||
|
||||
### Configure the Project
|
||||
|
||||
Open the project configuration menu:
|
||||
|
||||
```bash
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
In the `Example Configuration` menu:
|
||||
|
||||
* Change the `Peer Address` option if needed.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the [Getting Started Guide](https://idf.espressif.com/) for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
This is the console output on successful connection:
|
||||
|
||||
```
|
||||
I (356) BLE_INIT: BT controller compile version [921f253]
|
||||
I (356) phy_init: phy_version 950,f732b06,Feb 15 2023,18:57:12
|
||||
I (396) BLE_INIT: Bluetooth MAC: 84:f7:03:05:a5:f6
|
||||
|
||||
I (396) NimBLE_HTP_CENT: BLE Host Task Started
|
||||
I (406) NimBLE: GAP procedure initiated: stop advertising.
|
||||
|
||||
I (406) NimBLE: GAP procedure initiated: discovery;
|
||||
I (406) NimBLE: own_addr_type=0 filter_policy=0 passive=1 limited=0 filter_duplicates=1
|
||||
I (416) NimBLE: duration=forever
|
||||
I (426) NimBLE:
|
||||
|
||||
I (426) main_task: Returned from app_main()
|
||||
|
||||
I (616) NimBLE: GAP procedure initiated: connect;
|
||||
I (616) NimBLE: peer_addr_type=0 peer_addr=
|
||||
I (616) NimBLE: 70:04:1d:13:66:fe
|
||||
I (616) NimBLE: scan_itvl=16 scan_window=16 itvl_min=24 itvl_max=40 latency=0 supervision_timeout=256 min_ce_len=0 max_ce_len=0 own_addr_type=0
|
||||
I (636) NimBLE:
|
||||
|
||||
I (676) NimBLE: Connection established
|
||||
I (676) NimBLE:
|
||||
|
||||
I (676) NimBLE: GATT procedure initiated: discover all services
|
||||
|
||||
I (856) NimBLE: GATT procedure initiated: discover all characteristics;
|
||||
I (856) NimBLE: start_handle=1 end_handle=5
|
||||
|
||||
I (1056) NimBLE: GATT procedure initiated: discover all characteristics;
|
||||
I (1056) NimBLE: start_handle=6 end_handle=9
|
||||
|
||||
I (1256) NimBLE: GATT procedure initiated: discover all characteristics;
|
||||
I (1256) NimBLE: start_handle=10 end_handle=22
|
||||
|
||||
I (1556) NimBLE: GATT procedure initiated: discover all characteristics;
|
||||
I (1556) NimBLE: start_handle=23 end_handle=65535
|
||||
|
||||
I (1756) NimBLE: GATT procedure initiated: discover all descriptors;
|
||||
I (1756) NimBLE: chr_val_handle=8 end_handle=9
|
||||
|
||||
I (1856) NimBLE: GATT procedure initiated: discover all descriptors;
|
||||
I (1856) NimBLE: chr_val_handle=12 end_handle=13
|
||||
|
||||
I (1956) NimBLE: GATT procedure initiated: discover all descriptors;
|
||||
I (1956) NimBLE: chr_val_handle=17 end_handle=18
|
||||
|
||||
I (2056) NimBLE: GATT procedure initiated: discover all descriptors;
|
||||
I (2056) NimBLE: chr_val_handle=20 end_handle=22
|
||||
|
||||
I (2156) NimBLE: GATT procedure initiated: discover all descriptors;
|
||||
I (2156) NimBLE: chr_val_handle=29 end_handle=65535
|
||||
|
||||
I (2256) NimBLE: Service discovery complete; status=0 conn_handle=1
|
||||
|
||||
I (2256) NimBLE: GATT procedure initiated: read;
|
||||
I (2256) NimBLE: att_handle=15
|
||||
|
||||
I (283056) NimBLE: Read temperature type char completed; status=0 conn_handle=1
|
||||
I (283056) NimBLE: attr_handle=167 value=
|
||||
I (283056) NimBLE: 0x02
|
||||
I (283066) NimBLE:
|
||||
|
||||
I (283066) NimBLE: GATT procedure initiated: write;
|
||||
I (283066) NimBLE: att_handle=172 len=2
|
||||
|
||||
I (283156) NimBLE: Write to measurement interval char completed; status=0 conn_handle=1 attr_handle=172
|
||||
|
||||
I (283156) NimBLE: GATT procedure initiated: write;
|
||||
I (283166) NimBLE: att_handle=163 len=2
|
||||
|
||||
I (283256) NimBLE: Subscribe to temperature measurement char completed; status=0 conn_handle=1 attr_handle=163
|
||||
|
||||
I (283256) NimBLE: GATT procedure initiated: write;
|
||||
I (283266) NimBLE: att_handle=170 len=2
|
||||
|
||||
I (283356) NimBLE: Subscribe to intermediate temperature char completed; status=0 conn_handle=1 attr_handle=170
|
||||
|
||||
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
@ -0,0 +1,4 @@
|
||||
set(srcs "main.c")
|
||||
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS ".")
|
@ -0,0 +1,24 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
config EXAMPLE_PEER_ADDR
|
||||
string "Peer Address"
|
||||
default "ADDR_ANY"
|
||||
help
|
||||
Enter the peer address in aa:bb:cc:dd:ee:ff form to connect to a specific peripheral
|
||||
|
||||
config EXAMPLE_ENCRYPTION
|
||||
bool
|
||||
prompt "Enable Link Encryption"
|
||||
default y
|
||||
help
|
||||
This enables bonding and encryption after connection has been established.
|
||||
|
||||
config EXAMPLE_EXTENDED_ADV
|
||||
bool
|
||||
depends on SOC_BLE_50_SUPPORTED
|
||||
default y if SOC_ESP_NIMBLE_CONTROLLER
|
||||
select BT_NIMBLE_EXT_ADV
|
||||
prompt "Enable Extended Adv"
|
||||
help
|
||||
Use this option to enable extended advertising in the example
|
||||
endmenu
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef H_BLE_HTP_CENT_
|
||||
#define H_BLE_HTP_CENT_
|
||||
|
||||
#include "modlog/modlog.h"
|
||||
#include "esp_central.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ble_hs_adv_fields;
|
||||
struct ble_gap_conn_desc;
|
||||
struct ble_hs_cfg;
|
||||
union ble_store_value;
|
||||
union ble_store_key;
|
||||
|
||||
/* 16 Bit Health Thermometer Service UUID */
|
||||
#define BLE_SVC_HTP_UUID16 0x1809
|
||||
|
||||
/* 16 Bit Health Thermometer Service Characteristic UUIDs */
|
||||
#define BLE_SVC_HTP_CHR_UUID16_TEMP_MEASUREMENT 0x2A1C
|
||||
#define BLE_SVC_HTP_CHR_UUID16_TEMP_TYPE 0x2A1D
|
||||
#define BLE_SVC_HTP_CHR_UUID16_INTERMEDIATE_TEMP 0x2A1E
|
||||
#define BLE_SVC_HTP_CHR_UUID16_MEASUREMENT_ITVL 0x2A21
|
||||
|
||||
/* 16 Bit Device Information Service Characteristic UUIDs */
|
||||
#define GATT_DIS_DEVICE_INFO_UUID 0x180A
|
||||
#define GATT_DIS_CHR_UUID16_SYS_ID 0x2A23
|
||||
#define GATT_DIS_CHR_UUID16_MODEL_NO 0x2A24
|
||||
#define GATT_DIS_CHR_UUID16_MFC_NAME 0x2A29
|
||||
|
||||
/* 16 BIT CCCD UUID */
|
||||
#define BLE_SVC_HTP_DSC_CLT_CFG_UUID16 0x2902
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -368,7 +368,11 @@ ble_htp_cent_should_connect(const struct ble_gap_disc_desc *disc)
|
||||
|
||||
rc = ble_hs_adv_parse_fields(&fields, disc->data, disc->length_data);
|
||||
if (rc != 0) {
|
||||
<<<<<<< HEAD
|
||||
return 0;
|
||||
=======
|
||||
return rc;
|
||||
>>>>>>> NimBLE: Added health thermometer profile
|
||||
}
|
||||
|
||||
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) && (strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
|
||||
|
@ -0,0 +1,12 @@
|
||||
# Override some defaults so BT stack is enabled
|
||||
# in this example
|
||||
|
||||
#
|
||||
# BT config
|
||||
#
|
||||
CONFIG_BT_ENABLED=y
|
||||
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
|
||||
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
|
||||
CONFIG_BTDM_CTRL_MODE_BTDM=n
|
||||
CONFIG_BT_BLUEDROID_ENABLED=n
|
||||
CONFIG_BT_NIMBLE_ENABLED=y
|
@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(htp_prph)
|
84
examples/bluetooth/nimble/ble_htp/htp_prph/README.md
Normal file
84
examples/bluetooth/nimble/ble_htp/htp_prph/README.md
Normal file
@ -0,0 +1,84 @@
|
||||
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S3 |
|
||||
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- |
|
||||
|
||||
# BLE Heart Rate Measurement Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example creates GATT server demonstrating standard Health Thermometer profile. It simulates temperature measurement and notifies to client when the notifications are enabled.
|
||||
|
||||
It advertises support for the Health Thermometer service (0x1809) as primary service UUID.
|
||||
|
||||
When internmediate temperature characteristic is subscribed, the timer resets and it sends notifications to client every 1 sec.
|
||||
|
||||
It uses ESP32's Bluetooth controller and NimBLE stack based BLE host
|
||||
|
||||
This example aims at understanding notification subscriptions and sending notifications.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
Before project configuration and build, be sure to set the correct chip target using:
|
||||
|
||||
```bash
|
||||
idf.py set-target <chip_name>
|
||||
```
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with ESP32/ESP32-C3 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
|
||||
* A USB cable for Power supply and programming
|
||||
|
||||
See [Development Boards](https://www.espressif.com/en/products/devkits) for more information about it.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the [Getting Started Guide](https://idf.espressif.com/) for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
This console output can be observed when blehr is connected to client and client enables notifications:
|
||||
|
||||
```
|
||||
I (356) BLE_INIT: BT controller compile version [921f253]
|
||||
I (356) phy_init: phy_version 950,f732b06,Feb 15 2023,18:57:12
|
||||
I (396) BLE_INIT: Bluetooth MAC: 70:04:1d:13:66:fe
|
||||
|
||||
I (396) NimBLE_BLE_Health_Therm: BLE Host Task Started
|
||||
I (396) NimBLE: GAP procedure initiated: stop advertising.
|
||||
|
||||
I (406) NimBLE: Failed to restore IRKs from store; status=8
|
||||
|
||||
I (406) NimBLE: Device Address:
|
||||
I (416) NimBLE: 70:04:1d:13:66:fe
|
||||
I (416) NimBLE:
|
||||
|
||||
I (416) NimBLE: GAP procedure initiated: advertise;
|
||||
I (426) NimBLE: disc_mode=2
|
||||
I (426) NimBLE: adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=0 adv_itvl_max=0
|
||||
I (436) NimBLE:
|
||||
|
||||
I (446) main_task: Returned from app_main()
|
||||
I (6116) NimBLE: connection established; status=0
|
||||
|
||||
I (7946) NimBLE: subscribe event; cur_notify=0
|
||||
value handle; val_handle=12
|
||||
|
||||
I (7956) BLE_GAP_SUBSCRIBE_EVENT: conn_handle from subscribe=1
|
||||
|
||||
I (8046) NimBLE: subscribe event; cur_notify=1
|
||||
value handle; val_handle=17
|
||||
|
||||
I (8056) BLE_GAP_SUBSCRIBE_EVENT: conn_handle from subscribe=1
|
||||
I (9056) NimBLE: GATT procedure initiated: notify;
|
||||
I (9056) NimBLE: att_handle=17
|
||||
|
||||
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "main.c" "gatt_svr.c"
|
||||
INCLUDE_DIRS ".")
|
@ -0,0 +1,11 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
config EXAMPLE_EXTENDED_ADV
|
||||
bool
|
||||
depends on SOC_BLE_50_SUPPORTED
|
||||
default y if SOC_ESP_NIMBLE_CONTROLLER
|
||||
select BT_NIMBLE_EXT_ADV
|
||||
prompt "Enable Extended Adv"
|
||||
help
|
||||
Use this option to enable extended advertising in the example
|
||||
endmenu
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef H_BLE_HTP_PRPH_
|
||||
#define H_BLE_HTP_PRPH_
|
||||
|
||||
#include "nimble/ble.h"
|
||||
#include "modlog/modlog.h"
|
||||
#include "services/htp/ble_svc_htp.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 16 Bit Device Information Service Characteristic UUIDs */
|
||||
#define GATT_DIS_DEVICE_INFO_UUID 0x180A
|
||||
#define GATT_DIS_CHR_UUID16_SYS_ID 0x2A23
|
||||
#define GATT_DIS_CHR_UUID16_MODEL_NO 0x2A24
|
||||
#define GATT_DIS_CHR_UUID16_MFC_NAME 0x2A29
|
||||
|
||||
struct ble_hs_cfg;
|
||||
struct ble_gatt_register_ctxt;
|
||||
|
||||
void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg);
|
||||
int gatt_svr_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
135
examples/bluetooth/nimble/ble_htp/htp_prph/main/gatt_svr.c
Normal file
135
examples/bluetooth/nimble/ble_htp/htp_prph/main/gatt_svr.c
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/ble_uuid.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "services/gatt/ble_svc_gatt.h"
|
||||
#include "ble_htp_prph.h"
|
||||
|
||||
static const char *manuf_name = "ESP32 devkitC";
|
||||
static const char *model_num = "HTP Sensor demo";
|
||||
static const char *system_id = "HTP1";
|
||||
static int
|
||||
gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
|
||||
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
|
||||
{
|
||||
/* Service: Device Information */
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_DIS_DEVICE_INFO_UUID),
|
||||
.characteristics = (struct ble_gatt_chr_def[])
|
||||
{ {
|
||||
/* Characteristic: * Manufacturer name */
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_DIS_CHR_UUID16_MFC_NAME),
|
||||
.access_cb = gatt_svr_chr_access_device_info,
|
||||
.flags = BLE_GATT_CHR_F_READ,
|
||||
}, {
|
||||
/* Characteristic: Model number string */
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_DIS_CHR_UUID16_MODEL_NO),
|
||||
.access_cb = gatt_svr_chr_access_device_info,
|
||||
.flags = BLE_GATT_CHR_F_READ,
|
||||
}, {
|
||||
/* Characteristic: System ID */
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_DIS_CHR_UUID16_SYS_ID),
|
||||
.access_cb = gatt_svr_chr_access_device_info,
|
||||
.flags = BLE_GATT_CHR_F_READ,
|
||||
}, {
|
||||
0, /* No more characteristics in this service */
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
0, /* No more services */
|
||||
},
|
||||
};
|
||||
|
||||
static int
|
||||
gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||
{
|
||||
uint16_t uuid;
|
||||
int rc;
|
||||
|
||||
uuid = ble_uuid_u16(ctxt->chr->uuid);
|
||||
|
||||
if (uuid == GATT_DIS_CHR_UUID16_MODEL_NO) {
|
||||
rc = os_mbuf_append(ctxt->om, model_num, strlen(model_num));
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
|
||||
if (uuid == GATT_DIS_CHR_UUID16_MFC_NAME) {
|
||||
rc = os_mbuf_append(ctxt->om, manuf_name, strlen(manuf_name));
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
|
||||
if (uuid == GATT_DIS_CHR_UUID16_SYS_ID) {
|
||||
rc = os_mbuf_append(ctxt->om, system_id, strlen(system_id));
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
|
||||
assert(0);
|
||||
return BLE_ATT_ERR_UNLIKELY;
|
||||
}
|
||||
|
||||
void
|
||||
gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
|
||||
{
|
||||
char buf[BLE_UUID_STR_LEN];
|
||||
|
||||
switch (ctxt->op) {
|
||||
case BLE_GATT_REGISTER_OP_SVC:
|
||||
MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
|
||||
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
|
||||
ctxt->svc.handle);
|
||||
break;
|
||||
|
||||
case BLE_GATT_REGISTER_OP_CHR:
|
||||
MODLOG_DFLT(DEBUG, "registering characteristic %s with "
|
||||
"def_handle=%d val_handle=%d\n",
|
||||
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
|
||||
ctxt->chr.def_handle,
|
||||
ctxt->chr.val_handle);
|
||||
break;
|
||||
|
||||
case BLE_GATT_REGISTER_OP_DSC:
|
||||
MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
|
||||
ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
|
||||
ctxt->dsc.handle);
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
gatt_svr_init(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
ble_svc_gap_init();
|
||||
ble_svc_gatt_init();
|
||||
ble_svc_htp_init();
|
||||
|
||||
rc = ble_gatts_count_cfg(gatt_svr_svcs);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = ble_gatts_add_svcs(gatt_svr_svcs);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
381
examples/bluetooth/nimble/ble_htp/htp_prph/main/main.c
Normal file
381
examples/bluetooth/nimble/ble_htp/htp_prph/main/main.c
Normal file
@ -0,0 +1,381 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "freertos/FreeRTOSConfig.h"
|
||||
/* BLE */
|
||||
#include "nimble/nimble_port.h"
|
||||
#include "nimble/nimble_port_freertos.h"
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/util/util.h"
|
||||
#include "console/console.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "ble_htp_prph.h"
|
||||
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
static uint8_t ext_adv_pattern_1[] = {
|
||||
0x02, 0x01, 0x06,
|
||||
0x03, 0x03, 0xab, 0xcd,
|
||||
0x03, 0x03, 0x18, 0x09,
|
||||
0x12, 0X09, 'n', 'i', 'm', 'b', 'l', 'e', '-', 'h', 't', 'p', '-', 'p', 'r', 'p', 'h', '-', 'e',
|
||||
};
|
||||
#endif
|
||||
|
||||
static const char *tag = "NimBLE_HTP_PRPH";
|
||||
static TimerHandle_t ble_htp_prph_tx_timer;
|
||||
static uint16_t conn_handle;
|
||||
static const char *device_name = "ble_htp_prph";
|
||||
|
||||
static int ble_htp_prph_gap_event(struct ble_gap_event *event, void *arg);
|
||||
|
||||
static uint8_t ble_htp_prph_addr_type;
|
||||
|
||||
/**
|
||||
* Utility function to log an array of bytes.
|
||||
*/
|
||||
void
|
||||
print_bytes(const uint8_t *bytes, int len)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
MODLOG_DFLT(INFO, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
print_addr(const void *addr)
|
||||
{
|
||||
const uint8_t *u8p;
|
||||
|
||||
u8p = addr;
|
||||
MODLOG_DFLT(INFO, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
|
||||
}
|
||||
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
/**
|
||||
* Enables advertising with the following parameters:
|
||||
* o General discoverable mode.
|
||||
* o Undirected connectable mode.
|
||||
*/
|
||||
static void
|
||||
ext_ble_htp_prph_advertise(void)
|
||||
{
|
||||
struct ble_gap_ext_adv_params params;
|
||||
struct os_mbuf *data;
|
||||
uint8_t instance = 0;
|
||||
int rc;
|
||||
|
||||
/* First check if any instance is already active */
|
||||
if (ble_gap_ext_adv_active(instance)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* use defaults for non-set params */
|
||||
memset (¶ms, 0, sizeof(params));
|
||||
|
||||
/* enable connectable advertising */
|
||||
params.connectable = 1;
|
||||
|
||||
/* advertise using random addr */
|
||||
params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
|
||||
|
||||
params.primary_phy = BLE_HCI_LE_PHY_1M;
|
||||
params.secondary_phy = BLE_HCI_LE_PHY_2M;
|
||||
params.sid = 1;
|
||||
|
||||
params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
|
||||
params.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
|
||||
|
||||
/* configure instance 0 */
|
||||
rc = ble_gap_ext_adv_configure(instance, ¶ms, NULL,
|
||||
ble_htp_prph_gap_event, NULL);
|
||||
assert (rc == 0);
|
||||
|
||||
/* in this case only scan response is allowed */
|
||||
|
||||
/* get mbuf for scan rsp data */
|
||||
data = os_msys_get_pkthdr(sizeof(ext_adv_pattern_1), 0);
|
||||
assert(data);
|
||||
|
||||
/* fill mbuf with scan rsp data */
|
||||
rc = os_mbuf_append(data, ext_adv_pattern_1, sizeof(ext_adv_pattern_1));
|
||||
assert(rc == 0);
|
||||
|
||||
rc = ble_gap_ext_adv_set_data(instance, data);
|
||||
assert (rc == 0);
|
||||
|
||||
/* start advertising */
|
||||
rc = ble_gap_ext_adv_start(instance, 0, 0);
|
||||
assert (rc == 0);
|
||||
}
|
||||
#else
|
||||
|
||||
static void
|
||||
ble_htp_prph_advertise(void)
|
||||
{
|
||||
struct ble_gap_adv_params adv_params;
|
||||
struct ble_hs_adv_fields fields;
|
||||
int rc;
|
||||
|
||||
/*
|
||||
* Set the advertisement data included in our advertisements:
|
||||
* o Flags (indicates advertisement type and other general info)
|
||||
* o Advertising tx power
|
||||
* o Device name
|
||||
*/
|
||||
memset(&fields, 0, sizeof(fields));
|
||||
|
||||
/*
|
||||
* Advertise two flags:
|
||||
* o Discoverability in forthcoming advertisement (general)
|
||||
* o BLE-only (BR/EDR unsupported)
|
||||
*/
|
||||
fields.flags = BLE_HS_ADV_F_DISC_GEN |
|
||||
BLE_HS_ADV_F_BREDR_UNSUP;
|
||||
|
||||
/*
|
||||
* Indicate that the TX power level field should be included; have the
|
||||
* stack fill this value automatically. This is done by assigning the
|
||||
* special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
|
||||
*/
|
||||
fields.tx_pwr_lvl_is_present = 1;
|
||||
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
|
||||
|
||||
fields.name = (uint8_t *)device_name;
|
||||
fields.name_len = strlen(device_name);
|
||||
fields.name_is_complete = 1;
|
||||
|
||||
fields.uuids16 = (ble_uuid16_t[]) {
|
||||
BLE_UUID16_INIT(BLE_SVC_HTP_UUID16)
|
||||
};
|
||||
fields.num_uuids16 = 1;
|
||||
fields.uuids16_is_complete = 1;
|
||||
|
||||
rc = ble_gap_adv_set_fields(&fields);
|
||||
if (rc != 0) {
|
||||
MODLOG_DFLT(ERROR, "error setting advertisement data; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Begin advertising */
|
||||
memset(&adv_params, 0, sizeof(adv_params));
|
||||
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
|
||||
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
||||
rc = ble_gap_adv_start(ble_htp_prph_addr_type, NULL, BLE_HS_FOREVER,
|
||||
&adv_params, ble_htp_prph_gap_event, NULL);
|
||||
if (rc != 0) {
|
||||
MODLOG_DFLT(ERROR, "error enabling advertisement; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
ble_htp_prph_tx_htp_stop(void)
|
||||
{
|
||||
xTimerStop( ble_htp_prph_tx_timer, 1000 / portTICK_PERIOD_MS );
|
||||
}
|
||||
|
||||
/* Reset temperature measurement */
|
||||
static void
|
||||
ble_htp_prph_tx_htp_reset(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (xTimerReset(ble_htp_prph_tx_timer, 1000 / portTICK_PERIOD_MS ) == pdPASS) {
|
||||
rc = 0;
|
||||
} else {
|
||||
rc = 1;
|
||||
}
|
||||
|
||||
assert(rc == 0);
|
||||
|
||||
}
|
||||
|
||||
/* This function notifies intermediate temperature to the client */
|
||||
static void
|
||||
ble_htp_prph_tx(TimerHandle_t ev)
|
||||
{
|
||||
int rc;
|
||||
float temp;
|
||||
|
||||
/* If temperature value to be indicated is in Fahrenheit set temp_unit as true or else false
|
||||
for Celsius */
|
||||
bool temp_unit = true;
|
||||
|
||||
if (!ble_svc_htp_is_subscribed(conn_handle, INTERMEDIATE_TEMP)) {
|
||||
ble_htp_prph_tx_htp_stop();
|
||||
printf("\nreturn");
|
||||
return;
|
||||
}
|
||||
|
||||
temp = 0; /* 0 Fahrenheit */
|
||||
|
||||
rc = ble_svc_htp_notify(conn_handle, temp, temp_unit);
|
||||
if (rc == 0) {
|
||||
MODLOG_DFLT(INFO, "Notification sent successfully");
|
||||
} else {
|
||||
MODLOG_DFLT(INFO, "Error in sending notification");
|
||||
}
|
||||
|
||||
ble_htp_prph_tx_htp_reset();
|
||||
}
|
||||
|
||||
static int
|
||||
ble_htp_prph_gap_event(struct ble_gap_event *event, void *arg)
|
||||
{
|
||||
switch (event->type) {
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
/* A new connection was established or a connection attempt failed */
|
||||
MODLOG_DFLT(INFO, "connection %s; status=%d\n",
|
||||
event->connect.status == 0 ? "established" : "failed",
|
||||
event->connect.status);
|
||||
|
||||
if (event->connect.status != 0) {
|
||||
/* Connection failed; resume advertising */
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_ble_htp_prph_advertise();
|
||||
#else
|
||||
ble_htp_prph_advertise();
|
||||
#endif
|
||||
|
||||
}
|
||||
conn_handle = event->connect.conn_handle;
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
MODLOG_DFLT(INFO, "disconnect; reason=%d\n", event->disconnect.reason);
|
||||
|
||||
/* Connection terminated; resume advertising */
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_ble_htp_prph_advertise();
|
||||
#else
|
||||
ble_htp_prph_advertise();
|
||||
#endif
|
||||
ble_htp_prph_tx_htp_stop();
|
||||
|
||||
ble_svc_htp_on_disconnect(event->disconnect.conn.conn_handle);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_ADV_COMPLETE:
|
||||
MODLOG_DFLT(INFO, "adv complete\n");
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_ble_htp_prph_advertise();
|
||||
#else
|
||||
ble_htp_prph_advertise();
|
||||
#endif
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_SUBSCRIBE:
|
||||
MODLOG_DFLT(INFO, "subscribe event; cur_notify=%d\n value handle; "
|
||||
"val_handle=%d\n",
|
||||
event->subscribe.cur_notify, event->subscribe.attr_handle);
|
||||
|
||||
ble_svc_htp_subscribe(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
||||
|
||||
if (event->subscribe.cur_notify) {
|
||||
ble_htp_prph_tx_htp_reset();
|
||||
}
|
||||
|
||||
ESP_LOGI("BLE_GAP_SUBSCRIBE_EVENT", "conn_handle from subscribe=%d", conn_handle);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_MTU:
|
||||
MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d mtu=%d\n",
|
||||
event->mtu.conn_handle,
|
||||
event->mtu.value);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
ble_htp_prph_on_sync(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = ble_hs_id_infer_auto(0, &ble_htp_prph_addr_type);
|
||||
assert(rc == 0);
|
||||
|
||||
uint8_t addr_val[6] = {0};
|
||||
rc = ble_hs_id_copy_addr(ble_htp_prph_addr_type, addr_val, NULL);
|
||||
|
||||
MODLOG_DFLT(INFO, "Device Address: ");
|
||||
print_addr(addr_val);
|
||||
MODLOG_DFLT(INFO, "\n");
|
||||
|
||||
/* Begin advertising */
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_ble_htp_prph_advertise();
|
||||
#else
|
||||
ble_htp_prph_advertise();
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
ble_htp_prph_on_reset(int reason)
|
||||
{
|
||||
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
|
||||
}
|
||||
|
||||
void ble_htp_prph_host_task(void *param)
|
||||
{
|
||||
ESP_LOGI(tag, "BLE Host Task Started");
|
||||
/* This function will return only when nimble_port_stop() is executed */
|
||||
nimble_port_run();
|
||||
|
||||
nimble_port_freertos_deinit();
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
/* Initialize NVS — it is used to store PHY calibration data */
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
ret = nimble_port_init();
|
||||
if (ret != ESP_OK) {
|
||||
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Initialize the NimBLE host configuration */
|
||||
ble_hs_cfg.sync_cb = ble_htp_prph_on_sync;
|
||||
ble_hs_cfg.reset_cb = ble_htp_prph_on_reset;
|
||||
|
||||
/* Enable bonding */
|
||||
ble_hs_cfg.sm_bonding = 1;
|
||||
ble_hs_cfg.sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
|
||||
ble_hs_cfg.sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
|
||||
|
||||
ble_hs_cfg.sm_sc = 1;
|
||||
ble_hs_cfg.sm_mitm = 1;
|
||||
|
||||
/* name, period/time, auto reload, timer ID, callback */
|
||||
ble_htp_prph_tx_timer = xTimerCreate("ble_htp_prph_tx_timer", pdMS_TO_TICKS(1000), pdTRUE,
|
||||
(void *)0, ble_htp_prph_tx);
|
||||
|
||||
rc = gatt_svr_init();
|
||||
assert(rc == 0);
|
||||
|
||||
/* Set the default device name */
|
||||
rc = ble_svc_gap_device_name_set(device_name);
|
||||
assert(rc == 0);
|
||||
|
||||
/* Start the task */
|
||||
nimble_port_freertos_init(ble_htp_prph_host_task);
|
||||
|
||||
}
|
3
examples/bluetooth/nimble/ble_htp/htp_prph/sdkconfig.ci
Normal file
3
examples/bluetooth/nimble/ble_htp/htp_prph/sdkconfig.ci
Normal file
@ -0,0 +1,3 @@
|
||||
CONFIG_FREERTOS_UNICORE=y
|
||||
CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY=y
|
||||
CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_IRAM_8BIT=y
|
@ -0,0 +1,12 @@
|
||||
# Override some defaults so BT stack is enabled
|
||||
# in this example
|
||||
|
||||
#
|
||||
# BT config
|
||||
#
|
||||
CONFIG_BT_ENABLED=y
|
||||
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
|
||||
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
|
||||
CONFIG_BTDM_CTRL_MODE_BTDM=n
|
||||
CONFIG_BT_BLUEDROID_ENABLED=n
|
||||
CONFIG_BT_NIMBLE_ENABLED=y
|
Loading…
x
Reference in New Issue
Block a user