blob: 9b19c7ef75cf072e4181fcdadb046130d839e709 [file] [log] [blame]
vishwabmcba0bd5f2015-09-30 16:50:23 +05301#include <stdio.h>
2#include <dlfcn.h>
3#include <iostream>
4#include <unistd.h>
5#include <assert.h>
6#include <dirent.h>
Chris Austen0ba649e2015-10-13 12:28:13 -05007#include <systemd/sd-bus.h>
vishwabmcba0bd5f2015-09-30 16:50:23 +05308#include <string.h>
9#include <stdlib.h>
10#include <map>
11#include "ipmid.H"
Chris Austen0ba649e2015-10-13 12:28:13 -050012#include <sys/time.h>
13#include <errno.h>
Chris Austen0012e9b2015-10-22 01:37:46 -050014#include "sensorhandler.h"
Chris Austen0ba649e2015-10-13 12:28:13 -050015
16
17sd_bus *bus = NULL;
Chris Austen41a4b312015-10-25 03:45:42 -050018FILE *ipmiio, *ipmidbus, *ipmicmddetails;
vishwabmcba0bd5f2015-09-30 16:50:23 +053019
Chris Austen99497312015-10-22 13:00:16 -050020void 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
vishwabmcba0bd5f2015-09-30 16:50:23 +053030// Channel that is used for OpenBMC Barreleye
31const char * DBUS_NAME = "org.openbmc.HostIpmi";
32const char * OBJ_NAME = "/org/openbmc/HostIpmi/1";
33
Chris Austen0ba649e2015-10-13 12:28:13 -050034const char * FILTER = "type='signal',sender='org.openbmc.HostIpmi',member='ReceivedMessage'";
35
36
vishwabmcba0bd5f2015-09-30 16:50:23 +053037typedef std::pair<ipmi_netfn_t, ipmi_cmd_t> ipmi_fn_cmd_t;
38typedef 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.
41std::map<ipmi_fn_cmd_t, ipmi_fn_context_t> g_ipmid_router_map;
42
Chris Austen0ba649e2015-10-13 12:28:13 -050043
44
45#ifndef HEXDUMP_COLS
46#define HEXDUMP_COLS 16
47#endif
48
Chris Austen99497312015-10-22 13:00:16 -050049void hexdump(FILE *s, void *mem, size_t len)
Chris Austen0ba649e2015-10-13 12:28:13 -050050{
51 unsigned int i, j;
Chris Austen120f7322015-10-14 23:27:31 -050052
Chris Austen0ba649e2015-10-13 12:28:13 -050053 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 Austen99497312015-10-22 13:00:16 -050058 fprintf(s,"0x%06x: ", i);
Chris Austen0ba649e2015-10-13 12:28:13 -050059 }
Chris Austen120f7322015-10-14 23:27:31 -050060
Chris Austen0ba649e2015-10-13 12:28:13 -050061 /* print hex data */
62 if(i < len)
63 {
Chris Austen99497312015-10-22 13:00:16 -050064 fprintf(s,"%02x ", 0xFF & ((char*)mem)[i]);
Chris Austen0ba649e2015-10-13 12:28:13 -050065 }
66 else /* end of block, just aligning for ASCII dump */
67 {
Chris Austen99497312015-10-22 13:00:16 -050068 fprintf(s," ");
Chris Austen0ba649e2015-10-13 12:28:13 -050069 }
Chris Austen120f7322015-10-14 23:27:31 -050070
Chris Austen0ba649e2015-10-13 12:28:13 -050071 /* 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 Austen99497312015-10-22 13:00:16 -050078 fputc(' ', s);
Chris Austen0ba649e2015-10-13 12:28:13 -050079 }
80 else if(isprint(((char*)mem)[j])) /* printable char */
81 {
Chris Austen99497312015-10-22 13:00:16 -050082 fputc(0xFF & ((char*)mem)[j], s);
Chris Austen0ba649e2015-10-13 12:28:13 -050083 }
84 else /* other char */
85 {
Chris Austen99497312015-10-22 13:00:16 -050086 fputc('.',s);
Chris Austen0ba649e2015-10-13 12:28:13 -050087 }
88 }
Chris Austen99497312015-10-22 13:00:16 -050089 fputc('\n',s);
Chris Austen0ba649e2015-10-13 12:28:13 -050090 }
91 }
92}
93
94
vishwabmcba0bd5f2015-09-30 16:50:23 +053095// Method that gets called by shared libraries to get their command handlers registered
96void 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.
121ipmi_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 Austen99497312015-10-22 13:00:16 -0500132 fprintf(stderr, "No registered handlers for NetFn:[0x%X], Cmd:[0x%X]"
vishwabmcba0bd5f2015-09-30 16:50:23 +0530133 " 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 Austen99497312015-10-22 13:00:16 -0500140 fprintf(stderr, "No Registered handlers for NetFn:[0x%X],Cmd:[0x%X]\n",netfn, IPMI_CMD_WILDCARD);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530141
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 Austen120f7322015-10-14 23:27:31 -0500165
vishwabmcba0bd5f2015-09-30 16:50:23 +0530166 // 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 Austen120f7322015-10-14 23:27:31 -0500169
vishwabmcba0bd5f2015-09-30 16:50:23 +0530170 // Data length is now actual data + completion code.
171 *data_len = *data_len + IPMI_CC_LEN;
172
173 return rc;
174}
175
vishwabmcba0bd5f2015-09-30 16:50:23 +0530176
vishwabmcba0bd5f2015-09-30 16:50:23 +0530177
vishwabmcba0bd5f2015-09-30 16:50:23 +0530178
Chris Austen0ba649e2015-10-13 12:28:13 -0500179static int send_ipmi_message(unsigned char seq, unsigned char netfn, unsigned char cmd, unsigned char *buf, unsigned char len) {
vishwabmcba0bd5f2015-09-30 16:50:23 +0530180
Chris Austen0ba649e2015-10-13 12:28:13 -0500181 sd_bus_error error = SD_BUS_ERROR_NULL;
182 sd_bus_message *reply = NULL, *m=NULL;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530183
vishwabmcba0bd5f2015-09-30 16:50:23 +0530184
Chris Austen0ba649e2015-10-13 12:28:13 -0500185 const char *path;
186 int r, pty;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530187
vishwabmcba0bd5f2015-09-30 16:50:23 +0530188
Chris Austen0ba649e2015-10-13 12:28:13 -0500189 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;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530193 }
194
vishwabmcba0bd5f2015-09-30 16:50:23 +0530195
Chris Austenabfb5e82015-10-13 12:29:24 -0500196 // Responses in IPMI require a bit set. So there ya go...
197 netfn |= 0x04;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530198
Chris Austen0ba649e2015-10-13 12:28:13 -0500199
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 Austen120f7322015-10-14 23:27:31 -0500206
Chris Austen0ba649e2015-10-13 12:28:13 -0500207 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 Austen0ba649e2015-10-13 12:28:13 -0500223 if (r < 0) {
224 fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r));
225
226 }
227
Chris Austen0ba649e2015-10-13 12:28:13 -0500228 sd_bus_error_free(&error);
229 sd_bus_message_unref(m);
230
231
Chris Austen0ba649e2015-10-13 12:28:13 -0500232 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Chris Austen0ba649e2015-10-13 12:28:13 -0500233}
234
235static 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 Austen0ba649e2015-10-13 12:28:13 -0500245 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 Austen99497312015-10-22 13:00:16 -0500259 fprintf(ipmiio, "IPMI Incoming: Seq 0x%02x, NetFn 0x%02x, CMD: 0x%02x \n", sequence, netfn, cmd);
260 hexdump(ipmiio, (void*)request, sz);
Chris Austen0ba649e2015-10-13 12:28:13 -0500261
Chris Austen120f7322015-10-14 23:27:31 -0500262 // Allow the length field to be used for both input and output of the
Chris Austen0ba649e2015-10-13 12:28:13 -0500263 // ipmi call
264 resplen = sz;
265
Chris Austen120f7322015-10-14 23:27:31 -0500266 // Now that we have parsed the entire byte array from the caller
vishwabmcba0bd5f2015-09-30 16:50:23 +0530267 // we can call the ipmi router to do the work...
Chris Austen0ba649e2015-10-13 12:28:13 -0500268 r = ipmi_netfn_router(netfn, cmd, (void *)request, (void *)response, &resplen);
269 if(r != 0)
vishwabmcba0bd5f2015-09-30 16:50:23 +0530270 {
Chris Austen0ba649e2015-10-13 12:28:13 -0500271 fprintf(stderr,"ERROR:[0x%X] handling NetFn:[0x%X], Cmd:[0x%X]\n",r, netfn, cmd);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530272 }
273
Chris Austen99497312015-10-22 13:00:16 -0500274 fprintf(ipmiio, "IPMI Response:\n");
275 hexdump(ipmiio, (void*)response, resplen);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530276
Chris Austen0ba649e2015-10-13 12:28:13 -0500277 // 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;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530282 }
283
vishwabmcba0bd5f2015-09-30 16:50:23 +0530284
Chris Austen0ba649e2015-10-13 12:28:13 -0500285 return 0;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530286}
287
Chris Austen0ba649e2015-10-13 12:28:13 -0500288
vishwabmcba0bd5f2015-09-30 16:50:23 +0530289//----------------------------------------------------------------------
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//----------------------------------------------------------------------
294int 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 Austen120f7322015-10-14 23:27:31 -0500304 // But being careful here. Get the base address of the string, move
vishwabmcba0bd5f2015-09-30 16:50:23 +0530305 // 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 Austen120f7322015-10-14 23:27:31 -0500316// register a callback handler
vishwabmcba0bd5f2015-09-30 16:50:23 +0530317void 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 Austen120f7322015-10-14 23:27:31 -0500335 // 3: For each one of them, just do a 'dlopen' so that they register
vishwabmcba0bd5f2015-09-30 16:50:23 +0530336 // the handlers for callback routines.
337
338 std::string handler_fqdn = ipmi_lib_path;
Chris Austen120f7322015-10-14 23:27:31 -0500339
vishwabmcba0bd5f2015-09-30 16:50:23 +0530340 // 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 Austen54030262015-10-13 12:30:46 -0500347 handler_fqdn = ipmi_lib_path;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530348 handler_fqdn += handler_list[num_handlers]->d_name;
Chris Austen54030262015-10-13 12:30:46 -0500349 printf("Registering handler:[%s]\n",handler_fqdn.c_str());
350
vishwabmcba0bd5f2015-09-30 16:50:23 +0530351 lib_handler = dlopen(handler_fqdn.c_str(), RTLD_NOW);
352 if(lib_handler == NULL)
353 {
Chris Austen120f7322015-10-14 23:27:31 -0500354 fprintf(stderr,"ERROR opening [%s]: %s\n",
355 handler_fqdn.c_str(), dlerror());
vishwabmcba0bd5f2015-09-30 16:50:23 +0530356 }
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
368int main(int argc, char *argv[])
369{
Chris Austen0ba649e2015-10-13 12:28:13 -0500370 sd_bus_slot *slot = NULL;
371 int r;
372 char *mode = NULL;
Chris Austen99497312015-10-22 13:00:16 -0500373 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 Austen0ba649e2015-10-13 12:28:13 -0500401
402
vishwabmcba0bd5f2015-09-30 16:50:23 +0530403 // Register all the handlers that provider implementation to IPMI commands.
404 ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH);
405
Chris Austen0ba649e2015-10-13 12:28:13 -0500406 /* 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 }
vishwabmcba0bd5f2015-09-30 16:50:23 +0530413
Chris Austen0ba649e2015-10-13 12:28:13 -0500414 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 }
vishwabmcba0bd5f2015-09-30 16:50:23 +0530419
vishwabmcba0bd5f2015-09-30 16:50:23 +0530420
Chris Austen0ba649e2015-10-13 12:28:13 -0500421 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
440finish:
441 sd_bus_slot_unref(slot);
442 sd_bus_unref(bus);
443 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
444
vishwabmcba0bd5f2015-09-30 16:50:23 +0530445}
Chris Austenb071c702015-10-15 00:00:09 -0500446
Chris Austen0012e9b2015-10-22 01:37:46 -0500447// 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
450int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
Chris Austenb071c702015-10-15 00:00:09 -0500451
Chris Austen0012e9b2015-10-22 01:37:46 -0500452 char *str1, *str2, *str3;
453 sd_bus_error error = SD_BUS_ERROR_NULL;
454 sd_bus_message *reply = NULL, *m=NULL;
Chris Austenb071c702015-10-15 00:00:09 -0500455
456
Chris Austen0012e9b2015-10-22 01:37:46 -0500457 int r;
458
Chris Austen0012e9b2015-10-22 01:37:46 -0500459 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
487final:
488
489 sd_bus_error_free(&error);
490 sd_bus_message_unref(m);
491
492 return r;
493}
Chris Austenb071c702015-10-15 00:00:09 -0500494
Chris Austen41a4b312015-10-25 03:45:42 -0500495
Chris Austenb071c702015-10-15 00:00:09 -0500496// 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
499int 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 Austenb071c702015-10-15 00:00:09 -0500541final:
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.
558int 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 Austen99497312015-10-22 13:00:16 -0500566 fprintf(ipmidbus, "Attempting to set a dbus Sensor 0x%02x via %s with a value of %s\n",
Chris Austenb071c702015-10-15 00:00:09 -0500567 number, method, value);
568
569 r = find_openbmc_path("SENSOR", number, &a);
570
Chris Austenb071c702015-10-15 00:00:09 -0500571 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 Austenb071c702015-10-15 00:00:09 -0500581 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 Austenb071c702015-10-15 00:00:09 -0500586 sd_bus_error_free(&error);
587 sd_bus_message_unref(m);
588
589 return 0;
Chris Austenb071c702015-10-15 00:00:09 -0500590}
Chris Austen0130d6e2015-10-15 22:32:36 -0500591
592int 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 Austen99497312015-10-22 13:00:16 -0500600 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
Chris Austen0130d6e2015-10-15 22:32:36 -0500601 number, method, value);
602
603 r = find_openbmc_path("SENSOR", number, &a);
604
Chris Austen0130d6e2015-10-15 22:32:36 -0500605 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 Austen0130d6e2015-10-15 22:32:36 -0500616 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 Austen0130d6e2015-10-15 22:32:36 -0500622 sd_bus_error_free(&error);
623 sd_bus_message_unref(m);
624
625 return 0;
626}