/****************************************************** * 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 //--------------------------------------------------------------------------- // 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( const 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(); // Set command string to empty bool NewCommandStr(); // Create new empty command string with headers (length, param count) bool SetCommandStr( const char * pString, int pLength ); // Set complete command string (as received) bool AppendCommandStr( char *pString, int &pLength ); // Build up command string in chunks as it is received bool VerifyCommandStr(); // Validate received command string // Get Command String char* GetCommandStr( char ** pString = NULL, int * pLength = NULL ); // Return command string int GetCommandLen() { return Length; }; // Return length of command string int GetNoOfParams() { return NoOfParams; }; // Returns total number of parameters bool isVerified() { return Verified; }; // Returns whether command has been validated char* GetError() { return Error; }; // Return reason for not being valid // Set Parameters bool AppendParam( const char * pString, const int pLength = -1 ); // Add string param to command string bool AppendParam( const long pValue ); // Add long param as text to command string bool AppendParam( const double pValue, const int pDigits ); // Add real param as text to command string // Get Parameters bool GotoParam( int ParamNo ); // Reset pointer to specific parameter bool NextParam(); // Move pointer to next parameter char const * GetParam( char ** pString = NULL, int * pLength = NULL ); // Read parameter at pointer (always text) char const * GetParamNo( int ParamNo, char ** pString = NULL, int * pLength = NULL ); // Get total no of parameters in command string // Create & Read Structured Command string bool CreateCommand( char const * From, char const * To, char const * Command ); // Create new command string with core params: To, From & Command bool ReadCommand( char ** From, char ** To, char ** Command, int * DataParams, int * FromLen = NULL, int * ToLen = NULL, int * CommandLen = NULL ); // Read core parameters from structured command: To, From & Command // Read structured Command string inline int GetNoOfDataParams() { // Return number for data (additional) params return (NoOfParams - 3); }; inline bool GotoDataParam( int ParamNo ) { // Goto specific data (additional) param return (GotoParam( ParamNo+3 )); }; inline char const * GetDataParamNo( int ParamNo, char ** pString = NULL, int * pLength = NULL ) { // Read specific data param (by number) return (GetParamNo( ParamNo+3, pString, pLength )); }; }; #endif /* LITE_PROTOCOL_CORE */