Modbus Vault v1.0
Loading...
Searching...
No Matches
telemetry_service.c File Reference

Implementation of the telemetry service. More...

#include "telemetry_service.h"
#include "debug_pins.h"
#include "esp_err.h"
#include "esp_timer.h"
#include "event_bus.h"
#include "freertos/semphr.h"
#include "metrics.h"
#include "runtime_tasks.h"
Include dependency graph for telemetry_service.c:

Macros

#define TELEMETRY_SERVICE_LIVE_QUEUE_LENGTH   (16U)
 
#define TELEMETRY_SERVICE_LIVE_QUOTA   (2U)
 
#define TELEMETRY_SERVICE_REPLAY_QUOTA   (1U)
 
#define TELEMETRY_SERVICE_TASK_DELAY_TIME_MS    (30U)
 
#define TELEMETRY_SERVICE_PUBLISH_METRICS_PERIOD_US    (300000000UL)
 
#define TELEMETRY_SERVICE_FULL_TOPIC_MAX_SIZE   (128U)
 

Functions

static esp_err_t generate_topics_internal (const char *device_id_ptr)
 Generate publishing topics.
 
static void process_live_quota_internal (void)
 Process live records quota.
 
static bool process_replay_quota_internal (void)
 Process replay records quota.
 
static void handle_system_metrics_internal (void *arg_void_ptr)
 Handle sending system's metrics.
 
static bool publish_to_topic_helper (const telemetry_pipeline_record_t *payload_ptr, telemetry_service_topics_t topic)
 Publish to topic.
 
static void telemetry_service_task (void *parameters_void_ptr)
 Process live queue and replay if available.
 
static void telemetry_service_stop_task (void)
 Stop telemetry service task.
 
bool telemetry_service_init (mqtt_bridge_config_t *mqtt_bridge_config_ptr, bool(*replay_check_cb)(void))
 Initialize telemetry service.
 
bool telemetry_service_enqueue_live (const telemetry_pipeline_record_t *payload_ptr)
 Enqueue live record.
 
bool telemetry_service_publish_live (const telemetry_pipeline_record_t *payload_ptr)
 Publish live record.
 
bool telemetry_service_publish_replay (const telemetry_pipeline_record_t *payload_ptr)
 Publish replay record.
 
bool telemetry_service_publish_status (const telemetry_pipeline_record_t *payload_ptr)
 Publish status.
 
void telemetry_service_notify_replay_available (void)
 Send signal replay available.
 
void telemetry_service_set_online (bool online)
 Set online state.
 
bool telemetry_service_is_online (void)
 Get online state.
 
void telemetry_service_deinit (void)
 Deinitialize telemetry service.
 

Variables

static const char * TAG = "TELEMETRY_SERVICE"
 
static const char * topic_suffixes [] = {"/modbus/live", "/modbus/replay", "/modbus/status"}
 
static char topics [TELEMETRY_SERVICE_TOPICS_MAX][(128U)]
 
static uint8_t topics_qos [TELEMETRY_SERVICE_TOPICS_MAX]
 
static TaskHandle_t telemetry_service_task_handle = NULL
 
static QueueHandle_t live_queue = NULL
 
static SemaphoreHandle_t telemetry_service_state_lock = NULL
 
static bool telemetry_service_is_online_flag = false
 
static mqtt_bridge_t mqtt_bridge
 
esp_timer_handle_t pub_metrics_timer_handle = NULL
 
bool(* publish_replay_cb_func )(void) = NULL
 
runtime_task_config_t telemetry_service_task_config
 

Detailed Description

Implementation of the telemetry service.

Author
Abanoub Salah
  • Initializes MQTT bridge instance
  • Pre-generated publish topics
  • Pre-set topics QOS
  • Enqueue live records
  • Publish live/replay records
  • Holds MQTT connectivity status
  • Task process live records in queue and process replay if available

Macro Definition Documentation

◆ TELEMETRY_SERVICE_FULL_TOPIC_MAX_SIZE

#define TELEMETRY_SERVICE_FULL_TOPIC_MAX_SIZE   (128U)

MQTT topics maximum size

◆ TELEMETRY_SERVICE_LIVE_QUEUE_LENGTH

#define TELEMETRY_SERVICE_LIVE_QUEUE_LENGTH   (16U)

Live queue length

◆ TELEMETRY_SERVICE_LIVE_QUOTA

#define TELEMETRY_SERVICE_LIVE_QUOTA   (2U)

Live quota

◆ TELEMETRY_SERVICE_PUBLISH_METRICS_PERIOD_US

#define TELEMETRY_SERVICE_PUBLISH_METRICS_PERIOD_US    (300000000UL)

Publish system's metrics period in micro seconds

◆ TELEMETRY_SERVICE_REPLAY_QUOTA

#define TELEMETRY_SERVICE_REPLAY_QUOTA   (1U)

Replay quota

◆ TELEMETRY_SERVICE_TASK_DELAY_TIME_MS

#define TELEMETRY_SERVICE_TASK_DELAY_TIME_MS    (30U)

Replay task delay time after a burst in micro seconds

Function Documentation

◆ generate_topics_internal()

static esp_err_t generate_topics_internal ( const char *  device_id_ptr)
static

Generate publishing topics.

Generate publishing topics according to a preset template defined by topics base and topic_suffixes

Parameters
device_id_ptrPointer to device id
Returns
esp_err_t Generate result
Return values
ESP_OKGenerate success
ESP_ERR_INVALID_ARGProvided invalid argument(s)
ESP_ERR_INVALID_SIZETopic size exceeded maximum

< MQTT topics maximum size

