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

Class that keeps up devices to be tracked. More...

#include <cs_TrackedDevices.h>

Inheritance diagram for TrackedDevices:
Collaboration diagram for TrackedDevices:

Public Member Functions

 TrackedDevices ()
 
void init ()
 Init class. More...
 
void handleEvent (event_t &evt) override
 Handle events. More...
 
- Public 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 Public Attributes

static const uint8_t MAX_TRACKED_DEVICES = 20
 Maximum number of registered tracked devices. More...
 
static const uint8_t LOCATION_ID_TTL_MINUTES = 5
 After N minutes not hearing anything from the device, the location ID will be set to 0 (in sphere). More...
 
static const uint16_t HEARTBEAT_TTL_MINUTES_MAX = 60
 Max heartbeat TTL in minutes. More...
 

Private Member Functions

TrackedDevicefindOrAdd (device_id_t deviceId)
 Find device with given ID, else add a new device with given ID. More...
 
TrackedDevicefind (device_id_t deviceId)
 Find and return device with given id. More...
 
TrackedDevicefindToken (uint8_t *deviceToken, uint8_t size)
 Find and return device with given token. More...
 
TrackedDeviceadd ()
 Add device to list. More...
 
cs_ret_code_t handleRegister (internal_register_tracked_device_packet_t &packet)
 
cs_ret_code_t handleUpdate (internal_update_tracked_device_packet_t &packet)
 
void handleMeshRegister (TYPIFY(EVT_MESH_TRACKED_DEVICE_REGISTER) &packet)
 
void handleMeshToken (TYPIFY(EVT_MESH_TRACKED_DEVICE_TOKEN) &packet)
 
void handleMeshListSize (TYPIFY(EVT_MESH_TRACKED_DEVICE_LIST_SIZE) &packet)
 
void handleScannedDevice (adv_background_parsed_v1_t &packet)
 
cs_ret_code_t handleHeartbeat (internal_tracked_device_heartbeat_packet_t &packet)
 
cs_ret_code_t handleHeartbeat (TrackedDevice &device, uint8_t locationId, uint8_t ttlMinutes, bool fromMesh)
 
void handleMeshHeartbeat (TYPIFY(EVT_MESH_TRACKED_DEVICE_HEARTBEAT) &packet)
 
bool hasAccess (TrackedDevice &device, uint8_t accessLevel)
 Return true when given access level is equal or higher than device access level. More...
 
bool isTokenOkToSet (TrackedDevice &device, uint8_t *deviceToken, uint8_t size)
 Returns true when no other device has this token. More...
 
void print (TrackedDevice &device)
 
void tickMinute ()
 A minute has passed. More...
 
void tickSecond ()
 Send location of devices with non-timed out heartbeat. More...
 
void checkSynced ()
 Check if tracked device list is synced yet. More...
 
void sendBackgroundAdv (TrackedDevice &device, uint8_t *macAddress, int8_t rssi)
 Send background adv event. More...
 
void sendHeartbeatLocation (TrackedDevice &device, bool fromMesh, bool simulated)
 Send profile location to event dispatcher. More...
 
void sendHeartbeatToMesh (TrackedDevice &device)
 Send the heartbeat msg to the mesh. More...
 
void sendRegisterToMesh (TrackedDevice &device)
 Send tracked device register msg to mesh. More...
 
void sendTokenToMesh (TrackedDevice &device)
 Send tracked device token msg to mesh. More...
 
void sendListSizeToMesh (uint8_t deviceCount)
 Send tracked devices list size to mesh. More...
 
void sendDeviceList ()
 Send all tracked devices to mesh. More...
 

Private Attributes

uint16_t ticksLeftSecond = TICKS_PER_SECOND
 
uint16_t ticksLeftMinute = TICKS_PER_MINUTES
 
Store< TrackedDevice, MAX_TRACKED_DEVICES_store
 List of all tracked devices. More...
 
