~ubuntu-branches/ubuntu/utopic/ofono/utopic-proposed

« back to all changes in this revision

Viewing changes to dundee/bluetooth.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2012-08-22 19:59:08 UTC
  • mfrom: (1.4.3)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20120822195908-20ccett2uhgcz7f6
Tags: upstream-1.9
ImportĀ upstreamĀ versionĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  oFono - Open Source Telephony
 
3
 *
 
4
 *  Copyright (C) 2008-2012  Intel Corporation. All rights reserved.
 
5
 *  Copyright (C) 2012  BMW Car IT GmbH. All rights reserved.
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License version 2 as
 
9
 *  published by the Free Software Foundation.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 *
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
#include <fcntl.h>
 
29
#include <string.h>
 
30
#include <errno.h>
 
31
 
 
32
#include <glib.h>
 
33
 
 
34
#include "plugins/bluetooth.h"
 
35
 
 
36
#include "dundee.h"
 
37
 
 
38
static GHashTable *bluetooth_hash;
 
39
 
 
40
struct bluetooth_device {
 
41
        struct dundee_device *device;
 
42
 
 
43
        char *path;
 
44
        char *address;
 
45
        char *name;
 
46
 
 
47
        DBusPendingCall *call;
 
48
};
 
49
 
 
50
static void bt_disconnect(struct dundee_device *device,
 
51
                                dundee_device_disconnect_cb_t cb, void *data)
 
52
{
 
53
        struct bluetooth_device *bt = dundee_device_get_data(device);
 
54
 
 
55
        DBG("%p", bt);
 
56
 
 
57
        CALLBACK_WITH_SUCCESS(cb, data);
 
58
}
 
59
 
 
60
static void bt_connect_reply(DBusPendingCall *call, gpointer user_data)
 
61
{
 
62
        struct cb_data *cbd = user_data;
 
63
        dundee_device_connect_cb_t cb = cbd->cb;
 
64
        struct bluetooth_device *bt = cbd->user;
 
65
        DBusMessage *reply;
 
66
        DBusError derr;
 
67
        int fd;
 
68
 
 
69
        DBG("%p", bt);
 
70
 
 
71
        reply = dbus_pending_call_steal_reply(call);
 
72
 
 
73
        bt->call = NULL;
 
74
 
 
75
        dbus_error_init(&derr);
 
76
        if (dbus_set_error_from_message(&derr, reply)) {
 
77
                DBG("Connection to bt serial returned with error: %s, %s",
 
78
                                                derr.name, derr.message);
 
79
 
 
80
                dbus_error_free(&derr);
 
81
 
 
82
                CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
 
83
                goto done;
 
84
        }
 
85
 
 
86
        dbus_message_get_args(reply, NULL, DBUS_TYPE_UNIX_FD, &fd,
 
87
                        DBUS_TYPE_INVALID);
 
88
 
 
89
        DBG("%p fd %d", bt, fd);
 
90
 
 
91
        if (fd < 0) {
 
92
                CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
 
93
                goto done;
 
94
        }
 
95
 
 
96
        CALLBACK_WITH_SUCCESS(cb, fd, cbd->data);
 
97
 
 
98
done:
 
99
        dbus_message_unref(reply);
 
100
        g_free(cbd);
 
101
}
 
102
 
 
103
static void bt_connect(struct dundee_device *device,
 
104
                        dundee_device_connect_cb_t cb, void *data)
 
105
{
 
106
        struct bluetooth_device *bt = dundee_device_get_data(device);
 
107
        struct cb_data *cbd = cb_data_new(cb, data);
 
108
        char *profile = "dun";
 
109
        int status;
 
110
 
 
111
        DBG("%p", bt);
 
112
 
 
113
        cbd->user = bt;
 
114
 
 
115
        status = bluetooth_send_with_reply(bt->path,
 
116
                                        BLUEZ_SERIAL_INTERFACE, "ConnectFD",
 
117
                                        &bt->call, bt_connect_reply,
 
118
                                        cbd, NULL, DBUS_TIMEOUT,
 
119
                                        DBUS_TYPE_STRING, &profile,
 
120
                                        DBUS_TYPE_INVALID);
 
121
        if (status == 0)
 
122
                return;
 
123
 
 
124
        g_free(cbd);
 
125
 
 
126
        CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
 
127
}
 
128
 
 
129
struct dundee_device_driver bluetooth_driver = {
 
130
        .name = "bluetooth",
 
131
        .connect = bt_connect,
 
132
        .disconnect = bt_disconnect,
 
133
};
 
134
 
 
135
static int bt_probe(const char *path, const char *dev_addr,
 
136
                                const char *adapter_addr, const char *alias)
 
