From 29ca6bac4565d754a67414e503a215627d3aa566 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Tue, 22 Jul 2025 22:55:24 +0200 Subject: Add router implementation, untested --- daemon/src/connection_manager.cpp | 2 +- daemon/src/message.h | 27 +++++---------------------- daemon/src/routing.cpp | 15 ++++++++++++++- daemon/src/routing.h | 2 +- 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 *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 const bytes; + Device_Id const * source_id; + std::array bytes; - Type + Message_Type type() const { - if (bytes[0] & 0xF0 == 0xF0) return static_cast(bytes[0]); - else return static_cast(bytes[0] >> 4); + if (bytes[0] & 0xF0 == 0xF0) return static_cast(bytes[0]); + else return static_cast(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 #include 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 +#include #include #include #include +#include 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; -using Target_List = std::vector; -using Route_Map = std::map; + +using Send_Callback = std::function const & payload)>; +using Target_List = std::vector; +using Route_Map = std::map>; } // namespace midi_router -- cgit v1.2.3