1 #include "globalhandler.h"
2 #include "host-ipmid/ipmid-api.h"
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdint.h>
6 #include <mapper.h>
7 
8 const char  *control_object_name  =  "/org/openbmc/control/bmc0";
9 const char  *control_intf_name    =  "org.openbmc.control.Bmc";
10 
11 void register_netfn_global_functions() __attribute__((constructor));
12 
13 int dbus_reset(const char *method)
14 {
15     sd_bus_error error = SD_BUS_ERROR_NULL;
16     sd_bus_message *m = NULL;
17     sd_bus *bus = NULL;
18     char* connection = NULL;
19     int r;
20 
21     bus = ipmid_get_sd_bus_connection();
22     r = mapper_get_service(bus, control_object_name, &connection);
23     if (r < 0) {
24         fprintf(stderr, "Failed to get connection for %s: %s\n",
25                 control_object_name, strerror(-r));
26         goto finish;
27     }
28 
29     printf("connection: %s\n", connection);
30 
31     // Open the system bus where most system services are provided.
32     bus = ipmid_get_sd_bus_connection();
33 
34     /*
35      * Bus, service, object path, interface and method are provided to call
36      * the method.
37      * Signatures and input arguments are provided by the arguments at the
38      * end.
39      */
40     r = sd_bus_call_method(bus,
41             connection,                                /* service to contact */
42             control_object_name,                       /* object path */
43             control_intf_name,                         /* interface name */
44             method,                               /* method name */
45             &error,                                    /* object to return error in */
46             &m,                                        /* return message on success */
47             NULL,
48             NULL
49             );
50 
51     if (r < 0) {
52         fprintf(stderr, "Failed to issue method call: %s\n", error.message);
53         goto finish;
54     }
55 
56 finish:
57     sd_bus_error_free(&error);
58     sd_bus_message_unref(m);
59     free(connection);
60 
61     return r;
62 }
63 
64 ipmi_ret_t ipmi_global_warm_reset(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
65                               ipmi_request_t request, ipmi_response_t response,
66                               ipmi_data_len_t data_len, ipmi_context_t context)
67 {
68     printf("Handling GLOBAL warmReset Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
69 
70     // TODO: call the correct dbus method for warmReset.
71     dbus_reset("warmReset");
72 
73     // Status code.
74     ipmi_ret_t rc = IPMI_CC_OK;
75     *data_len = 0;
76     return rc;
77 }
78 
79 ipmi_ret_t ipmi_global_cold_reset(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
80                               ipmi_request_t request, ipmi_response_t response,
81                               ipmi_data_len_t data_len, ipmi_context_t context)
82 {
83     printf("Handling GLOBAL coldReset Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
84 
85     // TODO: call the correct dbus method for coldReset.
86     dbus_reset("coldReset");
87 
88     // Status code.
89     ipmi_ret_t rc = IPMI_CC_OK;
90     *data_len = 0;
91     return rc;
92 }
93 
94 void register_netfn_global_functions()
95 {
96     // Cold Reset
97     printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_COLD_RESET);
98     ipmi_register_callback(NETFUN_APP, IPMI_CMD_COLD_RESET, NULL, ipmi_global_cold_reset,
99                            PRIVILEGE_ADMIN);
100 
101     // <Warm Reset>
102     printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WARM_RESET);
103     ipmi_register_callback(NETFUN_APP, IPMI_CMD_WARM_RESET, NULL, ipmi_global_warm_reset,
104                            PRIVILEGE_ADMIN);
105 
106     return;
107 }
108