blob: a9dab258ab06e60e57c7012456824e586045873f [file] [log] [blame]
James Feist1df06a42019-04-11 14:23:04 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#pragma once
18
James Feist733f7652019-11-13 14:32:29 -080019#include "Utils.hpp"
20
James Feist1df06a42019-04-11 14:23:04 -070021#include <systemd/sd-journal.h>
22
James Feist733f7652019-11-13 14:32:29 -080023#include <boost/container/flat_map.hpp>
James Feist1a996582019-05-14 15:10:06 -070024#include <nlohmann/json.hpp>
James Feist4dc617b2020-05-01 09:54:47 -070025#include <sdbusplus/asio/object_server.hpp>
James Feist8c505da2020-05-28 10:06:33 -070026
27#include <iostream>
28#include <list>
James Feist1df06a42019-04-11 14:23:04 -070029#include <string>
30
James Feist733f7652019-11-13 14:32:29 -080031using DBusProbeObjectT = boost::container::flat_map<
32 std::string,
33 std::vector<boost::container::flat_map<std::string, BasicVariantType>>>;
34
James Feist7d807752019-11-13 14:47:57 -080035using FoundDeviceT =
36 std::vector<boost::container::flat_map<std::string, BasicVariantType>>;
37
James Feist733f7652019-11-13 14:32:29 -080038struct PerformScan : std::enable_shared_from_this<PerformScan>
39{
40
41 PerformScan(nlohmann::json& systemConfiguration,
42 nlohmann::json& missingConfigurations,
43 std::list<nlohmann::json>& configurations,
James Feist4dc617b2020-05-01 09:54:47 -070044 sdbusplus::asio::object_server& objServer,
45 std::function<void()>&& callback);
James Feist733f7652019-11-13 14:32:29 -080046 void run(void);
47 virtual ~PerformScan();
48 nlohmann::json& _systemConfiguration;
49 nlohmann::json& _missingConfigurations;
50 std::list<nlohmann::json> _configurations;
James Feist4dc617b2020-05-01 09:54:47 -070051 sdbusplus::asio::object_server& objServer;
52 std::function<void()> _callback;
James Feist733f7652019-11-13 14:32:29 -080053 bool _passed = false;
54 bool powerWasOn = isPowerOn();
55 DBusProbeObjectT dbusProbeObjects;
56 std::vector<std::string> passedProbes;
57};
58
James Feist7d807752019-11-13 14:47:57 -080059// this class finds the needed dbus fields and on destruction runs the probe
60struct PerformProbe : std::enable_shared_from_this<PerformProbe>
61{
62 PerformProbe(const std::vector<std::string>& probeCommand,
63 std::shared_ptr<PerformScan>& scanPtr,
64 std::function<void(FoundDeviceT&)>&& callback);
65 virtual ~PerformProbe();
66
67 std::vector<std::string> _probeCommand;
68 std::shared_ptr<PerformScan> scan;
69 std::function<void(FoundDeviceT&)> _callback;
70};
71
James Feist1a996582019-05-14 15:10:06 -070072inline void logDeviceAdded(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -070073{
74
James Feist1ffa4a42020-04-22 18:27:17 -070075 if (!deviceHasLogging(record))
76 {
77 return;
78 }
James Feist1a996582019-05-14 15:10:06 -070079 auto findType = record.find("Type");
80 auto findAsset =
81 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
82
83 std::string model = "Unkown";
84 std::string type = "Unknown";
85 std::string sn = "Unkown";
86
87 if (findType != record.end())
88 {
89 type = findType->get<std::string>();
90 }
91 if (findAsset != record.end())
92 {
93 auto findModel = findAsset->find("Model");
94 auto findSn = findAsset->find("SerialNumber");
95 if (findModel != findAsset->end())
96 {
97 model = findModel->get<std::string>();
98 }
99 if (findSn != findAsset->end())
100 {
James Feistf5125b02019-06-06 11:27:43 -0700101 const std::string* getSn = findSn->get_ptr<const std::string*>();
102 if (getSn != nullptr)
103 {
104 sn = *getSn;
105 }
106 else
107 {
108 sn = findSn->dump();
109 }
James Feist1a996582019-05-14 15:10:06 -0700110 }
111 }
112
James Feist1df06a42019-04-11 14:23:04 -0700113 sd_journal_send("MESSAGE=%s", "Inventory Added", "PRIORITY=%i", LOG_ERR,
114 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded",
James Feist1a996582019-05-14 15:10:06 -0700115 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
116 type.c_str(), sn.c_str(), NULL);
James Feist1df06a42019-04-11 14:23:04 -0700117}
118
James Feist1a996582019-05-14 15:10:06 -0700119inline void logDeviceRemoved(const nlohmann::json& record)
James Feist1df06a42019-04-11 14:23:04 -0700120{
James Feist1ffa4a42020-04-22 18:27:17 -0700121 if (!deviceHasLogging(record))
122 {
123 return;
124 }
James Feist1a996582019-05-14 15:10:06 -0700125 auto findType = record.find("Type");
126 auto findAsset =
127 record.find("xyz.openbmc_project.Inventory.Decorator.Asset");
128
129 std::string model = "Unkown";
130 std::string type = "Unknown";
131 std::string sn = "Unkown";
132
133 if (findType != record.end())
134 {
135 type = findType->get<std::string>();
136 }
137 if (findAsset != record.end())
138 {
139 auto findModel = findAsset->find("Model");
140 auto findSn = findAsset->find("SerialNumber");
141 if (findModel != findAsset->end())
142 {
143 model = findModel->get<std::string>();
144 }
145 if (findSn != findAsset->end())
146 {
James Feistf5125b02019-06-06 11:27:43 -0700147 const std::string* getSn = findSn->get_ptr<const std::string*>();
148 if (getSn != nullptr)
149 {
150 sn = *getSn;
151 }
152 else
153 {
154 sn = findSn->dump();
155 }
James Feist1a996582019-05-14 15:10:06 -0700156 }
157 }
James Feist1df06a42019-04-11 14:23:04 -0700158
159 sd_journal_send("MESSAGE=%s", "Inventory Removed", "PRIORITY=%i", LOG_ERR,
160 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved",
James Feist1a996582019-05-14 15:10:06 -0700161 "REDFISH_MESSAGE_ARGS=%s,%s,%s", model.c_str(),
162 type.c_str(), sn.c_str(), NULL);
James Feist4dc617b2020-05-01 09:54:47 -0700163}