Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <limits.h> |
| 3 | #include <stdio.h> |
| 4 | #include <errno.h> |
| 5 | #include <stdint.h> |
| 6 | #include <systemd/sd-bus.h> |
| 7 | #include "sensorhandler.h" |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 8 | #include <mapper.h> |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 9 | |
| 10 | |
| 11 | extern void send_esel(uint16_t recordid); |
| 12 | |
| 13 | sd_bus *bus = NULL; |
| 14 | |
| 15 | // Use a lookup table to find the interface name of a specific sensor |
| 16 | // This will be used until an alternative is found. this is the first |
| 17 | // step for mapping IPMI |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 18 | int find_openbmc_path(const uint8_t num, dbus_interface_t *interface) { |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 19 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 20 | const char *objname = "/org/openbmc/managers/System"; |
| 21 | |
| 22 | char *str1, *str2, *str3; |
| 23 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 24 | sd_bus_message *reply = NULL, *m=NULL; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 25 | int r; |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 26 | char *busname = NULL; |
| 27 | |
| 28 | r = mapper_get_service(bus, objname, &busname); |
| 29 | if (r < 0) { |
Brad Bishop | 819ddd4 | 2016-10-05 21:19:19 -0400 | [diff] [blame] | 30 | fprintf(stderr, "Failed to get %s busname: %s\n", |
| 31 | objname, strerror(-r)); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 32 | goto final; |
| 33 | } |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 34 | |
| 35 | r = sd_bus_message_new_method_call(bus,&m,busname,objname,busname,"getObjectFromByteId"); |
| 36 | if (r < 0) { |
| 37 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 38 | } |
| 39 | |
| 40 | r = sd_bus_message_append(m, "sy", type, num); |
| 41 | if (r < 0) { |
| 42 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 43 | } |
| 44 | |
| 45 | // Call the IPMI responder on the bus so the message can be sent to the CEC |
| 46 | r = sd_bus_call(bus, m, 0, &error, &reply); |
| 47 | if (r < 0) { |
| 48 | fprintf(stderr, "Failed to call the method: %s", strerror(-r)); |
| 49 | goto final; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3); |
| 54 | if (r < 0) { |
| 55 | fprintf(stderr, "Failed to get a response: %s", strerror(-r)); |
| 56 | goto final; |
| 57 | } |
| 58 | |
| 59 | strncpy(interface->bus, str1, MAX_DBUS_PATH); |
| 60 | strncpy(interface->path, str2, MAX_DBUS_PATH); |
| 61 | strncpy(interface->interface, str3, MAX_DBUS_PATH); |
| 62 | |
| 63 | interface->sensornumber = num; |
| 64 | |
| 65 | final: |
| 66 | |
| 67 | sd_bus_error_free(&error); |
| 68 | sd_bus_message_unref(m); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 69 | free (busname); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 70 | |
| 71 | return r; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | int main(int argc, char *argv[]) |
| 78 | { |
| 79 | int base; |
| 80 | char *endptr, *str; |
| 81 | long val; |
| 82 | uint16_t num; |
| 83 | int r; |
| 84 | |
| 85 | if (argc < 2) { |
| 86 | fprintf(stderr, "Usage: %s sensornumber\n", argv[0]); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | str = argv[1]; |
| 91 | base = (argc > 2) ? atoi(argv[2]) : 10; |
| 92 | |
| 93 | val = strtol(str, &endptr, base); |
| 94 | |
| 95 | num = (uint16_t) val; |
| 96 | |
| 97 | |
| 98 | |
| 99 | /* Connect to system bus */ |
| 100 | r = sd_bus_open_system(&bus); |
| 101 | if (r < 0) { |
| 102 | fprintf(stderr, "Failed to connect to system bus: %s\n", |
| 103 | strerror(-r)); |
| 104 | goto finish; |
| 105 | } |
| 106 | |
| 107 | send_esel(num); |
| 108 | |
| 109 | |
| 110 | finish: |
| 111 | sd_bus_unref(bus); |
| 112 | |
| 113 | return 0; |
Brad Bishop | 819ddd4 | 2016-10-05 21:19:19 -0400 | [diff] [blame] | 114 | } |