1 #pragma once
2 
3 #include "config.h"
4 
5 #include "occ_status.hpp"
6 #include "utils.hpp"
7 
8 #include <experimental/filesystem>
9 #include <sdbusplus/bus.hpp>
10 #include <sdbusplus/bus/match.hpp>
11 
12 namespace open_power
13 {
14 namespace occ
15 {
16 namespace powercap
17 {
18 
19 namespace sdbusRule = sdbusplus::bus::match::rules;
20 
21 /** @class PowerCap
22  *  @brief Monitors for changes to the power cap and notifies occ
23  *
24  *  The customer power cap is provided to the OCC by host TMGT when the occ
25  *  first goes active or is reset.  This code is responsible for sending
26  *  the power cap to the OCC if the cap is changed while the occ is active.
27  */
28 
29 class PowerCap
30 {
31   public:
32     /** @brief PowerCap object to inform occ of changes to cap
33      *
34      * This object will monitor for changes to the power cap setting and
35      * power cap enable properties.  If a change is detected, and the occ
36      * is active, then this object will notify the OCC of the change.
37      *
38      * @param[in] occStatus - The occ status object
39      */
40     PowerCap(Status& occStatus,
41              const std::string& occMasterName = OCC_MASTER_NAME) :
42         occMasterName(occMasterName),
43         occStatus(occStatus),
44         pcapMatch(
45             utils::getBus(),
46             sdbusRule::member("PropertiesChanged") +
47                 sdbusRule::path(
48                     "/xyz/openbmc_project/control/host0/power_cap") +
49                 sdbusRule::argN(0, "xyz.openbmc_project.Control.Power.Cap") +
50                 sdbusRule::interface("org.freedesktop.DBus.Properties"),
51             std::bind(std::mem_fn(&PowerCap::pcapChanged), this,
52                       std::placeholders::_1)){};
53 
54     /** @brief Return the appropriate value to write to the OCC
55      *
56      * @param[in]  pcap        - Current user power cap setting
57      * @param[in]  pcapEnabled - Current power cap enable setting
58      *
59      * @return The value to write to the occ user pcap
60      */
61     uint32_t getOccInput(uint32_t pcap, bool pcapEnabled);
62 
63   private:
64     /** @brief Callback for pcap setting changes
65      *
66      * Process change and inform OCC
67      *
68      * @param[in]  msg       - Data associated with pcap change signal
69      *
70      */
71     void pcapChanged(sdbusplus::message::message& msg);
72 
73     /** @brief Get the power cap property
74      *
75      * @return Power cap, 0 on failure to indicate no pcap
76      */
77     uint32_t getPcap();
78 
79     /** @brief Get the power cap enable property
80      *
81      * @return Whether power cap enabled, will return false on error
82      */
83     bool getPcapEnabled();
84 
85     /** @brief Write the input power cap to the occ hwmon entry
86      *
87      * @param[in]  pcapValue - Power cap value to write to OCC
88      */
89     void writeOcc(uint32_t pcapValue);
90 
91     /**
92      * @brief Returns the filename to use for the user power cap
93      *
94      * The file is of the form "powerX_cap_user", where X is any
95      * number.
96      *
97      * @param[in] path - The directory to look for the file in
98      *
99      * @return std::string - The filename, or empty string if not found.
100      */
101     std::string
102         getPcapFilename(const std::experimental::filesystem::path& path);
103 
104     /** @brief The master occ name */
105     std::string occMasterName;
106 
107     /* @brief OCC Status object */
108     Status& occStatus;
109 
110     /** @brief Used to subscribe to dbus pcap property changes **/
111     sdbusplus::bus::match_t pcapMatch;
112 };
113 
114 } // namespace powercap
115 
116 } // namespace occ
117 
118 } // namespace open_power
119