Skip to content

Commit 553c847

Browse files
committed
Refactor shared utils and cleanup warnings
Moves common error-printing and string constants into shared utils headers, then updates network, remoteconfig, EMAC, and timer code to use them. Also tightens several conditional compilation checks, replaces magic values with named constants, and cleans up a few small naming and formatting issues.
1 parent 0d8204d commit 553c847

15 files changed

Lines changed: 178 additions & 116 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @file utils_print.h
3+
*
4+
*/
5+
/* Copyright (C) 2026 by Arjan van Vught mailto:info@gd32-dmx.org
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
#ifndef COMMON_UTILS_UTILS_PRINT_H_
27+
#define COMMON_UTILS_UTILS_PRINT_H_
28+
29+
#include <cstdio>
30+
31+
#include "firmware/ansi_colour.h"
32+
33+
namespace common::print {
34+
inline void Error(const char* func, const char* string) {
35+
printf("%s%s: %s%s\n", ansi::Colours::Fg::kRed, func, string, ansi::Colours::Fg::kDefault);
36+
}
37+
} // namespace common::print
38+
39+
#define ERROR(s) \
40+
do { \
41+
common::print::Error(__func__, (s)); \
42+
} while (false)
43+
44+
#endif // COMMON_UTILS_UTILS_PRINT_H_

common/include/common/utils/utils_string.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
#include <cstdint>
3030

3131
namespace common {
32+
inline constexpr char kWarning[] = "Warning";
33+
inline constexpr char kError[] = "Error";
34+
inline constexpr char kSuccess[] = "Success";
35+
inline constexpr char kUnknown[] = "Unknown";
36+
37+
constexpr const char* IsSuccess(bool is_success) {
38+
return is_success ? kSuccess : kError;
39+
}
40+
3241
constexpr uint32_t ConstStrLen(const char* str) {
3342
uint32_t len = 0;
3443
while (str[len] != '\0') {

common/include/common/utils/utils_units.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@
2929
#include <cstdint>
3030

3131
namespace common::units {
32-
inline constexpr uint32_t kUsPerMs = 1'000U;
33-
inline constexpr uint32_t kMsPerSecond = 1'000U;
34-
inline constexpr uint32_t kUsPerSecond = 1'000'000U;
32+
inline constexpr int32_t kSecondPerMinute = 60;
3533

36-
inline constexpr uint32_t kNsPerUs = 1'000U;
37-
inline constexpr uint32_t kNsPerMs = 1'000'000U;
38-
inline constexpr uint32_t kNsPerSecond = 1'000'000'000U;
34+
inline constexpr uint32_t kMsPerSecond = 1'000;
35+
36+
inline constexpr uint32_t kUsPerMs = 1'000;
37+
inline constexpr uint32_t kUsPerSecond = 1'000'000;
38+
39+
inline constexpr uint32_t kNsPerUs = 1'000;
40+
inline constexpr uint32_t kNsPerMs = 1'000'000;
41+
inline constexpr uint32_t kNsPerSecond = 1'000'000'000;
3942

4043
inline constexpr uint32_t k1KiB = 1024;
4144

lib-flashcodeinstall/src/flashcodeinstall.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "firmware.h"
3131
#include "display.h" // IWYU pragma: keep
3232
#include "watchdog.h"
33+
#include "common/utils/utils_print.h"
3334

3435
bool FlashCodeInstall::WriteFirmware(std::span<const uint8_t> firmware) {
3536
FLASHCODE_INSTALL_DEBUG_ENTRY();
@@ -68,7 +69,7 @@ bool FlashCodeInstall::WriteFirmware(std::span<const uint8_t> firmware) {
6869
}
6970

7071
if (flashcode::Result::kError == result) {
71-
puts("Error: flash erase");
72+
ERROR("flash erase");
7273
return false;
7374
}
7475

@@ -78,7 +79,7 @@ bool FlashCodeInstall::WriteFirmware(std::span<const uint8_t> firmware) {
7879
}
7980

8081
if (flashcode::Result::kError == result) {
81-
puts("Error: flash write");
82+
ERROR("flash write");
8283
return false;
8384
}
8485

lib-network/src/core/ipv4/dhcp.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "core/ip4/acd.h"
4242
#endif // CONFIG_NET_DHCP_USE_ACD
4343
#include "firmware/debug/debug_debug.h"
44+
#include "common/utils/utils_print.h"
4445

4546
#ifdef DEBUG_NETWORK_DHCP
4647
#define DHCP_DEBUG_ENTRY() DEBUG_ENTRY()
@@ -68,9 +69,11 @@ static TimerHandle_t s_timer_id;
6869

6970
// https://tools.ietf.org/html/rfc1541
7071
namespace network::dhcp {
71-
static Message s_dhcp_message SECTION_NETWORK ALIGNED;
72+
static constexpr char kNoHandle[] = "No handle";
73+
namespace {
74+
Message s_dhcp_message SECTION_NETWORK ALIGNED;
7275

73-
static void MessageInit() {
76+
void MessageInit() {
7477
std::memset(&s_dhcp_message, 0, sizeof(dhcp::Message));
7578

7679
s_dhcp_message.op = dhcp::OpCode::kBootrequest;
@@ -87,7 +90,7 @@ static void MessageInit() {
8790
s_dhcp_message.options[5] = 0x01;
8891
}
8992

90-
static void UpdateMsg(uint8_t message_type) {
93+
void UpdateMsg(uint8_t message_type) {
9194
auto* dhcp = reinterpret_cast<struct dhcp::Dhcp*>(netif::global::netif_default.dhcp);
9295
assert(dhcp != nullptr);
9396

@@ -111,7 +114,7 @@ static void UpdateMsg(uint8_t message_type) {
111114
}
112115
}
113116

114-
static void SendDiscover() {
117+
void SendDiscover() {
115118
DHCP_DEBUG_ENTRY();
116119
auto* dhcp = reinterpret_cast<struct dhcp::Dhcp*>(netif::global::netif_default.dhcp);
117120
assert(dhcp != nullptr);
@@ -147,7 +150,7 @@ static void SendDiscover() {
147150
DHCP_DEBUG_EXIT();
148151
}
149152

150-
static void SendRequest() {
153+
void SendRequest() {
151154
DHCP_DEBUG_ENTRY();
152155
auto* dhcp = reinterpret_cast<struct dhcp::Dhcp*>(netif::global::netif_default.dhcp);
153156
assert(dhcp != nullptr);
@@ -199,7 +202,7 @@ static void SendRequest() {
199202
DHCP_DEBUG_EXIT();
200203
}
201204

202-
static void SendRelease(uint32_t destination_ip) {
205+
void SendRelease(uint32_t destination_ip) {
203206
DHCP_DEBUG_ENTRY();
204207
DHCP_DEBUG_PRINTF(IPSTR, IP2STR(destination_ip));
205208

@@ -221,6 +224,7 @@ static void SendRelease(uint32_t destination_ip) {
221224

222225
DHCP_DEBUG_EXIT();
223226
}
227+
} // namespace
224228

225229
void Input(const uint8_t* buffer, uint32_t size, [[maybe_unused]] uint32_t from_ip, uint16_t from_port) {
226230
DHCP_DEBUG_ENTRY();
@@ -250,7 +254,7 @@ void Inform() {
250254

251255
const auto kHandle = network::udp::Begin(network::iana::Ports::kPortDhcpClient, nullptr);
252256
if (kHandle < 0) {
253-
ERROR("No handle\n");
257+
ERROR(kNoHandle);
254258
return;
255259
}
256260

@@ -689,14 +693,14 @@ bool Start() {
689693
dhcp->handle = network::udp::Begin(network::iana::Ports::kPortDhcpClient, dhcp::Input);
690694

691695
if (dhcp->handle < 0) {
692-
ERROR("No handle.\n");
696+
ERROR(kNoHandle);
693697
DHCP_DEBUG_EXIT();
694698
return false;
695699
}
696700

697701
MessageInit();
698702

699-
#if defined(CONFIG_NET_DHCP_USE_ACD)
703+
#ifdef CONFIG_NET_DHCP_USE_ACD
700704
network::acd::Add(&dhcp->acd, ConflictCallback);
701705
#endif // CONFIG_NET_DHCP_USE_ACD
702706

lib-network/src/core/ipv4/igmp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "core/protocol/igmp.h"
4444
#include "softwaretimers.h" // IWYU pragma: keep
4545
#include "firmware/debug/debug_debug.h"
46+
#include "common/utils/utils_print.h"
4647

4748
#ifdef DEBUG_NETWORK_IGMP
4849
#define IGMP_DEBUG_ENTRY() DEBUG_ENTRY()

lib-network/src/core/network_memory.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,24 @@
3030
#include <cstring>
3131
#include <cassert>
3232

33-
#include "network_private.h"
33+
#include "common/utils/utils_print.h"
3434

3535
namespace network::memory {
36+
inline constexpr uint32_t kBlocksMin = 1;
37+
inline constexpr uint32_t kBlocksMax = 32;
38+
3639
inline constexpr uint32_t kBlocks =
37-
#if !defined(CONFIG_NETWORK_MEMORY_BLOCKS)
40+
#ifndef CONFIG_NETWORK_MEMORY_BLOCKS
3841
12;
3942
#else
4043
CONFIG_NETWORK_MEMORY_BLOCKS;
4144
#endif // CONFIG_NETWORK_MEMORY_BLOCKS
4245

43-
static_assert(kBlocks >= 1);
44-
static_assert(kBlocks <= 32);
46+
static_assert(kBlocks >= kBlocksMin);
47+
static_assert(kBlocks <= kBlocksMax);
4548

4649
inline constexpr uint32_t kBlockSize =
47-
#if !defined(CONFIG_NETWORK_MEMORY_BLOCKSIZE)
50+
#ifndef CONFIG_NETWORK_MEMORY_BLOCKSIZE
4851
1460;
4952
#else
5053
CONFIG_NETWORK_MEMORY_BLOCKSIZE;
@@ -79,16 +82,16 @@ class Allocator {
7982
Allocator(Allocator&&) = delete;
8083
Allocator& operator=(Allocator&&) = delete;
8184

82-
bool IsEmpty() const { return free_mask_ == kAllMask; }
83-
bool IsFull() const { return free_mask_ == 0; }
85+
[[nodiscard]] bool IsEmpty() const { return free_mask_ == kAllMask; }
86+
[[nodiscard]] bool IsFull() const { return free_mask_ == 0; }
8487

8588
uint8_t* Allocate() {
8689
if (IsFull()) {
87-
network::Error(__func__, "Allocate:Full!");
90+
ERROR("Allocate:Full!");
8891
return nullptr;
8992
}
9093

91-
const uint32_t kIndex = static_cast<uint32_t>(__builtin_ctz(free_mask_));
94+
const auto kIndex = static_cast<uint32_t>(__builtin_ctz(free_mask_));
9295
free_mask_ &= ~(1U << kIndex);
9396

9497
Status();
@@ -102,11 +105,11 @@ class Allocator {
102105
assert(size <= kBlockSize);
103106

104107
if (IsFull()) {
105-
network::Error(__func__, "Allocate:Full!");
108+
ERROR("Allocate:Full!");
106109
return UINT16_MAX;
107110
}
108111

109-
const uint32_t kIndex = static_cast<uint32_t>(__builtin_ctz(free_mask_));
112+
const auto kIndex = static_cast<uint32_t>(__builtin_ctz(free_mask_));
110113
free_mask_ &= ~(1U << kIndex);
111114

112115
size_[kIndex] = size;
@@ -155,7 +158,7 @@ class Allocator {
155158
}
156159

157160
void Status() const {
158-
#if defined DEBUG_NETWORK_MEMORY
161+
#ifdef DEBUG_NETWORK_MEMORY
159162
const uint32_t kUsedMask = (~free_mask_) & kAllMask;
160163
printf("free_mask=0x%08x used_mask=0x%08x free=%u used=%u\n", free_mask_, kUsedMask, __builtin_popcount(free_mask_), __builtin_popcount(kUsedMask));
161164
printf("IsEmpty=%c IsFull=%c\n", IsEmpty() ? 'Y' : 'N', IsFull() ? 'Y' : 'N');
@@ -164,7 +167,7 @@ class Allocator {
164167

165168
private:
166169
Allocator() = default;
167-
static constexpr uint32_t kAllMask = (kBlocks == 32) ? UINT32_MAX : ((1U << kBlocks) - 1U);
170+
static constexpr uint32_t kAllMask = (kBlocks == kBlocksMax) ? UINT32_MAX : ((1U << kBlocks) - 1U);
168171
uint32_t free_mask_{0};
169172
uint16_t size_[kBlocks]{0};
170173
};

lib-network/src/core/network_private.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ void FreePkt();
5353
} // namespace emac::eth
5454

5555
namespace network {
56-
inline void Error(const char* func, const char* string) {
57-
printf("%s%s: %s%s\n", ansi::Colours::Fg::kRed, func, string, ansi::Colours::Fg::kDefault);
58-
}
59-
60-
#define ERROR(s) Error(__func__, (s))
61-
6256
namespace global {
6357
extern uint32_t broadcast_mask;
6458
extern uint32_t on_network_mask;

lib-network/src/core/tcp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "network_memory.h"
7373
#include "network_tcp_datasegmentqueue.h"
7474
#include "common/utils/utils_math.h"
75+
#include "common/utils/utils_print.h"
7576

7677
#if defined(DEBUG_TCP)
7778
#define TCP_DEBUG_ENTRY() DEBUG_ENTRY()

lib-network/src/core/udp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "network_memcpy.h"
4444
#include "firmware/debug/debug_debug.h"
4545
#include "common/utils/utils_math.h"
46+
#include "common/utils/utils_print.h"
4647

4748
#if defined(DEBUG_UDP)
4849
#define UDP_DEBUG_ENTRY() DEBUG_ENTRY()

0 commit comments

Comments
 (0)