2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * This file implements wifi direct application client functions.
20 * @file wfd-app-client.c
21 * @author Sungsik Jang (sungsik.jang@samsung.com)
27 #include "wifi-direct.h"
29 #include "wfd-app-util.h"
32 void _cb_activation(int error_code, wifi_direct_device_state_e device_state,
35 __WFD_APP_FUNC_ENTER__;
37 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
41 case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
42 WFD_APP_LOG(WFD_APP_LOG_LOW,
43 "event ------------------ WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
46 case WIFI_DIRECT_DEVICE_STATE_DEACTIVATED:
47 WFD_APP_LOG(WFD_APP_LOG_LOW,
48 "event ------------------ WIFI_DIRECT_DEVICE_STATE_DEACTIVATED\n");
49 WFD_APP_LOG(WFD_APP_LOG_LOW,
50 "Termination process of wifi-direct popup begins...\n");
58 __WFD_APP_FUNC_EXIT__;
63 static wfd_device_info_t *_wfd_app_find_peer_by_mac_address(void *data,
64 const char *mac_address)
66 __WFD_APP_FUNC_ENTER__;
68 wfd_appdata_t *ad = (wfd_appdata_t *) data;
74 WFD_APP_LOG(WFD_APP_LOG_LOW, "Incorrect parameter(NULL)\n");
78 WFD_APP_LOG(WFD_APP_LOG_LOW, "find peer by MAC [%s] \n", mac_address);
80 for (i = 0; i < ad->discovered_peer_count; i++)
82 WFD_APP_LOG(WFD_APP_LOG_LOW, "check %dth peer\n", i);
84 if (!strncmp(mac_address, (const char *) ad->discovered_peers[i].mac_address, 18))
86 WFD_APP_LOG(WFD_APP_LOG_LOW, "found peer. [%d]\n", i);
87 __WFD_APP_FUNC_EXIT__;
88 return &ad->discovered_peers[i];
92 __WFD_APP_FUNC_EXIT__;
98 bool _wfd_app_discoverd_peer_cb(wifi_direct_discovered_peer_info_s * peer,
101 __WFD_APP_FUNC_ENTER__;
103 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
105 if (NULL != peer->ssid)
107 WFD_APP_LOG(WFD_APP_LOG_LOW, "discovered peer ssid[%s]\n", peer->ssid);
108 strncpy(ad->discovered_peers[ad->discovered_peer_count].ssid, peer->ssid, 32);
112 WFD_APP_LOG(WFD_APP_LOG_LOW, "peer's ssid is NULL\n");
115 if (NULL != peer->mac_address)
117 WFD_APP_LOG(WFD_APP_LOG_LOW, "discovered peer mac[%s]\n", peer->mac_address);
118 strncpy(ad->discovered_peers[ad->discovered_peer_count].mac_address, peer->mac_address, 18);
122 WFD_APP_LOG(WFD_APP_LOG_LOW, "peer's mac is NULL\n");
125 ad->discovered_peer_count++;
127 __WFD_APP_FUNC_EXIT__;
134 void _cb_discover(int error_code, wifi_direct_discovery_state_e discovery_state,
137 __WFD_APP_FUNC_ENTER__;
139 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
142 switch (discovery_state)
144 case WIFI_DIRECT_DISCOVERY_STARTED:
146 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_STARTED\n");
150 case WIFI_DIRECT_ONLY_LISTEN_STARTED:
152 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_ONLY_LISTEN_STARTED\n");
156 case WIFI_DIRECT_DISCOVERY_FINISHED:
158 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_FINISHED\n");
162 case WIFI_DIRECT_DISCOVERY_FOUND:
164 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_FOUND\n");
166 if (NULL != ad->discovered_peers)
167 free(ad->discovered_peers);
169 ad->discovered_peers = calloc(10, sizeof(wfd_device_info_t));
170 ad->discovered_peer_count = 0;
172 ret = wifi_direct_foreach_discovered_peers(_wfd_app_discoverd_peer_cb, (void *) ad);
173 if (ret != WIFI_DIRECT_ERROR_NONE)
174 WFD_APP_LOG(WFD_APP_LOG_LOW, "get discovery result failed: %d\n", ret);
182 __WFD_APP_FUNC_EXIT__;
186 void _cb_connection(int error_code,
187 wifi_direct_connection_state_e connection_state,
188 const char *mac_address, void *user_data)
190 __WFD_APP_FUNC_ENTER__;
192 wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
195 switch (connection_state)
197 case WIFI_DIRECT_CONNECTION_RSP:
199 WFD_APP_LOG(WFD_APP_LOG_LOW,
200 "event ------------------ WIFI_DIRECT_CONNECTION_RSP\n");
202 if (error_code == WIFI_DIRECT_ERROR_NONE)
204 WFD_APP_LOG(WFD_APP_LOG_LOW, "Link Complete!\n");
205 wfd_prepare_popup(WFD_POP_NOTI_CONNECTED, NULL);
209 if (error_code == WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT)
210 WFD_APP_LOG(WFD_APP_LOG_LOW,
211 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT\n");
212 else if (error_code == WIFI_DIRECT_ERROR_AUTH_FAILED)
213 WFD_APP_LOG(WFD_APP_LOG_LOW,
214 "Error Code - WIFI_DIRECT_ERROR_AUTH_FAILED\n");
215 else if (error_code == WIFI_DIRECT_ERROR_CONNECTION_FAILED)
216 WFD_APP_LOG(WFD_APP_LOG_LOW,
217 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_FAILED\n");
219 wfd_prepare_popup(WFD_POP_FAIL_CONNECT, NULL);
221 result = wifi_direct_start_discovery(FALSE, 0);
222 WFD_APP_LOG(WFD_APP_LOG_LOW,
223 "wifi_direct_start_discovery() result=[%d]\n",
229 case WIFI_DIRECT_CONNECTION_WPS_REQ:
231 wifi_direct_config_data_s *config = NULL;
233 memcpy(ad->peer_mac, mac_address, sizeof(ad->peer_mac));
235 WFD_APP_LOG(WFD_APP_LOG_LOW,
236 "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
237 result = wifi_direct_get_config_data(&config);
238 WFD_APP_LOG(WFD_APP_LOG_LOW,
239 "wifi_direct_client_get_config_data() result=[%d]\n",
242 if (config->wps_config == WIFI_DIRECT_WPS_PUSHBUTTON)
244 WFD_APP_LOG(WFD_APP_LOG_LOW,
245 "wps_config is WFD_WPS_PUSHBUTTON. Ignore it..\n");
247 else if (config->wps_config == WIFI_DIRECT_WPS_KEYPAD)
249 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_KEYPAD\n");
251 result = wifi_direct_generate_wps_pin();
252 WFD_APP_LOG(WFD_APP_LOG_LOW,
253 "wifi_direct_client_generate_wps_pin() result=[%d]\n",
256 char *pin_number = NULL;
257 result = wifi_direct_get_wps_pin(&pin_number);
258 WFD_APP_LOG(WFD_APP_LOG_LOW,
259 "wifi_direct_client_get_wps_pin() result=[%d]. pin=[%s]\n",
260 result, ad->pin_number);
262 strncpy(ad->pin_number, pin_number, 32);
264 result = wifi_direct_accept_connection(ad->peer_mac);
265 WFD_APP_LOG(WFD_APP_LOG_LOW,
266 "wifi_direct_accept_connection[%s] result=[%d].\n",
267 ad->peer_mac, result);
269 result = wifi_direct_activate_pushbutton();
270 wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
272 if (pin_number != NULL)
275 else if (config->wps_config == WIFI_DIRECT_WPS_DISPLAY)
277 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_DISPLAY\n");
278 wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD,
283 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is unkown!\n");
291 case WIFI_DIRECT_CONNECTION_REQ:
293 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");
295 wifi_direct_config_data_s *config = NULL;
296 wfd_device_info_t *peer_info = NULL;
298 if (NULL == mac_address)
300 WFD_APP_LOG(WFD_APP_LOG_LOW, "ERROR : incomming_peer_mac is NULL !!\n");
304 WFD_APP_LOG(WFD_APP_LOG_LOW, "Connection Request from MAC[%s]\n", mac_address);
305 strncpy(ad->peer_mac, mac_address, strlen(mac_address));
307 peer_info = _wfd_app_find_peer_by_mac_address(ad, mac_address);
309 if (NULL != peer_info->ssid)
311 WFD_APP_LOG(WFD_APP_LOG_LOW, "Connection Request from SSID[%s]\n", peer_info->ssid);
312 strncpy(ad->peer_name, peer_info->ssid, strlen(peer_info->ssid));
316 WFD_APP_LOG(WFD_APP_LOG_LOW, "incomming_peer SSID is NULL !!\n");
319 if (ad->peer_name == NULL || strlen(ad->peer_name) == 0)
320 strncpy(ad->peer_name, ad->peer_mac, strlen(ad->peer_mac));
322 result = wifi_direct_get_config_data(&config);
323 WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_client_get_config_data() result=[%d]\n", result);
325 if (config->wps_config == WIFI_DIRECT_WPS_PUSHBUTTON)
328 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_PUSHBUTTON\n");
330 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
332 else if (config->wps_config == WIFI_DIRECT_WPS_DISPLAY)
334 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_DISPLAY\n");
336 result = wifi_direct_generate_wps_pin();
337 WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_client_generate_wps_pin() result=[%d]\n", result);
339 char *pin_number = NULL;
340 result = wifi_direct_get_wps_pin(&pin_number);
341 WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_get_wps_pin() result=[%d]\n", result);
343 strncpy(ad->pin_number, pin_number, 32);
345 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
347 if (pin_number != NULL)
350 else if (config->wps_config == WIFI_DIRECT_WPS_KEYPAD)
352 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_KEYPAD\n");
353 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, (void *) NULL);
357 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is unkown!\n");
365 case WIFI_DIRECT_DISCONNECTION_IND:
367 WFD_APP_LOG(WFD_APP_LOG_LOW,
368 "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
372 case WIFI_DIRECT_DISCONNECTION_RSP:
376 result = wifi_direct_start_discovery(FALSE, 0);
377 WFD_APP_LOG(WFD_APP_LOG_LOW,
378 "wifi_direct_start_discovery() result=[%d]\n", result);
387 __WFD_APP_FUNC_EXIT__;
391 int init_wfd_popup_client(wfd_appdata_t * ad)
393 __WFD_APP_FUNC_ENTER__;
396 ret = wifi_direct_initialize();
398 ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void *) ad);
399 ret = wifi_direct_set_discovery_state_changed_cb(_cb_discover, (void *) ad);
401 wifi_direct_set_connection_state_changed_cb(_cb_connection,
404 __WFD_APP_FUNC_EXIT__;
412 int deinit_wfd_popup_client(void)
414 __WFD_APP_FUNC_ENTER__;
418 ret = wifi_direct_deinitialize();
420 __WFD_APP_FUNC_EXIT__;