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