-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathraspjson.cpp
619 lines (527 loc) · 16.5 KB
/
raspjson.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// **********************************************************************************
// Raspberry PI LibTeleinfo sample, display JSON data of modified teleinfo values received
// **********************************************************************************
// Creative Commons Attrib Share-Alike License
// You are free to use/extend this library but please abide with the CC-BY-SA license:
// http://creativecommons.org/licenses/by-sa/4.0/
//
// for detailled explanation of this library see dedicated article
// https://hallard.me/libteleinfo/
//
// For any explanation about teleinfo or use, see my blog
// https://hallard.me/category/tinfo
//
// Written by Charles-Henri Hallard (https://hallard.me)
//
// History : V1.00 2015-07-14 - First release
//
// All text above must be included in any redistribution.
//
// **********************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <termios.h>
#include <getopt.h>
#include <sys/sysinfo.h>
#include "../../src/LibTeleinfo.h"
// ----------------
// Constants
// ----------------
#define true 1
#define false 0
#define PRG_NAME "raspjson"
#define TELEINFO_DEVICE ""
#define TELEINFO_BUFSIZE 512
// Some enum for serial
enum parity_e { P_NONE, P_EVEN, P_ODD };
enum flowcntrl_e { FC_NONE, FC_RTSCTS, FC_XONXOFF };
enum mode_e { MODE_NONE, MODE_SEND, MODE_RECEIVE, MODE_TEST };
enum value_e { VALUE_NOTHING, VALUE_ADDED, VALUE_EXIST, VALUE_CHANGED};
// Configuration structure
static struct
{
char port[128];
int baud;
enum flowcntrl_e flow;
char flow_str[32];
enum parity_e parity;
char parity_str[32];
int databits;
int verbose;
// Configuration structure defaults values
} opts ;
void sendJSON(ValueList * me, boolean all);
// ======================================================================
// Global vars
// ======================================================================
int g_fd_teleinfo; // teleinfo serial handle
struct termios g_oldtermios ; // old serial config
int g_exit_pgm; // indicate en of the program
struct sysinfo g_info;
TInfo tinfo; // Teleinfo object
// Used to indicate if we need to send all date or just modified ones
boolean fulldata = true;
/* ======================================================================
Function: ADPSCallback
Purpose : called by library when we detected a ADPS on any phased
Input : phase number
0 for ADPS (monophase)
1 for ADIR1 triphase
2 for ADIR2 triphase
3 for ADIR3 triphase
Output : -
Comments: should have been initialised in the main sketch with a
tinfo.attachADPSCallback(ADPSCallback())
====================================================================== */
void ADPSCallback(uint8_t phase)
{
// Envoyer JSON { "ADPS"; n}
// n = numero de la phase 1 à 3
if (phase == 0)
phase = 1;
printf( "{\"ADPS\":%c}\r\n",'0' + phase);
fflush(stdout);
}
/* ======================================================================
Function: NewFrame
Purpose : callback when we received a complete teleinfo frame
Input : linked list pointer on the concerned data
Output : -
Comments: -
====================================================================== */
void NewFrame(ValueList * me)
{
// Envoyer les valeurs uniquement si demandé
if (fulldata)
sendJSON(me, true);
fulldata = false;
}
/* ======================================================================
Function: UpdatedFrame
Purpose : callback when we received a complete teleinfo frame
Input : linked list pointer on the concerned data
Output : -
Comments: it's called only if one data in the frame is different than
the previous frame
====================================================================== */
void UpdatedFrame(ValueList * me)
{
// Envoyer les valeurs
sendJSON(me, fulldata);
fulldata = false;
}
/* ======================================================================
Function: sendJSON
Purpose : dump teleinfo values on serial
Input : linked list pointer on the concerned data
true to dump all values, false for only modified ones
Output : -
Comments: -
====================================================================== */
void sendJSON(ValueList * me, boolean all)
{
bool firstdata = true;
// Got at least one ?
if (me) {
// Json start
printf("{");
if (all) {
printf("\"_UPTIME\":%ld", g_info.uptime);
firstdata = false;
}
// Loop thru the node
while (me->next) {
// go to next node
me = me->next;
// uniquement sur les nouvelles valeurs ou celles modifiées
// sauf si explicitement demandé toutes
if ( all || ( me->flags & (TINFO_FLAGS_UPDATED | TINFO_FLAGS_ADDED) ) )
{
// First elemement, no comma
if (firstdata)
firstdata = false;
else
printf(", ") ;
printf("\"%s\":", me->name) ;
// we have at least something ?
if (me->value && strlen(me->value))
{
boolean isNumber = true;
uint8_t c;
char * p = me->value;
// Format N° Serial 16 caractères
if ( strcmp(me->value, "ADCO") || strcmp(me->value, "ADSC") || strcmp(me->value, "PRM") ) {
isNumber = false;
}
// check if value is number
while (*p && isNumber) {
if ( *p < '0' || *p > '9' )
isNumber = false;
p++;
}
// this will add "" on not number values
if (!isNumber) {
printf("\"%s\"", me->value) ;
}
// this will remove leading zero on numbers
else
printf("%ld",atol(me->value));
}
}
}
// Json end
printf("}\r\n") ;
fflush(stdout);
}
}
// ======================================================================
// some func declaration
// ======================================================================
void tlf_close_serial(int);
/* ======================================================================
Function: log_syslog
Purpose : write event to syslog
Input : stream to write if needed
string to write in printf format
printf other arguments
Output : -
Comments:
====================================================================== */
void log_syslog( FILE * stream, const char *format, ...)
{
static char tmpbuff[512]="";
va_list args;
int len;
// do a style printf style in ou buffer
va_start (args, format);
len = vsnprintf (tmpbuff, sizeof(tmpbuff), format, args);
tmpbuff[sizeof(tmpbuff) - 1] = '\0';
va_end (args);
// Write to logfile
openlog( PRG_NAME, LOG_PID|LOG_CONS, LOG_USER);
syslog(LOG_INFO, "%s", tmpbuff);
closelog();
// stream passed ? write also to it
if (stream && opts.verbose )
{
fprintf(stream, "%s", tmpbuff);
//fprintf(stream, "\n");
fflush(stream);
}
}
/* ======================================================================
Function: clean_exit
Purpose : exit program
Input : exit code
Output : -
Comments:
====================================================================== */
void clean_exit (int exit_code)
{
int r;
// free up linked list
tinfo.listDelete();
// close serials
if (g_fd_teleinfo)
{
// Restore Old parameters.
if ( (r = tcsetattr(g_fd_teleinfo, TCSAFLUSH, &g_oldtermios)) < 0 )
log_syslog(stderr, "cannot restore old parameters %s: %s", opts.port, strerror(errno));
// then close
tlf_close_serial(g_fd_teleinfo);
}
exit(exit_code);
}
/* ======================================================================
Function: fatal
Purpose : exit program due to a fatal error
Input : string to write in printf format
printf other arguments
Output : -
Comments:
====================================================================== */
void fatal (const char *format, ...)
{
char tmpbuff[512] = "";
va_list args;
int len;
va_start(args, format);
len = vsnprintf(tmpbuff, sizeof(tmpbuff), format, args);
tmpbuff[sizeof(tmpbuff) - 1] = '\0';
va_end(args);
// Write to logfile
openlog( PRG_NAME, LOG_PID | LOG_CONS, LOG_USER);
syslog(LOG_INFO, "%s", tmpbuff);
closelog();
fprintf(stderr,"\r\nFATAL: %s \r\n", tmpbuff );
fflush(stderr);
clean_exit(EXIT_FAILURE);
}
/* ======================================================================
Function: signal_handler
Purpose : Interrupt routine Code for signal
Input : signal received
Output : -
Comments:
====================================================================== */
void signal_handler (int signum)
{
// Does we received CTRL-C ?
if ( signum==SIGINT )
{
// Indicate we want to quit
g_exit_pgm = true;
log_syslog(stdout, "\nReceived SIGINT\n");
}
else if ( signum==SIGTERM )
{
// Indicate we want to quit
g_exit_pgm = true;
log_syslog(stdout, "\nReceived SIGTERM\n");
}
}
/* ======================================================================
Function: tlf_init_serial
Purpose : initialize serial port for receiving teleinfo
Input : -
Output : Serial Port Handle
Comments: -
====================================================================== */
int tlf_init_serial(void)
{
int tty_fd, r ;
struct termios termios ;
// Open serial device
if ( (tty_fd = open(opts.port, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) < 0 )
fatal( "tlf_init_serial %s: %s", opts.port, strerror(errno));
else
log_syslog( stdout, "'%s' opened.\n",opts.port);
// Set descriptor status flags
fcntl (tty_fd, F_SETFL, O_RDWR ) ;
// Get current parameters for saving
if ( (r = tcgetattr(tty_fd, &g_oldtermios)) < 0 )
log_syslog(stderr, "cannot get current parameters %s: %s", opts.port, strerror(errno));
// copy current parameters and change for our own
memcpy( &termios, &g_oldtermios, sizeof(termios));
// raw mode
cfmakeraw(&termios);
// Set serial speed to 1200 bps
if (cfsetospeed(&termios, B1200) < 0 || cfsetispeed(&termios, B1200) < 0 )
log_syslog(stderr, "cannot set serial speed to 1200 bps: %s", strerror(errno));
// Parity Even
termios.c_cflag &= ~PARODD;
termios.c_cflag |= PARENB;
// 7 databits
termios.c_cflag = (termios.c_cflag & ~CSIZE) | CS7;
// No Flow Control
termios.c_cflag &= ~(CRTSCTS);
termios.c_iflag &= ~(IXON | IXOFF | IXANY);
// Local
termios.c_cflag |= CLOCAL;
// No minimal char but 5 sec timeout
termios.c_cc [VMIN] = 0 ;
termios.c_cc [VTIME] = 50 ;
// now setup the whole parameters
if ( tcsetattr (tty_fd, TCSANOW | TCSAFLUSH, &termios) <0)
log_syslog(stderr, "cannot set current parameters %s: %s", opts.port, strerror(errno));
// Sleep 50ms
// trust me don't forget this one, it will remove you some
// headache to find why serial is not working
usleep(50000);
return tty_fd ;
}
/* ======================================================================
Function: tlf_close_serial
Purpose : close serial port for receiving teleinfo
Input : Serial Port Handle
Output : -
Comments:
====================================================================== */
void tlf_close_serial(int device)
{
if (device)
{
// flush and restore old settings
tcsetattr(device, TCSANOW | TCSAFLUSH, &g_oldtermios);
close(device) ;
}
}
/* ======================================================================
Function: usage
Purpose : display usage
Input : program name
Output : -
Comments:
====================================================================== */
void usage( char * name)
{
printf("%s\n", PRG_NAME);
printf("Usage is: %s [options] -d device\n", PRG_NAME);
printf("Options are:\n");
printf(" --<d>evice dev : open serial device name\n");
printf(" --<v>erbose : speak more to user\n");
printf(" --<h>elp\n");
printf("<?> indicates the equivalent short option.\n");
printf("Short options are prefixed by \"-\" instead of by \"--\".\n");
printf("Example :\n");
printf( "%s -d /dev/ttyAMA0\n\tstart listeming on hardware serial port /dev/ttyAMA0\n\n", PRG_NAME);
printf( "%s -d /dev/ttyUSB0\n\tstart listeming on USB microteleinfo dongle\n\n", PRG_NAME);
}
/* ======================================================================
Function: read_config
Purpose : read configuration from config file and/or command line
Input : -
Output : -
Comments: Config is read from config file then from command line params
this means that command line parameters always override config
file parameters
====================================================================== */
void read_config(int argc, char *argv[])
{
static struct option longOptions[] =
{
{"port", required_argument,0, 'p'},
{"verbose", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int optionIndex = 0;
int c;
char str_opt[64];
char buffer[512];
char* bufp = NULL;
char* opt = NULL;
char* optdata = NULL;
// default values
*opts.port = '\0';
opts.baud = 1200;
opts.flow = FC_NONE;
strcpy(opts.flow_str, "none");
opts.parity = P_EVEN;
strcpy(opts.parity_str, "even");
opts.databits = 7;
opts.verbose = false;
// default options
strcpy( str_opt, "hvd:");
// We will scan all options given on command line.
while (1)
{
// no default error messages printed.
opterr = 0;
// Get option
c = getopt_long(argc, argv, str_opt, longOptions, &optionIndex);
// Last one ?
if (c < 0)
break;
switch (c)
{
case 'v':
opts.verbose = true;
break;
case 'd':
strncpy(opts.port, optarg, sizeof(opts.port) - 1);
opts.port[sizeof(opts.port) - 1] = '\0';
break;
// These ones exit direct
case 'h':
case '?':
usage(argv[0]);
exit(EXIT_SUCCESS);
break;
default:
fprintf(stderr, "Unrecognized option.\n");
fprintf(stderr, "Run %s with '--help'.\n", PRG_NAME);
exit(EXIT_FAILURE);
break;
}
}
if ( !*opts.port)
{
fprintf(stderr, "No tty device given\n");
fprintf(stderr, "please select at least tty device such as /dev/ttyS0\n");
fprintf(stderr, "Run %s with '--help'.\n", PRG_NAME);
exit(EXIT_FAILURE);
}
if (opts.verbose)
{
printf("%s\n", PRG_NAME);
printf("-- Serial Stuff -- \n");
printf("tty device : %s\n", opts.port);
printf("flowcontrol : %s\n", opts.flow_str);
printf("baudrate is : %d\n", opts.baud);
printf("parity is : %s\n", opts.parity_str);
printf("databits are : %d\n", opts.databits);
printf("-- Other Stuff -- \n");
printf("verbose is : %s\n", opts.verbose? "yes" : "no");
printf("\n");
}
}
/* ======================================================================
Function: main
Purpose : Main entry Point
Input : -
Output : -
Comments:
====================================================================== */
int main(int argc, char **argv)
{
struct sigaction sa;
fd_set rdset, wrset;
unsigned char c;
char rcv_buff[TELEINFO_BUFSIZE];
int rcv_idx;
char time_str[200];
time_t t;
struct tm *tmp;
int n;
struct sysinfo info;
g_fd_teleinfo = 0;
g_exit_pgm = false;
// get configuration
read_config(argc, argv);
// Set up the structure to specify the exit action.
sa.sa_handler = signal_handler;
sa.sa_flags = SA_RESTART;
sigfillset(&sa.sa_mask);
sigaction (SIGTERM, &sa, NULL);
sigaction (SIGINT, &sa, NULL);
// Open serial port
g_fd_teleinfo = tlf_init_serial();
// Init teleinfo
tinfo.init();
// Attacher les callback dont nous avons besoin
// pour cette demo, ADPS et TRAME modifiée
tinfo.attachADPS(ADPSCallback);
tinfo.attachUpdatedFrame(UpdatedFrame);
tinfo.attachNewFrame(NewFrame);
log_syslog(stdout, "Inits succeded, entering Main loop\n");
// Do while not end
while ( ! g_exit_pgm ) {
// Read from serial port
n = read(g_fd_teleinfo, &c, 1);
if (n >= 0)
tinfo.process(c);
// Check full frame every 60 sec
sysinfo(&info);
if (info.uptime >= g_info.uptime + 60) {
g_info.uptime = info.uptime;
fulldata = true;
}
// Sleep 10ms; let time to others process
usleep(10000);
}
log_syslog(stderr, "Program terminated\n");
clean_exit(EXIT_SUCCESS);
// avoid compiler warning
return (0);
}