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 | |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 7 | #include <ranges> |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | namespace redfish::registries |
| 12 | { |
| 13 | |
| 14 | const Message* getMessageFromRegistry(const std::string& messageKey, |
| 15 | std::span<const MessageEntry> registry) |
| 16 | { |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 17 | std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if( |
| 18 | registry, [&messageKey](const MessageEntry& messageEntry) { |
| 19 | return std::strcmp(messageEntry.first, messageKey.c_str()) == 0; |
Sui Chen | d1d411f | 2022-04-10 23:09:36 -0700 | [diff] [blame] | 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 |