b13a8dc
From a57817c7e52ede1589ffbd7e2abb444734e2df92 Mon Sep 17 00:00:00 2001
b13a8dc
From: Tomas Hozza <thozza@redhat.com>
b13a8dc
Date: Mon, 9 Sep 2013 18:12:00 +0200
b13a8dc
Subject: [PATCH 1/2] Add sample application obex_push
b13a8dc
b13a8dc
obex_push is a sample application capable of pushing
b13a8dc
and receiving files over the Bluetooth OBEX Push Channel.
b13a8dc
b13a8dc
Signed-off-by: Tomas Hozza <thozza@redhat.com>
b13a8dc
---
b13a8dc
 apps/CMakeLists.txt      |   9 +++
b13a8dc
 apps/obex_push.c         | 177 +++++++++++++++++++++++++++++++++++++++++++++++
b13a8dc
 doc/obex_push.xml        |  78 +++++++++++++++++++++
b13a8dc
 doc/openobex-apps.xml    |   1 +
b13a8dc
 doc/referenceinfo.xml.in |   6 ++
b13a8dc
 5 files changed, 271 insertions(+)
b13a8dc
 create mode 100644 apps/obex_push.c
b13a8dc
 create mode 100644 doc/obex_push.xml
b13a8dc
b13a8dc
diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt
b13a8dc
index 55268ca..6aefb23 100644
b13a8dc
--- a/apps/CMakeLists.txt
b13a8dc
+++ b/apps/CMakeLists.txt
b13a8dc
@@ -4,6 +4,10 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/lib )
b13a8dc
 add_subdirectory ( obex_test )
b13a8dc
 add_subdirectory ( ircp )
b13a8dc
 
