Skip to content

File syn_pubsub.h

FileList > src > syntropic > util > syn_pubsub.h

Go to the source code of this file

Synchronous publish/subscribe event broker. More...

  • #include "../common/syn_defs.h"
  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_PubSubBroker
PubSub broker instance — subscription array + count.
struct SYN_PubSubSub
A single subscription record.

Public Types

Type Name
typedef void(* SYN_PubSubHandler
Event callback function signature.

Public Functions

Type Name
void syn_pubsub_init (SYN_PubSubBroker * broker, SYN_PubSubSub * sub_array, size_t capacity)
Initialize a pubsub broker.
void syn_pubsub_publish (SYN_PubSubBroker * broker, uint16_t topic, const void * payload, size_t len)
Publish an event to all subscribers of a topic.
bool syn_pubsub_subscribe (SYN_PubSubBroker * broker, uint16_t topic, SYN_PubSubHandler handler, void * ctx)
Subscribe to a topic.
bool syn_pubsub_unsubscribe (SYN_PubSubBroker * broker, uint16_t topic, SYN_PubSubHandler handler)
Unsubscribe from a topic.

Public Static Functions

Type Name
size_t syn_pubsub_count (const SYN_PubSubBroker * broker)
Get current number of active subscriptions.
void syn_pubsub_reset (SYN_PubSubBroker * broker)
Reset the broker, removing all subscriptions.

Macros

Type Name
define SYN_PUBSUB_TOPIC_ALL 0xFFFF

Detailed Description

Provides a decoupled event system. Modules can subscribe to specific topics and receive callbacks when events are published.

Uses a static caller-provided array for storing subscriptions (zero allocation).

Public Types Documentation

typedef SYN_PubSubHandler

Event callback function signature.

typedef void(* SYN_PubSubHandler) (uint16_t topic, const void *payload, size_t len, void *ctx);

Parameters:

  • topic The topic ID that triggered this callback.
  • payload Pointer to the event data (can be NULL).
  • len Size of the event data in bytes (can be 0).
  • ctx User context pointer provided at subscription.

Public Functions Documentation

function syn_pubsub_init

Initialize a pubsub broker.

void syn_pubsub_init (
    SYN_PubSubBroker * broker,
    SYN_PubSubSub * sub_array,
    size_t capacity
) 

Parameters:

  • broker Pointer to broker instance.
  • sub_array Pointer to an array of SYN_PubSubSub structures.
  • capacity Number of elements in sub_array.

function syn_pubsub_publish

Publish an event to all subscribers of a topic.

void syn_pubsub_publish (
    SYN_PubSubBroker * broker,
    uint16_t topic,
    const void * payload,
    size_t len
) 

Parameters:

  • broker Pointer to broker instance.
  • topic Topic ID of the event.
  • payload Pointer to event data.
  • len Size of event data in bytes.

function syn_pubsub_subscribe

Subscribe to a topic.

bool syn_pubsub_subscribe (
    SYN_PubSubBroker * broker,
    uint16_t topic,
    SYN_PubSubHandler handler,
    void * ctx
) 

Parameters:

  • broker Pointer to broker instance.
  • topic Topic ID to listen for, or SYN_PUBSUB_TOPIC_ALL.
  • handler Callback function to invoke.
  • ctx Optional user context pointer.

Returns:

true if subscribed successfully, false if broker is full.


function syn_pubsub_unsubscribe

Unsubscribe from a topic.

bool syn_pubsub_unsubscribe (
    SYN_PubSubBroker * broker,
    uint16_t topic,
    SYN_PubSubHandler handler
) 

Parameters:

  • broker Pointer to broker instance.
  • topic Topic ID to unsubscribe from.
  • handler The specific callback function to remove.

Returns:

true if removed, false if not found.


Public Static Functions Documentation

function syn_pubsub_count

Get current number of active subscriptions.

static inline size_t syn_pubsub_count (
    const SYN_PubSubBroker * broker
) 

Parameters:

  • broker Broker.

Returns:

Subscription count.


function syn_pubsub_reset

Reset the broker, removing all subscriptions.

static inline void syn_pubsub_reset (
    SYN_PubSubBroker * broker
) 

Parameters:

  • broker Broker.

Macro Definition Documentation

define SYN_PUBSUB_TOPIC_ALL

#define SYN_PUBSUB_TOPIC_ALL `0xFFFF`

Special topic ID used to subscribe to ALL events.



The documentation for this class was generated from the following file src/syntropic/util/syn_pubsub.h