137
{
 
138
        struct bluetooth_device *bt;
 
139
        struct dundee_device *device;
 
140
        char buf[256];
 
141
 
 
142
        DBG("");
 
143
 
 
144
        /* We already have this device in our hash, ignore */
 
145
        if (g_hash_table_lookup(bluetooth_hash, path) != NULL)
 
146
                return -EALREADY;
 
147
 
 
148
        ofono_info("Using device: %s, devaddr: %s, adapter: %s",
 
149
                        path, dev_addr, adapter_addr);
 
150
 
 
151
        strcpy(buf, "dun/");
 
152
        bluetooth_create_path(dev_addr, adapter_addr, buf + 4, sizeof(buf) - 4);
 
153
 
 
154
        bt = g_try_new0(struct bluetooth_device, 1);
 
155
        if (bt == NULL)
 
156
                return -ENOMEM;
 
157
 
 
158
        DBG("%p", bt);
 
159
 
 
160
        device = dundee_device_create(&bluetooth_driver);
 
161
        if (device == NULL)
 
162
                goto free;
 
163
 
 
164
        dundee_device_set_data(device, bt);
 
165
 
 
166
        bt->path = g_strdup(path);
 
167
        if (bt->path == NULL)
 
168
                goto free;
 
169
 
 
170
        bt->address = g_strdup(dev_addr);
 
171
        if (bt->address == NULL)
 
172
                goto free;
 
173
 
 
174
        bt->name = g_strdup(alias);
 
175
        if (bt->name == NULL)
 
176
                goto free;
 
177
 
 
178
        dundee_device_set_name(device, bt->name);
 
179
 
 
180
        if (dundee_device_register(device) < 0) {
 
181
                g_free(device);
 
182
                goto free;
 
183
        }
 
184
 
 
185
        bt->device = device;
 
186
        g_hash_table_insert(bluetooth_hash, g_strdup(path), bt);
 
187
 
 
188
        return 0;
 
189
 
 
190
free:
 
191
        g_free(bt->path);
 
192
        g_free(bt->address);
 
193
        g_free(bt->name);
 
194
        g_free(bt);
 
195
 
 
196
        return -ENOMEM;
 
197
}
 
198
 
 
199
static void destroy_device(gpointer user)
 
200
{
 
201
        struct bluetooth_device *bt = user;
 
202
 
 
203
        DBG("%p", bt);
 
204
 
 
205
        if (bt->call != NULL)
 
206
                dbus_pending_call_cancel(bt->call);
 
207
 
 
208
        g_free(bt->path);
 
209
        g_free(bt->address);
 
210
 
 
211
        g_free(bt);
 
212
}
 
213
 
 
214
static gboolean bt_remove_device(gpointer key, gpointer value,
 
215
                                        gpointer user_data)
 
216
{
 
217
        struct bluetooth_device *bt = value;
 
218
        const char *path = key;
 
219
        const char *prefix = user_data;
 
220
 
 
221
        DBG("%p", bt);
 
222
 
 
223
        if (prefix && g_str_has_prefix(path, prefix) == FALSE)
 
224
                return FALSE;
 
225
 
 
226
        dundee_device_unregister(bt->device);
 
227
 
 
228
        return TRUE;
 
229
}
 
230
 
 
231
static void bt_remove(const char *prefix)
 
232
{
 
233
        DBG("%s", prefix);
 
234
 
 
235
        if (bluetooth_hash == NULL)
 
236
                return;
 
237
 
 
238
        g_hash_table_foreach_remove(bluetooth_hash, bt_remove_device,
 
239
                                                        (gpointer) prefix);
 
240
}
 
241
 
 
242
static void bt_set_alias(const char *path, const char *alias)
 
243
{
 
244
        struct bluetooth_device *bt;
 
245
 
 
246
        DBG("");
 
247
 
 
248
        if (path == NULL || alias == NULL)
 
249
                return;
 
250
 
 
251
        bt = g_hash_table_lookup(bluetooth_hash, path);
 
252
        if (bt == NULL)
 
253
                return;
 
254
 
 
255
        g_free(bt->name);
 
256
        bt->name = g_strdup(alias);
 
257
 
 
258
        dundee_device_set_name(bt->device, bt->name);
 
259
}
 
260
 
 
261
static struct bluetooth_profile dun_profile = {
 
262
        .name           = "dun_dt",
 
263
        .probe          = bt_probe,
 
264
        .remove         = bt_remove,
 
265
        .set_alias      = bt_set_alias,
 
266
};
 
267
 
 
268
int __dundee_bluetooth_init(void)
 
269
{
 
270
        int err;
 
271
 
 
272
        DBG("");
 
273
 
 
274
        err = bluetooth_register_uuid(DUN_GW_UUID, &dun_profile);
 
275
        if (err < 0)
 
276
                return err;
 
277
 
 
278
        bluetooth_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
 
279
                                                g_free, destroy_device);
 
280
 
 
281
        return 0;
 
282
}
 
283
 
 
284
void  __dundee_bluetooth_cleanup(void)
 
285
{
 
286
        DBG("");
 
287
 
 
288
        bluetooth_unregister_uuid(DUN_GW_UUID);
 
289
        g_hash_table_destroy(bluetooth_hash);
 
290
}