Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <cstring> |
| 4 | #include <vector> |
| 5 | #include <experimental/filesystem> |
| 6 | #include <functional> |
| 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include "occ_pass_through.hpp" |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 9 | #include "occ_status.hpp" |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 10 | #include "config.h" |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 11 | #include <powercap.hpp> |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 12 | |
| 13 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 14 | |
| 15 | namespace open_power |
| 16 | { |
| 17 | namespace occ |
| 18 | { |
| 19 | |
| 20 | /** @class Manager |
| 21 | * @brief Builds and manages OCC objects |
| 22 | */ |
| 23 | struct Manager |
| 24 | { |
| 25 | public: |
| 26 | Manager() = delete; |
| 27 | Manager(const Manager&) = delete; |
| 28 | Manager& operator=(const Manager&) = delete; |
| 29 | Manager(Manager&&) = default; |
| 30 | Manager& operator=(Manager&&) = default; |
| 31 | ~Manager() = default; |
| 32 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 33 | /** @brief Adds OCC pass-through and status objects on the bus |
| 34 | * when corresponding CPU inventory is created. |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 35 | * |
| 36 | * @param[in] bus - handle to the bus |
| 37 | * @param[in] event - Unique ptr reference to sd_event |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 38 | */ |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 39 | Manager(sdbusplus::bus::bus& bus, |
| 40 | EventPtr& event) : |
| 41 | bus(bus), |
| 42 | event(event) |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 43 | { |
| 44 | for (auto id = 0; id < MAX_CPUS; ++id) |
| 45 | { |
| 46 | auto path = std::string(CPU_PATH) + std::to_string(id); |
| 47 | cpuMatches.emplace_back( |
| 48 | bus, |
| 49 | sdbusRule::interfacesAdded() + |
| 50 | sdbusRule::argNpath(0, path), |
| 51 | std::bind(std::mem_fn(&Manager::cpuCreated), |
| 52 | this, std::placeholders::_1)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** @brief Callback that responds to cpu creation in the inventory - |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 57 | * by creating the needed objects. |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 58 | * |
| 59 | * @param[in] msg - bus message |
| 60 | * |
| 61 | * @returns 0 to indicate success |
| 62 | */ |
| 63 | int cpuCreated(sdbusplus::message::message& msg) |
| 64 | { |
| 65 | namespace fs = std::experimental::filesystem; |
| 66 | |
| 67 | sdbusplus::message::object_path o; |
| 68 | msg.read(o); |
| 69 | fs::path cpuPath(std::string(std::move(o))); |
| 70 | auto cpu = cpuPath.filename(); |
| 71 | |
| 72 | std::string name{cpu.c_str()}; |
| 73 | auto index = name.find(CPU_NAME); |
| 74 | name.replace(index, std::strlen(CPU_NAME), OCC_NAME); |
| 75 | |
| 76 | auto path = fs::path(OCC_CONTROL_ROOT) / name; |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 77 | passThroughObjects.emplace_back( |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 78 | std::make_unique<PassThrough>( |
| 79 | bus, |
| 80 | path.c_str())); |
| 81 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 82 | statusObjects.emplace_back( |
| 83 | std::make_unique<Status>( |
| 84 | bus, |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 85 | event, |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 86 | path.c_str())); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 87 | |
| 88 | // Create the power cap monitor object for master occ (0) |
| 89 | if(!pcap && (index == 0)) |
| 90 | { |
| 91 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
| 92 | bus, |
| 93 | *statusObjects[index]); |
| 94 | } |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | /** @brief reference to the bus */ |
| 100 | sdbusplus::bus::bus& bus; |
| 101 | |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 102 | /** @brief reference to sd_event wrapped in unique_ptr */ |
| 103 | EventPtr& event; |
| 104 | |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 105 | /** @brief OCC pass-through objects */ |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 106 | std::vector<std::unique_ptr<PassThrough>> passThroughObjects; |
| 107 | |
| 108 | /** @brief OCC Status objects */ |
| 109 | std::vector<std::unique_ptr<Status>> statusObjects; |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 110 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 111 | /** @brief Power cap monitor and occ notification object */ |
| 112 | std::unique_ptr<open_power::occ::powercap::PowerCap> pcap; |
| 113 | |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 114 | /** @brief sbdbusplus match objects */ |
| 115 | std::vector<sdbusplus::bus::match_t> cpuMatches; |
| 116 | }; |
| 117 | |
| 118 | } // namespace occ |
| 119 | } // namespace open_power |