1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2010 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 "log.h"
38 #include "dbus-hci.h"
39
formfactor_reply(DBusPendingCall * call,void * user_data)40 static 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 minor = 0;
46
47 reply = dbus_pending_call_steal_reply(call);
48
49 if (dbus_set_error_from_message(NULL, reply) == TRUE) {
50 error("Failed to access HAL");
51 dbus_message_unref(reply);
52 return;
53 }
54
55 if (dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &formfactor,
56 DBUS_TYPE_INVALID) == FALSE) {
57 error("Wrong formfactor arguments");
58 dbus_message_unref(reply);
59 return;
60 }
61
62 DBG("Computer is classified as %s", formfactor);
63
64 if (formfactor != NULL) {
65 if (g_str_equal(formfactor, "laptop") == TRUE)
66 minor |= (1 << 2) | (1 << 3);
67 else if (g_str_equal(formfactor, "desktop") == TRUE)
68 minor |= 1 << 2;
69 else if (g_str_equal(formfactor, "server") == TRUE)
70 minor |= 1 << 3;
71 else if (g_str_equal(formfactor, "handheld") == TRUE)
72 minor += 1 << 4;
73 }
74
75 dbus_message_unref(reply);
76
77 /* Computer major class */
78 DBG("Setting 0x%06x for major/minor device class", (1 << 8) | minor);
79
80 btd_adapter_set_class(adapter, 0x01, minor);
81 }
82
83 static DBusConnection *connection;
84
hal_probe(struct btd_adapter * adapter)85 static int hal_probe(struct btd_adapter *adapter)
86 {
87 const char *property = "system.formfactor";
88 DBusMessage *message;
89 DBusPendingCall *call;
90
91 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
92 if (connection == NULL)
93 return -ENOMEM;
94
95 message = dbus_message_new_method_call("org.freedesktop.Hal",
96 "/org/freedesktop/Hal/devices/computer",
97 "org.freedesktop.Hal.Device",
98 "GetPropertyString");
99 if (message == NULL) {
100 error("Failed to create formfactor request");
101 dbus_connection_unref(connection);
102 return -ENOMEM;
103 }
104
105 dbus_message_append_args(message, DBUS_TYPE_STRING, &property,
106 DBUS_TYPE_INVALID);
107
108 if (dbus_connection_send_with_reply(connection, message,
109 &call, -1) == FALSE) {
110 error("Failed to send formfactor request");
111 dbus_message_unref(message);
112 dbus_connection_unref(connection);
113 return -EIO;
114 }
115
116 dbus_pending_call_set_notify(call, formfactor_reply, adapter, NULL);
117
118 dbus_pending_call_unref(call);
119
120 dbus_message_unref(message);
121
122 return 0;
123 }
124
hal_remove(struct btd_adapter * adapter)125 static void hal_remove(struct btd_adapter *adapter)
126 {
127 dbus_connection_unref(connection);
128 }
129
130 static struct btd_adapter_driver hal_driver = {
131 .name = "hal",
132 .probe = hal_probe,
133 .remove = hal_remove,
134 };
135
hal_init(void)136 static int hal_init(void)
137 {
138 return btd_register_adapter_driver(&hal_driver);
139 }
140
hal_exit(void)141 static void hal_exit(void)
142 {
143 btd_unregister_adapter_driver(&hal_driver);
144 }
145
146 BLUETOOTH_PLUGIN_DEFINE(hal, VERSION,
147 BLUETOOTH_PLUGIN_PRIORITY_LOW, hal_init, hal_exit)
148