1 /**
2  * Copyright © 2020 IBM 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 #pragma once
17 
18 #include "config_base.hpp"
19 
20 #include <nlohmann/json.hpp>
21 #include <sdbusplus/bus.hpp>
22 
23 namespace phosphor::fan::control::json
24 {
25 
26 using json = nlohmann::json;
27 using methodHandler = std::function<bool(const json&)>;
28 
29 /**
30  * @class Profile - Represents a configured fan control profile
31  *
32  * Fan control profiles are optional, therefore the "profiles.json" file is
33  * also optional. A profile can be used to load specific fan control events
34  * based on the configuration of the profile. Fan control events configured
35  * with no profile(s) are always used and events configured for a specified
36  * profile are included when that profile is enabled.
37  *
38  * When no profiles exist, all configured fan control events are used.
39  */
40 class Profile : public ConfigBase
41 {
42   public:
43     /* JSON file name for profiles */
44     static constexpr auto confFileName = "profiles.json";
45 
46     Profile() = delete;
47     Profile(const Profile&) = delete;
48     Profile(Profile&&) = delete;
49     Profile& operator=(const Profile&) = delete;
50     Profile& operator=(Profile&&) = delete;
51     ~Profile() = default;
52 
53     /**
54      * Constructor
55      * Parses and populates a zone profile from JSON object data
56      *
57      * @param[in] bus - sdbusplus bus object
58      * @param[in] jsonObj - JSON object
59      */
60     Profile(sdbusplus::bus::bus& bus, const json& jsonObj);
61 
62     /**
63      * @brief Get the active state
64      *
65      * @return The active state of the profile
66      */
67     inline bool isActive() const
68     {
69         return _active;
70     }
71 
72   private:
73     /* The sdbusplus bus object */
74     sdbusplus::bus::bus& _bus;
75 
76     /* Active state of the profile */
77     bool _active;
78 
79     /* Supported methods to their corresponding handler functions */
80     static const std::map<std::string, methodHandler> _methods;
81 
82     /**
83      * @brief Parse and set the profile's active state
84      *
85      * @param[in] jsonObj - JSON object for the profile
86      *
87      * Sets the active state of the profile using the configured method of
88      * determining its active state.
89      */
90     void setActive(const json& jsonObj);
91 
92     /**
93      * @brief An active state method where all must be true
94      *
95      * @param[in] method - JSON for the profile's method
96      *
97      * Active state method that takes a list of configured dbus properties where
98      * all of those properties must equal their configured values to set the
99      * profile to be active.
100      *
101      * "name": "all_of",
102      * "properties": [
103      *     {
104      *         "path": "[DBUS PATH]",
105      *         "interface": "[DBUS INTERFACE]",
106      *         "property": "[DBUS PROPERTY]",
107      *         "value": [VALUE TO BE ACTIVE]
108      *     }
109      * ]
110      */
111     static bool allOf(const json& method);
112 };
113 
114 } // namespace phosphor::fan::control::json
115