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