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 |
| 18 | int find_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) { |
| 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) { |
| 30 | fprintf(stderr, "Failed to get busname: %s\n", strerror(-r)); |
| 31 | goto final; |
| 32 | } |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 33 | |
| 34 | r = sd_bus_message_new_method_call(bus,&m,busname,objname,busname,"getObjectFromByteId"); |
| 35 | if (r < 0) { |
| 36 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 37 | } |
| 38 | |
| 39 | r = sd_bus_message_append(m, "sy", type, num); |
| 40 | if (r < 0) { |
| 41 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 42 | } |
| 43 | |
| 44 | // Call the IPMI responder on the bus so the message can be sent to the CEC |
| 45 | r = sd_bus_call(bus, m, 0, &error, &reply); |
| 46 | if (r < 0) { |
| 47 | fprintf(stderr, "Failed to call the method: %s", strerror(-r)); |
| 48 | goto final; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3); |
| 53 | if (r < 0) { |
| 54 | fprintf(stderr, "Failed to get a response: %s", strerror(-r)); |
| 55 | goto final; |
| 56 | } |
| 57 | |
| 58 | strncpy(interface->bus, str1, MAX_DBUS_PATH); |
| 59 | strncpy(interface->path, str2, MAX_DBUS_PATH); |
| 60 | strncpy(interface->interface, str3, MAX_DBUS_PATH); |
| 61 | |
| 62 | interface->sensornumber = num; |
| 63 | |
| 64 | final: |
| 65 | |
| 66 | sd_bus_error_free(&error); |
| 67 | sd_bus_message_unref(m); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 68 | free (busname); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 69 | |
| 70 | return r; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | int main(int argc, char *argv[]) |
| 77 | { |
| 78 | int base; |
| 79 | char *endptr, *str; |
| 80 | long val; |
| 81 | uint16_t num; |
| 82 | int r; |
| 83 | |
| 84 | if (argc < 2) { |
| 85 | fprintf(stderr, "Usage: %s sensornumber\n", argv[0]); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | str = argv[1]; |
| 90 | base = (argc > 2) ? atoi(argv[2]) : 10; |
| 91 | |
| 92 | val = strtol(str, &endptr, base); |
| 93 | |
| 94 | num = (uint16_t) val; |
| 95 | |
| 96 | |
| 97 | |
| 98 | /* Connect to system bus */ |
| 99 | r = sd_bus_open_system(&bus); |
| 100 | if (r < 0) { |
| 101 | fprintf(stderr, "Failed to connect to system bus: %s\n", |
| 102 | strerror(-r)); |
| 103 | goto finish; |
| 104 | } |
| 105 | |
| 106 | send_esel(num); |
| 107 | |
| 108 | |
| 109 | finish: |
| 110 | sd_bus_unref(bus); |
| 111 | |
| 112 | return 0; |
| 113 | } |