Skyward boardcore
Loading...
Searching...
No Matches
EventSniffer.h
Go to the documentation of this file.
1/* Copyright (c) 2019 Skyward Experimental Rocketry
2 * Author: Luca Erbetta
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 <events/EventBroker.h>
26
27#include <functional>
28#include <iostream>
29#include <string>
30#include <vector>
31
32using std::cout;
33using std::function;
34using std::string;
35using std::vector;
36
37namespace Boardcore
38{
39
45{
46 using OnEventReceived = function<void(uint8_t, uint8_t)>;
47
48public:
55 EventSniffer(EventBroker& broker, vector<uint8_t> topics,
56 OnEventReceived onEventReceived)
57 : broker(broker), onEventReceived(onEventReceived)
58 {
59 for (uint8_t t : topics)
60 sniffers.push_back(new Sniffer(*this, t));
61 }
62
68 EventSniffer(EventBroker& broker, OnEventReceived onEventReceived)
69 : broker(broker), onEventReceived(onEventReceived)
70 {
71 for (int t = 0; t <= 255; t++)
72 sniffers.push_back(new Sniffer(*this, (uint8_t)t));
73 }
74
76 {
77 for (Sniffer* s : sniffers)
78 delete s;
79 }
80
81private:
82 class Sniffer : public EventHandlerBase
83 {
84 public:
85 Sniffer(EventSniffer& parent, uint8_t topic)
86 : parent(parent), topic(topic)
87 {
88 parent.broker.subscribe(this, topic);
89 }
90
91 void postEvent(const Event& ev) { parent.onEventReceived(ev, topic); }
92
93 ~Sniffer() { parent.broker.unsubscribe(this); }
94
95 private:
96 EventSniffer& parent;
97 uint8_t topic;
98 };
99
100 vector<Sniffer*> sniffers;
101
102 EventBroker& broker;
103 OnEventReceived onEventReceived;
104};
105
106} // namespace Boardcore
void subscribe(EventHandlerBase *subscriber, uint8_t topic)
void unsubscribe(EventHandlerBase *subscriber, uint8_t topic)
Unsubscribe an EventHandler from a specific topic This function should be used only for testing purpo...
EventSniffer(EventBroker &broker, vector< uint8_t > topics, OnEventReceived onEventReceived)
EventSniffer(EventBroker &broker, OnEventReceived onEventReceived)
This file includes all the types the logdecoder script will decode.
uint8_t Event
Definition Event.h:30