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" |
| 19 | |
| 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-objmgr config.h |
Patrick Williams | 6d9b18d | 2017-03-20 11:48:33 -0500 | [diff] [blame^] | 31 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
Leonel Gonzalez | 167bee7 | 2017-03-16 13:47:41 -0500 | [diff] [blame] | 32 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
Patrick Williams | 6d9b18d | 2017-03-20 11:48:33 -0500 | [diff] [blame^] | 33 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 34 | |
| 35 | //TODO Should get these from phosphor-inventory-manager config.h |
| 36 | constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"; |
| 37 | constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"; |
| 38 | |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 39 | presenceState FanEnclosure::getCurPresState() |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 40 | { |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 41 | 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 Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 46 | |
| 47 | return (isPresent) ? PRESENT : NOT_PRESENT; |
| 48 | } |
| 49 | |
| 50 | FanEnclosure::ObjectMap FanEnclosure::getObjectMap(const bool curPresState) |
| 51 | { |
| 52 | ObjectMap invObj; |
| 53 | InterfaceMap invIntf; |
| 54 | PropertyMap invProp; |
| 55 | |
| 56 | invProp.emplace("Present", curPresState); |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 57 | 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 Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 65 | std::string FanEnclosure::getInvService() |
Matthew Barth | b803445 | 2017-02-17 16:39:46 -0600 | [diff] [blame] | 66 | { |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 67 | 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 Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 78 | throw std::runtime_error( |
| 79 | "Error in mapper call to get inventory service name"); |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 83 | mapperResponseMsg.read(mapperResponse); |
| 84 | |
| 85 | if (mapperResponse.empty()) |
| 86 | { |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 87 | throw std::runtime_error( |
| 88 | "Error in mapper response for inventory service name"); |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return mapperResponse.begin()->first; |
Matthew Barth | b803445 | 2017-02-17 16:39:46 -0600 | [diff] [blame] | 92 | } |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 93 | |
Matthew Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 94 | void FanEnclosure::updInventory() |
| 95 | { |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 96 | auto curPresState = getCurPresState(); |
| 97 | // Only update inventory when presence state changed |
| 98 | if (presState != curPresState) |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 99 | { |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame] | 100 | // 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 Barth | 398257a | 2017-02-20 16:15:00 -0600 | [diff] [blame] | 128 | } |
Matthew Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 129 | } |
| 130 | |
Matthew Barth | d640382 | 2017-02-17 17:10:19 -0600 | [diff] [blame] | 131 | void FanEnclosure::addSensor( |
| 132 | std::unique_ptr<Sensor>&& sensor) |
| 133 | { |
| 134 | FanEnclosure::sensors.push_back(std::move(sensor)); |
| 135 | } |
| 136 | |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 137 | } // namespace presence |
| 138 | } // namespace fan |
| 139 | } // namespace phosphor |