Sync code with Tizen 3.0 branch
[platform/core/api/nfc.git] / src / net_nfc_client_system_handler.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *     http://floralicense.org/license/
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_ndef_message.h"
20 #include "net_nfc_gdbus.h"
21 #include "net_nfc_client.h"
22 #include "net_nfc_client_util_internal.h"
23 #include "net_nfc_client_manager.h"
24 #include "net_nfc_client_system_handler.h"
25
26
27 #ifndef NET_NFC_EXPORT_API
28 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
29 #endif
30
31 /* LCOV_EXCL_START */
32
33 static NetNfcGDbusPopup *popup_proxy = NULL;
34 static int popup_state = 0;
35
36 static void popup_set_active_callback(GObject *source_object,
37         GAsyncResult *res,
38         gpointer user_data);
39
40 static void popup_set_active_callback(GObject *source_object,
41         GAsyncResult *res,
42         gpointer user_data)
43 {
44         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
45         net_nfc_error_e result;
46         GError *error = NULL;
47
48         g_assert(user_data != NULL);
49
50         if (net_nfc_gdbus_popup_call_set_finish(
51                 NET_NFC_GDBUS_POPUP(source_object),
52                 &result,
53                 res,
54                 &error) == FALSE) {
55                 DEBUG_ERR_MSG("Can not finish popup_set_active: %s",
56                         error->message);
57                 result = NET_NFC_IPC_FAIL;
58
59                 g_error_free(error);
60         }
61
62         if (func_data->callback != NULL) {
63                 net_nfc_client_popup_set_state_callback callback =
64                         (net_nfc_client_popup_set_state_callback)func_data->callback;
65
66                 callback(result, func_data->user_data);
67         }
68
69         g_free(func_data);
70 }
71
72 NET_NFC_EXPORT_API
73 net_nfc_error_e net_nfc_client_sys_handler_set_state(int state,
74         net_nfc_client_popup_set_state_callback callback,
75         void *user_data)
76 {
77         NetNfcCallback *func_data;
78
79         if (popup_proxy == NULL) {
80                 if (net_nfc_client_sys_handler_init() != NET_NFC_OK) {
81                         DEBUG_ERR_MSG("popup_proxy fail");
82                         return NET_NFC_NOT_INITIALIZED;
83                 }
84         }
85
86         /* prevent executing daemon when nfc is off */
87         if (net_nfc_client_manager_is_activated() == false)
88                 return NET_NFC_NOT_ACTIVATED;
89
90         func_data = g_try_new0(NetNfcCallback, 1);
91         if (func_data == NULL)
92                 return NET_NFC_ALLOC_FAIL;
93
94         func_data->callback = (gpointer)callback;
95         func_data->user_data = user_data;
96
97         net_nfc_gdbus_popup_call_set(popup_proxy,
98                 state,
99                 CHECK_FOREGROUND,
100                 NULL,
101                 popup_set_active_callback,
102                 func_data);
103
104         return NET_NFC_OK;
105 }
106
107 NET_NFC_EXPORT_API
108 net_nfc_error_e net_nfc_client_sys_handler_set_state_sync(int state)
109 {
110         net_nfc_error_e result = NET_NFC_OK;
111         GError *error = NULL;
112
113         if (popup_proxy == NULL) {
114                 if (net_nfc_client_sys_handler_init() != NET_NFC_OK) {
115                         DEBUG_ERR_MSG("popup_proxy fail");
116                         return NET_NFC_NOT_INITIALIZED;
117                 }
118         }
119
120         /* prevent executing daemon when nfc is off */
121         if (net_nfc_client_manager_is_activated() == false)
122                 return NET_NFC_NOT_ACTIVATED;
123
124         if (net_nfc_gdbus_popup_call_set_sync(popup_proxy,
125                 state,
126                 CHECK_FOREGROUND,
127                 &result,
128                 NULL,
129                 &error) == FALSE) {
130                 DEBUG_ERR_MSG("can not call SetActive: %s",
131                         error->message);
132                 result = NET_NFC_IPC_FAIL;
133
134                 g_error_free(error);
135         }
136
137         return result;
138 }
139
140 NET_NFC_EXPORT_API
141 net_nfc_error_e net_nfc_client_sys_handler_set_state_force(int state,
142         net_nfc_client_popup_set_state_callback callback,
143         void *user_data)
144 {
145         NetNfcCallback *func_data;
146
147         if (popup_proxy == NULL) {
148                 if (net_nfc_client_sys_handler_init() != NET_NFC_OK) {
149                         DEBUG_ERR_MSG("popup_proxy fail");
150                         return NET_NFC_NOT_INITIALIZED;
151                 }
152         }
153
154         /* prevent executing daemon when nfc is off */
155         if (net_nfc_client_manager_is_activated() == false)
156                 return NET_NFC_NOT_ACTIVATED;
157
158         func_data = g_try_new0(NetNfcCallback, 1);
159         if (func_data == NULL)
160                 return NET_NFC_ALLOC_FAIL;
161
162         func_data->callback = (gpointer)callback;
163         func_data->user_data = user_data;
164
165         net_nfc_gdbus_popup_call_set(popup_proxy,
166                 state,
167                 NO_CHECK_FOREGROUND,
168                 NULL,
169                 popup_set_active_callback,
170                 func_data);
171
172         return NET_NFC_OK;
173 }
174
175 NET_NFC_EXPORT_API
176 net_nfc_error_e net_nfc_client_sys_handler_set_state_force_sync(int state)
177 {
178         net_nfc_error_e result;
179         GError *error = NULL;
180
181         if (popup_proxy == NULL) {
182                 if (net_nfc_client_sys_handler_init() != NET_NFC_OK) {
183                         DEBUG_ERR_MSG("popup_proxy fail");
184                         return NET_NFC_NOT_INITIALIZED;
185                 }
186         }
187
188         /* prevent executing daemon when nfc is off */
189         if (net_nfc_client_manager_is_activated() == false)
190                 return NET_NFC_NOT_ACTIVATED;
191
192         if (net_nfc_gdbus_popup_call_set_sync(popup_proxy,
193                 state,
194                 NO_CHECK_FOREGROUND,
195                 &result,
196                 NULL,
197                 &error) == FALSE) {
198                 DEBUG_ERR_MSG("can not call SetActive: %s",
199                         error->message);
200                 result = NET_NFC_IPC_FAIL;
201
202                 g_error_free(error);
203         }
204
205         return result;
206 }
207
208 NET_NFC_EXPORT_API
209 net_nfc_error_e net_nfc_client_sys_handler_set_launch_popup_state(
210         int enable)
211 {
212         popup_state = enable;
213
214         return net_nfc_client_sys_handler_set_state_sync(enable);
215 }
216
217 NET_NFC_EXPORT_API
218 net_nfc_error_e net_nfc_client_sys_handler_set_launch_popup_state_force(
219         int enable)
220 {
221         popup_state = enable;
222
223         return net_nfc_client_sys_handler_set_state_force_sync(enable);
224 }
225
226 NET_NFC_EXPORT_API
227 net_nfc_error_e net_nfc_client_sys_handler_get_launch_popup_state(
228                         int *state)
229 {
230         net_nfc_error_e result = NET_NFC_OK;
231         gint out_state = NET_NFC_LAUNCH_APP_SELECT;
232         GError *error = NULL;
233
234         if (state == NULL)
235                 return NET_NFC_NULL_PARAMETER;
236
237         *state = NET_NFC_LAUNCH_APP_SELECT;
238
239         if (popup_proxy == NULL) {
240                 if (net_nfc_client_sys_handler_init() != NET_NFC_OK) {
241                         DEBUG_ERR_MSG("popup_proxy fail");
242                         return NET_NFC_NOT_INITIALIZED;
243                 }
244         }
245
246         /* prevent executing daemon when nfc is off */
247         if (net_nfc_client_manager_is_activated() == false)
248                 return NET_NFC_NOT_ACTIVATED;
249
250         if (net_nfc_gdbus_popup_call_get_sync(popup_proxy,
251                 &result,
252                 &out_state,
253                 NULL,
254                 &error) == true) {
255                 *state = out_state;
256         } else {
257
258                 DEBUG_ERR_MSG("net_nfc_gdbus_popup_call_get_sync failed: %s",
259                                 error->message);
260                 result = NET_NFC_IPC_FAIL;
261
262                 g_error_free(error);
263         }
264
265         return result;
266 }
267
268 net_nfc_error_e net_nfc_client_sys_handler_init(void)
269 {
270         GError *error = NULL;
271
272         if (popup_proxy) {
273                 DEBUG_CLIENT_MSG("Already initialized");
274
275                 return NET_NFC_OK;
276         }
277
278         popup_proxy = net_nfc_gdbus_popup_proxy_new_for_bus_sync(
279                                         G_BUS_TYPE_SYSTEM,
280                                         G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
281                                         "org.tizen.NetNfcService",
282                                         "/org/tizen/NetNfcService/Popup",
283                                         NULL,
284                                         &error);
285         if (popup_proxy == NULL) {
286                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
287                 g_error_free(error);
288
289                 return NET_NFC_OPERATION_FAIL;
290         }
291
292         return NET_NFC_OK;
293 }
294
295 void net_nfc_client_sys_handler_deinit(void)
296 {
297         if (popup_proxy) {
298                 g_object_unref(popup_proxy);
299                 popup_proxy = NULL;
300         }
301 }
302
303 /* LCOV_EXCL_STOP */
304