Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 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" |
Matthew Barth | cd6f379 | 2021-09-30 15:13:25 -0500 | [diff] [blame] | 19 | #include "group.hpp" |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 20 | #include "trigger_aliases.hpp" |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 21 | |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 22 | #include <nlohmann/json.hpp> |
| 23 | #include <phosphor-logging/log.hpp> |
| 24 | |
| 25 | #include <chrono> |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 26 | #include <format> |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 27 | |
| 28 | namespace phosphor::fan::control::json::trigger::timer |
| 29 | { |
| 30 | |
| 31 | using json = nlohmann::json; |
| 32 | using namespace phosphor::logging; |
| 33 | |
| 34 | TimerType 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 Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 54 | std::format("Timer trigger type '{}' is not supported", type) |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 55 | .c_str(), |
| 56 | entry("AVAILABLE_TYPES={oneshot, repeating}")); |
| 57 | throw std::runtime_error("Unsupported timer trigger type given"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | std::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 Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 73 | bool 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 Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 83 | enableTrigger |
| 84 | triggerTimer(const json& jsonObj, const std::string& /*eventName*/, |
| 85 | std::vector<std::unique_ptr<ActionBase>>& /*actions*/) |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 86 | { |
| 87 | // Get the type and interval of this timer from the JSON |
| 88 | auto type = getType(jsonObj); |
| 89 | auto interval = getInterval(jsonObj); |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 90 | auto preload = getPreload(jsonObj); |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 91 | |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 92 | return [type = std::move(type), interval = std::move(interval), |
| 93 | preload = std::move(preload)]( |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 94 | const std::string& eventName, Manager* mgr, |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 95 | const std::vector<Group>& groups, |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 96 | std::vector<std::unique_ptr<ActionBase>>& actions) { |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 97 | auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions), |
| 98 | std::cref(groups), preload); |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 99 | mgr->addTimer(type, interval, std::move(tpPtr)); |
| 100 | }; |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace phosphor::fan::control::json::trigger::timer |