Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_ServiceData.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <cfg/cs_Config.h>
13#include <common/cs_Types.h>
14#include <drivers/cs_Timer.h>
18#include <storage/cs_State.h>
19
21
22public:
24
30 void init(uint8_t deviceType);
31
37 void setDeviceType(uint8_t deviceType);
38
44 void updatePowerUsage(int32_t powerUsage);
45
51 void updateAccumulatedEnergy(int32_t energy);
52
58 void updateCrownstoneId(stone_id_t crownstoneId);
59
65 void updateSwitchState(uint8_t switchState);
66
72 void updateTemperature(int8_t temperature);
73
85 void updateServiceData(bool initial);
86
90 uint8_t* getArray();
91
95 uint16_t getArraySize();
96
97private:
99 app_timer_t _updateTimerData;
100 app_timer_id_t _updateTimerId = NULL;
101
108
111
113 uint8_t _switchState = 0;
114
117
120
122 int8_t _temperature = 0;
123
125 int8_t _powerFactor = 0;
126
128 int32_t _powerUsageReal = 0;
129
131 int32_t _energyUsed = 0;
132
134 TYPIFY(STATE_ERRORS) _stateErrors;
135
138
140
142 OperationMode _operationMode = OperationMode::OPERATION_MODE_UNINITIALIZED;
143
145 uint32_t _updateCount = 0;
146
148
153
158
159 /* Static function for the timeout */
160 static void staticTimeout(ServiceData* ptr) { ptr->updateServiceData(false); }
161
167 bool fillServiceData(uint32_t timestamp);
168
173
177 void fillWithSetupState(uint32_t timestamp);
178
182 void fillWithState(uint32_t timestamp);
183
187 void fillWithError(uint32_t timestamp);
188
195
199 void fillWithAlternativeState(uint32_t timestamp);
200
204 void fillWithHubState(uint32_t timestamp);
205
211 bool fillWithMicroapp(uint32_t timestamp);
212
219 void handleEvent(event_t& event);
220
226 int16_t compressPowerUsageMilliWatt(int32_t powerUsageMW);
227
233 int32_t decompressPowerUsage(int16_t compressedPowerUsage);
234
240 uint16_t timestampToPartialTimestamp(uint32_t timestamp);
241
249 uint16_t getPartialTimestampOrCounter(uint32_t timestamp, uint32_t counter);
250
254 uint16_t getPartialBehaviourHash(uint32_t behaviourHash);
255
260 void sendMeshState(bool event);
261};
Event listener.
Definition: cs_EventListener.h:17
Class that keeps up states of other stones.
Definition: cs_ExternalStates.h:34
Definition: cs_ServiceData.h:20
service_data_encrypted_microapp_t _microappServiceData
Microapp data to be advertised in crownstone service data.
Definition: cs_ServiceData.h:157
uint16_t getPartialTimestampOrCounter(uint32_t timestamp, uint32_t counter)
When a timestamp is available, return partial timestamp, else returns a counter.
void setDeviceType(uint8_t deviceType)
Set the device type field of the service data.
uint8_t _switchState
Cache switch state.
Definition: cs_ServiceData.h:113
int32_t _powerUsageReal
Cache the power usage in mW.
Definition: cs_ServiceData.h:128
void updateAccumulatedEnergy(int32_t energy)
Set the energy used field of the service data.
TYPIFY(STATE_ERRORS) _stateErrors
Cache the state errors.
void fillWithSetupState(uint32_t timestamp)
Put the state of this Crownstone in setup mode in the service data.
int32_t _energyUsed
Cache the energy used, in units of 64 J.
Definition: cs_ServiceData.h:131
void sendMeshState(bool event)
Send the state over the mesh.
void fillWithAlternativeState(uint32_t timestamp)
Put the alternative state of this Crownstone in the service data.
bool _microappServiceDataSet
Whether the microapp wants to advertise service data.
Definition: cs_ServiceData.h:152
uint16_t getPartialBehaviourHash(uint32_t behaviourHash)
Get a part of the behaviour hash.
bool fillServiceData(uint32_t timestamp)
Selects a type of data and puts this in the service data.
int8_t _temperature
Cache the temperature.
Definition: cs_ServiceData.h:122
uint16_t getArraySize()
Get the size of the service data.
uint8_t * getArray()
Get the service data as array.
void updateTemperature(int8_t temperature)
Set the temperature field of the service data.
void updateCrownstoneId(stone_id_t crownstoneId)
Set the ID field of the service data.
void updateServiceData(bool initial)
Update the service data.
int16_t compressPowerUsageMilliWatt(int32_t powerUsageMW)
Compress power usage, according to service data protocol v3.
ExternalStates _externalStates
Definition: cs_ServiceData.h:147
int8_t _powerFactor
Cache the power factor.
Definition: cs_ServiceData.h:125
service_data_state_extra_flags_t _extraFlags
Cache extra flags.
Definition: cs_ServiceData.h:119
bool fillWithMicroapp(uint32_t timestamp)
Put microapp data in the service data.
void handleEvent(event_t &event)
Called when there are events to handle.
uint32_t _firstErrorTimestamp
Cache timestamp of first error.
Definition: cs_ServiceData.h:137
uint16_t timestampToPartialTimestamp(uint32_t timestamp)
Convert timestamp to a partial timestamp, according to service data protocol v3.
void fillWithError(uint32_t timestamp)
Put the error state of this Crownstone in the service data.
app_timer_id_t _updateTimerId
Definition: cs_ServiceData.h:100
uint32_t _updateCount
Counter that keeps up the number of times that the advertisement has been updated.
Definition: cs_ServiceData.h:145
void fillWithHubState(uint32_t timestamp)
Put the hub state in the service data.
app_timer_t _updateTimerData
Timer used to periodically update the advertisement.
Definition: cs_ServiceData.h:99
void encryptServiceData()
Encrypt the service data.
void init(uint8_t deviceType)
Init the service data, make sure you set important fields first.
void updateSwitchState(uint8_t switchState)
Set the switch state field of the service data.
void updatePowerUsage(int32_t powerUsage)
Set the power usage field of the service data.
OperationMode _operationMode
Cache the operation mode.
Definition: cs_ServiceData.h:142
uint32_t _sendStateCountdown
Definition: cs_ServiceData.h:139
int32_t decompressPowerUsage(int16_t compressedPowerUsage)
Decompress power usage, according to service data protocol v3.
service_data_t _serviceData
Stores the last (current) advertised service data.
Definition: cs_ServiceData.h:107
void fillWithState(uint32_t timestamp)
Put the state of this Crownstone in the service data.
bool fillWithExternalState()
Put the state or error state of another Crownstone in the service data.
static void staticTimeout(ServiceData *ptr)
Definition: cs_ServiceData.h:160
service_data_state_flags_t _flags
Cache flags.
Definition: cs_ServiceData.h:116
stone_id_t _crownstoneId
Cache own ID.
Definition: cs_ServiceData.h:110
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
#define MESH_SEND_STATE_INTERVAL_MS
Definition: cs_Config.h:207
#define TICK_INTERVAL_MS
Interval in milliseconds at which tick events are dispatched.
Definition: cs_Config.h:277
uint8_t stone_id_t
Definition: cs_Typedefs.h:23
OperationMode
Definition: cs_Types.h:440
@ STATE_ERRORS
Microapp data that will be encrypted.
Definition: cs_ServiceDataPackets.h:147
Definition: cs_ServiceDataPackets.h:54
Definition: cs_ServiceDataPackets.h:40
Service data.
Definition: cs_ServiceDataPackets.h:259