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