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) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 30 | log<level::ERR>("Failed to get busname", |
| 31 | entry("BUS=%s", objname), |
| 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 | |
| 36 | r = sd_bus_message_new_method_call(bus,&m,busname,objname,busname,"getObjectFromByteId"); |
| 37 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 38 | log<level::ERR>("Failed to create a method call", |
| 39 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | r = sd_bus_message_append(m, "sy", type, num); |
| 43 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 44 | log<level::ERR>("Failed to create a input parameter", |
| 45 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Call the IPMI responder on the bus so the message can be sent to the CEC |
| 49 | r = sd_bus_call(bus, m, 0, &error, &reply); |
| 50 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 51 | log<level::ERR>("Failed to call the method", |
| 52 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 53 | goto final; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3); |
| 58 | if (r < 0) { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 59 | log<level::ERR>("Failed to get a response", |
| 60 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 61 | goto final; |
| 62 | } |
| 63 | |
| 64 | strncpy(interface->bus, str1, MAX_DBUS_PATH); |
| 65 | strncpy(interface->path, str2, MAX_DBUS_PATH); |
| 66 | strncpy(interface->interface, str3, MAX_DBUS_PATH); |
| 67 | |
| 68 | interface->sensornumber = num; |
| 69 | |
| 70 | final: |
| 71 | |
| 72 | sd_bus_error_free(&error); |
| 73 | sd_bus_message_unref(m); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 74 | free (busname); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 75 | |
| 76 | return r; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | int main(int argc, char *argv[]) |
| 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 | |
Nagaraju Goruganti | 2fa0e70 | 2018-04-16 05:25:21 -0500 | [diff] [blame] | 90 | if (argc < 2) { |
| 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 | |
| 100 | num = (uint16_t) val; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 101 | |
| 102 | |
| 103 | |
| 104 | /* Connect to system bus */ |
| 105 | r = sd_bus_open_system(&bus); |
| 106 | if (r < 0) { |
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 | |
Nagaraju Goruganti | 2fa0e70 | 2018-04-16 05:25:21 -0500 | [diff] [blame] | 114 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 115 | finish: |
| 116 | sd_bus_unref(bus); |
| 117 | |
Nagaraju Goruganti | 2fa0e70 | 2018-04-16 05:25:21 -0500 | [diff] [blame] | 118 | return 0; |
Brad Bishop | 819ddd4 | 2016-10-05 21:19:19 -0400 | [diff] [blame] | 119 | } |