ICARUS OS (Intelligent Cooperative Architecture for Real-time Unified Systems) 0.1.0
Preemptive Real-Time Operating System for ARM Cortex-M7
Loading...
Searching...
No Matches
sb.c File Reference

Software Bus — publish/subscribe message router implementation. More...

#include "icarus/icarus.h"
#include "icarus/svc.h"
#include <string.h>

Go to the source code of this file.

Data Structures

struct  sb_route_t
 Single route entry: one msg_id → up to SB_MAX_SUBS_PER_MSG pipes. More...
 

Functions

static sb_route_tfind_route (sb_msg_id_t msg_id)
 Find the route entry for msg_id.
 
static sb_route_talloc_route (void)
 Allocate a fresh route slot.
 
void __sb_init (void)
 Privileged implementation of sb_init().
 
bool __sb_subscribe (sb_msg_id_t msg_id, uint8_t pipe_idx)
 Privileged implementation of sb_subscribe().
 
bool __sb_unsubscribe (sb_msg_id_t msg_id, uint8_t pipe_idx)
 Privileged implementation of sb_unsubscribe().
 
uint8_t __sb_publish (sb_msg_id_t msg_id, const uint8_t *data, uint8_t len)
 Privileged implementation of sb_publish().
 
uint8_t __sb_subscriber_count (sb_msg_id_t msg_id)
 Privileged implementation of sb_subscriber_count().
 
uint8_t __sb_route_count (void)
 Privileged implementation of sb_route_count().
 
void sb_init (void)
 Initialize the software bus via SVC gate.
 
bool sb_subscribe (sb_msg_id_t msg_id, uint8_t pipe_idx)
 Subscribe a pipe to a message ID via SVC gate.
 
bool sb_unsubscribe (sb_msg_id_t msg_id, uint8_t pipe_idx)
 Remove a subscription via SVC gate.
 
uint8_t sb_publish (sb_msg_id_t msg_id, const uint8_t *data, uint8_t len)
 Publish a message to all subscribers via SVC gate.
 
uint8_t sb_subscriber_count (sb_msg_id_t msg_id)
 Return the subscriber count for a message ID via SVC gate.
 
uint8_t sb_route_count (void)
 Return the total route count via SVC gate.
 

Variables

static sb_route_t routes [32]
 Route table — privileged DTCM, zero wait-state.
 
static uint8_t route_used
 Number of route slots in use.
 

Detailed Description

Software Bus — publish/subscribe message router implementation.

Version
0.1.0

Static routing table mapping msg_id → pipe_idx[]. Publishing iterates subscribers and enqueues into each pipe that has space.

All mutable state lives in DTCM_DATA_PRIV (privileged-only, zero wait-state on Cortex-M7). All functions are placed in ITCM_FUNC for zero wait-state instruction fetch. Public access goes through SVC call gates; the __-prefixed functions are the privileged implementations.

Thread safety:
Every public function issues an SVC instruction to execute the privileged implementation in handler mode. The SVC handler is non-preemptible, guaranteeing atomic access to the route table.
Author
Souham Biswas
Date
2026

Definition in file sb.c.

Function Documentation

◆ __sb_init()

void __sb_init ( void  )

Privileged implementation of sb_init().

Zeroes the entire route table and resets the used counter.

Definition at line 93 of file sb.c.

References route_used, and routes.

Referenced by sb_init(), and SVC_Handler_C().

◆ __sb_publish()

uint8_t __sb_publish ( sb_msg_id_t  msg_id,
const uint8_t *  data,
uint8_t  len 
)

Privileged implementation of sb_publish().

Parameters
[in]msg_idMessage identifier.
[in]dataPayload bytes.
[in]lenPayload length.
Returns
Number of subscribers successfully enqueued to.

Iterates all subscribers for msg_id. For each pipe, checks capacity via pipe_can_enqueue() before writing. Pipes that are full are silently skipped (best-effort).

Definition at line 173 of file sb.c.

References __pipe_can_enqueue(), __pipe_write_bytes(), sb_route_t::count, find_route(), and sb_route_t::pipes.

Referenced by sb_publish(), and SVC_Handler_C().

◆ __sb_route_count()

uint8_t __sb_route_count ( void  )

Privileged implementation of sb_route_count().

Returns
Number of distinct msg_ids with at least one subscriber.

Definition at line 209 of file sb.c.

References route_used.

Referenced by sb_route_count(), and SVC_Handler_C().

◆ __sb_subscribe()

bool __sb_subscribe ( sb_msg_id_t  msg_id,
uint8_t  pipe_idx 
)

Privileged implementation of sb_subscribe().

Parameters
[in]msg_idMessage identifier.
[in]pipe_idxKernel pipe index to bind.
Return values
trueSubscription added.
falseTable full, subscriber limit hit, or duplicate pair.

If no route exists for msg_id a new slot is allocated. Duplicate (msg_id, pipe_idx) pairs are rejected to prevent double-delivery.

Definition at line 110 of file sb.c.

