Add P2P handover support
authorArron Wang <arron.wang@intel.com>
Mon, 17 Mar 2014 02:26:48 +0000 (10:26 +0800)
committerArron Wang <arron.wang@intel.com>
Mon, 17 Mar 2014 05:55:51 +0000 (13:55 +0800)
Change-Id: I9077e334eb65744cc1157c724bba0ed2245bfcec

client/include/net_nfc_neard.h
client/net_nfc_client_handover.c
client/net_nfc_neard.c

index 46bb933..4b00f9a 100644 (file)
@@ -4,6 +4,11 @@
 #include "net_nfc.h"
 #include "net_nfc_typedef.h"
 
+net_nfc_error_e net_nfc_neard_p2p_connection_handover(
+               net_nfc_target_handle_s *handle,
+               net_nfc_conn_handover_carrier_type_e arg_type,
+               net_nfc_p2p_connection_handover_completed_cb callback,
+               void *cb_data);
 net_nfc_error_e net_nfc_neard_send_p2p(net_nfc_target_handle_s *handle, data_s *data,
                        net_nfc_client_p2p_send_completed callback, void *user_data);
 net_nfc_error_e net_nfc_neard_get_current_tag_info(net_nfc_target_info_s **info);
index 1b2a1fb..c7b7324 100644 (file)
@@ -24,6 +24,7 @@
 #include "net_nfc_client.h"
 #include "net_nfc_client_manager.h"
 #include "net_nfc_client_handover.h"
+#include "net_nfc_neard.h"
 
 
 static NetNfcGDbusHandover *handover_proxy = NULL;
@@ -110,28 +111,10 @@ API net_nfc_error_e net_nfc_client_p2p_connection_handover(
                net_nfc_p2p_connection_handover_completed_cb callback,
                void *cb_data)
 {
-       NetNfcCallback *funcdata;
-
-       RETV_IF(NULL == handover_proxy, NET_NFC_NOT_INITIALIZED);
-
        /* prevent executing daemon when nfc is off */
        RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
 
-       funcdata = g_try_new0(NetNfcCallback, 1);
-       RETV_IF(NULL == funcdata, NET_NFC_ALLOC_FAIL);
-
-       funcdata->callback = (gpointer)callback;
-       funcdata->user_data = cb_data;
-
-       net_nfc_gdbus_handover_call_request(handover_proxy,
-                       GPOINTER_TO_UINT(handle),
-                       arg_type,
-                       net_nfc_client_gdbus_get_privilege(),
-                       NULL,
-                       p2p_connection_handover,
-                       funcdata);
-
-       return NET_NFC_OK;
+       return net_nfc_neard_p2p_connection_handover(handle, arg_type, callback, cb_data);
 }
 
 
index d04dbc2..cfad3c6 100644 (file)
@@ -46,6 +46,8 @@ typedef struct _net_nfc_client_cb
        void *p2p_send_completed_ud;
        net_nfc_client_p2p_data_received p2p_data_received_cb;
        void *p2p_data_received_ud;
+       net_nfc_p2p_connection_handover_completed_cb p2p_handover_cb;
+       void *p2p_handover_ud;
 } net_nfc_client_cb;
 
 static net_nfc_client_cb client_cb;
@@ -59,6 +61,7 @@ static neardal_dev *dev;
 static data_s *rawNDEF;
 static net_nfc_target_info_s *target_info;
 static net_nfc_target_handle_s *target_handle;
+static net_nfc_connection_handover_info_s *handover_info;
 
 static net_nfc_error_e _convert_error_code(errorCode_t error_code)
 {
@@ -125,6 +128,18 @@ static int _convert_target_type(const char *type)
        return t_type;
 }
 
+static const char *carrier2string(net_nfc_conn_handover_carrier_type_e type)
+{
+       switch (type) {
+       case NET_NFC_CONN_HANDOVER_CARRIER_BT:
+               return "bluetooth";
+       case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS:
+               return "wifi";
+       }
+
+       return NULL;
+}
+
 static uint32_t _get_tag_id(const char *name)
 {
        uint32_t id;
@@ -555,6 +570,59 @@ static void _p2p_received_cb(GVariant *ret, void *user_data)
                client_cb.p2p_data_received_cb(rawNDEF, client_cb.p2p_data_received_ud);
 }
 
+net_nfc_error_e net_nfc_neard_p2p_connection_handover(
+               net_nfc_target_handle_s *handle,
+               net_nfc_conn_handover_carrier_type_e arg_type,
+               net_nfc_p2p_connection_handover_completed_cb callback,
+               void *cb_data)
+{
+       neardal_record *record;
+       const char *carrier;
+
+       NFC_DBG("neard send p2p handover");
+
+       if (target_handle == NULL || handle != target_handle)
+               return NET_NFC_TARGET_IS_MOVED_AWAY;
+
+       carrier = carrier2string(arg_type);
+
+       if (carrier == NULL)
+               return NET_NFC_NOT_SUPPORTED;
+
+       record = g_try_malloc0(sizeof(neardal_record));
+       if (record == NULL)
+               return NET_NFC_ALLOC_FAIL;
+
+       record->name = g_strdup(dev->name);
+       record->type = g_strdup("Handover");
+       record->carrier = g_strdup(carrier);
+
+       if (neardal_dev_push(record) != NEARDAL_SUCCESS) {
+               neardal_free_record(record);
+               return NET_NFC_P2P_SEND_FAIL;
+       }
+
+       if (handover_info == NULL)
+               handover_info = g_try_malloc0(
+                       sizeof(net_nfc_connection_handover_info_s));
+
+       if (handover_info == NULL) {
+               NFC_DBG("handover_info mem alloc is failed");
+               return NET_NFC_ALLOC_FAIL;
+       }
+
+       handover_info->type = arg_type;
+       handover_info->data.buffer = NULL;
+       handover_info->data.length = 0;
+
+       neardal_free_record(record);
+
+       client_cb.p2p_handover_cb = callback;
+       client_cb.p2p_handover_ud = cb_data;
+
+       return NET_NFC_OK;
+}
+
 static void _p2p_send_completed_cb(errorCode_t error_code, void *user_data)
 {
        net_nfc_error_e result;
@@ -566,6 +634,20 @@ static void _p2p_send_completed_cb(errorCode_t error_code, void *user_data)
        if (client_cb.p2p_send_completed_cb != NULL)
                client_cb.p2p_send_completed_cb(result,
                                client_cb.p2p_send_completed_ud);
+
+       if (client_cb.p2p_handover_cb != NULL) {
+               if (handover_info != NULL) {
+                       client_cb.p2p_handover_cb(result, handover_info->type,
+                                       &(handover_info->data),
+                                       client_cb.p2p_handover_ud);
+
+                       g_free(handover_info);
+                       handover_info = NULL;
+               }
+
+               client_cb.p2p_handover_cb = NULL;
+               client_cb.p2p_handover_ud = NULL;
+       }
 }
 
 net_nfc_error_e net_nfc_neard_send_p2p(net_nfc_target_handle_s *handle, data_s *data,
@@ -946,4 +1028,9 @@ void net_nfc_neard_deinitialize(void)
                g_free(target_info);
                target_info = NULL;
        }
+
+       if (handover_info != NULL) {
+               g_free(handover_info);
+               handover_info = NULL;
+       }
 }