/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Power Buoy logger app entry point.                            */
/* Filename : pblog.cpp                                                     */
/* Author   : Henthorn                                                      */
/* Project  : Power Buoy                                                    */
/* Version  : 1.0                                                           */
/* Created  : 04/13/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*                                                                          */
/* This application is the entry point for many (if not all) the CANBus     */
/*                                                                          */
/****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>

#include "LogWriter.hpp"
#include "Crossbow.hpp"
#include "CanBus.hpp"

#define  OK         0
#define  BAD_SOCK  -1      // Socket comms failed
#define  BAD_OPTS  -2      // Inconsistent option values

void usage()
{
   printf("usage:\n");
   printf(" pblog [-v] \n");
   printf("   -v       Verbose     - chatty\n");
}

void write_record(FILE* log, int canfd, int cannum, int cbfd, int cbnum)
{
   return;

   printf("Writing to log...\n");
   struct timeval time;
   gettimeofday( &time, 0 );

   double sec = time.tv_sec + time.tv_usec / 1000000.0;

   char logbuf[32];
   sprintf(logbuf, "%.3f, %d, %d, %d, %d\n", sec, canfd, cannum, cbfd, cbnum);
   fputs(logbuf, log);
}

int main(int argc, char **argv)
{
   char verbose = true;

   // Create log writer and readers
   //
   LogWriter w("log.csv");
   //FILE *log = fopen("log.csv", "w+");
   int cannum = 0, cbnum = 0;

   // CanbusReader   cbus(w);
   // CrossbowReader xbow(w);
   Crossbow xbow(&w);
   CanBus   can("can0", &w);

   // 
   //while(1)
   while(cbnum < 25)
   {
      struct timeval timeout = {0, 40000L};
      int max_fd = 0;
      fd_set readset;
      FD_ZERO(&readset);

      FD_SET(can.get_fd(), &readset);
      if (can.get_fd() > max_fd) max_fd = can.get_fd();

      FD_SET(xbow.get_fd(), &readset);
      if (xbow.get_fd() > max_fd) max_fd = xbow.get_fd();

      if (select((max_fd + 1), &readset, NULL, NULL, &timeout) >= 0)
      {
         int nsources = 0;
         if (FD_ISSET(can.get_fd(), &readset))
         {
            if (0 == can.read_data());
               //write_record(log, can.get_fd(), cannum++, xbow.get_fd(), cbnum);
         }

         if (FD_ISSET(xbow.get_fd(), &readset))
         {
            if (0 == xbow.read_data()) cbnum++;
               //write_record(log, can.get_fd(), cannum, xbow.get_fd(), cbnum++);
         }

         // Tell logger to write a log record if any data source updated
         //
//       if (nsources > 0) w->write();
      }
   }

//   fclose(log);

   return OK;
}
