summaryrefslogtreecommitdiff
path: root/src/power_commands.c
blob: e91b31dcac5134ffd685bfa40abd36b23b1e3408 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "utils.h"

#include "d_bus_commands.h"
#include "power_commands.h"

static const string_quadruple_t chassis_dbus = {
    "xyz.openbmc_project.State.Chassis",
    "/xyz/openbmc_project/state/chassis0",
    "xyz.openbmc_project.State.Chassis",
    "RequestedPowerTransition"};

static const string_quadruple_t host_dbus = {
    "xyz.openbmc_project.State.Host",
    "/xyz/openbmc_project/state/host0",
    "xyz.openbmc_project.State.Host",
    "RequestedHostTransition"};

static const string_quadruple_t policy_dbus = {
    "xyz.openbmc_project.Settings",
    "/xyz/openbmc_project/control/host0/power_restore_policy",
    "xyz.openbmc_project.Control.Power.RestorePolicy",
    "PowerRestorePolicy"};


typedef enum
{
    POWER_UNKNOWN=-1,
    POWER_ON_GRACEFUL=100,
    POWER_ON_FORCE,
    POWER_OFF_GRACEFUL,
    POWER_OFF_FORCE,
    POWER_RESET_GRACEFUL,
    POWER_RESET_FORCE
}power_command_t;

static const command_dbus_path_t power_command_paths[] = {
    { POWER_ON_GRACEFUL, &host_dbus },
    { POWER_ON_FORCE, &host_dbus },
    { POWER_OFF_GRACEFUL, &host_dbus },
    { POWER_OFF_FORCE, &chassis_dbus },
    { POWER_RESET_GRACEFUL, &host_dbus },
    { POWER_RESET_FORCE, &host_dbus },
    { 0, NULL }
};

static const command_dbus_member_t power_command_values[] = {
    { POWER_ON_GRACEFUL, {.type="s", .string="xyz.openbmc_project.State.Host.Transition.On" }},
    { POWER_ON_FORCE, {.type="s",  .string="xyz.openbmc_project.State.Host.Transition.On" }},
    { POWER_OFF_GRACEFUL, {.type="s", .string="xyz.openbmc_project.State.Host.Transition.Off" }},
    { POWER_OFF_FORCE, {.type="s", .string="xyz.openbmc_project.State.Chassis.Transition.Off" }},
    { POWER_RESET_GRACEFUL, {.type="s", .string="xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot" }},
    { POWER_RESET_FORCE, {.type="s", .string="xyz.openbmc_project.State.Host.Transition.ForceWarmReboot" }},
    { 0, {.type=NULL, .integer=0}}
};

typedef enum
{
    POWER_POLICY_UNKNOWN=-1,
    POWER_POLICY_ON=100,
    POWER_POLICY_OFF,
    POWER_POLICY_RESTORE

}power_policy_command_t;


static universal_command_t power_commands[] = {
    { "on-graceful", POWER_ON_GRACEFUL, "Graceful Power On" },
    { "on", POWER_ON_GRACEFUL, "Graceful Power On" },
    { "on-force", POWER_ON_FORCE, "Force Power On" },
    { "off-graceful", POWER_OFF_GRACEFUL, "Graceful Power Off" },
    { "off", POWER_OFF_GRACEFUL, "Graceful Power Off" },
    { "off-force", POWER_OFF_FORCE, "Force Power Off" },
    { "reset-graceful", POWER_RESET_GRACEFUL, "Graceful Reset" },
    { "reset", POWER_RESET_GRACEFUL, "Graceful Reset" },
    { "reset-force", POWER_RESET_FORCE, "Force Reset" },
    { NULL, 0, NULL }
};

static const universal_command_t power_policy_commands[] = {
    {"always-on", POWER_POLICY_ON, "Server is always ON"},
    {"always-off", POWER_POLICY_OFF, "Server is always ON"},
    {"restore", POWER_POLICY_RESTORE, "Server restores previous state"},
    { NULL, 0, NULL }
};

static const command_dbus_path_t power_command_policy_paths[] = {
    { POWER_POLICY_ON, &policy_dbus },
    { POWER_POLICY_OFF, &policy_dbus },
    { POWER_POLICY_RESTORE, &policy_dbus },
    { 0, NULL }
};

static const command_dbus_member_t power_command_policy_values[] = {
    { POWER_POLICY_ON, {.type="s", .string="xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn" }},
    { POWER_POLICY_OFF, {.type="s", .string="xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff" }},
    { POWER_POLICY_RESTORE, {.type="s", .string="xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore" }},
    { 0, {.type=NULL, .integer=0 }}
};

int com_status(const universal_command_t *commands, const command_dbus_path_t * paths, const command_dbus_member_t * values)
{
    for(const command_dbus_path_t * cur_path = paths;cur_path->path !=NULL; cur_path++)
    {
        _cleanup_string_ char * value = dbus_get_property_string(cur_path->path);
        if(!value)continue;
        const command_dbus_member_t* member = get_dbus_command_by_member(value, values);
        if(!member)continue;
        const universal_command_t* cur_cmd = get_command_by_enum(member->command, commands);
        if(!cur_cmd)
            continue;
        printf("Current status: %s - %s\n", cur_cmd->name, cur_cmd->help);

    }
    return 0;
}

static int power_status()
{

        _cleanup_string_ char * host_value = dbus_get_property_string(&host_dbus);
        if(!host_value)
            return -EOPNOTSUPP;
        const command_dbus_member_t* member = get_dbus_command_by_member(host_value, power_command_values);
        if(!member)
            return -EOPNOTSUPP;
        const universal_command_t* cur_cmd = get_command_by_enum(member->command, power_commands);
        if(!cur_cmd)
            return -EOPNOTSUPP;
        printf("Power status: \n");
        printf("\tHost: %s - %s\n", cur_cmd->name, cur_cmd->help);
        _cleanup_string_ char * chassis_value = dbus_get_property_string(&chassis_dbus);
        if(!host_value)
            return -EOPNOTSUPP;
        member = get_dbus_command_by_member(host_value, power_command_values);
        if(!member)
            return -EOPNOTSUPP;
        cur_cmd = get_command_by_enum(member->command, power_commands);
        if(!cur_cmd)
            return -EOPNOTSUPP;

        printf("\tChassis: %s - %s\n", cur_cmd->name, cur_cmd->help);


    return 0;
}

static int power_policy_status()
{

        _cleanup_string_ char * host_value = dbus_get_property_string(&host_dbus);
        if(!host_value)
            return -EOPNOTSUPP;
        const command_dbus_member_t* member = get_dbus_command_by_member(host_value, power_command_values);
        if(!member)
            return -EOPNOTSUPP;
        const universal_command_t* cur_cmd = get_command_by_enum(member->command, power_commands);
        if(!cur_cmd)
            return -EOPNOTSUPP;
        printf("Power policy tatus: \n");
        printf("\tHost: %s - %s\n", cur_cmd->name, cur_cmd->help);

    return 0;
}

int com_power( char *arg )
{
    if(strcmp(arg, "status")==0)return power_status();
    return com_dbus_property(arg, power_commands, power_command_paths, power_command_values);
}

int com_power_policy( char* arg )
{
    if(strcmp(arg, "status")==0)return power_policy_status();
    return com_dbus_property(arg, power_policy_commands, power_command_policy_paths, power_command_policy_values);
}