xref: /openbmc/entity-manager/src/devicetree_vpd_parser/machine_context.cpp (revision cfc7f4f423c8163c394fbd777afcaf10a835206f)
1*cfc7f4f4SChristopher Meis /*
2*cfc7f4f4SChristopher Meis // Copyright (c) 2024 Hewlett Packard Enterprise
3*cfc7f4f4SChristopher Meis //
4*cfc7f4f4SChristopher Meis // Licensed under the Apache License, Version 2.0 (the "License");
5*cfc7f4f4SChristopher Meis // you may not use this file except in compliance with the License.
6*cfc7f4f4SChristopher Meis // You may obtain a copy of the License at
7*cfc7f4f4SChristopher Meis //
8*cfc7f4f4SChristopher Meis //      http://www.apache.org/licenses/LICENSE-2.0
9*cfc7f4f4SChristopher Meis //
10*cfc7f4f4SChristopher Meis // Unless required by applicable law or agreed to in writing, software
11*cfc7f4f4SChristopher Meis // distributed under the License is distributed on an "AS IS" BASIS,
12*cfc7f4f4SChristopher Meis // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*cfc7f4f4SChristopher Meis // See the License for the specific language governing permissions and
14*cfc7f4f4SChristopher Meis // limitations under the License.
15*cfc7f4f4SChristopher Meis */
16*cfc7f4f4SChristopher Meis 
17*cfc7f4f4SChristopher Meis #include "machine_context.hpp"
18*cfc7f4f4SChristopher Meis 
19*cfc7f4f4SChristopher Meis #include <filesystem>
20*cfc7f4f4SChristopher Meis #include <fstream>
21*cfc7f4f4SChristopher Meis 
22*cfc7f4f4SChristopher Meis void MachineContext::populateFromDeviceTree()
23*cfc7f4f4SChristopher Meis {
24*cfc7f4f4SChristopher Meis     std::string nodeVal;
25*cfc7f4f4SChristopher Meis     std::ifstream vpdStream(nodeBasePath + std::string("model"));
26*cfc7f4f4SChristopher Meis     if (vpdStream && std::getline(vpdStream, nodeVal))
27*cfc7f4f4SChristopher Meis     {
28*cfc7f4f4SChristopher Meis         MachineContext::Asset::model(nodeVal);
29*cfc7f4f4SChristopher Meis         vpdStream.close();
30*cfc7f4f4SChristopher Meis     }
31*cfc7f4f4SChristopher Meis 
32*cfc7f4f4SChristopher Meis     vpdStream.open(nodeBasePath + std::string("serial-number"));
33*cfc7f4f4SChristopher Meis     if (vpdStream && std::getline(vpdStream, nodeVal))
34*cfc7f4f4SChristopher Meis     {
35*cfc7f4f4SChristopher Meis         MachineContext::Asset::serial_number(nodeVal);
36*cfc7f4f4SChristopher Meis         vpdStream.close();
37*cfc7f4f4SChristopher Meis     }
38*cfc7f4f4SChristopher Meis };
39*cfc7f4f4SChristopher Meis 
40*cfc7f4f4SChristopher Meis bool MachineContext::keyNodeExists()
41*cfc7f4f4SChristopher Meis {
42*cfc7f4f4SChristopher Meis     std::filesystem::path nodePath{nodeBasePath + std::string("model")};
43*cfc7f4f4SChristopher Meis 
44*cfc7f4f4SChristopher Meis     return std::filesystem::exists(nodePath);
45*cfc7f4f4SChristopher Meis };
46