blob: cb0c423f579174cad0dc3fb1af541611473e6193 [file] [log] [blame]
Matthew Barth5c15b792017-03-01 11:17:00 -06001/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matthew Barthcd4f4d12017-02-17 17:48:56 -060016#include <algorithm>
Matthew Barthfefdc452017-02-22 11:15:56 -060017#include <phosphor-logging/log.hpp>
Matthew Barth293477d2017-02-17 15:39:36 -060018#include "fan_enclosure.hpp"
19
20
21namespace phosphor
22{
23namespace fan
24{
25namespace presence
26{
27
Matthew Barthfefdc452017-02-22 11:15:56 -060028using namespace phosphor::logging;
29
Matthew Barth736d1432017-02-20 16:09:21 -060030//TODO Should get these from phosphor-objmgr config.h
Patrick Williams6d9b18d2017-03-20 11:48:33 -050031constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
Leonel Gonzalez167bee72017-03-16 13:47:41 -050032constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
Patrick Williams6d9b18d2017-03-20 11:48:33 -050033constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
Matthew Barth736d1432017-02-20 16:09:21 -060034
35//TODO Should get these from phosphor-inventory-manager config.h
36constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
37constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
38
Matthew Barth8db0f6f2017-02-23 10:16:57 -060039presenceState FanEnclosure::getCurPresState()
Matthew Barth1562ac72017-02-20 16:01:21 -060040{
Matthew Barth1562ac72017-02-20 16:01:21 -060041 auto presPred = [](auto const& s) {return s->isPresent();};
42 // Determine if all sensors show fan is not present
43 auto isPresent = std::any_of(sensors.begin(),
44 sensors.end(),
45 presPred);
Matthew Barth8db0f6f2017-02-23 10:16:57 -060046
47 return (isPresent) ? PRESENT : NOT_PRESENT;
48}
49
50FanEnclosure::ObjectMap FanEnclosure::getObjectMap(const bool curPresState)
51{
52 ObjectMap invObj;
53 InterfaceMap invIntf;
54 PropertyMap invProp;
55
56 invProp.emplace("Present", curPresState);
Matthew Barth1562ac72017-02-20 16:01:21 -060057 invProp.emplace("PrettyName", fanDesc);
58 invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp));
59 Object fanInvPath = invPath;
60 invObj.emplace(std::move(fanInvPath), std::move(invIntf));
61
62 return invObj;
63}
64
Matthew Barth736d1432017-02-20 16:09:21 -060065std::string FanEnclosure::getInvService()
Matthew Barthb8034452017-02-17 16:39:46 -060066{
Matthew Barth736d1432017-02-20 16:09:21 -060067 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
68 MAPPER_PATH,
69 MAPPER_INTERFACE,
70 "GetObject");
71
72 mapperCall.append(INVENTORY_PATH);
73 mapperCall.append(std::vector<std::string>({INVENTORY_INTF}));
74
75 auto mapperResponseMsg = bus.call(mapperCall);
76 if (mapperResponseMsg.is_method_error())
77 {
Matthew Barthfefdc452017-02-22 11:15:56 -060078 throw std::runtime_error(
79 "Error in mapper call to get inventory service name");
Matthew Barth736d1432017-02-20 16:09:21 -060080 }
81
82 std::map<std::string, std::vector<std::string>> mapperResponse;
83 mapperResponseMsg.read(mapperResponse);
84
85 if (mapperResponse.empty())
86 {
Matthew Barthfefdc452017-02-22 11:15:56 -060087 throw std::runtime_error(
88 "Error in mapper response for inventory service name");
Matthew Barth736d1432017-02-20 16:09:21 -060089 }
90
91 return mapperResponse.begin()->first;
Matthew Barthb8034452017-02-17 16:39:46 -060092}
Matthew Barth293477d2017-02-17 15:39:36 -060093
Matthew Barthcd4f4d12017-02-17 17:48:56 -060094void FanEnclosure::updInventory()
95{
Matthew Barth8db0f6f2017-02-23 10:16:57 -060096 auto curPresState = getCurPresState();
97 // Only update inventory when presence state changed
98 if (presState != curPresState)
Matthew Barthfefdc452017-02-22 11:15:56 -060099 {
Matthew Barth8db0f6f2017-02-23 10:16:57 -0600100 // Get inventory object for this fan
101 ObjectMap invObj = getObjectMap(curPresState);
102 // Get inventory manager service name from mapper
103 std::string invService;
104 try
105 {
106 invService = getInvService();
107 }
108 catch (const std::runtime_error& err)
109 {
110 log<level::ERR>(err.what());
111 return;
112 }
113 // Update inventory for this fan
114 auto invMsg = bus.new_method_call(invService.c_str(),
115 INVENTORY_PATH,
116 INVENTORY_INTF,
117 "Notify");
118 invMsg.append(std::move(invObj));
119 auto invMgrResponseMsg = bus.call(invMsg);
120 if (invMgrResponseMsg.is_method_error())
121 {
122 log<level::ERR>(
123 "Error in inventory manager call to update inventory");
124 return;
125 }
126 // Inventory updated, set presence state to current
127 presState = curPresState;
Matthew Barth398257a2017-02-20 16:15:00 -0600128 }
Matthew Barthcd4f4d12017-02-17 17:48:56 -0600129}
130
Matthew Barthd6403822017-02-17 17:10:19 -0600131void FanEnclosure::addSensor(
132 std::unique_ptr<Sensor>&& sensor)
133{
134 FanEnclosure::sensors.push_back(std::move(sensor));
135}
136
Matthew Barth293477d2017-02-17 15:39:36 -0600137} // namespace presence
138} // namespace fan
139} // namespace phosphor