Blob: bearer.c

Blob id: 9723b59e2e7b39b2974e258363aa0ace95862651

Size: 6.1 KB

  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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2025  Intel Corporation
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <errno.h>
#include <stdint.h>

#include <glib.h>
#include <dbus/dbus.h>

#include "bluetooth/bluetooth.h"
#include "bluetooth/mgmt.h"

#include "gdbus/gdbus.h"
#include "src/shared/util.h"

#include "log.h"
#include "error.h"
#include "adapter.h"
#include "device.h"
#include "dbus-common.h"
#include "bearer.h"

struct btd_bearer {
	struct btd_device *device;
	uint8_t type;
	const char *path;
};

static void bearer_free(void *data)
{
	struct btd_bearer *bearer = data;

	free(bearer);
}

static DBusMessage *bearer_connect(DBusConnection *conn, DBusMessage *msg,
							void *user_data)
{
	/* TODO */
	return NULL;
}

static DBusMessage *bearer_disconnect(DBusConnection *conn, DBusMessage *msg,
							void *user_data)
{
	/* TODO */
	return NULL;
}

static const GDBusMethodTable bearer_methods[] = {
	{ GDBUS_EXPERIMENTAL_ASYNC_METHOD("Connect", NULL, NULL,
						bearer_connect) },
	{ GDBUS_EXPERIMENTAL_ASYNC_METHOD("Disconnect", NULL, NULL,
						bearer_disconnect) },
	{}
};

static gboolean bearer_get_adapter(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *data)
{
	struct btd_bearer *bearer = data;
	struct btd_adapter *adapter = device_get_adapter(bearer->device);
	const char *path = adapter_get_path(adapter);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);

	return TRUE;
}

static gboolean bearer_get_paired(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *data)
{
	struct btd_bearer *bearer = data;
	dbus_bool_t paired = device_is_paired(bearer->device, bearer->type);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &paired);

	return TRUE;
}

static gboolean bearer_get_bonded(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *data)
{
	struct btd_bearer *bearer = data;
	dbus_bool_t bonded = device_is_bonded(bearer->device, bearer->type);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &bonded);

	return TRUE;
}

static gboolean bearer_get_connected(const GDBusPropertyTable *property,
					DBusMessageIter *iter, void *data)
{
	struct btd_bearer *bearer = data;
	dbus_bool_t connected = btd_device_bdaddr_type_connected(bearer->device,
								bearer->type);

	dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &connected);

	return TRUE;
}

static const GDBusSignalTable bearer_signals[] = {
	{ GDBUS_SIGNAL("Disconnected",
			GDBUS_ARGS({ "name", "s" }, { "message", "s" })) },
	{ }
};

static const GDBusPropertyTable bearer_properties[] = {
	{ "Adapter", "o", bearer_get_adapter, NULL, NULL,
			G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
	{ "Paired", "b", bearer_get_paired, NULL, NULL,
			G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
	{ "Bonded", "b", bearer_get_bonded, NULL, NULL,
			G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
	{ "Connected", "b", bearer_get_connected, NULL, NULL,
			G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
	{}
};

static const char *bearer_interface(uint8_t type)
{
	if (type == BDADDR_BREDR)
		return BTD_BEARER_BREDR_INTERFACE;
	else
		return BTD_BEARER_LE_INTERFACE;
}

struct btd_bearer *btd_bearer_new(struct btd_device *device, uint8_t type)
{
	struct btd_bearer *bearer;

	bearer = new0(struct btd_bearer, 1);
	bearer->device = device;
	bearer->type = type;
	bearer->path = device_get_path(device);

	if (!g_dbus_register_interface(btd_get_dbus_connection(),
					bearer->path, bearer_interface(type),
					bearer_methods, bearer_signals,
					bearer_properties,
					bearer, bearer_free)) {
		error("Unable to register BREDR interface");
		bearer->path = NULL;
	}

	return bearer;
}

void btd_bearer_destroy(struct btd_bearer *bearer)
{
	if (!bearer)
		return;

	if (!bearer->path) {
		bearer_free(bearer);
		return;
	}

	g_dbus_unregister_interface(btd_get_dbus_connection(), bearer->path,
					bearer_interface(bearer->type));
}

void btd_bearer_paired(struct btd_bearer *bearer)
{
	if (!bearer || !bearer->path)
		return;

	g_dbus_emit_property_changed(btd_get_dbus_connection(), bearer->path,
					bearer_interface(bearer->type),
					"Paired");
}

void btd_bearer_bonded(struct btd_bearer *bearer)
{
	if (!bearer || !bearer->path)
		return;

	g_dbus_emit_property_changed(btd_get_dbus_connection(), bearer->path,
					bearer_interface(bearer->type),
					"Bonded");
}

void btd_bearer_connected(struct btd_bearer *bearer)
{
	if (!bearer || !bearer->path)
		return;

	g_dbus_emit_property_changed(btd_get_dbus_connection(), bearer->path,
					bearer_interface(bearer->type),
					"Connected");
}

void btd_bearer_disconnected(struct btd_bearer *bearer, uint8_t reason)
{
	const char *name;
	const char *message;

	if (!bearer || !bearer->path)
		return;

	g_dbus_emit_property_changed(btd_get_dbus_connection(), bearer->path,
					bearer_interface(bearer->type),
					"Connected");

	switch (reason) {
	case MGMT_DEV_DISCONN_UNKNOWN:
		name = "org.bluez.Reason.Unknown";
		message = "Unspecified";
		break;
	case MGMT_DEV_DISCONN_TIMEOUT:
		name = "org.bluez.Reason.Timeout";
		message = "Connection timeout";
		break;
	case MGMT_DEV_DISCONN_LOCAL_HOST:
		name = "org.bluez.Reason.Local";
		message = "Connection terminated by local host";
		break;
	case MGMT_DEV_DISCONN_REMOTE:
		name = "org.bluez.Reason.Remote";
		message = "Connection terminated by remote user";
		break;
	case MGMT_DEV_DISCONN_AUTH_FAILURE:
		name = "org.bluez.Reason.Authentication";
		message = "Connection terminated due to authentication failure";
		break;
	case MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND:
		name = "org.bluez.Reason.Suspend";
		message = "Connection terminated by local host for suspend";
		break;
	default:
		warn("Unknown disconnection value: %u", reason);
		name = "org.bluez.Reason.Unknown";
		message = "Unspecified";
	}

	g_dbus_emit_signal(btd_get_dbus_connection(), bearer->path,
					bearer_interface(bearer->type),
					"Disconnected",
					DBUS_TYPE_STRING, &name,
					DBUS_TYPE_STRING, &message,
					DBUS_TYPE_INVALID);
}