blob: aaf415da9e012db2bc429156f8cf58df6c02ed06 [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 Barth8db0f6f2017-02-23 10:16:57 -060024presenceState FanEnclosure::getCurPresState()
Matthew Barth1562ac72017-02-20 16:01:21 -060025{
Matthew Barth1562ac72017-02-20 16:01:21 -060026 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);
Matthew Barth8db0f6f2017-02-23 10:16:57 -060031
32 return (isPresent) ? PRESENT : NOT_PRESENT;
33}
34
35FanEnclosure::ObjectMap FanEnclosure::getObjectMap(const bool curPresState)
36{
37 ObjectMap invObj;
38 InterfaceMap invIntf;
39 PropertyMap invProp;
40
41 invProp.emplace("Present", curPresState);
Matthew Barth1562ac72017-02-20 16:01:21 -060042 invProp.emplace("PrettyName", fanDesc);
43 invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp));
44 Object fanInvPath = invPath;
45 invObj.emplace(std::move(fanInvPath), std::move(invIntf));
46
47 return invObj;
48}
49
Matthew Barth736d1432017-02-20 16:09:21 -060050std::string FanEnclosure::getInvService()
Matthew Barthb8034452017-02-17 16:39:46 -060051{
Matthew Barth736d1432017-02-20 16:09:21 -060052 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
53 MAPPER_PATH,
54 MAPPER_INTERFACE,
55 "GetObject");
56
57 mapperCall.append(INVENTORY_PATH);
58 mapperCall.append(std::vector<std::string>({INVENTORY_INTF}));
59
60 auto mapperResponseMsg = bus.call(mapperCall);
61 if (mapperResponseMsg.is_method_error())
62 {
Matthew Barthfefdc452017-02-22 11:15:56 -060063 throw std::runtime_error(
64 "Error in mapper call to get inventory service name");
Matthew Barth736d1432017-02-20 16:09:21 -060065 }
66
67 std::map<std::string, std::vector<std::string>> mapperResponse;
68 mapperResponseMsg.read(mapperResponse);
69
70 if (mapperResponse.empty())
71 {
Matthew Barthfefdc452017-02-22 11:15:56 -060072 throw std::runtime_error(
73 "Error in mapper response for inventory service name");
Matthew Barth736d1432017-02-20 16:09:21 -060074 }
75
76 return mapperResponse.begin()->first;
Matthew Barthb8034452017-02-17 16:39:46 -060077}
Matthew Barth293477d2017-02-17 15:39:36 -060078
Matthew Barthcd4f4d12017-02-17 17:48:56 -060079void FanEnclosure::updInventory()
80{
Matthew Barth8db0f6f2017-02-23 10:16:57 -060081 auto curPresState = getCurPresState();
82 // Only update inventory when presence state changed
83 if (presState != curPresState)
Matthew Barthfefdc452017-02-22 11:15:56 -060084 {
Matthew Barth8db0f6f2017-02-23 10:16:57 -060085 // Get inventory object for this fan
86 ObjectMap invObj = getObjectMap(curPresState);
87 // Get inventory manager service name from mapper
88 std::string invService;
89 try
90 {
91 invService = getInvService();
92 }
93 catch (const std::runtime_error& err)
94 {
95 log<level::ERR>(err.what());
96 return;
97 }
98 // Update inventory for this fan
99 auto invMsg = bus.new_method_call(invService.c_str(),
100 INVENTORY_PATH,
101 INVENTORY_INTF,
102 "Notify");
103 invMsg.append(std::move(invObj));
104 auto invMgrResponseMsg = bus.call(invMsg);
105 if (invMgrResponseMsg.is_method_error())
106 {
107 log<level::ERR>(
108 "Error in inventory manager call to update inventory");
109 return;
110 }
111 // Inventory updated, set presence state to current
112 presState = curPresState;
Matthew Barth398257a2017-02-20 16:15:00 -0600113 }
Matthew Barthcd4f4d12017-02-17 17:48:56 -0600114}
115
Matthew Barthd6403822017-02-17 17:10:19 -0600116void FanEnclosure::addSensor(
117 std::unique_ptr<Sensor>&& sensor)
118{
119 FanEnclosure::sensors.push_back(std::move(sensor));
120}
121
Matthew Barth293477d2017-02-17 15:39:36 -0600122} // namespace presence
123} // namespace fan
124} // namespace phosphor