blob: 7f42acb0b5830a9fa09fbbea3a92cf080dde8f0a [file] [log] [blame]
Chris Austen41a4b312015-10-25 03:45:42 -05001#include <errno.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07002#include <limits.h>
Sergey Solomineb9b8142016-08-23 09:07:28 -05003#include <mapper.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07004#include <stdint.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <systemd/sd-bus.h>
Chris Austen41a4b312015-10-25 03:45:42 -05008
Patrick Venture0b02be92018-08-31 11:55:55 -07009#include "sensorhandler.h"
Chris Austen41a4b312015-10-25 03:45:42 -050010
11extern void send_esel(uint16_t recordid);
12
Patrick Venture0b02be92018-08-31 11:55:55 -070013sd_bus* bus = NULL;
Chris Austen41a4b312015-10-25 03:45:42 -050014
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 Venture0b02be92018-08-31 11:55:55 -070018int find_openbmc_path(const uint8_t num, dbus_interface_t* interface)
19{
Chris Austen41a4b312015-10-25 03:45:42 -050020
Patrick Venture0b02be92018-08-31 11:55:55 -070021 const char* objname = "/org/openbmc/managers/System";
Chris Austen41a4b312015-10-25 03:45:42 -050022
Patrick Venture0b02be92018-08-31 11:55:55 -070023 char *str1, *str2, *str3;
Chris Austen41a4b312015-10-25 03:45:42 -050024 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -070025 sd_bus_message *reply = NULL, *m = NULL;
Chris Austen41a4b312015-10-25 03:45:42 -050026 int r;
Patrick Venture0b02be92018-08-31 11:55:55 -070027 char* busname = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -050028
29 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -070030 if (r < 0)
31 {
32 log<level::ERR>("Failed to get busname", entry("BUS=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +053033 entry("ERRNO=0x%X", -r));
Sergey Solomineb9b8142016-08-23 09:07:28 -050034 goto final;
35 }
Chris Austen41a4b312015-10-25 03:45:42 -050036
Patrick Venture0b02be92018-08-31 11:55:55 -070037 r = sd_bus_message_new_method_call(bus, &m, busname, objname, busname,
38 "getObjectFromByteId");
39 if (r < 0)
40 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053041 log<level::ERR>("Failed to create a method call",
42 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050043 }
44
45 r = sd_bus_message_append(m, "sy", type, num);
Patrick Venture0b02be92018-08-31 11:55:55 -070046 if (r < 0)
47 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053048 log<level::ERR>("Failed to create a input parameter",
49 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050050 }
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 Venture0b02be92018-08-31 11:55:55 -070054 if (r < 0)
55 {
56 log<level::ERR>("Failed to call the method", entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050057 goto final;
58 }
59
Chris Austen41a4b312015-10-25 03:45:42 -050060 r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3);
Patrick Venture0b02be92018-08-31 11:55:55 -070061 if (r < 0)
62 {
63 log<level::ERR>("Failed to get a response", entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050064 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
73final:
74
75 sd_bus_error_free(&error);
76 sd_bus_message_unref(m);
Patrick Venture0b02be92018-08-31 11:55:55 -070077 free(busname);
Chris Austen41a4b312015-10-25 03:45:42 -050078
79 return r;
80}
81
Patrick Venture0b02be92018-08-31 11:55:55 -070082int main(int argc, char* argv[])
Chris Austen41a4b312015-10-25 03:45:42 -050083{
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050084 int base;
85 char *endptr, *str;
86 long val;
87 uint16_t num;
88 int r;
Chris Austen41a4b312015-10-25 03:45:42 -050089
Patrick Venture0b02be92018-08-31 11:55:55 -070090 if (argc < 2)
91 {
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050092 fprintf(stderr, "Usage: %s sensornumber\n", argv[0]);
93 return -1;
94 }
Chris Austen41a4b312015-10-25 03:45:42 -050095
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050096 str = argv[1];
97 base = (argc > 2) ? atoi(argv[2]) : 10;
98
99 val = strtol(str, &endptr, base);
100
Patrick Venture0b02be92018-08-31 11:55:55 -0700101 num = (uint16_t)val;
Chris Austen41a4b312015-10-25 03:45:42 -0500102
103 /* Connect to system bus */
104 r = sd_bus_open_system(&bus);
Patrick Venture0b02be92018-08-31 11:55:55 -0700105 if (r < 0)
106 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530107 log<level::ERR>("Failed to connect to system bus",
108 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -0500109 goto finish;
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500110 }
Chris Austen41a4b312015-10-25 03:45:42 -0500111
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500112 send_esel(num);
Chris Austen41a4b312015-10-25 03:45:42 -0500113
Chris Austen41a4b312015-10-25 03:45:42 -0500114finish:
115 sd_bus_unref(bus);
116
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500117 return 0;
Brad Bishop819ddd42016-10-05 21:19:19 -0400118}