129e9e385SMatthew Barth /** 229e9e385SMatthew Barth * Copyright © 2020 IBM Corporation 329e9e385SMatthew Barth * 429e9e385SMatthew Barth * Licensed under the Apache License, Version 2.0 (the "License"); 529e9e385SMatthew Barth * you may not use this file except in compliance with the License. 629e9e385SMatthew Barth * You may obtain a copy of the License at 729e9e385SMatthew Barth * 829e9e385SMatthew Barth * http://www.apache.org/licenses/LICENSE-2.0 929e9e385SMatthew Barth * 1029e9e385SMatthew Barth * Unless required by applicable law or agreed to in writing, software 1129e9e385SMatthew Barth * distributed under the License is distributed on an "AS IS" BASIS, 1229e9e385SMatthew Barth * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1329e9e385SMatthew Barth * See the License for the specific language governing permissions and 1429e9e385SMatthew Barth * limitations under the License. 1529e9e385SMatthew Barth */ 1629e9e385SMatthew Barth 1729e9e385SMatthew Barth #include "manager.hpp" 1829e9e385SMatthew Barth 19bbc7c583SMatthew Barth #include "utility.hpp" 20bbc7c583SMatthew Barth 2129e9e385SMatthew Barth #include <sdbusplus/bus.hpp> 2229e9e385SMatthew Barth 23f2bcf1f9SMatthew Barth #include <chrono> 24*250d0a98SMatthew Barth #include <variant> 25f2bcf1f9SMatthew Barth 2629e9e385SMatthew Barth namespace phosphor 2729e9e385SMatthew Barth { 2829e9e385SMatthew Barth namespace power 2929e9e385SMatthew Barth { 3029e9e385SMatthew Barth namespace regulators 3129e9e385SMatthew Barth { 3229e9e385SMatthew Barth 33f2bcf1f9SMatthew Barth Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) : 34bbc7c583SMatthew Barth ManagerObject(bus, objPath, true), bus(bus), eventLoop(event), fileName("") 3529e9e385SMatthew Barth { 36*250d0a98SMatthew Barth // Subscribe to interfacesAdded signal for filename property 37*250d0a98SMatthew Barth std::unique_ptr<sdbusplus::server::match::match> matchPtr = 38*250d0a98SMatthew Barth std::make_unique<sdbusplus::server::match::match>( 39*250d0a98SMatthew Barth bus, 40*250d0a98SMatthew Barth sdbusplus::bus::match::rules::interfacesAdded(sysDbusObj).c_str(), 41*250d0a98SMatthew Barth std::bind(std::mem_fn(&Manager::signalHandler), this, 42*250d0a98SMatthew Barth std::placeholders::_1)); 43*250d0a98SMatthew Barth signals.emplace_back(std::move(matchPtr)); 44*250d0a98SMatthew Barth 45bbc7c583SMatthew Barth // Attempt to get the filename property from dbus 46bbc7c583SMatthew Barth setFileName(getFileNameDbus()); 47bbc7c583SMatthew Barth 48bbc7c583SMatthew Barth if (!fileName.empty()) 49bbc7c583SMatthew Barth { 50bbc7c583SMatthew Barth // TODO Load & parse JSON configuration data file 51bbc7c583SMatthew Barth } 5229e9e385SMatthew Barth 5329e9e385SMatthew Barth // Obtain dbus service name 5429e9e385SMatthew Barth bus.request_name(busName); 5529e9e385SMatthew Barth } 5629e9e385SMatthew Barth 5729e9e385SMatthew Barth void Manager::configure() 5829e9e385SMatthew Barth { 5929e9e385SMatthew Barth // TODO Configuration errors that should halt poweron, 6029e9e385SMatthew Barth // throw InternalFailure exception (or similar) to 6129e9e385SMatthew Barth // fail the call(busctl) to this method 6229e9e385SMatthew Barth } 6329e9e385SMatthew Barth 6429e9e385SMatthew Barth void Manager::monitor(bool enable) 6529e9e385SMatthew Barth { 6629e9e385SMatthew Barth if (enable) 6729e9e385SMatthew Barth { 68f2bcf1f9SMatthew Barth Timer timer(eventLoop, std::bind(&Manager::timerExpired, this)); 69f2bcf1f9SMatthew Barth // Set timer as a repeating 1sec timer 70f2bcf1f9SMatthew Barth timer.restart(std::chrono::milliseconds(1000)); 71f2bcf1f9SMatthew Barth timers.emplace_back(std::move(timer)); 7229e9e385SMatthew Barth } 7329e9e385SMatthew Barth else 7429e9e385SMatthew Barth { 75f2bcf1f9SMatthew Barth // Delete all timers to disable monitoring 76f2bcf1f9SMatthew Barth timers.clear(); 7729e9e385SMatthew Barth } 7829e9e385SMatthew Barth } 7929e9e385SMatthew Barth 80f2bcf1f9SMatthew Barth void Manager::timerExpired() 81f2bcf1f9SMatthew Barth { 82f2bcf1f9SMatthew Barth // TODO Analyze, refresh sensor status, and 83f2bcf1f9SMatthew Barth // collect/update telemetry for each regulator 84f2bcf1f9SMatthew Barth } 85f2bcf1f9SMatthew Barth 867cbc5536SMatthew Barth void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/, 877cbc5536SMatthew Barth const struct signalfd_siginfo* /*sigInfo*/) 887cbc5536SMatthew Barth { 897cbc5536SMatthew Barth // TODO Reload and process the configuration data 907cbc5536SMatthew Barth } 917cbc5536SMatthew Barth 92*250d0a98SMatthew Barth void Manager::signalHandler(sdbusplus::message::message& msg) 93*250d0a98SMatthew Barth { 94*250d0a98SMatthew Barth if (msg) 95*250d0a98SMatthew Barth { 96*250d0a98SMatthew Barth sdbusplus::message::object_path op; 97*250d0a98SMatthew Barth msg.read(op); 98*250d0a98SMatthew Barth if (static_cast<const std::string&>(op) != sysDbusPath) 99*250d0a98SMatthew Barth { 100*250d0a98SMatthew Barth // Object path does not match the path 101*250d0a98SMatthew Barth return; 102*250d0a98SMatthew Barth } 103*250d0a98SMatthew Barth 104*250d0a98SMatthew Barth // An interfacesAdded signal returns a dictionary of interface 105*250d0a98SMatthew Barth // names to a dictionary of properties and their values 106*250d0a98SMatthew Barth // https://dbus.freedesktop.org/doc/dbus-specification.html 107*250d0a98SMatthew Barth std::map<std::string, std::map<std::string, std::variant<std::string>>> 108*250d0a98SMatthew Barth intfProp; 109*250d0a98SMatthew Barth msg.read(intfProp); 110*250d0a98SMatthew Barth auto itIntf = intfProp.find(sysDbusIntf); 111*250d0a98SMatthew Barth if (itIntf == intfProp.cend()) 112*250d0a98SMatthew Barth { 113*250d0a98SMatthew Barth // Interface not found on the path 114*250d0a98SMatthew Barth return; 115*250d0a98SMatthew Barth } 116*250d0a98SMatthew Barth auto itProp = itIntf->second.find(sysDbusProp); 117*250d0a98SMatthew Barth if (itProp == itIntf->second.cend()) 118*250d0a98SMatthew Barth { 119*250d0a98SMatthew Barth // Property not found on the interface 120*250d0a98SMatthew Barth return; 121*250d0a98SMatthew Barth } 122*250d0a98SMatthew Barth // Set fileName and call parse json function 123*250d0a98SMatthew Barth setFileName(std::get<std::string>(itProp->second)); 124*250d0a98SMatthew Barth // TODO Load & parse JSON configuration data file 125*250d0a98SMatthew Barth } 126*250d0a98SMatthew Barth } 127*250d0a98SMatthew Barth 128bbc7c583SMatthew Barth const std::string Manager::getFileNameDbus() 129bbc7c583SMatthew Barth { 130bbc7c583SMatthew Barth std::string fileName = ""; 131bbc7c583SMatthew Barth using namespace phosphor::power::util; 132bbc7c583SMatthew Barth 133bbc7c583SMatthew Barth try 134bbc7c583SMatthew Barth { 135bbc7c583SMatthew Barth // Do not log an error when service or property are not found 136bbc7c583SMatthew Barth auto service = getService(sysDbusPath, sysDbusIntf, bus, false); 137bbc7c583SMatthew Barth if (!service.empty()) 138bbc7c583SMatthew Barth { 139bbc7c583SMatthew Barth getProperty(sysDbusIntf, sysDbusProp, sysDbusPath, service, bus, 140bbc7c583SMatthew Barth fileName); 141bbc7c583SMatthew Barth } 142bbc7c583SMatthew Barth } 143bbc7c583SMatthew Barth catch (const sdbusplus::exception::SdBusError&) 144bbc7c583SMatthew Barth { 145bbc7c583SMatthew Barth // File name property not available on dbus 146bbc7c583SMatthew Barth fileName = ""; 147bbc7c583SMatthew Barth } 148bbc7c583SMatthew Barth 149bbc7c583SMatthew Barth return fileName; 150bbc7c583SMatthew Barth } 151bbc7c583SMatthew Barth 15229e9e385SMatthew Barth } // namespace regulators 15329e9e385SMatthew Barth } // namespace power 15429e9e385SMatthew Barth } // namespace phosphor 155