b13a8dc
+set ( OPENOBEX_BLUETOOTH_APPS
b13a8dc
+  obex_push
b13a8dc
+)
b13a8dc
+
b13a8dc
 set ( OPENOBEX_COMMON_APPS
b13a8dc
   irxfer
b13a8dc
   irobex_palm3
b13a8dc
@@ -18,6 +22,11 @@ if ( NOT CMAKE_SYSTEM_NAME STREQUAL "Windows" )
b13a8dc
   list ( APPEND OPENOBEX_COMMON_APPS obex_tcp )
b13a8dc
 endif ( NOT CMAKE_SYSTEM_NAME STREQUAL "Windows" )
b13a8dc
 
b13a8dc
+foreach ( prog ${OPENOBEX_BLUETOOTH_APPS} )
b13a8dc
+  list ( APPEND ${prog}_LIBS bluetooth )
b13a8dc
+  list ( APPEND OPENOBEX_COMMON_APPS ${prog} )
b13a8dc
+endforeach ( prog )
b13a8dc
+
b13a8dc
 foreach ( prog ${OPENOBEX_COMMON_APPS} )
b13a8dc
   list ( APPEND ${prog}_LIBS openobex-apps-common )
b13a8dc
   list ( APPEND OPENOBEX_APPS ${prog} )
b13a8dc
diff --git a/apps/obex_push.c b/apps/obex_push.c
b13a8dc
new file mode 100644
b13a8dc
index 0000000..c5b7d2e
b13a8dc
--- /dev/null
b13a8dc
+++ b/apps/obex_push.c
a958c0e
@@ -0,0 +1,177 @@
a958c0e
+/*********************************************************************
a958c0e
+ *                
a958c0e
+ * Filename:      obex_push.c
a958c0e
+ * Version:       0.1
a958c0e
+ * Description:   Demonstrates use of PUSH command
a958c0e
+ * Status:        Experimental.
a958c0e
+ * Author:        Harald Hoyer 
a958c0e
+ *
a958c0e
+ *     modified irobex_palm3.c
a958c0e
+ * 
0eb59d3
+ *     Copyright (C) 2003-2007 Harald Hoyer, All Rights Reserved.
0eb59d3
+ *     Copyright (C) 2003-2007 Red Hat, Inc.
a958c0e
+ *     
a958c0e
+ *     This program is free software; you can redistribute it and/or 
a958c0e
+ *     modify it under the terms of the GNU General Public License as 
a958c0e
+ *     published by the Free Software Foundation; either version 2 of 
a958c0e
+ *     the License, or (at your option) any later version.
a958c0e
+ * 
a958c0e
+ *     This program is distributed in the hope that it will be useful,
a958c0e
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
a958c0e
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a958c0e
+ *     GNU General Public License for more details.
a958c0e
+ * 
a958c0e
+ *     You should have received a copy of the GNU General Public License 
a958c0e
+ *     along with this program; if not, write to the Free Software 
a958c0e
+ *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
a958c0e
+ *     MA 02111-1307 USA
a958c0e
+ *
a958c0e
+ *
a958c0e
+ *     Start without arguments to receive a file.
a958c0e
+ *     Start with address and filename as argument to send a file. 
a958c0e
+ *     
a958c0e
+ ********************************************************************/
a958c0e
+
a958c0e
+#include <stdio.h>
a958c0e
+#include <stdlib.h>
a958c0e
+
a958c0e
+#ifndef _WIN32
a958c0e
+#include <unistd.h>
a958c0e
+#endif
a958c0e
+
a958c0e
+#ifdef HAVE_CONFIG_H
a958c0e
+#include <config.h>
a958c0e
+#endif
a958c0e
+
a958c0e
+#if _WIN32
a958c0e
+#include <winsock.h>
a958c0e
+#else
a958c0e
+#include <sys/socket.h>
a958c0e
+#include <arpa/inet.h>
a958c0e
+#include <netdb.h>
a958c0e
+#include <netinet/in.h>
a958c0e
+#endif /* _WIN32 */
a958c0e
+
a958c0e
+#include <libgen.h>
a958c0e
+
a958c0e
+#include <bluetooth/bluetooth.h>
a958c0e
+#include <bluetooth/rfcomm.h>
a958c0e
+
a958c0e
+#include <openobex/obex.h>
b13a8dc
+
a958c0e
+#include "obex_put_common.h"
b13a8dc
+#include "ircp/ircp_io.h"
a958c0e
+
a958c0e
+#define OBEX_PUSH_HANDLE	10
a958c0e
+
a958c0e
+#define TRUE  1
a958c0e
+#define FALSE 0
a958c0e
+
a958c0e
+obex_t *handle = NULL;
a958c0e
+volatile int finished = FALSE;
a958c0e
+extern int last_rsp;
a958c0e
+
a958c0e
+/*
a958c0e
+ * Function main (argc, )
a958c0e
+ *
a958c0e
+ *    Starts all the fun!
a958c0e
+ *
a958c0e
+ */
a958c0e
+int main(int argc, char *argv[])
a958c0e
+{
a958c0e
+	obex_object_t *object;
a958c0e
+	int ret, exitval = EXIT_SUCCESS;
a958c0e
+	bdaddr_t bdaddr;
a958c0e
+	uint8_t channel;
a958c0e
+	char *filename;
a958c0e
+	if ((argc < 1) || (argc > 4))	{
a958c0e
+		printf ("Usage: %s [<channel>] [<bdaddr> <filename>]\n", argv[0]); 
a958c0e
+		return -1;
a958c0e
+	}
a958c0e
+	handle = OBEX_Init(OBEX_TRANS_BLUETOOTH, obex_event, 0);
a958c0e
+
a958c0e
+	switch (argc) {
a958c0e
+#ifdef HAVE_BLUETOOTH
a958c0e
+	case 4:
a958c0e
+		channel = atoi(argv[1]);
a958c0e
+		str2ba(argv[2], &bdaddr);
a958c0e
+		filename = argv[3];
a958c0e
+		break;
a958c0e
+	case 3:
a958c0e
+		str2ba(argv[1], &bdaddr);
a958c0e
+		filename = argv[2];
a958c0e
+		channel = OBEX_PUSH_HANDLE;
a958c0e
+		break;
a958c0e
+	case 2:
a958c0e
+		channel = atoi(argv[1]);
a958c0e
+		break;
a958c0e
+	case 1:
a958c0e
+		channel = OBEX_PUSH_HANDLE;
a958c0e
+		break;
a958c0e
+#endif
a958c0e
+	default:
a958c0e
+		printf("Wrong number of arguments\n");
a958c0e
+		exit(0);
a958c0e
+	}
a958c0e
+
a958c0e
+	printf("Send and receive files through bluetooth OBEX PUSH channel %d\n", channel);
a958c0e
+	if (argc <= 2)	{
a958c0e
+		char cmd[1024];
a958c0e
+		int ret;
a958c0e
+		/* We are server*/
a958c0e
+		snprintf(cmd, sizeof(cmd), "sdptool add --channel=%d OPUSH", channel);
a958c0e
+		ret = system(cmd);
a958c0e
+		if (ret != 0) {
a958c0e
+			fprintf(stderr, "Command failed: %s\n", cmd);
a958c0e
+		}
a958c0e
+		
a958c0e
+		printf("Waiting for files\n");
a958c0e
+		BtOBEX_ServerRegister(handle, NULL, channel);
a958c0e
+		
a958c0e
+		while (!finished)
a958c0e
+			OBEX_HandleInput(handle, 1);
a958c0e
+	}
a958c0e
+	else {
a958c0e
+		char *basefilename = basename(strdup(filename));
a958c0e
+		/* We are a client */
a958c0e
+		if (bacmp(&bdaddr, BDADDR_ANY) == 0) {
a958c0e
+			printf("Device address error! (Bluetooth)\n");
a958c0e
+			return -1;
a958c0e
+		}
a958c0e
+
a958c0e
+		/* Try to connect to peer */
a958c0e
+		ret = BtOBEX_TransportConnect(handle, BDADDR_ANY, &bdaddr,
a958c0e
+					      channel);
a958c0e
+		if (ret < 0) {
a958c0e
+			printf("Sorry, unable to connect!\n");
a958c0e
+			return EXIT_FAILURE;
a958c0e
+		}
a958c0e
+
a958c0e
+		object = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT);
a958c0e
+		ret = do_sync_request(handle, object, FALSE);
a958c0e
+		if ((last_rsp != OBEX_RSP_SUCCESS) || (ret < 0)) {
a958c0e
+			printf("Sorry, unable to connect!\n");
a958c0e
+			return EXIT_FAILURE;
a958c0e
+		}
a958c0e
+		if ((object = build_object_from_file(handle, filename,
a958c0e
+					basefilename)))
a958c0e
+		{
a958c0e
+			ret = do_sync_request(handle, object, FALSE);
a958c0e
+			if ((last_rsp != OBEX_RSP_SUCCESS) || (ret < 0))
a958c0e
+				exitval = EXIT_FAILURE;
a958c0e
+		} else
a958c0e
+			exitval = EXIT_FAILURE;
a958c0e
+
a958c0e
+		object = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT);
a958c0e
+		ret = do_sync_request(handle, object, FALSE);
a958c0e
+		if ((last_rsp != OBEX_RSP_SUCCESS) || (ret < 0))
a958c0e
+			exitval = EXIT_FAILURE;
a958c0e
+
a958c0e
+		if (exitval == EXIT_SUCCESS)
a958c0e
+			printf("PUT successful\n");
a958c0e
+		else
a958c0e
+			printf("PUT failed\n");
a958c0e
+	}
a958c0e
+//	sleep(1);
a958c0e
+	return exitval;
a958c0e
+}
b13a8dc
diff --git a/doc/obex_push.xml b/doc/obex_push.xml
b13a8dc
new file mode 100644
b13a8dc
index 0000000..0d3b105
b13a8dc
--- /dev/null
b13a8dc
+++ b/doc/obex_push.xml
b13a8dc
@@ -0,0 +1,78 @@
b13a8dc
+
b13a8dc
+
b13a8dc
+<reference id="openobex-apps">
b13a8dc
+  <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="referenceinfo.xml" />
a958c0e
+
b13a8dc
+  <title>openobex</title>
a958c0e
+
b13a8dc
+  <refentry id="obex_push">
b13a8dc
+    <refmeta>
b13a8dc
+      <refentrytitle>obex_push</refentrytitle>
b13a8dc
+      <manvolnum>1</manvolnum>
b13a8dc
+      <refmiscinfo class="manual">User commands</refmiscinfo>
b13a8dc
+    </refmeta>
b13a8dc
+    <refnamediv>
b13a8dc
+      <refname>obex_push</refname>
b13a8dc
+      <refpurpose>Push and Receive Files over the Bluetooth OBEX Push Channel</refpurpose>
b13a8dc
+    </refnamediv>
b13a8dc
+    <refsynopsisdiv>
b13a8dc
+      <cmdsynopsis>
b13a8dc
+        <command>obex_push</command>
b13a8dc
+        <arg><replaceable>channel</replaceable></arg>
b13a8dc
+        <arg><replaceable>bdaddr</replaceable></arg>
b13a8dc
+        <arg><replaceable>filename</replaceable></arg>
b13a8dc
+      </cmdsynopsis>
b13a8dc
+    </refsynopsisdiv>
b13a8dc
+    <refsect1>
b13a8dc
+      <title>Description</title>
b13a8dc
+      <para>
b13a8dc
+        obex_push can send and receive files over the Bluetooth OBEX Push Channel.
b13a8dc
+      </para>
b13a8dc
+      <para>
b13a8dc
+        This program is designed to be an example application for the openobex library.
b13a8dc
+      </para>
b13a8dc
+      <para>
b13a8dc
+        Link your bluetooth device with your computer.
b13a8dc
+      </para>
b13a8dc
+      <para>
b13a8dc
+        You can see the SDP services with: "sdptool browse <replaceable>bdaddr</replaceable>"
b13a8dc
+      </para>
b13a8dc
+    </refsect1>
b13a8dc
+    <refsect1>
b13a8dc
+      <title>Options</title>
b13a8dc
+      <para>
b13a8dc
+        If no options are given, any files sent over bluetooth will be received over channel 10 and stored in /tmp.
b13a8dc
+        If <replaceable>bdaddr</replaceable> and <replaceable>filename</replaceable> are given, the file will be
b13a8dc
+        send to the device specified with <replaceable>bdaddr</replaceable>.
b13a8dc
+      </para>
b13a8dc
+      <para>
b13a8dc
+        <variablelist>
b13a8dc
+          <varlistentry>
b13a8dc
+            <term><replaceable>channel</replaceable></term>
b13a8dc
+            <listitem>
b13a8dc
+              <para>
b13a8dc
+                The bluetooth channel to use. Lookup the OBEX Push channel with sdptool browse <replaceable>bdaddr</replaceable>.
b13a8dc
+              </para>
b13a8dc
+            </listitem>
b13a8dc
+          </varlistentry>
b13a8dc
+          <varlistentry>
b13a8dc
+            <term><replaceable>bdaddr</replaceable></term>
b13a8dc
+            <listitem>
b13a8dc
+              <para>
b13a8dc
+                The bluetooth address <replaceable>bdaddr</replaceable> of the device to send to.
b13a8dc
+              </para>
b13a8dc
+            </listitem>
b13a8dc
+          </varlistentry>
b13a8dc
+          <varlistentry>
b13a8dc
+            <term><replaceable>filename</replaceable></term>
b13a8dc
+            <listitem>
b13a8dc
+              <para>
b13a8dc
+                The file to send.
b13a8dc
+              </para>
b13a8dc
+            </listitem>
b13a8dc
+          </varlistentry>
b13a8dc
+        </variablelist>
b13a8dc
+      </para>
b13a8dc
+    </refsect1>
b13a8dc
+  </refentry>
b13a8dc
+</reference>
b13a8dc
diff --git a/doc/openobex-apps.xml b/doc/openobex-apps.xml
b13a8dc
index e874f6c..202c842 100644
b13a8dc
--- a/doc/openobex-apps.xml
b13a8dc
+++ b/doc/openobex-apps.xml
b13a8dc
@@ -11,4 +11,5 @@
b13a8dc
   <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="obex_find.xml" xpointer="obex_find" />
