blob: 8c8b36ac57bd1dd24e547a6fdc9df9a4813c06e9 [file] [log] [blame]
Sui Chend1d411f2022-04-10 23:09:36 -07001#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 Tanousf0b59af2024-03-20 13:38:04 -07007#include <algorithm>
8#include <cstring>
Ed Tanous3544d2a2023-08-06 18:12:20 -07009#include <ranges>
Ed Tanousf0b59af2024-03-20 13:38:04 -070010#include <span>
Sui Chend1d411f2022-04-10 23:09:36 -070011#include <string>
Ed Tanousf0b59af2024-03-20 13:38:04 -070012#include <string_view>
Sui Chend1d411f2022-04-10 23:09:36 -070013#include <vector>
14
15namespace redfish::registries
16{
17
18const Message* getMessageFromRegistry(const std::string& messageKey,
19 std::span<const MessageEntry> registry)
20{
Ed Tanous3544d2a2023-08-06 18:12:20 -070021 std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if(
22 registry, [&messageKey](const MessageEntry& messageEntry) {
Patrick Williams5a39f772023-10-20 11:20:21 -050023 return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
24 });
Sui Chend1d411f2022-04-10 23:09:36 -070025 if (messageIt != registry.end())
26 {
27 return &messageIt->second;
28 }
29
30 return nullptr;
31}
32
33const 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