diff options
author | Eddy Pedroni <epedroni@pm.me> | 2025-07-22 22:55:24 +0200 |
---|---|---|
committer | Eddy Pedroni <epedroni@pm.me> | 2025-07-22 22:55:24 +0200 |
commit | 29ca6bac4565d754a67414e503a215627d3aa566 (patch) | |
tree | ae86ca581f6aae6e2d2a870b963a8fe3b05da625 | |
parent | 6da14e1590935710055ebc81ba55017b457ca1ad (diff) |
Add router implementation, untested
-rw-r--r-- | daemon/src/connection_manager.cpp | 2 | ||||
-rw-r--r-- | daemon/src/message.h | 27 | ||||
-rw-r--r-- | daemon/src/routing.cpp | 15 | ||||
-rw-r--r-- | daemon/src/routing.h | 2 | ||||
-rw-r--r-- | daemon/src/types.h | 26 |
5 files changed, 45 insertions, 27 deletions
diff --git a/daemon/src/connection_manager.cpp b/daemon/src/connection_manager.cpp index 820f1d2..3b2d7da 100644 --- a/daemon/src/connection_manager.cpp +++ b/daemon/src/connection_manager.cpp @@ -24,7 +24,7 @@ callback(double time_stamp, std::vector<unsigned char> *raw, void *user_data) std::cerr << "Received message with wrong size from " << device->source_id << ", dropping\n"; } - Message message { device->source_id, {raw->at(0), raw->at(1), raw->at(2)} }; + Message message { &device->source_id, {raw->at(0), raw->at(1), raw->at(2)} }; device->submit(message); } diff --git a/daemon/src/message.h b/daemon/src/message.h index 9339118..4668e0c 100644 --- a/daemon/src/message.h +++ b/daemon/src/message.h @@ -11,31 +11,14 @@ namespace midi_router struct Message { - enum class Type - { - NOTE_OFF = 0x8u, - NOTE_ON = 0x9u, - POLY_AT = 0xAu, - CONTROL_CHANGE = 0xBu, - PROGRAM_CHANGE = 0xCu, - CHANNEL_AT = 0xDu, - PITCH_WHEEL = 0xEu, - - CLOCK = 0xF8u, - MEAS_END = 0xF9u, - START = 0xFAu, - CONTINUE = 0xFBu, - STOP = 0xFCu, - }; - - Device_Id const & source_id; - std::array<std::uint8_t, 3> const bytes; + Device_Id const * source_id; + std::array<std::uint8_t, 3> bytes; - Type + Message_Type type() const { - if (bytes[0] & 0xF0 == 0xF0) return static_cast<Type>(bytes[0]); - else return static_cast<Type>(bytes[0] >> 4); + if (bytes[0] & 0xF0 == 0xF0) return static_cast<Message_Type>(bytes[0]); + else return static_cast<Message_Type>(bytes[0] >> 4); } }; diff --git a/daemon/src/routing.cpp b/daemon/src/routing.cpp index 709d815..15ae555 100644 --- a/daemon/src/routing.cpp +++ b/daemon/src/routing.cpp @@ -1,14 +1,27 @@ #include "routing.h" +#include <chrono> #include <iostream> namespace midi_router { void -Router::route(Route_Map const & config) const +Router::route(Route_Map const & config) { + Message m; + if (m_queue.wait_dequeue_timed(m, std::chrono::milliseconds(500))) + { + if (!config.contains(*m.source_id) || !config.at(*m.source_id).contains(m.type())) + { + return; + } + for (auto const & target: config.at(*m.source_id).at(m.type())) + { + target(m.bytes); + } + } } void diff --git a/daemon/src/routing.h b/daemon/src/routing.h index 2d68391..c7cd243 100644 --- a/daemon/src/routing.h +++ b/daemon/src/routing.h @@ -12,7 +12,7 @@ class Router { public: void - route(Route_Map const & config) const; + route(Route_Map const & config); void submit(Message const & message); diff --git a/daemon/src/types.h b/daemon/src/types.h index f09fb88..2942c0b 100644 --- a/daemon/src/types.h +++ b/daemon/src/types.h @@ -1,15 +1,37 @@ #pragma once +#include <cstdint> +#include <array> #include <string> #include <map> #include <vector> +#include <functional> namespace midi_router { +enum class Message_Type +{ + NOTE_OFF = 0x8u, + NOTE_ON = 0x9u, + POLY_AT = 0xAu, + CONTROL_CHANGE = 0xBu, + PROGRAM_CHANGE = 0xCu, + CHANNEL_AT = 0xDu, + PITCH_WHEEL = 0xEu, + + CLOCK = 0xF8u, + MEAS_END = 0xF9u, + START = 0xFAu, + CONTINUE = 0xFBu, + STOP = 0xFCu, +}; + using Device_Id = std::string; using Device_Map = std::map<std::string, Device_Id>; -using Target_List = std::vector<Device_Id>; -using Route_Map = std::map<Device_Id, Target_List>; + +using Send_Callback = std::function<void(std::array<std::uint8_t, 3> const & payload)>; +using Target_List = std::vector<Send_Callback>; +using Route_Map = std::map<Device_Id, std::map<Message_Type, Target_List>>; } // namespace midi_router |