HardFOC Core
API reference
Loading...
Searching...
No Matches
PipePort.h
Go to the documentation of this file.
1
30#pragma once
31
32#include <atomic>
33#include <cstddef>
34#include <cstdint>
35#include <memory>
36#include <mutex>
37#include <string>
38#include <thread>
39#include <vector>
40
41#include "HfCanPort.h"
42
43namespace hf::can {
44
46class PipeHub {
47 public:
49 static constexpr std::size_t kMaxBacklog = 1U << 20U;
50
51 PipeHub() = default;
52 ~PipeHub();
53 PipeHub(const PipeHub&) = delete;
54 PipeHub& operator=(const PipeHub&) = delete;
55
57 bool Start(const std::string& path);
59 void Stop();
60 bool Running() const noexcept { return running_.load(); }
62 uint64_t Forwarded() const noexcept { return forwarded_.load(); }
63 std::size_t Participants() const noexcept { return participants_.load(); }
64
65 private:
66 struct Client;
67 void Loop();
68
69 std::string path_;
70 int listen_fd_{-1};
71 int wake_[2]{-1, -1};
72 std::thread thread_;
73 std::atomic<bool> running_{false};
74 std::atomic<uint64_t> forwarded_{0};
75 std::atomic<std::size_t> participants_{0};
76};
77
79class PipePort final : public ICanPort {
80 public:
81 struct Options {
82 std::string path;
83 bool host_hub_if_absent{true};
84 std::size_t tx_queue_frames{256};
85 bool fd{false};
86 };
87
88 explicit PipePort(Options options);
89 ~PipePort() override;
90 PipePort(const PipePort&) = delete;
91 PipePort& operator=(const PipePort&) = delete;
92
94 bool Open();
96 void Close();
97 bool IsOpen() const noexcept { return connected_.load(); }
99 bool HostsHub() const noexcept { return hub_ != nullptr; }
100
101 PortCaps Caps() const override;
102 TxResult Send(const CanFrame& frame) override;
103 void SetRxSink(RxSink sink, void* ctx) override;
104 PortStatus Status() const override;
105 bool Recover() override;
106 void SetTxDoneSink(TxDoneSink sink, void* ctx) override;
108 bool RxBackpressure() const override { return true; }
109
110 private:
111 bool Connect();
112 void IoLoop();
113 void Wake() const;
114 bool Deliver(const CanFrame& f);
115
117 std::unique_ptr<PipeHub> hub_;
118 int fd_{-1};
119 int wake_[2]{-1, -1};
120 std::thread io_;
121 std::atomic<bool> run_{false};
122 std::atomic<bool> connected_{false};
123
124 // TX queue (Send → I/O thread).
125 std::mutex tx_mu_;
126 std::vector<CanFrame> tx_ring_;
127 std::size_t tx_head_{0};
128 std::size_t tx_count_{0};
129 bool tx_was_full_{false};
130
131 // Sinks; the mutex keeps a sink alive while it runs (SetRxSink(nullptr) waits).
132 std::mutex sink_mu_;
133 RxSink rx_sink_{nullptr};
134 void* rx_ctx_{nullptr};
136 void* tx_ctx_{nullptr};
137
138 std::atomic<uint32_t> tx_queue_full_{0};
139 uint16_t seq_{0};
140 uint32_t epoch_{0};
141};
142
143} // namespace hf::can
Transport-neutral CAN frame and the ICanPort interface for port bindings (EspTwaiPort,...
One CAN transport binding.
Definition HfCanPort.h:106
The bus: forwards datagrams between participants.
Definition PipePort.h:46
std::atomic< bool > running_
Definition PipePort.h:73
std::atomic< uint64_t > forwarded_
Definition PipePort.h:74
std::atomic< std::size_t > participants_
Definition PipePort.h:75
PipeHub & operator=(const PipeHub &)=delete
int listen_fd_
Definition PipePort.h:70
bool Start(const std::string &path)
Listen at path (an existing socket file is replaced) and start the hub thread.
Definition PipePort.cpp:157
void Stop()
Stop the thread, close every connection and remove the socket file.
Definition PipePort.cpp:183
PipeHub()=default
int wake_[2]
Definition PipePort.h:71
~PipeHub()
Definition PipePort.cpp:155
uint64_t Forwarded() const noexcept
Datagrams forwarded so far (each counted once per receiver).
Definition PipePort.h:62
std::size_t Participants() const noexcept
Definition PipePort.h:63
void Loop()
Definition PipePort.cpp:197
std::string path_
Definition PipePort.h:69
static constexpr std::size_t kMaxBacklog
Definition PipePort.h:49
bool Running() const noexcept
Definition PipePort.h:60
PipeHub(const PipeHub &)=delete
std::thread thread_
Definition PipePort.h:72
ICanPort over a PipeHub.
Definition PipePort.h:79
void Close()
Stop the I/O thread and disconnect (and stop a hub this port started).
Definition PipePort.cpp:356
std::thread io_
Definition PipePort.h:120
int fd_
Definition PipePort.h:118
PortStatus Status() const override
Error counters and state.
Definition PipePort.cpp:422
PipePort & operator=(const PipePort &)=delete
void IoLoop()
Definition PipePort.cpp:436
bool Open()
Connect (starting a hub if allowed and needed) and start the I/O thread.
Definition PipePort.cpp:337
bool IsOpen() const noexcept
Definition PipePort.h:97
int wake_[2]
Definition PipePort.h:119
bool RxBackpressure() const override
The pipe bus never drops: a refused frame is held and offered again.
Definition PipePort.h:108
bool Deliver(const CanFrame &f)
Definition PipePort.cpp:431
std::unique_ptr< PipeHub > hub_
Definition PipePort.h:117
TxDoneSink tx_sink_
Definition PipePort.h:135
void Wake() const
Definition PipePort.cpp:373
bool HostsHub() const noexcept
True when this port started the hub it uses.
Definition PipePort.h:99
std::atomic< bool > run_
Definition PipePort.h:121
bool Connect()
Definition PipePort.cpp:307
uint16_t seq_
Definition PipePort.h:139
uint32_t epoch_
Definition PipePort.h:140
PortCaps Caps() const override
Static capabilities.
Definition PipePort.cpp:379
void SetTxDoneSink(TxDoneSink sink, void *ctx) override
Install the TX-done sink. Ports without a TX-done event ignore it.
Definition PipePort.cpp:416
std::mutex tx_mu_
Definition PipePort.h:125
void * tx_ctx_
Definition PipePort.h:136
void * rx_ctx_
Definition PipePort.h:134
std::atomic< bool > connected_
Definition PipePort.h:122
PipePort(const PipePort &)=delete
bool tx_was_full_
Definition PipePort.h:129
~PipePort() override
Definition PipePort.cpp:305
bool Recover() override
Request bus-off recovery; false when not possible now.
Definition PipePort.cpp:429
std::vector< CanFrame > tx_ring_
Definition PipePort.h:126
std::size_t tx_head_
Definition PipePort.h:127
std::size_t tx_count_
Definition PipePort.h:128
std::atomic< uint32_t > tx_queue_full_
Definition PipePort.h:138
void SetRxSink(RxSink sink, void *ctx) override
Install the RX sink (nullptr detaches).
Definition PipePort.cpp:410
std::mutex sink_mu_
Definition PipePort.h:132
RxSink rx_sink_
Definition PipePort.h:133
Options opt_
Definition PipePort.h:116
TxResult Send(const CanFrame &frame) override
Queue frame without blocking.
Definition PipePort.cpp:389
Definition EspTwaiPort.cpp:12
TxResult
Result of a non-blocking ICanPort::Send.
Definition HfCanPort.h:85
bool(*)(void *ctx, const CanFrame &frame) RxSink
RX sink; may run in ISR context (copy and return). Returns false if it could not take the frame.
Definition HfCanPort.h:101
void(*)(void *ctx) TxDoneSink
TX-done sink: the port has room again; may run in ISR context.
Definition HfCanPort.h:103
One CAN or CAN FD frame; len is a byte count, never a DLC code.
Definition HfCanPort.h:49
Definition PipePort.h:81
std::size_t tx_queue_frames
Definition PipePort.h:84
bool host_hub_if_absent
start an in-process hub when none answers at path
Definition PipePort.h:83
bool fd
advertise FD frames (64-byte payloads)
Definition PipePort.h:85
std::string path
hub socket path
Definition PipePort.h:82
What a port binding can carry.
Definition HfCanPort.h:76
Error counters and statistics of one port.
Definition HfCanPort.h:91