Veethree D7 SDK
Loading...
Searching...
No Matches
serial_port_context.h
Go to the documentation of this file.
1
5#ifndef SERIAL_PORT_CONTEXT_H
6#define SERIAL_PORT_CONTEXT_H
7
9
10#include <atomic>
11#include <map>
12#include <thread>
13
14#include <gpiod.hpp>
15
16namespace vsdk::comms {
17
18#ifndef DOXYGEN_SKIP_THIS
19
20// this is private implementation, doxygen is not a requirement
21
22struct port_mapping {
23 // actual device port_alias to be referenced everywhere
24 port_alias alias{};
25 // actual device under /dev
26 std::string device;
27 // supported modes
28 uint32_t modes{};
29};
30
31class serial_port_context : public serial_port_context_interface {
32public:
33 explicit serial_port_context(const port_attributes &attr, serial_port_receive_fn fn = {});
34 serial_port_context();
35
36 ~serial_port_context() noexcept override;
37
38 serial_port_context(serial_port_context &&other) noexcept = delete;
39 serial_port_context &operator=(serial_port_context &&other) noexcept = delete;
40
41 serial_port_context(const serial_port_context &other) = delete;
42 serial_port_context &operator=(const serial_port_context &other) = delete;
43
44 void open(const port_attributes &attr, serial_port_receive_fn fn) override;
45 void close() override;
46 bool is_open() const override;
47 void flush(port_flush_mode mode) override;
48 void drain() override;
49 port_attributes attrs() const override;
50 port_baud_rate baud_rate() const override;
51 ssize_t write(uint8_t value) override;
52 ssize_t write(std::span<uint8_t> buf) override;
53 ssize_t write(std::span<uint8_t> buf, uint32_t ms_timeout) override;
54 ssize_t read(uint8_t &value) override;
55 ssize_t read(std::span<uint8_t> buf) override;
56 ssize_t read(std::span<uint8_t> buf, uint32_t ms_timeout) override;
57
58private:
59 static std::map<port_alias, port_mapping> available_ports(const port_attributes &attr);
60
61 void setup(const port_attributes &attr, serial_port_receive_fn fn);
62 void set_mode(port_mode mode);
63 void set_mode_pins(port_mode mode);
64
65 void gpio_set_j1708(const std::string &name, int value);
66 void gpio_set_rs485(const std::string &name, int value);
67 void gpio_reset();
68
69 void receive_thread_fn(std::stop_token token);
70
71 int _fd{};
72 std::jthread _receive_thread;
73 std::atomic<bool> _receive_thread_running;
74 serial_port_receive_fn _receive_fn;
75 port_mapping _port_map{};
76 port_attributes _port_attrs{};
77 gpiod::line _gpio_line_j1708;
78 gpiod::line _gpio_line_rs485;
79};
80
81#endif // DOXYGEN_SKIP_THIS
82
83} // namespace vsdk::comms
84
85#endif // SERIAL_PORT_CONTEXT_H
The serial_port_context_interface interface describes the required functions for providing an impleme...
Definition serial_port_context_interface.h:20
std::function< void(std::span< uint8_t >)> serial_port_receive_fn
Callback function type for receiving data from a serial port.
Definition serial_port_common.h:35
port_alias
Enumeration of supported serial port aliases.
Definition serial_port_common.h:136
@ device
User-defined device (e.g., /dev/ttyLP3)
Definition serial_port_common.h:139
Any entities that belong to the comms library.
Definition can_adapter.h:12
Serial Port Context Interface.