Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Stack.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <ble/cs_Nordic.h>
13#include <ble/cs_Service.h>
14#include <ble/cs_UUID.h>
15#include <cfg/cs_Config.h>
16#include <cfg/cs_Strings.h>
17#include <common/cs_BaseClass.h>
18#include <common/cs_Tuple.h>
19#include <common/cs_Types.h>
20#include <drivers/cs_Timer.h>
21#include <third/std/function.h>
22#include <util/cs_BleError.h>
23
24#include <string>
25
27
32class Service;
33
41class Stack : public BaseClass<3> {
42public:
43 static Stack& getInstance() {
44 static Stack instance;
45 return instance;
46 }
47
49 static const uint8_t MAX_SERVICE_COUNT = 5;
51
53
54protected:
56
57 // might want to change this to a linked list or something that
58 // we can loop over but doesn't allocate more space than needed
60
61 nrf_clock_lf_cfg_t _clockSource;
62 ble_gap_conn_params_t _connectionParams;
63
64 bool _scanning = false;
65
66 uint16_t _connectionHandle = BLE_CONN_HANDLE_INVALID;
68
70 app_timer_id_t _connectionWatchdogTimerId = NULL;
72
73 uint8_t _scanBuffer[31]; // Same size as buffer in cs_stack_scan_t.
74 ble_data_t _scanBufferStruct = {_scanBuffer, sizeof(_scanBuffer)};
75
76public:
91 void init();
92
96 void initRadio();
97
99
101
106
108
110
116
118
120 void setClockSource(nrf_clock_lf_cfg_t clockSource);
121
123 void updateMinConnectionInterval(uint16_t connectionInterval_1_25_ms);
124
126 void updateMaxConnectionInterval(uint16_t connectionInterval_1_25_ms);
127
129 void updateSlaveLatency(uint16_t slaveLatency);
130
132 void updateConnectionSupervisionTimeout(uint16_t conSupTimeout_10_ms);
133
138 void addService(Service* svc);
139
144
152
156
160
171 void onBleEvent(const ble_evt_t* p_ble_evt);
172
181 void onBleEventInterrupt(const ble_evt_t* p_ble_evt, bool isInterrupt);
182
183 void secReqTimeoutHandler(void* p_context);
185
186 bool checkCondition(condition_t condition, bool expectation);
187
188protected:
191
192 void onConnect(const ble_evt_t* p_ble_evt);
193 void onDisconnect(const ble_evt_t* p_ble_evt);
194 void onGapTimeout(uint8_t src);
195
197
202 void onIncomingConnected(const ble_evt_t* p_ble_evt);
203 void onIncomingDisconnected(const ble_evt_t* p_ble_evt);
204
205 void onMemoryRequest(uint16_t connectionHandle);
206 void onMemoryRelease(uint16_t connectionHandle);
207
208 void onWrite(uint16_t connectionHandle, const ble_gatts_evt_write_t& writeEvt);
209
214 void onTxComplete(const ble_evt_t* p_ble_evt);
215
218
219private:
227 Stack(Stack const&);
228 void operator=(Stack const&);
229
236};
BaseClass.
Definition: cs_BaseClass.h:15
Base class for a BLE service.
Definition: cs_Service.h:26
nRF51822 specific implementation of the BLEStack
Definition: cs_Stack.h:41
app_timer_id_t _connectionWatchdogTimerId
Definition: cs_Stack.h:70
bool checkCondition(condition_t condition, bool expectation)
Services_t _services
Definition: cs_Stack.h:59
void startScanning()
Start scanning for devices.
Stack()
Constructor of the BLE stack on the NRF5 series.
void stopConnectionWatchdog()
bool isDisconnecting()
In case a disconnect has been called, we cannot allow another write or we'll get an Fatal Error 8.
condition_t
Definition: cs_Stack.h:52
@ C_SERVICES_INITIALIZED
Definition: cs_Stack.h:52
@ C_STACK_INITIALIZED
Definition: cs_Stack.h:52
@ C_RADIO_INITIALIZED
Definition: cs_Stack.h:52
bool _connectionWatchdogRunning
Definition: cs_Stack.h:71
bool _disconnectingInProgress
Definition: cs_Stack.h:55
void updateConnParams()
Update connection parameters, can be called when already initialized.
~Stack()
The destructor shuts down the stack.
void onDisconnect(const ble_evt_t *p_ble_evt)
void onConnectionTimeout()
void createCharacteristics()
bool isConnected()
void onTxComplete(const ble_evt_t *p_ble_evt)
Transmission complete event.
uint8_t _scanBuffer[31]
Definition: cs_Stack.h:73
nrf_clock_lf_cfg_t _clockSource
Definition: cs_Stack.h:61
void onMemoryRelease(uint16_t connectionHandle)
void resetConnectionWatchdog()
Bluenet will automatically disconnect after nothing has been written for some time.
void updateMaxConnectionInterval(uint16_t connectionInterval_1_25_ms)
Set and update the preferred maximum connection interval in units of 1.25 ms.
uint16_t _connectionHandle
Definition: cs_Stack.h:66
app_timer_t _connectionWatchdogTimerData
Definition: cs_Stack.h:69
void onIncomingConnected(const ble_evt_t *p_ble_evt)
Connection request.
void stopScanning()
Stop scanning for devices.
void updateConnectionSupervisionTimeout(uint16_t conSupTimeout_10_ms)
Set and update the preferred connection supervision timeout in units of 10 ms.
bool _connectionIsOutgoing
Definition: cs_Stack.h:67
static Stack & getInstance()
Definition: cs_Stack.h:43
void setClockSource(nrf_clock_lf_cfg_t clockSource)
Set initial clock source, not applied unless done before radio init.
void init()
Initialization of the BLE stack.
void onWrite(uint16_t connectionHandle, const ble_gatts_evt_write_t &writeEvt)
void disconnect()
void updateMinConnectionInterval(uint16_t connectionInterval_1_25_ms)
Set and update the preferred minimum connection interval in units of 1.25 ms.
bool isConnectedPeripheral()
void secReqTimeoutHandler(void *p_context)
fixed_tuple< Service *, MAX_SERVICE_COUNT > Services_t
Definition: cs_Stack.h:50
ble_data_t _scanBufferStruct
Definition: cs_Stack.h:74
bool isScanning()
Returns true if currently scanning.
void operator=(Stack const &)
void onGapTimeout(uint8_t src)
bool _scanning
Definition: cs_Stack.h:64
void addService(Service *svc)
Add a service to the stack.
void onConnect(const ble_evt_t *p_ble_evt)
void onIncomingDisconnected(const ble_evt_t *p_ble_evt)
static const uint8_t MAX_SERVICE_COUNT
Maximum number of services (currently set to 5)
Definition: cs_Stack.h:49
void onBleEvent(const ble_evt_t *p_ble_evt)
Function that handles BLE events.
void initSoftdevice()
void startConnectionWatchdog()
Stack(Stack const &)
void initRadio()
Initialization of the radio.
void updateSlaveLatency(uint16_t slaveLatency)
Set and update the preferred slave latency count.
void onBleEventInterrupt(const ble_evt_t *p_ble_evt, bool isInterrupt)
Function that handles BLE events on interrupt level.
uint16_t getConnectionHandle()
Definition: cs_Stack.h:117
ble_gap_conn_params_t _connectionParams
Definition: cs_Stack.h:62
void initServices()
Register the added services to the softdevice.
void onMemoryRequest(uint16_t connectionHandle)
A fixed tuple is a vector with a templated type and a reserved capacity.
Definition: cs_Tuple.h:31
BLE service.