Skip to content

Commit

Permalink
feat(nimble): Added blecsc profile support for nimble examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ESPAbhinav committed Apr 3, 2024
1 parent 28f68a0 commit f8b2ab7
Show file tree
Hide file tree
Showing 10 changed files with 1,320 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .codespellrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[codespell]
skip = build,*.yuv,components/fatfs/src/*,alice.txt,*.rgb
ignore-words-list = ser,dout,rsource,fram,inout,shs
ignore-words-list = ser,dout,rsource,fram,inout,shs,ans
write-changes = true
6 changes: 6 additions & 0 deletions examples/bluetooth/nimble/blecsc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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(blecsc)
135 changes: 135 additions & 0 deletions examples/bluetooth/nimble/blecsc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- |



# BLE CSC(Cycling Speed and Cadence) Example

(See the README.md file in the upper level 'examples' directory for more information about examples.)

This Bluetooth Low Energy (BLE) Cycling Speed and Cadence (CSC) profile is designed to provide real-time measurements of a cyclist's speed and cadence. It allows cycling-related devices, such as bike sensors or fitness trackers, to communicate this data wirelessly to a receiving device, such as a smartphone or cycling computer. The major tasks of the BLE CSC profile include:

Connection Establishment: This example creates GATT server and then starts advertising, waiting to be connected to a GATT client.

Service Discovery: Once the connection is established, the client device performs service discovery to identify the available services and characteristics supported by the CSC Sensor. The CSC profile typically utilizes the Generic Attribute Profile (GATT) for this purpose.

CSC Measurement: The CSC Sensor periodically sends CSC Measurement notifications to the client device. These notifications contain the current speed and cadence data measured by the sensor. The client device can subscribe to these notifications to receive real-time updates. The `blecsc_simulate_speed_and_cadence()` function is used to calculate simulated CSC measurements, including speed and cadence.


It uses ESP's Bluetooth controller and NimBLE stack based BLE host.

This example aims at understanding CSC measurements, GATT database configuration, handling GATT reads and writes, handling subscribe events and understanding advertisement.


Note :

* To install the dependency packages needed, please refer to the top level [README file](../../../README.md#running-test-python-script-ttfw).
* Currently this Python utility is only supported on Linux (BLE communication is via BLuez + DBus).

## 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>
```

### Configure the project

Open the project configuration menu:

```bash
idf.py menuconfig
```

In the `Example Configuration` menu:

* Select I/O capabilities of device from `Example Configuration --> I/O Capability`, default is `Just_works`.
* Enable/Disable other security related parameters `Bonding, MITM option, secure connection(SM SC)`.

### 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

## There is this console output when blecsc starts advertising

```
I (424) NimBLE_BLE_CSC: BLE Host Task Started
I (424) NimBLE: GAP procedure initiated: stop advertising.
I (424) NimBLE: Failed to restore IRKs from store; status=8
I (434) NimBLE: GAP procedure initiated: advertise;
I (434) NimBLE: disc_mode=2
I (434) NimBLE: adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=0 adv_itvl_max=0
I (444) NimBLE:
I (454) main_task: Returned from app_main()
I (1424) NimBLE: CSC simulated values: speed = 1 kph, cadence = 21
I (1524) NimBLE: CSC simulated values: speed = 2 kph, cadence = 22
I (1624) NimBLE: CSC simulated values: speed = 3 kph, cadence = 23
I (1724) NimBLE: CSC simulated values: speed = 4 kph, cadence = 24
I (1824) NimBLE: CSC simulated values: speed = 5 kph, cadence = 25
I (1924) NimBLE: CSC simulated values: speed = 6 kph, cadence = 26
I (2024) NimBLE: CSC simulated values: speed = 7 kph, cadence = 27
I (2124) NimBLE: CSC simulated values: speed = 8 kph, cadence = 28
I (2224) NimBLE: CSC simulated values: speed = 9 kph, cadence = 29
I (2324) NimBLE: CSC simulated values: speed = 10 kph, cadence = 30
```

## This is the Console output when blecsc is connected and sends notifications.

```
I (8974) NimBLE: connection established; status=0
I (9024) NimBLE: CSC simulated values: speed = 7 kph, cadence = 97
I (9124) NimBLE: CSC simulated values: speed = 8 kph, cadence = 98
I (9224) NimBLE: CSC simulated values: speed = 9 kph, cadence = 99
I (26454) NimBLE: csc measurement notify state = 1
I (26534) NimBLE: CSC simulated values: speed = 7 kph, cadence = 32
I (26534) NimBLE: GATT procedure initiated: notify;
I (26534) NimBLE: att_handle=3
I (26634) NimBLE: CSC simulated values: speed = 8 kph, cadence = 33
I (26634) NimBLE: GATT procedure initiated: notify;
I (26634) NimBLE: att_handle=3
I (26734) NimBLE: CSC simulated values: speed = 9 kph, cadence = 34
I (26734) NimBLE: GATT procedure initiated: notify;
I (26734) NimBLE: att_handle=3
I (26834) NimBLE: CSC simulated values: speed = 10 kph, cadence = 35
I (26834) NimBLE: GATT procedure initiated: notify;
I (26834) NimBLE: att_handle=3
```

## Note
* NVS support is there for bonding. So, bonding is persistent across reboot.

## 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.
5 changes: 5 additions & 0 deletions examples/bluetooth/nimble/blecsc/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(srcs "main.c"
"gatt_svr.c")

idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS ".")
105 changes: 105 additions & 0 deletions examples/bluetooth/nimble/blecsc/main/blecsc_sens.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#ifndef H_BLECSC_SENSOR_
#define H_BLECSC_SENSOR_

#include "modlog/modlog.h"
#include "nimble/ble.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Cycling Speed and Cadence configuration */
#define GATT_CSC_UUID 0x1816
#define GATT_CSC_MEASUREMENT_UUID 0x2A5B
#define GATT_CSC_FEATURE_UUID 0x2A5C
#define GATT_SENSOR_LOCATION_UUID 0x2A5D
#define GATT_SC_CONTROL_POINT_UUID 0x2A55
/* Device Information configuration */
#define GATT_DEVICE_INFO_UUID 0x180A
#define GATT_MANUFACTURER_NAME_UUID 0x2A29
#define GATT_MODEL_NUMBER_UUID 0x2A24

