Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_BleCentral.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Mar 26, 2021
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7
8#pragma once
9
10#include <ble/cs_Nordic.h>
11#include <ble/cs_UUID.h>
12#include <common/cs_Types.h>
13#include <events/cs_Event.h>
16
28public:
30 static BleCentral instance;
31 return instance;
32 }
33
40 void init();
41
49 cs_ret_code_t connect(const device_address_t& address, uint16_t timeoutMs = 3000);
50
60
75 cs_ret_code_t discoverServices(const UUID* uuids, uint8_t uuidCount);
76
85 cs_ret_code_t read(uint16_t handle);
86
99 cs_ret_code_t write(uint16_t handle, const uint8_t* data, uint16_t len);
100
110 cs_ret_code_t writeNotificationConfig(uint16_t cccdHandle, bool enableNotifications);
111
120
125
129 bool isBusy();
130
131private:
132 enum class Operation : uint8_t {
133 NONE,
134 CONNECT_CLEARANCE, // Waiting for clearance to connect.
135 CONNECT,
137 DISCOVERY,
138 READ,
139 WRITE
140 };
141
144 void operator=(BleCentral const&);
145
147
148 uint16_t _timeoutMs;
149
154
158 uint16_t _bufDataSize = 0;
159
160 uint16_t _connectionHandle = BLE_CONN_HANDLE_INVALID;
161
163 uint16_t _currentHandle = BLE_GATT_HANDLE_INVALID;
164
165 ble_db_discovery_t _discoveryModule;
166
167#if NORDIC_SDK_VERSION > 15
168 ble_db_discovery_init_t _discoveryInit;
169
170 nrf_ble_gq_t* _queue;
171#endif
172
176 uint16_t _mtu = BLE_GATT_ATT_MTU_DEFAULT;
177
182
189
193 cs_ret_code_t connectWithClearance(const device_address_t& address, uint16_t timeoutMs = 3000);
194
198 cs_ret_code_t nextWrite(uint16_t handle, uint16_t offset);
199
204 void finalizeOperation(Operation operation, cs_ret_code_t retCode);
205 void finalizeOperation(Operation operation, uint8_t* data, uint8_t dataSize);
207
211 void onGapEvent(uint16_t evtId, const ble_gap_evt_t& event);
212 void onGattCentralEvent(uint16_t evtId, const ble_gattc_evt_t& event);
213
215 void onConnect(uint16_t connectionHandle, const ble_gap_evt_connected_t& event);
216 void onDisconnect(const ble_gap_evt_disconnected_t& event);
217 void onGapTimeout(const ble_gap_evt_timeout_t& event);
218
219 void onMtu(uint16_t gattStatus, const ble_gattc_evt_exchange_mtu_rsp_t& event);
220 void onRead(uint16_t gattStatus, const ble_gattc_evt_read_rsp_t& event);
221 void onWrite(uint16_t gattStatus, const ble_gattc_evt_write_rsp_t& event);
222 void onNotification(uint16_t gattStatus, const ble_gattc_evt_hvx_t& event);
223
224public:
228 void onBleEvent(const ble_evt_t* event);
229
233 void onDiscoveryEvent(ble_db_discovery_evt_t& event);
234
238 void handleEvent(event_t& event);
239};
Class that enables you to connect to a device, and perform write or read operations.
Definition: cs_BleCentral.h:27
cs_ret_code_t writeNotificationConfig(uint16_t cccdHandle, bool enableNotifications)
Performs a write() with the value to enable or disable notifications.
void onMtu(uint16_t gattStatus, const ble_gattc_evt_exchange_mtu_rsp_t &event)
void onGattCentralEvent(uint16_t evtId, const ble_gattc_evt_t &event)
void onWrite(uint16_t gattStatus, const ble_gattc_evt_write_rsp_t &event)
void onDisconnect(const ble_gap_evt_disconnected_t &event)
void onBleEvent(const ble_evt_t *event)
Internal usage: to be called on BLE events.
cs_ret_code_t discoverServices(const UUID *uuids, uint8_t uuidCount)
Discover services.
TYPIFY(CONFIG_SCAN_INTERVAL_625US) _scanInterval
Scan setting to be used when connecting.
uint16_t _connectionHandle
Definition: cs_BleCentral.h:160
BleCentral(BleCentral const &)
bool isBusy()
Check whether an operation is in progress.
void onConnect(uint16_t connectionHandle, const ble_gap_evt_connected_t &event)
cs_ret_code_t connectWithClearance(const device_address_t &address, uint16_t timeoutMs=3000)
Same as connect(), but now we have clearance.
uint16_t _mtu
Current MTU.
Definition: cs_BleCentral.h:176
void onConnectClearance()
void onGapTimeout(const ble_gap_evt_timeout_t &event)
void onDiscoveryEvent(ble_db_discovery_evt_t &event)
Internal usage.
void onNotification(uint16_t gattStatus, const ble_gattc_evt_hvx_t &event)
void onGapEvent(uint16_t evtId, const ble_gap_evt_t &event)
Event handlers.
cs_ret_code_t connect(const device_address_t &address, uint16_t timeoutMs=3000)
Connect to a device.
cs_data_t requestWriteBuffer()
Request the write buffer.
void handleEvent(event_t &event)
Internal usage.
Operation _currentOperation
Operation in progress.
Definition: cs_BleCentral.h:181
void operator=(BleCentral const &)
device_address_t _address
Definition: cs_BleCentral.h:146
uint16_t _bufDataSize
How much data is actually in the buffer.
Definition: cs_BleCentral.h:158
void sendOperationResult(event_t &event)
TYPIFY(CONFIG_SCAN_WINDOW_625US) _scanWindow
void onRead(uint16_t gattStatus, const ble_gattc_evt_read_rsp_t &event)
cs_ret_code_t write(uint16_t handle, const uint8_t *data, uint16_t len)
Write data to a characteristic.
void finalizeOperation(Operation operation, cs_ret_code_t retCode)
Finalize an operation.
static BleCentral & getInstance()
Definition: cs_BleCentral.h:29
cs_ret_code_t nextWrite(uint16_t handle, uint16_t offset)
Writes the next chunk of a long write.
uint16_t _timeoutMs
Definition: cs_BleCentral.h:148
void finalizeOperation(Operation operation, uint8_t *data, uint8_t dataSize)
Operation
Definition: cs_BleCentral.h:132
cs_ret_code_t read(uint16_t handle)
Read data from a characteristic.
cs_data_t _buf
Buffer used for reading and writing.
Definition: cs_BleCentral.h:153
void init()
Initializes the class:
cs_ret_code_t disconnect()
Terminate current connection.
uint16_t _currentHandle
Keep up the handle of the current write or read operation.
Definition: cs_BleCentral.h:163
ble_db_discovery_t _discoveryModule
Definition: cs_BleCentral.h:165
bool isConnected()
Check whether this crownstone is connected as central.
Event listener.
Definition: cs_EventListener.h:17
Class that enables the use of 128 bit service UUIDs.
Definition: cs_UUID.h:35
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
@ CONFIG_SCAN_INTERVAL_625US
@ CONFIG_SCAN_WINDOW_625US
Packets (structs) that are used internally in the firmware, and can be changed freely.
Definition: cs_PacketsInternal.h:27
Definition: cs_PacketsInternal.h:97