Solaris Software
Loading...
Searching...
No Matches
pubsub.c File Reference

Priority-aware publish-subscribe packet router implementation. More...

Include dependency graph for pubsub.c:

Go to the source code of this file.

Data Structures

struct  SPP_SERVICES_PUBSUB_Producer_t
struct  SPP_SERVICES_PUBSUB_Consumer_t

Functions

static void SPP_SERVICES_PUBSUB_sendToMailbox (void)
 Dispatches one buffered packet to subscribed consumers.
static void SPP_SERVICES_PUBSUB_sortConsumers (void)
 Sorts the registered consumer array in ascending priority order using insertion sort. Stable — consumers with equal priority retain their registration order. Called once during init before any dispatch.
SPP_RetVal_t SPP_SERVICES_PUBSUB_registerProducer (const SPP_SERVICE_ProducerContract_t *p_producerData, SPP_Kpid_t *p_assignedKpid)
 Registers a producer and assigns it a unique KPID.
SPP_RetVal_t SPP_SERVICES_PUBSUB_registerConsumer (const SPP_SERVICE_ConsumerContract_t *p_consumerData, SPP_Kpid_t subscription)
void SPP_SERVICES_PUBSUB_callProducers (void)
 Calls acquireData on every successfully initialized producer.
SPP_RetVal_t SPP_SERVICES_PUBSUB_publish (SPP_Packet_t *p_pkt)
 Copies a packet into the internal buffer and returns it to the databank.
void SPP_SERVICES_PUBSUB_callConsumers (void)
 Dispatches one buffered packet and executes initialized consumers.
spp_uint8_t SPP_SERVICES_PUBSUB_queueDepth (void)
 Returns the number of packets currently held in the internal FIFO buffer.
void SPP_SERVICES_PUBSUB_signalProducerReady (void)
 Signals that a producer has new data pending.
SPP_RetVal_t SPP_SERVICES_PUBSUB_init (void)
 Sorts consumers by priority and initializes registered modules.
SPP_RetVal_t SPP_SERVICES_PUBSUB_getProducerInitResult (const SPP_SERVICE_ProducerContract_t *p_producerData, SPP_RetVal_t *p_initResult)
 Retrieves the initialization result of a registered producer.
SPP_RetVal_t SPP_SERVICES_PUBSUB_getConsumerInitResult (const SPP_SERVICE_ConsumerContract_t *p_consumerData, SPP_RetVal_t *p_initResult)
 Retrieves the initialization result of a registered consumer.

Variables

static SPP_SERVICES_PUBSUB_Producer_t s_producers [K_SPP_SERVICES_PUBSUB_MAX_PRODUCERS] = {0}
static spp_uint16_t s_registeredProducers = 0U
static SPP_SERVICES_PUBSUB_Consumer_t s_consumers [K_SPP_SERVICES_PUBSUB_MAX_CONSUMERS] = {0}
static spp_uint8_t s_registeredConsumers = 0U
static SPP_Packet_t s_packetBuffer [K_SPP_SERVICES_PUBSUB_BUFFER_SIZE]
static spp_uint8_t s_bufferHead = 0U
static spp_uint8_t s_bufferTail = 0U
static spp_uint8_t s_bufferCount = 0U
static volatile spp_bool_t s_producerReady = false

Detailed Description

Priority-aware publish-subscribe packet router implementation.

Definition in file pubsub.c.

Function Documentation

◆ SPP_SERVICES_PUBSUB_sendToMailbox()

void SPP_SERVICES_PUBSUB_sendToMailbox ( void )
static

Dispatches one buffered packet to subscribed consumers.

Copies the oldest packet from the circular buffer to each successfully initialized consumer whose KPID subscription matches. Delivery is performed through the consumer's deliverToMailbox callback.

After dispatching, advances the buffer head and decrements the count.

Definition at line 257 of file pubsub.c.

