diff options
Diffstat (limited to 'src/d_bus_commands.c')
-rw-r--r-- | src/d_bus_commands.c | 62 |
1 files changed, 62 insertions, 0 deletions
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; +} |