Skip to content

Commit 3a7cee7

Browse files
committed
Refactor format helpers and add ConfigStore Reset
Rename and refactor JSON formatting helpers (FormatFloat -> Float, FormatUtcOffset -> UtcOffset), add Append2Digits helper, include <cassert>, and add bounds assertion; update callers (json_config_global.cpp). Add ConfigStore::Reset() to zero the store and mark it changed, and reformat kStateNames initialization. RemoteConfig: update copyright years, switch include to network_udp.h, call ConfigStore::Reset() from HandleFactory (replacing configstore::SetFactoryDefaults()), tidy formatting (kOutput array, spacing) and minor whitespace fixes. Also update copyright years in other files.
1 parent 00816e6 commit 3a7cee7

4 files changed

Lines changed: 75 additions & 32 deletions

File tree

common/include/json/json_format_helpers.h

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file json_format_helpers.h
33
*
44
*/
5-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -28,30 +28,46 @@
2828

2929
#include <cstdio>
3030
#include <cstdint>
31+
#include <cassert>
3132

3233
namespace format
3334
{
3435
constexpr size_t kFloatBufferSize = 8; // For "%.2f", "%.1f"
3536
constexpr size_t kOffsetBufferSize = 12; // For timezone offsets e.g. "+01:00"
3637

37-
[[nodiscard]] inline const char* FormatFloat(float value, char (&buf)[kFloatBufferSize], const char* fmt = "%.2f")
38+
inline void Append2Digits(char*& p, uint32_t v)
39+
{
40+
*p++ = static_cast<char>('0' + (v / 10));
41+
*p++ = static_cast<char>('0' + (v % 10));
42+
}
43+
44+
inline const char* Float(float value, char (&buf)[kFloatBufferSize], const char* fmt = "%.2f")
3845
{
3946
snprintf(buf, sizeof(buf), fmt, value);
4047
return buf;
4148
}
4249

43-
[[nodiscard]] inline const char* FormatUtcOffset(int32_t hours, uint32_t minutes, char (&buf)[kOffsetBufferSize])
50+
inline const char* UtcOffset(int32_t hours, uint32_t minutes, char (&buf)[kOffsetBufferSize])
4451
{
45-
if (hours <= 0)
46-
{
47-
snprintf(buf, sizeof(buf), "%.2d:%.2u", hours, minutes);
48-
}
49-
else
52+
const auto kNegative = hours < 0;
53+
if (kNegative) hours = -hours;
54+
55+
auto* p = buf;
56+
57+
if (hours != 0)
5058
{
51-
snprintf(buf, sizeof(buf), "+%.2d:%.2u", hours, minutes);
59+
*p++ = kNegative ? '-' : '+';
5260
}
61+
62+
Append2Digits(p, static_cast<uint32_t>(hours));
63+
*p++ = ':';
64+
Append2Digits(p, minutes);
65+
*p = '\0';
66+
67+
assert(static_cast<size_t>((p - buf) + 1) <= kOffsetBufferSize);
68+
5369
return buf;
5470
}
5571
} // namespace format
5672

57-
#endif // JSON_JSON_FORMAT_HELPERS_H_
73+
#endif // JSON_JSON_FORMAT_HELPERS_H_

lib-configstore/include/configstore.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@ class ConfigStore : StoreDevice
5555
kWriting
5656
};
5757

58-
[[maybe_unused]] static constexpr char kStateNames[7][16] = {"IDLE", "CHANGED", "CHANGED_WAITING", "ERASING", "ERASED", "ERASED_WAITING", "WRITING"};
58+
[[maybe_unused]] static constexpr char kStateNames[7][16] =
59+
{
60+
"IDLE",
61+
"CHANGED",
62+
"CHANGED_WAITING",
63+
"ERASING",
64+
"ERASED",
65+
"ERASED_WAITING",
66+
"WRITING"
67+
};
5968

6069
public:
6170
ConfigStore()
@@ -118,6 +127,12 @@ class ConfigStore : StoreDevice
118127
ConfigStore& operator=(ConfigStore&&) = delete;
119128

120129
~ConfigStore() = default;
130+
131+
void Reset()
132+
{
133+
memset(s_store, 0, sizeof(s_store));
134+
SetStatusChanged();
135+
}
121136

122137
bool Commit() { return Flash(); }
123138

lib-hal/src/json/json_config_global.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ uint32_t GetGlobal(char* buffer, uint32_t length) {
3737

3838
return json::helpers::Serialize(buffer, length, [&](JsonDoc& doc) {
3939
char offset[format::kOffsetBufferSize];
40-
doc[GlobalParamsConst::kUtcOffset.name] = format::FormatUtcOffset(hours, minutes, offset);
40+
doc[GlobalParamsConst::kUtcOffset.name] = format::UtcOffset(hours, minutes, offset);
4141
});
4242
}
4343

