blob: 9269afbae3317093b780c3726fe0961c1db12f49 [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "sensorhandler.hpp"
2
Chris Austen41a4b312015-10-25 03:45:42 -05003#include <errno.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07004#include <limits.h>
Sergey Solomineb9b8142016-08-23 09:07:28 -05005#include <mapper.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07006#include <stdint.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07007#include <stdlib.h>
8#include <systemd/sd-bus.h>
Chris Austen41a4b312015-10-25 03:45:42 -05009
Chris Austen41a4b312015-10-25 03:45:42 -050010extern void send_esel(uint16_t recordid);
11
Patrick Venture0b02be92018-08-31 11:55:55 -070012sd_bus* bus = NULL;
Chris Austen41a4b312015-10-25 03:45:42 -050013
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 Venture0b02be92018-08-31 11:55:55 -070017int find_openbmc_path(const uint8_t num, dbus_interface_t* interface)
18{
Patrick Venture0b02be92018-08-31 11:55:55 -070019 const char* objname = "/org/openbmc/managers/System";
Chris Austen41a4b312015-10-25 03:45:42 -050020
Patrick Venture0b02be92018-08-31 11:55:55 -070021 char *str1, *str2, *str3;
Chris Austen41a4b312015-10-25 03:45:42 -050022 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -070023 sd_bus_message *reply = NULL, *m = NULL;
Chris Austen41a4b312015-10-25 03:45:42 -050024 int r;
Patrick Venture0b02be92018-08-31 11:55:55 -070025 char* busname = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -050026
27 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -070028 if (r < 0)
29 {
30 log<level::ERR>("Failed to get busname", entry("BUS=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +053031 entry("ERRNO=0x%X", -r));
Sergey Solomineb9b8142016-08-23 09:07:28 -050032 goto final;
33 }
Chris Austen41a4b312015-10-25 03:45:42 -050034
Patrick Venture0b02be92018-08-31 11:55:55 -070035 r = sd_bus_message_new_method_call(bus, &m, busname, objname, busname,
36 "getObjectFromByteId");
37 if (r < 0)
38 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053039 log<level::ERR>("Failed to create a method call",
40 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050041 }
42
43 r = sd_bus_message_append(m, "sy", type, num);
Patrick Venture0b02be92018-08-31 11:55:55 -070044 if (r < 0)
45 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053046 log<level::ERR>("Failed to create a input parameter",
47 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050048 }
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 Venture0b02be92018-08-31 11:55:55 -070052 if (r < 0)
53 {
54 log<level::ERR>("Failed to call the method", entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050055 goto final;
56 }
57
Chris Austen41a4b312015-10-25 03:45:42 -050058 r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3);
Patrick Venture0b02be92018-08-31 11:55:55 -070059 if (r < 0)
60 {
61 log<level::ERR>("Failed to get a response", entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050062 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
71final:
72
73 sd_bus_error_free(&error);
74 sd_bus_message_unref(m);
Patrick Venture0b02be92018-08-31 11:55:55 -070075 free(busname);
Chris Austen41a4b312015-10-25 03:45:42 -050076
77 return r;
78}
79
Patrick Venture0b02be92018-08-31 11:55:55 -070080int main(int argc, char* argv[])
Chris Austen41a4b312015-10-25 03:45:42 -050081{
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050082 int base;
83 char *endptr, *str;
84 long val;
85 uint16_t num;
86 int r;
Chris Austen41a4b312015-10-25 03:45:42 -050087
Patrick Venture0b02be92018-08-31 11:55:55 -070088 if (argc < 2)
89 {
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050090 fprintf(stderr, "Usage: %s sensornumber\n", argv[0]);
91 return -1;
92 }
Chris Austen41a4b312015-10-25 03:45:42 -050093
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050094 str = argv[1];
95 base = (argc > 2) ? atoi(argv[2]) : 10;
96
97 val = strtol(str, &endptr, base);
98
Patrick Venture0b02be92018-08-31 11:55:55 -070099 num = (uint16_t)val;
Chris Austen41a4b312015-10-25 03:45:42 -0500100
101 /* Connect to system bus */
102 r = sd_bus_open_system(&bus);
Patrick Venture0b02be92018-08-31 11:55:55 -0700103 if (r < 0)
104 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530105 log<level::ERR>("Failed to connect to system bus",
106 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -0500107 goto finish;
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500108 }
Chris Austen41a4b312015-10-25 03:45:42 -0500109
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500110 send_esel(num);
Chris Austen41a4b312015-10-25 03:45:42 -0500111
Chris Austen41a4b312015-10-25 03:45:42 -0500112finish:
113 sd_bus_unref(bus);
114
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500115 return 0;
Brad Bishop819ddd42016-10-05 21:19:19 -0400116}