blob: cb90aab91357856c1c8768edf5a4d327a682a732 [file] [log] [blame]
Chris Austen41a4b312015-10-25 03:45:42 -05001#include <stdint.h>
2#include <cstdlib>
3#include <cstring>
4#include <fstream>
5#include <iostream>
6#include <algorithm>
7#include <vector>
8#include <memory>
9#include <systemd/sd-bus.h>
10
11#include "ipmid.H"
12#include "storagehandler.h"
13#include "sensorhandler.h"
14
15using namespace std;
16
17extern int find_openbmc_path(const char *, const uint8_t , dbus_interface_t *);
18
19
20//////////////////////////
21struct esel_section_headers_t {
22 uint8_t sectionid[2];
23 uint8_t sectionlength[2];
24 uint8_t version;
25 uint8_t subsectiontype;
26 uint8_t compid;
27};
28
29struct severity_values_t {
30 uint8_t type;
31 const char *description;
32};
33
34
35const std::vector<severity_values_t> g_sev_desc = {
36 {0x10, "recoverable error"},
37 {0x20, "predictive error"},
38 {0x40, "unrecoverable error"},
39 {0x50, "critical error"},
40 {0x60, "error from a diagnostic test"},
41 {0x70, "recovered symptom "},
42 {0xFF, "Unknown"},
43};
44
45const char* sev_lookup(uint8_t n) {
46 auto i = std::find_if(std::begin(g_sev_desc), std::end(g_sev_desc),
47 [n](auto p){ return p.type == n || p.type == 0xFF; });
48 return i->description;
49}
50
51
52
53
54int find_sensor_type_string(uint8_t sensor_number, char **s) {
55
56 dbus_interface_t a;
57 const char *p;
58 char r;
59
60 r = find_openbmc_path("SENSOR", sensor_number, &a);
61
62 if ((r < 0) || (a.bus[0] == 0)) {
63 // Just make a generic message for errors that
64 // occur on sensors that dont exist
65 asprintf(s, "Unknown Sensor (0x%02x)", sensor_number);
66 } else {
67
68 if ((p = strrchr (a.path, '/')) == NULL) {
69 p = "/Unknown Sensor";
70 }
71
72 asprintf(s, "%s", p+1);
73 }
74
75 return 0;
76}
77
78
79size_t getfilestream(const char *fn, uint8_t **buffer) {
80
81 FILE *fp;
82 size_t size = 0;
83
84 if ((fp = fopen(fn, "rb")) != NULL) {
85
86 fseek(fp, 0, SEEK_END);
87 size = ftell(fp);
88 fseek(fp, 0, SEEK_SET);
89
Chris Austen41a4b312015-10-25 03:45:42 -050090 *buffer = new uint8_t [size];
91
92 fread(*buffer, 1, size, fp);
93 fclose(fp);
94 }
95
96 return size;
97}
98
99
100const char *create_esel_severity(const uint8_t *buffer) {
101
102 uint8_t severity;
103 // Dive in to the IBM log to find the severity
104 severity = (0xF0 & buffer[0x4A]);
105
106 return sev_lookup(severity);
107}
108
109int create_esel_association(const uint8_t *buffer, char **m) {
110
111 ipmi_add_sel_request_t *p;
112 dbus_interface_t dbusint;
113 uint8_t sensor;
114
115 p = ( ipmi_add_sel_request_t *) buffer;
116
117 sensor = p->sensornumber;
118
119 find_openbmc_path("SENSOR", sensor, &dbusint);
120
Chris Austen6dc2e912016-02-20 01:00:06 -0600121 // Simply no associations if the sensor can not be found
Chris Austen41a4b312015-10-25 03:45:42 -0500122 if (strlen(dbusint.path) < 1) {
Chris Austen6dc2e912016-02-20 01:00:06 -0600123 printf("Sensor 0x%x not found\n", sensor);
124 memset(dbusint.path,0,sizeof(dbusint.path));
Chris Austen41a4b312015-10-25 03:45:42 -0500125 }
126
127 asprintf(m, "%s", dbusint.path);
128
129 return 0;
130}
131
132
133
134int create_esel_description(const uint8_t *buffer, const char *sev, char **message) {
135
136
137 ipmi_add_sel_request_t *p;
Chris Austen41a4b312015-10-25 03:45:42 -0500138 char *m;
139
140 p = ( ipmi_add_sel_request_t *) buffer;
141
142 find_sensor_type_string(p->sensornumber,&m);
143
144 asprintf(message, "A %s has experienced a %s", m, sev );
145
146 free(m);
147
148 return 0;
149}
150
151
152int send_esel_to_dbus(const char *desc, const char *sev, const char *details, uint8_t *debug, size_t debuglen) {
153
154 sd_bus *mbus = NULL;
155 sd_bus_error error = SD_BUS_ERROR_NULL;
156 sd_bus_message *reply = NULL, *m=NULL;
Chris Austen169395e2015-12-02 20:56:15 -0600157 uint16_t x;
Chris Austen41a4b312015-10-25 03:45:42 -0500158 int r;
159
Chris Austen30195fa2015-11-13 14:39:19 -0600160 mbus = ipmid_get_sd_bus_connection();
Chris Austen41a4b312015-10-25 03:45:42 -0500161
Chris Austen41a4b312015-10-25 03:45:42 -0500162 r = sd_bus_message_new_method_call(mbus,&m,
163 "org.openbmc.records.events",
164 "/org/openbmc/records/events",
165 "org.openbmc.recordlog",
166 "acceptHostMessage");
167 if (r < 0) {
168 fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600169 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500170 }
171
172 r = sd_bus_message_append(m, "sss", desc, sev, details);
173 if (r < 0) {
174 fprintf(stderr, "Failed add the message strings : %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600175 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500176 }
177
178 r = sd_bus_message_append_array(m, 'y', debug, debuglen);
179 if (r < 0) {
180 fprintf(stderr, "Failed to add the raw array of bytes: %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600181 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500182 }
Chris Austen41a4b312015-10-25 03:45:42 -0500183 // Call the IPMI responder on the bus so the message can be sent to the CEC
184 r = sd_bus_call(mbus, m, 0, &error, &reply);
185 if (r < 0) {
Chris Austen169395e2015-12-02 20:56:15 -0600186 fprintf(stderr, "Failed to call the method: %s %s\n", __FUNCTION__, strerror(-r));
187 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500188 }
Chris Austen169395e2015-12-02 20:56:15 -0600189 r = sd_bus_message_read(reply, "q", &x);
Chris Austen41a4b312015-10-25 03:45:42 -0500190 if (r < 0) {
Chris Austen7cc33322015-11-11 00:20:22 -0600191 fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r));
Chris Austen41a4b312015-10-25 03:45:42 -0500192 }
193
Chris Austen169395e2015-12-02 20:56:15 -0600194finish:
Chris Austen41a4b312015-10-25 03:45:42 -0500195 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600196 m = sd_bus_message_unref(m);
197 reply = sd_bus_message_unref(reply);
Chris Austen41a4b312015-10-25 03:45:42 -0500198 return r;
Chris Austen41a4b312015-10-25 03:45:42 -0500199}
200
201
202void send_esel(uint16_t recordid) {
Joel Stanleyf19539e2015-11-25 17:24:05 +1030203 char *desc, *assoc;
Chris Austen41a4b312015-10-25 03:45:42 -0500204 const char *sev;
205 uint8_t *buffer = NULL;
Chris Austen6dc2e912016-02-20 01:00:06 -0600206 const char *path = "/tmp/esel";
Chris Austen41a4b312015-10-25 03:45:42 -0500207 size_t sz;
208
Chris Austen41a4b312015-10-25 03:45:42 -0500209 sz = getfilestream(path, &buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500210 if (sz == 0) {
211 printf("Error file does not exist %d\n",__LINE__);
Chris Austen41a4b312015-10-25 03:45:42 -0500212 return;
213 }
214
Chris Austen41a4b312015-10-25 03:45:42 -0500215 sev = create_esel_severity(buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500216 create_esel_association(buffer, &assoc);
Chris Austen41a4b312015-10-25 03:45:42 -0500217 create_esel_description(buffer, sev, &desc);
218
Chris Austen6dc2e912016-02-20 01:00:06 -0600219 send_esel_to_dbus(desc, sev, assoc, buffer, sz);
Chris Austen41a4b312015-10-25 03:45:42 -0500220
Chris Austen41a4b312015-10-25 03:45:42 -0500221 free(assoc);
222 free(desc);
Chris Austen41a4b312015-10-25 03:45:42 -0500223 delete[] buffer;
224
Chris Austen41a4b312015-10-25 03:45:42 -0500225 return;
226}