- Converted CSocketCore to Class/Object - Implemented CFunctionCore as base class for CPortCore and CSocketCore - Moved common functions to CFunctionCore and renamed eg. Maintain() -> Process() - Pass Objects directly to each other as OutputFunction - New Input() method allows data transfer between objects
162 lines
3.6 KiB
C++
162 lines
3.6 KiB
C++
/*
|
|
* FunctionCore.cpp
|
|
*
|
|
* Created on: 18 May 2016
|
|
* Author: wentzelc
|
|
*/
|
|
|
|
// redA Libraries
|
|
#include "FunctionCore.h"
|
|
#include "TimingCore.h"
|
|
#include "LogCore.h"
|
|
|
|
// Standard C/C++ Libraries
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Life cycle
|
|
CFunctionCore::CFunctionCore( const char * FunctionName )
|
|
{
|
|
// Set name
|
|
if (FunctionName) {
|
|
Name = (char*)malloc( strlen(FunctionName) );
|
|
strcpy( Name, FunctionName );
|
|
}
|
|
else {
|
|
Name = NULL;
|
|
}
|
|
|
|
// In buffer
|
|
Buffer = new CBuffer( 20 );
|
|
OutFunction = NULL;
|
|
|
|
// Input Timeout
|
|
InStart.tv_sec = 0;
|
|
InStart.tv_usec = 0;
|
|
InTimeout = 100; // millisecs
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
CFunctionCore::~CFunctionCore()
|
|
{
|
|
// Destroy pointers
|
|
if (Name) {
|
|
free( Name );
|
|
}
|
|
if (Buffer) {
|
|
delete Buffer;
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Manual Data Input/Output
|
|
int CFunctionCore::Input( int InputID, char * Buffer, int MaxLen )
|
|
{
|
|
return 0;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
int CFunctionCore::Output( int OutputID, char * Buffer, int Len )
|
|
{
|
|
return 0;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Automated Data Input/Output
|
|
bool CFunctionCore::AddInput( int InputID, CFunctionCore * OutFunction, int OutputID )
|
|
{
|
|
return false;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool CFunctionCore::AddOutput( int OutputID, CFunctionCore * InFunction, int InputID )
|
|
{
|
|
OutFunction = InFunction;
|
|
|
|
return true;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool CFunctionCore::Process()
|
|
{
|
|
// Misc
|
|
long Duration = 0;
|
|
|
|
if (Buffer->Len() > 0)
|
|
{
|
|
// Check duration since last PortIn
|
|
Duration = TimePassed( InStart );
|
|
if (Duration > InTimeout)
|
|
{
|
|
// Process Input
|
|
ProcessBuffer( true );
|
|
|
|
// Reset timer
|
|
SetInterval( &InStart, 0 );
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Device Interface
|
|
bool CFunctionCore::Read( int fd )
|
|
|
|
{
|
|
int BytesRead = 0;
|
|
|
|
// Read File directly into buffer
|
|
if (!(BytesRead = Buffer->ReadFromFD( fd )))
|
|
return false;
|
|
|
|
// Process Buffer
|
|
ProcessBuffer( false );
|
|
|
|
// Reset timer
|
|
SetStartTime( &InStart );
|
|
|
|
return true;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool CFunctionCore::Write( int fd )
|
|
{
|
|
return false;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
int CFunctionCore::WriteToFD( int FD, char * Data, int Len )
|
|
{
|
|
int BytesWritten = 0;
|
|
int TotalWritten = 0;
|
|
int DataRemain = 0;
|
|
|
|
// Check if buffer created
|
|
if ((FD == -1) || !Data) {
|
|
return 0;
|
|
}
|
|
|
|
// Read Data into buffer
|
|
DataRemain = (Len == -1)? strlen(Data) : Len;
|
|
while (DataRemain)
|
|
{
|
|
// Read from file descriptor
|
|
BytesWritten = write( FD, Data, DataRemain );
|
|
if (BytesWritten <= 0)
|
|
break;
|
|
|
|
// Update Data Pointers
|
|
TotalWritten += BytesWritten;
|
|
DataRemain -= BytesWritten;
|
|
}
|
|
|
|
return TotalWritten;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
|