blob: 8e0e7ed1bd0bdc781b3b1b8883293ab20f5d4a37 [file] [log] [blame]
Carol Wangdc059392020-03-13 17:39:17 +08001#include "utils.hpp"
2
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -06003#include <gpiod.h>
4
Andrew Geissler8ffdb262021-09-20 15:25:19 -05005#include <phosphor-logging/lg2.hpp>
Carol Wangdc059392020-03-13 17:39:17 +08006
7namespace phosphor
8{
9namespace state
10{
11namespace manager
12{
13namespace utils
14{
15
Andrew Geissler8ffdb262021-09-20 15:25:19 -050016PHOSPHOR_LOG2_USING;
Carol Wangdc059392020-03-13 17:39:17 +080017
18constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
19constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
20constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
21constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
22
23std::string getService(sdbusplus::bus::bus& bus, std::string path,
24 std::string interface)
25{
26 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
27 MAPPER_INTERFACE, "GetObject");
28
29 mapper.append(path, std::vector<std::string>({interface}));
30
31 std::vector<std::pair<std::string, std::vector<std::string>>>
32 mapperResponse;
33
34 try
35 {
36 auto mapperResponseMsg = bus.call(mapper);
37
38 mapperResponseMsg.read(mapperResponse);
39 if (mapperResponse.empty())
40 {
Andrew Geisslerad65b2d2021-09-21 12:53:29 -050041 error(
42 "Error no matching service with path {PATH} and interface {INTERFACE}",
43 "PATH", path, "INTERFACE", interface);
Carol Wangdc059392020-03-13 17:39:17 +080044 throw std::runtime_error("Error no matching service");
45 }
46 }
Patrick Williams0a675212021-09-02 09:49:43 -050047 catch (const sdbusplus::exception::exception& e)
Carol Wangdc059392020-03-13 17:39:17 +080048 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050049 error("Error in mapper call with path {PATH}, interface "
50 "{INTERFACE}, and exception {ERROR}",
51 "PATH", path, "INTERFACE", interface, "ERROR", e);
Carol Wangdc059392020-03-13 17:39:17 +080052 throw;
53 }
54
55 return mapperResponse.begin()->first;
56}
57
58void setProperty(sdbusplus::bus::bus& bus, const std::string& path,
59 const std::string& interface, const std::string& property,
60 const std::string& value)
61{
Patrick Williams2975e262020-05-13 18:01:09 -050062 std::variant<std::string> variantValue = value;
Carol Wangdc059392020-03-13 17:39:17 +080063 std::string service = getService(bus, path, interface);
64
65 auto method = bus.new_method_call(service.c_str(), path.c_str(),
66 PROPERTY_INTERFACE, "Set");
67
68 method.append(interface, property, variantValue);
69 bus.call_noreply(method);
70
71 return;
72}
73
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -060074int getGpioValue(const std::string& gpioName)
75{
76
77 int gpioval = -1;
78 gpiod_line* line = gpiod_line_find(gpioName.c_str());
79
80 if (nullptr != line)
81 {
82 // take ownership of gpio
83 if (0 != gpiod_line_request_input(line, "state-manager"))
84 {
85 error("Failed request for {GPIO_NAME} GPIO", "GPIO_NAME", gpioName);
86 }
87 else
88 {
89 // get gpio value
90 gpioval = gpiod_line_get_value(line);
91
92 // release ownership of gpio
93 gpiod_line_close_chip(line);
94 }
95 }
96 return gpioval;
97}
98
Carol Wangdc059392020-03-13 17:39:17 +080099} // namespace utils
100} // namespace manager
101} // namespace state
102} // namespace phosphor