blob: 75e94dd8c2876ca87c7521dd94736b8c93919b25 [file] [log] [blame]
Willy Tu6c71b0f2021-10-10 13:34:41 -07001// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14#include "config.h"
15
16#include "bifurcation.hpp"
17
Patrick Williams444b5ea2023-05-19 13:56:42 -050018#include <nlohmann/json.hpp>
Willy Tuc5b55232023-11-01 09:27:40 -070019#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050020
Willy Tu6c71b0f2021-10-10 13:34:41 -070021#include <charconv>
22#include <filesystem>
23#include <fstream>
Willy Tu6c71b0f2021-10-10 13:34:41 -070024#include <optional>
25#include <string_view>
26#include <vector>
27
28namespace google
29{
30namespace ipmi
31{
32
33BifurcationStatic::BifurcationStatic() :
34 BifurcationStatic(STATIC_BIFURCATION_CONFIG)
Patrick Williams444b5ea2023-05-19 13:56:42 -050035{}
Willy Tu6c71b0f2021-10-10 13:34:41 -070036
37BifurcationStatic::BifurcationStatic(std::string_view bifurcationFile) :
38 bifurcationFile(bifurcationFile)
Patrick Williams444b5ea2023-05-19 13:56:42 -050039{}
Willy Tu6c71b0f2021-10-10 13:34:41 -070040
41std::optional<std::vector<uint8_t>>
42 BifurcationStatic::getBifurcation(uint8_t index) noexcept
43{
44 // Example valid data:
45 // {
46 // "1": [8,8],
47 // "2": [4, 4, 12]
48 // }
49 std::ifstream jsonFile(bifurcationFile.c_str());
50 if (!jsonFile.is_open())
51 {
Willy Tuc5b55232023-11-01 09:27:40 -070052 stdplus::print(stderr, "Unable to open file {} for bifurcation.\n",
53 bifurcationFile.data());
Willy Tu6c71b0f2021-10-10 13:34:41 -070054 return std::nullopt;
55 }
56
57 nlohmann::json jsonData;
58 try
59 {
60 jsonData = nlohmann::json::parse(jsonFile, nullptr, false);
61 }
62 catch (const nlohmann::json::parse_error& ex)
63 {
Willy Tuc5b55232023-11-01 09:27:40 -070064 stdplus::print(
Willy Tu6c71b0f2021-10-10 13:34:41 -070065 stderr,
66 "Failed to parse the static config. Parse error at byte {}\n",
67 ex.byte);
68 return std::nullopt;
69 }
70
71 std::vector<uint8_t> vec;
72 try
73 {
74 std::string key = std::to_string(index);
75 auto value = jsonData[key];
76 value.get_to(vec);
77 }
Patrick Williams444b5ea2023-05-19 13:56:42 -050078 catch (const std::exception& e)
Willy Tu6c71b0f2021-10-10 13:34:41 -070079 {
Willy Tuc5b55232023-11-01 09:27:40 -070080 stdplus::print(stderr,
81 "Failed to convert bifurcation value to vec[uin8_t]\n");
Willy Tu6c71b0f2021-10-10 13:34:41 -070082 return std::nullopt;
83 }
84
85 return vec;
86}
87
88} // namespace ipmi
89} // namespace google