xref: /openbmc/entity-manager/src/entity_manager/entity_manager.hpp (revision 6f4c6b4e7cbc7401d47f2bd15f6bbaf2b85ac083)
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 /// \file entity_manager.hpp
17 
18 #pragma once
19 
20 #include "configuration.hpp"
21 #include "dbus_interface.hpp"
22 #include "power_status_monitor.hpp"
23 #include "topology.hpp"
24 
25 #include <boost/container/flat_map.hpp>
26 #include <nlohmann/json.hpp>
27 #include <sdbusplus/asio/connection.hpp>
28 #include <sdbusplus/asio/object_server.hpp>
29 
30 #include <string>
31 
32 class EntityManager
33 {
34   public:
35     explicit EntityManager(
36         std::shared_ptr<sdbusplus::asio::connection>& systemBus,
37         boost::asio::io_context& io);
38 
39     // disable copy
40     EntityManager(const EntityManager&) = delete;
41     EntityManager& operator=(const EntityManager&) = delete;
42 
43     // disable move
44     EntityManager(EntityManager&&) = delete;
45     EntityManager& operator=(EntityManager&&) = delete;
46 
47     ~EntityManager() = default;
48 
49     std::shared_ptr<sdbusplus::asio::connection> systemBus;
50     sdbusplus::asio::object_server objServer;
51     std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface;
52     Configuration configuration;
53     nlohmann::json lastJson;
54     nlohmann::json systemConfiguration;
55     Topology topology;
56     boost::asio::io_context& io;
57 
58     dbus_interface::EMDBusInterface dbus_interface;
59 
60     power::PowerStatusMonitor powerStatus;
61 
62     void propertiesChangedCallback();
63     void registerCallback(const std::string& path);
64     void publishNewConfiguration(const size_t& instance, size_t count,
65                                  boost::asio::steady_timer& timer,
66                                  nlohmann::json newConfiguration);
67     void postToDbus(const nlohmann::json& newConfiguration);
68     void pruneConfiguration(bool powerOff, const std::string& name,
69                             const nlohmann::json& device);
70 
71     void handleCurrentConfigurationJson();
72 
73   private:
74     std::unique_ptr<sdbusplus::bus::match_t> nameOwnerChangedMatch = nullptr;
75     std::unique_ptr<sdbusplus::bus::match_t> interfacesAddedMatch = nullptr;
76     std::unique_ptr<sdbusplus::bus::match_t> interfacesRemovedMatch = nullptr;
77 
78     bool scannedPowerOff = false;
79     bool scannedPowerOn = false;
80 
81     bool propertiesChangedInProgress = false;
82     boost::asio::steady_timer propertiesChangedTimer;
83     size_t propertiesChangedInstance = 0;
84 
85     boost::container::flat_map<std::string, sdbusplus::bus::match_t>
86         dbusMatches;
87 
88     void startRemovedTimer(boost::asio::steady_timer& timer,
89                            nlohmann::json& systemConfiguration);
90     void initFilters(const std::unordered_set<std::string>& probeInterfaces);
91 };
92