Bluenet
5.7.0
Bluenet, firmware for nRF52 smart home devices
|
nRF51822 specific implementation of the BLEStack More...
#include <cs_Stack.h>
Public Types | |
enum | condition_t { C_STACK_INITIALIZED , C_RADIO_INITIALIZED , C_SERVICES_INITIALIZED } |
typedef fixed_tuple< Service *, MAX_SERVICE_COUNT > | Services_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) |
![]() | |
BaseClass () | |
bool | isInitialized (uint8_t i=0) |
void | setInitialized (uint8_t i=0) |
void | setUninitialized (uint8_t i=0) |
Static Public Member Functions | |
static Stack & | getInstance () |
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... | |
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.
typedef fixed_tuple<Service*, MAX_SERVICE_COUNT> Stack::Services_t |
enum Stack::condition_t |
|
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.
|
private |
|
private |
The destructor shuts down the stack.
TODO: The SoftDevice should be disabled as well.
void Stack::addService | ( | Service * | svc | ) |
Add a service to the stack.
This does not yet add it to the softdevice.
bool Stack::checkCondition | ( | condition_t | condition, |
bool | expectation | ||
) |
void Stack::createCharacteristics | ( | ) |
void Stack::disconnect | ( | ) |
|
inline |
|
inlinestatic |
void Stack::init | ( | ) |
Initialization of the BLE stack.
Performs a series of tasks:
void Stack::initRadio | ( | ) |
Initialization of the radio.
void Stack::initServices | ( | ) |
Register the added services to the softdevice.
void Stack::initSoftdevice | ( | ) |
bool Stack::isConnected | ( | ) |
bool Stack::isConnectedPeripheral | ( | ) |
bool Stack::isDisconnecting | ( | ) |
In case a disconnect has been called, we cannot allow another write or we'll get an Fatal Error 8.
bool Stack::isScanning | ( | ) |
Returns true if currently scanning.
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.
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.
[in] | p_ble_evt | The BLE event. |
[in] | isInterrupt | Whether this function is actually called on interrupt level. |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
Connection request.
On a connection request send it to all services.
|
protected |
|
protected |
|
protected |
|
protected |
Transmission complete event.
Inform all services that transmission was completed in case they have notifications pending
|
protected |
|
private |
void Stack::resetConnectionWatchdog | ( | ) |
Bluenet will automatically disconnect after nothing has been written for some time.
This function will reset that timeout.
void Stack::secReqTimeoutHandler | ( | void * | p_context | ) |
void Stack::setClockSource | ( | nrf_clock_lf_cfg_t | clockSource | ) |
Set initial clock source, not applied unless done before radio init.
|
protected |
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.
|
protected |
void Stack::stopScanning | ( | ) |
Stop scanning for devices.
void Stack::updateConnectionSupervisionTimeout | ( | uint16_t | conSupTimeout_10_ms | ) |
Set and update the preferred connection supervision timeout in units of 10 ms.
|
protected |
Update connection parameters, can be called when already initialized.
void Stack::updateMaxConnectionInterval | ( | uint16_t | connectionInterval_1_25_ms | ) |
Set and update the preferred maximum connection interval in units of 1.25 ms.
void Stack::updateMinConnectionInterval | ( | uint16_t | connectionInterval_1_25_ms | ) |
Set and update the preferred minimum connection interval in units of 1.25 ms.
void Stack::updateSlaveLatency | ( | uint16_t | slaveLatency | ) |
Set and update the preferred slave latency count.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
static |
Maximum number of services (currently set to 5)