-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.cpp
executable file
·538 lines (477 loc) · 14.4 KB
/
serial.cpp
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
The MIT License (MIT)
Copyright (c) 2017 Wassim Filali
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
___________________________________________________________________________________
dependencies :
- bme_280 with datasheet's associated rights see bme280_server.hpp
___________________________________________________________________________________
start date : 16.07.2016
serial port cpp wrapper
* update() to get data from the serial port buffer
* collect data into lines and parses each completed line
* calls the bme_280 as main sensors are calibrated from within that library
* transforms the registers values into ready to use sensors map Nodes.Sensors.Values,Timestamp
*/
//for ios::out,... #issue once placed in the end of the includes, it does not recognise cout part of std::
#include <iostream>
#include <fstream>
#include <string>
#include "serial.hpp"
//for getTime
#include "utils.hpp"
#include "bme280_server.hpp"
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "log.hpp"
#ifdef CUSTOM_SERIAL_PORT_SPEED_UNDER_INVESTIGATION
//for serial_struct
#include <serial.h>
//for warnx()
#include <err.h>
using namespace std;
void set_custom_speed(int fd,int rate)
{
struct serial_struct serinfo;
/* Custom divisor */
serinfo.reserved_char[0] = 0;
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
return -1;
serinfo.flags &= ~ASYNC_SPD_MASK;
serinfo.flags |= ASYNC_SPD_CUST;
serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate;
if (serinfo.custom_divisor < 1)
serinfo.custom_divisor = 1;
if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
return -1;
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
return -1;
if (serinfo.custom_divisor * rate != serinfo.baud_base) {
warnx("actual baudrate is %d / %d = %f",
serinfo.baud_base, serinfo.custom_divisor,
(float)serinfo.baud_base / serinfo.custom_divisor);
}
}
#endif /*CUSTOM_SERIAL_PORT_SPEED_UNDER_INVESTIGATION*/
int set_interface_attribs (int fd, speed_t baudrate, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf ("error %d from tcgetattr", errno);
return -1;
}
cfsetospeed (&tty, baudrate);
cfsetispeed (&tty, baudrate);
cfmakeraw(&tty); //very important to avoid swapping CR to LF
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 0; // timeout : 100 ms per unit
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf ("error %d from tcsetattr", errno);
return -1;
}
return 0;
}
LogBuffer_c::LogBuffer_c()
{
newLine = true;//must start with a timestamp on the first write;
plinebuf = linebuf;//points on the beginning of the line buffer
}
Serial::Serial()
{
isReady = false;
}
Serial::Serial(json &conf,json &calib)
{
isReady = config(conf,calib);
}
bool Serial::config(json &conf,json &calib)
{
bool success = false;
//serial port config------------------------------------------------------------
if(( conf.find("enable") != conf.end() ) && conf["enable"] )
{
std::string portName = conf["portname"];
std::string port_baud = "115200";
if( conf.find("portbaud") != conf.end() )
{
port_baud = conf["portbaud"];
}
if(start(portName,port_baud))
{
for(json::iterator node = calib.begin(); node != calib.end(); ++node)
{
Log::cout << "str\tloading calib node: " << node.key() << Log::Info();
int l_Id = std::stoi((std::string)node.key());
NodesMeasures[l_Id].load_calib_data(node.value());
}
success = true;
}
}
else
{
Log::cout << "str\tX :Serial Port is not enabled, will not be used" << Log::Info();
}
return success;
}
bool Serial::start(std::string port_name,std::string baudrate)
{
bool success = false;
isLogFile = true;
isLogOut = true;
fd = open (port_name.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
if (fd >= 0)
{
std::string strlog;
strlog+= "str\tport "+port_name+" is open @";
if( utl::compare(baudrate,"500000") )
{
set_interface_attribs (fd, B500000, 0);
strlog+="B500000";
}
else if( utl::compare(baudrate,"115200") )
{
set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity)
strlog+="B115200";
}
else if( utl::compare(baudrate,"9600") )
{
set_interface_attribs (fd, B9600, 0); // set speed to 115,200 bps, 8n1 (no parity)
strlog+="B9600";
}
else
{
set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity)
strlog+="B115200";
}
Log::cout << strlog << Log::Info();
success = true;
}
else
{
Log::cout << "str\tError "+std::to_string(errno)+" opening "+port_name+" : "+strerror(errno) << Log::Error();
}
return success;
}
bool LogBuffer_c::update(int fd)
{
bool res = false;
n = read (fd, buf, sizeof buf); // read up to 100 characters if ready to read
if(n > 0)
{
res = true;
//as we want to print it here, we make sure it is null terminated
if(n < sizeof buf)
{
buf[n] = '\0';//null terminated string
}
else
{
buf[(sizeof buf)-1] = '\0';//must insert a null terminated string, otherwise not safe to print nor search,...
Log::cout << "str\tSlow app Max Buffer reached, loss of data !!!"<< Log::Error();
}
}
else
{
//Log::cout << "Nothing" << std::endl;
}
return res;
}
bool Serial::update()
{
if(isReady)
{
return logbuf.update(fd);//update the content of the local buffer from the serial device
}
else
{
return false;
}
}
void Serial::logBuffer()
{
//Log::cout << "[nb lines]" << logbuf.currentlines.size() << std::endl;
for(std::string cl : logbuf.currentlines)
{
//log(cl);
}
}
void Serial::clearBuffer()
{
logbuf.currentlines.clear();
}
void LogBuffer_c::lastLinesDiscardOld()
{
const int nbSecondsToRetain = 2;
std::time_t right_now;
std::time(&right_now);
std::time_t oldTime = right_now - nbSecondsToRetain;
bool isDone = false;
while(!lastLines.empty() && !isDone)
{
auto &vLine = lastLines.front();
if(vLine.time<=oldTime)
{
//Log::cout << "lastline_Remove>" << vLine.line << std::endl;
lastLines.pop_front();
}
else
{
isDone = true;
}
}
}
bool LogBuffer_c::lastLinesCheck(std::string &line)
{
bool isFound = false;
//-------------------check duplicate
if(!lastLines.empty())
{
//more likely to be in the back
for(std::list<sensor_line_t>::reverse_iterator rit=lastLines.rbegin();
(rit!=lastLines.rend() && !isFound);
++rit)
{
if(utl::compare(line,(*rit).line))
{
isFound = true;
//Log::cout << "lastline_Found>" << line << std::endl;
}
}
}
return isFound;
}
void LogBuffer_c::lastLinesAdd(std::string &line)
{
lastLines.push_back({time_now,line});
//Log::cout << "lastline_add>" << line << std::endl;
}
void handle_float(const std::string &sensorname,strmap ¬if_map,NodeMap_t&nodes,const int node_id,const std::time_t &ts,float coeff=1.0)
{
sensor_measure_t sensor_measure;
sensor_measure.time = ts;
std::string sensor_value_text = notif_map[sensorname];
float sensor_value_int;
try
{
sensor_value_int = std::stof(sensor_value_text);
sensor_measure.value = sensor_value_int * coeff;
nodes[node_id][sensorname].push_back(sensor_measure);
}
catch(const std::exception& ex)
{
Log::cout << "str> !!! Caught exception \"" << ex.what() << "\" trying to convert<"<<sensorname<< "> : <"<< sensor_value_text << ">!!!\n"<< Log::Error();
}
}
void Serial::processLine(NodeMap_t &nodes)
{
//replace end of line by end of string
(*logbuf.plinebuf) = '\0';
std::string logline(logbuf.linebuf);
utl::replace(logline,',',';');
//reset the line buffer pointer to the beginning of the line
logbuf.plinebuf = logbuf.linebuf;
strmap notif_map;
utl::str2map( logline, notif_map);
if(utl::exists(notif_map,"src"))
{
std::string t_Id = notif_map["src"];
int l_Id = std::stoi(t_Id);
//----------------------------------------------------------------------
//TODO complete rework of duplicate detection by excluding rssi and ctrl
//----------------------------------------------------------------------
logbuf.lastLinesDiscardOld();//remove from lastLines[] what is loder than 2 sec
bool isDuplicate = logbuf.lastLinesCheck(logline);//if the line was available in lastLines[]
// GO OUT Completely "return" so "logline" will not be further processed
if(isDuplicate)
{
Log::cout << "str\tDiscarded Duplicate: "<< logline << Log::Debug();
return;
}
//if we reached it here, that means the log line is not duplicate with the previous 2 sec
logbuf.lastLinesAdd(logline);
bool is_partly_handled = false;
logbuf.currentlines.push_back( logbuf.day + "\t" + logbuf.time + "\t" + logline);
// ------------------------------ TODO ------------------------------
//could handle all of these as configurable abilities
if(utl::exists(notif_map,"light"))
{
handle_float("light",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"ambient"))
{
handle_float("ambient",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"red"))
{
handle_float("red",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"green"))
{
handle_float("green",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"blue"))
{
handle_float("blue",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"proximity"))
{
handle_float("proximity",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"temp"))
{
notif_map["temperature"] = notif_map["temp"];
handle_float("temperature",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"hum"))
{
notif_map["humidity"] = notif_map["hum"];
handle_float("humidity",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"press"))
{
notif_map["pressure"] = notif_map["press"];
handle_float("pressure",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"heat"))
{
handle_float("heat",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"battery"))
{
handle_float("battery",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"button"))
{
handle_float("button",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"alive"))
{
handle_float("alive",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"reset"))
{
handle_float("reset",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"power"))
{
handle_float("power",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(utl::exists(notif_map,"rssi"))
{
handle_float("rssi",notif_map,nodes,l_Id,logbuf.time_now);
is_partly_handled = true;
}
if(!is_partly_handled)
{
Log::cout << "rf_soc\t"<<logline << Log::Info();
}
}
}
//we use Serial::buf for data and Serial::n for data size
//isReady is protected by the update that has to change the .n value
NodeMap_t Serial::processBuffer()
{
NodeMap_t nodes;
clearBuffer();//return only the last gathered data
if(logbuf.n>0)
{
char * buf_w = logbuf.buf;
char * buf_end = logbuf.buf + logbuf.n;
while(buf_w != buf_end)
{
bool isp = isprint(*buf_w);
//Timestamping : avoid empty lines do not create a new timestamp if the char is a line ending
if(logbuf.newLine && isp)
{
time(&logbuf.time_now);//take a timestamp
logbuf.day = utl::getDay(logbuf.time_now);
logbuf.time = utl::getTime(logbuf.time_now);
logbuf.newLine = false;
}
//Process characters
if( ((*buf_w) == '\n') || ((*buf_w) == 10) || ((*buf_w) == 13))//only allowed printable character
{
logbuf.newLine = true;
(*logbuf.plinebuf) = (*buf_w);
processLine(nodes);
}
else if(isp)//skip the CR and any other control
{
(*logbuf.plinebuf++) = (*buf_w);
}
//else non printable characters other than '\n' are discarded
buf_w++;
}
}
if(!logbuf.currentlines.empty())
{
Log::cout << "str\tProcessed " << logbuf.currentlines.size() << " Line(s)" << Log::Debug();
for(int i=0;i<logbuf.currentlines.size();i++)
{
Log::cout << "str\t"<< logbuf.currentlines[i] << Log::Verbose();
}
}
return nodes;
}
void Serial::send(char* buffer,int size)
{
if(isReady)
{
write(fd,buffer,size);
//TODO add test for log level that avoids serialising the string
std::string line(buffer);
Log::cout << "str\t" << line << Log::Verbose();
}
else
{
Log::cout << "str\t not enabled" << Log::Verbose();
}
}