summaryrefslogtreecommitdiff
path: root/ug-wifidirect/src/wfd_client.c
blob: 255baa9b75ac5f1b2611e99a9105290c42969d8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.tizenopensource.org/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * This file implements Wi-Fi direct client functions.
 *
 * @file    wfd_client.c
 * @author  Gibyoung Kim (lastkgb.kim@samsung.com)
 * @version 0.1
 */

#include <stdio.h>
#include <stdbool.h>
#include <libintl.h>

#include <Elementary.h>
#include <pmapi.h>
#include <vconf.h>
#include <network-cm-intf.h>
#include <network-wifi-intf.h>
#include <wifi-direct.h>

#include "wfd_ug.h"
#include "wfd_ug_view.h"
#include "wfd_client.h"




static void _wifi_state_cb(keynode_t *key, void *data)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data*) data;
    int res;
    int wifi_state;

    res = vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
    if (res != 0)
    {
        DBG(LOG_ERROR, "Failed to get wifi state from vconf. [%d]\n", res);
        return;
    }

    if (wifi_state == VCONFKEY_WIFI_OFF)
    {
        DBG(LOG_VERBOSE, "WiFi is turned off\n");
        wfd_client_swtch_force(ugd, TRUE);
    }
    else
    {
        DBG(LOG_VERBOSE, "WiFi is turned on\n");
    }

    res = net_deregister_client();
    if (res != NET_ERR_NONE)
    {
        DBG(LOG_ERROR, "Failed to deregister network client. [%d]\n", res);
    }

    __FUNC_EXIT__;
}

static void _network_event_cb(net_event_info_t *event_info, void *user_data)
{
    __FUNC_ENTER__;
    DBG(LOG_VERBOSE, "Event from network. [%d]\n", event_info->Event);
    __FUNC_EXIT__;
}

int wfd_wifi_off(void *data)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data*) data;
    int res;

    res = vconf_notify_key_changed(VCONFKEY_WIFI_STATE, _wifi_state_cb, ugd);
    if (res == -1)
    {
        DBG(LOG_ERROR, "Failed to register vconf callback\n");
        return -1;
    }
    DBG(LOG_VERBOSE, "Vconf key callback is registered\n");
    res = net_register_client((net_event_cb_t) _network_event_cb, NULL);
    if (res != NET_ERR_NONE)
    {
        DBG(LOG_ERROR, "Failed to register network client. [%d]\n", res);
        return -1;
    }
    DBG(LOG_VERBOSE, "Network client is registered\n");
    res = net_wifi_power_off();
    if (res != NET_ERR_NONE)
    {
        DBG(LOG_ERROR, "Failed to turn off wifi. [%d]\n", res);
        return -1;
    }
    DBG(LOG_VERBOSE, "WiFi power off\n");
    __FUNC_EXIT__;
    return 0;
}

static device_type_s *wfd_client_find_peer_by_ssid(void *data, const char *ssid)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data *) data;
    int i;

    if (ugd == NULL)
    {
        DBG(LOG_ERROR, "Incorrect parameter(NULL)\n");
        return NULL;
    }

    for (i = 0; i < ugd->peer_cnt; i++)
    {
        DBG(LOG_VERBOSE, "check %dth peer\n", i);
        if (!strcmp(ugd->peers[i].ssid, ssid))
        {
            DBG(LOG_VERBOSE, "found peer. [%d]\n", i);
            __FUNC_EXIT__;
            return &ugd->peers[i];
        }
    }
    __FUNC_EXIT__;

    return NULL;
}

static device_type_s *wfd_client_find_peer_by_mac(void *data,
                                                  const char *mac_addr)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data *) data;
    int i;

    if (ugd == NULL)
    {
        DBG(LOG_ERROR, "Incorrect parameter(NULL)\n");
        return NULL;
    }

    for (i = 0; i < ugd->peer_cnt; i++)
    {
        DBG(LOG_VERBOSE, "check %dth peer\n", i);
        if (!strncmp
            (mac_addr, (const char *) ugd->peers[i].mac_addr, MAC_LENGTH))
        {
            DBG(LOG_VERBOSE, "found peer. [%d]\n", i);
            __FUNC_EXIT__;
            return &ugd->peers[i];
        }
    }
    __FUNC_EXIT__;

    return NULL;
}

