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

#include <cs_ServiceData.h>

Inheritance diagram for ServiceData:
Collaboration diagram for ServiceData:

Public Member Functions

 ServiceData ()
 
void init (uint8_t deviceType)
 Init the service data, make sure you set important fields first. More...
 
void setDeviceType (uint8_t deviceType)
 Set the device type field of the service data. More...
 
void updatePowerUsage (int32_t powerUsage)
 Set the power usage field of the service data. More...
 
void updateAccumulatedEnergy (int32_t energy)
 Set the energy used field of the service data. More...
 
void updateCrownstoneId (stone_id_t crownstoneId)
 Set the ID field of the service data. More...
 
void updateSwitchState (uint8_t switchState)
 Set the switch state field of the service data. More...
 
void updateTemperature (int8_t temperature)
 Set the temperature field of the service data. More...
 
void updateServiceData (bool initial)
 Update the service data. More...
 
uint8_t * getArray ()
 Get the service data as array. More...
 
uint16_t getArraySize ()
 Get the size of the service data. More...
 

Private Member Functions

 TYPIFY (STATE_ERRORS) _stateErrors
 Cache the state errors. More...
 
bool fillServiceData (uint32_t timestamp)
 Selects a type of data and puts this in the service data. More...
 
void encryptServiceData ()
 Encrypt the service data. More...
 
void fillWithSetupState (uint32_t timestamp)
 Put the state of this Crownstone in setup mode in the service data. More...
 
void fillWithState (uint32_t timestamp)
 Put the state of this Crownstone in the service data. More...
 
void fillWithError (uint32_t timestamp)
 Put the error state of this Crownstone in the service data. More...
 
bool fillWithExternalState ()
 Put the state or error state of another Crownstone in the service data. More...
 
void fillWithAlternativeState (uint32_t timestamp)
 Put the alternative state of this Crownstone in the service data. More...
 
void fillWithHubState (uint32_t timestamp)
 Put the hub state in the service data. More...
 
bool fillWithMicroapp (uint32_t timestamp)
 Put microapp data in the service data. More...
 
void handleEvent (event_t &event)
 Called when there are events to handle. More...
 
int16_t compressPowerUsageMilliWatt (int32_t powerUsageMW)
 Compress power usage, according to service data protocol v3. More...
 
int32_t decompressPowerUsage (int16_t compressedPowerUsage)
 Decompress power usage, according to service data protocol v3. More...
 
uint16_t timestampToPartialTimestamp (uint32_t timestamp)
 Convert timestamp to a partial timestamp, according to service data protocol v3. More...
 
uint16_t getPartialTimestampOrCounter (uint32_t timestamp, uint32_t counter)
 When a timestamp is available, return partial timestamp, else returns a counter. More...
 
uint16_t getPartialBehaviourHash (uint32_t behaviourHash)
 Get a part of the behaviour hash. More...
 
void sendMeshState (bool event)
 Send the state over the mesh. More...
 
- Private Member Functions inherited from EventListener
 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...
 

Static Private Member Functions

static void staticTimeout (ServiceData *ptr)
 

Private Attributes

app_timer_t _updateTimerData
 Timer used to periodically update the advertisement. More...
 
app_timer_id_t _updateTimerId = NULL
 
service_data_t _serviceData
 Stores the last (current) advertised service data. More...
 
stone_id_t _crownstoneId = 0
 Cache own ID. More...
 
uint8_t _switchState = 0
 Cache switch state. More...
 
service_data_state_flags_t _flags
 Cache flags. More...
 
service_data_state_extra_flags_t _extraFlags
 Cache extra flags. More...
 
int8_t _temperature = 0
 Cache the temperature. More...
 
int8_t _powerFactor = 0
 Cache the power factor. More...
 
int32_t _powerUsageReal = 0
 Cache the power usage in mW. More...
 
int32_t _energyUsed = 0
 Cache the energy used, in units of 64 J. More...
 
uint32_t _firstErrorTimestamp = 0
 Cache timestamp of first error. More...
 
uint32_t _sendStateCountdown = MESH_SEND_STATE_INTERVAL_MS / TICK_INTERVAL_MS
 
