1 #include "util.hpp" 2 3 #include <gpiod.hpp> 4 5 namespace phosphor::power::psu 6 { 7 8 const UtilBase& getUtils() 9 { 10 static Util util; 11 return util; 12 } 13 14 GPIOReader::GPIOReader(const std::string& namedGpio) 15 { 16 try 17 { 18 line = gpiod::find_line(namedGpio); 19 } 20 catch (std::exception& e) 21 { 22 phosphor::logging::log<phosphor::logging::level::ERR>( 23 fmt::format("Failed to find line: {}", e.what()).c_str()); 24 throw; 25 } 26 } 27 28 std::unique_ptr<GPIOInterface> 29 GPIOReader::createGPIO(const std::string& namedGpio) 30 { 31 return std::make_unique<GPIOReader>(namedGpio); 32 } 33 34 int GPIOReader::read() 35 { 36 using namespace phosphor::logging; 37 38 int value = -1; 39 40 if (!line) 41 { 42 log<level::ERR>("Failed line"); 43 throw std::runtime_error{std::string{"Failed to find line"}}; 44 } 45 46 try 47 { 48 line.request({__FUNCTION__, gpiod::line_request::DIRECTION_INPUT, 49 gpiod::line_request::FLAG_ACTIVE_LOW}); 50 try 51 { 52 value = line.get_value(); 53 } 54 catch (std::exception& e) 55 { 56 log<level::ERR>( 57 fmt::format("Failed to get_value of GPIO line: {}", e.what()) 58 .c_str()); 59 line.release(); 60 throw; 61 } 62 63 line.release(); 64 } 65 catch (std::exception& e) 66 { 67 log<level::ERR>("Failed to request GPIO line", 68 entry("MSG=%s", e.what())); 69 throw; 70 } 71 72 return value; 73 } 74 75 std::unique_ptr<GPIOInterface> createGPIO(const std::string& namedGpio) 76 { 77 return GPIOReader::createGPIO(namedGpio); 78 } 79 80 } // namespace phosphor::power::psu 81