void _activation_cb(int error_code, wifi_direct_device_state_e device_state,
                    void *user_data)
{
    __FUNC_ENTER__;
    int res;
    wifi_direct_state_e wfd_status;
    struct ug_data *ugd = (struct ug_data *) user_data;

    wifi_direct_get_state(&wfd_status);
    DBG(LOG_VERBOSE, "WFD status [%d]", wfd_status);
    ugd->wfd_status = wfd_status;

    switch (device_state)
    {
    case WIFI_DIRECT_DEVICE_STATE_ACTIVATED:
        DBG(LOG_VERBOSE, "WIFI_DIRECT_DEVICE_STATE_ACTIVATED\n");
        if(error_code != WIFI_DIRECT_ERROR_NONE)
        {
            DBG(LOG_ERROR, "Error in Activation/Deactivation [%d]\n", error_code);
            wfd_ug_warn_popup(ugd, _("IDS_WFD_POP_ACTIVATE_FAIL"), POPUP_TYPE_ACTIVATE_FAIL);

            ugd->head_text_mode = HEAD_TEXT_TYPE_DIRECT;
            ugd->wfd_onoff = 0;
            wfd_ug_view_refresh_glitem(ugd->head);
            return;
        }

        ugd->head_text_mode = HEAD_TEXT_TYPE_ACTIVATED;
        ugd->wfd_onoff = 1;
        wfd_ug_view_refresh_glitem(ugd->head);
        res = vconf_set_int("db/wifi_direct/onoff", ugd->wfd_onoff);
        if (res != 0)
        {
            DBG(LOG_ERROR, "Failed to set vconf value for WFD onoff status\n");
        }
        wfg_ug_act_popup_remove(ugd);

        res = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wifi_state_cb);
        if (res == -1)
        {
            DBG(LOG_ERROR,
                "Failed to ignore vconf key callback for wifi state\n");
        }

        res = wifi_direct_start_discovery(FALSE, 0);
        if (res != WIFI_DIRECT_ERROR_NONE)
        {
            DBG(LOG_ERROR, "Failed to start discovery. [%d]\n", res);
        }
        break;
        DBG(LOG_VERBOSE, "Discovery is started\n");
    case WIFI_DIRECT_DEVICE_STATE_DEACTIVATED:
        DBG(LOG_VERBOSE, "WIFI_DIRECT_DEVICE_STATE_DEACTIVATED\n");
        if(error_code != WIFI_DIRECT_ERROR_NONE)
        {
            DBG(LOG_ERROR, "Error in Activation/Deactivation [%d]\n", error_code);
            wfd_ug_warn_popup(ugd, _("IDS_WFD_POP_DEACTIVATE_FAIL"), POPUP_TYPE_DEACTIVATE_FAIL);
            ugd->head_text_mode = HEAD_TEXT_TYPE_DIRECT;
            ugd->wfd_onoff = 1;
            wfd_ug_view_refresh_glitem(ugd->head);
            return;
        }

        ugd->head_text_mode = HEAD_TEXT_TYPE_DIRECT;
        ugd->wfd_onoff = 0;
        wfd_ug_view_refresh_glitem(ugd->head);
        res = vconf_set_int("db/wifi_direct/onoff", ugd->wfd_onoff);
        if (res != 0)
        {
            DBG(LOG_ERROR, "Failed to set vconf value for WFD onoff status\n");
        }
        wfd_ug_view_free_peers(ugd);
        wfd_ug_view_update_peers(ugd);
        break;
    default:
        break;
    }
    wfd_ug_view_refresh_button(ugd->scan_btn, ugd->wfd_onoff);

    __FUNC_EXIT__;
    return;
}

static int peer_cnt;
static int connected_cnt;
static int discovered_cnt;