References K_SPP_OK, K_SPP_SERVICES_PUBSUB_BUFFER_SIZE, K_SPP_SERVICES_PUBSUB_STEP, s_bufferCount, s_bufferHead, s_consumers, s_packetBuffer, s_registeredConsumers, SPP_Packet_t, and SPP_SERVICES_PUBSUB_sendToMailbox().

Referenced by SPP_SERVICES_PUBSUB_callConsumers(), and SPP_SERVICES_PUBSUB_sendToMailbox().

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

◆ SPP_SERVICES_PUBSUB_sortConsumers()

void SPP_SERVICES_PUBSUB_sortConsumers ( void )
static

Sorts the registered consumer array in ascending priority order using insertion sort. Stable — consumers with equal priority retain their registration order. Called once during init before any dispatch.

Definition at line 293 of file pubsub.c.

References K_SPP_SERVICES_PUBSUB_OFFSET, K_SPP_SERVICES_PUBSUB_SORT_START_INDEX, K_SPP_SERVICES_PUBSUB_ZERO_INDEX, SPP_SERVICES_PUBSUB_Consumer_t::p_contract, SPP_SERVICE_ConsumerContract_t::priority, s_consumers, s_registeredConsumers, and SPP_SERVICES_PUBSUB_sortConsumers().

Referenced by SPP_SERVICES_PUBSUB_init(), and SPP_SERVICES_PUBSUB_sortConsumers().

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

◆ SPP_SERVICES_PUBSUB_registerProducer()

SPP_RetVal_t SPP_SERVICES_PUBSUB_registerProducer ( const SPP_SERVICE_ProducerContract_t * p_producerData,
SPP_Kpid_t * p_assignedKpid )

Registers a producer and assigns it a unique KPID.

Parameters
p_producerDataPointer to the producer contract.
p_assignedKpidPointer where the assigned KPID is stored.
Returns
K_SPP_OK, K_SPP_ERROR_NULL_POINTER or K_SPP_ERROR_REGISTRY_FULL.
Parameters
p_assignedKpidRegisters a consumer with its KPID subscription bitmask.
p_consumerDataPointer to the consumer contract.
subscriptionKPID bitmask identifying subscribed producers.
Returns
K_SPP_OK, K_SPP_ERROR_NULL_POINTER or K_SPP_ERROR_REGISTRY_FULL.

Definition at line 54 of file pubsub.c.

References SPP_SERVICES_PUBSUB_Producer_t::initResult, K_SPP_ERROR, K_SPP_ERROR_NOT_INITIALIZED, K_SPP_ERROR_NULL_POINTER, K_SPP_ERROR_REGISTRY_FULL, K_SPP_OK, K_SPP_SERVICES_PUBSUB_MAX_PRODUCERS, SPP_SERVICES_PUBSUB_Producer_t::kpid, SPP_SERVICES_PUBSUB_Producer_t::p_contract, s_producers, s_registeredProducers, SPP_SERVICES_PUBSUB_registerProducer(), and SPP_Kpid_t::value.

Referenced by CUSTOM_registerConsumerProducer(), and SPP_SERVICES_PUBSUB_registerProducer().

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

◆ SPP_SERVICES_PUBSUB_registerConsumer()

SPP_RetVal_t SPP_SERVICES_PUBSUB_registerConsumer ( const SPP_SERVICE_ConsumerContract_t * p_consumerData,
SPP_Kpid_t subscription )

Definition at line 91 of file pubsub.c.

References SPP_SERVICES_PUBSUB_Consumer_t::initResult, K_SPP_ERROR, K_SPP_ERROR_NOT_INITIALIZED, K_SPP_ERROR_NULL_POINTER, K_SPP_ERROR_REGISTRY_FULL, K_SPP_OK, K_SPP_SERVICES_PUBSUB_MAX_CONSUMERS, SPP_SERVICES_PUBSUB_Consumer_t::p_contract, s_consumers, s_registeredConsumers, SPP_SERVICES_PUBSUB_registerConsumer(), and SPP_SERVICES_PUBSUB_Consumer_t::subcription.

