summaryrefslogtreecommitdiff
path: root/src/power_commands.c
blob: 33f1366ca7bfe39827fac5979f5a2a604aa3fea1 (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
180
181
182
183
184
185
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include<d_bus.h>

#include "utils.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 struct
{
    int command;
    const string_quadruple_t * path;
}command_dbus_path_t;

typedef struct
{
    int command;
    const char* value;
}command_dbus_value_t;

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;

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 }
};

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

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

}power_policy_command_t;

typedef struct
{
    const char* name;
    int command;
    const char* help;
}universal_command_t;

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 }
};

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 }
};

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 }
};

const command_dbus_value_t power_command_policy_values[] = {
    { POWER_POLICY_ON, "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn" },
    { POWER_POLICY_OFF, "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOff" },
    { POWER_POLICY_RESTORE, "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore" },
    { 0, NULL }
};

static void print_power_help(const universal_command_t* cmds)
{
    printf("Power commands list: \n");
    for(const universal_command_t* current=cmds;current->name!=NULL;current++)
    {
        printf("\t %s - %s \n", current->name, current->help);
    }
}

static power_command_t parse_power_command(const char* command, const universal_command_t* cmds)
{
    power_command_t result = POWER_UNKNOWN;
    for(const universal_command_t* current=cmds;current->name!=NULL;current++)
    {
        if(strcmp(command, current->name)==0)
            result = current->command;
    }
    return result;
}

const string_quadruple_t* get_dbus_interface(power_command_t cmd, const command_dbus_path_t* pathes)
{
    for(const command_dbus_path_t* current = pathes; current->path!=NULL; current++)
    {
        if(current->command == cmd)return current->path;
    }
    return NULL;
}

const char* get_dbus_value(power_command_t cmd, const command_dbus_value_t* values)
{
    for(const command_dbus_value_t* current = values; current->value!=NULL; current++)
    {
        if(current->command == cmd)return current->value;
    }
    return NULL;
}


int com_dbus_property( char *arg, const universal_command_t* commands, const command_dbus_path_t* paths, const command_dbus_value_t* values)
{
    power_command_t cmd = parse_power_command(arg, commands);
    if(cmd==POWER_UNKNOWN) {
        fprintf(stderr, "Unknown command: %s\n", arg);
        print_power_help(commands);
        return -EOPNOTSUPP;
    }
    const string_quadruple_t* dbus_power_interface = get_dbus_interface(cmd, paths);
    if(!dbus_power_interface)
        return -EOPNOTSUPP;
    const char* property_val =  get_dbus_value(cmd, values);
    if(!property_val)
        return -EOPNOTSUPP;
    return dbus_set_property_string(dbus_power_interface, property_val);
    return 0;
}
int com_power( char *arg )
{
    return com_dbus_property(arg, power_commands, power_command_paths, power_command_values);
}

int com_power_policy( char* arg )
{
    return com_dbus_property(arg, power_policy_commands, power_command_policy_paths, power_command_policy_values);
}