MotorControlModuleSDFM_TMS3.../Projects/EFC_Communication/UMLibrary/driver/BinaryDataRecycler.cpp
2024-06-07 11:12:56 +03:00

71 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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();
}