Referenced by CUSTOM_registerConsumerProducer(), and SPP_SERVICES_PUBSUB_registerConsumer().

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

◆ SPP_SERVICES_PUBSUB_callProducers()

void SPP_SERVICES_PUBSUB_callProducers ( void )

Calls acquireData on every successfully initialized producer.

Definition at line 115 of file pubsub.c.

References K_SPP_OK, SPP_SERVICES_PUBSUB_Producer_t::kpid, s_producerReady, s_producers, s_registeredProducers, and SPP_SERVICES_PUBSUB_callProducers().

Referenced by FSM_pubsubCycle(), and SPP_SERVICES_PUBSUB_callProducers().

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

◆ SPP_SERVICES_PUBSUB_publish()

SPP_RetVal_t SPP_SERVICES_PUBSUB_publish ( SPP_Packet_t * p_pkt)

Copies a packet into the internal buffer and returns it to the databank.

Called by producers after filling a packet. The packet is copied by value into the next free slot of the internal circular buffer; the original is returned to the databank pool immediately. Returns K_SPP_ERROR if the buffer is full (packet is still returned to the pool).

Parameters
p_pktPacket to publish (ownership transferred — do not use after this call).
Returns
K_SPP_OK on success, K_SPP_ERROR_NULL_POINTER if p_pkt is NULL, K_SPP_ERROR if the buffer is full.

Definition at line 129 of file pubsub.c.

References K_SPP_ERROR, K_SPP_ERROR_NULL_POINTER, K_SPP_OK, K_SPP_SERVICES_PUBSUB_BUFFER_SIZE, K_SPP_SERVICES_PUBSUB_STEP, s_bufferCount, s_bufferTail, s_packetBuffer, SPP_Packet_t, SPP_SERVICES_DATABANK_returnPacket(), and SPP_SERVICES_PUBSUB_publish().

Referenced by action_emitTelemetry(), SPP_SERVICES_BMP390_acquireData(), SPP_SERVICES_BMP390_DUMMY_acquireData(), SPP_SERVICES_ICM20948_acquireData(), and SPP_SERVICES_PUBSUB_publish().

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

◆ SPP_SERVICES_PUBSUB_callConsumers()

void SPP_SERVICES_PUBSUB_callConsumers ( void )

Dispatches one buffered packet and executes initialized consumers.

Consumers execute in ascending priority order. Lower-priority consumers may be skipped when a producer signals that new data is ready.

Definition at line 152 of file pubsub.c.

References K_SPP_OK, K_SPP_SERVICES_PUBSUB_PREEMPT_PRIORITY, s_consumers, s_producerReady, s_registeredConsumers, SPP_SERVICES_PUBSUB_callConsumers(), and SPP_SERVICES_PUBSUB_sendToMailbox().

Referenced by FSM_pubsubCycle(), and SPP_SERVICES_PUBSUB_callConsumers().

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

◆ SPP_SERVICES_PUBSUB_queueDepth()

spp_uint8_t SPP_SERVICES_PUBSUB_queueDepth ( void )

Returns the number of packets currently held in the internal FIFO buffer.

Definition at line 179 of file pubsub.c.

References s_bufferCount, and SPP_SERVICES_PUBSUB_queueDepth().

Referenced by SPP_SERVICES_PUBSUB_queueDepth().

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

◆ SPP_SERVICES_PUBSUB_signalProducerReady()

void SPP_SERVICES_PUBSUB_signalProducerReady ( void )

Signals that a producer has new data pending.

Must be called from within acquireData() as soon as a DRDY flag is detected, so that callConsumers() can preempt consumers with priority > K_SPP_SERVICES_PUBSUB_PREEMPT_PRIORITY.

Definition at line 184 of file pubsub.c.

References s_producerReady, and SPP_SERVICES_PUBSUB_signalProducerReady().

