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