From 81f88de144d6803bf4d53986193bf342772367eb Mon Sep 17 00:00:00 2001 From: ironcaterpillar Date: Sat, 8 Oct 2022 13:42:22 +0500 Subject: Refactor d_bus_commands to separate file --- meson.build | 3 +- src/d_bus_commands.c | 62 ++++++++++++++++++++++++++++++++++++++++ src/d_bus_commands.h | 42 +++++++++++++++++++++++++++ src/power_commands.c | 80 ++-------------------------------------------------- src/power_commands.h | 10 +++++++ 5 files changed, 119 insertions(+), 78 deletions(-) create mode 100644 src/d_bus_commands.c create mode 100644 src/d_bus_commands.h 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 +#include +#include + +#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 #include -#include +#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 -- cgit v1.2.3