All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
9#pragma once
10
40#include <assert.h>
41#include <stdint.h>
42
43#include "list.h"
44#include "msg.h"
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
53typedef struct {
55 uint16_t id;
56} msg_bus_t;
57
67
72#define MSB_BUS_PID_FLAG (1U << ((8 * sizeof(kernel_pid_t)) - 1))
73
85
99static inline uint16_t msg_bus_get_type(const msg_t *msg)
100{
101 if (msg->sender_pid & MSB_BUS_PID_FLAG) {
102 return msg->type & 0x1F;
103 }
104 else {
105 return msg->type;
106 }
107}
108
121{
122 return msg->sender_pid & ~MSB_BUS_PID_FLAG;
123}
124
140static 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
164
176
189
198static 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
212static 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
235
250static 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
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:135
Messaging API for inter process communication.
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:138
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:99
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
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
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:198
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
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
#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:72
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:39
Message bus subscriber entry.
Definition msg_bus.h:62
kernel_pid_t pid
Subscriber PID.
Definition msg_bus.h:65
uint32_t event_mask
Bitmask of event classes.
Definition msg_bus.h:64
list_node_t next
next subscriber
Definition msg_bus.h:63
A message bus is just a list of subscribers.
Definition msg_bus.h:53
list_node_t subs
List of subscribers to the bus.
Definition msg_bus.h:54
uint16_t id
Message Bus ID.
Definition msg_bus.h:55
Describes a message object which can be sent between threads.
Definition msg.h:195
uint16_t type
Type field.
Definition msg.h:198
kernel_pid_t sender_pid
PID of sending thread.
Definition msg.h:196