Skip navigation
Home > All Places > WICED Studio Bluetooth > WICED Studio Bluetooth Forums > Blog > Authors SrikanthN_96

Objective

This example demonstrates the implementation of different security levels of pairing and the data transfer in a multi-connection environment.

 

 

Overview

This project instantiates a data hub that plays the roles of GAP Central, GAP Peripheral, GATT Client and GATT Server at the same time, and that is able to pair with up to 3 slaves at different security levels. In addition, the device will also be able to pair with a Master, to which it can send the data collected from the slaves or distribute the data from the master to the slaves.

 

Requirements                                                                 

Tool: WICED Studio 5.1 and above, Any BLE Central (Example: CySmart Mobile Application), Serial Terminal (Example: TeraTerm, CoolTerm)

Programming Language: C

Associated Parts: CYW20706

Related Hardware: CYW920706WCDEVAL Board (2 to 4 required)

 

  Block Diagram:

Figure 1: Block Diagram

           

As shown in the abobe block diagram, the Secure Hub can connect with upto 3 slave devices (programmed with hello_sensor project). It in turn serves as a slave for a master device. All the CYW920706 devices will be able to communicate with the PC via PUART interfaces that appear as serial terminal COM Ports in the PC.

 

Firmware Flow of secure_hub Device:

   Figure 2: Firmware Flow

 

APPLICATION_START is the program entry point. Here the peripheral UART is set for displaying Debug Messages. The BT Stack and the corresponding management callback are started using the wiced_bt_stack_init () API.

db_management_cback()  function in secure_hub.c handles the events generated for the BLE Stack. The BTM_ENABLED_EVT indicates that the Bluetooth stack is ready. Upon the reception of this event, we start the Non Connectable High Duty Cycle advertisement. The Timer and GPIO Interrupts are registered. The Timer and GPIO Button Press Interrupt callbacks increment the suffix in the Device Name by 1 and then continues the advertisement with the incremented name.

In the secure_hub and the hello_sensor projects, the user needs to enter the desired security level and IO Capabilities via console. The console accepts inputs via PUART. The 4 security levels supported in this project are

  1. Just Works
  2. MITM
  3. LE Secure Connection
  4. LE Secure Connection with MITM

 

The hello_sensor project starts advertising, once these details are entered. A Scan is initiated via GPIO Interrupt button press on the secure_hub device. The UUID of the service available in the hello_sensor node is verified in the callback, after which the connection is initiated by the secure_hub.

Once a connection is established with the slave, the secure_hub performs a GATT Discovery of the slave using the custom UUIDs of hello_service. The discovery happens in 3 stages:

(i)Service

(ii) Characteristic

(iii) Descriptor

 

GATT DB:

The GATT DB of the hello_sensor device contains the hello service. The hello_service contains two characteristics (Refer hello_sensor/gatt_db.c and hello_sensor/gatt_db.h files)

  • (i)HANDLE_HSENS_SERVICE_CHAR_NOTIFY:

This characteristic has the properties of Notification and Indication. The Bytes to be notified / indicated are typed in the serial terminal of the hello_sensor device and are transmitted as Notification or Indication to the secure_hub.

  • (ii) HANDLE_HSENS_SERVICE_CHAR_BLINK:

