<commit_before>/*
 * This file is part of the KNOT Project
 *
 * Copyright (c) 2018, CESAR. 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

#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#include <ell/ell.h>

#include "hal/linux_log.h"

#include "dbus.h"
#include "device.h"
#include "proxy.h"

struct proxy {
	char *name;
	char *path;
	char *interface;
	unsigned int watch_id;
	struct l_dbus_client *client;
	struct l_hashmap *device_list;
};

static struct proxy *proxy;

static void proxy_free(struct proxy *proxy)
{
	l_hashmap_destroy(proxy->device_list,
			  (l_hashmap_destroy_func_t) device_destroy);
	l_free(proxy->name);
	l_free(proxy->path);
	l_free(proxy->interface);
	l_free(proxy);
}

static void service_appeared(struct l_dbus *dbus, void *user_data)
{
	struct proxy *proxy = user_data;
	hal_log_info("Service appeared: %s", proxy->name);
	proxy->device_list = l_hashmap_new();
}

static void service_disappeared(struct l_dbus *dbus, void *user_data)
{
	struct proxy *proxy = user_data;
	hal_log_info("Service disappeared: %s", proxy->name);

	/* FIXME: Investigate if proxy should be released */
	l_hashmap_destroy(proxy->device_list,
			  (l_hashmap_destroy_func_t) device_destroy);
	proxy->device_list = NULL;
}

static void added(struct l_dbus_proxy *ellproxy, void *user_data)
{
	const char *interface = l_dbus_proxy_get_interface(ellproxy);
	const char *path = l_dbus_proxy_get_path(ellproxy);
	struct proxy *proxy = user_data;
	struct knot_device *device;
	uint64_t id = 0;
	bool paired = false;

	if (strcmp(interface, proxy->interface) != 0)
		return;

	/* Debug purpose only */
	hal_log_info("proxy added: %s %s", path, interface);

	if (!l_dbus_proxy_get_property(ellproxy, "Id", "t", &id))
		return;

	if (!l_dbus_proxy_get_property(ellproxy, "Paired", "b", &paired))
		return;

	device = device_create(ellproxy, id, "device:unknown", paired);
	if (device) {
		l_hashmap_insert(proxy->device_list, ellproxy, device);
		return;
	}

	/* Critical error: probably D-Bus object is not registered properly */
	hal_log_error("Can't create device: %"PRIu64, id);
}

static void removed(struct l_dbus_proxy *ellproxy, void *user_data)<commit_msg>dbus: Read name from proxy when creating device<commit_after>/*
 * This file is part of the KNOT Project
 *
 * Copyright (c) 2018, CESAR. 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

#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#include <ell/ell.h>

#include "hal/linux_log.h"

#include "dbus.h"
#include "device.h"
#include "proxy.h"

struct proxy {
	char *name;
	char *path;
	char *interface;
	unsigned int watch_id;
	struct l_dbus_client *client;
	struct l_hashmap *device_list;
};

static struct proxy *proxy;

static void proxy_free(struct proxy *proxy)
{
	l_hashmap_destroy(proxy->device_list,
			  (l_hashmap_destroy_func_t) device_destroy);
	l_free(proxy->name);
	l_free(proxy->path);
	l_free(proxy->interface);
	l_free(proxy);
}

static void service_appeared(struct l_dbus *dbus, void *user_data)
{
	struct proxy *proxy = user_data;
	hal_log_info("Service appeared: %s", proxy->name);
	proxy->device_list = l_hashmap_new();
}

static void service_disappeared(struct l_dbus *dbus, void *user_data)
{
	struct proxy *proxy = user_data;
	hal_log_info("Service disappeared: %s", proxy->name);

	/* FIXME: Investigate if proxy should be released */
	l_hashmap_destroy(proxy->device_list,
			  (l_hashmap_destroy_func_t) device_destroy);
	proxy->device_list = NULL;
}

static void added(struct l_dbus_proxy *ellproxy, void *user_data)
{
	const char *interface = l_dbus_proxy_get_interface(ellproxy);
	const char *path = l_dbus_proxy_get_path(ellproxy);
	struct proxy *proxy = user_data;
	struct knot_device *device;
	uint64_t id = 0;
	bool paired = false;
	const char *name;

	if (strcmp(interface, proxy->interface) != 0)
		return;

	/* Debug purpose only */
	hal_log_info("proxy added: %s %s", path, interface);

	if (!l_dbus_proxy_get_property(ellproxy, "Id", "t", &id))
		return;

	if (!l_dbus_proxy_get_property(ellproxy, "Paired", "b", &paired))
		return;

	if (!l_dbus_proxy_get_property(ellproxy, "Name", "s", &name))
		return;

	device = device_create(ellproxy, id, name, paired);
	if (device) {
		l_hashmap_insert(proxy->device_list, ellproxy, device);
		return;
	}

	/* Critical error: probably D-Bus object is not registered properly */
	hal_log_error("Can't create device: %"PRIu64, id);
}

static void removed(struct l_dbus_proxy *ellproxy, void *user_data)
{'fraction_non_alphanumeric': 0.10884353741496598, 'fraction_numerical': 0.0076323212211713955, 'mean_word_length': 4.796153846153846, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5468299', 'n_tokens_mistral': 2107, 'n_tokens_neox': 2033, 'n_words': 647}