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