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" |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 19 | #include "utility.hpp" |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 20 | |
| 21 | namespace phosphor |
| 22 | { |
| 23 | namespace fan |
| 24 | { |
| 25 | namespace presence |
| 26 | { |
| 27 | |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 28 | using namespace phosphor::logging; |
| 29 | |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 30 | //TODO Should get these from phosphor-inventory-manager config.h |
| 31 | constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"; |
| 32 | constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"; |
| 33 | |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 34 | presenceState FanEnclosure::getCurPresState() |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 35 | { |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 36 | 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 Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 41 | |
| 42 | return (isPresent) ? PRESENT : NOT_PRESENT; |
| 43 | } |
| 44 | |
| 45 | FanEnclosure::ObjectMap FanEnclosure::getObjectMap(const bool curPresState) |
| 46 | { |
| 47 | ObjectMap invObj; |
| 48 | InterfaceMap invIntf; |
| 49 | PropertyMap invProp; |
| 50 | |
| 51 | invProp.emplace("Present", curPresState); |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 52 | 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 Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 60 | void FanEnclosure::updInventory() |
| 61 | { |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 62 | auto curPresState = getCurPresState(); |
| 63 | // Only update inventory when presence state changed |
| 64 | if (presState != curPresState) |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 65 | { |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 66 | // 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 Spinler | 5cfdf94 | 2017-04-10 14:25:47 -0500 | [diff] [blame] | 72 | invService = phosphor::fan::util::getInvService(bus); |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 73 | } |
| 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 Barth | 398257a | 2017-02-20 16:15:00 -0600 | [diff] [blame] | 94 | } |
Matthew Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 95 | } |
| 96 | |
Matthew Barth | d640382 | 2017-02-17 17:10:19 -0600 | [diff] [blame] | 97 | void FanEnclosure::addSensor( |
| 98 | std::unique_ptr<Sensor>&& sensor) |
| 99 | { |
| 100 | FanEnclosure::sensors.push_back(std::move(sensor)); |
| 101 | } |
| 102 | |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 103 | } // namespace presence |
| 104 | } // namespace fan |
| 105 | } // namespace phosphor |