2011-06-21 01:32:02 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* OBEX library with GLib integration
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-08-04 06:20:38 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-06-27 04:06:16 +08:00
|
|
|
#include <unistd.h>
|
2011-06-21 19:48:54 +08:00
|
|
|
#include <string.h>
|
2011-07-12 16:52:05 +08:00
|
|
|
#include <errno.h>
|
2011-06-21 19:48:54 +08:00
|
|
|
|
2011-06-21 01:32:02 +08:00
|
|
|
#include "gobex.h"
|
2011-08-04 06:20:38 +08:00
|
|
|
#include "glib-helper.h"
|
2011-06-21 01:32:02 +08:00
|
|
|
|
2011-06-27 04:17:20 +08:00
|
|
|
#define G_OBEX_DEFAULT_MTU 4096
|
|
|
|
#define G_OBEX_MINIMUM_MTU 255
|
|
|
|
#define G_OBEX_MAXIMUM_MTU 65535
|
|
|
|
|
2011-06-29 23:30:18 +08:00
|
|
|
#define G_OBEX_DEFAULT_TIMEOUT 5
|
|
|
|
|
2011-07-05 23:57:23 +08:00
|
|
|
#define G_OBEX_OP_NONE 0xff
|
|
|
|
|
2011-06-29 19:37:34 +08:00
|
|
|
#define FINAL_BIT 0x80
|
|
|
|
|
2011-07-05 04:02:52 +08:00
|
|
|
#define CONNID_INVALID 0xffffffff
|
|
|
|
|
2011-06-21 01:32:02 +08:00
|
|
|
struct _GObex {
|
|
|
|
gint ref_count;
|
|
|
|
GIOChannel *io;
|
2011-06-27 15:23:36 +08:00
|
|
|
guint io_source;
|
|
|
|
|
2011-06-30 02:23:14 +08:00
|
|
|
gboolean (*read) (GObex *obex, GError **err);
|
|
|
|
gboolean (*write) (GObex *obex, GError **err);
|
2011-06-27 21:54:59 +08:00
|
|
|
|
2011-06-27 15:23:36 +08:00
|
|
|
guint8 *rx_buf;
|
|
|
|
size_t rx_data;
|
|
|
|
guint16 rx_pkt_len;
|
2011-07-05 23:57:23 +08:00
|
|
|
guint8 rx_last_op;
|
2011-06-21 05:29:26 +08:00
|
|
|
|
2011-06-27 19:08:33 +08:00
|
|
|
guint8 *tx_buf;
|
|
|
|
size_t tx_data;
|
|
|
|
size_t tx_sent;
|
|
|
|
|
2011-07-07 19:33:42 +08:00
|
|
|
gboolean suspended;
|
|
|
|
|
2011-06-27 19:08:33 +08:00
|
|
|
guint write_source;
|
|
|
|
|
2011-07-01 17:24:33 +08:00
|
|
|
gssize io_rx_mtu;
|
|
|
|
gssize io_tx_mtu;
|
|
|
|
|
2011-06-27 04:17:20 +08:00
|
|
|
guint16 rx_mtu;
|
|
|
|
guint16 tx_mtu;
|
|
|
|
|
2011-07-05 04:02:52 +08:00
|
|
|
guint32 conn_id;
|
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
GQueue *tx_queue;
|
2011-06-27 16:06:48 +08:00
|
|
|
|
2011-07-06 03:51:32 +08:00
|
|
|
GSList *req_handlers;
|
|
|
|
|
2011-07-05 18:16:35 +08:00
|
|
|
GObexFunc disconn_func;
|
2011-07-01 16:25:20 +08:00
|
|
|
gpointer disconn_func_data;
|
2011-06-29 17:20:42 +08:00
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
struct pending_pkt *pending_req;
|
2011-06-27 21:17:04 +08:00
|
|
|
};
|
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
struct pending_pkt {
|
2011-06-27 21:17:04 +08:00
|
|
|
guint id;
|
2011-06-30 05:26:19 +08:00
|
|
|
GObex *obex;
|
2011-06-28 22:54:25 +08:00
|
|
|
GObexPacket *pkt;
|
2011-06-29 23:30:18 +08:00
|
|
|
guint timeout;
|
|
|
|
guint timeout_id;
|
2011-06-27 21:17:04 +08:00
|
|
|
GObexResponseFunc rsp_func;
|
|
|
|
gpointer rsp_data;
|
2011-06-30 05:26:19 +08:00
|
|
|
gboolean cancelled;
|
2011-06-21 01:32:02 +08:00
|
|
|
};
|
|
|
|
|
2011-07-06 03:51:32 +08:00
|
|
|
struct req_handler {
|
2011-07-11 15:47:34 +08:00
|
|
|
guint id;
|
2011-07-06 03:51:32 +08:00
|
|
|
guint8 opcode;
|
|
|
|
GObexRequestFunc func;
|
|
|
|
gpointer user_data;
|
|
|
|
};
|
|
|
|
|
2011-06-27 04:06:16 +08:00
|
|
|
struct connect_data {
|
|
|
|
guint8 version;
|
|
|
|
guint8 flags;
|
|
|
|
guint16 mtu;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
struct setpath_data {
|
|
|
|
guint8 flags;
|
|
|
|
guint8 constants;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2011-06-27 21:17:04 +08:00
|
|
|
static ssize_t req_header_offset(guint8 opcode)
|
2011-06-27 04:06:16 +08:00
|
|
|
{
|
2011-06-27 15:56:16 +08:00
|
|
|
switch (opcode) {
|
2011-06-27 04:06:16 +08:00
|
|
|
case G_OBEX_OP_CONNECT:
|
|
|
|
return sizeof(struct connect_data);
|
|
|
|
case G_OBEX_OP_SETPATH:
|
|
|
|
return sizeof(struct setpath_data);
|
|
|
|
case G_OBEX_OP_DISCONNECT:
|
|
|
|
case G_OBEX_OP_PUT:
|
|
|
|
case G_OBEX_OP_GET:
|
|
|
|
case G_OBEX_OP_SESSION:
|
|
|
|
case G_OBEX_OP_ABORT:
|
2011-08-30 17:57:09 +08:00
|
|
|
case G_OBEX_OP_ACTION:
|
2011-06-27 04:06:16 +08:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 21:17:04 +08:00
|
|
|
static ssize_t rsp_header_offset(guint8 opcode)
|
|
|
|
{
|
|
|
|
switch (opcode) {
|
|
|
|
case G_OBEX_OP_CONNECT:
|
|
|
|
return sizeof(struct connect_data);
|
|
|
|
case G_OBEX_OP_SETPATH:
|
|
|
|
case G_OBEX_OP_DISCONNECT:
|
|
|
|
case G_OBEX_OP_PUT:
|
|
|
|
case G_OBEX_OP_GET:
|
|
|
|
case G_OBEX_OP_SESSION:
|
|
|
|
case G_OBEX_OP_ABORT:
|
2011-08-30 17:57:09 +08:00
|
|
|
case G_OBEX_OP_ACTION:
|
2011-06-27 21:17:04 +08:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
static void pending_pkt_free(struct pending_pkt *p)
|
2011-06-27 21:17:04 +08:00
|
|
|
{
|
2011-06-30 05:26:19 +08:00
|
|
|
if (p->obex != NULL)
|
|
|
|
g_obex_unref(p->obex);
|
2011-07-04 16:10:23 +08:00
|
|
|
|
|
|
|
if (p->timeout_id > 0)
|
|
|
|
g_source_remove(p->timeout_id);
|
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
g_obex_packet_free(p->pkt);
|
2011-07-04 16:10:23 +08:00
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
g_free(p);
|
2011-06-27 21:17:04 +08:00
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:18 +08:00
|
|
|
static gboolean req_timeout(gpointer user_data)
|
|
|
|
{
|
|
|
|
GObex *obex = user_data;
|
|
|
|
struct pending_pkt *p = obex->pending_req;
|
|
|
|
|
|
|
|
g_assert(p != NULL);
|
|
|
|
|
|
|
|
obex->pending_req = NULL;
|
|
|
|
|
|
|
|
if (p->rsp_func) {
|
|
|
|
GError *err = g_error_new(G_OBEX_ERROR, G_OBEX_ERROR_TIMEOUT,
|
|
|
|
"Timed out waiting for response");
|
|
|
|
p->rsp_func(obex, err, NULL, p->rsp_data);
|
|
|
|
g_error_free(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_pkt_free(p);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-06-30 02:23:14 +08:00
|
|
|
static gboolean write_stream(GObex *obex, GError **err)
|
2011-06-27 19:08:33 +08:00
|
|
|
{
|
|
|
|
GIOStatus status;
|
|
|
|
gsize bytes_written;
|
|
|
|
gchar *buf;
|
|
|
|
|
2011-06-27 21:54:59 +08:00
|
|
|
buf = (gchar *) &obex->tx_buf[obex->tx_sent];
|
|
|
|
status = g_io_channel_write_chars(obex->io, buf, obex->tx_data,
|
2011-06-30 02:23:14 +08:00
|
|
|
&bytes_written, err);
|
2011-06-27 21:54:59 +08:00
|
|
|
if (status != G_IO_STATUS_NORMAL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
obex->tx_sent += bytes_written;
|
|
|
|
obex->tx_data -= bytes_written;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-06-30 02:23:14 +08:00
|
|
|
static gboolean write_packet(GObex *obex, GError **err)
|
2011-06-27 21:54:59 +08:00
|
|
|
{
|
2011-06-30 19:13:04 +08:00
|
|
|
GIOStatus status;
|
|
|
|
gsize bytes_written;
|
|
|
|
gchar *buf;
|
|
|
|
|
|
|
|
buf = (gchar *) &obex->tx_buf[obex->tx_sent];
|
|
|
|
status = g_io_channel_write_chars(obex->io, buf, obex->tx_data,
|
|
|
|
&bytes_written, err);
|
|
|
|
if (status != G_IO_STATUS_NORMAL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (bytes_written != obex->tx_data)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
obex->tx_sent += bytes_written;
|
|
|
|
obex->tx_data -= bytes_written;
|
|
|
|
|
|
|
|
return TRUE;
|
2011-06-27 21:54:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean write_data(GIOChannel *io, GIOCondition cond,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GObex *obex = user_data;
|
|
|
|
|
2011-06-27 19:08:33 +08:00
|
|
|
if (cond & G_IO_NVAL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (cond & (G_IO_HUP | G_IO_ERR))
|
2011-07-05 23:27:09 +08:00
|
|
|
goto stop_tx;
|
2011-06-27 19:08:33 +08:00
|
|
|
|
|
|
|
if (obex->tx_data == 0) {
|
2011-06-28 22:54:25 +08:00
|
|
|
struct pending_pkt *p = g_queue_pop_head(obex->tx_queue);
|
2011-06-27 19:08:33 +08:00
|
|
|
ssize_t len;
|
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
if (p == NULL)
|
2011-07-05 23:27:09 +08:00
|
|
|
goto stop_tx;
|
2011-06-27 19:08:33 +08:00
|
|
|
|
2011-06-27 21:17:04 +08:00
|
|
|
/* Can't send a request while there's a pending one */
|
2011-06-29 22:30:05 +08:00
|
|
|
if (obex->pending_req && p->id > 0) {
|
|
|
|
g_queue_push_head(obex->tx_queue, p);
|
2011-07-05 23:27:09 +08:00
|
|
|
goto stop_tx;
|
2011-06-29 22:30:05 +08:00
|
|
|
}
|
2011-06-27 21:17:04 +08:00
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
len = g_obex_packet_encode(p->pkt, obex->tx_buf, obex->tx_mtu);
|
2011-07-12 16:52:05 +08:00
|
|
|
if (len == -EAGAIN) {
|
|
|
|
g_queue_push_head(obex->tx_queue, p);
|
|
|
|
g_obex_suspend(obex);
|
|
|
|
goto stop_tx;
|
|
|
|
}
|
|
|
|
|
2011-06-27 21:17:04 +08:00
|
|
|
if (len < 0) {
|
2011-06-28 22:54:25 +08:00
|
|
|
pending_pkt_free(p);
|
2011-06-27 21:17:04 +08:00
|
|
|
goto done;
|
|
|
|
}
|
2011-06-27 19:08:33 +08:00
|
|
|
|
2011-06-29 23:30:18 +08:00
|
|
|
if (p->id > 0) {
|
2011-06-28 22:54:25 +08:00
|
|
|
obex->pending_req = p;
|
2011-06-29 23:30:18 +08:00
|
|
|
p->timeout_id = g_timeout_add_seconds(p->timeout,
|
|
|
|
req_timeout, obex);
|
|
|
|
} else
|
2011-06-28 22:54:25 +08:00
|
|
|
pending_pkt_free(p);
|
2011-06-27 19:08:33 +08:00
|
|
|
|
|
|
|
obex->tx_data = len;
|
|
|
|
obex->tx_sent = 0;
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:33:42 +08:00
|
|
|
if (obex->suspended) {
|
|
|
|
obex->write_source = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-06-30 02:23:14 +08:00
|
|
|
if (!obex->write(obex, NULL))
|
2011-07-05 23:27:09 +08:00
|
|
|
goto stop_tx;
|
2011-06-27 19:08:33 +08:00
|
|
|
|
2011-07-05 23:27:09 +08:00
|
|
|
done:
|
2011-06-28 22:54:25 +08:00
|
|
|
if (obex->tx_data > 0 || g_queue_get_length(obex->tx_queue) > 0)
|
2011-06-27 19:08:33 +08:00
|
|
|
return TRUE;
|
|
|
|
|
2011-07-05 23:27:09 +08:00
|
|
|
stop_tx:
|
2011-07-05 23:57:23 +08:00
|
|
|
obex->rx_last_op = G_OBEX_OP_NONE;
|
2011-06-27 19:08:33 +08:00
|
|
|
obex->tx_data = 0;
|
|
|
|
obex->write_source = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-06-29 22:30:05 +08:00
|
|
|
static void enable_tx(GObex *obex)
|
|
|
|
{
|
|
|
|
GIOCondition cond;
|
|
|
|
|
2011-07-07 19:33:42 +08:00
|
|
|
if (obex->suspended)
|
|
|
|
return;
|
|
|
|
|
2011-06-29 22:30:05 +08:00
|
|
|
if (obex->write_source > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cond = G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
|
|
|
|
obex->write_source = g_io_add_watch(obex->io, cond, write_data, obex);
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:33:04 +08:00
|
|
|
static gboolean g_obex_send_internal(GObex *obex, struct pending_pkt *p,
|
|
|
|
GError **err)
|
2011-06-21 05:29:26 +08:00
|
|
|
{
|
2011-06-27 19:08:33 +08:00
|
|
|
|
2011-06-29 19:33:04 +08:00
|
|
|
if (obex->io == NULL) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR, G_OBEX_ERROR_DISCONNECTED,
|
|
|
|
"The transport is not connected");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-06-30 05:26:19 +08:00
|
|
|
if (g_obex_packet_get_operation(p->pkt, NULL) == G_OBEX_OP_ABORT)
|
|
|
|
g_queue_push_head(obex->tx_queue, p);
|
|
|
|
else
|
|
|
|
g_queue_push_tail(obex->tx_queue, p);
|
2011-06-29 22:30:05 +08:00
|
|
|
|
2011-06-30 05:26:19 +08:00
|
|
|
if (obex->pending_req == NULL || p->id == 0)
|
|
|
|
enable_tx(obex);
|
2011-06-27 19:08:33 +08:00
|
|
|
|
2011-06-21 05:29:26 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-07-05 23:57:23 +08:00
|
|
|
static void init_connect_data(GObex *obex, struct connect_data *data)
|
|
|
|
{
|
|
|
|
guint16 u16;
|
|
|
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
|
|
|
|
data->version = 0x10;
|
|
|
|
data->flags = 0;
|
|
|
|
|
|
|
|
u16 = g_htons(obex->rx_mtu);
|
|
|
|
memcpy(&data->mtu, &u16, sizeof(u16));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void prepare_connect_rsp(GObex *obex, GObexPacket *rsp)
|
|
|
|
{
|
|
|
|
GObexHeader *connid;
|
|
|
|
struct connect_data data;
|
|
|
|
static guint32 next_connid = 1;
|
|
|
|
|
|
|
|
init_connect_data(obex, &data);
|
|
|
|
g_obex_packet_set_data(rsp, &data, sizeof(data), G_OBEX_DATA_COPY);
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
connid = g_obex_packet_find_header(rsp, G_OBEX_HDR_CONNECTION);
|
2011-07-05 23:57:23 +08:00
|
|
|
if (connid != NULL) {
|
|
|
|
g_obex_header_get_uint32(connid, &obex->conn_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
obex->conn_id = next_connid++;
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
connid = g_obex_header_new_uint32(G_OBEX_HDR_CONNECTION,
|
2011-07-05 23:57:23 +08:00
|
|
|
obex->conn_id);
|
|
|
|
g_obex_packet_prepend_header(rsp, connid);
|
|
|
|
}
|
|
|
|
|
2011-06-29 19:33:04 +08:00
|
|
|
gboolean g_obex_send(GObex *obex, GObexPacket *pkt, GError **err)
|
2011-06-28 22:54:25 +08:00
|
|
|
{
|
|
|
|
struct pending_pkt *p;
|
2011-06-29 19:33:04 +08:00
|
|
|
gboolean ret;
|
2011-06-28 22:54:25 +08:00
|
|
|
|
2011-06-29 19:33:04 +08:00
|
|
|
if (obex == NULL || pkt == NULL) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR, G_OBEX_ERROR_INVALID_ARGS,
|
|
|
|
"Invalid arguments");
|
2011-06-28 22:54:25 +08:00
|
|
|
return FALSE;
|
2011-06-29 19:33:04 +08:00
|
|
|
}
|
2011-06-28 22:54:25 +08:00
|
|
|
|
2011-07-05 23:57:23 +08:00
|
|
|
if (obex->rx_last_op == G_OBEX_OP_CONNECT)
|
|
|
|
prepare_connect_rsp(obex, pkt);
|
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
p = g_new0(struct pending_pkt, 1);
|
|
|
|
p->pkt = pkt;
|
|
|
|
|
2011-06-29 19:33:04 +08:00
|
|
|
ret = g_obex_send_internal(obex, p, err);
|
|
|
|
if (ret == FALSE)
|
|
|
|
pending_pkt_free(p);
|
|
|
|
|
|
|
|
return ret;
|
2011-06-28 22:54:25 +08:00
|
|
|
}
|
|
|
|
|
2011-06-29 23:30:18 +08:00
|
|
|
guint g_obex_send_req(GObex *obex, GObexPacket *req, gint timeout,
|
|
|
|
GObexResponseFunc func, gpointer user_data,
|
|
|
|
GError **err)
|
2011-06-27 21:17:04 +08:00
|
|
|
{
|
2011-07-05 04:02:52 +08:00
|
|
|
GObexHeader *connid;
|
2011-06-28 22:54:25 +08:00
|
|
|
struct pending_pkt *p;
|
|
|
|
static guint id = 1;
|
2011-06-27 21:17:04 +08:00
|
|
|
|
2011-07-05 04:02:52 +08:00
|
|
|
if (obex->conn_id == CONNID_INVALID)
|
|
|
|
goto create_pending;
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
connid = g_obex_packet_find_header(req, G_OBEX_HDR_CONNECTION);
|
2011-07-05 04:02:52 +08:00
|
|
|
if (connid != NULL)
|
|
|
|
goto create_pending;
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
connid = g_obex_header_new_uint32(G_OBEX_HDR_CONNECTION,
|
2011-07-05 04:02:52 +08:00
|
|
|
obex->conn_id);
|
|
|
|
g_obex_packet_prepend_header(req, connid);
|
|
|
|
|
|
|
|
create_pending:
|
2011-06-28 22:54:25 +08:00
|
|
|
p = g_new0(struct pending_pkt, 1);
|
2011-06-27 21:17:04 +08:00
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
p->pkt = req;
|
|
|
|
p->id = id++;
|
|
|
|
p->rsp_func = func;
|
|
|
|
p->rsp_data = user_data;
|
|
|
|
|
2011-06-29 23:30:18 +08:00
|
|
|
if (timeout < 0)
|
|
|
|
p->timeout = G_OBEX_DEFAULT_TIMEOUT;
|
|
|
|
else
|
|
|
|
p->timeout = timeout;
|
|
|
|
|
2011-06-29 19:33:04 +08:00
|
|
|
if (!g_obex_send_internal(obex, p, err)) {
|
|
|
|
pending_pkt_free(p);
|
2011-06-27 21:17:04 +08:00
|
|
|
return 0;
|
2011-06-29 19:33:04 +08:00
|
|
|
}
|
2011-06-27 21:17:04 +08:00
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
return p->id;
|
2011-06-27 21:17:04 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 05:26:19 +08:00
|
|
|
static gint pending_pkt_cmp(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
const struct pending_pkt *p = a;
|
2011-07-11 15:47:34 +08:00
|
|
|
guint id = GPOINTER_TO_UINT(b);
|
2011-06-30 05:26:19 +08:00
|
|
|
|
|
|
|
return (p->id - id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean pending_req_abort(GObex *obex, GError **err)
|
|
|
|
{
|
|
|
|
if (obex->pending_req->cancelled)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
obex->pending_req->cancelled = TRUE;
|
|
|
|
|
2011-07-12 04:45:49 +08:00
|
|
|
return g_obex_send_rsp(obex, G_OBEX_OP_ABORT, err, G_OBEX_HDR_INVALID);
|
2011-06-30 05:26:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean cancel_complete(gpointer user_data)
|
2011-06-27 21:17:04 +08:00
|
|
|
{
|
2011-06-30 05:26:19 +08:00
|
|
|
struct pending_pkt *p = user_data;
|
|
|
|
GObex *obex = p->obex;
|
|
|
|
GError *err;
|
|
|
|
|
|
|
|
g_assert(p->rsp_func != NULL);
|
|
|
|
|
|
|
|
err = g_error_new(G_OBEX_ERROR, G_OBEX_ERROR_CANCELLED,
|
|
|
|
"The request was cancelled");
|
|
|
|
p->rsp_func(obex, err, NULL, p->rsp_data);
|
|
|
|
|
|
|
|
g_error_free(err);
|
|
|
|
|
|
|
|
pending_pkt_free(p);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean g_obex_cancel_req(GObex *obex, guint req_id, gboolean remove_callback)
|
|
|
|
{
|
|
|
|
GList *match;
|
|
|
|
struct pending_pkt *p;
|
|
|
|
|
|
|
|
if (obex->pending_req && obex->pending_req->id == req_id) {
|
|
|
|
if (!pending_req_abort(obex, NULL)) {
|
|
|
|
p = obex->pending_req;
|
|
|
|
obex->pending_req = NULL;
|
|
|
|
goto immediate_completion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-07-11 15:47:34 +08:00
|
|
|
match = g_queue_find_custom(obex->tx_queue, GUINT_TO_POINTER(req_id),
|
2011-06-30 05:26:19 +08:00
|
|
|
pending_pkt_cmp);
|
|
|
|
if (match == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
p = match->data;
|
|
|
|
|
|
|
|
g_queue_delete_link(obex->tx_queue, match);
|
|
|
|
|
|
|
|
immediate_completion:
|
|
|
|
p->cancelled = TRUE;
|
|
|
|
p->obex = g_obex_ref(obex);
|
|
|
|
|
|
|
|
if (remove_callback || p->rsp_func == NULL)
|
|
|
|
pending_pkt_free(p);
|
|
|
|
else
|
|
|
|
g_idle_add(cancel_complete, p);
|
|
|
|
|
2011-06-27 21:17:04 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-07-12 03:28:47 +08:00
|
|
|
gboolean g_obex_send_rsp(GObex *obex, guint8 rspcode, GError **err,
|
|
|
|
guint8 first_hdr_type, ...)
|
2011-07-11 06:07:33 +08:00
|
|
|
{
|
|
|
|
GObexPacket *rsp;
|
2011-07-12 03:28:47 +08:00
|
|
|
va_list args;
|
2011-07-11 06:07:33 +08:00
|
|
|
|
2011-07-12 03:28:47 +08:00
|
|
|
va_start(args, first_hdr_type);
|
|
|
|
rsp = g_obex_packet_new_valist(rspcode, TRUE, first_hdr_type, args);
|
|
|
|
va_end(args);
|
2011-07-11 06:07:33 +08:00
|
|
|
|
|
|
|
return g_obex_send(obex, rsp, err);
|
|
|
|
}
|
|
|
|
|
2011-07-05 18:16:35 +08:00
|
|
|
void g_obex_set_disconnect_function(GObex *obex, GObexFunc func,
|
2011-06-29 17:20:42 +08:00
|
|
|
gpointer user_data)
|
|
|
|
{
|
2011-07-01 16:25:20 +08:00
|
|
|
obex->disconn_func = func;
|
|
|
|
obex->disconn_func_data = user_data;
|
2011-06-29 17:20:42 +08:00
|
|
|
}
|
|
|
|
|
2011-07-11 15:47:34 +08:00
|
|
|
static gint req_handler_cmpop(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
const struct req_handler *handler = a;
|
2011-07-18 18:35:16 +08:00
|
|
|
guint opcode = GPOINTER_TO_UINT(b);
|
2011-07-11 15:47:34 +08:00
|
|
|
|
|
|
|
return (gint) handler->opcode - (gint) opcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint req_handler_cmpid(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
const struct req_handler *handler = a;
|
|
|
|
guint id = GPOINTER_TO_UINT(b);
|
|
|
|
|
|
|
|
return (gint) handler->id - (gint) id;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint g_obex_add_request_function(GObex *obex, guint8 opcode,
|
2011-07-06 03:51:32 +08:00
|
|
|
GObexRequestFunc func,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
struct req_handler *handler;
|
2011-07-11 15:47:34 +08:00
|
|
|
static guint next_id = 1;
|
2011-07-06 03:51:32 +08:00
|
|
|
|
|
|
|
handler = g_new0(struct req_handler, 1);
|
2011-07-11 15:47:34 +08:00
|
|
|
handler->id = next_id++;
|
2011-07-06 03:51:32 +08:00
|
|
|
handler->opcode = opcode;
|
|
|
|
handler->func = func;
|
|
|
|
handler->user_data = user_data;
|
|
|
|
|
|
|
|
obex->req_handlers = g_slist_prepend(obex->req_handlers, handler);
|
|
|
|
|
2011-07-11 15:47:34 +08:00
|
|
|
return handler->id;
|
2011-07-06 03:51:32 +08:00
|
|
|
}
|
|
|
|
|
2011-07-11 15:47:34 +08:00
|
|
|
gboolean g_obex_remove_request_function(GObex *obex, guint id)
|
2011-07-06 03:51:32 +08:00
|
|
|
{
|
|
|
|
struct req_handler *handler;
|
|
|
|
GSList *match;
|
|
|
|
|
2011-07-11 15:47:34 +08:00
|
|
|
match = g_slist_find_custom(obex->req_handlers, GUINT_TO_POINTER(id),
|
|
|
|
req_handler_cmpid);
|
2011-07-06 03:51:32 +08:00
|
|
|
if (match == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
handler = match->data;
|
|
|
|
|
|
|
|
obex->req_handlers = g_slist_delete_link(obex->req_handlers, match);
|
|
|
|
g_free(handler);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:33:42 +08:00
|
|
|
void g_obex_suspend(GObex *obex)
|
|
|
|
{
|
|
|
|
if (obex->write_source > 0) {
|
|
|
|
g_source_remove(obex->write_source);
|
|
|
|
obex->write_source = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
obex->suspended = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_obex_resume(GObex *obex)
|
|
|
|
{
|
|
|
|
obex->suspended = FALSE;
|
|
|
|
|
|
|
|
if (g_queue_get_length(obex->tx_queue) > 0 || obex->tx_data > 0)
|
|
|
|
enable_tx(obex);
|
|
|
|
}
|
|
|
|
|
2011-06-28 21:54:41 +08:00
|
|
|
static void parse_connect_data(GObex *obex, GObexPacket *pkt)
|
2011-06-27 22:15:20 +08:00
|
|
|
{
|
|
|
|
const struct connect_data *data;
|
2011-07-05 04:02:52 +08:00
|
|
|
GObexHeader *connid;
|
2011-06-27 22:15:20 +08:00
|
|
|
guint16 u16;
|
|
|
|
size_t data_len;
|
|
|
|
|
2011-06-28 21:54:41 +08:00
|
|
|
data = g_obex_packet_get_data(pkt, &data_len);
|
2011-06-27 22:15:20 +08:00
|
|
|
if (data == NULL || data_len != sizeof(*data))
|
2011-06-28 21:54:41 +08:00
|
|
|
return;
|
2011-06-27 22:15:20 +08:00
|
|
|
|
|
|
|
memcpy(&u16, &data->mtu, sizeof(u16));
|
|
|
|
|
|
|
|
obex->tx_mtu = g_ntohs(u16);
|
2011-07-01 17:24:33 +08:00
|
|
|
if (obex->io_tx_mtu > 0 && obex->tx_mtu > obex->io_tx_mtu)
|
|
|
|
obex->tx_mtu = obex->io_tx_mtu;
|
2011-06-27 22:15:20 +08:00
|
|
|
obex->tx_buf = g_realloc(obex->tx_buf, obex->tx_mtu);
|
2011-07-05 04:02:52 +08:00
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
connid = g_obex_packet_find_header(pkt, G_OBEX_HDR_CONNECTION);
|
2011-07-05 04:02:52 +08:00
|
|
|
if (connid != NULL)
|
|
|
|
g_obex_header_get_uint32(connid, &obex->conn_id);
|
2011-06-28 21:54:41 +08:00
|
|
|
}
|
2011-06-27 22:15:20 +08:00
|
|
|
|
2011-06-29 18:15:05 +08:00
|
|
|
static void handle_response(GObex *obex, GError *err, GObexPacket *rsp)
|
2011-06-28 21:54:41 +08:00
|
|
|
{
|
2011-06-28 22:54:25 +08:00
|
|
|
struct pending_pkt *p = obex->pending_req;
|
2011-07-03 18:06:36 +08:00
|
|
|
gboolean disconn = err ? TRUE : FALSE, final_rsp = TRUE;
|
2011-06-28 21:54:41 +08:00
|
|
|
|
2011-06-29 18:15:05 +08:00
|
|
|
if (rsp != NULL) {
|
2011-07-03 18:06:36 +08:00
|
|
|
guint8 opcode;
|
|
|
|
|
|
|
|
g_obex_packet_get_operation(rsp, &final_rsp);
|
|
|
|
|
2011-07-04 20:55:12 +08:00
|
|
|
opcode = g_obex_packet_get_operation(p->pkt, NULL);
|
2011-07-03 18:06:36 +08:00
|
|
|
if (opcode == G_OBEX_OP_CONNECT)
|
2011-06-29 18:15:05 +08:00
|
|
|
parse_connect_data(obex, rsp);
|
|
|
|
}
|
2011-06-28 21:54:41 +08:00
|
|
|
|
2011-06-30 05:26:19 +08:00
|
|
|
if (p->cancelled)
|
|
|
|
err = g_error_new(G_OBEX_ERROR, G_OBEX_ERROR_CANCELLED,
|
|
|
|
"The operation was cancelled");
|
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
if (p->rsp_func)
|
2011-06-29 18:15:05 +08:00
|
|
|
p->rsp_func(obex, err, rsp, p->rsp_data);
|
2011-06-28 21:54:41 +08:00
|
|
|
|
2011-06-30 05:26:19 +08:00
|
|
|
if (p->cancelled)
|
|
|
|
g_error_free(err);
|
|
|
|
|
2011-07-03 18:06:36 +08:00
|
|
|
if (final_rsp) {
|
|
|
|
pending_pkt_free(p);
|
|
|
|
obex->pending_req = NULL;
|
|
|
|
}
|
2011-06-29 22:30:05 +08:00
|
|
|
|
2011-07-01 16:25:20 +08:00
|
|
|
if (!disconn && g_queue_get_length(obex->tx_queue) > 0)
|
2011-06-29 22:30:05 +08:00
|
|
|
enable_tx(obex);
|
2011-06-27 22:15:20 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 16:25:20 +08:00
|
|
|
static void handle_request(GObex *obex, GObexPacket *req)
|
2011-06-27 15:23:36 +08:00
|
|
|
{
|
2011-07-06 03:51:32 +08:00
|
|
|
GSList *match;
|
2011-07-18 18:35:16 +08:00
|
|
|
guint op;
|
2011-07-06 03:51:32 +08:00
|
|
|
|
2011-07-01 16:25:20 +08:00
|
|
|
if (g_obex_packet_get_operation(req, NULL) == G_OBEX_OP_CONNECT)
|
|
|
|
parse_connect_data(obex, req);
|
2011-06-27 22:15:20 +08:00
|
|
|
|
2011-07-11 15:47:34 +08:00
|
|
|
op = g_obex_packet_get_operation(req, NULL);
|
2011-07-06 03:51:32 +08:00
|
|
|
|
2011-07-11 15:47:34 +08:00
|
|
|
match = g_slist_find_custom(obex->req_handlers, GUINT_TO_POINTER(op),
|
|
|
|
req_handler_cmpop);
|
2011-07-06 03:51:32 +08:00
|
|
|
if (match) {
|
|
|
|
struct req_handler *handler = match->data;
|
|
|
|
handler->func(obex, req, handler->user_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-12 04:45:49 +08:00
|
|
|
g_obex_send_rsp(obex, G_OBEX_RSP_NOT_IMPLEMENTED, NULL,
|
2011-07-11 02:47:49 +08:00
|
|
|
G_OBEX_HDR_INVALID);
|
2011-06-27 21:17:04 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 02:23:14 +08:00
|
|
|
static gboolean read_stream(GObex *obex, GError **err)
|
2011-06-27 15:23:36 +08:00
|
|
|
{
|
|
|
|
GIOChannel *io = obex->io;
|
|
|
|
GIOStatus status;
|
|
|
|
gsize rbytes, toread;
|
|
|
|
guint16 u16;
|
|
|
|
gchar *buf;
|
|
|
|
|
|
|
|
if (obex->rx_data >= 3)
|
|
|
|
goto read_body;
|
|
|
|
|
|
|
|
rbytes = 0;
|
|
|
|
toread = 3 - obex->rx_data;
|
|
|
|
buf = (gchar *) &obex->rx_buf[obex->rx_data];
|
|
|
|
|
|
|
|
status = g_io_channel_read_chars(io, buf, toread, &rbytes, NULL);
|
|
|
|
if (status != G_IO_STATUS_NORMAL)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
obex->rx_data += rbytes;
|
|
|
|
if (obex->rx_data < 3)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
memcpy(&u16, &buf[1], sizeof(u16));
|
|
|
|
obex->rx_pkt_len = g_ntohs(u16);
|
|
|
|
|
2011-07-05 21:37:02 +08:00
|
|
|
if (obex->rx_pkt_len > obex->rx_mtu) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR, G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Too big incoming packet");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:23:36 +08:00
|
|
|
read_body:
|
|
|
|
if (obex->rx_data >= obex->rx_pkt_len)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
do {
|
|
|
|
toread = obex->rx_pkt_len - obex->rx_data;
|
|
|
|
buf = (gchar *) &obex->rx_buf[obex->rx_data];
|
|
|
|
|
|
|
|
status = g_io_channel_read_chars(io, buf, toread, &rbytes, NULL);
|
|
|
|
if (status != G_IO_STATUS_NORMAL)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
obex->rx_data += rbytes;
|
|
|
|
} while (rbytes > 0 && obex->rx_data < obex->rx_pkt_len);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-06-30 02:23:14 +08:00
|
|
|
static gboolean read_packet(GObex *obex, GError **err)
|
2011-06-27 21:54:59 +08:00
|
|
|
{
|
2011-06-30 19:13:04 +08:00
|
|
|
GIOChannel *io = obex->io;
|
|
|
|
GError *read_err = NULL;
|
|
|
|
GIOStatus status;
|
|
|
|
gsize rbytes;
|
|
|
|
guint16 u16;
|
|
|
|
|
|
|
|
if (obex->rx_data > 0) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR, G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"RX buffer not empty before reading packet");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = g_io_channel_read_chars(io, (gchar *) obex->rx_buf,
|
|
|
|
obex->rx_mtu, &rbytes, &read_err);
|
|
|
|
if (status != G_IO_STATUS_NORMAL) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR, G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Unable to read data: %s", read_err->message);
|
|
|
|
g_error_free(read_err);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
obex->rx_data += rbytes;
|
|
|
|
|
|
|
|
if (rbytes < 3) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR, G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Incomplete packet received");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&u16, &obex->rx_buf[1], sizeof(u16));
|
|
|
|
obex->rx_pkt_len = g_ntohs(u16);
|
|
|
|
|
|
|
|
if (obex->rx_pkt_len != rbytes) {
|
|
|
|
g_set_error(err, G_OBEX_ERROR, G_OBEX_ERROR_PARSE_ERROR,
|
|
|
|
"Data size doesn't match packet size (%zu != %u)",
|
|
|
|
rbytes, obex->rx_pkt_len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2011-06-27 21:54:59 +08:00
|
|
|
}
|
|
|
|
|
2011-06-27 15:23:36 +08:00
|
|
|
static gboolean incoming_data(GIOChannel *io, GIOCondition cond,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GObex *obex = user_data;
|
|
|
|
GObexPacket *pkt;
|
2011-06-27 21:17:04 +08:00
|
|
|
ssize_t header_offset;
|
2011-06-29 18:15:05 +08:00
|
|
|
GError *err = NULL;
|
2011-06-30 19:13:04 +08:00
|
|
|
guint8 opcode;
|
2011-06-27 15:23:36 +08:00
|
|
|
|
|
|
|
if (cond & G_IO_NVAL)
|
|
|
|
return FALSE;
|
|
|
|
|
2011-06-30 02:23:14 +08:00
|
|
|
if (cond & (G_IO_HUP | G_IO_ERR)) {
|
|
|
|
err = g_error_new(G_OBEX_ERROR, G_OBEX_ERROR_DISCONNECTED,
|
|
|
|
"Transport got disconnected");
|
2011-06-27 15:23:36 +08:00
|
|
|
goto failed;
|
2011-06-30 02:23:14 +08:00
|
|
|
}
|
2011-06-27 15:23:36 +08:00
|
|
|
|
2011-06-30 02:23:14 +08:00
|
|
|
if (!obex->read(obex, &err))
|
2011-06-27 21:54:59 +08:00
|
|
|
goto failed;
|
2011-06-27 15:23:36 +08:00
|
|
|
|
|
|
|
if (obex->rx_data < 3 || obex->rx_data < obex->rx_pkt_len)
|
|
|
|
return TRUE;
|
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
if (obex->pending_req) {
|
|
|
|
struct pending_pkt *p = obex->pending_req;
|
2011-06-30 19:13:04 +08:00
|
|
|
opcode = g_obex_packet_get_operation(p->pkt, NULL);
|
2011-07-09 19:14:46 +08:00
|
|
|
header_offset = rsp_header_offset(opcode);
|
2011-06-28 22:54:25 +08:00
|
|
|
} else {
|
2011-06-30 19:13:04 +08:00
|
|
|
opcode = obex->rx_buf[0] & ~FINAL_BIT;
|
2011-07-09 21:26:46 +08:00
|
|
|
/* Unexpected response -- fail silently */
|
|
|
|
if (opcode > 0x1f && opcode < 0xff) {
|
|
|
|
obex->rx_data = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-07-09 19:14:46 +08:00
|
|
|
header_offset = req_header_offset(opcode);
|
2011-07-05 23:57:23 +08:00
|
|
|
obex->rx_last_op = opcode;
|
2011-06-27 21:17:04 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 19:13:04 +08:00
|
|
|
if (header_offset < 0) {
|
|
|
|
err = g_error_new(G_OBEX_ERROR, G_OBEX_ERROR_PARSE_ERROR,
|
2011-07-06 06:53:46 +08:00
|
|
|
"Unknown header offset for opcode 0x%02x",
|
2011-06-30 19:13:04 +08:00
|
|
|
opcode);
|
2011-06-27 21:17:04 +08:00
|
|
|
goto failed;
|
2011-06-30 19:13:04 +08:00
|
|
|
}
|
2011-06-27 21:17:04 +08:00
|
|
|
|
|
|
|
pkt = g_obex_packet_decode(obex->rx_buf, obex->rx_data, header_offset,
|
2011-06-29 18:15:05 +08:00
|
|
|
G_OBEX_DATA_REF, &err);
|
2011-07-01 16:25:20 +08:00
|
|
|
if (pkt == NULL)
|
|
|
|
goto failed;
|
2011-06-29 18:15:05 +08:00
|
|
|
|
2011-06-30 19:13:04 +08:00
|
|
|
if (obex->pending_req)
|
2011-07-01 16:25:20 +08:00
|
|
|
handle_response(obex, NULL, pkt);
|
2011-06-30 19:13:04 +08:00
|
|
|
else
|
2011-07-01 16:25:20 +08:00
|
|
|
handle_request(obex, pkt);
|
2011-06-29 18:15:05 +08:00
|
|
|
|
2011-06-29 21:01:39 +08:00
|
|
|
if (err != NULL)
|
|
|
|
g_error_free(err);
|
|
|
|
|
2011-06-29 18:15:05 +08:00
|
|
|
if (pkt != NULL)
|
2011-06-27 15:23:36 +08:00
|
|
|
g_obex_packet_free(pkt);
|
|
|
|
|
|
|
|
obex->rx_data = 0;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
g_io_channel_unref(obex->io);
|
|
|
|
obex->io = NULL;
|
|
|
|
obex->io_source = 0;
|
2011-07-05 21:37:02 +08:00
|
|
|
obex->rx_data = 0;
|
2011-06-29 17:20:42 +08:00
|
|
|
|
2011-06-30 19:13:04 +08:00
|
|
|
if (obex->pending_req)
|
|
|
|
handle_response(obex, err, NULL);
|
|
|
|
|
2011-07-01 16:25:20 +08:00
|
|
|
if (obex->disconn_func)
|
|
|
|
obex->disconn_func(obex, err, obex->disconn_func_data);
|
2011-06-30 02:23:14 +08:00
|
|
|
|
|
|
|
g_error_free(err);
|
2011-06-29 17:20:42 +08:00
|
|
|
|
2011-06-27 15:23:36 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-07-01 17:24:33 +08:00
|
|
|
GObex *g_obex_new(GIOChannel *io, GObexTransportType transport_type,
|
|
|
|
gssize io_rx_mtu, gssize io_tx_mtu)
|
2011-06-21 01:32:02 +08:00
|
|
|
{
|
|
|
|
GObex *obex;
|
2011-06-27 15:23:36 +08:00
|
|
|
GIOCondition cond;
|
2011-06-21 01:32:02 +08:00
|
|
|
|
|
|
|
if (io == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-07-01 17:24:33 +08:00
|
|
|
if (io_rx_mtu >= 0 && io_rx_mtu < G_OBEX_MINIMUM_MTU)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (io_tx_mtu >= 0 && io_tx_mtu < G_OBEX_MINIMUM_MTU)
|
|
|
|
return NULL;
|
|
|
|
|
2011-06-21 01:32:02 +08:00
|
|
|
obex = g_new0(GObex, 1);
|
|
|
|
|
2011-07-04 16:17:48 +08:00
|
|
|
obex->io = g_io_channel_ref(io);
|
2011-06-21 01:32:02 +08:00
|
|
|
obex->ref_count = 1;
|
2011-07-05 04:02:52 +08:00
|
|
|
obex->conn_id = CONNID_INVALID;
|
2011-07-05 23:57:23 +08:00
|
|
|
obex->rx_last_op = G_OBEX_OP_NONE;
|
2011-07-01 17:24:33 +08:00
|
|
|
|
|
|
|
obex->io_rx_mtu = io_rx_mtu;
|
|
|
|
obex->io_tx_mtu = io_tx_mtu;
|
|
|
|
|
|
|
|
if (io_rx_mtu > G_OBEX_MAXIMUM_MTU)
|
|
|
|
obex->rx_mtu = G_OBEX_MAXIMUM_MTU;
|
2011-07-04 21:20:42 +08:00
|
|
|
else if (io_rx_mtu < G_OBEX_MINIMUM_MTU)
|
|
|
|
obex->rx_mtu = G_OBEX_DEFAULT_MTU;
|
2011-07-01 17:24:33 +08:00
|
|
|
else
|
|
|
|
obex->rx_mtu = io_rx_mtu;
|
|
|
|
|
2011-06-27 04:17:20 +08:00
|
|
|
obex->tx_mtu = G_OBEX_MINIMUM_MTU;
|
2011-07-01 17:24:33 +08:00
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
obex->tx_queue = g_queue_new();
|
2011-06-27 15:23:36 +08:00
|
|
|
obex->rx_buf = g_malloc(obex->rx_mtu);
|
2011-06-27 19:08:33 +08:00
|
|
|
obex->tx_buf = g_malloc(obex->tx_mtu);
|
2011-06-27 15:23:36 +08:00
|
|
|
|
2011-06-27 21:54:59 +08:00
|
|
|
switch (transport_type) {
|
|
|
|
case G_OBEX_TRANSPORT_STREAM:
|
|
|
|
obex->read = read_stream;
|
|
|
|
obex->write = write_stream;
|
|
|
|
break;
|
|
|
|
case G_OBEX_TRANSPORT_PACKET:
|
|
|
|
obex->read = read_packet;
|
|
|
|
obex->write = write_packet;
|
|
|
|
break;
|
2011-08-26 17:49:31 +08:00
|
|
|
default:
|
|
|
|
g_obex_unref(obex);
|
|
|
|
return NULL;
|
2011-06-27 21:54:59 +08:00
|
|
|
}
|
|
|
|
|
2011-06-27 15:23:36 +08:00
|
|
|
g_io_channel_set_encoding(io, NULL, NULL);
|
2011-06-27 19:08:33 +08:00
|
|
|
g_io_channel_set_buffered(io, FALSE);
|
2011-06-27 15:23:36 +08:00
|
|
|
cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
|
|
|
|
obex->io_source = g_io_add_watch(io, cond, incoming_data, obex);
|
2011-06-21 01:32:02 +08:00
|
|
|
|
|
|
|
return obex;
|
|
|
|
}
|
|
|
|
|
|
|
|
GObex *g_obex_ref(GObex *obex)
|
|
|
|
{
|
|
|
|
if (obex == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
g_atomic_int_inc(&obex->ref_count);
|
|
|
|
|
|
|
|
return obex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_obex_unref(GObex *obex)
|
|
|
|
{
|
|
|
|
gboolean last_ref;
|
|
|
|
|
|
|
|
last_ref = g_atomic_int_dec_and_test(&obex->ref_count);
|
|
|
|
|
|
|
|
if (!last_ref)
|
|
|
|
return;
|
|
|
|
|
2011-07-06 03:51:32 +08:00
|
|
|
g_slist_free_full(obex->req_handlers, g_free);
|
|
|
|
|
2011-06-28 22:54:25 +08:00
|
|
|
g_queue_foreach(obex->tx_queue, (GFunc) pending_pkt_free, NULL);
|
|
|
|
g_queue_free(obex->tx_queue);
|
2011-06-21 05:29:26 +08:00
|
|
|
|
2011-06-29 17:20:42 +08:00
|
|
|
if (obex->io != NULL)
|
|
|
|
g_io_channel_unref(obex->io);
|
2011-06-21 01:32:02 +08:00
|
|
|
|
2011-06-27 15:23:36 +08:00
|
|
|
if (obex->io_source > 0)
|
|
|
|
g_source_remove(obex->io_source);
|
|
|
|
|
2011-06-30 05:25:58 +08:00
|
|
|
if (obex->write_source > 0)
|
|
|
|
g_source_remove(obex->write_source);
|
|
|
|
|
2011-06-27 15:23:36 +08:00
|
|
|
g_free(obex->rx_buf);
|
2011-06-27 19:08:33 +08:00
|
|
|
g_free(obex->tx_buf);
|
2011-06-27 15:23:36 +08:00
|
|
|
|
2011-06-27 21:17:04 +08:00
|
|
|
if (obex->pending_req)
|
2011-06-28 22:54:25 +08:00
|
|
|
pending_pkt_free(obex->pending_req);
|
2011-06-27 21:17:04 +08:00
|
|
|
|
2011-06-21 01:32:02 +08:00
|
|
|
g_free(obex);
|
|
|
|
}
|
2011-07-04 21:21:45 +08:00
|
|
|
|
|
|
|
/* Higher level functions */
|
|
|
|
|
2011-07-11 04:09:02 +08:00
|
|
|
guint g_obex_connect(GObex *obex, GObexResponseFunc func, gpointer user_data,
|
|
|
|
GError **err, guint8 first_hdr_id, ...)
|
2011-07-04 21:21:45 +08:00
|
|
|
{
|
|
|
|
GObexPacket *req;
|
|
|
|
struct connect_data data;
|
2011-07-11 04:09:02 +08:00
|
|
|
va_list args;
|
2011-07-04 21:21:45 +08:00
|
|
|
|
2011-07-11 04:09:02 +08:00
|
|
|
va_start(args, first_hdr_id);
|
|
|
|
req = g_obex_packet_new_valist(G_OBEX_OP_CONNECT, TRUE,
|
|
|
|
first_hdr_id, args);
|
|
|
|
va_end(args);
|
2011-07-04 21:21:45 +08:00
|
|
|
|
2011-07-05 03:13:28 +08:00
|
|
|
init_connect_data(obex, &data);
|
2011-07-04 21:21:45 +08:00
|
|
|
g_obex_packet_set_data(req, &data, sizeof(data), G_OBEX_DATA_COPY);
|
|
|
|
|
|
|
|
return g_obex_send_req(obex, req, -1, func, user_data, err);
|
|
|
|
}
|
2011-07-09 19:22:19 +08:00
|
|
|
|
|
|
|
guint g_obex_setpath(GObex *obex, const char *path, GObexResponseFunc func,
|
|
|
|
gpointer user_data, GError **err)
|
|
|
|
{
|
|
|
|
GObexPacket *req;
|
|
|
|
struct setpath_data data;
|
|
|
|
|
2011-07-11 02:47:49 +08:00
|
|
|
req = g_obex_packet_new(G_OBEX_OP_SETPATH, TRUE, G_OBEX_HDR_INVALID);
|
2011-07-09 19:22:19 +08:00
|
|
|
|
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
|
|
|
|
if (strcmp(path, "..") == 0)
|
|
|
|
data.flags = 0x03;
|
|
|
|
else {
|
|
|
|
GObexHeader *hdr;
|
|
|
|
data.flags = 0x02;
|
2011-07-11 02:47:49 +08:00
|
|
|
hdr = g_obex_header_new_unicode(G_OBEX_HDR_NAME, path);
|
2011-07-09 19:22:19 +08:00
|
|
|
g_obex_packet_add_header(req, hdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_obex_packet_set_data(req, &data, sizeof(data), G_OBEX_DATA_COPY);
|
|
|
|
|
|
|
|
return g_obex_send_req(obex, req, -1, func, user_data, err);
|
|
|
|
}
|
2011-07-09 19:29:22 +08:00
|
|
|
|
|
|
|
guint g_obex_mkdir(GObex *obex, const char *path, GObexResponseFunc func,
|
|
|
|
gpointer user_data, GError **err)
|
|
|
|
{
|
|
|
|
GObexPacket *req;
|
|
|
|
struct setpath_data data;
|
|
|
|
|
2011-07-11 06:13:46 +08:00
|
|
|
req = g_obex_packet_new(G_OBEX_OP_SETPATH, TRUE, G_OBEX_HDR_NAME, path,
|
|
|
|
G_OBEX_HDR_INVALID);
|
2011-07-09 19:29:22 +08:00
|
|
|
|
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
g_obex_packet_set_data(req, &data, sizeof(data), G_OBEX_DATA_COPY);
|
|
|
|
|
|
|
|
return g_obex_send_req(obex, req, -1, func, user_data, err);
|
|
|
|
}
|
2011-07-09 19:36:05 +08:00
|
|
|
|
|
|
|
guint g_obex_delete(GObex *obex, const char *name, GObexResponseFunc func,
|
|
|
|
gpointer user_data, GError **err)
|
|
|
|
{
|
|
|
|
GObexPacket *req;
|
|
|
|
|
2011-07-11 06:13:46 +08:00
|
|
|
req = g_obex_packet_new(G_OBEX_OP_PUT, TRUE, G_OBEX_HDR_NAME, name,
|
|
|
|
G_OBEX_HDR_INVALID);
|
2011-07-09 19:36:05 +08:00
|
|
|
|
|
|
|
return g_obex_send_req(obex, req, -1, func, user_data, err);
|
|
|
|
}
|
2011-08-30 17:30:39 +08:00
|
|
|
|
|
|
|
guint g_obex_copy(GObex *obex, const char *name, const char *dest,
|
|
|
|
GObexResponseFunc func, gpointer user_data,
|
|
|
|
GError **err)
|
|
|
|
{
|
|
|
|
GObexPacket *req;
|
|
|
|
|
2011-08-30 17:57:29 +08:00
|
|
|
req = g_obex_packet_new(G_OBEX_OP_ACTION, TRUE,
|
2011-08-30 17:30:39 +08:00
|
|
|
G_OBEX_HDR_ACTION, G_OBEX_ACTION_COPY,
|
|
|
|
G_OBEX_HDR_NAME, name,
|
|
|
|
G_OBEX_HDR_DESTNAME, dest,
|
|
|
|
G_OBEX_HDR_INVALID);
|
|
|
|
|
|
|
|
return g_obex_send_req(obex, req, -1, func, user_data, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
guint g_obex_move(GObex *obex, const char *name, const char *dest,
|
|
|
|
GObexResponseFunc func, gpointer user_data,
|
|
|
|
GError **err)
|
|
|
|
{
|
|
|
|
GObexPacket *req;
|
|
|
|
|
2011-08-30 17:57:29 +08:00
|
|
|
req = g_obex_packet_new(G_OBEX_OP_ACTION, TRUE,
|
2011-08-30 17:30:39 +08:00
|
|
|
G_OBEX_HDR_ACTION, G_OBEX_ACTION_MOVE,
|
|
|
|
G_OBEX_HDR_NAME, name,
|
|
|
|
G_OBEX_HDR_DESTNAME, dest,
|
|
|
|
G_OBEX_HDR_INVALID);
|
|
|
|
|
|
|
|
return g_obex_send_req(obex, req, -1, func, user_data, err);
|
|
|
|
}
|