Fix issues reported by static analyzer
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-main.c
1 /*
2 *  WiFi-Direct UG
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://www.tizenopensource.org/license
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 /**
21  * This file implements wifi direct application main functions.
22  *
23  * @file    wfd-app-main.c
24  * @author  Sungsik Jang (sungsik.jang@samsung.com)
25  * @version 0.1
26  */
27
28 #include <libintl.h>
29 #include <Elementary.h>
30 #include <notification.h>
31 #include <ui-gadget-module.h>
32 #include <app_control_internal.h>
33 #include <feedback.h>
34 #include <wifi-direct.h>
35 #include <efl_util.h>
36 #include <linux/unistd.h>
37 #include <vconf.h>
38
39 #include "wfd-app.h"
40 #include "wfd-app-util.h"
41
42 wfd_appdata_t *g_wfd_ad;
43
44
45 wfd_appdata_t *wfd_get_appdata()
46 {
47         return g_wfd_ad;
48 }
49
50 static void _win_del(void *data, Evas_Object *obj, void *event)
51 {
52         elm_exit();
53 }
54
55 static Evas_Object *_create_win(Evas_Object *parent, const char *name)
56 {
57         Evas_Object *eo;
58
59         /* eo = elm_win_add(parent, name, ELM_WIN_BASIC); */
60         eo = elm_win_add(NULL, name, ELM_WIN_NOTIFICATION);
61         if (eo) {
62                 elm_win_title_set(eo, name);
63                 elm_win_borderless_set(eo, EINA_TRUE);
64                 elm_win_alpha_set(eo, EINA_TRUE);
65                 evas_object_smart_callback_add(eo, "delete,request", _win_del, NULL);
66                 efl_util_set_notification_window_level(eo, EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT);
67                 evas_object_raise(eo);
68         }
69
70         return eo;
71 }
72
73 static bool _app_create(void *data)
74 {
75         __WFD_APP_FUNC_ENTER__;
76         wfd_appdata_t *ad = wfd_get_appdata();
77         int ret = 0;
78
79         if (data == NULL) {
80                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
81                 return FALSE;
82         }
83
84         bindtextdomain(LOCALE_FILE_NAME, LOCALEDIR);
85
86         ad->popup_data = (wfd_popup_t *) malloc(sizeof(wfd_popup_t));
87         if (!ad->popup_data) {
88                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "malloc failed\n");
89                 return FALSE;
90         }
91
92         memset(ad->popup_data, 0x0, sizeof(wfd_popup_t));
93         ad->win = _create_win(NULL, PACKAGE);
94
95         /* set rotation */
96         if (elm_win_wm_rotation_supported_get(ad->win)) {
97                 int rots[4] = {0, 90, 180, 270};
98                 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
99         }
100
101         ad->conformant = elm_conformant_add(ad->win);
102         assertm_if(NULL == ad->conformant, "conformant is NULL!!");
103         elm_win_conformant_set(ad->win, EINA_TRUE);
104         elm_win_resize_object_add(ad->win, ad->conformant);
105         evas_object_size_hint_weight_set(ad->conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
106         evas_object_size_hint_align_set(ad->conformant, EVAS_HINT_FILL, 0.0);
107         evas_object_show(ad->conformant);
108
109
110         ad->back_grnd = elm_bg_add(ad->conformant);
111         if (NULL == ad->back_grnd) {
112                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Create background failed\n");
113                 return FALSE;
114         }
115         elm_object_signal_emit(ad->conformant, "elm,state,indicator,nooverlap", "elm");
116         elm_object_style_set(ad->back_grnd, "indicator/headerbg");
117         elm_object_part_content_set(ad->conformant, "elm.swallow.indicator_bg", ad->back_grnd);
118         evas_object_size_hint_weight_set(ad->back_grnd,
119                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
120         evas_object_show(ad->back_grnd);
121
122         ad->layout = elm_layout_add(ad->conformant);
123         elm_object_content_set(ad->conformant, ad->layout);
124
125         ret = init_wfd_client(ad);
126         if (!ret) {
127                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "init_wfd_popup_client error\n");
128                 wfd_prepare_popup(WFD_POP_FAIL_INIT, NULL);
129                 __WFD_APP_FUNC_EXIT__;
130                 return FALSE;
131         }
132
133         ret = wfd_app_util_register_vconf_callbacks(ad);
134         if (ret < 0) {
135                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
136                 return FALSE;
137         }
138
139         /* Register Hard Key Press CB */
140         wfd_app_util_register_hard_key_down_cb(ad);
141
142         /* Initializes feedback API */
143         ret = feedback_initialize();
144         if (ret != FEEDBACK_ERROR_NONE) {
145                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_initialize error : %d\n", ret);
146                 return FALSE;
147         }
148         __WFD_APP_FUNC_EXIT__;
149         return TRUE;
150 }
151
152 static void _app_terminate(void *data)
153 {
154         __WFD_APP_FUNC_ENTER__;
155         wfd_appdata_t *ad = (wfd_appdata_t *) data;
156         int ret = 0;
157
158         if (data == NULL) {
159                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
160                 return;
161         }
162
163         wfd_app_util_del_notification(ad);
164
165         ret = wfd_app_util_deregister_vconf_callbacks(ad);
166         if (ret < 0)
167                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
168
169         /* Deregister Hardkey CB */
170         wfd_app_util_deregister_hard_key_down_cb(ad);
171
172         /* Deinitializes feedback API */
173         ret = feedback_deinitialize();
174         if (ret != FEEDBACK_ERROR_NONE)
175                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_deinitialize error : %d\n", ret);
176
177         if (ad->transmit_timer) {
178                 ecore_timer_del(ad->transmit_timer);
179                 ad->transmit_timer = NULL;
180         }
181
182         wfd_destroy_popup();
183
184         ret = deinit_wfd_client(ad);
185         if (ret < 0)
186                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "deinit_wfd_client error\n");
187
188         if (ad->back_grnd) {
189                 evas_object_del(ad->back_grnd);
190                 ad->back_grnd = NULL;
191         }
192
193         if (ad->win) {
194                 evas_object_del(ad->win);
195                 ad->win = NULL;
196         }
197
198         if (ad->popup_data) {
199                 free(ad->popup_data);
200                 ad->popup_data = NULL;
201         }
202
203         __WFD_APP_FUNC_EXIT__;
204         return;
205 }
206
207 static void _app_pause(void *data)
208 {
209         __WFD_APP_FUNC_ENTER__;
210         __WFD_APP_FUNC_EXIT__;
211         return;
212 }
213
214 static void _app_resume(void *data)
215 {
216         __WFD_APP_FUNC_ENTER__;
217         __WFD_APP_FUNC_EXIT__;
218         return;
219 }
220
221 static void _app_reset(app_control_h control, void *data)
222 {
223         __WFD_APP_FUNC_ENTER__;
224
225         int ret;
226         wfd_appdata_t *ad = (wfd_appdata_t *) data;
227         if (ad == NULL) {
228                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
229                 return;
230         }
231         if (control == NULL) {
232                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Service is NULL");
233                 return;
234         }
235
236         // From Notification
237         char *noti_type = NULL;
238         app_control_get_extra_data(control, NOTIFICATION_BUNDLE_PARAM, &noti_type);
239
240         if (noti_type == NULL) {
241                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Notification type is wrong.");
242                 return;
243         }
244
245         WFD_APP_LOG(WFD_APP_LOG_LOW, "Notification type is [%s]", noti_type);
246         if (strncmp(noti_type, NOTIFICATION_BUNDLE_VALUE, strlen(NOTIFICATION_BUNDLE_PARAM)) == 0) {
247                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch wifidirect-ug");
248                 wifi_direct_get_state(&ad->wfd_status);
249                 WFD_APP_LOG(WFD_APP_LOG_LOW, "State: %d", ad->wfd_status);
250                 if (ad->wfd_status == WIFI_DIRECT_STATE_CONNECTED) {
251                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Connected");
252                         if (ad->transmit_timer) {
253                                 ecore_timer_del(ad->transmit_timer);
254                                 ad->transmit_timer = NULL;
255                         }
256                         WFD_APP_LOG(WFD_APP_LOG_LOW, "start the transmit timer again\n");
257                         ad->last_wfd_transmit_time = time(NULL);
258                         ad->transmit_timer = ecore_timer_add(5.0,
259                                 (Ecore_Task_Cb)wfd_automatic_deactivated_for_connection_cb, ad);
260                 }
261                 app_control_h ug_control;
262                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Launching Settings EFL from notification\n");
263                 app_control_create(&ug_control);
264                 app_control_set_operation(ug_control, APP_CONTROL_OPERATION_DEFAULT);
265                 app_control_set_launch_mode(ug_control, APP_CONTROL_LAUNCH_MODE_GROUP);
266                 app_control_set_app_id(ug_control, "setting-wifidirect-efl");
267
268                 ret = app_control_send_launch_request(ug_control, NULL, NULL);
269                 if (ret == APP_CONTROL_ERROR_NONE)
270                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch Wi-Fi Direct successful");
271                 else
272                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to launch Wi-Fi Direct");
273
274                 app_control_destroy(ug_control);
275
276         }
277         WFD_IF_FREE_MEM(noti_type);
278         __WFD_APP_FUNC_EXIT__;
279         return;
280 }
281
282 int main(int argc, char *argv[])
283 {
284         wfd_appdata_t ad;
285         ui_app_lifecycle_callback_s event_callback;
286         memset(&event_callback, 0x0, sizeof(ui_app_lifecycle_callback_s));
287
288         event_callback.create = _app_create;
289         event_callback.terminate = _app_terminate;
290         event_callback.pause = _app_pause;
291         event_callback.resume = _app_resume;
292         event_callback.app_control = _app_reset;
293
294         memset(&ad, 0x0, sizeof(wfd_appdata_t));
295         g_wfd_ad = &ad;
296
297         return ui_app_main(argc, argv, &event_callback, &ad);
298 }