OperationMode _operationMode = OperationMode::OPERATION_MODE_UNINITIALIZED
 Cache the operation mode. More...
 
uint32_t _updateCount = 0
 Counter that keeps up the number of times that the advertisement has been updated. More...
 
ExternalStates _externalStates
 
bool _microappServiceDataSet = false
 Whether the microapp wants to advertise service data. More...
 
service_data_encrypted_microapp_t _microappServiceData
 Microapp data to be advertised in crownstone service data. More...
 

Constructor & Destructor Documentation

◆ ServiceData()

Member Function Documentation

◆ compressPowerUsageMilliWatt()

int16_t ServiceData::compressPowerUsageMilliWatt ( int32_t  powerUsageMW)
private

Compress power usage, according to service data protocol v3.

Parameters
[in]powerUsageMWPower usage in milliWatt
Returns
Compressed representation of the power usage.

◆ decompressPowerUsage()

int32_t ServiceData::decompressPowerUsage ( int16_t  compressedPowerUsage)
private

Decompress power usage, according to service data protocol v3.

Parameters
[in]powerUsageMWCompressed representation of the power usage.
Returns
Power usage in milliWatt.

◆ encryptServiceData()

void ServiceData::encryptServiceData ( )
private

Encrypt the service data.

◆ fillServiceData()

bool ServiceData::fillServiceData ( uint32_t  timestamp)
private

Selects a type of data and puts this in the service data.

Returns
True when service data has to be encrypted.

◆ fillWithAlternativeState()

void ServiceData::fillWithAlternativeState ( uint32_t  timestamp)
private

Put the alternative state of this Crownstone in the service data.

◆ fillWithError()

void ServiceData::fillWithError ( uint32_t  timestamp)
private

Put the error state of this Crownstone in the service data.

◆ fillWithExternalState()

bool ServiceData::fillWithExternalState ( )
private

Put the state or error state of another Crownstone in the service data.

Returns
True when Crownstone service data has been filled.

◆ fillWithHubState()

void ServiceData::fillWithHubState ( uint32_t  timestamp)
private

Put the hub state in the service data.

◆ fillWithMicroapp()

bool ServiceData::fillWithMicroapp ( uint32_t  timestamp)
private

Put microapp data in the service data.

Returns
True when Crownstone service data has been filled.

◆ fillWithSetupState()

void ServiceData::fillWithSetupState ( uint32_t  timestamp)
private

Put the state of this Crownstone in setup mode in the service data.

◆ fillWithState()

void ServiceData::fillWithState ( uint32_t  timestamp)
private

Put the state of this Crownstone in the service data.

◆ getArray()

uint8_t * ServiceData::getArray ( )

Get the service data as array.

◆ getArraySize()

uint16_t ServiceData::getArraySize ( )

Get the size of the service data.

◆ getPartialBehaviourHash()

uint16_t ServiceData::getPartialBehaviourHash ( uint32_t  behaviourHash)
private

Get a part of the behaviour hash.

◆ getPartialTimestampOrCounter()

uint16_t ServiceData::getPartialTimestampOrCounter ( uint32_t  timestamp,
uint32_t  counter 
)
private

When a timestamp is available, return partial timestamp, else returns a counter.

Parameters
[in]timestampThe timestamp.
[in]counterThe counter.
Returns
Counter, or the least significant part of the timestamp.

◆ handleEvent()

void ServiceData::handleEvent ( event_t event)
privatevirtual

Called when there are events to handle.

Parameters
[in]evtEvent type, see cs_EventTypes.h.
[in]p_dataPointer to the data.
[in]lengthLength of the data.

Implements EventListener.

◆ init()

void ServiceData::init ( uint8_t  deviceType)

Init the service data, make sure you set important fields first.

Parameters
[in]deviceTypeThe device type, see cs_DeviceTypes.h

◆ sendMeshState()

void ServiceData::sendMeshState ( bool  event)
private

Send the state over the mesh.

Parameters
[in]eventTrue when sending state because of an event.

◆ setDeviceType()

void ServiceData::setDeviceType ( uint8_t  deviceType)

Set the device type field of the service data.

