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