71 lines
1.6 KiB
C++
71 lines
1.6 KiB
C++
/*
|
||
* BinaryDataRecycler.cpp
|
||
*
|
||
* Created on: 8 окт. 2022 г.
|
||
* Author: titov
|
||
*/
|
||
|
||
#include "BinaryDataRecycler.hh"
|
||
|
||
#include <cstring>
|
||
|
||
#include "../systemic/SystemException.hh"
|
||
|
||
struct DataUpdateException : public systemic::SystemException {
|
||
|
||
std::size_t id() const noexcept { return 34643; };
|
||
std::pair<const char *, std::size_t> binary() const noexcept {
|
||
return { reinterpret_cast<const char *>(this), sizeof(DataUpdateException) };
|
||
}
|
||
const char * what() const noexcept { return "Data update blocked"; };
|
||
|
||
};
|
||
|
||
driver::BinaryDataRecycler::BinaryDataRecycler(
|
||
std::pmr::memory_resource * memory_resource, std::size_t bit_len ) :
|
||
subscribers(memory_resource), buffer(memory_resource), bitsize(bit_len) {
|
||
|
||
buffer.resize( communication::format::bits::bytes_on_bits(bitsize), 0 );
|
||
|
||
}
|
||
|
||
void driver::BinaryDataRecycler::addSubscriber(
|
||
std::size_t bit_address, std::size_t bit_len,
|
||
communication::IBinaryDataSubscriber * subscriber ) {
|
||
|
||
if( bit_address + bit_len < bitsize ) {
|
||
|
||
subscribers.addSubscriber(bit_address, bit_len, subscriber);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
void driver::BinaryDataRecycler::read( const void * data, std::size_t size ) {
|
||
|
||
if( buffer_lock.try_lock() ) {
|
||
|
||
std::memcpy( &buffer[0], data, buffer.size() );
|
||
updated = true;
|
||
|
||
buffer_lock.unlock();
|
||
}
|
||
|
||
}
|
||
|
||
void driver::BinaryDataRecycler::process() {
|
||
|
||
if( buffer_lock.try_lock() ) {
|
||
|
||
if( updated ) {
|
||
subscribers.read( &buffer[0], buffer.size() );
|
||
updated = false;
|
||
}
|
||
|
||
buffer_lock.unlock();
|
||
|
||
} else if( updated ) throw DataUpdateException();
|
||
|
||
}
|
||
|