-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
in_serial.c
364 lines (307 loc) · 10.8 KB
/
in_serial.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Serial input plugin for Fluent Bit
* ==================================
* Copyright (C) 2015-2016 Takeshi HASEGAWA
* Copyright (C) 2015-2016 Treasure Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <sys/stat.h>
#include <inttypes.h>
#include <termios.h>
#include <msgpack.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_engine.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_error.h>
#include "in_serial.h"
#include "in_serial_config.h"
void *in_serial_flush(void *in_context, size_t *size)
{
char *buf;
msgpack_sbuffer *sbuf;
struct flb_in_serial_config *ctx = in_context;
if (ctx->buffer_id == 0)
return NULL;
sbuf = &ctx->mp_sbuf;
*size = sbuf->size;
buf = flb_malloc(sbuf->size);
if (!buf) {
return NULL;
}
/* set a new buffer and re-initialize our MessagePack context */
memcpy(buf, sbuf->data, sbuf->size);
msgpack_sbuffer_destroy(&ctx->mp_sbuf);
msgpack_sbuffer_init(&ctx->mp_sbuf);
msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write);
ctx->buffer_id = 0;
return buf;
}
static inline int process_line(char *line, int len,
struct flb_in_serial_config *ctx)
{
/* Increase buffer position */
ctx->buffer_id++;
/*
* Store the new data into the MessagePack buffer,
* we handle this as a list of maps.
*/
msgpack_pack_array(&ctx->mp_pck, 2);
msgpack_pack_uint64(&ctx->mp_pck, time(NULL));
msgpack_pack_map(&ctx->mp_pck, 1);
msgpack_pack_bin(&ctx->mp_pck, 3);
msgpack_pack_bin_body(&ctx->mp_pck, "msg", 3);
msgpack_pack_bin(&ctx->mp_pck, len);
msgpack_pack_bin_body(&ctx->mp_pck, line, len);
flb_debug("[in_serial] message '%s'",
(const char *) line);
return 0;
}
static inline int process_pack(struct flb_in_serial_config *ctx,
char *pack, size_t size)
{
size_t off = 0;
msgpack_unpacked result;
msgpack_object entry;
ctx->buffer_id++;
/* First pack the results, iterate concatenated messages */
msgpack_unpacked_init(&result);
while (msgpack_unpack_next(&result, pack, size, &off)) {
entry = result.data;
msgpack_pack_array(&ctx->mp_pck, 2);
msgpack_pack_uint64(&ctx->mp_pck, time(NULL));
msgpack_pack_map(&ctx->mp_pck, 1);
msgpack_pack_bin(&ctx->mp_pck, 3);
msgpack_pack_bin_body(&ctx->mp_pck, "msg", 3);
msgpack_pack_object(&ctx->mp_pck, entry);
}
msgpack_unpacked_destroy(&result);
return 0;
}
static inline void consume_bytes(char *buf, int bytes, int length)
{
memmove(buf, buf + bytes, length - bytes);
}
/* Callback triggered when some serial msgs are available */
int in_serial_collect(struct flb_config *config, void *in_context)
{
int ret;
int bytes = 0;
int available;
int len;
int hits;
char *sep;
char *buf;
jsmntok_t *t;
struct flb_in_serial_config *ctx = in_context;
while (1) {
available = (sizeof(ctx->buf_data) -1) - ctx->buf_len;
if (available > 1) {
bytes = read(ctx->fd, ctx->buf_data + ctx->buf_len, available);
if (bytes == -1) {
if (errno == EPIPE || errno == EINTR) {
return -1;
}
return 0;
}
else if (bytes == 0) {
return 0;
}
}
ctx->buf_len += bytes;
/* Always set a delimiter to avoid buffer trash */
ctx->buf_data[ctx->buf_len] = '\0';
/* Check if our buffer is full */
if (ctx->buffer_id + 1 == SERIAL_BUFFER_SIZE) {
ret = flb_engine_flush(config, &in_serial_plugin);
if (ret == -1) {
ctx->buffer_id = 0;
}
}
sep = NULL;
buf = ctx->buf_data;
len = ctx->buf_len;
hits = 0;
/* Handle FTDI handshake */
if (ctx->buf_data[0] == '\0') {
consume_bytes(ctx->buf_data, 1, ctx->buf_len);
ctx->buf_len--;
}
/* Strip CR or LF if found at first byte */
if (ctx->buf_data[0] == '\r' || ctx->buf_data[0] == '\n') {
/* Skip message with one byte with CR or LF */
flb_trace("[in_serial] skip one byte message with ASCII code=%i",
ctx->buf_data[0]);
consume_bytes(ctx->buf_data, 1, ctx->buf_len);
ctx->buf_len--;
}
/* Handle the case when a Separator is set */
if (ctx->separator) {
while ((sep = strstr(ctx->buf_data, ctx->separator))) {
len = (sep - ctx->buf_data);
if (len > 0) {
/* process the line based in the separator position */
process_line(buf, len, ctx);
consume_bytes(ctx->buf_data, len + ctx->sep_len, ctx->buf_len);
ctx->buf_len -= (len + ctx->sep_len);
hits++;
}
else {
consume_bytes(ctx->buf_data, ctx->sep_len, ctx->buf_len);
ctx->buf_len -= ctx->sep_len;
}
ctx->buf_data[ctx->buf_len] = '\0';
}
if (hits == 0 && available <= 1) {
flb_debug("[in_serial] no separator found, no more space");
ctx->buf_len = 0;
return 0;
}
}
else if (ctx->format == FLB_SERIAL_FORMAT_JSON) {
/* JSON Format handler */
char *pack;
int out_size;
ret = flb_pack_json_state(ctx->buf_data, ctx->buf_len,
&pack, &out_size, &ctx->pack_state);
if (ret == FLB_ERR_JSON_PART) {
flb_debug("[in_serial] JSON incomplete, waiting for more data...");
return 0;
}
else if (ret == FLB_ERR_JSON_INVAL) {
flb_debug("[in_serial] invalid JSON message, skipping");
flb_pack_state_reset(&ctx->pack_state);
flb_pack_state_init(&ctx->pack_state);
ctx->pack_state.multiple = FLB_TRUE;
return -1;
}
/*
* Given the Tokens used for the packaged message, append
* the records and then adjust buffer.
*/
process_pack(ctx, pack, out_size);
flb_free(pack);
t = &ctx->pack_state.tokens[0];
consume_bytes(ctx->buf_data, t->end, ctx->buf_len);
ctx->buf_len -= t->end;
ctx->buf_data[ctx->buf_len] = '\0';
flb_pack_state_reset(&ctx->pack_state);
flb_pack_state_init(&ctx->pack_state);
ctx->pack_state.multiple = FLB_TRUE;
}
else {
/* Process and enqueue the received line */
process_line(ctx->buf_data, ctx->buf_len, ctx);
ctx->buf_len = 0;
}
}
return 0;
}
/* Cleanup serial input */
int in_serial_exit(void *in_context, struct flb_config *config)
{
struct flb_in_serial_config *ctx = in_context;
flb_trace("[in_serial] Restoring original termios...");
tcsetattr(ctx->fd, TCSANOW, &ctx->tio_orig);
flb_pack_state_reset(&ctx->pack_state);
flb_free(ctx);
return 0;
}
/* Init serial input */
int in_serial_init(struct flb_input_instance *in,
struct flb_config *config, void *data)
{
int fd;
int ret;
int br;
struct flb_in_serial_config *ctx;
(void) data;
ctx = flb_calloc(1, sizeof(struct flb_in_serial_config));
if (!ctx) {
perror("calloc");
return -1;
}
ctx->format = FLB_SERIAL_FORMAT_NONE;
if (!serial_config_read(ctx, in)) {
return -1;
}
/* Initialize JSON pack state */
if (ctx->format == FLB_SERIAL_FORMAT_JSON) {
flb_pack_state_init(&ctx->pack_state);
}
/* set context */
flb_input_set_context(in, ctx);
/* initialize MessagePack buffers */
msgpack_sbuffer_init(&ctx->mp_sbuf);
msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write);
/* open device */
fd = open(ctx->file, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1) {
perror("open");
flb_error("[in_serial] Could not open serial port device");
flb_free(ctx);
return -1;
}
ctx->fd = fd;
/* Store original settings */
tcgetattr(fd, &ctx->tio_orig);
/* Reset for new... */
memset(&ctx->tio, 0, sizeof(ctx->tio));
tcgetattr(fd, &ctx->tio);
br = atoi(ctx->bitrate);
cfsetospeed(&ctx->tio, (speed_t) flb_serial_speed(br));
cfsetispeed(&ctx->tio, (speed_t) flb_serial_speed(br));
/* Settings */
ctx->tio.c_cflag &= ~PARENB; /* 8N1 */
ctx->tio.c_cflag &= ~CSTOPB;
ctx->tio.c_cflag &= ~CSIZE;
ctx->tio.c_cflag |= CS8;
ctx->tio.c_cflag &= ~CRTSCTS; /* No flow control */
ctx->tio.c_cc[VMIN] = ctx->min_bytes; /* Min number of bytes to read */
ctx->tio.c_cflag |= CREAD | CLOCAL; /* Enable READ & ign ctrl lines */
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &ctx->tio);
#if __linux__
/* Set our collector based on a file descriptor event */
ret = flb_input_set_collector_event(in,
in_serial_collect,
ctx->fd,
config);
#else
/* Set our collector based on a timer event */
ret = flb_input_set_collector_time(in,
in_serial_collect,
IN_SERIAL_COLLECT_SEC,
IN_SERIAL_COLLECT_NSEC,
config);
#endif
return ret;
}
/* Plugin reference */
struct flb_input_plugin in_serial_plugin = {
.name = "serial",
.description = "Serial input",
.cb_init = in_serial_init,
.cb_pre_run = NULL,
.cb_collect = in_serial_collect,
.cb_flush_buf = in_serial_flush,
.cb_exit = in_serial_exit
};