blob: c3b317a2832ad3dc4467382ac20d7c864fd8deee [file] [log] [blame]
Chris Austen41a4b312015-10-25 03:45:42 -05001#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 Solomineb9b8142016-08-23 09:07:28 -05008#include <mapper.h>
Chris Austen41a4b312015-10-25 03:45:42 -05009
10
11extern void send_esel(uint16_t recordid);
12
13sd_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 Shaffer2ae09b92017-04-05 15:09:41 -070018int find_openbmc_path(const uint8_t num, dbus_interface_t *interface) {
Chris Austen41a4b312015-10-25 03:45:42 -050019
Chris Austen41a4b312015-10-25 03:45:42 -050020 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 Austen41a4b312015-10-25 03:45:42 -050025 int r;
Sergey Solomineb9b8142016-08-23 09:07:28 -050026 char *busname = NULL;
27
28 r = mapper_get_service(bus, objname, &busname);
29 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053030 log<level::ERR>("Failed to get busname",
31 entry("BUS=%s", objname),
32 entry("ERRNO=0x%X", -r));
Sergey Solomineb9b8142016-08-23 09:07:28 -050033 goto final;
34 }
Chris Austen41a4b312015-10-25 03:45:42 -050035
36 r = sd_bus_message_new_method_call(bus,&m,busname,objname,busname,"getObjectFromByteId");
37 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053038 log<level::ERR>("Failed to create a method call",
39 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050040 }
41
42 r = sd_bus_message_append(m, "sy", type, num);
43 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053044 log<level::ERR>("Failed to create a input parameter",
45 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050046 }
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 Saripalli5fb14602017-11-09 14:46:27 +053051 log<level::ERR>("Failed to call the method",
52 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050053 goto final;
54 }
55
56
57 r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3);
58 if (r < 0) {
Aditya Saripalli5fb14602017-11-09 14:46:27 +053059 log<level::ERR>("Failed to get a response",
60 entry("ERRNO=0x%X", -r));
Chris Austen41a4b312015-10-25 03:45:42 -050061 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
70final:
71
72 sd_bus_error_free(&error);
73 sd_bus_message_unref(m);
Sergey Solomineb9b8142016-08-23 09:07:28 -050074 free (busname);
Chris Austen41a4b312015-10-25 03:45:42 -050075
76 return r;
77}
78
79
80
81
82int main(int argc, char *argv[])
83{
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
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -050090 if (argc < 2) {
91 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
100 num = (uint16_t) val;
Chris Austen41a4b312015-10-25 03:45:42 -0500101
102
103
104 /* Connect to system bus */
105 r = sd_bus_open_system(&bus);
106 if (r < 0) {
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
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500114
Chris Austen41a4b312015-10-25 03:45:42 -0500115finish:
116 sd_bus_unref(bus);
117
Nagaraju Goruganti2fa0e702018-04-16 05:25:21 -0500118 return 0;
Brad Bishop819ddd42016-10-05 21:19:19 -0400119}