Merge branch 'mueller_framework' into front_branch

This commit is contained in:
Robin Müller 2020-05-15 21:10:56 +02:00
commit abdf04ce79
36 changed files with 1047 additions and 461 deletions

View File

@ -1,9 +1,9 @@
#include <framework/action/ActionHelper.h>
#include <framework/action/HasActionsIF.h>
#include <framework/objectmanager/ObjectManagerIF.h>
ActionHelper::ActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue) :
owner(setOwner), queueToUse(useThisQueue), ipcStore(
NULL) {
owner(setOwner), queueToUse(useThisQueue), ipcStore(nullptr) {
}
ActionHelper::~ActionHelper() {
@ -22,10 +22,12 @@ ReturnValue_t ActionHelper::handleActionMessage(CommandMessage* command) {
ReturnValue_t ActionHelper::initialize(MessageQueueIF* queueToUse_) {
ipcStore = objectManager->get<StorageManagerIF>(objects::IPC_STORE);
if (ipcStore == NULL) {
if (ipcStore == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
if(queueToUse_ != nullptr) {
setQueueToUse(queueToUse_);
}
return HasReturnvaluesIF::RETURN_OK;
}

View File

@ -38,7 +38,7 @@ public:
* @param queueToUse_ Pointer to the messageQueue to be used
* @return Returns RETURN_OK if successful
*/
ReturnValue_t initialize(MessageQueueIF* queueToUse_);
ReturnValue_t initialize(MessageQueueIF* queueToUse_ = nullptr);
/**
* Function to be called from the owner to send a step message. Success or failure will be determined by the result value.
*

View File

@ -70,13 +70,8 @@ public:
}
ReturnValue_t pop() {
if(empty()) {
return EMPTY;
} else {
readIndex = next(readIndex);
--currentSize;
return HasReturnvaluesIF::RETURN_OK;
}
T value;
return this->retrieve(&value);
}
static const uint8_t INTERFACE_ID = CLASS_ID::FIFO_CLASS;

View File

@ -13,15 +13,11 @@
#include <framework/devicehandlers/DeviceHandlerBase.h>
/**
* @brief Used to pass communication information between tasks
* @brief Message type to send larger messages
*
* @details
* Can be used to pass information like data pointers and
* data sizes between communication tasks
* like the Device Handler Comm Interfaces and Polling Tasks.
*
* Can also be used to exchange actual data if its not too large
* (e.g. Sensor values).
* data sizes between communication tasks.
*
*/
class CommunicationMessage: public MessageQueueMessage {

View File

@ -16,6 +16,5 @@ FixedSequenceSlot::FixedSequenceSlot(object_id_t handlerId, uint32_t setTime,
handler->setTaskIF(executingTask);
}
FixedSequenceSlot::~FixedSequenceSlot() {
}
FixedSequenceSlot::~FixedSequenceSlot() {}

View File

@ -13,9 +13,10 @@
class PeriodicTaskIF;
/**
* \brief This class is the representation of a single polling sequence table entry.
* @brief This class is the representation of a single polling sequence table entry.
*
* \details The PollingSlot class is the representation of a single polling sequence table entry.
* @details The PollingSlot class is the representation of a single polling
* sequence table entry.
*/
class FixedSequenceSlot {
public:
@ -37,13 +38,19 @@ public:
uint32_t pollingTimeMs;
/**
* \brief This value defines the type of device communication.
* @brief This value defines the type of device communication.
*
* \details The state of this value decides what communication routine is
* @details The state of this value decides what communication routine is
* called in the PST executable or the device handler object.
*/
uint8_t opcode;
/**
* @brief Operator overload for the comparison operator to
* allow sorting by polling time.
* @param fixedSequenceSlot
* @return
*/
bool operator <(const FixedSequenceSlot & fixedSequenceSlot) const {
return pollingTimeMs < fixedSequenceSlot.pollingTimeMs;
}

View File

@ -8,14 +8,8 @@ FixedSlotSequence::FixedSlotSequence(uint32_t setLengthMs) :
}
FixedSlotSequence::~FixedSlotSequence() {
// This should call the destructor on each list entry.
// Call the destructor on each list entry.
slotList.clear();
// SlotListIter slotListIter = this->slotList.begin();
// //Iterate through slotList and delete all entries.
// while (slotListIter != this->slotList.end()) {
// delete (*slotIt);
// slotIt++;
// }
}
void FixedSlotSequence::executeAndAdvance() {
@ -92,11 +86,12 @@ ReturnValue_t FixedSlotSequence::checkSequence() const {
sif::error << "Fixed Slot Sequence: Slot list is empty!" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
auto slotIt = slotList.begin();
uint32_t count = 0;
uint32_t time = 0;
while (slotIt != slotList.end()) {
if (slotIt->handler == NULL) {
if (slotIt->handler == nullptr) {
sif::error << "FixedSlotSequene::initialize: ObjectId does not exist!"
<< std::endl;
count++;

View File

@ -3,8 +3,7 @@
#include <framework/devicehandlers/FixedSequenceSlot.h>
#include <framework/objectmanager/SystemObject.h>
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <list>
#include <set>
using SlotList = std::multiset<FixedSequenceSlot>;
@ -12,7 +11,6 @@ using SlotListIter = std::multiset<FixedSequenceSlot>::iterator;
/**
* @brief This class is the representation of a Polling Sequence Table in software.
*
* @details
* The FixedSlotSequence object maintains the dynamic execution of
* device handler objects.
@ -73,9 +71,10 @@ public:
bool slotFollowsImmediately();
/**
* \brief This method returns the time until the next software component is invoked.
* @brief This method returns the time until the next software
* component is invoked.
*
* \details
* @details
* This method is vitally important for the operation of the PST.
* By fetching the polling time of the current slot and that of the
* next one (or the first one, if the list end is reached)
@ -87,11 +86,15 @@ public:
uint32_t getIntervalToNextSlotMs();
/**
* \brief This method returns the time difference between the current slot and the previous slot
* @brief This method returns the time difference between the current
* slot and the previous slot
*
* \details This method is vitally important for the operation of the PST. By fetching the polling time
* of the current slot and that of the prevous one (or the last one, if the slot is the first one)
* it calculates and returns the interval in milliseconds that the handler execution shall be delayed.
* @details
* This method is vitally important for the operation of the PST.
* By fetching the polling time of the current slot and that of the previous
* one (or the last one, if the slot is the first one) it calculates and
* returns the interval in milliseconds that the handler execution shall
* be delayed.
*/
uint32_t getIntervalToPreviousSlotMs();
@ -101,12 +104,15 @@ public:
uint32_t getLengthMs() const;
/**
* \brief The method to execute the device handler entered in the current PollingSlot object.
* @brief The method to execute the device handler entered in the current
* PollingSlot object.
*
* \details Within this method the device handler object to be executed is chosen by looking up the
* handler address of the current slot in the handlerMap. Either the device handler's
* talkToInterface or its listenToInterface method is invoked, depending on the isTalking flag
* of the polling slot. After execution the iterator current is increased or, by reaching the
* @details
* Within this method the device handler object to be executed is chosen by
* looking up the handler address of the current slot in the handlerMap.
* Either the device handler's talkToInterface or its listenToInterface
* method is invoked, depending on the isTalking flag of the polling slot.
* After execution the iterator current is increased or, by reaching the
* end of slotList, reset to the beginning.
*/
void executeAndAdvance();
@ -114,7 +120,8 @@ public:
/**
* @brief An iterator that indicates the current polling slot to execute.
*
* @details This is an iterator for slotList and always points to the polling slot which is executed next.
* @details This is an iterator for slotList and always points to the
* polling slot which is executed next.
*/
SlotListIter current;
@ -124,16 +131,19 @@ public:
* @return
*/
ReturnValue_t checkSequence() const;
protected:
/**
* @brief This list contains all PollingSlot objects, defining order and execution time of the
* device handler objects.
* @brief This list contains all PollingSlot objects, defining order and
* execution time of the device handler objects.
*
* @details The slot list is a std:list object that contains all created PollingSlot instances.
* They are NOT ordered automatically, so by adding entries, the correct order needs to be ensured.
* By iterating through this list the polling sequence is executed. Two entries with identical
* polling times are executed immediately one after another.
* @details
* The slot list is a std:list object that contains all created
* PollingSlot instances. They are NOT ordered automatically, so by
* adding entries, the correct order needs to be ensured. By iterating
* through this list the polling sequence is executed. Two entries with
* identical polling times are executed immediately one after another.
*/
SlotList slotList;

View File

@ -0,0 +1,36 @@
#include <framework/globalfunctions/printer.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
void printer::print(uint8_t *data, size_t size, OutputType type) {
sif::info << "Printing data with size " << size << ": [";
if(type == OutputType::HEX) {
printer::printHex(data, size);
}
else {
printer::printDec(data, size);
}
}
void printer::printHex(uint8_t *data, size_t size) {
sif::info << std::hex;
for(size_t i = 0; i < size; i++) {
sif::info << "0x" << static_cast<int>(data[i]);
if(i < size - 1){
sif::info << " , ";
}
}
sif::info << std::dec;
sif::info << "]" << std::endl;
}
void printer::printDec(uint8_t *data, size_t size) {
sif::info << std::dec;
for(size_t i = 0; i < size; i++) {
sif::info << "0x" << static_cast<int>(data[i]);
if(i < size - 1){
sif::info << " , ";
}
}
sif::info << "]" << std::endl;
}

18
globalfunctions/printer.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef FRAMEWORK_GLOBALFUNCTIONS_PRINTER_H_
#define FRAMEWORK_GLOBALFUNCTIONS_PRINTER_H_
#include <cstdint>
#include <cstddef>
namespace printer {
enum class OutputType {
DEC,
HEX
};
void print(uint8_t* data, size_t size, OutputType type = OutputType::HEX);
void printHex(uint8_t* data, size_t size);
void printDec(uint8_t* data, size_t size);
}
#endif /* FRAMEWORK_GLOBALFUNCTIONS_PRINTER_H_ */

View File

@ -14,7 +14,8 @@ enum FW_MESSAGE_TYPE {
MONITORING,
MEMORY,
PARAMETER,
FW_MESSAGES_COUNT
FW_MESSAGES_COUNT,
FILE_SYSTEM_MESSAGE
};
}

View File

@ -10,11 +10,11 @@ public:
internalMutex(mutex) {
ReturnValue_t status = mutex->lockMutex(timeoutMs);
if(status != HasReturnvaluesIF::RETURN_OK){
sif::error << "MutexHelper: Lock of Mutex failed " << status << std::endl;
sif::error << "MutexHelper: Lock of Mutex failed " <<
status << std::endl;
}
}
~MutexHelper() {
internalMutex->unlockMutex();
}

View File

@ -0,0 +1,28 @@
/*
* FileSystemMessage.cpp
*
* Created on: 19.01.2020
* Author: Jakob Meier
*/
#include "FileSystemMessage.h"
#include <framework/objectmanager/ObjectManagerIF.h>
ReturnValue_t FileSystemMessage::setWriteToFileCommand(CommandMessage* message,
MessageQueueId_t replyQueueId, store_address_t storageID) {
message->setCommand(WRITE_TO_FILE);
message->setParameter(replyQueueId);
message->setParameter2(storageID.raw);
return HasReturnvaluesIF::RETURN_OK;
}
store_address_t FileSystemMessage::getStoreID(const CommandMessage* message) {
store_address_t temp;
temp.raw = message->getParameter2();
return temp;
}
MessageQueueId_t FileSystemMessage::getReplyQueueId(const CommandMessage* message){
return message->getParameter();
}

View File

@ -0,0 +1,30 @@
/*
* FileSystemMessage.h
*
* Created on: 19.01.2020
* Author: Jakob Meier
*/
#ifndef FRAMEWORK_MEMORY_FILESYSTEMMESSAGE_H_
#define FRAMEWORK_MEMORY_FILESYSTEMMESSAGE_H_
#include <framework/ipc/CommandMessage.h>
#include <framework/storagemanager/StorageManagerIF.h>
#include <framework/objectmanager/SystemObject.h>
class FileSystemMessage {
private:
FileSystemMessage(); //A private ctor inhibits instantiation
public:
static const uint8_t MESSAGE_ID = MESSAGE_TYPE::FILE_SYSTEM_MESSAGE;
static const Command_t CREATE_FILE = MAKE_COMMAND_ID( 0x01 );
static const Command_t DELETE_FILE = MAKE_COMMAND_ID( 0x02 );
static const Command_t WRITE_TO_FILE = MAKE_COMMAND_ID( 0x80 );
static ReturnValue_t setWriteToFileCommand(CommandMessage* message, MessageQueueId_t replyToQueue, store_address_t storageID );
static store_address_t getStoreID( const CommandMessage* message );
static MessageQueueId_t getReplyQueueId(const CommandMessage* message);
};
#endif /* FRAMEWORK_MEMORY_FILESYSTEMMESSAGE_H_ */

36
memory/HasFileSystemIF.h Normal file
View File

@ -0,0 +1,36 @@
/*
* HasFileSystemIF.h
*
* Created on: 19.01.2020
* Author: Jakob Meier
*/
#ifndef FRAMEWORK_MEMORY_HASFILESYSTEMIF_H_
#define FRAMEWORK_MEMORY_HASFILESYSTEMIF_H_
#include <framework/returnvalues/HasReturnvaluesIF.h>
class HasFileSystemIF {
public:
virtual ~HasFileSystemIF() {}
/**
* Function to get the MessageQueueId_t of the implementing object
* @return MessageQueueId_t of the object
*/
virtual MessageQueueId_t getCommandQueue() const = 0;
/**
* Function to write to a file
* @param dirname Directory of the file
* @param filename The filename of the file
* @param data The data to write to the file
* @param size The size of the data to write
* @param packetNumber Counts the number of packets. For large files the write procedure must be split in multiple calls to writeToFile
*/
virtual ReturnValue_t writeToFile(const char* dirname, char* filename, const uint8_t* data, uint32_t size, uint16_t packetNumber) = 0;
virtual ReturnValue_t createFile(const char* dirname, const char* filename, const uint8_t* data, uint32_t size) = 0;
virtual ReturnValue_t deleteFile(const char* dirname, const char* filename) = 0;
};
#endif /* FRAMEWORK_MEMORY_HASFILESYSTEMIF_H_ */

View File

@ -31,12 +31,11 @@ ReturnValue_t MemoryMessage::setMemoryDumpReply(CommandMessage* message, store_a
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t MemoryMessage::setMemoryLoadCommand(CommandMessage* message,
void MemoryMessage::setMemoryLoadCommand(CommandMessage* message,
uint32_t address, store_address_t storageID) {
message->setCommand(CMD_MEMORY_LOAD);
message->setParameter( address );
message->setParameter2( storageID.raw );
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t MemoryMessage::getErrorCode(const CommandMessage* message) {

View File

@ -24,7 +24,7 @@ public:
static ReturnValue_t getErrorCode( const CommandMessage* message );
static ReturnValue_t setMemoryDumpCommand( CommandMessage* message, uint32_t address, uint32_t length );
static ReturnValue_t setMemoryDumpReply( CommandMessage* message, store_address_t storageID );
static ReturnValue_t setMemoryLoadCommand( CommandMessage* message, uint32_t address, store_address_t storageID );
static void setMemoryLoadCommand( CommandMessage* message, uint32_t address, store_address_t storageID );
static ReturnValue_t setMemoryCheckCommand( CommandMessage* message, uint32_t address, uint32_t length );
static ReturnValue_t setMemoryCheckReply( CommandMessage* message, uint16_t crc );
static ReturnValue_t setMemoryReplyFailed( CommandMessage* message, ReturnValue_t errorCode, Command_t initialCommand );

View File

@ -57,6 +57,10 @@ ReturnValue_t FixedTimeslotTask::startTask() {
ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId,
uint32_t slotTimeMs, int8_t executionStep) {
<<<<<<< HEAD
=======
>>>>>>> mueller_framework
if (objectManager->get<ExecutableObjectIF>(componentId) != nullptr) {
if(slotTimeMs == 0) {
// FreeRTOS throws a sanity error for zero values, so we set

View File

@ -120,16 +120,16 @@ ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo,
message->setSender(sentFrom);
BaseType_t result;
if(callContext == CallContext::task) {
result = xQueueSendToBack(reinterpret_cast<void*>(sendTo),
reinterpret_cast<const void*>(message->getBuffer()), 0);
result = xQueueSendToBack(reinterpret_cast<QueueHandle_t>(sendTo),
static_cast<const void*>(message->getBuffer()), 0);
}
else {
// If the call context is from an interrupt,
// request a context switch if a higher priority task
// was blocked by the interrupt.
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
result = xQueueSendFromISR(reinterpret_cast<void*>(sendTo),
reinterpret_cast<const void*>(message->getBuffer()),
result = xQueueSendFromISR(reinterpret_cast<QueueHandle_t>(sendTo),
static_cast<const void*>(message->getBuffer()),
&xHigherPriorityTaskWoken);
if(xHigherPriorityTaskWoken == pdTRUE) {
TaskManagement::requestContextSwitch(callContext);

View File

@ -6,8 +6,11 @@
#include <framework/ipc/MessageQueueMessage.h>
#include <framework/osal/FreeRTOS/TaskManagement.h>
#include <FreeRTOS.h>
#include "queue.h"
extern "C" {
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
}
//TODO this class assumes that MessageQueueId_t is the same size as void* (the FreeRTOS handle type), compiler will catch this but it might be nice to have something checking or even an always working solution

View File

@ -3,9 +3,11 @@
#include <framework/ipc/MutexIF.h>
extern "C" {
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
}
#include <FreeRTOS.h>
#include "semphr.h"
/**
* @brief OS component to implement MUTual EXclusion

View File

@ -4,8 +4,8 @@
#include <framework/returnvalues/HasReturnvaluesIF.h>
extern "C" {
#include "FreeRTOS.h"
#include "task.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
}
#include <cstdint>

View File

@ -73,7 +73,7 @@ ReturnValue_t PollingTask::addSlot(object_id_t componentId,
return HasReturnvaluesIF::RETURN_OK;
}
error << "Component " << std::hex << componentId <<
sif::error << "Component " << std::hex << componentId <<
" not found, not adding it to pst" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}

View File

@ -1,21 +1,13 @@
#ifndef FRAMEWORK_STORAGEMANAGER_LOCALPOOL_H_
#define FRAMEWORK_STORAGEMANAGER_LOCALPOOL_H_
/**
* @file LocalPool
*
* @date 02.02.2012
* @author Bastian Baetz
*
* @brief This file contains the definition of the LocalPool class.
*/
#include <framework/objectmanager/SystemObject.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <framework/storagemanager/StorageManagerIF.h>
#include <framework/objectmanager/ObjectManagerIF.h>
#include <framework/internalError/InternalErrorReporterIF.h>
#include <string.h>
#include <framework/storagemanager/StorageAccessor.h>
#include <cstring>
/**
* @brief The LocalPool class provides an intermediate data storage with
@ -70,53 +62,28 @@ public:
virtual ~LocalPool(void);
/**
* Add data to local data pool, performs range check
* @param storageId [out] Store ID in which the data will be stored
* @param data
* @param size
* @param ignoreFault
* @return @c RETURN_OK if write was successful
* Documentation: See StorageManagerIF.h
*/
ReturnValue_t addData(store_address_t* storageId, const uint8_t * data,
uint32_t size, bool ignoreFault = false);
size_t size, bool ignoreFault = false) override;
ReturnValue_t getFreeElement(store_address_t* storageId,const size_t size,
uint8_t** p_data, bool ignoreFault = false) override;
/**
* With this helper method, a free element of \c size is reserved.
* @param storageId [out] storeID of the free element
* @param size The minimum packet size that shall be reserved.
* @param p_data [out] pointer to the pointer of free element
* @param ignoreFault
* @return Returns the storage identifier within the storage or
* StorageManagerIF::INVALID_ADDRESS (in raw).
*/
ReturnValue_t getFreeElement(store_address_t* storageId,
const uint32_t size, uint8_t** p_data, bool ignoreFault = false);
/**
* Retrieve data from local pool
* @param packet_id
* @param packet_ptr
* @param size [out] Size of retrieved data
* @return @c RETURN_OK if data retrieval was successfull
*/
ConstAccessorPair getData(store_address_t packet_id) override;
ReturnValue_t getData(store_address_t packet_id, ConstStorageAccessor&) override;
ReturnValue_t getData(store_address_t packet_id, const uint8_t** packet_ptr,
size_t * size);
size_t * size) override;
/**
* Modify data by supplying a packet pointer and using that packet pointer
* to access and modify the pool entry (via *pointer call)
* @param packet_id Store ID of data to modify
* @param packet_ptr [out] pointer to the pool entry to modify
* @param size [out] size of pool entry
* @return
*/
AccessorPair modifyData(store_address_t packet_id) override;
ReturnValue_t modifyData(store_address_t packet_id, StorageAccessor&) override;
ReturnValue_t modifyData(store_address_t packet_id, uint8_t** packet_ptr,
size_t * size);
virtual ReturnValue_t deleteData(store_address_t);
virtual ReturnValue_t deleteData(uint8_t* ptr, uint32_t size,
store_address_t* storeId = NULL);
void clearStore();
ReturnValue_t initialize();
size_t * size) override;
virtual ReturnValue_t deleteData(store_address_t) override;
virtual ReturnValue_t deleteData(uint8_t* ptr, size_t size,
store_address_t* storeId = NULL) override;
void clearStore() override;
ReturnValue_t initialize() override;
protected:
/**
* With this helper method, a free element of \c size is reserved.
@ -125,7 +92,8 @@ protected:
* @return - #RETURN_OK on success,
* - the return codes of #getPoolIndex or #findEmpty otherwise.
*/
virtual ReturnValue_t reserveSpace(const uint32_t size, store_address_t* address, bool ignoreFault);
virtual ReturnValue_t reserveSpace(const uint32_t size,
store_address_t* address, bool ignoreFault);
InternalErrorReporterIF *internalErrorReporter;
private:
@ -166,7 +134,7 @@ private:
* @param data The data to be stored.
* @param size The size of the data to be stored.
*/
void write(store_address_t packet_id, const uint8_t* data, uint32_t size);
void write(store_address_t packet_id, const uint8_t* data, size_t size);
/**
* @brief A helper method to read the element size of a certain pool.
* @param pool_index The pool in which to look.
@ -189,7 +157,8 @@ private:
* @return - #RETURN_OK on success,
* - #DATA_TOO_LARGE otherwise.
*/
ReturnValue_t getPoolIndex(uint32_t packet_size, uint16_t* poolIndex, uint16_t startAtIndex = 0);
ReturnValue_t getPoolIndex(size_t packet_size, uint16_t* poolIndex,
uint16_t startAtIndex = 0);
/**
* @brief This helper method calculates the true array position in store
* of a given packet id.
@ -211,249 +180,6 @@ private:
ReturnValue_t findEmpty(uint16_t pool_index, uint16_t* element);
};
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::findEmpty(uint16_t pool_index,
uint16_t* element) {
ReturnValue_t status = DATA_STORAGE_FULL;
for (uint16_t foundElement = 0; foundElement < n_elements[pool_index];
foundElement++) {
if (size_list[pool_index][foundElement] == STORAGE_FREE) {
*element = foundElement;
status = RETURN_OK;
break;
}
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline void LocalPool<NUMBER_OF_POOLS>::write(store_address_t packet_id,
const uint8_t* data, uint32_t size) {
uint8_t* ptr;
uint32_t packet_position = getRawPosition(packet_id);
//check size? -> Not necessary, because size is checked before calling this function.
ptr = &store[packet_id.pool_index][packet_position];
memcpy(ptr, data, size);
size_list[packet_id.pool_index][packet_id.packet_index] = size;
}
//Returns page size of 0 in case store_index is illegal
template<uint8_t NUMBER_OF_POOLS>
inline uint32_t LocalPool<NUMBER_OF_POOLS>::getPageSize(uint16_t pool_index) {
if (pool_index < NUMBER_OF_POOLS) {
return element_sizes[pool_index];
} else {
return 0;
}
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::getPoolIndex(
uint32_t packet_size, uint16_t* poolIndex, uint16_t startAtIndex) {
for (uint16_t n = startAtIndex; n < NUMBER_OF_POOLS; n++) {
// debug << "LocalPool " << getObjectId() << "::getPoolIndex: Pool: " << n << ", Element Size: " << element_sizes[n] << std::endl;
if (element_sizes[n] >= packet_size) {
*poolIndex = n;
return RETURN_OK;
}
}
return DATA_TOO_LARGE;
}
template<uint8_t NUMBER_OF_POOLS>
inline uint32_t LocalPool<NUMBER_OF_POOLS>::getRawPosition(
store_address_t packet_id) {
return packet_id.packet_index * element_sizes[packet_id.pool_index];
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::reserveSpace(
const uint32_t size, store_address_t* address, bool ignoreFault) {
ReturnValue_t status = getPoolIndex(size, &address->pool_index);
if (status != RETURN_OK) {
sif::error << "LocalPool( " << std::hex << getObjectId() << std::dec
<< " )::reserveSpace: Packet too large." << std::endl;
return status;
}
status = findEmpty(address->pool_index, &address->packet_index);
while (status != RETURN_OK && spillsToHigherPools) {
status = getPoolIndex(size, &address->pool_index, address->pool_index + 1);
if (status != RETURN_OK) {
//We don't find any fitting pool anymore.
break;
}
status = findEmpty(address->pool_index, &address->packet_index);
}
if (status == RETURN_OK) {
// if (getObjectId() == objects::IPC_STORE && address->pool_index >= 3) {
// debug << "Reserve: Pool: " << std::dec << address->pool_index << " Index: " << address->packet_index << std::endl;
// }
size_list[address->pool_index][address->packet_index] = size;
} else {
if (!ignoreFault) {
internalErrorReporter->storeFull();
}
sif::error << "LocalPool( " << std::hex << getObjectId() << std::dec
<< " )::reserveSpace: Packet store is full." << std::endl;
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline LocalPool<NUMBER_OF_POOLS>::LocalPool(object_id_t setObjectId,
const uint16_t element_sizes[NUMBER_OF_POOLS],
const uint16_t n_elements[NUMBER_OF_POOLS], bool registered, bool spillsToHigherPools) :
SystemObject(setObjectId, registered), internalErrorReporter(NULL), spillsToHigherPools(spillsToHigherPools){
for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) {
this->element_sizes[n] = element_sizes[n];
this->n_elements[n] = n_elements[n];
store[n] = new uint8_t[n_elements[n] * element_sizes[n]];
size_list[n] = new uint32_t[n_elements[n]];
memset(store[n], 0x00, (n_elements[n] * element_sizes[n]));
memset(size_list[n], STORAGE_FREE, (n_elements[n] * sizeof(**size_list))); //TODO checkme
}
}
template<uint8_t NUMBER_OF_POOLS>
inline LocalPool<NUMBER_OF_POOLS>::~LocalPool(void) {
for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) {
delete[] store[n];
delete[] size_list[n];
}
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::addData(
store_address_t* storageId, const uint8_t* data, uint32_t size, bool ignoreFault) {
ReturnValue_t status = reserveSpace(size, storageId, ignoreFault);
if (status == RETURN_OK) {
write(*storageId, data, size);
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::getFreeElement(
store_address_t* storageId, const uint32_t size, uint8_t** p_data, bool ignoreFault) {
ReturnValue_t status = reserveSpace(size, storageId, ignoreFault);
if (status == RETURN_OK) {
*p_data = &store[storageId->pool_index][getRawPosition(*storageId)];
} else {
*p_data = NULL;
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::getData(
store_address_t packet_id, const uint8_t** packet_ptr, size_t * size) {
uint8_t* tempData = NULL;
ReturnValue_t status = modifyData(packet_id, &tempData, size);
*packet_ptr = tempData;
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::modifyData(store_address_t packet_id,
uint8_t** packet_ptr, size_t * size) {
ReturnValue_t status = RETURN_FAILED;
if (packet_id.pool_index >= NUMBER_OF_POOLS) {
return ILLEGAL_STORAGE_ID;
}
if ((packet_id.packet_index >= n_elements[packet_id.pool_index])) {
return ILLEGAL_STORAGE_ID;
}
if (size_list[packet_id.pool_index][packet_id.packet_index]
!= STORAGE_FREE) {
uint32_t packet_position = getRawPosition(packet_id);
*packet_ptr = &store[packet_id.pool_index][packet_position];
*size = size_list[packet_id.pool_index][packet_id.packet_index];
status = RETURN_OK;
} else {
status = DATA_DOES_NOT_EXIST;
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::deleteData(
store_address_t packet_id) {
// if (getObjectId() == objects::IPC_STORE && packet_id.pool_index >= 3) {
// debug << "Delete: Pool: " << std::dec << packet_id.pool_index << " Index: " << packet_id.packet_index << std::endl;
// }
ReturnValue_t status = RETURN_OK;
uint32_t page_size = getPageSize(packet_id.pool_index);
if ((page_size != 0)
&& (packet_id.packet_index < n_elements[packet_id.pool_index])) {
uint16_t packet_position = getRawPosition(packet_id);
uint8_t* ptr = &store[packet_id.pool_index][packet_position];
memset(ptr, 0, page_size);
//Set free list
size_list[packet_id.pool_index][packet_id.packet_index] = STORAGE_FREE;
} else {
//pool_index or packet_index is too large
sif::error << "LocalPool:deleteData failed." << std::endl;
status = ILLEGAL_STORAGE_ID;
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline void LocalPool<NUMBER_OF_POOLS>::clearStore() {
for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) {
memset(size_list[n], STORAGE_FREE, (n_elements[n] * sizeof(**size_list)));//TODO checkme
}
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::deleteData(uint8_t* ptr,
uint32_t size, store_address_t* storeId) {
store_address_t localId;
ReturnValue_t result = ILLEGAL_ADDRESS;
for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) {
//Not sure if new allocates all stores in order. so better be careful.
if ((store[n] <= ptr) && (&store[n][n_elements[n]*element_sizes[n]]) > ptr) {
localId.pool_index = n;
uint32_t deltaAddress = ptr - store[n];
//Getting any data from the right "block" is ok. This is necessary, as IF's sometimes don't point to the first element of an object.
localId.packet_index = deltaAddress / element_sizes[n];
result = deleteData(localId);
// if (deltaAddress % element_sizes[n] != 0) {
// error << "Pool::deleteData: address not aligned!" << std::endl;
// }
break;
}
}
if (storeId != NULL) {
*storeId = localId;
}
return result;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::initialize() {
ReturnValue_t result = SystemObject::initialize();
if (result != RETURN_OK) {
return result;
}
internalErrorReporter = objectManager->get<InternalErrorReporterIF>(objects::INTERNAL_ERROR_REPORTER);
if (internalErrorReporter == NULL){
return RETURN_FAILED;
}
//Check if any pool size is large than the maximum allowed.
for (uint8_t count = 0; count < NUMBER_OF_POOLS; count++) {
if (element_sizes[count] >= STORAGE_FREE) {
sif::error
<< "LocalPool::initialize: Pool is too large! Max. allowed size is: "
<< (STORAGE_FREE - 1) << std::endl;
return RETURN_FAILED;
}
}
return RETURN_OK;
}
#include <framework/storagemanager/LocalPool.tpp>
#endif /* FRAMEWORK_STORAGEMANAGER_LOCALPOOL_H_ */

View File

@ -0,0 +1,300 @@
#ifndef LOCALPOOL_TPP
#define LOCALPOOL_TPP
template<uint8_t NUMBER_OF_POOLS>
inline LocalPool<NUMBER_OF_POOLS>::LocalPool(object_id_t setObjectId,
const uint16_t element_sizes[NUMBER_OF_POOLS],
const uint16_t n_elements[NUMBER_OF_POOLS], bool registered,
bool spillsToHigherPools) :
SystemObject(setObjectId, registered), internalErrorReporter(nullptr),
spillsToHigherPools(spillsToHigherPools)
{
for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) {
this->element_sizes[n] = element_sizes[n];
this->n_elements[n] = n_elements[n];
store[n] = new uint8_t[n_elements[n] * element_sizes[n]];
size_list[n] = new uint32_t[n_elements[n]];
memset(store[n], 0x00, (n_elements[n] * element_sizes[n]));
//TODO checkme
memset(size_list[n], STORAGE_FREE, (n_elements[n] * sizeof(**size_list)));
}
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::findEmpty(uint16_t pool_index,
uint16_t* element) {
ReturnValue_t status = DATA_STORAGE_FULL;
for (uint16_t foundElement = 0; foundElement < n_elements[pool_index];
foundElement++) {
if (size_list[pool_index][foundElement] == STORAGE_FREE) {
*element = foundElement;
status = RETURN_OK;
break;
}
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline void LocalPool<NUMBER_OF_POOLS>::write(store_address_t packet_id,
const uint8_t* data, size_t size) {
uint8_t* ptr;
uint32_t packet_position = getRawPosition(packet_id);
//check size? -> Not necessary, because size is checked before calling this function.
ptr = &store[packet_id.pool_index][packet_position];
memcpy(ptr, data, size);
size_list[packet_id.pool_index][packet_id.packet_index] = size;
}
//Returns page size of 0 in case store_index is illegal
template<uint8_t NUMBER_OF_POOLS>
inline uint32_t LocalPool<NUMBER_OF_POOLS>::getPageSize(uint16_t pool_index) {
if (pool_index < NUMBER_OF_POOLS) {
return element_sizes[pool_index];
} else {
return 0;
}
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::getPoolIndex(
size_t packet_size, uint16_t* poolIndex, uint16_t startAtIndex) {
for (uint16_t n = startAtIndex; n < NUMBER_OF_POOLS; n++) {
//debug << "LocalPool " << getObjectId() << "::getPoolIndex: Pool: " <<
// n << ", Element Size: " << element_sizes[n] << std::endl;
if (element_sizes[n] >= packet_size) {
*poolIndex = n;
return RETURN_OK;
}
}
return DATA_TOO_LARGE;
}
template<uint8_t NUMBER_OF_POOLS>
inline uint32_t LocalPool<NUMBER_OF_POOLS>::getRawPosition(
store_address_t packet_id) {
return packet_id.packet_index * element_sizes[packet_id.pool_index];
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::reserveSpace(
const uint32_t size, store_address_t* address, bool ignoreFault) {
ReturnValue_t status = getPoolIndex(size, &address->pool_index);
if (status != RETURN_OK) {
sif::error << "LocalPool( " << std::hex << getObjectId() << std::dec
<< " )::reserveSpace: Packet too large." << std::endl;
return status;
}
status = findEmpty(address->pool_index, &address->packet_index);
while (status != RETURN_OK && spillsToHigherPools) {
status = getPoolIndex(size, &address->pool_index, address->pool_index + 1);
if (status != RETURN_OK) {
//We don't find any fitting pool anymore.
break;
}
status = findEmpty(address->pool_index, &address->packet_index);
}
if (status == RETURN_OK) {
// if (getObjectId() == objects::IPC_STORE && address->pool_index >= 3) {
// debug << "Reserve: Pool: " << std::dec << address->pool_index <<
// " Index: " << address->packet_index << std::endl;
// }
size_list[address->pool_index][address->packet_index] = size;
} else {
if (!ignoreFault and internalErrorReporter != nullptr) {
internalErrorReporter->storeFull();
}
// error << "LocalPool( " << std::hex << getObjectId() << std::dec
// << " )::reserveSpace: Packet store is full." << std::endl;
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline LocalPool<NUMBER_OF_POOLS>::~LocalPool(void) {
for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) {
delete[] store[n];
delete[] size_list[n];
}
}
template<uint8_t NUMBER_OF_POOLS> inline
ReturnValue_t LocalPool<NUMBER_OF_POOLS>::addData(store_address_t* storageId,
const uint8_t* data, size_t size, bool ignoreFault) {
ReturnValue_t status = reserveSpace(size, storageId, ignoreFault);
if (status == RETURN_OK) {
write(*storageId, data, size);
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::getFreeElement(
store_address_t* storageId, const size_t size,
uint8_t** p_data, bool ignoreFault) {
ReturnValue_t status = reserveSpace(size, storageId, ignoreFault);
if (status == RETURN_OK) {
*p_data = &store[storageId->pool_index][getRawPosition(*storageId)];
} else {
*p_data = NULL;
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ConstAccessorPair LocalPool<NUMBER_OF_POOLS>::getData(
store_address_t storeId) {
uint8_t* tempData = nullptr;
ConstStorageAccessor constAccessor(storeId, this);
ReturnValue_t status = modifyData(storeId, &tempData, &constAccessor.size_);
constAccessor.constDataPointer = tempData;
return ConstAccessorPair(status, std::move(constAccessor));
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::getData(store_address_t storeId,
ConstStorageAccessor& storeAccessor) {
uint8_t* tempData = nullptr;
ReturnValue_t status = modifyData(storeId, &tempData, &storeAccessor.size_);
storeAccessor.assignStore(this);
storeAccessor.constDataPointer = tempData;
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::getData(
store_address_t packet_id, const uint8_t** packet_ptr, size_t* size) {
uint8_t* tempData = NULL;
ReturnValue_t status = modifyData(packet_id, &tempData, size);
*packet_ptr = tempData;
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline AccessorPair LocalPool<NUMBER_OF_POOLS>::modifyData(
store_address_t storeId) {
StorageAccessor accessor(storeId, this);
ReturnValue_t status = modifyData(storeId, &accessor.dataPointer,
&accessor.size_);
accessor.assignConstPointer();
return AccessorPair(status, std::move(accessor));
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::modifyData(
store_address_t storeId, StorageAccessor& storeAccessor) {
storeAccessor.assignStore(this);
ReturnValue_t status = modifyData(storeId, &storeAccessor.dataPointer,
&storeAccessor.size_);
storeAccessor.assignConstPointer();
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::modifyData(
store_address_t packet_id, uint8_t** packet_ptr, size_t* size) {
ReturnValue_t status = RETURN_FAILED;
if (packet_id.pool_index >= NUMBER_OF_POOLS) {
return ILLEGAL_STORAGE_ID;
}
if ((packet_id.packet_index >= n_elements[packet_id.pool_index])) {
return ILLEGAL_STORAGE_ID;
}
if (size_list[packet_id.pool_index][packet_id.packet_index]
!= STORAGE_FREE) {
uint32_t packet_position = getRawPosition(packet_id);
*packet_ptr = &store[packet_id.pool_index][packet_position];
*size = size_list[packet_id.pool_index][packet_id.packet_index];
status = RETURN_OK;
} else {
status = DATA_DOES_NOT_EXIST;
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::deleteData(
store_address_t packet_id) {
//if (getObjectId() == objects::IPC_STORE && packet_id.pool_index >= 3) {
// debug << "Delete: Pool: " << std::dec << packet_id.pool_index << " Index: "
// << packet_id.packet_index << std::endl;
//}
ReturnValue_t status = RETURN_OK;
uint32_t page_size = getPageSize(packet_id.pool_index);
if ((page_size != 0)
&& (packet_id.packet_index < n_elements[packet_id.pool_index])) {
uint16_t packet_position = getRawPosition(packet_id);
uint8_t* ptr = &store[packet_id.pool_index][packet_position];
memset(ptr, 0, page_size);
//Set free list
size_list[packet_id.pool_index][packet_id.packet_index] = STORAGE_FREE;
} else {
//pool_index or packet_index is too large
sif::error << "LocalPool:deleteData failed." << std::endl;
status = ILLEGAL_STORAGE_ID;
}
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline void LocalPool<NUMBER_OF_POOLS>::clearStore() {
for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) {
//TODO checkme
memset(size_list[n], STORAGE_FREE, (n_elements[n] * sizeof(**size_list)));
}
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::deleteData(uint8_t* ptr,
size_t size, store_address_t* storeId) {
store_address_t localId;
ReturnValue_t result = ILLEGAL_ADDRESS;
for (uint16_t n = 0; n < NUMBER_OF_POOLS; n++) {
//Not sure if new allocates all stores in order. so better be careful.
if ((store[n] <= ptr) && (&store[n][n_elements[n]*element_sizes[n]]) > ptr) {
localId.pool_index = n;
uint32_t deltaAddress = ptr - store[n];
// Getting any data from the right "block" is ok.
// This is necessary, as IF's sometimes don't point to the first
// element of an object.
localId.packet_index = deltaAddress / element_sizes[n];
result = deleteData(localId);
//if (deltaAddress % element_sizes[n] != 0) {
// error << "Pool::deleteData: address not aligned!" << std::endl;
//}
break;
}
}
if (storeId != NULL) {
*storeId = localId;
}
return result;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t LocalPool<NUMBER_OF_POOLS>::initialize() {
ReturnValue_t result = SystemObject::initialize();
if (result != RETURN_OK) {
return result;
}
internalErrorReporter = objectManager->get<InternalErrorReporterIF>(
objects::INTERNAL_ERROR_REPORTER);
if (internalErrorReporter == nullptr){
return ObjectManagerIF::INTERNAL_ERR_REPORTER_UNINIT;
}
//Check if any pool size is large than the maximum allowed.
for (uint8_t count = 0; count < NUMBER_OF_POOLS; count++) {
if (element_sizes[count] >= STORAGE_FREE) {
sif::error << "LocalPool::initialize: Pool is too large! "
"Max. allowed size is: " << (STORAGE_FREE - 1) << std::endl;
return StorageManagerIF::POOL_TOO_LARGE;
}
}
return RETURN_OK;
}
#endif

View File

@ -1,86 +1,44 @@
/**
* @file PoolManager
*
* @date 02.02.2012
* @author Bastian Baetz
*
* @brief This file contains the definition of the PoolManager class.
*/
#ifndef POOLMANAGER_H_
#define POOLMANAGER_H_
#include <framework/storagemanager/LocalPool.h>
#include <framework/ipc/MutexHelper.h>
#include <framework/storagemanager/StorageAccessor.h>
/**
* @brief The PoolManager class provides an intermediate data storage with
* a fixed pool size policy for inter-process communication.
* \details Uses local pool, but is thread-safe.
* @details Uses local pool calls but is thread safe by protecting the call
* with a lock.
*/
template <uint8_t NUMBER_OF_POOLS = 5>
template <uint8_t NUMBER_OF_POOLS>
class PoolManager : public LocalPool<NUMBER_OF_POOLS> {
public:
PoolManager(object_id_t setObjectId,
const uint16_t element_sizes[NUMBER_OF_POOLS],
const uint16_t n_elements[NUMBER_OF_POOLS]);
//! @brief In the PoolManager's destructor all allocated memory is freed.
virtual ~PoolManager();
//! @brief LocalPool overrides for thread-safety. Decorator function which
//! wraps LocalPool calls with a mutex protection.
ReturnValue_t deleteData(store_address_t) override;
ReturnValue_t deleteData(uint8_t* buffer, size_t size,
store_address_t* storeId = nullptr) override;
protected:
/**
* Overwritten for thread safety.
* Locks during execution.
*/
virtual ReturnValue_t reserveSpace(const uint32_t size, store_address_t* address, bool ignoreFault);
ReturnValue_t reserveSpace(const uint32_t size, store_address_t* address,
bool ignoreFault) override;
/**
* \brief The mutex is created in the constructor and makes access mutual exclusive.
* \details Locking and unlocking is done during searching for free slots and deleting existing slots.
* @brief The mutex is created in the constructor and makes
* access mutual exclusive.
* @details Locking and unlocking is done during searching for free slots
* and deleting existing slots.
*/
MutexIF* mutex;
public:
PoolManager( object_id_t setObjectId, const uint16_t element_sizes[NUMBER_OF_POOLS], const uint16_t n_elements[NUMBER_OF_POOLS] );
/**
* @brief In the PoolManager's destructor all allocated memory is freed.
*/
virtual ~PoolManager( void );
/**
* Overwritten for thread safety.
*/
virtual ReturnValue_t deleteData(store_address_t);
virtual ReturnValue_t deleteData(uint8_t* buffer, uint32_t size, store_address_t* storeId = NULL);
};
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t PoolManager<NUMBER_OF_POOLS>::reserveSpace(const uint32_t size, store_address_t* address, bool ignoreFault) {
MutexHelper mutexHelper(mutex,MutexIF::NO_TIMEOUT);
ReturnValue_t status = LocalPool<NUMBER_OF_POOLS>::reserveSpace(size,address,ignoreFault);
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline PoolManager<NUMBER_OF_POOLS>::PoolManager(object_id_t setObjectId,
const uint16_t element_sizes[NUMBER_OF_POOLS],
const uint16_t n_elements[NUMBER_OF_POOLS]) : LocalPool<NUMBER_OF_POOLS>(setObjectId, element_sizes, n_elements, true) {
mutex = MutexFactory::instance()->createMutex();
}
template<uint8_t NUMBER_OF_POOLS>
inline PoolManager<NUMBER_OF_POOLS>::~PoolManager(void) {
MutexFactory::instance()->deleteMutex(mutex);
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t PoolManager<NUMBER_OF_POOLS>::deleteData(
store_address_t packet_id) {
// debug << "PoolManager( " << translateObject(getObjectId()) << " )::deleteData from store " << packet_id.pool_index << ". id is " << packet_id.packet_index << std::endl;
MutexHelper mutexHelper(mutex,MutexIF::NO_TIMEOUT);
ReturnValue_t status = LocalPool<NUMBER_OF_POOLS>::deleteData(packet_id);
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t PoolManager<NUMBER_OF_POOLS>::deleteData(uint8_t* buffer, uint32_t size,
store_address_t* storeId) {
MutexHelper mutexHelper(mutex,MutexIF::NO_TIMEOUT);
ReturnValue_t status = LocalPool<NUMBER_OF_POOLS>::deleteData(buffer, size, storeId);
return status;
}
#include "PoolManager.tpp"
#endif /* POOLMANAGER_H_ */

View File

@ -0,0 +1,47 @@
#ifndef POOLMANAGER_TPP_
#define POOLMANAGER_TPP_
template<uint8_t NUMBER_OF_POOLS>
inline PoolManager<NUMBER_OF_POOLS>::PoolManager(object_id_t setObjectId,
const uint16_t element_sizes[NUMBER_OF_POOLS],
const uint16_t n_elements[NUMBER_OF_POOLS]) :
LocalPool<NUMBER_OF_POOLS>(setObjectId, element_sizes, n_elements, true) {
mutex = MutexFactory::instance()->createMutex();
}
template<uint8_t NUMBER_OF_POOLS>
inline PoolManager<NUMBER_OF_POOLS>::~PoolManager(void) {
MutexFactory::instance()->deleteMutex(mutex);
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t PoolManager<NUMBER_OF_POOLS>::reserveSpace(
const uint32_t size, store_address_t* address, bool ignoreFault) {
MutexHelper mutexHelper(mutex,MutexIF::NO_TIMEOUT);
ReturnValue_t status = LocalPool<NUMBER_OF_POOLS>::reserveSpace(size,
address,ignoreFault);
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t PoolManager<NUMBER_OF_POOLS>::deleteData(
store_address_t packet_id) {
// debug << "PoolManager( " << translateObject(getObjectId()) <<
// " )::deleteData from store " << packet_id.pool_index <<
// ". id is "<< packet_id.packet_index << std::endl;
MutexHelper mutexHelper(mutex,MutexIF::NO_TIMEOUT);
ReturnValue_t status = LocalPool<NUMBER_OF_POOLS>::deleteData(packet_id);
return status;
}
template<uint8_t NUMBER_OF_POOLS>
inline ReturnValue_t PoolManager<NUMBER_OF_POOLS>::deleteData(uint8_t* buffer,
size_t size, store_address_t* storeId) {
MutexHelper mutexHelper(mutex,MutexIF::NO_TIMEOUT);
ReturnValue_t status = LocalPool<NUMBER_OF_POOLS>::deleteData(buffer,
size, storeId);
return status;
}
#endif

View File

@ -0,0 +1,154 @@
#include <framework/storagemanager/StorageAccessor.h>
ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId):
storeId(storeId) {}
ConstStorageAccessor::ConstStorageAccessor(store_address_t storeId,
StorageManagerIF* store):
storeId(storeId), store(store) {
internalState = AccessState::ASSIGNED;
}
ConstStorageAccessor::~ConstStorageAccessor() {
if(deleteData and store != nullptr) {
sif::debug << "deleting store data" << std::endl;
store->deleteData(storeId);
}
}
ConstStorageAccessor::ConstStorageAccessor(ConstStorageAccessor&& other):
constDataPointer(other.constDataPointer), storeId(other.storeId),
size_(other.size_), store(other.store), deleteData(other.deleteData),
internalState(other.internalState) {
// This prevent premature deletion
other.store = nullptr;
}
ConstStorageAccessor& ConstStorageAccessor::operator=(
ConstStorageAccessor&& other) {
constDataPointer = other.constDataPointer;
storeId = other.storeId;
store = other.store;
size_ = other.size_;
deleteData = other.deleteData;
this->store = other.store;
// This prevents premature deletion
other.store = nullptr;
return *this;
}
const uint8_t* ConstStorageAccessor::data() const {
return constDataPointer;
}
size_t ConstStorageAccessor::size() const {
if(internalState == AccessState::UNINIT) {
sif::warning << "StorageAccessor: Not initialized!" << std::endl;
}
return size_;
}
ReturnValue_t ConstStorageAccessor::getDataCopy(uint8_t *pointer,
size_t maxSize) {
if(internalState == AccessState::UNINIT) {
sif::warning << "StorageAccessor: Not initialized!" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
if(size_ > maxSize) {
sif::error << "StorageAccessor: Supplied buffer not large enough" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
std::copy(constDataPointer, constDataPointer + size_, pointer);
return HasReturnvaluesIF::RETURN_OK;
}
void ConstStorageAccessor::release() {
deleteData = false;
}
store_address_t ConstStorageAccessor::getId() const {
return storeId;
}
void ConstStorageAccessor::print() const {
if(internalState == AccessState::UNINIT) {
sif::warning << "StorageAccessor: Not initialized!" << std::endl;
return;
}
sif::info << "StorageAccessor: Printing data: [";
for(uint16_t iPool = 0; iPool < size_; iPool++) {
sif::info << std::hex << (int)constDataPointer[iPool];
if(iPool < size_ - 1){
sif::info << " , ";
}
}
sif::info << " ] " << std::endl;
}
void ConstStorageAccessor::assignStore(StorageManagerIF* store) {
internalState = AccessState::ASSIGNED;
this->store = store;
}
StorageAccessor::StorageAccessor(store_address_t storeId):
ConstStorageAccessor(storeId) {
}
StorageAccessor::StorageAccessor(store_address_t storeId,
StorageManagerIF* store):
ConstStorageAccessor(storeId, store) {
}
StorageAccessor& StorageAccessor::operator =(
StorageAccessor&& other) {
// Call the parent move assignment and also assign own member.
dataPointer = other.dataPointer;
StorageAccessor::operator=(std::move(other));
return * this;
}
// Call the parent move ctor and also transfer own member.
StorageAccessor::StorageAccessor(StorageAccessor&& other):
ConstStorageAccessor(std::move(other)), dataPointer(other.dataPointer) {
}
ReturnValue_t StorageAccessor::getDataCopy(uint8_t *pointer, size_t maxSize) {
if(internalState == AccessState::UNINIT) {
sif::warning << "StorageAccessor: Not initialized!" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
if(size_ > maxSize) {
sif::error << "StorageAccessor: Supplied buffer not large enough" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
std::copy(dataPointer, dataPointer + size_, pointer);
return HasReturnvaluesIF::RETURN_OK;
}
uint8_t* StorageAccessor::data() {
if(internalState == AccessState::UNINIT) {
sif::warning << "StorageAccessor: Not initialized!" << std::endl;
}
return dataPointer;
}
ReturnValue_t StorageAccessor::write(uint8_t *data, size_t size,
uint16_t offset) {
if(internalState == AccessState::UNINIT) {
sif::warning << "StorageAccessor: Not initialized!" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
if(offset + size > size_) {
sif::error << "StorageAccessor: Data too large for pool entry!" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
std::copy(data, data + size, dataPointer + offset);
return HasReturnvaluesIF::RETURN_OK;
}
void StorageAccessor::assignConstPointer() {
constDataPointer = dataPointer;
}

View File

@ -0,0 +1,152 @@
/**
* @brief Helper classes to facilitate safe access to storages which is also
* conforming to RAII principles
* @details These helper can be used together with the
* StorageManager classes to manage access to a storage.
* It can take care of thread-safety while also providing
* mechanisms to automatically clear storage data.
*/
#ifndef TEST_PROTOTYPES_STORAGEACCESSOR_H_
#define TEST_PROTOTYPES_STORAGEACCESSOR_H_
#include <framework/ipc/MutexHelper.h>
#include <framework/storagemanager/StorageManagerIF.h>
#include <memory>
/**
* @brief Accessor class which can be returned by pool managers
* or passed and set by pool managers to have safe access to the pool
* resources.
*/
class ConstStorageAccessor {
//! StorageManager classes have exclusive access to private variables.
template<uint8_t NUMBER_OF_POOLS>
friend class PoolManager;
template<uint8_t NUMBER_OF_POOLS>
friend class LocalPool;
public:
/**
* @brief Simple constructor which takes the store ID of the storage
* entry to access.
* @param storeId
*/
ConstStorageAccessor(store_address_t storeId);
ConstStorageAccessor(store_address_t storeId, StorageManagerIF* store);
/**
* @brief The destructor in default configuration takes care of
* deleting the accessed pool entry and unlocking the mutex
*/
virtual ~ConstStorageAccessor();
/**
* @brief Returns a pointer to the read-only data
* @return
*/
const uint8_t* data() const;
/**
* @brief Copies the read-only data to the supplied pointer
* @param pointer
*/
virtual ReturnValue_t getDataCopy(uint8_t *pointer, size_t maxSize);
/**
* @brief Calling this will prevent the Accessor from deleting the data
* when the destructor is called.
*/
void release();
/**
* Get the size of the data
* @return
*/
size_t size() const;
/**
* Get the storage ID.
* @return
*/
store_address_t getId() const;
void print() const;
/**
* @brief Move ctor and move assignment allow returning accessors as
* a returnvalue. They prevent resource being free prematurely.
* Refer to: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/
* move-constructors-and-move-assignment-operators-cpp.md
* @param
* @return
*/
ConstStorageAccessor& operator= (ConstStorageAccessor&&);
ConstStorageAccessor (ConstStorageAccessor&&);
//! The copy ctor and copy assignemnt should be deleted implicitely
//! according to https://foonathan.net/2019/02/special-member-functions/
//! but I still deleted them to make it more explicit. (remember rule of 5).
ConstStorageAccessor& operator= (ConstStorageAccessor&) = delete;
ConstStorageAccessor (ConstStorageAccessor&) = delete;
protected:
const uint8_t* constDataPointer = nullptr;
store_address_t storeId;
size_t size_ = 0;
//! Managing pool, has to assign itself.
StorageManagerIF* store = nullptr;
bool deleteData = true;
enum class AccessState {
UNINIT,
ASSIGNED
};
//! Internal state for safety reasons.
AccessState internalState = AccessState::UNINIT;
/**
* Used by the pool manager instances to assign themselves to the
* accessor. This is necessary to delete the data when the acessor
* exits the scope ! The internal state will be considered read
* when this function is called, so take care all data is set properly as
* well.
* @param
*/
void assignStore(StorageManagerIF*);
};
/**
* @brief Child class for modifyable data. Also has a normal pointer member.
*/
class StorageAccessor: public ConstStorageAccessor {
//! StorageManager classes have exclusive access to private variables.
template<uint8_t NUMBER_OF_POOLS>
friend class PoolManager;
template<uint8_t NUMBER_OF_POOLS>
friend class LocalPool;
public:
StorageAccessor(store_address_t storeId);
StorageAccessor(store_address_t storeId, StorageManagerIF* store);
/**
* @brief Move ctor and move assignment allow returning accessors as
* a returnvalue. They prevent resource being free prematurely.
* Refer to: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/cpp/
* move-constructors-and-move-assignment-operators-cpp.md
* @param
* @return
*/
StorageAccessor& operator= (StorageAccessor&&);
StorageAccessor (StorageAccessor&&);
ReturnValue_t write(uint8_t *data, size_t size,
uint16_t offset = 0);
uint8_t* data();
ReturnValue_t getDataCopy(uint8_t *pointer, size_t maxSize) override;
private:
//! Non-const pointer for modifyable data.
uint8_t* dataPointer = nullptr;
//! For modifyable data, the const pointer is assigned to the normal
//! pointer by the pool manager so both access functions can be used safely
void assignConstPointer();
};
#endif /* TEST_PROTOTYPES_STORAGEACCESSOR_H_ */

View File

@ -3,12 +3,19 @@
#include <framework/events/Event.h>
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <stddef.h>
#include <cstddef>
#include <utility>
class StorageAccessor;
class ConstStorageAccessor;
using AccessorPair = std::pair<ReturnValue_t, StorageAccessor>;
using ConstAccessorPair = std::pair<ReturnValue_t, ConstStorageAccessor>;
/**
* This union defines the type that identifies where a data packet is stored in the store.
* It comprises of a raw part to read it as raw value and a structured part to use it in
* pool-like stores.
* This union defines the type that identifies where a data packet is
* stored in the store. It comprises of a raw part to read it as raw value and
* a structured part to use it in pool-like stores.
*/
union store_address_t {
/**
@ -48,6 +55,10 @@ union store_address_t {
* Alternative access to the raw value.
*/
uint32_t raw;
bool operator==(const store_address_t& other) const {
return raw == other.raw;
}
};
/**
@ -71,6 +82,7 @@ public:
static const ReturnValue_t ILLEGAL_STORAGE_ID = MAKE_RETURN_CODE(3); //!< This return code indicates that data was requested with an illegal storage ID.
static const ReturnValue_t DATA_DOES_NOT_EXIST = MAKE_RETURN_CODE(4); //!< This return code indicates that the requested ID was valid, but no data is stored there.
static const ReturnValue_t ILLEGAL_ADDRESS = MAKE_RETURN_CODE(5);
static const ReturnValue_t POOL_TOO_LARGE = MAKE_RETURN_CODE(6); //!< Pool size too large on initialization.
static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::OBSW;
static const Event GET_DATA_FAILED = MAKE_EVENT(0, SEVERITY::LOW);
@ -94,7 +106,8 @@ public:
* @li RETURN_FAILED if data could not be added.
* storageId is unchanged then.
*/
virtual ReturnValue_t addData(store_address_t* storageId, const uint8_t * data, uint32_t size, bool ignoreFault = false) = 0;
virtual ReturnValue_t addData(store_address_t* storageId,
const uint8_t * data, size_t size, bool ignoreFault = false) = 0;
/**
* @brief With deleteData, the storageManager frees the memory region
* identified by packet_id.
@ -105,14 +118,37 @@ public:
*/
virtual ReturnValue_t deleteData(store_address_t packet_id) = 0;
/**
* @brief Another deleteData which uses the pointer and size of the stored data to delete the content.
* @brief Another deleteData which uses the pointer and size of the
* stored data to delete the content.
* @param buffer Pointer to the data.
* @param size Size of data to be stored.
* @param storeId Store id of the deleted element (optional)
* @return @li RETURN_OK on success.
* @li failure code if deletion did not work
*/
virtual ReturnValue_t deleteData(uint8_t* buffer, uint32_t size, store_address_t* storeId = NULL) = 0;
virtual ReturnValue_t deleteData(uint8_t* buffer, size_t size,
store_address_t* storeId = nullptr) = 0;
/**
* @brief Access the data by supplying a store ID.
* @details
* A pair consisting of the retrieval result and an instance of a
* ConstStorageAccessor class is returned
* @param storeId
* @return Pair of return value and a ConstStorageAccessor instance
*/
virtual ConstAccessorPair getData(store_address_t storeId) = 0;
/**
* @brief Access the data by supplying a store ID and a helper
* instance
* @param storeId
* @param constAccessor Wrapper function to access store data.
* @return
*/
virtual ReturnValue_t getData(store_address_t storeId,
ConstStorageAccessor& constAccessor) = 0;
/**
* @brief getData returns an address to data and the size of the data
* for a given packet_id.
@ -126,8 +162,30 @@ public:
*/
virtual ReturnValue_t getData(store_address_t packet_id,
const uint8_t** packet_ptr, size_t* size) = 0;
/**
* Same as above, but not const and therefore modifiable.
* Modify data by supplying a store ID
* @param storeId
* @return Pair of return value and StorageAccessor helper
*/
virtual AccessorPair modifyData(store_address_t storeId) = 0;
/**
* Modify data by supplying a store ID and a StorageAccessor helper instance.
* @param storeId
* @param accessor Helper class to access the modifiable data.
* @return
*/
virtual ReturnValue_t modifyData(store_address_t storeId,
StorageAccessor& accessor) = 0;
/**
* Get pointer and size of modifiable data by supplying the storeId
* @param packet_id
* @param packet_ptr [out] Pointer to pointer of data to set
* @param size [out] Pointer to size to set
* @return
*/
virtual ReturnValue_t modifyData(store_address_t packet_id,
uint8_t** packet_ptr, size_t* size) = 0;
@ -145,13 +203,13 @@ public:
* storageId is unchanged then.
*/
virtual ReturnValue_t getFreeElement(store_address_t* storageId,
const uint32_t size, uint8_t** p_data, bool ignoreFault = false ) = 0;
const size_t size, uint8_t** p_data, bool ignoreFault = false ) = 0;
/**
* Clears the whole store.
* Use with care!
*/
virtual void clearStore() = 0;
};
#endif /* STORAGEMANAGERIF_H_ */

View File

@ -14,7 +14,8 @@ uint8_t SpacePacketBase::getPacketVersionNumber( void ) {
return (this->data->header.packet_id_h & 0b11100000) >> 5;
}
void SpacePacketBase::initSpacePacketHeader(bool isTelecommand, bool hasSecondaryHeader, uint16_t apid, uint16_t sequenceCount) {
void SpacePacketBase::initSpacePacketHeader(bool isTelecommand,
bool hasSecondaryHeader, uint16_t apid, uint16_t sequenceCount) {
//reset header to zero:
memset(data,0, sizeof(this->data->header) );
//Set TC/TM bit.
@ -81,7 +82,7 @@ void SpacePacketBase::setPacketDataLength( uint16_t new_length) {
this->data->header.packet_length_l = ( new_length & 0x00FF );
}
uint32_t SpacePacketBase::getFullSize() {
size_t SpacePacketBase::getFullSize() {
//+1 is done because size in packet data length field is: size of data field -1
return this->getPacketDataLength() + sizeof(this->data->header) + 1;
}

View File

@ -2,9 +2,10 @@
#define SPACEPACKETBASE_H_
#include <framework/tmtcpacket/ccsds_header.h>
#include <cstddef>
/**
* \defgroup tmtcpackets Space Packets
* @defgroup tmtcpackets Space Packets
* This is the group, where all classes associated with Telecommand and
* Telemetry packets belong to.
* The class hierarchy resembles the dependency between the different standards
@ -167,7 +168,7 @@ public:
* This method returns the full raw packet size.
* @return The full size of the packet in bytes.
*/
uint32_t getFullSize();
size_t getFullSize();
uint32_t getApidAndSequenceCount() const;

View File

@ -28,7 +28,7 @@ const uint8_t* TcPacketBase::getApplicationData() const {
return &tcData->data;
}
size_t TcPacketBase::getApplicationDataSize() {
uint16_t TcPacketBase::getApplicationDataSize() {
return getPacketDataLength() - sizeof(tcData->data_field) - CRC_SIZE + 1;
}
@ -46,9 +46,16 @@ void TcPacketBase::setErrorControl() {
(&tcData->data)[size + 1] = (crc) & 0X00FF; // CRCL
}
void TcPacketBase::setData(const uint8_t* p_Data) {
SpacePacketBase::setData(p_Data);
tcData = (TcPacketPointer*) p_Data;
void TcPacketBase::setData(const uint8_t* pData) {
SpacePacketBase::setData(pData);
tcData = (TcPacketPointer*) pData;
}
void TcPacketBase::setApplicationData(const uint8_t * pData, uint16_t dataLen) {
setData(pData);
// packet data length is actual size of data field minus 1
SpacePacketBase::setPacketDataLength(dataLen +
sizeof(PUSTcDataFieldHeader) + TcPacketBase::CRC_SIZE - 1);
}
uint8_t TcPacketBase::getSecondaryHeaderFlag() {
@ -72,7 +79,7 @@ void TcPacketBase::initializeTcPacket(uint16_t apid, uint16_t sequenceCount,
uint8_t ack, uint8_t service, uint8_t subservice) {
initSpacePacketHeader(true, true, apid, sequenceCount);
memset(&tcData->data_field, 0, sizeof(tcData->data_field));
setPacketDataLength(sizeof(tcData->data_field) + CRC_SIZE);
setPacketDataLength(sizeof(PUSTcDataFieldHeader) + CRC_SIZE - 1);
//Data Field Header:
//Set CCSDS_secondary_header_flag to 0, version number to 001 and ack to 0000
tcData->data_field.version_type_ack = 0b00010000;
@ -80,3 +87,8 @@ void TcPacketBase::initializeTcPacket(uint16_t apid, uint16_t sequenceCount,
tcData->data_field.service_type = service;
tcData->data_field.service_subtype = subservice;
}
size_t TcPacketBase::calculateFullPacketLength(size_t appDataLen) {
return sizeof(CCSDSPrimaryHeader) + sizeof(PUSTcDataFieldHeader) +
appDataLen + TcPacketBase::CRC_SIZE;
}

View File

@ -2,6 +2,7 @@
#define TCPACKETBASE_H_
#include <framework/tmtcpacket/SpacePacketBase.h>
#include <cstddef>
/**
* This struct defines a byte-wise structured PUS TC Data Field Header.
@ -99,7 +100,8 @@ public:
* @param service PUS Service
* @param subservice PUS Subservice
*/
void initializeTcPacket(uint16_t apid, uint16_t sequenceCount, uint8_t ack, uint8_t service, uint8_t subservice);
void initializeTcPacket(uint16_t apid, uint16_t sequenceCount, uint8_t ack,
uint8_t service, uint8_t subservice);
/**
* This command returns the CCSDS Secondary Header Flag.
* It shall always be zero for PUS Packets. This is the
@ -151,7 +153,7 @@ public:
* @return The size of the PUS Application Data (without Error Control
* field)
*/
size_t getApplicationDataSize();
uint16_t getApplicationDataSize();
/**
* This getter returns the Error Control Field of the packet.
*
@ -175,12 +177,24 @@ public:
*
* @param p_data A pointer to another PUS Telecommand Packet.
*/
void setData( const uint8_t* p_data );
void setData( const uint8_t* pData );
/**
* Set application data and corresponding length field.
* @param pData
* @param dataLen
*/
void setApplicationData(const uint8_t * pData, uint16_t dataLen);
/**
* This is a debugging helper method that prints the whole packet content
* to the screen.
*/
void print();
/**
* Calculate full packet length from application data length.
* @param appDataLen
* @return
*/
static size_t calculateFullPacketLength(size_t appDataLen);
};

View File

@ -212,6 +212,8 @@ protected:
*/
PeriodicTaskIF* executingTask;
// todo: why do these functions not have returnvalues? the caller should be
// able to check whether the send operations actually work.
/**
* Send TM data from pointer to data. If a header is supplied it is added before data
* @param subservice Number of subservice