blob: fd3aa109b4a662644bdef457763992d6cfba71f9 [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>
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000023#include <phosphor-logging/lg2.hpp>
Matthew Barth93341e02021-04-14 13:56:28 -050024
25#include <chrono>
26
27namespace phosphor::fan::control::json::trigger::timer
28{
29
30using json = nlohmann::json;
Matthew Barth93341e02021-04-14 13:56:28 -050031
32TimerType getType(const json& jsonObj)
33{
34 if (!jsonObj.contains("type"))
35 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000036 lg2::error("Missing required timer trigger type", "JSON",
37 jsonObj.dump());
Matthew Barth93341e02021-04-14 13:56:28 -050038 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 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000051 lg2::error(
52 "Timer trigger type '{TYPE}' is not supported. Available types are 'oneshot, repeating'",
53 "TYPE", type);
Matthew Barth93341e02021-04-14 13:56:28 -050054 throw std::runtime_error("Unsupported timer trigger type given");
55 }
56}
57
58std::chrono::microseconds getInterval(const json& jsonObj)
59{
60 if (!jsonObj.contains("interval"))
61 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000062 lg2::error("Missing required timer trigger interval", "JSON",
63 jsonObj.dump());
Matthew Barth93341e02021-04-14 13:56:28 -050064 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
Matt Spinlerade0c372021-10-28 16:09:44 -050070bool 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
Patrick Williams4fa67aa2025-02-03 14:28:47 -050080enableTrigger triggerTimer(
81 const json& jsonObj, const std::string& /*eventName*/,
82 std::vector<std::unique_ptr<ActionBase>>& /*actions*/)
Matthew Barth93341e02021-04-14 13:56:28 -050083{
84 // Get the type and interval of this timer from the JSON
85 auto type = getType(jsonObj);
86 auto interval = getInterval(jsonObj);
Matt Spinlerade0c372021-10-28 16:09:44 -050087 auto preload = getPreload(jsonObj);
Matthew Barth93341e02021-04-14 13:56:28 -050088
Matt Spinlerade0c372021-10-28 16:09:44 -050089 return [type = std::move(type), interval = std::move(interval),
90 preload = std::move(preload)](
Matthew Barth54b5a242021-05-21 11:02:52 -050091 const std::string& eventName, Manager* mgr,
Matt Spinlerade0c372021-10-28 16:09:44 -050092 const std::vector<Group>& groups,
Matthew Barth54b5a242021-05-21 11:02:52 -050093 std::vector<std::unique_ptr<ActionBase>>& actions) {
Matt Spinlerade0c372021-10-28 16:09:44 -050094 auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions),
95 std::cref(groups), preload);
Matthew Barth54b5a242021-05-21 11:02:52 -050096 mgr->addTimer(type, interval, std::move(tpPtr));
97 };
Matthew Barth93341e02021-04-14 13:56:28 -050098}
99
100} // namespace phosphor::fan::control::json::trigger::timer