Debian Codegraph @

IndiSerialWrapper.hpp

raw
1 /*
2 * IndiSerialWrapper.hpp
3 *
4 * Copyright 2020 Kevin Krüger <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 *
22 */
23
24 #pragma once
25
26 #include <cstdint>
27 #include <cmath>
28 #include <memory>
29 #include <iostream>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <termios.h>
33 #include <sys/ioctl.h>
34 #include <mutex>
35
36 #include <indicom.h>
37 #include <inditelescope.h>
38
39 #include "ISerialInterface.hpp"
40 #include "config.h"
41
42 namespace GoToDriver
43 {
44 //Implementation of the serial interface abstraction dedicated for the indi interface.
45 class IndiSerialWrapper : public SerialDeviceControl::ISerialInterface
46 {
47 private:
48 //Handle of the serial port provided by the indi interface.
49 int mTtyFd;
50
51 static constexpr const uint8_t DRIVER_TIMEOUT {3};
52
53 //mutex to protect writing queue, from being concurrently modified, causing corrupt messages.
54 std::mutex mMutex;
55
56 public:
57 //Default implementation constructor.
58 IndiSerialWrapper();
59
60 //Default Destructor.
61 virtual ~IndiSerialWrapper();
62
63 //Get the current device handle.
64 int GetFD();
65
66 //Set the device handle.
67 void SetFD(int fd);
68
69 //Opens the serial device, the acutal implementation has to deal with the handles!
70 virtual bool Open();
71
72 //Closes the serial device, the actual implementation has to deal with the handles!
73 virtual bool Close();
74
75 //Returns true if the serial port is open and ready to receive or transmit data.
76 virtual bool IsOpen();
77
78 //Returns the number of bytes to read available in the serial receiver queue.
79 virtual size_t BytesToRead();
80
81 //Reads a byte from the serial device. Can safely cast to uint8_t unless -1 is returned, corresponding to "stream end reached".
82 virtual int16_t ReadByte();
83
84 //writes the buffer to the serial interface.
85 //this function should handle all the quirks of various serial interfaces.
86 virtual bool Write(uint8_t* buffer, size_t offset, size_t length);
87
88 //flush the buffer.
89 virtual bool Flush();
90 };
91 }