blob: 29883424700538efac6db907f441e56280c6c277 [file] [log] [blame]
Jagpal Singh Gillca8c7e92024-11-02 16:51:48 -07001#include "CableConfig.hpp"
2
3#include <nlohmann/json.hpp>
4#include <phosphor-logging/lg2.hpp>
5#include <sdbusplus/async.hpp>
6
7#include <algorithm>
8#include <exception>
9#include <fstream>
10#include <string>
11
12namespace cable
13{
14
15PHOSPHOR_LOG2_USING;
16
17using json = nlohmann::json;
18
19static auto parseConfigFile(std::string configFile) -> json
20{
21 std::ifstream jsonFile(configFile);
22 if (!jsonFile.is_open())
23 {
24 error("Config file not found: {PATH}", "PATH", configFile);
25 return {};
26 }
27
28 try
29 {
30 return json::parse(jsonFile, nullptr, true);
31 }
32 catch (const json::parse_error& e)
33 {
34 error("Failed to parse config file {PATH}: {ERROR}", "PATH", configFile,
35 "ERROR", e);
36 }
37
38 return {};
39}
40
41auto Config::processConfig(std::string configFile)
42 -> sdbusplus::async::task<Config::Cables>
43{
44 Cables cables{};
45 auto jsonConfig = parseConfigFile(configFile);
46 if (jsonConfig.empty())
47 {
48 co_return cables;
49 }
50
51 static constexpr auto connectedCablesProperty = "ConnectedCables";
52 try
53 {
54 jsonConfig.at(connectedCablesProperty).get_to(cables);
55 }
56 catch (const std::exception& e)
57 {
58 error("Failed to find {PROPERTY} in config, {ERROR}", "PROPERTY",
59 connectedCablesProperty, "ERROR", e);
60 co_return cables;
61 }
62
63 Cables cablesTemp{};
64
65 for (auto cable : cables)
66 {
67 std::replace(cable.begin(), cable.end(), ' ', '_');
68 debug("Config: Cable {NAME}", "NAME", cable);
69 cablesTemp.insert(cable);
70 }
71
72 co_return cablesTemp;
73}
74
75} // namespace cable