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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
/*
* Embedded Linux library
* Copyright (C) 2011-2014 Intel Corporation
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <ell/ell.h>
static void do_debug(const char *str, void *user_data)
{
const char *prefix = user_data;
l_info("%s%s", prefix, str);
}
static void signal_handler(uint32_t signo, void *user_data)
{
switch (signo) {
case SIGINT:
case SIGTERM:
l_info("Terminate");
l_main_quit();
break;
}
}
static void request_name_callback(struct l_dbus *dbus, bool success,
bool queued, void *user_data)
{
l_info("request name result=%s",
success ? (queued ? "queued" : "success") : "failed");
}
static void ready_callback(void *user_data)
{
l_info("ready");
}
static void disconnect_callback(void *user_data)
{
l_main_quit();
}
struct test_data {
char *string;
uint32_t integer;
};
static void test_data_destroy(void *data)
{
struct test_data *test = data;
l_free(test->string);
l_free(test);
}
static struct l_dbus_message *test_method_call(struct l_dbus *dbus,
struct l_dbus_message *message,
void *user_data)
{
struct l_dbus_message *reply;
l_info("Method Call");
reply = l_dbus_message_new_method_return(message);
l_dbus_message_set_arguments(reply, "");
return reply;
}
static bool test_string_getter(struct l_dbus *dbus,
struct l_dbus_message *message,
struct l_dbus_message_builder *builder,
void *user_data)
{
struct test_data *test = user_data;
l_dbus_message_builder_append_basic(builder, 's', test->string);
return true;
}
static struct l_dbus_message *test_string_setter(struct l_dbus *dbus,
struct l_dbus_message *message,
struct l_dbus_message_iter *new_value,
l_dbus_property_complete_cb_t complete,
void *user_data)
{
const char *strvalue;
struct test_data *test = user_data;
if (!l_dbus_message_iter_get_variant(new_value, "s", &strvalue))
return l_dbus_message_new_error(message,
"org.test.InvalidArguments",
"String value expected");
l_info("New String value: %s", strvalue);
l_free(test->string);
test->string = l_strdup(strvalue);
complete(dbus, message, NULL);
return NULL;
}
static bool test_int_getter(struct l_dbus *dbus,
struct l_dbus_message *message,
struct l_dbus_message_builder *builder,
void *user_data)
{
struct test_data *test = user_data;
l_dbus_message_builder_append_basic(builder, 'u', &test->integer);
return true;
}
static struct l_dbus_message *test_int_setter(struct l_dbus *dbus,
struct l_dbus_message *message,
struct l_dbus_message_iter *new_value,
l_dbus_property_complete_cb_t complete,
void *user_data)
{
uint32_t u;
struct test_data *test = user_data;
if (!l_dbus_message_iter_get_variant(new_value, "u", &u))
return l_dbus_message_new_error(message,
"org.test.InvalidArguments",
"Integer value expected");
l_info("New Integer value: %u", u);
test->integer = u;
complete(dbus, message, NULL);
return NULL;
}
static void setup_test_interface(struct l_dbus_interface *interface)
{
l_dbus_interface_method(interface, "MethodCall", 0,
test_method_call, "", "");
l_dbus_interface_property(interface, "String", 0, "s",
test_string_getter, test_string_setter);
l_dbus_interface_property(interface, "Integer", 0, "u",
test_int_getter, test_int_setter);
}
int main(int argc, char *argv[])
{
struct l_dbus *dbus;
struct test_data *test;
if (!l_main_init())
return -1;
l_log_set_stderr();
dbus = l_dbus_new_default(L_DBUS_SESSION_BUS);
l_dbus_set_debug(dbus, do_debug, "[DBUS] ", NULL);
l_dbus_set_ready_handler(dbus, ready_callback, dbus, NULL);
l_dbus_set_disconnect_handler(dbus, disconnect_callback, NULL, NULL);
l_dbus_name_acquire(dbus, "org.test", false, false, false,
request_name_callback, NULL);
if (!l_dbus_object_manager_enable(dbus, "/")) {
l_info("Unable to enable Object Manager");
goto cleanup;
}
test = l_new(struct test_data, 1);
test->string = l_strdup("Default");
test->integer = 42;
if (!l_dbus_register_interface(dbus, "org.test", setup_test_interface,
test_data_destroy, true)) {
l_info("Unable to register interface");
test_data_destroy(test);
goto cleanup;
}
if (!l_dbus_object_add_interface(dbus, "/test", "org.test", test)) {
l_info("Unable to instantiate interface");
test_data_destroy(test);
goto cleanup;
}
if (!l_dbus_object_add_interface(dbus, "/test",
L_DBUS_INTERFACE_PROPERTIES, NULL)) {
l_info("Unable to instantiate the properties interface");
test_data_destroy(test);
goto cleanup;
}
l_main_run_with_signal(signal_handler, NULL);
l_dbus_unregister_object(dbus, "/test");
cleanup:
l_dbus_destroy(dbus);
l_main_exit();
return 0;
}
|