4 * Copyright 2012 Samsung Electronics Co., Ltd
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
10 * http://www.tizenopensource.org/license
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.
22 #define UG_MODULE_API __attribute__ ((visibility("default")))
28 #include <sys/utsname.h>
31 #include <Elementary.h>
32 #include <ui-gadget-module.h>
33 #include <wifi-direct.h>
36 #include "wfd_ug_view.h"
37 #include "wfd_client.h"
39 void initialize_gen_item_class();
41 struct ug_data *global_ugd = NULL;
43 struct ug_data *wfd_get_ug_data()
49 * This function let the ug create backgroud
51 * @param[in] ugd the pointer to the parent object
52 * @param[in] ugd the pointer to the main data structure
54 static Evas_Object *_create_bg(Evas_Object *parent, char *style)
59 bg = elm_bg_add(parent);
60 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
61 elm_object_style_set(bg, style);
62 elm_win_resize_object_add(parent, bg);
70 * This function let the ug create full view
72 * @param[in] ugd the pointer to the parent object
73 * @param[in] ugd the pointer to the main data structure
75 static Evas_Object *_create_fullview(Evas_Object *parent, struct ug_data *ugd)
81 DBG(LOG_ERROR, "Incorrenct parameter");
85 /* Create Full view */
86 base = elm_layout_add(parent);
88 DBG(LOG_ERROR, "Failed to add layout");
92 elm_layout_theme_set(base, "layout", "application", "default");
93 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
94 evas_object_size_hint_align_set(base, EVAS_HINT_FILL, EVAS_HINT_FILL);
101 * This function let the ug create frame view
103 * @param[in] ugd the pointer to the parent object
104 * @param[in] ugd the pointer to the main data structure
106 static Evas_Object *_create_frameview(Evas_Object *parent, struct ug_data *ugd)
111 if (parent == NULL) {
112 DBG(LOG_ERROR, "Incorrenct parameter");
116 /* Create Frame view */
117 base = elm_layout_add(parent);
119 DBG(LOG_ERROR, "Failed to add layout");
123 elm_layout_theme_set(base, "layout", "application", "default");
124 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
125 evas_object_size_hint_align_set(base, EVAS_HINT_FILL, EVAS_HINT_FILL);
132 * This function let the ug destroy the main view
134 * @param[in] data the pointer to the main data structure
136 void destroy_wfd_ug_view(void *data)
139 struct ug_data *ugd = (struct ug_data *) data;
142 evas_object_del(ugd->genlist);
146 if (ugd->naviframe) {
147 evas_object_del(ugd->naviframe);
148 ugd->naviframe = NULL;
155 * This function let the ug initialize wfd when timeout
156 * @return if stop the timer, return false, else return true
157 * @param[in] data the pointer to the main data structure
159 gboolean _wfd_init_cb(void *data)
163 struct ug_data *ugd = (struct ug_data *) data;
165 if (ugd->wfd_status == WIFI_DIRECT_STATE_DEACTIVATED) {
166 res = init_wfd_client(ugd);
168 DBG(LOG_ERROR, "Failed to initialize WFD client library\n");
177 static void *on_create(ui_gadget_h ug, enum ug_mode mode, service_h service, void *priv)
190 bindtextdomain(PACKAGE, LOCALEDIR);
192 ugd->win = ug_get_window();
197 if (mode == UG_MODE_FULLVIEW) {
198 ugd->base = _create_fullview(ugd->win, ugd);
200 ugd->base = _create_frameview(ugd->win, ugd);
204 ugd->bg = _create_bg(ugd->win, "group_list");
205 elm_object_part_content_set(ugd->base, "elm.swallow.bg", ugd->bg);
207 DBG(LOG_ERROR, "Failed to create base layout\n");
211 /* check status of wifi-direct from vconf */
212 wfd_get_vconf_status(ugd);
215 * if not deactivated, do initialization at once;
216 * otherwise, do initialization later
218 if (ugd->wfd_status > WIFI_DIRECT_STATE_DEACTIVATED || service) {
219 res = init_wfd_client(ugd);
221 DBG(LOG_ERROR, "Failed to initialize WFD client library\n");
228 /* get the status from appsvc */
229 service_get_extra_data(service, "status", &data);
240 if (status == 0x01) {
241 wfd_client_switch_on(ugd);
242 } else if (status == 0x02) {
243 wfd_client_switch_off(ugd);
247 g_timeout_add(100, _wfd_init_cb, ugd);
250 if (ugd->wfd_status >= WIFI_DIRECT_STATE_ACTIVATED) {
251 wfd_ug_get_discovered_peers(ugd);
254 if (ugd->wfd_status >= WIFI_DIRECT_STATE_CONNECTED) {
255 wfd_ug_get_connected_peers(ugd);
258 if (ugd->wfd_status == WIFI_DIRECT_STATE_ACTIVATED) {
259 /* start discovery */
260 res = wifi_direct_start_discovery(FALSE, MAX_SCAN_TIME_OUT);
261 if (res != WIFI_DIRECT_ERROR_NONE) {
262 DBG(LOG_ERROR, "Failed to start discovery. [%d]\n", res);
263 ugd->is_re_discover = TRUE;
264 wifi_direct_cancel_discovery();
266 DBG(LOG_VERBOSE, "Discovery is started\n");
267 ugd->is_re_discover = FALSE;
272 initialize_gen_item_class();
273 create_wfd_ug_view(ugd);
274 wfd_ug_view_update_peers(ugd);
276 evas_object_show(ugd->base);
282 static void on_start(ui_gadget_h ug, service_h service, void *priv)
294 struct utsname kernel_info;
295 res = uname(&kernel_info);
297 DBG(LOG_ERROR, "Failed to detect target type\n");
299 DBG(LOG_VERBOSE, "HW ID of this device [%s]\n", kernel_info.machine);
300 if (strncmp(kernel_info.machine, "arm", 3) != 0) {
301 wfd_ug_warn_popup(ugd, _("IDS_WFD_POP_NOT_SUPPORTED_DEVICE"), POPUP_TYPE_TERMINATE);
309 static void on_pause(ui_gadget_h ug, service_h service, void *priv)
315 static void on_resume(ui_gadget_h ug, service_h service, void *priv)
321 static void on_destroy(ui_gadget_h ug, service_h service, void *priv)
326 DBG(LOG_ERROR, "The param is NULL\n");
330 struct ug_data *ugd = priv;
331 if (ugd == NULL || ugd->base == NULL) {
332 DBG(LOG_ERROR, "The param is NULL\n");
336 deinit_wfd_client(ugd);
337 DBG(LOG_VERBOSE, "WFD client deregistered");
339 destroy_wfd_ug_view(ugd);
340 DBG(LOG_VERBOSE, "Destroying About item");
342 wfd_ug_view_free_peers(ugd);
344 DBG(LOG_VERBOSE, "WFD client deregistered");
346 evas_object_del(ugd->bg);
349 DBG(LOG_VERBOSE, "WFD client deregistered");
352 evas_object_del(ugd->base);
360 static void on_message(ui_gadget_h ug, service_h msg, service_h service, void *priv)
366 static void on_event(ui_gadget_h ug, enum ug_event event, service_h service, void *priv)
371 DBG(LOG_ERROR, "The param is NULL\n");
376 case UG_EVENT_LOW_MEMORY:
377 DBG(LOG_VERBOSE, "UG_EVENT_LOW_MEMORY\n");
379 case UG_EVENT_LOW_BATTERY:
380 DBG(LOG_VERBOSE, "UG_EVENT_LOW_BATTERY\n");
382 case UG_EVENT_LANG_CHANGE:
383 DBG(LOG_VERBOSE, "UG_EVENT_LANG_CHANGE\n");
385 case UG_EVENT_ROTATE_PORTRAIT:
386 DBG(LOG_VERBOSE, "UG_EVENT_ROTATE_PORTRAIT\n");
388 case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
389 DBG(LOG_VERBOSE, "UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN\n");
391 case UG_EVENT_ROTATE_LANDSCAPE:
392 DBG(LOG_VERBOSE, "UG_EVENT_ROTATE_LANDSCAPE\n");
394 case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
395 DBG(LOG_VERBOSE, "UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN\n");
398 DBG(LOG_VERBOSE, "default\n");
405 static void on_key_event(ui_gadget_h ug, enum ug_key_event event, service_h service, void *priv)
410 DBG(LOG_ERROR, "The param is NULL\n");
415 case UG_KEY_EVENT_END:
416 DBG(LOG_VERBOSE, "UG_KEY_EVENT_END\n");
425 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
431 DBG(LOG_ERROR, "The param is NULL\n");
435 ugd = calloc(1, sizeof(struct ug_data));
437 DBG(LOG_ERROR, "Failed to allocate memory for UG data\n");
443 ops->create = on_create;
444 ops->start = on_start;
445 ops->pause = on_pause;
446 ops->resume = on_resume;
447 ops->destroy = on_destroy;
448 ops->message = on_message;
449 ops->event = on_event;
450 ops->key_event = on_key_event;
452 ops->opt = UG_OPT_INDICATOR_ENABLE;
458 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
465 DBG(LOG_ERROR, "The param is NULL\n");
478 UG_MODULE_API int setting_plugin_reset(service_h service, void *priv)
482 wifi_direct_state_e state;
484 res = wifi_direct_initialize();
485 if (res != WIFI_DIRECT_ERROR_NONE) {
486 DBG(LOG_ERROR, "Failed to initialize wifi direct. [%d]\n", res);
490 res = wifi_direct_get_state(&state);
491 if (res != WIFI_DIRECT_ERROR_NONE) {
492 DBG(LOG_ERROR, "Failed to get link status. [%d]\n", res);
496 if (state < WIFI_DIRECT_STATE_ACTIVATING) {
497 DBG(LOG_VERBOSE, "No need to reset Wi-Fi Direct.\n");
499 /*if connected, disconnect all devices*/
500 if (WIFI_DIRECT_STATE_CONNECTED == state) {
501 res = wifi_direct_disconnect_all();
502 if (res != WIFI_DIRECT_ERROR_NONE) {
503 DBG(LOG_ERROR, "Failed to send disconnection request to all. [%d]\n", res);
508 res = wifi_direct_deactivate();
509 if (res != WIFI_DIRECT_ERROR_NONE) {
510 DBG(LOG_ERROR, "Failed to reset Wi-Fi Direct. [%d]\n", res);
515 res = wifi_direct_deinitialize();
516 if (res != WIFI_DIRECT_ERROR_NONE) {
517 DBG(LOG_ERROR, "Failed to deinitialize wifi direct. [%d]\n", res);