< MQTT topics maximum size

Here is the caller graph for this function:

◆ handle_system_metrics_internal()

static void handle_system_metrics_internal ( void *  arg_void_ptr)
static

Handle sending system's metrics.

Get system metrics snapshot, stamp it and publish it

Parameters
arg_void_ptrVoid pointer to argument
Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_live_quota_internal()

static void process_live_quota_internal ( void  )
static

Process live records quota.

< Live quota

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_replay_quota_internal()

static bool process_replay_quota_internal ( void  )
static

Process replay records quota.

Returns
true on last replay process success false otherwise

< Replay quota

Here is the call graph for this function:
Here is the caller graph for this function:

◆ publish_to_topic_helper()

static bool publish_to_topic_helper ( const telemetry_pipeline_record_t payload_ptr,
telemetry_service_topics_t  topic 
)
static

Publish to topic.

Parameters
payload_ptrPointer to record
topicTopic
Returns
true if published false otherwise
Here is the call graph for this function:
Here is the caller graph for this function:

◆ telemetry_service_enqueue_live()

bool telemetry_service_enqueue_live ( const telemetry_pipeline_record_t payload_ptr)

Enqueue live record.

Parameters
payload_ptrPointer to record
Returns
true on success false otherwise
Here is the caller graph for this function:

◆ telemetry_service_init()

bool telemetry_service_init ( mqtt_bridge_config_t mqtt_bridge_config_ptr,
bool(*)(void)  replay_check_cb 
)

Initialize telemetry service.

Initialize MQTT bridge instance and create queue

Parameters
mqtt_bridge_config_ptrPointer to MQTT bridge configuration structure
replay_check_cbCallback for replay check
Returns
true on Initialize success false otherwise

< Live queue length

< Publish system's metrics period in micro seconds

Here is the call graph for this function:
Here is the caller graph for this function:

◆ telemetry_service_is_online()

bool telemetry_service_is_online ( void  )

Get online state.

Returns
true on online false otherwise
Here is the caller graph for this function:

◆ telemetry_service_publish_live()

bool telemetry_service_publish_live ( const telemetry_pipeline_record_t payload_ptr)

Publish live record.

Parameters
payload_ptrPointer to record
Returns
true on success false otherwise
Here is the call graph for this function:
Here is the caller graph for this function:

◆ telemetry_service_publish_replay()

bool telemetry_service_publish_replay ( const telemetry_pipeline_record_t payload_ptr)

Publish replay record.

Parameters
payload_ptrPointer to record
Returns
true on success false otherwise
Here is the call graph for this function:
Here is the caller graph for this function:

◆ telemetry_service_publish_status()

bool telemetry_service_publish_status ( const telemetry_pipeline_record_t payload_ptr)

Publish status.

Parameters
payload_ptrPointer to record
Returns
true on success false otherwise
Here is the call graph for this function:
Here is the caller graph for this function:

◆ telemetry_service_set_online()

void telemetry_service_set_online ( bool  online)

Set online state.

Parameters
onlineOnline state
Here is the caller graph for this function:

◆ telemetry_service_task()

static void telemetry_service_task ( void *  parameters_void_ptr)
static

Process live queue and replay if available.

Parameters
parameters_void_ptrVoid pointer to task parameters

< Replay task delay time after a burst in micro seconds

Here is the call graph for this function:

Variable Documentation

◆ live_queue

QueueHandle_t live_queue = NULL
static

Telemetry Service live queue handle

◆ mqtt_bridge

mqtt_bridge_t mqtt_bridge
static

MQTT Bridge instance

◆ pub_metrics_timer_handle

esp_timer_handle_t pub_metrics_timer_handle = NULL

Timer handle for system's metrics publishing

◆ publish_replay_cb_func

bool(* publish_replay_cb_func) (void) ( void  ) = NULL

Callback for publishing replay

◆ TAG

const char* TAG = "TELEMETRY_SERVICE"
static

Telemetry Service TAG name

◆ telemetry_service_is_online_flag

bool telemetry_service_is_online_flag = false
static

Telemetry Service state flag

◆ telemetry_service_state_lock

SemaphoreHandle_t telemetry_service_state_lock = NULL
static

Telemetry Service state flag lock

◆ telemetry_service_task_config

runtime_task_config_t telemetry_service_task_config
Initial value:
.arg = NULL,
static void telemetry_service_task(void *parameters_void_ptr)
Process live queue and replay if available.
Definition telemetry_service.c:195
static void telemetry_service_stop_task(void)
Stop telemetry service task.
Definition telemetry_service.c:244
static TaskHandle_t telemetry_service_task_handle
Definition telemetry_service.c:47
#define TELEMETRY_SERVICE_TASK_STACK_DEPTH
Definition telemetry_service.h:20
#define TELEMETRY_SERVICE_TASK_PRIORITY
Definition telemetry_service.h:21
#define TELEMETRY_SERVICE_TASK_NAME
Definition telemetry_service.h:19
#define TELEMETRY_SERVICE_TASK_CPU_AFFINITY
Definition telemetry_service.h:22

Telemetry Service task configuration structure

◆ telemetry_service_task_handle

TaskHandle_t telemetry_service_task_handle = NULL
static

Telemetry Service task handle

◆ topic_suffixes

const char* topic_suffixes[] = {"/modbus/live", "/modbus/replay", "/modbus/status"}
static

Topics preset suffixes

◆ topics

char topics[TELEMETRY_SERVICE_TOPICS_MAX][(128U)]
static

Full topics

◆ topics_qos

uint8_t topics_qos[TELEMETRY_SERVICE_TOPICS_MAX]
static

Topic QoS