From 8f464780d7b7a614f0a3565790e1847fa994328a Mon Sep 17 00:00:00 2001
From: flocci <frank.locci@cern.sh>
Date: Fri, 8 Sep 2017 14:31:51 +0200
Subject: [PATCH] [SIL-352] Non-regression test fix: default initialization for
 missing IO addresses and area

---
 .../interface/equipment/SilecsDevice.cpp      |  4 ++--
 .../interface/equipment/SilecsPLC.cpp         | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsDevice.cpp b/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsDevice.cpp
index 22fda20..2455ab0 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsDevice.cpp
+++ b/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsDevice.cpp
@@ -40,8 +40,8 @@ Device::Device(PLC* thePLC, const ElementXML& deviceNode, boost::ptr_vector<Elem
     label_ = deviceNode.getAttribute("label");
     StringUtilities::fromString(address_, deviceNode.getAttribute("address"));
 
-    //For backward compatibility reason, IO addresses could be undefined.
-    //It will be initialized to (-1) by default.
+    //For backward compatibility (IO addresses are not defined in Silecs version <1.4.0)
+    //Undefined address = -1 by default.
     ai_address_ = -1;
     ao_address_ = -1;
     di_address_ = -1;
diff --git a/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.cpp b/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.cpp
index c2edd39..da46231 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.cpp
+++ b/silecs-communication-cpp/src/silecs-communication/interface/equipment/SilecsPLC.cpp
@@ -435,11 +435,22 @@ void PLC::extractDatabase()
     system_ = mappingNode.getAttribute("plc-system");
     protocolMode_ = mappingNode.getAttribute("protocol");
     model_ = mappingNode.getAttribute("plc-model");
+
     StringUtilities::fromString(baseMemAddr_, mappingNode.getAttribute("address"));
-    StringUtilities::fromString(baseDIAddr_, mappingNode.getAttribute("DI-address"));
-    StringUtilities::fromString(baseDOAddr_, mappingNode.getAttribute("DO-address"));
-    StringUtilities::fromString(baseAIAddr_, mappingNode.getAttribute("AI-address"));
-    StringUtilities::fromString(baseAOAddr_, mappingNode.getAttribute("AO-address"));
+    //For backward compatibility (IO addresses are not defined in Silecs version <1.4.0)
+    //Undefined address = -1 by default.
+    baseDIAddr_ = -1;
+    baseDOAddr_ = -1;
+    baseAIAddr_ = -1;
+    baseAOAddr_ = -1;
+    if (mappingNode.hasAttribute("AI-address"))
+        StringUtilities::fromString(baseAIAddr_, mappingNode.getAttribute("AI-address"));
+    if (mappingNode.hasAttribute("AO-address"))
+        StringUtilities::fromString(baseAOAddr_, mappingNode.getAttribute("AO-address"));
+    if (mappingNode.hasAttribute("DI-address"))
+        StringUtilities::fromString(baseDIAddr_, mappingNode.getAttribute("DI-address"));
+    if (mappingNode.hasAttribute("DO-address"))
+        StringUtilities::fromString(baseDOAddr_, mappingNode.getAttribute("DO-address"));
     usedMem_ = mappingNode.getAttribute("used-mem");
     typeID_ = whichPLCType(model_);
     brandID_ = whichPLCBrand(brand_);
-- 
GitLab