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 void serialOpen();
80
86 bool processInputData(std::vector<BufferStore> &receivedData);
87
88 void sendReceivedData(std::vector<BufferStore> &receivedData);
89
93 void shutdown();
94
95 void internalReset(bool resetBaudRateIndex);
96
97 void signalPumpThread();
98
100
101 unsigned int crc_table[256];
102
103 // The following sections represent groups of variables that comprise the state associated
104 // with maintaining the underlying serial device, reading data from the serial device, and
105 // writing data to the serial device. These should be treated as three distinct domains
106 // wrt. concurrency and data within each group should be updated atomically.
107
108 // When acquiring locks, the order _must_ be:
109 // 1) serial
110 // 2) input
111 // 3) output
112
113 // Serial.
114
115 std::mutex serialMutex_;
116 int serialFd_ = -1;
117 int serialStatus = -1;
120
121 // Reading from serial.
122
123 std::mutex inputMutex_;
124 bool esc = false;
125 bool justStarted = true;
127 int startPkt = -1;
128 int lastSYN = -1;
129 unsigned short crcIn = 0;
130 unsigned short inCRCstate;
131 unsigned short receivedCRC;
132 unsigned char *inBuffer; int inWrite = 0; int inRead = 0;
133
134 // Writing to serial.
135
136 std::mutex outputMutex_;
137 bool isCancelled_ = false;
138 unsigned char *outBuffer; int outWrite = 0; int outRead = 0;
139 bool noDSRCheck_ = false;
140
141 // Signaling.
142
143 std::condition_variable outputCondition_;
144
145 // Initial configuration (const).
146
147 Link * const link_;
148
149 const std::string devname;
150
155
162 const short int verbose_;
163
164 int outputDataReadyPipe_[2] = { -1, -1 };
165
166};
A generic container for an array of bytes.
Definition bufferstore.h:36
static RFSV * a
Definition main.cc:60
int verbose
Definition plpprintd.cc:58