GIOChannel *io;
guint io_source;
+ gboolean (*read) (GObex *obex);
+ gboolean (*write) (GObex *obex);
+
guint8 *rx_buf;
size_t rx_data;
guint16 rx_pkt_len;
return req;
}
-static gboolean write_data(GIOChannel *io, GIOCondition cond,
- gpointer user_data)
+static gboolean write_stream(GObex *obex)
{
- GObex *obex = user_data;
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, NULL);
+ if (status != G_IO_STATUS_NORMAL)
+ return FALSE;
+
+ obex->tx_sent += bytes_written;
+ obex->tx_data -= bytes_written;
+
+ return TRUE;
+}
+
+static gboolean write_packet(GObex *obex)
+{
+ return FALSE;
+}
+
+static gboolean write_data(GIOChannel *io, GIOCondition cond,
+ gpointer user_data)
+{
+ GObex *obex = user_data;
+
if (cond & G_IO_NVAL)
return FALSE;
obex->tx_sent = 0;
}
- buf = (gchar *) &obex->tx_buf[obex->tx_sent];
- status = g_io_channel_write_chars(io, buf, obex->tx_data,
- &bytes_written, NULL);
- if (status != G_IO_STATUS_NORMAL)
+ if (!obex->write(obex))
goto done;
- obex->tx_sent += bytes_written;
- obex->tx_data -= bytes_written;
-
if (obex->tx_data > 0 || g_queue_get_length(obex->req_queue) > 0)
return TRUE;
return TRUE;
}
+static gboolean read_packet(GObex *obex)
+{
+ return FALSE;
+}
+
static gboolean incoming_data(GIOChannel *io, GIOCondition cond,
gpointer user_data)
{
if (cond & (G_IO_HUP | G_IO_ERR))
goto failed;
- read_stream(obex);
+ if (!obex->read(obex))
+ goto failed;
if (obex->rx_data < 3 || obex->rx_data < obex->rx_pkt_len)
return TRUE;
return FALSE;
}
-GObex *g_obex_new(GIOChannel *io)
+GObex *g_obex_new(GIOChannel *io, GObexTransportType transport_type)
{
GObex *obex;
GIOCondition cond;
obex->rx_buf = g_malloc(obex->rx_mtu);
obex->tx_buf = g_malloc(obex->tx_mtu);
+ 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;
+ }
+
g_io_channel_set_encoding(io, NULL, NULL);
g_io_channel_set_buffered(io, FALSE);
cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
G_OBEX_DATA_REF,
} GObexDataPolicy;
+typedef enum {
+ G_OBEX_TRANSPORT_STREAM,
+ G_OBEX_TRANSPORT_PACKET,
+} GObexTransportType;
+
typedef struct _GObex GObex;
typedef struct _GObexPacket GObexPacket;
typedef struct _GObexHeader GObexHeader;
void g_obex_set_request_function(GObex *obex, GObexRequestFunc func,
gpointer user_data);
-GObex *g_obex_new(GIOChannel *io);
+GObex *g_obex_new(GIOChannel *io, GObexTransportType transport_type);
GObex *g_obex_ref(GObex *obex);
void g_obex_unref(GObex *obex);
}
#define TEST_ERROR test_error_quark()
-static GObex *create_gobex(int fd, gboolean close_on_unref)
+static GObex *create_gobex(int fd, GObexTransportType transport_type,
+ gboolean close_on_unref)
{
GIOChannel *io;
g_io_channel_set_close_on_unref(io, close_on_unref);
- return g_obex_new(io);
+ return g_obex_new(io, transport_type);
}
static void dump_bytes(uint8_t *buf, size_t buf_len)
static void create_endpoints(GObex **obex, GIOChannel **io, int sock_type)
{
+ GObexTransportType transport_type;
int sv[2];
if (socketpair(AF_UNIX, sock_type | SOCK_NONBLOCK, 0, sv) < 0) {
abort();
}
- *obex = create_gobex(sv[0], TRUE);
+ if (sock_type == SOCK_STREAM)
+ transport_type = G_OBEX_TRANSPORT_STREAM;
+ else
+ transport_type = G_OBEX_TRANSPORT_PACKET;
+
+ *obex = create_gobex(sv[0], transport_type, TRUE);
g_assert(*obex != NULL);
*io = g_io_channel_unix_new(sv[1]);
{
GObex *obex;
- obex = create_gobex(STDIN_FILENO, FALSE);
+ obex = create_gobex(STDIN_FILENO, G_OBEX_TRANSPORT_STREAM, FALSE);
g_assert(obex != NULL);
{
GObex *obex;
- obex = create_gobex(STDIN_FILENO, FALSE);
+ obex = create_gobex(STDIN_FILENO, G_OBEX_TRANSPORT_STREAM, FALSE);
g_assert(obex != NULL);
{
GObex *obex;
- obex = g_obex_new(NULL);
+ obex = g_obex_new(NULL, 0);
g_assert(obex == NULL);
}