First version of datalog for TorusTech
[lpc82x] / host / exanh_datalog / main.c
1 /****************************************************************************
2  *  Get data from sensors and decode
3  *
4  *   main.c
5  *
6  *
7  * Copyright 2013-2014 Nathael Pajani <nathael.pajani@ed3l.fr>
8  *
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  ****************************************************************************/
26 /* this protocol handler is designed to run on a host replacing the DTPlug,
27  * but should be easy to use as a source for the protocol handling code for
28  * the DTPlug itself.
29  */
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <termios.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <getopt.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/select.h>
44 #include <sys/time.h>
45 #include <arpa/inet.h>
46 #include <time.h>
47 #include "serial_utils.h"
50 #define BUF_SIZE  100
52 #define PROG_NAME  "Sensors polling and decode"
53 #define VERSION  "0.1"
55 #define MIN_SECS_SINCE_EPOCH 1580000000
56 #define MAX_RETRY  10
58 /* Set to 0, 1 or 2 to change debug level */
59 int debug = 0;
61 struct sensor_info {
62         uint8_t addr;
63         uint8_t got_tsl;
64         uint8_t got_veml;
65         uint8_t got_bme;
66         uint16_t raw_humidity; /* Soil */
67         uint16_t lux;
68         uint16_t ir;
69         uint16_t uv;
70         uint16_t pressure;
71         int16_t comp_temp;
72         uint16_t humidity; /* From BME */
73         uint16_t seqnum;
74 };
76 void help(char *prog_name)
77 {
78         fprintf(stderr, "---------------- "PROG_NAME" --------------------------------\n");
79         fprintf(stderr, "Usage: %s [options]\n" \
80                 "  Available options:\n" \
81                 "  \t -d | --device : Serial device to use for serial communication with the module\n" \
82                 "  \t -c | --chain : Sensor Chain number (used to store data to the right directory)\n" \
83                 "  \t -p | --period : delay in seconds between two sensors data request\n" \
84                 "  \t -V | --verbose : be more verbose\n" \
85                 "  \t -D | --debug : add full debug\n" \
86                 "  \t -h | --help : Print this help\n" \
87                 "  \t -v | --version : Print programm version\n" \
88                 "  All other arguments are data for the command, handled depending on the command.\n", prog_name);
89         fprintf(stderr, "-----------------------------------------------------------------------\n");
90 }
93 /*****************************************************************************/
94 /* Address handling */
96 #define NB_ADDRESS 8
97 uint8_t addr_list[NB_ADDRESS + 1];
98 uint8_t addr_retry[NB_ADDRESS + 1];
99 uint16_t last_seq_num[NB_ADDRESS + 1];
100 uint8_t probe_sensors[NB_ADDRESS + 1];
101 int pkt_count[NB_ADDRESS];
103 int get_next_free_addr(void)
105         int i = 0;
106         /* Start at 1, do not use address 0 */
107         for (i = 1; i < NB_ADDRESS; i++) {
108                 if (addr_list[i] == 0) {
109                         addr_list[i] = i;
110                         return i;
111                 }
112         }
113         return -1;
116 /*****************************************************************************/
117 /* Requests to sensors */
119 void send_request(int slave_fd, int addr_index)
121         char buf[4];
123         buf[0] = '#';
124         buf[1] = addr_index;
125         buf[2] = 'a'; /* Request all sensor info */
127         write(slave_fd, buf, 3);
128         if (debug) {
129                 printf("Sent request to %d(%d)\n", addr_index, addr_list[addr_index]);
130         }
133 void send_new_address(int slave_fd)
135         char buf[4];
137         buf[0] = '#';
138         buf[1] = 0xFF;
139         buf[2] = get_next_free_addr();
141         write(slave_fd, buf, 3);
142         if (debug) {
143                 printf("New address sent: %d\n", buf[2]);
144         }
147 void set_led(int slave_fd, struct sensor_info* info)
149         uint8_t buf[6];
150         int red = 0;
151         int green = 0;
153         red = (info->raw_humidity - 1500) / 10;
154         green = 200 - red;
155         buf[0] = '#';
156         buf[1] = info->addr;
157         buf[2] = 'l';
158         buf[3] = red; /* Red */
159         buf[4] = green; /* Green */
160         buf[5] = 0; /* Blue */
162         write(slave_fd, buf, 6);
163         if (debug) {
164                 printf("Led color sent: %d : %d - %d\n", buf[1], buf[3], buf[4]);
165         }
168 void clear_leds(int slave_fd)
170         char buf[6];
172         buf[0] = '#';
173         buf[1] = 0xFF;
174         buf[2] = 'l';
175         buf[3] = 0; /* Red */
176         buf[4] = 0; /* Green */
177         buf[5] = 0; /* Blue */
179         write(slave_fd, buf, 6);
180         if (debug) {
181                 printf("Leds cleared\n");
182         }
185 /*****************************************************************************/
186 /* Packets handling */
188 #define PKT_SIZE 20
189 uint8_t data[150];
190 int data_idx = 0;
191 int bad_checksums = 0;
193 int packet_slice(char c)
195         if (data_idx == 0) {
196                 if (c == '#') {
197                         data[0] = c;
198                         data_idx = 1;
199                 } else if (c == '$') {
200                         data[0] = c;
201                         /* Set packet length to 19 so that next character received (request type)
202                                 will terminate the packet */
203                         data_idx = PKT_SIZE - 1;
204                 }
205                 return 0;
206         }
207         if (data_idx > 0) {
208                 data[data_idx] = c;
209                 if (data_idx == (PKT_SIZE - 1)) {
210                         data_idx = 0;
211                         return 1;
212                 }
213                 data_idx++;
214         }
215         return 0;
218 int packet_check(void)
220         switch (data[0]) {
221                 case '#' : { /* Data packet */
222                         int i = 0;
223                         uint32_t sum = 0;
224                         for (i = 0; i < (PKT_SIZE - 1); i++) {
225                                 sum += data[i];
226                         }
227                         if ((sum & 0xFF) == data[(PKT_SIZE - 1)]) {
228                                 printf("Packet OK\n");
229                                 return 1; /* Checksum OK */
230                         }
231                         printf("Bad packet (cksum)\n");
232                         bad_checksums++; /* Count bad checksum for current address */
233                         return 2;
234                 }
235                 break;
236                 case '$' : /* Request from device */
237                         printf("Request received: %c\n", data[(PKT_SIZE - 1)]);
238                         if (data[(PKT_SIZE - 1)] == 'c') {
239                                 /* Request for a new address */
240                                 return 3;
241                         }
242                 break;
243         }
244         return 0;
247 /*****************************************************************************/
248 void parse_packet(struct sensor_info* info)
250         uint16_t* vars = (uint16_t*)data;
251         
252         /* Decode data */
253         info->addr = data[1] & 0x1F;
254         info->raw_humidity = (uint16_t)htons(vars[1]);
255         info->lux = (uint16_t)htons(vars[2]);
256         info->ir = (uint16_t)htons(vars[3]);
257         info->uv = (uint16_t)htons(vars[4]);
258         info->pressure = (uint16_t)htons(vars[5]);
259         info->comp_temp = (int16_t)htons(vars[6]);
260         info->humidity = (uint16_t)htons(vars[7]);
261         info->seqnum = (uint16_t)htons(vars[8]);
263         pkt_count[info->addr]++;
266 void display_info(struct sensor_info* info)
268         if (debug) {
269                 /* Display data */
270                 if (debug == 3) {
271                         printf("\e[H"); /* Goto terminal home (top left) */
272                 }
273                 printf("\e[KSensor %d, pkt %d, seq %d\n", info->addr, pkt_count[info->addr], info->seqnum);
274                 if (debug == 3) {
275                         printf("\e[K\tSoil: %d\n", info->raw_humidity);
276                         printf("\e[K\tLux: %d, IR: %d, UV: %d\n", info->lux, info->ir, info->uv);
277                         printf("\e[K\tPatm: %d hPa, Temp: %d,%02d degC, Humidity: %d,%d rH\n\n",
278                                 info->pressure,
279                                 info->comp_temp / 10,  (info->comp_temp > 0) ? (info->comp_temp % 10) : ((-info->comp_temp) % 10),
280                                 info->humidity / 10, info->humidity % 10);
281                         printf("\e[K\n");
282                 }
283         }
286 #define LOGFILE_NAME_LEN 100
287 #define LINE_LEN 200
288 void store_info(struct sensor_info* info, int chain, time_t seconds)
290         char logfile_name[LOGFILE_NAME_LEN];
291         struct tm time;
292         struct stat statbuf;
293         int logfile = 0, ret = 0;
295         gmtime_r(&seconds, &time);
296         snprintf(logfile_name, LOGFILE_NAME_LEN, "chain%d/sensor%d/%d-%02d-%02d.log",
297                                                  chain, info->addr,
298                                                  (time.tm_year + 1900), (time.tm_mon + 1), time.tm_mday);
300         /* Upon each day change, create a new file with it's header */
301         if (stat(logfile_name, &statbuf) == -1) {
302                 logfile = open(logfile_name, (O_CREAT | O_WRONLY), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
303                 if (logfile > 0) {
304                         /* Write header to file */
305                         char header[LINE_LEN];
306                         int nb = 0;
307                         nb = snprintf(header, LINE_LEN, "# Data logs for sensor %d on chain %d - %s#\n",
308                                                  info->addr, chain, ctime(&seconds));
309                         ret = write(logfile, header, nb);
310                         if (ret != nb) {
311                                 perror("Write error for hearder");
312                                 fprintf(stderr, "Error while writting first part of header to log file %s (%d) for sensor %d\n",
313                                                         logfile_name, logfile, info->addr);
314                                 close(logfile);
315                                 return;
316                         }
317                         nb = snprintf(header, LINE_LEN,
318                                  "# Format : seconds(epoch), seq number, raw soil, raw lux, raw ir, raw uv, press (hPa), temp(1/10°C), air hum(1/10%%)\n#\n\n");
319                         ret = write(logfile, header, nb);
320                         if (ret != nb) {
321                                 perror("Write error for hearder");
322                                 fprintf(stderr, "Error while writting second part of header to log file %s (%d) for sensor %d\n",
323                                                         logfile_name, logfile, info->addr);
324                                 close(logfile);
325                                 return;
326                         }
327                 } else {
328                         perror("Create logfile error");
329                         fprintf(stderr, "Unable to create log file %s for sensor %d\n", logfile_name, info->addr);
330                         return;
331                 }
332                 close(logfile);
333         }
335         logfile = open(logfile_name, (O_APPEND | O_WRONLY));
336         if (logfile > 0) {
337                 char line[LINE_LEN];
338                 int nb = 0;
339                 nb = snprintf(line, LINE_LEN, "%ld, %d, %d, %d, %d, %d, %d, %d, %d\n",
340                                                         seconds, info->seqnum, info->raw_humidity,
341                                                         info->lux, info->ir, info->uv,
342                                                         info->pressure, info->comp_temp, info->humidity);
343                 ret = write(logfile, line, nb);
344                 if (ret != nb) {
345                         perror("Write error for data");
346                         fprintf(stderr, "Error while writting data line to log file %s (%d) for sensor %d\n",
347                                                 logfile_name, logfile, info->addr);
348                 }
349                 close(logfile);
350         } else {
351                 perror("Error openning logfile for data");
352                 fprintf(stderr, "Unable to open logfile %s for sensor %d\n", logfile_name, info->addr);
353         }
356 /*****************************************************************************/
357 /* Main part of programm */
359 int main(int argc, char* argv[])
361         /* Serial */
362         char* device = NULL;
363         int slave_fd = 0;
364         int chain = 0;
365         int period = 1;
366         struct timeval start_tstamp;
368         while(1) {
369                 int option_index = 0;
370                 int c = 0;
372                 struct option long_options[] = {
373                         {"device", required_argument, 0, 'd'},
374                         {"chain", required_argument, 0, 'c'},
375                         {"period", required_argument, 0, 'p'},
376                         {"debug", no_argument, 0, 'D'},
377                         {"verbose", no_argument, 0, 'V'},
378                         {"help", no_argument, 0, 'h'},
379                         {"version", no_argument, 0, 'v'},
380                         {0, 0, 0, 0}
381                 };
383                 /* If time is not yet setup, wait for system to fix this */
384                 do {
385                         gettimeofday(&start_tstamp, NULL);
386                 } while (start_tstamp.tv_sec < MIN_SECS_SINCE_EPOCH);
388                 c = getopt_long(argc, argv, "d:c:p:DVhv", long_options, &option_index);
390                 /* no more options to parse */
391                 if (c == -1) break;
392                 switch (c) {
394                         /* d, device */
395                         case 'd':
396                                 device = optarg;
397                                 break;
399                         /* p, period */
400                         case 'p':
401                                 period = strtoul(optarg, NULL, 10);
402                                 break;
404                         /* c, chain */
405                         case 'c':
406                                 chain = strtoul(optarg, NULL, 10);
407                                 break;
409                         /* V, verbose */
410                         /* D, debug */
411                         case 'D':
412                                 debug++;
413                                 __attribute__ ((fallthrough));
414                         case 'V':
415                                 debug++;
416                                 break;
418                         /* v, version */
419                         case 'v':
420                                 printf("%s Version %s\n", PROG_NAME, VERSION);
421                                 return 0;
422                                 break;
424                         /* h, help */
425                         case 'h':
426                         default:
427                                 help(argv[0]);
428                                 return 0;
429                 }
430         }
433         /* Need Serial port as parameter */
434         if (device == NULL) {
435                 printf("Error, need serial (tty) device\n");
436                 help(argv[0]);
437                 return -1;
438         }
440         memset(addr_list, 0, NB_ADDRESS);
441         memset(pkt_count, 0, NB_ADDRESS * sizeof(int));
443         /* Open tty */
444         slave_fd = serial_setup(device);
445         if (slave_fd < 0) {
446                 printf("Unable to open specified serial port %s\n", device);
447                 return -2;
448         }
450         printf("Logs started on %s\n", ctime(&(start_tstamp.tv_sec)));
452         /* ************************************************* */
453         /* And never stop handling data ! */
454         while (1) {
455                 static int addr_index = 0;
456                 static time_t last = 0;
457                 int len = 0, ret = 0;
458                 struct timeval now;
459                 char buf[BUF_SIZE];
461                 /* Handle time */
462                 gettimeofday(&now, NULL); /* Get seconds since epoch */
464                 /* Send periodic requests for data */
465                 if (now.tv_sec >= (last + period)) {
466                         addr_index = NB_ADDRESS - 1;
467                         last = now.tv_sec;
468                         if (debug) {
469                                 printf("Here\n");
470                         }
471                         clear_leds(slave_fd);
472                         /* Reset all retry counters */
473                         memset(addr_retry, MAX_RETRY, NB_ADDRESS);
474                 }
475                 if (addr_index > 0) {
476                         send_request(slave_fd, addr_index);
477                         data_idx = 0; /* Moving to next sensor, reset data index for packet Rx */
478                         bad_checksums = 0; /* Reset bad checksums for current address */
479                         addr_index--;
480                 }
481                 usleep(100 * 1000); /* Leave some time for the sensor to reply, and for the system */
483                 memset(buf, 0, BUF_SIZE);
484                 /* Get serial data and try to build a packet */
485                 len = read(slave_fd, buf, BUF_SIZE);
486                 /* FIXME */ //printf("read: len: %d, idx: %d\n", len, data_idx);
487                 if (len < 0) {
488                         if (errno == EAGAIN) {
489                                 /* Nothing to do, we're in non-blocking mode */
490                         } else {
491                                 if (len == 0) {
492                                         printf("\nError, got activity on serial link, but no data ... End of file.\n");
493                                 } else {
494                                         perror("serial read error");
495                                 }
496                                 close(slave_fd);
497                                 len = 0;
498                                 do {
499                                         slave_fd = serial_setup(device);
500                                         usleep(10 * 1000);
501                                 } while (slave_fd < 0);
502                         }
503                 } else {
504                         int idx = 0;
505                         /* We are receiving something, log to stderr */
506                         if (debug == 2) {
507                                 write(2, buf, len);
508                         }
509                         while (idx < len) {
510                                 ret = packet_slice(buf[idx]);
511                                 /* Check return code to know if we have enough data for a packet */
512                                 if (ret == 1) {
513                                         /* check that the packet is valid */
514                                         ret = packet_check();
515                                 }
516                                 switch (ret) {
517                                         case 1: {
518                                                         struct sensor_info info;
519                                                         /* Valid packet received, parse data */
520                                                         parse_packet(&info);
521                                                         if (info.seqnum == last_seq_num[info.addr]) {
522                                                                 /* Ignore duplicated packet */
523                                                                 break;
524                                                         }
525                                                         /* Check data validity */
526                                                         if ((info.raw_humidity < 1000) || (info.raw_humidity > 4000)) {
527                                                                 break;
528                                                         }
529                                                         if ((info.pressure < 800) || (info.pressure > 1200)) {
530                                                                 break;
531                                                         }
532                                                         if ((info.comp_temp < -400) || (info.comp_temp > 1000)) {
533                                                                 break;
534                                                         }
535                                                         if (info.humidity > 1000) {
536                                                                 break;
537                                                         }
539                                                         /* New packet received */
540                                                         last_seq_num[info.addr] = info.seqnum;
541                                                         display_info(&info);
543                                                         /* New device ? store address */
544                                                         if ((info.addr < NB_ADDRESS) && (addr_list[info.addr] == 0)) {
545                                                                 addr_list[info.addr] = info.addr;
546                                                         }
547                 
548                                                         /* Store data to log file */
549                                                         store_info(&info, chain, now.tv_sec);
551                                                         /* Update leds */
552                                                         set_led(slave_fd, &info);
553                                                 }
554                                                 break;
555                                         case 2:
556                                                 /* Checksum error for all three packets, retry */
557                                                 if ((addr_retry[addr_index + 1] > 0) && (bad_checksums == 3)) {
558                                                         addr_index++;
559                                                         addr_retry[addr_index]--;
560                                                 }
561                                                 break;
562                                         case 3:
563                                                 /* Done handling received data, need to send a new address to a sensor ? */
564                                                 send_new_address(slave_fd);
565                                                 break;
566                                 }
567                                 idx++;
568                         }
569                 }
570         } /* End of infinite loop */
572         close(slave_fd);
573         return 0;