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 "trigger_aliases.hpp" 20 21 #include <fmt/format.h> 22 23 #include <nlohmann/json.hpp> 24 #include <phosphor-logging/log.hpp> 25 26 #include <chrono> 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 fmt::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 enableTrigger triggerTimer(const json& jsonObj, const std::string& eventName, 74 std::vector<std::unique_ptr<ActionBase>>& actions) 75 { 76 // Get the type and interval of this timer from the JSON 77 auto type = getType(jsonObj); 78 auto interval = getInterval(jsonObj); 79 80 return [type = std::move(type), interval = std::move(interval)]( 81 const std::string& eventName, Manager* mgr, 82 std::vector<std::unique_ptr<ActionBase>>& actions) { 83 auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions)); 84 mgr->addTimer(type, interval, std::move(tpPtr)); 85 }; 86 } 87 88 } // namespace phosphor::fan::control::json::trigger::timer 89