Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
Stack Class Reference

nRF51822 specific implementation of the BLEStack More...

#include <cs_Stack.h>

Inheritance diagram for Stack:
Collaboration diagram for Stack:

Public Types

enum  condition_t { C_STACK_INITIALIZED , C_RADIO_INITIALIZED , C_SERVICES_INITIALIZED }
 
typedef fixed_tuple< Service *, MAX_SERVICE_COUNTServices_t
 

Public Member Functions

void init ()
 Initialization of the BLE stack. More...
 
void initRadio ()
 Initialization of the radio. More...
 
void initSoftdevice ()
 
void createCharacteristics ()
 
bool isDisconnecting ()
 In case a disconnect has been called, we cannot allow another write or we'll get an Fatal Error 8. More...
 
bool isConnected ()
 
bool isConnectedPeripheral ()
 
void resetConnectionWatchdog ()
 Bluenet will automatically disconnect after nothing has been written for some time. More...
 
uint16_t getConnectionHandle ()
 
void setClockSource (nrf_clock_lf_cfg_t clockSource)
 Set initial clock source, not applied unless done before radio init. More...
 
void updateMinConnectionInterval (uint16_t connectionInterval_1_25_ms)
 Set and update the preferred minimum connection interval in units of 1.25 ms. More...
 
void updateMaxConnectionInterval (uint16_t connectionInterval_1_25_ms)
 Set and update the preferred maximum connection interval in units of 1.25 ms. More...
 
void updateSlaveLatency (uint16_t slaveLatency)
 Set and update the preferred slave latency count. More...
 
void updateConnectionSupervisionTimeout (uint16_t conSupTimeout_10_ms)
 Set and update the preferred connection supervision timeout in units of 10 ms. More...
 
void addService (Service *svc)
 Add a service to the stack. More...
 
void initServices ()
 Register the added services to the softdevice. More...
 
void startScanning ()
 Start scanning for devices. More...
 
void stopScanning ()
 Stop scanning for devices. More...
 
bool isScanning ()
 Returns true if currently scanning. More...
 
void onBleEvent (const ble_evt_t *p_ble_evt)
 Function that handles BLE events. More...
 
void onBleEventInterrupt (const ble_evt_t *p_ble_evt, bool isInterrupt)
 Function that handles BLE events on interrupt level. More...
 
void secReqTimeoutHandler (void *p_context)
 
void disconnect ()
 
bool checkCondition (condition_t condition, bool expectation)
 
- Public Member Functions inherited from BaseClass< 3 >
 BaseClass ()
 
bool isInitialized (uint8_t i=0)
 
void setInitialized (uint8_t i=0)
 
void setUninitialized (uint8_t i=0)
 

Static Public Member Functions

static StackgetInstance ()
 

Static Public Attributes

static const uint8_t MAX_SERVICE_COUNT = 5
 Maximum number of services (currently set to 5) More...
 

Protected Member Functions

void updateConnParams ()
 Update connection parameters, can be called when already initialized. More...
 
void onConnect (const ble_evt_t *p_ble_evt)
 
void onDisconnect (const ble_evt_t *p_ble_evt)
 
void onGapTimeout (uint8_t src)
 
void onConnectionTimeout ()
 
void onIncomingConnected (const ble_evt_t *p_ble_evt)
 Connection request. More...
 
void onIncomingDisconnected (const ble_evt_t *p_ble_evt)
 
void onMemoryRequest (uint16_t connectionHandle)
 
void onMemoryRelease (uint16_t connectionHandle)
 
void onWrite (uint16_t connectionHandle, const ble_gatts_evt_write_t &writeEvt)
 
void onTxComplete (const ble_evt_t *p_ble_evt)
 Transmission complete event. More...
 
void startConnectionWatchdog ()
 
void stopConnectionWatchdog ()
 

Protected Attributes

bool _disconnectingInProgress = false
 
Services_t _services
 
nrf_clock_lf_cfg_t _clockSource
 
ble_gap_conn_params_t _connectionParams
 
bool _scanning = false
 
uint16_t _connectionHandle = BLE_CONN_HANDLE_INVALID
 
bool _connectionIsOutgoing = false
 
app_timer_t _connectionWatchdogTimerData
 
app_timer_id_t _connectionWatchdogTimerId = NULL
 
bool _connectionWatchdogRunning = false
 
uint8_t _scanBuffer [31]
 
