blob: 615ad810222bd2a87131677dab7538eb486b95f7 [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "config.h"
2
3#include "selutility.hpp"
4
Tom Joseph6b7a1432017-05-19 10:43:36 +05305#include <chrono>
Vernon Mauerybdda8002019-02-26 10:18:51 -08006#include <filesystem>
Vernon Mauerye08fbff2019-04-03 09:19:34 -07007#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -07008#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07009#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070010#include <phosphor-logging/elog-errors.hpp>
Tom Joseph6b7a1432017-05-19 10:43:36 +053011#include <vector>
Patrick Venture3a5071a2018-09-12 13:27:42 -070012#include <xyz/openbmc_project/Common/error.hpp>
Patrick Venture46470a32018-09-07 19:26:25 -070013
Tom Joseph6b7a1432017-05-19 10:43:36 +053014extern const ipmi::sensor::InvObjectIDMap invSensors;
15using namespace phosphor::logging;
16using InternalFailure =
Patrick Venture0b02be92018-08-31 11:55:55 -070017 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Tom Joseph6b7a1432017-05-19 10:43:36 +053018
19namespace ipmi
20{
21
22namespace sel
23{
24
25namespace internal
26{
27
Patrick Venture0b02be92018-08-31 11:55:55 -070028GetSELEntryResponse
29 prepareSELEntry(const std::string& objPath,
30 ipmi::sensor::InvObjectIDMap::const_iterator iter)
Tom Joseph6b7a1432017-05-19 10:43:36 +053031{
Patrick Venture0b02be92018-08-31 11:55:55 -070032 GetSELEntryResponse record{};
Tom Joseph6b7a1432017-05-19 10:43:36 +053033
34 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
35 auto service = ipmi::getService(bus, logEntryIntf, objPath);
36
37 // Read all the log entry properties.
Patrick Venture0b02be92018-08-31 11:55:55 -070038 auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
39 propIntf, "GetAll");
Tom Joseph6b7a1432017-05-19 10:43:36 +053040 methodCall.append(logEntryIntf);
41
42 auto reply = bus.call(methodCall);
43 if (reply.is_method_error())
44 {
45 log<level::ERR>("Error in reading logging property entries");
46 elog<InternalFailure>();
47 }
48
Tom Joseph306878b2017-07-10 19:30:54 +053049 std::map<PropertyName, PropertyType> entryData;
Tom Joseph6b7a1432017-05-19 10:43:36 +053050 reply.read(entryData);
51
52 // Read Id from the log entry.
53 static constexpr auto propId = "Id";
54 auto iterId = entryData.find(propId);
55 if (iterId == entryData.end())
56 {
57 log<level::ERR>("Error in reading Id of logging entry");
58 elog<InternalFailure>();
59 }
60
Vernon Maueryf442e112019-04-09 11:44:36 -070061 record.recordID = static_cast<uint16_t>(std::get<uint32_t>(iterId->second));
Tom Joseph6b7a1432017-05-19 10:43:36 +053062
63 // Read Timestamp from the log entry.
64 static constexpr auto propTimeStamp = "Timestamp";
65 auto iterTimeStamp = entryData.find(propTimeStamp);
66 if (iterTimeStamp == entryData.end())
67 {
68 log<level::ERR>("Error in reading Timestamp of logging entry");
69 elog<InternalFailure>();
70 }
71
72 std::chrono::milliseconds chronoTimeStamp(
Vernon Maueryf442e112019-04-09 11:44:36 -070073 std::get<uint64_t>(iterTimeStamp->second));
Patrick Venture0b02be92018-08-31 11:55:55 -070074 record.timeStamp = static_cast<uint32_t>(
75 std::chrono::duration_cast<std::chrono::seconds>(chronoTimeStamp)
76 .count());
Tom Joseph6b7a1432017-05-19 10:43:36 +053077
78 static constexpr auto systemEventRecord = 0x02;
79 static constexpr auto generatorID = 0x2000;
80 static constexpr auto eventMsgRevision = 0x04;
81
82 record.recordType = systemEventRecord;
83 record.generatorID = generatorID;
84 record.eventMsgRevision = eventMsgRevision;
85
86 record.sensorType = iter->second.sensorType;
87 record.sensorNum = iter->second.sensorID;
88 record.eventData1 = iter->second.eventOffset;
89
90 // Read Resolved from the log entry.
91 static constexpr auto propResolved = "Resolved";
92 auto iterResolved = entryData.find(propResolved);
93 if (iterResolved == entryData.end())
94 {
95 log<level::ERR>("Error in reading Resolved field of logging entry");
96 elog<InternalFailure>();
97 }
98
99 static constexpr auto deassertEvent = 0x80;
100
101 // Evaluate if the event is assertion or deassertion event
Vernon Maueryf442e112019-04-09 11:44:36 -0700102 if (std::get<bool>(iterResolved->second))
Tom Joseph6b7a1432017-05-19 10:43:36 +0530103 {
104 record.eventType = deassertEvent | iter->second.eventReadingType;
105 }
106 else
107 {
108 record.eventType = iter->second.eventReadingType;
109 }
110
111 return record;
112}
113
114} // namespace internal
115
Tom Joseph6edc8a02017-06-30 18:52:56 +0530116GetSELEntryResponse convertLogEntrytoSEL(const std::string& objPath)
117{
118 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
119
120 static constexpr auto assocIntf = "org.openbmc.Associations";
121 static constexpr auto assocProp = "associations";
122
123 auto service = ipmi::getService(bus, assocIntf, objPath);
124
125 // Read the Associations interface.
Patrick Venture0b02be92018-08-31 11:55:55 -0700126 auto methodCall =
127 bus.new_method_call(service.c_str(), objPath.c_str(), propIntf, "Get");
Tom Joseph6edc8a02017-06-30 18:52:56 +0530128 methodCall.append(assocIntf);
129 methodCall.append(assocProp);
130
131 auto reply = bus.call(methodCall);
132 if (reply.is_method_error())
133 {
134 log<level::ERR>("Error in reading Associations interface");
135 elog<InternalFailure>();
136 }
137
Patrick Venture0b02be92018-08-31 11:55:55 -0700138 using AssociationList =
139 std::vector<std::tuple<std::string, std::string, std::string>>;
Tom Joseph6edc8a02017-06-30 18:52:56 +0530140
Vernon Mauery16b86932019-05-01 08:36:11 -0700141 std::variant<AssociationList> list;
Tom Joseph6edc8a02017-06-30 18:52:56 +0530142 reply.read(list);
143
Vernon Maueryf442e112019-04-09 11:44:36 -0700144 auto& assocs = std::get<AssociationList>(list);
Tom Joseph6edc8a02017-06-30 18:52:56 +0530145
146 /*
147 * Check if the log entry has any callout associations, if there is a
148 * callout association try to match the inventory path to the corresponding
149 * IPMI sensor.
150 */
151 for (const auto& item : assocs)
152 {
153 if (std::get<0>(item).compare(CALLOUT_FWD_ASSOCIATION) == 0)
154 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700155 auto iter = invSensors.find(std::get<2>(item));
156 if (iter == invSensors.end())
157 {
158 iter = invSensors.find(BOARD_SENSOR);
159 if (iter == invSensors.end())
160 {
161 log<level::ERR>("Motherboard sensor not found");
162 elog<InternalFailure>();
163 }
164 }
Tom Joseph6edc8a02017-06-30 18:52:56 +0530165
Patrick Venture0b02be92018-08-31 11:55:55 -0700166 return internal::prepareSELEntry(objPath, iter);
Tom Joseph6edc8a02017-06-30 18:52:56 +0530167 }
168 }
169
170 // If there are no callout associations link the log entry to system event
171 // sensor
172 auto iter = invSensors.find(SYSTEM_SENSOR);
173 if (iter == invSensors.end())
174 {
175 log<level::ERR>("System event sensor not found");
176 elog<InternalFailure>();
177 }
178
179 return internal::prepareSELEntry(objPath, iter);
180}
181
Tom Joseph399fd922017-06-30 18:40:30 +0530182std::chrono::seconds getEntryTimeStamp(const std::string& objPath)
183{
184 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
185
186 auto service = ipmi::getService(bus, logEntryIntf, objPath);
187
188 using namespace std::string_literals;
189 static const auto propTimeStamp = "Timestamp"s;
190
Patrick Venture0b02be92018-08-31 11:55:55 -0700191 auto methodCall =
192 bus.new_method_call(service.c_str(), objPath.c_str(), propIntf, "Get");
Tom Joseph399fd922017-06-30 18:40:30 +0530193 methodCall.append(logEntryIntf);
194 methodCall.append(propTimeStamp);
195
196 auto reply = bus.call(methodCall);
197 if (reply.is_method_error())
198 {
199 log<level::ERR>("Error in reading Timestamp from Entry interface");
200 elog<InternalFailure>();
201 }
202
Vernon Mauery16b86932019-05-01 08:36:11 -0700203 std::variant<uint64_t> timeStamp;
Tom Joseph399fd922017-06-30 18:40:30 +0530204 reply.read(timeStamp);
205
Vernon Maueryf442e112019-04-09 11:44:36 -0700206 std::chrono::milliseconds chronoTimeStamp(std::get<uint64_t>(timeStamp));
Tom Joseph399fd922017-06-30 18:40:30 +0530207
208 return std::chrono::duration_cast<std::chrono::seconds>(chronoTimeStamp);
209}
210
Tom Joseph232f5292017-07-07 20:14:02 +0530211void readLoggingObjectPaths(ObjectPaths& paths)
212{
213 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
214 auto depth = 0;
215 paths.clear();
216
Patrick Venture0b02be92018-08-31 11:55:55 -0700217 auto mapperCall = bus.new_method_call(mapperBusName, mapperObjPath,
218 mapperIntf, "GetSubTreePaths");
Tom Joseph232f5292017-07-07 20:14:02 +0530219 mapperCall.append(logBasePath);
220 mapperCall.append(depth);
221 mapperCall.append(ObjectPaths({logEntryIntf}));
222
223 auto reply = bus.call(mapperCall);
224 if (reply.is_method_error())
225 {
226 log<level::INFO>("Error in reading logging entry object paths");
227 }
228 else
229 {
230 reply.read(paths);
231
Patrick Venture0b02be92018-08-31 11:55:55 -0700232 std::sort(paths.begin(), paths.end(),
233 [](const std::string& a, const std::string& b) {
234 namespace fs = std::filesystem;
235 fs::path pathA(a);
236 fs::path pathB(b);
237 auto idA = std::stoul(pathA.filename().string());
238 auto idB = std::stoul(pathB.filename().string());
Tom Joseph232f5292017-07-07 20:14:02 +0530239
Patrick Venture0b02be92018-08-31 11:55:55 -0700240 return idA < idB;
241 });
Tom Joseph232f5292017-07-07 20:14:02 +0530242 }
243}
244
Tom Joseph6b7a1432017-05-19 10:43:36 +0530245} // namespace sel
246
247} // namespace ipmi