bool _wfd_discoverd_peer_cb(wifi_direct_discovered_peer_info_s * peer,
                            void *user_data)
{
    __FUNC_ENTER__;
    device_type_s *peers = (device_type_s *) user_data;

    DBG(LOG_VERBOSE, "%dth discovered peer. [%s]\n", peer_cnt, peer->ssid);
    if (peer->is_connected == TRUE)
        return FALSE;
    memcpy(peers[peer_cnt].ssid, peer->ssid, SSID_LENGTH);
    peers[peer_cnt].ssid[31] = '\0';
    DBG(LOG_VERBOSE, "\tSSID: [%s]\n", peers[peer_cnt].ssid);
    peers[peer_cnt].category = peer->primary_device_type;
    DBG(LOG_VERBOSE, "\tPeer category [%d] -> [%d]\n", peer->primary_device_type, peers[peer_cnt].category);
    strncpy(peers[peer_cnt].mac_addr, peer->mac_address, MAC_LENGTH);
    strncpy(peers[peer_cnt].if_addr, peer->interface_address, MAC_LENGTH);
    peers[peer_cnt].conn_status = PEER_CONN_STATUS_DISCONNECTED;
    DBG(LOG_VERBOSE, "\tStatus: [%d]\n", peers[peer_cnt].conn_status);
    peer_cnt++;

    free(peer->ssid);
    free(peer->mac_address);
    free(peer->interface_address);
    free(peer);
    __FUNC_EXIT__;
    return TRUE;
}

bool _wfd_connected_peer_cb(wifi_direct_connected_peer_info_s * peer,
                            void *user_data)
{
    __FUNC_ENTER__;
    device_type_s *peers = (device_type_s *) user_data;

    DBG(LOG_VERBOSE, "%dth connected peer. [%s]\n", peer_cnt, peer->ssid);
    memcpy(peers[peer_cnt].ssid, peer->ssid, SSID_LENGTH);
    peers[peer_cnt].ssid[31] = '\0';
    DBG(LOG_VERBOSE, "\tSSID: [%s]\n", peers[peer_cnt].ssid);
    peers[peer_cnt].category = peer->primary_device_type;
    DBG(LOG_VERBOSE, "\tCategory: [%d]\n", peers[peer_cnt].category);
    strncpy(peers[peer_cnt].mac_addr, peer->mac_address, MAC_LENGTH);
    strncpy(peers[peer_cnt].if_addr, peer->interface_address, MAC_LENGTH);
    peers[peer_cnt].conn_status = PEER_CONN_STATUS_CONNECTED;
    DBG(LOG_VERBOSE, "\tStatus: [%d]\n", peers[peer_cnt].conn_status);
    peer_cnt++;

    free(peer->ssid);
    free(peer->mac_address);
    free(peer->interface_address);
    free(peer);
    __FUNC_EXIT__;
    return TRUE;
}

