blob: d37914700d0299c3e09cebf835609cf0c92b6ac0 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
Jason M. Bills70304cb2019-03-27 12:03:59 -07004#pragma once
Ed Tanousc5ba4c22022-02-07 09:59:55 -08005
Ed Tanous56b81992024-12-02 10:36:37 -08006#include "bmcweb_config.h"
7
Ed Tanous65e4f1f2022-02-08 00:23:54 -08008#include <nlohmann/json.hpp>
9
Nan Zhoud5c80ad2022-07-11 01:16:31 +000010#include <array>
Ed Tanous80f595e2022-02-14 09:32:05 -080011#include <charconv>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000012#include <cstddef>
Ed Tanous56b81992024-12-02 10:36:37 -080013#include <format>
Ed Tanousc5ba4c22022-02-07 09:59:55 -080014#include <span>
15#include <string>
16#include <string_view>
Ed Tanousd7857202025-01-28 15:32:26 -080017#include <system_error>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000018#include <utility>
19
Ed Tanousfffb8c12022-02-07 23:53:03 -080020namespace redfish::registries
Jason M. Bills70304cb2019-03-27 12:03:59 -070021{
Jason M. Bills351d3062019-03-27 12:58:21 -070022struct Header
23{
24 const char* copyright;
25 const char* type;
Ed Tanous56b81992024-12-02 10:36:37 -080026 unsigned int versionMajor;
27 unsigned int versionMinor;
28 unsigned int versionPatch;
Jason M. Bills351d3062019-03-27 12:58:21 -070029 const char* name;
30 const char* language;
31 const char* description;
32 const char* registryPrefix;
Jason M. Bills351d3062019-03-27 12:58:21 -070033 const char* owningEntity;
34};
Jason M. Bills70304cb2019-03-27 12:03:59 -070035
36struct Message
37{
38 const char* description;
39 const char* message;
Gunnar Millse7808c92020-07-08 21:17:44 -050040 const char* messageSeverity;
Ed Tanous271584a2019-07-09 16:24:22 -070041 const size_t numberOfArgs;
Jason M. Bills70304cb2019-03-27 12:03:59 -070042 std::array<const char*, 5> paramTypes;
43 const char* resolution;
44};
45using MessageEntry = std::pair<const char*, const Message>;
Ed Tanousc5ba4c22022-02-07 09:59:55 -080046
Patrick Williamsbd79bce2024-08-16 15:22:20 -040047inline std::string fillMessageArgs(
48 const std::span<const std::string_view> messageArgs, std::string_view msg)
Ed Tanousc5ba4c22022-02-07 09:59:55 -080049{
Ed Tanous80f595e2022-02-14 09:32:05 -080050 std::string ret;
51 size_t reserve = msg.size();
Ed Tanous26ccae32023-02-16 10:28:44 -080052 for (std::string_view arg : messageArgs)
Ed Tanousc5ba4c22022-02-07 09:59:55 -080053 {
Ed Tanous80f595e2022-02-14 09:32:05 -080054 reserve += arg.size();
Ed Tanousc5ba4c22022-02-07 09:59:55 -080055 }
Ed Tanous80f595e2022-02-14 09:32:05 -080056 ret.reserve(reserve);
57
58 for (size_t stringIndex = msg.find('%'); stringIndex != std::string::npos;
59 stringIndex = msg.find('%'))
60 {
61 ret += msg.substr(0, stringIndex);
62 msg.remove_prefix(stringIndex + 1);
63 size_t number = 0;
Ed Tanous7da633f2024-12-02 08:25:38 -080064 auto it = std::from_chars(&*msg.begin(), &*msg.end(), number);
Ed Tanous80f595e2022-02-14 09:32:05 -080065 if (it.ec != std::errc())
66 {
67 return "";
68 }
69 msg.remove_prefix(1);
70 // Redfish message args are 1 indexed.
71 number--;
72 if (number >= messageArgs.size())
73 {
74 return "";
75 }
76 ret += messageArgs[number];
77 }
78 ret += msg;
79 return ret;
Ed Tanousc5ba4c22022-02-07 09:59:55 -080080}
81
Patrick Williamsbd79bce2024-08-16 15:22:20 -040082inline nlohmann::json::object_t getLogFromRegistry(
83 const Header& header, std::span<const MessageEntry> registry, size_t index,
84 std::span<const std::string_view> args)
Ed Tanous65e4f1f2022-02-08 00:23:54 -080085{
86 const redfish::registries::MessageEntry& entry = registry[index];
87 // Intentionally make a copy of the string, so we can append in the
88 // parameters.
Krzysztof Grobelny2e30bc22022-09-09 10:13:41 +020089 std::string msg =
90 redfish::registries::fillMessageArgs(args, entry.second.message);
Ed Tanous65e4f1f2022-02-08 00:23:54 -080091 nlohmann::json jArgs = nlohmann::json::array();
Ed Tanous26ccae32023-02-16 10:28:44 -080092 for (std::string_view arg : args)
Ed Tanous65e4f1f2022-02-08 00:23:54 -080093 {
94 jArgs.push_back(arg);
95 }
Ed Tanous56b81992024-12-02 10:36:37 -080096 std::string msgId;
97 if (BMCWEB_REDFISH_USE_3_DIGIT_MESSAGEID)
98 {
99 msgId = std::format("{}.{}.{}.{}.{}", header.registryPrefix,
100 header.versionMajor, header.versionMinor,
101 header.versionPatch, entry.first);
102 }
103 else
104 {
105 msgId =
106 std::format("{}.{}.{}.{}", header.registryPrefix,
107 header.versionMajor, header.versionMinor, entry.first);
108 }
Ed Tanous65e4f1f2022-02-08 00:23:54 -0800109 nlohmann::json::object_t response;
110 response["@odata.type"] = "#Message.v1_1_1.Message";
111 response["MessageId"] = std::move(msgId);
112 response["Message"] = std::move(msg);
113 response["MessageArgs"] = std::move(jArgs);
114 response["MessageSeverity"] = entry.second.messageSeverity;
115 response["Resolution"] = entry.second.resolution;
116 return response;
117}
118
Sui Chend1d411f2022-04-10 23:09:36 -0700119const Message* getMessage(std::string_view messageID);
120
121const Message* getMessageFromRegistry(const std::string& messageKey,
122 std::span<const MessageEntry> registry);
123
Ed Tanousfffb8c12022-02-07 23:53:03 -0800124} // namespace redfish::registries