Parameters
[in]deviceTypeThe device type, see cs_DeviceTypes.h

◆ staticTimeout()

static void ServiceData::staticTimeout ( ServiceData ptr)
inlinestaticprivate

◆ timestampToPartialTimestamp()

uint16_t ServiceData::timestampToPartialTimestamp ( uint32_t  timestamp)
private

Convert timestamp to a partial timestamp, according to service data protocol v3.

Parameters
[in]timestampThe timestamp.
Returns
The least significant part of the timestamp.

◆ TYPIFY()

ServiceData::TYPIFY ( STATE_ERRORS  )
private

Cache the state errors.

◆ updateAccumulatedEnergy()

void ServiceData::updateAccumulatedEnergy ( int32_t  energy)

Set the energy used field of the service data.

Parameters
[in]energyThe energy used in units of 64 Joule.

◆ updateCrownstoneId()

void ServiceData::updateCrownstoneId ( stone_id_t  crownstoneId)

Set the ID field of the service data.

Parameters
[in]crownstoneIdThe Crownstone ID.

◆ updatePowerUsage()

void ServiceData::updatePowerUsage ( int32_t  powerUsage)

Set the power usage field of the service data.

Parameters
[in]powerUsageThe power usage in milliWatt.

◆ updateServiceData()

void ServiceData::updateServiceData ( bool  initial)

Update the service data.

Updates some states. Selects a type of data, and puts this in the service data. Writes the service data to UART. Sends out event EVT_ADVERTISEMENT_UPDATED.

Parameters
[in]initialSet initial to true when this is just the initial data when there's no need to send out the event.

◆ updateSwitchState()

void ServiceData::updateSwitchState ( uint8_t  switchState)

Set the switch state field of the service data.

Parameters
[in]switchStateThe switch state.

◆ updateTemperature()

void ServiceData::updateTemperature ( int8_t  temperature)

Set the temperature field of the service data.

Parameters
[in]temperatureThe temperature.

Member Data Documentation

◆ _crownstoneId

stone_id_t ServiceData::_crownstoneId = 0
private

Cache own ID.

◆ _energyUsed

int32_t ServiceData::_energyUsed = 0
private

Cache the energy used, in units of 64 J.

◆ _externalStates

ExternalStates ServiceData::_externalStates
private

◆ _extraFlags

service_data_state_extra_flags_t ServiceData::_extraFlags
private

Cache extra flags.

◆ _firstErrorTimestamp

uint32_t ServiceData::_firstErrorTimestamp = 0
private

Cache timestamp of first error.

◆ _flags

service_data_state_flags_t ServiceData::_flags
private

Cache flags.

◆ _microappServiceData

service_data_encrypted_microapp_t ServiceData::_microappServiceData
private

Microapp data to be advertised in crownstone service data.

◆ _microappServiceDataSet

bool ServiceData::_microappServiceDataSet = false
private

Whether the microapp wants to advertise service data.

◆ _operationMode

OperationMode ServiceData::_operationMode = OperationMode::OPERATION_MODE_UNINITIALIZED
private

Cache the operation mode.

◆ _powerFactor

int8_t ServiceData::_powerFactor = 0
private

Cache the power factor.

◆ _powerUsageReal

int32_t ServiceData::_powerUsageReal = 0
private

Cache the power usage in mW.

◆ _sendStateCountdown

uint32_t ServiceData::_sendStateCountdown = MESH_SEND_STATE_INTERVAL_MS / TICK_INTERVAL_MS
private

◆ _serviceData

service_data_t ServiceData::_serviceData
private

Stores the last (current) advertised service data.

This data will be copied by the advertiser.

◆ _switchState

uint8_t ServiceData::_switchState = 0
private

Cache switch state.

◆ _temperature

int8_t ServiceData::_temperature = 0
private

Cache the temperature.

◆ _updateCount

uint32_t ServiceData::_updateCount = 0
private

Counter that keeps up the number of times that the advertisement has been updated.

◆ _updateTimerData

app_timer_t ServiceData::_updateTimerData
private

Timer used to periodically update the advertisement.

◆ _updateTimerId

app_timer_id_t ServiceData::_updateTimerId = NULL
private

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