Bluenet
5.7.0
Bluenet, firmware for nRF52 smart home devices
|
Class to connect to another crownstone, and write control commands. More...
#include <cs_CrownstoneCentral.h>
Public Member Functions | |
cs_ret_code_t | init () |
Initializes the class: More... | |
cs_ret_code_t | connect (stone_id_t stoneId, uint16_t timeoutMs=3000) |
Connect, discover, and read session data. More... | |
cs_ret_code_t | connect (const device_address_t &address, uint16_t timeoutMs=3000) |
Connect, discover, and read session data. More... | |
cs_ret_code_t | disconnect () |
Terminate current connection. More... | |
cs_ret_code_t | write (cs_control_cmd_t commandType, uint8_t *data, uint16_t size) |
Write a control command, and get the result. More... | |
cs_data_t | requestWriteBuffer () |
Request the write buffer. More... | |
void | handleEvent (event_t &event) |
Internal usage. More... | |
Private Types | |
enum | ServiceIndex { SERVICE_INDEX_CROWNSTONE = 0 , SERVICE_INDEX_SETUP = 1 , SERVICE_INDEX_DEVICE_INFO = 2 , SERVICE_INDEX_DFU = 3 , SERVICE_INDEX_COUNT = 4 } |
enum class | Operation : uint8_t { NONE , CONNECT , WRITE } |
enum class | ConnectSteps : uint8_t { NONE = 0 , GET_ADDRESS , CONNECT , DISCOVER , ENABLE_NOTIFICATIONS , SESSION_KEY , SESSION_DATA , DONE } |
enum class | WriteControlSteps : uint8_t { NONE = 0 , WRITE , RECEIVE_RESULT , DONE } |
Private Member Functions | |
void | reset () |
Reset connection variables. More... | |
void | resetNotifactionMergerState () |
Reset notification merger variables. More... | |
void | readSessionData () |
Read session key or session data, depending on operation mode. More... | |
cs_ret_code_t | mergeNotification (const cs_const_data_t ¬ificationData, cs_data_t &resultData) |
Merge this notification data with that of others. More... | |
bool | isBusy () |
Check whether an operation is in progress. More... | |
void | startTimeoutTimer (uint16_t timeoutMs) |
void | stopTimeoutTimer () |
void | setStep (ConnectSteps step) |
void | setStep (WriteControlSteps step) |
void | setStep (uint8_t step) |
bool | finalizeStep (uint8_t step, cs_ret_code_t retCode) |
Returns true when you can continue. More... | |
bool | finalizeStep (ConnectSteps step, cs_ret_code_t retCode) |
bool | finalizeStep (WriteControlSteps step, cs_ret_code_t retCode) |
void | finalizeOperation (Operation operation, cs_ret_code_t retCode) |
Finalize an operation. More... | |
void | finalizeOperation (Operation operation, uint8_t *data, uint8_t dataSize) |
void | sendOperationResult (event_t &event) |
void | onTimeout () |
Event handlers. More... | |
void | onMacAddress (mesh_topo_mac_result_t &result) |
void | onConnect (cs_ret_code_t retCode) |
void | onDisconnect () |
void | onDiscovery (ble_central_discovery_t &result) |
void | onDiscoveryDone (cs_ret_code_t retCode) |
void | onRead (ble_central_read_result_t &result) |
void | onReadDuringConnect (ble_central_read_result_t &result) |
void | onWrite (ble_central_write_result_t &result) |
void | onNotification (ble_central_notification_t &result) |
![]() | |
EventListener () | |
virtual | ~EventListener () |
unregisters the listener. More... | |
virtual void | handleEvent (event_t &event)=0 |
Handle events. More... | |
void | listen () |
Registers this with the EventDispatcher. More... | |
Private Attributes | |
UUID | _serviceUuids [SERVICE_INDEX_COUNT] |
Service UUIDs we need for discovery. More... | |
uint16_t | _sessionKeyHandle |
Handles that we need for reading and writing. More... | |
uint16_t | _sessionDataHandle |
uint16_t | _controlHandle |
uint16_t | _resultHandle |
uint16_t | _resultCccdHandle |
OperationMode | _opMode = OperationMode::OPERATION_MODE_UNINITIALIZED |
Operation mode of the crownstone we are connected to. More... | |
Operation | _currentOperation = Operation::NONE |
The operation we're currently executing. More... | |
uint8_t | _currentStep |
Asynchronous step of an operation we are currently executing. More... | |
uint8_t | _notificationNextIndex = 0 |
The notification index that we expect to get next. More... | |
uint16_t | _notificationMergedDataSize = 0 |
The current size of the merged notification data. More... | |
stone_id_t | _stoneId = 0 |
Keep up to which stone ID we're connected. More... | |
uint16_t | _timeoutMs |
Keep up which timeout we use. More... | |
uint16_t | _timeoutCountDown = 0 |
A countdown until timeout. More... | |
Class to connect to another crownstone, and write control commands.
The result of a control command will be received via notifications. These notifications are merged into a single result packet.
Performs encryption and decryption of packets.
This class uses:
|
strongprivate |
|
strongprivate |
|
private |
|
strongprivate |
cs_ret_code_t CrownstoneCentral::connect | ( | const device_address_t & | address, |
uint16_t | timeoutMs = 3000 |
||
) |
Connect, discover, and read session data.
[in] | address | The MAC address of the crownstone you want to connect to. |
[in] | timeoutMs | How long (in milliseconds) to try connecting, before giving up. |
cs_ret_code_t CrownstoneCentral::connect | ( | stone_id_t | stoneId, |
uint16_t | timeoutMs = 3000 |
||
) |
Connect, discover, and read session data.
[in] | stoneId | The stone ID of the crownstone you want to connect to. |
[in] | timeoutMs | How long (in milliseconds) to try connecting, before giving up. |
cs_ret_code_t CrownstoneCentral::disconnect | ( | ) |
Terminate current connection.
Can be called at any moment: will cancel the current operation if any.
|
private |
Finalize an operation.
Will always lead to sending an event, and resetting current operation.
|
private |
|
private |
|
private |
Returns true when you can continue.
|
private |
|
virtual |
Internal usage.
Implements EventListener.
cs_ret_code_t CrownstoneCentral::init | ( | ) |
Initializes the class:
|
private |
Check whether an operation is in progress.
|
private |
Merge this notification data with that of others.
[in] | notificationData | The data of this notification. |
[out] | resultData | The result data, only set on success. |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
Event handlers.
|
private |
|
private |
Read session key or session data, depending on operation mode.
Sets current step.
cs_data_t CrownstoneCentral::requestWriteBuffer | ( | ) |
Request the write buffer.
You can then put your data in this buffer and use it as data in the write() command, so no copy has to take place.
|
private |
Reset connection variables.
|
private |
Reset notification merger variables.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
cs_ret_code_t CrownstoneCentral::write | ( | cs_control_cmd_t | commandType, |
uint8_t * | data, | ||
uint16_t | size | ||
) |
Write a control command, and get the result.
When the result code in the result data is ERR_WAIT_FOR_SUCCESS, you will get another EVT_CS_CENTRAL_WRITE_RESULT event.
TODO: add timeout.
[in] | commandType | What control command. |
[in] | data | Pointer to the command payload data, which will be copied. If the pointer equals the requested write buffer, no copying will take place. |
[in] | size | Size of the command payload data. |
|
private |
|
private |
The operation we're currently executing.
|
private |
Asynchronous step of an operation we are currently executing.
|
private |
The current size of the merged notification data.
|
private |
The notification index that we expect to get next.
|
private |
Operation mode of the crownstone we are connected to.
|
private |
|
private |
|
private |
Service UUIDs we need for discovery.
|
private |
|
private |
Handles that we need for reading and writing.
|
private |
Keep up to which stone ID we're connected.
Not always set though.
|
private |
A countdown until timeout.
|
private |
Keep up which timeout we use.