void _discover_cb(int error_code, wifi_direct_discovery_state_e discovery_state,
                  void *user_data)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data *) user_data;
    int res;
    device_type_s *peers = NULL;

    if (ugd == NULL)
    {
        DBG(LOG_ERROR, "Incorrect parameter(NULL)\n");
        return;
    }

    peers = calloc(MAX_PEER_NUM, sizeof(device_type_s));

    if (discovery_state == WIFI_DIRECT_ONLY_LISTEN_STARTED)
    {
        ugd->head_text_mode = HEAD_TEXT_TYPE_DIRECT;
    }
    else if (discovery_state == WIFI_DIRECT_DISCOVERY_STARTED)
    {
        ugd->head_text_mode = HEAD_TEXT_TYPE_SCANING;
    }
    wfd_ug_view_refresh_glitem(ugd->head);

    if (ugd->wfd_status < WFD_LINK_STATUS_ACTIVATED
        || ugd->wfd_status > WFD_LINK_STATUS_GROUP_OWNER)
    {
        return;
    }
    else
    {
        peer_cnt = 0;
    }

    if (ugd->wfd_status >= WFD_LINK_STATUS_CONNECTED)
    {
        DBG(LOG_VERBOSE, "getting connected peer..\n");
        res =
            wifi_direct_foreach_connected_peers(_wfd_connected_peer_cb,
                                                (void *) peers);
        if (res != WIFI_DIRECT_ERROR_NONE)
        {
            connected_cnt = 0;
            DBG(LOG_ERROR, "get discovery result failed: %d\n", res);
        }
    }

    if (discovery_state == WIFI_DIRECT_DISCOVERY_FOUND)
    {
        DBG(LOG_VERBOSE, "Peer is found\n");
        ugd->head_text_mode = HEAD_TEXT_TYPE_DIRECT;
        wfd_ug_view_refresh_glitem(ugd->head);

        if (ugd->wfd_status >= WFD_LINK_STATUS_ACTIVATED)
        {
            DBG(LOG_VERBOSE, "getting discovered peer..\n");
            res =
                wifi_direct_foreach_discovered_peers(_wfd_discoverd_peer_cb,
                                                     (void *) peers);
            if (res != WIFI_DIRECT_ERROR_NONE)
            {
                discovered_cnt = 0;
                DBG(LOG_ERROR, "get discovery result failed: %d\n", res);
            }
        }
    }

    wfd_ug_view_free_peers(ugd);

    ugd->peers = peers;
    ugd->peer_cnt = peer_cnt;

    wfd_ug_view_update_peers(ugd);
    DBG(LOG_VERBOSE, "%d peers are updated\n", peer_cnt);

    __FUNC_EXIT__;
    return;
}

void _connection_cb(int error_code,
                    wifi_direct_connection_state_e connection_state,
                    const char *mac_address, void *user_data)
{
    __FUNC_ENTER__;
    DBG(LOG_VERBOSE, "Connection event [%d], error_code [%d]\n",
        connection_state, error_code);
    struct ug_data *ugd = (struct ug_data *) user_data;
    device_type_s *peer = NULL;
    bool owner = FALSE;
    int res = 0;

    if (mac_address == NULL)
    {
        DBG(LOG_ERROR, "Incorrect parameter(peer mac is NULL)\n");
        return;
    }
    DBG(LOG_VERBOSE, "Connection event from %s", mac_address);

    peer = wfd_client_find_peer_by_mac(ugd, mac_address);
    if (peer == NULL)
    {
        DBG(LOG_ERROR, "Failed to find peer [mac: %s]\n", mac_address);
        return;
    }

    switch (connection_state)
    {
    case WIFI_DIRECT_CONNECTION_RSP:
        DBG(LOG_VERBOSE, "WIFI_DIRECT_CONNECTION_RSP\n");

        if (error_code == WIFI_DIRECT_ERROR_NONE)
        {
            ugd->wfd_status = WFD_LINK_STATUS_CONNECTED;
            peer->conn_status = PEER_CONN_STATUS_CONNECTED;
            res = wifi_direct_is_group_owner(&owner);
            if (res == WIFI_DIRECT_ERROR_NONE)
            {
                if (!owner)
                    wfd_ug_view_refresh_button(ugd->scan_btn, FALSE);
            }
            else
            {
                DBG(LOG_ERROR,
                    "Failed to get whether client is group owner. [%d]\n", res);
            }
        }
        else
        {
            peer->conn_status = PEER_CONN_STATUS_DISCONNECTED;
            wifi_direct_start_discovery(FALSE, 0);
        }
        break;
    case WIFI_DIRECT_DISCONNECTION_RSP:
    case WIFI_DIRECT_DISCONNECTION_IND:
    case WIFI_DIRECT_DISASSOCIATION_IND:
        DBG(LOG_VERBOSE, "WIFI_DIRECT_DISCONNECTION_X\n");
        if (error_code != WIFI_DIRECT_ERROR_NONE)
        {
            return;
        }

        peer->conn_status = PEER_CONN_STATUS_DISCONNECTED;

        ugd->wfd_status = WFD_LINK_STATUS_ACTIVATED;
        ugd->head_text_mode = HEAD_TEXT_TYPE_ACTIVATED;

        wfd_ug_view_refresh_button(ugd->scan_btn, TRUE);
        wifi_direct_start_discovery(FALSE, 0);
        ugd->wfd_status = WFD_LINK_STATUS_DISCOVERING;
        ugd->head_text_mode = HEAD_TEXT_TYPE_SCANING;
        wfd_ug_view_refresh_glitem(ugd->head);
        break;
    case WIFI_DIRECT_CONNECTION_IN_PROGRESS:
        DBG(LOG_VERBOSE, "WIFI_DIRECT_CONNECTION_IN_PROGRESS\n");
        peer->conn_status = PEER_CONN_STATUS_CONNECTING;
        break;
    case WIFI_DIRECT_CONNECTION_REQ:
    case WIFI_DIRECT_CONNECTION_WPS_REQ:
        DBG(LOG_VERBOSE, "WIFI_DIRECT_CLI_EVENT_CONNECTION_REQ\n");
        break;
    default:
        break;
    }

    if (peer != NULL)
        wfd_ug_view_refresh_glitem(peer->gl_item);

    __FUNC_EXIT__;
    return;
}

