Loading...
Searching...
No Matches
topic.c
1/***********************************************************************************
2 * @file topic.c *
3 * @brief Implementation of the Topic-based Publish/Subscribe system. *
4 * *
5 * @{ *
6 ***********************************************************************************/
7
8#include "_topic.h"
9#include "topic.h"
10
11#include "FreeRTOS.h"
12#include "queue.h"
13
14/*
15 * TODO:
16 * Add argument for topic message length. Messages on topics should be packaged
17 * by the publish/comment methods into structs containing the data and a field
18 * defining the length of the received data in subscription and publication
19 * inboxes.
20 */
21
22/* =============================================================================== */
43bool Topic_comment(Topic *topic, uint8_t *comment) {
44 // Early return if invalid argument
45 if (topic == NULL || comment == NULL)
46 return false;
47
48 // Send comment data to author queue
49 xQueueSend(topic->commentInbox, comment, 2);
50 return true; // Returns true if args were valid and send was attempted
51}
52
53/* =============================================================================== */
74bool Topic_publish(PrivateTopic *topic, uint8_t *article) {
75 // Early return if invalid argument
76 if (topic == NULL || article == NULL)
77 return false;
78
79 // Calculate first and last addresses in subscription list
80 const QueueHandle_t *start = topic->subscriptions;
81 const QueueHandle_t *end = topic->subscriptions + topic->numSubscriptions;
82
83 // Send article data to each subscriber queue in the list
84 for (const QueueHandle_t *entry = start; entry < end; entry++) {
85 // Check if the retrieved handle is valid before sending
86 // (Subscribers are responsible for creating their queues)
87 if (entry != NULL)
88 // Send data to the subscriber's queue (non-blocking)
89 xQueueSend(*entry, article, 0);
90
91 // Silently ignore if the handle is NULL
92 // (subscriber might not have created queue yet/correctly)
93 }
94 return true; // Returns true if args were valid and loop completed
95}
96
bool Topic_publish(PrivateTopic *topic, uint8_t *article)
Publish an "article" to all discovered subscribers of a topic.
Definition topic.c:74
const SubInbox_t * subscriptions
Subscription inbox array pointer.
Definition _topic.h:75
size_t numSubscriptions
Number of subscriptions to the topic.
Definition _topic.h:76
Internal representation of a Topic instance.
Definition _topic.h:73
PubInbox_t commentInbox
Queue handle used to send messages back to topic authors.
Definition topic.h:72
bool Topic_comment(Topic *topic, uint8_t *comment)
Send a "comment" back to the originator of a topic.
Definition topic.c:43
Public representation of a Topic.
Definition topic.h:71