OXYGENHUB
◆ main openmsx / src / serial / RS232Raw.hh
3.4 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef RS232RAW_HH
2
#define RS232RAW_HH
3
4
#include "RS232Device.hh"
5
6
#include "EventListener.hh"
7
#include "Observer.hh"
8
#include "StringSetting.hh"
9
#include "SerialPort.hh"
10
#include "ImGuiPluggable.hh"
11
12
#include "Poller.hh"
13
#include "circular_buffer.hh"
14
#include "zstring_view.hh"
15
16
#include <atomic>
17
#include <cstdint>
18
#include <mutex>
19
#include <optional>
20
#include <span>
21
#include <string>
22
#include <thread>
23
#include <vector>
24
25
namespace openmsx {
26
27
class CliComm;
28
class EventDistributor;
29
class RS232Connector;
30
class Scheduler;
31
class CommandController;
32
33
class RS232Raw final : public RS232Device, public ImGuiPluggable,
34
                       private EventListener, private Observer<Setting>
35
{
36
public:
37
	RS232Raw(EventDistributor& eventDistributor, Scheduler& scheduler,
38
	         CommandController& commandController);
39
	~RS232Raw() override;
40
41
	// Pluggable
42
	void plugHelper(Connector& connector, EmuTime time) override;
43
	void unplugHelper(EmuTime time) override;
44
	[[nodiscard]] zstring_view getName() const override;
45
	[[nodiscard]] zstring_view getDescription() const override;
46
47
	// ImGuiPluggable
48
	void handleImGuiExtraMenuItems() override;
49
50
	// input
51
	void signal(EmuTime time) override;
52
53
	// output
54
	void recvByte(uint8_t value, EmuTime time) override;
55
56
	// control lines
57
	[[nodiscard]] std::optional<bool> getDSR(EmuTime time) const override;
58
	[[nodiscard]] std::optional<bool> getCTS(EmuTime time) const override;
59
	[[nodiscard]] std::optional<bool> getDCD(EmuTime time) const override;
60
	[[nodiscard]] std::optional<bool> getRI(EmuTime time) const override;
61
	void setDTR(bool status, EmuTime time) override;
62
	void setRTS(bool status, EmuTime time) override;
63
64
	// serial params
65
	void setDataBits(DataBits bits) override;
66
	void setStopBits(StopBits bits) override;
67
	void setParityBit(bool enable, Parity parity) override;
68
	void setBaudRate(unsigned baud) override;
69
70
	// Whether the host serial port is currently open (the worker thread
71
	// retries opening it briefly after plugging; false while it fails).
72
	[[nodiscard]] bool isOpen() const { return handle.has_value(); }
73
74
	template<typename Archive>
75
	void serialize(Archive& ar, unsigned version);
76
77
private:
78
	void openAndRun(std::string portName, serial::SerialParams params);
79
	void run();
80
	void applyParams();
81
	void setup(std::string portName, serial::SerialParams params);
82
	void tearDown();
83
	[[nodiscard]] serial::SerialParams buildParams() const;
84
85
	// EventListener
86
	bool signalEvent(const Event& event) override;
87
88
	// Observer<Setting>
89
	void update(const Setting& setting) noexcept override;
90
91
private:
92
	EventDistributor& eventDistributor;
93
	Scheduler& scheduler;
94
	CliComm& cliComm;
95
96
	StringSetting rs232RawPortSetting;
97
98
	std::optional<serial::Handle> handle;
99
	std::thread thread;
100
	std::mutex mutex;
101
	std::optional<Poller> poller;
102
	cb_queue<uint8_t> queue;
103
104
	std::atomic<bool> DCD = false;
105
	std::atomic<bool> RI  = false;
106
	std::atomic<bool> CTS = false;
107
	std::atomic<bool> DSR = false;
108
109
	bool DTR = false;
110
	bool RTS = false;
111
	RS232Connector* directConn = nullptr;
112
113
	std::atomic<unsigned> currentBaud = 115200;
114
	std::atomic<DataBits> currentDataBits = DataBits::D8;
115
	std::atomic<StopBits> currentStopBits = StopBits::S1;
116
	std::atomic<Parity> currentParity = Parity::EVEN;
117
	std::atomic<bool> currentParityEnabled = false;
118
119
	// ImGui pluggable row: cached host serial port list and the last-seen
120
	// rs232-raw-port value (refresh the list only when the setting changed).
121
	std::vector<std::string> ports;
122
	std::string currentPort;
123
};
124
125
} // namespace openmsx
126
127
#endif