diff options
Diffstat (limited to 'src/power_commands.c')
-rw-r--r-- | src/power_commands.c | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/src/power_commands.c b/src/power_commands.c new file mode 100644 index 0000000..e91b31d --- /dev/null +++ b/src/power_commands.c @@ -0,0 +1,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); +} |