/
serialport-pipe.c
250 lines (218 loc) · 6.13 KB
/
serialport-pipe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <errno.h>
#include <signal.h>
#include <string.h> // memset, strcmp
#include <stdio.h> // stdin, fread, fwrite
#include <sys/select.h> // pselect
#include <unistd.h> // STDIN_FILENO, STDOUT_FILENO
#include <popt.h>
#include <libserialport.h>
#include "shared.h"
static const int IOBufferSize = 1048576; // 1 MiB
enum ConversionMode
{
CONVERSION_NONE,
CONVERSION_CRLF_TO_LF,
CONVERSION_LF_TO_CRLF
};
static char* ConvertCrLfStr = "on";
static int ConvertCrLf = 1;
struct poptOption PipeOptions[] =
{
{"convert-crlf", '\0',
POPT_ARG_STRING,
&ConvertCrLfStr, 0,
"Incoming CR-LF squences are replaced by LF and vice versa. Defaults to on.", "[on,off]"},
POPT_TABLEEND
};
#define PIPE_OPTION_TABLE \
{NULL, '\0', POPT_ARG_INCLUDE_TABLE, &PipeOptions, 0, "Pipe behaviour configuration:", NULL},
static struct poptOption options[] =
{
PIPE_OPTION_TABLE
SERIALPORT_OPTION_TABLE
POPT_AUTOHELP
POPT_TABLEEND
};
static struct sp_port* SerialPort = NULL;
static volatile int StopEventLoop = 0;
static void ParseOptions( int argc, const char** argv )
{
poptContext optionContext = poptGetContext("serialport-pipe",
argc, argv,
options,
0);
int optionResult;
while((optionResult = poptGetNextOpt(optionContext)) != -1)
{
if(optionResult < 0)
{
FatalError("%s: %s\n",
poptBadOption(optionContext, 0),
poptStrerror(optionResult));
}
}
poptFreeContext(optionContext);
}
static void SignalHandler( int signal )
{
StopEventLoop = 1;
}
static void InstallSignalHandler()
{
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = SignalHandler;
if(sigaction(SIGTERM, &action, 0))
FatalError("sigaction");
if(sigaction(SIGINT, &action, 0))
FatalError("sigaction");
}
static inline void Write_ConvertLfToCrLf( int destinationFD,
const char* source,
int count )
{
int chunkStart = 0;
int i = 0;
for(; i < count; i++)
{
if(source[i] == '\n')
{
write(destinationFD, &source[chunkStart], i-chunkStart);
write(destinationFD, "\r\n", 2);
chunkStart = i+1;
}
}
write(destinationFD, &source[chunkStart], count-chunkStart);
}
static inline void Write_ConvertCrLfToLf( int destinationFD,
const char* source,
int count )
{
int chunkStart = 0;
int i = 1;
for(; i < count; i++)
{
if(source[i-1] == '\r' &&
source[i] == '\n')
{
write(destinationFD, &source[chunkStart], (i-1)-chunkStart);
write(destinationFD, "\n", 1);
chunkStart = i+1;
}
}
write(destinationFD, &source[chunkStart], count-chunkStart);
}
static void Pump( int sourceFD,
int destinationFD,
enum ConversionMode conversionMode )
{
char buffer[IOBufferSize];
int totalBytesRead = 0;
for(;;)
{
const ssize_t bytesRead = read(sourceFD, buffer, IOBufferSize);
if(bytesRead == 0)
{
break;
}
else if(bytesRead < 0)
{
if(errno == EAGAIN)
break;
else
FatalError("read");
}
else
{
switch(conversionMode)
{
case CONVERSION_NONE:
write(destinationFD, buffer, bytesRead);
break;
case CONVERSION_LF_TO_CRLF:
Write_ConvertLfToCrLf(destinationFD, buffer, bytesRead);
break;
case CONVERSION_CRLF_TO_LF:
Write_ConvertCrLfToLf(destinationFD, buffer, bytesRead);
break;
}
totalBytesRead += bytesRead;
if(bytesRead < IOBufferSize)
break;
}
}
if(totalBytesRead == 0)
{
StopEventLoop = 1;
}
fsync(destinationFD);
}
static void EventLoop()
{
sigset_t oldMask;
sigset_t newMask;
sigemptyset(&newMask);
sigaddset(&newMask, SIGTERM);
sigaddset(&newMask, SIGINT);
if(sigprocmask(SIG_BLOCK, &newMask, &oldMask) < 0)
FatalError("sigprocmask");
int serialPortFD = -1;
HandleSerialPortError(sp_get_port_handle(SerialPort, &serialPortFD));
enum ConversionMode incomingConversion;
enum ConversionMode outgoingConversion;
if(ConvertCrLf)
{
incomingConversion = CONVERSION_CRLF_TO_LF;
outgoingConversion = CONVERSION_LF_TO_CRLF;
}
else
{
incomingConversion = CONVERSION_NONE;
outgoingConversion = CONVERSION_NONE;
}
while(!StopEventLoop)
{
fd_set fdReadSet;
FD_ZERO(&fdReadSet);
FD_SET(STDIN_FILENO, &fdReadSet);
FD_SET(serialPortFD, &fdReadSet);
const int maxFD = serialPortFD;
const int ready = pselect(maxFD+1, &fdReadSet, NULL, NULL, NULL, &oldMask);
if(ready < 0 && errno != EINTR)
{
FatalError("select");
}
else if(StopEventLoop)
{
puts("exit");
break;
}
else if(ready == 0)
{
puts("not ready");
continue;
}
else
{
if(FD_ISSET(STDIN_FILENO, &fdReadSet))
{
Pump(STDIN_FILENO, serialPortFD, outgoingConversion);
}
if(FD_ISSET(serialPortFD, &fdReadSet))
{
Pump(serialPortFD, STDOUT_FILENO, incomingConversion);
}
}
}
}
int main( int argc, const char** argv )
{
ParseOptions(argc, argv);
ConvertCrLf = (strcmp(ConvertCrLfStr, "on") == 0);
OpenSerialPort(&SerialPort, SP_MODE_READ|SP_MODE_WRITE);
InstallSignalHandler();
EventLoop();
sp_close(SerialPort);
sp_free_port(SerialPort);
return 0;
}