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" |
| 11 | |
| 12 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 13 | |
| 14 | namespace open_power |
| 15 | { |
| 16 | namespace occ |
| 17 | { |
| 18 | |
| 19 | /** @class Manager |
| 20 | * @brief Builds and manages OCC objects |
| 21 | */ |
| 22 | struct Manager |
| 23 | { |
| 24 | public: |
| 25 | Manager() = delete; |
| 26 | Manager(const Manager&) = delete; |
| 27 | Manager& operator=(const Manager&) = delete; |
| 28 | Manager(Manager&&) = default; |
| 29 | Manager& operator=(Manager&&) = default; |
| 30 | ~Manager() = default; |
| 31 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 32 | /** @brief Adds OCC pass-through and status objects on the bus |
| 33 | * when corresponding CPU inventory is created. |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 34 | * @param[in] bus - handle to the bus |
| 35 | */ |
| 36 | Manager(sdbusplus::bus::bus& bus): |
| 37 | bus(bus) |
| 38 | { |
| 39 | for (auto id = 0; id < MAX_CPUS; ++id) |
| 40 | { |
| 41 | auto path = std::string(CPU_PATH) + std::to_string(id); |
| 42 | cpuMatches.emplace_back( |
| 43 | bus, |
| 44 | sdbusRule::interfacesAdded() + |
| 45 | sdbusRule::argNpath(0, path), |
| 46 | std::bind(std::mem_fn(&Manager::cpuCreated), |
| 47 | this, std::placeholders::_1)); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** @brief Callback that responds to cpu creation in the inventory - |
| 52 | * by creating the occ passthrough and status objects. |
| 53 | * |
| 54 | * @param[in] msg - bus message |
| 55 | * |
| 56 | * @returns 0 to indicate success |
| 57 | */ |
| 58 | int cpuCreated(sdbusplus::message::message& msg) |
| 59 | { |
| 60 | namespace fs = std::experimental::filesystem; |
| 61 | |
| 62 | sdbusplus::message::object_path o; |
| 63 | msg.read(o); |
| 64 | fs::path cpuPath(std::string(std::move(o))); |
| 65 | auto cpu = cpuPath.filename(); |
| 66 | |
| 67 | std::string name{cpu.c_str()}; |
| 68 | auto index = name.find(CPU_NAME); |
| 69 | name.replace(index, std::strlen(CPU_NAME), OCC_NAME); |
| 70 | |
| 71 | auto path = fs::path(OCC_CONTROL_ROOT) / name; |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 72 | passThroughObjects.emplace_back( |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 73 | std::make_unique<PassThrough>( |
| 74 | bus, |
| 75 | path.c_str())); |
| 76 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 77 | statusObjects.emplace_back( |
| 78 | std::make_unique<Status>( |
| 79 | bus, |
| 80 | path.c_str())); |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | /** @brief reference to the bus */ |
| 86 | sdbusplus::bus::bus& bus; |
| 87 | |
| 88 | /** @brief OCC pass-through objects */ |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 89 | std::vector<std::unique_ptr<PassThrough>> passThroughObjects; |
| 90 | |
| 91 | /** @brief OCC Status objects */ |
| 92 | std::vector<std::unique_ptr<Status>> statusObjects; |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 93 | |
| 94 | /** @brief sbdbusplus match objects */ |
| 95 | std::vector<sdbusplus::bus::match_t> cpuMatches; |
| 96 | }; |
| 97 | |
| 98 | } // namespace occ |
| 99 | } // namespace open_power |