lib-remoteconfig/src/remoteconfig.cpp

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file remoteconfig.cpp
33
*
44
*/
5-
/* Copyright (C) 2019-2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2019-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -35,7 +35,7 @@
3535
#include "remoteconfig.h"
3636
#include "firmwareversion.h"
3737
#include "hal.h"
38-
#include "network.h"
38+
#include "network_udp.h"
3939
#if !defined(CONFIG_REMOTECONFIG_MINIMUM)
4040
#include "apps/mdns.h"
4141
#include "dmxnode_nodetype.h"
@@ -44,7 +44,7 @@
4444
#include "display.h"
4545
#include "configstore.h"
4646
#include "firmware/debug/debug_dump.h"
47-
#include "firmware/debug/debug_debug.h"
47+
#include "firmware/debug/debug_debug.h"
4848

4949
namespace remoteconfig::udp
5050
{
@@ -91,11 +91,24 @@ constexpr struct RemoteConfig::Commands RemoteConfig::kSet[] = {
9191
{&RemoteConfig::HandleDisplaySet, "display#", 8, true} //
9292
};
9393

94-
static constexpr char kOutput[static_cast<uint32_t>(remoteconfig::Output::LAST)][12] = {"DMX", "RDM", "Monitor", "Pixel", "TimeCode", "OSC", "Config",
95-
"Stepper", "Player", "Art-Net", "Serial", "RGB Panel", "PWM"};
94+
static constexpr char kOutput[static_cast<uint32_t>(remoteconfig::Output::LAST)][12] =
95+
{
96+
"DMX",
97+
"RDM",
98+
"Monitor",
99+
"Pixel",
100+
"TimeCode",
101+
"OSC",
102+
"Config",
103+
"Stepper",
104+
"Player",
105+
"Art-Net",
106+
"Serial",
107+
"RGB Panel",
108+
"PWM"
109+
};
96110

97-
RemoteConfig::RemoteConfig(remoteconfig::Output output, uint32_t active_outputs)
98-
: output_(output), active_outputs_(active_outputs)
111+
RemoteConfig::RemoteConfig(remoteconfig::Output output, uint32_t active_outputs) : output_(output), active_outputs_(active_outputs)
99112
{
100113
DEBUG_ENTRY();
101114

@@ -104,7 +117,7 @@ RemoteConfig::RemoteConfig(remoteconfig::Output output, uint32_t active_outputs)
104117
assert(s_this == nullptr);
105118
s_this = this;
106119

107-
network::iface::CopyMacAddressTo(s_list.mac_address);
120+
network::iface::CopyMacAddressTo(s_list.mac_address);
108121
s_list.output = static_cast<uint8_t>(output);
109122
s_list.active_outputs = static_cast<uint8_t>(active_outputs);
110123

@@ -143,7 +156,6 @@ RemoteConfig::~RemoteConfig()
143156
delete http_daemon_;
144157
}
145158
#endif
146-
147159
network::apps::mdns::ServiceRecordDelete(network::apps::mdns::Services::kConfig);
148160
#endif
149161

@@ -156,31 +168,31 @@ RemoteConfig::~RemoteConfig()
156168
void RemoteConfig::SetDisplayName(const char* display_name)
157169
{
158170
DEBUG_ENTRY();
159-
171+
160172
char array[common::store::remoteconfig::kDisplayNameLength];
161173

162-
size_t len = strlen(display_name);
174+
size_t len = strlen(display_name);
163175
len = len > (common::store::remoteconfig::kDisplayNameLength - 1) ? common::store::remoteconfig::kDisplayNameLength - 1 : len;
164176
memcpy(reinterpret_cast<char*>(array), display_name, len);
165177

166178
for (uint32_t i = len; i < common::store::remoteconfig::kDisplayNameLength; i++)
167179
{
168180
array[i] = '\0';
169181
}
170-
171-
ConfigStore::Instance().RemoteConfigUpdateArray(&common::store::RemoteConfig::display_name, array, common::store::remoteconfig::kDisplayNameLength);
172182

173-
DEBUG_EXIT();
174-
}
183+
ConfigStore::Instance().RemoteConfigUpdateArray(&common::store::RemoteConfig::display_name, array, common::store::remoteconfig::kDisplayNameLength);
175184

176-
namespace configstore
177-
{
178-
void SetFactoryDefaults();
185+
DEBUG_EXIT();
179186
}
180187

181188
void RemoteConfig::HandleFactory()
182189
{
183-
configstore::SetFactoryDefaults();
190+
DEBUG_ENTRY();
191+
192+
ConfigStore::Instance().Reset();
193+
HandleReboot();
194+
195+
DEBUG_EXIT();
184196
}
185197

186198
void RemoteConfig::Input(const uint8_t* buffer, uint32_t size, uint32_t from_ip, [[maybe_unused]] uint16_t from_port)
@@ -301,7 +313,7 @@ void RemoteConfig::HandleList()
301313
#if !defined(CONFIG_REMOTECONFIG_MINIMUM)
302314
const auto* const kNodeTypeName = dmxnode::GetNodeType(dmxnode::kNodeType);
303315
#else
304-
const auto* const kNodeTypeName = "Bootloader TFTP";
316+
const auto* const kNodeTypeName = "Bootloader TFTP";
305317
#endif
306318

307319
if (display_name[0] != '\0')

0 commit comments

Comments
 (0)