3fbbbdbf415e3646e655b4f827ab57d98779bc01
[apps/home/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-client.c
1 /*
2 *  WiFi-Direct UG
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 /**
21  * This file implements wifi direct application client  functions.
22  *
23  * @file    wfd-app-client.c
24  * @author  Sungsik Jang (sungsik.jang@samsung.com)
25  * @version 0.1
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "wifi-direct.h"
32 #include "wfd-app.h"
33 #include "wfd-app-util.h"
34 #include "vconf.h"
35
36
37 bool _wfd_connected_peer_cb(wifi_direct_connected_peer_info_s *peer, void *user_data)
38 {
39         __WFD_APP_FUNC_ENTER__;
40
41         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
42         if (NULL == ad || NULL == peer || NULL == peer->ssid || NULL == peer->mac_address) {
43                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameters.\n");
44                 return FALSE;
45         }
46
47         int peer_cnt = ad->raw_connected_peer_cnt;
48         WFD_APP_LOG(WFD_APP_LOG_LOW, "%dth connected peer. [%s]\n", peer_cnt, peer->ssid);
49
50         strncpy(ad->raw_connected_peers[peer_cnt].ssid, peer->ssid, sizeof(ad->raw_connected_peers[peer_cnt].ssid));
51         strncpy(ad->raw_connected_peers[peer_cnt].mac_address, peer->mac_address, WFD_MAC_ADDRESS_SIZE);
52         WFD_APP_LOG(WFD_APP_LOG_LOW, "\tSSID: [%s]\n", ad->raw_connected_peers[peer_cnt].ssid);
53         ad->raw_connected_peer_cnt++;
54
55         free(peer->ssid);
56         free(peer->mac_address);
57         free(peer);
58
59         __WFD_APP_FUNC_EXIT__;
60         return TRUE;
61 }
62
63 int _wfd_app_get_connected_peers(void *user_data)
64 {
65         __WFD_APP_FUNC_ENTER__;
66
67         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
68         if (NULL == ad) {
69                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameters.\n");
70                 return -1;
71         }
72
73         int res = 0;
74
75         ad->raw_connected_peer_cnt = 0;
76         res = wifi_direct_foreach_connected_peers(_wfd_connected_peer_cb, (void *)ad);
77         if (res != WIFI_DIRECT_ERROR_NONE) {
78                 ad->raw_connected_peer_cnt = 0;
79                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Get connected peer failed: %d\n", res);
80         }
81
82         __WFD_APP_FUNC_EXIT__;
83         return 0;
84 }
85
86 void _del_wfd_notification()
87 {
88         __WFD_APP_FUNC_ENTER__;
89
90         /* delete the notification */
91         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
92         noti_err  = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
93         if(noti_err != NOTIFICATION_ERROR_NONE) {
94                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_delete_all_by_type.(%d)\n", noti_err);
95                 return;
96         }
97
98         __WFD_APP_FUNC_EXIT__;
99 }
100
101 #if 0
102 void _add_wfd_actived_notification(void *user_data)
103 {
104         __WFD_APP_FUNC_ENTER__;
105
106         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
107         if (NULL == ad || NULL == ad->noti) {
108                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameters.\n");
109                 return;
110         }
111
112         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
113
114         /* set the icon */
115         noti_err = notification_set_image(ad->noti, NOTIFICATION_IMAGE_TYPE_ICON,  RESDIR"/images/A09_notification_icon.png");
116         if (noti_err != NOTIFICATION_ERROR_NONE) {
117                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_image. (%d)\n", noti_err);
118                 return;
119         }
120
121         /* set the title and content */
122         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_TITLE,
123                 "Wi-Fi Direct activated", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
124         if (noti_err != NOTIFICATION_ERROR_NONE) {
125                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_text. (%d)\n", noti_err);
126                 return;
127         }
128
129         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_CONTENT,
130                 "Tap to change settings", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
131         if (noti_err != NOTIFICATION_ERROR_NONE) {
132                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_text. (%d)\n", noti_err);
133                 return;
134         }
135
136         bundle *b = NULL;
137         b = bundle_create();
138         appsvc_set_pkgname(b, PACKAGE);
139         appsvc_add_data(b, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
140
141         int res = NOTIFICATION_ERROR_NONE;
142         res = notification_set_execute_option(ad->noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, /*Button Text*/NULL, NULL, b);
143         if (res != NOTIFICATION_ERROR_NONE) {
144                 WFD_APP_LOG(WFD_APP_LOG_LOW,"Failed to notification_set_execute_option. [%d]", res);
145                 return;
146         }
147
148         bundle_free(b);
149
150         /* set display application list */
151         noti_err = notification_set_display_applist(ad->noti, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
152         if(noti_err != NOTIFICATION_ERROR_NONE) {
153                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_display_applist : %d\n", noti_err);
154                 return;
155         }
156
157         /* notify the quick panel */
158         if (0 == ad->is_insert) {
159                 noti_err = notification_insert(ad->noti, NULL);
160                 if (noti_err != NOTIFICATION_ERROR_NONE) {
161                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_insert.(%d)\n", noti_err);
162                         return;
163                 }
164
165                 ad->is_insert= 1;
166         } else {
167                 noti_err  = notification_update(ad->noti);
168                 if (noti_err != NOTIFICATION_ERROR_NONE) {
169                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_update. (%d)\n", noti_err);
170                         return;
171                 }
172         }
173
174         __WFD_APP_FUNC_EXIT__;
175 }
176 #endif
177
178 void _add_wfd_peers_connected_notification(void *user_data)
179 {
180         __WFD_APP_FUNC_ENTER__;
181
182         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
183         if (NULL == ad || NULL == ad->noti) {
184                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameters.\n");
185                 return;
186         }
187
188         char msg[WFD_MAX_SIZE] = {0};
189         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
190
191         /* delete all notifications */
192         _del_wfd_notification();
193
194         /* set the icon */
195         noti_err = notification_set_image(ad->noti, NOTIFICATION_IMAGE_TYPE_ICON,  RESDIR"/images/A09_notification_icon.png");
196         if (noti_err != NOTIFICATION_ERROR_NONE) {
197                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_image. (%d)\n", noti_err);
198                 return;
199         }
200
201         /* set the title and content */
202         _wfd_app_get_connected_peers(ad);
203         snprintf(msg, WFD_MAX_SIZE, "Connected with %d devices via Wi-Fi Direct", ad->raw_connected_peer_cnt);
204         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_TITLE, msg, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
205         if (noti_err != NOTIFICATION_ERROR_NONE) {
206                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_text. (%d)\n", noti_err);
207                 return;
208         }
209
210         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_CONTENT,
211                 "Tap to change settings", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
212         if (noti_err != NOTIFICATION_ERROR_NONE) {
213                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_text. (%d)\n", noti_err);
214                 return;
215         }
216
217         bundle *b = NULL;
218         b = bundle_create();
219         appsvc_set_pkgname(b, PACKAGE);
220         appsvc_add_data(b, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
221
222         int res = NOTIFICATION_ERROR_NONE;
223         res = notification_set_execute_option(ad->noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, /*Button Text*/NULL, NULL, b);
224         if (res != NOTIFICATION_ERROR_NONE) {
225                 WFD_APP_LOG(WFD_APP_LOG_LOW,"Failed to notification_set_execute_option. [%d]", res);
226                 return;
227         }
228
229         bundle_free(b);
230
231         /* set display application list */
232         noti_err = notification_set_display_applist(ad->noti, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
233         if(noti_err != NOTIFICATION_ERROR_NONE) {
234                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_display_applist : %d\n", noti_err);
235                 return;
236         }
237
238         /* notify the quick panel */
239         noti_err = notification_insert(ad->noti, NULL);
240         if (noti_err != NOTIFICATION_ERROR_NONE) {
241                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_insert.(%d)\n", noti_err);
242                 return;
243         }
244
245         __WFD_APP_FUNC_EXIT__;
246 }
247
248 void _add_wfd_turn_off_notification(void *user_data)
249 {
250         __WFD_APP_FUNC_ENTER__;
251
252         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
253         if (NULL == ad || NULL == ad->noti) {
254                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameters.\n");
255                 return;
256         }
257
258         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
259
260         /* delete all notifications */
261         _del_wfd_notification();
262
263         /* set the icon */
264         noti_err = notification_set_image(ad->noti, NOTIFICATION_IMAGE_TYPE_ICON,  RESDIR"/images/A09_notification_icon.png");
265         if (noti_err != NOTIFICATION_ERROR_NONE) {
266                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_image. (%d)\n", noti_err);
267                 return;
268         }
269
270         /* set the title and content */
271         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_TITLE,
272                 "Turn off Wi-Fi direct after using", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
273         if (noti_err != NOTIFICATION_ERROR_NONE) {
274                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_text. (%d)\n", noti_err);
275                 return;
276         }
277
278         noti_err = notification_set_text(ad->noti, NOTIFICATION_TEXT_TYPE_CONTENT,
279                 "To save battery turn off Wi-Fi direct after using", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
280         if (noti_err != NOTIFICATION_ERROR_NONE) {
281                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_text. (%d)\n", noti_err);
282                 return;
283         }
284
285         bundle *b = NULL;
286         b = bundle_create();
287         appsvc_set_pkgname(b, PACKAGE);
288         appsvc_add_data(b, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
289
290         int res = NOTIFICATION_ERROR_NONE;
291         res = notification_set_execute_option(ad->noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, /*Button Text*/NULL, NULL, b);
292         if (res != NOTIFICATION_ERROR_NONE) {
293                 WFD_APP_LOG(WFD_APP_LOG_LOW,"Failed to notification_set_execute_option. [%d]", res);
294                 return;
295         }
296
297         bundle_free(b);
298
299         /* set display application list */
300         noti_err = notification_set_display_applist(ad->noti, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
301         if(noti_err != NOTIFICATION_ERROR_NONE) {
302                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_set_display_applist : %d\n", noti_err);
303                 return;
304         }
305
306         /* notify the quick panel */
307         noti_err = notification_insert(ad->noti, NULL);
308         if (noti_err != NOTIFICATION_ERROR_NONE) {
309                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_insert.(%d)\n", noti_err);
310                 return;
311         }
312
313         __WFD_APP_FUNC_EXIT__;
314 }
315
316 static Eina_Bool _wfd_automatic_deactivated_for_connection_cb(void *user_data)
317 {
318         int interval = 0;
319         unsigned int transmit_packet = 0;
320         int wfd_transfer_state = 0;
321         wfd_appdata_t *ad = (wfd_appdata_t*) user_data;
322
323         if (NULL == ad) {
324                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameters.\n");
325                 return ECORE_CALLBACK_CANCEL;
326         }
327
328         /* check the timeout, if not timeout, keep the cb */
329         interval = time(NULL) - ad->last_wfd_transmit_time;
330         if (interval < NO_ACTION_TIME_OUT) {
331                 return ECORE_CALLBACK_RENEW;
332         }
333
334         /* get transfer state */
335         if (vconf_get_int(VCONFKEY_WIFI_DIRECT_TRANSFER_STATE, &wfd_transfer_state) < 0) {
336                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Error reading vconf (%s)\n",
337                         VCONFKEY_WIFI_DIRECT_TRANSFER_STATE);
338                 return ECORE_CALLBACK_CANCEL;
339         }
340
341         /* show tickernoti*/
342         if (wfd_transfer_state > VCONFKEY_WIFI_DIRECT_TRANSFER_START) {
343                 WFD_APP_LOG(WFD_APP_LOG_LOW, "No RX/TX packet, turn off WFD automatically.\n");
344                 _add_wfd_turn_off_notification(ad);
345         } else {
346                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Has RX/TX packet, restart.\n");
347                 ad->last_wfd_transmit_time = time(NULL);
348                 return ECORE_CALLBACK_RENEW;
349         }
350
351         return ECORE_CALLBACK_CANCEL;
352 }
353
354 /* automatic deactivated wfd callback*/
355 static Eina_Bool _wfd_automatic_deactivated_for_no_connection_cb(void *user_data)
356 {
357         int res = -1;
358         int interval = 0;
359         wfd_appdata_t *ad = (wfd_appdata_t*) user_data;
360
361         if (NULL == ad) {
362                 WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameters.\n");
363                 return ECORE_CALLBACK_CANCEL;
364         }
365
366         /* check the action, if action is exist, keep the cb */
367         res = wifi_direct_get_state(&ad->wfd_status);
368         if (res != WIFI_DIRECT_ERROR_NONE) {
369                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Failed to get link status. [%d]\n", res);
370                 return ECORE_CALLBACK_CANCEL;
371         }
372
373         if (ad->last_wfd_status != ad->wfd_status) {
374                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Action is exist, last status: %d\n",
375                         ad->last_wfd_status);
376                 ad->last_wfd_status = ad->wfd_status;
377                 ad->last_wfd_time = time(NULL);
378                 return ECORE_CALLBACK_RENEW;
379         }
380
381         /* check the timeout, if not timeout, keep the cb */
382         interval = time(NULL) - ad->last_wfd_time;
383         if (interval < NO_ACTION_TIME_OUT) {
384                 return ECORE_CALLBACK_RENEW;
385         }
386
387         /* turn off the Wi-Fi Direct */
388         wifi_direct_get_state(&ad->wfd_status);
389         if (ad->wfd_status < WIFI_DIRECT_STATE_ACTIVATING) {
390                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Wi-Fi Direct is already deactivated\n");
391         } else {
392                 wfd_prepare_popup(WFD_POP_AUTOMATIC_TURN_OFF, NULL);
393         }
394
395         return ECORE_CALLBACK_CANCEL;
396 }
397
398 void _cb_activation(int error_code, wifi_direct_device_state_e device_state,
399                     void *user_data)
400 {
401     __WFD_APP_FUNC_ENTER__;
402
403     wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
404
405     switch (device_state)
406     {
407     case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
408         WFD_APP_LOG(WFD_APP_LOG_LOW,
409                     "event ------------------ WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
410         break;
411
412     case WIFI_DIRECT_DEVICE_STATE_DEACTIVATED:
413         WFD_APP_LOG(WFD_APP_LOG_LOW,
414                     "event ------------------ WIFI_DIRECT_DEVICE_STATE_DEACTIVATED\n");
415         WFD_APP_LOG(WFD_APP_LOG_LOW,
416                     "Termination process of wifi-direct popup begins...\n");
417
418         /* when deactivated, stop the timer */
419         if (ad->transmit_timer) {
420             ecore_timer_del(ad->transmit_timer);
421             ad->transmit_timer = NULL;
422         }
423
424         if (ad->monitor_timer) {
425             ecore_timer_del(ad->monitor_timer);
426             ad->monitor_timer = NULL;
427         }
428
429         elm_exit();
430         break;
431
432     default:
433         break;
434     }
435
436     __WFD_APP_FUNC_EXIT__;
437
438 }
439
440
441 static wfd_device_info_t *_wfd_app_find_peer_by_mac_address(void *data,
442                                                           const char *mac_address)
443 {
444         __WFD_APP_FUNC_ENTER__;
445
446         wfd_appdata_t *ad = (wfd_appdata_t *) data;
447         
448         int i;
449
450         if (ad == NULL)
451         {
452                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Incorrect parameter(NULL)\n");
453                 return NULL;
454         }
455
456         WFD_APP_LOG(WFD_APP_LOG_LOW, "find peer by MAC [%s] \n", mac_address);
457
458         for (i = 0; i < ad->discovered_peer_count; i++)
459         {
460                 WFD_APP_LOG(WFD_APP_LOG_LOW, "check %dth peer\n", i);
461                 
462                 if (!strncmp(mac_address, (const char *) ad->discovered_peers[i].mac_address, 18))
463                 {
464                         WFD_APP_LOG(WFD_APP_LOG_LOW, "found peer. [%d]\n", i);
465                         __WFD_APP_FUNC_EXIT__;
466                         return &ad->discovered_peers[i];
467                 }
468         }
469     
470         __WFD_APP_FUNC_EXIT__;
471
472     return NULL;
473 }
474
475
476 bool _wfd_app_discoverd_peer_cb(wifi_direct_discovered_peer_info_s * peer,
477                             void *user_data)
478 {
479         __WFD_APP_FUNC_ENTER__;
480
481         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
482
483         if (NULL != peer->ssid)
484         {
485                 WFD_APP_LOG(WFD_APP_LOG_LOW, "discovered peer ssid[%s]\n", peer->ssid);
486                 strncpy(ad->discovered_peers[ad->discovered_peer_count].ssid, peer->ssid, 32);
487         }
488         else
489         {
490                 WFD_APP_LOG(WFD_APP_LOG_LOW, "peer's ssid is NULL\n");
491         }
492
493         if (NULL != peer->mac_address)
494         {
495                 WFD_APP_LOG(WFD_APP_LOG_LOW, "discovered peer mac[%s]\n", peer->mac_address);
496                 strncpy(ad->discovered_peers[ad->discovered_peer_count].mac_address, peer->mac_address, 18);
497         }
498         else
499         {
500                 WFD_APP_LOG(WFD_APP_LOG_LOW, "peer's mac is NULL\n");
501         }
502         
503         ad->discovered_peer_count++;
504
505         __WFD_APP_FUNC_EXIT__;
506         
507         return TRUE;
508
509 }
510
511
512 void _cb_discover(int error_code, wifi_direct_discovery_state_e discovery_state,
513                   void *user_data)
514 {
515     __WFD_APP_FUNC_ENTER__;
516
517     wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
518     int ret;
519
520     switch (discovery_state)
521     {
522         case WIFI_DIRECT_DISCOVERY_STARTED:
523                 {
524                         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_STARTED\n");
525                 }
526                 break;
527
528         case WIFI_DIRECT_ONLY_LISTEN_STARTED:
529                 {
530                         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_ONLY_LISTEN_STARTED\n");
531                 }
532                 break;
533
534         case WIFI_DIRECT_DISCOVERY_FINISHED:
535                 {
536                         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_FINISHED\n");
537                 }
538                 break;
539
540         case WIFI_DIRECT_DISCOVERY_FOUND:
541                 {
542                         WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_DISCOVERY_FOUND\n");
543
544                         if (NULL != ad->discovered_peers)
545                                 free(ad->discovered_peers);
546
547                         ad->discovered_peers = calloc(10, sizeof(wfd_device_info_t));
548                         ad->discovered_peer_count = 0;
549
550                         ret = wifi_direct_foreach_discovered_peers(_wfd_app_discoverd_peer_cb, (void *) ad);
551                         if (ret != WIFI_DIRECT_ERROR_NONE)
552                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "get discovery result failed: %d\n", ret);
553                 }
554                 break;
555
556         default:
557                 break;
558     }
559
560     __WFD_APP_FUNC_EXIT__;
561
562 }
563
564 void _cb_connection(int error_code,
565                     wifi_direct_connection_state_e connection_state,
566                     const char *mac_address, void *user_data)
567 {
568         __WFD_APP_FUNC_ENTER__;
569
570         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
571         int result = -1;
572         char msg[WFD_POP_STR_MAX_LEN] = {0};
573         wfd_device_info_t *peer_info = NULL;
574
575         /* find the peer's name by the mac address */
576         if (NULL == mac_address) {
577                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "ERROR : mac address is NULL !!\n");
578                 return;
579         }
580
581         strncpy(ad->peer_mac, mac_address, strlen(mac_address));
582
583         peer_info = _wfd_app_find_peer_by_mac_address(ad, mac_address);
584
585         if (NULL == peer_info)
586         {
587                 WFD_APP_LOG(WFD_APP_LOG_LOW, "peer_info is NULL !!\n");
588         }
589         else if (NULL == peer_info->ssid)
590         {
591                 WFD_APP_LOG(WFD_APP_LOG_LOW, "SSID from connection is NULL !!\n");
592         }
593         else
594         {
595                 WFD_APP_LOG(WFD_APP_LOG_LOW, "SSID from connection is %s.\n", peer_info->ssid);
596                 strncpy(ad->peer_name, peer_info->ssid, strlen(peer_info->ssid));
597         }
598
599         if (ad->peer_name == NULL || strlen(ad->peer_name) == 0) {
600                 strncpy(ad->peer_name, ad->peer_mac, strlen(ad->peer_mac));
601         }
602
603         switch (connection_state)
604         {
605         case WIFI_DIRECT_CONNECTION_RSP:
606         {
607                 WFD_APP_LOG(WFD_APP_LOG_LOW,
608                                 "event ------------------ WIFI_DIRECT_CONNECTION_RSP\n");
609                 wfd_destroy_popup();
610
611                 if (error_code == WIFI_DIRECT_ERROR_NONE)
612                 {
613                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Link Complete!\n");
614
615                         /* add connected notification */
616                         _add_wfd_peers_connected_notification(ad);
617
618                         /* tickernoti popup */
619                         snprintf(msg, WFD_POP_STR_MAX_LEN, _("IDS_WFD_POP_CONNECTED"), ad->peer_name);
620                         wfd_tickernoti_popup(msg);
621                 }
622                 else
623                 {
624                         if (error_code == WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT)
625                                 WFD_APP_LOG(WFD_APP_LOG_LOW,
626                                                 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT\n");
627                         else if (error_code == WIFI_DIRECT_ERROR_AUTH_FAILED)
628                                 WFD_APP_LOG(WFD_APP_LOG_LOW,
629                                                 "Error Code - WIFI_DIRECT_ERROR_AUTH_FAILED\n");
630                         else if (error_code == WIFI_DIRECT_ERROR_CONNECTION_FAILED)
631                                 WFD_APP_LOG(WFD_APP_LOG_LOW,
632                                                 "Error Code - WIFI_DIRECT_ERROR_CONNECTION_FAILED\n");
633
634                         result = wifi_direct_start_discovery(FALSE, 0);
635                         WFD_APP_LOG(WFD_APP_LOG_LOW,
636                                         "wifi_direct_start_discovery() result=[%d]\n",
637                                         result);
638                 }
639         }
640         break;
641
642         case WIFI_DIRECT_CONNECTION_WPS_REQ:
643         {
644                 wifi_direct_config_data_s *config = NULL;
645
646                 memcpy(ad->peer_mac, mac_address, sizeof(ad->peer_mac));
647
648                 WFD_APP_LOG(WFD_APP_LOG_LOW,
649                                 "event ------------------ WIFI_DIRECT_CONNECTION_WPS_REQ\n");
650                 result = wifi_direct_get_config_data(&config);
651                 WFD_APP_LOG(WFD_APP_LOG_LOW,
652                                 "wifi_direct_client_get_config_data() result=[%d]\n",
653                                 result);
654
655                 if (config->wps_config == WIFI_DIRECT_WPS_TYPE_PBC)
656                 {
657                         WFD_APP_LOG(WFD_APP_LOG_LOW,
658                                         "wps_config is WIFI_DIRECT_WPS_TYPE_PBC. Ignore it..\n");
659                 }
660                 else if (config->wps_config == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD)
661                 {
662                         char *pin;
663                         WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
664
665                         if (wifi_direct_generate_wps_pin() != WIFI_DIRECT_ERROR_NONE)
666                         {
667                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_generate_wps_pin() is failed\n");
668                                 return;
669                         }
670
671                         if (wifi_direct_get_wps_pin(&pin) != WIFI_DIRECT_ERROR_NONE)
672                         {
673                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_generate_wps_pin() is failed\n");
674                                 return;
675                         }
676                         strncpy(ad->pin_number, pin, 32);
677                         free(pin);
678                         pin=NULL;
679
680                         WFD_APP_LOG(WFD_APP_LOG_LOW, "pin=[%s]\n", ad->pin_number);
681
682                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_PIN, NULL);
683                 }
684                 else if (config->wps_config == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY)
685                 {
686                         WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
687                         wfd_prepare_popup(WFD_POP_PROG_CONNECT_WITH_KEYPAD, (void *) NULL);
688                 }
689                 else
690                 {
691                         WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is unkown!\n");
692
693                 }
694                 if (config != NULL)
695                         free(config);
696         }
697         break;
698
699         case WIFI_DIRECT_CONNECTION_REQ:
700         {
701                 WFD_APP_LOG(WFD_APP_LOG_LOW, "event ------------------ WIFI_DIRECT_CONNECTION_REQ\n");
702
703                 wifi_direct_config_data_s *config = NULL;
704
705                 result = wifi_direct_get_config_data(&config);
706                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wifi_direct_client_get_config_data() result=[%d]\n", result);
707
708                 if(config->auto_connection == TRUE)
709                 {
710                         result = wifi_direct_accept_connection(ad->peer_mac);
711                         printf("wifi_direct_accept_connection() result=[%d]\n", result);
712                 }
713                 else
714                 {
715
716                         if (config->wps_config == WIFI_DIRECT_WPS_TYPE_PBC)
717                         {
718                                 char pushbutton;
719                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WIFI_DIRECT_WPS_TYPE_PBC\n");
720
721                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_PUSHBUTTON_REQ, NULL);
722                         }
723                         else if (config->wps_config == WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY)
724                         {
725                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_DISPLAY\n");
726
727                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_DISPLAY_REQ, NULL);
728                         }
729                         else if (config->wps_config == WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD)
730                         {
731                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is WIFI_DIRECT_WPS_TYPE_PIN_KEYPAD\n");
732                                 wfd_prepare_popup(WFD_POP_APRV_CONNECTION_WPS_KEYPAD_REQ, (void *) NULL);
733                         }
734                         else
735                         {
736                                 WFD_APP_LOG(WFD_APP_LOG_LOW, "wps_config is unkown!\n");
737                         }
738
739                         if (config != NULL)
740                                 free(config);
741
742                 }
743         }
744         break;
745
746         case WIFI_DIRECT_DISCONNECTION_IND:
747         {
748                 _del_wfd_notification();
749                 WFD_APP_LOG(WFD_APP_LOG_LOW,
750                                 "event ------------------ WIFI_DIRECT_DISCONNECTION_IND\n");
751
752                 result = wifi_direct_set_autoconnection_mode(false);
753                 WFD_APP_LOG(WFD_APP_LOG_LOW,"wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
754
755                 /* tickernoti popup */
756                 snprintf(msg, WFD_POP_STR_MAX_LEN, _("IDS_WFD_POP_DISCONNECTED"), ad->peer_name);
757                 wfd_tickernoti_popup(msg);
758         }
759         break;
760
761         case WIFI_DIRECT_DISCONNECTION_RSP:
762         {
763                 _del_wfd_notification();
764                 wfd_destroy_popup();
765
766                 result = wifi_direct_set_autoconnection_mode(false);
767                 WFD_APP_LOG(WFD_APP_LOG_LOW,"wifi_direct_set_autoconnection_mode() result=[%d]\n", result);
768
769                 result = wifi_direct_start_discovery(FALSE, 0);
770                 WFD_APP_LOG(WFD_APP_LOG_LOW,
771                                 "wifi_direct_start_discovery() result=[%d]\n", result);
772
773                 /* tickernoti popup */
774                 snprintf(msg, WFD_POP_STR_MAX_LEN, _("IDS_WFD_POP_DISCONNECTED"), ad->peer_name);
775                 wfd_tickernoti_popup(msg);
776         }
777         break;
778         case WIFI_DIRECT_CONNECTION_IN_PROGRESS:
779         {
780                 WFD_APP_LOG(WFD_APP_LOG_LOW,
781                                 "event ------------------ WIFI_DIRECT_CONNECTION_IN_PROGRESS\n");
782                 /* tickernoti popup */
783                 wfd_tickernoti_popup(_("IDS_WFD_POP_CONNECTING"));
784         }
785         default:
786                 break;
787
788         }
789
790         /* if connected, switch to the transmit timer; Otherwise, switch to monitor timer */
791         wifi_direct_get_state(&ad->wfd_status);
792         WFD_APP_LOG(WFD_APP_LOG_LOW,"status: %d", ad->wfd_status);
793
794         if (ad->wfd_status > WIFI_DIRECT_STATE_CONNECTING) {
795                 if (ad->monitor_timer) {
796                         ecore_timer_del(ad->monitor_timer);
797                         ad->monitor_timer = NULL;
798                 }
799
800                 if (NULL == ad->transmit_timer) {
801                         WFD_APP_LOG(WFD_APP_LOG_LOW, "switch to the transmit timer\n");
802                         ad->last_wfd_transmit_time = time(NULL);
803                         ad->transmit_timer = ecore_timer_add(5.0,
804                                         (Ecore_Task_Cb)_wfd_automatic_deactivated_for_connection_cb, ad);
805                 }
806         } else {
807                 if (ad->transmit_timer) {
808                         ecore_timer_del(ad->transmit_timer);
809                         ad->transmit_timer = NULL;
810                 }
811
812                 if (NULL == ad->monitor_timer) {
813                         WFD_APP_LOG(WFD_APP_LOG_LOW, "switch to the monitor timer\n");
814                         ad->last_wfd_time = time(NULL);
815                         ad->monitor_timer = ecore_timer_add(5.0,
816                                         (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
817                 }
818         }
819
820         __WFD_APP_FUNC_EXIT__;
821 }
822
823
824 int init_wfd_popup_client(wfd_appdata_t * ad)
825 {
826     __WFD_APP_FUNC_ENTER__;
827
828     if (NULL == ad) {
829         WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameters.\n");
830         return FALSE;
831     }
832
833     int ret = -1;
834
835     ret = wifi_direct_initialize();
836
837     ret = wifi_direct_set_device_state_changed_cb(_cb_activation, (void *) ad);
838     ret = wifi_direct_set_discovery_state_changed_cb(_cb_discover, (void *) ad);
839     ret =
840         wifi_direct_set_connection_state_changed_cb(_cb_connection,
841                                                     (void *) ad);
842
843     /* initialize notification */
844     ad->noti = NULL;
845     ad->raw_connected_peer_cnt = 0;
846
847     ad->noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
848     if (NULL == ad->noti) {
849         WFD_APP_LOG(WFD_APP_LOG_LOW, "notification_new failed.\n");
850         return FALSE;
851     }
852
853     /* start the monitor timer */
854     ad->last_wfd_time = time(NULL);
855     ad->last_wfd_status = WIFI_DIRECT_STATE_DEACTIVATED;
856     ad->monitor_timer = ecore_timer_add(5.0, (Ecore_Task_Cb)_wfd_automatic_deactivated_for_no_connection_cb, ad);
857
858     __WFD_APP_FUNC_EXIT__;
859
860     if (ret == WIFI_DIRECT_ERROR_NONE)
861         return TRUE;
862     else
863         return FALSE;
864 }
865
866 int deinit_wfd_popup_client(wfd_appdata_t * ad)
867 {
868     __WFD_APP_FUNC_ENTER__;
869
870     if (NULL == ad || NULL == ad->noti) {
871         WFD_APP_LOG(WFD_APP_LOG_LOW, "NULL parameters.\n");
872         return FALSE;
873     }
874
875     int ret = -1;
876
877     ret = wifi_direct_deinitialize();
878
879     _del_wfd_notification(ad);
880
881     notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
882     noti_err = notification_free(ad->noti);
883     if (noti_err != NOTIFICATION_ERROR_NONE) {
884         WFD_APP_LOG(WFD_APP_LOG_LOW, "Fail to notification_free.(%d)\n", noti_err);
885         ret = WIFI_DIRECT_ERROR_RESOURCE_BUSY;
886     }
887
888     if (ad->transmit_timer) {
889         ecore_timer_del(ad->transmit_timer);
890         ad->transmit_timer = NULL;
891     }
892
893     if (ad->monitor_timer) {
894         ecore_timer_del(ad->monitor_timer);
895         ad->monitor_timer = NULL;
896     }
897
898     __WFD_APP_FUNC_EXIT__;
899
900     if (ret == WIFI_DIRECT_ERROR_NONE)
901         return TRUE;
902     else
903         return FALSE;
904 }