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