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 #include "timer.hpp"
17 
18 #include "../manager.hpp"
19 #include "group.hpp"
20 #include "trigger_aliases.hpp"
21 
22 #include <fmt/format.h>
23 
24 #include <nlohmann/json.hpp>
25 #include <phosphor-logging/log.hpp>
26 
27 #include <chrono>
28 
29 namespace phosphor::fan::control::json::trigger::timer
30 {
31 
32 using json = nlohmann::json;
33 using namespace phosphor::logging;
34 
35 TimerType getType(const json& jsonObj)
36 {
37     if (!jsonObj.contains("type"))
38     {
39         log<level::ERR>("Missing required timer trigger type",
40                         entry("JSON=%s", jsonObj.dump().c_str()));
41         throw std::runtime_error("Missing required timer trigger type");
42     }
43     auto type = jsonObj["type"].get<std::string>();
44     if (type == "oneshot")
45     {
46         return TimerType::oneshot;
47     }
48     else if (type == "repeating")
49     {
50         return TimerType::repeating;
51     }
52     else
53     {
54         log<level::ERR>(
55             fmt::format("Timer trigger type '{}' is not supported", type)
56                 .c_str(),
57             entry("AVAILABLE_TYPES={oneshot, repeating}"));
58         throw std::runtime_error("Unsupported timer trigger type given");
59     }
60 }
61 
62 std::chrono::microseconds getInterval(const json& jsonObj)
63 {
64     if (!jsonObj.contains("interval"))
65     {
66         log<level::ERR>("Missing required timer trigger interval",
67                         entry("JSON=%s", jsonObj.dump().c_str()));
68         throw std::runtime_error("Missing required timer trigger interval");
69     }
70     return static_cast<std::chrono::microseconds>(
71         jsonObj["interval"].get<uint64_t>());
72 }
73 
74 bool getPreload(const json& jsonObj)
75 {
76     if (jsonObj.contains("preload_groups") &&
77         jsonObj["preload_groups"].get<bool>())
78     {
79         return true;
80     }
81     return false;
82 }
83 
84 enableTrigger
85     triggerTimer(const json& jsonObj, const std::string& /*eventName*/,
86                  std::vector<std::unique_ptr<ActionBase>>& /*actions*/)
87 {
88     // Get the type and interval of this timer from the JSON
89     auto type = getType(jsonObj);
90     auto interval = getInterval(jsonObj);
91     auto preload = getPreload(jsonObj);
92 
93     return [type = std::move(type), interval = std::move(interval),
94             preload = std::move(preload)](
95                const std::string& eventName, Manager* mgr,
96                const std::vector<Group>& groups,
97                std::vector<std::unique_ptr<ActionBase>>& actions) {
98         auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions),
99                                                 std::cref(groups), preload);
100         mgr->addTimer(type, interval, std::move(tpPtr));
101     };
102 }
103 
104 } // namespace phosphor::fan::control::json::trigger::timer
105