apply FSL(Flora Software License)
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-client.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  * 
8  *    http://www.tizenopensource.org/license
9  * 
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.
15  */
16
17 /**
18  * This file implements wifi direct application client  functions.
19  *
20  * @file    wfd-app-client.c
21  * @author  Sungsik Jang (sungsik.jang@samsung.com)
22  * @version 0.1
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include "wifi-direct.h"
28 #include "wfd-app.h"
29 #include "wfd-app-util.h"
30
31
32 void _cb_activation(int error_code, wifi_direct_device_state_e device_state,
33                     void *user_data)
34 {
35     __WFD_APP_FUNC_ENTER__;
36
37     wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
38
39     switch (device_state)
40     {
41     case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
42         WFD_APP_LOG(WFD_APP_LOG_LOW,
43                     "event ------------------ WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
44         break;
45
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");
51         elm_exit();
52         break;
53
54     default:
55         break;
56     }
57
58     __WFD_APP_FUNC_EXIT__;
59
60 }
61
62
63 static wfd_device_info_t *_wfd_app_find_peer_by_mac_address(void *data,
64                                                           const char *mac_address)
65 {
66         __WFD_APP_FUNC_ENTER__;
67
68         wfd_appdata_t *ad = (wfd_appdata_t *) data;
69         
70         int i;
71
72         if (ad == NULL)
73         {
74                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Incorrect parameter(NULL)\n");
75                 return NULL;
76         }
77
78         WFD_APP_LOG(WFD_APP_LOG_LOW, "find peer by MAC [%s] \n", mac_address);
79
80         for (i = 0; i < ad->discovered_peer_count; i++)
81         {
82                 WFD_APP_LOG(WFD_APP_LOG_LOW, "check %dth peer\n", i);
83                 
84                 if (!strncmp(mac_address, (const char *) ad->discovered_peers[i].mac_address, 18))
85                 {
86                         WFD_APP_LOG(WFD_APP_LOG_LOW, "found peer. [%d]\n", i);
87                         __WFD_APP_FUNC_EXIT__;
88                         return &ad->discovered_peers[i];
89                 }
90         }
91     
92         __WFD_APP_FUNC_EXIT__;
93
94     return NULL;
95 }
96
97
98 bool _wfd_app_discoverd_peer_cb(wifi_direct_discovered_peer_info_s * peer,
99                             void *user_data)
100 {
101         __WFD_APP_FUNC_ENTER__;
102
103         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
104
105         if (NULL != peer->ssid)
106         {
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);
109         }
110         else
111         {
112                 WFD_APP_LOG(WFD_APP_LOG_LOW, "peer's ssid is NULL\n");
113         }
114
115         if (NULL != peer->mac_address)
116         {
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);
119         }
120         else
121         {
122                 WFD_APP_LOG(WFD_APP_LOG_LOW, "peer's mac is NULL\n");
123         }
124         
125         ad->discovered_peer_count++;
126
127         __WFD_APP_FUNC_EXIT__;
128         
129         return TRUE;
130
131 }
132
133
134 void _cb_discover(int error_code, wifi_direct_discovery_state_e discovery_state,
135                   void *user_data)
136 {
137     __WFD_APP_FUNC_ENTER__;
138
139     wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
140     int ret;
141
142     switch (discovery_state)
143     {
144         case WIFI_DIRECT_DISCOVERY_STARTED:
145                 {
146                         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_STARTED\n");
147                 }
148                 break;
149
150         case WIFI_DIRECT_ONLY_LISTEN_STARTED:
151                 {
152                         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_ONLY_LISTEN_STARTED\n");
153                 }
154                 break;
155
156         case WIFI_DIRECT_DISCOVERY_FINISHED:
157                 {
158                         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_FINISHED\n");
159                 }
160                 break;
161
162         case WIFI_DIRECT_DISCOVERY_FOUND:
163                 {
164                         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_FOUND\n");
165
166                         if (NULL != ad->discovered_peers)
167                                 free(ad->discovered_peers);
168
169                         ad->discovered_peers = calloc(10, sizeof(wfd_device_info_t));
170                         ad->discovered_peer_count = 0;
171
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);
175                 }
176                 break;
177
178         default:
179                 break;
180     }
181
182     __WFD_APP_FUNC_EXIT__;
183
184 }
185
186 void _cb_connection(int error_code,
187                     wifi_direct_connection_state_e connection_state,
188                     const char *mac_address, void *user_data)
189 {
190     __WFD_APP_FUNC_ENTER__;
191
192     wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
193     int result;
194
195     switch (connection_state)
196     {
197     case WIFI_DIRECT_CONNECTION_RSP:
198         {
199             WFD_APP_LOG(WFD_APP_LOG_LOW,
200                         "event ------------------ WIFI_DIRECT_CONNECTION_RSP\n");
201
202             if (error_code == WIFI_DIRECT_ERROR_NONE)
203             {
204                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Link Complete!\n");
205                 wfd_prepare_popup(WFD_POP_NOTI_CONNECTED, NULL);
206             }
207             else
208             {
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");
218
219                 wfd_prepare_popup(WFD_POP_FAIL_CONNECT, NULL);
220
221                 result = wifi_direct_start_discovery(FALSE, 0);
222                 WFD_APP_LOG(WFD_APP_LOG_LOW,
223                             "wifi_direct_start_discovery() result=[%d]\n",
224                             result);
225             }
226         }
227         break;
228
229     case WIFI_DIRECT_CONNECTION_WPS_REQ:
230         {
231             wifi_direct_config_data_s *config = NULL;
232
233             memcpy(ad->peer_mac, mac_address, sizeof(ad->peer_mac));
234
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",
240                         result);
241
242             if (config->wps_config == WIFI_DIRECT_WPS_PUSHBUTTON)
243             {
244                 WFD_APP_LOG(WFD_APP_LOG_LOW,
245                             "wps_config is WFD_WPS_PUSHBUTTON. Ignore it..\n");
246             }
247             else if (config->wps_config == WIFI_DIRECT_WPS_KEYPAD)
248             {
249                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_KEYPAD\n");
250
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",
254                             result);
255
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);
261
262                 strncpy(ad->pin_number, pin_number, 32);
263
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);
268
269                 result = wifi_direct_activate_pushbutton();
270                 wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
271
272                 if (pin_number != NULL)
273                     free(pin_number);
274             }
275             else if (config->wps_config == WIFI_DIRECT_WPS_DISPLAY)
276             {
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,
279                                   (void *) NULL);
280             }
281             else
282             {
283                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is unkown!\n");
284
285             }
286             if (config != NULL)
287                 free(config);
288         }
289         break;
290
291     case WIFI_DIRECT_CONNECTION_REQ:
292         {
293                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");
294
295                 wifi_direct_config_data_s *config = NULL;
296                 wfd_device_info_t *peer_info = NULL;
297
298                 if (NULL == mac_address)
299                 {
300                         WFD_APP_LOG(WFD_APP_LOG_LOW, "ERROR : incomming_peer_mac is NULL !!\n");
301                         return;
302                 }
303
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));
306
307                 peer_info = _wfd_app_find_peer_by_mac_address(ad, mac_address);
308
309                 if (NULL != peer_info->ssid)
310                 {
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));
313                 }
314                 else
315                 {
316                         WFD_APP_LOG(WFD_APP_LOG_LOW, "incomming_peer SSID is NULL !!\n");
317                 }
318
319                 if (ad->peer_name == NULL || strlen(ad->peer_name) == 0)
320                         strncpy(ad->peer_name, ad->peer_mac, strlen(ad->peer_mac));
321
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);
324
325                 if (config->wps_config == WIFI_DIRECT_WPS_PUSHBUTTON)
326                 {
327                         char pushbutton;
328                         WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_PUSHBUTTON\n");
329
330                         wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
331                 }
332                 else if (config->wps_config == WIFI_DIRECT_WPS_DISPLAY)
333                 {
334                         WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WFD_WPS_DISPLAY\n");
335
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);
338
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);
342
343                         strncpy(ad->pin_number, pin_number, 32);
344
345                         wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
346                         
347                         if (pin_number != NULL)
348                                 free(pin_number);
349                 }
350                 else if (config->wps_config == WIFI_DIRECT_WPS_KEYPAD)
351                 {
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);
354                 }
355                 else
356                 {
357                         WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is unkown!\n");
358                 }
359                 
360                 if (config != NULL)
361                         free(config);
362         }
363         break;
364
365     case WIFI_DIRECT_DISCONNECTION_IND:
366         {
367             WFD_APP_LOG(WFD_APP_LOG_LOW,
368                         "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
369         }
370         break;
371
372     case WIFI_DIRECT_DISCONNECTION_RSP:
373         {
374             wfd_destroy_popup();
375
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);
379         }
380         break;
381
382     default:
383         break;
384
385     }
386
387     __WFD_APP_FUNC_EXIT__;
388 }
389
390
391 int init_wfd_popup_client(wfd_appdata_t * ad)
392 {
393     __WFD_APP_FUNC_ENTER__;
394     int ret;
395
396     ret = wifi_direct_initialize();
397
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);
400     ret =
401         wifi_direct_set_connection_state_changed_cb(_cb_connection,
402                                                     (void *) ad);
403
404     __WFD_APP_FUNC_EXIT__;
405
406     if (ret)
407         return TRUE;
408     else
409         return FALSE;
410 }
411
412 int deinit_wfd_popup_client(void)
413 {
414     __WFD_APP_FUNC_ENTER__;
415
416     int ret;
417
418     ret = wifi_direct_deinitialize();
419
420     __WFD_APP_FUNC_EXIT__;
421
422     if (ret)
423         return TRUE;
424     else
425         return FALSE;
426 }