Sync code with Tizen 3.0 branch
[platform/core/api/nfc.git] / src / net_nfc_client_hce.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 "vconf.h"
18
19 #include "net_nfc_typedef_internal.h"
20 #include "net_nfc_debug_internal.h"
21 #include "net_nfc_util_internal.h"
22 #include "net_nfc_util_ndef_message.h"
23 #include "net_nfc_util_gdbus_internal.h"
24 #include "net_nfc_gdbus.h"
25 #include "net_nfc_client.h"
26 #include "net_nfc_client_util_internal.h"
27 #include "net_nfc_client_manager.h"
28 #include "net_nfc_client_hce_ipc.h"
29 #include "net_nfc_client_hce.h"
30
31 /* LCOV_EXCL_START */
32
33
34 #ifndef NET_NFC_EXPORT_API
35 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
36 #endif
37
38 typedef struct _HceHandler HceHandler;
39
40 struct _HceHandler {
41         net_nfc_client_hce_event_cb hce_event_cb;
42         gpointer hce_data;
43 };
44
45 static NetNfcGDbusHce *hce_proxy = NULL;
46
47 static HceHandler hce_handler;
48 static char package_name[1024];
49
50 static void __load_package_name()
51 {
52         if (net_nfc_util_get_pkgid_by_pid(getpid(),
53                 package_name, sizeof(package_name)) == false) {
54                 DEBUG_ERR_MSG("failed to get package name, pid [%d]", getpid());
55         }
56 }
57
58 static void hce_event_received(GObject *source_object, guint arg_handle,
59         guint arg_event, GVariant *arg_apdu, gchar *arg_package)
60 {
61         INFO_MSG(">>> SIGNAL arrived");
62
63         if (hce_handler.hce_event_cb != NULL) {
64
65                 data_s apdu = { NULL, 0 };
66
67                 net_nfc_util_gdbus_variant_to_data_s(arg_apdu, &apdu);
68
69                 hce_handler.hce_event_cb((net_nfc_target_handle_h)GUINT_TO_POINTER(arg_handle),
70                         (net_nfc_hce_event_t)arg_event, &apdu,
71                         hce_handler.hce_data);
72
73                 net_nfc_util_clear_data(&apdu);
74         }
75 }
76
77 void net_nfc_client_hce_process_received_event(int event,
78         net_nfc_target_handle_h handle, data_h data)
79 {
80         INFO_MSG(">>> SIGNAL arrived");
81
82         if (hce_handler.hce_event_cb != NULL) {
83                 hce_handler.hce_event_cb(handle,
84                         (net_nfc_hce_event_t)event, data,
85                         hce_handler.hce_data);
86         }
87 }
88
89
90
91 NET_NFC_EXPORT_API
92 net_nfc_error_e net_nfc_client_hce_set_event_received_cb(
93         net_nfc_client_hce_event_cb callback, void *user_data)
94 {
95         net_nfc_error_e result = NET_NFC_OK;
96         GError *error = NULL;
97
98         if (callback == NULL)
99                 return net_nfc_client_hce_unset_event_received_cb();
100
101         if (hce_proxy == NULL) {
102                 result = net_nfc_client_hce_init();
103                 if (result != NET_NFC_OK) {
104                         DEBUG_ERR_MSG("net_nfc_client_hce_init failed, [%d]", result);
105
106                         return result;
107                 }
108         }
109
110         if (net_nfc_gdbus_hce_call_start_hce_handler_sync(hce_proxy,
111                 &result, NULL, &error) == true) {
112                 hce_handler.hce_event_cb = callback;
113                 hce_handler.hce_data = user_data;
114
115                 if (net_nfc_client_hce_ipc_is_initialized() == false) {
116                         result = net_nfc_client_hce_ipc_init();
117                         if (result != NET_NFC_OK) {
118                                 DEBUG_ERR_MSG("net_nfc_client_hce_ipc_init failed");
119
120                                 result = NET_NFC_IPC_FAIL;
121                         }
122                 }
123         } else {
124                 DEBUG_ERR_MSG("net_nfc_gdbus_hce_call_start_hce_handler_sync failed: %s", error->message);
125                 g_error_free(error);
126
127                 result = NET_NFC_IPC_FAIL;
128         }
129
130         return result;
131 }
132
133 NET_NFC_EXPORT_API
134 net_nfc_error_e net_nfc_client_hce_unset_event_received_cb(void)
135 {
136         net_nfc_error_e result = NET_NFC_OK;
137         GError *error = NULL;
138
139         if (hce_proxy == NULL) {
140                 DEBUG_ERR_MSG("not initialized!!!");
141
142                 return NET_NFC_NOT_INITIALIZED;
143         }
144
145         if (net_nfc_gdbus_hce_call_stop_hce_handler_sync(hce_proxy,
146                 &result, NULL, &error) == true) {
147                 hce_handler.hce_event_cb = NULL;
148                 hce_handler.hce_data = NULL;
149
150                 net_nfc_client_hce_ipc_deinit();
151         } else {
152                 DEBUG_ERR_MSG("net_nfc_gdbus_hce_call_stop_hce_handler_sync failed: %s", error->message);
153                 g_error_free(error);
154
155                 result = NET_NFC_IPC_FAIL;
156         }
157
158         return result;
159 }
160 #if 0
161 NET_NFC_EXPORT_API
162 net_nfc_error_e net_nfc_client_hce_response_apdu_sync(
163                                 net_nfc_target_handle_h handle,
164                                 data_h resp_apdu_data)
165 {
166         net_nfc_error_e result = NET_NFC_OK;
167         GError *error = NULL;
168         GVariant *arg_data = NULL;
169
170         INFO_MSG(">>> net_nfc_client_hce_response_apdu_sync!!");
171
172         if (hce_proxy == NULL) {
173                 result = net_nfc_client_hce_init();
174                 if (result != NET_NFC_OK) {
175                         DEBUG_ERR_MSG("net_nfc_client_hce_init failed, [%d]", result);
176
177                         return result;
178                 }
179         }
180
181         arg_data = net_nfc_util_gdbus_data_to_variant((data_s *)resp_apdu_data);
182         if (arg_data == NULL) {
183
184                 INFO_MSG(">>> resp_apdu_data is null !!");
185                 return NET_NFC_INVALID_PARAM;
186         }
187
188         if (net_nfc_gdbus_hce_call_response_apdu_sync(
189                 hce_proxy,
190                 GPOINTER_TO_UINT(handle),
191                 arg_data,
192                 &result,
193                 NULL,
194                 &error) == true) {
195         } else {
196                 DEBUG_ERR_MSG("Response APDU failed: %s", error->message);
197                 result = NET_NFC_IPC_FAIL;
198
199                 g_error_free(error);
200         }
201
202         return result;
203 }
204 #else
205 NET_NFC_EXPORT_API
206 net_nfc_error_e net_nfc_client_hce_response_apdu_sync(
207                                 net_nfc_target_handle_h handle,
208                                 data_h resp_apdu_data)
209 {
210         net_nfc_error_e result;
211
212         DEBUG_CLIENT_MSG(">>> net_nfc_client_hce_response_apdu_sync!!");
213
214         if (hce_proxy == NULL) {
215                 result = net_nfc_client_hce_init();
216                 if (result != NET_NFC_OK) {
217                         DEBUG_ERR_MSG("net_nfc_client_hce_init failed, [%d]", result);
218
219                         return result;
220                 }
221         }
222
223         if (net_nfc_client_hce_ipc_is_initialized() == false) {
224                 if (net_nfc_client_hce_ipc_init() == false) {
225                         DEBUG_ERR_MSG("net_nfc_client_hce_ipc_init failed");
226
227                         return NET_NFC_NOT_INITIALIZED;
228                 }
229         }
230
231         if (net_nfc_server_hce_ipc_send_to_server(0, handle, resp_apdu_data) == true) {
232                 result = NET_NFC_OK;
233         } else {
234                 DEBUG_ERR_MSG("net_nfc_server_hce_ipc_send_to_server failed");
235
236                 result = NET_NFC_IPC_FAIL;
237         }
238
239         return result;
240 }
241 #endif
242 net_nfc_error_e net_nfc_client_hce_init(void)
243 {
244         GError *error = NULL;
245
246         DEBUG_CLIENT_MSG("net_nfc_client_hce_init call");
247
248         if (hce_proxy) {
249                 DEBUG_CLIENT_MSG("Already initialized");
250
251                 return NET_NFC_OK;
252         }
253
254         hce_proxy = net_nfc_gdbus_hce_proxy_new_for_bus_sync(
255                                 G_BUS_TYPE_SYSTEM,
256                                 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
257                                 "org.tizen.NetNfcService",
258                                 "/org/tizen/NetNfcService/Hce",
259                                 NULL,
260                                 &error);
261
262         if (hce_proxy == NULL) {
263                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
264
265                 g_error_free(error);
266
267                 return NET_NFC_UNKNOWN_ERROR;
268         }
269
270         g_signal_connect(hce_proxy, "event-received",
271                 G_CALLBACK(hce_event_received), NULL);
272
273         /* get package name */
274         __load_package_name();
275
276         return NET_NFC_OK;
277 }
278
279 void net_nfc_client_hce_deinit(void)
280 {
281         if (hce_proxy != NULL) {
282                 net_nfc_client_hce_ipc_deinit();
283
284                 g_object_unref(hce_proxy);
285                 hce_proxy = NULL;
286         }
287 }
288
289 /* LCOV_EXCL_STOP */
290