Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 1 | #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 | |
| 15 | using namespace std; |
| 16 | |
| 17 | extern int find_openbmc_path(const char *, const uint8_t , dbus_interface_t *); |
| 18 | |
| 19 | |
| 20 | ////////////////////////// |
| 21 | struct 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 | |
| 29 | struct severity_values_t { |
| 30 | uint8_t type; |
| 31 | const char *description; |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | const 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 | |
| 45 | const 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 | |
| 54 | int 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 | |
| 79 | size_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 | |
| 102 | const 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 | |
| 111 | int 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 | |
| 135 | int create_esel_description(const uint8_t *buffer, const char *sev, char **message) { |
| 136 | |
| 137 | |
| 138 | ipmi_add_sel_request_t *p; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 139 | 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 | |
| 153 | int 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 158 | uint16_t x; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 159 | int r; |
| 160 | |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 161 | mbus = ipmid_get_sd_bus_connection(); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 162 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 163 | 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 170 | goto finish; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 171 | } |
| 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 176 | goto finish; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 177 | } |
| 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 182 | goto finish; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 183 | } |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 184 | // 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 187 | fprintf(stderr, "Failed to call the method: %s %s\n", __FUNCTION__, strerror(-r)); |
| 188 | goto finish; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 189 | } |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 190 | r = sd_bus_message_read(reply, "q", &x); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 191 | if (r < 0) { |
Chris Austen | 7cc3332 | 2015-11-11 00:20:22 -0600 | [diff] [blame] | 192 | fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r)); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 195 | finish: |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 196 | sd_bus_error_free(&error); |
| 197 | sd_bus_message_unref(m); |
| 198 | sd_bus_message_unref(reply); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 199 | return r; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
| 203 | void send_esel(uint16_t recordid) { |
Joel Stanley | f19539e | 2015-11-25 17:24:05 +1030 | [diff] [blame] | 204 | char *desc, *assoc; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 205 | const char *sev; |
| 206 | uint8_t *buffer = NULL; |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 207 | char *path; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 208 | size_t sz; |
| 209 | |
| 210 | uint8_t hack[] = {0x30, 0x32, 0x34}; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 211 | asprintf(&path,"%s%04x", "/tmp/esel", recordid); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 212 | sz = getfilestream(path, &buffer); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 213 | if (sz == 0) { |
| 214 | printf("Error file does not exist %d\n",__LINE__); |
| 215 | free(path); |
| 216 | return; |
| 217 | } |
| 218 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 219 | sev = create_esel_severity(buffer); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 220 | create_esel_association(buffer, &assoc); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 221 | create_esel_description(buffer, sev, &desc); |
| 222 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 223 | // 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 225 | send_esel_to_dbus(desc, sev, assoc, hack, 3); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 226 | |
| 227 | free(path); |
| 228 | free(assoc); |
| 229 | free(desc); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 230 | delete[] buffer; |
| 231 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 232 | return; |
| 233 | } |