blob: c4a676305acf66dfbf8a700c77496dfa1743c514 [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
Chris Austen0ba649e2015-10-13 12:28:13 -050016sd_bus *bus = NULL;
vishwab9f559a2016-01-13 01:53:08 -060017sd_bus_slot *ipmid_slot = NULL;
Chris Austen30195fa2015-11-13 14:39:19 -060018
Chris Austen41a4b312015-10-25 03:45:42 -050019FILE *ipmiio, *ipmidbus, *ipmicmddetails;
vishwabmcba0bd5f2015-09-30 16:50:23 +053020
Chris Austen99497312015-10-22 13:00:16 -050021void print_usage(void) {
22 fprintf(stderr, "Options: [-d mask]\n");
23 fprintf(stderr, " mask : 0x01 - Print ipmi packets\n");
24 fprintf(stderr, " mask : 0x02 - Print DBUS operations\n");
25 fprintf(stderr, " mask : 0x04 - Print ipmi command details\n");
26 fprintf(stderr, " mask : 0xFF - Print all trace\n");
27}
28
Jeremy Kerre41081f2015-10-27 12:11:36 +080029const char * DBUS_INTF = "org.openbmc.HostIpmi";
vishwabmcba0bd5f2015-09-30 16:50:23 +053030
Jeremy Kerre41081f2015-10-27 12:11:36 +080031const char * FILTER = "type='signal',interface='org.openbmc.HostIpmi',member='ReceivedMessage'";
Chris Austen0ba649e2015-10-13 12:28:13 -050032
33
vishwabmcba0bd5f2015-09-30 16:50:23 +053034typedef std::pair<ipmi_netfn_t, ipmi_cmd_t> ipmi_fn_cmd_t;
35typedef std::pair<ipmid_callback_t, ipmi_context_t> ipmi_fn_context_t;
36
37// Global data structure that contains the IPMI command handler's registrations.
38std::map<ipmi_fn_cmd_t, ipmi_fn_context_t> g_ipmid_router_map;
39
Nan Li36c0cb62016-03-31 11:16:08 +080040// IPMI Spec, shared Reservation ID.
41unsigned short g_sel_reserve = 0xFFFF;
42
43unsigned short get_sel_reserve_id(void)
44{
45 return g_sel_reserve;
46}
Chris Austen0ba649e2015-10-13 12:28:13 -050047
Chris Austen0ba649e2015-10-13 12:28:13 -050048#ifndef HEXDUMP_COLS
49#define HEXDUMP_COLS 16
50#endif
51
Chris Austen99497312015-10-22 13:00:16 -050052void hexdump(FILE *s, void *mem, size_t len)
Chris Austen0ba649e2015-10-13 12:28:13 -050053{
54 unsigned int i, j;
Chris Austen120f7322015-10-14 23:27:31 -050055
Chris Austen0ba649e2015-10-13 12:28:13 -050056 for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
57 {
58 /* print offset */
59 if(i % HEXDUMP_COLS == 0)
60 {
Chris Austen99497312015-10-22 13:00:16 -050061 fprintf(s,"0x%06x: ", i);
Chris Austen0ba649e2015-10-13 12:28:13 -050062 }
Chris Austen120f7322015-10-14 23:27:31 -050063
Chris Austen0ba649e2015-10-13 12:28:13 -050064 /* print hex data */
65 if(i < len)
66 {
Chris Austen99497312015-10-22 13:00:16 -050067 fprintf(s,"%02x ", 0xFF & ((char*)mem)[i]);
Chris Austen0ba649e2015-10-13 12:28:13 -050068 }
69 else /* end of block, just aligning for ASCII dump */
70 {
Chris Austen99497312015-10-22 13:00:16 -050071 fprintf(s," ");
Chris Austen0ba649e2015-10-13 12:28:13 -050072 }
Chris Austen120f7322015-10-14 23:27:31 -050073
Chris Austen0ba649e2015-10-13 12:28:13 -050074 /* print ASCII dump */
75 if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
76 {
77 for(j = i - (HEXDUMP_COLS - 1); j <= i; j++)
78 {
79 if(j >= len) /* end of block, not really printing */
80 {
Chris Austen99497312015-10-22 13:00:16 -050081 fputc(' ', s);
Chris Austen0ba649e2015-10-13 12:28:13 -050082 }
83 else if(isprint(((char*)mem)[j])) /* printable char */
84 {
Chris Austen99497312015-10-22 13:00:16 -050085 fputc(0xFF & ((char*)mem)[j], s);
Chris Austen0ba649e2015-10-13 12:28:13 -050086 }
87 else /* other char */
88 {
Chris Austen99497312015-10-22 13:00:16 -050089 fputc('.',s);
Chris Austen0ba649e2015-10-13 12:28:13 -050090 }
91 }
Chris Austen99497312015-10-22 13:00:16 -050092 fputc('\n',s);
Chris Austen0ba649e2015-10-13 12:28:13 -050093 }
94 }
95}
96
97
vishwabmcba0bd5f2015-09-30 16:50:23 +053098// Method that gets called by shared libraries to get their command handlers registered
99void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
100 ipmi_context_t context, ipmid_callback_t handler)
101{
102 // Pack NetFn and Command in one.
103 auto netfn_and_cmd = std::make_pair(netfn, cmd);
104
105 // Pack Function handler and Data in another.
106 auto handler_and_context = std::make_pair(handler, context);
107
108 // Check if the registration has already been made..
109 auto iter = g_ipmid_router_map.find(netfn_and_cmd);
110 if(iter != g_ipmid_router_map.end())
111 {
112 fprintf(stderr,"ERROR : Duplicate registration for NetFn [0x%X], Cmd:[0x%X]\n",netfn, cmd);
113 }
114 else
115 {
116 // This is a fresh registration.. Add it to the map.
117 g_ipmid_router_map.emplace(netfn_and_cmd, handler_and_context);
118 }
119
120 return;
121}
122
123// Looks at the map and calls corresponding handler functions.
124ipmi_ret_t ipmi_netfn_router(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
125 ipmi_response_t response, ipmi_data_len_t data_len)
126{
127 // return from the Command handlers.
128 ipmi_ret_t rc = IPMI_CC_INVALID;
129
130 // Walk the map that has the registered handlers and invoke the approprite
131 // handlers for matching commands.
132 auto iter = g_ipmid_router_map.find(std::make_pair(netfn, cmd));
133 if(iter == g_ipmid_router_map.end())
134 {
Chris Austen99497312015-10-22 13:00:16 -0500135 fprintf(stderr, "No registered handlers for NetFn:[0x%X], Cmd:[0x%X]"
vishwabmcba0bd5f2015-09-30 16:50:23 +0530136 " trying Wilcard implementation \n",netfn, cmd);
137
138 // Now that we did not find any specific [NetFn,Cmd], tuple, check for
139 // NetFn, WildCard command present.
140 iter = g_ipmid_router_map.find(std::make_pair(netfn, IPMI_CMD_WILDCARD));
141 if(iter == g_ipmid_router_map.end())
142 {
Chris Austen99497312015-10-22 13:00:16 -0500143 fprintf(stderr, "No Registered handlers for NetFn:[0x%X],Cmd:[0x%X]\n",netfn, IPMI_CMD_WILDCARD);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530144
145 // Respond with a 0xC1
146 memcpy(response, &rc, IPMI_CC_LEN);
147 *data_len = IPMI_CC_LEN;
148 return rc;
149 }
150 }
151
152#ifdef __IPMI_DEBUG__
153 // We have either a perfect match -OR- a wild card atleast,
154 printf("Calling Net function:[0x%X], Command:[0x%X]\n", netfn, cmd);
155#endif
156
157 // Extract the map data onto appropriate containers
158 auto handler_and_context = iter->second;
159
160 // Creating a pointer type casted to char* to make sure we advance 1 byte
161 // when we advance pointer to next's address. advancing void * would not
162 // make sense.
163 char *respo = &((char *)response)[IPMI_CC_LEN];
164
165 // Response message from the plugin goes into a byte post the base response
166 rc = (handler_and_context.first) (netfn, cmd, request, respo,
167 data_len, handler_and_context.second);
Chris Austen120f7322015-10-14 23:27:31 -0500168
vishwabmcba0bd5f2015-09-30 16:50:23 +0530169 // Now copy the return code that we got from handler and pack it in first
170 // byte.
171 memcpy(response, &rc, IPMI_CC_LEN);
Chris Austen120f7322015-10-14 23:27:31 -0500172
vishwabmcba0bd5f2015-09-30 16:50:23 +0530173 // Data length is now actual data + completion code.
174 *data_len = *data_len + IPMI_CC_LEN;
175
176 return rc;
177}
178
vishwabmcba0bd5f2015-09-30 16:50:23 +0530179
vishwabmcba0bd5f2015-09-30 16:50:23 +0530180
vishwabmcba0bd5f2015-09-30 16:50:23 +0530181
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800182static int send_ipmi_message(sd_bus_message *req, unsigned char seq, unsigned char netfn, unsigned char lun, unsigned char cmd, unsigned char cc, unsigned char *buf, unsigned char len) {
vishwabmcba0bd5f2015-09-30 16:50:23 +0530183
Chris Austen0ba649e2015-10-13 12:28:13 -0500184 sd_bus_error error = SD_BUS_ERROR_NULL;
185 sd_bus_message *reply = NULL, *m=NULL;
Jeremy Kerre41081f2015-10-27 12:11:36 +0800186 const char *dest, *path;
Chris Austen0ba649e2015-10-13 12:28:13 -0500187 int r, pty;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530188
Jeremy Kerre41081f2015-10-27 12:11:36 +0800189 dest = sd_bus_message_get_sender(req);
190 path = sd_bus_message_get_path(req);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530191
Jeremy Kerre41081f2015-10-27 12:11:36 +0800192 r = sd_bus_message_new_method_call(bus,&m,dest,path,DBUS_INTF,"sendMessage");
Chris Austen0ba649e2015-10-13 12:28:13 -0500193 if (r < 0) {
194 fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r));
195 return -1;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530196 }
197
vishwabmcba0bd5f2015-09-30 16:50:23 +0530198
Chris Austenabfb5e82015-10-13 12:29:24 -0500199 // Responses in IPMI require a bit set. So there ya go...
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800200 netfn |= 0x01;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530201
Chris Austen0ba649e2015-10-13 12:28:13 -0500202
203 // Add the bytes needed for the methods to be called
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800204 r = sd_bus_message_append(m, "yyyyy", seq, netfn, lun, cmd, cc);
Chris Austen0ba649e2015-10-13 12:28:13 -0500205 if (r < 0) {
206 fprintf(stderr, "Failed add the netfn and others : %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600207 goto final;
Chris Austen0ba649e2015-10-13 12:28:13 -0500208 }
Chris Austen120f7322015-10-14 23:27:31 -0500209
Chris Austen0ba649e2015-10-13 12:28:13 -0500210 r = sd_bus_message_append_array(m, 'y', buf, len);
211 if (r < 0) {
212 fprintf(stderr, "Failed to add the string of response bytes: %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600213 goto final;
Chris Austen0ba649e2015-10-13 12:28:13 -0500214 }
215
216
217
218 // Call the IPMI responder on the bus so the message can be sent to the CEC
219 r = sd_bus_call(bus, m, 0, &error, &reply);
220 if (r < 0) {
Chris Austen169395e2015-12-02 20:56:15 -0600221 fprintf(stderr, "Failed to call the method: %s\n", strerror(-r));
Chris Austen6bd23962015-12-07 21:31:48 -0600222 fprintf(stderr, "Dest: %s, Path: %s\n", dest, path);
Chris Austen169395e2015-12-02 20:56:15 -0600223 goto final;
Chris Austen0ba649e2015-10-13 12:28:13 -0500224 }
225
226 r = sd_bus_message_read(reply, "x", &pty);
Chris Austen0ba649e2015-10-13 12:28:13 -0500227 if (r < 0) {
228 fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r));
Chris Austen0ba649e2015-10-13 12:28:13 -0500229 }
230
Chris Austen169395e2015-12-02 20:56:15 -0600231final:
Chris Austen0ba649e2015-10-13 12:28:13 -0500232 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600233 m = sd_bus_message_unref(m);
234 reply = sd_bus_message_unref(reply);
Chris Austen0ba649e2015-10-13 12:28:13 -0500235
Chris Austen0ba649e2015-10-13 12:28:13 -0500236 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Chris Austen0ba649e2015-10-13 12:28:13 -0500237}
238
239static int handle_ipmi_command(sd_bus_message *m, void *user_data, sd_bus_error
240 *ret_error) {
241 int r = 0;
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800242 unsigned char sequence, netfn, lun, cmd;
Chris Austen0ba649e2015-10-13 12:28:13 -0500243 const void *request;
244 size_t sz;
245 size_t resplen =MAX_IPMI_BUFFER;
246 unsigned char response[MAX_IPMI_BUFFER];
247
Chris Austen0ba649e2015-10-13 12:28:13 -0500248 memset(response, 0, MAX_IPMI_BUFFER);
249
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800250 r = sd_bus_message_read(m, "yyyy", &sequence, &netfn, &lun, &cmd);
Chris Austen0ba649e2015-10-13 12:28:13 -0500251 if (r < 0) {
252 fprintf(stderr, "Failed to parse signal message: %s\n", strerror(-r));
253 return -1;
254 }
255
256 r = sd_bus_message_read_array(m, 'y', &request, &sz );
257 if (r < 0) {
258 fprintf(stderr, "Failed to parse signal message: %s\n", strerror(-r));
259 return -1;
260 }
261
Chris Austen99497312015-10-22 13:00:16 -0500262 fprintf(ipmiio, "IPMI Incoming: Seq 0x%02x, NetFn 0x%02x, CMD: 0x%02x \n", sequence, netfn, cmd);
263 hexdump(ipmiio, (void*)request, sz);
Chris Austen0ba649e2015-10-13 12:28:13 -0500264
Chris Austen120f7322015-10-14 23:27:31 -0500265 // Allow the length field to be used for both input and output of the
Chris Austen0ba649e2015-10-13 12:28:13 -0500266 // ipmi call
267 resplen = sz;
268
Chris Austen120f7322015-10-14 23:27:31 -0500269 // Now that we have parsed the entire byte array from the caller
vishwabmcba0bd5f2015-09-30 16:50:23 +0530270 // we can call the ipmi router to do the work...
Chris Austen0ba649e2015-10-13 12:28:13 -0500271 r = ipmi_netfn_router(netfn, cmd, (void *)request, (void *)response, &resplen);
272 if(r != 0)
vishwabmcba0bd5f2015-09-30 16:50:23 +0530273 {
Chris Austen0ba649e2015-10-13 12:28:13 -0500274 fprintf(stderr,"ERROR:[0x%X] handling NetFn:[0x%X], Cmd:[0x%X]\n",r, netfn, cmd);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530275 }
276
Chris Austen99497312015-10-22 13:00:16 -0500277 fprintf(ipmiio, "IPMI Response:\n");
278 hexdump(ipmiio, (void*)response, resplen);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530279
Chris Austen0ba649e2015-10-13 12:28:13 -0500280 // Send the response buffer from the ipmi command
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800281 r = send_ipmi_message(m, sequence, netfn, lun, cmd, response[0],
282 ((unsigned char *)response) + 1, resplen - 1);
Chris Austen0ba649e2015-10-13 12:28:13 -0500283 if (r < 0) {
284 fprintf(stderr, "Failed to send the response message\n");
285 return -1;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530286 }
287
vishwabmcba0bd5f2015-09-30 16:50:23 +0530288
Chris Austen0ba649e2015-10-13 12:28:13 -0500289 return 0;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530290}
291
Chris Austen0ba649e2015-10-13 12:28:13 -0500292
vishwabmcba0bd5f2015-09-30 16:50:23 +0530293//----------------------------------------------------------------------
294// handler_select
295// Select all the files ending with with .so. in the given diretcory
296// @d: dirent structure containing the file name
297//----------------------------------------------------------------------
298int handler_select(const struct dirent *entry)
299{
300 // To hold ".so" from entry->d_name;
301 char dname_copy[4] = {0};
302
303 // We want to avoid checking for everything and isolate to the ones having
304 // .so in them.
305 if(strstr(entry->d_name, IPMI_PLUGIN_EXTN))
306 {
307 // It is possible that .so could be anywhere in the string but unlikely
Chris Austen120f7322015-10-14 23:27:31 -0500308 // But being careful here. Get the base address of the string, move
vishwabmcba0bd5f2015-09-30 16:50:23 +0530309 // until end and come back 3 steps and that gets what we need.
310 strcpy(dname_copy, (entry->d_name + strlen(entry->d_name)-strlen(IPMI_PLUGIN_EXTN)));
311 if(strcmp(dname_copy, IPMI_PLUGIN_EXTN) == 0)
312 {
313 return 1;
314 }
315 }
316 return 0;
317}
318
319// 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 -0500320// register a callback handler
vishwabmcba0bd5f2015-09-30 16:50:23 +0530321void ipmi_register_callback_handlers(const char* ipmi_lib_path)
322{
323 // For walking the ipmi_lib_path
324 struct dirent **handler_list;
325 int num_handlers = 0;
326
327 // This is used to check and abort if someone tries to register a bad one.
328 void *lib_handler = NULL;
329
330 if(ipmi_lib_path == NULL)
331 {
332 fprintf(stderr,"ERROR; No handlers to be registered for ipmi.. Aborting\n");
333 assert(0);
334 }
335 else
336 {
337 // 1: Open ipmi_lib_path. Its usually "/usr/lib/phosphor-host-ipmid"
338 // 2: Scan the directory for the files that end with .so
Chris Austen120f7322015-10-14 23:27:31 -0500339 // 3: For each one of them, just do a 'dlopen' so that they register
vishwabmcba0bd5f2015-09-30 16:50:23 +0530340 // the handlers for callback routines.
341
342 std::string handler_fqdn = ipmi_lib_path;
Chris Austen120f7322015-10-14 23:27:31 -0500343
vishwabmcba0bd5f2015-09-30 16:50:23 +0530344 // Append a "/" since we need to add the name of the .so. If there is
345 // already a .so, adding one more is not any harm.
346 handler_fqdn += "/";
347
348 num_handlers = scandir(ipmi_lib_path, &handler_list, handler_select, alphasort);
Nan Li36c0cb62016-03-31 11:16:08 +0800349 if (num_handlers < 0)
350 return;
Jeremy Kerr5e8f85e2015-10-27 13:43:54 +0800351
vishwabmcba0bd5f2015-09-30 16:50:23 +0530352 while(num_handlers--)
353 {
Chris Austen54030262015-10-13 12:30:46 -0500354 handler_fqdn = ipmi_lib_path;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530355 handler_fqdn += handler_list[num_handlers]->d_name;
Chris Austen54030262015-10-13 12:30:46 -0500356 printf("Registering handler:[%s]\n",handler_fqdn.c_str());
357
vishwabmcba0bd5f2015-09-30 16:50:23 +0530358 lib_handler = dlopen(handler_fqdn.c_str(), RTLD_NOW);
Nan Li36c0cb62016-03-31 11:16:08 +0800359
vishwabmcba0bd5f2015-09-30 16:50:23 +0530360 if(lib_handler == NULL)
361 {
Chris Austen120f7322015-10-14 23:27:31 -0500362 fprintf(stderr,"ERROR opening [%s]: %s\n",
363 handler_fqdn.c_str(), dlerror());
vishwabmcba0bd5f2015-09-30 16:50:23 +0530364 }
365 // Wipe the memory allocated for this particular entry.
366 free(handler_list[num_handlers]);
367 }
Nan Li36c0cb62016-03-31 11:16:08 +0800368
vishwabmcba0bd5f2015-09-30 16:50:23 +0530369 // Done with all registration.
370 free(handler_list);
371 }
372
373 // TODO : What to be done on the memory that is given by dlopen ?.
374 return;
375}
376
Chris Austen30195fa2015-11-13 14:39:19 -0600377sd_bus *ipmid_get_sd_bus_connection(void) {
378 return bus;
379}
380
vishwab9f559a2016-01-13 01:53:08 -0600381sd_bus_slot *ipmid_get_sd_bus_slot(void) {
382 return ipmid_slot;
383}
384
vishwabmcba0bd5f2015-09-30 16:50:23 +0530385int main(int argc, char *argv[])
386{
Chris Austen0ba649e2015-10-13 12:28:13 -0500387 int r;
Chris Austen99497312015-10-22 13:00:16 -0500388 unsigned long tvalue;
389 int c;
390
391
392
393 // This file and subsequient switch is for turning on levels
394 // of trace
395 ipmicmddetails = ipmiio = ipmidbus = fopen("/dev/null", "w");
396
397 while ((c = getopt (argc, argv, "h:d:")) != -1)
398 switch (c) {
399 case 'd':
400 tvalue = strtoul(optarg, NULL, 16);
401 if (1&tvalue) {
402 ipmiio = stdout;
403 }
404 if (2&tvalue) {
405 ipmidbus = stdout;
406 }
407 if (4&tvalue) {
408 ipmicmddetails = stdout;
409 }
410 break;
411 case 'h':
412 case '?':
413 print_usage();
414 return 1;
415 }
Chris Austen0ba649e2015-10-13 12:28:13 -0500416
417
Chris Austen0ba649e2015-10-13 12:28:13 -0500418 /* Connect to system bus */
419 r = sd_bus_open_system(&bus);
420 if (r < 0) {
421 fprintf(stderr, "Failed to connect to system bus: %s\n",
422 strerror(-r));
423 goto finish;
424 }
vishwabmcba0bd5f2015-09-30 16:50:23 +0530425
Chris Austen30195fa2015-11-13 14:39:19 -0600426 // Register all the handlers that provider implementation to IPMI commands.
427 ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH);
428
vishwa36993272015-11-20 12:43:49 -0600429 // Watch for BT messages
vishwab9f559a2016-01-13 01:53:08 -0600430 r = sd_bus_add_match(bus, &ipmid_slot, FILTER, handle_ipmi_command, NULL);
Chris Austen0ba649e2015-10-13 12:28:13 -0500431 if (r < 0) {
432 fprintf(stderr, "Failed: sd_bus_add_match: %s : %s\n", strerror(-r), FILTER);
433 goto finish;
434 }
vishwabmcba0bd5f2015-09-30 16:50:23 +0530435
vishwabmcba0bd5f2015-09-30 16:50:23 +0530436
Chris Austen0ba649e2015-10-13 12:28:13 -0500437 for (;;) {
438 /* Process requests */
Chris Austen0ba649e2015-10-13 12:28:13 -0500439 r = sd_bus_process(bus, NULL);
440 if (r < 0) {
441 fprintf(stderr, "Failed to process bus: %s\n", strerror(-r));
442 goto finish;
443 }
444 if (r > 0) {
445 continue;
446 }
447
448 r = sd_bus_wait(bus, (uint64_t) - 1);
449 if (r < 0) {
450 fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r));
451 goto finish;
452 }
453 }
454
455finish:
vishwab9f559a2016-01-13 01:53:08 -0600456 sd_bus_slot_unref(ipmid_slot);
Chris Austen0ba649e2015-10-13 12:28:13 -0500457 sd_bus_unref(bus);
458 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
459
vishwabmcba0bd5f2015-09-30 16:50:23 +0530460}
Chris Austenb071c702015-10-15 00:00:09 -0500461
Chris Austen0012e9b2015-10-22 01:37:46 -0500462// Use a lookup table to find the interface name of a specific sensor
463// This will be used until an alternative is found. this is the first
464// step for mapping IPMI
465int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
Chris Austenb071c702015-10-15 00:00:09 -0500466
Joel Stanleyf19539e2015-11-25 17:24:05 +1030467 char *str1;
Chris Austen0012e9b2015-10-22 01:37:46 -0500468 sd_bus_error error = SD_BUS_ERROR_NULL;
469 sd_bus_message *reply = NULL, *m=NULL;
Chris Austenb071c702015-10-15 00:00:09 -0500470
471
Chris Austen0012e9b2015-10-22 01:37:46 -0500472 int r;
473
Chris Austen0012e9b2015-10-22 01:37:46 -0500474 r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get");
475 if (r < 0) {
476 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
477 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
478 interface->bus, interface->path, interface->interface);
vishwa1eaea4f2016-02-26 11:57:40 -0600479 goto final;
Chris Austen0012e9b2015-10-22 01:37:46 -0500480 }
481
482 r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name);
483 if (r < 0) {
484 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
485 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
486 interface->bus, interface->path, interface->interface);
vishwa1eaea4f2016-02-26 11:57:40 -0600487 goto final;
Chris Austen0012e9b2015-10-22 01:37:46 -0500488 }
489
490 r = sd_bus_call(bus, m, 0, &error, &reply);
491 if (r < 0) {
492 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
493 goto final;
494 }
495
496 r = sd_bus_message_read(reply, "v", "s", &str1) ;
497 if (r < 0) {
498 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
499 goto final;
500 }
501
502 strcpy(property_value, str1);
503
504final:
505
506 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600507 m = sd_bus_message_unref(m);
508 reply = sd_bus_message_unref(reply);
Chris Austen0012e9b2015-10-22 01:37:46 -0500509
510 return r;
511}
Chris Austenb071c702015-10-15 00:00:09 -0500512
Chris Austen41a4b312015-10-25 03:45:42 -0500513
Chris Austenb071c702015-10-15 00:00:09 -0500514// Use a lookup table to find the interface name of a specific sensor
515// This will be used until an alternative is found. this is the first
516// step for mapping IPMI
517int find_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
518
519 const char *busname = "org.openbmc.managers.System";
520 const char *objname = "/org/openbmc/managers/System";
521
522 char *str1, *str2, *str3;
523 sd_bus_error error = SD_BUS_ERROR_NULL;
vishwa1eaea4f2016-02-26 11:57:40 -0600524 sd_bus_message *reply = NULL;
Chris Austenb071c702015-10-15 00:00:09 -0500525
526
527 int r;
528
vishwa1eaea4f2016-02-26 11:57:40 -0600529 r = sd_bus_call_method(bus,busname,objname,busname, "getObjectFromByteId",
530 &error, &reply, "sy", type, num);
Chris Austenb071c702015-10-15 00:00:09 -0500531 if (r < 0) {
532 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
Chris Austenb071c702015-10-15 00:00:09 -0500533 goto final;
534 }
535
Chris Austenb071c702015-10-15 00:00:09 -0500536 r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3);
537 if (r < 0) {
538 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
539 goto final;
540 }
541
542 strncpy(interface->bus, str1, MAX_DBUS_PATH);
543 strncpy(interface->path, str2, MAX_DBUS_PATH);
544 strncpy(interface->interface, str3, MAX_DBUS_PATH);
545
546 interface->sensornumber = num;
547
Chris Austenb071c702015-10-15 00:00:09 -0500548final:
549
550 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600551 reply = sd_bus_message_unref(reply);
Chris Austenb071c702015-10-15 00:00:09 -0500552
553 return r;
554}
555
556
557/////////////////////////////////////////////////////////////////////
558//
559// Routines used by ipmi commands wanting to interact on the dbus
560//
561/////////////////////////////////////////////////////////////////////
Chris Austen10ccc0f2015-12-10 18:27:04 -0600562int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
Chris Austen0130d6e2015-10-15 22:32:36 -0500563
564
565 dbus_interface_t a;
566 int r;
567 sd_bus_error error = SD_BUS_ERROR_NULL;
Joel Stanleyf19539e2015-11-25 17:24:05 +1030568 sd_bus_message *m=NULL;
Chris Austen0130d6e2015-10-15 22:32:36 -0500569
Chris Austen99497312015-10-22 13:00:16 -0500570 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 -0500571 number, method, value);
572
573 r = find_openbmc_path("SENSOR", number, &a);
574
Chris Austen0130d6e2015-10-15 22:32:36 -0500575 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
576 if (r < 0) {
577 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600578 goto final;
Chris Austen0130d6e2015-10-15 22:32:36 -0500579 }
580
581 r = sd_bus_message_append(m, "v", "s", value);
582 if (r < 0) {
583 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600584 goto final;
Chris Austen0130d6e2015-10-15 22:32:36 -0500585 }
586
587
Chris Austen0130d6e2015-10-15 22:32:36 -0500588 r = sd_bus_call(bus, m, 0, &error, NULL);
589 if (r < 0) {
Chris Austen10ccc0f2015-12-10 18:27:04 -0600590 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
Chris Austen0130d6e2015-10-15 22:32:36 -0500591 }
592
vishwa1eaea4f2016-02-26 11:57:40 -0600593final:
Chris Austen0130d6e2015-10-15 22:32:36 -0500594 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600595 m = sd_bus_message_unref(m);
Chris Austen0130d6e2015-10-15 22:32:36 -0500596
597 return 0;
598}
Chris Austen10ccc0f2015-12-10 18:27:04 -0600599int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
600
601
602 dbus_interface_t a;
603 int r;
604 sd_bus_error error = SD_BUS_ERROR_NULL;
605 sd_bus_message *m=NULL;
606
607 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
608 number, method, value);
609
610 r = find_openbmc_path("SENSOR", number, &a);
611
612 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
613 if (r < 0) {
614 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600615 goto final;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600616 }
617
Adriana Kobylak93125982016-03-01 12:48:10 -0600618 r = sd_bus_message_append(m, "v", "i", value);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600619 if (r < 0) {
620 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600621 goto final;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600622 }
623
624
625 r = sd_bus_call(bus, m, 0, &error, NULL);
626 if (r < 0) {
627 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
628 }
629
vishwa1eaea4f2016-02-26 11:57:40 -0600630final:
Chris Austen10ccc0f2015-12-10 18:27:04 -0600631 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600632 m = sd_bus_message_unref(m);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600633
634 return 0;
vishwa1eaea4f2016-02-26 11:57:40 -0600635}