/*CSC Measurement flags*/
#define CSC_MEASUREMENT_WHEEL_REV_PRESENT 0x01
#define CSC_MEASUREMENT_CRANK_REV_PRESENT 0x02

/* CSC feature flags */
#define CSC_FEATURE_WHEEL_REV_DATA 0x01
#define CSC_FEATURE_CRANK_REV_DATA 0x02
#define CSC_FEATURE_MULTIPLE_SENSOR_LOC 0x04

/* Sensor location enum */
#define SENSOR_LOCATION_OTHER 0
#define SENSOR_LOCATION_TOP_OF_SHOE 1
#define SENSOR_LOCATION_IN_SHOE 2
#define SENSOR_LOCATION_HIP 3
#define SENSOR_LOCATION_FRONT_WHEEL 4
#define SENSOR_LOCATION_LEFT_CRANK 5
#define SENSOR_LOCATION_RIGHT_CRANK 6
#define SENSOR_LOCATION_LEFT_PEDAL 7
#define SENSOR_LOCATION_RIGHT_PEDAL 8
#define SENSOR_LOCATION_FROT_HUB 9
#define SENSOR_LOCATION_REAR_DROPOUT 10
#define SENSOR_LOCATION_CHAINSTAY 11
#define SENSOR_LOCATION_REAR_WHEEL 12
#define SENSOR_LOCATION_REAR_HUB 13
#define SENSOR_LOCATION_CHEST 14
#define SENSOR_LOCATION_SPIDER 15
#define SENSOR_LOCATION_CHAIN_RING 16

/* SC Control Point op codes */
#define SC_CP_OP_SET_CUMULATIVE_VALUE 1
#define SC_CP_OP_START_SENSOR_CALIBRATION 2
#define SC_CP_OP_UPDATE_SENSOR_LOCATION 3
#define SC_CP_OP_REQ_SUPPORTED_SENSOR_LOCATIONS 4
#define SC_CP_OP_RESPONSE 16

/*SC Control Point response values */
#define SC_CP_RESPONSE_SUCCESS 1
#define SC_CP_RESPONSE_OP_NOT_SUPPORTED 2
#define SC_CP_RESPONSE_INVALID_PARAM 3
#define SC_CP_RESPONSE_OP_FAILED 4

/* CSC simulation configuration */
#define CSC_FEATURES (CSC_FEATURE_WHEEL_REV_DATA | \
CSC_FEATURE_CRANK_REV_DATA |\
CSC_FEATURE_MULTIPLE_SENSOR_LOC)

struct ble_csc_measurement_state {
uint32_t cumulative_wheel_rev;
uint16_t last_wheel_evt_time;
uint16_t cumulative_crank_rev;
uint16_t last_crank_evt_time;
};

extern uint16_t csc_measurement_handle;
extern uint16_t csc_control_point_handle;

int gatt_svr_init(struct ble_csc_measurement_state * csc_measurement_state);
int gatt_svr_chr_notify_csc_measurement(uint16_t conn_handle);
void gatt_svr_set_cp_indicate(uint8_t indication_status);

#ifdef __cplusplus
}
#endif

#endif
Loading

0 comments on commit f8b2ab7

Please sign in to comment.