Remove build error in Tizen 3.0
[platform/core/connectivity/nfc-manager-neard.git] / client / net_nfc_client_handover.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *                               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "net_nfc_typedef_internal.h"
18 #include "net_nfc_debug_internal.h"
19 #include "net_nfc_util_internal.h"
20 #include "net_nfc_util_ndef_message.h"
21 #include "net_nfc_util_gdbus_internal.h"
22 #include "net_nfc_gdbus.h"
23 #include "net_nfc_data.h"
24 #include "net_nfc_client.h"
25 #include "net_nfc_client_manager.h"
26 #include "net_nfc_client_handover.h"
27 #include "net_nfc_neard.h"
28
29
30 static NetNfcGDbusHandover *handover_proxy = NULL;
31
32 API net_nfc_error_e net_nfc_client_handover_free_alternative_carrier_data(
33                 net_nfc_connection_handover_info_s *info)
34 {
35         RETV_IF(NULL == info, NET_NFC_NULL_PARAMETER);
36
37         if (info->data.buffer != NULL)
38                 _net_nfc_util_free_mem(info->data.buffer);
39
40         _net_nfc_util_free_mem(info);
41
42         return NET_NFC_OK;
43 }
44
45
46 API net_nfc_error_e net_nfc_client_handover_get_alternative_carrier_type(
47                 net_nfc_connection_handover_info_s *info,
48                 net_nfc_conn_handover_carrier_type_e *type)
49 {
50         RETV_IF(NULL == info, NET_NFC_NULL_PARAMETER);
51         RETV_IF(NULL == type, NET_NFC_NULL_PARAMETER);
52
53         *type = info->type;
54
55         return NET_NFC_OK;
56 }
57
58 API net_nfc_error_e net_nfc_client_handover_get_alternative_carrier_data(
59                 net_nfc_connection_handover_info_s *info, data_s **data)
60 {
61         RETV_IF(NULL == info, NET_NFC_NULL_PARAMETER);
62         RETV_IF(NULL == data, NET_NFC_NULL_PARAMETER);
63
64         return net_nfc_create_data(data, info->data.buffer, info->data.length);
65 }
66
67
68 API net_nfc_error_e net_nfc_client_p2p_connection_handover(
69                 net_nfc_target_handle_s *handle,
70                 net_nfc_conn_handover_carrier_type_e arg_type,
71                 net_nfc_p2p_connection_handover_completed_cb callback,
72                 void *cb_data)
73 {
74         /* prevent executing daemon when nfc is off */
75         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
76
77         return net_nfc_neard_p2p_connection_handover(handle, arg_type, callback, cb_data);
78 }
79
80
81 API net_nfc_error_e net_nfc_client_p2p_connection_handover_sync(
82                 net_nfc_target_handle_s *handle,
83                 net_nfc_conn_handover_carrier_type_e arg_type,
84                 net_nfc_conn_handover_carrier_type_e *out_carrier,
85                 data_s **out_ac_data)
86 {
87         gboolean ret;
88         GError *error = NULL;
89         GVariant *out_data = NULL;
90         net_nfc_error_e out_result = NET_NFC_OK;
91         net_nfc_conn_handover_carrier_type_e out_type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
92
93         RETV_IF(NULL == handover_proxy, NET_NFC_NOT_INITIALIZED);
94
95         /* prevent executing daemon when nfc is off */
96         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
97
98         ret = net_nfc_gdbus_handover_call_request_sync(handover_proxy,
99                         GPOINTER_TO_UINT(handle),
100                         arg_type,
101                         net_nfc_client_gdbus_get_privilege(),
102                         (gint32 *)&out_result,
103                         (gint32 *)&out_type,
104                         &out_data,
105                         NULL,
106                         &error);
107
108         if (TRUE == ret)
109         {
110                 if (out_carrier)
111                         *out_carrier = out_type;
112
113                 if (out_ac_data)
114                         *out_ac_data = net_nfc_util_gdbus_variant_to_data(out_data);
115         }
116         else
117         {
118                 NFC_ERR("handover (sync call) failed: %s",error->message);
119                 g_error_free(error);
120
121                 out_result = NET_NFC_IPC_FAIL;
122         }
123
124         return out_result;
125 }
126
127
128 net_nfc_error_e net_nfc_client_handover_init(void)
129 {
130         GError *error = NULL;
131
132         if (handover_proxy)
133         {
134                 NFC_WARN("Already initialized");
135                 return NET_NFC_OK;
136         }
137
138         handover_proxy = net_nfc_gdbus_handover_proxy_new_for_bus_sync(
139                         G_BUS_TYPE_SYSTEM,
140                         G_DBUS_PROXY_FLAGS_NONE,
141                         "org.tizen.NetNfcService",
142                         "/org/tizen/NetNfcService/Handover",
143                         NULL,
144                         &error);
145
146         if (NULL == handover_proxy)
147         {
148                 NFC_ERR("Can not create proxy : %s", error->message);
149                 g_error_free(error);
150                 return NET_NFC_UNKNOWN_ERROR;
151         }
152
153         return NET_NFC_OK;
154 }
155
156
157 void net_nfc_client_handover_deinit(void)
158 {
159         if (handover_proxy)
160         {
161                 g_object_unref(handover_proxy);
162                 handover_proxy = NULL;
163         }
164 }