blob: 2036042c31b484a8ae3f36f548d8e35146165c68 [file] [log] [blame]
Matthew Barth93341e02021-04-14 13:56:28 -05001/**
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"
Matthew Barthcd6f3792021-09-30 15:13:25 -050019#include "group.hpp"
Matthew Barth54b5a242021-05-21 11:02:52 -050020#include "trigger_aliases.hpp"
Matthew Barth93341e02021-04-14 13:56:28 -050021
Matthew Barth93341e02021-04-14 13:56:28 -050022#include <nlohmann/json.hpp>
23#include <phosphor-logging/log.hpp>
24
25#include <chrono>
Patrick Williamsfbf47032023-07-17 12:27:34 -050026#include <format>
Matthew Barth93341e02021-04-14 13:56:28 -050027
28namespace phosphor::fan::control::json::trigger::timer
29{
30
31using json = nlohmann::json;
32using namespace phosphor::logging;
33
34TimerType getType(const json& jsonObj)
35{
36 if (!jsonObj.contains("type"))
37 {
38 log<level::ERR>("Missing required timer trigger type",
39 entry("JSON=%s", jsonObj.dump().c_str()));
40 throw std::runtime_error("Missing required timer trigger type");
41 }
42 auto type = jsonObj["type"].get<std::string>();
43 if (type == "oneshot")
44 {
45 return TimerType::oneshot;
46 }
47 else if (type == "repeating")
48 {
49 return TimerType::repeating;
50 }
51 else
52 {
53 log<level::ERR>(
Patrick Williamsfbf47032023-07-17 12:27:34 -050054 std::format("Timer trigger type '{}' is not supported", type)
Matthew Barth93341e02021-04-14 13:56:28 -050055 .c_str(),
56 entry("AVAILABLE_TYPES={oneshot, repeating}"));
57 throw std::runtime_error("Unsupported timer trigger type given");
58 }
59}
60
61std::chrono::microseconds getInterval(const json& jsonObj)
62{
63 if (!jsonObj.contains("interval"))
64 {
65 log<level::ERR>("Missing required timer trigger interval",
66 entry("JSON=%s", jsonObj.dump().c_str()));
67 throw std::runtime_error("Missing required timer trigger interval");
68 }
69 return static_cast<std::chrono::microseconds>(
70 jsonObj["interval"].get<uint64_t>());
71}
72
Matt Spinlerade0c372021-10-28 16:09:44 -050073bool getPreload(const json& jsonObj)
74{
75 if (jsonObj.contains("preload_groups") &&
76 jsonObj["preload_groups"].get<bool>())
77 {
78 return true;
79 }
80 return false;
81}
82
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040083enableTrigger
84 triggerTimer(const json& jsonObj, const std::string& /*eventName*/,
85 std::vector<std::unique_ptr<ActionBase>>& /*actions*/)
Matthew Barth93341e02021-04-14 13:56:28 -050086{
87 // Get the type and interval of this timer from the JSON
88 auto type = getType(jsonObj);
89 auto interval = getInterval(jsonObj);
Matt Spinlerade0c372021-10-28 16:09:44 -050090 auto preload = getPreload(jsonObj);
Matthew Barth93341e02021-04-14 13:56:28 -050091
Matt Spinlerade0c372021-10-28 16:09:44 -050092 return [type = std::move(type), interval = std::move(interval),
93 preload = std::move(preload)](
Matthew Barth54b5a242021-05-21 11:02:52 -050094 const std::string& eventName, Manager* mgr,
Matt Spinlerade0c372021-10-28 16:09:44 -050095 const std::vector<Group>& groups,
Matthew Barth54b5a242021-05-21 11:02:52 -050096 std::vector<std::unique_ptr<ActionBase>>& actions) {
Matt Spinlerade0c372021-10-28 16:09:44 -050097 auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions),
98 std::cref(groups), preload);
Matthew Barth54b5a242021-05-21 11:02:52 -050099 mgr->addTimer(type, interval, std::move(tpPtr));
100 };
Matthew Barth93341e02021-04-14 13:56:28 -0500101}
102
103} // namespace phosphor::fan::control::json::trigger::timer