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_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_snep.h"
26 #include "net_nfc_server_p2p.h"
29 typedef struct _P2pSendData P2pSendData;
34 GDBusMethodInvocation *invocation;
40 static NetNfcGDbusP2p *p2p_skeleton = NULL;
42 static void p2p_send_data_thread_func(gpointer user_data)
44 net_nfc_error_e result;
45 net_nfc_target_handle_s *handle;
46 P2pSendData *p2p_data = user_data;
48 g_assert(p2p_data != NULL);
49 g_assert(p2p_data->p2p != NULL);
50 g_assert(p2p_data->invocation != NULL);
52 handle = GUINT_TO_POINTER(p2p_data->p2p_handle);
54 result = net_nfc_server_snep_default_client_start(handle, SNEP_REQ_PUT,
55 &p2p_data->data, -1, p2p_data);
56 if (result != NET_NFC_OK)
58 net_nfc_gdbus_p2p_complete_send(p2p_data->p2p, p2p_data->invocation, (gint)result);
60 net_nfc_util_free_data(&p2p_data->data);
62 g_object_unref(p2p_data->invocation);
63 g_object_unref(p2p_data->p2p);
69 static gboolean p2p_handle_send(NetNfcGDbusP2p *p2p,
70 GDBusMethodInvocation *invocation,
74 GVariant *smack_privilege,
81 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
83 /* check privilege and update client context */
84 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
85 "nfc-manager::p2p", "w");
88 NFC_ERR("permission denied, and finished request");
93 data = g_new0(P2pSendData, 1);
96 NFC_ERR("Memory allocation failed");
97 g_dbus_method_invocation_return_dbus_error(invocation,
98 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
103 data->p2p = g_object_ref(p2p);
104 data->invocation = g_object_ref(invocation);
105 data->type = arg_type;
106 data->p2p_handle = handle;
107 net_nfc_util_gdbus_variant_to_data_s(arg_data, &data->data);
109 result = net_nfc_server_controller_async_queue_push(p2p_send_data_thread_func, data);
113 g_dbus_method_invocation_return_dbus_error(invocation,
114 "org.tizen.NetNfcService.P2p.ThreadError",
115 "can not push to controller thread");
117 net_nfc_util_free_data(&data->data);
119 g_object_unref(data->invocation);
120 g_object_unref(data->p2p);
129 gboolean net_nfc_server_p2p_init(GDBusConnection *connection)
132 GError *error = NULL;
135 net_nfc_server_p2p_deinit();
137 p2p_skeleton = net_nfc_gdbus_p2p_skeleton_new();
139 g_signal_connect(p2p_skeleton, "handle-send", G_CALLBACK(p2p_handle_send), NULL);
141 result = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(p2p_skeleton),
142 connection, "/org/tizen/NetNfcService/P2p", &error);
147 net_nfc_server_p2p_deinit();
153 void net_nfc_server_p2p_deinit(void)
157 g_object_unref(p2p_skeleton);
162 void net_nfc_server_p2p_detached(void)
164 NFC_INFO("====== p2p target detached ======");
166 /* release target information */
167 net_nfc_server_free_target_info();
169 if (p2p_skeleton != NULL)
170 net_nfc_gdbus_p2p_emit_detached(p2p_skeleton);
173 void net_nfc_server_p2p_discovered(net_nfc_target_handle_s *handle)
175 NFC_INFO("====== p2p target discovered ======");
177 if (NULL == p2p_skeleton)
179 NFC_ERR("p2p_skeleton is not initialized");
184 net_nfc_gdbus_p2p_emit_discovered(p2p_skeleton, GPOINTER_TO_UINT(handle));
187 void net_nfc_server_p2p_received(data_s *user_data)
191 if (NULL == p2p_skeleton)
193 NFC_ERR("p2p_skeleton is not initialized");
198 arg_data = net_nfc_util_gdbus_data_to_variant((data_s *)user_data);
200 net_nfc_gdbus_p2p_emit_received(p2p_skeleton, arg_data);
203 void net_nfc_server_p2p_data_sent(net_nfc_error_e result, gpointer user_data)
205 P2pSendData *data = user_data;
207 g_assert(data != NULL);
208 g_assert(data->p2p != NULL);
209 g_assert(data->invocation != NULL);
211 net_nfc_gdbus_p2p_complete_send(data->p2p, data->invocation, (gint)result);
213 net_nfc_util_free_data(&data->data);
215 g_object_unref(data->invocation);
216 g_object_unref(data->p2p);