Move global variable to local variable.

Moved the global variable unknownBusObjectCount declaration
to the local variable and passed as parameter to appropriate
functions.

This fix to avoid the usage of global variable and declaring
global variable in .hpp files while moving common FruDevice.cpp
functions to FruUtils.cpp

TESTED : Verified all the fru's read, write and scan
in Facebook YosemiteV2 platform.

Signed-off-by: Kumar Thangavel <thangavel.k@hcl.com>
Change-Id: Ife8172f4753bbd07e3dc471f948338297f78058e
diff --git a/src/FruDevice.cpp b/src/FruDevice.cpp
index 1576e30..c774b65 100644
--- a/src/FruDevice.cpp
+++ b/src/FruDevice.cpp
@@ -58,7 +58,6 @@
 
 namespace fs = std::filesystem;
 static constexpr bool debug = false;
-static size_t unknownBusObjectCount = 0;
 constexpr size_t maxFruSize = 512;
 constexpr size_t maxEepromPageIndex = 255;
 constexpr size_t busTimeoutSeconds = 5;
@@ -97,7 +96,8 @@
     const std::string& propertyName,
     boost::container::flat_map<
         std::pair<size_t, size_t>,
-        std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap);
+        std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
+    size_t& unknownBusObjectCount);
 
 // Given a bus/address, produce the path in sysfs for an eeprom.
 static std::string getEepromPath(size_t bus, size_t address)
@@ -643,7 +643,7 @@
     boost::container::flat_map<
         std::pair<size_t, size_t>,
         std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
-    uint32_t bus, uint32_t address)
+    uint32_t bus, uint32_t address, size_t& unknownBusObjectCount)
 {
     boost::container::flat_map<std::string, std::string> formattedFRU;
     resCodes res = formatFRU(device, formattedFRU);
@@ -754,13 +754,15 @@
             std::string propertyName = property.first;
             iface->register_property(
                 key, property.second + '\0',
-                [bus, address, propertyName,
-                 &dbusInterfaceMap](const std::string& req, std::string& resp) {
+                [bus, address, propertyName, &dbusInterfaceMap,
+                 &unknownBusObjectCount](const std::string& req,
+                                         std::string& resp) {
                     if (strcmp(req.c_str(), resp.c_str()) != 0)
                     {
                         // call the method which will update
                         if (updateFRUProperty(req, bus, address, propertyName,
-                                              dbusInterfaceMap))
+                                              dbusInterfaceMap,
+                                              unknownBusObjectCount))
                         {
                             resp = req;
                         }
@@ -926,7 +928,7 @@
     boost::container::flat_map<
         std::pair<size_t, size_t>,
         std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
-    bool dbusCall)
+    bool dbusCall, size_t& unknownBusObjectCount)
 {
     for (auto& [pair, interface] : foundDevices)
     {
@@ -953,7 +955,8 @@
     i2cBuses.emplace_back(busPath);
 
     auto scan = std::make_shared<FindDevicesWithCallback>(
-        i2cBuses, busmap, [busNum, &busmap, &dbusInterfaceMap]() {
+        i2cBuses, busmap,
+        [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount]() {
             for (auto& busIface : dbusInterfaceMap)
             {
                 if (busIface.first.first == static_cast<size_t>(busNum))
@@ -969,7 +972,8 @@
             for (auto& device : *(found->second))
             {
                 addFruObjectToDbus(device.second, dbusInterfaceMap,
-                                   static_cast<uint32_t>(busNum), device.first);
+                                   static_cast<uint32_t>(busNum), device.first,
+                                   unknownBusObjectCount);
             }
         });
     scan->run();
@@ -979,7 +983,8 @@
     BusMap& busmap,
     boost::container::flat_map<
         std::pair<size_t, size_t>,
-        std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap)
+        std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
+    size_t& unknownBusObjectCount)
 {
     static boost::asio::deadline_timer timer(io);
     timer.expires_from_now(boost::posix_time::seconds(1));
@@ -1032,7 +1037,8 @@
                     for (auto& device : *devicemap.second)
                     {
                         addFruObjectToDbus(device.second, dbusInterfaceMap,
-                                           devicemap.first, device.first);
+                                           devicemap.first, device.first,
+                                           unknownBusObjectCount);
                     }
                 }
             });
@@ -1056,7 +1062,8 @@
     const std::string& propertyName,
     boost::container::flat_map<
         std::pair<size_t, size_t>,
-        std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap)
+        std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
+    size_t& unknownBusObjectCount)
 {
     size_t updatePropertyReqLen = updatePropertyReq.length();
     if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
@@ -1317,12 +1324,13 @@
     }
 
     // Rescan the bus so that GetRawFru dbus-call fetches updated values
-    rescanBusses(busMap, dbusInterfaceMap);
+    rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount);
     return true;
 }
 
 int main()
 {
+    static size_t unknownBusObjectCount = 0;
     auto devDir = fs::path("/dev/");
     auto matchString = std::string(R"(i2c-\d+$)");
     std::vector<fs::path> i2cBuses;
@@ -1348,11 +1356,13 @@
         objServer.add_interface("/xyz/openbmc_project/FruDevice",
                                 "xyz.openbmc_project.FruDeviceManager");
 
-    iface->register_method("ReScan",
-                           [&]() { rescanBusses(busMap, dbusInterfaceMap); });
+    iface->register_method("ReScan", [&]() {
+        rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount);
+    });
 
     iface->register_method("ReScanBus", [&](uint8_t bus) {
-        rescanOneBus(busMap, bus, dbusInterfaceMap, true);
+        rescanOneBus(busMap, bus, dbusInterfaceMap, true,
+                     unknownBusObjectCount);
     });
 
     iface->register_method("GetRawFru", getFRUInfo);
@@ -1366,7 +1376,7 @@
             return;
         }
         // schedule rescan on success
-        rescanBusses(busMap, dbusInterfaceMap);
+        rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount);
     });
     iface->initialize();
 
@@ -1387,7 +1397,7 @@
 
             if (powerIsOn)
             {
-                rescanBusses(busMap, dbusInterfaceMap);
+                rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount);
             }
         };
 
@@ -1434,7 +1444,8 @@
                                 continue;
                             }
                             rescanOneBus(busMap, static_cast<uint8_t>(bus),
-                                         dbusInterfaceMap, false);
+                                         dbusInterfaceMap, false,
+                                         unknownBusObjectCount);
                         }
                 }
 
@@ -1447,7 +1458,7 @@
 
     dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
     // run the initial scan
-    rescanBusses(busMap, dbusInterfaceMap);
+    rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount);
 
     io.run();
     return 0;