Skip to content

Commit 67a4c7e

Browse files
committed
Improve JSON param parsing and status enums
Replace ParseAndApply with ParseValue/ParseInRange and add explicit range/length checks for DMX, OSC and network JSON parameters (break/mab times, refresh rate, slots, ports, ping delay, flags). Fix DMX port iteration to use ::dmx::config::max::kPorts, add IWYU pragma for dmx include, and tighten SetUseStaticIp handling. Update status LED enum usage from FAST/NORMAL to kFast/kNormal. Also apply minor style/formatting refinements (brace placement, constexpr formatting) and ensure proper rounding for DMX slot calculations.
1 parent 331b0b0 commit 67a4c7e

6 files changed

Lines changed: 51 additions & 64 deletions

File tree

lib-dmx/src/json/dmxsendparams.cpp

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,91 +35,80 @@
3535
#include "json/json_parsehelper.h"
3636
#include "dmxconst.h"
3737
#include "configstore.h"
38-
#include "dmx.h"
38+
#include "dmx.h" // IWYU pragma: keep
3939

40-
namespace json
41-
{
40+
namespace json {
4241
using dmx::kChannelsMax;
4342

44-
static constexpr uint8_t RounddownSlots(uint16_t n)
45-
{
43+
static constexpr uint8_t RounddownSlots(uint16_t n) {
4644
return static_cast<uint8_t>((n / 2U) - 1);
4745
}
4846

49-
static constexpr uint16_t RoundupSlots(uint8_t n)
50-
{
47+
static constexpr uint16_t RoundupSlots(uint8_t n) {
5148
return static_cast<uint16_t>((n + 1U) * 2U);
5249
}
5350

54-
DmxSendParams::DmxSendParams()
55-
{
51+
DmxSendParams::DmxSendParams() {
5652
ConfigStore::Instance().Copy(&store_dmx_send, &ConfigurationStore::dmx_send);
5753
}
5854

59-
void DmxSendParams::SetBreakTime(const char* val, uint32_t len)
60-
{
61-
ParseAndApply<uint16_t>(val, len, [](uint16_t v) { store_dmx_send.break_time = v > dmx::transmit::kBreakTimeMin ? v : dmx::transmit::kBreakTimeMin; });
55+
void DmxSendParams::SetBreakTime(const char* val, uint32_t len) {
56+
const auto kV = ParseValue<uint16_t>(val, len);
57+
store_dmx_send.break_time = kV > dmx::transmit::kBreakTimeMin ? kV : dmx::transmit::kBreakTimeMin;
6258
}
6359

64-
void DmxSendParams::SetMabTime(const char* val, uint32_t len)
65-
{
66-
ParseAndApply<uint16_t>(val, len, [](uint16_t v) { store_dmx_send.mab_time = v > dmx::transmit::kMabTimeMin ? v : dmx::transmit::kMabTimeMin; });
60+
void DmxSendParams::SetMabTime(const char* val, uint32_t len) {
61+
const auto kV = ParseValue<uint16_t>(val, len);
62+
store_dmx_send.mab_time = kV > dmx::transmit::kMabTimeMin ? kV : dmx::transmit::kMabTimeMin;
6763
}
6864

69-
void DmxSendParams::SetRefreshRate(const char* val, uint32_t len)
70-
{
71-
ParseAndApply<uint16_t>(val, len, [](uint16_t v) { store_dmx_send.refresh_rate = v; });
65+
void DmxSendParams::SetRefreshRate(const char* val, uint32_t len) {
66+
uint8_t v;
67+
68+
if (ParseInRange<uint16_t, uint8_t>(val, len, 1U, 255U, &v)) {
69+
store_dmx_send.refresh_rate = v;
70+
}
7271
}
7372

74-
void DmxSendParams::SetSlotsCount(const char* val, uint32_t len)
75-
{
76-
ParseAndApply<uint16_t>(val, len,
77-
[](uint16_t v)
78-
{
79-
if (v >= 2 && v < dmx::kChannelsMax)
80-
{
81-
store_dmx_send.slots_count = RounddownSlots(v);
82-
}
83-
else
84-
{
85-
store_dmx_send.slots_count = RounddownSlots(dmx::kChannelsMax);
86-
}
87-
});
73+
void DmxSendParams::SetSlotsCount(const char* val, uint32_t len) {
74+
const auto kV = ParseValue<uint16_t>(val, len);
75+
76+
if (kV >= 2U && kV <= dmx::kChannelsMax) {
77+
store_dmx_send.slots_count = RounddownSlots(kV);
78+
} else {
79+
store_dmx_send.slots_count = RounddownSlots(dmx::kChannelsMax);
80+
}
8881
}
8982

90-
void DmxSendParams::Store(const char* buffer, uint32_t buffer_size)
91-
{
83+
void DmxSendParams::Store(const char* buffer, uint32_t buffer_size) {
9284
ParseJsonWithTable(buffer, buffer_size, kDmxSendKeys);
9385
ConfigStore::Instance().Store(&store_dmx_send, &ConfigurationStore::dmx_send);
9486
}
9587

96-
void DmxSendParams::Set()
97-
{
88+
void DmxSendParams::Set() {
9889
auto& dmx = *Dmx::Get();
9990

10091
dmx.SetDmxBreakTime(store_dmx_send.break_time);
10192
dmx.SetDmxMabTime(store_dmx_send.mab_time);
10293
dmx.SetDmxSlots(RoundupSlots(store_dmx_send.slots_count));
10394

10495
uint32_t period = 0;
105-
if (store_dmx_send.refresh_rate != 0)
106-
{
96+
if (store_dmx_send.refresh_rate != 0) {
10797
period = 1000000U / store_dmx_send.refresh_rate;
10898
}
99+
109100
dmx.SetDmxPeriodTime(period);
110101

111102
#ifndef NDEBUG
112103
Dump();
113104
#endif
114105
}
115106

116-
void DmxSendParams::Dump()
117-
{
107+
void DmxSendParams::Dump() {
118108
printf("%s::%s \'%s\':\n", __FILE__, __FUNCTION__, json::DmxSendParamsConst::kFileName);
119109
printf(" %s=%u\n", DmxSendParamsConst::kBreakTime.name, store_dmx_send.break_time);
120110
printf(" %s=%u\n", DmxSendParamsConst::kMabTime.name, store_dmx_send.mab_time);
121111
printf(" %s=%u\n", DmxSendParamsConst::kRefreshRate.name, store_dmx_send.refresh_rate);
122112
printf(" %s=%u [%u]\n", DmxSendParamsConst::kSlotsCount.name, RoundupSlots(store_dmx_send.slots_count), store_dmx_send.slots_count);
123113
}
124-
125114
} // namespace json

lib-dmx/src/json/json_status_dmx.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace json::status
2424

2525

2626
uint32_t Dmx(char* out_buffer, uint32_t out_buffer_size, uint32_t port_index) {
27-
if (port_index < ::dmx::config::max::PORTS)
27+
if (port_index < ::dmx::config::max::kPorts)
2828
{
2929
auto& statistics = Dmx::Get()->GetTotalStatistics(port_index);
3030
auto length = static_cast<uint32_t>(snprintf(out_buffer, out_buffer_size,
@@ -45,7 +45,7 @@ uint32_t Dmx(char* out_buffer, uint32_t out_buffer_size) {
4545
out_buffer[0] = '[';
4646
uint32_t length = 1;
4747

48-
for (uint32_t port_index = 0; port_index < ::dmx::config::max::PORTS; port_index++)
48+
for (uint32_t port_index = 0; port_index < ::dmx::config::max::kPorts; port_index++)
4949
{
5050
length += Dmx(&out_buffer[length], out_buffer_size - length, port_index);
5151
out_buffer[length++] = ',';

lib-network/src/json/networkparams.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ NetworkParams::NetworkParams() {
5555
}
5656

5757
void NetworkParams::SetUseStaticIp(const char* val, uint32_t len) {
58-
if (len == 1) store_network.flags = common::SetFlagValue(store_network.flags, Flags::Flag::kUseStaticIp, val[0] != '0');
58+
if (len == 1) {
59+
store_network.flags = common::SetFlagValue(store_network.flags, Flags::Flag::kUseStaticIp, val[0] != '0');
60+
}
5961
}
6062

6163
void NetworkParams::SetIpAddress(const char* val, uint32_t len) {

lib-osc/src/json/client/oscclientparams.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,35 @@ OscClientParams::OscClientParams() {
4949
}
5050

5151
void OscClientParams::SetIncomingPort(const char* val, uint32_t len) {
52-
if (len <= 3) {
53-
return;
54-
}
52+
uint16_t v;
5553

56-
auto v = ParseValue<uint16_t>(val, len);
57-
store_oscclient.incoming_port = v;
54+
if (ParseInRange<uint32_t, uint16_t>(val, len, 1U, 65535U, &v)) {
55+
store_oscclient.incoming_port = v;
56+
}
5857
}
5958

6059
void OscClientParams::SetOutgoingPort(const char* val, uint32_t len) {
61-
if (len <= 3) {
62-
return;
63-
}
60+
uint16_t v;
6461

65-
auto v = ParseValue<uint16_t>(val, len);
66-
store_oscclient.outgoing_port = v;
62+
if (ParseInRange<uint32_t, uint16_t>(val, len, 1U, 65535U, &v)) {
63+
store_oscclient.outgoing_port = v;
64+
}
6765
}
6866

6967
void OscClientParams::SetServerIp(const char* val, uint32_t len) {
7068
store_oscclient.server_ip = net::ParseIpString(val, len);
7169
}
7270

7371
void OscClientParams::SetPingDisable(const char* val, [[maybe_unused]] uint32_t len) {
74-
ParseAndApply<uint8_t>(val, len, [](uint8_t v) { store_oscclient.flags = common::SetFlagValue(store_oscclient.flags, Flags::Flag::kPingDisable, v != 0); });
72+
if (len == 1) {
73+
store_oscclient.flags = common::SetFlagValue(store_oscclient.flags, Flags::Flag::kPingDisable, val[0] != '0');
74+
}
7575
}
7676

7777
void OscClientParams::SetPingDelay(const char* val, uint32_t len) {
78-
if (len >= 3) {
79-
return;
80-
}
81-
82-
auto v = ParseValue<uint8_t>(val, len);
78+
uint8_t v;
8379

84-
if ((v >= 2) && (v <= 60)) {
80+
if (ParseInRange<uint16_t, uint8_t>(val, len, 2U, 60U, &v)) {
8581
store_oscclient.ping_delay = v;
8682
}
8783
}

lib-remoteconfig/src/http/json_action.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ static void SetIdentify(const char* val, uint32_t len) {
4040
if (len != 1) return;
4141

4242
if (val[0] != '0') {
43-
hal::statusled::SetMode(hal::statusled::Mode::FAST);
43+
hal::statusled::SetMode(hal::statusled::Mode::kFast);
4444
} else {
45-
hal::statusled::SetMode(hal::statusled::Mode::NORMAL);
45+
hal::statusled::SetMode(hal::statusled::Mode::kNormal);
4646
}
4747
}
4848

lib-remoteconfig/src/http/json_status_identify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
namespace json::status {
3131
uint32_t Identify(char* out_buffer, uint32_t out_buffer_size) {
32-
const bool kIsOn = hal::statusled::GetMode() == hal::statusled::Mode::FAST;
32+
const bool kIsOn = hal::statusled::GetMode() == hal::statusled::Mode::kFast;
3333
const auto kLength = static_cast<uint32_t>(snprintf(out_buffer, out_buffer_size,
3434
"{\"identify\":%d}",
3535
kIsOn));

0 commit comments

Comments
 (0)