Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 1 | #include "registries.hpp" |
| 2 | |
| 3 | #include "registries/base_message_registry.hpp" |
| 4 | #include "registries/openbmc_message_registry.hpp" |
| 5 | #include "str_utility.hpp" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | namespace redfish::registries |
| 11 | { |
| 12 | |
| 13 | const Message* getMessageFromRegistry(const std::string& messageKey, |
| 14 | std::span<const MessageEntry> registry) |
| 15 | { |
| 16 | std::span<const MessageEntry>::iterator messageIt = |
| 17 | std::find_if(registry.begin(), registry.end(), |
| 18 | [&messageKey](const MessageEntry& messageEntry) { |
| 19 | return std::strcmp(messageEntry.first, messageKey.c_str()) == 0; |
| 20 | }); |
| 21 | if (messageIt != registry.end()) |
| 22 | { |
| 23 | return &messageIt->second; |
| 24 | } |
| 25 | |
| 26 | return nullptr; |
| 27 | } |
| 28 | |
| 29 | const Message* getMessage(std::string_view messageID) |
| 30 | { |
| 31 | // Redfish MessageIds are in the form |
| 32 | // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find |
| 33 | // the right Message |
| 34 | std::vector<std::string> fields; |
| 35 | fields.reserve(4); |
| 36 | bmcweb::split(fields, messageID, '.'); |
| 37 | const std::string& registryName = fields[0]; |
| 38 | const std::string& messageKey = fields[3]; |
| 39 | |
| 40 | // Find the right registry and check it for the MessageKey |
| 41 | if (std::string(base::header.registryPrefix) == registryName) |
| 42 | { |
| 43 | return getMessageFromRegistry( |
| 44 | messageKey, std::span<const MessageEntry>(base::registry)); |
| 45 | } |
| 46 | if (std::string(openbmc::header.registryPrefix) == registryName) |
| 47 | { |
| 48 | return getMessageFromRegistry( |
| 49 | messageKey, std::span<const MessageEntry>(openbmc::registry)); |
| 50 | } |
| 51 | return nullptr; |
| 52 | } |
| 53 | |
| 54 | } // namespace redfish::registries |