|
| 1 | +#include <memory> |
| 2 | +#include <chrono> |
| 3 | + |
| 4 | +#include "science/scipp/status.h" |
| 5 | +#include "science/synapse/channel.h" |
| 6 | +#include "science/synapse/data.h" |
| 7 | +#include "science/synapse/device.h" |
| 8 | +#include "science/synapse/nodes/broadband_source.h" |
| 9 | +#include "science/synapse/nodes/stream_out.h" |
| 10 | +#include "packet_monitoring.h" |
| 11 | + |
| 12 | +using science::libndtp::NDTPHeader; |
| 13 | +using synapse::Ch; |
| 14 | +using synapse::Config; |
| 15 | +using synapse::Device; |
| 16 | +using synapse::DeviceInfo; |
| 17 | +using synapse::Electrodes; |
| 18 | +using synapse::NodeType; |
| 19 | +using synapse::Signal; |
| 20 | +using synapse::StreamOut; |
| 21 | +using synapse::SynapseData; |
| 22 | +using synapse::NodeConfig; |
| 23 | +using synapse::Node; |
| 24 | + |
| 25 | +auto configure_stream(Device& device, std::shared_ptr<StreamOut>* stream_out_ptr) -> science::Status { |
| 26 | + const uint32_t N_CHANNELS = 32; // Using more channels for stats testing |
| 27 | + if (stream_out_ptr == nullptr) { |
| 28 | + return { science::StatusCode::kInvalidArgument, "stream out pointer is null" }; |
| 29 | + } |
| 30 | + |
| 31 | + science::Status s; |
| 32 | + DeviceInfo info; |
| 33 | + s = device.info(&info); |
| 34 | + if (!s.ok()) return s; |
| 35 | + |
| 36 | + // Configure signal with more channels for statistics gathering |
| 37 | + Signal signal{ |
| 38 | + Electrodes{ |
| 39 | + .channels = {}, |
| 40 | + .low_cutoff_hz = 500, |
| 41 | + .high_cutoff_hz = 6000 |
| 42 | + } |
| 43 | + }; |
| 44 | + auto& electrodes = std::get<Electrodes>(signal.signal); |
| 45 | + electrodes.channels.reserve(N_CHANNELS); |
| 46 | + for (unsigned int i = 0; i < N_CHANNELS; i++) { |
| 47 | + electrodes.channels.push_back(Ch{ |
| 48 | + .id = i, |
| 49 | + .electrode_id = i * 2, |
| 50 | + .reference_id = i * 2 + 1 |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + Config config; |
| 55 | + auto broadband_source = std::make_shared<synapse::BroadbandSource>(100, 16, 30000, 20.0, signal); |
| 56 | + |
| 57 | + NodeConfig stream_out_config; |
| 58 | + auto* stream_out_proto = stream_out_config.mutable_stream_out(); |
| 59 | + stream_out_proto->set_multicast_group("224.0.0.115"); |
| 60 | + |
| 61 | + std::shared_ptr<Node> stream_out_node; |
| 62 | + s = StreamOut::from_proto(stream_out_config, &stream_out_node); |
| 63 | + if (!s.ok()) return s; |
| 64 | + |
| 65 | + *stream_out_ptr = std::dynamic_pointer_cast<StreamOut>(stream_out_node); |
| 66 | + if (!*stream_out_ptr) { |
| 67 | + return { science::StatusCode::kInternal, "failed to cast stream out node" }; |
| 68 | + } |
| 69 | + |
| 70 | + s = config.add_node(broadband_source); |
| 71 | + if (!s.ok()) return s; |
| 72 | + |
| 73 | + s = config.add_node(*stream_out_ptr); |
| 74 | + if (!s.ok()) return s; |
| 75 | + |
| 76 | + s = config.connect(broadband_source, *stream_out_ptr); |
| 77 | + if (!s.ok()) return s; |
| 78 | + |
| 79 | + s = device.configure(&config); |
| 80 | + if (!s.ok()) return s; |
| 81 | + |
| 82 | + std::cout << "Configured device..." << std::endl; |
| 83 | + |
| 84 | + s = device.start(); |
| 85 | + if (!s.ok()) return s; |
| 86 | + |
| 87 | + std::cout << "Started device..." << std::endl; |
| 88 | + return s; |
| 89 | +} |
| 90 | + |
| 91 | +auto stream(const std::string& uri) -> int { |
| 92 | + synapse::Device device(uri); |
| 93 | + science::Status s; |
| 94 | + |
| 95 | + std::shared_ptr<synapse::StreamOut> stream_out; |
| 96 | + s = configure_stream(device, &stream_out); |
| 97 | + if (!s.ok()) { |
| 98 | + std::cout << "error configuring stream: (" |
| 99 | + << static_cast<int>(s.code()) << ") " << s.message() << std::endl; |
| 100 | + return 1; |
| 101 | + } |
| 102 | + |
| 103 | + if (stream_out == nullptr) { |
| 104 | + std::cout << "stream out node not initialized" << std::endl; |
| 105 | + return 1; |
| 106 | + } |
| 107 | + |
| 108 | + std::cout << "Monitoring packet statistics..." << std::endl; |
| 109 | + |
| 110 | + // Initialize packet monitor |
| 111 | + PacketMonitor monitor; |
| 112 | + monitor.start_monitoring(); |
| 113 | + auto last_stats_time = std::chrono::steady_clock::now(); |
| 114 | + |
| 115 | + while (true) { |
| 116 | + size_t bytes_read; |
| 117 | + NDTPHeader header; |
| 118 | + SynapseData out; |
| 119 | + s = stream_out->read(&out, &header, &bytes_read); |
| 120 | + if (s.code() == science::StatusCode::kUnavailable) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + if (!s.ok()) { |
| 125 | + std::cout << "error reading from stream out node: (" |
| 126 | + << static_cast<int>(s.code()) << ") " << s.message() << std::endl; |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + monitor.process_packet(header.seq_number, bytes_read); |
| 131 | + |
| 132 | + auto now = std::chrono::steady_clock::now(); |
| 133 | + if (std::chrono::duration_cast<std::chrono::seconds>(now - last_stats_time).count() >= 1) { |
| 134 | + monitor.print_stats(); |
| 135 | + last_stats_time = now; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | +int main(int argc, char** argv) { |
| 143 | + if (argc != 2) { |
| 144 | + std::cout << "Usage: " << argv[0] << " <uri>" << std::endl; |
| 145 | + std::cout << " uri: device URI (e.g., 192.168.0.1:647)" << std::endl; |
| 146 | + return 1; |
| 147 | + } |
| 148 | + |
| 149 | + std::string uri = argv[1]; |
| 150 | + return stream(uri); |
| 151 | +} |
0 commit comments