blob: b2ea9f7b4eb716ea0168f2baa42fdd2898e4b182 [file] [log] [blame]
Matthew Barthcd4f4d12017-02-17 17:48:56 -06001#include <algorithm>
Matthew Barth293477d2017-02-17 15:39:36 -06002#include "fan_enclosure.hpp"
3
4
5namespace phosphor
6{
7namespace fan
8{
9namespace presence
10{
11
Matthew Barth736d1432017-02-20 16:09:21 -060012//TODO Should get these from phosphor-objmgr config.h
13constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
14constexpr auto MAPPER_PATH = "/xyz/openbmc_project/ObjectMapper";
15constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
16
17//TODO Should get these from phosphor-inventory-manager config.h
18constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
19constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
20
Matthew Barth1562ac72017-02-20 16:01:21 -060021FanEnclosure::ObjectMap FanEnclosure::getObjectMap()
22{
23 ObjectMap invObj;
24 InterfaceMap invIntf;
25 PropertyMap invProp;
26 auto presPred = [](auto const& s) {return s->isPresent();};
27 // Determine if all sensors show fan is not present
28 auto isPresent = std::any_of(sensors.begin(),
29 sensors.end(),
30 presPred);
31 invProp.emplace("Present", isPresent);
32 invProp.emplace("PrettyName", fanDesc);
33 invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp));
34 Object fanInvPath = invPath;
35 invObj.emplace(std::move(fanInvPath), std::move(invIntf));
36
37 return invObj;
38}
39
Matthew Barth736d1432017-02-20 16:09:21 -060040std::string FanEnclosure::getInvService()
Matthew Barthb8034452017-02-17 16:39:46 -060041{
Matthew Barth736d1432017-02-20 16:09:21 -060042 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
43 MAPPER_PATH,
44 MAPPER_INTERFACE,
45 "GetObject");
46
47 mapperCall.append(INVENTORY_PATH);
48 mapperCall.append(std::vector<std::string>({INVENTORY_INTF}));
49
50 auto mapperResponseMsg = bus.call(mapperCall);
51 if (mapperResponseMsg.is_method_error())
52 {
53 //TODO Retry or throw exception/log error?
54 }
55
56 std::map<std::string, std::vector<std::string>> mapperResponse;
57 mapperResponseMsg.read(mapperResponse);
58
59 if (mapperResponse.empty())
60 {
61 //TODO Nothing found, throw exception/log error?
62 }
63
64 return mapperResponse.begin()->first;
Matthew Barthb8034452017-02-17 16:39:46 -060065}
Matthew Barth293477d2017-02-17 15:39:36 -060066
Matthew Barthcd4f4d12017-02-17 17:48:56 -060067void FanEnclosure::updInventory()
68{
Matthew Barth1562ac72017-02-20 16:01:21 -060069 //Get inventory object for this fan
70 ObjectMap invObj = getObjectMap();
Matthew Barth736d1432017-02-20 16:09:21 -060071 //Get inventory manager service name from mapper
72 std::string invService = getInvService();
Matthew Barth398257a2017-02-20 16:15:00 -060073 // Update inventory for this fan
74 auto invMsg = bus.new_method_call(invService.c_str(),
75 INVENTORY_PATH,
76 INVENTORY_INTF,
77 "Notify");
78 invMsg.append(std::move(invObj));
79 auto invMgrResponseMsg = bus.call(invMsg);
80 if (invMgrResponseMsg.is_method_error())
81 {
82 //TODO Handle error in notify call
83 }
Matthew Barthcd4f4d12017-02-17 17:48:56 -060084}
85
Matthew Barthd6403822017-02-17 17:10:19 -060086void FanEnclosure::addSensor(
87 std::unique_ptr<Sensor>&& sensor)
88{
89 FanEnclosure::sensors.push_back(std::move(sensor));
90}
91
Matthew Barth293477d2017-02-17 15:39:36 -060092} // namespace presence
93} // namespace fan
94} // namespace phosphor