#include <iomanip>
#include <vsomeip/internal/logger.hpp>
#include "../../../../include/e2e/profile/profile07/protector.hpp"
#include "../../../../../utility/include/byteorder.hpp"
namespace vsomeip_v3 {
namespace e2e {
namespace profile07 {
void
protector::protect(e2e_buffer &_buffer, instance_t _instance) {
std::lock_guard<std::mutex> lock(protect_mutex_);
if (verify_inputs(_buffer)) {
write_32(_buffer, static_cast<uint16_t>(_buffer.size()), PROFILE_07_SIZE_OFFSET);
write_32(_buffer, get_counter(_instance), PROFILE_07_COUNTER_OFFSET);
write_32(_buffer, config_.data_id_, PROFILE_07_DATAID_OFFSET);
uint64_t its_crc = profile_07::compute_crc(config_, _buffer);
write_64(_buffer, its_crc, PROFILE_07_CRC_OFFSET);
increment_counter(_instance);
}
}
bool
protector::verify_inputs(e2e_buffer &_buffer) {
return (_buffer.size() >= config_.min_data_length_
&& _buffer.size() <= config_.max_data_length_);
}
void
protector::write_32(e2e_buffer &_buffer, uint32_t _data, size_t _index) {
_buffer[config_.offset_ + _index] = VSOMEIP_LONG_BYTE3(_data);
_buffer[config_.offset_ + _index + 1] = VSOMEIP_LONG_BYTE2(_data);
_buffer[config_.offset_ + _index + 2] = VSOMEIP_LONG_BYTE1(_data);
_buffer[config_.offset_ + _index + 3] = VSOMEIP_LONG_BYTE0(_data);
}
void
protector::write_64(e2e_buffer &_buffer, uint64_t _data, size_t _index) {
_buffer[config_.offset_ + _index] = VSOMEIP_LONG_LONG_BYTE7(_data);
_buffer[config_.offset_ + _index + 1] = VSOMEIP_LONG_LONG_BYTE6(_data);
_buffer[config_.offset_ + _index + 2] = VSOMEIP_LONG_LONG_BYTE5(_data);
_buffer[config_.offset_ + _index + 3] = VSOMEIP_LONG_LONG_BYTE4(_data);
_buffer[config_.offset_ + _index + 4] = VSOMEIP_LONG_LONG_BYTE3(_data);
_buffer[config_.offset_ + _index + 5] = VSOMEIP_LONG_LONG_BYTE2(_data);
_buffer[config_.offset_ + _index + 6] = VSOMEIP_LONG_LONG_BYTE1(_data);
_buffer[config_.offset_ + _index + 7] = VSOMEIP_LONG_LONG_BYTE0(_data);
}
uint32_t
protector::get_counter(instance_t _instance) const {
uint32_t its_counter(0);
auto find_counter = counter_.find(_instance);
if (find_counter != counter_.end())
its_counter = find_counter->second;
return its_counter;
}
void
protector::increment_counter(instance_t _instance) {
auto find_counter = counter_.find(_instance);
if (find_counter != counter_.end())
find_counter->second++;
else
counter_[_instance] = 1;
}
} } }