plptools
Loading...
Searching...
No Matches
datalink.h
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999 Philip Proudman <philip.proudman@btinternet.com>
5 * Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
6 * Copyright (C) 2026 Jason Morley <hello@jbmorley.co.uk>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * along with this program; if not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22#pragma once
23
24#include "config.h"
25
26#include <condition_variable>
27#include <stdio.h>
28#include <pthread.h>
29#include <mutex>
30
31#include "bufferstore.h"
32#include "bufferarray.h"
33
34extern "C" {
35 static void *data_pump_thread(void *);
36}
37
38class Link;
39
44{
45public:
46 DataLink(const char *fname, int baud, Link *_link, unsigned short verbose, const int cancellationFd);
47 ~DataLink();
48
61 void send(BufferStore &b, bool isEPOC);
62
63 int getSpeed();
64 bool linkFailed();
65 void reset();
66
67private:
68 friend void * data_pump_thread(void *);
69
70 inline void addToCrc(unsigned char a, unsigned short *crc) {
71 *crc = (*crc << 8) ^ crc_table[((*crc >> 8) ^ a) & 0xff];
72 }
73
79 bool processInputData(std::vector<BufferStore> &receivedData);
80
81 void sendReceivedData(std::vector<BufferStore> &receivedData);
82
86 void shutdown();
87
88 void internalReset(bool resetBaudRateIndex);
89
91
92 unsigned int crc_table[256];
93
94 // The following sections represent groups of variables that comprise the state associated
95 // with maintaining the underlying serial device, reading data from the serial device, and
96 // writing data to the serial device. These should be treated as three distinct domains
97 // wrt. concurrency and data within each group should be updated atomically.
98
99 // When acquiring locks, the order _must_ be:
100 // 1) serial
101 // 2) input
102 // 3) output
103
104 // Serial.
105
106 std::mutex serialMutex_;
107 int fd;
108 int serialStatus = -1;
111 bool lastFatal = false;
112
113 // Reading from serial.
114
115 std::mutex inputMutex_;
116 bool esc = false;
117 bool justStarted = true;
119 int startPkt = -1;
120 int lastSYN = -1;
121 unsigned short crcIn = 0;
122 unsigned short inCRCstate;
123 unsigned short receivedCRC;
124 unsigned char *inBuffer; int inWrite = 0; int inRead = 0;
125
126 // Writing to serial.
127
128 std::mutex outputMutex_;
129 bool isCancelled_ = false;
130 unsigned char *outBuffer; int outWrite = 0; int outRead = 0;
131
132 // Signaling.
133
134 std::condition_variable outputCondition_;
135
136 // Initial configuration (const).
137
138 Link * const link_;
139
140 const std::string devname;
141
146
153 const short int verbose_;
154
155};
A generic container for an array of bytes.
Definition: bufferstore.h:37
static rfsv * a
Definition: main.cc:53
int verbose
Definition: plpprintd.cc:58