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