blob: 2c3c99472a2119a2160e3749c4d9e8a65c8e38ba [file] [log] [blame]
Williamcb8ac882015-12-31 19:15:17 +08001#include "globalhandler.h"
Patrick Williams37af7332016-09-02 21:21:42 -05002#include "host-ipmid/ipmid-api.h"
Williamcb8ac882015-12-31 19:15:17 +08003#include <stdio.h>
4#include <string.h>
5#include <stdint.h>
6
7const char *control_object_name = "/org/openbmc/control/bmc0";
8const char *control_intf_name = "org.openbmc.control.Bmc";
9
Brad Bishopfa97b0c2016-06-14 20:34:44 -040010const char *objectmapper_service_name = "org.openbmc.ObjectMapper";
11const char *objectmapper_object_name = "/org/openbmc/ObjectMapper";
12const char *objectmapper_intf_name = "org.openbmc.ObjectMapper";
Williamcb8ac882015-12-31 19:15:17 +080013
14void register_netfn_global_functions() __attribute__((constructor));
15
16int obj_mapper_get_connection(char** buf, const char* obj_path)
17{
18 sd_bus_error error = SD_BUS_ERROR_NULL;
19 sd_bus_message *m = NULL;
20 sd_bus *bus = NULL;
21 char *temp_buf = NULL, *intf = NULL;
22 size_t buf_size = 0;
23 int r;
24
25 //Get the system bus where most system services are provided.
26 bus = ipmid_get_sd_bus_connection();
27
28 /*
29 * Bus, service, object path, interface and method are provided to call
30 * the method.
31 * Signatures and input arguments are provided by the arguments at the
32 * end.
33 */
34 r = sd_bus_call_method(bus,
35 objectmapper_service_name, /* service to contact */
36 objectmapper_object_name, /* object path */
37 objectmapper_intf_name, /* interface name */
38 "GetObject", /* method name */
39 &error, /* object to return error in */
40 &m, /* return message on success */
41 "s", /* input signature */
42 obj_path /* first argument */
43 );
44
45 if (r < 0) {
46 fprintf(stderr, "Failed to issue method call: %s\n", error.message);
47 goto finish;
48 }
49
50 // Get the key, aka, the connection name
51 sd_bus_message_read(m, "a{sas}", 1, &temp_buf, 1, &intf);
52
53 /*
54 * TODO: check the return code. Currently for no reason the message
55 * parsing of object mapper is always complaining about
56 * "Device or resource busy", but the result seems OK for now. Need
57 * further checks.
58 */
59
60 buf_size = strlen(temp_buf) + 1;
61 printf("IPMID connection name: %s\n", temp_buf);
62 *buf = (char*)malloc(buf_size);
63
64 if (*buf == NULL) {
65 fprintf(stderr, "Malloc failed for warm reset");
66 r = -1;
67 goto finish;
68 }
69
70 memcpy(*buf, temp_buf, buf_size);
71
72finish:
73 sd_bus_error_free(&error);
74 sd_bus_message_unref(m);
75
76 return r;
77}
78
Nan Libc759882016-08-22 15:00:21 +080079
80int dbus_reset(const char *method)
Williamcb8ac882015-12-31 19:15:17 +080081{
82 sd_bus_error error = SD_BUS_ERROR_NULL;
83 sd_bus_message *m = NULL;
84 sd_bus *bus = NULL;
Williamcb8ac882015-12-31 19:15:17 +080085 char* connection = NULL;
Matthew Barth8b470052016-09-21 10:02:57 -050086 int r;
Williamcb8ac882015-12-31 19:15:17 +080087
88 r = obj_mapper_get_connection(&connection, control_object_name);
89 if (r < 0) {
90 fprintf(stderr, "Failed to get connection, return value: %d.\n", r);
91 goto finish;
92 }
93
94 printf("connection: %s\n", connection);
95
96 // Open the system bus where most system services are provided.
97 bus = ipmid_get_sd_bus_connection();
98
99 /*
100 * Bus, service, object path, interface and method are provided to call
101 * the method.
102 * Signatures and input arguments are provided by the arguments at the
103 * end.
Nan Libc759882016-08-22 15:00:21 +0800104 */
Williamcb8ac882015-12-31 19:15:17 +0800105 r = sd_bus_call_method(bus,
106 connection, /* service to contact */
107 control_object_name, /* object path */
108 control_intf_name, /* interface name */
Nan Libc759882016-08-22 15:00:21 +0800109 method, /* method name */
Williamcb8ac882015-12-31 19:15:17 +0800110 &error, /* object to return error in */
111 &m, /* return message on success */
112 NULL,
113 NULL
114 );
115
116 if (r < 0) {
117 fprintf(stderr, "Failed to issue method call: %s\n", error.message);
118 goto finish;
119 }
120
121finish:
122 sd_bus_error_free(&error);
123 sd_bus_message_unref(m);
124 free(connection);
125
126 return r;
127}
128
129ipmi_ret_t ipmi_global_warm_reset(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
130 ipmi_request_t request, ipmi_response_t response,
131 ipmi_data_len_t data_len, ipmi_context_t context)
132{
133 printf("Handling GLOBAL warmReset Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
134
135 // TODO: call the correct dbus method for warmReset.
Nan Libc759882016-08-22 15:00:21 +0800136 dbus_reset("warmReset");
Williamcb8ac882015-12-31 19:15:17 +0800137
138 // Status code.
139 ipmi_ret_t rc = IPMI_CC_OK;
140 *data_len = 0;
141 return rc;
142}
143
Nan Libc759882016-08-22 15:00:21 +0800144ipmi_ret_t ipmi_global_cold_reset(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
145 ipmi_request_t request, ipmi_response_t response,
146 ipmi_data_len_t data_len, ipmi_context_t context)
147{
148 printf("Handling GLOBAL coldReset Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
149
150 // TODO: call the correct dbus method for coldReset.
151 dbus_reset("coldReset");
152
153 // Status code.
154 ipmi_ret_t rc = IPMI_CC_OK;
155 *data_len = 0;
156 return rc;
157}
Williamcb8ac882015-12-31 19:15:17 +0800158
159void register_netfn_global_functions()
160{
Nan Libc759882016-08-22 15:00:21 +0800161 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_COLD_RESET);
162 ipmi_register_callback(NETFUN_APP, IPMI_CMD_COLD_RESET, NULL, ipmi_global_cold_reset);
163
Williamcb8ac882015-12-31 19:15:17 +0800164 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WARM_RESET);
165 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WARM_RESET, NULL, ipmi_global_warm_reset);
166
Ratan Guptaeedf4f52016-02-25 18:53:52 +0530167 return;
Williamcb8ac882015-12-31 19:15:17 +0800168}