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