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