summaryrefslogtreecommitdiff
path: root/tools/obex-client-tool.c
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@intel.com>2011-07-11 00:50:15 +0300
committerMarcel Holtmann <marcel@holtmann.org>2012-12-04 22:22:01 +0100
commit14d2bde30d43c7d677f75530360231ea64f634e6 (patch)
tree8e34b6f111ebc36d57ba711081c5ed2e46679230 /tools/obex-client-tool.c
parentc869ca28b82757500bb5c86d1f58cdcd9fe5ea52 (diff)
downloadbluez-14d2bde30d43c7d677f75530360231ea64f634e6.tar.gz
gobex: Add Get support to test-client and test-server
Diffstat (limited to 'tools/obex-client-tool.c')
-rw-r--r--tools/obex-client-tool.c64
1 files changed, 54 insertions, 10 deletions
diff --git a/tools/obex-client-tool.c b/tools/obex-client-tool.c
index a621ec241..f83edfa4d 100644
--- a/tools/obex-client-tool.c
+++ b/tools/obex-client-tool.c
@@ -115,18 +115,18 @@ static void cmd_connect(int argc, char **argv)
g_obex_connect(obex, conn_complete, NULL, NULL, G_OBEX_HDR_INVALID);
}
-struct put_data {
+struct transfer_data {
int fd;
};
-static void put_complete(GObex *obex, GError *err, gpointer user_data)
+static void transfer_complete(GObex *obex, GError *err, gpointer user_data)
{
- struct put_data *data = user_data;
+ struct transfer_data *data = user_data;
if (err != NULL)
- g_printerr("put failed: %s\n", err->message);
+ g_printerr("failed: %s\n", err->message);
else
- g_print("put succeeded\n");
+ g_print("transfer succeeded\n");
close(data->fd);
g_free(data);
@@ -134,14 +134,14 @@ static void put_complete(GObex *obex, GError *err, gpointer user_data)
static gssize put_data_cb(void *buf, gsize len, gpointer user_data)
{
- struct put_data *data = user_data;
+ struct transfer_data *data = user_data;
return read(data->fd, buf, len);
}
static void cmd_put(int argc, char **argv)
{
- struct put_data *data;
+ struct transfer_data *data;
GError *err = NULL;
int fd;
@@ -152,14 +152,57 @@ static void cmd_put(int argc, char **argv)
fd = open(argv[1], O_RDONLY | O_NOCTTY, 0);
if (fd < 0) {
- g_printerr("open: %s", strerror(errno));
+ g_printerr("open: %s\n", strerror(errno));
+ return;
+ }
+
+ data = g_new0(struct transfer_data, 1);
+ data->fd = fd;
+
+ g_obex_put_req(obex, put_data_cb, transfer_complete, data, &err,
+ G_OBEX_HDR_NAME, argv[1],
+ G_OBEX_HDR_INVALID);
+ if (err != NULL) {
+ g_printerr("put failed: %s\n", err->message);
+ g_error_free(err);
+ close(data->fd);
+ g_free(data);
+ }
+}
+
+static gboolean get_data_cb(const void *buf, gsize len, gpointer user_data)
+{
+ struct transfer_data *data = user_data;
+
+ if (write(data->fd, buf, len) < 0) {
+ g_printerr("write: %s\n", strerror(errno));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static void cmd_get(int argc, char **argv)
+{
+ struct transfer_data *data;
+ GError *err = NULL;
+ int fd;
+
+ if (argc < 2) {
+ g_printerr("Filename required\n");
+ return;
+ }
+
+ fd = open(argv[1], O_WRONLY | O_CREAT | O_NOCTTY, 0);
+ if (fd < 0) {
+ g_printerr("open: %s\n", strerror(errno));
return;
}
- data = g_new0(struct put_data, 1);
+ data = g_new0(struct transfer_data, 1);
data->fd = fd;
- g_obex_put_req(obex, put_data_cb, put_complete, data, &err,
+ g_obex_get_req(obex, get_data_cb, transfer_complete, data, &err,
G_OBEX_HDR_NAME, argv[1],
G_OBEX_HDR_INVALID);
if (err != NULL) {
@@ -188,6 +231,7 @@ static struct {
{ "quit", cmd_exit, "", "Exit application" },
{ "connect", cmd_connect, "[target]", "OBEX Connect" },
{ "put", cmd_put, "<file>", "Send a file" },
+ { "get", cmd_get, "<file>", "Receive a file" },
{ NULL },
};