Skip to content

File syn_pubsub.h

File List > src > syntropic > util > syn_pubsub.h

Go to the documentation of this file

#ifndef SYN_PUBSUB_H
#define SYN_PUBSUB_H

#include "../common/syn_defs.h"

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define SYN_PUBSUB_TOPIC_ALL 0xFFFF

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

typedef struct {
    uint16_t topic;            
    SYN_PubSubHandler handler; 
    void *ctx;                 
} SYN_PubSubSub;

typedef struct {
    SYN_PubSubSub *subs; 
    size_t capacity;     
    size_t count;        
} SYN_PubSubBroker;

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

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

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

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

static inline size_t syn_pubsub_count(const SYN_PubSubBroker *broker)
{
    return broker->count;
}

static inline void syn_pubsub_reset(SYN_PubSubBroker *broker)
{
    broker->count = 0;
}

#ifdef __cplusplus
}
#endif
#endif // SYN_PUBSUB_H