blob: 87c2a635070b955305e70ed0b368b7d496d7d652 [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
22#include <fmt/format.h>
23
24#include <nlohmann/json.hpp>
25#include <phosphor-logging/log.hpp>
26
27#include <chrono>
28
29namespace phosphor::fan::control::json::trigger::timer
30{
31
32using json = nlohmann::json;
33using namespace phosphor::logging;
34
35TimerType 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
62std::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
Matthew Barth54b5a242021-05-21 11:02:52 -050074enableTrigger triggerTimer(const json& jsonObj, const std::string& eventName,
Matthew Barth54b5a242021-05-21 11:02:52 -050075 std::vector<std::unique_ptr<ActionBase>>& actions)
Matthew Barth93341e02021-04-14 13:56:28 -050076{
77 // Get the type and interval of this timer from the JSON
78 auto type = getType(jsonObj);
79 auto interval = getInterval(jsonObj);
80
Matthew Barth54b5a242021-05-21 11:02:52 -050081 return [type = std::move(type), interval = std::move(interval)](
82 const std::string& eventName, Manager* mgr,
Matthew Barthcd6f3792021-09-30 15:13:25 -050083 const std::vector<Group>&,
Matthew Barth54b5a242021-05-21 11:02:52 -050084 std::vector<std::unique_ptr<ActionBase>>& actions) {
85 auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions));
86 mgr->addTimer(type, interval, std::move(tpPtr));
87 };
Matthew Barth93341e02021-04-14 13:56:28 -050088}
89
90} // namespace phosphor::fan::control::json::trigger::timer