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