xref: /openbmc/entity-manager/src/entity_manager/entity_manager.hpp (revision 7719269f0d269b1bf206545fcc645e86c9c42e90)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
3 
4 #pragma once
5 
6 #include "configuration.hpp"
7 #include "dbus_interface.hpp"
8 #include "power_status_monitor.hpp"
9 #include "topology.hpp"
10 
11 #include <nlohmann/json.hpp>
12 #include <sdbusplus/asio/connection.hpp>
13 #include <sdbusplus/asio/object_server.hpp>
14 
15 #include <flat_map>
16 #include <string>
17 
18 class EntityManager
19 {
20   public:
21     explicit EntityManager(
22         std::shared_ptr<sdbusplus::asio::connection>& systemBus,
23         boost::asio::io_context& io,
24         const std::vector<std::filesystem::path>& configurationDirectories);
25 
26     // disable copy
27     EntityManager(const EntityManager&) = delete;
28     EntityManager& operator=(const EntityManager&) = delete;
29 
30     // disable move
31     EntityManager(EntityManager&&) = delete;
32     EntityManager& operator=(EntityManager&&) = delete;
33 
34     ~EntityManager() = default;
35 
36     std::shared_ptr<sdbusplus::asio::connection> systemBus;
37     sdbusplus::asio::object_server objServer;
38     std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface;
39     Configuration configuration;
40     nlohmann::json lastJson;
41     nlohmann::json systemConfiguration;
42     Topology topology;
43     boost::asio::io_context& io;
44 
45     dbus_interface::EMDBusInterface dbus_interface;
46 
47     power::PowerStatusMonitor powerStatus;
48 
49     void propertiesChangedCallback();
50     void registerCallback(const std::string& path);
51     void publishNewConfiguration(const size_t& instance, size_t count,
52                                  boost::asio::steady_timer& timer,
53                                  nlohmann::json newConfiguration);
54     void postToDbus(const nlohmann::json& newConfiguration);
55     void postBoardToDBus(const std::string& boardId,
56                          const nlohmann::json::object_t& boardConfig,
57                          std::map<std::string, std::string>& newBoards);
58     void postExposesRecordsToDBus(
59         nlohmann::json& item, size_t& exposesIndex,
60         const std::string& boardNameOrig, std::string jsonPointerPath,
61         const std::string& jsonPointerPathBoard, const std::string& boardPath,
62         const std::string& boardType);
63 
64     // @returns false on error
65     bool postConfigurationRecord(
66         const std::string& name, nlohmann::json& config,
67         const std::string& boardNameOrig, const std::string& itemType,
68         const std::string& jsonPointerPath, const std::string& ifacePath);
69 
70     void pruneConfiguration(bool powerOff, const std::string& name,
71                             const nlohmann::json& device);
72 
73     void handleCurrentConfigurationJson();
74 
75   private:
76     std::unique_ptr<sdbusplus::bus::match_t> nameOwnerChangedMatch = nullptr;
77     std::unique_ptr<sdbusplus::bus::match_t> interfacesAddedMatch = nullptr;
78     std::unique_ptr<sdbusplus::bus::match_t> interfacesRemovedMatch = nullptr;
79 
80     bool scannedPowerOff = false;
81     bool scannedPowerOn = false;
82 
83     bool propertiesChangedInProgress = false;
84     boost::asio::steady_timer propertiesChangedTimer;
85     size_t propertiesChangedInstance = 0;
86 
87     std::flat_map<std::string, sdbusplus::bus::match_t, std::less<>>
88         dbusMatches;
89 
90     void startRemovedTimer(boost::asio::steady_timer& timer,
91                            nlohmann::json& systemConfiguration);
92     void initFilters(const std::unordered_set<std::string>& probeInterfaces);
93 };
94