int wfd_get_vconf_status(void *data)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data *) data;
    int res;
    char *dev_name;

    res = vconf_get_int("db/wifi_direct/onoff", &ugd->wfd_onoff);
    if (res != 0)
    {
        DBG(LOG_ERROR, "vconf_get_int is failed\n");
    }
    DBG(LOG_VERBOSE, "VCONF_WFD_ONOFF : %d\n", ugd->wfd_onoff);

    dev_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
    if (dev_name == NULL)
    {
        ugd->dev_name = strdup(DEFAULT_DEV_NAME);
        DBG(LOG_ERROR, "The AP name is NULL(setting default value)\n");
    }
    else
    {
        ugd->dev_name = strdup(dev_name);
        free(dev_name);
    }

    __FUNC_EXIT__;

    return 0;
}

int init_wfd_client(void *data)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data *) data;
    int res = 0;
    wifi_direct_state_e wfd_status;

    res = wifi_direct_initialize();
    if (res != WIFI_DIRECT_ERROR_NONE)
    {
        DBG(LOG_ERROR, "Failed to initialize wifi direct. [%d]\n", res);
        return -1;
    }

    res = wifi_direct_set_device_state_changed_cb(_activation_cb, (void *) ugd);
    res =
        wifi_direct_set_discovery_state_changed_cb(_discover_cb, (void *) ugd);
    res =
        wifi_direct_set_connection_state_changed_cb(_connection_cb,
                                                    (void *) ugd);

    res = wifi_direct_get_state(&wfd_status);
    if (res != WIFI_DIRECT_ERROR_NONE)
    {
        DBG(LOG_ERROR, "Failed to get link status. [%d]\n", res);
        return -1;
    }
    ugd->wfd_status = wfd_status;
    DBG(LOG_VERBOSE, "WFD link status. [%d]\n", wfd_status);

    if (wfd_status > WIFI_DIRECT_STATE_ACTIVATING)
    {
        vconf_set_int("db/wifi_direct/onoff", 1);
        ugd->wfd_onoff = 1;
    }
    else
    {
        vconf_set_int("db/wifi_direct/onoff", 0);
        ugd->wfd_onoff = 0;
    }
    wfd_ug_view_refresh_glitem(ugd->head);
    wfd_ug_view_refresh_button(ugd->scan_btn, ugd->wfd_onoff);

    if(wfd_status >= WIFI_DIRECT_STATE_CONNECTED)
    {
        device_type_s *peers = NULL;

        peers = calloc(MAX_PEER_NUM, sizeof(device_type_s));
        res = wifi_direct_foreach_connected_peers(_wfd_connected_peer_cb, (void*) peers);
        if(res != WIFI_DIRECT_ERROR_NONE)
        {
            connected_cnt = 0;
            DBG(LOG_ERROR, "get discovery result failed: %d\n", res);
        }
        wfd_ug_view_free_peers(ugd);

        ugd->peers = peers;
        ugd->peer_cnt = peer_cnt;

        wfd_ug_view_update_peers(ugd);
        DBG(LOG_VERBOSE, "%d peers are updated\n", peer_cnt);
    }

    if (wfd_status > WIFI_DIRECT_STATE_ACTIVATING)
    {
        int wifi_state;
        vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);

        if (wifi_state < VCONFKEY_WIFI_CONNECTED)
        {
            res = wifi_direct_start_discovery(FALSE, 0);
            if (res != WIFI_DIRECT_ERROR_NONE)
            {
                DBG(LOG_ERROR, "Failed to start discovery. [%d]\n", res);
            }
            DBG(LOG_VERBOSE, "Discovery is started\n");
        }
        else
        {
            wfd_ug_act_popup(ugd, _("IDS_WFD_POP_WIFI_OFF"),
                             POPUP_TYPE_WIFI_OFF);
        }
    }

    __FUNC_EXIT__;

    return 0;
}

