2018-04-01 06:53:37 +00:00
|
|
|
/*
|
2022-02-12 14:28:17 +00:00
|
|
|
Copyright (C) 2017-2021 Fredrik Öhrström (gpl-3.0-or-later)
|
2018-04-01 06:53:37 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2017-08-09 10:00:11 +00:00
|
|
|
|
|
|
|
#ifndef SERIAL_H_
|
|
|
|
#define SERIAL_H_
|
|
|
|
|
|
|
|
#include"util.h"
|
|
|
|
|
|
|
|
#include<functional>
|
2018-12-28 17:35:32 +00:00
|
|
|
#include<memory>
|
2017-08-09 10:00:11 +00:00
|
|
|
#include<string>
|
|
|
|
#include<vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
struct SerialCommunicationManager;
|
|
|
|
|
2021-02-13 14:58:38 +00:00
|
|
|
enum class PARITY { NONE, EVEN, ODD };
|
|
|
|
|
2019-11-03 15:31:30 +00:00
|
|
|
/**
|
|
|
|
A SerialDevice can be connected to a tty with a baudrate.
|
|
|
|
But can also be connected to stdin, a file, or the output from a subshell.
|
|
|
|
If you try to do send bytes to such a non-tty, then send will return false.
|
|
|
|
*/
|
2019-04-03 15:31:51 +00:00
|
|
|
struct SerialDevice
|
|
|
|
{
|
2020-08-30 19:33:48 +00:00
|
|
|
// If fail_if_not_ok then forcefully exit the program if cannot be opened.
|
2021-10-16 18:20:22 +00:00
|
|
|
virtual bool open(bool fail_if_not_ok) = 0;
|
2017-08-09 10:00:11 +00:00
|
|
|
virtual void close() = 0;
|
2020-09-26 11:52:24 +00:00
|
|
|
// Explicitly closed fd == -1
|
|
|
|
virtual bool isClosed() = 0;
|
2019-11-03 15:31:30 +00:00
|
|
|
// Send will return true only if sending on a tty.
|
2018-04-01 06:53:37 +00:00
|
|
|
virtual bool send(std::vector<uchar> &data) = 0;
|
2019-11-03 15:31:30 +00:00
|
|
|
// Receive returns the number of bytes received.
|
2018-04-01 06:53:37 +00:00
|
|
|
virtual int receive(std::vector<uchar> *data) = 0;
|
2020-10-24 14:16:50 +00:00
|
|
|
// Read and skip until the desired character is found
|
|
|
|
// and no further bytes can be read.
|
|
|
|
virtual bool waitFor(uchar c) = 0;
|
2017-08-09 10:00:11 +00:00
|
|
|
virtual int fd() = 0;
|
2020-09-25 14:38:40 +00:00
|
|
|
virtual bool opened() = 0;
|
2019-04-03 15:31:51 +00:00
|
|
|
virtual bool working() = 0;
|
2021-08-08 21:07:34 +00:00
|
|
|
virtual bool resetting() = 0; // The serial device is working but can lack a valid file descriptor.
|
2020-02-15 20:18:21 +00:00
|
|
|
// Used when connecting stdin to a tty driver for testing.
|
|
|
|
virtual bool readonly() = 0;
|
2020-08-30 19:33:48 +00:00
|
|
|
// Mark this device so that it is ignored by the select/callback event loop.
|
2020-10-24 14:16:50 +00:00
|
|
|
virtual void disableCallbacks() = 0;
|
|
|
|
// Enable this device to trigger callbacks from the event loop.
|
|
|
|
virtual void enableCallbacks() = 0;
|
2020-08-30 19:33:48 +00:00
|
|
|
virtual bool skippingCallbacks() = 0;
|
2019-11-03 15:31:30 +00:00
|
|
|
|
2020-08-01 19:56:46 +00:00
|
|
|
// Return underlying device as string.
|
|
|
|
virtual std::string device() = 0;
|
|
|
|
|
2020-09-25 21:56:50 +00:00
|
|
|
virtual bool checkIfDataIsPending() = 0;
|
2019-10-28 21:10:29 +00:00
|
|
|
virtual void fill(std::vector<uchar> &data) = 0; // Fill buffer with raw data.
|
2017-08-09 10:00:11 +00:00
|
|
|
virtual SerialCommunicationManager *manager() = 0;
|
2020-09-25 19:16:20 +00:00
|
|
|
virtual void resetInitiated() = 0;
|
|
|
|
virtual void resetCompleted() = 0;
|
|
|
|
|
2019-01-04 21:27:51 +00:00
|
|
|
virtual ~SerialDevice() = default;
|
2017-08-09 10:00:11 +00:00
|
|
|
};
|
|
|
|
|
2019-04-03 15:31:51 +00:00
|
|
|
struct SerialCommunicationManager
|
|
|
|
{
|
2019-11-03 15:31:30 +00:00
|
|
|
// Read from a /dev/ttyUSB0 or /dev/ttyACM0 device with baud settings.
|
2021-02-13 14:58:38 +00:00
|
|
|
virtual shared_ptr<SerialDevice> createSerialDeviceTTY(string dev, int baud_rate, PARITY parity, string purpose) = 0;
|
2019-11-03 15:31:30 +00:00
|
|
|
// Read from a sub shell.
|
2020-10-05 17:47:58 +00:00
|
|
|
virtual shared_ptr<SerialDevice> createSerialDeviceCommand(string identifier,
|
2020-09-13 13:39:09 +00:00
|
|
|
string command,
|
2019-11-03 15:31:30 +00:00
|
|
|
vector<string> args,
|
2019-04-03 15:31:51 +00:00
|
|
|
vector<string> envs,
|
2020-09-26 11:52:24 +00:00
|
|
|
string purpose) = 0;
|
2019-11-03 15:31:30 +00:00
|
|
|
// Read from stdin (file="stdin") or a specific file.
|
2020-09-26 11:52:24 +00:00
|
|
|
virtual shared_ptr<SerialDevice> createSerialDeviceFile(string file, string purpose) = 0;
|
2019-11-03 15:31:30 +00:00
|
|
|
// A serial device simulator used for internal testing.
|
2020-09-21 19:55:21 +00:00
|
|
|
virtual shared_ptr<SerialDevice> createSerialDeviceSimulator() = 0;
|
2019-11-03 15:31:30 +00:00
|
|
|
|
2020-08-30 19:33:48 +00:00
|
|
|
// Invoke cb callback when data arrives on the serial device.
|
2017-08-09 10:00:11 +00:00
|
|
|
virtual void listenTo(SerialDevice *sd, function<void()> cb) = 0;
|
2020-08-30 19:33:48 +00:00
|
|
|
// Invoke cb callback when the serial device has disappeared!
|
|
|
|
virtual void onDisappear(SerialDevice *sd, function<void()> cb) = 0;
|
2020-09-06 08:41:04 +00:00
|
|
|
// Normally the communication mananager runs for ever.
|
|
|
|
// But if you expect configured devices to work, then
|
|
|
|
// the manager will exit when there are no working devices.
|
|
|
|
virtual void expectDevicesToWork() = 0;
|
2017-08-09 10:00:11 +00:00
|
|
|
virtual void stop() = 0;
|
2019-11-03 22:43:29 +00:00
|
|
|
virtual void startEventLoop() = 0;
|
2017-08-09 10:00:11 +00:00
|
|
|
virtual void waitForStop() = 0;
|
|
|
|
virtual bool isRunning() = 0;
|
2020-08-01 19:56:46 +00:00
|
|
|
// Register a new timer that regularly, every seconds, invokes the callback.
|
|
|
|
// Returns an id for the timer.
|
2020-08-30 19:33:48 +00:00
|
|
|
virtual int startRegularCallback(std::string name, int seconds, function<void()> callback) = 0;
|
2020-08-01 19:56:46 +00:00
|
|
|
virtual void stopRegularCallback(int id) = 0;
|
2020-08-10 18:57:20 +00:00
|
|
|
|
2021-10-16 18:20:22 +00:00
|
|
|
// Verify if the device can be accessed and verbose any failures.
|
|
|
|
virtual AccessCheck checkAccess(std::string device,
|
|
|
|
shared_ptr<SerialCommunicationManager> manager, // Silly but for now, needs shared pointer to itself....
|
|
|
|
std::string extra_info = "",
|
|
|
|
function<AccessCheck(string,shared_ptr<SerialCommunicationManager>)> extra_probe = NULL) = 0;
|
2020-09-13 13:39:09 +00:00
|
|
|
// List all real serial devices (avoid pseudo ttys)
|
2020-10-04 20:52:05 +00:00
|
|
|
virtual std::vector<std::string> listSerialTTYs() = 0;
|
2020-08-30 19:33:48 +00:00
|
|
|
// Return a serial device for the given device, if it exists! Otherwise NULL.
|
2020-09-21 19:55:21 +00:00
|
|
|
virtual std::shared_ptr<SerialDevice> lookup(std::string device) = 0;
|
2020-10-07 19:40:33 +00:00
|
|
|
// Remove a closed device, returns false and do not remove, if the device is still in use.
|
|
|
|
virtual bool removeNonWorking(std::string device) = 0;
|
2018-12-28 17:35:32 +00:00
|
|
|
virtual ~SerialCommunicationManager();
|
2017-08-09 10:00:11 +00:00
|
|
|
};
|
|
|
|
|
2020-09-25 20:27:34 +00:00
|
|
|
shared_ptr<SerialCommunicationManager> createSerialCommunicationManager(time_t exit_after_seconds,
|
|
|
|
bool start_event_loop);
|
2017-08-09 10:00:11 +00:00
|
|
|
|
|
|
|
#endif
|