summaryrefslogtreecommitdiff
path: root/src/linux/up-device-bluez.c
blob: d10cc56cefb113d5ef61bdeab45cc447c5e0e35c (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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2017 Bastien Nocera <hadess@hadess.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"

#include <gio/gio.h>

#include "up-types.h"
#include "up-device-bluez.h"

G_DEFINE_TYPE (UpDeviceBluez, up_device_bluez, UP_TYPE_DEVICE)

static UpDeviceKind
appearance_to_kind (guint16 appearance)
{
        switch ((appearance & 0xffc0) >> 6) {
        case 0x01:
                return UP_DEVICE_KIND_PHONE;
        case 0x02:
                return UP_DEVICE_KIND_COMPUTER;
        case 0x05:
                return UP_DEVICE_KIND_MONITOR;
        case 0x0a:
                return UP_DEVICE_KIND_MEDIA_PLAYER;
        case 0x0f: /* HID Generic */
                switch (appearance & 0x3f) {
                case 0x01:
                        return UP_DEVICE_KIND_KEYBOARD;
                case 0x02:
                        return UP_DEVICE_KIND_MOUSE;
                case 0x03:
                case 0x04:
                        return UP_DEVICE_KIND_GAMING_INPUT;
                case 0x05:
                        return UP_DEVICE_KIND_TABLET;
                case 0x0e:
                case 0x0f:
                        return UP_DEVICE_KIND_PEN;
                }
                break;
        }

	return UP_DEVICE_KIND_BLUETOOTH_GENERIC;
}

/**
 * class_to_kind:
 * @class: a Bluetooth device class
 *
 * Returns value: the type of device corresponding to the given @class value.
 **/
static UpDeviceKind
class_to_kind (guint32 class)
{
	/*
	 * See Bluetooth Assigned Numbers for Baseband
	 * https://www.bluetooth.com/specifications/assigned-numbers/baseband/
	 */

	switch ((class & 0x1f00) >> 8) {
	case 0x01:
		return UP_DEVICE_KIND_COMPUTER;
	case 0x02:
		switch ((class & 0xfc) >> 2) {
		case 0x01:
		case 0x02:
		case 0x03:
		case 0x05:
			return UP_DEVICE_KIND_PHONE;
		case 0x04:
			return UP_DEVICE_KIND_MODEM;
		}
		break;
	case 0x03:
		return UP_DEVICE_KIND_NETWORK;
	case 0x04:
		switch ((class & 0xfc) >> 2) {
		case 0x01:
		case 0x02:
			return UP_DEVICE_KIND_HEADSET;
		case 0x05:
			return UP_DEVICE_KIND_SPEAKERS;
		case 0x06:
			return UP_DEVICE_KIND_HEADPHONES;
		case 0x0b: /* VCR */
		case 0x0c: /* Video Camera */
		case 0x0d: /* Camcorder */
			return UP_DEVICE_KIND_VIDEO;
		default:
			return UP_DEVICE_KIND_OTHER_AUDIO;
		}
		break;
	case 0x05:
		switch ((class & 0xc0) >> 6) {
		case 0x00:
			switch ((class & 0x1e) >> 2) {
			case 0x01:
			case 0x02:
				return UP_DEVICE_KIND_GAMING_INPUT;
			case 0x03:
				return UP_DEVICE_KIND_REMOTE_CONTROL;
			}
			break;
		case 0x01:
			return UP_DEVICE_KIND_KEYBOARD;
		case 0x02:
			switch ((class & 0x1e) >> 2) {
			case 0x05:
				return UP_DEVICE_KIND_TABLET;
			default:
				return UP_DEVICE_KIND_MOUSE;
			}
		}
		break;
	case 0x06:
		if (class & 0x80)
			return UP_DEVICE_KIND_PRINTER;
		if (class & 0x40)
			return UP_DEVICE_KIND_SCANNER;
		if (class & 0x20)
			return UP_DEVICE_KIND_CAMERA;
		if (class & 0x10)
			return UP_DEVICE_KIND_MONITOR;
		break;
	case 0x07:
		return UP_DEVICE_KIND_WEARABLE;
	case 0x08:
		return UP_DEVICE_KIND_TOY;
	}

	return UP_DEVICE_KIND_BLUETOOTH_GENERIC;
}

/**
 * up_device_bluez_coldplug:
 *
 * Return %TRUE on success, %FALSE if we failed to get data and should be removed
 **/
static gboolean
up_device_bluez_coldplug (UpDevice *device)
{
	GDBusObjectProxy *object_proxy;
	GDBusProxy *proxy;
	GError *error = NULL;
	UpDeviceKind kind;
	const char *uuid;
	const char *model;
	GVariant *v;
	guchar percentage;

	/* Static device properties */
	object_proxy = G_DBUS_OBJECT_PROXY (up_device_get_native (device));
	proxy = g_dbus_proxy_new_sync (g_dbus_object_proxy_get_connection (object_proxy),
				       G_DBUS_PROXY_FLAGS_NONE,
				       NULL,
				       "org.bluez",
				       g_dbus_object_get_object_path (G_DBUS_OBJECT (object_proxy)),
				       "org.bluez.Device1",
				       NULL,
				       &error);

	if (!proxy) {
		g_warning ("Failed to get proxy for %s (iface org.bluez.Device1)",
			   g_dbus_object_get_object_path (G_DBUS_OBJECT (object_proxy)));
		return FALSE;
	}

	v = g_dbus_proxy_get_cached_property (proxy, "Appearance");
	if (v && g_variant_get_uint16 (v) != 0) {
		guint16 appearance;

		appearance = g_variant_get_uint16 (v);
		kind = appearance_to_kind (appearance);
		g_variant_unref (v);
	} else if ((v = g_dbus_proxy_get_cached_property (proxy, "Class"))) {
		guint32 class;

		class = g_variant_get_uint32 (v);
		kind = class_to_kind (class);
		g_variant_unref (v);
	} else {
		kind = UP_DEVICE_KIND_BLUETOOTH_GENERIC;
	}

	v = g_dbus_proxy_get_cached_property (proxy, "Address");
	uuid = g_variant_get_string (v, NULL);
	g_variant_unref (v);

	v = g_dbus_proxy_get_cached_property (proxy, "Alias");
	model = g_variant_get_string (v, NULL);
	g_variant_unref (v);

	/* hardcode some values */
	g_object_set (device,
		      "type", kind,
		      "serial", uuid,
		      "model", model,
		      "power-supply", FALSE,
		      "has-history", TRUE,
		      NULL);

	g_object_unref (proxy);

	/* Initial battery values */
	proxy = g_dbus_proxy_new_sync (g_dbus_object_proxy_get_connection (object_proxy),
				       G_DBUS_PROXY_FLAGS_NONE,
				       NULL,
				       "org.bluez",
				       g_dbus_object_get_object_path (G_DBUS_OBJECT (object_proxy)),
				       "org.bluez.Battery1",
				       NULL,
				       &error);

	if (!proxy) {
		g_warning ("Failed to get proxy for %s",
			   g_dbus_object_get_object_path (G_DBUS_OBJECT (object_proxy)));
		return FALSE;
	}

	percentage = g_variant_get_byte (g_dbus_proxy_get_cached_property (proxy, "Percentage"));

	g_object_set (device,
		      "is-present", TRUE,
		      "percentage", (gdouble) percentage,
		      "update-time", (guint64) g_get_real_time () / G_USEC_PER_SEC,
		      NULL);

	g_object_unref (proxy);

	return TRUE;
}

static void
up_device_bluez_init (UpDeviceBluez *bluez)
{
}

void
up_device_bluez_update (UpDeviceBluez *bluez,
			GVariant      *properties)
{
	UpDevice *device = UP_DEVICE (bluez);
	GVariantIter iter;
	const gchar *key;
	GVariant *value;

	g_variant_iter_init (&iter, properties);
	while (g_variant_iter_next (&iter, "{&sv}", &key, &value)) {
		if (g_str_equal (key, "Percentage")) {
			g_object_set (device,
				      "percentage", (gdouble) g_variant_get_byte (value),
				      "update-time", (guint64) g_get_real_time () / G_USEC_PER_SEC,
				      NULL);
		} else if (g_str_equal (key, "Alias")) {
			g_object_set (device,
				      "model", g_variant_get_string (value, NULL),
				      NULL);
		} else {
			char *str = g_variant_print (value, TRUE);

			g_warning ("Unhandled key: %s value: %s", key, str);
			g_free (str);
		}
		g_variant_unref (value);
	}
}

static void
up_device_bluez_class_init (UpDeviceBluezClass *klass)
{
	UpDeviceClass *device_class = UP_DEVICE_CLASS (klass);

	device_class->coldplug = up_device_bluez_coldplug;
}