Matthew Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 1 | #include <algorithm> |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 2 | #include <phosphor-logging/log.hpp> |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 3 | #include "fan_enclosure.hpp" |
| 4 | |
| 5 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace fan |
| 9 | { |
| 10 | namespace presence |
| 11 | { |
| 12 | |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 13 | using namespace phosphor::logging; |
| 14 | |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 15 | //TODO Should get these from phosphor-objmgr config.h |
| 16 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 17 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/ObjectMapper"; |
| 18 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 19 | |
| 20 | //TODO Should get these from phosphor-inventory-manager config.h |
| 21 | constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"; |
| 22 | constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"; |
| 23 | |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame^] | 24 | presenceState FanEnclosure::getCurPresState() |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 25 | { |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 26 | auto presPred = [](auto const& s) {return s->isPresent();}; |
| 27 | // Determine if all sensors show fan is not present |
| 28 | auto isPresent = std::any_of(sensors.begin(), |
| 29 | sensors.end(), |
| 30 | presPred); |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame^] | 31 | |
| 32 | return (isPresent) ? PRESENT : NOT_PRESENT; |
| 33 | } |
| 34 | |
| 35 | FanEnclosure::ObjectMap FanEnclosure::getObjectMap(const bool curPresState) |
| 36 | { |
| 37 | ObjectMap invObj; |
| 38 | InterfaceMap invIntf; |
| 39 | PropertyMap invProp; |
| 40 | |
| 41 | invProp.emplace("Present", curPresState); |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 42 | invProp.emplace("PrettyName", fanDesc); |
| 43 | invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp)); |
| 44 | Object fanInvPath = invPath; |
| 45 | invObj.emplace(std::move(fanInvPath), std::move(invIntf)); |
| 46 | |
| 47 | return invObj; |
| 48 | } |
| 49 | |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 50 | std::string FanEnclosure::getInvService() |
Matthew Barth | b803445 | 2017-02-17 16:39:46 -0600 | [diff] [blame] | 51 | { |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 52 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, |
| 53 | MAPPER_PATH, |
| 54 | MAPPER_INTERFACE, |
| 55 | "GetObject"); |
| 56 | |
| 57 | mapperCall.append(INVENTORY_PATH); |
| 58 | mapperCall.append(std::vector<std::string>({INVENTORY_INTF})); |
| 59 | |
| 60 | auto mapperResponseMsg = bus.call(mapperCall); |
| 61 | if (mapperResponseMsg.is_method_error()) |
| 62 | { |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 63 | throw std::runtime_error( |
| 64 | "Error in mapper call to get inventory service name"); |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 68 | mapperResponseMsg.read(mapperResponse); |
| 69 | |
| 70 | if (mapperResponse.empty()) |
| 71 | { |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 72 | throw std::runtime_error( |
| 73 | "Error in mapper response for inventory service name"); |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return mapperResponse.begin()->first; |
Matthew Barth | b803445 | 2017-02-17 16:39:46 -0600 | [diff] [blame] | 77 | } |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 78 | |
Matthew Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 79 | void FanEnclosure::updInventory() |
| 80 | { |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame^] | 81 | auto curPresState = getCurPresState(); |
| 82 | // Only update inventory when presence state changed |
| 83 | if (presState != curPresState) |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame] | 84 | { |
Matthew Barth | 8db0f6f | 2017-02-23 10:16:57 -0600 | [diff] [blame^] | 85 | // Get inventory object for this fan |
| 86 | ObjectMap invObj = getObjectMap(curPresState); |
| 87 | // Get inventory manager service name from mapper |
| 88 | std::string invService; |
| 89 | try |
| 90 | { |
| 91 | invService = getInvService(); |
| 92 | } |
| 93 | catch (const std::runtime_error& err) |
| 94 | { |
| 95 | log<level::ERR>(err.what()); |
| 96 | return; |
| 97 | } |
| 98 | // Update inventory for this fan |
| 99 | auto invMsg = bus.new_method_call(invService.c_str(), |
| 100 | INVENTORY_PATH, |
| 101 | INVENTORY_INTF, |
| 102 | "Notify"); |
| 103 | invMsg.append(std::move(invObj)); |
| 104 | auto invMgrResponseMsg = bus.call(invMsg); |
| 105 | if (invMgrResponseMsg.is_method_error()) |
| 106 | { |
| 107 | log<level::ERR>( |
| 108 | "Error in inventory manager call to update inventory"); |
| 109 | return; |
| 110 | } |
| 111 | // Inventory updated, set presence state to current |
| 112 | presState = curPresState; |
Matthew Barth | 398257a | 2017-02-20 16:15:00 -0600 | [diff] [blame] | 113 | } |
Matthew Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 114 | } |
| 115 | |
Matthew Barth | d640382 | 2017-02-17 17:10:19 -0600 | [diff] [blame] | 116 | void FanEnclosure::addSensor( |
| 117 | std::unique_ptr<Sensor>&& sensor) |
| 118 | { |
| 119 | FanEnclosure::sensors.push_back(std::move(sensor)); |
| 120 | } |
| 121 | |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 122 | } // namespace presence |
| 123 | } // namespace fan |
| 124 | } // namespace phosphor |