From 82cbb126a96c14e92fcf3219fe0ff52288b727b2 Mon Sep 17 00:00:00 2001 From: Allen Hill Date: Mon, 6 Jul 2026 13:01:59 -0700 Subject: [PATCH] Fix (centralize) setting of controller/peripheral addrs --- src/avclan/cdchanger.cc | 3 ++- src/avclan/cdchanger.hpp | 2 +- src/avclan/device.hpp | 22 +++++++++++----------- src/avclan/peripheral.hpp | 11 ++++++++--- src/sniffer.cc | 5 +---- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/avclan/cdchanger.cc b/src/avclan/cdchanger.cc index e34f931..e015bb8 100644 --- a/src/avclan/cdchanger.cc +++ b/src/avclan/cdchanger.cc @@ -319,7 +319,8 @@ void CDChanger::enable(Frame *out) { bool CDChanger::pending() { return statustimer_tickPending(); } void CDChanger::resolvepending() { statustimer_clearTick(); } -void CDChanger::emit(Frame *out) { +void CDChanger::emit(Frame *out, uint16_t peripheral) { + out->peripheral_addr = peripheral; generateStatus(out, true, dev_STATUS); out->reaction = r_StateReport; } diff --git a/src/avclan/cdchanger.hpp b/src/avclan/cdchanger.hpp index 828125e..de53afb 100644 --- a/src/avclan/cdchanger.hpp +++ b/src/avclan/cdchanger.hpp @@ -66,7 +66,7 @@ public: void disable(Frame *out); static bool pending(); static void resolvepending(); - void emit(Frame *out); + void emit(Frame *out, uint16_t peripheral); void incrementTime(); bool isPlaying() const; diff --git a/src/avclan/device.hpp b/src/avclan/device.hpp index 9315a7d..e85f411 100644 --- a/src/avclan/device.hpp +++ b/src/avclan/device.hpp @@ -11,15 +11,15 @@ namespace avclan { template -concept Device = requires { - std::integral_constant{}; -} && requires(T dev, const Frame *in, Frame *out, detail::Error::Send err) { - dev.init(); - dev.handle(in, out); - dev.enable(out); - dev.react(out, err); - { dev.pending() } -> std::convertible_to; - dev.resolvepending(); - dev.emit(out); -}; +concept Device = requires { std::integral_constant{}; } && + requires(T dev, const Frame *in, Frame *out, + detail::Error::Send err, uint16_t peripheral) { + dev.init(); + dev.handle(in, out); + dev.enable(out); + dev.react(out, err); + { dev.pending() } -> std::convertible_to; + dev.resolvepending(); + dev.emit(out, peripheral); + }; } // namespace avclan diff --git a/src/avclan/peripheral.hpp b/src/avclan/peripheral.hpp index a690f1d..4f7df40 100644 --- a/src/avclan/peripheral.hpp +++ b/src/avclan/peripheral.hpp @@ -29,7 +29,9 @@ public: Error::Read read(Frame *in, Frame::Print print) { return bus.read(address_, in, print); }; - Error::Send send(const Frame *out, Frame::Print print) { + // To "forge" a controller_addr, instantiate a new/different Peripheral + Error::Send send(Frame *out, Frame::Print print) { + postmark(out); return bus.send(out, print); }; @@ -46,9 +48,7 @@ public: static const uint8_t lancheck_resp[] = {0x00, dev_COMM_CTRL, dev_LAN, 0xFF, 0xFF}; - out->controller_addr = address_; out->peripheral_addr = controller_; - out->control = 0xF; const uint8_t *data = in->data; const uint8_t b0 = *data++; @@ -138,6 +138,11 @@ public: } private: + void postmark(Frame *out) const { + out->controller_addr = address_; + out->control = 0xF; + } + template void poller(Dev &dev, F &&fun) { if (dev.pending() && fun(dev)) dev.resolvepending(); diff --git a/src/sniffer.cc b/src/sniffer.cc index e5df446..e9df159 100644 --- a/src/sniffer.cc +++ b/src/sniffer.cc @@ -101,7 +101,7 @@ int main() { peripheral.poll_devices([&](auto &dev) { if (auto status = cache.pop()) { - dev.emit(status.get()); + dev.emit(status.get(), peripheral.controller()); outgoing.push(std::move(status)); return true; } @@ -138,7 +138,6 @@ int main() { case 'E': // Beep if (auto out = cache.pop()) { out->is_unicast = true; - out->controller_addr = peripheral.address(); out->peripheral_addr = peripheral.controller(); { const uint8_t beep[] = {0x00, dev_CD_CHANGER, dev_BEEP_SPEAKERS, @@ -153,7 +152,6 @@ int main() { case 'P': if (auto out = cache.pop()) { out->is_unicast = true; - out->controller_addr = peripheral.address(); out->peripheral_addr = peripheral.controller(); { const uint8_t play[] = {0x00, dev_COMM_CTRL, dev_COMM_v1, @@ -236,7 +234,6 @@ int main() { } else { if (auto out = cache.pop()) { out->is_unicast = seqIsUnicast; - out->controller_addr = peripheral.address(); out->peripheral_addr = seqIsUnicast ? peripheral.controller() : 0x1FF; out->length = seqIdx;