2 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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_debug_internal.h"
18 #include "net_nfc_util_internal.h"
19 #include "net_nfc_util_gdbus_internal.h"
20 #include "net_nfc_server_controller.h"
21 #include "net_nfc_gdbus.h"
22 #include "net_nfc_server_common.h"
23 #include "net_nfc_server_tag.h"
24 #include "net_nfc_server_context.h"
25 #include "net_nfc_server_process_phdc.h"
26 #include "net_nfc_server_phdc.h"
27 #include "net_nfc_server_llcp.h"
29 typedef struct _PhdcSendData PhdcSendData;
33 NetNfcGDbusPhdc *phdc;
34 GDBusMethodInvocation *invocation;
39 static NetNfcGDbusPhdc *phdc_skeleton = NULL;
42 static gboolean _phdc_send_request_cb_(net_nfc_phdc_handle_h handle,
43 net_nfc_error_e result, net_nfc_server_phdc_indication_type indication,
47 NFC_DBG("phdc_send_request_cb_result [%d]",result);
49 GVariant *parameter = (GVariant *)user_data;
51 g_assert(parameter != NULL);
53 NetNfcGDbusPhdc *object;
54 GDBusMethodInvocation *invocation;
55 net_nfc_phdc_handle_h phdc_handle;
58 g_variant_get(parameter,
62 (guint *)&phdc_handle,
65 net_nfc_gdbus_phdc_complete_send(object, invocation, (gint)result);
67 g_variant_unref(phdc_data);
68 g_object_unref(invocation);
69 g_object_unref(object);
70 g_variant_unref(parameter);
77 static void phdc_send_data_thread_func(gpointer user_data)
79 NetNfcGDbusPhdc *object;
80 GDBusMethodInvocation *invocation;
81 net_nfc_error_e result;
82 net_nfc_phdc_handle_h handle = NULL;
84 data_s data = { NULL, };
86 if (NULL == user_data)
88 NFC_ERR("cannot get PHDC client data");
92 NFC_DBG(">>> phdc_send_data_thread_func");
94 g_variant_get((GVariant *)user_data,
101 g_assert(object != NULL);
102 g_assert(invocation != NULL);
104 net_nfc_util_gdbus_variant_to_data_s(phdc_data, &data);
106 result = net_nfc_server_phdc_agent_request(handle, &data,
107 _phdc_send_request_cb_, user_data);
109 if (result != NET_NFC_OK)
111 net_nfc_gdbus_phdc_complete_send(object, invocation, (gint)result);
113 g_object_unref(invocation);
114 g_object_unref(object);
115 g_variant_unref(user_data);
118 g_variant_unref(phdc_data);
121 static gboolean phdc_handle_send(NetNfcGDbusPhdc *phdc,
122 GDBusMethodInvocation *invocation, //object for handling remote calls,provides a way to asynly return data
125 GVariant *smack_privilege)
129 GVariant *parameter = NULL;
131 NFC_INFO(">>> REQUEST from [%s]",g_dbus_method_invocation_get_sender(invocation));
133 /* check privilege and update client context */
134 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
135 "nfc-manager::p2p", "w");
139 NFC_ERR("permission denied, and finished request");
140 result = NET_NFC_SECURITY_FAIL;
144 parameter = g_variant_new("(uuu@a(y))",
145 GPOINTER_TO_UINT(g_object_ref(phdc)),
146 GPOINTER_TO_UINT(g_object_ref(invocation)),
150 if (NULL == parameter)
152 NFC_ERR("Memory allocation failed");
153 result = NET_NFC_ALLOC_FAIL;
158 result = net_nfc_server_controller_async_queue_push(phdc_send_data_thread_func, parameter);
162 /* return error if queue was blocked */
163 NFC_ERR("controller is processing important message..");
164 result = NET_NFC_BUSY;
174 if (parameter != NULL)
176 g_object_unref(invocation);
177 g_object_unref(phdc);
178 g_variant_unref(parameter);
181 net_nfc_gdbus_phdc_complete_send(phdc, invocation, result);
185 void net_nfc_server_phdc_data_received_indication(data_s *arg_data)
187 NFC_INFO("=net_nfc_server_phdc_transport_data_received_indication=");
190 data = net_nfc_util_gdbus_data_to_variant(arg_data);
192 if (NULL == phdc_skeleton)
194 NFC_ERR("phdc_skeleton is not initialized");
198 net_nfc_gdbus_phdc_emit_phdc_received(phdc_skeleton, data);
201 static void _emit_phdc_event_signal(GVariant *parameter,
202 net_nfc_phdc_handle_h handle, net_nfc_error_e result, uint32_t type)
205 char *client_id = NULL;
206 void *user_data = NULL;
207 GError *error = NULL;
208 GDBusConnection *connection;
210 g_variant_get(parameter, "(usu)", (guint *)&connection, &client_id,
211 (guint *)&user_data);
213 ret = g_dbus_connection_emit_signal(
216 "/org/tizen/NetNfcService/Phdc",
217 "org.tizen.NetNfcService.Phdc",
219 g_variant_new("(iuu)", (gint)result, type,
220 GPOINTER_TO_UINT(user_data)), &error);
224 if (error != NULL && error->message != NULL)
225 NFC_ERR("g_dbus_connection_emit_signal failed : %s", error->message);
227 NFC_ERR("g_dbus_connection_emit_signal failed");
233 static void _server_phdc_agent_cb_(net_nfc_phdc_handle_h handle,
234 net_nfc_error_e result, net_nfc_server_phdc_indication_type indication, data_s *data)
236 if(NET_NFC_OK != result)
238 net_nfc_server_phdc_transport_disconnect_indication();
242 RET_IF(NULL == handle);
244 NFC_DBG(" handle [%p], result[%d]", handle, result);
246 if( NET_NFC_PHDC_TARGET_CONNECTED == indication)
247 net_nfc_server_phdc_transport_connect_indication(handle);
248 else if(NET_NFC_PHDC_DATA_RECEIVED == indication)
249 net_nfc_server_phdc_data_received_indication(data);
255 static void _phdc_agent_activate_cb(int event, net_nfc_target_handle_s *handle,
256 uint32_t sap, const char *san, void *user_param)
258 net_nfc_error_e result = NET_NFC_OK;
259 GVariant *parameter = (GVariant *)user_param;
260 char *client_id = NULL;
261 void *user_data = NULL;
262 GVariant *param = NULL;
263 GDBusConnection *connection = NULL;
265 NFC_DBG("event [%d], handle [%p], sap [%d], san [%s]", event, handle, sap, san);
267 if (NET_NFC_LLCP_START == event)
269 g_variant_get(parameter, "(usu)", (guint *)&connection, &client_id, (guint *)&user_data);
271 param = g_variant_new("(usu)", GPOINTER_TO_UINT(g_object_ref(connection)),client_id,
272 GPOINTER_TO_UINT(user_data));
276 /* start phdc agent service*/
277 result = net_nfc_server_phdc_agent_start(handle, (char *)san, sap,
278 _server_phdc_agent_cb_, param);
280 if (NET_NFC_OK == result)
282 _emit_phdc_event_signal(parameter, handle, result, event);
286 NFC_ERR("net_nfc_server_phdc_manager_start failed, [%d]", result);
287 g_variant_unref(param);
292 _emit_phdc_event_signal(parameter, handle, result, NET_NFC_LLCP_UNREGISTERED);
293 /* unregister server */
294 g_variant_unref(parameter);
299 static void _server_phdc_manager_cb_(net_nfc_phdc_handle_h handle,
300 net_nfc_error_e result, net_nfc_server_phdc_indication_type indication, data_s *data)
303 if(NET_NFC_OK != result)
305 net_nfc_server_phdc_transport_disconnect_indication();
309 RET_IF(NULL == handle);
311 NFC_DBG("result [%d], data [%p]", result, data);
313 if( NET_NFC_PHDC_TARGET_CONNECTED == indication)
314 net_nfc_server_phdc_transport_connect_indication(handle);
315 else if(NET_NFC_PHDC_DATA_RECEIVED == indication)
316 net_nfc_server_phdc_data_received_indication(data);
322 static void _phdc_manager_activate_cb(int event, net_nfc_target_handle_s *handle,
323 uint32_t sap, const char *san, void *user_param)
325 net_nfc_error_e result = NET_NFC_OK;
326 GVariant *parameter = (GVariant *)user_param;
327 char *client_id = NULL;
328 void *user_data = NULL;
329 GVariant *param = NULL;
330 GDBusConnection *connection = NULL;
332 NFC_DBG("event [%d], handle [%p], sap [%d], san [%s]", event, handle, sap, san);
334 if (NET_NFC_LLCP_START == event)
336 g_variant_get(parameter, "(usu)", (guint *)&connection, &client_id, (guint *)&user_data);
338 param = g_variant_new("(usu)", GPOINTER_TO_UINT(g_object_ref(connection)),client_id,
339 GPOINTER_TO_UINT(user_data));
343 /* start phdc manager service*/
344 result = net_nfc_server_phdc_manager_start(handle, (char *)san, sap,
345 _server_phdc_manager_cb_, param);
347 if (NET_NFC_OK == result)
349 _emit_phdc_event_signal(parameter, handle, result, event);
353 NFC_ERR("net_nfc_server_phdc_manager_start failed, [%d]", result);
354 g_variant_unref(param);
359 _emit_phdc_event_signal(parameter, handle, result, NET_NFC_LLCP_UNREGISTERED);
360 /* unregister server */
361 g_variant_unref(parameter);
366 static void phdc_register_server_thread_func(gpointer user_data)
368 gchar *arg_san = NULL;
372 net_nfc_error_e result = NET_NFC_OK;
373 NetNfcGDbusPhdc *object = NULL;
374 g_assert(user_data != NULL);
375 GVariant *parameter = NULL;
376 GDBusConnection *connection = NULL;
377 GDBusMethodInvocation *invocation = NULL;
379 g_variant_get((GVariant *)user_data, "(uuusu)", (guint *)&object, (guint *)&invocation,
380 &arg_role, &arg_san, &arg_user_data);
382 g_assert(object != NULL);
383 g_assert(invocation != NULL);
385 connection = g_dbus_method_invocation_get_connection(invocation);
387 parameter = g_variant_new("(usu)", GPOINTER_TO_UINT(g_object_ref(connection)),
388 g_dbus_method_invocation_get_sender(invocation), arg_user_data);
390 if (parameter != NULL)
392 if(!strcmp(arg_san,PHDC_SAN))
397 else if (!strcmp(arg_san,PHDS_SAN))
404 // anything else, as of now,defaulting to PHDC default server
409 if(NET_NFC_PHDC_MANAGER == arg_role)
411 result = net_nfc_server_llcp_register_service(
412 g_dbus_method_invocation_get_sender(invocation), arg_sap,
413 arg_san, _phdc_manager_activate_cb, parameter);
415 else if(NET_NFC_PHDC_AGENT == arg_role)
417 result = net_nfc_server_llcp_register_service(
418 g_dbus_method_invocation_get_sender(invocation), arg_sap,
419 arg_san, _phdc_agent_activate_cb, parameter);
422 if (result != NET_NFC_OK)
424 NFC_ERR("net_nfc_service_llcp_register_service failed, [%d]", result);
425 g_object_unref(connection);
426 g_variant_unref(parameter);
431 result = NET_NFC_ALLOC_FAIL;
432 g_object_unref(connection);
435 net_nfc_gdbus_phdc_complete_register_role(object, invocation, result);
438 g_variant_unref(user_data);
443 static gboolean phdc_handle_register(
444 NetNfcGDbusPhdc *object,
445 GDBusMethodInvocation *invocation,
447 const gchar *arg_san,
449 GVariant *arg_privilege)
453 GVariant *parameter = NULL;
455 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
457 /* check privilege and update client context */
458 ret = net_nfc_server_gdbus_check_privilege(invocation, arg_privilege,
459 "nfc-manager::p2p", "rw");
462 NFC_ERR("permission denied, and finished request");
463 result = NET_NFC_SECURITY_FAIL;
467 parameter = g_variant_new("(uuusu)", GPOINTER_TO_UINT(g_object_ref(object)),
468 GPOINTER_TO_UINT(g_object_ref(invocation)), arg_role, arg_san, arg_user_data);
470 if (parameter == NULL)
472 NFC_ERR("Memory allocation failed");
473 result = NET_NFC_ALLOC_FAIL;
476 if(net_nfc_server_controller_async_queue_push(
477 phdc_register_server_thread_func, parameter) == FALSE)
479 NFC_ERR("controller is processing important message.");
480 result = NET_NFC_ALLOC_FAIL;
487 if (parameter != NULL)
489 g_object_unref(invocation);
490 g_object_unref(object);
491 g_variant_unref(parameter);
494 net_nfc_gdbus_phdc_complete_register_role(object,
501 static void phdc_unregister_server_thread_func(gpointer user_data)
505 gchar *arg_san = NULL;
506 net_nfc_error_e result;
507 NetNfcGDbusSnep *object = NULL;
508 GDBusMethodInvocation *invocation = NULL;
510 g_assert(user_data != NULL);
512 g_variant_get((GVariant *)user_data, "(uuus)", (guint *)&object, (guint *)&invocation,
513 &arg_role, &arg_san);
515 g_assert(object != NULL);
516 g_assert(invocation != NULL);
518 if(!strcmp(arg_san,PHDS_SAN))
523 result = net_nfc_server_llcp_unregister_service(
524 g_dbus_method_invocation_get_sender(invocation), arg_sap, arg_san);
526 net_nfc_gdbus_snep_complete_server_unregister(object, invocation, result);
530 g_object_unref(invocation);
531 g_object_unref(object);
533 g_variant_unref(user_data);
536 static gboolean phdc_handle_unregister(
537 NetNfcGDbusPhdc *object,
538 GDBusMethodInvocation *invocation,
540 const gchar *arg_san,
541 GVariant *arg_privilege)
545 GVariant *parameter = NULL;
547 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
549 /* check privilege and update client context */
550 ret = net_nfc_server_gdbus_check_privilege(invocation, arg_privilege,
551 "nfc-manager::p2p", "rw");
555 NFC_ERR("permission denied, and finished request");
556 result = NET_NFC_SECURITY_FAIL;
560 parameter = g_variant_new("(uuus)", GPOINTER_TO_UINT(g_object_ref(object)),
561 GPOINTER_TO_UINT(g_object_ref(invocation)), arg_role, arg_san);
563 if (parameter == NULL)
565 NFC_ERR("Memory allocation failed");
566 result = NET_NFC_ALLOC_FAIL;
569 if(net_nfc_server_controller_async_queue_push(
570 phdc_unregister_server_thread_func, parameter) == FALSE)
572 NFC_ERR("controller is processing important message.");
573 result = NET_NFC_ALLOC_FAIL;
580 if (parameter != NULL)
582 g_object_unref(invocation);
583 g_object_unref(object);
585 g_variant_unref(parameter);
588 net_nfc_gdbus_phdc_complete_register_role(object,
596 gboolean net_nfc_server_phdc_init(GDBusConnection *connection)
599 GError *error = NULL;
602 net_nfc_server_phdc_deinit();
604 phdc_skeleton = net_nfc_gdbus_phdc_skeleton_new();
606 g_signal_connect(phdc_skeleton,"handle-send", G_CALLBACK(phdc_handle_send), NULL);
608 g_signal_connect(phdc_skeleton,"handle-register-role", G_CALLBACK(phdc_handle_register), NULL);
610 g_signal_connect(phdc_skeleton,"handle-unregister-role", G_CALLBACK(phdc_handle_unregister), NULL);
612 result = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(phdc_skeleton),
613 connection, "/org/tizen/NetNfcService/Phdc", &error);
618 net_nfc_server_phdc_deinit();
624 void net_nfc_server_phdc_deinit(void)
628 g_object_unref(phdc_skeleton);
629 phdc_skeleton = NULL;
633 void net_nfc_server_phdc_transport_disconnect_indication(void)
635 NFC_INFO("====== phdc target disconnected ======");
637 /* release target information */
638 net_nfc_server_free_target_info();
640 if (phdc_skeleton != NULL)
641 net_nfc_gdbus_phdc_emit_phdc_disconnect(phdc_skeleton);
645 void net_nfc_server_phdc_transport_connect_indication(net_nfc_phdc_handle_h handle)
647 NFC_INFO("====== phdc target connected ======");
649 if (NULL == phdc_skeleton)
651 NFC_ERR("phdc_skeleton is not initialized");
655 net_nfc_gdbus_phdc_emit_phdc_connect(phdc_skeleton, GPOINTER_TO_UINT(handle));
658 void net_nfc_server_phdc_data_sent(net_nfc_error_e result, gpointer user_data)
660 PhdcSendData *data = (PhdcSendData *)user_data;
662 g_assert(data != NULL);
663 g_assert(data->phdc != NULL);
664 g_assert(data->invocation != NULL);
666 net_nfc_gdbus_phdc_complete_send(data->phdc, data->invocation, (gint)result);
668 net_nfc_util_free_data(&data->data);
670 g_object_unref(data->invocation);
671 g_object_unref(data->phdc);