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

Implementation of the event bus. More...

#include "event_bus.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "runtime_tasks.h"
#include "string.h"
Include dependency graph for event_bus.c:

Data Structures

struct  subscriber_t
 Subscriber structure. More...
 

Macros

#define EVENT_BUS_MAX_SUBSCRIBERS   (8U)
 
#define EVENT_BUS_COUNT_BACKOFF_WATERMARK   (5U)
 

Functions

static void event_bus_task (void *parameters_void_ptr)
 Event bus task.
 
static void event_bus_stop_task (void)
 Stop event bus task.
 
esp_err_t event_bus_init (size_t queue_size)
 Initialize event bus.
 
bool event_bus_publish (const event_bus_t *event_ptr)
 Publish an event.
 
int8_t event_bus_subscribe (event_bus_event_t type, event_bus_cb_t cb, void *ctx)
 Subscribe to event.
 
bool event_bus_unsubscribe (int8_t slot)
 Unsubscribe from event.
 
void event_bus_deinit (void)
 deinitialize event bus
 

Variables

static const char * TAG = "EVENT_BUS"
 
static TaskHandle_t event_bus_task_handle = NULL
 
static QueueHandle_t event_queue
 
static subscriber_t subscribers [(8U)]
 
runtime_task_config_t event_bus_task_config
 

Detailed Description

Implementation of the event bus.

Author
Abanoub Salah
  • Uses queues for publishers
  • Starts a task for dispatching events to subscribers
  • Subscribers can subscribe if slots are available which are limited
  • Subscribers can unsubscribe using their slot ID provided during subscribing

Macro Definition Documentation

◆ EVENT_BUS_COUNT_BACKOFF_WATERMARK

#define EVENT_BUS_COUNT_BACKOFF_WATERMARK   (5U)

Task backoff when queue reach watermark

◆ EVENT_BUS_MAX_SUBSCRIBERS

#define EVENT_BUS_MAX_SUBSCRIBERS   (8U)

Maximum possible subscribers

Function Documentation

◆ event_bus_init()

esp_err_t event_bus_init ( size_t  queue_size)

Initialize event bus.

Parameters
queue_sizeSize of the event queue
Returns
esp_err_t Initialize result
Return values
ESP_OKInitialize success
BLACKBOX_FAILFailed to create event task
ESP_ERR_INVALID_ARGProvided invalid argument(s)
ESP_ERR_NO_MEMNo available memory for resource allocation
Here is the call graph for this function:
Here is the caller graph for this function:

◆ event_bus_publish()

bool event_bus_publish ( const event_bus_t event_ptr)

Publish an event.

Publish an event by queue it to the event bus queue

Parameters
event_ptrPointer to an event
Returns
bool true on publish false otherwise
Here is the caller graph for this function:

◆ event_bus_subscribe()

int8_t event_bus_subscribe ( event_bus_event_t  type,
event_bus_cb_t  cb,
void *  ctx 
)

Subscribe to event.

Parameters
typeEvent type
cbCallback for when event happens
ctxContext for the callback
Returns
int8_t Subscription slot number and '-1' On fail

< Maximum possible subscribers

Here is the caller graph for this function:

◆ event_bus_task()

static void event_bus_task ( void *  parameters_void_ptr)
static

Event bus task.

Loops over subscribers calling them if their event match current one

Parameters
parameters_void_ptrVoid pointer to task parameters

< Maximum possible subscribers

< Task backoff when queue reach watermark

◆ event_bus_unsubscribe()

bool event_bus_unsubscribe ( int8_t  slot)

Unsubscribe from event.

Parameters
slotSubscription slot number
Returns
true on unsubscription false otherwise

< Maximum possible subscribers

Variable Documentation

◆ event_bus_task_config

runtime_task_config_t event_bus_task_config
Initial value:
.entry = event_bus_task,
.arg = NULL,
.stop_func = event_bus_stop_task}
static void event_bus_task(void *parameters_void_ptr)
Event bus task.
Definition event_bus.c:51
static void event_bus_stop_task(void)
Stop event bus task.
Definition event_bus.c:93
static TaskHandle_t event_bus_task_handle
Definition event_bus.c:37
#define EVENT_BUS_TASK_CPU_AFFINITY
Definition event_bus.h:25
#define EVENT_BUS_TASK_NAME
Definition event_bus.h:22
#define EVENT_BUS_TASK_STACK_DEPTH
Definition event_bus.h:23
#define EVENT_BUS_TASK_PRIORITY
Definition event_bus.h:24

Event bus task configuration structure

◆ event_bus_task_handle

TaskHandle_t event_bus_task_handle = NULL
static

Event bus task handle

◆ event_queue

QueueHandle_t event_queue
static

Event bus queue handle

◆ subscribers

subscriber_t subscribers[(8U)]
static

Event bus subscribers list

◆ TAG

const char* TAG = "EVENT_BUS"
static

Event bus TAG name