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> |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 23 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 24 | |
| 25 | #include <chrono> |
| 26 | |
| 27 | namespace phosphor::fan::control::json::trigger::timer |
| 28 | { |
| 29 | |
| 30 | using json = nlohmann::json; |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 31 | |
| 32 | TimerType getType(const json& jsonObj) |
| 33 | { |
| 34 | if (!jsonObj.contains("type")) |
| 35 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 36 | lg2::error("Missing required timer trigger type", "JSON", |
| 37 | jsonObj.dump()); |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 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 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 51 | lg2::error( |
| 52 | "Timer trigger type '{TYPE}' is not supported. Available types are 'oneshot, repeating'", |
| 53 | "TYPE", type); |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 54 | throw std::runtime_error("Unsupported timer trigger type given"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | std::chrono::microseconds getInterval(const json& jsonObj) |
| 59 | { |
| 60 | if (!jsonObj.contains("interval")) |
| 61 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 62 | lg2::error("Missing required timer trigger interval", "JSON", |
| 63 | jsonObj.dump()); |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 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 | |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 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 | |
Patrick Williams | 4fa67aa | 2025-02-03 14:28:47 -0500 | [diff] [blame] | 80 | enableTrigger triggerTimer( |
| 81 | const json& jsonObj, const std::string& /*eventName*/, |
| 82 | std::vector<std::unique_ptr<ActionBase>>& /*actions*/) |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 83 | { |
| 84 | // Get the type and interval of this timer from the JSON |
| 85 | auto type = getType(jsonObj); |
| 86 | auto interval = getInterval(jsonObj); |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 87 | auto preload = getPreload(jsonObj); |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 88 | |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 89 | return [type = std::move(type), interval = std::move(interval), |
| 90 | preload = std::move(preload)]( |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 91 | const std::string& eventName, Manager* mgr, |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 92 | const std::vector<Group>& groups, |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 93 | std::vector<std::unique_ptr<ActionBase>>& actions) { |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 94 | auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions), |
| 95 | std::cref(groups), preload); |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 96 | mgr->addTimer(type, interval, std::move(tpPtr)); |
| 97 | }; |
Matthew Barth | 93341e0 | 2021-04-14 13:56:28 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace phosphor::fan::control::json::trigger::timer |