William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 1 | #include "globalhandler.h" |
Patrick Williams | 37af733 | 2016-09-02 21:21:42 -0500 | [diff] [blame] | 2 | #include "host-ipmid/ipmid-api.h" |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <stdint.h> |
Brad Bishop | 30be0f7 | 2016-10-05 23:39:41 -0400 | [diff] [blame] | 6 | #include <mapper.h> |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 7 | |
| 8 | const char *control_object_name = "/org/openbmc/control/bmc0"; |
| 9 | const char *control_intf_name = "org.openbmc.control.Bmc"; |
| 10 | |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 11 | void register_netfn_global_functions() __attribute__((constructor)); |
| 12 | |
Nan Li | bc75988 | 2016-08-22 15:00:21 +0800 | [diff] [blame] | 13 | int dbus_reset(const char *method) |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 14 | { |
| 15 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 16 | sd_bus_message *m = NULL; |
| 17 | sd_bus *bus = NULL; |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 18 | char* connection = NULL; |
Matthew Barth | 8b47005 | 2016-09-21 10:02:57 -0500 | [diff] [blame] | 19 | int r; |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 20 | |
Brad Bishop | 30be0f7 | 2016-10-05 23:39:41 -0400 | [diff] [blame] | 21 | bus = ipmid_get_sd_bus_connection(); |
| 22 | r = mapper_get_service(bus, control_object_name, &connection); |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 23 | if (r < 0) { |
Brad Bishop | 30be0f7 | 2016-10-05 23:39:41 -0400 | [diff] [blame] | 24 | fprintf(stderr, "Failed to get connection for %s: %s\n", |
| 25 | control_object_name, strerror(-r)); |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 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. |
Nan Li | bc75988 | 2016-08-22 15:00:21 +0800 | [diff] [blame] | 39 | */ |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 40 | r = sd_bus_call_method(bus, |
| 41 | connection, /* service to contact */ |
| 42 | control_object_name, /* object path */ |
| 43 | control_intf_name, /* interface name */ |
Nan Li | bc75988 | 2016-08-22 15:00:21 +0800 | [diff] [blame] | 44 | method, /* method name */ |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 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. |
Nan Li | bc75988 | 2016-08-22 15:00:21 +0800 | [diff] [blame] | 71 | dbus_reset("warmReset"); |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 72 | |
| 73 | // Status code. |
| 74 | ipmi_ret_t rc = IPMI_CC_OK; |
| 75 | *data_len = 0; |
| 76 | return rc; |
| 77 | } |
| 78 | |
Nan Li | bc75988 | 2016-08-22 15:00:21 +0800 | [diff] [blame] | 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 | } |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 93 | |
| 94 | void register_netfn_global_functions() |
| 95 | { |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame^] | 96 | // Cold Reset |
Nan Li | bc75988 | 2016-08-22 15:00:21 +0800 | [diff] [blame] | 97 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_COLD_RESET); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame^] | 98 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_COLD_RESET, NULL, ipmi_global_cold_reset, |
| 99 | PRIVILEGE_ADMIN); |
Nan Li | bc75988 | 2016-08-22 15:00:21 +0800 | [diff] [blame] | 100 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame^] | 101 | // <Warm Reset> |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 102 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WARM_RESET); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame^] | 103 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_WARM_RESET, NULL, ipmi_global_warm_reset, |
| 104 | PRIVILEGE_ADMIN); |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 105 | |
Ratan Gupta | eedf4f5 | 2016-02-25 18:53:52 +0530 | [diff] [blame] | 106 | return; |
William | cb8ac88 | 2015-12-31 19:15:17 +0800 | [diff] [blame] | 107 | } |