Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
uart_requirements.hpp
Go to the documentation of this file.
1/**
2 * @file uart_requirements.hpp
3 * @brief UART driver requirements validator
4 * @ingroup validation
5 *
6 * Validates that UART drivers provide required functionality using the
7 * method detection system. This is the core UART contract enforcement
8 * for Sound Byte Libs drivers.
9 *
10 * ## UART Driver Contract
11 *
12 * Every Sound Byte Libs UART driver MUST implement these methods:
13 * - `init(const UartHandle&)` - Initialize UART with manifest-resolved handle
14 * - `init_rx(const UartHandle&)` - Initialize UART for RX-only (MIDI, sensors)
15 * - `write_byte(uint8_t)` - Write single byte (blocking)
16 * - `write(const uint8_t*, size_t)` - Write buffer (blocking)
17 * - `write_string(const char*)` - Write null-terminated string (blocking)
18 * - `available() -> bool` - Check if RX data is available
19 * - `read_byte() -> uint8_t` - Read single byte from RX buffer
20 *
21 * Optional methods (validated if present):
22 * - `try_write_byte(uint8_t) -> bool` - Non-spinning TX
23 * - `try_write_string(const char*) -> size_t` - Non-spinning TX
24 *
25 * ## Validation Usage
26 *
27 * Add validation at the bottom of driver uart.hpp:
28 * ```cpp
29 * #include <sbl/hw/validation/uart_requirements.hpp>
30 * static_assert(sbl::validation::uart_driver_valid<sbl::driver::Uart<>>,
31 * "UART driver missing required methods");
32 * ```
33 *
34 * @see method_detection.hpp
35 */
36
37#ifndef SBL_HW_VALIDATION_UART_REQUIREMENTS_HPP_
38#define SBL_HW_VALIDATION_UART_REQUIREMENTS_HPP_
39
40#include "method_detection.hpp"
41
42namespace sbl {
43namespace validation {
44
45/**
46 * @brief UART driver requirements validator
47 *
48 * Validates that UART drivers provide all required methods with correct
49 * signatures and return types.
50 */
51template<typename UartDriver>
53public:
54 /**
55 * @brief Validate all required UART methods
56 */
57 static constexpr bool validate() {
58 // Required methods (all platforms)
59
61 "SBL ERROR: UART driver missing init() method.\n"
62 "Required: static void/bool init(const sbl::UartHandle& handle)");
63
65 "SBL ERROR: UART driver missing init_rx() method.\n"
66 "Required: static void/bool init_rx(const sbl::UartHandle& handle)");
67
68 static_assert(has_uart_write_byte<UartDriver>::value, "SBL ERROR: UART driver missing write_byte() method.\n"
69 "Required: static void write_byte(uint8_t byte)");
70
72 "SBL ERROR: UART driver missing write() method.\n"
73 "Required: static void write(const uint8_t* data, size_t len)");
74
76 "SBL ERROR: UART driver missing write_string() method.\n"
77 "Required: static void write_string(const char* str)");
78
79 static_assert(has_uart_available<UartDriver>::value, "SBL ERROR: UART driver missing available() method.\n"
80 "Required: static bool available()");
81
82 static_assert(has_uart_read_byte<UartDriver>::value, "SBL ERROR: UART driver missing read_byte() method.\n"
83 "Required: static uint8_t read_byte()");
84
85 // Return type validation
86
87 static_assert(uart_available_returns_bool<UartDriver>::value, "SBL ERROR: UART available() must return bool.");
88
90 "SBL ERROR: UART read_byte() must return uint8_t.");
91
92 // Optional methods (validated if present)
93
96 "SBL ERROR: If UART provides try_write_byte(), it must return bool.");
97
98 return true;
99 }
100};
101
102/**
103 * @brief Helper to validate a UART driver implementation
104 *
105 * Usage:
106 * ```cpp
107 * static_assert(sbl::validation::uart_driver_valid<MyUartDriver>,
108 * "UART driver incomplete");
109 * ```
110 */
111template<typename UartDriver>
113
114} // namespace validation
115} // namespace sbl
116
117#endif // SBL_HW_VALIDATION_UART_REQUIREMENTS_HPP_
UART driver requirements validator.
static constexpr bool validate()
Validate all required UART methods.
Method and interface detection for driver validation.
constexpr bool uart_driver_valid
Helper to validate a UART driver implementation.
Root namespace for all Sound Byte Libs code.
True if T::available() is a valid static call.
True if T::init_rx(const UartHandle&) is a valid static call.
True if T::init(const UartHandle&) is a valid static call.
True if T::read_byte() is a valid static call.
True if T::try_write_byte(uint8_t) is a valid static call (optional)
True if T::write_byte(uint8_t) is a valid static call.
True if T::write_string(const char*) is a valid static call.
True if T::write(const uint8_t*, std::size_t) is a valid static call.
True if T::available() returns bool.
True if T::read_byte() returns uint8_t.
True if T::try_write_byte(uint8_t) returns bool.