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.
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_gdbus_internal.h"
21 #include "net_nfc_server_controller.h"
22 #include "net_nfc_gdbus.h"
23 #include "net_nfc_server_common.h"
24 #include "net_nfc_server_tag.h"
25 #include "net_nfc_server_context.h"
26 #include "net_nfc_server_transceive.h"
29 static NetNfcGDbusTransceive *transceive_skeleton = NULL;
31 static void transceive_thread_func(gpointer user_data);
33 static void transceive_data_thread_func(gpointer user_data);
35 static gboolean transceive_handle(NetNfcGDbusTransceive *transceive,
36 GDBusMethodInvocation *invocation,
40 GVariant *smack_privilege,
43 static gboolean transceive_data_handle(NetNfcGDbusTransceive *transceive,
44 GDBusMethodInvocation *invocation,
48 GVariant *smack_privilege,
52 typedef struct _TransceiveSendData TransceiveSendData;
54 struct _TransceiveSendData
56 NetNfcGDbusTransceive *transceive;
57 GDBusMethodInvocation *invocation;
58 guint transceive_handle;
59 net_nfc_transceive_info_s transceive_info;
62 static void transceive_data_thread_func(gpointer user_data)
64 TransceiveSendData *transceive_data = user_data;
65 net_nfc_target_handle_s *handle =
66 (net_nfc_target_handle_s *)transceive_data->transceive_handle;
67 net_nfc_error_e result = NET_NFC_OK;
69 GVariant *resp_data = NULL;
71 /* use assert because it was checked in handle function */
72 g_assert(transceive_data != NULL);
73 g_assert(transceive_data->transceive != NULL);
74 g_assert(transceive_data->invocation != NULL);
76 if (net_nfc_server_target_connected(handle) == true)
78 NFC_DBG("call transceive");
80 if (net_nfc_controller_transceive(handle,
81 &transceive_data->transceive_info,
86 NFC_DBG("Transceive data received [%d]", data->length);
91 result = NET_NFC_TARGET_IS_MOVED_AWAY;
94 NFC_DBG("transceive result : %d", result);
96 resp_data = net_nfc_util_gdbus_data_to_variant(data);
98 net_nfc_gdbus_transceive_complete_transceive_data(
99 transceive_data->transceive,
100 transceive_data->invocation,
106 g_free(data->buffer);
110 net_nfc_util_free_data(&transceive_data->transceive_info.trans_data);
112 g_object_unref(transceive_data->invocation);
113 g_object_unref(transceive_data->transceive);
115 g_free(transceive_data);
118 static gboolean transceive_data_handle(NetNfcGDbusTransceive *transceive,
119 GDBusMethodInvocation *invocation,
123 GVariant *smack_privilege,
126 TransceiveSendData *data;
129 NFC_INFO(">>> REQUEST from [%s]",
130 g_dbus_method_invocation_get_sender(invocation));
132 /* check privilege and update client context */
133 if (net_nfc_server_gdbus_check_privilege(invocation,
137 NFC_ERR("permission denied, and finished request");
142 data = g_new0(TransceiveSendData, 1);
145 NFC_ERR("Memory allocation failed");
146 g_dbus_method_invocation_return_dbus_error(invocation,
147 "org.tizen.NetNfcService.AllocationError",
148 "Can not allocate memory");
153 data->transceive = g_object_ref(transceive);
154 data->invocation = g_object_ref(invocation);
155 data->transceive_handle = handle;
156 data->transceive_info.dev_type = dev_type;
157 net_nfc_util_gdbus_variant_to_data_s(arg_data,
158 &data->transceive_info.trans_data);
160 result = net_nfc_server_controller_async_queue_push(
161 transceive_data_thread_func, data);
164 g_dbus_method_invocation_return_dbus_error(invocation,
165 "org.tizen.NetNfcService.Transceive.ThreadError",
166 "can not push to controller thread");
168 net_nfc_util_free_data(&data->transceive_info.trans_data);
170 g_object_unref(data->transceive);
171 g_object_unref(data->invocation);
179 static void transceive_thread_func(gpointer user_data)
181 TransceiveSendData *transceive_data = user_data;
182 net_nfc_target_handle_s *handle =
183 (net_nfc_target_handle_s *)transceive_data->transceive_handle;
184 net_nfc_error_e result = NET_NFC_OK;
187 /* use assert because it was checked in handle function */
188 g_assert(transceive_data != NULL);
189 g_assert(transceive_data->transceive != NULL);
190 g_assert(transceive_data->invocation != NULL);
192 if (net_nfc_server_target_connected(handle) == true)
194 NFC_DBG("call transceive");
196 if (net_nfc_controller_transceive(handle,
197 &transceive_data->transceive_info,
203 NFC_DBG("Transceive data received [%d]", data->length);
205 /* free resource because it doesn't need */
206 g_free(data->buffer);
213 NFC_ERR("target is not connected");
215 result = NET_NFC_TARGET_IS_MOVED_AWAY;
218 NFC_DBG("transceive result : %d", result);
220 net_nfc_gdbus_transceive_complete_transceive(
221 transceive_data->transceive,
222 transceive_data->invocation,
225 net_nfc_util_free_data(&transceive_data->transceive_info.trans_data);
227 g_object_unref(transceive_data->invocation);
228 g_object_unref(transceive_data->transceive);
230 g_free(transceive_data);
233 static gboolean transceive_handle(NetNfcGDbusTransceive *transceive,
234 GDBusMethodInvocation *invocation,
238 GVariant *smack_privilege,
241 TransceiveSendData *data;
244 NFC_INFO(">>> REQUEST from [%s]",
245 g_dbus_method_invocation_get_sender(invocation));
247 /* check privilege and update client context */
248 if (net_nfc_server_gdbus_check_privilege(invocation,
252 NFC_ERR("permission denied, and finished request");
257 data = g_new0(TransceiveSendData, 1);
260 NFC_ERR("Memory allocation failed");
261 g_dbus_method_invocation_return_dbus_error(invocation,
262 "org.tizen.NetNfcService.AllocationError",
263 "Can not allocate memory");
268 data->transceive = g_object_ref(transceive);
269 data->invocation = g_object_ref(invocation);
270 data->transceive_handle = handle;
271 data->transceive_info.dev_type = dev_type;
272 net_nfc_util_gdbus_variant_to_data_s(arg_data,
273 &data->transceive_info.trans_data);
275 result = net_nfc_server_controller_async_queue_push(
276 transceive_thread_func, data);
279 g_dbus_method_invocation_return_dbus_error(invocation,
280 "org.tizen.NetNfcService.Transceive.ThreadError",
281 "can not push to controller thread");
283 net_nfc_util_free_data(&data->transceive_info.trans_data);
285 g_object_unref(data->transceive);
286 g_object_unref(data->invocation);
295 gboolean net_nfc_server_transceive_init(GDBusConnection *connection)
297 GError *error = NULL;
300 if (transceive_skeleton)
301 g_object_unref(transceive_skeleton);
303 transceive_skeleton = net_nfc_gdbus_transceive_skeleton_new();
305 g_signal_connect(transceive_skeleton,
306 "handle-transceive-data",
307 G_CALLBACK(transceive_data_handle),
310 g_signal_connect(transceive_skeleton,
312 G_CALLBACK(transceive_handle),
315 result = g_dbus_interface_skeleton_export(
316 G_DBUS_INTERFACE_SKELETON(transceive_skeleton),
318 "/org/tizen/NetNfcService/Transceive",
323 g_object_unref(transceive_skeleton);
324 transceive_skeleton = NULL;
330 void net_nfc_server_transceive_deinit(void)
332 if (transceive_skeleton)
334 g_object_unref(transceive_skeleton);
335 transceive_skeleton = NULL;