summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/p1905_managerd/src/p1905_utils.c
blob: 8af9049f382e476a240a930fe67033f43b761972 (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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
 * cleopatre/application/p1905_managerd/src/p1905_utils.c
 *
 * (C) Copyright 2013 MSsar Semiconductor, Inc.
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <errno.h>
#include <linux/if_packet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <syslog.h>
#include <assert.h>
#include <linux/if_ether.h>
#include <linux/sockios.h>
#include <linux/if_bridge.h>
#include "p1905_utils.h"
#include "cmdu_tlv.h"
#include "libmme.h"
#include <sys/queue.h>
#ifdef SUPPORT_PLC
#include "plc_utils.h"
#endif

//#define P1905_UTILS_DEBUG
#ifdef P1905_UTILS_DEBUG
#define debug(...)          printf(__VA_ARGS__)
#define debug_syslog(...)        printf(__VA_ARGS__)
#else
#define debug(...)
#define debug_syslog(format, ...)   syslog(LOG_WARNING, format, ##__VA_ARGS__)
#endif

static unsigned int has_confirm = 0;

#define MAX_FDB_ENTRIES 48

#define PATH_PLC0_PORT_NO "/sys/class/net/plc0/brport/port_no"
#define PATH_ETH0_PORT_NO "/sys/class/net/eth0/brport/port_no"
#ifdef SUPPORT_WIFI
#define PATH_WIFI0_PORT_NO "/sys/class/net/ra0/brport/port_no"
#endif

int get_receive_port_addr(unsigned char *inputmac,unsigned char *outputmac)
{
    int fd;
    struct ifreq ifr;
    unsigned long args[4];

    ifr.ifr_data = (char *) &args;

    fd = socket(AF_INET, SOCK_STREAM, 0);
    strncpy(ifr.ifr_name, (char*)BR0_IFNAME, IFNAMSIZ);
    args[0] = BRCTL_GET_RECV_PORT_ADDR;
    args[1] = (unsigned long)outputmac;
    args[2] = (unsigned long)inputmac;
    args[3] = 0;

    if(0 > (ioctl(fd, SIOCDEVPRIVATE, &ifr)))
    {
        debug("bridge_get_port_addr error");
        close(fd);
        return -1;
    }

    close(fd);
    return 0;
}

int set_bridge_not_forward_dest(unsigned char *mac)
{
    int fd;
    struct ifreq ifr;
    unsigned long args[4];

    ifr.ifr_data = (char *) &args;

    fd = socket(AF_INET, SOCK_STREAM, 0);
    strncpy(ifr.ifr_name, (char*)BR0_IFNAME, IFNAMSIZ);
    args[0] = BRCTL_SET_BRIDGE_NOT_FORWARD_DEST;
    args[1] = (unsigned long)mac;
    args[2] = 0;
    args[3] = 0;

    if(0 > (ioctl(fd, SIOCDEVPRIVATE, &ifr)))
    {
        debug("set bridge not forward dest fail\n");
        close(fd);
        return -1;
    }

    close(fd);
    return 0;
}

int get_br_fdb_entries(struct __fdb_entry *fdb_buf)
{
    int fd;
    struct ifreq ifr;
    unsigned long args[4];
    int num = 0;

    ifr.ifr_data = (char *) &args;
    fd = socket(AF_INET, SOCK_STREAM, 0);
    strncpy(ifr.ifr_name, (char*)BR0_IFNAME, IFNAMSIZ);

    args[0] = BRCTL_GET_FDB_ENTRIES;
    args[1] = (unsigned long)fdb_buf;
    //we temporarily define max entry number to 48, it can be modified
    args[2] = MAX_FDB_ENTRIES;
    args[3] = 0;

    num = ioctl(fd, SIOCDEVPRIVATE, &ifr);
    if(0 > num)
    {
        debug("get_br_fdb_entries fail\n");
        close(fd);
        return -1;
    }
    close(fd);
    return num;
}

int set_br_fdb_ageing_time(unsigned long ageing_time)
{
    int fd;
    struct ifreq ifr;
    unsigned long args[4];
    int result = 0;

    ifr.ifr_data = (char *) &args;
    fd = socket(AF_INET, SOCK_STREAM, 0);
    strncpy(ifr.ifr_name, (char*)BR0_IFNAME, IFNAMSIZ);

    args[0] = BRCTL_SET_AGEING_TIME;
    args[1] = (unsigned long)(ageing_time * 100);
    args[2] = 0;
    args[3] = 0;

    result = ioctl(fd, SIOCDEVPRIVATE, &ifr);
    if(0 > result)
    {
        debug("set br ageing time fail\n");
        close(fd);
        return -1;
    }
    close(fd);
    return 0;
}

static int is_p1905_device(struct list_head_tpddb *tpd_head, unsigned char *mac)
{
    struct list_head *pos, *pos1;
    struct topology_discovery_db *dcv_db;

    if(!LIST_EMPTY(tpd_head))
    {
        LIST_FOREACH(dcv_db, tpd_head, tpddb_entry)
        {
            if(!memcmp(mac, dcv_db->al_mac, ETH_ALEN))
            {
                /* this mac existed in topology discovery database.
                 * so it is a p1905 neighbor , return 1.
                 */
                debug("AL ==>exist in topology discovery\n");
                return 1;
            }

            if(!memcmp(mac, dcv_db->itf_mac, ETH_ALEN))
            {
                /* this mac existed in topology discovery database.
                 * so it is a p1905 neighbor , return 1.
                 */
                debug("itf ==>exist in topology discovery\n");
                return 1;
            }
        }
    }

#if 0
    /*compare with topology response database*/
    if(!SLIST_EMPTY(tpgr_head))
    {
        SLIST_FOREACH(tpr_db, tpgr_head, tprdb_entry)
        {
            if(!memcmp(mac, tpr_db->al_mac_addr, ETH_ALEN))
            {
                /* mac address exist in AL mac field in topology response
                 * database, so this device is a 1905.1 device
                 */
                debug("AL ==>exist in topology response\n");
                return 1;
            }

            /* topology response db is not empty , so we already init devinfo
             * head before. We can use this list head directly
             */
            if(!SLIST_EMPTY(&(tpr_db->devinfo_head)))
            {
                SLIST_FOREACH(dev_info, &(tpr_db->devinfo_head), devinfo_entry)
                {
                    if(!memcmp(mac, dev_info->mac_addr, ETH_ALEN))
                    {
                        /* mac address exist in local mac field in topology response
                         * database, so this device is a 1905.1 device
                         */
                        debug("interface ==> exist in topology response\n");
                        return 1;
                    }
                }
            }
        }
    }
#endif


    return 0;
}

void update_non_1905_device_from_fdb(struct list_head_tpddb *tpd_head,
                        struct non_p1905_neighbor *dev)
{
    struct __fdb_entry *fdb, *fdb1;
    int num = 0, i = 0, j=0;

    struct non_p1905_neighbor_info *dev_info;

    fdb = (struct __fdb_entry*)malloc(MAX_FDB_ENTRIES * sizeof(struct __fdb_entry));
    num = get_br_fdb_entries(fdb);

    if(num > 0)
    {
        fdb1 = fdb;
        for(i = 0;i < num; i++)
        {
            debug("fdb%d mac:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x portno%d: local:%d\n",
                    i,fdb1->mac_addr[0],fdb1->mac_addr[1],fdb1->mac_addr[2],
                    fdb1->mac_addr[3],fdb1->mac_addr[4],fdb1->mac_addr[5],
                    fdb1->port_no, fdb1->is_local);
            if(!fdb1->is_local)
            {
                if(!is_p1905_device(tpd_head, fdb1->mac_addr))
                {
                    /* update to non-1905.1 neighbor device database
                     * use fdb1->port_no to distinguish differnet interface
                     */
                    for(j=0;j<ITF_NUM;j++)
                    {
                        if(fdb1->port_no == dev[j].br_port_no)
                        {
                            debug("get br port no = %d\n",dev[j].br_port_no);
                            break;
                        }
                    }
                    /* no need to check existence of non-1905.1 device
                     * because we will delete all before updating this database
                     */
                    dev_info = (struct non_p1905_neighbor_info *)
                        malloc(sizeof(struct non_p1905_neighbor_info));
                    memcpy(dev_info->itf_mac_addr, fdb1->mac_addr, ETH_ALEN);
                    LIST_INSERT_HEAD(&(dev[j].non_p1905nbr_head), dev_info,
                                    non_p1905nbr_entry);
                    debug("new non-1905.1 device added\n");
                }
            }
            fdb1++;
        }
    }
    free(fdb);
}

int is_topology_changed(struct list_head_tpddb *tpd_head,
            struct non_p1905_neighbor *dev)
{
    struct __fdb_entry *fdb, *fdb1;
    int num = 0, i = 0, j=0;

    struct non_p1905_neighbor_info *dev_info;
    unsigned char exist = 0;
    unsigned short non_1905_neighbor_num = 0;
    unsigned short cnt = 0;

    /*detect any new or removed non-1905.1 neighbor device*/
    fdb = (struct __fdb_entry*)malloc(MAX_FDB_ENTRIES * sizeof(struct __fdb_entry));
    num = get_br_fdb_entries(fdb);

    if(num > 0)
    {
        fdb1 = fdb;
        for(i = 0;i < num; i++)
        {
            debug("fdb1%d mac:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x portno%d: local:%d\n",
                    i,fdb1->mac_addr[0],fdb1->mac_addr[1],fdb1->mac_addr[2],
                    fdb1->mac_addr[3],fdb1->mac_addr[4],fdb1->mac_addr[5],
                    fdb1->port_no, fdb1->is_local);

            if(!fdb1->is_local)
            {
                if(!is_p1905_device(tpd_head, fdb1->mac_addr))
                {
                    /*calculate the number of total non-1901.1 neighbors*/
                    non_1905_neighbor_num++;

                    for(j=0;j<ITF_NUM;j++)
                    {
                        if(fdb1->port_no == dev[j].br_port_no)
                        {
                            debug("get br port no = %d\n",dev[j].br_port_no);
                            break;
                        }
                    }


                    /* if list is empty, it means that no non-1905.1 device
                     * originally and a new non-1905.1 neighbor device joined.
                     * So return true
                     */
                    if(LIST_EMPTY(&(dev[j].non_p1905nbr_head)))
                    {
                        debug("topology change, new device joined\n");
                        free(fdb);
                        return 1;
                    }
                    else
                    {
                        exist = 0;
                        LIST_FOREACH(dev_info, &(dev[j].non_p1905nbr_head),
                                    non_p1905nbr_entry)
                        {
                            if(!memcmp(dev_info->itf_mac_addr, fdb1->mac_addr,\
                                ETH_ALEN))
                            {
                                exist = 1;
                                break;
                            }
                        }
                        /* compare with all the non-1905.1 neighbor device
                         * and cannot find a matched device. So this is a new
                         * joined non-1905.1 neighbor , return true
                         */
                        if(!exist)
                        {
                            debug("topology change, another new device joined\n");
                            free(fdb);
                            return 1;
                        }
                    }
                }
            }
            fdb1++;
        }
    }
    free(fdb);

    /* if goto here, it means no new non-1905.1 neighbor joined. So check if any
     * non-1905.1 neighbor removed. Calculate total num of local non-1905.1
     * neighbor and compare with  non_1905_neighbor_num.
     */
    for(j=0;j<ITF_NUM;j++)
        LIST_FOREACH(dev_info, &(dev[j].non_p1905nbr_head), non_p1905nbr_entry)
        {
            cnt++;
        }
    debug("non_1905_neighbor_num=%d, cnt = %d\n",non_1905_neighbor_num,cnt);
    if(non_1905_neighbor_num < cnt)
    {
        debug("topology change, device removed\n");
        return 1;
    }
    else
    {
        debug("topology is the same\n");
        return 0;
    }
}

int get_tx_link_statistics(link_stat *stat, itftype media_type,
        unsigned char *local_itf, unsigned char *neighbor_itf)
{
    FILE *fp;
    char tmp_buffer[256];
    char name[64], *ptr;

    if((media_type >= IEEE802_3_GROUP) && (media_type < IEEE802_11_GROUP))
    {
        if((fp = fopen ("/proc/net/dev", "r")) == NULL)
            return 0;

        fgets(tmp_buffer, sizeof(tmp_buffer), fp);
        fgets(tmp_buffer, sizeof(tmp_buffer), fp);

        while(fgets (tmp_buffer, sizeof(tmp_buffer), fp))
        {
            if((ptr = strchr (tmp_buffer, ':')) == NULL)
                continue;
            *ptr = '\0';
            ptr++;
            if((sscanf (tmp_buffer, "%s", name) == 1)
                && !strncmp (name, ETH0, strlen(ETH0))
                && (sscanf (ptr, "%*s %lu %lu %*s %*s %*s %*s %*s %*s %lu %lu %*s %*s %*s %*s %*s",
                       &stat->total_rx_packets, &stat->error_rx_packets,
                       &stat->total_tx_packets, &stat->error_tx_packets)) == 4)
            {
                debug("ETH tx statics %lu,%lu,%lu,%lu\n",stat->total_rx_packets,
                    stat->error_rx_packets,stat->total_tx_packets,
                    stat->error_tx_packets);

                fclose(fp);
                return 1;
            }
        }

        fclose(fp);
        return 0;
    }
#ifdef SUPPORT_WIFI
    else if(media_type >= IEEE802_11_GROUP && media_type < IEEE1901_GROUP)
    {
        if(wifi_utils_success != get_tx_statistics(neighbor_itf, &stat->total_tx_packets,
                &stat->error_tx_packets))
            return 0;

        return 1;
    }
#endif
#ifdef SUPPORT_PLC
    else if(media_type >= IEEE1901_GROUP && media_type < MOCA_GROUP)
    {
        if(plc_utils_success != plc_get_tx_link_statistics(local_itf, 
            neighbor_itf, &stat->total_tx_packets, &stat->error_tx_packets, 1))
            return 0;
        return 1;
    }
#endif
    else
    {
        debug_syslog("get_tx_link_statistics unsupport media type\n");
        return 0;
    }
}

int get_rx_link_statistics(link_stat *stat, itftype media_type,
        unsigned char *local_itf, unsigned char *neighbor_itf)
{
    FILE *fp;
    char tmp_buffer[256];
    char name[64], *ptr;

    if(media_type >= IEEE802_3_GROUP && media_type < IEEE802_11_GROUP)
    {
        if((fp = fopen ("/proc/net/dev", "r")) == NULL)
            return 0;

        fgets(tmp_buffer, sizeof(tmp_buffer), fp);
        fgets(tmp_buffer, sizeof(tmp_buffer), fp);

        while(fgets (tmp_buffer, sizeof(tmp_buffer), fp))
        {
            if((ptr = strchr (tmp_buffer, ':')) == NULL)
                continue;
            *ptr = '\0';
            ptr++;
            if((sscanf (tmp_buffer, "%s", name) == 1)
                && !strncmp (name, ETH0, strlen(ETH0))
                && (sscanf (ptr, "%*s %lu %lu %*s %*s %*s %*s %*s %*s %lu %lu %*s %*s %*s %*s %*s",
                       &stat->total_rx_packets, &stat->error_rx_packets,
                       &stat->total_tx_packets, &stat->error_tx_packets)) == 4)
            {
                debug("ETH tx statics %lu,%lu,%lu,%lu\n",stat->total_rx_packets,
                    stat->error_rx_packets,stat->total_tx_packets,
                    stat->error_tx_packets);

                fclose(fp);
                return 1;
            }
        }

        fclose(fp);
        return 0;
    }
#ifdef SUPPORT_WIFI
    else if(media_type >= IEEE802_11_GROUP && media_type < IEEE1901_GROUP)
    {
        if(wifi_utils_success != get_rx_statistics(neighbor_itf, &stat->total_rx_packets,
                &stat->error_rx_packets))
            return 0;

        return 1;
    }
#endif
#ifdef SUPPORT_PLC
    else if(media_type >= IEEE1901_GROUP && media_type < MOCA_GROUP)
    {
        if(plc_utils_success != plc_get_tx_link_statistics(neighbor_itf, 
            local_itf, &stat->total_rx_packets, &stat->error_rx_packets, 0))
            return 0;
        return 1;
    }
#endif
    else
    {
        debug_syslog("get_rx_link_statistics unsupport media type\n");
        return 0;
    }
}

unsigned short get_bridge_port_no(char *itf_name)
{
    FILE *fp;
    char tmp_buffer[10];
    unsigned short port_no = 0;

    if(!strncmp(itf_name,"plc0",5))
    {
        if((fp = fopen (PATH_PLC0_PORT_NO, "r")) == NULL)
            return 0;
    }
    else if(!strncmp(itf_name,"eth0",5))
    {
        if((fp = fopen (PATH_ETH0_PORT_NO, "r")) == NULL)
            return 0;
    }
#ifdef SUPPORT_WIFI
    else if(!strncmp(itf_name, "ra0", 5))
    {
        if((fp = fopen (PATH_WIFI0_PORT_NO, "r")) == NULL)
            return 0;
    }
#endif
    else
        return 0;

    fgets(tmp_buffer, sizeof(tmp_buffer), fp);
    sscanf(tmp_buffer, "0x%hx",&port_no);
    fclose(fp);
    return port_no;
}

int get_link_phy_rate(itftype media_type, unsigned char *target,
                      unsigned char *local_itf, unsigned short *phyrate)
{
    if(media_type >= IEEE802_3_GROUP && media_type < IEEE802_11_GROUP)
    {
        *phyrate = 0;//need to implement ETH phyrate
    }
#ifdef SUPPORT_WIFI
    /* If the media type of the link is IEEE 802.3, then IEEE 1901 or MoCA 1.1
     * (8 MSB bits value of media type as defined in Table 6-12 is 0, 2, or 3). 
     * This value is the PHY rate estimated at the transmitter of the link expressed in Mb/s;
     * otherwise, it is set to 0xFFFF.
     * Samuel: based on description above in spec p40, we should set phy rate to 0xffff
     *         but for good user experience, I still get real PHY rate from wifi
     */
    else if(media_type >= IEEE802_11_GROUP && media_type < IEEE1901_GROUP)
    {
        if(wifi_utils_success != get_phyrate(target, phyrate))
            *phyrate = 0;
    }
#endif
#ifdef SUPPORT_PLC
    else if(media_type >= IEEE1901_GROUP && media_type < MOCA_GROUP)
    {
        if(plc_utils_success != plc_get_tx_link_phyrate(local_itf, target,
            phyrate))
            *phyrate = 0xFFFF;
    }
#endif
    else
      *phyrate = 0xFFFF;

    return 1;
}

#ifdef SUPPORT_PLC
int get_network_id(unsigned char *local_itf, unsigned char *network_id)
{
    if(plc_utils_success != PLC_get_network_id(local_itf, network_id))
        return 0;

    return 1;
}
#endif

unsigned short
get_link_max_throughput_capacity(unsigned short phyrate, itftype media_type)
{
    /*max throughput capacity = phyrate & mac overhead*/
    unsigned short cap;

    if(media_type >= IEEE802_3_GROUP && media_type < IEEE802_11_GROUP)
    {
        cap = 0;//need to implement
    }
#ifdef SUPPORT_WIFI
    else if(media_type >= IEEE802_11_GROUP && media_type < IEEE1901_GROUP)
    {
        cap = 0;
    }
#endif
#ifdef SUPPORT_PLC
    else if(media_type >= IEEE1901_GROUP && media_type < MOCA_GROUP)
    {
        plc_get_mac_throughput_capacity(phyrate, &cap);
    }
#endif
    else
        cap = 0;

    return cap;
}

int get_link_rssi(char *rssi, unsigned char peer_mac[], itftype media_type)
{
#ifdef SUPPORT_WIFI
    if(media_type >= IEEE802_11_GROUP && media_type < IEEE1901_GROUP)
    {
        if(wifi_utils_success != get_rssi(rssi, peer_mac))
        {
            debug_syslog("get rssi fail\n");
            return 0;
        }
    }
    else
#endif 
       *rssi = 0xFF;

    return 1;
}

int get_station_info(struct p1905_managerd_ctx *ctx, unsigned char step)
{

    if(step == 1)
    {
        if(plc_utils_success != plc_get_station_info(ctx->plc0_mac_addr, &(ctx->pbc_param.info), 1))
            return 0;
    }
    else if(step == 2)
    {
        if(plc_utils_success != plc_get_station_info(ctx->plc0_mac_addr, &(ctx->pbc_param.info), 2))
            return 0;
    }

    return 1;
}

int is_neighbor_wifi_sta(struct p1905_managerd_ctx *ctx, unsigned char sta_itf[],
    unsigned char al_mac[])
{
    struct topology_discovery_db *tpg_db;
    int i = 0;

    if(!LIST_EMPTY(&(ctx->topology_entry.tpddb_head)))
    {
        LIST_FOREACH(tpg_db, &(ctx->topology_entry.tpddb_head), tpddb_entry)
        {
            if(memcmp(tpg_db->al_mac, al_mac, 6))
                continue;
            for(i=0;i<ITF_NUM;i++)
            {
                /*check 802.11 specific information length*/
                if(ctx->itf[i].vs_info_length == 10)
                {
                    /*check the receiving interface is AP*/
                    if(*((ctx->itf[i].vs_info)+6) == 0x00)
                    {
                        if(!memcmp(tpg_db->receive_itf_mac, ctx->itf[i].mac_addr, 6))
                        {
                            memcpy(sta_itf, tpg_db->itf_mac, 6);
                            return 1;
                        }
                    }
                }
            }
        }
    }

    return 0;
}

void dump_topology_info(struct p1905_managerd_ctx *ctx)
{
    struct topology_discovery_db *tpg_db;
    struct topology_response_db  *tpr_db;
    struct device_info_db *dev_info;
    struct p1905_neighbor_device_db *p1905_dev;
    struct non_p1905_neighbor_device_list_db *non_p1905_dev;
    struct p1905_neighbor_info *neighbor_dev_info;
    struct non_p1905_neighbor_info *non_1905neighbor_info;

    struct device_bridge_capability_db *br_cap;
    int i = 0;
    int j = 0;
    int k = 0;
    int z = 0;
    int a = 0, b = 0;
    int f = 0;

    printf("====================topology discovery====================\n");
    if(!LIST_EMPTY(&(ctx->topology_entry.tpddb_head)))
    {
        LIST_FOREACH(tpg_db, &(ctx->topology_entry.tpddb_head), tpddb_entry)
        {
            i++;
            printf("neighbor%d AL MAC:\n",i);
            printf("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",tpg_db->al_mac[0],
                  tpg_db->al_mac[1],tpg_db->al_mac[2],tpg_db->al_mac[3],
                  tpg_db->al_mac[4],tpg_db->al_mac[5]);
            printf("neighbor%d connect interface MAC:\n",i);
            printf("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",tpg_db->itf_mac[0],
                  tpg_db->itf_mac[1],tpg_db->itf_mac[2],tpg_db->itf_mac[3],
                  tpg_db->itf_mac[4],tpg_db->itf_mac[5]);
        }
    }
    else
        printf("!!!no topology discovery\n");

    printf("====================topology response====================\n");
    if(!SLIST_EMPTY(&(ctx->topology_entry.tprdb_head)))
    {
        i = 0;
        SLIST_FOREACH(tpr_db, &(ctx->topology_entry.tprdb_head), tprdb_entry)
        {

            i++;
            printf("device%d AL MAC:\n",i);
            printf("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",tpr_db->al_mac_addr[0],
                    tpr_db->al_mac_addr[1],tpr_db->al_mac_addr[2],
                    tpr_db->al_mac_addr[3],tpr_db->al_mac_addr[4],
                    tpr_db->al_mac_addr[5]);
            if(!SLIST_EMPTY(&(tpr_db->devinfo_head)))
            {
                SLIST_FOREACH(dev_info, &(tpr_db->devinfo_head), devinfo_entry)
                {
                    j++;
                    printf("device%d interface%d :\n",i,j);
                    printf("mac = 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
                            dev_info->mac_addr[0],dev_info->mac_addr[1],
                            dev_info->mac_addr[2],dev_info->mac_addr[3],
                            dev_info->mac_addr[4],dev_info->mac_addr[5]);
                    printf("media type = %d\n",dev_info->media_type);
                    if(dev_info->vs_info_len > 0)
                    {
                        printf("media specific info: \n");
                        for(k=0;k<7;k++)
                            printf("0x%x ",*(dev_info->vs_info + k));
                        printf("\n");
                    }

                    if(!SLIST_EMPTY(&(dev_info->p1905_nbrdb_head)))
                    {
                        SLIST_FOREACH(p1905_dev, &(dev_info->p1905_nbrdb_head), p1905_nbrdb_entry)
                        {
                            z++;
                            printf("p1905 neighbor device%d :\n",z);
                            printf("AL MAC = 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
                                    p1905_dev->p1905_neighbor_al_mac[0],
                                    p1905_dev->p1905_neighbor_al_mac[1],
                                    p1905_dev->p1905_neighbor_al_mac[2],
                                    p1905_dev->p1905_neighbor_al_mac[3],
                                    p1905_dev->p1905_neighbor_al_mac[4],
                                    p1905_dev->p1905_neighbor_al_mac[5]);
                            if(p1905_dev->ieee_802_1_bridge_exist)
                                printf("802.1 bridge exist\n");
                            else
                                printf("802.1 bridge not exist\n");

                        }
                        z = 0;

                    }
                    else
                        printf("!!!no p1905.1 neighbor device\n");

                    if(!SLIST_EMPTY(&(dev_info->non_p1905_nbrdb_head)))
                    {
                        SLIST_FOREACH(non_p1905_dev, &(dev_info->non_p1905_nbrdb_head), non_p1905_nbrdb_entry)
                        {
                            z++;

                        printf("non p1905 neighbor device%d :\n",z);
                        printf("MAC = 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
                                non_p1905_dev->non_p1905_device_interface_mac[0],
                                non_p1905_dev->non_p1905_device_interface_mac[1],
                                non_p1905_dev->non_p1905_device_interface_mac[2],
                                non_p1905_dev->non_p1905_device_interface_mac[3],
                                non_p1905_dev->non_p1905_device_interface_mac[4],
                                non_p1905_dev->non_p1905_device_interface_mac[5]);

                        }
                        z = 0;
                    }
                    else
                        printf("!!!no non-p1905.1 neighbor device\n");
                }
                j = 0;

            }

            if(!LIST_EMPTY(&(tpr_db->brcap_head)))
            {
                LIST_FOREACH(br_cap, &(tpr_db->brcap_head), brcap_entry)
                {
                    a++;
                    printf("device%d bridge%d:\n",i,a);
                    printf("interface amount = %d\n",br_cap->interface_amount);
                    if(br_cap->interface_amount > 0)
                    {
                        printf("interface_mac_tuple:\n");
                        for(b=0;b<(br_cap->interface_amount)*6;b++)
                            printf("0x%x ",*((br_cap->interface_mac_tuple)+b));
                        printf("\n");
                    }       
                }
            }
        }
    }

     printf("====================local info====================\n");
     j=0;
     f=0;
     for(i=0;i<ITF_NUM;i++)
     {
        printf("local MAC %d = 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", i,
            ctx->p1905_neighbor_dev[i].local_mac_addr[0],
            ctx->p1905_neighbor_dev[i].local_mac_addr[1],
            ctx->p1905_neighbor_dev[i].local_mac_addr[2],
            ctx->p1905_neighbor_dev[i].local_mac_addr[3],
            ctx->p1905_neighbor_dev[i].local_mac_addr[4],
            ctx->p1905_neighbor_dev[i].local_mac_addr[5]);

        if(!LIST_EMPTY(&(ctx->p1905_neighbor_dev[i].p1905nbr_head)))
        {
            LIST_FOREACH(neighbor_dev_info, &(ctx->p1905_neighbor_dev[i].p1905nbr_head),
                        p1905nbr_entry)
            {
                j++;
                printf("neighbor device %d\n AL MAC =\n",j);
                printf("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
                       neighbor_dev_info->al_mac_addr[0],
                       neighbor_dev_info->al_mac_addr[1],
                       neighbor_dev_info->al_mac_addr[2],
                       neighbor_dev_info->al_mac_addr[3],
                       neighbor_dev_info->al_mac_addr[4],
                       neighbor_dev_info->al_mac_addr[5]);

                printf("802.1 bridge: ");
                if(neighbor_dev_info->ieee802_1_bridge)
                    printf("yes\n");
                else
                    printf("NO\n");
            }
        }
        else
            printf("no 1905.1 neighbors!!!\n");


        if(!LIST_EMPTY(&(ctx->non_p1905_neighbor_dev[i].non_p1905nbr_head)))
        {
            LIST_FOREACH(non_1905neighbor_info, &(ctx->non_p1905_neighbor_dev[i].non_p1905nbr_head),
                        non_p1905nbr_entry)
            {
                f++;
                printf("non 1905 neighbor device %d\n MAC =\n",f);
                printf("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
                    non_1905neighbor_info->itf_mac_addr[0],
                    non_1905neighbor_info->itf_mac_addr[1],
                    non_1905neighbor_info->itf_mac_addr[2],
                    non_1905neighbor_info->itf_mac_addr[3],
                    non_1905neighbor_info->itf_mac_addr[4],
                    non_1905neighbor_info->itf_mac_addr[5]);
            }
        }
        else
            printf("no non-1905.1 neighbors!!!\n");
    }
}