Skip to content

Commit bdd3c91

Browse files
committed
Uni: store only valid config.
E.g. do not send config in json wile module in bootloader after start.
1 parent f2ccf54 commit bdd3c91

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

src/modules/uni.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ QJsonObject MtbUni::moduleInfo(bool state, bool config) const {
2626
{"ir", this->isIrSupport()},
2727
};
2828

29-
if (config)
30-
uni["config"] = this->config.json(this->isIrSupport(), false);
29+
if ((config) && (this->config.has_value()))
30+
uni["config"] = this->config.value().json(this->isIrSupport(), false);
3131

3232
if (state && this->active && !this->busModuleInfo.inBootloader()) {
3333
uni["state"] = QJsonObject{
@@ -238,14 +238,14 @@ void MtbUni::jsonSetConfig(QTcpSocket *socket, const QJsonObject &request) {
238238

239239
MtbModule::jsonSetConfig(socket, request);
240240

241-
MtbUniConfig oldConfig = this->configToWrite;
242-
this->configToWrite.fromJson(request["config"].toObject());
241+
std::optional<MtbUniConfig> oldConfig = this->configToWrite;
242+
this->configToWrite.emplace(MtbUniConfig(request["config"].toObject()));
243243
this->configWriting = ServerRequest(socket, request);
244244

245245
if ((this->active) && (oldConfig != this->configToWrite)) {
246246
mtbusb.send(
247247
Mtb::CmdMtbModuleSetConfig(
248-
this->address, this->configToWrite.serializeForMtbUsb(this->isIrSupport()),
248+
this->address, this->configToWrite.value().serializeForMtbUsb(this->isIrSupport()),
249249
{[this](uint8_t, void*) { this->mtbBusConfigWritten(); }},
250250
{[this](Mtb::CmdError error, void*) { this->mtbBusConfigNotWritten(error); }}
251251
)
@@ -334,11 +334,13 @@ void MtbUni::resetOutputsOfClient(QTcpSocket *socket) {
334334
MtbModule::resetOutputsOfClient(socket);
335335

336336
bool send = false;
337-
for (size_t i = 0; i < UNI_IO_CNT; i++) {
338-
if (this->whoSetOutput[i] == socket) {
339-
this->outputsWant[i] = this->config.outputsSafe[i];
340-
this->whoSetOutput[i] = nullptr;
341-
send = true;
337+
if (this->config.has_value()) {
338+
for (size_t i = 0; i < UNI_IO_CNT; i++) {
339+
if (this->whoSetOutput[i] == socket) {
340+
this->outputsWant[i] = this->config.value().outputsSafe[i];
341+
this->whoSetOutput[i] = nullptr;
342+
send = true;
343+
}
342344
}
343345
}
344346

@@ -409,7 +411,7 @@ std::array<uint8_t, UNI_IO_CNT> MtbUni::moduleOutputsData(const std::vector<uint
409411

410412
void MtbUni::allOutputsReset() {
411413
for (size_t i = 0; i < UNI_IO_CNT; i++) {
412-
this->outputsWant[i] = this->config.outputsSafe[i];
414+
this->outputsWant[i] = this->config.has_value() ? this->config.value().outputsSafe[i] : 0;
413415
this->outputsConfirmed[i] = this->outputsWant[i];
414416
this->whoSetOutput[i] = nullptr;
415417
}
@@ -450,11 +452,11 @@ void MtbUni::activate() {
450452
this->mlog("Module warning="+QString::number(this->busModuleInfo.warning)+", error="+
451453
QString::number(this->busModuleInfo.error), Mtb::LogLevel::Warning);
452454

453-
if (this->configLoaded) {
455+
if (this->config.has_value()) {
454456
this->mlog("Config previously loaded from file, setting to module...", Mtb::LogLevel::Info);
455457
mtbusb.send(
456458
Mtb::CmdMtbModuleSetConfig(
457-
this->address, this->config.serializeForMtbUsb(this->isIrSupport()),
459+
this->address, this->config.value().serializeForMtbUsb(this->isIrSupport()),
458460
{[this](uint8_t, void*) { this->configSet(); }},
459461
{[this](Mtb::CmdError error, void*) {
460462
this->mlog("Unable to set module config.", Mtb::LogLevel::Error);
@@ -468,8 +470,7 @@ void MtbUni::activate() {
468470
Mtb::CmdMtbModuleGetConfig(
469471
this->address,
470472
{[this](uint8_t, const std::vector<uint8_t>& data, void*) {
471-
this->config.fromMtbUsb(data);
472-
this->configLoaded = true;
473+
this->config.emplace(MtbUniConfig(data));
473474
this->configSet();
474475
}},
475476
{[this](Mtb::CmdError error, void*) {
@@ -518,8 +519,10 @@ void MtbUni::storeInputsState(const std::vector<uint8_t> &data) {
518519
}
519520

520521
void MtbUni::outputsReset() {
521-
this->outputsWant = this->config.outputsSafe;
522-
this->outputsConfirmed = this->outputsWant;
522+
for (size_t i = 0; i < UNI_IO_CNT; i++) {
523+
this->outputsWant[i] = this->config.has_value() ? this->config.value().outputsSafe[i] : 0;
524+
this->outputsConfirmed[i] = this->outputsWant[i];
525+
}
523526

524527
for (size_t i = 0; i < UNI_IO_CNT; i++)
525528
this->whoSetOutput[i] = nullptr;
@@ -669,14 +672,13 @@ void MtbUni::reactivateCheck() {
669672

670673
void MtbUni::loadConfig(const QJsonObject &json) {
671674
MtbModule::loadConfig(json);
672-
this->config.fromJson(json["config"].toObject());
673-
this->configLoaded = true;
675+
this->config.emplace(MtbUniConfig(json["config"].toObject()));
674676
}
675677

676678
void MtbUni::saveConfig(QJsonObject &json) const {
677679
MtbModule::saveConfig(json);
678-
if (this->configLoaded)
679-
json["config"] = this->config.json(this->isIrSupport(), true);
680+
if (this->config.has_value())
681+
json["config"] = this->config.value().json(this->isIrSupport(), true);
680682
}
681683

682684
/* Diagnostic Values -------------------------------------------------------- */

src/modules/uni.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ struct MtbUniConfig {
1111
std::array<size_t, UNI_IO_CNT> inputsDelay; // 0 = 0s, 1 = 0.1s, 15 = 1.5s, min=0, max=15
1212
uint16_t irs;
1313

14+
MtbUniConfig(const QJsonObject& json) { this->fromJson(json); }
15+
MtbUniConfig(const std::vector<uint8_t>& mtbUsbData) { this->fromMtbUsb(mtbUsbData); }
16+
1417
std::vector<uint8_t> serializeForMtbUsb(bool withIrs) const;
1518
void fromMtbUsb(const std::vector<uint8_t>&);
1619

@@ -31,9 +34,8 @@ class MtbUni : public MtbModule {
3134
uint16_t inputs;
3235
std::array<uint8_t, UNI_IO_CNT> outputsWant;
3336
std::array<uint8_t, UNI_IO_CNT> outputsConfirmed;
34-
MtbUniConfig config;
35-
MtbUniConfig configToWrite;
36-
bool configLoaded = false;
37+
std::optional<MtbUniConfig> config;
38+
std::optional<MtbUniConfig> configToWrite;
3739
std::array<QTcpSocket*, UNI_IO_CNT> whoSetOutput;
3840

3941
std::vector<ServerRequest> setOutputsWaiting;

0 commit comments

Comments
 (0)