blob: ca97c073c558eb1a31fb6e28a658ad7d0c361acd [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"
Brandon Wyman5914f652017-03-16 18:17:07 -050019#include "utility.hpp"
Matthew Barth293477d2017-02-17 15:39:36 -060020
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-inventory-manager config.h
31constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
32constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
33
Matthew Barth8db0f6f2017-02-23 10:16:57 -060034presenceState FanEnclosure::getCurPresState()
Matthew Barth1562ac72017-02-20 16:01:21 -060035{
Matthew Barth1562ac72017-02-20 16:01:21 -060036 auto presPred = [](auto const& s) {return s->isPresent();};
37 // Determine if all sensors show fan is not present
38 auto isPresent = std::any_of(sensors.begin(),
39 sensors.end(),
40 presPred);
Matthew Barth8db0f6f2017-02-23 10:16:57 -060041
42 return (isPresent) ? PRESENT : NOT_PRESENT;
43}
44
45FanEnclosure::ObjectMap FanEnclosure::getObjectMap(const bool curPresState)
46{
47 ObjectMap invObj;
48 InterfaceMap invIntf;
49 PropertyMap invProp;
50
51 invProp.emplace("Present", curPresState);
Matthew Barth1562ac72017-02-20 16:01:21 -060052 invProp.emplace("PrettyName", fanDesc);
53 invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp));
54 Object fanInvPath = invPath;
55 invObj.emplace(std::move(fanInvPath), std::move(invIntf));
56
57 return invObj;
58}
59
Matthew Barthcd4f4d12017-02-17 17:48:56 -060060void FanEnclosure::updInventory()
61{
Matthew Barth8db0f6f2017-02-23 10:16:57 -060062 auto curPresState = getCurPresState();
63 // Only update inventory when presence state changed
64 if (presState != curPresState)
Matthew Barthfefdc452017-02-22 11:15:56 -060065 {
Matthew Barth8db0f6f2017-02-23 10:16:57 -060066 // Get inventory object for this fan
67 ObjectMap invObj = getObjectMap(curPresState);
68 // Get inventory manager service name from mapper
69 std::string invService;
70 try
71 {
Matt Spinler5cfdf942017-04-10 14:25:47 -050072 invService = phosphor::fan::util::getInvService(bus);
Matthew Barth8db0f6f2017-02-23 10:16:57 -060073 }
74 catch (const std::runtime_error& err)
75 {
76 log<level::ERR>(err.what());
77 return;
78 }
79 // Update inventory for this fan
80 auto invMsg = bus.new_method_call(invService.c_str(),
81 INVENTORY_PATH,
82 INVENTORY_INTF,
83 "Notify");
84 invMsg.append(std::move(invObj));
85 auto invMgrResponseMsg = bus.call(invMsg);
86 if (invMgrResponseMsg.is_method_error())
87 {
88 log<level::ERR>(
89 "Error in inventory manager call to update inventory");
90 return;
91 }
92 // Inventory updated, set presence state to current
93 presState = curPresState;
Matthew Barth398257a2017-02-20 16:15:00 -060094 }
Matthew Barthcd4f4d12017-02-17 17:48:56 -060095}
96
Matthew Barthd6403822017-02-17 17:10:19 -060097void FanEnclosure::addSensor(
98 std::unique_ptr<Sensor>&& sensor)
99{
100 FanEnclosure::sensors.push_back(std::move(sensor));
101}
102
Matthew Barth293477d2017-02-17 15:39:36 -0600103} // namespace presence
104} // namespace fan
105} // namespace phosphor