xref: /openbmc/s2600wf-misc/callback-manager/include/callback_manager.hpp (revision 05f8d57e6592414302fb5ad2eb2d90c518b1f6bf)
1 #pragma once
2 
3 #include <boost/algorithm/string/predicate.hpp>
4 #include <iostream>
5 #include <sdbusplus/asio/connection.hpp>
6 #include <sdbusplus/asio/object_server.hpp>
7 
8 using Association = std::tuple<std::string, std::string, std::string>;
9 
10 constexpr const char* rootPath = "/xyz/openbmc_project/CallbackManager";
11 constexpr const char* sensorPath = "/xyz/openbmc_project/sensors";
12 
13 constexpr const char* globalInventoryIface =
14     "xyz.openbmc_project.Inventory.Item.Global";
15 constexpr const char* associationIface =
16     "xyz.openbmc_project.Association.Definitions";
17 
18 namespace threshold
19 {
20 constexpr const char* critical = "critical";
21 constexpr const char* warning = "warning";
22 } // namespace threshold
23 
24 struct AssociationManager
25 {
26     AssociationManager(sdbusplus::asio::object_server& objectServer,
27                        std::shared_ptr<sdbusplus::asio::connection>& conn) :
28         objectServer(objectServer),
29         association(objectServer.add_interface(rootPath, associationIface)),
30         sensorAssociation(
31             objectServer.add_interface(sensorPath, associationIface))
32     {
33         association->register_property("Associations", std::set<Association>());
34         sensorAssociation->register_property("Associations",
35                                              std::set<Association>());
36         association->initialize();
37         sensorAssociation->initialize();
38     }
39     ~AssociationManager()
40     {
41         objectServer.remove_interface(association);
42         objectServer.remove_interface(sensorAssociation);
43     }
44 
45     void setLocalAssociations(const std::vector<std::string>& fatal,
46                               const std::vector<std::string>& critical,
47                               const std::vector<std::string>& warning)
48     {
49         std::set<Association> result;
50 
51         // fatal maps to redfish critical as refish only has 3 states and LED
52         // has 4
53         for (const std::string& path : fatal)
54         {
55             result.emplace(threshold::critical, "", path);
56         }
57         for (const std::string& path : critical)
58         {
59             result.emplace(threshold::warning, "", path);
60         }
61         for (const std::string& path : warning)
62         {
63             result.emplace(threshold::warning, "", path);
64         }
65         association->set_property("Associations", result);
66     }
67 
68     void setSensorAssociations(const std::vector<std::string>& critical,
69                                const std::vector<std::string>& warning)
70     {
71         std::set<Association> result;
72         for (const std::string& path : critical)
73         {
74             if (!boost::starts_with(path, sensorPath))
75             {
76                 continue;
77             }
78             result.emplace(threshold::critical, "", path);
79         }
80         for (const std::string& path : warning)
81         {
82             if (!boost::starts_with(path, sensorPath))
83             {
84                 continue;
85             }
86             result.emplace(threshold::warning, "", path);
87         }
88         sensorAssociation->set_property("Associations", result);
89     }
90 
91     sdbusplus::asio::object_server& objectServer;
92     std::shared_ptr<sdbusplus::asio::dbus_interface> association;
93     std::shared_ptr<sdbusplus::asio::dbus_interface> sensorAssociation;
94 };
95