int deinit_wfd_client(void *data)
{
    __FUNC_ENTER__;
    int res = 0;
    wifi_direct_state_e status = 0;

    wifi_direct_get_state(&status);

    if (status == WIFI_DIRECT_STATE_DISCOVERING)
    {
        DBG(LOG_VERBOSE, "Stop discovery before deregister client\n");
        wifi_direct_cancel_discovery();
    }

    res = wifi_direct_deinitialize();
    if (res != WIFI_DIRECT_ERROR_NONE)
    {
        DBG(LOG_ERROR, "Failed to deregister client. [%d]\n", res);
    }

    res = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wifi_state_cb);
    if (res == -1)
    {
        DBG(LOG_ERROR, "Failed to ignore vconf key callback for wifi state\n");
    }

    res = net_deregister_client();
    if (res != NET_ERR_NONE)
    {
        DBG(LOG_ERROR, "Failed to deregister network client. [%d]\n", res);
    }


    __FUNC_EXIT__;

    return 0;
}

int wfd_client_get_link_status()
{
    __FUNC_ENTER__;
    wifi_direct_state_e status;
    int res;

    res = wifi_direct_get_state(&status);
    if (res != WIFI_DIRECT_ERROR_NONE)
    {
        DBG(LOG_ERROR, "Failed to get link status from wfd-server. [%d]", res);
        return -1;
    }

    __FUNC_EXIT__;
    return status;
}

int wfd_client_switch_on(void *data)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data *) data;
    int res;

    DBG(LOG_VERBOSE, "WFD status [%d]\n", ugd->wfd_status);

    if (ugd->wfd_status < WFD_LINK_STATUS_ACTIVATING)
    {
        ugd->wfd_status = WFD_LINK_STATUS_ACTIVATING;

        int wifi_state;
        res = vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
        if (res != 0)
        {
            DBG(LOG_ERROR, "Failed to get wifi state from vconf. [%d]\n", res);
            return -1;
        }

        if (wifi_state > VCONFKEY_WIFI_OFF)
        {
            DBG(LOG_VERBOSE, "WiFi is connected, so have to turn off WiFi");
            wfd_ug_act_popup(ugd, _("IDS_WFD_POP_WIFI_OFF"),
                             POPUP_TYPE_WIFI_OFF);
        }
        else
        {
            res = wifi_direct_activate();
            if (res != WIFI_DIRECT_ERROR_NONE)
            {
                DBG(LOG_ERROR,
                    "Failed to activate Wi-Fi Direct. error code = [%d]\n",
                    res);
                wfd_ug_warn_popup(ugd, _("IDS_WFD_POP_ACTIVATE_FAIL"), POPUP_TYPE_TERMINATE);

                ugd->head_text_mode = HEAD_TEXT_TYPE_DIRECT;
                wfd_ug_view_refresh_glitem(ugd->head);
                return -1;
            }
        }
    }
    else
    {
        DBG(LOG_VERBOSE, "Wi-Fi Direct is already activated\n");
    }

    __FUNC_EXIT__;
    return 0;
}

