summaryrefslogtreecommitdiff
path: root/src/UwbDbusManager.cpp
blob: 4f58e51dad5d3cec9c7ab01dc7c1348f6d51696b (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
/*
 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <uwb-log-def.h>

#include <GdbusError.h>
#include <UwbDbusManager.h>

using namespace UwbManagerNamespace;

UwbDbusManager::~UwbDbusManager()
{
	if (_owner_id) {
		g_bus_unown_name(_owner_id);
		_owner_id = 0;
	}
}

void UwbDbusManager::init(UwbIfaceHandler *iface_handler)
{
	GdbusError gdbus_error = GdbusError();
	if (_owner_id) {
		UWB_LOGI("Already initialized");
		return;
	}

	gdbus_error.registerGdbusError();
	_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
			uwb_dbus_service_str.c_str(),
			G_BUS_NAME_OWNER_FLAGS_NONE,
			onBusAcquired,
			NULL,
			NULL,
			(gpointer)iface_handler,
			NULL);
	UWB_LOGI("Request to Own [%u]", _owner_id);
}

void UwbDbusManager::deinit(void)
{
	GdbusError gdbus_error = GdbusError();

	gdbus_error.deregisterGdbusError();
	if (_owner_id) {
		g_bus_unown_name(_owner_id);
		_owner_id = 0;
	}
}

UwbGdbuslibManager *manager_skeleton;

void UwbDbusManager::onBusAcquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
{
	gboolean ret = FALSE;
	GError *error = NULL;

	GDBusObjectManagerServer *manager;
	UwbIfaceHandler *iface_handler = (UwbIfaceHandler *)user_data;

	__UWB_LOG_FUNC_ENTER__;
	/* Add interface to default object path */
	manager_skeleton = uwb_gdbuslib_manager_skeleton_new();

	/* Register method callbacks as signal callback */
	iface_handler->registerHandler(manager_skeleton);

	/* Set connection to 'manager' */
	manager = g_dbus_object_manager_server_new(iface_handler->get_dbus_path_str().c_str());
	g_dbus_object_manager_server_set_connection(manager, connection);

	/* Export 'manager' interface on UWB DBUS */
	ret = g_dbus_interface_skeleton_export(
		G_DBUS_INTERFACE_SKELETON(manager_skeleton),
		connection, iface_handler->get_dbus_path_str().c_str(), &error);

	if (ret == FALSE) {
		UWB_LOGE("Can not skeleton_export %s", error->message);
		g_error_free(error);
	}
	__UWB_LOG_FUNC_EXIT__;
}

static GVariant *__data_to_variant(const unsigned char *data, int length)
{
	GVariantBuilder builder;

	g_variant_builder_init(&builder, G_VARIANT_TYPE("a(y)"));

	if (data && length > 0)
	{
		for(int i = 0; i < length; i++)
			g_variant_builder_add(&builder, "(y)", *(data + i));
	}

	return g_variant_builder_end(&builder);
}

void UwbDbusManager::emitMessageReceivedSignal(uint16_t node_id,
	const unsigned char *message, int message_length)
{
	GVariant *data = __data_to_variant(message, message_length);

	uwb_gdbuslib_manager_emit_message_received(manager_skeleton, node_id, data, message_length);

	g_variant_unref(data);
}

void UwbDbusManager::emitPositionChangedSignal(uint16_t node_id, int x, int y, int z)
{
	uwb_gdbuslib_manager_emit_position_changed(manager_skeleton, node_id, x, y, z);
}