1 /**
2  * Copyright © 2021 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 "../manager.hpp"
19 #include "action.hpp"
20 #include "group.hpp"
21 #include "trigger_aliases.hpp"
22 
23 #include <nlohmann/json.hpp>
24 
25 #include <functional>
26 #include <map>
27 #include <memory>
28 #include <vector>
29 
30 namespace phosphor::fan::control::json::trigger::init
31 {
32 
33 using json = nlohmann::json;
34 
35 /**
36  * @brief An init method to get properties used in an event
37  *
38  * @param[in] mgr - Pointer to manager of the event
39  * @param[in] group - Group associated with the event
40  */
41 void getProperties(Manager* mgr, const Group& group);
42 
43 /**
44  * @brief An init method to get the owner name of a service used in an event
45  *
46  * @param[in] mgr - Pointer to manager of the event
47  * @param[in] group - Group associated with the event
48  */
49 void nameHasOwner(Manager* mgr, const Group& group);
50 
51 // Handler function for method calls
52 using methodHandler = std::function<void(Manager*, const Group&)>;
53 
54 /* Supported methods to their corresponding handler functions */
55 static const std::map<std::string, methodHandler> methods = {
56     {"get_properties", getProperties}, {"name_has_owner", nameHasOwner}};
57 
58 /**
59  * @brief Trigger to process an event immediately upon fan control starting
60  *
61  * @param[in] jsonObj - JSON object for the trigger
62  * @param[in] eventName - Name of event creating the trigger
63  * @param[in] actions - Actions associated with the trigger
64  *
65  * When fan control starts (or restarts), all events with 'init' triggers are
66  * processed immediately, per its configuration, and its corresponding actions
67  * are run.
68  *
69  * Generally, this type of trigger is paired with a 'signal' class of trigger on
70  * an event so the initial data for an event is collected, processed, and run
71  * before any signal may be received.
72  */
73 enableTrigger triggerInit(const json& jsonObj, const std::string& eventName,
74                           std::vector<std::unique_ptr<ActionBase>>& actions);
75 
76 } // namespace phosphor::fan::control::json::trigger::init
77