1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation; either version 2 of the License, or
11 *  (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *  GNU General Public License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with this program; if not, write to the Free Software
20 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include <errno.h>
29#include <dbus/dbus.h>
30
31#include <bluetooth/bluetooth.h>
32#include <bluetooth/hci.h>
33#include <bluetooth/hci_lib.h>
34
35#include "plugin.h"
36#include "adapter.h"
37#include "logging.h"
38#include "dbus-hci.h"
39
40static void formfactor_reply(DBusPendingCall *call, void *user_data)
41{
42	struct btd_adapter *adapter = user_data;
43	const char *formfactor = NULL;
44	DBusMessage *reply;
45	uint8_t cls[3], minor = 0;
46	int dd;
47
48	reply = dbus_pending_call_steal_reply(call);
49
50	if (dbus_set_error_from_message(NULL, reply) == TRUE) {
51		error("Failed to access HAL");
52		dbus_message_unref(reply);
53		return;
54	}
55
56	if (dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &formfactor,
57						DBUS_TYPE_INVALID) == FALSE) {
58		error("Wrong formfactor arguments");
59		dbus_message_unref(reply);
60		return;
61	}
62
63	debug("Computer is classified as %s", formfactor);
64
65	if (formfactor != NULL) {
66		if (g_str_equal(formfactor, "laptop") == TRUE)
67			minor |= (1 << 2) | (1 << 3);
68		else if (g_str_equal(formfactor, "desktop") == TRUE)
69			minor |= 1 << 2;
70		else if (g_str_equal(formfactor, "server") == TRUE)
71			minor |= 1 << 3;
72		else if (g_str_equal(formfactor, "handheld") == TRUE)
73			minor += 1 << 4;
74	}
75
76	dbus_message_unref(reply);
77
78	dd = hci_open_dev(adapter_get_dev_id(adapter));
79	if (dd < 0)
80		return;
81
82	if (hci_read_class_of_dev(dd, cls, 500) < 0) {
83		hci_close_dev(dd);
84		return;
85	}
86
87	debug("Current device class is 0x%02x%02x%02x\n",
88						cls[2], cls[1], cls[0]);
89
90	/* Computer major class */
91	debug("Setting 0x%06x for major/minor device class", (1 << 8) | minor);
92
93	hci_close_dev(dd);
94
95	set_major_and_minor_class(adapter, 0x01, minor);
96}
97
98static DBusConnection *connection;
99
100static int hal_probe(struct btd_adapter *adapter)
101{
102	const char *property = "system.formfactor";
103	DBusMessage *message;
104	DBusPendingCall *call;
105
106	connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
107	if (connection == NULL)
108		return -ENOMEM;
109
110	message = dbus_message_new_method_call("org.freedesktop.Hal",
111				"/org/freedesktop/Hal/devices/computer",
112						"org.freedesktop.Hal.Device",
113							"GetPropertyString");
114	if (message == NULL) {
115		error("Failed to create formfactor request");
116		dbus_connection_unref(connection);
117		return -ENOMEM;
118	}
119
120	dbus_message_append_args(message, DBUS_TYPE_STRING, &property,
121							DBUS_TYPE_INVALID);
122
123	if (dbus_connection_send_with_reply(connection, message,
124						&call, -1) == FALSE) {
125		error("Failed to send formfactor request");
126		dbus_message_unref(message);
127		dbus_connection_unref(connection);
128		return -EIO;
129	}
130
131	dbus_pending_call_set_notify(call, formfactor_reply, adapter, NULL);
132
133	dbus_pending_call_unref(call);
134
135	dbus_message_unref(message);
136
137	return 0;
138}
139
140static void hal_remove(struct btd_adapter *adapter)
141{
142	dbus_connection_unref(connection);
143}
144
145static struct btd_adapter_driver hal_driver = {
146	.name	= "hal",
147	.probe	= hal_probe,
148	.remove	= hal_remove,
149};
150
151static int hal_init(void)
152{
153	return btd_register_adapter_driver(&hal_driver);
154}
155
156static void hal_exit(void)
157{
158	btd_unregister_adapter_driver(&hal_driver);
159}
160
161BLUETOOTH_PLUGIN_DEFINE(hal, VERSION,
162		BLUETOOTH_PLUGIN_PRIORITY_LOW, hal_init, hal_exit)
163