xref: /openbmc/phosphor-logging/extensions/openpower-pels/device_callouts.cpp (revision 18c42b0f7ff2e570a795e291452383ded7eab3aa)
1*18c42b0fSMatt Spinler /**
2*18c42b0fSMatt Spinler  * Copyright © 2020 IBM Corporation
3*18c42b0fSMatt Spinler  *
4*18c42b0fSMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5*18c42b0fSMatt Spinler  * you may not use this file except in compliance with the License.
6*18c42b0fSMatt Spinler  * You may obtain a copy of the License at
7*18c42b0fSMatt Spinler  *
8*18c42b0fSMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9*18c42b0fSMatt Spinler  *
10*18c42b0fSMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11*18c42b0fSMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12*18c42b0fSMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*18c42b0fSMatt Spinler  * See the License for the specific language governing permissions and
14*18c42b0fSMatt Spinler  * limitations under the License.
15*18c42b0fSMatt Spinler  */
16*18c42b0fSMatt Spinler #include "device_callouts.hpp"
17*18c42b0fSMatt Spinler 
18*18c42b0fSMatt Spinler #include "paths.hpp"
19*18c42b0fSMatt Spinler 
20*18c42b0fSMatt Spinler #include <fstream>
21*18c42b0fSMatt Spinler #include <phosphor-logging/log.hpp>
22*18c42b0fSMatt Spinler #include <regex>
23*18c42b0fSMatt Spinler 
24*18c42b0fSMatt Spinler namespace openpower::pels::device_callouts
25*18c42b0fSMatt Spinler {
26*18c42b0fSMatt Spinler 
27*18c42b0fSMatt Spinler constexpr auto debugFilePath = "/etc/phosphor-logging/";
28*18c42b0fSMatt Spinler constexpr auto calloutFileSuffix = "_dev_callouts.json";
29*18c42b0fSMatt Spinler 
30*18c42b0fSMatt Spinler namespace fs = std::filesystem;
31*18c42b0fSMatt Spinler using namespace phosphor::logging;
32*18c42b0fSMatt Spinler 
33*18c42b0fSMatt Spinler namespace util
34*18c42b0fSMatt Spinler {
35*18c42b0fSMatt Spinler 
36*18c42b0fSMatt Spinler fs::path getJSONFilename(const std::vector<std::string>& compatibleList)
37*18c42b0fSMatt Spinler {
38*18c42b0fSMatt Spinler     auto basePath = getPELReadOnlyDataPath();
39*18c42b0fSMatt Spinler     fs::path fullPath;
40*18c42b0fSMatt Spinler 
41*18c42b0fSMatt Spinler     // Find an entry in the list of compatible system names that
42*18c42b0fSMatt Spinler     // matches a filename we have.
43*18c42b0fSMatt Spinler 
44*18c42b0fSMatt Spinler     for (const auto& name : compatibleList)
45*18c42b0fSMatt Spinler     {
46*18c42b0fSMatt Spinler         fs::path filename = name + calloutFileSuffix;
47*18c42b0fSMatt Spinler 
48*18c42b0fSMatt Spinler         // Check the debug path first
49*18c42b0fSMatt Spinler         fs::path path{fs::path{debugFilePath} / filename};
50*18c42b0fSMatt Spinler 
51*18c42b0fSMatt Spinler         if (fs::exists(path))
52*18c42b0fSMatt Spinler         {
53*18c42b0fSMatt Spinler             log<level::INFO>("Found device callout debug file");
54*18c42b0fSMatt Spinler             fullPath = path;
55*18c42b0fSMatt Spinler             break;
56*18c42b0fSMatt Spinler         }
57*18c42b0fSMatt Spinler 
58*18c42b0fSMatt Spinler         path = basePath / filename;
59*18c42b0fSMatt Spinler 
60*18c42b0fSMatt Spinler         if (fs::exists(path))
61*18c42b0fSMatt Spinler         {
62*18c42b0fSMatt Spinler             fullPath = path;
63*18c42b0fSMatt Spinler             break;
64*18c42b0fSMatt Spinler         }
65*18c42b0fSMatt Spinler     }
66*18c42b0fSMatt Spinler 
67*18c42b0fSMatt Spinler     if (fullPath.empty())
68*18c42b0fSMatt Spinler     {
69*18c42b0fSMatt Spinler         throw std::invalid_argument(
70*18c42b0fSMatt Spinler             "No JSON dev path callout file for this system");
71*18c42b0fSMatt Spinler     }
72*18c42b0fSMatt Spinler 
73*18c42b0fSMatt Spinler     return fullPath;
74*18c42b0fSMatt Spinler }
75*18c42b0fSMatt Spinler 
76*18c42b0fSMatt Spinler /**
77*18c42b0fSMatt Spinler  * @brief Reads the callout JSON into an object based on the
78*18c42b0fSMatt Spinler  *        compatible system names list.
79*18c42b0fSMatt Spinler  *
80*18c42b0fSMatt Spinler  * @param[in] compatibleList - The list of compatible names for this
81*18c42b0fSMatt Spinler  *                             system.
82*18c42b0fSMatt Spinler  *
83*18c42b0fSMatt Spinler  * @return nlohmann::json - The JSON object
84*18c42b0fSMatt Spinler  */
85*18c42b0fSMatt Spinler nlohmann::json loadJSON(const std::vector<std::string>& compatibleList)
86*18c42b0fSMatt Spinler {
87*18c42b0fSMatt Spinler     auto filename = getJSONFilename(compatibleList);
88*18c42b0fSMatt Spinler     std::ifstream file{filename};
89*18c42b0fSMatt Spinler     return nlohmann::json::parse(file);
90*18c42b0fSMatt Spinler }
91*18c42b0fSMatt Spinler 
92*18c42b0fSMatt Spinler std::vector<device_callouts::Callout>
93*18c42b0fSMatt Spinler     calloutI2C(size_t i2cBus, uint8_t i2cAddress,
94*18c42b0fSMatt Spinler                const nlohmann::json& calloutJSON)
95*18c42b0fSMatt Spinler {
96*18c42b0fSMatt Spinler     // TODO
97*18c42b0fSMatt Spinler     return {};
98*18c42b0fSMatt Spinler }
99*18c42b0fSMatt Spinler 
100*18c42b0fSMatt Spinler std::vector<device_callouts::Callout> findCallouts(const std::string& devPath,
101*18c42b0fSMatt Spinler                                                    const nlohmann::json& json)
102*18c42b0fSMatt Spinler {
103*18c42b0fSMatt Spinler     std::vector<Callout> callouts;
104*18c42b0fSMatt Spinler 
105*18c42b0fSMatt Spinler     // TODO
106*18c42b0fSMatt Spinler 
107*18c42b0fSMatt Spinler     return callouts;
108*18c42b0fSMatt Spinler }
109*18c42b0fSMatt Spinler 
110*18c42b0fSMatt Spinler } // namespace util
111*18c42b0fSMatt Spinler 
112*18c42b0fSMatt Spinler std::vector<Callout> getCallouts(const std::string& devPath,
113*18c42b0fSMatt Spinler                                  const std::vector<std::string>& compatibleList)
114*18c42b0fSMatt Spinler {
115*18c42b0fSMatt Spinler     auto json = util::loadJSON(compatibleList);
116*18c42b0fSMatt Spinler     return util::findCallouts(devPath, json);
117*18c42b0fSMatt Spinler }
118*18c42b0fSMatt Spinler 
119*18c42b0fSMatt Spinler std::vector<Callout>
120*18c42b0fSMatt Spinler     getI2CCallouts(size_t i2cBus, uint8_t i2cAddress,
121*18c42b0fSMatt Spinler                    const std::vector<std::string>& compatibleList)
122*18c42b0fSMatt Spinler {
123*18c42b0fSMatt Spinler     auto json = util::loadJSON(compatibleList);
124*18c42b0fSMatt Spinler     return util::calloutI2C(i2cBus, i2cAddress, json);
125*18c42b0fSMatt Spinler }
126*18c42b0fSMatt Spinler 
127*18c42b0fSMatt Spinler } // namespace openpower::pels::device_callouts
128