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