blob: cc4743979ef133b58def2b55084cd5c041cdcf64 [file] [log] [blame]
vishwa36993272015-11-20 12:43:49 -06001#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <systemd/sd-bus.h>
5
Joel Stanley43776832015-11-25 17:28:33 +10306#include "ipmid-api.h"
7
vishwab9f559a2016-01-13 01:53:08 -06008void register_host_services() __attribute__((constructor));
9
vishwa36993272015-11-20 12:43:49 -060010// OpenBMC Host IPMI dbus framework
11const char *bus_name = "org.openbmc.HostIpmi";
12const char *object_name = "/org/openbmc/HostIpmi/1";
13const char *intf_name = "org.openbmc.HostIpmi";
14
15//-------------------------------------------------------------------
16// Gets called by PowerOff handler when a Soft Power off is requested
17//-------------------------------------------------------------------
vishwab9f559a2016-01-13 01:53:08 -060018static int soft_power_off(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
vishwa36993272015-11-20 12:43:49 -060019{
vishwab9f559a2016-01-13 01:53:08 -060020 int64_t bt_resp = -1;
21 int rc = 0;
vishwa36993272015-11-20 12:43:49 -060022
vishwab9f559a2016-01-13 01:53:08 -060023 // 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
vishwa36993272015-11-20 12:43:49 -060037 // 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
vishwab9f559a2016-01-13 01:53:08 -060041 // Error return mechanism
vishwa36993272015-11-20 12:43:49 -060042 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
43
vishwab9f559a2016-01-13 01:53:08 -060044 // Gets a hook onto either a SYSTEM or SESSION bus
45 sd_bus *bus = ipmid_get_sd_bus_connection();
vishwa36993272015-11-20 12:43:49 -060046
vishwab9f559a2016-01-13 01:53:08 -060047 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 }
vishwa36993272015-11-20 12:43:49 -060060
vishwab9f559a2016-01-13 01:53:08 -060061 // See if we were able to successfully raise SMS_ATN
vishwa36993272015-11-20 12:43:49 -060062 rc = sd_bus_message_read(response, "x", &bt_resp);
vishwab9f559a2016-01-13 01:53:08 -060063 if (rc < 0)
64 {
65 fprintf(stderr, "Failed to get a rc from BT for SMS_ATN: %s\n", strerror(-rc));
66 goto finish;
vishwa36993272015-11-20 12:43:49 -060067 }
68
69finish:
70 sd_bus_error_free(&bus_error);
vishwa1eaea4f2016-02-26 11:57:40 -060071 response = sd_bus_message_unref(response);
vishwa36993272015-11-20 12:43:49 -060072
vishwab9f559a2016-01-13 01:53:08 -060073 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 }
vishwa36993272015-11-20 12:43:49 -060081}
82
83//-------------------------------------------
84// Function pointer of APIs exposed via Dbus
85//-------------------------------------------
86static const sd_bus_vtable host_services_vtable[] =
87{
vishwab9f559a2016-01-13 01:53:08 -060088 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,
vishwa36993272015-11-20 12:43:49 -060092};
93
94//------------------------------------------------------
95// Called by IPMID as part of the start up
96// -----------------------------------------------------
97int start_host_service(sd_bus *bus, sd_bus_slot *slot)
98{
vishwab9f559a2016-01-13 01:53:08 -060099 int rc = 0;
vishwa36993272015-11-20 12:43:49 -0600100
vishwab9f559a2016-01-13 01:53:08 -0600101 /* 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 }
vishwa36993272015-11-20 12:43:49 -0600121
vishwab9f559a2016-01-13 01:53:08 -0600122 return rc < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
123}
124
125//------------------------------------------------------
126// Callback register function
127// -----------------------------------------------------
128void 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);
vishwa36993272015-11-20 12:43:49 -0600138}