ble_data_t _scanBufferStruct = {_scanBuffer, sizeof(_scanBuffer)}
 

Private Member Functions

 Stack ()
 Constructor of the BLE stack on the NRF5 series. More...
 
 Stack (Stack const &)
 
void operator= (Stack const &)
 
 ~Stack ()
 The destructor shuts down the stack. More...
 

Detailed Description

nRF51822 specific implementation of the BLEStack

The Stack class is a direct descendant from BLEStack. It is implemented as a singleton, such that it can only be allocated once and it can be reached from everywhere in the code, especially in interrupt handlers. However, please, if an object depends on it, try to make this dependency explicit, and use this stack object as an argument w.r.t. this object. This makes dependencies traceable for the user.

Member Typedef Documentation

◆ Services_t

Member Enumeration Documentation

◆ condition_t

Enumerator
C_STACK_INITIALIZED 
C_RADIO_INITIALIZED 
C_SERVICES_INITIALIZED 

Constructor & Destructor Documentation

◆ Stack() [1/2]

Stack::Stack ( )
private

Constructor of the BLE stack on the NRF5 series.

The constructor sets up very little! Only enough memory is allocated. Also there are a lot of defaults set. However, the SoftDevice is not enabled yet, nor any function on the SoftDevice is called. This is done in the init() function.

◆ Stack() [2/2]

Stack::Stack ( Stack const &  )
private

◆ ~Stack()

Stack::~Stack ( )
private

The destructor shuts down the stack.

TODO: The SoftDevice should be disabled as well.

Member Function Documentation

◆ addService()

void Stack::addService ( Service svc)

Add a service to the stack.

This does not yet add it to the softdevice.

◆ checkCondition()

bool Stack::checkCondition ( condition_t  condition,
bool  expectation 
)

◆ createCharacteristics()

void Stack::createCharacteristics ( )

◆ disconnect()

void Stack::disconnect ( )

◆ getConnectionHandle()

uint16_t Stack::getConnectionHandle ( )
inline

◆ getInstance()

static Stack & Stack::getInstance ( )
inlinestatic

◆ init()

void Stack::init ( )

Initialization of the BLE stack.

