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"
24 #include "net_nfc_server_common.h"
25 #include "net_nfc_server_context.h"
26 #include "net_nfc_server_tag.h"
27 #include "net_nfc_server_ndef.h"
29 typedef struct _ReadData ReadData;
33 NetNfcGDbusNdef *ndef;
34 GDBusMethodInvocation *invocation;
38 typedef struct _WriteData WriteData;
42 NetNfcGDbusNdef *ndef;
43 GDBusMethodInvocation *invocation;
48 typedef struct _MakeReadOnlyData MakeReadOnlyData;
50 struct _MakeReadOnlyData
52 NetNfcGDbusNdef *ndef;
53 GDBusMethodInvocation *invocation;
57 typedef struct _FormatData FormatData;
61 NetNfcGDbusNdef *ndef;
62 GDBusMethodInvocation *invocation;
68 static NetNfcGDbusNdef *ndef_skeleton = NULL;
70 static void ndef_read_thread_func(gpointer user_data);
72 static void ndef_write_thread_func(gpointer user_data);
74 static void ndef_make_read_only_thread_func(gpointer user_data);
76 static void ndef_format_thread_func(gpointer user_data);
79 static gboolean ndef_handle_read(NetNfcGDbusNdef *ndef,
80 GDBusMethodInvocation *invocation,
82 GVariant *smack_privilege,
85 static gboolean ndef_handle_write(NetNfcGDbusNdef *ndef,
86 GDBusMethodInvocation *invocation,
89 GVariant *smack_privilege,
92 static gboolean ndef_handle_make_read_only(NetNfcGDbusNdef *ndef,
93 GDBusMethodInvocation *invocation,
95 GVariant *smack_privilege,
98 static gboolean ndef_handle_format(NetNfcGDbusNdef *ndef,
99 GDBusMethodInvocation *invocation,
102 GVariant *smack_privilege,
106 static void ndef_read_thread_func(gpointer user_data)
108 ReadData *data = user_data;
110 net_nfc_target_handle_s *handle;
111 net_nfc_error_e result;
112 data_s *read_data = NULL;
113 GVariant *data_variant;
115 g_assert(data != NULL);
116 g_assert(data->ndef != NULL);
117 g_assert(data->invocation != NULL);
119 handle = GUINT_TO_POINTER(data->handle);
121 if (net_nfc_server_target_connected(handle) == true) {
122 net_nfc_controller_read_ndef(handle, &read_data, &result);
124 result = NET_NFC_TARGET_IS_MOVED_AWAY;
127 data_variant = net_nfc_util_gdbus_data_to_variant(read_data);
129 net_nfc_gdbus_ndef_complete_read(data->ndef,
135 net_nfc_util_free_data(read_data);
139 g_object_unref(data->invocation);
140 g_object_unref(data->ndef);
145 static void ndef_write_thread_func(gpointer user_data)
147 WriteData *data = user_data;
149 net_nfc_target_handle_s *handle;
150 net_nfc_error_e result;
152 g_assert(data != NULL);
153 g_assert(data->ndef != NULL);
154 g_assert(data->invocation != NULL);
156 handle = GUINT_TO_POINTER(data->handle);
158 if (net_nfc_server_target_connected(handle) == true) {
159 net_nfc_controller_write_ndef(handle, &data->data, &result);
161 result = NET_NFC_TARGET_IS_MOVED_AWAY;
164 net_nfc_gdbus_ndef_complete_write(data->ndef,
168 net_nfc_util_free_data(&data->data);
170 g_object_unref(data->invocation);
171 g_object_unref(data->ndef);
176 static void ndef_make_read_only_thread_func(gpointer user_data)
178 MakeReadOnlyData *data = user_data;
180 net_nfc_target_handle_s *handle;
181 net_nfc_error_e result;
183 g_assert(data != NULL);
184 g_assert(data->ndef != NULL);
185 g_assert(data->invocation != NULL);
187 handle = GUINT_TO_POINTER(data->handle);
189 if (net_nfc_server_target_connected(handle) == true) {
190 net_nfc_controller_make_read_only_ndef(handle, &result);
192 result = NET_NFC_TARGET_IS_MOVED_AWAY;
195 net_nfc_gdbus_ndef_complete_make_read_only(data->ndef,
199 g_object_unref(data->invocation);
200 g_object_unref(data->ndef);
205 static void ndef_format_thread_func(gpointer user_data)
207 FormatData *data = user_data;
209 net_nfc_target_handle_s *handle;
210 net_nfc_error_e result;
212 g_assert(data != NULL);
213 g_assert(data->ndef != NULL);
214 g_assert(data->invocation != NULL);
216 handle = GUINT_TO_POINTER(data->handle);
218 if (net_nfc_server_target_connected(handle) == true) {
219 net_nfc_controller_format_ndef(handle, &data->key, &result);
221 result = NET_NFC_TARGET_IS_MOVED_AWAY;
224 net_nfc_gdbus_ndef_complete_format(data->ndef,
228 net_nfc_util_free_data(&data->key);
230 g_object_unref(data->invocation);
231 g_object_unref(data->ndef);
236 static gboolean ndef_handle_read(NetNfcGDbusNdef *ndef,
237 GDBusMethodInvocation *invocation,
239 GVariant *smack_privilege,
245 NFC_INFO(">>> REQUEST from [%s]",
246 g_dbus_method_invocation_get_sender(invocation));
248 /* check privilege and update client context */
249 if (net_nfc_server_gdbus_check_privilege(invocation,
253 NFC_ERR("permission denied, and finished request");
258 data = g_new0(ReadData, 1);
261 NFC_ERR("Memory allocation failed");
262 g_dbus_method_invocation_return_dbus_error(invocation,
263 "org.tizen.NetNfcService.AllocationError",
264 "Can not allocate memory");
269 data->ndef = g_object_ref(ndef);
270 data->invocation = g_object_ref(invocation);
271 data->handle = arg_handle;
273 result = net_nfc_server_controller_async_queue_push(
274 ndef_read_thread_func,
278 g_dbus_method_invocation_return_dbus_error(invocation,
279 "org.tizen.NetNfcService.Ndef.ThreadError",
280 "can not push to controller thread");
282 g_object_unref(data->invocation);
283 g_object_unref(data->ndef);
291 static gboolean ndef_handle_write(NetNfcGDbusNdef *ndef,
292 GDBusMethodInvocation *invocation,
295 GVariant *smack_privilege,
301 NFC_INFO(">>> REQUEST from [%s]",
302 g_dbus_method_invocation_get_sender(invocation));
304 /* check privilege and update client context */
305 if (net_nfc_server_gdbus_check_privilege(invocation,
309 NFC_ERR("permission denied, and finished request");
314 data = g_new0(WriteData, 1);
317 NFC_ERR("Memory allocation failed");
318 g_dbus_method_invocation_return_dbus_error(invocation,
319 "org.tizen.NetNfcService.AllocationError",
320 "Can not allocate memory");
325 data->ndef = g_object_ref(ndef);
326 data->invocation = g_object_ref(invocation);
327 data->handle = arg_handle;
329 net_nfc_util_gdbus_variant_to_data_s(arg_data, &data->data);
331 result = net_nfc_server_controller_async_queue_push(
332 ndef_write_thread_func,
336 g_dbus_method_invocation_return_dbus_error(invocation,
337 "org.tizen.NetNfcService.Ndef.ThreadError",
338 "can not push to controller thread");
340 net_nfc_util_free_data(&data->data);
342 g_object_unref(data->invocation);
343 g_object_unref(data->ndef);
351 static gboolean ndef_handle_make_read_only(NetNfcGDbusNdef *ndef,
352 GDBusMethodInvocation *invocation,
354 GVariant *smack_privilege,
357 MakeReadOnlyData *data;
360 NFC_INFO(">>> REQUEST from [%s]",
361 g_dbus_method_invocation_get_sender(invocation));
363 /* check privilege and update client context */
364 if (net_nfc_server_gdbus_check_privilege(invocation,
368 NFC_ERR("permission denied, and finished request");
373 data = g_new0(MakeReadOnlyData, 1);
376 NFC_ERR("Memory allocation failed");
377 g_dbus_method_invocation_return_dbus_error(invocation,
378 "org.tizen.NetNfcService.AllocationError",
379 "Can not allocate memory");
384 data->ndef = g_object_ref(ndef);
385 data->invocation = g_object_ref(invocation);
386 data->handle = arg_handle;
388 result = net_nfc_server_controller_async_queue_push(
389 ndef_make_read_only_thread_func,
393 g_dbus_method_invocation_return_dbus_error(invocation,
394 "org.tizen.NetNfcService.Ndef.ThreadError",
395 "can not push to controller thread");
397 g_object_unref(data->invocation);
398 g_object_unref(data->ndef);
406 static gboolean ndef_handle_format(NetNfcGDbusNdef *ndef,
407 GDBusMethodInvocation *invocation,
410 GVariant *smack_privilege,
416 NFC_INFO(">>> REQUEST from [%s]",
417 g_dbus_method_invocation_get_sender(invocation));
419 /* check privilege and update client context */
420 if (net_nfc_server_gdbus_check_privilege(invocation,
424 NFC_ERR("permission denied, and finished request");
429 data = g_new0(FormatData, 1);
432 NFC_ERR("Memory allocation failed");
433 g_dbus_method_invocation_return_dbus_error(invocation,
434 "org.tizen.NetNfcService.AllocationError",
435 "Can not allocate memory");
440 data->ndef = g_object_ref(ndef);
441 data->invocation = g_object_ref(invocation);
442 data->handle = arg_handle;
443 net_nfc_util_gdbus_variant_to_data_s(arg_key, &data->key);
445 result = net_nfc_server_controller_async_queue_push(
446 ndef_format_thread_func,
450 g_dbus_method_invocation_return_dbus_error(invocation,
451 "org.tizen.NetNfcService.Ndef.ThreadError",
452 "can not push to controller thread");
454 net_nfc_util_free_data(&data->key);
456 g_object_unref(data->invocation);
457 g_object_unref(data->ndef);
465 gboolean net_nfc_server_ndef_init(GDBusConnection *connection)
468 GError *error = NULL;
471 net_nfc_server_ndef_deinit();
473 ndef_skeleton = net_nfc_gdbus_ndef_skeleton_new();
475 g_signal_connect(ndef_skeleton,
477 G_CALLBACK(ndef_handle_read),
480 g_signal_connect(ndef_skeleton,
482 G_CALLBACK(ndef_handle_write),
485 g_signal_connect(ndef_skeleton,
486 "handle-make-read-only",
487 G_CALLBACK(ndef_handle_make_read_only),
490 g_signal_connect(ndef_skeleton,
492 G_CALLBACK(ndef_handle_format),
495 result = g_dbus_interface_skeleton_export(
496 G_DBUS_INTERFACE_SKELETON(ndef_skeleton),
498 "/org/tizen/NetNfcService/Ndef",
504 net_nfc_server_ndef_deinit();
510 void net_nfc_server_ndef_deinit(void)
514 g_object_unref(ndef_skeleton);
515 ndef_skeleton = NULL;