Skyward boardcore
Loading...
Searching...
No Matches
EventBroker.h
Go to the documentation of this file.
1/* Copyright (c) 2015-2018 Skyward Experimental Rocketry
2 * Authors: Luca Erbetta, Matteo Piazzolla
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <assert.h>
27#include <events/Event.h>
28#include <events/FSM.h>
29#include <miosix.h>
30
31#include <cstdint>
32#include <map>
33#include <vector>
34
35#include "ActiveObject.h"
36#include "Singleton.h"
37
38using std::map;
39using std::vector;
40
41using miosix::FastMutex;
42using miosix::Lock;
43using miosix::Thread;
44using miosix::Unlock;
45
46namespace Boardcore
47{
48
49// Minimum guaranteed delay for an event posted with postDelayed(...) in ms
50static constexpr unsigned int EVENT_BROKER_MIN_DELAY = 50;
51
58class EventBroker : public Singleton<EventBroker>, public ActiveObject
59{
60 friend class Singleton<EventBroker>;
61
62public:
69 void post(const Event& ev, uint8_t topic,
70 EventHandlerBase* subscriber = nullptr);
71
82 uint16_t postDelayed(const Event& ev, uint8_t topic, unsigned int delayMs);
83
88 void removeDelayed(uint16_t id);
89
96 void subscribe(EventHandlerBase* subscriber, uint8_t topic);
97
104 void unsubscribe(EventHandlerBase* subscriber, uint8_t topic);
105
111 void unsubscribe(EventHandlerBase* subscriber);
112
117 void clearDelayedEvents();
118
125 EventBroker();
126
127private:
131 struct DelayedEvent
132 {
133 uint16_t schedId;
134 Event event;
135 uint8_t topic;
136 long long deadline;
137 };
138
142 void run() override;
143
144 void deleteSubscriber(vector<EventHandlerBase*>& subVector,
145 EventHandlerBase* subscriber);
146
147 vector<DelayedEvent> delayedEvents;
148 FastMutex mtxDelayedEvents;
149
150 map<uint8_t, vector<EventHandlerBase*>> subscribers;
151 FastMutex mtxSubscribers;
152
153 uint16_t eventCounter = 0;
154
155 PrintLogger logger = Logging::getLogger("eventbroker");
156};
157
158} // namespace Boardcore
void post(const Event &ev, uint8_t topic, EventHandlerBase *subscriber=nullptr)
uint16_t postDelayed(const Event &ev, uint8_t topic, unsigned int delayMs)
void subscribe(EventHandlerBase *subscriber, uint8_t topic)
void removeDelayed(uint16_t id)
void unsubscribe(EventHandlerBase *subscriber, uint8_t topic)
Unsubscribe an EventHandler from a specific topic This function should be used only for testing purpo...
EventBroker()
Construct a new Event Broker object.
void clearDelayedEvents()
Unschedules all pending events. This function should be used only for testing purposes.
static PrintLogger getLogger(const string &name)
This file includes all the types the logdecoder script will decode.
uint8_t Event
Definition Event.h:30