blob: 0d19e60ec6b0725668496bbc767809a1535b7cd4 [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"
Brad Bishopec613db2017-06-08 12:12:53 -040019#include "sdbusplus.hpp"
Brandon Wyman5914f652017-03-16 18:17:07 -050020#include "utility.hpp"
Matthew Barth293477d2017-02-17 15:39:36 -060021
22namespace phosphor
23{
24namespace fan
25{
26namespace presence
27{
28
Matthew Barthfefdc452017-02-22 11:15:56 -060029using namespace phosphor::logging;
Brad Bishopec613db2017-06-08 12:12:53 -040030using namespace std::literals::string_literals;
Matthew Barthfefdc452017-02-22 11:15:56 -060031
Matthew Barth736d1432017-02-20 16:09:21 -060032//TODO Should get these from phosphor-inventory-manager config.h
Brad Bishopec613db2017-06-08 12:12:53 -040033const auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"s;
34const auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"s;
Matthew Barth736d1432017-02-20 16:09:21 -060035
Matthew Barth8db0f6f2017-02-23 10:16:57 -060036presenceState FanEnclosure::getCurPresState()
Matthew Barth1562ac72017-02-20 16:01:21 -060037{
Matthew Barth1562ac72017-02-20 16:01:21 -060038 auto presPred = [](auto const& s) {return s->isPresent();};
39 // Determine if all sensors show fan is not present
40 auto isPresent = std::any_of(sensors.begin(),
41 sensors.end(),
42 presPred);
Matthew Barth8db0f6f2017-02-23 10:16:57 -060043
44 return (isPresent) ? PRESENT : NOT_PRESENT;
45}
46
47FanEnclosure::ObjectMap FanEnclosure::getObjectMap(const bool curPresState)
48{
49 ObjectMap invObj;
50 InterfaceMap invIntf;
51 PropertyMap invProp;
52
53 invProp.emplace("Present", curPresState);
Matthew Barth1562ac72017-02-20 16:01:21 -060054 invProp.emplace("PrettyName", fanDesc);
55 invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp));
56 Object fanInvPath = invPath;
57 invObj.emplace(std::move(fanInvPath), std::move(invIntf));
58
59 return invObj;
60}
61
Matthew Barthcd4f4d12017-02-17 17:48:56 -060062void FanEnclosure::updInventory()
63{
Matthew Barth8db0f6f2017-02-23 10:16:57 -060064 auto curPresState = getCurPresState();
65 // Only update inventory when presence state changed
66 if (presState != curPresState)
Matthew Barthfefdc452017-02-22 11:15:56 -060067 {
Matthew Barth8db0f6f2017-02-23 10:16:57 -060068 // Update inventory for this fan
Brad Bishopec613db2017-06-08 12:12:53 -040069 util::SDBusPlus::lookupAndCallMethod(
70 INVENTORY_PATH,
71 INVENTORY_INTF,
72 "Notify"s,
73 getObjectMap(curPresState));
Matthew Barth8db0f6f2017-02-23 10:16:57 -060074 // Inventory updated, set presence state to current
75 presState = curPresState;
Matthew Barth398257a2017-02-20 16:15:00 -060076 }
Matthew Barthcd4f4d12017-02-17 17:48:56 -060077}
78
Matthew Barthd6403822017-02-17 17:10:19 -060079void FanEnclosure::addSensor(
80 std::unique_ptr<Sensor>&& sensor)
81{
82 FanEnclosure::sensors.push_back(std::move(sensor));
83}
84
Matthew Barth293477d2017-02-17 15:39:36 -060085} // namespace presence
86} // namespace fan
87} // namespace phosphor