diff options
author | Johan Hedberg <johan.hedberg@intel.com> | 2011-07-04 14:09:17 +0300 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2012-12-04 22:21:58 +0100 |
commit | f817405d04a8eb6ab9f45d1cc07aeb51c01e3634 (patch) | |
tree | 35a0ffd72347280d6a0116bd224c2a9769d13fc9 /tools/obex-client-tool.c | |
parent | d0903aa40797642b10b6ce67f783c9d707b9a7aa (diff) | |
download | bluez-f817405d04a8eb6ab9f45d1cc07aeb51c01e3634.tar.gz |
gobex: Add basic skeleton for test-client
Diffstat (limited to 'tools/obex-client-tool.c')
-rw-r--r-- | tools/obex-client-tool.c | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/tools/obex-client-tool.c b/tools/obex-client-tool.c new file mode 100644 index 000000000..17b0e4c43 --- /dev/null +++ b/tools/obex-client-tool.c @@ -0,0 +1,144 @@ +/* + * + * 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 + * + */ + +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#include <gobex/gobex.h> + +static GMainLoop *main_loop = NULL; + +static gboolean option_packet = FALSE; +static gboolean option_bluetooth = FALSE; + +static void sig_term(int sig) +{ + g_print("Terminating due to signal %d\n", sig); + g_main_loop_quit(main_loop); +} + +static GOptionEntry options[] = { + { "unix", 'u', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, + &option_bluetooth, "Use a UNIX socket" }, + { "bluetooth", 'b', 0, G_OPTION_ARG_NONE, + &option_bluetooth, "Use Bluetooth" }, + { "packet", 'p', 0, G_OPTION_ARG_NONE, + &option_packet, "Packet based transport" }, + { "stream", 's', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, + &option_packet, "Stream based transport" }, + { NULL }, +}; + +static void disconn_func(GObex *obex, GError *err, gpointer user_data) +{ + g_printerr("Disconnected\n"); + g_main_loop_quit(main_loop); +} + +static GIOChannel *unix_connect(void) +{ + GIOChannel *io; + struct sockaddr_un addr = { + AF_UNIX, "\0/gobex/server" + }; + int sk, err, sock_type; + + if (option_packet) + sock_type = SOCK_SEQPACKET; + else + sock_type = SOCK_STREAM; + + sk = socket(PF_LOCAL, sock_type, 0); + if (sk < 0) { + err = errno; + g_printerr("Can't create unix socket: %s (%d)\n", + strerror(err), err); + return NULL; + } + + if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + err = errno; + g_printerr("connect: %s (%d)\n", strerror(err), err); + return NULL; + } + + io = g_io_channel_unix_new(sk); + g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL); + g_io_channel_set_close_on_unref(io, TRUE); + + g_print("Unix socket created: %d\n", sk); + + return io; +} + +int main(int argc, char *argv[]) +{ + GOptionContext *context; + GError *err = NULL; + struct sigaction sa; + GIOChannel *io; + GObexTransportType transport; + GObex *obex; + + context = g_option_context_new(NULL); + g_option_context_add_main_entries(context, options, NULL); + + g_option_context_parse(context, &argc, &argv, &err); + if (err != NULL) { + g_printerr("%s\n", err->message); + g_error_free(err); + exit(EXIT_FAILURE); + } + + io = unix_connect(); + if (io == NULL) + exit(EXIT_FAILURE); + + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = sig_term; + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + + main_loop = g_main_loop_new(NULL, FALSE); + + if (option_packet) + transport = G_OBEX_TRANSPORT_PACKET; + else + transport = G_OBEX_TRANSPORT_STREAM; + + obex = g_obex_new(io, transport, -1, -1); + g_io_channel_unref(io); + + g_obex_set_disconnect_function(obex, disconn_func, NULL); + + g_main_loop_run(main_loop); + + g_obex_unref(obex); + g_option_context_free(context); + g_main_loop_unref(main_loop); + + exit(EXIT_SUCCESS); +} |