2014-10-07 04:50:27 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2016-04-01 04:27:37 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-10-07 04:50:27 +02:00
|
|
|
#include <ell/ell.h>
|
2016-04-01 04:27:37 +02:00
|
|
|
#include <ell/dbus-private.h>
|
2014-10-07 04:50:27 +02:00
|
|
|
#include "src/dbus.h"
|
2014-10-07 05:13:14 +02:00
|
|
|
#include "src/manager.h"
|
2014-10-07 04:50:27 +02:00
|
|
|
|
|
|
|
struct l_dbus *g_dbus = 0;
|
2016-04-01 04:27:37 +02:00
|
|
|
static int kdbus_fd = -1;
|
2014-10-07 04:50:27 +02:00
|
|
|
|
|
|
|
static void do_debug(const char *str, void *user_data)
|
|
|
|
{
|
|
|
|
const char *prefix = user_data;
|
|
|
|
|
|
|
|
l_info("%s%s", prefix, str);
|
|
|
|
}
|
|
|
|
|
2014-10-23 23:48:08 +02:00
|
|
|
void dbus_dict_append_string(struct l_dbus_message_builder *builder,
|
|
|
|
const char *key, const char *strval)
|
|
|
|
{
|
|
|
|
l_dbus_message_builder_enter_dict(builder, "sv");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', key);
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "s");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', strval);
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
|
|
|
l_dbus_message_builder_leave_dict(builder);
|
|
|
|
}
|
|
|
|
|
2014-10-23 23:48:32 +02:00
|
|
|
void dbus_dict_append_bool(struct l_dbus_message_builder *builder,
|
|
|
|
const char *key, bool boolval)
|
|
|
|
{
|
|
|
|
l_dbus_message_builder_enter_dict(builder, "sv");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', key);
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "b");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'b', &boolval);
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
2014-12-12 11:33:33 +01:00
|
|
|
l_dbus_message_builder_leave_dict(builder);
|
2016-03-09 19:23:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void dbus_dict_append_object(struct l_dbus_message_builder *builder,
|
|
|
|
const char *key, const char *object_path)
|
|
|
|
{
|
|
|
|
l_dbus_message_builder_enter_dict(builder, "sv");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', key);
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "o");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'o', object_path);
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
|
|
|
l_dbus_message_builder_leave_dict(builder);
|
2014-12-12 11:33:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void dbus_dict_append_bytearray(struct l_dbus_message_builder *builder,
|
|
|
|
const char *key, const uint8_t *arrayval,
|
|
|
|
const int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
l_dbus_message_builder_enter_dict(builder, "sv");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', key);
|
|
|
|
l_dbus_message_builder_enter_variant(builder, "ay");
|
|
|
|
l_dbus_message_builder_enter_array(builder, "y");
|
2014-12-12 16:52:12 +01:00
|
|
|
|
2014-12-12 11:33:33 +01:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'y', &arrayval[i]);
|
2014-12-12 16:52:12 +01:00
|
|
|
|
2014-12-12 11:33:33 +01:00
|
|
|
l_dbus_message_builder_leave_array(builder);
|
|
|
|
l_dbus_message_builder_leave_variant(builder);
|
2014-10-23 23:48:32 +02:00
|
|
|
l_dbus_message_builder_leave_dict(builder);
|
|
|
|
}
|
|
|
|
|
2014-10-28 05:42:43 +01:00
|
|
|
struct l_dbus_message *dbus_error_busy(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".InProgress",
|
|
|
|
"Operation already in progress");
|
|
|
|
}
|
|
|
|
|
2014-10-28 05:42:59 +01:00
|
|
|
struct l_dbus_message *dbus_error_failed(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".Failed",
|
|
|
|
"Operation failed");
|
|
|
|
}
|
|
|
|
|
2015-02-13 23:12:05 +01:00
|
|
|
struct l_dbus_message *dbus_error_aborted(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".Aborted",
|
|
|
|
"Operation aborted");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct l_dbus_message *dbus_error_not_available(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".NotAvailable",
|
|
|
|
"Operation not available");
|
|
|
|
}
|
|
|
|
|
2015-02-24 16:23:19 +01:00
|
|
|
struct l_dbus_message *dbus_error_invalid_args(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".InvalidArgs",
|
|
|
|
"Argument type is wrong");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct l_dbus_message *dbus_error_already_exists(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".AlreadyExists",
|
|
|
|
"Object already exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct l_dbus_message *dbus_error_not_found(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".NotFound",
|
|
|
|
"Object not found");
|
|
|
|
}
|
|
|
|
|
2015-02-25 06:19:16 +01:00
|
|
|
struct l_dbus_message *dbus_error_not_supported(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".NotSupported",
|
|
|
|
"Operation not supported");
|
|
|
|
}
|
|
|
|
|
2015-03-20 21:42:39 +01:00
|
|
|
struct l_dbus_message *dbus_error_no_agent(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".NoAgent",
|
|
|
|
"No Agent registered");
|
|
|
|
}
|
|
|
|
|
2015-05-01 01:41:53 +02:00
|
|
|
struct l_dbus_message *dbus_error_not_connected(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".NotConnected",
|
|
|
|
"Not connected");
|
|
|
|
}
|
|
|
|
|
2015-09-28 18:27:33 +02:00
|
|
|
struct l_dbus_message *dbus_error_not_implemented(struct l_dbus_message *msg)
|
|
|
|
{
|
|
|
|
return l_dbus_message_new_error(msg, IWD_SERVICE ".NotImplemented",
|
|
|
|
"Not implemented");
|
|
|
|
}
|
|
|
|
|
2014-10-28 05:42:05 +01:00
|
|
|
void dbus_pending_reply(struct l_dbus_message **msg,
|
|
|
|
struct l_dbus_message *reply)
|
|
|
|
{
|
|
|
|
struct l_dbus *dbus = dbus_get_bus();
|
|
|
|
|
|
|
|
l_dbus_send(dbus, reply);
|
|
|
|
l_dbus_message_unref(*msg);
|
|
|
|
*msg = NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-01 04:27:40 +02:00
|
|
|
static void request_name_callback(struct l_dbus *dbus, bool success,
|
|
|
|
bool queued, void *user_data)
|
2014-10-07 04:50:27 +02:00
|
|
|
{
|
2016-04-01 04:27:40 +02:00
|
|
|
if (!success)
|
|
|
|
l_error("Name request failed");
|
2014-10-07 04:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ready_callback(void *user_data)
|
|
|
|
{
|
2016-04-01 04:27:40 +02:00
|
|
|
l_dbus_name_acquire(g_dbus, "net.connman.iwd", false, false, true,
|
|
|
|
request_name_callback, NULL);
|
|
|
|
|
2014-10-07 05:13:14 +02:00
|
|
|
manager_init(g_dbus);
|
2014-10-07 04:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void disconnect_callback(void *user_data)
|
|
|
|
{
|
|
|
|
l_info("D-Bus disconnected, quitting...");
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct l_dbus *dbus_get_bus(void)
|
|
|
|
{
|
|
|
|
return g_dbus;
|
|
|
|
}
|
|
|
|
|
2016-04-01 04:27:37 +02:00
|
|
|
bool dbus_init(bool enable_debug, bool use_kdbus)
|
2014-10-07 04:50:27 +02:00
|
|
|
{
|
2016-04-01 04:27:37 +02:00
|
|
|
if (!use_kdbus)
|
|
|
|
g_dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
|
|
|
|
else {
|
|
|
|
char bus_name[32], bus_address[64];
|
|
|
|
|
|
|
|
snprintf(bus_name, sizeof(bus_name), "%u-iwd", getuid());
|
|
|
|
|
|
|
|
kdbus_fd = _dbus_kernel_create_bus(bus_name);
|
|
|
|
if (kdbus_fd < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
snprintf(bus_address, sizeof(bus_address),
|
|
|
|
"kernel:path=/dev/kdbus/%s/bus", bus_name);
|
|
|
|
|
|
|
|
l_debug("Bus location: %s", bus_address);
|
|
|
|
|
|
|
|
g_dbus = l_dbus_new(bus_address);
|
|
|
|
|
|
|
|
if (!g_dbus) {
|
|
|
|
close(kdbus_fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 04:42:58 +01:00
|
|
|
|
|
|
|
if (enable_debug)
|
|
|
|
l_dbus_set_debug(g_dbus, do_debug, "[DBUS] ", NULL);
|
|
|
|
|
2014-10-07 04:50:27 +02:00
|
|
|
l_dbus_set_ready_handler(g_dbus, ready_callback, g_dbus, NULL);
|
|
|
|
l_dbus_set_disconnect_handler(g_dbus, disconnect_callback, NULL, NULL);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dbus_exit(void)
|
|
|
|
{
|
2014-10-07 05:13:14 +02:00
|
|
|
manager_exit(g_dbus);
|
2014-10-07 04:50:27 +02:00
|
|
|
l_dbus_destroy(g_dbus);
|
|
|
|
g_dbus = NULL;
|
|
|
|
|
2016-04-01 04:27:37 +02:00
|
|
|
if (kdbus_fd >= 0)
|
|
|
|
close(kdbus_fd);
|
|
|
|
|
2014-10-07 04:50:27 +02:00
|
|
|
return true;
|
|
|
|
}
|