Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Williams | 0bb89f8 | 2021-04-16 16:30:04 -0500 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 5 | #include "callouts-gen.hpp" |
| 6 | #include "elog_entry.hpp" |
| 7 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <cstring> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <tuple> |
| 14 | #include <vector> |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace logging |
| 19 | { |
| 20 | namespace metadata |
| 21 | { |
| 22 | |
| 23 | using Metadata = std::string; |
| 24 | |
| 25 | namespace associations |
| 26 | { |
| 27 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 28 | using Type = void(const std::string&, const std::vector<std::string>&, |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 29 | AssociationList& list); |
| 30 | |
Deepak Kodihalli | 66c46eb | 2017-03-02 03:07:10 -0600 | [diff] [blame] | 31 | /** @brief Pull out metadata name and value from the string |
| 32 | * <metadata name>=<metadata value> |
| 33 | * @param [in] data - metadata key=value entries |
| 34 | * @param [out] metadata - map of metadata name:value |
| 35 | */ |
| 36 | inline void parse(const std::vector<std::string>& data, |
| 37 | std::map<std::string, std::string>& metadata) |
| 38 | { |
| 39 | constexpr auto separator = '='; |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 40 | for (const auto& entryItem : data) |
Deepak Kodihalli | 66c46eb | 2017-03-02 03:07:10 -0600 | [diff] [blame] | 41 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 42 | auto pos = entryItem.find(separator); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 43 | if (std::string::npos != pos) |
Deepak Kodihalli | 66c46eb | 2017-03-02 03:07:10 -0600 | [diff] [blame] | 44 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 45 | auto key = entryItem.substr(0, entryItem.find(separator)); |
| 46 | auto value = entryItem.substr(entryItem.find(separator) + 1); |
Deepak Kodihalli | 66c46eb | 2017-03-02 03:07:10 -0600 | [diff] [blame] | 47 | metadata.emplace(std::move(key), std::move(value)); |
| 48 | } |
| 49 | } |
Patrick Williams | 44681ef | 2021-04-16 06:31:41 -0500 | [diff] [blame] | 50 | } |
Deepak Kodihalli | 66c46eb | 2017-03-02 03:07:10 -0600 | [diff] [blame] | 51 | |
Matt Spinler | 3fb83b3 | 2019-07-26 11:22:44 -0500 | [diff] [blame] | 52 | /** @brief Combine the metadata keys and values from the map |
| 53 | * into a vector of strings that look like: |
| 54 | * "<metadata name>=<metadata value>" |
| 55 | * @param [in] data - metadata key:value map |
| 56 | * @param [out] metadata - vector of "key=value" strings |
| 57 | */ |
| 58 | inline void combine(const std::map<std::string, std::string>& data, |
| 59 | std::vector<std::string>& metadata) |
| 60 | { |
| 61 | for (const auto& [key, value] : data) |
| 62 | { |
| 63 | std::string line{key}; |
| 64 | line += "=" + value; |
| 65 | metadata.push_back(std::move(line)); |
| 66 | } |
| 67 | } |
| 68 | |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 69 | /** @brief Build error associations specific to metadata. Specialize this |
| 70 | * template for handling a specific type of metadata. |
| 71 | * @tparam M - type of metadata |
Deepak Kodihalli | 327e580 | 2017-02-28 02:39:56 -0600 | [diff] [blame] | 72 | * @param [in] match - metadata to be handled |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 73 | * @param [in] data - metadata key=value entries |
| 74 | * @param [out] list - list of error association objects |
| 75 | */ |
| 76 | template <typename M> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 77 | void build(const std::string& match, const std::vector<std::string>& data, |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 78 | AssociationList& list) = delete; |
| 79 | |
Deepak Kodihalli | 327e580 | 2017-02-28 02:39:56 -0600 | [diff] [blame] | 80 | // Example template specialization - we don't want to do anything |
| 81 | // for this metadata. |
| 82 | using namespace example::xyz::openbmc_project::Example::Elog; |
| 83 | template <> |
Patrick Williams | 86bb3fe | 2021-04-16 06:34:05 -0500 | [diff] [blame] | 84 | inline void |
| 85 | build<TestErrorTwo::DEV_ID>(const std::string& /*match*/, |
| 86 | const std::vector<std::string>& /*data*/, |
| 87 | AssociationList& /*list*/) |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 88 | {} |
Deepak Kodihalli | 327e580 | 2017-02-28 02:39:56 -0600 | [diff] [blame] | 89 | |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 90 | template <> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 91 | inline void |
| 92 | build<example::xyz::openbmc_project::Example::Device::Callout:: |
| 93 | CALLOUT_DEVICE_PATH_TEST>(const std::string& match, |
| 94 | const std::vector<std::string>& data, |
| 95 | AssociationList& list) |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 96 | { |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 97 | std::map<std::string, std::string> metadata; |
| 98 | parse(data, metadata); |
| 99 | auto iter = metadata.find(match); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 100 | if (metadata.end() != iter) |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 101 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 102 | auto comp = [](const auto& first, const auto& second) { |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 103 | return (std::strcmp(std::get<0>(first), second) < 0); |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 104 | }; |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 105 | auto callout = std::lower_bound(callouts.begin(), callouts.end(), |
| 106 | (iter->second).c_str(), comp); |
| 107 | if ((callouts.end() != callout) && |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 108 | !std::strcmp((iter->second).c_str(), std::get<0>(*callout))) |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 109 | { |
Patrick Venture | caa73ad | 2018-10-30 13:13:06 -0700 | [diff] [blame] | 110 | constexpr auto ROOT = "/xyz/openbmc_project/inventory"; |
| 111 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 112 | list.push_back(std::make_tuple( |
| 113 | "callout", "fault", std::string(ROOT) + std::get<1>(*callout))); |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Deepak Kodihalli | 5edacf3 | 2017-03-16 07:04:07 -0500 | [diff] [blame] | 118 | // The PROCESS_META flag is needed to get out of tree builds working. Such |
| 119 | // builds will have access only to internal error interfaces, hence handlers |
| 120 | // for out dbus error interfaces won't compile. This flag is not set by default, |
| 121 | // the phosphor-logging recipe enabled it. |
Deepak Kodihalli | 682326a | 2017-03-06 05:26:53 -0600 | [diff] [blame] | 122 | #if defined PROCESS_META |
| 123 | |
| 124 | template <> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 125 | void build<xyz::openbmc_project::Common::Callout::Device::CALLOUT_DEVICE_PATH>( |
| 126 | const std::string& match, const std::vector<std::string>& data, |
Deepak Kodihalli | 682326a | 2017-03-06 05:26:53 -0600 | [diff] [blame] | 127 | AssociationList& list); |
| 128 | |
Tom Joseph | 213aaf6 | 2017-07-25 00:02:09 +0530 | [diff] [blame] | 129 | template <> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 130 | void build< |
| 131 | xyz::openbmc_project::Common::Callout::Inventory::CALLOUT_INVENTORY_PATH>( |
| 132 | const std::string& match, const std::vector<std::string>& data, |
Tom Joseph | 213aaf6 | 2017-07-25 00:02:09 +0530 | [diff] [blame] | 133 | AssociationList& list); |
| 134 | |
Deepak Kodihalli | 682326a | 2017-03-06 05:26:53 -0600 | [diff] [blame] | 135 | #endif // PROCESS_META |
| 136 | |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 137 | } // namespace associations |
| 138 | } // namespace metadata |
| 139 | } // namespace logging |
| 140 | } // namespace phosphor |