~ubuntu-branches/ubuntu/wily/bluez/wily

« back to all changes in this revision

Viewing changes to input/manager.c

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2008-10-07 12:10:29 UTC
  • Revision ID: james.westby@ubuntu.com-20081007121029-4gup4fmmh2vfo5nh
Tags: upstream-4.12
ImportĀ upstreamĀ versionĀ 4.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  BlueZ - Bluetooth protocol stack for Linux
 
4
 *
 
5
 *  Copyright (C) 2004-2008  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
 
 
30
#include <bluetooth/bluetooth.h>
 
31
#include <bluetooth/hci.h>
 
32
#include <bluetooth/sdp.h>
 
33
#include <bluetooth/sdp_lib.h>
 
34
 
 
35
#include <gdbus.h>
 
36
 
 
37
#include "logging.h"
 
38
#include "../src/adapter.h"
 
39
#include "../src/device.h"
 
40
 
 
41
#include "device.h"
 
42
#include "server.h"
 
43
 
 
44
static int idle_timeout = 0;
 
45
 
 
46
static DBusConnection *connection = NULL;
 
47
 
 
48
static void input_remove(struct btd_device *device, const char *uuid)
 
49
{
 
50
        const gchar *path = device_get_path(device);
 
51
 
 
52
        DBG("path %s", path);
 
53
 
 
54
        input_device_unregister(path, uuid);
 
55
}
 
56
 
 
57
static int hid_device_probe(struct btd_device *device, GSList *records)
 
58
{
 
59
        struct btd_adapter *adapter = device_get_adapter(device);
 
60
        const gchar *path = device_get_path(device);
 
61
        sdp_record_t *rec = records->data;
 
62
        sdp_data_t *pdlist;
 
63
        bdaddr_t src, dst;
 
64
        uint32_t handle;
 
65
 
 
66
        DBG("path %s", path);
 
67
 
 
68
        adapter_get_address(adapter, &src);
 
69
        device_get_address(device, &dst);
 
70
        pdlist = sdp_data_get(rec, SDP_SERVER_RECORD_HANDLE);
 
71
        if (!pdlist)
 
72
                return -1;
 
73
 
 
74
        handle = pdlist->val.uint32;
 
75
 
 
76
        return input_device_register(connection, path, &src, &dst,
 
77
                                HID_UUID, handle, idle_timeout * 60);
 
78
}
 
79
 
 
80
static void hid_device_remove(struct btd_device *device)
 
81
{
 
82
        input_remove(device, HID_UUID);
 
83
}
 
84
 
 
85
static int headset_probe(struct btd_device *device, GSList *records)
 
86
{
 
87
        struct btd_adapter *adapter = device_get_adapter(device);
 
88
        const gchar *path = device_get_path(device);
 
89
        sdp_record_t *record = records->data;
 
90
        sdp_list_t *protos;
 
91
        uint8_t ch;
 
92
        bdaddr_t src, dst;
 
93
 
 
94
        DBG("path %s", path);
 
95
 
 
96
        if (sdp_get_access_protos(record, &protos) < 0) {
 
97
                error("Invalid record");
 
98
                return -EINVAL;
 
99
        }
 
100
 
 
101
        ch = sdp_get_proto_port(protos, RFCOMM_UUID);
 
102
        sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL);
 
103
        sdp_list_free(protos, NULL);
 
104
 
 
105
        if (ch <= 0) {
 
106
                error("Invalid RFCOMM channel");
 
107
                return -EINVAL;
 
108
        }
 
109
 
 
110
        adapter_get_address(adapter, &src);
 
111
        device_get_address(device, &dst);
 
112
 
 
113
        return fake_input_register(connection, path, &src, &dst,
 
114
                                HSP_HS_UUID, ch);
 
115
}
 
116
 
 
117
static void headset_remove(struct btd_device *device)
 
118
{
 
119
        input_remove(device, HSP_HS_UUID);
 
120
}
 
121
 
 
122
static int hid_server_probe(struct btd_adapter *adapter)
 
123
{
 
124
        bdaddr_t src;
 
125
 
 
126
        adapter_get_address(adapter, &src);
 
127
 
 
128
        return server_start(&src);
 
129
}
 
130
 
 
131
static void hid_server_remove(struct btd_adapter *adapter)
 
132
{
 
133
        bdaddr_t src;
 
134
 
 
135
        adapter_get_address(adapter, &src);
 
136
 
 
137
        server_stop(&src);
 
138
}
 
139
 
 
140
static struct btd_device_driver input_hid_driver = {
 
141
        .name   = "input-hid",
 
142
        .uuids  = BTD_UUIDS(HID_UUID),
 
143
        .probe  = hid_device_probe,
 
144
        .remove = hid_device_remove,
 
145
};
 
146
 
 
147
static struct btd_device_driver input_headset_driver = {
 
148
        .name   = "input-headset",
 
149
        .uuids  = BTD_UUIDS(HSP_HS_UUID),
 
150
        .probe  = headset_probe,
 
151
        .remove = headset_remove,
 
152
};
 
153
 
 
154
static struct btd_adapter_driver input_server_driver = {
 
155
        .name   = "input-server",
 
156
        .probe  = hid_server_probe,
 
157
        .remove = hid_server_remove,
 
158
};
 
159
 
 
160
int input_manager_init(DBusConnection *conn, GKeyFile *config)
 
161
{
 
162
        GError *err = NULL;
 
163
 
 
164
        if (config) {
 
165
                idle_timeout = g_key_file_get_integer(config, "General",
 
166
                                                "IdleTimeout", &err);
 
167
                if (err) {
 
168
                        debug("input.conf: %s", err->message);
 
169
                        g_error_free(err);
 
170
                }
 
171
        }
 
172
 
 
173
        connection = dbus_connection_ref(conn);
 
174
 
 
175
        btd_register_adapter_driver(&input_server_driver);
 
176
 
 
177
        btd_register_device_driver(&input_hid_driver);
 
178
        btd_register_device_driver(&input_headset_driver);
 
179
 
 
180
        return 0;
 
181
}
 
182
 
 
183
void input_manager_exit(void)
 
184
{
 
185
        btd_unregister_device_driver(&input_hid_driver);
 
186
        btd_unregister_device_driver(&input_headset_driver);
 
187
 
 
188
        btd_unregister_adapter_driver(&input_server_driver);
 
189
 
 
190
        dbus_connection_unref(connection);
 
191
 
 
192
        connection = NULL;
 
193
}