b13a8dc
   <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="obex_tcp.xml" xpointer="obex_tcp" />
b13a8dc
   <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="obex_test.xml" xpointer="obex_test" />
b13a8dc
+  <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="obex_push.xml" xpointer="obex_push" />
b13a8dc
 </reference>
b13a8dc
diff --git a/doc/referenceinfo.xml.in b/doc/referenceinfo.xml.in
b13a8dc
index 77f56c3..1dde6bd 100644
b13a8dc
--- a/doc/referenceinfo.xml.in
b13a8dc
+++ b/doc/referenceinfo.xml.in
b13a8dc
@@ -11,5 +11,11 @@
b13a8dc
       <contrib>initial version of manpage</contrib>
b13a8dc
       <email>post@hendrik-sattler.de</email>
b13a8dc
     </author>
b13a8dc
+    <author>
b13a8dc
+      <firstname>Harald</firstname>
b13a8dc
+      <surname>Hoyer</surname>
b13a8dc
+      <contrib>Initial version of obex_push manpage</contrib>
b13a8dc
+      <email>harald@redhat.com</email>
b13a8dc
+    </author>
b13a8dc
   </authorgroup>
b13a8dc
 </referenceinfo>
b13a8dc
-- 
b13a8dc
1.8.3.1
b13a8dc