Referenced by SPP_SERVICES_PUBSUB_signalProducerReady().

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

◆ SPP_SERVICES_PUBSUB_init()

SPP_RetVal_t SPP_SERVICES_PUBSUB_init ( void )

Sorts consumers by priority and initializes registered modules.

Consumers are initialized before producers. Individual initialization results are stored for later inspection.

Returns
K_SPP_OK. Individual module failures do not change this return value.

Definition at line 189 of file pubsub.c.

References SPP_SERVICE_ConsumerContract_t::init, SPP_SERVICE_ProducerContract_t::init, SPP_SERVICES_PUBSUB_Consumer_t::initResult, SPP_SERVICES_PUBSUB_Producer_t::initResult, K_SPP_OK, SPP_SERVICES_PUBSUB_Consumer_t::p_contract, SPP_SERVICES_PUBSUB_Producer_t::p_contract, s_consumers, s_producers, s_registeredConsumers, s_registeredProducers, SPP_SERVICES_PUBSUB_init(), and SPP_SERVICES_PUBSUB_sortConsumers().

Referenced by SPP_CORE_init(), and SPP_SERVICES_PUBSUB_init().

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

◆ SPP_SERVICES_PUBSUB_getProducerInitResult()

SPP_RetVal_t SPP_SERVICES_PUBSUB_getProducerInitResult ( const SPP_SERVICE_ProducerContract_t * p_producerData,
SPP_RetVal_t * p_initResult )

Retrieves the initialization result of a registered producer.

Parameters
p_producerDataPointer to the producer contract.
p_initResultOutput pointer for the initialization result.
Returns
K_SPP_OK if found, K_SPP_ERROR if not registered, or K_SPP_ERROR_NULL_POINTER for invalid pointers.

Definition at line 206 of file pubsub.c.

References SPP_SERVICES_PUBSUB_Producer_t::initResult, K_SPP_ERROR, K_SPP_ERROR_NULL_POINTER, K_SPP_OK, s_producers, s_registeredProducers, and SPP_SERVICES_PUBSUB_getProducerInitResult().

Referenced by guard_startInitializationOfModules(), and SPP_SERVICES_PUBSUB_getProducerInitResult().

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

◆ SPP_SERVICES_PUBSUB_getConsumerInitResult()

SPP_RetVal_t SPP_SERVICES_PUBSUB_getConsumerInitResult ( const SPP_SERVICE_ConsumerContract_t * p_consumerData,
SPP_RetVal_t * p_initResult )

Retrieves the initialization result of a registered consumer.

Parameters
p_consumerDataPointer to the consumer contract.
p_initResultOutput pointer for the initialization result.
Returns
K_SPP_OK if found, K_SPP_ERROR if not registered, or K_SPP_ERROR_NULL_POINTER for invalid pointers.

Definition at line 225 of file pubsub.c.

References SPP_SERVICES_PUBSUB_Consumer_t::initResult, K_SPP_ERROR, K_SPP_ERROR_NULL_POINTER, K_SPP_OK, s_consumers, s_registeredConsumers, and SPP_SERVICES_PUBSUB_getConsumerInitResult().

Referenced by guard_startInitializationOfModules(), and SPP_SERVICES_PUBSUB_getConsumerInitResult().

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

Variable Documentation

◆ s_producers

◆ s_registeredProducers

◆ s_consumers

◆ s_registeredConsumers

◆ s_packetBuffer

◆ s_bufferHead

spp_uint8_t s_bufferHead = 0U
static

Definition at line 38 of file pubsub.c.

Referenced by SPP_SERVICES_PUBSUB_sendToMailbox().

◆ s_bufferTail

spp_uint8_t s_bufferTail = 0U
static

Definition at line 39 of file pubsub.c.

Referenced by SPP_SERVICES_PUBSUB_publish().

◆ s_bufferCount

spp_uint8_t s_bufferCount = 0U
static

◆ s_producerReady

volatile spp_bool_t s_producerReady = false
static