Loading...
Searching...
No Matches
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
48extern "C" {
49#endif
50
54typedef struct {
56 uint16_t id;
57} msg_bus_t;
58
68
73#define MSB_BUS_PID_FLAG (1U << ((8 * sizeof(kernel_pid_t)) - 1))
74
86
100static 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 }
105 else {
106 return msg->type;
107 }
108}
109
122{
123 return msg->sender_pid & ~MSB_BUS_PID_FLAG;
124}
125
141static inline bool msg_is_from_bus(const msg_bus_t *bus, const msg_t *msg)
142{
143 if (bus != NULL && (bus->id != (msg->type >> 5))) {
144 return false;
145 }
146
147 return msg->sender_pid & MSB_BUS_PID_FLAG;
148}
149
165
177
190
199static inline void msg_bus_subscribe(msg_bus_entry_t *entry, uint8_t type)
200{
201 assert(type < 32);
202 entry->event_mask |= (1UL << type);
203}
204
213static inline void msg_bus_unsubscribe(msg_bus_entry_t *entry, uint8_t type)
214{
215 assert(type < 32);
216 entry->event_mask &= ~(1UL << type);
217}
218
236
251static inline int msg_bus_post(msg_bus_t *bus, uint8_t type, const void *arg)
252{
253 assert(type < 32);
254
255 msg_t m = {
256 .type = type | ((bus->id) << 5),
257 .content.ptr = (void *)arg,
258 };
259
260 return msg_send_bus(&m, bus);
261}
262
263#ifdef __cplusplus
264}
265#endif
266
267#endif /* MSG_BUS_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:136
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:139
Intrusive linked list.
void msg_bus_init(msg_bus_t *bus)
Initialize a message bus.
msg_bus_entry_t * msg_bus_get_entry(msg_bus_t *bus)
Get the message bus entry for the current thread.
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
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:251
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:213
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:199
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:141
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:121
#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
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:40
Message bus subscriber entry.
Definition msg_bus.h:63
kernel_pid_t pid
Subscriber PID.
Definition msg_bus.h:66
uint32_t event_mask
Bitmask of event classes.
Definition msg_bus.h:65
list_node_t next
next subscriber
Definition msg_bus.h:64
A message bus is just a list of subscribers.
Definition msg_bus.h:54
list_node_t subs
List of subscribers to the bus.
Definition msg_bus.h:55
uint16_t id
Message Bus ID.
Definition msg_bus.h:56
Describes a message object which can be sent between threads.
Definition msg.h:196
uint16_t type
Type field.
Definition msg.h:199
kernel_pid_t sender_pid
PID of sending thread.
Definition msg.h:197
Definitions for parsing and composition of DNS messages.