blob: 90f63a2660604ad5a0bdae82e1962203a9ef3c5f [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) {
Brad Bishop819ddd42016-10-05 21:19:19 -040030 fprintf(stderr, "Failed to get %s busname: %s\n",
31 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -050032 goto final;
33 }
Chris Austen41a4b312015-10-25 03:45:42 -050034
35 r = sd_bus_message_new_method_call(bus,&m,busname,objname,busname,"getObjectFromByteId");
36 if (r < 0) {
37 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
38 }
39
40 r = sd_bus_message_append(m, "sy", type, num);
41 if (r < 0) {
42 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
43 }
44
45 // Call the IPMI responder on the bus so the message can be sent to the CEC
46 r = sd_bus_call(bus, m, 0, &error, &reply);
47 if (r < 0) {
48 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
49 goto final;
50 }
51
52
53 r = sd_bus_message_read(reply, "(sss)", &str1, &str2, &str3);
54 if (r < 0) {
55 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
56 goto final;
57 }
58
59 strncpy(interface->bus, str1, MAX_DBUS_PATH);
60 strncpy(interface->path, str2, MAX_DBUS_PATH);
61 strncpy(interface->interface, str3, MAX_DBUS_PATH);
62
63 interface->sensornumber = num;
64
65final:
66
67 sd_bus_error_free(&error);
68 sd_bus_message_unref(m);
Sergey Solomineb9b8142016-08-23 09:07:28 -050069 free (busname);
Chris Austen41a4b312015-10-25 03:45:42 -050070
71 return r;
72}
73
74
75
76
77int main(int argc, char *argv[])
78{
79 int base;
80 char *endptr, *str;
81 long val;
82 uint16_t num;
83 int r;
84
85 if (argc < 2) {
86 fprintf(stderr, "Usage: %s sensornumber\n", argv[0]);
87 return -1;
88 }
89
90 str = argv[1];
91 base = (argc > 2) ? atoi(argv[2]) : 10;
92
93 val = strtol(str, &endptr, base);
94
95 num = (uint16_t) val;
96
97
98
99 /* Connect to system bus */
100 r = sd_bus_open_system(&bus);
101 if (r < 0) {
102 fprintf(stderr, "Failed to connect to system bus: %s\n",
103 strerror(-r));
104 goto finish;
105 }
106
107 send_esel(num);
108
109
110finish:
111 sd_bus_unref(bus);
112
113 return 0;
Brad Bishop819ddd42016-10-05 21:19:19 -0400114}