Performs a series of tasks:

  • disables softdevice if it is currently enabled
  • enables softdevice with own clock and assertion handler
  • enable service changed characteristic for S110
  • disable automatic address recycling for S110
  • set BLE device name
  • set appearance (e.g. used in GUIs to interface with BLE devices)
  • set connection parameters
  • set Tx power level
  • set the callback for BLE events (if we use Source/sd_common/softdevice_handler.c in Nordic's SDK)

◆ initRadio()

void Stack::initRadio ( )

Initialization of the radio.

◆ initServices()

void Stack::initServices ( )

Register the added services to the softdevice.

◆ initSoftdevice()

void Stack::initSoftdevice ( )

◆ isConnected()

bool Stack::isConnected ( )

◆ isConnectedPeripheral()

bool Stack::isConnectedPeripheral ( )

◆ isDisconnecting()

bool Stack::isDisconnecting ( )

In case a disconnect has been called, we cannot allow another write or we'll get an Fatal Error 8.

◆ isScanning()

bool Stack::isScanning ( )

Returns true if currently scanning.

◆ onBleEvent()

void Stack::onBleEvent ( const ble_evt_t *  p_ble_evt)

Function that handles BLE events.

A BLE event is generated, these can be connect or disconnect events. It can also be RSSI values that changed, or an authorization request. Not all event structures are exactly the same over the different SoftDevices, so there are some defines for minor changes. And of course, e.g. the S110 softdevice cannot listen to advertisements at all, so BLE_GAP_EVT_ADV_REPORT is entirely disabled.

TODO: Currently we loop through every service and send e.g. BLE_GATTS_EVT_WRITE only when some handle matches. It is faster to set up maps from handles to directly the right function.

◆ onBleEventInterrupt()

void Stack::onBleEventInterrupt ( const ble_evt_t *  p_ble_evt,
bool  isInterrupt 
)

Function that handles BLE events on interrupt level.

These need to be handled quickly and not use any class state.

Parameters
[in]p_ble_evtThe BLE event.
[in]isInterruptWhether this function is actually called on interrupt level.

◆ onConnect()

void Stack::onConnect ( const ble_evt_t *  p_ble_evt)
protected

◆ onConnectionTimeout()

void Stack::onConnectionTimeout ( )
protected

◆ onDisconnect()

void Stack::onDisconnect ( const ble_evt_t *  p_ble_evt)
protected

◆ onGapTimeout()

void Stack::onGapTimeout ( uint8_t  src)
protected

◆ onIncomingConnected()

void Stack::onIncomingConnected ( const ble_evt_t *  p_ble_evt)
protected

Connection request.

On a connection request send it to all services.

◆ onIncomingDisconnected()

void Stack::onIncomingDisconnected ( const ble_evt_t *  p_ble_evt)
protected

◆ onMemoryRelease()

void Stack::onMemoryRelease ( uint16_t  connectionHandle)
protected

◆ onMemoryRequest()

void Stack::onMemoryRequest ( uint16_t  connectionHandle)
protected

◆ onTxComplete()

void Stack::onTxComplete ( const ble_evt_t *  p_ble_evt)
protected

Transmission complete event.

Inform all services that transmission was completed in case they have notifications pending

◆ onWrite()

void Stack::onWrite ( uint16_t  connectionHandle,
const ble_gatts_evt_write_t &  writeEvt 
)
protected

◆ operator=()

void Stack::operator= ( Stack const &  )
private

◆ resetConnectionWatchdog()

void Stack::resetConnectionWatchdog ( )

Bluenet will automatically disconnect after nothing has been written for some time.

This function will reset that timeout.

◆ secReqTimeoutHandler()

void Stack::secReqTimeoutHandler ( void *  p_context)

◆ setClockSource()

void Stack::setClockSource ( nrf_clock_lf_cfg_t  clockSource)

Set initial clock source, not applied unless done before radio init.

◆ startConnectionWatchdog()

void Stack::startConnectionWatchdog ( )
protected

◆ startScanning()

void Stack::startScanning ( )

Start scanning for devices.

Only call the following functions with a S120 or S130 device that can play a central role. The following functions are probably the ones your recognize from implementing BLE functionality on Android or iOS if you are a smartphone developer.

◆ stopConnectionWatchdog()

void Stack::stopConnectionWatchdog ( )
protected

◆ stopScanning()

void Stack::stopScanning ( )

Stop scanning for devices.

◆ updateConnectionSupervisionTimeout()

void Stack::updateConnectionSupervisionTimeout ( uint16_t  conSupTimeout_10_ms)

Set and update the preferred connection supervision timeout in units of 10 ms.

◆ updateConnParams()

void Stack::updateConnParams ( )
protected

Update connection parameters, can be called when already initialized.

◆ updateMaxConnectionInterval()

void Stack::updateMaxConnectionInterval ( uint16_t  connectionInterval_1_25_ms)

Set and update the preferred maximum connection interval in units of 1.25 ms.

◆ updateMinConnectionInterval()

void Stack::updateMinConnectionInterval ( uint16_t  connectionInterval_1_25_ms)

Set and update the preferred minimum connection interval in units of 1.25 ms.

◆ updateSlaveLatency()

void Stack::updateSlaveLatency ( uint16_t  slaveLatency)

Set and update the preferred slave latency count.

Member Data Documentation

◆ _clockSource

nrf_clock_lf_cfg_t Stack::_clockSource
protected

◆ _connectionHandle

uint16_t Stack::_connectionHandle = BLE_CONN_HANDLE_INVALID
protected

◆ _connectionIsOutgoing

bool Stack::_connectionIsOutgoing = false
protected

◆ _connectionParams

ble_gap_conn_params_t Stack::_connectionParams
protected

◆ _connectionWatchdogRunning

bool Stack::_connectionWatchdogRunning = false
protected

◆ _connectionWatchdogTimerData

app_timer_t Stack::_connectionWatchdogTimerData
protected

◆ _connectionWatchdogTimerId

app_timer_id_t Stack::_connectionWatchdogTimerId = NULL
protected

◆ _disconnectingInProgress

bool Stack::_disconnectingInProgress = false
protected

◆ _scanBuffer

uint8_t Stack::_scanBuffer[31]
protected

◆ _scanBufferStruct

ble_data_t Stack::_scanBufferStruct = {_scanBuffer, sizeof(_scanBuffer)}
protected

◆ _scanning

bool Stack::_scanning = false
protected

◆ _services

Services_t Stack::_services
protected

◆ MAX_SERVICE_COUNT

const uint8_t Stack::MAX_SERVICE_COUNT = 5
static

Maximum number of services (currently set to 5)


The documentation for this class was generated from the following file: