entity-manager: extract postExposesRecordsToDBus

Extract function postExposesRecordsToDBus from postBoardToDBus.

This one is a bit tricky as there is indices and iterators involved. It
is not a simple extraction but worthwhile since we can get rid of
indentation and simplify / shorter function.

The function postBoardToDBus looks for 'Exposes' properties in the json
which are the exposes arrays of configuration records.

Once it finds such an array, it calls the postExposesRecordsToDBus
function which increments an index / iterator to go from -1 to 0 and
access the first such element.

That is then repeated for all such instances. Which covers the case of
multiple entity-manager configs present in one json file.

Tested: on Tyan S8030

Checked that `busctl tree` looks as before

```
`- /xyz
      `- /xyz/openbmc_project
        |- /xyz/openbmc_project/EntityManager
        `- /xyz/openbmc_project/inventory
          `- /xyz/openbmc_project/inventory/system
            |- /xyz/openbmc_project/inventory/system/board
            | `- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/CPU0_Power_Consumption
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/CPU0_Temp
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/GARBO_SENSOR
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_18_RUN
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_CORE_RUN
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_MEM_ABCD
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_MEM_EFGH
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_SOC_RUN
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VBAT_33
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_12_RUN
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_33_DUAL
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_33_RUN
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_5_DUAL
            |   |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_5_RUN
            |   `- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/X550
            |- /xyz/openbmc_project/inventory/system/chassis
            | `- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis
            |   |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/Fan_Control
            |   |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/NIC_1_Ignore
            |   |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/NIC_2_Ignore
            |   |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/NIC_FRU_1
            |   |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/NIC_FRU_2
            |   |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/SYS_FAN_1
            |   |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/SYS_FAN_2
            |   |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/SYS_FAN_3
            |   |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/Temperature_Control
            |   `- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/Zone0
            `- /xyz/openbmc_project/inventory/system/powersupply
              |- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_0
              | |- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_0/PSU0
              | |- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_0/PSU0_ADDR
              | `- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_0/PSU0_FRU
              `- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_1
                |- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_1/PSU1
                |- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_1/PSU1_ADDR
                `- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_1/PSU1_FRU
```
Checked some individual object paths, also appear same as before.

```
busctl introspect xyz.openbmc_project.EntityManager /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VBAT_33
```

output:

```
xyz.openbmc_project.Configuration.ADC             interface -         -                -
.Index                                            property  t         10               emits-change
.Name                                             property  s         "VBAT_33"        emits-change
.PowerState                                       property  s         "Always"         emits-change
.ScaleFactor                                      property  d         0.3333           emits-change
.Type                                             property  s         "ADC"            emits-change
xyz.openbmc_project.Configuration.ADC.Thresholds0 interface -         -                -
.Delete                                           method    -         -                -
.Direction                                        property  s         "greater than"   emits-change
.Name                                             property  s         "upper critical" emits-change
.Severity                                         property  d         1                emits-change
.Value                                            property  d         3.507            emits-change
xyz.openbmc_project.Configuration.ADC.Thresholds1 interface -         -                -
.Delete                                           method    -         -                -
.Direction                                        property  s         "less than"      emits-change
.Name                                             property  s         "lower critical" emits-change
.Severity                                         property  d         1                emits-change
.Value                                            property  d         2.688            emits-change
```

Change-Id: Ia0932c862672be03ac7d462af2ce9abfd226b32a
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/src/entity_manager/entity_manager.cpp b/src/entity_manager/entity_manager.cpp
index 7734abc..29ee897 100644
--- a/src/entity_manager/entity_manager.cpp
+++ b/src/entity_manager/entity_manager.cpp
@@ -172,7 +172,7 @@
         }
     }
 
