Commit 3e0c6276 authored by Pietro Saccardi's avatar Pietro Saccardi
Browse files

Mark points that could be using a buffer pool [skip ci]

parent f264edf8
Pipeline #1500 skipped
......@@ -31,7 +31,7 @@ namespace desfire {
std::uint8_t _last_byte_xor;
std::unique_ptr<std::uint8_t[]> _subkey_pad;
std::unique_ptr<std::uint8_t[]> _subkey_nopad;
bin_data _cmac_buffer;
bin_data _cmac_buffer; // TODO Investigate whether this can become a borrowed buffer
/**
* @brief Key to use for messages that need padding.
......
......@@ -1238,7 +1238,7 @@ namespace desfire {
template <class T>
tag::result<> tag::write_record(file_id fid, T &&record, file_security security) {
static bin_data buffer{};
static bin_data buffer{}; // TODO Borrow buffer
buffer.clear();
buffer << std::forward<T>(record);
return write_record(fid, 0, buffer, security);
......@@ -1246,7 +1246,7 @@ namespace desfire {
template <class T>
tag::result<> tag::write_record(file_id fid, T &&record) {
static bin_data buffer{};
static bin_data buffer{}; // TODO Borrow buffer
buffer.clear();
buffer << std::forward<T>(record);
return write_record(fid, 0, buffer);
......
......@@ -49,7 +49,7 @@ namespace pn532 {
struct frame<frame_type::info> {
bits::transport transport = bits::transport::host_to_pn532;
bits::command command = bits::command::diagnose;
bin_data data;
bin_data data; // TODO Investigate whether this can become a borrowed buffer. This especially has consequences on the signature of send
};
/**
......
......@@ -39,8 +39,12 @@
#include "data.hpp"
#include "msg.hpp"
#include <mlab/result.hpp>
#include <mlab/pool.hpp>
namespace pn532 {
using buffer_pool = mlab::pool<bin_data>;
using borrowed_buffer = mlab::borrowed<bin_data>;
namespace {
using namespace std::chrono_literals;
}
......@@ -54,7 +58,7 @@ namespace pn532 {
template <class... Tn>
using result = channel::result<Tn...>;
inline explicit controller(channel &chn);
inline explicit controller(channel &chn, std::shared_ptr<buffer_pool> pool = nullptr);
controller(controller const &) = delete;
......@@ -1286,6 +1290,9 @@ namespace pn532 {
private:
channel *_channel;
mutable std::shared_ptr<buffer_pool> _pool;
[[nodiscard]] borrowed_buffer borrow_buffer(std::size_t prealloc_size = std::numeric_limits<std::size_t>::max()) const;
[[nodiscard]] inline channel &chn() const;
......@@ -1302,7 +1309,12 @@ namespace pn532 {
namespace pn532 {
controller::controller(channel &chn) : _channel{&chn} {}
controller::controller(channel &chn, std::shared_ptr<buffer_pool> pool) : _channel{&chn}, _pool{std::move(pool)}
{
if (_pool == nullptr) {
_pool = std::make_shared<buffer_pool>();
}
}
channel &controller::chn() const { return *_channel; }
......@@ -1320,10 +1332,9 @@ namespace pn532 {
template <class T, class>
controller::result<rf_status, bin_data> controller::initiator_data_exchange(std::uint8_t target_logical_index, T &&data, ms timeout) {
static bin_data buffer{};
buffer.clear();
auto buffer = borrow_buffer();
buffer << std::forward<T>(data);
return initiator_data_exchange(target_logical_index, buffer, timeout);
return initiator_data_exchange(target_logical_index, *buffer, timeout);
}
}// namespace pn532
......
......@@ -44,7 +44,7 @@ namespace desfire {
cipher_legacy::mac_t cipher_legacy::compute_mac(range<bin_data::const_iterator> data) {
static bin_data buffer{};
static bin_data buffer{}; // TODO Borrow buffer
// Resize the buffer and copy data
buffer.clear();
......
......@@ -166,7 +166,7 @@ namespace desfire {
}
bin_data any_key::get_packed_key_body() const {
bin_data body{};
bin_data body{}; // TODO Investigate whether this can become a borrowed buffer
// Extract packed key data from the other key
switch (type()) {
case cipher_type::none:
......@@ -199,7 +199,7 @@ namespace desfire {
if (their_data.empty()) {
return {};
}
bin_data our_data = get_packed_key_body();
bin_data our_data = get_packed_key_body(); // TODO Investigate whether this can become a borrowed buffer
if (our_data.empty()) {
return {};
}
......
......@@ -123,12 +123,12 @@ namespace desfire {
tag::result<bin_data> tag::raw_command_response(bin_stream &tx_data, bool rx_fetch_additional_frames) {
static constexpr auto chunk_size = bits::max_packet_length;
static bin_data tx_chunk{prealloc(chunk_size)};
static bin_data tx_chunk{prealloc(chunk_size)}; // TODO Borrow buffer
const auto num_tx_chunks = 1 + div_round_up(saturate_sub(tx_data.remaining(), chunk_size), chunk_size - 1);
tx_chunk.clear();
status last_status = status::additional_frame;
bin_data rx_data;
bin_data rx_data; // TODO Investigate whether this can become a borrowed buffer
for (std::size_t chunk_idx = 0; last_status == status::additional_frame; ++chunk_idx, tx_chunk.clear()) {
assert(tx_chunk.empty());
......@@ -198,7 +198,7 @@ namespace desfire {
cipher &c = override_cipher == nullptr ? *_active_cipher : *override_cipher;
// Assemble data to transmit and preprocess
static bin_data tx_data;
static bin_data tx_data; // TODO Borrow buffer
tx_data.clear();
tx_data << prealloc(data.size() + 1) << cmd << data;
......@@ -463,7 +463,7 @@ namespace desfire {
const std::uint8_t key_no_flag = (active_app() == root_app
? key_no_to_change | static_cast<std::uint8_t>(app_crypto_from_cipher(new_key.type()))
: key_no_to_change);
bin_data payload{prealloc(33)};
bin_data payload{prealloc(33)}; // TODO Borrow buffer
payload << key_no_flag;
// Changing from a different key requires to xor it with that other key
if (current_key != nullptr) {
......@@ -587,7 +587,7 @@ namespace desfire {
}
// RX happens with the chosen file protection, except on nonlegacy ciphers where plain becomes maced
const auto rx_cipher_mode = cipher_mode_most_secure(cipher_mode_from_security(security), default_comm_cfg().rx);
bin_data payload{prealloc(7)};
bin_data payload{prealloc(7)}; // TODO Borrow buffer
payload << fid << lsb24 << offset << lsb24 << length;
return command_response(command_code::read_data, payload, comm_cfg{default_comm_cfg().tx, rx_cipher_mode});
}
......@@ -618,7 +618,7 @@ namespace desfire {
const comm_cfg cfg{cipher_mode_from_security(security), default_comm_cfg().rx,
8 /* secure with legacy MAC only data */};
bin_data payload{prealloc(data.size() + 7)};
bin_data payload{prealloc(data.size() + 7)}; // TODO Borrow buffer
payload << fid << lsb24 << offset << lsb24 << data.size() << data;
return safe_drop_payload(command_code::write_data, command_response(command_code::write_data, payload, cfg));
......@@ -659,7 +659,7 @@ namespace desfire {
return error::parameter_error;
}
const comm_cfg cfg{cipher_mode_from_security(security), default_comm_cfg().rx, 2 /* after FID */};
bin_data payload{prealloc(5)};
bin_data payload{prealloc(5)}; // TODO Borrow buffer
payload << fid << lsb32 << amount;
return safe_drop_payload(cmd, command_response(cmd, payload, cfg));
}
......@@ -726,7 +726,7 @@ namespace desfire {
const comm_cfg cfg{cipher_mode_from_security(security), default_comm_cfg().rx,
8 /* secure with legacy MAC only data */};
bin_data payload{prealloc(data.size() + 7)};
bin_data payload{prealloc(data.size() + 7)}; // TODO Borrow buffer
payload << fid << lsb24 << offset << lsb24 << data.size() << data;
return safe_drop_payload(command_code::write_record,
......@@ -758,7 +758,7 @@ namespace desfire {
}
// RX happens with the chosen file protection, except on nonlegacy ciphers where plain becomes maced
const auto rx_cipher_mode = cipher_mode_most_secure(cipher_mode_from_security(security), default_comm_cfg().rx);
bin_data payload{prealloc(record_count + 7)};
bin_data payload{prealloc(record_count + 7)}; // TODO Borrow buffer
payload << fid << lsb24 << record_index << lsb24 << record_count;
return command_response(command_code::read_records, payload, comm_cfg{default_comm_cfg().tx, rx_cipher_mode});
......
......@@ -16,7 +16,7 @@ namespace pn532 {
namespace {
[[nodiscard]] bin_data &get_clean_buffer() {
static bin_data _buffer{prealloc(384)};
static bin_data _buffer{prealloc(384)}; // TODO Borrow buffer
_buffer.clear();
return _buffer;
}
......
......@@ -27,15 +27,15 @@ namespace pn532 {
controller::result<bool> controller::diagnose_comm_line(ms timeout) {
PN532_LOGI("%s: running %s...", to_string(command_code::diagnose), to_string(bits::test::comm_line));
// Generate 256 bytes of random data to test
bin_data payload;
payload.resize(0xff);
std::iota(std::begin(payload), std::end(payload), 0x00);
auto payload = borrow_buffer(0xff);
payload->resize(0xff);
std::iota(std::begin(*payload), std::end(*payload), 0x00);
// Set the first byte to be the test number
payload[0] = static_cast<std::uint8_t>(bits::test::comm_line);
if (const auto res_cmd = chn().command_response(command_code::diagnose, /* copy */ payload, timeout); res_cmd) {
(*payload)[0] = static_cast<std::uint8_t>(bits::test::comm_line);
if (const auto res_cmd = chn().command_response(command_code::diagnose, /* copy */ *payload, timeout); res_cmd) {
// Test that the reurned data coincides
if (payload.size() == res_cmd->size() and
std::equal(std::begin(payload), std::end(payload), std::begin(*res_cmd))) {
if (payload->size() == res_cmd->size() and
std::equal(std::begin(*payload), std::end(*payload), std::begin(*res_cmd))) {
PN532_LOGI("%s: %s test succeeded.", to_string(command_code::diagnose), to_string(bits::test::comm_line));
return true;
} else {
......@@ -54,6 +54,7 @@ namespace pn532 {
channel &chn, bits::test test, std::uint8_t expected, ms timeout,
std::size_t expected_body_size = 0, Args &&...append_to_body) {
PN532_LOGI("%s: running %s...", to_string(command_code::diagnose), to_string(test));
// TODO Investigate whether this can become a borrowed buffer
bin_data payload = bin_data::chain(prealloc(expected_body_size + 1), test,
std::forward<Args>(append_to_body)...);
if (const auto res_cmd = chn.command_response(command_code::diagnose, std::move(payload), timeout); res_cmd) {
......@@ -312,6 +313,14 @@ namespace pn532 {
return chn().command_response(command_code::rf_configuration, std::move(payload), timeout);
}
borrowed_buffer controller::borrow_buffer(std::size_t prealloc_size) const {
auto buffer = _pool->take();
if (prealloc_size < std::numeric_limits<std::size_t>::max()) {
buffer << prealloc(prealloc_size);
}
return buffer;
}
std::uint8_t controller::get_target(command_code cmd, std::uint8_t target_logical_index, bool expect_more_data) {
if (target_logical_index > bits::max_num_targets) {
PN532_LOGE("%s: out of range (unsupported) logical target index %u (> %u).",
......
......@@ -6,6 +6,7 @@
#include "ut/test_pn532.hpp"
#include <mbcontroller.h>
#include <unity.h>
#include <mlab/pool.hpp>
#define TEST_TAG "UT"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment