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 <algorithm>
8 #include <cstring>
9 #include <ranges>
10 #include <span>
11 #include <string>
12 #include <string_view>
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 {
21     std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if(
22         registry, [&messageKey](const MessageEntry& messageEntry) {
23         return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
24     });
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
59