-    auto exposes = boardValues.find("Exposes");
+    nlohmann::json::iterator exposes = boardValues.find("Exposes");
     if (exposes == boardValues.end())
     {
         return;
@@ -183,148 +183,158 @@
     // store the board level pointer so we can modify it on the way down
     std::string jsonPointerPathBoard = jsonPointerPath;
     size_t exposesIndex = -1;
-    for (auto& item : *exposes)
+    for (nlohmann::json& item : *exposes)
     {
-        exposesIndex++;
-        jsonPointerPath = jsonPointerPathBoard;
-        jsonPointerPath += std::to_string(exposesIndex);
+        postExposesRecordsToDBus(item, exposesIndex, boardNameOrig,
+                                 jsonPointerPath, jsonPointerPathBoard,
+                                 boardPath, boardType);
+    }
 
-        auto findName = item.find("Name");
-        if (findName == item.end())
+    newBoards.emplace(boardPath, boardNameOrig);
+}
+
+void EntityManager::postExposesRecordsToDBus(
+    nlohmann::json& item, size_t& exposesIndex,
+    const std::string& boardNameOrig, std::string jsonPointerPath,
+    const std::string& jsonPointerPathBoard, const std::string& boardPath,
+    const std::string& boardType)
+{
+    exposesIndex++;
+    jsonPointerPath = jsonPointerPathBoard;
+    jsonPointerPath += std::to_string(exposesIndex);
+
+    auto findName = item.find("Name");
+    if (findName == item.end())
+    {
+        std::cerr << "cannot find name in field " << item << "\n";
+        return;
+    }
+    auto findStatus = item.find("Status");
+    // if status is not found it is assumed to be status = 'okay'
+    if (findStatus != item.end())
+    {
+        if (*findStatus == "disabled")
         {
-            std::cerr << "cannot find name in field " << item << "\n";
-            continue;
+            return;
         }
-        auto findStatus = item.find("Status");
-        // if status is not found it is assumed to be status = 'okay'
-        if (findStatus != item.end())
+    }
+    auto findType = item.find("Type");
+    std::string itemType;
+    if (findType != item.end())
+    {
+        itemType = findType->get<std::string>();
+        std::regex_replace(itemType.begin(), itemType.begin(), itemType.end(),
+                           illegalDbusPathRegex, "_");
+    }
+    else
+    {
+        itemType = "unknown";
+    }
+    std::string itemName = findName->get<std::string>();
+    std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
+                       illegalDbusMemberRegex, "_");
+    std::string ifacePath = boardPath;
+    ifacePath += "/";
+    ifacePath += itemName;
+
+    if (itemType == "BMC")
+    {
+        std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
+            dbus_interface.createInterface(
+                objServer, ifacePath, "xyz.openbmc_project.Inventory.Item.Bmc",
+                boardNameOrig);
+        dbus_interface::populateInterfaceFromJson(
+            io, systemConfiguration, jsonPointerPath, bmcIface, item, objServer,
+            getPermission(itemType));
+    }
+    else if (itemType == "System")
+    {
+        std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
+            dbus_interface.createInterface(
+                objServer, ifacePath,
+                "xyz.openbmc_project.Inventory.Item.System", boardNameOrig);
+        dbus_interface::populateInterfaceFromJson(
+            io, systemConfiguration, jsonPointerPath, systemIface, item,
+            objServer, getPermission(itemType));
+    }
+
+    for (const auto& [name, config] : item.items())
+    {
+        jsonPointerPath = jsonPointerPathBoard;
+        jsonPointerPath.append(std::to_string(exposesIndex))
+            .append("/")
+            .append(name);
+        if (config.type() == nlohmann::json::value_t::object)
         {
-            if (*findStatus == "disabled")
+            std::string ifaceName = "xyz.openbmc_project.Configuration.";
+            ifaceName.append(itemType).append(".").append(name);
+
+            std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
+                dbus_interface.createInterface(objServer, ifacePath, ifaceName,
+                                               boardNameOrig);
+
+            dbus_interface::populateInterfaceFromJson(
+                io, systemConfiguration, jsonPointerPath, objectIface, config,
+                objServer, getPermission(name));
+        }
+        else if (config.type() == nlohmann::json::value_t::array)
+        {
+            size_t index = 0;
+            if (config.empty())
             {
                 continue;
             }
-        }
-        auto findType = item.find("Type");
-        std::string itemType;
-        if (findType != item.end())
-        {
-            itemType = findType->get<std::string>();
-            std::regex_replace(itemType.begin(), itemType.begin(),
-                               itemType.end(), illegalDbusPathRegex, "_");
-        }
-        else
-        {
-            itemType = "unknown";
-        }
-        std::string itemName = findName->get<std::string>();
-        std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
-                           illegalDbusMemberRegex, "_");
-        std::string ifacePath = boardPath;
-        ifacePath += "/";
-        ifacePath += itemName;
+            bool isLegal = true;
+            auto type = config[0].type();
+            if (type != nlohmann::json::value_t::object)
+            {
+                continue;
+            }
 
