CAPI/OPP: Implement function bt_server_accept/reject() 27/23027/1
authorWu Jiangbo <jiangbox.wu@intel.com>
Fri, 13 Jun 2014 07:32:32 +0000 (15:32 +0800)
committerWu Jiangbo <jiangbox.wu@intel.com>
Mon, 16 Jun 2014 09:15:01 +0000 (17:15 +0800)
Change-Id: I47d68b9d47bfa945b0f4bfb395287f5b7f0ce032
Signed-off-by: Wu Jiangbo <jiangbox.wu@intel.com>
capi/bluetooth-obex.c
include/bluetooth.h
test/bluez-capi-test.c

index 290e959..60825b4 100644 (file)
@@ -32,6 +32,8 @@
 #define OBEX_LIB_SERVICE "org.obex.lib"
 #define AGENT_OBJECT_PATH "/org/obex/lib"
 
+#define OBEX_ERROR_INTERFACE "org.bluez.obex.Error"
+
 static struct {
        char *root_folder;
        char *pending_name;
@@ -338,7 +340,7 @@ void bt_opp_clear_transfers_state_cb(void)
        watch_node = NULL;
 }
 
-int bt_opp_server_accept(const char *name, bt_opp_transfer_state_cb cb,
+int bt_opp_server_accept_request(const char *name, bt_opp_transfer_state_cb cb,
                                        void *user_data, int *transfer_id)
 {
        obex_transfer_t *transfer;
@@ -377,6 +379,8 @@ int bt_opp_server_accept(const char *name, bt_opp_transfer_state_cb cb,
        g_dbus_method_invocation_return_value(invocation,
                                        g_variant_new("(s)", file_name));
 
+       opp_server.pending_invocation = NULL;
+
        g_free(file_name);
 
        *transfer_id = obex_transfer_get_id(transfer);
@@ -386,8 +390,17 @@ int bt_opp_server_accept(const char *name, bt_opp_transfer_state_cb cb,
        return BT_SUCCESS;
 }
 
-int bt_opp_server_reject(void)
+int bt_opp_server_reject_request(void)
 {
+       if (opp_server.pending_invocation) {
+               g_dbus_method_invocation_return_dbus_error(
+                                       opp_server.pending_invocation,
+                                       OBEX_ERROR_INTERFACE ".Rejected",
+                                       "RejectByUser");
+
+               opp_server.pending_invocation = NULL;
+       }
+
        return 0;
 }
 
@@ -466,6 +479,8 @@ struct opp_server_push_cb_node {
 };
 
 struct opp_server_push_cb_node *opp_server_push_node;
+static bt_opp_server_transfer_progress_cb bt_transfer_progress_cb;
+static bt_opp_server_transfer_finished_cb bt_transfer_finished_cb;
 
 void server_push_requested_cb(const char *remote_address, const char *name,
                                        uint64_t size, void *user_data)
@@ -524,3 +539,36 @@ int bt_opp_server_deinitialize(void)
 
        return bt_opp_unregister_server();
 }
+
+static void bt_opp_server_transfer_state_cb(int transfer_id,
+                       bt_opp_transfer_state_e state, const char *name,
+                       uint64_t size, unsigned char percent, void *user_data)
+{
+       if (transfer_id < 10000)
+               return;
+
+       if (state == BT_OPP_TRANSFER_QUEUED ||
+                       state == BT_OPP_TRANSFER_ACTIVE)
+               bt_transfer_progress_cb(name, size, percent, user_data);
+       else if (state == BT_OPP_TRANSFER_COMPLETED)
+               bt_transfer_finished_cb(BT_ERROR_NONE, name, size, user_data);
+       else if (state == BT_OPP_TRANSFER_ERROR || BT_OPP_TRANSFER_CANCELED)
+               bt_transfer_finished_cb(BT_ERROR_CANCELLED, name, size, user_data);
+
+}
+
+int bt_opp_server_accept(bt_opp_server_transfer_progress_cb progress_cb,
+                       bt_opp_server_transfer_finished_cb finished_cb,
+                       const char *name, void *user_data, int *transfer_id)
+{
+       bt_transfer_progress_cb = progress_cb;
+       bt_transfer_finished_cb = finished_cb;
+
+       return bt_opp_server_accept_request(name, bt_opp_server_transfer_state_cb,
+                                                       user_data, transfer_id);
+}
+
+int bt_opp_server_reject(void)
+{
+       return bt_opp_server_reject_request();
+}
index 78f6309..cb936e7 100644 (file)
@@ -2132,6 +2132,65 @@ int bt_opp_server_initialize_by_connection_request(const char *destination, bt_o
  */
 int bt_opp_server_deinitialize(void);
 
+/**
+ * @ingroup CAPI_NETWORK_BLUETOOTH_OPP_SERVER_MODULE
+ * @brief  Called when a file is being transfered.
+ * @param[in] file  The path of file to be pushed
+ * @param[in] size The file size (bytes)
+ * @param[in] percent The progress in percentage (1 ~ 100)
+ * @param[in] user_data The user data passed from the callback registration function
+ * @see bt_opp_server_accept()
+ * @see bt_opp_server_accept_connection()
+ */
+typedef void (*bt_opp_server_transfer_progress_cb) (const char *file, long long size, int percent, void *user_data);
+
+/**
+ * @ingroup CAPI_NETWORK_BLUETOOTH_OPP_SERVER_MODULE
+ * @brief  Called when a transfer is finished.
+ * @param[in] error_code  The result of push
+ * @param[in] file  The path of file to be pushed
+ * @param[in] size The file size (bytes)
+ * @param[in] user_data The user data passed from the callback registration function
+ * @see bt_opp_server_accept()
+ * @see bt_opp_server_accept_connection()
+ */
+typedef void (*bt_opp_server_transfer_finished_cb) (int result, const char *file, long long size, void *user_data);
+
+/**
+ * @ingroup CAPI_NETWORK_BLUETOOTH_OPP_SERVER_MODULE
+ * @brief Accepts the push request from the remote device.
+ * @remarks If you initialize OPP server by bt_opp_server_initialize_by_connection_request(), then name is ignored.
+ * You can cancel the pushes by bt_opp_server_cancel_transfer() with transfer_id.
+ * @param[in] progress_cb  The callback called when a file is being transfered
+ * @param[in] finished_cb  The callback called when a transfer is finished
+ * @param[in] name  The name to store. This can be NULL if you initialize OPP server by bt_opp_server_initialize_by_connection_request().
+ * @param[in] user_data The user data to be passed to the callback function
+ * @param[out]  transfer_id  The ID of transfer
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #BT_ERROR_NONE  Successful
+ * @retval #BT_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #BT_ERROR_NOT_INITIALIZED  Not initialized
+ * @retval #BT_ERROR_NOT_ENABLED  Not enabled
+ * @retval #BT_ERROR_OPERATION_FAILED  Operation failed
+ * @retval #BT_ERROR_NOW_IN_PROGRESS  Operation now in progress
+ * @see  bt_opp_server_reject()
+ */
+int bt_opp_server_accept(bt_opp_server_transfer_progress_cb progress_cb, bt_opp_server_transfer_finished_cb finished_cb, const char *name,
+ void *user_data, int *transfer_id);
+
+/**
+ * @ingroup CAPI_NETWORK_BLUETOOTH_OPP_SERVER_MODULE
+ * @brief Rejects the push request from the remote device.
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #BT_ERROR_NONE  Successful
+ * @retval #BT_ERROR_NOT_INITIALIZED  Not initialized
+ * @retval #BT_ERROR_NOT_ENABLED  Not enabled
+ * @retval #BT_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #BT_ERROR_OPERATION_FAILED  Operation failed
+ * @see  bt_opp_server_accept()
+ */
+int bt_opp_server_reject(void);
+
 /* New OPP API*/
 int bt_opp_init(void);
 
@@ -2168,13 +2227,13 @@ typedef void (*bt_opp_transfer_state_cb)(
                        unsigned char percent,
                        void *user_data);
 
-int bt_opp_server_accept(
+int bt_opp_server_accept_request(
                        const char *name,
                        bt_opp_transfer_state_cb cb,
                        void *user_data,
                        int *transfer_id);
 
-int bt_opp_server_reject(void);
+int bt_opp_server_reject_request(void);
 
 int bt_opp_server_set_directroy(const char *dir);
 
index 983ecb4..5d91dcb 100644 (file)
@@ -618,7 +618,7 @@ static void server_push_requested_cb(const char *remote_address,
        printf("\n\t%s push %s size %ju\n\t", remote_address, file, size);
        printf("\n\tAccept it...\n\t");
 
-       bt_opp_server_accept(NULL, NULL, NULL, &id);
+       bt_opp_server_accept_request(NULL, NULL, NULL, &id);
 
        printf("\n\ttransfer %d accepted\n", id);
 }