wmbusmeters/src/serial.h

113 wiersze
4.7 KiB
C
Czysty Zwykły widok Historia

2018-04-01 06:53:37 +00:00
/*
Copyright (C) 2017-2020 Fredrik Öhrström
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>
#include<memory>
2017-08-09 10:00:11 +00:00
#include<string>
#include<vector>
using namespace std;
struct SerialCommunicationManager;
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.
*/
struct SerialDevice
{
// If fail_if_not_ok then forcefully exit the program if cannot be opened.
2020-05-09 19:56:37 +00:00
virtual AccessCheck open(bool fail_if_not_ok) = 0;
2017-08-09 10:00:11 +00:00
virtual void close() = 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;
2017-08-09 10:00:11 +00:00
virtual int fd() = 0;
virtual bool working() = 0;
// Used when connecting stdin to a tty driver for testing.
virtual bool readonly() = 0;
// Mark this device so that it is ignored by the select/callback event loop.
virtual void doNotUseCallbacks() = 0;
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;
2019-11-03 15:31:30 +00:00
virtual void checkIfShouldReopen() = 0;
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;
2019-01-04 21:27:51 +00:00
virtual ~SerialDevice() = default;
2017-08-09 10:00:11 +00:00
};
struct SerialCommunicationManager
{
2019-11-03 15:31:30 +00:00
// Read from a /dev/ttyUSB0 or /dev/ttyACM0 device with baud settings.
virtual shared_ptr<SerialDevice> createSerialDeviceTTY(string dev, int baud_rate) = 0;
2019-11-03 15:31:30 +00:00
// Read from a sub shell.
virtual shared_ptr<SerialDevice> createSerialDeviceCommand(string device,
2020-09-13 13:39:09 +00:00
string command,
2019-11-03 15:31:30 +00:00
vector<string> args,
vector<string> envs,
function<void()> on_exit) = 0;
2019-11-03 15:31:30 +00:00
// Read from stdin (file="stdin") or a specific file.
virtual shared_ptr<SerialDevice> createSerialDeviceFile(string file) = 0;
2019-11-03 15:31:30 +00:00
// A serial device simulator used for internal testing.
virtual shared_ptr<SerialDevice> createSerialDeviceSimulator() = 0;
2019-11-03 15:31:30 +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;
// 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;
2019-10-20 18:39:12 +00:00
virtual void setReopenAfter(int seconds) = 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.
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;
virtual void resetInitiated() = 0;
virtual void resetCompleted() = 0;
2020-09-13 13:39:09 +00:00
// List all real serial devices (avoid pseudo ttys)
2020-05-09 19:56:37 +00:00
virtual std::vector<std::string> listSerialDevices() = 0;
2020-09-13 13:39:09 +00:00
// List all all rtlsdr swradio devices
virtual std::vector<std::string> listSWRadioDevices() = 0;
// Return a serial device for the given device, if it exists! Otherwise NULL.
virtual std::shared_ptr<SerialDevice> lookup(std::string device) = 0;
virtual ~SerialCommunicationManager();
2017-08-09 10:00:11 +00:00
};
shared_ptr<SerialCommunicationManager> createSerialCommunicationManager(time_t exit_after_seconds = 0,
2019-11-03 21:00:18 +00:00
time_t reopen_after_seconds = 0,
bool start_event_loop = true);
2017-08-09 10:00:11 +00:00
#endif