blob: 3c24ec00ed6b983fb6e7cee5a473218734db2d78 [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
90 size = 0x100;
91
92 *buffer = new uint8_t [size];
93
94 fread(*buffer, 1, size, fp);
95 fclose(fp);
96 }
97
98 return size;
99}
100
101
102const char *create_esel_severity(const uint8_t *buffer) {
103
104 uint8_t severity;
105 // Dive in to the IBM log to find the severity
106 severity = (0xF0 & buffer[0x4A]);
107
108 return sev_lookup(severity);
109}
110
111int create_esel_association(const uint8_t *buffer, char **m) {
112
113 ipmi_add_sel_request_t *p;
114 dbus_interface_t dbusint;
115 uint8_t sensor;
116
117 p = ( ipmi_add_sel_request_t *) buffer;
118
119 sensor = p->sensornumber;
120
121 find_openbmc_path("SENSOR", sensor, &dbusint);
122
123 if (strlen(dbusint.path) < 1) {
124 printf("Sensor not found\n");
125 snprintf(dbusint.path, sizeof(dbusint.path), "0x%x", sensor);
126 }
127
128 asprintf(m, "%s", dbusint.path);
129
130 return 0;
131}
132
133
134
135int create_esel_description(const uint8_t *buffer, const char *sev, char **message) {
136
137
138 ipmi_add_sel_request_t *p;
Chris Austen41a4b312015-10-25 03:45:42 -0500139 char *m;
140
141 p = ( ipmi_add_sel_request_t *) buffer;
142
143 find_sensor_type_string(p->sensornumber,&m);
144
145 asprintf(message, "A %s has experienced a %s", m, sev );
146
147 free(m);
148
149 return 0;
150}
151
152
153int send_esel_to_dbus(const char *desc, const char *sev, const char *details, uint8_t *debug, size_t debuglen) {
154
155 sd_bus *mbus = NULL;
156 sd_bus_error error = SD_BUS_ERROR_NULL;
157 sd_bus_message *reply = NULL, *m=NULL;
Chris Austen169395e2015-12-02 20:56:15 -0600158 uint16_t x;
Chris Austen41a4b312015-10-25 03:45:42 -0500159 int r;
160
Chris Austen30195fa2015-11-13 14:39:19 -0600161 mbus = ipmid_get_sd_bus_connection();
Chris Austen41a4b312015-10-25 03:45:42 -0500162
Chris Austen41a4b312015-10-25 03:45:42 -0500163 r = sd_bus_message_new_method_call(mbus,&m,
164 "org.openbmc.records.events",
165 "/org/openbmc/records/events",
166 "org.openbmc.recordlog",
167 "acceptHostMessage");
168 if (r < 0) {
169 fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600170 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500171 }
172
173 r = sd_bus_message_append(m, "sss", desc, sev, details);
174 if (r < 0) {
175 fprintf(stderr, "Failed add the message strings : %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600176 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500177 }
178
179 r = sd_bus_message_append_array(m, 'y', debug, debuglen);
180 if (r < 0) {
181 fprintf(stderr, "Failed to add the raw array of bytes: %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600182 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500183 }
Chris Austen41a4b312015-10-25 03:45:42 -0500184 // Call the IPMI responder on the bus so the message can be sent to the CEC
185 r = sd_bus_call(mbus, m, 0, &error, &reply);
186 if (r < 0) {
Chris Austen169395e2015-12-02 20:56:15 -0600187 fprintf(stderr, "Failed to call the method: %s %s\n", __FUNCTION__, strerror(-r));
188 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500189 }
Chris Austen169395e2015-12-02 20:56:15 -0600190 r = sd_bus_message_read(reply, "q", &x);
Chris Austen41a4b312015-10-25 03:45:42 -0500191 if (r < 0) {
Chris Austen7cc33322015-11-11 00:20:22 -0600192 fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r));
Chris Austen41a4b312015-10-25 03:45:42 -0500193 }
194
Chris Austen169395e2015-12-02 20:56:15 -0600195finish:
Chris Austen41a4b312015-10-25 03:45:42 -0500196 sd_bus_error_free(&error);
197 sd_bus_message_unref(m);
198 sd_bus_message_unref(reply);
Chris Austen41a4b312015-10-25 03:45:42 -0500199 return r;
Chris Austen41a4b312015-10-25 03:45:42 -0500200}
201
202
203void send_esel(uint16_t recordid) {
Joel Stanleyf19539e2015-11-25 17:24:05 +1030204 char *desc, *assoc;
Chris Austen41a4b312015-10-25 03:45:42 -0500205 const char *sev;
206 uint8_t *buffer = NULL;
Chris Austen169395e2015-12-02 20:56:15 -0600207 char *path;
Chris Austen41a4b312015-10-25 03:45:42 -0500208 size_t sz;
209
210 uint8_t hack[] = {0x30, 0x32, 0x34};
Chris Austen41a4b312015-10-25 03:45:42 -0500211 asprintf(&path,"%s%04x", "/tmp/esel", recordid);
Chris Austen41a4b312015-10-25 03:45:42 -0500212 sz = getfilestream(path, &buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500213 if (sz == 0) {
214 printf("Error file does not exist %d\n",__LINE__);
215 free(path);
216 return;
217 }
218
Chris Austen41a4b312015-10-25 03:45:42 -0500219 sev = create_esel_severity(buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500220 create_esel_association(buffer, &assoc);
Chris Austen41a4b312015-10-25 03:45:42 -0500221 create_esel_description(buffer, sev, &desc);
222
Chris Austen41a4b312015-10-25 03:45:42 -0500223 // TODO until ISSUE https://github.com/openbmc/rest-dbus/issues/2
224 // I cant send extended ascii chars. So 0,2,4 for now...
Chris Austen169395e2015-12-02 20:56:15 -0600225 send_esel_to_dbus(desc, sev, assoc, hack, 3);
Chris Austen41a4b312015-10-25 03:45:42 -0500226
227 free(path);
228 free(assoc);
229 free(desc);
Chris Austen41a4b312015-10-25 03:45:42 -0500230 delete[] buffer;
231
Chris Austen41a4b312015-10-25 03:45:42 -0500232 return;
233}