|
| 1 | +/** @brief Mock UART interface for testing purposes. */ |
| 2 | + |
| 3 | +#ifndef CSUP_TESTS_MOCKS_UART_HPP |
| 4 | +#define CSUP_TESTS_MOCKS_UART_HPP |
| 5 | + |
| 6 | +#include "csup/hal/interfaces/uart.hpp" |
| 7 | + |
| 8 | +#include <chrono> |
| 9 | +#include <iostream> |
| 10 | +#include <memory> |
| 11 | +#include <mutex> |
| 12 | +#include <queue> |
| 13 | +#include <thread> |
| 14 | + |
| 15 | +namespace csup { namespace hal { |
| 16 | + |
| 17 | +/** @brief Thread-safe queue for simulating UART buffers. */ |
| 18 | +struct MutexQueue |
| 19 | +{ |
| 20 | + std::queue<uint8_t> q; |
| 21 | + std::mutex mtx; |
| 22 | + |
| 23 | + void push(uint8_t val) |
| 24 | + { |
| 25 | + std::lock_guard<std::mutex> lock(mtx); |
| 26 | + q.push(val); |
| 27 | + } |
| 28 | + |
| 29 | + void push(const uint8_t* data, size_t size) |
| 30 | + { |
| 31 | + std::lock_guard<std::mutex> lock(mtx); |
| 32 | + for (size_t i = 0; i < size; ++i) |
| 33 | + q.push(data[i]); |
| 34 | + } |
| 35 | + |
| 36 | + void pop() |
| 37 | + { |
| 38 | + std::lock_guard<std::mutex> lock(mtx); |
| 39 | + if (!q.empty()) |
| 40 | + q.pop(); |
| 41 | + } |
| 42 | + |
| 43 | + uint8_t front() |
| 44 | + { |
| 45 | + std::lock_guard<std::mutex> lock(mtx); |
| 46 | + return q.front(); |
| 47 | + } |
| 48 | + |
| 49 | + bool empty() |
| 50 | + { |
| 51 | + std::lock_guard<std::mutex> lock(mtx); |
| 52 | + return q.empty(); |
| 53 | + } |
| 54 | + |
| 55 | + size_t size() |
| 56 | + { |
| 57 | + std::lock_guard<std::mutex> lock(mtx); |
| 58 | + return q.size(); |
| 59 | + } |
| 60 | + |
| 61 | + void clear() |
| 62 | + { |
| 63 | + std::lock_guard<std::mutex> lock(mtx); |
| 64 | + while (!q.empty()) |
| 65 | + q.pop(); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +/** @brief Mock UART implementation for testing. */ |
| 70 | +class MockUart : public IUart<MockUart> |
| 71 | +{ |
| 72 | +public: |
| 73 | + MockUart() = default; |
| 74 | + ~MockUart() = default; |
| 75 | + |
| 76 | + bool open_impl() |
| 77 | + { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + void close_impl() {} |
| 82 | + |
| 83 | + bool read_impl(types::u8& byte) |
| 84 | + { |
| 85 | + if (!rx_buffer_) |
| 86 | + return false; |
| 87 | + |
| 88 | + auto start = std::chrono::steady_clock::now(); |
| 89 | + auto timeout = std::chrono::milliseconds(cfg_.timeout); |
| 90 | + auto check_interval = std::chrono::milliseconds(10); |
| 91 | + |
| 92 | + do |
| 93 | + { |
| 94 | + if (!rx_buffer_->empty()) |
| 95 | + { |
| 96 | + byte = rx_buffer_->front(); |
| 97 | + rx_buffer_->pop(); |
| 98 | + return true; |
| 99 | + } |
| 100 | + std::this_thread::sleep_for(check_interval); |
| 101 | + } |
| 102 | + while (std::chrono::steady_clock::now() - start < timeout); |
| 103 | + |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + bool write_impl(const types::u8* buffer, types::size size) |
| 108 | + { |
| 109 | + if (!tx_buffer_) |
| 110 | + return false; |
| 111 | + |
| 112 | + tx_buffer_->push(buffer, size); |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + bool write_impl(types::u8 byte) |
| 117 | + { |
| 118 | + if (!tx_buffer_) |
| 119 | + return false; |
| 120 | + |
| 121 | + tx_buffer_->push(byte); |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + types::size available_impl() |
| 126 | + { |
| 127 | + return rx_buffer_ ? rx_buffer_->size() : 0; |
| 128 | + } |
| 129 | + |
| 130 | + void flush_impl() |
| 131 | + { |
| 132 | + if (rx_buffer_) |
| 133 | + rx_buffer_->clear(); |
| 134 | + } |
| 135 | + |
| 136 | + void connect(std::shared_ptr<MutexQueue> tx_buffer, std::shared_ptr<MutexQueue> rx_buffer) |
| 137 | + { |
| 138 | + tx_buffer_ = tx_buffer; |
| 139 | + rx_buffer_ = rx_buffer; |
| 140 | + } |
| 141 | + |
| 142 | +private: |
| 143 | + std::shared_ptr<MutexQueue> tx_buffer_; |
| 144 | + std::shared_ptr<MutexQueue> rx_buffer_; |
| 145 | +}; |
| 146 | + |
| 147 | + |
| 148 | +}} // namespace csup::hal |
| 149 | + |
| 150 | +#endif |
0 commit comments