|
| 1 | +// Copyright 2024 - 2025 Khalil Estell and the libhal contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <atomic> |
| 18 | +#include <memory_resource> |
| 19 | +#include <span> |
| 20 | +#include <thread> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include <libhal/pointers.hpp> |
| 24 | +#include <libhal/serial.hpp> |
| 25 | +#include <libhal/units.hpp> |
| 26 | + |
| 27 | +namespace hal::mac::inline v1 { |
| 28 | +/** |
| 29 | + * @brief Serial communication interface using macOS console (stdin/stdout) |
| 30 | + * |
| 31 | + * Provides a way to simulate serial communication on macOS by redirecting |
| 32 | + * serial I/O operations to the console. This is particularly useful for |
| 33 | + * testing serial-based communication protocols and debugging embedded |
| 34 | + * applications that rely on serial interfaces. |
| 35 | + * |
| 36 | + * The implementation uses a background thread to continuously read from stdin |
| 37 | + * and store data in a circular buffer, while write operations are sent |
| 38 | + * directly to stdout. |
| 39 | + */ |
| 40 | +class console_serial : public hal::v5::serial |
| 41 | +{ |
| 42 | +public: |
| 43 | + /** |
| 44 | + * @brief Create a console serial instance |
| 45 | + * |
| 46 | + * @param p_allocator Memory allocator for internal buffer management |
| 47 | + * @param p_buffer_size Size of the internal circular receive buffer (min: 32) |
| 48 | + * @return A strong_ptr to the created console_serial instance |
| 49 | + */ |
| 50 | + [[nodiscard]] static hal::v5::strong_ptr<console_serial> create( |
| 51 | + std::pmr::polymorphic_allocator<> p_allocator, |
| 52 | + hal::usize p_buffer_size); |
| 53 | + |
| 54 | + /** |
| 55 | + * @brief Public constructor - but use create() instead |
| 56 | + */ |
| 57 | + console_serial(hal::v5::strong_ptr_only_token, |
| 58 | + std::pmr::polymorphic_allocator<> p_allocator, |
| 59 | + hal::usize p_buffer_size); |
| 60 | + |
| 61 | + /** |
| 62 | + * @brief Destroy the console serial object |
| 63 | + * |
| 64 | + * Stops the background receive thread and waits for it to complete before |
| 65 | + * destruction. |
| 66 | + */ |
| 67 | + ~console_serial() override; |
| 68 | + |
| 69 | + // Non-copyable and non-movable |
| 70 | + console_serial(console_serial const&) = delete; |
| 71 | + console_serial& operator=(console_serial const&) = delete; |
| 72 | + console_serial(console_serial&&) = delete; |
| 73 | + console_serial& operator=(console_serial&&) = delete; |
| 74 | + |
| 75 | +private: |
| 76 | + void driver_configure(hal::v5::serial::settings const& p_settings) override; |
| 77 | + void driver_write(std::span<hal::byte const> p_data) override; |
| 78 | + std::span<hal::byte const> driver_receive_buffer() override; |
| 79 | + hal::usize driver_cursor() override; |
| 80 | + |
| 81 | + /** |
| 82 | + * @brief Background thread function for reading from stdin |
| 83 | + */ |
| 84 | + void receive_thread_function(); |
| 85 | + |
| 86 | + /// Memory allocator for buffer management |
| 87 | + std::pmr::polymorphic_allocator<> m_allocator; |
| 88 | + /// Circular buffer for storing received data from stdin |
| 89 | + std::pmr::vector<hal::byte> m_receive_buffer; |
| 90 | + /// Atomic cursor position for thread-safe buffer access |
| 91 | + std::atomic<hal::usize> m_receive_cursor{ 0 }; |
| 92 | + /// Atomic flag to signal thread termination |
| 93 | + std::atomic<bool> m_stop_thread{ false }; |
| 94 | + /// Background thread for reading from stdin |
| 95 | + std::thread m_receive_thread; |
| 96 | +}; |
| 97 | +} // namespace hal::mac::inline v1 |
0 commit comments