Woman, Life, Freedom


OBD-II Emulator
Linux ODB-II Emulator public and protected interfaces documentation
serial_communication.h
1#ifndef SERIAL_COMMUNICATION_H
2#define SERIAL_COMMUNICATION_H
3
4#include <signal.h>
5#include <poll.h>
6#include <queue>
7#include <future>
8#include <asm/termbits.h>
9#include "./communication_layer.h"
10#include "./packet_buffer.h"
11
12namespace ObdEmulator
13{
16 {
17 private:
18 static const int cErrorCode{-1};
19 static const int cSignal{SIGUSR1};
20 static const size_t cSingalFdIndex{0};
21 static const size_t cCommunicationFdIndex{1};
22 static const size_t cNumberOfFileDescriptors{2};
24 static const size_t cReadBufferSize{30};
25
26 const std::string mSerialPort;
27 const speed_t mBaudrate;
28 const int mTimeout;
29
30 struct pollfd mFileDescriptors[cNumberOfFileDescriptors];
31 PacketBuffer mSendBuffer;
32 std::promise<bool> mPromise;
33 std::future<bool> mFuture;
34 std::thread mPollingThread;
35
36 bool trySetupCommunication(int &fileDescriptor) noexcept;
37 bool tryReceive();
38 bool trySend();
39 void tryPoll();
40
41 public:
48 std::string serialPort, speed_t baudrate, int timeout = 1);
49
51
52 bool TryStart(std::vector<uint8_t> &&configuration) override;
53
54 bool TrySendAsync(std::vector<uint8_t> &&data) override;
55
56 bool TryStop() override;
57 };
58}
59
60#endif
Communication medium abstraction layer to send and receive data.
Definition: communication_layer.h:12
Thread-safe queue for buffering packet data bytes.
Definition: packet_buffer.h:13
Serial port communication layer.
Definition: serial_communication.h:16
bool TryStop() override
Try to stop the communication.
SerialCommunication(std::string serialPort, speed_t baudrate, int timeout=1)
Constructor.
bool TrySendAsync(std::vector< uint8_t > &&data) override
Try to buffer data for sending.
bool TryStart(std::vector< uint8_t > &&configuration) override
Try to start the communication.