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 if (!line) 20 { 21 throw std::runtime_error("Line does not exist: " + namedGpio); 22 } 23 } 24 catch (const std::exception& e) 25 { 26 phosphor::logging::log<phosphor::logging::level::ERR>( 27 fmt::format("Failed to find line: {}", e.what()).c_str()); 28 throw; 29 } 30 } 31 32 std::unique_ptr<GPIOInterface> 33 GPIOReader::createGPIO(const std::string& namedGpio) 34 { 35 return std::make_unique<GPIOReader>(namedGpio); 36 } 37 38 std::string GPIOReader::getName() const 39 { 40 return line.name(); 41 } 42 43 int GPIOReader::read() 44 { 45 using namespace phosphor::logging; 46 47 int value = -1; 48 49 if (!line) 50 { 51 log<level::ERR>("Failed line"); 52 throw std::runtime_error{std::string{"Failed to find line"}}; 53 } 54 55 try 56 { 57 line.request({__FUNCTION__, gpiod::line_request::DIRECTION_INPUT, 58 gpiod::line_request::FLAG_ACTIVE_LOW}); 59 try 60 { 61 value = line.get_value(); 62 } 63 catch (const std::exception& e) 64 { 65 log<level::ERR>( 66 fmt::format("Failed to get_value of GPIO line: {}", e.what()) 67 .c_str()); 68 line.release(); 69 throw; 70 } 71 72 line.release(); 73 } 74 catch (const std::exception& e) 75 { 76 log<level::ERR>("Failed to request GPIO line", 77 entry("MSG=%s", e.what())); 78 throw; 79 } 80 81 return value; 82 } 83 84 std::unique_ptr<GPIOInterface> createGPIO(const std::string& namedGpio) 85 { 86 return GPIOReader::createGPIO(namedGpio); 87 } 88 89 } // namespace phosphor::power::psu 90