blob: f0ffffb7e1930029e1290dc7e4e08926f02f42ef [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001#include <iostream>
2
3#include "dbus/util.hpp"
4
5using Property = std::string;
6using Value = sdbusplus::message::variant<int64_t, std::string>;
7using PropertyMap = std::map<Property, Value>;
8
9/* TODO(venture): Basically all phosphor apps need this, maybe it should be a
10 * part of sdbusplus. There is an old version in libmapper.
11 */
Patrick Venture0df7c0f2018-06-13 09:02:13 -070012std::string DbusHelper::GetService(sdbusplus::bus::bus& bus,
13 const std::string& intf,
14 const std::string& path)
Patrick Venture863b9242018-03-08 08:29:23 -080015{
16 auto mapper = bus.new_method_call(
17 "xyz.openbmc_project.ObjectMapper",
18 "/xyz/openbmc_project/object_mapper",
19 "xyz.openbmc_project.ObjectMapper",
20 "GetObject");
21
22 mapper.append(path);
23 mapper.append(std::vector<std::string>({intf}));
24
25 auto responseMsg = bus.call(mapper);
26 if (responseMsg.is_method_error())
27 {
28 throw std::runtime_error("ObjectMapper Call Failure");
29 }
30
31 std::map<std::string, std::vector<std::string>> response;
32 responseMsg.read(response);
33
34 if (response.begin() == response.end())
35 {
36 throw std::runtime_error("Unable to find Object: " + path);
37 }
38
39 return response.begin()->first;
40}
41
Patrick Venture0df7c0f2018-06-13 09:02:13 -070042void DbusHelper::GetProperties(sdbusplus::bus::bus& bus,
43 const std::string& service,
44 const std::string& path,
45 struct SensorProperties* prop)
Patrick Venture863b9242018-03-08 08:29:23 -080046{
Patrick Venture863b9242018-03-08 08:29:23 -080047 auto pimMsg = bus.new_method_call(service.c_str(),
48 path.c_str(),
49 propertiesintf.c_str(),
50 "GetAll");
51
52 pimMsg.append(sensorintf);
53 auto valueResponseMsg = bus.call(pimMsg);
54
55 if (valueResponseMsg.is_method_error())
56 {
57 std::cerr << "Error in value call\n";
58 throw std::runtime_error("ERROR in value call.");
59 }
60
61 // The PropertyMap returned will look like this because it's always
62 // reading a Sensor.Value interface.
63 // a{sv} 3:
64 // "Value" x 24875
65 // "Unit" s "xyz.openbmc_project.Sensor.Value.Unit.DegreesC"
66 // "Scale" x -3
67 PropertyMap propMap;
68 valueResponseMsg.read(propMap);
69
Patrick Venture0d73b102018-05-09 10:29:42 -070070 // If no error was set, the values should all be there.
Patrick Venture0df7c0f2018-06-13 09:02:13 -070071 prop->unit = sdbusplus::message::variant_ns::get<std::string>(
72 propMap["Unit"]);
73 prop->scale = sdbusplus::message::variant_ns::get<int64_t>(
74 propMap["Scale"]);
75 prop->value = sdbusplus::message::variant_ns::get<int64_t>(
76 propMap["Value"]);
Patrick Venture863b9242018-03-08 08:29:23 -080077
78 return;
79}
80
81std::string GetSensorPath(const std::string& type, const std::string& id)
82{
83 std::string layer = type;
84 if (type == "fan")
85 {
86 layer = "fan_tach";
87 }
88 else if (type == "temp")
89 {
90 layer = "temperature";
91 }
92 else
93 {
94 layer = "unknown"; // TODO(venture): Need to handle.
95 }
96
97 return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id);
98}
99
100std::string GetMatch(const std::string& type, const std::string& id)
101{
102 return std::string("type='signal',"
103 "interface='org.freedesktop.DBus.Properties',"
104 "member='PropertiesChanged',"
105 "path='" + GetSensorPath(type, id) + "'");
106}
107