/* * FunctionCore.h * * Created on: 18 May 2016 * Author: wentzelc */ #ifndef REDACORE_DEVICECORE_H_ #define REDACORE_DEVICECORE_H_ // redA Libraries #include "FunctionCore.h" // Standard C/C++ Libraries /* no additional */ //--------------------------------------------------------------------------- // Enumerated types typedef enum { dtUnsigned16 = 0, dtSigned16 = 1, dtUnsigned32 = 2, dtSigned32 = 3, dtFloat32 = 4, dtString = 5 } EMBDataType; // Constants const char DataTypeName[][20] = { "Unsigned16", "Signed16", "Unsigned32", "Signed32", "Float32", "String" }; //--------------------------------------------------------------------------- // Structure prototypes typedef struct SDevice TDevice; typedef struct SDeviceParam TDeviceParam; // Devices with are polled struct SDevice { char * Name; bool Online; TDeviceParam * FirstParam; TDevice * Next; }; // Data parameters of devices struct SDeviceParam { char * Name; EMBDataType DataType; bool Scan; TChannel * EventChannel; long UpdateInterval; timeval UpdateTimeout; void * Value; int Len; bool Changed; void * SetValue; int SetLen; bool SetChanged; TDeviceParam * Next; }; //--------------------------------------------------------------------------- class CDeviceCore : public CFunctionCore { protected: // Update int UpdateInterval; timeval UpdateTimeout; // Parameters TDevice * FirstDevice; TDevice * ActiveDevice; // Standard channels TChannel * DeviceChannel; TChannel * CmdChannel; // Poll int PollStep; // Position in polling sequence timeval PollWait; // Time at which last poll was done long PollInterval; // Minimum delay between polls // Reply bool WaitingForReply; // Command sent, waiting for reply long ReplyTimeout; // Max waiting time for reply // Retry int PollRetry; // No of polling retries that has timed out int MaxRetries; // Max allowed retries // Manage Devices bool DestroyDevice( TDevice ** Device ); // Find Devices inline TDevice * GetDevice( const char * Name ) { TDevice * Device = FirstDevice; while (Device && strcasecmp( Device->Name, Name )) Device = Device->Next; return Device; } inline TDevice ** GetDevicePtr( const char * Name ) { TDevice ** Device = &FirstDevice; while (*Device && strcasecmp( (*Device)->Name, Name )) Device = &((*Device)->Next); return Device; } inline TDevice * GetNextOnlineDevice( TDevice * LastDevice ) { TDevice * Device = LastDevice; while (Device && !Device->Online) Device = Device->Next; return Device; } // Manage Params bool DestroyDeviceParam( TDeviceParam ** Param ); // Find Params inline TDeviceParam * GetDeviceParam( TDevice * Device, const char * Name ) { if (!Device) return NULL; TDeviceParam * Param = Device->FirstParam; while (Param && strcasecmp( Param->Name, Name )) Param = Param->Next; return Param; } inline TDeviceParam ** GetDeviceParamPtr( TDevice * Device, const char * Name ) { if (!Device) return NULL; TDeviceParam ** Param = &Device->FirstParam; while (*Param && strcasecmp( (*Param)->Name, Name )) Param = &((*Param)->Next); return Param; } inline TDeviceParam * GetNextScanParam( TDevice * Device, TDeviceParam * LastParam = NULL ) { if (!Device) return NULL; TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam; while (Param && (!Param->Scan)) Param = Param->Next; return Param; } inline TDeviceParam * GetNextChangedParam( TDevice * Device, TDeviceParam * LastParam = NULL ) { if (!Device) return NULL; TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam; while (Param && (!Param->Changed)) Param = Param->Next; return Param; } inline TDeviceParam * GetNextSetChangedParam( TDevice * Device, TDeviceParam * LastParam = NULL ) { if (!Device) return NULL; TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam; while (Param && (!Param->SetChanged)) Param = Param->Next; return Param; } // Tools bool CompareParamString( const char * ParamValue, const int ParamLen, const char * Value, const int Len ); // Generate events bool EventOutput( TDeviceParam * Param, bool Force ); bool TimedParamEvents(); // Manage device virtual bool DeviceOnline( TDevice * Device, bool Online ); // Handle Reply Timing virtual void SetWaitForReply(); virtual bool CheckReplyTimeout( int TimeoutPollStep ); public: // Life cycle CDeviceCore( const char * Name, CLogCore * pLog, EDebugLevel DebugLevel, int pOuputDisplay ); ~CDeviceCore(); // Generate events bool SetPollParam( int pPollInterval ); bool SetReplyParam( int pReplyTimeout, int pMaxRetries ); bool SetUpdate( int pUpdateInterval ); bool SetParamScan( TDeviceParam * Param, bool Scan, const char * ChannelName, long pUpdateInterval ); // Manage Devices TDevice * AddDevice( const char * DeviceName ); bool DestroyDevice( const char * DeviceName ); // Manage Params TDeviceParam * AddDeviceParam( TDevice * Device, const char * ParamName, EMBDataType DataType, int ParamLen = 1 ); bool DestroyDeviceParam( TDevice * Device, const char * ParamName ); // Update/Init Param values bool UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Value, bool Init ); bool UpdateSignedValue( TDeviceParam * Param, const int32_t Value, bool Init ); bool UpdateFloatValue( TDeviceParam * Param, const float Value, bool Init ); bool UpdateStringValue( TDeviceParam * Param, const char * Value, const int Len, bool Init ); // Change Param Update instruction bool SetUnsignedValue( TDeviceParam * Param, const u_int32_t Value, bool Force ); bool SetSignedValue( TDeviceParam * Param, const int32_t Value, bool Force ); bool SetFloatValue( TDeviceParam * Param, const float Value, bool Force ); bool SetStringValue( TDeviceParam * Param, const char * Value, const int Len, bool Force ); // Text Interfaces bool SetValue( TDeviceParam * Param, const char * Value, const int Len, bool Force ); bool GetValue( TDeviceParam * Param, char * Value, int &Len ); }; //--------------------------------------------------------------------------- #endif /* REDACORE_DEVICECORE_H_ */