1 #pragma once
2 
3 #include <functional>
4 #include <sdbusplus/bus.hpp>
5 #include "xyz/openbmc_project/State/BMC/server.hpp"
6 
7 namespace phosphor
8 {
9 namespace state
10 {
11 namespace manager
12 {
13 
14 using BMCInherit = sdbusplus::server::object::object<
15         sdbusplus::xyz::openbmc_project::State::server::BMC>;
16 namespace sdbusRule = sdbusplus::bus::match::rules;
17 
18 
19 /** @class BMC
20  *  @brief OpenBMC BMC state management implementation.
21  *  @details A concrete implementation for xyz.openbmc_project.State.BMC
22  *  DBus API.
23  */
24 class BMC : public BMCInherit
25 {
26     public:
27         /** @brief Constructs BMC State Manager
28          *
29          * @param[in] bus       - The Dbus bus object
30          * @param[in] busName   - The Dbus name to own
31          * @param[in] objPath   - The Dbus object path
32          */
33         BMC(sdbusplus::bus::bus& bus,
34             const char* objPath) :
35                 BMCInherit(bus, objPath, true),
36                 bus(bus),
37                 stateSignal(
38                         std::make_unique<decltype(stateSignal)::element_type>(
39                                 bus,
40                                 sdbusRule::type::signal() +
41                                 sdbusRule::member("JobRemoved") +
42                                 sdbusRule::path("/org/freedesktop/systemd1") +
43                                 sdbusRule::interface(
44                                         "org.freedesktop.systemd1.Manager"),
45                                 std::bind(std::mem_fn(&BMC::bmcStateChange),
46                                           this, std::placeholders::_1)))
47         {
48             subscribeToSystemdSignals();
49             discoverInitialState();
50             this->emit_object_added();
51         };
52 
53         /** @brief Set value of BMCTransition **/
54         Transition requestedBMCTransition(Transition value) override;
55 
56         /** @brief Set value of CurrentBMCState **/
57         BMCState currentBMCState(BMCState value) override;
58 
59     private:
60         /**
61          * @brief discover the state of the bmc
62          **/
63         void discoverInitialState();
64 
65         /**
66          * @brief subscribe to the systemd signals
67          **/
68         void subscribeToSystemdSignals();
69 
70         /** @brief Execute the transition request
71          *
72          *  @param[in] tranReq   - Transition requested
73          */
74         void executeTransition(Transition tranReq);
75 
76         /** @brief Callback function on bmc state change
77          *
78          * Check if the state is relevant to the BMC and if so, update
79          * corresponding BMC object's state
80          *
81          * @param[in]  msg       - Data associated with subscribed signal
82          *
83          */
84         int bmcStateChange(sdbusplus::message::message& msg);
85 
86         /** @brief Persistent sdbusplus DBus bus connection. **/
87         sdbusplus::bus::bus& bus;
88 
89         /** @brief Used to subscribe to dbus system state changes **/
90         std::unique_ptr<sdbusplus::bus::match_t> stateSignal;
91 };
92 
93 } // namespace manager
94 } // namespace state
95 } // namespace phosphor
96