References alloc_route(), sb_route_t::count, find_route(), sb_route_t::msg_id, sb_route_t::pipes, and SB_MAX_SUBS_PER_MSG.

Referenced by sb_subscribe(), and SVC_Handler_C().

◆ __sb_subscriber_count()

uint8_t __sb_subscriber_count ( sb_msg_id_t  msg_id)

Privileged implementation of sb_subscriber_count().

Parameters
[in]msg_idMessage identifier.
Returns
Active subscriber count, or 0 if no route exists.

Definition at line 200 of file sb.c.

References sb_route_t::count, and find_route().

Referenced by sb_subscriber_count(), and SVC_Handler_C().

◆ __sb_unsubscribe()

bool __sb_unsubscribe ( sb_msg_id_t  msg_id,
uint8_t  pipe_idx 
)

Privileged implementation of sb_unsubscribe().

Parameters
[in]msg_idMessage identifier.
[in]pipe_idxPipe to remove.
Return values
trueSubscription found and removed (last-swap compaction).
falseNo matching subscription.

Definition at line 144 of file sb.c.

References sb_route_t::count, find_route(), and sb_route_t::pipes.

Referenced by sb_unsubscribe(), and SVC_Handler_C().

◆ alloc_route()

static sb_route_t * alloc_route ( void  )
static

Allocate a fresh route slot.

Returns
Pointer to new slot, or NULL if table is full.
Note
Caller must hold critical section.

Definition at line 75 of file sb.c.

References route_used, routes, SB_MAX_ROUTES, and sb_route_t::used.

Referenced by __sb_subscribe().

◆ find_route()

static sb_route_t * find_route ( sb_msg_id_t  msg_id)
static

Find the route entry for msg_id.

Returns
Pointer to route, or NULL if not found.
Note
Caller must hold critical section.

Definition at line 61 of file sb.c.

References route_used, and routes.

Referenced by __sb_publish(), __sb_subscribe(), __sb_subscriber_count(), and __sb_unsubscribe().

◆ sb_init()

void sb_init ( void  )

Initialize the software bus via SVC gate.

Initialize the software bus.

Clears all routes in privileged mode.

Definition at line 221 of file sb.c.

References __sb_init(), and SVC_SB_INIT.

◆ sb_publish()

uint8_t sb_publish ( sb_msg_id_t  msg_id,
const uint8_t *  data,
uint8_t  len 
)

Publish a message to all subscribers via SVC gate.

Publish a message to all subscribers of msg_id.

Parameters
[in]msg_idMessage identifier.
[in]dataPayload bytes.
[in]lenPayload length.
Returns
Number of subscribers successfully enqueued to.

Definition at line 288 of file sb.c.

References __sb_publish(), and SVC_SB_PUBLISH.

◆ sb_route_count()

uint8_t sb_route_count ( void  )

Return the total route count via SVC gate.

Return the total number of route entries in use.

Returns
Number of distinct msg_ids with at least one subscriber.

Definition at line 334 of file sb.c.

References __sb_route_count(), and SVC_SB_ROUTE_COUNT.

◆ sb_subscribe()

bool sb_subscribe ( sb_msg_id_t  msg_id,
uint8_t  pipe_idx 
)

Subscribe a pipe to a message ID via SVC gate.

Subscribe a pipe to a message ID.

Parameters
[in]msg_idMessage identifier.
[in]pipe_idxKernel pipe index.
Return values
trueSubscription added.
falseTable full, limit hit, or duplicate.

Definition at line 236 of file sb.c.

References __sb_subscribe(), and SVC_SB_SUBSCRIBE.

◆ sb_subscriber_count()

uint8_t sb_subscriber_count ( sb_msg_id_t  msg_id)

Return the subscriber count for a message ID via SVC gate.

Return the number of active subscriptions for a message ID.

Parameters
[in]msg_idMessage identifier.
Returns
Active subscriber count, or 0 if no route exists.

Definition at line 313 of file sb.c.

References __sb_subscriber_count(), and SVC_SB_SUBSCRIBER_COUNT.

◆ sb_unsubscribe()

bool sb_unsubscribe ( sb_msg_id_t  msg_id,
uint8_t  pipe_idx 
)

Remove a subscription via SVC gate.

Remove a subscription.

Parameters
[in]msg_idMessage identifier.
[in]pipe_idxPipe to unsubscribe.
Return values
trueSubscription found and removed.
falseNo matching subscription.

Definition at line 262 of file sb.c.

References __sb_unsubscribe(), and SVC_SB_UNSUBSCRIBE.

Variable Documentation

◆ route_used

uint8_t route_used
static

Number of route slots in use.

Definition at line 50 of file sb.c.

Referenced by __sb_init(), __sb_route_count(), alloc_route(), and find_route().

◆ routes

sb_route_t routes[32]
static

Route table — privileged DTCM, zero wait-state.

Definition at line 47 of file sb.c.

Referenced by __sb_init(), alloc_route(), and find_route().