/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Power Buoy Data Logger CANBus instrument reader class         */
/* Filename : CanBus.hpp                                                    */
/* Author   : Henthorn                                                      */
/* Project  : Power Buoy                                                    */
/* Version  : 1.0                                                           */
/* Created  : 04/20/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*                                                                          */
/* This class is the data source reader for the Boost Controller instrument */
/* It is derived from the generic FileSource class.                         */
/*                                                                          */
/****************************************************************************/

#ifndef _CANBUS_
#define _CANBUS_

#include <linux/can.h>
#include <linux/can/raw.h>

#include "FileSource.hpp"

class LogWriter;

class CanBus : public FileSource
{

public:

   struct BC_Record {
      short  seq_num;
      float  time;
      float  voltage;
      short  p1dc;
      short  pmeas;
      short  mindbar;
      short  lag;
      short  itemp1;
      short  itemp2;
      short  itemp3;
      short  itemp4;
      float  vtarget;
      float  ctarget;
      float  ibridge;
      float  iload;
      float  bridgedc;
      float  loaddc;
      float  rpms;
      float  torque;
   };

   // Open a connection to the data source.
   //
   CanBus(const char* cansock, LogWriter* w = NULL);

   ~CanBus();

   // Return 0 if full record was read
   // Otherwise, return the number of bytes still needed for
   // a full record.
   // Return < 0 on error.
   //
   int read_data();

   // Return the latest full data record in the buffer supplied by the caller.
   // Return the number of bytes written to the buffer (0 or more).
   // Return < 0 on error.
   //
   int get_record(const char *buffer);

protected:

   unsigned createMask(unsigned a, unsigned b);

   // Handle Boost Controller frame
   //
   int handle_boost_msg(struct can_frame);

   // Handle Pneumatic Spring Controller frame
   //
   int handle_spring_msg(struct can_frame);

   char* _cansock;   // e.g., "can0", "can1"
   int   _curr_sc_seq;

   struct CanBus::BC_Record  _bc_record;
   void *_sc_record;
   LogWriter* _logger;
};

#endif
