2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
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"
34 #ifndef NET_NFC_EXPORT_API
35 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
38 typedef struct _HceHandler HceHandler;
41 net_nfc_client_hce_event_cb hce_event_cb;
45 static NetNfcGDbusHce *hce_proxy = NULL;
47 static HceHandler hce_handler;
48 static char package_name[1024];
50 static void __load_package_name()
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());
58 static void hce_event_received(GObject *source_object, guint arg_handle,
59 guint arg_event, GVariant *arg_apdu, gchar *arg_package)
61 INFO_MSG(">>> SIGNAL arrived");
63 if (hce_handler.hce_event_cb != NULL) {
65 data_s apdu = { NULL, 0 };
67 net_nfc_util_gdbus_variant_to_data_s(arg_apdu, &apdu);
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);
73 net_nfc_util_clear_data(&apdu);
77 void net_nfc_client_hce_process_received_event(int event,
78 net_nfc_target_handle_h handle, data_h data)
80 INFO_MSG(">>> SIGNAL arrived");
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);
92 net_nfc_error_e net_nfc_client_hce_set_event_received_cb(
93 net_nfc_client_hce_event_cb callback, void *user_data)
95 net_nfc_error_e result = NET_NFC_OK;
99 return net_nfc_client_hce_unset_event_received_cb();
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);
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;
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");
120 result = NET_NFC_IPC_FAIL;
125 DEBUG_ERR_MSG("net_nfc_gdbus_hce_call_start_hce_handler_sync failed: %s", error->message);
129 result = NET_NFC_IPC_FAIL;
136 net_nfc_error_e net_nfc_client_hce_unset_event_received_cb(void)
138 net_nfc_error_e result = NET_NFC_OK;
139 GError *error = NULL;
141 if (hce_proxy == NULL) {
142 DEBUG_ERR_MSG("not initialized!!!");
144 return NET_NFC_NOT_INITIALIZED;
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;
152 net_nfc_client_hce_ipc_deinit();
155 DEBUG_ERR_MSG("net_nfc_gdbus_hce_call_stop_hce_handler_sync failed: %s", error->message);
159 result = NET_NFC_IPC_FAIL;
166 net_nfc_error_e net_nfc_client_hce_response_apdu_sync(
167 net_nfc_target_handle_h handle,
168 data_h resp_apdu_data)
170 net_nfc_error_e result = NET_NFC_OK;
171 GError *error = NULL;
172 GVariant *arg_data = NULL;
174 INFO_MSG(">>> net_nfc_client_hce_response_apdu_sync!!");
176 if (hce_proxy == NULL) {
177 result = net_nfc_client_hce_init();
178 if (result != NET_NFC_OK) {
179 DEBUG_ERR_MSG("net_nfc_client_hce_init failed, [%d]", result);
185 arg_data = net_nfc_util_gdbus_data_to_variant((data_s *)resp_apdu_data);
186 if (arg_data == NULL) {
188 INFO_MSG(">>> resp_apdu_data is null !!");
189 return NET_NFC_INVALID_PARAM;
192 if (net_nfc_gdbus_hce_call_response_apdu_sync(
194 GPOINTER_TO_UINT(handle),
200 DEBUG_ERR_MSG("Response APDU failed: %s", error->message);
201 result = NET_NFC_IPC_FAIL;
210 net_nfc_error_e net_nfc_client_hce_response_apdu_sync(
211 net_nfc_target_handle_h handle,
212 data_h resp_apdu_data)
214 net_nfc_error_e result;
216 DEBUG_CLIENT_MSG(">>> net_nfc_client_hce_response_apdu_sync!!");
218 if (hce_proxy == NULL) {
219 result = net_nfc_client_hce_init();
220 if (result != NET_NFC_OK) {
221 DEBUG_ERR_MSG("net_nfc_client_hce_init failed, [%d]", result);
227 if (net_nfc_client_hce_ipc_is_initialized() == false) {
228 if (net_nfc_client_hce_ipc_init() == false) {
229 DEBUG_ERR_MSG("net_nfc_client_hce_ipc_init failed");
231 return NET_NFC_NOT_INITIALIZED;
235 if (net_nfc_server_hce_ipc_send_to_server(0, handle, resp_apdu_data) == true) {
238 DEBUG_ERR_MSG("net_nfc_server_hce_ipc_send_to_server failed");
240 result = NET_NFC_IPC_FAIL;
246 net_nfc_error_e net_nfc_client_hce_init(void)
248 GError *error = NULL;
250 DEBUG_CLIENT_MSG("net_nfc_client_hce_init call");
253 DEBUG_CLIENT_MSG("Already initialized");
258 hce_proxy = net_nfc_gdbus_hce_proxy_new_for_bus_sync(
260 G_DBUS_PROXY_FLAGS_NONE,
261 "org.tizen.NetNfcService",
262 "/org/tizen/NetNfcService/Hce",
266 if (hce_proxy == NULL) {
268 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
272 return NET_NFC_UNKNOWN_ERROR;
275 g_signal_connect(hce_proxy, "event-received",
276 G_CALLBACK(hce_event_received), NULL);
278 /* get package name */
279 __load_package_name();
284 void net_nfc_client_hce_deinit(void)
286 if (hce_proxy != NULL) {
287 net_nfc_client_hce_ipc_deinit();
289 g_object_unref(hce_proxy);