From 7a44fe29b7c7b538518a66bb415f151c229215f5 Mon Sep 17 00:00:00 2001
From: Tomasz Kozak <tomasz.kozak@desy.de>
Date: Wed, 1 Apr 2020 15:43:37 +0200
Subject: [PATCH] Fix bug that lastWriteToFlash was accesed in wrong place

---
 tests/include/TecDummy.h | 28 +++++++++++++++-------------
 tests/src/TecDummy.cc    | 10 ++++++----
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/tests/include/TecDummy.h b/tests/include/TecDummy.h
index 8d06c17..8177219 100644
--- a/tests/include/TecDummy.h
+++ b/tests/include/TecDummy.h
@@ -10,21 +10,20 @@
 
 struct TecDummy : ChimeraTK::DummyBackend {
   TecDummy(std::string mapFileName) : DummyBackend(mapFileName) {
-    // inititialize write to flash as disabled on startup; 
+    // inititialize write to flash as disabled on startup;
     rawValues[TecFrame::ParameterSystemFlashSaveOff] = 1;
-
   }
   ~TecDummy() override {}
 
   enum class TecFramePart {
-      MARKER,
-      ADDRESS,
-      SEQUENCE,
-      COMMAND,
-      PARAMETER_ID,
-      CHANNEL,
-      PAYLOAD,
-      CRC,
+    MARKER,
+    ADDRESS,
+    SEQUENCE,
+    COMMAND,
+    PARAMETER_ID,
+    CHANNEL,
+    PAYLOAD,
+    CRC,
   };
 
   // getter and setter functions for parameters
@@ -35,7 +34,10 @@ struct TecDummy : ChimeraTK::DummyBackend {
 
   // returns whether the last write operation for the given parameter was stored in flash or not. Will return false
   // if the parameter has not been yet at all.
-  bool getLastWriteToFlash(TecFrame::TecParameter param) { return lastWriteToFlash[param]; }
+  bool getLastWriteToFlash(TecFrame::TecParameter param) {
+    if(lastWriteToFlash.find(param) != lastWriteToFlash.end()) return lastWriteToFlash.at(param);
+    return false;
+  }
 
   // flag whether timeouts should be simulated. This simulates e.g. a broken UART connection between FPGA and TEC device
   bool simulateSerialTimeout{false};
@@ -75,8 +77,8 @@ struct TecDummy : ChimeraTK::DummyBackend {
 
   void read(uint8_t bar, uint32_t address, int32_t* data, size_t sizeInBytes) override;
 
-  std::map<TecFramePart, std::string> splitFrame(const std::string &command);
-  std::string getCrc(std::string &frame);
+  std::map<TecFramePart, std::string> splitFrame(const std::string& command);
+  std::string getCrc(std::string& frame);
 
   // In this function the TEC device is actually simulated.
   std::string processFrame(const std::string& command);
diff --git a/tests/src/TecDummy.cc b/tests/src/TecDummy.cc
index aad2972..719827a 100644
--- a/tests/src/TecDummy.cc
+++ b/tests/src/TecDummy.cc
@@ -160,6 +160,11 @@ std::string TecDummy::processFrame(const std::string& command) {
     rawValues[parameterId] = value;
     result = "!" + parsedFrame[TecFramePart::ADDRESS] + parsedFrame[TecFramePart::SEQUENCE];
     result += getCrc(result);
+
+    //
+    // mark received parameter as written to flash or not
+    auto isLastWriteToFlash = (not rawValues.at(TecFrame::ParameterSystemFlashSaveOff)) ? true : false;
+    lastWriteToFlash[parameterId] = isLastWriteToFlash;
   }
   else if(parsedFrame[TecFramePart::COMMAND] == "?VR") {
     // Handle read command
@@ -170,10 +175,7 @@ std::string TecDummy::processFrame(const std::string& command) {
     result += getCrc(result);
   }
 
-  //
-  // mark received parameter as written to flash or not
-  auto isLastWriteToFlash = (not rawValues.at(TecFrame::ParameterSystemFlashSaveOff)) ? true : false;
-  lastWriteToFlash[parameterId] = isLastWriteToFlash;
+
 
   std::cout << "Returning result " << result << std::endl;
 
-- 
GitLab