blob: 998bd5013078b168d649add8662803a008d3e520 [file] [log] [blame]
Matthew Barthcd4f4d12017-02-17 17:48:56 -06001#include <algorithm>
Matthew Barthfefdc452017-02-22 11:15:56 -06002#include <phosphor-logging/log.hpp>
Matthew Barth293477d2017-02-17 15:39:36 -06003#include "fan_enclosure.hpp"
4
5
6namespace phosphor
7{
8namespace fan
9{
10namespace presence
11{
12
Matthew Barthfefdc452017-02-22 11:15:56 -060013using namespace phosphor::logging;
14
Matthew Barth736d1432017-02-20 16:09:21 -060015//TODO Should get these from phosphor-objmgr config.h
16constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
17constexpr auto MAPPER_PATH = "/xyz/openbmc_project/ObjectMapper";
18constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
19
20//TODO Should get these from phosphor-inventory-manager config.h
21constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
22constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
23
Matthew Barth1562ac72017-02-20 16:01:21 -060024FanEnclosure::ObjectMap FanEnclosure::getObjectMap()
25{
26 ObjectMap invObj;
27 InterfaceMap invIntf;
28 PropertyMap invProp;
29 auto presPred = [](auto const& s) {return s->isPresent();};
30 // Determine if all sensors show fan is not present
31 auto isPresent = std::any_of(sensors.begin(),
32 sensors.end(),
33 presPred);
34 invProp.emplace("Present", isPresent);
35 invProp.emplace("PrettyName", fanDesc);
36 invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp));
37 Object fanInvPath = invPath;
38 invObj.emplace(std::move(fanInvPath), std::move(invIntf));
39
40 return invObj;
41}
42
Matthew Barth736d1432017-02-20 16:09:21 -060043std::string FanEnclosure::getInvService()
Matthew Barthb8034452017-02-17 16:39:46 -060044{
Matthew Barth736d1432017-02-20 16:09:21 -060045 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
46 MAPPER_PATH,
47 MAPPER_INTERFACE,
48 "GetObject");
49
50 mapperCall.append(INVENTORY_PATH);
51 mapperCall.append(std::vector<std::string>({INVENTORY_INTF}));
52
53 auto mapperResponseMsg = bus.call(mapperCall);
54 if (mapperResponseMsg.is_method_error())
55 {
Matthew Barthfefdc452017-02-22 11:15:56 -060056 throw std::runtime_error(
57 "Error in mapper call to get inventory service name");
Matthew Barth736d1432017-02-20 16:09:21 -060058 }
59
60 std::map<std::string, std::vector<std::string>> mapperResponse;
61 mapperResponseMsg.read(mapperResponse);
62
63 if (mapperResponse.empty())
64 {
Matthew Barthfefdc452017-02-22 11:15:56 -060065 throw std::runtime_error(
66 "Error in mapper response for inventory service name");
Matthew Barth736d1432017-02-20 16:09:21 -060067 }
68
69 return mapperResponse.begin()->first;
Matthew Barthb8034452017-02-17 16:39:46 -060070}
Matthew Barth293477d2017-02-17 15:39:36 -060071
Matthew Barthcd4f4d12017-02-17 17:48:56 -060072void FanEnclosure::updInventory()
73{
Matthew Barth1562ac72017-02-20 16:01:21 -060074 //Get inventory object for this fan
75 ObjectMap invObj = getObjectMap();
Matthew Barth736d1432017-02-20 16:09:21 -060076 //Get inventory manager service name from mapper
Matthew Barthfefdc452017-02-22 11:15:56 -060077 std::string invService;
78 try
79 {
80 invService = getInvService();
81 }
82 catch (const std::runtime_error& err)
83 {
84 log<level::ERR>(err.what());
85 return;
86 }
Matthew Barth398257a2017-02-20 16:15:00 -060087 // Update inventory for this fan
88 auto invMsg = bus.new_method_call(invService.c_str(),
89 INVENTORY_PATH,
90 INVENTORY_INTF,
91 "Notify");
92 invMsg.append(std::move(invObj));
93 auto invMgrResponseMsg = bus.call(invMsg);
94 if (invMgrResponseMsg.is_method_error())
95 {
Matthew Barthfefdc452017-02-22 11:15:56 -060096 log<level::ERR>(
97 "Error in inventory manager call to update inventory");
98 return;
Matthew Barth398257a2017-02-20 16:15:00 -060099 }
Matthew Barthcd4f4d12017-02-17 17:48:56 -0600100}
101
Matthew Barthd6403822017-02-17 17:10:19 -0600102void FanEnclosure::addSensor(
103 std::unique_ptr<Sensor>&& sensor)
104{
105 FanEnclosure::sensors.push_back(std::move(sensor));
106}
107
Matthew Barth293477d2017-02-17 15:39:36 -0600108} // namespace presence
109} // namespace fan
110} // namespace phosphor