Remove build error in Tizen 3.0
[platform/core/connectivity/nfc-manager-neard.git] / client / net_nfc_client_p2p.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *                               http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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_ndef_message.h"
21 #include "net_nfc_util_gdbus_internal.h"
22 #include "net_nfc_gdbus.h"
23 #include "net_nfc_client.h"
24 #include "net_nfc_client_manager.h"
25 #include "net_nfc_client_p2p.h"
26 #include "net_nfc_neard.h"
27
28
29 typedef struct _P2p_SignalHandler P2pSignalHandler;
30
31 struct _P2p_SignalHandler
32 {
33         net_nfc_client_p2p_device_discovered p2p_device_discovered_cb;
34         net_nfc_client_p2p_device_detached p2p_device_detached_cb;
35         net_nfc_client_p2p_data_received p2p_data_received_cb;
36
37         gpointer p2p_device_discovered_data;
38         gpointer p2p_device_detached_data;
39         gpointer p2p_data_received_data;
40 };
41
42 static NetNfcGDbusP2p *p2p_proxy = NULL;
43 static P2pSignalHandler p2p_signal_handler;
44
45 static void p2p_device_detached(GObject *source_object,
46                 gpointer user_data)
47 {
48         NFC_INFO(">>> SIGNAL arrived");
49
50         RET_IF(NULL == p2p_signal_handler.p2p_device_detached_cb);
51
52         /*llcp client function to set/unset the current target id needs to be implemented*/
53         /*net_nfc_client_llcp_current_target_id(NULL);*/
54
55         p2p_signal_handler.p2p_device_detached_cb(p2p_signal_handler.p2p_device_detached_data);
56
57         /*llcp client function to close all socket needs to be implemented*/
58         /*net_nfc_client_llcp_close_all_socket();*/
59 }
60
61 static void p2p_device_discovered(GObject *source_object, guint arg_handle,
62                 gpointer user_data)
63 {
64         net_nfc_target_handle_s *handle_info = NULL;
65
66         NFC_INFO(">>> SIGNAL arrived");
67
68         RET_IF(NULL == p2p_signal_handler.p2p_device_discovered_cb);
69
70         handle_info = GUINT_TO_POINTER(arg_handle);
71
72
73         p2p_signal_handler.p2p_device_discovered_cb(handle_info,
74                                 p2p_signal_handler.p2p_device_discovered_data);
75
76 }
77
78 static void p2p_device_data_received(GObject *source_object, GVariant *arg_data,
79                 gpointer user_data)
80 {
81         data_s p2p_data = { NULL, };
82
83         NFC_INFO(">>> SIGNAL arrived");
84
85         RET_IF(NULL == p2p_signal_handler.p2p_data_received_cb);
86
87         net_nfc_util_gdbus_variant_to_data_s(arg_data, &p2p_data);
88         p2p_signal_handler.p2p_data_received_cb(&p2p_data,
89                 p2p_signal_handler.p2p_data_received_data);
90
91         net_nfc_util_free_data(&p2p_data);
92 }
93
94 API net_nfc_error_e net_nfc_client_p2p_send(net_nfc_target_handle_s *handle,
95                 data_s *data, net_nfc_client_p2p_send_completed callback, void *user_data)
96 {
97         /* prevent executing daemon when nfc is off */
98         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
99
100         return net_nfc_neard_send_p2p(handle, data, callback, user_data);
101 }
102
103
104
105 API net_nfc_error_e net_nfc_client_p2p_send_sync(net_nfc_target_handle_s *handle,
106                 data_s *data)
107 {
108         gboolean ret;
109         GVariant *arg_data;
110         GError *error = NULL;
111         net_nfc_error_e out_result;
112
113         RETV_IF(NULL == p2p_proxy, NET_NFC_NOT_INITIALIZED);
114
115         /* prevent executing daemon when nfc is off */
116         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
117
118         arg_data = net_nfc_util_gdbus_data_to_variant(data);
119
120         ret = net_nfc_gdbus_p2p_call_send_sync(p2p_proxy,
121                         0 /* FIXME */,
122                         arg_data,
123                         GPOINTER_TO_UINT(handle),
124                         net_nfc_client_gdbus_get_privilege(),
125                         (gint *)&out_result,
126                         NULL,
127                         &error);
128
129         if (FALSE == ret)
130         {
131                 NFC_ERR("p2p send (sync call) failed: %s", error->message);
132
133                 g_error_free(error);
134                 out_result = NET_NFC_IPC_FAIL;
135         }
136
137         return out_result;
138 }
139
140
141 API void net_nfc_client_p2p_set_device_discovered(
142                 net_nfc_client_p2p_device_discovered callback, void *user_data)
143 {
144         RET_IF(NULL == callback);
145
146         net_nfc_neard_set_p2p_discovered(callback, user_data);
147 }
148
149
150 API void net_nfc_client_p2p_set_device_detached(
151                 net_nfc_client_p2p_device_detached callback, void *user_data)
152 {
153         RET_IF(NULL == callback);
154
155         net_nfc_neard_set_p2p_detached(callback, user_data);
156 }
157
158
159 API void net_nfc_client_p2p_set_data_received(
160                 net_nfc_client_p2p_data_received callback, void *user_data)
161 {
162         RET_IF(NULL == callback);
163
164         net_nfc_neard_set_p2p_data_received(callback, user_data);
165 }
166
167
168 API void net_nfc_client_p2p_unset_device_discovered(void)
169 {
170         net_nfc_neard_unset_p2p_discovered();
171 }
172
173
174 API void net_nfc_client_p2p_unset_device_detached(void)
175 {
176         net_nfc_neard_unset_p2p_detached();
177 }
178
179
180 API void net_nfc_client_p2p_unset_data_received(void)
181 {
182         net_nfc_neard_unset_p2p_data_received();
183 }
184
185 net_nfc_error_e net_nfc_client_p2p_init(void)
186 {
187         GError *error = NULL;
188
189         if (p2p_proxy)
190         {
191                 NFC_WARN("Already initialized");
192                 return NET_NFC_OK;
193         }
194
195         p2p_proxy = net_nfc_gdbus_p2p_proxy_new_for_bus_sync(
196                         G_BUS_TYPE_SYSTEM,
197                         G_DBUS_PROXY_FLAGS_NONE,
198                         "org.tizen.NetNfcService",
199                         "/org/tizen/NetNfcService/P2p",
200                         NULL,
201                         &error);
202
203         if (NULL == p2p_proxy)
204         {
205                 NFC_ERR("Can not create proxy : %s", error->message);
206                 g_error_free(error);
207
208                 return NET_NFC_UNKNOWN_ERROR;
209         }
210
211         g_signal_connect(p2p_proxy, "detached", G_CALLBACK(p2p_device_detached), NULL);
212         g_signal_connect(p2p_proxy, "discovered", G_CALLBACK(p2p_device_discovered), NULL);
213         g_signal_connect(p2p_proxy, "received", G_CALLBACK(p2p_device_data_received), NULL);
214
215         return NET_NFC_OK;
216 }
217
218 void net_nfc_client_p2p_deinit(void)
219 {
220         if (p2p_proxy)
221         {
222                 g_object_unref(p2p_proxy);
223                 p2p_proxy = NULL;
224         }
225 }