int wfd_client_switch_off(void *data)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data *) data;
    int res;

    DBG(LOG_VERBOSE, "WFD status [%d]\n", ugd->wfd_status);

    if (ugd->wfd_status < WFD_LINK_STATUS_ACTIVATING)
    {
        DBG(LOG_VERBOSE, "Wi-Fi Direct is already deactivated\n");
    }
    else
    {
        ugd->wfd_status = WFD_LINK_STATUS_DEACTIVATING;

        res = wifi_direct_deactivate();
        if (res != WIFI_DIRECT_ERROR_NONE)
        {
            DBG(LOG_ERROR,
                "Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
            wfd_ug_warn_popup(ugd, _("IDS_WFD_POP_DEACTIVATE_FAIL"), POPUP_TYPE_TERMINATE);

            ugd->head_text_mode = HEAD_TEXT_TYPE_DIRECT;
            wfd_ug_view_refresh_glitem(ugd->head);
            return -1;
        }
    }

    __FUNC_EXIT__;
    return 0;
}

int wfd_client_swtch_force(void *data, int onoff)
{
    __FUNC_ENTER__;
    struct ug_data *ugd = (struct ug_data*) data;
    int res;

    if (onoff)
    {
        res = wifi_direct_activate();
        if (res != WIFI_DIRECT_ERROR_NONE)
        {
            DBG(LOG_ERROR,
                "Failed to activate Wi-Fi Direct. error code = [%d]\n", res);
            wfd_ug_warn_popup(ugd, _("IDS_WFD_POP_ACTIVATE_FAIL"), POPUP_TYPE_TERMINATE);

            ugd->head_text_mode = HEAD_TEXT_TYPE_DIRECT;
            wfd_ug_view_refresh_glitem(ugd->head);
            return -1;
        }
    }
    else
    {
        res = wifi_direct_deactivate();
        if (res != WIFI_DIRECT_ERROR_NONE)
        {
            DBG(LOG_ERROR,
                "Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
            wfd_ug_warn_popup(ugd, _("IDS_WFD_POP_DEACTIVATE_FAIL"), POPUP_TYPE_TERMINATE);

            ugd->head_text_mode = HEAD_TEXT_TYPE_DIRECT;
            wfd_ug_view_refresh_glitem(ugd->head);
            return -1;
        }
    }
    __FUNC_EXIT__;
    return 0;
}

int wfd_client_start_discovery(void *data)
{
    __FUNC_ENTER__;
    int res;
    wifi_direct_state_e status;

    wifi_direct_get_state(&status);
    if (status >= WIFI_DIRECT_STATE_ACTIVATED)
    {
        res = wifi_direct_start_discovery(FALSE, 0);
        if (res != WIFI_DIRECT_ERROR_NONE)
        {
            DBG(LOG_ERROR, "Failed to start wfd discovery. [%d]", res);
        }
    }
    __FUNC_EXIT__;

    return 0;
}

int wfd_client_connect(const char *mac_addr)
{
    __FUNC_ENTER__;
    int res;

    res = wifi_direct_connect(mac_addr);
    if (res != WIFI_DIRECT_ERROR_NONE)
    {
        DBG(LOG_ERROR, "Failed to send connection request. [%d]\n", res);
        return -1;
    }
    __FUNC_EXIT__;
    return 0;
}

int wfd_client_disconnect(const char *mac_addr)
{
    __FUNC_ENTER__;
    int res;

    if (mac_addr == NULL)
    {
        res = wifi_direct_disconnect_all();
        if (res != WIFI_DIRECT_ERROR_NONE)
        {
            DBG(LOG_ERROR,
                "Failed to send disconnection request to all. [%d]\n", res);
            return -1;
        }
    }
    else
    {
        res = wifi_direct_disconnect(mac_addr);
        if (res != WIFI_DIRECT_ERROR_NONE)
        {
            DBG(LOG_ERROR, "Failed to send disconnection request. [%d]\n", res);
            return -1;
        }
    }
    __FUNC_EXIT__;
    return 0;
}