Important Update:

- Bug fix JSONparseCore: Allow for blank path in file name
- Added new Class LiteProtocolCore for basic protocol
This commit is contained in:
Charl Wentzel
2017-04-30 17:32:19 +02:00
parent ee29625b9a
commit 9677052042
4 changed files with 602 additions and 2 deletions

View File

@@ -1,3 +1,3 @@
PROJECT(lib_redAcore) PROJECT(lib_redAcore)
ADD_LIBRARY(redAcore TimingCore.cpp DateTimeCore.cpp LogCore.cpp SignalCore.cpp DataTreeCore.cpp JSONparseCore.cpp BufferCore.cpp FunctionCore.cpp DeviceCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp) ADD_LIBRARY(redAcore TimingCore.cpp DateTimeCore.cpp LogCore.cpp SignalCore.cpp DataTreeCore.cpp JSONparseCore.cpp BufferCore.cpp LiteProtocolCore.cpp FunctionCore.cpp DeviceCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp)

View File

@@ -68,7 +68,8 @@ bool CJSONparse::WriteToFile( const char * BasePath, const char * Path, const ch
char FilePath[250] = ""; char FilePath[250] = "";
// Build file name // Build file name
strcpy( FilePath, Path ); if (Path)
strcpy( FilePath, Path );
strcat( FilePath, FileName ); strcat( FilePath, FileName );
// Read file // Read file

513
LiteProtocolCore.cpp Normal file
View File

@@ -0,0 +1,513 @@
/******************************************************
* Rapid Embedded Development Architecture (redA) *
* Copyright (C) 2008 RecoSys *
* -------------- *
* TLiteProtcol: *
* Simplified protocol object to allow for fast *
* communications between componets *
* *
* Contents: *
* CLiteProtocol - Object Functions & Procedures *
* *
******************************************************/
using namespace std;
// LAStronic Libraries
#include "LiteProtocolCore.h"
// Standard C/C++ Libraries
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <stdio.h>
//---------------------------------------------------------------------------
CLiteProtocol::CLiteProtocol( int pBufferInc, char pStartChar, char pEndChar, char pSeparator )
{
// Set Buffer increment
if (pBufferInc < 1)
BufferInc = 100;
else
BufferInc = pBufferInc;
// Set Protocol markers
StartChar = pStartChar;
EndChar = pEndChar;
Separator = pSeparator;
// Reset Buffer and protocol parameters
CommandStr = NULL;
BufferSize = 0;
ClearCommandStr();
}
//---------------------------------------------------------------------------
CLiteProtocol::~CLiteProtocol()
{
// Destroy string if exist
if (CommandStr)
free (CommandStr);
}
//---------------------------------------------------------------------------
bool CLiteProtocol::IncCommandStr( int pLength )
{
// Calculate new buffer length
pLength = BufferInc * (pLength/BufferInc + 1);
// Allocate memory
if (!CommandStr)
CommandStr = (char*)malloc(sizeof(char)*(pLength+1)); // Allow one more for zero termination
else if (BufferSize < pLength)
CommandStr = (char*)realloc(CommandStr, sizeof(char)*(pLength+1)); // Allow one more for zero termination
else
return true;
// Check if successful
if (!CommandStr) {
strcpy( Error, "Memory allocation error");
Verified = false;
return false;
}
// Update Buffer size and clear appended buffer
BufferSize = pLength;
memset( &CommandStr[Length], 0, pLength-Length+1 );
return true;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::UpdateCommandStr()
{
// Check if command sting exists
if (!CommandStr)
return false;
// Set Size & No of Params
sprintf( &CommandStr[1], "%06ld", Length );
CommandStr[7] = Separator;
sprintf( &CommandStr[8], "%04d", NoOfParams );
CommandStr[12] = Separator;
return true;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::VerifyStr( char * pString, int pLength, int &ParamCount, char * pError )
{
char * Mark;
long tmpLen;
int tmpParams;
long Pos;
long ParamLen;
// Validate buffer
if (!pString) {
strcpy( pError, "NULL Protocol string" );
return false;
}
// Validate buffer
if (!pLength) {
strcpy( pError, "Empty Protocol string" );
return false;
}
// Verify Start and end
if (pString[0] != StartChar) {
strcpy( pError, "Invalid Start character" );
return false;
}
// Verify Start and end
if (pString[pLength-1] != EndChar) {
strcpy( pError, "Invalid End character" );
return false;
}
// Verify Length
Mark = (char*)memchr( &pString[1], Separator, 7 );
if ((Mark && (Mark-pString-1 != 6)) || ((tmpLen = atol(&pString[1])) != pLength)) {
strcpy( pError, "Invalid Protocol length" );
return false;
}
// Verify no of parameters
Mark = (char*)memchr( &pString[8], Separator, 5 );
if ((Mark && (Mark-pString-8 != 4)) || ((tmpParams = atoi(&pString[8])) < 1)) {
strcpy( pError, "Invalid Parameter count" );
return false;
};
// Count parameters
Pos = 13;
ParamCount = 0;
while ((pString[Pos] != EndChar) && (Pos < tmpLen-1))
{
// Get param length & move to next param
if (!(Mark = (char*)memchr( &pString[Pos], Separator, tmpLen-Pos )))
break;
ParamLen = Mark-pString-Pos;
Pos = Pos + ParamLen + 1;
ParamCount++;
};
// Check if match
if (ParamCount != tmpParams){
strcpy( pError, "Invalid No of Parameters" );
return false;
};
return true;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::ClearCommandStr()
{
// Set to empty buffers
if (CommandStr){
free( CommandStr );
CommandStr = NULL;
BufferSize = 0;
};
// Reset Parameters
Length = 0;
NoOfParams = 0;
// No string to verify
Verified = false;
strcpy( Error, "Empty String" );
// Set references to zero
CurrPos = 0;
CurrParam = 0;
return true;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::NewCommandStr()
{
// First clear existing command string
if (CommandStr){
free( CommandStr );
CommandStr = NULL;
BufferSize = 0;
};
IncCommandStr( 14 );
// Build basic string
NoOfParams = 0;
Length = 14;
CommandStr[0] = StartChar;
CommandStr[13] = EndChar;
UpdateCommandStr();
// Not verified
Verified = false;
strcpy( Error, "No Parameters" );
// Set references
CurrParam = 1;
CurrPos = 13;
return true;
}
//---------------------------------------------------------------------------
char* CLiteProtocol::GetCommandStr( char ** pString, int * pLength )
{
// Set return values
if (pString)
*pString = CommandStr;
if (pLength)
*pLength = Length;
return CommandStr;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::SetCommandStr( char * pString, int pLength, char * Error )
{
int ParamCount;
// Verify new string
if (!VerifyStr( pString, pLength, ParamCount, Error ))
return false;
// Allocate memory
ClearCommandStr();
if (!IncCommandStr( pLength ))
return false;
// Copy string
memcpy( CommandStr, pString, pLength );
Length = pLength;
NoOfParams = ParamCount;
// Verified
Verified = true;
Error[0] = 0;
// Set all pointers to start position
GotoParam( 1 );
return true;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::AppendCommandStr( char * pString, int &pLength )
{
long Pos = 0;
long Start = pLength;
long End = pLength;
long newLen = 0;
// Check if valid parameters
if (!pString || (pLength < 1))
return false;
// Clear if last string is already verified
if (Verified)
ClearCommandStr();
// if partial string already received mark start position
if (Length != 0)
Start = 0;
// Look for other markers
while (Pos < pLength)
{
// Check if start character
if (pString[Pos] == StartChar)
{
// Mark start point
Start = Pos;
}
// Check if end character with valid start
else if ((pString[Pos] == EndChar) && (Start < pLength))
{
// Mark end and continue
End = Pos+1;
break;
}
// next char
Pos++;
};
// Check if anything available
if (Start < pLength)
{
// Calculate length
newLen = End - Start;
// Increase buffer size
IncCommandStr( Length + newLen ); // negative value -> add length
// Copy buffer section
memcpy( &CommandStr[Length], &pString[Start], newLen );
Length = Length + newLen;
CommandStr[Length] = 0;
// Check if complete string
if ((pString[End-1] == EndChar))
VerifyCommandStr();
};
// Shift buffer
if (End < pLength)
memmove( pString, &pString[End], pLength-End );
memset( &pString[pLength-End], 0, End );
pLength = pLength - End;
return Verified;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::VerifyCommandStr()
{
// Check command string
if (VerifyStr( CommandStr, Length, NoOfParams, Error )) {
Verified = true;
Error[0] = 0;
}
// Move reference to first parameter
GotoParam( 1 );
return Verified;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::AppendParam( const char * pString, const int pLength )
{
// Check if valid
if (!pString)
return false;
// Increase string length if required
IncCommandStr( Length + pLength+1 );
// Copy string
memcpy( &CommandStr[CurrPos], (char*)pString, pLength );
CommandStr[CurrPos+pLength] = Separator;
Length = Length + pLength + 1; // Allow for separator character
// Move pointer to end and set end char
CurrParam++;
CurrPos = CurrPos + pLength + 1; // Allow for separator character
CommandStr[CurrPos] = EndChar;
// Update references
NoOfParams++;
UpdateCommandStr();
// String now verified - at least one parameter
Verified = true;
Error[0] = 0;
return true;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::AppendParam( long pValue )
{
char TempStr[10];
// Create string representation
sprintf( TempStr, "%ld", pValue );
// Insert
if (AppendParam( TempStr, strlen(TempStr) ))
return true;
else
return false;
}
//---------------------------------------------------------------------------
bool CLiteProtocol::AppendParam( double pValue, int pDigits )
{
char ConvStr[10];
char TempStr[20];
// Check if command sting exists
if (!CommandStr)
return false;
// Create string representation
if (pDigits)
sprintf( ConvStr, "%%.%df", pDigits );
else
strcpy( ConvStr, "%f" );
sprintf( TempStr, ConvStr, pValue );
// Insert
if (AppendParam( TempStr, strlen(TempStr) ))
return true;
else
return false;
}
//---------------------------------------------------------------------------
char const * CLiteProtocol::GetParamNo( int ParamNo, char ** pString, int * pLength )
{
char * Mark = NULL;
// Check if valid parameter no
if (!CommandStr || !CommandStr[0] || !Length || (ParamNo < 1) || (ParamNo > NoOfParams))
return NULL;
// Set Starting point
if (ParamNo < CurrParam)
GotoParam( 1 );
// Jump until parameter found
while ((CurrParam != ParamNo) && NextParam()) {};
// Check if found
if (CurrParam != ParamNo)
return NULL;
// Return reference to parameter
if (pString)
*pString = &CommandStr[CurrPos];
if (pLength) {
if (!(Mark = (char*)memchr( &CommandStr[CurrPos], Separator, Length-CurrPos )))
*pLength = 0;
else
*pLength = Mark - &CommandStr[CurrPos];
}
return (&CommandStr[CurrPos]);
}
//---------------------------------------------------------------------------
bool CLiteProtocol::GotoParam( int ParamNo )
{
// Check if command sting exists
if (!CommandStr || (ParamNo < 1))
return false;
// Goto Param one
CurrPos = 13;
CurrParam = 1;
// Find Parameter
while ((CurrParam < ParamNo) && NextParam()) {};
return ((ParamNo == CurrParam)? true : false);
}
//---------------------------------------------------------------------------
bool CLiteProtocol::NextParam()
{
int ParamLen;
char * Mark = NULL;
// Check if command sting exists
if (!CommandStr || !CommandStr[0] || !Length)
return false;
// Check if next param exists
if (CurrParam == NoOfParams)
return false;
// Get Current parameter length
if (!(Mark = (char*)memchr( &CommandStr[CurrPos], Separator, Length-CurrPos )))
return false;
ParamLen = Mark - &CommandStr[CurrPos];
// Move reference
CurrPos = CurrPos + ParamLen + 1;
CurrParam++;
return true;
}
//---------------------------------------------------------------------------
char const * CLiteProtocol::GetParam( char ** pString, int * pLength )
{
char * Mark = NULL;
// Check if valid parameter no
if (!CommandStr || !CommandStr[0] || !Length)
{
if (pString) *pString = NULL;
if (pLength) pLength = 0;
return NULL;
};
// Return reference to parameter
if (pString)
*pString = &CommandStr[CurrPos];
if (pLength) {
if (!(Mark = (char*)memchr( &CommandStr[CurrPos], Separator, Length-CurrPos )))
*pLength = 0;
else
*pLength = Mark - &CommandStr[CurrPos];
}
return (&CommandStr[CurrPos]);
}
//---------------------------------------------------------------------------

86
LiteProtocolCore.h Normal file
View File

@@ -0,0 +1,86 @@
/******************************************************
* Rapid Embedded Development Architecture (redA) *
* Copyright (C) 2008 RecoSys *
* -------------- *
* TLiteProtcol: *
* Simplified protocol object to allow for fast *
* communications between componets *
* *
* Contents: *
* CLiteProtocol - Core of simple protocol *
* *
******************************************************/
#ifndef LITE_PROTOCOL_CORE
#define LITE_PROTOCOL_CORE
using namespace std;
// redA Libraries
/* none */
// Standard C/C++ Libraries
#include <unistd.h>
//---------------------------------------------------------------------------
// Very Simple Lightweight Protcol
class CLiteProtocol
{
protected:
// Parameters
int BufferInc;
char StartChar;
char EndChar;
char Separator;
char *CommandStr;
int BufferSize;
long Length;
int NoOfParams;
bool Verified;
char Error[100];
int CurrPos;
int CurrParam;
// Internal tools
bool IncCommandStr( int pLength );
bool UpdateCommandStr();
bool VerifyStr( char * pString, int pLength, int &ParamCount, char * pError );
public:
// Constructor & Destructor
CLiteProtocol( int pBufferInc, char pStartChar = 0x02, char pEndChar = 0x03, char pSeparator = 0x00 );
~CLiteProtocol();
// Set Command string
bool ClearCommandStr();
bool NewCommandStr();
bool SetCommandStr( char * pString, int pLength, char * Error );
bool AppendCommandStr( char *pString, int &pLength );
bool VerifyCommandStr();
// Get Command String
char* GetCommandStr( char ** pString = NULL, int * pLength = NULL );
int GetCommandLen() { return Length; };
int GetNoOfParams() { return NoOfParams; };
bool isVerified() { return Verified; };
char* GetError() { return Error; };
// Set Parameters
bool AppendParam( const char * pString, const int pLength );
bool AppendParam( long pValue );
bool AppendParam( double pValue, int pDigits );
// Get Parameters
char const * GetParamNo( int ParamNo, char ** pString = NULL, int * pLength = NULL );
bool GotoParam( int ParamNo );
bool NextParam();
char const * GetParam( char ** pString = NULL, int * pLength = NULL );
};
#endif /* LITE_PROTOCOL_CORE */