blob: 424ecdd2792036a4cea54870ea2d8c12d76f0a5f [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{
Chris Austen41a4b312015-10-25 03:45:42 -050019
Patrick Venture0b02be92018-08-31 11:55:55 -070020 const char* objname = "/org/openbmc/managers/System";
Chris Austen41a4b312015-10-25 03:45:42 -050021
Patrick Venture0b02be92018-08-31 11:55:55 -070022 char *str1, *str2, *str3;
Chris Austen41a4b312015-10-25 03:45:42 -050023 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -070024 sd_bus_message *reply = NULL, *m = NULL;
Chris Austen41a4b312015-10-25 03:45:42 -050025 int r;
Patrick Venture0b02be92018-08-31 11:55:55 -070026 char* busname = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -050027
28 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -070029 if (r < 0)
30 {
31 log<level::ERR>("Failed to get busname", entry("BUS=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +053032 entry("ERRNO=0x%X", -r));
Sergey Solomineb9b8142016-08-23 09:07:28 -050033 goto final;
34 }
Chris Austen41a4b312015-10-25 03:45:42 -050035
Patrick Venture0b02be92018-08-31 11:55:55 -070036 r = sd_bus_message_new_method_call(bus, &m, busname, objname, busname,
37 "getObjectFromByteId");
38 if (r < 0)
39 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053040 log<level::ERR>("Failed to create a method call",
41 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050042 }
43
44 r = sd_bus_message_append(m, "sy", type, num);
Patrick Venture0b02be92018-08-31 11:55:55 -070045 if (r < 0)
46 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053047 log<level::ERR>("Failed to create a input parameter",
48 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050049 }
50
51 // Call the IPMI responder on the bus so the message can be sent to the CEC
52 r = sd_bus_call(bus, m, 0, &error, &reply);
Patrick Venture0b02be92018-08-31 11:55:55 -070053 if (r < 0)
54 {
55 log<level::ERR>("Failed to call the method", entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050056 goto final;
57 }
58
Chris Austen41a4b312015-10-25 03:45:42 -050059 r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3);
Patrick Venture0b02be92018-08-31 11:55:55 -070060 if (r < 0)
61 {
62 log<level::ERR>("Failed to get a response", entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050063 goto final;
64 }
65
66 strncpy(interface->bus, str1, MAX_DBUS_PATH);
67 strncpy(interface->path, str2, MAX_DBUS_PATH);
68 strncpy(interface->interface, str3, MAX_DBUS_PATH);
69
70 interface->sensornumber = num;
71
72final:
73
74 sd_bus_error_free(&error);
75 sd_bus_message_unref(m);
Patrick Venture0b02be92018-08-31 11:55:55 -070076 free(busname);
Chris Austen41a4b312015-10-25 03:45:42 -050077
78 return r;
79}
80
Patrick Venture0b02be92018-08-31 11:55:55 -070081int main(int argc, char* argv[])
Chris Austen41a4b312015-10-25 03:45:42 -050082{
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050083 int base;
84 char *endptr, *str;
85 long val;
86 uint16_t num;
87 int r;
Chris Austen41a4b312015-10-25 03:45:42 -050088
Patrick Venture0b02be92018-08-31 11:55:55 -070089 if (argc < 2)
90 {
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050091 fprintf(stderr, "Usage: %s sensornumber\n", argv[0]);
92 return -1;
93 }
Chris Austen41a4b312015-10-25 03:45:42 -050094
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050095 str = argv[1];
96 base = (argc > 2) ? atoi(argv[2]) : 10;
97
98 val = strtol(str, &endptr, base);
99
Patrick Venture0b02be92018-08-31 11:55:55 -0700100 num = (uint16_t)val;
Chris Austen41a4b312015-10-25 03:45:42 -0500101
102 /* Connect to system bus */
103 r = sd_bus_open_system(&bus);
Patrick Venture0b02be92018-08-31 11:55:55 -0700104 if (r < 0)
105 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530106 log<level::ERR>("Failed to connect to system bus",
107 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -0500108 goto finish;
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500109 }
Chris Austen41a4b312015-10-25 03:45:42 -0500110
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500111 send_esel(num);
Chris Austen41a4b312015-10-25 03:45:42 -0500112
Chris Austen41a4b312015-10-25 03:45:42 -0500113finish:
114 sd_bus_unref(bus);
115
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500116 return 0;
Brad Bishop819ddd42016-10-05 21:19:19 -0400117}