/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Power Buoy Data Logger generic file-based data source class   */
/* Filename : fileSource.hpp                                                */
/* Author   : Henthorn                                                      */
/* Project  : Power Buoy                                                    */
/* Version  : 1.0                                                           */
/* Created  : 04/13/2015                                                    */
/* Modified : 05/01/2018                                                    */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*                                                                          */
/* This is a generic class for file-based data sources. If the source can   */
/* be read through a file descriptor, then create a class based on this.    */
/*                                                                          */
/* 05/01/2018: Enable use for non-logging applications (e.g., control) that */
/* want to read and parse the data flow, and return up to date records.     */
/*                                                                          */
/****************************************************************************/

#ifndef _FILESOURCE_
#define _FILESOURCE_

typedef char SourceID;
typedef char DataID;

// These CanBus IDs must match the Controller ID in the CanBus frame
// as it is used in parsing the Canbus data.
// 
const SourceID BatteryConID = 0;
const SourceID SpringConID  = 1;
const SourceID PowerConID   = 2;
const SourceID RudderConID  = 3;
const SourceID TrefoilConID = 4;
const SourceID CrossbowID   = 5;

// Battery controller data packet indicators
// 
const SourceID BC_0         = 10;
const SourceID BC_1         = 11;
const SourceID BC_HYDROGEN  = BC_1;

// Spring controller data packet indicators
// 
const SourceID SC_0         = 20;
const SourceID SC_RANGE     = SC_0;
const SourceID SC_1         = 21;
const SourceID SC_2         = 22;

// Power controller data packet indicators
// 
const SourceID PC_0         = 30;
const SourceID PC_RPM_SDEV  = PC_0;
const SourceID PC_1         = 31;
const SourceID PC_2         = 32;
const SourceID PC_3         = 33;

// Rudder controller data packet indicators
// 
const SourceID RC_0         = 40;
const SourceID RC_1         = 41;
const SourceID RC_2         = 42;
const SourceID RC_3         = 43;
const SourceID RC_4         = 44;
const SourceID RC_5         = 45;
const SourceID RC_6         = 46;
const SourceID RC_7         = 47;

// Trefoil controller data packet indicators
// 
const SourceID TF_0         = 50;
const SourceID TF_1         = 51;
const SourceID TF_2         = 52;
const SourceID TF_3         = 53;
const SourceID TF_4         = 54;
const SourceID TF_5         = 55;

class FileSource {

public:

   // Open a connection to the data source.
   //
   FileSource(const char *name) : _name(NULL), _fd(-1)
   { _name = strdup(name); }

   virtual ~FileSource()
   { if (_name) delete _name; }

   virtual int get_fd() { return _fd; }

   // Return 0 on error
   // Return the (0 - SourceID) if a full record was read from that source
   // Otherwise, return the number of bytes still needed for
   // a full record.
   // 
   // int v = read_data(0);
   // if (v == 0)
   //   still some reading to do; nothing new to return
   // else if (v > 0)
   //   partial or complete record from source (see below for more)
   // else
   //   v < 0, error
   //
   virtual int read_data(char verbose) { return 0; }
   //
   // If a base SourceID is returned, then a complete fresh record from
   // that source is available.
   // If a partial SourceID is returned, then just that part of the record was 
   // read and parsed.

protected:

   char *_name;
   int   _fd;

};

#endif