This characteristic has the properties of Read and Write. The secure nature of the application is demonstrated using this characteristic. In the gatt_db.c file of the hello_sensor, it can be noticed that the permissions of this characteristic includes these bitmasks: LEGATTDB_PERM_WRITE_REQ| LEGATTDB_PERM_AUTH_WRITABLE. This means that this characteristic can be written only when the link has been paired with MITM (Man in the middle protection).  Security Levels (either BTM_LE_AUTH_REQ_MITM or BTM_LE_AUTH_REQ_SC_MITM  The user has to manually take care of this in the application level. When other security levels are used, a write to this characteristic will result in an “Insufficient Authentication” error. Once this characteristic is  successfully written by a one byte value, the Red LED on the board blinks as many times as the written value.

By the end of the discovery, the secure_hub device stores the Attribute handles of the HANDLE_HSENS_SERVICE_CHAR_BLINK characteristic and the CCCD (Client Characteristic Configuration Descriptor) of the HANDLE_HSENS_SERVICE_CHAR_NOTIFY characteristic. With the attribute handles, the secure_hub device can write to these characteristics descriptors. The CCCD Handle is required, so that the notifications / indications can be enabled / disabled on the hello_sensor slaves. The handles are stored in a global structure g_secure_hub.

  • (iii) The GATT DB of the secure_hub device correspondingly has two characteristics HANDLE_SECURE_HUB_SERVICE_CHAR_NOTIFY and HANDLE_SECURE_HUB_SERVICE_CHAR_LED_WRITE. The former is used to transmit the Notifications / Indications received from the hello_sensor devices to the master (if present any). Such notifications are appended along with the Connection ID of the slave, so that the master can identify the slave that has sent the notification. The latter is used to receive writes from the master, which is in-turn written to the HANDLE_HSENS_SERVICE_CHAR_BLINK characteristic on the hello_sensor slaves.

 

At any instant a new slave may be connected to the secure_hub or a old one may detach itself by pressing the SW1 button the board. A maximum of 3 hello_sensor slaves can be connected to the secure_hub device at a time, in addition to a master.

The secure_hub keeps advertising, until a master has connected to it. It will again start advertising when the master has disconnected from it.

 

Programming and Testing:

  • Install the WICED Studio 5.0 (or above) in your PC. Plug in two BCM920706_EVAL Boards and let the drivers bind to the devices. Make sure the jumpers are in place. Open WICED Studio and set the platform as 20706-A2_Bluetooth. For more details, please refer WICED-20706-BT-Quick-Start-Guide present in Doc folder in the project explorer.
  • Copy the BLE_Seucre_Hub and the hello_sensor folders from the BCM920706_EVAL Board package. In the WICED Studio, right-click the Apps folder in the Project Explorer pane and click Paste. You can see these folders under the Apps folder.
  • Open two windows of Serial Terminal (Example: Teraterm or Coolterm), and bind each of those windows to the PUART COM Ports of each devices. Each device has two COM Ports with consecutive numbers. The COM Port with the bigger number corresponds to the PUART that displays Debug Messages. The one with the smaller number corresponds to the HCI UART. The BaudRate is set to 115200 in the firmware. Make sure the BaudRate in serial terminals is also set to 115200.
  • Create a Make Target for the project by selecting Project -> Make Target -> Create. Paste the following as the entry in the Target Name field:

Secure_hub-BCM920706_P49 UART=COMxx download

Make sure that the Same as the Target Name checkbox is checked. Click OK. Create another target, in addition to the previous one as:

Helo_sensor-BCM920706_P49 UART=COMyy download

(Note: In the above targets, “xx” and “yy” are the HCI COM Port numbers of each of the devices)

  • Double click the secure_hub Target you just created, shown in the Make Target Pane on the right. You can see the Secure_hub program being built and downloaded into one of the boards. (For more details or troubleshooting, please refer the WICED-20706-BT-Quick-Start-Guide)
  • Similarly program the hello_sensor firmware to another device.
  • In the Teraterm, the debug logs can be seen asking to select the security level among these 4 – No Security, MITM (Man in the middle), LE Secure Connection, LE Secure Connection with MITM. Select the appropriate security level on either devices, by entering the corresponding number. Following this, for the security levels that involve MITM, you will have to select the IO Capabilities. The user has to select the MITM.

 

  • The hello_sensor device will start to advertise. In the secure_hub device, press the SW6 button for more than 5 seconds for the scan to start. Once the secure_hub finds the hello_sensor device advertising, it will automatically initiate connection and pairing process. In case of the security levels with MITM, passkey entry might be required, where the user has to enter the passkey displayed by one device as input to another device.
  • The secure_hub device keeps advertising and it can be connected to another central (any BLE App on Smart Phone, say CySmart. )
  • Once the pairing is complete, the secure_hub automatically enables notifications on the hello_sensor device. The user will be able to send notifications by directly typing on the terminal of the hello_sensor byte by byte. The secure_hub displays the notifications received from the hello_sensor and in turn sends them to the Central if present, and if notifications / indications have been enabled by the Central.
  • At any time, another hello_sensor device can be connected the secure_hub by following steps 7,8,9 while preserving the existing connection. A maximum of 3 hello_sensor devices can be connected to the secure hub.
  • The Central can send write requests to the secure_hub which in turn performs write requests to the slave hello_sensor devices connected to it.

Note: When the secure_hub tries to write to a slave with which it has paired with security levels without MITM, it will receive “Insufficient Authentication Error”. Also, when the master tries to write to the secure_hub, when no slaves are connected to the secure_hub, it will result in an “insufficient authorization” error.

Related Documents

Table1 lists all relevant application notes, code examples, knowledge base articles, device datasheets, and Component / user module datasheets.

Table 1: Related Documents

Document

Title

Comment

002-18191

AN218191  - WICED™ Quick Start Guide for BT CYW20706

The  Quick Start Guide can be accessed from Project Explorer -> 20706-A2_Bluetooth -> Doc

002-16535

AN216535 - CYW92070xV3_EVAL Evaluation Board Hardware User Manual

The Hardware User Manual can be accessed from Project Explorer -> 20706-A2_Bluetooth -> Doc

Overview

The project demonstrates a BLE Beacon with battery monitoring, low power operation and power save functionalities.

 

Requirements                                                              

Tool: WICED Studio 5.1 or above, Central device (Eg: CySmart Mobile Application), Serial Terminal (Example: TeraTerm, CoolTerm), Voltage Source, Digital Multimeter

Programming Language: C

Associated Parts: CYW20706

Related Hardware: CYW920706WCDEVAL

 

Project Description:

This project demonstrates the use of the battery monitoring, BLE low power mode and power save feature that is available in CYW20706. The project instantiates a BLE beacon that periodically transmits advertisement packets with the current battery level in the advertisement data. The beacon initially starts high duty non-connectable advertisement and periodically checks the battery level. If the battery level falls below the set shutdown voltage threshold, it switches to low duty non-connectable advertisement. After the low duty advertisement times out, the device enter power save mode. In the power save mode, the chip consumes extremely low power, which can be used to extend the life of the beacon till the battery is replaced. An external interrupt or an internal timer can be used to exit the power save mode.

 

Battery Monitor:


The Battery Monitor driver is used to monitor the battery voltage and decides when to suspend or shutdown the system based upon the threshold values defined by the user. This ensures that the system is able to cleanly shut down and notify the application and/or host before there is not enough power necessary for proper system functionality.

 

The Battery Monitor can be configured by including wiced_hal_batmon.h header file. wiced_hal_batmon_config() configures the internal ADC to monitor the voltage from one of the specified ADC inputs. In this project, an external voltage source is connected to P37 which emulates the battery input to the system. The batmon driver automatically calculates the battery level of the system depending on the full voltage and empty voltage specified by the user. It is to be noted that the Battery Monitor relies on polling to check on the status of the battery. The poll timer can be configured to control the rate of polling the ADC.

 

BLE Low Power Mode:

 

In order to make use of the low power modes available in the CYW20706, you will have to include the wiced_power_save.h header file. A single function call to the wiced_sleep_config() function will enable low power operation of CYW20706. It configures the device to sleep in between advertisement intervals, thereby reducing the average current consumption.

 

Power Save Mode:

 

The power save mode is the most efficient power mode available. The device can be pushed to this mode asynchronously from the application. In this project, the system is pushed into power save mode once the voltage falls below the shutdown voltage and the low duty advertisement times out. The system can be configured to wake up by a timeout or by a manual interrupt. In interrupt mode, a GPIO needs to be configured to act as an interrupt. In timeout mode, an internal low power oscillator is configured to wake up the system after a specific time period. Since the low power oscillator is active in the timeout mode, the timeout mode consumes slightly higher power than interrupt mode. It is to be noted that the application will be restarted on exiting out of the power save mode.


Procedure:

 

Below is the flowchart of the project,

      

   To demonstrate the project, work through the following steps,

  1. Before programming the board, make sure the SW5 DIP switches are in the following configuration:
    1. Switch 1: OFF
    2. Switch 2: ON
    3. Switch 3: ON
    4. Switch 4: OFF
    5. Switch 5: OFF
    6. Switch 6: OFF

This is done so that LEDs D9 and D10 can be turned off to measure the power consumption of the chip.

  1. Connect the external voltage source to P37 (J22 – 5) and set the voltage source to the configured full voltage. (3.3V)
  2. Program the board and open the COM port of PUART. You should now see the debug prints.
  3. Batmon will be polled periodically and high duty ADV will be started. You can now see the ADV packets in a central device. The advertisement data will contain the battery level under Manufacturer Specific section.
  4. Change the voltage on the voltage source and observe the updated value in the advertisement packet.
  5. Reduce the voltage of the source below the shutdown voltage (1.7V) and the system will now start low duty ADV. Once low duty ADV times out, the system goes into power save.
  6. Wake up the system either by interrupt or the device will wake up when the low power oscillator times out. In order to enable/disable the timeout mode, uncomment/comment out the macro TIMEOUT_MODE in the low_power_beacon.h file.

 

Project Configuration:

The various parameters configured in the project are as follows:

  • Fast ADV interval and timeout: 100ms and 0(infinite)
  • Slow ADV interval and timeout: 1280ms and 10 seconds
  • Tx power: -12 dBm
  • LPO timer period (wakeup from power save mode): 10 seconds

Current Measurement:

In order to measure the current during various modes, you will need a digital multimeter.

  • The total current can be measured by connecting an ammeter at jumper J17.
  • The core current can be measured at jumper J9.
  • The IO domain current can be measured at jumper J3.
  • The 2.5V LDO regulator current can be measured at jumper J10.

 

The below table lists the average currents measured in different low power modes.

 

                                          

 

Active Mode (Wiced sleep is disabled)

Wiced Sleep with High Duty ADV

Wiced Sleep with Low Duty ADV

Power Save mode with LPO

Power Save mode without LPO (only LHL)

WICED Power Mode

Active

PDS

PDS

Timed Wake

HID-off

Average Current (sum of current at   J3+J9+J10)

6.98mA

1.6mA

0.5mA

5.2uA

1.2uA

Average Current measured at J177.2mA1.75mA0.6mA5.2uA1.2uA

 

Note: The current measured at jumper J17 includes current consumed by CYW2070x, Serial Flash, on-board LEDs and few other external peripherals. The sum of currents measured at J3, J9, J10 indicates the current consumed only by CYW2070x chip.

 

With a current probe, you can observe the current profile as well. The current peaks during transmission of the ADV packet can be observed.

  

The above figure is a graphical representation of the advertisement current profile when the CYW20706 is configured for low power. The device enters low power mode in between advertisement intervals where it consumes a few uA of current. It wakes up a few milliseconds before the advertisement interval, in the above figure this number is around 3.4ms. The device then transmits the ADV_IND packet into the air. Once it has sent the advertisement packets on all the 3 channels (unless configured otherwise), it will perform the required post-advertisement processing, in this case it is about 6ms and after this activity device goes back to low power mode.

 

Expected Results:

Once you have followed the steps mentioned above, you should be able to see the advertisement packets in CySmart or any BLE scanner. You can observe the dynamically changing ADV packet to note the battery level as show in the figure below,

 

 

 

Related Documents

Table1 lists all relevant application notes, code examples, knowledge base articles, device datasheets, and Component / user module datasheets.

Table 1: Related Documents

Document

Title

Comment

002-18191

AN218191  - WICED™ Quick Start Guide for BT CYW20706

The  Quick Start Guide can be accessed from Project Explorer -> 20706-A2_Bluetooth -> Doc

002-16535

AN216535 - CYW92070xV3_EVAL Evaluation Board Hardware User Manual

The Hardware User Manual can be accessed from Project Explorer -> 20706-A2_Bluetooth -> Doc

 

                                          

 

Active Mode (Wiced sleep is disabled)

Wiced Sleep with High Duty ADV

Wiced Sleep with Low Duty ADV

Power Save mode with LPO

Power Save mode without LPO (only LHL)

WICED Power Mode

Active

PDS

PDS

Timed Wake

HID-off

Average Current (sum of current at   J3+J9+J10)

6.98mA

1.6mA

0.5mA

5.2uA

1.2uA

Average Current measured at J17

7.2mA

1.75mA

0.6mA

5.2uA

1.2uA

Filter Blog

By date:
By tag: