Matthew Barth | 5c15b79 | 2017-03-01 11:17:00 -0600 | [diff] [blame] | 1 | /** |
| 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 Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 16 | #include <algorithm> |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 17 | #include <phosphor-logging/log.hpp> |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 18 | #include "fan_enclosure.hpp" |
Brad Bishop | ec613db | 2017-06-08 12:12:53 -0400 | [diff] [blame] | 19 | #include "sdbusplus.hpp" |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 20 | #include "utility.hpp" |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace fan |
| 25 | { |
| 26 | namespace presence |
| 27 | { |
| 28 | |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 29 | using namespace phosphor::logging; |
Brad Bishop | ec613db | 2017-06-08 12:12:53 -0400 | [diff] [blame] | 30 | using namespace std::literals::string_literals; |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 31 | |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 32 | //TODO Should get these from phosphor-inventory-manager config.h |
Brad Bishop | ec613db | 2017-06-08 12:12:53 -0400 | [diff] [blame] | 33 | const auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"s; |
| 34 | const auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"s; |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 35 | |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 36 | presenceState FanEnclosure::getCurPresState() |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 37 | { |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 38 | 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 Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 43 | |
| 44 | return (isPresent) ? PRESENT : NOT_PRESENT; |
| 45 | } |
| 46 | |
| 47 | FanEnclosure::ObjectMap FanEnclosure::getObjectMap(const bool curPresState) |
| 48 | { |
| 49 | ObjectMap invObj; |
| 50 | InterfaceMap invIntf; |
| 51 | PropertyMap invProp; |
| 52 | |
| 53 | invProp.emplace("Present", curPresState); |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 54 | 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 Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 62 | void FanEnclosure::updInventory() |
| 63 | { |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 64 | auto curPresState = getCurPresState(); |
| 65 | // Only update inventory when presence state changed |
| 66 | if (presState != curPresState) |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 67 | { |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 68 | // Update inventory for this fan |
Brad Bishop | ec613db | 2017-06-08 12:12:53 -0400 | [diff] [blame] | 69 | util::SDBusPlus::lookupAndCallMethod( |
| 70 | INVENTORY_PATH, |
| 71 | INVENTORY_INTF, |
| 72 | "Notify"s, |
| 73 | getObjectMap(curPresState)); |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 74 | // Inventory updated, set presence state to current |
| 75 | presState = curPresState; |
Matthew Barth | 398257a | 2017-02-20 16:15:00 -0600 | [diff] [blame] | 76 | } |
Matthew Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 77 | } |
| 78 | |
Matthew Barth | d640382 | 2017-02-17 17:10:19 -0600 | [diff] [blame] | 79 | void FanEnclosure::addSensor( |
| 80 | std::unique_ptr<Sensor>&& sensor) |
| 81 | { |
| 82 | FanEnclosure::sensors.push_back(std::move(sensor)); |
| 83 | } |
| 84 | |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 85 | } // namespace presence |
| 86 | } // namespace fan |
| 87 | } // namespace phosphor |