cryptnox-sdk-esp32 1.0.0
ESP32 SDK for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
ESP32Logger.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: LGPL-3.0-or-later
3 * Copyright (c) 2026 Cryptnox SA
4 */
5
16
17/******************************************************************
18 * 1. Included files
19 ******************************************************************/
20
21#include "ESP32Logger.h"
22
23#include <stdint.h>
24#include <stddef.h>
25#include <stdio.h>
26#include <string.h>
27#include "driver/uart.h"
28
29/******************************************************************
30 * 2. Module constants
31 ******************************************************************/
32
33static const uart_port_t UART_LOG_PORT = UART_NUM_0;
34static const uint8_t UART_RX_FLOW_THRESH_NONE = static_cast<uint8_t>(0U);
35
36/* 32 binary digits + NUL terminator */
37static const uint32_t NUM_BUF_SIZE = 33U;
38static const uint32_t NUM_BASE_MIN = 2U;
39static const uint32_t NUM_BASE_MAX = 16U;
40static const size_t ELEMENT_SIZE = 1U;
41
42static const char LOGGER_NEWLINE[] = "\r\n";
43static const char HEX_CHARS[] = "0123456789ABCDEF";
44
45/******************************************************************
46 * 3. Static helpers
47 ******************************************************************/
48
50static uint32_t clamp_base(int base)
51{
52 uint32_t result = static_cast<uint32_t>(DEC);
53 bool in_range = ((static_cast<uint32_t>(base) >= NUM_BASE_MIN) &&
54 (static_cast<uint32_t>(base) <= NUM_BASE_MAX));
55 if (in_range) {
56 result = static_cast<uint32_t>(base);
57 }
58 return result;
59}
60
62static void uart_write_str(const char *str)
63{
64 size_t len = strlen(str);
65 (void)fwrite(str, ELEMENT_SIZE, len, stdout);
66}
67
69static void write_uint_to_uart(uint32_t value, uint32_t base)
70{
71 char buf[NUM_BUF_SIZE] = { 0 };
72 uint32_t pos = NUM_BUF_SIZE - 1U;
73 uint32_t v = value;
74
75 buf[pos] = '\0';
76
77 if (v == 0U) {
78 pos--;
79 buf[pos] = '0';
80 } else {
81 while (v > 0U) {
82 pos--;
83 buf[pos] = HEX_CHARS[v % base];
84 v = v / base;
85 }
86 }
87
88 (void)fputs(&buf[pos], stdout);
89}
90
91/******************************************************************
92 * 4. Public method implementations
93 ******************************************************************/
94
95bool ESP32Logger::begin(unsigned long baudRate)
96{
97 uart_config_t cfg = {};
98 cfg.baud_rate = static_cast<int>(baudRate);
99 cfg.data_bits = UART_DATA_8_BITS;
100 cfg.parity = UART_PARITY_DISABLE;
101 cfg.stop_bits = UART_STOP_BITS_1;
102 cfg.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
103 cfg.rx_flow_ctrl_thresh = UART_RX_FLOW_THRESH_NONE;
104 cfg.source_clk = UART_SCLK_DEFAULT;
105
106 esp_err_t err = uart_param_config(UART_LOG_PORT, &cfg);
107 m_initialized = (err == ESP_OK);
108
109 return m_initialized;
110}
111
112void ESP32Logger::print(const __FlashStringHelper *str)
113{
114 if (m_initialized) {
115 uart_write_str(reinterpret_cast<const char *>(str));
116 }
117}
118
119void ESP32Logger::print(const char *str)
120{
121 if (m_initialized) {
122 uart_write_str(str);
123 }
124}
125
127{
128 if (m_initialized) {
129 (void)fputc(static_cast<int>(c), stdout);
130 }
131}
132
133void ESP32Logger::print(uint8_t value, int base)
134{
135 if (m_initialized) {
136 uint32_t safe_base = clamp_base(base);
137 write_uint_to_uart(static_cast<uint32_t>(value), safe_base);
138 }
139}
140
141void ESP32Logger::print(uint16_t value, int base)
142{
143 if (m_initialized) {
144 uint32_t safe_base = clamp_base(base);
145 write_uint_to_uart(static_cast<uint32_t>(value), safe_base);
146 }
147}
148
149void ESP32Logger::print(uint32_t value, int base)
150{
151 if (m_initialized) {
152 uint32_t safe_base = clamp_base(base);
153 write_uint_to_uart(value, safe_base);
154 }
155}
156
157void ESP32Logger::print(int value, int base)
158{
159 if (m_initialized) {
160 uint32_t safe_base = clamp_base(base);
161 bool is_negative_decimal = ((value < 0) && (safe_base == static_cast<uint32_t>(DEC)));
162 if (is_negative_decimal) {
163 (void)fputc(static_cast<int>('-'), stdout);
164 /* Two's-complement negation — avoids UB on INT_MIN */
165 uint32_t abs_val = (~static_cast<uint32_t>(value)) + 1U;
166 write_uint_to_uart(abs_val, safe_base);
167 } else {
168 write_uint_to_uart(static_cast<uint32_t>(value), safe_base);
169 }
170 }
171}
172
174{
175 if (m_initialized) {
177 }
178}
179
180void ESP32Logger::println(const __FlashStringHelper *str)
181{
182 print(str);
183 println();
184}
185
186void ESP32Logger::println(const char *str)
187{
188 print(str);
189 println();
190}
191
193{
194 print(c);
195 println();
196}
197
198void ESP32Logger::println(uint8_t value, int base)
199{
200 print(value, base);
201 println();
202}
203
204void ESP32Logger::println(uint16_t value, int base)
205{
206 print(value, base);
207 println();
208}
209
210void ESP32Logger::println(uint32_t value, int base)
211{
212 print(value, base);
213 println();
214}
215
216void ESP32Logger::println(int value, int base)
217{
218 print(value, base);
219 println();
220}
static const char HEX_CHARS[]
static const uart_port_t UART_LOG_PORT
static const size_t ELEMENT_SIZE
static uint32_t clamp_base(int base)
static const uint32_t NUM_BASE_MAX
static const uint32_t NUM_BUF_SIZE
static void uart_write_str(const char *str)
static void write_uint_to_uart(uint32_t value, uint32_t base)
static const char LOGGER_NEWLINE[]
static const uint32_t NUM_BASE_MIN
static const uint8_t UART_RX_FLOW_THRESH_NONE
CW_Logger implementation that writes to ESP32 UART0 via printf.
bool begin(unsigned long baudRate=115200UL) override
Initialise UART0 at the given baud rate.
void println() override
Print a CR+LF newline sequence.
bool m_initialized
true after a successful begin call.
void print(const __FlashStringHelper *str) override
Print a PROGMEM string.