Sound Byte Libs 29c5ff3
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 (STM32 only)
23 * - `try_write_string(const char*) -> size_t` - Non-spinning TX (STM32 only)
24 *
25 * ## Validation Usage
26 *
27 * Add validation at the bottom of driver uart.hpp:
28 * ```cpp
29 * #include <sbl/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_VALIDATION_UART_REQUIREMENTS_HPP_
38#define SBL_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
69 "SBL ERROR: UART driver missing write_byte() method.\n"
70 "Required: static void write_byte(uint8_t byte)");
71
73 "SBL ERROR: UART driver missing write() method.\n"
74 "Required: static void write(const uint8_t* data, size_t len)");
75
77 "SBL ERROR: UART driver missing write_string() method.\n"
78 "Required: static void write_string(const char* str)");
79
81 "SBL ERROR: UART driver missing available() method.\n"
82 "Required: static bool available()");
83
85 "SBL ERROR: UART driver missing read_byte() method.\n"
86 "Required: static uint8_t read_byte()");
87
88 // --- Return type validation ---
89
91 "SBL ERROR: UART available() must return bool.");
92
94 "SBL ERROR: UART read_byte() must return uint8_t.");
95
96 // --- Optional methods (validated if present) ---
97
98 static_assert(
101 "SBL ERROR: If UART provides try_write_byte(), it must return bool.");
102
103 return true;
104 }
105};
106
107/**
108 * @brief Helper to validate a UART driver implementation
109 *
110 * Usage:
111 * ```cpp
112 * static_assert(sbl::validation::uart_driver_valid<MyUartDriver>,
113 * "UART driver incomplete");
114 * ```
115 */
116template<typename UartDriver>
118
119} // namespace validation
120} // namespace sbl
121
122#endif // SBL_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 functionality.
Definition aliases.hpp:24
UART Driver Method Detection.