msg_bus.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2020 ML!PA Consulting GmbH
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
37 #include <assert.h>
38 #include <stdint.h>
39 
40 #include "list.h"
41 #include "msg.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
50 typedef struct {
52  uint16_t id;
53 } msg_bus_t;
54 
59 typedef struct {
61  uint32_t event_mask;
64 
69 #define MSB_BUS_PID_FLAG (1U << ((8 * sizeof(kernel_pid_t)) - 1))
70 
82 
96 static inline uint16_t msg_bus_get_type(const msg_t *msg)
97 {
98  if (msg->sender_pid & MSB_BUS_PID_FLAG) {
99  return msg->type & 0x1F;
100  }
101  else {
102  return msg->type;
103  }
104 }
105 
117 static inline kernel_pid_t msg_bus_get_sender_pid(const msg_t *msg)
118 {
119  return msg->sender_pid & ~MSB_BUS_PID_FLAG;
120 }
121 
137 static inline bool msg_is_from_bus(const msg_bus_t *bus, const msg_t *msg)
138 {
139  if (bus != NULL && (bus->id != (msg->type >> 5))) {
140  return false;
141  }
142 
143  return msg->sender_pid & MSB_BUS_PID_FLAG;
144 }
145 
161 
173 
186 
195 static inline void msg_bus_subscribe(msg_bus_entry_t *entry, uint8_t type)
196 {
197  assert(type < 32);
198  entry->event_mask |= (1UL << type);
199 }
200 
209 static inline void msg_bus_unsubscribe(msg_bus_entry_t *entry, uint8_t type)
210 {
211  assert(type < 32);
212  entry->event_mask &= ~(1UL << type);
213 }
214 
232 
247 static inline int msg_bus_post(msg_bus_t *bus, uint8_t type, const void *arg)
248 {
249  assert(type < 32);
250 
251  msg_t m = {
252  .type = type | ((bus->id) << 5),
253  .content.ptr = (void *)arg,
254  };
255 
256  return msg_send_bus(&m, bus);
257 }
258 
259 #ifdef __cplusplus
260 }
261 #endif
262 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
Intrusive linked list.
msg_bus_entry_t * msg_bus_get_entry(msg_bus_t *bus)
Get the message bus entry for the current thread.
void msg_bus_init(msg_bus_t *bus)
Initialize a message bus.
static uint16_t msg_bus_get_type(const msg_t *msg)
Get the message type of a message bus message.
Definition: msg_bus.h:96
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:247
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:209
void msg_bus_attach(msg_bus_t *bus, msg_bus_entry_t *entry)
Attach a thread to a message bus.
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:195
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:137
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:117
#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:69
int msg_send_bus(msg_t *m, msg_bus_t *bus)
Post a pre-assembled message to a bus.
void msg_bus_detach(msg_bus_t *bus, msg_bus_entry_t *entry)
Remove a thread from a message bus.
List node structure.
Definition: list.h:36
Message bus subscriber entry.
Definition: msg_bus.h:59
kernel_pid_t pid
Subscriber PID.
Definition: msg_bus.h:62
uint32_t event_mask
Bitmask of event classes.
Definition: msg_bus.h:61
list_node_t next
next subscriber
Definition: msg_bus.h:60
A message bus is just a list of subscribers.
Definition: msg_bus.h:50
list_node_t subs
List of subscribers to the bus.
Definition: msg_bus.h:51
uint16_t id
Message Bus ID.
Definition: msg_bus.h:52
Describes a message object which can be sent between threads.
Definition: msg.h:192
uint16_t type
Type field.
Definition: msg.h:195
kernel_pid_t sender_pid
PID of sending thread.
Definition: msg.h:193
Definitions for parsing and composition of DNS messages.