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 | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 24 | FanEnclosure::ObjectMap FanEnclosure::getObjectMap() |
| 25 | { |
| 26 | ObjectMap invObj; |
| 27 | InterfaceMap invIntf; |
| 28 | PropertyMap invProp; |
| 29 | auto presPred = [](auto const& s) {return s->isPresent();}; |
| 30 | // Determine if all sensors show fan is not present |
| 31 | auto isPresent = std::any_of(sensors.begin(), |
| 32 | sensors.end(), |
| 33 | presPred); |
| 34 | invProp.emplace("Present", isPresent); |
| 35 | invProp.emplace("PrettyName", fanDesc); |
| 36 | invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp)); |
| 37 | Object fanInvPath = invPath; |
| 38 | invObj.emplace(std::move(fanInvPath), std::move(invIntf)); |
| 39 | |
| 40 | return invObj; |
| 41 | } |
| 42 | |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 43 | std::string FanEnclosure::getInvService() |
Matthew Barth | b803445 | 2017-02-17 16:39:46 -0600 | [diff] [blame] | 44 | { |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 45 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, |
| 46 | MAPPER_PATH, |
| 47 | MAPPER_INTERFACE, |
| 48 | "GetObject"); |
| 49 | |
| 50 | mapperCall.append(INVENTORY_PATH); |
| 51 | mapperCall.append(std::vector<std::string>({INVENTORY_INTF})); |
| 52 | |
| 53 | auto mapperResponseMsg = bus.call(mapperCall); |
| 54 | if (mapperResponseMsg.is_method_error()) |
| 55 | { |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame^] | 56 | throw std::runtime_error( |
| 57 | "Error in mapper call to get inventory service name"); |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 61 | mapperResponseMsg.read(mapperResponse); |
| 62 | |
| 63 | if (mapperResponse.empty()) |
| 64 | { |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame^] | 65 | throw std::runtime_error( |
| 66 | "Error in mapper response for inventory service name"); |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return mapperResponse.begin()->first; |
Matthew Barth | b803445 | 2017-02-17 16:39:46 -0600 | [diff] [blame] | 70 | } |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 71 | |
Matthew Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 72 | void FanEnclosure::updInventory() |
| 73 | { |
Matthew Barth | 1562ac7 | 2017-02-20 16:01:21 -0600 | [diff] [blame] | 74 | //Get inventory object for this fan |
| 75 | ObjectMap invObj = getObjectMap(); |
Matthew Barth | 736d143 | 2017-02-20 16:09:21 -0600 | [diff] [blame] | 76 | //Get inventory manager service name from mapper |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame^] | 77 | std::string invService; |
| 78 | try |
| 79 | { |
| 80 | invService = getInvService(); |
| 81 | } |
| 82 | catch (const std::runtime_error& err) |
| 83 | { |
| 84 | log<level::ERR>(err.what()); |
| 85 | return; |
| 86 | } |
Matthew Barth | 398257a | 2017-02-20 16:15:00 -0600 | [diff] [blame] | 87 | // Update inventory for this fan |
| 88 | auto invMsg = bus.new_method_call(invService.c_str(), |
| 89 | INVENTORY_PATH, |
| 90 | INVENTORY_INTF, |
| 91 | "Notify"); |
| 92 | invMsg.append(std::move(invObj)); |
| 93 | auto invMgrResponseMsg = bus.call(invMsg); |
| 94 | if (invMgrResponseMsg.is_method_error()) |
| 95 | { |
Matthew Barth | fefdc45 | 2017-02-22 11:15:56 -0600 | [diff] [blame^] | 96 | log<level::ERR>( |
| 97 | "Error in inventory manager call to update inventory"); |
| 98 | return; |
Matthew Barth | 398257a | 2017-02-20 16:15:00 -0600 | [diff] [blame] | 99 | } |
Matthew Barth | cd4f4d1 | 2017-02-17 17:48:56 -0600 | [diff] [blame] | 100 | } |
| 101 | |
Matthew Barth | d640382 | 2017-02-17 17:10:19 -0600 | [diff] [blame] | 102 | void FanEnclosure::addSensor( |
| 103 | std::unique_ptr<Sensor>&& sensor) |
| 104 | { |
| 105 | FanEnclosure::sensors.push_back(std::move(sensor)); |
| 106 | } |
| 107 | |
Matthew Barth | 293477d | 2017-02-17 15:39:36 -0600 | [diff] [blame] | 108 | } // namespace presence |
| 109 | } // namespace fan |
| 110 | } // namespace phosphor |