bool _deviceListIsSynced = false
 Whether there has been a successful sync of tracked devices. More...
 
uint8_t _expectedDeviceListSize = 0xFF
 When syncing, the remote crownstone will tell how many devices there are. More...
 

Static Private Attributes

static const uint16_t TICKS_PER_SECOND = (1000 / TICK_INTERVAL_MS)
 
static const uint16_t TICKS_PER_MINUTES = (60 * 1000 / TICK_INTERVAL_MS)
 

Detailed Description

Class that keeps up devices to be tracked.

Alternative to dynamic background broadcasts (no heartbeat required), or to no background broadcasts at all (heartbeats required).

  • Register devices: each device has a unique ID, and registers a unique token.
  • Make sure device tokens expire.
  • Cache the data of the devices (profile, location, flags, etc).
  • Handle scans with device token only as data, and dispatch background broadcast event, by adding cached data.
  • Handle device heartbeats, which are treated as if the device was scanned, by dispatching profile location event.

Constructor & Destructor Documentation

◆ TrackedDevices()

Member Function Documentation

◆ add()

TrackedDevice * TrackedDevices::add ( )
private

Add device to list.

Returns invalidated entry when added. Returns null if couldn't be added.

◆ checkSynced()

void TrackedDevices::checkSynced ( )
private

Check if tracked device list is synced yet.

◆ find()

TrackedDevice * TrackedDevices::find ( device_id_t  deviceId)
private

Find and return device with given id.

Returns null if it couldn't be found.

◆ findOrAdd()

TrackedDevice * TrackedDevices::findOrAdd ( device_id_t  deviceId)
private

Find device with given ID, else add a new device with given ID.

Returns device with given ID when found or added. Returns null if couldn't be added.

◆ findToken()

TrackedDevice * TrackedDevices::findToken ( uint8_t *  deviceToken,
uint8_t  size 
)
private

Find and return device with given token.

Returns null if it couldn't be found.

◆ handleEvent()

void TrackedDevices::handleEvent ( event_t evt)
overridevirtual

Handle events.

Implements EventListener.

◆ handleHeartbeat() [1/2]

cs_ret_code_t TrackedDevices::handleHeartbeat ( internal_tracked_device_heartbeat_packet_t packet)
private

◆ handleHeartbeat() [2/2]

cs_ret_code_t TrackedDevices::handleHeartbeat ( TrackedDevice device,
uint8_t  locationId,
uint8_t  ttlMinutes,
bool  fromMesh 
)
private

◆ handleMeshHeartbeat()

void TrackedDevices::handleMeshHeartbeat ( TYPIFY(EVT_MESH_TRACKED_DEVICE_HEARTBEAT) &  packet)
private

◆ handleMeshListSize()

void TrackedDevices::handleMeshListSize ( TYPIFY(EVT_MESH_TRACKED_DEVICE_LIST_SIZE) &  packet)
private

◆ handleMeshRegister()

void TrackedDevices::handleMeshRegister ( TYPIFY(EVT_MESH_TRACKED_DEVICE_REGISTER) &  packet)
private

◆ handleMeshToken()

void TrackedDevices::handleMeshToken ( TYPIFY(EVT_MESH_TRACKED_DEVICE_TOKEN) &  packet)
private

◆ handleRegister()

cs_ret_code_t TrackedDevices::handleRegister ( internal_register_tracked_device_packet_t packet)
private

◆ handleScannedDevice()

void TrackedDevices::handleScannedDevice ( adv_background_parsed_v1_t packet)
private

◆ handleUpdate()

cs_ret_code_t TrackedDevices::handleUpdate ( internal_update_tracked_device_packet_t packet)
private

◆ hasAccess()

bool TrackedDevices::hasAccess ( TrackedDevice device,
uint8_t  accessLevel 
)
private

Return true when given access level is equal or higher than device access level.

◆ init()

void TrackedDevices::init ( )

Init class.

Register as event listener.

◆ isTokenOkToSet()

