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