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, bool noDSRCheck, 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
90 void signalPumpThread();
91
93
94 unsigned int crc_table[256];
95
96 // The following sections represent groups of variables that comprise the state associated
97 // with maintaining the underlying serial device, reading data from the serial device, and
98 // writing data to the serial device. These should be treated as three distinct domains
99 // wrt. concurrency and data within each group should be updated atomically.
100
101 // When acquiring locks, the order _must_ be:
102 // 1) serial
103 // 2) input
104 // 3) output
105
106 // Serial.
107
108 std::mutex serialMutex_;
109 int fd;
110 int serialStatus = -1;
113 bool lastFatal = false;
114
115 // Reading from serial.
116
117 std::mutex inputMutex_;
118 bool esc = false;
119 bool justStarted = true;
121 int startPkt = -1;
122 int lastSYN = -1;
123 unsigned short crcIn = 0;
124 unsigned short inCRCstate;
125 unsigned short receivedCRC;
126 unsigned char *inBuffer; int inWrite = 0; int inRead = 0;
127
128 // Writing to serial.
129
130 std::mutex outputMutex_;
131 bool isCancelled_ = false;
132 unsigned char *outBuffer; int outWrite = 0; int outRead = 0;
133 bool noDSRCheck_ = false;
134
135 // Signaling.
136
137 std::condition_variable outputCondition_;
138
139 // Initial configuration (const).
140
141 Link * const link_;
142
143 const std::string devname;
144
149
156 const short int verbose_;
157
158 int outputDataReadyPipe_[2] = { -1, -1 };
159
160};
A generic container for an array of bytes.
Definition: bufferstore.h:36
static RFSV * a
Definition: main.cc:55
int verbose
Definition: plpprintd.cc:58