summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorironcaterpillar <ironcaterpillar@yandex.ru>2022-10-08 11:42:22 +0300
committerironcaterpillar <ironcaterpillar@yandex.ru>2022-10-08 11:42:22 +0300
commit81f88de144d6803bf4d53986193bf342772367eb (patch)
tree10a6544f1e94b89adbe058a65a5141d547ca8f6e
parent224e5b8317e7e3ffbb04ada342447bf678297002 (diff)
downloadsila-shell-81f88de144d6803bf4d53986193bf342772367eb.tar.xz
Refactor d_bus_commands to separate file
-rw-r--r--meson.build3
-rw-r--r--src/d_bus_commands.c62
-rw-r--r--src/d_bus_commands.h42
-rw-r--r--src/power_commands.c80
-rw-r--r--src/power_commands.h10
5 files changed, 119 insertions, 78 deletions
diff --git a/meson.build b/meson.build
index 6a5cefe..cf2a685 100644
--- a/meson.build
+++ b/meson.build
@@ -11,7 +11,8 @@ src = [
'src/shell.c',
'src/users.c',
'src/d_bus.c',
- 'src/power_commands.c'
+ 'src/power_commands.c',
+ 'src/d_bus_commands.c'
]
readline = dependency('readline')
diff --git a/src/d_bus_commands.c b/src/d_bus_commands.c
new file mode 100644
index 0000000..6c96085
--- /dev/null
+++ b/src/d_bus_commands.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "d_bus_commands.h"
+
+void print_command_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);
+ }
+}
+
+int parse_command2enum(const char* command, const universal_command_t* cmds)
+{
+ int result = COMMAND_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_command_interface(int 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_command_member(int cmd, const command_dbus_member_t* values)
+{
+ for(const command_dbus_member_t* current = values; current->member!=NULL; current++)
+ {
+ if(current->command == cmd)return current->member;
+ }
+ return NULL;
+}
+
+
+int com_dbus_property( char *arg, const universal_command_t* commands, const command_dbus_path_t* paths, const command_dbus_member_t* values)
+{
+ int cmd = parse_command2enum(arg, commands);
+ if(cmd==COMMAND_UNKNOWN) {
+ fprintf(stderr, "Unknown command: %s\n", arg);
+ print_command_help(commands);
+ return -EOPNOTSUPP;
+ }
+ const string_quadruple_t* dbus_power_interface = get_dbus_command_interface(cmd, paths);
+ if(!dbus_power_interface)
+ return -EOPNOTSUPP;
+ const char* property_val = get_dbus_command_member(cmd, values);
+ if(!property_val)
+ return -EOPNOTSUPP;
+ return dbus_set_property_string(dbus_power_interface, property_val);
+ return 0;
+}
diff --git a/src/d_bus_commands.h b/src/d_bus_commands.h
new file mode 100644
index 0000000..24e1c80
--- /dev/null
+++ b/src/d_bus_commands.h
@@ -0,0 +1,42 @@
+#ifndef D_BUS_COMMANDS_H
+#define D_BUS_COMMANDS_H
+
+
+
+#include "d_bus.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define COMMAND_UNKNOWN -1
+typedef struct
+{
+ int command;
+ const string_quadruple_t * path;
+}command_dbus_path_t;
+
+typedef struct
+{
+ int command;
+ const char* member;
+}command_dbus_member_t;
+
+typedef struct
+{
+ const char* name;
+ int command;
+ const char* help;
+}universal_command_t;
+
+int parse_command2enum(const char* command, const universal_command_t* cmds);
+const string_quadruple_t* get_dbus_command_interface(int cmd, const command_dbus_path_t* pathes);
+const char* get_dbus_command_member(int cmd, const command_dbus_member_t* values);
+int com_dbus_property( char *arg, const universal_command_t* commands, const command_dbus_path_t* paths, const command_dbus_member_t* values);
+void print_command_help(const universal_command_t* cmds);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // D_BUS_COMMANDS_H
diff --git a/src/power_commands.c b/src/power_commands.c
index 33f1366..4000733 100644
--- a/src/power_commands.c
+++ b/src/power_commands.c
@@ -2,9 +2,8 @@
#include <string.h>
#include <errno.h>
-#include<d_bus.h>
+#include "d_bus_commands.h"
-#include "utils.h"
#include "power_commands.h"
static const string_quadruple_t chassis_dbus = {
@@ -25,17 +24,6 @@ static const string_quadruple_t policy_dbus = {
"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
{
@@ -58,7 +46,7 @@ const command_dbus_path_t power_command_paths[] = {
{ 0, NULL }
};
-const command_dbus_value_t power_command_values[] = {
+const command_dbus_member_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" },
@@ -77,12 +65,6 @@ typedef enum
}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" },
@@ -111,69 +93,13 @@ const command_dbus_path_t power_command_policy_paths[] = {
{ 0, NULL }
};
-const command_dbus_value_t power_command_policy_values[] = {
+const command_dbus_member_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);
diff --git a/src/power_commands.h b/src/power_commands.h
index d6b6bf2..da5d979 100644
--- a/src/power_commands.h
+++ b/src/power_commands.h
@@ -2,8 +2,18 @@
#define POWER_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
int com_power( char *arg );
int com_power_policy( char* arg );
+
+#ifdef __cplusplus
+}
+#endif
+
+
#endif // POWER_H