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" 19 #include "group.hpp" 20 #include "trigger_aliases.hpp" 21 22 #include <nlohmann/json.hpp> 23 #include <phosphor-logging/log.hpp> 24 25 #include <chrono> 26 #include <format> 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>( 54 std::format("Timer trigger type '{}' is not supported", type) 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 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 83 enableTrigger 84 triggerTimer(const json& jsonObj, const std::string& /*eventName*/, 85 std::vector<std::unique_ptr<ActionBase>>& /*actions*/) 86 { 87 // Get the type and interval of this timer from the JSON 88 auto type = getType(jsonObj); 89 auto interval = getInterval(jsonObj); 90 auto preload = getPreload(jsonObj); 91 92 return [type = std::move(type), interval = std::move(interval), 93 preload = std::move(preload)]( 94 const std::string& eventName, Manager* mgr, 95 const std::vector<Group>& groups, 96 std::vector<std::unique_ptr<ActionBase>>& actions) { 97 auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions), 98 std::cref(groups), preload); 99 mgr->addTimer(type, interval, std::move(tpPtr)); 100 }; 101 } 102 103 } // namespace phosphor::fan::control::json::trigger::timer 104