summaryrefslogtreecommitdiff
path: root/src/d_bus_commands.c
blob: 6c96085e97c62d4c1ed06838d9c1b0043429a38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
}