-        if (itemType == "BMC")
-        {
-            std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
-                dbus_interface.createInterface(
-                    objServer, ifacePath,
-                    "xyz.openbmc_project.Inventory.Item.Bmc", boardNameOrig);
-            dbus_interface::populateInterfaceFromJson(
-                io, systemConfiguration, jsonPointerPath, bmcIface, item,
-                objServer, getPermission(itemType));
-        }
-        else if (itemType == "System")
-        {
-            std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
-                dbus_interface.createInterface(
-                    objServer, ifacePath,
-                    "xyz.openbmc_project.Inventory.Item.System", boardNameOrig);
-            dbus_interface::populateInterfaceFromJson(
-                io, systemConfiguration, jsonPointerPath, systemIface, item,
-                objServer, getPermission(itemType));
-        }
+            // verify legal json
+            for (const auto& arrayItem : config)
+            {
+                if (arrayItem.type() != type)
+                {
+                    isLegal = false;
+                    break;
+                }
+            }
+            if (!isLegal)
+            {
+                std::cerr << "dbus format error" << config << "\n";
+                break;
+            }
 
-        for (const auto& [name, config] : item.items())
-        {
-            jsonPointerPath = jsonPointerPathBoard;
-            jsonPointerPath.append(std::to_string(exposesIndex))
-                .append("/")
-                .append(name);
-            if (config.type() == nlohmann::json::value_t::object)
+            for (auto& arrayItem : config)
             {
                 std::string ifaceName = "xyz.openbmc_project.Configuration.";
                 ifaceName.append(itemType).append(".").append(name);
+                ifaceName.append(std::to_string(index));
 
                 std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
                     dbus_interface.createInterface(objServer, ifacePath,
                                                    ifaceName, boardNameOrig);
 
                 dbus_interface::populateInterfaceFromJson(
-                    io, systemConfiguration, jsonPointerPath, objectIface,
-                    config, objServer, getPermission(name));
-            }
-            else if (config.type() == nlohmann::json::value_t::array)
-            {
-                size_t index = 0;
-                if (config.empty())
-                {
-                    continue;
-                }
-                bool isLegal = true;
-                auto type = config[0].type();
-                if (type != nlohmann::json::value_t::object)
-                {
-                    continue;
-                }
-
-                // verify legal json
-                for (const auto& arrayItem : config)
-                {
-                    if (arrayItem.type() != type)
-                    {
-                        isLegal = false;
-                        break;
-                    }
-                }
-                if (!isLegal)
-                {
-                    std::cerr << "dbus format error" << config << "\n";
-                    break;
-                }
-
-                for (auto& arrayItem : config)
-                {
-                    std::string ifaceName =
-                        "xyz.openbmc_project.Configuration.";
-                    ifaceName.append(itemType).append(".").append(name);
-                    ifaceName.append(std::to_string(index));
-
-                    std::shared_ptr<sdbusplus::asio::dbus_interface>
-                        objectIface = dbus_interface.createInterface(
-                            objServer, ifacePath, ifaceName, boardNameOrig);
-
-                    dbus_interface::populateInterfaceFromJson(
-                        io, systemConfiguration,
-                        jsonPointerPath + "/" + std::to_string(index),
-                        objectIface, arrayItem, objServer, getPermission(name));
-                    index++;
-                }
+                    io, systemConfiguration,
+                    jsonPointerPath + "/" + std::to_string(index), objectIface,
+                    arrayItem, objServer, getPermission(name));
+                index++;
             }
         }
-
-        std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
-            dbus_interface.createInterface(
-                objServer, ifacePath,
-                "xyz.openbmc_project.Configuration." + itemType, boardNameOrig);
-
-        dbus_interface::populateInterfaceFromJson(
-            io, systemConfiguration, jsonPointerPath, itemIface, item,
-            objServer, getPermission(itemType));
-
-        topology.addBoard(boardPath, boardType, boardNameOrig, item);
     }
 
-    newBoards.emplace(boardPath, boardNameOrig);
+    std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
+        dbus_interface.createInterface(
+            objServer, ifacePath,
+            "xyz.openbmc_project.Configuration." + itemType, boardNameOrig);
+
+    dbus_interface::populateInterfaceFromJson(
+        io, systemConfiguration, jsonPointerPath, itemIface, item, objServer,
+        getPermission(itemType));
+
+    topology.addBoard(boardPath, boardType, boardNameOrig, item);
 }
 
 static bool deviceRequiresPowerOn(const nlohmann::json& entity)