[ug-wifi-direct]Sync with Tizen 2.4
[apps/native/ug-wifi-direct.git] / ug-wifidirect / src / wfd_motion_control.c
1 /*
2  * Wi-Fi direct
3  *
4  * Copyright 2012 Samsung Electronics Co., Ltd
5  *
6  * Licensed under the Flora License, Version 1.0 (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 #include <vconf.h>
21 #include <sensor_internal.h>
22 #include <vconf-keys.h>
23
24 #include <libintl.h>
25 #include <glib.h>
26
27 #include <Elementary.h>
28 #include <vconf.h>
29 #include <ui-gadget-module.h>
30 #include <wifi-direct.h>
31
32 #include "wfd_motion_control.h"
33 #include "wfd_ug_view.h"
34 #include "wfd_client.h"
35
36
37 static int motion_handle = -1;
38
39 static TARGET_VIEW_FOCUS __motion_target_view_focus_get(void *data)
40 {
41         struct ug_data *ugd = (struct ug_data *)data;
42
43         if ((ugd == NULL) || (ugd->naviframe == NULL)) {
44                 return MOTION_TARGET_VIEW_FOCUS_OFF;
45         }
46
47         if (elm_object_focus_get(ugd->naviframe)) {
48                 return MOTION_TARGET_VIEW_FOCUS_ON;
49         } else {
50                 return MOTION_TARGET_VIEW_FOCUS_OFF;
51         }
52 }
53
54
55 static void __motion_shake_cb(unsigned int event_type, sensor_event_data_t *event_data, void *data)
56 {
57         __FUNC_ENTER__;
58
59         gboolean motion_activated = FALSE;
60         struct ug_data *ugd = (struct ug_data *) data;
61         if (NULL == ugd) {
62                 DBG(LOG_ERROR,"NULL pointer!");
63                 return;
64         }
65         TARGET_VIEW_FOCUS focus_state = __motion_target_view_focus_get(ugd);
66
67         DBG(LOG_DEBUG, "event type: %d, focus state: %d\n", event_type, focus_state);
68
69         if (focus_state != MOTION_TARGET_VIEW_FOCUS_ON) {
70                 return;
71         }
72
73         if (vconf_get_bool(VCONFKEY_SETAPPL_MOTION_ACTIVATION,
74                         (void *)&motion_activated)) {
75                 DBG(LOG_ERROR,"Get motion activation status fail");
76                 return;
77         }
78
79         if (FALSE == motion_activated) {
80                 DBG(LOG_INFO,"Motion value is false");
81                 return;
82         }
83
84         if (vconf_get_bool(VCONFKEY_SETAPPL_USE_SHAKE,
85                         (void *)&motion_activated)) {
86                 DBG(LOG_ERROR, "Get use shake status fail");
87                 return;
88         }
89
90         if (FALSE == motion_activated) {
91                 DBG(LOG_INFO,"Shake status is false");
92                 return;
93         }
94
95         int ret = -1;
96         const char *btn_text = NULL;
97
98         if (NULL == ugd) {
99                 DBG(LOG_ERROR, "Incorrect parameter(NULL)\n");
100                 return;
101         }
102
103         wfd_refresh_wifi_direct_state(ugd);
104
105         if (NULL == ugd->scan_toolbar) {
106                 DBG(LOG_ERROR, "NULL == ugd->scan_toolbar\n");
107                 return;
108         }
109
110         btn_text = elm_object_part_text_get(ugd->scan_toolbar, "default");
111         if (NULL == btn_text) {
112                 DBG(LOG_ERROR, "Incorrect button text(NULL)\n");
113                 return;
114         }
115
116         GList *iterator = NULL;
117
118         if (0 == strcmp(btn_text, _("IDS_WIFI_SK4_SCAN"))) {
119                 if (WIFI_DIRECT_STATE_CONNECTED == ugd->wfd_status) {
120                         ret = wfd_client_disconnect(NULL);
121                         if (ret != WIFI_DIRECT_ERROR_NONE) {
122                                 DBG(LOG_ERROR, "Failed wfd_client_disconnect() [%d]\n", ret);
123                                 __FUNC_EXIT__;
124                                 return;
125                         }
126
127                         if (ugd->multi_connect_mode != WFD_MULTI_CONNECT_MODE_NONE) {
128                                 wfd_free_multi_selected_peers(ugd);
129                         } else {
130                                 /* update the connecting icon */
131                                 for (iterator = ugd->raw_discovered_peer_list; iterator; iterator = iterator->next) {
132                                         ((device_type_s *)iterator->data)->conn_status = PEER_CONN_STATUS_DISCONNECTED;
133                                         wfd_ug_view_refresh_glitem(((device_type_s *)iterator->data)->gl_item);
134                                 }
135                         }
136                 } else if (WIFI_DIRECT_STATE_DEACTIVATED == ugd->wfd_status) {
137                         wfd_client_switch_on(ugd);
138                 } else if ((WIFI_DIRECT_STATE_DISCOVERING == ugd->wfd_status) || (WIFI_DIRECT_STATE_ACTIVATED == ugd->wfd_status)) {
139                         ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL_START;
140                         ret = wifi_direct_start_discovery_specific_channel(false, 1, WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL);
141                         if (ret != WIFI_DIRECT_ERROR_NONE) {
142                                 ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_NONE;
143                                 DBG(LOG_ERROR, "Failed to start discovery. [%d]\n", ret);
144                                 wifi_direct_cancel_discovery();
145                         }
146                 }
147         }
148
149         __FUNC_EXIT__;
150 }
151
152 void motion_create(struct ug_data *ugd)
153 {
154         int ret = -1;
155
156         motion_handle = sf_connect(MOTION_SENSOR);
157         if (motion_handle < 0) {
158                 DBG(LOG_ERROR, "Failed Operation sf_connect.\n");
159                 return;
160         }
161         ret = sf_register_event(motion_handle, MOTION_ENGINE_EVENT_SHAKE, NULL,
162                         __motion_shake_cb, ugd);
163         if (ret < 0) {
164                 DBG(LOG_ERROR, "Failed Operation sf_register_event. [%d]\n", ret);
165                 goto fail;
166         }
167         ret = sf_start(motion_handle, 0);
168         if (ret < 0) {
169                 DBG(LOG_ERROR, "Failed Operation sf_start. [%d]\n", ret);
170                 goto fail;
171         }
172
173         DBG(LOG_INFO, "Succesfully, Init Sensor Handle\n");
174         return;
175
176 fail:
177         ret = sf_disconnect(motion_handle);
178         if (ret < 0) {
179                 DBG(LOG_ERROR, "Failed Operation sf_disconnect. [%d]\n", ret);
180         }
181 }
182
183 void motion_destroy(void)
184 {
185         int ret = -1;
186
187         if (motion_handle < 0) {
188                 DBG(LOG_ERROR, "Motion Handle Not valid !!!");
189                 return;
190         }
191
192         ret = sf_stop(motion_handle);
193         if (ret < 0) {
194                 DBG(LOG_ERROR, "Failed Operation sf_stop. [%d]\n", ret);
195         }
196         ret = sf_unregister_event(motion_handle, MOTION_ENGINE_EVENT_SHAKE);
197         if (ret < 0) {
198                 DBG(LOG_ERROR, "Failed Operation sf_unregister_event. [%d]\n", ret);
199         }
200         ret = sf_disconnect(motion_handle);
201         if (ret < 0) {
202                 DBG(LOG_ERROR, "Failed Operation sf_disconnect. [%d]\n", ret);
203         }
204 }