2 * Network Configuration Module
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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.
21 * This file implements wifi direct manager functions.
23 * @file wifi-direct-manager.c
24 * @author Gibyoung Kim (lastkgb.kim@samsung.com)
31 #include <sys/utsname.h>
36 #include <glib-object.h>
38 #include <wifi-direct.h>
40 #include "wifi-direct-ipc.h"
41 #include "wifi-direct-manager.h"
42 #include "wifi-direct-oem.h"
43 #include "wifi-direct-session.h"
44 #include "wifi-direct-group.h"
45 #include "wifi-direct-peer.h"
46 #include "wifi-direct-state.h"
47 #include "wifi-direct-event.h"
48 #include "wifi-direct-util.h"
49 #include "wifi-direct-log.h"
50 #include "wifi-direct-error.h"
51 #include "wifi-direct-iface.h"
52 #include "wifi-direct-dbus.h"
54 #if defined TIZEN_ENABLE_PRD
55 #include "wifi-direct-prd.h"
56 #endif /* TIZEN_ENABLE_PRD */
58 wfd_manager_s *g_manager;
60 wfd_manager_s *wfd_get_manager()
65 static int _wfd_local_init_device(wfd_manager_s *manager)
67 __WDS_LOG_FUNC_ENTER__;
68 wfd_device_s *local = NULL;
72 WDS_LOGE("Invalid parameter");
77 local = (wfd_device_s*) g_try_malloc0(sizeof(wfd_device_s));
79 WDS_LOGE("Failed to allocate memory for local device [%s]", strerror(errno));
83 res = wfd_util_get_phone_name(local->dev_name);
85 WDS_LOGE("Failed to get phone name of local device. Use default device name");
86 g_strlcpy(local->dev_name, DEFAULT_DEVICE_NAME, DEV_NAME_LEN + 1);
88 WDS_LOGD("Local Device name [%s]", local->dev_name);
89 wfd_util_set_dev_name_notification();
91 res = wfd_util_get_local_dev_mac(local->dev_addr);
93 WDS_LOGE("Failed to get local device MAC address");
95 memcpy(local->intf_addr, local->dev_addr, MACADDR_LEN);
96 local->intf_addr[4] ^= 0x80;
97 WDS_LOGD("Local Interface MAC address [" MACSECSTR "]",
98 MAC2SECSTR(local->intf_addr));
100 local->config_methods = WFD_WPS_MODE_PBC | WFD_WPS_MODE_DISPLAY | WFD_WPS_MODE_KEYPAD;
101 local->wps_mode = WFD_WPS_MODE_PBC;
102 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
103 local->services = NULL;
104 local->service_count = 0;
105 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
106 /* TODO: initialize other local device datas */
107 manager->local = local;
109 __WDS_LOG_FUNC_EXIT__;
113 static int _wfd_local_deinit_device(wfd_manager_s *manager)
115 __WDS_LOG_FUNC_ENTER__;
118 WDS_LOGE("Invalid parameter");
122 wfd_util_unset_dev_name_notification();
124 /* TODO: free member of local device */
125 g_free(manager->local);
127 __WDS_LOG_FUNC_EXIT__;
131 int wfd_local_reset_data(wfd_manager_s *manager)
133 __WDS_LOG_FUNC_ENTER__;
134 wfd_device_s *local = NULL;
137 WDS_LOGE("Invalid parameter");
141 local = manager->local;
142 /* init local device data */
143 local->dev_role = WFD_DEV_ROLE_NONE;
144 local->wps_mode = WFD_WPS_MODE_PBC;
145 memset(local->go_dev_addr, 0x0, MACADDR_LEN);
146 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
147 memset(&(local->display), 0x0, sizeof(wfd_display_s));
148 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
149 memset(local->ip_addr, 0x0, IPADDR_LEN);
151 __WDS_LOG_FUNC_EXIT__;
155 int wfd_local_get_dev_name(char *dev_name)
157 __WDS_LOG_FUNC_ENTER__;
158 wfd_device_s *local = g_manager->local;
161 WDS_LOGE("Invalid parameter");
162 __WDS_LOG_FUNC_EXIT__;
166 g_strlcpy(dev_name, local->dev_name, DEV_NAME_LEN + 1);
167 WDS_LOGD("Local device name [%s]", dev_name);
169 __WDS_LOG_FUNC_EXIT__;
173 int wfd_local_set_dev_name(char *dev_name)
175 __WDS_LOG_FUNC_ENTER__;
176 wfd_device_s *local = g_manager->local;
179 WDS_LOGE("Invalid parameter");
180 __WDS_LOG_FUNC_EXIT__;
184 g_strlcpy(local->dev_name, dev_name, DEV_NAME_LEN + 1);
186 if (g_manager->state >= WIFI_DIRECT_STATE_ACTIVATED) {
187 wfd_oem_set_dev_name(g_manager->oem_ops, dev_name);
188 WDS_LOGD("Device name changed.");
190 WDS_LOGE("Device name can't changed: state is %d", g_manager->state);
193 __WDS_LOG_FUNC_EXIT__;
197 int wfd_local_get_dev_mac(char *dev_mac)
199 __WDS_LOG_FUNC_ENTER__;
200 wfd_device_s *local = g_manager->local;
203 WDS_LOGE("Invalid parameter");
204 __WDS_LOG_FUNC_EXIT__;
208 g_snprintf(dev_mac, MACSTR_LEN, MACSTR, MAC2STR(local->dev_addr));
209 WDS_SECLOGD("Local device MAC address [%s]", dev_mac);
211 __WDS_LOG_FUNC_EXIT__;
216 int wfd_local_get_intf_mac(unsigned char *intf_mac)
218 __WDS_LOG_FUNC_ENTER__;
219 wfd_device_s *local = g_manager->local;
222 WDS_LOGE("Invalid parameter");
223 __WDS_LOG_FUNC_EXIT__;
227 g_snprintf(intf_mac, MACSTR_LEN, MACSTR, MAC2STR(local->intf_addr));
228 WDS_SECLOGD("Local interface MAC address [%s]", intf_mac);
230 __WDS_LOG_FUNC_EXIT__;
235 int wfd_local_get_ip_addr(char *ip_str)
237 __WDS_LOG_FUNC_ENTER__;
238 wfd_device_s *local = g_manager->local;
241 WDS_LOGE("Invalid parameter");
242 __WDS_LOG_FUNC_EXIT__;
246 snprintf(ip_str, IPSTR_LEN, IPSTR, IP2STR(local->ip_addr));
247 WDS_SECLOGD("Local IP address [" IPSECSTR "]", IP2SECSTR(local->ip_addr));
249 __WDS_LOG_FUNC_EXIT__;
253 int wfd_local_get_supported_wps_mode(int *config_methods)
255 __WDS_LOG_FUNC_ENTER__;
256 wfd_device_s *local = g_manager->local;
258 if (!config_methods) {
259 WDS_LOGE("Invalid parameter");
260 __WDS_LOG_FUNC_EXIT__;
264 *config_methods = local->config_methods;
265 WDS_LOGD("Local config method [0x%x]", *config_methods);
267 __WDS_LOG_FUNC_EXIT__;
271 int wfd_local_get_wps_mode(int *wps_mode)
273 __WDS_LOG_FUNC_ENTER__;
274 wfd_device_s *local = g_manager->local;
277 WDS_LOGE("Invalid parameter");
278 __WDS_LOG_FUNC_EXIT__;
282 *wps_mode = local->wps_mode;
283 WDS_LOGD("Local wps mode [0x%x]", *wps_mode);
285 __WDS_LOG_FUNC_EXIT__;
290 int wfd_local_set_wps_mode(int wps_mode)
292 __WDS_LOG_FUNC_ENTER__;
293 wfd_device_s *local = g_manager->local;
296 WDS_LOGE("Invalid parameter");
297 __WDS_LOG_FUNC_EXIT__;
301 local->wps_mode = wps_mode;
302 WDS_LOGD("Local wps mode [0x%x]", wps_mode);
304 __WDS_LOG_FUNC_EXIT__;
309 int wfd_manager_get_go_intent(int *go_intent)
311 __WDS_LOG_FUNC_ENTER__;
313 WDS_LOGE("Invalid parameter");
314 __WDS_LOG_FUNC_EXIT__;
318 *go_intent = g_manager->go_intent;
319 WDS_LOGD("Local GO intent [%d]", *go_intent);
321 __WDS_LOG_FUNC_EXIT__;
325 int wfd_manager_set_go_intent(int go_intent)
327 __WDS_LOG_FUNC_ENTER__;
329 if (go_intent < 0 || go_intent > 15) {
330 WDS_LOGE("Invalid parameter");
331 __WDS_LOG_FUNC_EXIT__;
335 g_manager->go_intent = go_intent;
336 if (g_manager->state >= WIFI_DIRECT_STATE_ACTIVATED)
337 wfd_oem_set_go_intent(g_manager->oem_ops, go_intent);
339 __WDS_LOG_FUNC_EXIT__;
343 int wfd_manager_get_max_station(int *max_station)
345 __WDS_LOG_FUNC_ENTER__;
348 WDS_LOGE("Invalid parameter");
349 __WDS_LOG_FUNC_EXIT__;
353 *max_station = g_manager->max_station;
354 WDS_LOGD("Local max station[%d]", *max_station);
356 __WDS_LOG_FUNC_EXIT__;
360 int wfd_manager_set_max_station(int max_station)
362 __WDS_LOG_FUNC_ENTER__;
364 if (max_station < 1) {
365 WDS_LOGE("Invalid parameter");
366 __WDS_LOG_FUNC_EXIT__;
370 g_manager->max_station = max_station;
372 __WDS_LOG_FUNC_EXIT__;
376 int wfd_manager_get_autoconnection(int *autoconnection)
378 __WDS_LOG_FUNC_ENTER__;
379 if (!autoconnection) {
380 WDS_LOGE("Invalid parameter");
381 __WDS_LOG_FUNC_EXIT__;
385 *autoconnection = g_manager->autoconnection;
386 WDS_LOGD("Local autoconnection [%s]", *autoconnection ? "TRUE" : "FALSE");
388 __WDS_LOG_FUNC_EXIT__;
392 int wfd_manager_set_autoconnection(int autoconnection)
394 __WDS_LOG_FUNC_ENTER__;
395 if (autoconnection < 0) {
396 WDS_LOGE("Invalid parameter");
397 __WDS_LOG_FUNC_EXIT__;
401 g_manager->autoconnection = autoconnection;
403 __WDS_LOG_FUNC_EXIT__;
407 int wfd_manager_get_req_wps_mode(int *req_wps_mode)
409 __WDS_LOG_FUNC_ENTER__;
412 WDS_LOGE("Invalid parameter");
413 __WDS_LOG_FUNC_EXIT__;
417 *req_wps_mode = g_manager->req_wps_mode;
418 WDS_LOGD("Requested wps mode [0x%x]", *req_wps_mode);
420 __WDS_LOG_FUNC_EXIT__;
424 int wfd_manager_set_req_wps_mode(int req_wps_mode)
426 __WDS_LOG_FUNC_ENTER__;
427 wfd_device_s *local = g_manager->local;
429 if (req_wps_mode != WIFI_DIRECT_WPS_TYPE_PBC &&
430 req_wps_mode != WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY &&
431 req_wps_mode != WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD) {
432 WDS_LOGE("Invalid parameter");
433 __WDS_LOG_FUNC_EXIT__;
437 g_manager->req_wps_mode = req_wps_mode;
438 WDS_LOGD("Requested wps mode [0x%x]", req_wps_mode);
439 if (req_wps_mode == WFD_WPS_MODE_DISPLAY)
440 local->wps_mode = WFD_WPS_MODE_KEYPAD;
441 else if (req_wps_mode == WFD_WPS_MODE_KEYPAD)
442 local->wps_mode = WFD_WPS_MODE_DISPLAY;
444 local->wps_mode = req_wps_mode;
446 __WDS_LOG_FUNC_EXIT__;
450 int wfd_manager_local_config_set(wfd_manager_s *manager)
452 __WDS_LOG_FUNC_ENTER__;
453 wfd_device_s *local = NULL;
457 WDS_LOGE("Invalid parameter");
458 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
461 local = manager->local;
463 local->wps_mode = WFD_WPS_MODE_PBC;
464 WDS_LOGD("Device name set as %s", local->dev_name);
465 res = wfd_oem_set_dev_name(manager->oem_ops, local->dev_name);
467 WDS_LOGE("Failed to set device name");
468 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
471 local->pri_dev_type = DEFAULT_PRIMARY_DEVICE_TYPE;
472 local->sec_dev_type = DEFAULT_SECONDARY_DEVICE_TYPE;
473 res = wfd_oem_set_dev_type(manager->oem_ops, local->pri_dev_type, local->sec_dev_type);
475 WDS_LOGE("Failed to set device type");
476 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
479 res = wfd_oem_set_go_intent(manager->oem_ops, manager->go_intent);
481 WDS_LOGE("Failed to set go intent");
482 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
485 return WIFI_DIRECT_ERROR_NONE;
488 int wfd_manager_activate(wfd_manager_s *manager)
490 __WDS_LOG_FUNC_ENTER__;
495 WDS_LOGE("Invalid parameter");
496 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
499 if (manager->state > WIFI_DIRECT_STATE_ACTIVATING) {
500 WDS_LOGE("Already activated");
504 if (manager->state == WIFI_DIRECT_STATE_ACTIVATING) {
505 WDS_LOGE("In progress");
506 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
509 res = wfd_util_wifi_direct_activatable();
511 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
513 wfd_state_get(manager, &prev_state);
514 wfd_state_set(manager, WIFI_DIRECT_STATE_ACTIVATING);
515 #if defined(TIZEN_WLAN_CONCURRENT_ENABLE)
516 res = wfd_util_check_wifi_state();
518 WDS_LOGE("Failed to get wifi state");
519 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
520 } else if (res == 0) {
521 #endif /* TIZEN_WLAN_CONCURRENT_ENABLE */
522 res = wfd_oem_activate(manager->oem_ops, 0);
524 WDS_LOGE("Failed to activate");
525 wfd_state_set(manager, prev_state);
526 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
528 #if defined(TIZEN_WLAN_CONCURRENT_ENABLE)
530 res = wfd_oem_activate(manager->oem_ops, res);
532 WDS_LOGE("Failed to activate");
533 wfd_state_set(manager, prev_state);
534 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
537 #endif /* TIZEN_WLAN_CONCURRENT_ENABLE */
538 WDS_LOGE("Succeeded to activate");
540 wfd_state_set(manager, WIFI_DIRECT_STATE_ACTIVATED);
541 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_ACTIVATED);
543 wfd_manager_local_config_set(manager);
544 wfd_util_set_country();
545 #ifdef TIZEN_FEATURE_DEFAULT_CONNECTION_AGENT
546 wfd_util_start_wifi_direct_popup();
547 #endif /* TIZEN_FEATURE_DEFAULT_CONNECTION_AGENT */
549 res = wfd_util_get_local_dev_mac(manager->local->dev_addr);
551 WDS_LOGE("Failed to get local device MAC address");
553 memcpy(manager->local->intf_addr, manager->local->dev_addr, MACADDR_LEN);
554 manager->local->intf_addr[4] ^= 0x80;
555 WDS_LOGD("Local Interface MAC address [" MACSECSTR "]",
556 MAC2SECSTR(manager->local->intf_addr));
558 __WDS_LOG_FUNC_EXIT__;
559 return WIFI_DIRECT_ERROR_NONE;
562 int wfd_manager_deactivate(wfd_manager_s *manager)
564 __WDS_LOG_FUNC_ENTER__;
567 wfd_group_s *group = NULL;
570 WDS_LOGE("Invalid parameter");
571 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
574 wfd_state_get(manager, &prev_state);
575 wfd_state_set(manager, WIFI_DIRECT_STATE_DEACTIVATING);
577 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
578 res = wfd_oem_miracast_init(manager->oem_ops, false);
580 WDS_LOGE("Failed to initialize miracast");
581 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
583 group = (wfd_group_s*) manager->group;
584 if (group && group->pending == FALSE) {
585 res = wfd_oem_destroy_group(manager->oem_ops, group->ifname);
587 WDS_LOGE("Failed to destroy group before deactivation");
590 #if defined(TIZEN_WLAN_CONCURRENT_ENABLE) && defined(TIZEN_PROFILE_MOBILE)
591 res = wfd_util_check_wifi_state();
593 WDS_LOGE("Failed to get wifi state");
594 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
595 } else if (res == 0) {
596 #endif /* TIZEN_WLAN_CONCURRENT_ENABLE && TIZEN_PROFILE_MOBILE */
597 res = wfd_oem_deactivate(manager->oem_ops, 0);
599 WDS_LOGE("Failed to deactivate");
600 wfd_state_set(manager, prev_state);
601 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
603 #if defined(TIZEN_WLAN_CONCURRENT_ENABLE) && defined(TIZEN_PROFILE_MOBILE)
605 /* FIXME: We should do something to stop p2p feature of Driver */
606 res = wfd_oem_deactivate(manager->oem_ops, res);
608 WDS_LOGE("Failed to deactivate");
609 wfd_state_set(manager, prev_state);
610 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
612 WDS_LOGE("Do not need to deactivate Wi-Fi");
614 #endif /* TIZEN_WLAN_CONCURRENT_ENABLE && TIZEN_PROFILE_MOBILE */
615 WDS_LOGE("Succeeded to deactivate");
617 wfd_state_set(manager, WIFI_DIRECT_STATE_DEACTIVATED);
618 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_DEACTIVATED);
620 manager->req_wps_mode = WFD_WPS_MODE_PBC;
622 wfd_destroy_group(manager);
623 wfd_destroy_session(manager);
624 wfd_peer_clear_all(manager);
625 wfd_local_reset_data(manager);
627 #ifdef TIZEN_FEATURE_DEFAULT_CONNECTION_AGENT
628 wfd_util_stop_wifi_direct_popup();
629 #endif /* TIZEN_FEATURE_DEFAULT_CONNECTION_AGENT */
630 __WDS_LOG_FUNC_EXIT__;
631 return WIFI_DIRECT_ERROR_NONE;
634 int wfd_manager_connect(wfd_manager_s *manager, unsigned char *peer_addr)
636 __WDS_LOG_FUNC_ENTER__;
637 wfd_session_s *session = NULL;
640 if (!manager || !peer_addr) {
641 WDS_LOGE("Invalid parameter");
642 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
645 if (manager->state != WIFI_DIRECT_STATE_ACTIVATED &&
646 manager->state != WIFI_DIRECT_STATE_DISCOVERING &&
647 manager->state != WIFI_DIRECT_STATE_GROUP_OWNER) {
648 __WDS_LOG_FUNC_EXIT__;
649 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
652 wfd_group_s *group = (wfd_group_s*) manager->group;
653 if (group && group->member_count >= manager->max_station) {
654 __WDS_LOG_FUNC_EXIT__;
655 return WIFI_DIRECT_ERROR_TOO_MANY_CLIENT;
658 session = (wfd_session_s*) manager->session;
659 if (session && session->type != SESSION_TYPE_INVITE) {
660 WDS_LOGE("Session already exist and it's not an invitation session");
661 __WDS_LOG_FUNC_EXIT__;
662 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
666 session = wfd_create_session(manager, peer_addr,
667 manager->req_wps_mode, SESSION_DIRECTION_OUTGOING);
669 WDS_LOGE("Failed to create new session");
670 __WDS_LOG_FUNC_EXIT__;
671 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
675 if (manager->local->dev_role == WFD_DEV_ROLE_GO &&
676 session->type != SESSION_TYPE_INVITE) {
677 session->type = SESSION_TYPE_INVITE;
678 res = wfd_session_invite(session);
680 res = wfd_session_start(session);
683 WDS_LOGE("Failed to start session");
684 wfd_destroy_session(manager);
685 __WDS_LOG_FUNC_EXIT__;
686 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
688 wfd_state_set(manager, WIFI_DIRECT_STATE_CONNECTING);
690 __WDS_LOG_FUNC_EXIT__;
691 return WIFI_DIRECT_ERROR_NONE;
694 #if defined(TIZEN_FEATURE_ASP)
695 int wfd_manager_asp_connect_session(wfd_manager_s *manager, void *params)
697 __WDS_LOG_FUNC_ENTER__;
698 wfd_session_s *session = NULL;
699 wfd_oem_asp_prov_s *prov_params = NULL;
700 int req_wps_mode = WFD_WPS_MODE_P2PS;
703 prov_params = (wfd_oem_asp_prov_s *)params;
704 if (!manager || !prov_params) {
705 WDS_LOGE("Invalid parameter");
706 __WDS_LOG_FUNC_EXIT__;
707 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
710 session = (wfd_session_s*) manager->session;
712 WDS_LOGE("Session already exists");
713 __WDS_LOG_FUNC_EXIT__;
714 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
717 if (prov_params->network_config == WFD_OEM_ASP_WPS_TYPE_PIN_DISPLAY)
718 req_wps_mode = WFD_WPS_MODE_DISPLAY;
719 else if (prov_params->network_config == WFD_OEM_ASP_WPS_TYPE_PIN_KEYPAD)
720 req_wps_mode = WFD_WPS_MODE_KEYPAD;
722 req_wps_mode = WFD_WPS_MODE_P2PS;
724 session = wfd_create_session(manager, prov_params->service_mac,
725 req_wps_mode, SESSION_DIRECTION_OUTGOING);
727 WDS_LOGE("Failed to create new session");
728 __WDS_LOG_FUNC_EXIT__;
729 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
732 res = wfd_session_asp_session_start(session, prov_params);
734 WDS_LOGE("Failed to start session");
735 wfd_destroy_session(manager);
736 __WDS_LOG_FUNC_EXIT__;
737 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
740 wfd_state_set(manager, WIFI_DIRECT_STATE_CONNECTING);
742 __WDS_LOG_FUNC_EXIT__;
743 return WIFI_DIRECT_ERROR_NONE;
746 int wfd_manager_asp_confirm_session(wfd_manager_s *manager, void *params, int confirmed)
748 __WDS_LOG_FUNC_ENTER__;
749 wfd_session_s *session = NULL;
750 wfd_oem_asp_prov_s *prov_params = NULL;
753 prov_params = (wfd_oem_asp_prov_s *)params;
754 if (!manager || !prov_params) {
755 WDS_LOGE("Invalid parameter");
756 __WDS_LOG_FUNC_EXIT__;
757 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
760 session = (wfd_session_s*) manager->session;
762 WDS_LOGE("Session not exists");
763 __WDS_LOG_FUNC_EXIT__;
764 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
768 WDS_LOGD("confirm session [%u] with peer [" MACSTR "]", prov_params->session_id,
769 MAC2STR(prov_params->session_mac));
771 WDS_LOGD("created session [%u] with peer [" MACSTR "]", session->session_id,
772 MAC2STR(session->session_mac));
774 if (session->session_id != prov_params->session_id ||
775 memcmp(&(session->session_mac), prov_params->session_mac, MACADDR_LEN) != 0) {
776 WDS_LOGE("Session MAC or ID not matched");
777 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
781 prov_params->status = 12;
783 prov_params->status = 11;
785 res = wfd_oem_asp_prov_disc_req(manager->oem_ops, prov_params);
787 WDS_LOGE("Failed to start session");
788 wfd_destroy_session(manager);
789 __WDS_LOG_FUNC_EXIT__;
790 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
792 wfd_state_set(manager, WIFI_DIRECT_STATE_CONNECTING);
794 __WDS_LOG_FUNC_EXIT__;
795 return WIFI_DIRECT_ERROR_NONE;
799 int wfd_manager_accept_connection(wfd_manager_s *manager, unsigned char *peer_addr)
801 __WDS_LOG_FUNC_ENTER__;
802 wfd_session_s *session = NULL;
803 wfd_device_s *peer = NULL;
806 if (!manager || !peer_addr) {
807 WDS_LOGE("Invalid parameter");
808 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
811 session = (wfd_session_s*) manager->session;
813 WDS_LOGE("Session not found");
814 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
817 peer = wfd_peer_find_by_dev_addr(manager, peer_addr);
819 WDS_LOGE("Peer is NULL");
820 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
823 if (memcmp(session->peer->dev_addr, peer_addr, MACADDR_LEN) != 0) {
824 WDS_LOGE("Peer and ongoing session peer are different");
825 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
828 if (manager->local->dev_role == WFD_DEV_ROLE_GO) {
829 WDS_LOGD("My device is GO and peer want to join my group, so WPS will be started");
830 res = wfd_session_wps(session);
831 } else if (peer->dev_role == WFD_DEV_ROLE_GO) {
832 WDS_LOGD("Peer device is GO, so Prov_Disc or Join will be started");
833 if (session->type == SESSION_TYPE_INVITE) {
834 if (session->state == SESSION_STATE_CREATED) {
835 WDS_LOGD("Invitation session. PD will be started");
836 res = wfd_session_start(session);
838 WDS_LOGD("Invitation session. Join will be started");
839 res = wfd_session_join(session);
842 if (manager->autoconnection && (manager->auto_pin[0] != 0))
843 g_strlcpy(session->wps_pin, manager->auto_pin, PINSTR_LEN + 1);
845 WDS_LOGD("Peer device is GO, so WPS will be started");
846 res = wfd_session_connect(session);
849 /* We should wait GO_NEGO_REQ from peer(MO) in autoconnection mode. */
850 /* Otherwise, GO Nego is sometimes failed. */
851 if (manager->autoconnection == FALSE) {
852 WDS_LOGD("My device is Device, so Negotiation will be started");
853 res = wfd_session_connect(session);
857 WDS_LOGE("Failed to start session");
858 if (manager->local->dev_role == WFD_DEV_ROLE_GO) {
859 wfd_state_set(manager, WIFI_DIRECT_STATE_GROUP_OWNER);
860 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_GROUP_OWNER);
862 wfd_state_set(manager, WIFI_DIRECT_STATE_ACTIVATED);
863 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_ACTIVATED);
865 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
867 wfd_state_set(manager, WIFI_DIRECT_STATE_CONNECTING);
869 __WDS_LOG_FUNC_EXIT__;
870 return WIFI_DIRECT_ERROR_NONE;
874 int wfd_manager_cancel_connection(wfd_manager_s *manager, unsigned char *peer_addr)
876 __WDS_LOG_FUNC_ENTER__;
877 wfd_group_s *group = NULL;
880 if (!manager || !peer_addr) {
881 WDS_LOGE("Invalid parameter");
882 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
885 if (!manager->session && manager->state != WIFI_DIRECT_STATE_CONNECTING) {
886 WDS_LOGE("It's not CONNECTING state");
887 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
890 res = wfd_session_cancel(manager->session, peer_addr);
892 WDS_LOGE("Failed to cancel session");
893 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
896 group = (wfd_group_s*) manager->group;
898 wfd_group_remove_member(group, peer_addr);
900 if (manager->local->dev_role == WFD_DEV_ROLE_GO) {
901 wfd_state_set(manager, WIFI_DIRECT_STATE_GROUP_OWNER);
902 if (group && group->member_count)
903 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_GROUP_OWNER);
905 wfd_state_set(manager, WIFI_DIRECT_STATE_ACTIVATED);
906 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_ACTIVATED);
909 __WDS_LOG_FUNC_EXIT__;
910 return WIFI_DIRECT_ERROR_NONE;
914 int wfd_manager_reject_connection(wfd_manager_s *manager, unsigned char *peer_addr)
916 __WDS_LOG_FUNC_ENTER__;
917 wfd_session_s *session = NULL;
920 if (!manager || !peer_addr) {
921 WDS_LOGE("Invalid parameter");
922 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
925 session = (wfd_session_s*) manager->session;
927 WDS_LOGE("Session not found");
928 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
931 if (manager->state != WIFI_DIRECT_STATE_CONNECTING) {
932 WDS_LOGE("It's not permitted with this state [%d]", manager->state);
933 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
936 if (session->direction != SESSION_DIRECTION_INCOMING) {
937 WDS_LOGE("Only incomming session can be rejected");
938 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
941 res = wfd_session_reject(session, peer_addr);
943 WDS_LOGE("Failed to reject connection");
944 /* TODO: check whether set state and break */
946 wfd_destroy_session(manager);
948 if (manager->local->dev_role == WFD_DEV_ROLE_GO) {
949 wfd_state_set(manager, WIFI_DIRECT_STATE_GROUP_OWNER);
950 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_GROUP_OWNER);
952 wfd_state_set(manager, WIFI_DIRECT_STATE_ACTIVATED);
953 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_ACTIVATED);
956 __WDS_LOG_FUNC_EXIT__;
957 return WIFI_DIRECT_ERROR_NONE;
961 int wfd_manager_disconnect(wfd_manager_s *manager, unsigned char *peer_addr)
963 __WDS_LOG_FUNC_ENTER__;
964 wfd_group_s *group = NULL;
965 wfd_device_s *peer = NULL;
969 WDS_LOGE("Invalid parameter");
970 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
974 WDS_LOGE("Invalid parameter");
975 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
978 group = (wfd_group_s*) manager->group;
980 WDS_LOGE("Group not found");
981 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
984 peer = wfd_group_find_member_by_addr(group, peer_addr);
986 WDS_LOGE("Connected peer not found");
987 return WIFI_DIRECT_ERROR_INVALID_PARAMETER;
990 wfd_state_set(manager, WIFI_DIRECT_STATE_DISCONNECTING);
992 if (manager->local->dev_role == WFD_DEV_ROLE_GO) {
994 res = wfd_oem_disconnect(manager->oem_ops, peer->dev_addr, 0);
996 res = wfd_oem_disconnect(manager->oem_ops, peer->intf_addr, 1);
998 res = wfd_oem_destroy_group(manager->oem_ops, group->ifname);
1002 WDS_LOGE("Failed to disconnect peer");
1003 res = WIFI_DIRECT_ERROR_OPERATION_FAILED;
1006 WDS_LOGE("Succeeded to disconnect peer");
1008 wfd_group_remove_member(group, peer_addr);
1009 if (!group->member_count) {
1010 wfd_oem_destroy_group(manager->oem_ops, group->ifname);
1011 wfd_destroy_group(manager);
1014 if (manager->local->dev_role == WFD_DEV_ROLE_GO) {
1015 wfd_state_set(manager, WIFI_DIRECT_STATE_GROUP_OWNER);
1016 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_GROUP_OWNER);
1018 wfd_state_set(manager, WIFI_DIRECT_STATE_ACTIVATED);
1019 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_ACTIVATED);
1022 __WDS_LOG_FUNC_EXIT__;
1023 return WIFI_DIRECT_ERROR_NONE;
1026 if (manager->local->dev_role == WFD_DEV_ROLE_GO) {
1027 wfd_state_set(manager, WIFI_DIRECT_STATE_GROUP_OWNER);
1028 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_GROUP_OWNER);
1030 wfd_state_set(manager, WIFI_DIRECT_STATE_CONNECTED);
1031 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_CONNECTED);
1034 __WDS_LOG_FUNC_EXIT__;
1038 int wfd_manager_disconnect_all(wfd_manager_s *manager)
1040 __WDS_LOG_FUNC_ENTER__;
1041 wfd_group_s *group = NULL;
1045 WDS_LOGE("Invalid parameter");
1046 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
1049 group = (wfd_group_s*) manager->group;
1051 WDS_LOGE("Group not found");
1052 return WIFI_DIRECT_ERROR_NOT_PERMITTED;
1055 wfd_state_set(manager, WIFI_DIRECT_STATE_DISCONNECTING);
1057 res = wfd_oem_destroy_group(manager->oem_ops, group->ifname);
1059 WDS_LOGE("Failed to destroy group");
1060 res = WIFI_DIRECT_ERROR_OPERATION_FAILED;
1063 WDS_LOGE("Succeeded to disconnect all peer");
1065 wfd_destroy_group(manager);
1067 wfd_state_set(manager, WIFI_DIRECT_STATE_ACTIVATED);
1068 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_ACTIVATED);
1070 __WDS_LOG_FUNC_EXIT__;
1071 return WIFI_DIRECT_ERROR_NONE;
1074 if (manager->local->dev_role == WFD_DEV_ROLE_GO) {
1075 wfd_state_set(manager, WIFI_DIRECT_STATE_GROUP_OWNER);
1076 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_GROUP_OWNER);
1078 wfd_state_set(manager, WIFI_DIRECT_STATE_CONNECTED);
1079 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_CONNECTED);
1082 __WDS_LOG_FUNC_EXIT__;
1086 int wfd_manager_get_peer_info(wfd_manager_s *manager, unsigned char *addr, wfd_discovery_entry_s **peer)
1088 __WDS_LOG_FUNC_ENTER__;
1089 wfd_device_s *peer_dev = NULL;
1090 wfd_discovery_entry_s *peer_info;
1091 wfd_oem_device_s *oem_dev = NULL;
1094 if (!manager || !addr) {
1095 WDS_LOGE("Invalid parameter");
1099 unsigned long time = 0;
1100 #if !(__GNUC__ <= 4 && __GNUC_MINOR__ < 8)
1101 wfd_util_get_current_time(&time);
1103 struct timeval tval;
1104 gettimeofday(&tval, NULL);
1107 WDS_LOGI("Current time [%ld]", time);
1109 res = wfd_oem_get_peer_info(manager->oem_ops, addr, &oem_dev);
1110 if (res < 0 || !oem_dev) {
1111 WDS_LOGE("Failed to get peer information");
1115 peer_dev = wfd_peer_find_by_addr(manager, addr);
1117 peer_dev = (wfd_device_s*) g_try_malloc0(sizeof(wfd_device_s));
1119 WDS_LOGE("Failed to allocate memory for peer device. [%s]", strerror(errno));
1123 memcpy(peer_dev->dev_addr, addr, MACADDR_LEN);
1124 manager->peers = g_list_prepend(manager->peers, peer_dev);
1125 manager->peer_count++;
1126 peer_dev->time = time;
1127 WDS_LOGD("peer_count[%d]", manager->peer_count);
1129 if (oem_dev->age > 30 && peer_dev->state == WFD_PEER_STATE_DISCOVERED) {
1130 WDS_LOGE("Too old age to update peer");
1136 g_strlcpy(peer_dev->dev_name, oem_dev->dev_name, DEV_NAME_LEN + 1);
1137 memcpy(peer_dev->intf_addr, oem_dev->intf_addr, MACADDR_LEN);
1138 memcpy(peer_dev->go_dev_addr, oem_dev->go_dev_addr, MACADDR_LEN);
1139 peer_dev->dev_role = oem_dev->dev_role;
1140 peer_dev->config_methods = oem_dev->config_methods;
1141 peer_dev->pri_dev_type = oem_dev->pri_dev_type;
1142 peer_dev->sec_dev_type = oem_dev->sec_dev_type;
1143 peer_dev->dev_flags = oem_dev->dev_flags;
1144 peer_dev->group_flags = oem_dev->group_flags;
1145 peer_dev->wps_mode = oem_dev->wps_mode;
1147 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
1148 memcpy(&(peer_dev->display), &(oem_dev->display), sizeof(wfd_display_s));
1149 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
1151 peer_dev->time = time;
1152 peer_dev->channel = oem_dev->channel;
1156 peer_info = (wfd_discovery_entry_s*) g_try_malloc0(sizeof(wfd_discovery_entry_s));
1158 WDS_LOGE("Failed to allocate memory for peer data. [%s]", strerror(errno));
1162 g_strlcpy(peer_info->device_name, peer_dev->dev_name, DEV_NAME_LEN + 1);
1163 memcpy(peer_info->mac_address, peer_dev->dev_addr, MACADDR_LEN);
1164 memcpy(peer_info->intf_address, peer_dev->intf_addr, MACADDR_LEN);
1165 peer_info->channel = peer_dev->channel;
1166 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1167 peer_info->services = 0;
1168 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1169 peer_info->is_group_owner = peer_dev->dev_role == WFD_DEV_ROLE_GO;
1170 peer_info->is_persistent_go = peer_dev->group_flags & WFD_OEM_GROUP_FLAG_PERSISTENT_GROUP;
1171 peer_info->is_connected = peer_dev->dev_role == WFD_DEV_ROLE_GC;
1172 peer_info->wps_device_pwd_id = 0;
1173 peer_info->wps_cfg_methods = peer_dev->config_methods;
1174 peer_info->category = peer_dev->pri_dev_type;
1175 peer_info->subcategory = peer_dev->sec_dev_type;
1177 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
1178 if (peer_dev->display.availability && peer_dev->display.port)
1179 peer_info->is_wfd_device = 1;
1180 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
1184 __WDS_LOG_FUNC_EXIT__;
1189 int wfd_manager_get_peers(wfd_manager_s *manager, wfd_discovery_entry_s **peers_data)
1191 __WDS_LOG_FUNC_ENTER__;
1193 wfd_device_s *peer = NULL;
1194 wfd_discovery_entry_s *peers = NULL;
1199 if (!manager || !peers_data) {
1200 WDS_LOGE("Invalid parameter");
1204 unsigned long time = 0;
1205 #if !(__GNUC__ <= 4 && __GNUC_MINOR__ < 8)
1206 wfd_util_get_current_time(&time);
1208 struct timeval tval;
1209 gettimeofday(&tval, NULL);
1212 WDS_LOGI("Current time [%ld]", time);
1214 peer_count = manager->peer_count;
1215 WDS_LOGI("peer count [%ld]", peer_count);
1218 else if (peer_count == 0)
1222 peers = (wfd_discovery_entry_s*) g_try_malloc0_n(peer_count, sizeof(wfd_discovery_entry_s));
1224 WDS_LOGE("Failed to allocate memory for peer data. [%s]", strerror(errno));
1228 temp = g_list_first(manager->peers);
1229 while (temp && count < peer_count) {
1233 if (peer->time + 8 < time) {
1234 WDS_LOGD("Device data is too old to report to application [%s]", peer->dev_name);
1235 res = wfd_update_peer(manager, peer);
1237 WDS_LOGE("This device is disappeared [%s]", peer->dev_name);
1238 temp = g_list_next(temp);
1239 manager->peers = g_list_remove(manager->peers, peer);
1240 manager->peer_count--;
1247 g_strlcpy(peers[count].device_name, peer->dev_name, DEV_NAME_LEN + 1);
1248 memcpy(peers[count].mac_address, peer->dev_addr, MACADDR_LEN);
1249 memcpy(peers[count].intf_address, peer->intf_addr, MACADDR_LEN);
1250 peers[count].channel = peer->channel;
1251 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1252 peers[count].services = 0;
1253 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1254 peers[count].is_group_owner = peer->dev_role == WFD_DEV_ROLE_GO;
1255 peers[count].is_persistent_go = peer->group_flags & WFD_OEM_GROUP_FLAG_PERSISTENT_GROUP;
1256 peers[count].is_connected = peer->dev_role == WFD_DEV_ROLE_GC;
1257 peers[count].wps_device_pwd_id = 0;
1258 peers[count].wps_cfg_methods = peer->config_methods;
1259 peers[count].category = peer->pri_dev_type;
1260 peers[count].subcategory = peer->sec_dev_type;
1262 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
1263 if (peer->display.availability && peer->display.port)
1264 peers[count].is_wfd_device = 1;
1265 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
1267 WDS_LOGD("%dth peer [%s]", count, peer->dev_name);
1269 temp = g_list_next(temp);
1272 WDS_LOGD("%d peers converted", count);
1273 WDS_LOGD("Final peer count is %d", manager->peer_count);
1275 *peers_data = peers;
1277 __WDS_LOG_FUNC_EXIT__;
1281 int wfd_manager_get_connected_peers(wfd_manager_s *manager, wfd_connected_peer_info_s **peers_data)
1283 __WDS_LOG_FUNC_ENTER__;
1284 wfd_connected_peer_info_s *peers = NULL;
1285 wfd_group_s *group = NULL;
1286 wfd_device_s *peer = NULL;
1291 if (!manager || !peers_data) {
1292 WDS_LOGE("Invalid parameter");
1296 group = manager->group;
1298 WDS_LOGE("Group not exist");
1302 peer_count = group->member_count;
1303 if (peer_count == 0) {
1304 WDS_LOGD("Member not exist");
1309 peers = (wfd_connected_peer_info_s*) g_try_malloc0_n(peer_count, sizeof(wfd_connected_peer_info_s));
1311 WDS_LOGE("Failed to allocate memory for connected peer data. [%s]", strerror(errno));
1315 temp = g_list_first(group->members);
1316 while (temp && count < group->member_count) {
1319 g_strlcpy(peers[count].device_name, peer->dev_name, DEV_NAME_LEN + 1);
1320 memcpy(peers[count].mac_address, peer->dev_addr, MACADDR_LEN);
1321 memcpy(peers[count].intf_address, peer->intf_addr, MACADDR_LEN);
1322 memcpy(peers[count].ip_address, peer->ip_addr, IPADDR_LEN);
1323 peers[count].category = peer->pri_dev_type;
1324 peers[count].subcategory = peer->sec_dev_type;
1325 peers[count].channel = peer->channel;
1326 peers[count].is_p2p = peer->is_p2p;
1327 #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY
1328 peers[count].services = 0;
1329 #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */
1331 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
1332 if (peer->display.availability && peer->display.port)
1333 peers[count].is_wfd_device = 1;
1335 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
1337 WDS_LOGD("%dth member converted[%s]", count, peers[count].device_name);
1340 temp = g_list_next(temp);
1343 WDS_LOGD("%d members converted", count);
1345 *peers_data = peers;
1347 __WDS_LOG_FUNC_EXIT__;
1351 #ifdef TIZEN_FEATURE_ASP
1352 wfd_device_s *wfd_manager_get_connected_peer_by_addr(wfd_manager_s *manager, unsigned char *peer_addr)
1354 __WDS_LOG_FUNC_ENTER__;
1355 wfd_device_s *peer = NULL;
1357 if (peer_addr == NULL) {
1358 WDS_LOGE("Invalid parameter");
1359 __WDS_LOG_FUNC_EXIT__;
1364 peer = wfd_group_find_member_by_addr(manager->group, peer_addr);
1366 __WDS_LOG_FUNC_EXIT__;
1369 #endif /* TIZEN_FEATURE_ASP */
1372 wfd_device_s *wfd_manager_find_connected_peer(wfd_manager_s *manager, unsigned char *peer_addr)
1374 __WDS_LOG_FUNC_ENTER__;
1375 wfd_device_s *peer = NULL;
1377 if (!manager || !peer_addr) {
1378 WDS_LOGE("Invalid parameter");
1382 peer = wfd_group_find_member_by_addr(manager->group, peer_addr);
1384 __WDS_LOG_FUNC_EXIT__;
1389 int wfd_manager_get_goup_ifname(char **ifname)
1391 __WDS_LOG_FUNC_ENTER__;
1392 wfd_group_s *group = g_manager->group;
1395 WDS_LOGE("Invalid parameter");
1396 __WDS_LOG_FUNC_EXIT__;
1401 WDS_LOGE("Group not exist");
1405 *ifname = group->ifname;
1407 __WDS_LOG_FUNC_EXIT__;
1411 #ifdef TIZEN_FEATURE_WIFI_DISPLAY
1412 int wfd_manager_set_display_device(int type, int port, int hdcp)
1414 __WDS_LOG_FUNC_ENTER__;
1415 wfd_device_s * device = g_manager->local;
1416 wfd_oem_display_s display;
1420 WDS_LOGE("Invalid parameter");
1421 __WDS_LOG_FUNC_EXIT__;
1425 memset(&display, 0x0, sizeof(wfd_oem_display_s));
1427 display.type = type;
1428 display.port = port;
1429 display.hdcp_support = hdcp;
1431 display.availability = device->display.availability;
1432 display.max_tput = device->display.max_tput;
1434 res = wfd_oem_set_display(g_manager->oem_ops, (wfd_oem_display_s*)&display);
1436 WDS_LOGE("Failed to set wifi display");
1440 device->display.type = type;
1441 device->display.port = port;
1442 device->display.hdcp_support = hdcp;
1444 __WDS_LOG_FUNC_EXIT__;
1448 int wfd_manager_set_session_availability(int availability)
1450 __WDS_LOG_FUNC_ENTER__;
1451 wfd_device_s * device = g_manager->local;
1452 wfd_oem_display_s display;
1456 WDS_LOGE("Invalid parameter");
1457 __WDS_LOG_FUNC_EXIT__;
1461 memset(&display, 0x0, sizeof(wfd_oem_display_s));
1463 display.availability = availability;
1465 display.type = device->display.type;
1466 display.hdcp_support = device->display.hdcp_support;
1467 display.port = device->display.port;
1468 display.max_tput = device->display.max_tput;
1470 res = wfd_oem_set_display(g_manager->oem_ops, (wfd_oem_display_s*)&display);
1472 WDS_LOGE("Failed to set wifi display session availability");
1476 device->display.availability = availability;
1478 __WDS_LOG_FUNC_EXIT__;
1482 #endif /* TIZEN_FEATURE_WIFI_DISPLAY */
1484 int wfd_manager_start_discovery(wfd_manager_s *manager, int mode, int timeout,
1485 const char* type, int channel)
1487 __WDS_LOG_FUNC_ENTER__;
1488 int res = WIFI_DIRECT_ERROR_OPERATION_FAILED;
1489 wfd_oem_scan_param_s param;
1490 memset(¶m, 0x0, sizeof(wfd_oem_scan_param_s));
1492 WDS_LOGI("Mode: [%d], Timeout: [%d], type: [%s]", mode, timeout, type, channel);
1494 if (manager->local->dev_role == WFD_DEV_ROLE_GO)
1495 param.scan_type = WFD_OEM_SCAN_TYPE_SOCIAL;
1497 if (channel == WFD_DISCOVERY_FULL_SCAN) {
1498 param.scan_type = WFD_OEM_SCAN_TYPE_FULL;
1499 } else if (channel == WFD_DISCOVERY_SOCIAL_CHANNEL) {
1500 param.scan_type = WFD_OEM_SCAN_TYPE_SOCIAL;
1501 } else if (channel == WFD_DISCOVERY_CHANNEL1) {
1502 param.scan_type = WFD_OEM_SCAN_TYPE_CHANNEL1;
1504 } else if (channel == WFD_DISCOVERY_CHANNEL6) {
1505 param.scan_type = WFD_OEM_SCAN_TYPE_CHANNEL6;
1507 } else if (channel == WFD_DISCOVERY_CHANNEL11) {
1508 param.scan_type = WFD_OEM_SCAN_TYPE_CHANNEL11;
1511 param.scan_type = WFD_OEM_SCAN_TYPE_SPECIFIC;
1512 param.freq = wfd_util_channel_to_freq(channel);
1516 param.scan_mode = WFD_OEM_SCAN_MODE_PASSIVE;
1518 param.scan_mode = WFD_OEM_SCAN_MODE_ACTIVE;
1520 param.scan_time = timeout;
1522 res = wfd_oem_start_scan(manager->oem_ops, ¶m);
1524 WDS_LOGE("Failed to start scan");
1525 __WDS_LOG_FUNC_EXIT__;
1526 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
1530 manager->scan_mode = WFD_SCAN_MODE_PASSIVE;
1532 manager->scan_mode = WFD_SCAN_MODE_ACTIVE;
1534 if (manager->local->dev_role != WFD_DEV_ROLE_GO) {
1535 wfd_state_set(manager, WIFI_DIRECT_STATE_DISCOVERING);
1536 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_DISCOVERING);
1539 WDS_LOGD("Succeeded to start scan");
1540 __WDS_LOG_FUNC_EXIT__;
1541 return WIFI_DIRECT_ERROR_NONE;
1544 int wfd_manager_cancel_discovery(wfd_manager_s *manager)
1546 __WDS_LOG_FUNC_ENTER__;
1549 res = wfd_oem_stop_scan(manager->oem_ops);
1551 WDS_LOGE("Failed to stop scan");
1552 __WDS_LOG_FUNC_EXIT__;
1553 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
1556 if (manager->local->dev_role == WFD_DEV_ROLE_GO) {
1557 wfd_state_set(manager, WIFI_DIRECT_STATE_GROUP_OWNER);
1558 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_GROUP_OWNER);
1560 wfd_state_set(manager, WIFI_DIRECT_STATE_ACTIVATED);
1561 wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_ACTIVATED);
1564 WDS_LOGD("Succeeded to stop scan");
1565 __WDS_LOG_FUNC_EXIT__;
1566 return WIFI_DIRECT_ERROR_NONE;
1569 wfd_device_s *wfd_manager_get_peer_by_addr(wfd_manager_s *manager, unsigned char *peer_addr)
1571 __WDS_LOG_FUNC_ENTER__;
1572 wfd_device_s *peer = NULL;
1574 peer = wfd_group_find_member_by_addr(manager->group, peer_addr);
1579 peer = wfd_peer_find_by_addr(manager, peer_addr);
1581 __WDS_LOG_FUNC_EXIT__;
1585 static wfd_manager_s *wfd_manager_init()
1587 __WDS_LOG_FUNC_ENTER__;
1588 wfd_manager_s *manager = NULL;
1591 manager = (wfd_manager_s*) g_try_malloc0(sizeof(wfd_manager_s));
1593 WDS_LOGE("Failed to allocate memory for wfd_manager structure");
1597 manager->go_intent = 7;
1598 manager->req_wps_mode = WFD_WPS_MODE_PBC;
1599 manager->max_station = 8;
1600 manager->session_timer = 120;
1601 manager->auto_group_remove_enable = TRUE;
1602 res = _wfd_local_init_device(manager);
1604 WDS_LOGE("Failed to initialize local device");
1606 return NULL; /* really stop manager? */
1608 WDS_LOGD("Succeeded to initialize local device");
1610 __WDS_LOG_FUNC_EXIT__;
1614 int wfd_manager_deinit(wfd_manager_s *manager)
1616 __WDS_LOG_FUNC_ENTER__;
1619 WDS_LOGE("Invalid parameter");
1620 __WDS_LOG_FUNC_EXIT__;
1624 _wfd_local_deinit_device(manager);
1628 __WDS_LOG_FUNC_EXIT__;
1632 static void *wfd_plugin_init(wfd_manager_s *manager)
1634 __WDS_LOG_FUNC_ENTER__;
1636 struct utsname kernel_info;
1640 WDS_LOGE("Invalid parameter");
1641 __WDS_LOG_FUNC_EXIT__;
1645 res = uname(&kernel_info);
1647 WDS_LOGE("Failed to detect target type");
1648 __WDS_LOG_FUNC_EXIT__;
1651 WDS_LOGD("Node name [%s], HW ID [%s]", kernel_info.nodename, kernel_info.machine);
1655 #if defined(TIZEN_ARCH_64)
1656 handle = dlopen(SUPPL_PLUGIN_64BIT_PATH, RTLD_NOW);
1658 handle = dlopen(SUPPL_PLUGIN_PATH, RTLD_NOW);
1661 WDS_LOGE("Failed to open shared object. [%s]", dlerror());
1662 __WDS_LOG_FUNC_EXIT__;
1667 int (*plugin_load)(wfd_oem_ops_s **ops) = NULL;
1668 plugin_load = (int (*)(wfd_oem_ops_s **ops)) dlsym(handle, "wfd_plugin_load");
1670 WDS_LOGE("Failed to load symbol. Error = [%s]", strerror(errno));
1672 __WDS_LOG_FUNC_EXIT__;
1676 wfd_oem_ops_s *temp_ops;
1677 (*plugin_load)(&temp_ops);
1678 manager->oem_ops = temp_ops;
1680 res = wfd_oem_init(temp_ops);
1682 WDS_LOGE("Failed to initialize OEM");
1684 __WDS_LOG_FUNC_EXIT__;
1687 WDS_LOGD("Succeeded to initialize OEM");
1689 __WDS_LOG_FUNC_EXIT__;
1693 static int wfd_plugin_deinit(wfd_manager_s *manager)
1695 __WDS_LOG_FUNC_ENTER__;
1697 if (!manager || !manager->plugin_handle) {
1698 WDS_LOGE("Invalid parameter");
1699 __WDS_LOG_FUNC_EXIT__;
1703 dlclose(manager->plugin_handle);
1704 manager->plugin_handle = NULL;
1706 __WDS_LOG_FUNC_EXIT__;
1710 #if defined TIZEN_ENABLE_PRD
1711 static void *wfd_prd_plugin_init(wfd_manager_s *manager)
1713 __WDS_LOG_FUNC_ENTER__;
1715 struct utsname kernel_info;
1719 WDS_LOGE("Invalid parameter");
1720 __WDS_LOG_FUNC_EXIT__;
1724 res = uname(&kernel_info);
1726 WDS_LOGE("Failed to detect target type");
1727 __WDS_LOG_FUNC_EXIT__;
1730 WDS_LOGD("Node name [%s], HW ID [%s]", kernel_info.nodename, kernel_info.machine);
1734 #if defined(TIZEN_ARCH_64)
1735 handle = dlopen(SUPPL_PRD_PLUGIN_64BIT_PATH, RTLD_NOW);
1737 handle = dlopen(SUPPL_PRD_PLUGIN_PATH, RTLD_NOW);
1740 WDS_LOGE("Failed to open shared object. [%s]", dlerror());
1741 __WDS_LOG_FUNC_EXIT__;
1746 int (*plugin_load)(wfd_oem_ops_s **ops) = NULL;
1747 plugin_load = (int (*)(wfd_oem_ops_s **ops)) dlsym(handle, "wfd_prd_plugin_load");
1749 WDS_LOGE("Failed to load symbol. Error = [%s]", strerror(errno));
1751 __WDS_LOG_FUNC_EXIT__;
1755 (*plugin_load)((wfd_oem_ops_s **)&manager->oem_ops);
1757 res = wfd_oem_prd_init((wfd_oem_ops_s *)manager->oem_ops);
1759 WDS_LOGE("Failed to initialize PRD OEM");
1761 __WDS_LOG_FUNC_EXIT__;
1764 WDS_LOGD("Succeeded to initialize PRD OEM");
1766 __WDS_LOG_FUNC_EXIT__;
1770 static int wfd_prd_plugin_deinit(wfd_manager_s *manager)
1772 __WDS_LOG_FUNC_ENTER__;
1774 if (!manager || !manager->prd_plugin_handle) {
1775 WDS_LOGE("Invalid parameter");
1776 __WDS_LOG_FUNC_EXIT__;
1780 dlclose(manager->prd_plugin_handle);
1781 manager->prd_plugin_handle = NULL;
1783 __WDS_LOG_FUNC_EXIT__;
1786 #endif /* TIZEN_ENABLE_PRD */
1788 int main(int argc, char *argv[])
1790 __WDS_LOG_FUNC_ENTER__;
1791 GMainLoop *main_loop = NULL;
1793 #if !GLIB_CHECK_VERSION(2, 32, 0)
1794 if (!g_thread_supported())
1795 g_thread_init(NULL);
1798 #if !GLIB_CHECK_VERSION(2, 36, 0)
1802 /* TODO: Parsing argument */
1803 /* Wi-Fi direct connection for S-Beam can be optimized using argument */
1806 * wfd-manager initialization
1808 g_manager = wfd_manager_init();
1810 WDS_LOGE("Failed to initialize wifi-direct manager");
1811 __WDS_LOG_FUNC_EXIT__;
1814 WDS_LOGD("Succeeded to initialize manager");
1817 * wfd_manager_plugin initialization
1819 g_manager->plugin_handle = wfd_plugin_init(g_manager);
1820 if (!g_manager->plugin_handle) {
1821 WDS_LOGE("Failed to initialize plugin");
1822 wfd_manager_deinit(g_manager);
1823 __WDS_LOG_FUNC_EXIT__;
1826 WDS_LOGD("Succeeded to load plugin");
1828 #if defined TIZEN_ENABLE_PRD
1830 * wfd_manager_prd_plugin initialization
1832 g_manager->prd_plugin_handle = wfd_prd_plugin_init(g_manager);
1833 if (!g_manager->prd_plugin_handle)
1834 WDS_LOGW("Failed to initialize prd plugin");
1836 WDS_LOGD("Succeeded to load plugin");
1837 #endif /* TIZEN_ENABLE_PRD */
1839 if (!wfd_manager_dbus_init()) {
1840 WDS_LOGE("Failed to DBus");
1841 wfd_plugin_deinit(g_manager);
1842 wfd_manager_deinit(g_manager);
1843 __WDS_LOG_FUNC_EXIT__;
1847 main_loop = g_main_loop_new(NULL, FALSE);
1848 if (main_loop == NULL) {
1849 WDS_LOGE("Failed to create GMainLoop structure");
1850 __WDS_LOG_FUNC_EXIT__;
1853 g_manager->main_loop = main_loop;
1854 g_main_loop_run(main_loop);
1856 wfd_manager_dbus_unregister();
1857 wfd_manager_dbus_deinit();
1859 #if defined TIZEN_ENABLE_PRD
1860 wfd_prd_plugin_deinit(g_manager);
1861 #endif /* TIZEN_ENABLE_PRD */
1863 wfd_plugin_deinit(g_manager);
1864 wfd_manager_deinit(g_manager);
1866 __WDS_LOG_FUNC_EXIT__;