vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <dlfcn.h> |
| 3 | #include <iostream> |
| 4 | #include <unistd.h> |
| 5 | #include <assert.h> |
| 6 | #include <dirent.h> |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 7 | #include <systemd/sd-bus.h> |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 8 | #include <string.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <map> |
| 11 | #include "ipmid.H" |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 12 | #include <sys/time.h> |
| 13 | #include <errno.h> |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 14 | #include "sensorhandler.h" |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 15 | |
| 16 | |
| 17 | sd_bus *bus = NULL; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 18 | FILE *ipmiio, *ipmidbus, *ipmicmddetails; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 19 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 20 | void print_usage(void) { |
| 21 | fprintf(stderr, "Options: [-d mask]\n"); |
| 22 | fprintf(stderr, " mask : 0x01 - Print ipmi packets\n"); |
| 23 | fprintf(stderr, " mask : 0x02 - Print DBUS operations\n"); |
| 24 | fprintf(stderr, " mask : 0x04 - Print ipmi command details\n"); |
| 25 | fprintf(stderr, " mask : 0xFF - Print all trace\n"); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 30 | // Channel that is used for OpenBMC Barreleye |
| 31 | const char * DBUS_NAME = "org.openbmc.HostIpmi"; |
| 32 | const char * OBJ_NAME = "/org/openbmc/HostIpmi/1"; |
| 33 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 34 | const char * FILTER = "type='signal',sender='org.openbmc.HostIpmi',member='ReceivedMessage'"; |
| 35 | |
| 36 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 37 | typedef std::pair<ipmi_netfn_t, ipmi_cmd_t> ipmi_fn_cmd_t; |
| 38 | typedef std::pair<ipmid_callback_t, ipmi_context_t> ipmi_fn_context_t; |
| 39 | |
| 40 | // Global data structure that contains the IPMI command handler's registrations. |
| 41 | std::map<ipmi_fn_cmd_t, ipmi_fn_context_t> g_ipmid_router_map; |
| 42 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 43 | |
| 44 | |
| 45 | #ifndef HEXDUMP_COLS |
| 46 | #define HEXDUMP_COLS 16 |
| 47 | #endif |
| 48 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 49 | void hexdump(FILE *s, void *mem, size_t len) |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 50 | { |
| 51 | unsigned int i, j; |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 52 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 53 | for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++) |
| 54 | { |
| 55 | /* print offset */ |
| 56 | if(i % HEXDUMP_COLS == 0) |
| 57 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 58 | fprintf(s,"0x%06x: ", i); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 59 | } |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 60 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 61 | /* print hex data */ |
| 62 | if(i < len) |
| 63 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 64 | fprintf(s,"%02x ", 0xFF & ((char*)mem)[i]); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 65 | } |
| 66 | else /* end of block, just aligning for ASCII dump */ |
| 67 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 68 | fprintf(s," "); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 69 | } |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 70 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 71 | /* print ASCII dump */ |
| 72 | if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1)) |
| 73 | { |
| 74 | for(j = i - (HEXDUMP_COLS - 1); j <= i; j++) |
| 75 | { |
| 76 | if(j >= len) /* end of block, not really printing */ |
| 77 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 78 | fputc(' ', s); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 79 | } |
| 80 | else if(isprint(((char*)mem)[j])) /* printable char */ |
| 81 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 82 | fputc(0xFF & ((char*)mem)[j], s); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 83 | } |
| 84 | else /* other char */ |
| 85 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 86 | fputc('.',s); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 87 | } |
| 88 | } |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 89 | fputc('\n',s); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 95 | // Method that gets called by shared libraries to get their command handlers registered |
| 96 | void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 97 | ipmi_context_t context, ipmid_callback_t handler) |
| 98 | { |
| 99 | // Pack NetFn and Command in one. |
| 100 | auto netfn_and_cmd = std::make_pair(netfn, cmd); |
| 101 | |
| 102 | // Pack Function handler and Data in another. |
| 103 | auto handler_and_context = std::make_pair(handler, context); |
| 104 | |
| 105 | // Check if the registration has already been made.. |
| 106 | auto iter = g_ipmid_router_map.find(netfn_and_cmd); |
| 107 | if(iter != g_ipmid_router_map.end()) |
| 108 | { |
| 109 | fprintf(stderr,"ERROR : Duplicate registration for NetFn [0x%X], Cmd:[0x%X]\n",netfn, cmd); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // This is a fresh registration.. Add it to the map. |
| 114 | g_ipmid_router_map.emplace(netfn_and_cmd, handler_and_context); |
| 115 | } |
| 116 | |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // Looks at the map and calls corresponding handler functions. |
| 121 | ipmi_ret_t ipmi_netfn_router(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request, |
| 122 | ipmi_response_t response, ipmi_data_len_t data_len) |
| 123 | { |
| 124 | // return from the Command handlers. |
| 125 | ipmi_ret_t rc = IPMI_CC_INVALID; |
| 126 | |
| 127 | // Walk the map that has the registered handlers and invoke the approprite |
| 128 | // handlers for matching commands. |
| 129 | auto iter = g_ipmid_router_map.find(std::make_pair(netfn, cmd)); |
| 130 | if(iter == g_ipmid_router_map.end()) |
| 131 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 132 | fprintf(stderr, "No registered handlers for NetFn:[0x%X], Cmd:[0x%X]" |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 133 | " trying Wilcard implementation \n",netfn, cmd); |
| 134 | |
| 135 | // Now that we did not find any specific [NetFn,Cmd], tuple, check for |
| 136 | // NetFn, WildCard command present. |
| 137 | iter = g_ipmid_router_map.find(std::make_pair(netfn, IPMI_CMD_WILDCARD)); |
| 138 | if(iter == g_ipmid_router_map.end()) |
| 139 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 140 | fprintf(stderr, "No Registered handlers for NetFn:[0x%X],Cmd:[0x%X]\n",netfn, IPMI_CMD_WILDCARD); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 141 | |
| 142 | // Respond with a 0xC1 |
| 143 | memcpy(response, &rc, IPMI_CC_LEN); |
| 144 | *data_len = IPMI_CC_LEN; |
| 145 | return rc; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | #ifdef __IPMI_DEBUG__ |
| 150 | // We have either a perfect match -OR- a wild card atleast, |
| 151 | printf("Calling Net function:[0x%X], Command:[0x%X]\n", netfn, cmd); |
| 152 | #endif |
| 153 | |
| 154 | // Extract the map data onto appropriate containers |
| 155 | auto handler_and_context = iter->second; |
| 156 | |
| 157 | // Creating a pointer type casted to char* to make sure we advance 1 byte |
| 158 | // when we advance pointer to next's address. advancing void * would not |
| 159 | // make sense. |
| 160 | char *respo = &((char *)response)[IPMI_CC_LEN]; |
| 161 | |
| 162 | // Response message from the plugin goes into a byte post the base response |
| 163 | rc = (handler_and_context.first) (netfn, cmd, request, respo, |
| 164 | data_len, handler_and_context.second); |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 165 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 166 | // Now copy the return code that we got from handler and pack it in first |
| 167 | // byte. |
| 168 | memcpy(response, &rc, IPMI_CC_LEN); |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 169 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 170 | // Data length is now actual data + completion code. |
| 171 | *data_len = *data_len + IPMI_CC_LEN; |
| 172 | |
| 173 | return rc; |
| 174 | } |
| 175 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 176 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 177 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 178 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 179 | static int send_ipmi_message(unsigned char seq, unsigned char netfn, unsigned char cmd, unsigned char *buf, unsigned char len) { |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 180 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 181 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 182 | sd_bus_message *reply = NULL, *m=NULL; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 183 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 184 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 185 | const char *path; |
| 186 | int r, pty; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 187 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 188 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 189 | r = sd_bus_message_new_method_call(bus,&m,DBUS_NAME,OBJ_NAME,DBUS_NAME,"sendMessage"); |
| 190 | if (r < 0) { |
| 191 | fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r)); |
| 192 | return -1; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 193 | } |
| 194 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 195 | |
Chris Austen | abfb5e8 | 2015-10-13 12:29:24 -0500 | [diff] [blame] | 196 | // Responses in IPMI require a bit set. So there ya go... |
| 197 | netfn |= 0x04; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 198 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 199 | |
| 200 | // Add the bytes needed for the methods to be called |
| 201 | r = sd_bus_message_append(m, "yyy", seq, netfn, cmd); |
| 202 | if (r < 0) { |
| 203 | fprintf(stderr, "Failed add the netfn and others : %s\n", strerror(-r)); |
| 204 | return -1; |
| 205 | } |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 206 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 207 | r = sd_bus_message_append_array(m, 'y', buf, len); |
| 208 | if (r < 0) { |
| 209 | fprintf(stderr, "Failed to add the string of response bytes: %s\n", strerror(-r)); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | |
| 215 | // Call the IPMI responder on the bus so the message can be sent to the CEC |
| 216 | r = sd_bus_call(bus, m, 0, &error, &reply); |
| 217 | if (r < 0) { |
| 218 | fprintf(stderr, "Failed to call the method: %s", strerror(-r)); |
| 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | r = sd_bus_message_read(reply, "x", &pty); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 223 | if (r < 0) { |
| 224 | fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r)); |
| 225 | |
| 226 | } |
| 227 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 228 | sd_bus_error_free(&error); |
| 229 | sd_bus_message_unref(m); |
| 230 | |
| 231 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 232 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static int handle_ipmi_command(sd_bus_message *m, void *user_data, sd_bus_error |
| 236 | *ret_error) { |
| 237 | int r = 0; |
| 238 | const char *msg = NULL; |
| 239 | char sequence, netfn, cmd; |
| 240 | const void *request; |
| 241 | size_t sz; |
| 242 | size_t resplen =MAX_IPMI_BUFFER; |
| 243 | unsigned char response[MAX_IPMI_BUFFER]; |
| 244 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 245 | memset(response, 0, MAX_IPMI_BUFFER); |
| 246 | |
| 247 | r = sd_bus_message_read(m, "yyy", &sequence, &netfn, &cmd); |
| 248 | if (r < 0) { |
| 249 | fprintf(stderr, "Failed to parse signal message: %s\n", strerror(-r)); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | r = sd_bus_message_read_array(m, 'y', &request, &sz ); |
| 254 | if (r < 0) { |
| 255 | fprintf(stderr, "Failed to parse signal message: %s\n", strerror(-r)); |
| 256 | return -1; |
| 257 | } |
| 258 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 259 | fprintf(ipmiio, "IPMI Incoming: Seq 0x%02x, NetFn 0x%02x, CMD: 0x%02x \n", sequence, netfn, cmd); |
| 260 | hexdump(ipmiio, (void*)request, sz); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 261 | |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 262 | // Allow the length field to be used for both input and output of the |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 263 | // ipmi call |
| 264 | resplen = sz; |
| 265 | |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 266 | // Now that we have parsed the entire byte array from the caller |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 267 | // we can call the ipmi router to do the work... |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 268 | r = ipmi_netfn_router(netfn, cmd, (void *)request, (void *)response, &resplen); |
| 269 | if(r != 0) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 270 | { |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 271 | fprintf(stderr,"ERROR:[0x%X] handling NetFn:[0x%X], Cmd:[0x%X]\n",r, netfn, cmd); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 272 | } |
| 273 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 274 | fprintf(ipmiio, "IPMI Response:\n"); |
| 275 | hexdump(ipmiio, (void*)response, resplen); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 276 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 277 | // Send the response buffer from the ipmi command |
| 278 | r = send_ipmi_message(sequence, netfn, cmd, response, resplen); |
| 279 | if (r < 0) { |
| 280 | fprintf(stderr, "Failed to send the response message\n"); |
| 281 | return -1; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 282 | } |
| 283 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 284 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 285 | return 0; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 286 | } |
| 287 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 288 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 289 | //---------------------------------------------------------------------- |
| 290 | // handler_select |
| 291 | // Select all the files ending with with .so. in the given diretcory |
| 292 | // @d: dirent structure containing the file name |
| 293 | //---------------------------------------------------------------------- |
| 294 | int handler_select(const struct dirent *entry) |
| 295 | { |
| 296 | // To hold ".so" from entry->d_name; |
| 297 | char dname_copy[4] = {0}; |
| 298 | |
| 299 | // We want to avoid checking for everything and isolate to the ones having |
| 300 | // .so in them. |
| 301 | if(strstr(entry->d_name, IPMI_PLUGIN_EXTN)) |
| 302 | { |
| 303 | // It is possible that .so could be anywhere in the string but unlikely |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 304 | // But being careful here. Get the base address of the string, move |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 305 | // until end and come back 3 steps and that gets what we need. |
| 306 | strcpy(dname_copy, (entry->d_name + strlen(entry->d_name)-strlen(IPMI_PLUGIN_EXTN))); |
| 307 | if(strcmp(dname_copy, IPMI_PLUGIN_EXTN) == 0) |
| 308 | { |
| 309 | return 1; |
| 310 | } |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | // This will do a dlopen of every .so in ipmi_lib_path and will dlopen everything so that they will |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 316 | // register a callback handler |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 317 | void ipmi_register_callback_handlers(const char* ipmi_lib_path) |
| 318 | { |
| 319 | // For walking the ipmi_lib_path |
| 320 | struct dirent **handler_list; |
| 321 | int num_handlers = 0; |
| 322 | |
| 323 | // This is used to check and abort if someone tries to register a bad one. |
| 324 | void *lib_handler = NULL; |
| 325 | |
| 326 | if(ipmi_lib_path == NULL) |
| 327 | { |
| 328 | fprintf(stderr,"ERROR; No handlers to be registered for ipmi.. Aborting\n"); |
| 329 | assert(0); |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | // 1: Open ipmi_lib_path. Its usually "/usr/lib/phosphor-host-ipmid" |
| 334 | // 2: Scan the directory for the files that end with .so |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 335 | // 3: For each one of them, just do a 'dlopen' so that they register |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 336 | // the handlers for callback routines. |
| 337 | |
| 338 | std::string handler_fqdn = ipmi_lib_path; |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 339 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 340 | // Append a "/" since we need to add the name of the .so. If there is |
| 341 | // already a .so, adding one more is not any harm. |
| 342 | handler_fqdn += "/"; |
| 343 | |
| 344 | num_handlers = scandir(ipmi_lib_path, &handler_list, handler_select, alphasort); |
| 345 | while(num_handlers--) |
| 346 | { |
Chris Austen | 5403026 | 2015-10-13 12:30:46 -0500 | [diff] [blame] | 347 | handler_fqdn = ipmi_lib_path; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 348 | handler_fqdn += handler_list[num_handlers]->d_name; |
Chris Austen | 5403026 | 2015-10-13 12:30:46 -0500 | [diff] [blame] | 349 | printf("Registering handler:[%s]\n",handler_fqdn.c_str()); |
| 350 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 351 | lib_handler = dlopen(handler_fqdn.c_str(), RTLD_NOW); |
| 352 | if(lib_handler == NULL) |
| 353 | { |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 354 | fprintf(stderr,"ERROR opening [%s]: %s\n", |
| 355 | handler_fqdn.c_str(), dlerror()); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 356 | } |
| 357 | // Wipe the memory allocated for this particular entry. |
| 358 | free(handler_list[num_handlers]); |
| 359 | } |
| 360 | // Done with all registration. |
| 361 | free(handler_list); |
| 362 | } |
| 363 | |
| 364 | // TODO : What to be done on the memory that is given by dlopen ?. |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | int main(int argc, char *argv[]) |
| 369 | { |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 370 | sd_bus_slot *slot = NULL; |
| 371 | int r; |
| 372 | char *mode = NULL; |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 373 | unsigned long tvalue; |
| 374 | int c; |
| 375 | |
| 376 | |
| 377 | |
| 378 | // This file and subsequient switch is for turning on levels |
| 379 | // of trace |
| 380 | ipmicmddetails = ipmiio = ipmidbus = fopen("/dev/null", "w"); |
| 381 | |
| 382 | while ((c = getopt (argc, argv, "h:d:")) != -1) |
| 383 | switch (c) { |
| 384 | case 'd': |
| 385 | tvalue = strtoul(optarg, NULL, 16); |
| 386 | if (1&tvalue) { |
| 387 | ipmiio = stdout; |
| 388 | } |
| 389 | if (2&tvalue) { |
| 390 | ipmidbus = stdout; |
| 391 | } |
| 392 | if (4&tvalue) { |
| 393 | ipmicmddetails = stdout; |
| 394 | } |
| 395 | break; |
| 396 | case 'h': |
| 397 | case '?': |
| 398 | print_usage(); |
| 399 | return 1; |
| 400 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 401 | |
| 402 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 403 | // Register all the handlers that provider implementation to IPMI commands. |
| 404 | ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH); |
| 405 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 406 | /* Connect to system bus */ |
| 407 | r = sd_bus_open_system(&bus); |
| 408 | if (r < 0) { |
| 409 | fprintf(stderr, "Failed to connect to system bus: %s\n", |
| 410 | strerror(-r)); |
| 411 | goto finish; |
| 412 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 413 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 414 | r = sd_bus_add_match(bus, &slot, FILTER, handle_ipmi_command, NULL); |
| 415 | if (r < 0) { |
| 416 | fprintf(stderr, "Failed: sd_bus_add_match: %s : %s\n", strerror(-r), FILTER); |
| 417 | goto finish; |
| 418 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 419 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 420 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 421 | for (;;) { |
| 422 | /* Process requests */ |
| 423 | |
| 424 | r = sd_bus_process(bus, NULL); |
| 425 | if (r < 0) { |
| 426 | fprintf(stderr, "Failed to process bus: %s\n", strerror(-r)); |
| 427 | goto finish; |
| 428 | } |
| 429 | if (r > 0) { |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | r = sd_bus_wait(bus, (uint64_t) - 1); |
| 434 | if (r < 0) { |
| 435 | fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r)); |
| 436 | goto finish; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | finish: |
| 441 | sd_bus_slot_unref(slot); |
| 442 | sd_bus_unref(bus); |
| 443 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
| 444 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 445 | } |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 446 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 447 | // Use a lookup table to find the interface name of a specific sensor |
| 448 | // This will be used until an alternative is found. this is the first |
| 449 | // step for mapping IPMI |
| 450 | int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) { |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 451 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 452 | char *str1, *str2, *str3; |
| 453 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 454 | sd_bus_message *reply = NULL, *m=NULL; |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 455 | |
| 456 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 457 | int r; |
| 458 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 459 | r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get"); |
| 460 | if (r < 0) { |
| 461 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 462 | fprintf(stderr,"Bus: %s Path: %s Interface: %s \n", |
| 463 | interface->bus, interface->path, interface->interface); |
| 464 | } |
| 465 | |
| 466 | r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name); |
| 467 | if (r < 0) { |
| 468 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 469 | fprintf(stderr,"Bus: %s Path: %s Interface: %s \n", |
| 470 | interface->bus, interface->path, interface->interface); |
| 471 | } |
| 472 | |
| 473 | r = sd_bus_call(bus, m, 0, &error, &reply); |
| 474 | if (r < 0) { |
| 475 | fprintf(stderr, "Failed to call the method: %s", strerror(-r)); |
| 476 | goto final; |
| 477 | } |
| 478 | |
| 479 | r = sd_bus_message_read(reply, "v", "s", &str1) ; |
| 480 | if (r < 0) { |
| 481 | fprintf(stderr, "Failed to get a response: %s", strerror(-r)); |
| 482 | goto final; |
| 483 | } |
| 484 | |
| 485 | strcpy(property_value, str1); |
| 486 | |
| 487 | final: |
| 488 | |
| 489 | sd_bus_error_free(&error); |
| 490 | sd_bus_message_unref(m); |
| 491 | |
| 492 | return r; |
| 493 | } |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 494 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 495 | |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 496 | // Use a lookup table to find the interface name of a specific sensor |
| 497 | // This will be used until an alternative is found. this is the first |
| 498 | // step for mapping IPMI |
| 499 | int find_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) { |
| 500 | |
| 501 | const char *busname = "org.openbmc.managers.System"; |
| 502 | const char *objname = "/org/openbmc/managers/System"; |
| 503 | |
| 504 | char *str1, *str2, *str3; |
| 505 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 506 | sd_bus_message *reply = NULL, *m=NULL; |
| 507 | |
| 508 | |
| 509 | int r; |
| 510 | |
| 511 | r = sd_bus_message_new_method_call(bus,&m,busname,objname,busname,"getObjectFromByteId"); |
| 512 | if (r < 0) { |
| 513 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 514 | } |
| 515 | |
| 516 | r = sd_bus_message_append(m, "sy", type, num); |
| 517 | if (r < 0) { |
| 518 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 519 | } |
| 520 | |
| 521 | // Call the IPMI responder on the bus so the message can be sent to the CEC |
| 522 | r = sd_bus_call(bus, m, 0, &error, &reply); |
| 523 | if (r < 0) { |
| 524 | fprintf(stderr, "Failed to call the method: %s", strerror(-r)); |
| 525 | goto final; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3); |
| 530 | if (r < 0) { |
| 531 | fprintf(stderr, "Failed to get a response: %s", strerror(-r)); |
| 532 | goto final; |
| 533 | } |
| 534 | |
| 535 | strncpy(interface->bus, str1, MAX_DBUS_PATH); |
| 536 | strncpy(interface->path, str2, MAX_DBUS_PATH); |
| 537 | strncpy(interface->interface, str3, MAX_DBUS_PATH); |
| 538 | |
| 539 | interface->sensornumber = num; |
| 540 | |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 541 | final: |
| 542 | |
| 543 | sd_bus_error_free(&error); |
| 544 | sd_bus_message_unref(m); |
| 545 | |
| 546 | return r; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | ///////////////////////////////////////////////////////////////////// |
| 551 | // |
| 552 | // Routines used by ipmi commands wanting to interact on the dbus |
| 553 | // |
| 554 | ///////////////////////////////////////////////////////////////////// |
| 555 | |
| 556 | |
| 557 | // Simple set routine because some methods are standard. |
| 558 | int set_sensor_dbus_state(uint8_t number, const char *method, const char *value) { |
| 559 | |
| 560 | |
| 561 | dbus_interface_t a; |
| 562 | int r; |
| 563 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 564 | sd_bus_message *reply = NULL, *m=NULL; |
| 565 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 566 | fprintf(ipmidbus, "Attempting to set a dbus Sensor 0x%02x via %s with a value of %s\n", |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 567 | number, method, value); |
| 568 | |
| 569 | r = find_openbmc_path("SENSOR", number, &a); |
| 570 | |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 571 | r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method); |
| 572 | if (r < 0) { |
| 573 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 574 | } |
| 575 | |
| 576 | r = sd_bus_message_append(m, "s", value); |
| 577 | if (r < 0) { |
| 578 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 579 | } |
| 580 | |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 581 | r = sd_bus_call(bus, m, 0, &error, &reply); |
| 582 | if (r < 0) { |
| 583 | fprintf(stderr, "Failed to call the method: %s", strerror(-r)); |
| 584 | } |
| 585 | |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 586 | sd_bus_error_free(&error); |
| 587 | sd_bus_message_unref(m); |
| 588 | |
| 589 | return 0; |
Chris Austen | b071c70 | 2015-10-15 00:00:09 -0500 | [diff] [blame] | 590 | } |
Chris Austen | 0130d6e | 2015-10-15 22:32:36 -0500 | [diff] [blame] | 591 | |
| 592 | int set_sensor_dbus_state_v(uint8_t number, const char *method, char *value) { |
| 593 | |
| 594 | |
| 595 | dbus_interface_t a; |
| 596 | int r; |
| 597 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 598 | sd_bus_message *reply = NULL, *m=NULL; |
| 599 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 600 | fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n", |
Chris Austen | 0130d6e | 2015-10-15 22:32:36 -0500 | [diff] [blame] | 601 | number, method, value); |
| 602 | |
| 603 | r = find_openbmc_path("SENSOR", number, &a); |
| 604 | |
Chris Austen | 0130d6e | 2015-10-15 22:32:36 -0500 | [diff] [blame] | 605 | r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method); |
| 606 | if (r < 0) { |
| 607 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 608 | } |
| 609 | |
| 610 | r = sd_bus_message_append(m, "v", "s", value); |
| 611 | if (r < 0) { |
| 612 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 613 | } |
| 614 | |
| 615 | |
Chris Austen | 0130d6e | 2015-10-15 22:32:36 -0500 | [diff] [blame] | 616 | r = sd_bus_call(bus, m, 0, &error, NULL); |
| 617 | if (r < 0) { |
| 618 | fprintf(stderr, "12 Failed to call the method: %s", strerror(-r)); |
| 619 | } |
| 620 | |
| 621 | |
Chris Austen | 0130d6e | 2015-10-15 22:32:36 -0500 | [diff] [blame] | 622 | sd_bus_error_free(&error); |
| 623 | sd_bus_message_unref(m); |
| 624 | |
| 625 | return 0; |
| 626 | } |