msg_bus.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 ML!PA Consulting GmbH
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
38 #ifndef MSG_BUS_H
39 #define MSG_BUS_H
40 
41 #include <assert.h>
42 #include <stdint.h>
43 
44 #include "list.h"
45 #include "msg.h"
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
54 typedef struct {
56  uint16_t id;
57 } msg_bus_t;
58 
63 typedef struct {
65  uint32_t event_mask;
68 
73 #define MSB_BUS_PID_FLAG (1U << ((8 * sizeof(kernel_pid_t)) - 1))
74 
85 void msg_bus_init(msg_bus_t *bus);
86 
100 static inline uint16_t msg_bus_get_type(const msg_t *msg)
101 {
102  if (msg->sender_pid & MSB_BUS_PID_FLAG) {
103  return msg->type & 0x1F;
104  } else {
105  return msg->type;
106  }
107 }
108 
120 static inline kernel_pid_t msg_bus_get_sender_pid(const msg_t *msg)
121 {
122  return msg->sender_pid & ~MSB_BUS_PID_FLAG;
123 }
124 
140 static inline bool msg_is_from_bus(const msg_bus_t *bus, const msg_t *msg)
141 {
142  if (bus != NULL && (bus->id != (msg->type >> 5))) {
143  return false;
144  }
145 
146  return msg->sender_pid & MSB_BUS_PID_FLAG;
147 }
148 
163 void msg_bus_attach(msg_bus_t *bus, msg_bus_entry_t *entry);
164 
175 void msg_bus_detach(msg_bus_t *bus, msg_bus_entry_t *entry);
176 
189 
198 static inline void msg_bus_subscribe(msg_bus_entry_t *entry, uint8_t type)
199 {
200  assert(type < 32);
201  entry->event_mask |= (1UL << type);
202 }
203 
212 static inline void msg_bus_unsubscribe(msg_bus_entry_t *entry, uint8_t type)
213 {
214  assert(type < 32);
215  entry->event_mask &= ~(1UL << type);
216 }
217 
234 int msg_send_bus(msg_t *m, msg_bus_t *bus);
235 
250 static inline int msg_bus_post(msg_bus_t *bus, uint8_t type, const void *arg)
251 {
252  assert(type < 32);
253 
254  msg_t m = {
255  .type = type | ((bus->id) << 5),
256  .content.ptr = (void *)arg,
257  };
258 
259  return msg_send_bus(&m, bus);
260 }
261 
262 #ifdef __cplusplus
263 }
264 #endif
265 
266 #endif /* MSG_BUS_H */
267 
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
msg_bus_entry_t::next
list_node_t next
next subscriber
Definition: msg_bus.h:64
msg_bus_subscribe
static void msg_bus_subscribe(msg_bus_entry_t *entry, uint8_t type)
Subscribe to an event on the message bus.
Definition: msg_bus.h:198
assert
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
msg_bus_get_entry
msg_bus_entry_t * msg_bus_get_entry(msg_bus_t *bus)
Get the message bus entry for the current thread.
msg_bus_entry_t
Message bus subscriber entry.
Definition: msg_bus.h:63
msg_bus_attach
void msg_bus_attach(msg_bus_t *bus, msg_bus_entry_t *entry)
Attach a thread to a message bus.
assert.h
POSIX.1-2008 compliant version of the assert macro.
msg_t::type
uint16_t type
Type field.
Definition: msg.h:188
msg_bus_post
static int msg_bus_post(msg_bus_t *bus, uint8_t type, const void *arg)
Post a message to a bus.
Definition: msg_bus.h:250
msg_bus_entry_t::event_mask
uint32_t event_mask
Bitmask of event classes.
Definition: msg_bus.h:65
msg.h
Messaging API for inter process communication.
msg_bus_t::id
uint16_t id
Message Bus ID.
Definition: msg_bus.h:56
msg_bus_get_type
static uint16_t msg_bus_get_type(const msg_t *msg)
Get the message type of a message bus message.
Definition: msg_bus.h:100
msg_send_bus
int msg_send_bus(msg_t *m, msg_bus_t *bus)
Post a pre-assembled message to a bus.
msg_bus_get_sender_pid
static kernel_pid_t msg_bus_get_sender_pid(const msg_t *msg)
Get the sender PID of a message bus message.
Definition: msg_bus.h:120
msg_bus_unsubscribe
static void msg_bus_unsubscribe(msg_bus_entry_t *entry, uint8_t type)
Unsubscribe from an event on the message bus.
Definition: msg_bus.h:212
msg_bus_init
void msg_bus_init(msg_bus_t *bus)
Initialize a message bus.
msg_bus_detach
void msg_bus_detach(msg_bus_t *bus, msg_bus_entry_t *entry)
Remove a thread from a message bus.
msg_is_from_bus
static bool msg_is_from_bus(const msg_bus_t *bus, const msg_t *msg)
Check if a message originates from a bus.
Definition: msg_bus.h:140
msg_bus_t
A message bus is just a list of subscribers.
Definition: msg_bus.h:54
MSB_BUS_PID_FLAG
#define MSB_BUS_PID_FLAG
Flag set on sender_pid of msg_t that indicates that the message was sent over a bus.
Definition: msg_bus.h:73
msg_bus_entry_t::pid
kernel_pid_t pid
Subscriber PID.
Definition: msg_bus.h:66
msg_bus_t::subs
list_node_t subs
List of subscribers to the bus.
Definition: msg_bus.h:55
list_node
List node structure.
Definition: list.h:40
msg_t::sender_pid
kernel_pid_t sender_pid
PID of sending thread.
Definition: msg.h:186
msg_t
Describes a message object which can be sent between threads.
Definition: msg.h:185
list.h
Intrusive linked list.