summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeniy Alexeev <e.alekseev@k-soft-spb.ru>2022-10-07 17:18:14 +0300
committerEvgeniy Alexeev <e.alekseev@k-soft-spb.ru>2022-10-07 17:18:14 +0300
commit224e5b8317e7e3ffbb04ada342447bf678297002 (patch)
tree5cde381c8d3ebf703e653fbbcd60a223dd1ddd34
parent94427ebcf778a2c0cc148c0344002491abdb052c (diff)
downloadsila-shell-224e5b8317e7e3ffbb04ada342447bf678297002.tar.xz
Add power policy commands, refactor
-rw-r--r--meson.build2
-rw-r--r--src/d_bus.c (renamed from src/sd_bus.c)51
-rw-r--r--src/d_bus.h37
-rw-r--r--src/main.c10
-rw-r--r--src/power_commands.c190
-rw-r--r--src/power_commands.h1
-rw-r--r--src/sd_bus.h19
-rw-r--r--src/utils.h8
8 files changed, 170 insertions, 148 deletions
diff --git a/meson.build b/meson.build
index 7990c18..6a5cefe 100644
--- a/meson.build
+++ b/meson.build
@@ -10,7 +10,7 @@ src = [
'src/utils.c',
'src/shell.c',
'src/users.c',
- 'src/sd_bus.c',
+ 'src/d_bus.c',
'src/power_commands.c'
]
diff --git a/src/sd_bus.c b/src/d_bus.c
index 3dc3eb0..79405f5 100644
--- a/src/sd_bus.c
+++ b/src/d_bus.c
@@ -6,13 +6,10 @@
#include <systemd/sd-bus.h>
#include <utils.h>
-#include <sd_bus.h>
+#include <d_bus.h>
-int dbus_set_property_string(const char *destination,
- const char *path,
- const char *interface,
- const char *member, const char* value)
+int dbus_set_property_string(const string_quadruple_t* path, const char* value)
{
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
@@ -27,10 +24,10 @@ int dbus_set_property_string(const char *destination,
r = sd_bus_set_property(
bus,
- destination,
- path,
- interface,
- member,
+ path->first,
+ path->second,
+ path->third,
+ path->fourth,
&error,
"s",
value);
@@ -52,7 +49,7 @@ const char* dbus_get_property_string(const char *destination,
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
_cleanup_bus_unref_ sd_bus *bus = NULL;
- const char *state = NULL;
+ char *state = NULL;
int r;
/* Connect to the system bus */
@@ -82,37 +79,3 @@ const char* dbus_get_property_string(const char *destination,
return (r>=0?(state):(NULL));
}
-int cmd_get_string()
-{
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- _cleanup_bus_unref_ sd_bus *bus = NULL;
- _cleanup_free_ char *state = NULL;
- int r;
-
- /* Connect to the system bus */
- r = sd_bus_open_system(&bus);
- if (r >= 0)
- {
-
- r = sd_bus_get_property_string(
- bus,
- "org.bluez",
- "/org/bluez/hci0",
- "org.bluez.Adapter1",
- "Alias",
- &error,
- &state);
- if (r >= 0)
- {
- printf("Result: %s\n", state);
- }
- /* Issue the method call and store the respons message in m */
- }
- if (r < 0)
- {
- fprintf(stderr, "Failed to issue dbus call: %d %s\n", r, error.message);
- }
-
- return (r>0?(0):(r));
-}
diff --git a/src/d_bus.h b/src/d_bus.h
new file mode 100644
index 0000000..c2742d0
--- /dev/null
+++ b/src/d_bus.h
@@ -0,0 +1,37 @@
+#ifndef __SD_BUS_COMMANDS_H
+#define __SD_BUS_COMMANDS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define _cleanup_bus_unref_ _cleanup_(sd_bus_unrefp)
+#define _cleanup_bus_close_unref_ _cleanup_(sd_bus_close_unrefp)
+#define _cleanup_bus_slot_unref_ _cleanup_(sd_bus_slot_unrefp)
+#define _cleanup_bus_message_unref_ _cleanup_(sd_bus_message_unrefp)
+#define _cleanup_bus_creds_unref_ _cleanup_(sd_bus_creds_unrefp)
+#define _cleanup_bus_track_unref_ _cleanup_(sd_bus_slot_unrefp)
+#define _cleanup_bus_error_free_ _cleanup_(sd_bus_error_free)
+
+
+typedef struct
+{ const char* first;
+ const char* second;
+}string_pair_t;
+
+typedef struct
+{ const char* first;
+ const char* second;
+ const char* third;
+ const char* fourth;
+}string_quadruple_t;
+
+int cmd_get_string();
+
+int dbus_set_property_string(const string_quadruple_t* path, const char* value);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //__SD_BUS_COMMANDS_H
diff --git a/src/main.c b/src/main.c
index 3bf2e5a..529436c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -21,7 +21,7 @@
#include <shell.h>
#include <users.h>
#include <power_commands.h>
-#include <sd_bus.h>
+#include <d_bus.h>
char *progname; /* The name of this program, as taken from argv[0]. */
int done; /* When non-zero, this global means the user is done using this program. */
@@ -79,7 +79,6 @@ COMMAND shell_admin_list[] = {
{ "more", com_more, "View the contents of FILE" },
{ "vi", com_vi, "Edit the contents of text FILE" },
{ "poweroff", com_poweroff, "Turn off the BMC power" },
- { "get_string", cmd_get_string, "GetProperty" },
{ "..", com_top, "Return to top menu" },
{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
};
@@ -98,7 +97,6 @@ COMMAND shell_operator_list[] = {
{ "stat", com_stat, "Print out statistics on FILE" },
{ "more", com_more, "View the contents of FILE" },
{ "vi", com_vi, "Edit the contents of text FILE" },
- { "get_string", cmd_get_string, "GetProperty" },
{ "..", com_top, "Return to top menu" },
{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
};
@@ -117,7 +115,6 @@ COMMAND shell_user_list[] = {
{ "stat", com_stat, "Print out statistics on FILE" },
{ "more", com_more, "View the contents of FILE" },
{ "vi", com_vi, "Edit the contents of text FILE" },
- { "get_string", cmd_get_string, "GetProperty" },
{ "..", com_top, "Return to top menu" },
{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
};
@@ -164,6 +161,7 @@ COMMAND server_admin_list[] = {
{ "help", com_help, "Display this text" },
{ "?", com_help, "Synonym for `help'" },
{ "power", com_power, "Return to top menu" },
+ { "restore-policy", com_power_policy, "Set power restore policy" },
{ "..", com_top, "Return to top menu" },
{ "quit", com_quit, "Quit using SILA Shell" },
{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
@@ -172,7 +170,8 @@ COMMAND server_admin_list[] = {
COMMAND server_operator_list[] = {
{ "help", com_help, "Display this text" },
{ "?", com_help, "Synonym for `help'" },
- { "power", com_power, "Return to top menu" },
+ { "power", com_power, "Run on/off/reset" },
+ { "restore-policy", com_power_policy, "Set power restore policy" },
{ "..", com_top, "Return to top menu" },
{ "quit", com_quit, "Quit using SILA Shell" },
{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
@@ -182,6 +181,7 @@ COMMAND server_user_list[] = {
{ "help", com_help, "Display this text" },
{ "?", com_help, "Synonym for `help'" },
{ "power", com_power, "Return to top menu" },
+ { "restore-policy", com_power_policy, "Set power restore policy" },
{ "..", com_top, "Return to top menu" },
{ "quit", com_quit, "Quit using SILA Shell" },
{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
diff --git a/src/power_commands.c b/src/power_commands.c
index 144dbe8..33f1366 100644
--- a/src/power_commands.c
+++ b/src/power_commands.c
@@ -1,22 +1,11 @@
#include <stdio.h>
#include <string.h>
+#include <errno.h>
-#include<sd_bus.h>
-#include "power_commands.h"
-#include "errno.h"
-
-typedef struct
-{ const char* first;
- const char* second;
-}string_pair_t;
-
-typedef struct
-{ const char* first;
- const char* second;
- const char* third;
- const char* fourth;
-}string_quadruple_t;
+#include<d_bus.h>
+#include "utils.h"
+#include "power_commands.h"
static const string_quadruple_t chassis_dbus = {
"xyz.openbmc_project.State.Chassis",
@@ -30,6 +19,24 @@ static const string_quadruple_t host_dbus = {
"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,
@@ -41,97 +48,138 @@ typedef enum
POWER_RESET_FORCE
}power_command_t;
-static struct
+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;
- power_command_t command;
+ int command;
const char* help;
-} 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"}
+}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()
+static void print_power_help(const universal_command_t* cmds)
{
- printf("Power commands list: \n");
- for(int i=0;i<(sizeof(commands)/sizeof(commands[0]));i++)
+ printf("Power commands list: \n");
+ for(const universal_command_t* current=cmds;current->name!=NULL;current++)
{
- printf("\t %s - %s \n", commands[i].name, commands[i].help);
+ printf("\t %s - %s \n", current->name, current->help);
}
-
}
-static power_command_t parse_power_command(const char* command)
+static power_command_t parse_power_command(const char* command, const universal_command_t* cmds)
{
power_command_t result = POWER_UNKNOWN;
- for(int i=0;i<(sizeof(commands)/sizeof(commands[0]));i++)
+ for(const universal_command_t* current=cmds;current->name!=NULL;current++)
{
- if(strcmp(command, commands[i].name)==0)
- result = commands[i].command;
+ if(strcmp(command, current->name)==0)
+ result = current->command;
}
return result;
}
-const string_quadruple_t* get_dbus_power_interface(power_command_t cmd)
+const string_quadruple_t* get_dbus_interface(power_command_t cmd, const command_dbus_path_t* pathes)
{
- switch(cmd)
+ for(const command_dbus_path_t* current = pathes; current->path!=NULL; current++)
{
- case POWER_OFF_FORCE: return &chassis_dbus;break;
-
- case POWER_ON_GRACEFUL:
- case POWER_ON_FORCE:
- case POWER_OFF_GRACEFUL:
- case POWER_RESET_GRACEFUL:
- case POWER_RESET_FORCE:
- return &host_dbus;break;
- default: return NULL;
+ if(current->command == cmd)return current->path;
}
+ return NULL;
}
-const char* get_dbus_power_property_value(power_command_t cmd)
+const char* get_dbus_value(power_command_t cmd, const command_dbus_value_t* values)
{
- switch(cmd)
+ for(const command_dbus_value_t* current = values; current->value!=NULL; current++)
{
- case POWER_ON_GRACEFUL:
- case POWER_ON_FORCE:
- return "xyz.openbmc_project.State.Host.Transition.On";break;
- case POWER_OFF_GRACEFUL:
- return "xyz.openbmc_project.State.Host.Transition.Off";break;
- case POWER_OFF_FORCE:
- return "xyz.openbmc_project.State.Chassis.Transition.Off";break;
- case POWER_RESET_GRACEFUL:
- return "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot";break;
- case POWER_RESET_FORCE:
- return "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot";break;
- default: return NULL;
+ if(current->command == cmd)return current->value;
}
+ return NULL;
}
-int com_power( char *arg )
+
+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);
+ power_command_t cmd = parse_power_command(arg, commands);
if(cmd==POWER_UNKNOWN) {
fprintf(stderr, "Unknown command: %s\n", arg);
- print_power_help();
+ print_power_help(commands);
return -EOPNOTSUPP;
}
- const string_quadruple_t* dbus_power_interface = get_dbus_power_interface(cmd);
+ const string_quadruple_t* dbus_power_interface = get_dbus_interface(cmd, paths);
if(!dbus_power_interface)
return -EOPNOTSUPP;
- const char* property_val = get_dbus_power_property_value(cmd);
+ const char* property_val = get_dbus_value(cmd, values);
if(!property_val)
return -EOPNOTSUPP;
- return dbus_set_property_string(dbus_power_interface->first,
- dbus_power_interface->second,
- dbus_power_interface->third,
- dbus_power_interface->fourth,
- property_val);
+ 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);
+}
diff --git a/src/power_commands.h b/src/power_commands.h
index 8bd6bf4..d6b6bf2 100644
--- a/src/power_commands.h
+++ b/src/power_commands.h
@@ -4,5 +4,6 @@
int com_power( char *arg );
+int com_power_policy( char* arg );
#endif // POWER_H
diff --git a/src/sd_bus.h b/src/sd_bus.h
deleted file mode 100644
index ff72b92..0000000
--- a/src/sd_bus.h
+++ /dev/null
@@ -1,19 +0,0 @@
-#ifndef __SD_BUS_COMMANDS_H
-#define __SD_BUS_COMMANDS_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int cmd_get_string();
-
-int dbus_set_property_string(const char *destination,
- const char *path,
- const char *interface,
- const char *member, const char* value);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif //__SD_BUS_COMMANDS_H
diff --git a/src/utils.h b/src/utils.h
index 92ff61d..841579d 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -11,7 +11,6 @@ extern "C" {
#define MAX_ERROR_MSG_SIZE PATH_MAX
-
extern void *xmalloc( size_t );
extern void *xrealloc( void *, size_t );
@@ -31,13 +30,6 @@ static inline void freep(void **p)
}
#define _cleanup_(_some_) __attribute__((__cleanup__(_some_)))
-#define _cleanup_bus_unref_ _cleanup_(sd_bus_unrefp)
-#define _cleanup_bus_close_unref_ _cleanup_(sd_bus_close_unrefp)
-#define _cleanup_bus_slot_unref_ _cleanup_(sd_bus_slot_unrefp)
-#define _cleanup_bus_message_unref_ _cleanup_(sd_bus_message_unrefp)
-#define _cleanup_bus_creds_unref_ _cleanup_(sd_bus_creds_unrefp)
-#define _cleanup_bus_track_unref_ _cleanup_(sd_bus_slot_unrefp)
-#define _cleanup_bus_error_free_ _cleanup_(sd_bus_error_free)
#define _cleanup_free_ _cleanup_(freep)
#ifdef __cplusplus