HardFOC Core
API reference
Loading...
Searching...
No Matches
LinuxSocketCanPort.h
Go to the documentation of this file.
1
16#pragma once
17
18#include <atomic>
19#include <cstdint>
20#include <mutex>
21#include <string>
22#include <thread>
23
24#include "HfCanPort.h"
25
26namespace hf::can {
27
28class LinuxSocketCanPort final : public ICanPort {
29 public:
30 struct Options {
31 std::string ifname{"vcan0"};
32 bool fd{false};
33 uint32_t nominal_bps{0};
34 };
35
36 explicit LinuxSocketCanPort(Options options);
40
41 bool Open();
42 void Close();
43 bool IsOpen() const noexcept { return fd_ >= 0; }
44
45 PortCaps Caps() const override;
46 TxResult Send(const CanFrame& frame) override;
47 void SetRxSink(RxSink sink, void* ctx) override;
48 PortStatus Status() const override;
49 bool Recover() override;
50 void SetTxDoneSink(TxDoneSink sink, void* ctx) override;
51
52 private:
53 void ReadLoop();
54 void HandleErrorFrame(uint32_t can_id, const uint8_t* data, uint8_t len);
55
57 int fd_{-1};
58 int wake_[2]{-1, -1};
59 std::thread reader_;
60 std::atomic<bool> run_{false};
61 std::atomic<bool> want_writable_{false};
62
63 std::mutex sink_mu_;
64 RxSink rx_sink_{nullptr};
65 void* rx_ctx_{nullptr};
67 void* tx_ctx_{nullptr};
68
69 std::atomic<uint8_t> state_{static_cast<uint8_t>(BusState::Stopped)};
70 std::atomic<uint16_t> tec_{0};
71 std::atomic<uint16_t> rec_{0};
72 std::atomic<uint32_t> rx_overruns_{0};
73 std::atomic<uint32_t> tx_queue_full_{0};
74 std::atomic<uint32_t> bus_off_count_{0};
75};
76
77} // namespace hf::can
Transport-neutral CAN frame and the ICanPort interface for port bindings (EspTwaiPort,...
One CAN transport binding.
Definition HfCanPort.h:106
Definition LinuxSocketCanPort.h:28
std::mutex sink_mu_
Definition LinuxSocketCanPort.h:63
std::atomic< uint32_t > tx_queue_full_
Definition LinuxSocketCanPort.h:73
PortStatus Status() const override
Error counters and state.
std::atomic< uint16_t > tec_
Definition LinuxSocketCanPort.h:70
bool Recover() override
Request bus-off recovery; false when not possible now.
int fd_
Definition LinuxSocketCanPort.h:57
RxSink rx_sink_
Definition LinuxSocketCanPort.h:64
std::thread reader_
Definition LinuxSocketCanPort.h:59
void HandleErrorFrame(uint32_t can_id, const uint8_t *data, uint8_t len)
LinuxSocketCanPort(Options options)
Options opt_
Definition LinuxSocketCanPort.h:56
void SetRxSink(RxSink sink, void *ctx) override
Install the RX sink (nullptr detaches).
TxResult Send(const CanFrame &frame) override
Queue frame without blocking.
std::atomic< uint32_t > bus_off_count_
Definition LinuxSocketCanPort.h:74
std::atomic< uint8_t > state_
Definition LinuxSocketCanPort.h:69
void * rx_ctx_
Definition LinuxSocketCanPort.h:65
void SetTxDoneSink(TxDoneSink sink, void *ctx) override
Install the TX-done sink. Ports without a TX-done event ignore it.
LinuxSocketCanPort(const LinuxSocketCanPort &)=delete
std::atomic< uint32_t > rx_overruns_
Definition LinuxSocketCanPort.h:72
PortCaps Caps() const override
Static capabilities.
std::atomic< uint16_t > rec_
Definition LinuxSocketCanPort.h:71
void * tx_ctx_
Definition LinuxSocketCanPort.h:67
std::atomic< bool > run_
Definition LinuxSocketCanPort.h:60
LinuxSocketCanPort & operator=(const LinuxSocketCanPort &)=delete
std::atomic< bool > want_writable_
Definition LinuxSocketCanPort.h:61
bool IsOpen() const noexcept
Definition LinuxSocketCanPort.h:43
TxDoneSink tx_sink_
Definition LinuxSocketCanPort.h:66
int wake_[2]
Definition LinuxSocketCanPort.h:58
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 LinuxSocketCanPort.h:30
bool fd
enable CAN_RAW_FD_FRAMES
Definition LinuxSocketCanPort.h:32
uint32_t nominal_bps
reported in Caps() (0 for vcan)
Definition LinuxSocketCanPort.h:33
std::string ifname
Definition LinuxSocketCanPort.h:31
What a port binding can carry.
Definition HfCanPort.h:76
Error counters and statistics of one port.
Definition HfCanPort.h:91