ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
Advertisement tasks

Bluetooth Low Energy advertising. More...

Files

file  ruuvi_task_advertisement.c
 
file  ruuvi_task_advertisement.h
 

Data Structures

struct  rt_adv_init_t
 Initial configuration for advertisement. PHY will be transferred to GATT.
More...
 

Macros

#define SCAN_RSP_NAME_MAX_LEN   (11U)
 Longer name gets truncated when advertised with UUID. More...
 
#define RT_ADV_SCAN_WINDOW_MS   (7000U)
 Window of one scan per channel. Scan takes this * num_channels time. More...
 
#define RT_ADV_SCAN_INTERVAL_MS   (7000U)
 

Functions

rd_status_t rt_adv_init (rt_adv_init_t *const adv_init_settings)
 Initializes data advertising. More...
 
rd_status_t rt_adv_uninit (void)
 Uninitializes data advertising. More...
 
rd_status_t rt_adv_stop (void)
 Stops advertising. This is relevant only if the message was on repeat. More...
 
rd_status_t rt_adv_send_data (ri_comm_message_t *const msg)
 Send given message as a BLE advertisement. More...
 
rd_status_t rt_adv_connectability_set (const bool enable, const char *const device_name)
 Start advertising BLE GATT connection. More...
 
bool rt_adv_is_init (void)
 check if advertisement is initialized More...
 
rd_status_t rt_adv_scan_start (const ri_comm_evt_handler_fp_t on_evt)
 Start scanning BLE advertisements. More...
 
rd_status_t rt_adv_scan_stop (void)
 Abort scanning. More...
 

Detailed Description

Bluetooth Low Energy advertising.

Macro Definition Documentation

◆ RT_ADV_SCAN_INTERVAL_MS

#define RT_ADV_SCAN_INTERVAL_MS   (7000U)

Definition at line 55 of file ruuvi_task_advertisement.h.

◆ RT_ADV_SCAN_WINDOW_MS

#define RT_ADV_SCAN_WINDOW_MS   (7000U)

Window of one scan per channel. Scan takes this * num_channels time.

Interval of one scan within a window. Can be at most equal to scan window. *‍/

Definition at line 54 of file ruuvi_task_advertisement.h.

◆ SCAN_RSP_NAME_MAX_LEN

#define SCAN_RSP_NAME_MAX_LEN   (11U)

Longer name gets truncated when advertised with UUID.

Definition at line 51 of file ruuvi_task_advertisement.h.

Function Documentation

◆ rt_adv_connectability_set()

rd_status_t rt_adv_connectability_set ( const bool  enable,
const char *const  device_name 
)

Start advertising BLE GATT connection.

This function configures the primary advertisement to be SCANNABLE_CONNECTABLE and sets up a scan response which has given device name (max 10 characters + NULL) and UUID of Nordic UART Service.

Be sure to configure the GATT before calling this function, as behaviour is undefined if someone tries to connect to tag while GATT is not configured.

Parameters
[in]enabletrue to enable connectability, false to disable.
[in]device_nameNULL-terminated string representing device name, max 10 Chars + NULL.
Return values
RD_SUCCESSif operation was finished as expected.
RD_ERROR_NULLif name is NULL and trying to enable the scan response
RD_ERROR_INVALID_STATEif advertising isn't initialized or started.
RD_ERROR_INVALID_LENGTHif name size exceeds 10 bytes + NULL
Returns
error code from stack on other error.

◆ rt_adv_init()

rd_status_t rt_adv_init ( rt_adv_init_t *const  adv_init_settings)

Initializes data advertising.

The function setups advertisement interval, advertisement power, advertisement type, manufacturer ID for manufacturer specific data according to constants in application_config.h and ruuvi_boards.h.

It also configures a callback to be executed after advertisement for internal use. After calling this function advertisement data can be queued into advertisement buffer.

Parameters
[in,out]adv_init_settingsInput: Desired setup. Output: Configured setup.
Return values
RD_SUCCESSon success.
RD_ERROR_INVALID_STATEif advertising is already initialized.
RD_ERROR_INVALID_PARAMif configuration constant is invalid. Not initialized.

◆ rt_adv_is_init()

bool rt_adv_is_init ( void  )

check if advertisement is initialized

Returns
true if advertisement is initialized, false otherwise.

◆ rt_adv_scan_start()

rd_status_t rt_adv_scan_start ( const ri_comm_evt_handler_fp_t  on_evt)

Start scanning BLE advertisements.

This is non-blocking, you'll need to handle incoming events.

PHY to be scanned is determined by radio initialization. If you have selected 2 MBit / s PHY, primary advertisements are scanned at 1 Mbit / s and secondary extended advertisement can be scanned at at 2 MBit / s.

Scan is done at 7000 ms window and interval, this consumes a lot of power and may collapse a coin cell battery.

Events are:

  • on_evt(RI_COMM_RECEIVED, scan, sizeof(ri_adv_scan_t));
  • on_evt(RI_COMM_TIMEOUT, NULL, 0);
Parameters
[in]on_evtEvent handler for scan results.
Return values
RD_SUCCESSScanning was started.
RD_ERROR_INVALID_STATEAdvertising isn't initialized.
Returns
error code from stack on other error.
Note
Scanning is stopped on timeout, you can restart the scan on event handler.
Warning
Event handler is called in interrupt context.

◆ rt_adv_scan_stop()

rd_status_t rt_adv_scan_stop ( void  )

Abort scanning.

After calling this function scanning is immediately stopped.

◆ rt_adv_send_data()

rd_status_t rt_adv_send_data ( ri_comm_message_t *const  msg)

Send given message as a BLE advertisement.

This function configures the primary advertisement packet with the flags and manufacturer specific data. Payload of the msg will be sent as the manufacturer specific data payload. Manufacturer ID is defined by RUUVI_BOARD_BLE_MANUFACTURER_ID in ruuvi_boards.h.

If the device is connectable, call

rd_adv_connectability_set

to setup the scan response and flags to advertise connectability.

Call rt_adv_stop to stop advertisements on repeat.

Parameters
[in]msgmessage to be sent as manufacturer specific data payload.
Return values
RD_ERROR_NULLif msg is NULL.
RD_ERROR_INVALID_STATEif advertising isn't initialized or started.
RD_ERROR_DATA_SIZEif payload size is larger than 24 bytes.
Returns
error code from stack on other error.

◆ rt_adv_stop()

rd_status_t rt_adv_stop ( void  )

Stops advertising. This is relevant only if the message was on repeat.

Return values
RD_SUCCESSon success
Returns
error code from stack on error

◆ rt_adv_uninit()

rd_status_t rt_adv_uninit ( void  )

Uninitializes data advertising.

Can be called even if advertising was not initialized. Clears previous advertisement data if there was any.

Return values
RD_SUCCESSon success
Returns
error code from stack on error