2020-08-13 20:53:35 +02:00
# include "DataPool.h"
# include "../serviceinterface/ServiceInterfaceStream.h"
# include "../ipc/MutexFactory.h"
2018-07-12 16:29:32 +02:00
2016-06-15 23:48:41 +02:00
DataPool : : DataPool ( void ( * initFunction ) ( std : : map < uint32_t , PoolEntryIF * > * pool_map ) ) {
2018-07-12 16:29:32 +02:00
mutex = MutexFactory : : instance ( ) - > createMutex ( ) ;
2016-06-15 23:48:41 +02:00
if ( initFunction ! = NULL ) {
initFunction ( & this - > data_pool ) ;
}
}
DataPool : : ~ DataPool ( ) {
2018-07-12 16:29:32 +02:00
MutexFactory : : instance ( ) - > deleteMutex ( mutex ) ;
2016-06-15 23:48:41 +02:00
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 ;
}
}
2020-04-21 22:28:43 +02:00
//uint8_t DataPool::getRawData( uint32_t data_pool_id, uint8_t* address, uint16_t* size, uint32_t maxSize ) {
2016-06-15 23:48:41 +02:00
// std::map<uint32_t, PoolEntryIF*>::iterator it = this->data_pool.find( data_pool_id );
// if ( it != this->data_pool.end() ) {
2020-04-21 22:28:43 +02:00
// if ( it->second->getByteSize() <= maxSize ) {
2016-06-15 23:48:41 +02:00
// *size = it->second->getByteSize();
// memcpy( address, it->second->getRawData(), *size );
// return DP_SUCCESSFUL;
// }
// }
// *size = 0;
// return DP_FAILURE;
//}
ReturnValue_t DataPool : : freeDataPoolLock ( ) {
2018-07-12 16:29:32 +02:00
ReturnValue_t status = mutex - > unlockMutex ( ) ;
2016-06-15 23:48:41 +02:00
if ( status ! = RETURN_OK ) {
2020-04-23 19:13:18 +02:00
sif : : error < < " DataPool::DataPool: unlock of mutex failed with error code: " < < status < < std : : endl ;
2016-06-15 23:48:41 +02:00
}
return status ;
}
ReturnValue_t DataPool : : lockDataPool ( ) {
2020-06-04 20:20:38 +02:00
ReturnValue_t status = mutex - > lockMutex ( MutexIF : : BLOCKING ) ;
2016-06-15 23:48:41 +02:00
if ( status ! = RETURN_OK ) {
2020-04-23 19:13:18 +02:00
sif : : error < < " DataPool::DataPool: lock of mutex failed with error code: " < < status < < std : : endl ;
2016-06-15 23:48:41 +02:00
}
return status ;
}
void DataPool : : print ( ) {
2020-04-23 19:13:18 +02:00
sif : : debug < < " DataPool contains: " < < std : : endl ;
2016-06-15 23:48:41 +02:00
std : : map < uint32_t , PoolEntryIF * > : : iterator dataPoolIt ;
dataPoolIt = this - > data_pool . begin ( ) ;
while ( dataPoolIt ! = this - > data_pool . end ( ) ) {
2020-04-23 19:13:18 +02:00
sif : : debug < < std : : hex < < dataPoolIt - > first < < std : : dec < < " | " ;
2016-06-15 23:48:41 +02:00
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 ;
}
2018-07-12 16:29:32 +02:00
//SHOULDDO: 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.
//there is no guarantee in the standard, but it seems to me that the implementation is safe -UM
ReturnValue_t DataPool : : getType ( uint32_t parameter_id , Type * type ) {
2016-06-15 23:48:41 +02:00
std : : map < uint32_t , PoolEntryIF * > : : iterator it = this - > data_pool . find ( PIDToDataPoolId ( parameter_id ) ) ;
if ( it ! = this - > data_pool . end ( ) ) {
2018-07-12 16:29:32 +02:00
* type = it - > second - > getType ( ) ;
return RETURN_OK ;
2016-06-15 23:48:41 +02:00
} else {
2018-07-12 16:29:32 +02:00
* type = Type : : UNKNOWN_TYPE ;
return RETURN_FAILED ;
2016-06-15 23:48:41 +02:00
}
}
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 ;
}