summaryrefslogtreecommitdiff
path: root/gwobex/gw-obex.c
blob: e6566929626e671eeb945b8003e76d587690229e (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
  @file gw-obex.c

  @author Johan Hedberg <johan.hedberg@nokia.com>

  Copyright (C) 2004-2006 Nokia Corporation. All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License, version 2.1, as published by the Free Software Foundation.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA.

*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <glib.h>

#include "log.h"
#include "gw-obex.h"
#include "utils.h"
#include "obex-xfer.h"
#include "obex-priv.h"


gboolean gw_obex_get_file(GwObex *ctx,
                          const gchar *local,
                          const gchar *remote,
                          const gchar *type,
                          gint *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_get(ctx, local, remote, type, NULL, 0, NULL, NULL, -1, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_get_fd(GwObex      *ctx,
                        gint         fd,
                        const gchar *remote,
                        const gchar *type,
                        gint        *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_get(ctx, NULL, remote, type, NULL, 0, NULL, NULL, fd, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_put_fd(GwObex      *ctx,
                        gint         fd,
                        const gchar *remote,
                        const gchar *type,
                        gint        *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_put(ctx, NULL, remote, type, NULL, 0, NULL, 0, -1, fd, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_put_file(GwObex      *ctx,
                          const gchar *local,
                          const gchar *remote,
                          const gchar *type,
                          gint        *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_put(ctx, local, remote, type, NULL, 0, NULL, 0, -1, -1, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_get_buf(GwObex       *ctx,
                         const gchar  *remote,
                         const gchar  *type,
                         gchar       **buf,
                         gint         *buf_size,
                         gint         *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_get(ctx, NULL, remote, type, NULL, 0, buf, buf_size, -1, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_put_buf(GwObex      *ctx,
                         const gchar *remote,
                         const gchar *type,
                         const gchar *buf,
                         gint         buf_size,
                         gint         time,
                         gint        *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_put(ctx, NULL, remote, type, NULL, 0, buf, buf_size, time, -1, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_get_buf_with_apparam(GwObex        *ctx,
                                      const gchar   *remote,
                                      const gchar   *type,
                                      const guint8  *apparam,
                                      gint           apparam_size,
                                      gchar        **buf,
                                      gint          *buf_size,
                                      gint          *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_get(ctx, NULL, remote, type, apparam, apparam_size, buf, buf_size, -1, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_put_buf_with_apparam(GwObex       *ctx,
                                      const gchar  *remote,
                                      const gchar  *type,
                                      const guint8 *apparam,
                                      gint          apparam_size,
                                      const gchar  *buf,
                                      gint          buf_size,
                                      gint          time,
                                      gint         *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_put(ctx, NULL, remote, type, apparam, apparam_size, buf, buf_size, time, -1, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_chdir(GwObex *ctx, const gchar *dir, gint *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_setpath(ctx, dir ? dir : "", 0);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_mkdir(GwObex *ctx, const gchar *dir, gint *error) {
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    if (!gw_obex_setpath(ctx, dir ? dir : "", SETPATH_CREATE)) {
        gw_obex_get_error(ctx, error);
        GW_OBEX_UNLOCK(ctx);
        return FALSE;
    }
    (void) gw_obex_setpath(ctx, "..", 0);
    GW_OBEX_UNLOCK(ctx);
    return TRUE;
}

gboolean gw_obex_read_dir(GwObex *ctx, const gchar *dir,
                          gchar **buf, gint *buf_size, gint *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_get(ctx, NULL, dir ? dir : "", LST_TYPE, NULL, 0, buf, buf_size, -1, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    else if (*buf_size > 0) {
        /* Hack for some OBEX implementations which send nul's
         * at the end of the listing */
        int i;

        for (i = *buf_size - 1; i > 0; i--) {
            if ((*buf)[i] == '\0')
                (*buf_size)--;
            else
                break;
        }
    }
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_delete(GwObex *ctx, const gchar *name, gint *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_put(ctx, NULL, name, NULL, NULL, 0, NULL, 0, -1, -1, FALSE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_move(GwObex *ctx, const gchar *src, const gchar *dst, gint *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_action_op(ctx, src, dst, OBEX_ACTION_MOVE);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_copy(GwObex *ctx, const gchar *src, const gchar *dst, gint *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_action_op(ctx, src, dst, OBEX_ACTION_COPY);
    if (ret == FALSE)
        gw_obex_get_error(ctx, error);
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

gboolean gw_obex_get_capability(GwObex *ctx, gchar **cap, gint *cap_len, gint *error) {
    gboolean ret;
    GW_OBEX_LOCK(ctx);
    CHECK_DISCONNECT(FALSE, error, ctx);
    ret = gw_obex_get(ctx, NULL, NULL, CAP_TYPE, NULL, 0, cap, cap_len, -1, FALSE);
    if (ret == FALSE) {
        *cap = NULL;
        *cap_len = 0;
        gw_obex_get_error(ctx, error);
    }
    GW_OBEX_UNLOCK(ctx);
    return ret;
}

void gw_obex_set_disconnect_callback(GwObex *ctx, gw_obex_disconnect_cb_t callback, gpointer data) {
    GW_OBEX_LOCK(ctx);
    ctx->dc_cb = callback;
    ctx->dc_data = data;
    GW_OBEX_UNLOCK(ctx);
}

void gw_obex_set_progress_callback(GwObex *ctx, gw_obex_progress_cb_t callback, gpointer data) {
    GW_OBEX_LOCK(ctx);
    ctx->pr_cb = callback;
    ctx->pr_data = data;
    GW_OBEX_UNLOCK(ctx);
}

void gw_obex_set_cancel_callback(GwObex *ctx, gw_obex_cancel_cb_t callback, gpointer data) {
    GW_OBEX_LOCK(ctx);
    ctx->cancel_cb = callback;
    ctx->cancel_data = data;
    GW_OBEX_UNLOCK(ctx);
}

void gw_obex_close(GwObex *ctx) {
    GW_OBEX_LOCK(ctx);
    if (ctx->xfer) {
        GwObexXfer *xfer = ctx->xfer;
        GW_OBEX_UNLOCK(ctx);
        gw_obex_xfer_close(ctx->xfer, NULL);
        GW_OBEX_LOCK(ctx);
        /* In the async case the caller of put/get_async owns the xfer object */
        if (!xfer->async)
            _gw_obex_xfer_free(xfer);
        ctx->xfer = NULL;
    }
    if (ctx->conn_fd >= 0) {
        if (!gw_obex_disconnect(ctx))
            debug("OBEX Disconnect command failed\n");
        OBEX_TransportDisconnect(ctx->handle);
        close(ctx->conn_fd);
        ctx->conn_fd = -1;
    }
    if (ctx->handle) {
        OBEX_Cleanup(ctx->handle);
        ctx->handle = NULL;
    }
    if (ctx->gio) {
        g_io_channel_unref(ctx->gio);
        ctx->gio = NULL;
    }
    if (ctx->gio_source) {
        g_source_destroy(ctx->gio_source);
        ctx->gio_source = NULL;
    }
    GW_OBEX_UNLOCK(ctx);
#ifdef GW_OBEX_THREADS_ENABLED
    g_mutex_free(ctx->mutex);
    ctx->mutex = NULL;
#endif
    g_free(ctx);
}

GwObex *gw_obex_setup_fd(int fd, const gchar *uuid, gint uuid_len,
                         GMainContext *context, gint *error) {
    obex_t *handle;
    GwObex *ctx;

    if (!gw_obex_transport_setup(fd, &handle)) {
        debug("gw_obex_open() failed\n");
        if (error)
            *error = GW_OBEX_ERROR_CONNECT_FAILED;
        return NULL;
    }

    debug("Transport connection opened.\n");

    ctx = make_context(handle);

#ifdef GW_OBEX_THREADS_ENABLED
    if (!g_thread_supported())
        g_thread_init(NULL);
    ctx->mutex = g_mutex_new();
#endif

    OBEX_SetCustomData(handle, ctx);

    debug("Connecting to OBEX service\n");
    if (!gw_obex_connect(ctx, uuid, uuid_len)) {
        debug("Unable to connect to OBEX service\n");
#ifdef GW_OBEX_THREADS_ENABLED
        g_mutex_free(ctx->mutex);
        ctx->mutex = NULL;
#endif
        g_free(ctx);
        OBEX_Cleanup(handle);
        if (error)
            *error = GW_OBEX_ERROR_NO_SERVICE;
        return NULL;
    }

    debug("Connected (Connection ID: %#x)\n", ctx->conid);

    ctx->gio = g_io_channel_unix_new(ctx->conn_fd);
    ctx->gio_source = g_io_create_watch (ctx->gio,
                                         G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL);
    g_source_set_callback(ctx->gio_source, (GSourceFunc)gw_obex_cb, ctx, NULL);
    (void) g_source_attach(ctx->gio_source, context);
    g_source_unref(ctx->gio_source);

    ctx->main_ctx = context;

    return ctx;
}

GwObex *gw_obex_setup_dev(const char *dev, const gchar *uuid, gint uuid_len,
                          GMainContext *context, gint *error) {
    GwObex *ctx;
    int fd;

    fd = open(dev, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0) {
        debug("open(\"%s\"): %s\n", dev, strerror(errno));
        if (error)
            *error = GW_OBEX_ERROR_CONNECT_FAILED;
        return NULL;
    }

    if (!fd_raw_mode(fd)) {
        debug("setting raw mode failed\n");
        close(fd);
        if (error)
            *error = GW_OBEX_ERROR_CONNECT_FAILED;
        return NULL;
    }

    ctx = gw_obex_setup_fd(fd, uuid, uuid_len, context, error);
    if (ctx == NULL) {
        close(fd);
    }

    return ctx;
}