bool TrackedDevices::isTokenOkToSet ( TrackedDevice device,
uint8_t *  deviceToken,
uint8_t  size 
)
private

Returns true when no other device has this token.

◆ print()

void TrackedDevices::print ( TrackedDevice device)
private

◆ sendBackgroundAdv()

void TrackedDevices::sendBackgroundAdv ( TrackedDevice device,
uint8_t *  macAddress,
int8_t  rssi 
)
private

Send background adv event.

Checks if all fields are set.

◆ sendDeviceList()

void TrackedDevices::sendDeviceList ( )
private

Send all tracked devices to mesh.

◆ sendHeartbeatLocation()

void TrackedDevices::sendHeartbeatLocation ( TrackedDevice device,
bool  fromMesh,
bool  simulated 
)
private

Send profile location to event dispatcher.

Checks if all fields are set. Set fromMesh to true when location is based on a mesh message. Set simulated to true when the location is from the heartbeat TTL, not from a command or broadcast.

◆ sendHeartbeatToMesh()

void TrackedDevices::sendHeartbeatToMesh ( TrackedDevice device)
private

Send the heartbeat msg to the mesh.

◆ sendListSizeToMesh()

void TrackedDevices::sendListSizeToMesh ( uint8_t  deviceCount)
private

Send tracked devices list size to mesh.

◆ sendRegisterToMesh()

void TrackedDevices::sendRegisterToMesh ( TrackedDevice device)
private

Send tracked device register msg to mesh.

◆ sendTokenToMesh()

void TrackedDevices::sendTokenToMesh ( TrackedDevice device)
private

Send tracked device token msg to mesh.

◆ tickMinute()

void TrackedDevices::tickMinute ( )
private

A minute has passed.

Decrease TTL of all devices by 1. Decrease location timeout of all device by 1. Decrease heartbeat TTL.

◆ tickSecond()

void TrackedDevices::tickSecond ( )
private

Send location of devices with non-timed out heartbeat.

Member Data Documentation

◆ _deviceListIsSynced

bool TrackedDevices::_deviceListIsSynced = false
private

Whether there has been a successful sync of tracked devices.

For now, this means just getting a list of devices from another crownstone, after boot.

◆ _expectedDeviceListSize

uint8_t TrackedDevices::_expectedDeviceListSize = 0xFF
private

When syncing, the remote crownstone will tell how many devices there are.

This number is cached in this variable, so we know when we're synced.

◆ _store

Store<TrackedDevice, MAX_TRACKED_DEVICES> TrackedDevices::_store
private

List of all tracked devices.

Device ID should be unique.

◆ HEARTBEAT_TTL_MINUTES_MAX

const uint16_t TrackedDevices::HEARTBEAT_TTL_MINUTES_MAX = 60
static

Max heartbeat TTL in minutes.

Make sure that it's not larger than what fits in location id timeout.

◆ LOCATION_ID_TTL_MINUTES

const uint8_t TrackedDevices::LOCATION_ID_TTL_MINUTES = 5
static

After N minutes not hearing anything from the device, the location ID will be set to 0 (in sphere).

This prevents sending out old locations.

◆ MAX_TRACKED_DEVICES

const uint8_t TrackedDevices::MAX_TRACKED_DEVICES = 20
static

Maximum number of registered tracked devices.

◆ TICKS_PER_MINUTES

const uint16_t TrackedDevices::TICKS_PER_MINUTES = (60 * 1000 / TICK_INTERVAL_MS)
staticprivate

◆ TICKS_PER_SECOND

const uint16_t TrackedDevices::TICKS_PER_SECOND = (1000 / TICK_INTERVAL_MS)
staticprivate

◆ ticksLeftMinute

uint16_t TrackedDevices::ticksLeftMinute = TICKS_PER_MINUTES
private

◆ ticksLeftSecond

uint16_t TrackedDevices::ticksLeftSecond = TICKS_PER_SECOND
private

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