summaryrefslogtreecommitdiff
path: root/src/d_bus.c
blob: 76a84fcb35f74287df6f495c1803d26f6ea42367 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>

#include <systemd/sd-bus.h>

#include "utils.h"
#include "d_bus_variant.h"
#include "d_bus.h"


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;
    _cleanup_bus_unref_ sd_bus *bus = NULL;

    int r;

    /* Connect to the system bus */
    r = sd_bus_open_system(&bus);
    if (r >= 0)
    {

        r = sd_bus_set_property(
            bus,
            path->first,
            path->second,
            path->third,
            path->fourth,
            &error,
            "s",
            value);

    }
    if (r < 0)
    {
        fprintf(stderr, "Failed to issue dbus call: %d %s\n", r, error.message);
    }

    return (r>0?(0):(r));
}

int dbus_set_property(const string_quadruple_t* path, dbus_value_variant_t value)
{
    _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;

    int r;

    /* Connect to the system bus */
    r = sd_bus_open_system(&bus);
    if (r >= 0)
    {

        if(strcmp(value.type, "s") ==0 )
        {

        r = sd_bus_set_property(
            bus,
            path->first,
            path->second,
            path->third,
            path->fourth,
            &error,
            value.type,
            value.string);
        }
        else if(strcmp(value.type, "b") ==0 )
        {

        r = sd_bus_set_property(
            bus,
            path->first,
            path->second,
            path->third,
            path->fourth,
            &error,
            value.type,
            value.boolean);
        }
        else {
            fprintf(stderr, "Failed to find type: %s\n", value.type);
            return -EOPNOTSUPP;
        }
    }
    if (r < 0)
    {
        fprintf(stderr, "Failed to issue dbus call: %d %s\n", r, error.message);
    }

    return (r>0?(0):(r));
}


char* dbus_get_property_string(const string_quadruple_t* path )
{
    _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;
    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,
            path->first,
            path->second,
            path->third,
            path->fourth,
            &error,
            &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?(state):(NULL));
}