/****************************************************************************/
/* 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;

// 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;

// These IDs are not currently used in any processing
// 
const SourceID CrossbowID   = 0x4;

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
   // else if (v < 0)
   //   complete record from source (0 - v)
   // else
   //   v == 0, error
   //
   virtual int read_data(char verbose) { return 0; }

protected:

   char *_name;
   int   _fd;

};

#endif
