Today's the day. Renamed platform to framework.
This commit is contained in:
136
datapool/DataPool.cpp
Normal file
136
datapool/DataPool.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* DataPool.cpp
|
||||
*
|
||||
* Created on: 17.10.2012
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
|
||||
#include <framework/datapool/DataPool.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
DataPool::DataPool( void ( *initFunction )( std::map<uint32_t, PoolEntryIF*>* pool_map ) ) {
|
||||
this->mutex = new MutexId_t;
|
||||
OSAL::createMutex( OSAL::buildName('M','T','X','0'), ( this->mutex ) );
|
||||
if (initFunction != NULL ) {
|
||||
initFunction( &this->data_pool );
|
||||
}
|
||||
}
|
||||
|
||||
DataPool::~DataPool() {
|
||||
OSAL::deleteMutex( this->mutex );
|
||||
delete this->mutex;
|
||||
for ( std::map<uint32_t, PoolEntryIF*>::iterator it = this->data_pool.begin(); it != this->data_pool.end(); ++it ) {
|
||||
delete it->second;
|
||||
}
|
||||
}
|
||||
|
||||
//The function checks PID, type and array length before returning a copy of the PoolEntry. In failure case, it returns a temp-Entry with size 0 and NULL-ptr.
|
||||
template <typename T> PoolEntry<T>* DataPool::getData( uint32_t data_pool_id, uint8_t sizeOrPosition ) {
|
||||
std::map<uint32_t, PoolEntryIF*>::iterator it = this->data_pool.find( data_pool_id );
|
||||
if ( it != this->data_pool.end() ) {
|
||||
PoolEntry<T>* entry = dynamic_cast< PoolEntry<T>* >( it->second );
|
||||
if (entry != NULL ) {
|
||||
if ( sizeOrPosition <= entry->length ) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PoolEntryIF* DataPool::getRawData( uint32_t data_pool_id ) {
|
||||
std::map<uint32_t, PoolEntryIF*>::iterator it = this->data_pool.find( data_pool_id );
|
||||
if ( it != this->data_pool.end() ) {
|
||||
return it->second;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
//uint8_t DataPool::getRawData( uint32_t data_pool_id, uint8_t* address, uint16_t* size, uint32_t max_size ) {
|
||||
// std::map<uint32_t, PoolEntryIF*>::iterator it = this->data_pool.find( data_pool_id );
|
||||
// if ( it != this->data_pool.end() ) {
|
||||
// if ( it->second->getByteSize() <= max_size ) {
|
||||
// *size = it->second->getByteSize();
|
||||
// memcpy( address, it->second->getRawData(), *size );
|
||||
// return DP_SUCCESSFUL;
|
||||
// }
|
||||
// }
|
||||
// *size = 0;
|
||||
// return DP_FAILURE;
|
||||
//}
|
||||
|
||||
ReturnValue_t DataPool::freeDataPoolLock() {
|
||||
ReturnValue_t status = OSAL::unlockMutex( this->mutex );
|
||||
if ( status != RETURN_OK ) {
|
||||
error << "DataPool::DataPool: unlock of mutex failed with error code: " << status << std::endl;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
ReturnValue_t DataPool::lockDataPool() {
|
||||
ReturnValue_t status = OSAL::lockMutex( this->mutex, OSAL::NO_TIMEOUT );
|
||||
if ( status != RETURN_OK ) {
|
||||
error << "DataPool::DataPool: lock of mutex failed with error code: " << status << std::endl;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
void DataPool::print() {
|
||||
debug << "DataPool contains: " << std::endl;
|
||||
std::map<uint32_t, PoolEntryIF*>::iterator dataPoolIt;
|
||||
dataPoolIt = this->data_pool.begin();
|
||||
while( dataPoolIt != this->data_pool.end() ) {
|
||||
debug << std::hex << dataPoolIt->first << std::dec << " |";
|
||||
dataPoolIt->second->print();
|
||||
dataPoolIt++;
|
||||
}
|
||||
}
|
||||
|
||||
template PoolEntry<uint8_t>* DataPool::getData<uint8_t>( uint32_t data_pool_id, uint8_t size );
|
||||
template PoolEntry<uint16_t>* DataPool::getData<uint16_t>( uint32_t data_pool_id, uint8_t size );
|
||||
template PoolEntry<uint32_t>* DataPool::getData<uint32_t>( uint32_t data_pool_id, uint8_t size );
|
||||
template PoolEntry<uint64_t>* DataPool::getData<uint64_t>(uint32_t data_pool_id,
|
||||
uint8_t size);
|
||||
template PoolEntry<int8_t>* DataPool::getData<int8_t>( uint32_t data_pool_id, uint8_t size );
|
||||
template PoolEntry<int16_t>* DataPool::getData<int16_t>( uint32_t data_pool_id, uint8_t size );
|
||||
template PoolEntry<int32_t>* DataPool::getData<int32_t>( uint32_t data_pool_id, uint8_t size );
|
||||
template PoolEntry<float>* DataPool::getData<float>( uint32_t data_pool_id, uint8_t size );
|
||||
template PoolEntry<double>* DataPool::getData<double>(uint32_t data_pool_id,
|
||||
uint8_t size);
|
||||
|
||||
|
||||
uint32_t DataPool::PIDToDataPoolId(uint32_t parameter_id) {
|
||||
return (parameter_id >> 8) & 0x00FFFFFF;
|
||||
}
|
||||
|
||||
uint8_t DataPool::PIDToArrayIndex(uint32_t parameter_id) {
|
||||
return (parameter_id & 0x000000FF);
|
||||
}
|
||||
|
||||
uint32_t DataPool::poolIdAndPositionToPid(uint32_t poolId, uint8_t index) {
|
||||
return (poolId << 8) + index;
|
||||
}
|
||||
|
||||
//TODO: This is not 100% clean. Should return returnValue and type by passing...
|
||||
//TODO: Do we need a mutex lock here... I don't think so, as we only check static const values of elements in a list that do not change.
|
||||
Type DataPool::getType(uint32_t parameter_id) {
|
||||
std::map<uint32_t, PoolEntryIF*>::iterator it = this->data_pool.find( PIDToDataPoolId(parameter_id));
|
||||
if ( it != this->data_pool.end() ) {
|
||||
return it->second->getType();
|
||||
} else {
|
||||
return Type::UNKNOWN_TYPE;
|
||||
}
|
||||
}
|
||||
|
||||
bool DataPool::exists(uint32_t parameterId) {
|
||||
uint32_t poolId = PIDToDataPoolId(parameterId);
|
||||
uint32_t index = PIDToArrayIndex(parameterId);
|
||||
std::map<uint32_t, PoolEntryIF*>::iterator it = this->data_pool.find( poolId );
|
||||
if (it != data_pool.end()) {
|
||||
if (it->second->getSize() >= index) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
129
datapool/DataPool.h
Normal file
129
datapool/DataPool.h
Normal file
@ -0,0 +1,129 @@
|
||||
/**
|
||||
* \file DataPool.h
|
||||
*
|
||||
* \date 10/17/2012
|
||||
* \author Bastian Baetz
|
||||
*
|
||||
* \brief This file contains the definition of the DataPool class and (temporarily)
|
||||
* the "extern" definition of the global dataPool instance.
|
||||
*/
|
||||
|
||||
#ifndef DATAPOOL_H_
|
||||
#define DATAPOOL_H_
|
||||
|
||||
#include <framework/datapool/PoolEntry.h>
|
||||
#include <framework/globalfunctions/Type.h>
|
||||
#include <framework/osal/OSAL.h>
|
||||
#include <map>
|
||||
|
||||
/**
|
||||
* \defgroup data_pool Data Pool
|
||||
* This is the group, where all classes associated with Data Pool Handling belong to.
|
||||
* This includes classes to access Data Pool variables.
|
||||
*/
|
||||
|
||||
#define DP_SUCCESSFUL 0
|
||||
#define DP_FAILURE 1
|
||||
|
||||
/**
|
||||
* \brief This class represents the OBSW global data-pool.
|
||||
*
|
||||
* \details All variables are registered and space is allocated in an initialization
|
||||
* function, which is passed do the constructor.
|
||||
* Space for the variables is allocated on the heap (with a new call).
|
||||
* The data is found by a data pool id, which uniquely represents a variable.
|
||||
* Data pool variables should be used with a blackboard logic in mind,
|
||||
* which means read data is valid (if flagged so), but not necessarily up-to-date.
|
||||
* Variables are either single values or arrays.
|
||||
* \ingroup data_pool
|
||||
*/
|
||||
class DataPool : public HasReturnvaluesIF {
|
||||
private:
|
||||
/**
|
||||
* \brief This is the actual data pool itself.
|
||||
* \details It is represented by a map
|
||||
* with the data pool id as index and a pointer to a single PoolEntry as value.
|
||||
*/
|
||||
std::map<uint32_t, PoolEntryIF*> data_pool;
|
||||
public:
|
||||
/**
|
||||
* \brief The mutex is created in the constructor and makes access mutual exclusive.
|
||||
* \details Locking and unlocking the pool is only done by the DataSet class.
|
||||
*/
|
||||
MutexId_t* mutex;
|
||||
/**
|
||||
* \brief In the classes constructor, the passed initialization function is called.
|
||||
* \details To enable filling the pool,
|
||||
* a pointer to the map is passed, allowing direct access to the pool's content.
|
||||
* On runtime, adding or removing variables is forbidden.
|
||||
*/
|
||||
DataPool( void ( *initFunction )( std::map<uint32_t, PoolEntryIF*>* pool_map ) );
|
||||
/**
|
||||
* \brief The destructor iterates through the data_pool map and calls all Entries destructors to clean up the heap.
|
||||
*/
|
||||
~DataPool();
|
||||
/**
|
||||
* \brief This is the default call to access the pool.
|
||||
* \details A pointer to the PoolEntry object is returned.
|
||||
* The call checks data pool id, type and array size. Returns NULL in case of failure.
|
||||
* \param data_pool_id The data pool id to search.
|
||||
* \param sizeOrPosition The array size (not byte size!) of the pool entry, or the position the user wants to read.
|
||||
* If smaller than the entry size, everything's ok.
|
||||
*/
|
||||
template <typename T> PoolEntry<T>* getData( uint32_t data_pool_id, uint8_t sizeOrPosition );
|
||||
/**
|
||||
* \brief An alternative call to get a data pool entry in case the type is not implicitly known
|
||||
* (i.e. in Housekeeping Telemetry).
|
||||
* \details It returns a basic interface and does NOT perform
|
||||
* a size check. The caller has to assure he does not copy too much data.
|
||||
* Returns NULL in case the entry is not found.
|
||||
* \param data_pool_id The data pool id to search.
|
||||
*/
|
||||
PoolEntryIF* getRawData( uint32_t data_pool_id );
|
||||
/**
|
||||
* \brief This is a small helper function to facilitate locking the global data pool.
|
||||
* \details It fetches the pool's mutex id and tries to acquire the mutex.
|
||||
*/
|
||||
ReturnValue_t lockDataPool();
|
||||
/**
|
||||
* \brief This is a small helper function to facilitate unlocking the global data pool.
|
||||
* \details It fetches the pool's mutex id and tries to free the mutex.
|
||||
*/
|
||||
ReturnValue_t freeDataPoolLock();
|
||||
/**
|
||||
* \brief The print call is a simple debug method.
|
||||
* \details It prints the current content of the data pool.
|
||||
* It iterates through the data_pool map and calls each entry's print() method.
|
||||
*/
|
||||
void print();
|
||||
/**
|
||||
* Extracts the data pool id from a SCOS 2000 PID.
|
||||
* @param parameter_id The passed Parameter ID.
|
||||
* @return The data pool id as used within the OBSW.
|
||||
*/
|
||||
static uint32_t PIDToDataPoolId( uint32_t parameter_id );
|
||||
/**
|
||||
* Extracts an array index out of a SCOS 2000 PID.
|
||||
* @param parameter_id The passed Parameter ID.
|
||||
* @return The index of the corresponding data pool entry.
|
||||
*/
|
||||
static uint8_t PIDToArrayIndex( uint32_t parameter_id );
|
||||
/**
|
||||
* Retransforms a data pool id and an array index to a SCOS 2000 PID.
|
||||
*/
|
||||
static uint32_t poolIdAndPositionToPid( uint32_t poolId, uint8_t index );
|
||||
|
||||
Type getType( uint32_t parameter_id );
|
||||
/**
|
||||
* Method to check if a PID exists.
|
||||
* Does not lock, as there's no possibility to alter the list that is checked during run-time.
|
||||
* @param parameterId The PID (not pool id!) of a parameter.
|
||||
* @return true if exists, false else.
|
||||
*/
|
||||
bool exists(uint32_t parameterId);
|
||||
};
|
||||
|
||||
//TODO: Remove this, if data pool is get by Satellite Manager
|
||||
//Better make clean Singleton.
|
||||
extern DataPool dataPool;
|
||||
#endif /* DATAPOOL_H_ */
|
112
datapool/DataPoolAdmin.cpp
Normal file
112
datapool/DataPoolAdmin.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* DataPoolAdmin.cpp
|
||||
*
|
||||
* Created on: 05.12.2013
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <framework/datapool/DataPool.h>
|
||||
#include <framework/datapool/DataPoolAdmin.h>
|
||||
#include <framework/datapool/DataSet.h>
|
||||
#include <framework/datapool/PoolRawAccess.h>
|
||||
#include <framework/ipc/CommandMessage.h>
|
||||
|
||||
|
||||
DataPoolAdmin::DataPoolAdmin(object_id_t objectId ) : SystemObject(objectId), commandQueue(), memoryHelper(this, &commandQueue){
|
||||
}
|
||||
|
||||
ReturnValue_t DataPoolAdmin::performOperation() {
|
||||
handleCommand();
|
||||
return RETURN_OK;
|
||||
}
|
||||
|
||||
MessageQueueId_t DataPoolAdmin::getCommandQueue() const {
|
||||
return commandQueue.getId();
|
||||
}
|
||||
|
||||
void DataPoolAdmin::handleCommand() {
|
||||
CommandMessage command;
|
||||
ReturnValue_t result = commandQueue.receiveMessage(&command);
|
||||
if (result != RETURN_OK) {
|
||||
return;
|
||||
}
|
||||
result = memoryHelper.handleMemoryCommand(&command);
|
||||
if (result != RETURN_OK) {
|
||||
command.setToUnknownCommand(command.getCommand());
|
||||
commandQueue.reply( &command );
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t DataPoolAdmin::handleMemoryLoad(uint32_t address,
|
||||
const uint8_t* data, uint32_t size, uint8_t** dataPointer) {
|
||||
uint32_t poolId = ::dataPool.PIDToDataPoolId( address );
|
||||
uint8_t arrayIndex = ::dataPool.PIDToArrayIndex( address );
|
||||
DataSet testSet;
|
||||
PoolRawAccess varToGetSize( poolId, arrayIndex, &testSet, PoolVariableIF::VAR_READ );
|
||||
ReturnValue_t status = testSet.read();
|
||||
if (status != RETURN_OK) {
|
||||
return INVALID_ADDRESS;
|
||||
}
|
||||
uint8_t typeSize = varToGetSize.getSizeOfType();
|
||||
if ( size > varToGetSize.getSizeTillEnd() ) {
|
||||
return INVALID_SIZE;
|
||||
}
|
||||
const uint8_t* readPosition = data;
|
||||
for ( ; size > 0; size -= typeSize ) {
|
||||
DataSet rawSet;
|
||||
PoolRawAccess variable( poolId, arrayIndex, &rawSet, PoolVariableIF::VAR_READ_WRITE );
|
||||
status = rawSet.read();
|
||||
if (status == RETURN_OK) {
|
||||
status = variable.setEntryFromBigEndian( readPosition, typeSize );
|
||||
if (status == RETURN_OK) {
|
||||
status = rawSet.commit(PoolVariableIF::VALID);
|
||||
}
|
||||
}
|
||||
arrayIndex += 1;
|
||||
readPosition += typeSize;
|
||||
}
|
||||
return ACTIVITY_COMPLETED;
|
||||
}
|
||||
|
||||
ReturnValue_t DataPoolAdmin::handleMemoryDump(uint32_t address, uint32_t size,
|
||||
uint8_t** dataPointer, uint8_t* copyHere) {
|
||||
uint32_t poolId = ::dataPool.PIDToDataPoolId( address );
|
||||
uint8_t arrayIndex = ::dataPool.PIDToArrayIndex( address );
|
||||
DataSet testSet;
|
||||
PoolRawAccess varToGetSize( poolId, arrayIndex, &testSet, PoolVariableIF::VAR_READ );
|
||||
ReturnValue_t status = testSet.read();
|
||||
if (status != RETURN_OK) {
|
||||
return INVALID_ADDRESS;
|
||||
}
|
||||
uint8_t typeSize = varToGetSize.getSizeOfType();
|
||||
if ( size > varToGetSize.getSizeTillEnd() ) {
|
||||
return INVALID_SIZE;
|
||||
}
|
||||
uint8_t* ptrToCopy = copyHere;
|
||||
for ( ; size > 0; size -= typeSize ) {
|
||||
DataSet rawSet;
|
||||
PoolRawAccess variable( poolId, arrayIndex, &rawSet, PoolVariableIF::VAR_READ );
|
||||
status = rawSet.read();
|
||||
if (status == RETURN_OK) {
|
||||
uint32_t temp = 0;
|
||||
status = variable.getEntryEndianSafe( ptrToCopy, &temp, size);
|
||||
if ( status != RETURN_OK ) {
|
||||
return RETURN_FAILED;
|
||||
}
|
||||
} else {
|
||||
//Error reading parameter.
|
||||
}
|
||||
arrayIndex += 1;
|
||||
ptrToCopy += typeSize;
|
||||
}
|
||||
return ACTIVITY_COMPLETED;
|
||||
}
|
||||
|
||||
ReturnValue_t DataPoolAdmin::initialize() {
|
||||
if (memoryHelper.initialize() == RETURN_OK) {
|
||||
return SystemObject::initialize();
|
||||
}
|
||||
return RETURN_FAILED;
|
||||
}
|
35
datapool/DataPoolAdmin.h
Normal file
35
datapool/DataPoolAdmin.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* DataPoolAdmin.h
|
||||
*
|
||||
* Created on: 05.12.2013
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef DATAPOOLADMIN_H_
|
||||
#define DATAPOOLADMIN_H_
|
||||
|
||||
#include <framework/ipc/MessageQueue.h>
|
||||
#include <framework/memory/MemoryHelper.h>
|
||||
#include <framework/objectmanager/SystemObject.h>
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
#include <framework/tasks/ExecutableObjectIF.h>
|
||||
|
||||
class DataPoolAdmin : public ExecutableObjectIF, public AcceptsMemoryMessagesIF, public HasReturnvaluesIF, public SystemObject {
|
||||
private:
|
||||
MessageQueue commandQueue;
|
||||
MemoryHelper memoryHelper;
|
||||
void handleCommand();
|
||||
public:
|
||||
DataPoolAdmin(object_id_t objectId);
|
||||
|
||||
ReturnValue_t performOperation();
|
||||
|
||||
MessageQueueId_t getCommandQueue() const;
|
||||
|
||||
ReturnValue_t handleMemoryLoad(uint32_t address, const uint8_t* data, uint32_t size, uint8_t** dataPointer);
|
||||
ReturnValue_t handleMemoryDump(uint32_t address, uint32_t size, uint8_t** dataPointer, uint8_t* copyHere );
|
||||
ReturnValue_t initialize();
|
||||
};
|
||||
|
||||
|
||||
#endif /* DATAPOOLADMIN_H_ */
|
153
datapool/DataSet.cpp
Normal file
153
datapool/DataSet.cpp
Normal file
@ -0,0 +1,153 @@
|
||||
/*
|
||||
* DataSet.cpp
|
||||
*
|
||||
* Created on: 24.10.2012
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#include <framework/datapool/DataSet.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
DataSet::DataSet() :
|
||||
fill_count(0), state(DATA_SET_UNINITIALISED) {
|
||||
for (unsigned count = 0; count < DATA_SET_MAX_SIZE; count++) {
|
||||
registeredVariables[count] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
DataSet::~DataSet() {
|
||||
//Don't do anything with your variables, they are dead already! (Destructor is already called)
|
||||
}
|
||||
|
||||
ReturnValue_t DataSet::read() {
|
||||
ReturnValue_t result = RETURN_OK;
|
||||
if (state == DATA_SET_UNINITIALISED) {
|
||||
lockDataPool();
|
||||
for (uint16_t count = 0; count < fill_count; count++) {
|
||||
if (registeredVariables[count]->getReadWriteMode()
|
||||
!= PoolVariableIF::VAR_WRITE
|
||||
&& registeredVariables[count]->getDataPoolId()
|
||||
!= PoolVariableIF::NO_PARAMETER) {
|
||||
ReturnValue_t status = registeredVariables[count]->read();
|
||||
if (status != RETURN_OK) {
|
||||
result = INVALID_PARAMETER_DEFINITION;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
state = DATA_SET_WAS_READ;
|
||||
freeDataPoolLock();
|
||||
} else {
|
||||
error << "DataSet::read(): Call made in wrong position." << std::endl;
|
||||
result = SET_WAS_ALREADY_READ;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ReturnValue_t DataSet::commit(uint8_t valid) {
|
||||
for (uint16_t count = 0; count < fill_count; count++) {
|
||||
if (registeredVariables[count]->getReadWriteMode()
|
||||
!= PoolVariableIF::VAR_READ) {
|
||||
registeredVariables[count]->setValid(valid);
|
||||
}
|
||||
}
|
||||
return commit();
|
||||
}
|
||||
|
||||
ReturnValue_t DataSet::commit() {
|
||||
if (state == DATA_SET_WAS_READ) {
|
||||
lockDataPool();
|
||||
for (uint16_t count = 0; count < fill_count; count++) {
|
||||
if (registeredVariables[count]->getReadWriteMode()
|
||||
!= PoolVariableIF::VAR_READ
|
||||
&& registeredVariables[count]->getDataPoolId()
|
||||
!= PoolVariableIF::NO_PARAMETER) {
|
||||
registeredVariables[count]->commit();
|
||||
}
|
||||
}
|
||||
state = DATA_SET_UNINITIALISED;
|
||||
freeDataPoolLock();
|
||||
return RETURN_OK;
|
||||
} else {
|
||||
ReturnValue_t result = RETURN_OK;
|
||||
lockDataPool();
|
||||
for (uint16_t count = 0; count < fill_count; count++) {
|
||||
if (registeredVariables[count]->getReadWriteMode()
|
||||
== PoolVariableIF::VAR_WRITE
|
||||
&& registeredVariables[count]->getDataPoolId()
|
||||
!= PoolVariableIF::NO_PARAMETER) {
|
||||
registeredVariables[count]->commit();
|
||||
} else if (registeredVariables[count]->getDataPoolId()
|
||||
!= PoolVariableIF::NO_PARAMETER) {
|
||||
if (result != COMMITING_WITHOUT_READING) {
|
||||
error
|
||||
<< "DataSet::commit(): commit-without-read call made with non write-only variable."
|
||||
<< std::endl;
|
||||
result = COMMITING_WITHOUT_READING;
|
||||
}
|
||||
}
|
||||
}
|
||||
state = DATA_SET_UNINITIALISED;
|
||||
freeDataPoolLock();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DataSet::registerVariable(PoolVariableIF* variable) {
|
||||
if (state == DATA_SET_UNINITIALISED) {
|
||||
if (variable != NULL) {
|
||||
if (fill_count < DATA_SET_MAX_SIZE) {
|
||||
registeredVariables[fill_count] = variable;
|
||||
fill_count++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
error
|
||||
<< "DataSet::registerVariable: failed. Either NULL, or set is full, or call made in wrong position."
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t DataSet::freeDataPoolLock() {
|
||||
return ::dataPool.freeDataPoolLock();
|
||||
}
|
||||
|
||||
uint8_t DataSet::lockDataPool() {
|
||||
return ::dataPool.lockDataPool();
|
||||
}
|
||||
|
||||
ReturnValue_t DataSet::serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
ReturnValue_t result = RETURN_FAILED;
|
||||
for (uint16_t count = 0; count < fill_count; count++) {
|
||||
result = registeredVariables[count]->serialize(buffer, size, max_size,
|
||||
bigEndian);
|
||||
if (result != RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t DataSet::getSerializedSize() const {
|
||||
uint32_t size = 0;
|
||||
for (uint16_t count = 0; count < fill_count; count++) {
|
||||
size += registeredVariables[count]->getSerializedSize();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
ReturnValue_t DataSet::deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
ReturnValue_t result = RETURN_FAILED;
|
||||
for (uint16_t count = 0; count < fill_count; count++) {
|
||||
result = registeredVariables[count]->deSerialize(buffer, size,
|
||||
bigEndian);
|
||||
if (result != RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
152
datapool/DataSet.h
Normal file
152
datapool/DataSet.h
Normal file
@ -0,0 +1,152 @@
|
||||
/*
|
||||
* \file DataSet.h
|
||||
*
|
||||
* \brief This file contains the DataSet class and a small structure called DataSetContent.
|
||||
*
|
||||
* \date 10/17/2012
|
||||
*
|
||||
* \author Bastian Baetz
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef DATASET_H_
|
||||
#define DATASET_H_
|
||||
|
||||
#include <framework/datapool/DataPool.h>
|
||||
#include <framework/datapool/DataSetIF.h>
|
||||
#include <framework/datapool/PoolRawAccess.h>
|
||||
#include <framework/datapool/PoolVariable.h>
|
||||
#include <framework/datapool/PoolVarList.h>
|
||||
#include <framework/datapool/PoolVector.h>
|
||||
#include <framework/serialize/SerializeAdapter.h>
|
||||
/**
|
||||
* \brief The DataSet class manages a set of locally checked out variables.
|
||||
*
|
||||
* \details This class manages a list, where a set of local variables (or pool variables) are
|
||||
* registered. They are checked-out (i.e. their values are looked up and copied)
|
||||
* with the read call. After the user finishes working with the pool variables,
|
||||
* he can write back all variable values to the pool with the commit call.
|
||||
* The data set manages locking and freeing the data pool, to ensure that all values
|
||||
* are read and written back at once.
|
||||
* An internal state manages usage of this class. Variables may only be registered before
|
||||
* the read call is made, and the commit call only after the read call.
|
||||
* If pool variables are writable and not committed until destruction of the set, the
|
||||
* DataSet class automatically sets the valid flag in the data pool to invalid (without)
|
||||
* changing the variable's value.
|
||||
*
|
||||
* \ingroup data_pool
|
||||
*/
|
||||
class DataSet: public DataSetIF, public HasReturnvaluesIF, public SerializeIF {
|
||||
private:
|
||||
//TODO we could use a linked list of datapool variables
|
||||
static const uint8_t DATA_SET_MAX_SIZE = 63; //!< This definition sets the maximum number of variables to register in one DataSet.
|
||||
|
||||
/**
|
||||
* \brief This array represents all pool variables registered in this set.
|
||||
* \details It has a maximum size of DATA_SET_MAX_SIZE.
|
||||
*/
|
||||
PoolVariableIF* registeredVariables[DATA_SET_MAX_SIZE];
|
||||
/**
|
||||
* \brief The fill_count attribute ensures that the variables register in the correct array
|
||||
* position and that the maximum number of variables is not exceeded.
|
||||
*/
|
||||
uint16_t fill_count;
|
||||
/**
|
||||
* States of the seet.
|
||||
*/
|
||||
enum States {
|
||||
DATA_SET_UNINITIALISED, //!< DATA_SET_UNINITIALISED
|
||||
DATA_SET_WAS_READ //!< DATA_SET_WAS_READ
|
||||
};
|
||||
/**
|
||||
* \brief state manages the internal state of the data set, which is important e.g. for the
|
||||
* behavior on destruction.
|
||||
*/
|
||||
States state;
|
||||
/**
|
||||
* \brief This is a small helper function to facilitate locking the global data pool.
|
||||
* \details It makes use of the lockDataPool method offered by the DataPool class.
|
||||
*/
|
||||
uint8_t lockDataPool();
|
||||
/**
|
||||
* \brief This is a small helper function to facilitate unlocking the global data pool.
|
||||
* \details It makes use of the freeDataPoolLock method offered by the DataPool class.
|
||||
*/
|
||||
uint8_t freeDataPoolLock();
|
||||
|
||||
public:
|
||||
static const uint8_t INTERFACE_ID = DATA_SET_CLASS;
|
||||
static const ReturnValue_t INVALID_PARAMETER_DEFINITION =
|
||||
MAKE_RETURN_CODE( 0x01 );
|
||||
static const ReturnValue_t SET_WAS_ALREADY_READ = MAKE_RETURN_CODE( 0x02 );
|
||||
static const ReturnValue_t COMMITING_WITHOUT_READING =
|
||||
MAKE_RETURN_CODE(0x03);
|
||||
|
||||
/**
|
||||
* \brief The constructor simply sets the fill_count to zero and sets the state to "uninitialized".
|
||||
*/
|
||||
DataSet();
|
||||
/**
|
||||
* \brief The destructor automatically manages writing the valid information of variables.
|
||||
* \details In case the data set was read out, but not committed (indicated by state),
|
||||
* the destructor parses all variables that are still registered to the set.
|
||||
* For each, the valid flag in the data pool is set to "invalid".
|
||||
*/
|
||||
~DataSet();
|
||||
/**
|
||||
* \brief The read call initializes reading out all registered variables.
|
||||
* \details It iterates through the list of registered variables and calls all read()
|
||||
* functions of the registered pool variables (which read out their values from the
|
||||
* data pool) which are not write-only. In case of an error (e.g. a wrong data type,
|
||||
* or an invalid data pool id), the operation is aborted and
|
||||
* \c INVALID_PARAMETER_DEFINITION returned.
|
||||
* The data pool is locked during the whole read operation and freed afterwards.
|
||||
* The state changes to "was written" after this operation.
|
||||
* \return - \c RETURN_OK if all variables were read successfully.
|
||||
* - \c INVALID_PARAMETER_DEFINITION if PID, size or type of the
|
||||
* requested variable is invalid.
|
||||
* - \c SET_WAS_ALREADY_READ if read() is called twice without calling
|
||||
* commit() in between
|
||||
*/
|
||||
ReturnValue_t read();
|
||||
/**
|
||||
* \brief The commit call initializes writing back the registered variables.
|
||||
* \details It iterates through the list of registered variables and calls
|
||||
* the commit() method of the remaining registered variables (which write back
|
||||
* their values to the pool).
|
||||
* The data pool is locked during the whole commit operation and freed afterwards.
|
||||
* The state changes to "was committed" after this operation.
|
||||
* If the set does contain at least one variable which is not write-only commit()
|
||||
* can only be called after read(). If the set only contains variables which are
|
||||
* write only, commit() can be called without a preceding read() call.
|
||||
* \return - \c RETURN_OK if all variables were read successfully.
|
||||
* - \c COMMITING_WITHOUT_READING if set was not read yet and contains non write-only
|
||||
* variables
|
||||
*/
|
||||
ReturnValue_t commit(void);
|
||||
/**
|
||||
* Variant of method above which sets validity of all elements of the set.
|
||||
* @param valid Validity information from PoolVariableIF.
|
||||
* \return - \c RETURN_OK if all variables were read successfully.
|
||||
* - \c COMMITING_WITHOUT_READING if set was not read yet and contains non write-only
|
||||
* variables
|
||||
*/
|
||||
ReturnValue_t commit(uint8_t valid);
|
||||
/**
|
||||
* \brief This operation is used to register the local variables in the set.
|
||||
* \details It copies all required information to the currently
|
||||
* free space in the registeredVariables list.
|
||||
*/
|
||||
void registerVariable(PoolVariableIF* variable);
|
||||
|
||||
ReturnValue_t serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const;
|
||||
|
||||
uint32_t getSerializedSize() const;
|
||||
|
||||
ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian);
|
||||
|
||||
};
|
||||
|
||||
#endif /* DATASET_H_ */
|
42
datapool/DataSetIF.h
Normal file
42
datapool/DataSetIF.h
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* \file DataSetIF.h
|
||||
*
|
||||
* \brief This file contains the small interface to access the DataSet class.
|
||||
*
|
||||
* \date 10/23/2012
|
||||
*
|
||||
* \author Bastian Baetz
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef DATASETIF_H_
|
||||
#define DATASETIF_H_
|
||||
|
||||
|
||||
#include <framework/osal/OSAL.h>
|
||||
|
||||
class PoolVariableIF;
|
||||
|
||||
/**
|
||||
* \brief This class defines a small interface to register on a DataSet.
|
||||
*
|
||||
* \details Currently, the only purpose of this interface is to provide a method for locally
|
||||
* checked-out variables to register on a data set. Still, it may become useful for
|
||||
* other purposes as well.
|
||||
*
|
||||
* \ingroup data_pool
|
||||
*/
|
||||
class DataSetIF {
|
||||
public:
|
||||
/**
|
||||
* \brief This is an empty virtual destructor, as it is proposed for C++ interfaces.
|
||||
*/
|
||||
virtual ~DataSetIF() {}
|
||||
/**
|
||||
* \brief This operation provides a method to register local data pool variables
|
||||
* to register in a data set by passing itself to this DataSet operation.
|
||||
*/
|
||||
virtual void registerVariable( PoolVariableIF* variable ) = 0;
|
||||
};
|
||||
|
||||
#endif /* DATASETIF_H_ */
|
25
datapool/Makefile
Executable file
25
datapool/Makefile
Executable file
@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# OPUS makefile
|
||||
#
|
||||
# Created on: Mar 04, 2010
|
||||
# Author: ziemke
|
||||
# Author: Claas Ziemke
|
||||
# Copyright 2010, Claas Ziemke <claas.ziemke@gmx.net>
|
||||
#
|
||||
|
||||
BASEDIR=../../
|
||||
include $(BASEDIR)/options.mk
|
||||
|
||||
OBJ = $(BUILDDIR)/OPUSDataPool.o \
|
||||
$(BUILDDIR)/OPUSDataPoolItem.o \
|
||||
$(BUILDDIR)/OPUSDataSet.o \
|
||||
$(BUILDDIR)/OPUSRegVar.o
|
||||
|
||||
all: $(OBJ)
|
||||
|
||||
$(BUILDDIR)/%.o: %.cpp %.h
|
||||
$(CPP) $(CFLAGS) $(DEFINES) $(CCOPT) ${INCLUDE} -c $< -o $@
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.gcno *.gcda
|
152
datapool/PIDReader.h
Normal file
152
datapool/PIDReader.h
Normal file
@ -0,0 +1,152 @@
|
||||
/*
|
||||
* PIDReader.h
|
||||
*
|
||||
* Created on: 14.05.2014
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef PIDREADER_H_
|
||||
#define PIDREADER_H_
|
||||
#include <framework/datapool/DataPool.h>
|
||||
#include <framework/datapool/DataSetIF.h>
|
||||
#include <framework/datapool/PoolEntry.h>
|
||||
#include <framework/datapool/PoolVariableIF.h>
|
||||
#include <framework/serialize/SerializeAdapter.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
template<typename U, uint8_t n_var> class PIDReaderList;
|
||||
|
||||
template<typename T>
|
||||
class PIDReader: public PoolVariableIF {
|
||||
template<typename U, uint8_t n_var> friend class PIDReaderList;
|
||||
protected:
|
||||
uint32_t parameterId;
|
||||
uint8_t valid;
|
||||
ReturnValue_t read() {
|
||||
uint8_t arrayIndex = DataPool::PIDToArrayIndex(parameterId);
|
||||
PoolEntry<T>* read_out = ::dataPool.getData<T>(
|
||||
DataPool::PIDToDataPoolId(parameterId), arrayIndex);
|
||||
if (read_out != NULL) {
|
||||
valid = read_out->valid;
|
||||
value = read_out->address[arrayIndex];
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
value = 0;
|
||||
valid = false;
|
||||
error << "PIDReader: read of PID 0x" << std::hex << parameterId
|
||||
<< std::dec << " failed." << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Never commit, is read-only.
|
||||
* Reason is the possibility to access a single DP vector element, but if we commit,
|
||||
* we set validity of the whole vector.
|
||||
*/
|
||||
ReturnValue_t commit() {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
/**
|
||||
* Empty ctor for List initialization
|
||||
*/
|
||||
PIDReader() :
|
||||
parameterId(PoolVariableIF::NO_PARAMETER), valid(PoolVariableIF::INVALID), value(0) {
|
||||
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* \brief This is the local copy of the data pool entry.
|
||||
*/
|
||||
T value;
|
||||
/**
|
||||
* \brief In the constructor, the variable can register itself in a DataSet (if not NULL is
|
||||
* passed).
|
||||
* \details It DOES NOT fetch the current value from the data pool, but sets the value
|
||||
* attribute to default (0). The value is fetched within the read() operation.
|
||||
* \param set_id This is the id in the global data pool this instance of the access class
|
||||
* corresponds to.
|
||||
* \param dataSet The data set in which the variable shall register itself. If NULL,
|
||||
* the variable is not registered.
|
||||
* \param setWritable If this flag is set to true, changes in the value attribute can be
|
||||
* written back to the data pool, otherwise not.
|
||||
*/
|
||||
PIDReader(uint32_t setParameterId, DataSetIF* dataSet) :
|
||||
parameterId(setParameterId), valid(
|
||||
PoolVariableIF::INVALID), value(0) {
|
||||
if (dataSet != NULL) {
|
||||
dataSet->registerVariable(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy ctor to copy classes containing Pool Variables.
|
||||
*/
|
||||
PIDReader(const PIDReader& rhs) :
|
||||
parameterId(rhs.parameterId), valid(rhs.valid), value(rhs.value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief The classes destructor is empty.
|
||||
*/
|
||||
~PIDReader() {
|
||||
|
||||
}
|
||||
/**
|
||||
* \brief This operation returns the data pool id of the variable.
|
||||
*/
|
||||
uint32_t getDataPoolId() const {
|
||||
return DataPool::PIDToDataPoolId(parameterId);
|
||||
}
|
||||
uint32_t getParameterId() const {
|
||||
return parameterId;
|
||||
}
|
||||
/**
|
||||
* This method returns if the variable is write-only, read-write or read-only.
|
||||
*/
|
||||
ReadWriteMode_t getReadWriteMode() const {
|
||||
return VAR_READ;
|
||||
}
|
||||
/**
|
||||
* \brief With this call, the valid information of the variable is returned.
|
||||
*/
|
||||
bool isValid() const {
|
||||
if (valid)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t getValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
void setValid(uint8_t valid) {
|
||||
this->valid = valid;
|
||||
}
|
||||
|
||||
operator T() {
|
||||
return value;
|
||||
}
|
||||
|
||||
PIDReader<T> &operator=(T newValue) {
|
||||
value = newValue;
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
return SerializeAdapter<T>::serialize(&value, buffer, size, max_size,
|
||||
bigEndian);
|
||||
}
|
||||
|
||||
virtual uint32_t getSerializedSize() const {
|
||||
return SerializeAdapter<T>::getSerializedSize(&value);
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
return SerializeAdapter<T>::deSerialize(&value, buffer, size, bigEndian);
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* PIDREADER_H_ */
|
34
datapool/PIDReaderList.h
Normal file
34
datapool/PIDReaderList.h
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* PIDReaderList.h
|
||||
*
|
||||
* Created on: 15.07.2015
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef FRAMEWORK_DATAPOOL_PIDREADERLIST_H_
|
||||
#define FRAMEWORK_DATAPOOL_PIDREADERLIST_H_
|
||||
|
||||
#include <framework/datapool/PIDReader.h>
|
||||
#include <framework/datapool/PoolVariableIF.h>
|
||||
template <class T, uint8_t n_var>
|
||||
class PIDReaderList {
|
||||
private:
|
||||
PIDReader<T> variables[n_var];
|
||||
public:
|
||||
PIDReaderList( const uint32_t setPid[n_var], DataSetIF* dataSet) {
|
||||
//I really should have a look at the new init list c++ syntax.
|
||||
if (dataSet == NULL) {
|
||||
return;
|
||||
}
|
||||
for (uint8_t count = 0; count < n_var; count++) {
|
||||
variables[count].parameterId = setPid[count];
|
||||
dataSet->registerVariable(&variables[count]);
|
||||
}
|
||||
}
|
||||
|
||||
PIDReader<T> &operator [](int i) { return variables[i]; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* FRAMEWORK_DATAPOOL_PIDREADERLIST_H_ */
|
73
datapool/PoolEntry.cpp
Normal file
73
datapool/PoolEntry.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* PoolEntry.cpp
|
||||
*
|
||||
* Created on: Oct 25, 2012
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#include <framework/datapool/PoolEntry.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
template <typename T>
|
||||
PoolEntry<T>::PoolEntry( T* initValue, uint8_t set_length, uint8_t set_valid ) : length(set_length), valid(set_valid) {
|
||||
this->address = new T[this->length];
|
||||
if (initValue != NULL) {
|
||||
memcpy(this->address, initValue, this->getByteSize() );
|
||||
} else {
|
||||
memset(this->address, 0, this->getByteSize() );
|
||||
}
|
||||
}
|
||||
|
||||
//As the data pool is global, this dtor is only be called on program exit.
|
||||
//Warning! Never copy pool entries!
|
||||
template <typename T>
|
||||
PoolEntry<T>::~PoolEntry() {
|
||||
delete[] this->address;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
uint16_t PoolEntry<T>::getByteSize() {
|
||||
return ( sizeof(T) * this->length );
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
uint8_t PoolEntry<T>::getSize() {
|
||||
return this->length;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void* PoolEntry<T>::getRawData() {
|
||||
return this->address;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void PoolEntry<T>::setValid( uint8_t isValid ) {
|
||||
this->valid = isValid;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
uint8_t PoolEntry<T>::getValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void PoolEntry<T>::print() {
|
||||
for (uint8_t size = 0; size < this->length; size++ ) {
|
||||
debug << "| " << std::hex << (double)this->address[size] << (this->valid? " (valid) " : " (invalid) ");
|
||||
}
|
||||
debug << std::dec << std::endl;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Type PoolEntry<T>::getType() {
|
||||
return PodTypeConversion<T>::type;
|
||||
}
|
||||
|
||||
template class PoolEntry<uint8_t>;
|
||||
template class PoolEntry<uint16_t>;
|
||||
template class PoolEntry<uint32_t>;
|
||||
template class PoolEntry<int8_t>;
|
||||
template class PoolEntry<int16_t>;
|
||||
template class PoolEntry<int32_t>;
|
||||
template class PoolEntry<float>;
|
||||
template class PoolEntry<double>;
|
84
datapool/PoolEntry.h
Normal file
84
datapool/PoolEntry.h
Normal file
@ -0,0 +1,84 @@
|
||||
#ifndef POOLENTRY_H_
|
||||
#define POOLENTRY_H_
|
||||
|
||||
|
||||
#include <framework/datapool/PoolEntryIF.h>
|
||||
#include <framework/osal/OSAL.h>
|
||||
/**
|
||||
* \brief This is a small helper class that defines a single data pool entry.
|
||||
*
|
||||
* \details The helper is used to store all information together with the data as a single data pool entry.
|
||||
* The content's type is defined by the template argument.
|
||||
* It is prepared for use with plain old data types,
|
||||
* but may be extended to complex types if necessary.
|
||||
* It can be initialized with a certain value, size and validity flag.
|
||||
* It holds a pointer to the real data and offers methods to access this data and to acquire
|
||||
* additional information (such as validity and array/byte size).
|
||||
* It is NOT intended to be used outside the DataPool class.
|
||||
*
|
||||
* \ingroup data_pool
|
||||
*
|
||||
*/
|
||||
template <typename T>
|
||||
class PoolEntry : public PoolEntryIF {
|
||||
public:
|
||||
/**
|
||||
* \brief In the classe's constructor, space is allocated on the heap and
|
||||
* potential init values are copied to that space.
|
||||
* \param initValue A pointer to the single value or array that holds the init value.
|
||||
* With the default value (NULL), the entry is initalized with all 0.
|
||||
* \param set_length Defines the array length of this entry.
|
||||
* \param set_valid Sets the initialization flag. It is invalid (0) by default.
|
||||
*/
|
||||
PoolEntry( T* initValue = NULL, uint8_t set_length = 1, uint8_t set_valid = 0 );
|
||||
/**
|
||||
* \brief The allocated memory for the variable is freed in the destructor.
|
||||
* \details As the data pool is global, this dtor is only called on program exit.
|
||||
* PoolEntries shall never be copied, as a copy might delete the variable on the heap.
|
||||
*/
|
||||
~PoolEntry();
|
||||
/**
|
||||
* \brief This is the address pointing to the allocated memory.
|
||||
*/
|
||||
T* address;
|
||||
/**
|
||||
* \brief This attribute stores the length information.
|
||||
*/
|
||||
uint8_t length;
|
||||
/**
|
||||
* \brief Here, the validity information for a variable is stored.
|
||||
* Every entry (single variable or vector) has one valid flag.
|
||||
*/
|
||||
uint8_t valid;
|
||||
/**
|
||||
* \brief getSize returns the array size of the entry.
|
||||
* \details A single parameter has size 1.
|
||||
*/
|
||||
uint8_t getSize();
|
||||
/**
|
||||
* \brief This operation returns the size in bytes.
|
||||
* \details The size is calculated by sizeof(type) * array_size.
|
||||
*/
|
||||
uint16_t getByteSize();
|
||||
/**
|
||||
* \brief This operation returns a the address pointer casted to void*.
|
||||
*/
|
||||
void* getRawData();
|
||||
/**
|
||||
* \brief This method allows to set the valid information of the pool entry.
|
||||
*/
|
||||
void setValid( uint8_t isValid );
|
||||
/**
|
||||
* \brief This method allows to get the valid information of the pool entry.
|
||||
*/
|
||||
uint8_t getValid();
|
||||
/**
|
||||
* \brief This is a debug method that prints all values and the valid information to the screen.
|
||||
* It prints all array entries in a row.
|
||||
*/
|
||||
void print();
|
||||
|
||||
Type getType();
|
||||
};
|
||||
|
||||
#endif /* POOLENTRY_H_ */
|
67
datapool/PoolEntryIF.h
Normal file
67
datapool/PoolEntryIF.h
Normal file
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* \file PoolEntryIF.h
|
||||
*
|
||||
* \brief This file holds the class that defines the Interface for Pool Entry elements.
|
||||
*
|
||||
* \date 10/18/2012
|
||||
*
|
||||
* \author Bastian Baetz
|
||||
*/
|
||||
|
||||
#ifndef POOLENTRYIF_H_
|
||||
#define POOLENTRYIF_H_
|
||||
|
||||
#include <framework/globalfunctions/Type.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
/**
|
||||
* \brief This interface defines the access possibilities to a single data pool entry.
|
||||
*
|
||||
* \details The interface provides methods to determine the size and the validity information of a value.
|
||||
* It also defines a method to receive a pointer to the raw data content.
|
||||
* It is mainly used by DataPool itself, but also as a return pointer.
|
||||
*
|
||||
* \ingroup data_pool
|
||||
*
|
||||
*/
|
||||
class PoolEntryIF {
|
||||
public:
|
||||
/**
|
||||
* \brief This is an empty virtual destructor, as it is proposed for C++ interfaces.
|
||||
*/
|
||||
virtual ~PoolEntryIF() {
|
||||
}
|
||||
/**
|
||||
* \brief getSize returns the array size of the entry. A single variable parameter has size 1.
|
||||
*/
|
||||
virtual uint8_t getSize() = 0;
|
||||
/**
|
||||
* \brief This operation returns the size in bytes, which is calculated by
|
||||
* sizeof(type) * array_size.
|
||||
*/
|
||||
virtual uint16_t getByteSize() = 0;
|
||||
/**
|
||||
* \brief This operation returns a the address pointer casted to void*.
|
||||
*/
|
||||
virtual void* getRawData() = 0;
|
||||
/**
|
||||
* \brief This method allows to set the valid information of the pool entry.
|
||||
*/
|
||||
virtual void setValid(uint8_t isValid) = 0;
|
||||
/**
|
||||
* \brief This method allows to set the valid information of the pool entry.
|
||||
*/
|
||||
virtual uint8_t getValid() = 0;
|
||||
/**
|
||||
* \brief This is a debug method that prints all values and the valid information to the screen.
|
||||
* It prints all array entries in a row.
|
||||
*/
|
||||
virtual void print() = 0;
|
||||
/**
|
||||
* Returns the type of the entry.
|
||||
*/
|
||||
virtual Type getType() = 0;
|
||||
};
|
||||
|
||||
#endif /* POOLENTRYIF_H_ */
|
194
datapool/PoolRawAccess.cpp
Normal file
194
datapool/PoolRawAccess.cpp
Normal file
@ -0,0 +1,194 @@
|
||||
/*
|
||||
* PoolRawAccess.cpp
|
||||
*
|
||||
* Created on: 29.10.2012
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#include <framework/datapool/DataPool.h>
|
||||
#include <framework/datapool/PoolEntryIF.h>
|
||||
#include <framework/datapool/PoolRawAccess.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
PoolRawAccess::PoolRawAccess(uint32_t set_id, uint8_t setArrayEntry,
|
||||
DataSetIF* data_set, ReadWriteMode_t setReadWriteMode) :
|
||||
dataPoolId(set_id), arrayEntry(setArrayEntry), valid(false), typeSize(
|
||||
0), sizeTillEnd(0), readWriteMode(setReadWriteMode) {
|
||||
memset(value, 0, sizeof(value));
|
||||
if (data_set != NULL) {
|
||||
data_set->registerVariable(this);
|
||||
}
|
||||
}
|
||||
|
||||
PoolRawAccess::~PoolRawAccess() {
|
||||
|
||||
}
|
||||
|
||||
ReturnValue_t PoolRawAccess::read() {
|
||||
PoolEntryIF* read_out = ::dataPool.getRawData(dataPoolId);
|
||||
if (read_out != NULL) {
|
||||
valid = read_out->getValid();
|
||||
if (read_out->getSize() > arrayEntry) {
|
||||
typeSize = read_out->getByteSize() / read_out->getSize();
|
||||
if (typeSize <= sizeof(value)) {
|
||||
uint16_t arrayPosition = arrayEntry * typeSize;
|
||||
sizeTillEnd = read_out->getByteSize() - arrayPosition;
|
||||
uint8_t* ptr =
|
||||
&((uint8_t*) read_out->getRawData())[arrayPosition];
|
||||
memcpy(value, ptr, typeSize);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
//Error value type too large.
|
||||
}
|
||||
} else {
|
||||
//Error index requested too large
|
||||
}
|
||||
} else {
|
||||
//Error entry does not exist.
|
||||
}
|
||||
error << "PoolRawAccess: read of DP Variable 0x" << std::hex << dataPoolId
|
||||
<< std::dec << " failed." << std::endl;
|
||||
valid = INVALID;
|
||||
typeSize = 0;
|
||||
sizeTillEnd = 0;
|
||||
memset(value, 0, sizeof(value));
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
ReturnValue_t PoolRawAccess::commit() {
|
||||
PoolEntryIF* write_back = ::dataPool.getRawData(dataPoolId);
|
||||
if ((write_back != NULL) && (readWriteMode != VAR_READ)) {
|
||||
write_back->setValid(valid);
|
||||
uint8_t array_position = arrayEntry * typeSize;
|
||||
uint8_t* ptr = &((uint8_t*) write_back->getRawData())[array_position];
|
||||
memcpy(ptr, value, typeSize);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t* PoolRawAccess::getEntry() {
|
||||
return value;
|
||||
}
|
||||
|
||||
ReturnValue_t PoolRawAccess::getEntryEndianSafe(uint8_t* buffer,
|
||||
uint32_t* writtenBytes, uint32_t max_size) {
|
||||
uint8_t* data_ptr = getEntry();
|
||||
// debug << "PoolRawAccess::getEntry: Array position: " << index * size_of_type << " Size of T: " << (int)size_of_type << " ByteSize: " << byte_size << " Position: " << *size << std::endl;
|
||||
if (typeSize == 0)
|
||||
return DATA_POOL_ACCESS_FAILED;
|
||||
if (typeSize > max_size)
|
||||
return INCORRECT_SIZE;
|
||||
#ifndef BYTE_ORDER
|
||||
#error BYTE_ORDER not defined
|
||||
#elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
for (uint8_t count = 0; count < typeSize; count++) {
|
||||
buffer[count] = data_ptr[typeSize - count - 1];
|
||||
}
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
memcpy(buffer, data_ptr, typeSize);
|
||||
#endif
|
||||
*writtenBytes = typeSize;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
uint8_t PoolRawAccess::getSizeOfType() {
|
||||
return typeSize;
|
||||
}
|
||||
|
||||
uint32_t PoolRawAccess::getDataPoolId() const {
|
||||
return dataPoolId;
|
||||
}
|
||||
|
||||
PoolVariableIF::ReadWriteMode_t PoolRawAccess::getReadWriteMode() const {
|
||||
return readWriteMode;
|
||||
}
|
||||
|
||||
ReturnValue_t PoolRawAccess::setEntryFromBigEndian(const uint8_t* buffer,
|
||||
uint32_t setSize) {
|
||||
if (typeSize == setSize) {
|
||||
#ifndef BYTE_ORDER
|
||||
#error BYTE_ORDER not defined
|
||||
#elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
for (uint8_t count = 0; count < typeSize; count++) {
|
||||
value[count] = buffer[typeSize - count - 1];
|
||||
}
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
memcpy(value, buffer, typeSize);
|
||||
#endif
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
error << "PoolRawAccess::setEntryFromBigEndian: Illegal sizes: Internal"
|
||||
<< (uint32_t) typeSize << ", Requested: " << setSize
|
||||
<< std::endl;
|
||||
return INCORRECT_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
bool PoolRawAccess::isValid() const {
|
||||
if (valid != INVALID)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void PoolRawAccess::setValid(uint8_t valid) {
|
||||
this->valid = valid;
|
||||
}
|
||||
|
||||
uint16_t PoolRawAccess::getSizeTillEnd() const {
|
||||
return sizeTillEnd;
|
||||
}
|
||||
|
||||
ReturnValue_t PoolRawAccess::serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
if (typeSize + *size <= max_size) {
|
||||
if (bigEndian) {
|
||||
#ifndef BYTE_ORDER
|
||||
#error BYTE_ORDER not defined
|
||||
#elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
for (uint8_t count = 0; count < typeSize; count++) {
|
||||
(*buffer)[count] = value[typeSize - count - 1];
|
||||
}
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
memcpy(*buffer, value, typeSize);
|
||||
#endif
|
||||
} else {
|
||||
memcpy(*buffer, value, typeSize);
|
||||
}
|
||||
*size += typeSize;
|
||||
(*buffer) += typeSize;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return SerializeIF::BUFFER_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t PoolRawAccess::getSerializedSize() const {
|
||||
return typeSize;
|
||||
}
|
||||
|
||||
ReturnValue_t PoolRawAccess::deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
*size -= typeSize;
|
||||
if (*size >= 0) {
|
||||
|
||||
if (bigEndian) {
|
||||
#ifndef BYTE_ORDER
|
||||
#error BYTE_ORDER not defined
|
||||
#elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
for (uint8_t count = 0; count < typeSize; count++) {
|
||||
value[count] = (*buffer)[typeSize - count - 1];
|
||||
}
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
memcpy(value, *buffer, typeSize);
|
||||
#endif
|
||||
} else {
|
||||
memcpy(value, *buffer, typeSize);
|
||||
}
|
||||
*buffer += typeSize;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return SerializeIF::STREAM_TOO_SHORT;
|
||||
}
|
||||
}
|
135
datapool/PoolRawAccess.h
Normal file
135
datapool/PoolRawAccess.h
Normal file
@ -0,0 +1,135 @@
|
||||
#ifndef POOLRAWACCESS_H_
|
||||
#define POOLRAWACCESS_H_
|
||||
|
||||
#include <framework/datapool/DataSetIF.h>
|
||||
#include <framework/datapool/PoolVariableIF.h>
|
||||
|
||||
/**
|
||||
* This class allows accessing Data Pool variables as raw bytes.
|
||||
* This is necessary to have an access method for HK data, as the PID's alone do not
|
||||
* provide a type information.
|
||||
* \ingroup data_pool
|
||||
*/
|
||||
class PoolRawAccess: public PoolVariableIF {
|
||||
private:
|
||||
/**
|
||||
* \brief To access the correct data pool entry on read and commit calls, the data pool id
|
||||
* is stored.
|
||||
*/
|
||||
uint32_t dataPoolId;
|
||||
/**
|
||||
* \brief The array entry that is fetched from the data pool.
|
||||
*/
|
||||
uint8_t arrayEntry;
|
||||
/**
|
||||
* \brief The valid information as it was stored in the data pool is copied to this attribute.
|
||||
*/
|
||||
uint8_t valid;
|
||||
/**
|
||||
* \brief This value contains the size of the data pool entry in bytes.
|
||||
*/
|
||||
uint8_t typeSize;
|
||||
/**
|
||||
* The size (in bytes) from the selected entry till the end of this DataPool variable.
|
||||
*/
|
||||
uint16_t sizeTillEnd;
|
||||
/**
|
||||
* \brief The information whether the class is read-write or read-only is stored here.
|
||||
*/
|
||||
ReadWriteMode_t readWriteMode;
|
||||
static const uint8_t RAW_MAX_SIZE = sizeof(double);
|
||||
protected:
|
||||
/**
|
||||
* \brief This is a call to read the value from the global data pool.
|
||||
* \details When executed, this operation tries to fetch the pool entry with matching
|
||||
* data pool id from the global data pool and copies the value and the valid
|
||||
* information to its local attributes. In case of a failure (wrong type or
|
||||
* pool id not found), the variable is set to zero and invalid.
|
||||
* The operation does NOT provide any mutual exclusive protection by itself.
|
||||
*/
|
||||
ReturnValue_t read();
|
||||
/**
|
||||
* \brief The commit call writes back the variable's value to the data pool.
|
||||
* \details It checks type and size, as well as if the variable is writable. If so,
|
||||
* the value is copied and the valid flag is automatically set to "valid".
|
||||
* The operation does NOT provide any mutual exclusive protection by itself.
|
||||
*
|
||||
*/
|
||||
ReturnValue_t commit();
|
||||
public:
|
||||
static const uint8_t INTERFACE_ID = POOL_RAW_ACCESS_CLASS;
|
||||
static const ReturnValue_t INCORRECT_SIZE = MAKE_RETURN_CODE(0x01);
|
||||
static const ReturnValue_t DATA_POOL_ACCESS_FAILED = MAKE_RETURN_CODE(0x02);
|
||||
uint8_t value[RAW_MAX_SIZE];
|
||||
PoolRawAccess(uint32_t data_pool_id, uint8_t arrayEntry,
|
||||
DataSetIF* data_set, ReadWriteMode_t setReadWriteMode =
|
||||
PoolVariableIF::VAR_READ);
|
||||
/**
|
||||
* \brief The classes destructor is empty. If commit() was not called, the local value is
|
||||
* discarded and not written back to the data pool.
|
||||
*/
|
||||
~PoolRawAccess();
|
||||
/**
|
||||
* \brief This operation returns a pointer to the entry fetched.
|
||||
* \details This means, it does not return a pointer to byte "index", but to the start byte of
|
||||
* array entry "index". Example: If the original data pool array consists of an double
|
||||
* array of size four, getEntry(1) returns &(this->value[8]).
|
||||
*/
|
||||
uint8_t* getEntry();
|
||||
/**
|
||||
* \brief This operation returns the fetched entry from the data pool and
|
||||
* flips the bytes, if necessary.
|
||||
* \details It makes use of the getEntry call of this function, but additionally flips the
|
||||
* bytes to big endian, which is the default for external communication (as House-
|
||||
* keeping telemetry). To achieve this, the data is copied directly to the passed
|
||||
* buffer, if it fits in the given max_size.
|
||||
* \param buffer A pointer to a buffer to write to
|
||||
* \param writtenBytes The number of bytes written is returned with this value.
|
||||
* \param max_size The maximum size that the function may write to buffer.
|
||||
* \return - \c RETURN_OK if entry could be acquired
|
||||
* - \c RETURN_FAILED else.
|
||||
*/
|
||||
ReturnValue_t getEntryEndianSafe(uint8_t* buffer, uint32_t* size,
|
||||
uint32_t max_size);
|
||||
/**
|
||||
* With this method, the content can be set from a big endian buffer safely.
|
||||
* @param buffer Pointer to the data to set
|
||||
* @param size Size of the data to write. Must fit this->size.
|
||||
* @return - \c RETURN_OK on success
|
||||
* - \c RETURN_FAILED on failure
|
||||
*/
|
||||
ReturnValue_t setEntryFromBigEndian(const uint8_t* buffer,
|
||||
uint32_t setSize);
|
||||
/**
|
||||
* \brief This operation returns the size of the entry currently stored.
|
||||
*/
|
||||
uint8_t getSizeOfType();
|
||||
/**
|
||||
* \brief This operation returns the data pool id of the variable.
|
||||
*/
|
||||
uint32_t getDataPoolId() const;
|
||||
/**
|
||||
* This method returns if the variable is read-write or read-only.
|
||||
*/
|
||||
ReadWriteMode_t getReadWriteMode() const;
|
||||
/**
|
||||
* \brief With this call, the valid information of the variable is returned.
|
||||
*/
|
||||
bool isValid() const;
|
||||
|
||||
void setValid(uint8_t valid);
|
||||
/**
|
||||
* Getter for the remaining size.
|
||||
*/
|
||||
uint16_t getSizeTillEnd() const;
|
||||
|
||||
ReturnValue_t serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const;
|
||||
|
||||
uint32_t getSerializedSize() const;
|
||||
|
||||
ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian);
|
||||
};
|
||||
|
||||
#endif /* POOLRAWACCESS_H_ */
|
35
datapool/PoolVarList.h
Normal file
35
datapool/PoolVarList.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* PoolVarList.h
|
||||
*
|
||||
* Created on: 06.03.2014
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef POOLVARLIST_H_
|
||||
#define POOLVARLIST_H_
|
||||
|
||||
#include <framework/datapool/PoolVariable.h>
|
||||
#include <framework/datapool/PoolVariableIF.h>
|
||||
template <class T, uint8_t n_var>
|
||||
class PoolVarList {
|
||||
private:
|
||||
PoolVariable<T> variables[n_var];
|
||||
public:
|
||||
PoolVarList( const uint32_t set_id[n_var], DataSetIF* dataSet, PoolVariableIF::ReadWriteMode_t setReadWriteMode ) {
|
||||
//I really should have a look at the new init list c++ syntax.
|
||||
if (dataSet == NULL) {
|
||||
return;
|
||||
}
|
||||
for (uint8_t count = 0; count < n_var; count++) {
|
||||
variables[count].dataPoolId = set_id[count];
|
||||
variables[count].readWriteMode = setReadWriteMode;
|
||||
dataSet->registerVariable(&variables[count]);
|
||||
}
|
||||
}
|
||||
|
||||
PoolVariable<T> &operator [](int i) { return variables[i]; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* POOLVARLIST_H_ */
|
295
datapool/PoolVariable.h
Normal file
295
datapool/PoolVariable.h
Normal file
@ -0,0 +1,295 @@
|
||||
/*
|
||||
* \file PoolVariable.h
|
||||
*
|
||||
* \brief This file contains the PoolVariable class, which locally represents a non-array data pool variable.
|
||||
*
|
||||
* \date 10/17/2012
|
||||
*
|
||||
* \author Bastian Baetz
|
||||
*/
|
||||
|
||||
#ifndef POOLVARIABLE_H_
|
||||
#define POOLVARIABLE_H_
|
||||
|
||||
#include <framework/datapool/DataSetIF.h>
|
||||
#include <framework/datapool/PoolEntry.h>
|
||||
#include <framework/datapool/PoolVariableIF.h>
|
||||
#include <framework/serialize/SerializeAdapter.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
template<typename T, uint8_t n_var> class PoolVarList;
|
||||
|
||||
/**
|
||||
* \brief This is the access class for non-array data pool entries.
|
||||
*
|
||||
* \details To ensure safe usage of the data pool, operation is not done directly on the data pool
|
||||
* entries, but on local copies. This class provides simple type-safe access to single
|
||||
* data pool entries (i.e. entries with length = 1).
|
||||
* The class can be instantiated as read-write and read only.
|
||||
* It provides a commit-and-roll-back semantic, which means that the variable's value in
|
||||
* the data pool is not changed until the commit call is executed.
|
||||
* \tparam T The template parameter sets the type of the variable. Currently, all plain data types
|
||||
* are supported, but in principle any type is possible.
|
||||
* \ingroup data_pool
|
||||
*/
|
||||
template<typename T>
|
||||
class PoolVariable: public PoolVariableIF {
|
||||
template<typename U, uint8_t n_var> friend class PoolVarList;
|
||||
protected:
|
||||
/**
|
||||
* \brief To access the correct data pool entry on read and commit calls, the data pool id
|
||||
* is stored.
|
||||
*/
|
||||
uint32_t dataPoolId;
|
||||
/**
|
||||
* \brief The valid information as it was stored in the data pool is copied to this attribute.
|
||||
*/
|
||||
uint8_t valid;
|
||||
/**
|
||||
* \brief The information whether the class is read-write or read-only is stored here.
|
||||
*/
|
||||
ReadWriteMode_t readWriteMode;
|
||||
/**
|
||||
* \brief This is a call to read the value from the global data pool.
|
||||
* \details When executed, this operation tries to fetch the pool entry with matching
|
||||
* data pool id from the global data pool and copies the value and the valid
|
||||
* information to its local attributes. In case of a failure (wrong type or
|
||||
* pool id not found), the variable is set to zero and invalid.
|
||||
* The operation does NOT provide any mutual exclusive protection by itself.
|
||||
*/
|
||||
ReturnValue_t read() {
|
||||
PoolEntry<T>* read_out = ::dataPool.getData<T>(dataPoolId, 1);
|
||||
if (read_out != NULL) {
|
||||
valid = read_out->valid;
|
||||
value = *(read_out->address);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
value = 0;
|
||||
valid = false;
|
||||
error << "PoolVariable: read of DP Variable 0x" << std::hex
|
||||
<< dataPoolId << std::dec << " failed." << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* \brief The commit call writes back the variable's value to the data pool.
|
||||
* \details It checks type and size, as well as if the variable is writable. If so,
|
||||
* the value is copied and the valid flag is automatically set to "valid".
|
||||
* The operation does NOT provide any mutual exclusive protection by itself.
|
||||
*
|
||||
*/
|
||||
ReturnValue_t commit() {
|
||||
PoolEntry<T>* write_back = ::dataPool.getData<T>(dataPoolId, 1);
|
||||
if ((write_back != NULL) && (readWriteMode != VAR_READ)) {
|
||||
write_back->valid = valid;
|
||||
*(write_back->address) = value;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Empty ctor for List initialization
|
||||
*/
|
||||
PoolVariable() :
|
||||
dataPoolId(PoolVariableIF::NO_PARAMETER), valid(
|
||||
PoolVariableIF::INVALID), readWriteMode(VAR_READ), value(0) {
|
||||
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* \brief This is the local copy of the data pool entry.
|
||||
* \details The user can work on this attribute
|
||||
* just like he would on a simple local variable.
|
||||
*/
|
||||
T value;
|
||||
/**
|
||||
* \brief In the constructor, the variable can register itself in a DataSet (if not NULL is
|
||||
* passed).
|
||||
* \details It DOES NOT fetch the current value from the data pool, but sets the value
|
||||
* attribute to default (0). The value is fetched within the read() operation.
|
||||
* \param set_id This is the id in the global data pool this instance of the access class
|
||||
* corresponds to.
|
||||
* \param dataSet The data set in which the variable shall register itself. If NULL,
|
||||
* the variable is not registered.
|
||||
* \param setWritable If this flag is set to true, changes in the value attribute can be
|
||||
* written back to the data pool, otherwise not.
|
||||
*/
|
||||
PoolVariable(uint32_t set_id, DataSetIF* dataSet,
|
||||
ReadWriteMode_t setReadWriteMode) :
|
||||
dataPoolId(set_id), valid(PoolVariableIF::INVALID), readWriteMode(
|
||||
setReadWriteMode), value(0) {
|
||||
if (dataSet != NULL) {
|
||||
dataSet->registerVariable(this);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Copy ctor to copy classes containing Pool Variables.
|
||||
*/
|
||||
PoolVariable(const PoolVariable& rhs) :
|
||||
dataPoolId(rhs.dataPoolId), valid(rhs.valid), readWriteMode(
|
||||
rhs.readWriteMode), value(rhs.value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief The classes destructor is empty.
|
||||
* \details If commit() was not called, the local value is
|
||||
* discarded and not written back to the data pool.
|
||||
*/
|
||||
~PoolVariable() {
|
||||
|
||||
}
|
||||
/**
|
||||
* \brief This operation returns the data pool id of the variable.
|
||||
*/
|
||||
uint32_t getDataPoolId() const {
|
||||
return dataPoolId;
|
||||
}
|
||||
/**
|
||||
* This operation sets the data pool id of the variable.
|
||||
* The method is necessary to set id's of data pool member variables with bad initialization.
|
||||
*/
|
||||
void setDataPoolId(uint32_t poolId) {
|
||||
dataPoolId = poolId;
|
||||
}
|
||||
/**
|
||||
* This method returns if the variable is write-only, read-write or read-only.
|
||||
*/
|
||||
ReadWriteMode_t getReadWriteMode() const {
|
||||
return readWriteMode;
|
||||
}
|
||||
/**
|
||||
* \brief With this call, the valid information of the variable is returned.
|
||||
*/
|
||||
bool isValid() const {
|
||||
if (valid)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t getValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
void setValid(uint8_t valid) {
|
||||
this->valid = valid;
|
||||
}
|
||||
|
||||
operator T() {
|
||||
return value;
|
||||
}
|
||||
|
||||
operator T() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
PoolVariable<T> &operator=(T newValue) {
|
||||
value = newValue;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PoolVariable<T> &operator=(PoolVariable<T> newPoolVariable) {
|
||||
value = newPoolVariable.value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
return SerializeAdapter<T>::serialize(&value, buffer, size, max_size,
|
||||
bigEndian);
|
||||
}
|
||||
|
||||
virtual uint32_t getSerializedSize() const {
|
||||
return SerializeAdapter<T>::getSerializedSize(&value);
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
return SerializeAdapter<T>::deSerialize(&value, buffer, size, bigEndian);
|
||||
}
|
||||
};
|
||||
|
||||
typedef PoolVariable<uint8_t> db_uint8_t;
|
||||
typedef PoolVariable<uint16_t> db_uint16_t;
|
||||
typedef PoolVariable<uint32_t> db_uint32_t;
|
||||
typedef PoolVariable<int8_t> db_int8_t;
|
||||
typedef PoolVariable<int16_t> db_int16_t;
|
||||
typedef PoolVariable<int32_t> db_int32_t;
|
||||
typedef PoolVariable<uint8_t> db_bool_t;
|
||||
typedef PoolVariable<float> db_float_t;
|
||||
typedef PoolVariable<double> db_double_t;
|
||||
//Alternative (but I thing this is not as useful: code duplication, differences too small):
|
||||
|
||||
//template <typename T>
|
||||
//class PoolReader : public PoolVariableIF {
|
||||
//private:
|
||||
// uint32_t parameter_id;
|
||||
// uint8_t valid;
|
||||
//public:
|
||||
// T value;
|
||||
// PoolReader( uint32_t set_id, DataSetIF* set ) : parameter_id(set_id), valid(false), value(0) {
|
||||
// set->registerVariable( this );
|
||||
// }
|
||||
//
|
||||
// ~PoolReader() {};
|
||||
//
|
||||
// uint8_t commit() {
|
||||
// return HasReturnvaluesIF::RETURN_OK;
|
||||
// }
|
||||
//
|
||||
// uint8_t read() {
|
||||
// PoolEntry<T>* read_out = ::dataPool.getData<T>( parameter_id, 1 );
|
||||
// if ( read_out != NULL ) {
|
||||
// valid = read_out->valid;
|
||||
// value = *(read_out->address);
|
||||
// return HasReturnvaluesIF::RETURN_OK;
|
||||
// } else {
|
||||
// value = 0;
|
||||
// valid = false;
|
||||
// return CHECKOUT_FAILED;
|
||||
// }
|
||||
// }
|
||||
// uint32_t getParameterId() { return parameter_id; }
|
||||
// bool isWritable() { return false; };
|
||||
// bool isValid() { if (valid) return true; else return false; }
|
||||
//};
|
||||
//
|
||||
//template <typename T>
|
||||
//class PoolWriter : public PoolVariableIF {
|
||||
//private:
|
||||
// uint32_t parameter_id;
|
||||
//public:
|
||||
// T value;
|
||||
// PoolWriter( uint32_t set_id, DataSetIF* set ) : parameter_id(set_id), value(0) {
|
||||
// set->registerVariable( this );
|
||||
// }
|
||||
//
|
||||
// ~PoolWriter() {};
|
||||
//
|
||||
// uint8_t commit() {
|
||||
// PoolEntry<T>* write_back = ::dataPool.getData<T>( parameter_id, 1 );
|
||||
// if ( write_back != NULL ) {
|
||||
// write_back->valid = true;
|
||||
// *(write_back->address) = value;
|
||||
// return HasReturnvaluesIF::RETURN_OK;
|
||||
// } else {
|
||||
// return CHECKOUT_FAILED;
|
||||
// }
|
||||
// }
|
||||
// uint8_t read() {
|
||||
// PoolEntry<T>* read_out = ::dataPool.getData<T>( parameter_id, 1 );
|
||||
// if ( read_out != NULL ) {
|
||||
// value = *(read_out->address);
|
||||
// return HasReturnvaluesIF::RETURN_OK;
|
||||
// } else {
|
||||
// value = 0;
|
||||
// return CHECKOUT_FAILED;
|
||||
// }
|
||||
// }
|
||||
// uint32_t getParameterId() { return parameter_id; }
|
||||
// bool isWritable() { return true; };
|
||||
// bool isValid() { return false; }
|
||||
//};
|
||||
|
||||
#endif /* POOLVARIABLE_H_ */
|
71
datapool/PoolVariableIF.h
Normal file
71
datapool/PoolVariableIF.h
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* \file PoolVariableIF.h
|
||||
*
|
||||
* \brief This file contains the interface definition for pool variables.
|
||||
*
|
||||
* \date 10/17/2012
|
||||
*
|
||||
* \author Bastian Baetz
|
||||
*/
|
||||
|
||||
#ifndef POOLVARIABLEIF_H_
|
||||
#define POOLVARIABLEIF_H_
|
||||
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
#include <framework/serialize/SerializeIF.h>
|
||||
|
||||
/**
|
||||
* \brief This interface is used to control local data pool variable representations.
|
||||
*
|
||||
* \details To securely handle data pool variables, all pool entries are locally managed by
|
||||
* data pool variable access classes, which are called pool variables. To ensure a
|
||||
* common state of a set of variables needed in a function, these local pool variables
|
||||
* again are managed by other classes, e.g. the DataSet. This interface provides unified
|
||||
* access to local pool variables for such manager classes.
|
||||
* \ingroup data_pool
|
||||
*/
|
||||
class PoolVariableIF : public SerializeIF {
|
||||
friend class DataSet;
|
||||
protected:
|
||||
/**
|
||||
* \brief The commit call shall write back a newly calculated local value to the data pool.
|
||||
*/
|
||||
virtual ReturnValue_t commit() = 0;
|
||||
/**
|
||||
* \brief The read call shall read the value of this parameter from the data pool and store
|
||||
* the content locally.
|
||||
*/
|
||||
virtual ReturnValue_t read() = 0;
|
||||
public:
|
||||
static const uint8_t VALID = 1;
|
||||
static const uint8_t INVALID = 0;
|
||||
static const uint32_t NO_PARAMETER = 0;
|
||||
enum ReadWriteMode_t {
|
||||
VAR_READ, VAR_WRITE, VAR_READ_WRITE
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief This is an empty virtual destructor, as it is proposed for C++ interfaces.
|
||||
*/
|
||||
virtual ~PoolVariableIF() {
|
||||
}
|
||||
/**
|
||||
* \brief This method returns if the variable is write-only, read-write or read-only.
|
||||
*/
|
||||
virtual ReadWriteMode_t getReadWriteMode() const = 0;
|
||||
/**
|
||||
* \brief This operation shall return the data pool id of the variable.
|
||||
*/
|
||||
virtual uint32_t getDataPoolId() const = 0;
|
||||
/**
|
||||
* \brief With this call, the valid information of the variable is returned.
|
||||
*/
|
||||
virtual bool isValid() const = 0;
|
||||
/**
|
||||
* \brief With this call, the valid information of the variable is set.
|
||||
*/
|
||||
virtual void setValid(uint8_t validity) = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif /* POOLVARIABLEIF_H_ */
|
233
datapool/PoolVector.h
Normal file
233
datapool/PoolVector.h
Normal file
@ -0,0 +1,233 @@
|
||||
/*
|
||||
* \file PoolVector.h
|
||||
*
|
||||
* \brief This file contains the PoolVector class, the header only class to handle data pool vectors.
|
||||
*
|
||||
* \date 10/23/2012
|
||||
*
|
||||
* \author Bastian Baetz
|
||||
*/
|
||||
|
||||
#ifndef POOLVECTOR_H_
|
||||
#define POOLVECTOR_H_
|
||||
|
||||
#include <framework/datapool/DataSetIF.h>
|
||||
#include <framework/datapool/PoolEntry.h>
|
||||
#include <framework/datapool/PoolVariableIF.h>
|
||||
#include <framework/serialize/SerializeAdapter.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
/**
|
||||
* \brief This is the access class for array-type data pool entries.
|
||||
*
|
||||
* \details To ensure safe usage of the data pool, operation is not done directly on the data pool
|
||||
* entries, but on local copies. This class provides simple type- and length-safe access
|
||||
* to vector-style data pool entries (i.e. entries with length > 1).
|
||||
* The class can be instantiated as read-write and read only.
|
||||
* It provides a commit-and-roll-back semantic, which means that no array entry in
|
||||
* the data pool is changed until the commit call is executed.
|
||||
* There are two template parameters:
|
||||
* \tparam T This template parameter specifies the data type of an array entry. Currently, all
|
||||
* plain data types are supported, but in principle any type is possible.
|
||||
* \tparam vector_size This template parameter specifies the vector size of this entry.
|
||||
* Using a template parameter for this is not perfect, but avoids dynamic memory allocation.
|
||||
* \ingroup data_pool
|
||||
*/
|
||||
template<typename T, uint16_t vector_size>
|
||||
class PoolVector: public PoolVariableIF {
|
||||
private:
|
||||
/**
|
||||
* \brief To access the correct data pool entry on read and commit calls, the data pool id
|
||||
* is stored.
|
||||
*/
|
||||
uint32_t dataPoolId;
|
||||
/**
|
||||
* \brief The valid information as it was stored in the data pool is copied to this attribute.
|
||||
*/
|
||||
uint8_t valid;
|
||||
/**
|
||||
* \brief The information whether the class is read-write or read-only is stored here.
|
||||
*/
|
||||
ReadWriteMode_t readWriteMode;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* \brief This is a call to read the array's values from the global data pool.
|
||||
* \details When executed, this operation tries to fetch the pool entry with matching
|
||||
* data pool id from the global data pool and copies all array values and the valid
|
||||
* information to its local attributes. In case of a failure (wrong type, size or
|
||||
* pool id not found), the variable is set to zero and invalid.
|
||||
* The operation does NOT provide any mutual exclusive protection by itself.
|
||||
*/
|
||||
ReturnValue_t read() {
|
||||
PoolEntry<T>* read_out = ::dataPool.getData<T>(this->dataPoolId,
|
||||
vector_size);
|
||||
if (read_out != NULL) {
|
||||
this->valid = read_out->valid;
|
||||
memcpy(this->value, read_out->address, read_out->getByteSize());
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
|
||||
} else {
|
||||
memset(this->value, 0, vector_size * sizeof(T));
|
||||
error << "PoolVector: read of DP Variable 0x" << std::hex
|
||||
<< dataPoolId << std::dec << " failed." << std::endl;
|
||||
this->valid = INVALID;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* \brief The commit call copies the array values back to the data pool.
|
||||
* \details It checks type and size, as well as if the variable is writable. If so,
|
||||
* the value is copied and the valid flag is automatically set to "valid".
|
||||
* The operation does NOT provide any mutual exclusive protection by itself.
|
||||
*
|
||||
*/
|
||||
ReturnValue_t commit() {
|
||||
PoolEntry<T>* write_back = ::dataPool.getData<T>(this->dataPoolId,
|
||||
vector_size);
|
||||
if ((write_back != NULL) && (this->readWriteMode != VAR_READ)) {
|
||||
write_back->valid = valid;
|
||||
memcpy(write_back->address, this->value, write_back->getByteSize());
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* \brief This is the local copy of the data pool entry.
|
||||
* \detials The user can work on this attribute
|
||||
* just like he would on a local array of this type.
|
||||
*/
|
||||
T value[vector_size];
|
||||
/**
|
||||
* \brief In the constructor, the variable can register itself in a DataSet (if not NULL is
|
||||
* passed).
|
||||
* \details It DOES NOT fetch the current value from the data pool, but sets the value
|
||||
* attribute to default (0). The value is fetched within the read() operation.
|
||||
* \param set_id This is the id in the global data pool this instance of the access class
|
||||
* corresponds to.
|
||||
* \param dataSet The data set in which the variable shall register itself. If NULL,
|
||||
* the variable is not registered.
|
||||
* \param setWritable If this flag is set to true, changes in the value attribute can be
|
||||
* written back to the data pool, otherwise not.
|
||||
*/
|
||||
PoolVector(uint32_t set_id, DataSetIF* set,
|
||||
ReadWriteMode_t setReadWriteMode) :
|
||||
dataPoolId(set_id), valid(false), readWriteMode(setReadWriteMode) {
|
||||
memset(this->value, 0, vector_size * sizeof(T));
|
||||
if (set != NULL) {
|
||||
set->registerVariable(this);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Copy ctor to copy classes containing Pool Variables.
|
||||
*/
|
||||
// PoolVector(const PoolVector& rhs) {
|
||||
// PoolVector<T, vector_size> temp(rhs.dataPoolId, rhs.)
|
||||
// memcpy(value, rhs.value, sizeof(T)*vector_size);
|
||||
// }
|
||||
/**
|
||||
* \brief The classes destructor is empty.
|
||||
* \details If commit() was not called, the local value is
|
||||
* discarded and not written back to the data pool.
|
||||
*/
|
||||
~PoolVector() {
|
||||
}
|
||||
;
|
||||
/**
|
||||
* \brief The operation returns the number of array entries in this variable.
|
||||
*/
|
||||
uint8_t getSize() {
|
||||
return vector_size;
|
||||
}
|
||||
/**
|
||||
* \brief This operation returns the data pool id of the variable.
|
||||
*/
|
||||
uint32_t getDataPoolId() const {
|
||||
return dataPoolId;
|
||||
}
|
||||
/**
|
||||
* This operation sets the data pool id of the variable.
|
||||
* The method is necessary to set id's of data pool member variables with bad initialization.
|
||||
*/
|
||||
void setDataPoolId(uint32_t poolId) {
|
||||
dataPoolId = poolId;
|
||||
}
|
||||
/**
|
||||
* This method returns if the variable is write-only, read-write or read-only.
|
||||
*/
|
||||
ReadWriteMode_t getReadWriteMode() const {
|
||||
return readWriteMode;
|
||||
}
|
||||
;
|
||||
/**
|
||||
* \brief With this call, the valid information of the variable is returned.
|
||||
*/
|
||||
bool isValid() const {
|
||||
if (valid != INVALID)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void setValid(uint8_t valid) {
|
||||
this->valid = valid;
|
||||
}
|
||||
|
||||
uint8_t getValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
T &operator [](int i) {
|
||||
return value[i];
|
||||
}
|
||||
|
||||
const T &operator [](int i) const {
|
||||
return value[i];
|
||||
}
|
||||
|
||||
PoolVector<T, vector_size> &operator=(
|
||||
PoolVector<T, vector_size> newPoolVector) {
|
||||
|
||||
for (uint16_t i = 0; i < vector_size; i++) {
|
||||
this->value[i] = newPoolVector.value[i];
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
uint16_t i;
|
||||
ReturnValue_t result;
|
||||
for (i = 0; i < vector_size; i++) {
|
||||
result = SerializeAdapter<T>::serialize(&(value[i]), buffer, size,
|
||||
max_size, bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
virtual uint32_t getSerializedSize() const {
|
||||
return vector_size * SerializeAdapter<T>::getSerializedSize(value);
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
uint16_t i;
|
||||
ReturnValue_t result;
|
||||
for (i = 0; i < vector_size; i++) {
|
||||
result = SerializeAdapter<T>::deSerialize(&(value[i]), buffer, size,
|
||||
bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* POOLVECTOR_H_ */
|
Reference in New Issue
Block a user