From 2d9faa7fa65ffe3fcc794705c99d2ba43dc2ed51 Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Thu, 2 Mar 2017 12:31:50 +0200 Subject: [PATCH] Important Update: - Add GetValue/SetValue commands for Device command interface - Bug fix: use case compare on searches --- DeviceCore.cpp | 170 +++++++++++++++++++++++++++++++------------------ DeviceCore.h | 12 ++-- 2 files changed, 117 insertions(+), 65 deletions(-) diff --git a/DeviceCore.cpp b/DeviceCore.cpp index ec0ed07..b1f3bfc 100644 --- a/DeviceCore.cpp +++ b/DeviceCore.cpp @@ -520,67 +520,6 @@ bool CDeviceCore::UpdateStringValue( TDeviceParam * Param, const char * Value, c switch (Param->DataType) { -// case dtUnsigned16 : -// u_int16_t TestVal; -// if (Init || (*((u_int16_t*)Param->Value) != Value)) -// { -// // Set new value & mark change -// *((u_int16_t*)Param->Value) = Value; -// -// // Mark change & log event -// Changed = true; -// Log->Message( DebugLevel, dlLow, "%s: '%s' %s - %u", Name, Param->Name, ((Init)? "initialised" : "changed"), *((u_int16_t*)Param->Value) ); -// } -// break; -// -// case dtUnsigned32 : -// if (Init || (*((u_int32_t*)Param->Value) != Value)) -// { -// // Set new value & mark change -// *((u_int32_t*)Param->Value) = Value; -// -// // Mark change & log event -// Changed = true; -// Log->Message( DebugLevel, dlLow, "%s: '%s' %s - %u", Name, Param->Name, ((Init)? "initialised" : "changed"), *((u_int32_t*)Param->Value) ); -// } -// break; -// -// case dtSigned16 : -// if (Init || (*((int16_t*)Param->Value) != Value)) -// { -// // Set new value & mark change -// *((int16_t*)Param->Value) = Value; -// -// // Mark change & log event -// Changed = true; -// Log->Message( DebugLevel, dlLow, "%s: '%s' %s - %d", Name, Param->Name, ((Init)? "initialised" : "changed"), *((int16_t*)Param->Value) ); -// } -// break; -// -// case dtSigned32 : -// if (Init || (*((int32_t*)Param->Value) != Value)) -// { -// // Set new value & mark change -// *((int32_t*)Param->Value) = Value; -// -// // Mark change & log event -// Changed = true; -// Log->Message( DebugLevel, dlLow, "%s: '%s' %s - %d", Name, Param->Name, ((Init)? "initialised" : "changed"), *((int32_t*)Param->Value) ); -// } -// break; -// -// case dtFloat32 : // Special case, where float value is stored in Integer memory (e.g. modbus register) -// if (Init || (*((u_int32_t*)Param->Value) != Value)) -// { -// // Set new value & mark change -// *((u_int32_t*)Param->Value) = Value; -// -// // Mark change & log event -// Changed = true; -// Log->Message( DebugLevel, dlLow, "%s: '%s' %s - %f", Name, Param->Name, ((Init)? "initialised" : "changed"), *((float*)Param->Value) ); -// } -// break; - case dtString : // Update register if (Init || !CompareParamString( (char*)Param->Value, Param->Len, Value, Len)) @@ -750,6 +689,115 @@ bool CDeviceCore::SetStringValue( TDeviceParam * Param, const char * Value, cons } //--------------------------------------------------------------------------- +bool CDeviceCore::SetValue( TDeviceParam * Param, const char * Value, const int Len, bool Force ) +{ + u_int32_t UnsignedVal; + int32_t SignedVal; + float FloatVal; + + char * TempStr; + bool UseTempStr = false; + + // Validate + if (!Param || !Value || !Len) + return false; + + // Check if string + if (Param->DataType == dtString) + { + // Simply set string + SetStringValue( Param, Value, Len, Force ); + } + else + { + // Ensure string is zero terminated + if (Value[Len] != 0) { + TempStr = (char *)malloc( Len+1 ); + memcpy( TempStr, Value, Len ); + TempStr[Len] = 0; + UseTempStr = true; + } + + // Convert to correct type + switch (Param->DataType) + { + case dtUnsigned16 : + case dtUnsigned32 : + UnsignedVal = (u_int32_t)strtoul( ((UseTempStr)? TempStr : Value), NULL, 10 ); + SetUnsignedValue( Param, UnsignedVal, Force ); + break; + + case dtSigned16 : + case dtSigned32 : + SignedVal = (u_int32_t)strtol( ((UseTempStr)? TempStr : Value), NULL, 10 ); + SetUnsignedValue( Param, SignedVal, Force ); + break; + + case dtFloat32 : + FloatVal = (u_int32_t) strtof( ((UseTempStr)? TempStr : Value), NULL ); + SetUnsignedValue( Param, FloatVal, Force ); + break; + + default : + return false; + } + + // Clear memory + if (UseTempStr) { + free( TempStr ); + } + } + return true; +} +//--------------------------------------------------------------------------- + +bool CDeviceCore::GetValue( TDeviceParam * Param, char * Value, int &Len ) +{ + // Validate + if (!Param || !Value) + return false; + + // Check if return value longer than actual value + switch (Param->DataType) + { + case dtUnsigned16 : + sprintf( Value, "%u", (*((u_int16_t*)Param->Value)) ); + break; + + case dtUnsigned32 : + sprintf( Value, "%u", (*((u_int32_t*)Param->Value)) ); + break; + + case dtSigned16 : + sprintf( Value, "%d", (*((int16_t*)Param->Value)) ); + break; + + case dtSigned32 : + sprintf( Value, "%d", (*((int32_t*)Param->Value)) ); + break; + + case dtFloat32 : + sprintf( Value, "%f", (*((float*)Param->Value)) ); + break; + + case dtString : + if (Len < Param->Len) + { + // Only copy requested no of chars + memcpy( Value, Param->Value, Len ); + Value[ Len ] = 0; + } + else + { + memcpy( Value, Param->Value, Param->Len ); + Value[ Param->Len ] = 0; + } + break; + } + return true; +} +//--------------------------------------------------------------------------- + bool CDeviceCore::CompareParamString( const char * ParamValue, const int ParamLen, const char * Value, const int Len ) { bool Match = true; diff --git a/DeviceCore.h b/DeviceCore.h index cc37a95..ff50060 100644 --- a/DeviceCore.h +++ b/DeviceCore.h @@ -95,13 +95,13 @@ protected: // Find Devices inline TDevice * GetDevice( const char * Name ) { TDevice * Device = FirstDevice; - while (Device && strcmp( Device->Name, Name )) + while (Device && strcasecmp( Device->Name, Name )) Device = Device->Next; return Device; } inline TDevice ** GetDevicePtr( const char * Name ) { TDevice ** Device = &FirstDevice; - while (*Device && strcmp( (*Device)->Name, Name )) + while (*Device && strcasecmp( (*Device)->Name, Name )) Device = &((*Device)->Next); return Device; } @@ -119,14 +119,14 @@ protected: inline TDeviceParam * GetDeviceParam( TDevice * Device, const char * Name ) { if (!Device) return NULL; TDeviceParam * Param = Device->FirstParam; - while (Param && strcmp( Param->Name, Name )) + 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 && strcmp( (*Param)->Name, Name )) + while (*Param && strcasecmp( (*Param)->Name, Name )) Param = &((*Param)->Next); return Param; } @@ -196,6 +196,10 @@ public: 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 ); }; //---------------------------------------------------------------------------