vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <errno.h> |
| 4 | #include <systemd/sd-bus.h> |
| 5 | |
Joel Stanley | 4377683 | 2015-11-25 17:28:33 +1030 | [diff] [blame] | 6 | #include "ipmid-api.h" |
| 7 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 8 | void register_host_services() __attribute__((constructor)); |
| 9 | |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 10 | // OpenBMC Host IPMI dbus framework |
| 11 | const char *bus_name = "org.openbmc.HostIpmi"; |
| 12 | const char *object_name = "/org/openbmc/HostIpmi/1"; |
| 13 | const char *intf_name = "org.openbmc.HostIpmi"; |
| 14 | |
| 15 | //------------------------------------------------------------------- |
| 16 | // Gets called by PowerOff handler when a Soft Power off is requested |
| 17 | //------------------------------------------------------------------- |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 18 | static int soft_power_off(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 19 | { |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 20 | int64_t bt_resp = -1; |
| 21 | int rc = 0; |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 22 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 23 | // Steps to be taken when we get this. |
| 24 | // 1: Send a SMS_ATN to the Host |
| 25 | // 2: Host receives it and sends a GetMsgFlags IPMI command |
| 26 | // 3: IPMID app handler will respond to that with a MSgFlag with bit:0x2 |
| 27 | // set indicating we have a message for Host |
| 28 | // 4: Host sends a GetMsgBuffer command and app handler will respond to |
| 29 | // that with a OEM-SEL with certain fields packed indicating to the |
| 30 | // host that it do a shutdown of the partitions. |
| 31 | // 5: Host does the partition shutdown and calls Chassis Power off command |
| 32 | // 6: App handler handles the command by making a call to ChassisManager |
| 33 | // Dbus |
| 34 | |
| 35 | // Now the job is to send the SMS_ATTN. |
| 36 | |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 37 | // Req message contains the specifics about which method etc that we want to |
| 38 | // access on which bus, object |
| 39 | sd_bus_message *response = NULL; |
| 40 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 41 | // Error return mechanism |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 42 | sd_bus_error bus_error = SD_BUS_ERROR_NULL; |
| 43 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 44 | // Gets a hook onto either a SYSTEM or SESSION bus |
| 45 | sd_bus *bus = ipmid_get_sd_bus_connection(); |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 46 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 47 | rc = sd_bus_call_method(bus, // In the System Bus |
| 48 | bus_name, // Service to contact |
| 49 | object_name, // Object path |
| 50 | intf_name, // Interface name |
| 51 | "setAttention", // Method to be called |
| 52 | &bus_error, // object to return error |
| 53 | &response, // Response buffer if any |
| 54 | NULL); // No input arguments |
| 55 | if(rc < 0) |
| 56 | { |
| 57 | fprintf(stderr,"ERROR initiating Power Off:[%s]\n",bus_error.message); |
| 58 | goto finish; |
| 59 | } |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 60 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 61 | // See if we were able to successfully raise SMS_ATN |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 62 | rc = sd_bus_message_read(response, "x", &bt_resp); |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 63 | if (rc < 0) |
| 64 | { |
| 65 | fprintf(stderr, "Failed to get a rc from BT for SMS_ATN: %s\n", strerror(-rc)); |
| 66 | goto finish; |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | finish: |
| 70 | sd_bus_error_free(&bus_error); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 71 | response = sd_bus_message_unref(response); |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 72 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 73 | if(rc < 0) |
| 74 | { |
| 75 | return sd_bus_reply_method_return(m, "x", rc); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | return sd_bus_reply_method_return(m, "x", bt_resp); |
| 80 | } |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | //------------------------------------------- |
| 84 | // Function pointer of APIs exposed via Dbus |
| 85 | //------------------------------------------- |
| 86 | static const sd_bus_vtable host_services_vtable[] = |
| 87 | { |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 88 | SD_BUS_VTABLE_START(0), |
| 89 | // Takes No("") arguments -but- returns a value of type 64 bit integer("x") |
| 90 | SD_BUS_METHOD("SoftPowerOff", "", "x", &soft_power_off, SD_BUS_VTABLE_UNPRIVILEGED), |
| 91 | SD_BUS_VTABLE_END, |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | //------------------------------------------------------ |
| 95 | // Called by IPMID as part of the start up |
| 96 | // ----------------------------------------------------- |
| 97 | int start_host_service(sd_bus *bus, sd_bus_slot *slot) |
| 98 | { |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 99 | int rc = 0; |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 100 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 101 | /* Install the object */ |
| 102 | rc = sd_bus_add_object_vtable(bus, |
| 103 | &slot, |
| 104 | "/org/openbmc/HostServices", /* object path */ |
| 105 | "org.openbmc.HostServices", /* interface name */ |
| 106 | host_services_vtable, |
| 107 | NULL); |
| 108 | if (rc < 0) |
| 109 | { |
| 110 | fprintf(stderr, "Failed to issue method call: %s\n", strerror(-rc)); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | /* Take one in OpenBmc */ |
| 115 | rc = sd_bus_request_name(bus, "org.openbmc.HostServices", 0); |
| 116 | if (rc < 0) |
| 117 | { |
| 118 | fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-rc)); |
| 119 | } |
| 120 | } |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 121 | |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 122 | return rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
| 123 | } |
| 124 | |
| 125 | //------------------------------------------------------ |
| 126 | // Callback register function |
| 127 | // ----------------------------------------------------- |
| 128 | void register_host_services() |
| 129 | { |
| 130 | // Gets a hook onto SYSTEM bus used by host-ipmid |
| 131 | sd_bus *bus = ipmid_get_sd_bus_connection(); |
| 132 | |
| 133 | // Gets a hook onto SYSTEM bus slot used by host-ipmid |
| 134 | sd_bus_slot *ipmid_slot = ipmid_get_sd_bus_slot(); |
| 135 | |
| 136 | //start_host_service(bus, ipmid_slot); |
| 137 | start_host_service(bus, ipmid_slot); |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 138 | } |