/* * FunctionCore.h * * Created on: 18 May 2016 * Author: wentzelc */ #ifndef REDACORE_DEVICECORE_H_ #define REDACORE_DEVICECORE_H_ // Standard C/C++ Libraries /* none */ // redA Libraries #include "FunctionCore.h" //--------------------------------------------------------------------------- // Enumerated types typedef enum { dtNone = 0, dtUnsigned16 = 1, dtSigned16 = 2, dtUnsigned32 = 3, dtSigned32 = 4, dtFloat32 = 5, dtString = 6 } EMBDataType; // Constants const char DataTypeCount = 6; 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; }; //--------------------------------------------------------------------------- // Function Constructor List #define TYPE_DEVICE "Device" //CFunctionCore * NewDeviceCore( const char * Name ); //--------------------------------------------------------------------------- class CDeviceCore : public CFunctionCore { protected: // Configuration CDataMember * Config; // 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 inline EMBDataType GetDataType( const char * TypeName ) { int Type; for (Type = 0; Type < DataTypeCount; Type++) if (!strcasecmp( TypeName, DataTypeName[Type])) break; return (Type == DataTypeCount)? dtNone : (EMBDataType)Type; } 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 * pName, const char * pType = TYPE_DEVICE ); virtual ~CDeviceCore(); // Configuration bool Init( CDataMember * FunctionConfig ); // 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 ); bool UpdateStringValue( TDeviceParam * Param, const char * Value, bool Init ) { return UpdateStringValue( Param, Value, strlen(Value), 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 ); bool SetStringValue( TDeviceParam * Param, const char * Value, bool Force ) { return SetStringValue( Param, Value, strlen(Value), 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_ */