diff --git a/src/main.cc b/src/main.cc index c6919b9..aa72216 100644 --- a/src/main.cc +++ b/src/main.cc @@ -481,7 +481,8 @@ void perform_auto_scan_of_devices(Configuration *config) wmbus->setLinkModes(config->listen_to_link_modes); //string using_link_modes = wmbus->getLinkModes().hr(); //verbose("(config) listen to link modes: %s\n", using_link_modes.c_str()); - wmbus->onTelegram([&](vector data){return meter_manager_->handleTelegram(data);}); + bool simulated = detected.type == WMBusDeviceType::DEVICE_SIMULATOR; + wmbus->onTelegram([&, simulated](vector data){return meter_manager_->handleTelegram(data, simulated);}); wmbus->setTimeout(config->alarm_timeout, config->alarm_expected_activity); } } @@ -518,8 +519,10 @@ void detectAndConfigureWMBusDevices(Configuration *config) wmbus->setLinkModes(config->listen_to_link_modes); //string using_link_modes = wmbus->getLinkModes().hr(); //verbose("(config) listen to link modes: %s\n", using_link_modes.c_str()); - wmbus->onTelegram([&](vector data){return meter_manager_->handleTelegram(data);}); + bool simulated = detected.type == WMBusDeviceType::DEVICE_SIMULATOR; + wmbus->onTelegram([&, simulated](vector data){return meter_manager_->handleTelegram(data, simulated);}); wmbus->setTimeout(config->alarm_timeout, config->alarm_expected_activity); + serial_manager_->expectDevicesToWork(); } } diff --git a/src/meters.cc b/src/meters.cc index d3aa7a2..63f57cb 100644 --- a/src/meters.cc +++ b/src/meters.cc @@ -60,7 +60,7 @@ struct MeterManagerImplementation : public virtual MeterManager return meters_.size() != 0; } - bool handleTelegram(vector data) + bool handleTelegram(vector data, bool simulated) { if (!hasMeters()) { @@ -75,7 +75,7 @@ struct MeterManagerImplementation : public virtual MeterManager for (auto &m : meters_) { - bool h = m->handleTelegram(data); + bool h = m->handleTelegram(data, simulated); if (h) handled = true; } if (isVerboseEnabled() && !handled) @@ -412,11 +412,13 @@ string concatFields(Meter *m, Telegram *t, char c, vector &prints, vector return s; } -bool MeterCommonImplementation::handleTelegram(vector input_frame) +bool MeterCommonImplementation::handleTelegram(vector input_frame, bool simulated) { Telegram t; bool ok = t.parseHeader(input_frame); + if (simulated) t.markAsSimulated(); + if (!ok || !isTelegramForMe(&t)) { // This telegram is not intended for this meter. diff --git a/src/meters.h b/src/meters.h index de5e699..5c50d3f 100644 --- a/src/meters.h +++ b/src/meters.h @@ -201,7 +201,7 @@ struct Meter // The handleTelegram expects an input_frame where the DLL crcs have been removed. // Returns true of this meter handled this telegram! - virtual bool handleTelegram(vector input_frame) = 0; + virtual bool handleTelegram(vector input_frame, bool simulated) = 0; virtual bool isTelegramForMe(Telegram *t) = 0; virtual MeterKeys *meterKeys() = 0; @@ -222,7 +222,7 @@ struct MeterManager virtual void addMeter(unique_ptr meter) = 0; virtual void removeAllMeters() = 0; virtual void forEachMeter(std::function cb) = 0; - virtual bool handleTelegram(vector data) = 0; + virtual bool handleTelegram(vector data, bool simulated) = 0; virtual bool hasAllMetersReceivedATelegram() = 0; virtual bool hasMeters() = 0; virtual void onTelegram(function)> cb) = 0; diff --git a/src/meters_common_implementation.h b/src/meters_common_implementation.h index 2fce24e..aa63080 100644 --- a/src/meters_common_implementation.h +++ b/src/meters_common_implementation.h @@ -85,7 +85,7 @@ protected: // Print the dimensionless Text quantity, no unit is needed. void addPrint(string vname, Quantity vquantity, function getValueFunc, string help, bool field, bool json); - bool handleTelegram(vector frame); + bool handleTelegram(vector frame, bool simulated); void printMeter(Telegram *t, string *human_readable, string *fields, char separator, diff --git a/src/wmbus.cc b/src/wmbus.cc index f05e98e..fd03546 100644 --- a/src/wmbus.cc +++ b/src/wmbus.cc @@ -1215,10 +1215,12 @@ bool Telegram::parseTPLConfig(std::vector::iterator &pos) if (meter_keys->confidentiality_key.size() != 16) { - if (meter_keys->isSimulation()) { + if (isSimulated()) + { debug("(wmbus) simulation without keys, not generating Kmac and Kenc.\n"); return true; } + debug("(wmbus) no key, thus cannot execute kdf.\n"); return false; } AES_CMAC(&meter_keys->confidentiality_key[0], &input[0], 16, &mac[0]); @@ -1351,7 +1353,7 @@ bool Telegram::potentiallyDecrypt(vector::iterator &pos) } else if (tpl_sec_mode == TPLSecurityMode::AES_CBC_NO_IV) { - if (!meter_keys->hasConfidentialityKey() && meter_keys->isSimulation()) + if (!meter_keys->hasConfidentialityKey() && isSimulated()) { CHECK(2); addExplanationAndIncrementPos(pos, 2, "%02x%02x (already) decrypted check bytes", *(pos+0), *(pos+1)); diff --git a/src/wmbus.h b/src/wmbus.h index a750a35..ef13f4e 100644 --- a/src/wmbus.h +++ b/src/wmbus.h @@ -294,11 +294,9 @@ struct MeterKeys { vector confidentiality_key; vector authentication_key; - bool simulation {}; bool hasConfidentialityKey() { return confidentiality_key.size() > 0; } bool hasAuthenticationKey() { return authentication_key.size() > 0; } - bool isSimulation() { return simulation; } }; struct Telegram @@ -413,6 +411,7 @@ struct Telegram void explainParse(string intro, int from); bool isSimulated() { return is_simulated_; } + void markAsSimulated() { is_simulated_ = true; } // Extracted mbus values. std::map> values; diff --git a/tests/test_unknown.sh b/tests/test_unknown.sh index c8a366a..7388dca 100755 --- a/tests/test_unknown.sh +++ b/tests/test_unknown.sh @@ -14,6 +14,8 @@ Forren lansensm 00010206 NOKEY cat > $TEST/test_expected.txt <