summaryrefslogtreecommitdiff
path: root/cleopatre/application/libspid/src/port_eoc.c
blob: 59cba207edf95c92830eccfca77117c43c370c8c (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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
/* SPC300 bundle {{{
 *
 * Copyright (C) 2012 MStar
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    application/libspid/src/port_eoc.c
 * \brief   Functions specific to EoC ports
 * \ingroup libspid
 *
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <syslog.h>
#include <assert.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/netlink.h>
#include <ctype.h>

#include "libspid.h"
#include "libmme.h"

#ifdef __UTESTS__
libspid_error_t
port_send_single_value (unsigned int mmtype, const void *value,
                        unsigned int length);
#endif

/**
 * Returns the port entry for the given slave MAC address and port index.<BR>
 * \param mac_address slave MAC address
 * \param index  port index
 * \param entry  port entry
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_port_get (char *mac_address, char *index,
                      libspid_eoc_port_entry_t *entry)
{
    const char delimiters[2] = LIBSPID_PORT_CONF_DELIMITER "\0";
    char key[LIBSPID_CONFIG_KEY_MAX_LEN] = {0};
    unsigned int elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
    char *elt[LIBSPID_CONFIG_ELT_MAX_NB] = {0};
    char buffer[LIBSPID_CONFIG_LINE_MAX_LEN] = {0};
    libspid_error_t error;
    int i;
    int value;

    /* check input parameters */
    if ((mac_address == NULL) || (index == NULL) || (entry == NULL))
        return LIBSPID_ERROR_PARAM;

    if (LIBSPID_SUCCESS != libspid_system_nscrtv_eponeoc_mod_eoc (&value))
        return LIBSPID_ERROR_SYSTEM;

    memset (entry, 0x0, sizeof (entry));

    for (i  =0; i < LIBSPID_MAC_STR_LEN; i++)
    {
        mac_address[i] = tolower (mac_address[i]);
    }

    /* use slave MAC and port index to compose key */
    /* for line in configuration file */
    sprintf (key, "%s.%s", mac_address, index);

    strcpy (entry->mac_addr, mac_address);
    strcpy (entry->port_index, index);

    error = libspid_config_read_line (LIBSPID_PORT_CONF_PATH, delimiters, key,
                                      &elt_number, elt, buffer,
                                      LIBSPID_CONFIG_LINE_MAX_LEN);
    if (LIBSPID_SUCCESS != error)
        return error;

    if (elt_number < LIBSPID_PORT_PARAM_NUM)
    {
        strcpy (entry->port_en, "");
        strcpy (entry->service_index, "");
        strcpy (entry->vlan_tag_en, "");
        strcpy (entry->vlan_mark_id, "");
    }
    else
    {
        strcpy (entry->port_en, elt[0]);
        strcpy (entry->service_index, elt[1]);

        if (LIBSPID_FALSE == value)
        {
            strcpy (entry->vlan_tag_en, elt[2]);
            strcpy (entry->vlan_mark_id, elt[3]);
        }
        else
        {
            strcpy (entry->vlan_tag_en, "0");
            strcpy (entry->vlan_mark_id, "0");
        }
    }


    return LIBSPID_SUCCESS;
}

/**
 * Returns the whole port list from configuration file in a
 * pointer to an array of port entries.<BR>
 * \param entry  pointer to an array of port entries
 * \param count  pointer to an integer for number of port entries
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_port_get_list (libspid_eoc_port_entry_t *entry, int *count)
{
    const char delimiters[2] = LIBSPID_PORT_CONF_DELIMITER "\0";
    char key[LIBSPID_CONFIG_KEY_MAX_LEN] = {0};
    unsigned int elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
    char *elt[LIBSPID_CONFIG_ELT_MAX_NB] = {0};
    char buffer[LIBSPID_CONFIG_LINE_MAX_LEN] = {0};
    libspid_error_t error;
    libspid_eoc_port_entry_t *entry_ptr;
    char *index_str;
    int value;
    void * user_data = NULL;
    /* check input parameters */
    if ((entry == NULL) || (count == NULL))
        return LIBSPID_ERROR_PARAM;

    if (LIBSPID_SUCCESS != libspid_system_nscrtv_eponeoc_mod_eoc (&value))
        return LIBSPID_ERROR_SYSTEM;

    *count = 0;
    memset (key, 0x0, sizeof (key));
    memset (entry, 0x0, sizeof (entry));

    entry_ptr = entry;

    /* get the first key */
    error = libspid_config_read_line_repetitive (LIBSPID_PORT_CONF_PATH,
                                                 delimiters, key, &elt_number,
                                                 elt, buffer,
                                                 LIBSPID_CONFIG_LINE_MAX_LEN,
                                                 &user_data);
    if (LIBSPID_SUCCESS != error)
        return error;

    for (; *count < LIBSPID_PORT_TOTAL_MAX && strcmp (key, ""); (*count)++)
    {
        /* Get a line from the conf file */
        elt_number = LIBSPID_CONFIG_ELT_MAX_NB;
        error = libspid_config_read_line_repetitive (
                    LIBSPID_PORT_CONF_PATH, delimiters, key,
                    &elt_number, elt, buffer,
                    LIBSPID_CONFIG_LINE_MAX_LEN, &user_data);
        if (LIBSPID_SUCCESS != error)
            return error;

        /* parse MAC address and port index from buffer */
        index_str = strchr (buffer, '.');
        if (index_str!= NULL)
            *index_str++ = 0;
        else
        {
            LIBSPID_EOC_LOG_2 ("%s: error parsing MAC "
                               "address and port number (errno=%d)",
                               __FUNCTION__, errno);
            return LIBSPID_ERROR_SYSTEM;
        }

        strcpy (entry_ptr->mac_addr, buffer);
        strcpy (entry_ptr->port_index, index_str);
        if (elt_number < LIBSPID_PORT_PARAM_NUM)
        {
            strcpy (entry_ptr->port_en, "");
            strcpy (entry_ptr->service_index, "");
            strcpy (entry_ptr->vlan_tag_en, "");
            strcpy (entry_ptr->vlan_mark_id, "");
        }
        else
        {
            strcpy (entry_ptr->port_en, elt[0]);
            strcpy (entry_ptr->service_index, elt[1]);
            if (LIBSPID_FALSE == value)
            {
                strcpy (entry_ptr->vlan_tag_en, elt[2]);
                strcpy (entry_ptr->vlan_mark_id, elt[3]);
            }
            else
            {
                strcpy (entry_ptr->vlan_tag_en, "0");
                strcpy (entry_ptr->vlan_mark_id, "0");
            }
        }

        entry_ptr++;
    }
    libspid_config_read_line_repetitive (0,0,0,0,0,0,0, &user_data);
    return LIBSPID_SUCCESS;
}

#ifndef __UTESTS__
static libspid_error_t
port_send_single_value (unsigned int mmtype, const void *value,
                        unsigned int length)
{
    unsigned char buffer[ETH_DATA_LEN];
    mme_ctx_t request_ctx, confirm_ctx;
    unsigned char result;
    unsigned int result_len;
    char *iface = LIBSPID_EOC_PLC_IFACE;
    unsigned char mac_address[LIBSPID_MAC_STR_LEN];
    unsigned char mac_address_bin[ETH_ALEN];
    int ret;

    /* init mme */
    if (MME_SUCCESS != mme_init (&request_ctx,
                                 mmtype | MME_REQ,
                                 buffer, ETH_DATA_LEN))
    {
        LIBSPID_EOC_LOG_1 ("%s: initialization of request context failed\n",
                           __FUNCTION__);
        return LIBSPID_ERROR_SYSTEM;
    }

    if (MME_SUCCESS != mme_init (&confirm_ctx, mmtype | MME_CNF,
                                 buffer, ETH_DATA_LEN))
    {
        LIBSPID_EOC_LOG_1 ("%s: initialization of confirm context failed\n",
                           __FUNCTION__);
        return LIBSPID_ERROR_SYSTEM;
    }

    if (value != NULL)
    {
        if (MME_SUCCESS != mme_put (&request_ctx, value, length, &result_len))
        {
            LIBSPID_EOC_LOG_1 ("%s: mme_put for value failed\n",
                               __FUNCTION__);
            return LIBSPID_ERROR_SYSTEM;
        }
    }

    ret = libspid_network_get_mac (iface, mac_address);
    if (ret != LIBSPID_SUCCESS)
        return ret;

    ret = libspid_mac_str_to_bin ((char *)mac_address, mac_address_bin);
    if (ret != LIBSPID_SUCCESS)
        return ret;

    if ((mme_send (&request_ctx, MME_SEND_REQ_CNF, NULL,
                   mac_address_bin, &confirm_ctx) == MME_SUCCESS)
         && (confirm_ctx.oui == MME_OUI_SPIDCOM)
         && (mme_pull (&confirm_ctx, &result,
                       sizeof (result), &result_len) == MME_SUCCESS))
    {
        return LIBSPID_SUCCESS;
    }
    else
    {
        LIBSPID_EOC_LOG_1 ("%s: MME send failed\n", __FUNCTION__);
        return LIBSPID_ERROR_SYSTEM;
    }
}

#endif /* __UTESTS__ */

/**
 * Retrieves current contents of port list in a
 * pointer to an array of port entries.<BR>
 * \param entry  pointer to an array of port entries
 * \param count  pointer to an integer for number of port entries
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_port_retrieve (libspid_eoc_port_entry_t *entry, int *count)
{
    unsigned char index = 0;
    unsigned char i, j, ret;
    unsigned int total_sta_entry_nb = LIBSPID_WHITE_LIST_MAX_STA_NUM;
    unsigned int curr_sta_entry_nb = LIBSPID_PORT_STA_MAX_CNT;
    libspid_eoc_port_entry_t *curr_port_entry = entry;
    unsigned char buffer[ETH_DATA_LEN];
    mme_ctx_t request_ctx, confirm_ctx;
    unsigned char result;
    unsigned int result_len;
    unsigned char mac_address[LIBSPID_MAC_STR_LEN];
    unsigned char mac_address_bin[ETH_ALEN];

    /* check input parameters */
    if ((entry == NULL) || (count == NULL))
        return LIBSPID_ERROR_PARAM;

    for (index = 0; index < total_sta_entry_nb; index += curr_sta_entry_nb)
    {
        /* init mme */
        ret = mme_init (&request_ctx, VS_EOC_CCO_GET_PORTS_REQ,
                        buffer, ETH_DATA_LEN);
        if (ret != MME_SUCCESS)
            return LIBSPID_ERROR_MME;
        ret = mme_init (&confirm_ctx, VS_EOC_CCO_GET_PORTS_CNF,
                        buffer, ETH_DATA_LEN);
        if (ret != MME_SUCCESS)
            return LIBSPID_ERROR_MME;

        /* Index of the first station MAC address to be returned . */
        ret = mme_put (&request_ctx, &index, 1, &result_len);
        if (ret != MME_SUCCESS)
            return LIBSPID_ERROR_MME;

#ifndef __UTESTS__
        ret = libspid_network_get_mac (LIBSPID_EOC_PLC_IFACE, mac_address);
        if (ret != LIBSPID_SUCCESS)
            return ret;
#else
        strcpy ((char *) mac_address, "00:d7:20:00:00:01");
#endif /* __UTESTS__ */

        ret = libspid_mac_str_to_bin ((char *) mac_address, mac_address_bin);
        if (ret != LIBSPID_SUCCESS)
            return ret;

        ret = mme_send (&request_ctx, MME_SEND_REQ_CNF, NULL, mac_address_bin,
                        &confirm_ctx);
        if (ret != MME_SUCCESS)
            return LIBSPID_ERROR_MME;

        /* extract SPIDCOM OUI and check if it is correct */
        if (confirm_ctx.oui != MME_OUI_SPIDCOM)
            return LIBSPID_ERROR_MME;

        /* collect confirmation result */
        ret = mme_pull (&confirm_ctx, &result, 1, &result_len);
        if (MME_SUCCESS != ret)
            return LIBSPID_ERROR_MME;

        /* Total number of stations */
        ret = mme_pull (&confirm_ctx, &total_sta_entry_nb, 1, &result_len);
        if (MME_SUCCESS != ret)
            return LIBSPID_ERROR_MME;

        /* Index of the first station MAC address to be returned  */
        /* there is no need for this parameter */
        ret = mme_pull (&confirm_ctx, &result, 1, &result_len);
        if (MME_SUCCESS != ret)
            return LIBSPID_ERROR_MME;

        /* Number of stations whose port state is following. */
        ret = mme_pull (&confirm_ctx, &curr_sta_entry_nb, 1, &result_len);
        if (MME_SUCCESS != ret)
            return LIBSPID_ERROR_MME;

        *count = LIBSPID_PORT_PER_SLAVE_MAX * total_sta_entry_nb;

        for (i = 0; i < curr_sta_entry_nb; i++)
        {
            unsigned char mac_addr_bin[ETH_ALEN];
            unsigned int mac_addr_bin_len = 0;
            unsigned char port_en, service_index;

            /* collect MAC address */
            ret = mme_pull (&confirm_ctx, mac_addr_bin,
                            ETH_ALEN, &mac_addr_bin_len);
            if (MME_SUCCESS != ret)
                return LIBSPID_ERROR_MME;

            for (j = 0;  j< LIBSPID_PORT_PER_SLAVE_MAX; j++)
            {

                ret = libspid_mac_bin_to_str (mac_addr_bin,
                                              curr_port_entry->mac_addr);
                if (LIBSPID_SUCCESS != ret)
                    return LIBSPID_ERROR_MME;

                sprintf (curr_port_entry->port_index, "%d", j);

                /* collect state of port (enable/disable) */
                ret = mme_pull (&confirm_ctx, &port_en, 1, &result_len);
                if (MME_SUCCESS != ret)
                    return LIBSPID_ERROR_MME;
                sprintf (curr_port_entry->port_en, "%d", port_en);

                /* collect index of service attached to port */
                ret = mme_pull (&confirm_ctx, &service_index, 1, &result_len);
                if (MME_SUCCESS != ret)
                    return LIBSPID_ERROR_MME;
                sprintf (curr_port_entry->service_index, "%d", service_index);

                curr_port_entry++;
            }
        }
    }

    return LIBSPID_SUCCESS;
}

/**
 * Set port entry with the given slave MAC address and index.<BR>
 * \param mac_address slave MAC address
 * \param index  port index
 * \param entry  port entry
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_port_set (char *mac_address, char *index,
                      libspid_eoc_port_entry_t *entry)
{
    const char delimiter = LIBSPID_PORT_CONF_DELIMITER[0];
    char key[LIBSPID_CONFIG_KEY_MAX_LEN] = {0};
    char *elt[4];
    libspid_error_t ret;
    libspid_eoc_port_entry_t port_get_entry;

    int i;

    int value;

    /* check input parameters */
    if ((mac_address == NULL) || (index == NULL) || (entry == NULL))
        return LIBSPID_ERROR_PARAM;

    if (LIBSPID_SUCCESS != libspid_system_nscrtv_eponeoc_mod_eoc (&value))
        return LIBSPID_ERROR_SYSTEM;

    /* check that MAC address and port index already exist */
    ret = libspid_eoc_port_get (mac_address, index, &port_get_entry);

    if (LIBSPID_SUCCESS != ret)
    {
        return ret;
    }

    /* use slave MAC and port index to compose */
    /* key for line in configuration file */
    sprintf (key, "%s.%s", mac_address, index);

    /* allocate buffers */
    for (i = 0; i < 4; i++)
    {
        elt[i] = (char *) malloc (5 * sizeof (char));
        if (NULL != elt[i])
            memset (elt[i], 0, 5);
        else
        {
            LIBSPID_EOC_LOG_2 ("%s: error allocating element "
                               "buffers (errno=%d)", __FUNCTION__, errno);
            return LIBSPID_ERROR_SYSTEM;
        }
    }

    strcpy (elt[0], entry->port_en);
    strcpy (elt[1], entry->service_index);
    if (LIBSPID_FALSE == value)
    {
        strcpy (elt[2], entry->vlan_tag_en);
        strcpy (elt[3], entry->vlan_mark_id);
    }
    else
    {
        strcpy (elt[2], "0");
        strcpy (elt[3], "0");
    }


    ret = libspid_config_write_line (LIBSPID_PORT_CONF_PATH,
                                     delimiter, key, 4, elt);

    /* free element buffers */
    for (i = 0; i < 4; i++)
    {
        if (elt[i])
        {
            free (elt[i]);
            elt[i] = NULL;
        }
    }

    if (ret != LIBSPID_SUCCESS)
    {
        LIBSPID_EOC_LOG_2 ("%s: cannot write port entry (errno=%d)",
                           __FUNCTION__, errno);
        return ret;
    }

    return LIBSPID_SUCCESS;
}


/**
 * Synchronize ports specified in config file with the current settings.<BR>
 * Get list of entries added, changed or removed in
 * config file and propagate it via MMEs.
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 */
extern libspid_error_t
libspid_eoc_port_sync (void)
{
    /* pointer to list of port entries read from config file */
    libspid_eoc_port_entry_t *port_config_entries;
    libspid_error_t error = LIBSPID_SUCCESS;
    unsigned int i, j, port_count, sta_count, curr_count, mme_num, remain;
    int k, t, idx, temp_count;
    unsigned char *mme_body;

    mme_body = NULL;
    port_config_entries = NULL;

    /* allocate and initialize pointer to list of port entries */
    /* read from config file */
    port_config_entries = (libspid_eoc_port_entry_t *)
        malloc (LIBSPID_PORT_TOTAL_MAX * sizeof (libspid_eoc_port_entry_t));

    if (NULL == port_config_entries)
    {
        LIBSPID_EOC_LOG_2 ("%s: error allocating port config array(errno=%d)",
                           __FUNCTION__, errno);
        return LIBSPID_ERROR_SYSTEM;
    }

    memset (port_config_entries, 0x0,
            LIBSPID_PORT_TOTAL_MAX * sizeof (libspid_eoc_port_entry_t));

    /* get current contents of port config file */
    error = libspid_eoc_port_get_list (port_config_entries, &temp_count);

    if (LIBSPID_SUCCESS != error)
    {
        LIBSPID_EOC_LOG_1 ("%s: port config file not read correctly\n",
                           __FUNCTION__);
        goto libspid_eoc_port_sync_end;
    }

    port_count = temp_count;

    sta_count = port_count / LIBSPID_PORT_PER_SLAVE_MAX;

    /* allocate and initialize pointer to MME body */
    if (sta_count == 0)
    {
        /* prepare and send MME with number of stations = 0 */
        /* allocate and initialize pointer to MME body */
        mme_body = (unsigned char *) malloc (sizeof (unsigned char));
        if (NULL == mme_body)
        {
            LIBSPID_EOC_LOG_2 ("%s: error allocating MME body (errno=%d)",
                               __FUNCTION__, errno);
            error = LIBSPID_ERROR_SYSTEM;
            goto libspid_eoc_port_sync_end;
        }

        memset (mme_body, 0x0, 1);
        mme_body[0] = sta_count;

        error = port_send_single_value (VS_EOC_SET_PORTS,
                                        mme_body, 1);
        if (error != LIBSPID_SUCCESS)
        {
            LIBSPID_EOC_LOG_1 ("%s: port set MME not sent correctly\n",
                               __FUNCTION__);
            goto libspid_eoc_port_sync_end;
        }

        error = LIBSPID_SUCCESS;
        goto libspid_eoc_port_sync_end;
    }
    else
    {
        mme_body = (unsigned char *) malloc ((sta_count
                                              * LIBSPID_PORT_BYTE_PER_STA + 1)
                                              * sizeof (unsigned char));
        if (NULL == mme_body)
        {
            LIBSPID_EOC_LOG_2 ("%s: error allocating MME body (errno=%d)",
                               __FUNCTION__, errno);
            error = LIBSPID_ERROR_SYSTEM;
            goto libspid_eoc_port_sync_end;
        }
        memset (mme_body, 0x0, sta_count * LIBSPID_PORT_BYTE_PER_STA + 1);

        /* calculate number of MMEs to fully pack */
        mme_num = sta_count / LIBSPID_PORT_STA_MAX_CNT;
        /* calculate remainder of station count */
        remain = sta_count % LIBSPID_PORT_STA_MAX_CNT;

        /* pack contents of port list to MMEs */
        for (i = 0; i <= mme_num; i++)
        {
            /* determine count of stations for current MME */
            if (i == mme_num)
                curr_count = remain;
            else
                curr_count = LIBSPID_PORT_STA_MAX_CNT;

            /* pack count of stations */
            mme_body[0] = curr_count;

            /* pack data for ports */
            for (j = 0; j < curr_count; j++)
            {
                idx = i* LIBSPID_PORT_STA_MAX_CNT + j;
                t = j * LIBSPID_PORT_BYTE_PER_STA;

                /* pack MAC address of current station */
                error = libspid_mac_str_to_bin (
                            port_config_entries[idx
                            * LIBSPID_PORT_PER_SLAVE_MAX].mac_addr,
                            mme_body + t + 1);
                if (error != LIBSPID_SUCCESS)
                    goto libspid_eoc_port_sync_end;

                /* pack ports for current station */
                for (k = 0; k < LIBSPID_PORT_PER_SLAVE_MAX; k++)
                {
                    /* pack port enable */
                    mme_body[t+7+2*k] = (uint8_t) strtol (
                        port_config_entries[idx
                        * LIBSPID_PORT_PER_SLAVE_MAX
                        + k].port_en, NULL, 10);
                    /* pack service index */
                    mme_body[t+8+2*k] = (uint8_t) strtol (
                        port_config_entries[idx
                        * LIBSPID_PORT_PER_SLAVE_MAX
                        + k].service_index, NULL, 10);
                }

            }

            error = port_send_single_value (VS_EOC_SET_PORTS,
                                            mme_body, curr_count *
                                            LIBSPID_PORT_BYTE_PER_STA + 1);
            if (error != LIBSPID_SUCCESS)
            {
                LIBSPID_EOC_LOG_1 ("%s: port SET MME not sent correctly\n",
                                   __FUNCTION__);
                goto libspid_eoc_port_sync_end;
            }
        }

        error = LIBSPID_SUCCESS;
        goto libspid_eoc_port_sync_end;
    }

libspid_eoc_port_sync_end:
    if (mme_body != NULL)
         free (mme_body);
    if (port_config_entries != NULL)
        free (port_config_entries);

    return error;
}

/** internal helper function for checking parameters of port.conf */
int
port_check_input_parameters (libspid_eoc_port_entry_t *port_entry,
                             libspid_eoc_port_entry_t *port_entries_cnf,
                             int port_count_cnf,
                             libspid_port_check_error_t *error_types)
{
    int error_number = 0;
    int i = 0, value = 0;
    int ret = 0;
    char *pnumber;
    libspid_eoc_service_entry_t tmp_service_entry;

    if (port_entry == NULL || port_entries_cnf == NULL ||
        error_types == NULL)
    {
        return -1;
    }

    /** check MAC address*/
    if (strlen (port_entry->mac_addr) != LIBSPID_MAC_STR_LEN-1)
        ret = -1;

    for (i = 0; i < LIBSPID_MAC_STR_LEN-1; i++)
    {
        port_entry->mac_addr[i] = tolower (port_entry->mac_addr[i]);
        if (!((port_entry->mac_addr[i] >= '0'
               && port_entry->mac_addr[i] <= '9')
              || (port_entry->mac_addr[i] >= 'a'
               && port_entry->mac_addr[i] <= 'f')
              || port_entry->mac_addr[i] == ':'))
            ret = -1;

        if ((i % 3) == 2 && port_entry->mac_addr[i] != ':')
            ret = -1;
    }

    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_PORT_MAC_FORMAT;
        error_number++;
        ret = 0;
    }

    /** check port INDEX value */
    value = strtol (port_entry->port_index, &pnumber, 10);
    if (*pnumber != '\0' || strcmp (port_entry->port_index,"") == 0)
        ret = -1;
    else
        ret = (value < 0 || value > LIBSPID_PORT_PER_SLAVE_MAX-1) ? -1 : 0;

    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_PORT_INDEX_VALUE;
        error_number++;
        ret = 0;
    }

    /** check port enable value */
    ret = (strcmp (port_entry->port_en, "0") == 0 ||
           strcmp (port_entry->port_en, "1") == 0) ? 0 : -1;
    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_PORT_ENABLE_VALUE;
        error_number++;
        ret = 0;
    }

    /** check service index value */
    value = strtol (port_entry->service_index, &pnumber, 10);
    if (*pnumber != '\0' || strcmp (port_entry->service_index,"") == 0)
        ret = -1;
    else
        ret = (value < 0 || value >= LIBSPID_SERVICE_AMOUNT_LIMIT) ? -1 : 0;

    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_PORT_SERVICE_VALUE;
        error_number++;
        ret = 0;
    }

    /** check port vlan enable value */
    ret = (strcmp (port_entry->vlan_tag_en, "0") == 0 ||
           strcmp (port_entry->vlan_tag_en, "1") == 0) ? 0 : -1;

    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_PORT_VLANEN_VALUE;
        error_number++;
        ret = 0;
    }

    /** check vlan id value */
    value = strtol (port_entry->vlan_mark_id, &pnumber, 10);
    if (*pnumber != '\0' || strcmp (port_entry->vlan_mark_id,"") == 0)
        ret = -1;
    else
        ret = (value < 0 || value > LIBSPID_SERVICE_MAX_MATCH_VID) ? -1 : 0;

    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_PORT_VLANID_VALUE;
        error_number++;
        ret = 0;
    }

    /* skip check for special service index 0 (used as the default value) */
    if (0 != strcmp ("0", port_entry->service_index))
    {
        /** check if service index defined in service.conf */
        if (libspid_eoc_service_get (port_entry->service_index,
                                     &tmp_service_entry) != LIBSPID_SUCCESS)
        ret = -1;
    }

    if (ret != 0)
    {
        error_types[error_number] = LIBSPID_ERROR_PORT_SERVICE_DEFINED;
        error_number++;
        ret = 0;
    }

    return error_number;
}

static int
cmp_string_mac (const void *p1, const void *p2)
{
    return strcmp (((libspid_eoc_port_entry_t *)p1)->mac_addr,
                    ((libspid_eoc_port_entry_t *)p2)->mac_addr);
}


/**
 * Check port.conf file
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameters
 * \return LIBSPID_ERROR_NOT_FOUND: entry not found
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno for details
 * \return LIBSPID_ERROR_CHECK: check utility failed
 */
extern libspid_error_t
libspid_eoc_port_check (void)
{
    int port_count_cnf = 0, wl_count_cnf = 0;;
    int ret = -1;
    int i = 0;
    libspid_eoc_port_entry_t port_entries_cnf[LIBSPID_PORT_TOTAL_MAX];
    libspid_eoc_port_entry_t port_entries_cnf_sort[LIBSPID_PORT_TOTAL_MAX];
    libspid_eoc_wl_entry_t wl_entries_cnf_sort[LIBSPID_WHITE_LIST_MAX_STA_NUM];
    libspid_eoc_port_entry_t port_entry;
    char key_not_unique[LIBSPID_MAC_STR_LEN+2];
    libspid_boolean_t unique_address = LIBSPID_TRUE;

    LIBSPID_EOC_LOG_1 ("%s: Check port.conf...", __FUNCTION__);
    /** obtain the port list from configuration file */
    ret = libspid_eoc_port_get_list (port_entries_cnf, &port_count_cnf);
    if ( ret != LIBSPID_SUCCESS)
    {
        return ret;
    }

    ret = libspid_eoc_wl_get_list (wl_entries_cnf_sort, &wl_count_cnf);
    if ( ret != LIBSPID_SUCCESS)
    {
        return ret;
    }

    memcpy (port_entries_cnf_sort, port_entries_cnf,
            port_count_cnf * sizeof (libspid_eoc_port_entry_t));

    /* sort port.conf */
    qsort (port_entries_cnf_sort, port_count_cnf,
           sizeof (libspid_eoc_port_entry_t), cmp_string_mac);

    /* sort white_list.conf*/
    qsort (wl_entries_cnf_sort, wl_count_cnf,
           sizeof (libspid_eoc_wl_entry_t), cmp_string_mac);

    /* check if MAC.index is unique key */
    for (i = 0; i < port_count_cnf-4; i++)
    {
        if (strcmp (port_entries_cnf_sort[i].mac_addr,
                    port_entries_cnf_sort[i+1].mac_addr) == 0 &&
            strcmp (port_entries_cnf_sort[i].port_index,
                    port_entries_cnf_sort[i+1].port_index) == 0)
        {
            strcpy (key_not_unique, port_entries_cnf_sort[i].mac_addr);
            strcat (key_not_unique, ".");
            strcat (key_not_unique, port_entries_cnf_sort[i].port_index);
            break;
        }

        if (strcmp (port_entries_cnf_sort[i].mac_addr,
                    port_entries_cnf_sort[i+2].mac_addr) == 0 &&
            strcmp (port_entries_cnf_sort[i].port_index,
                    port_entries_cnf_sort[i+2].port_index) == 0)
        {
            strcpy (key_not_unique, port_entries_cnf_sort[i].mac_addr);
            strcat (key_not_unique, ".");
            strcat (key_not_unique, port_entries_cnf_sort[i].port_index);
            break;
        }

        if (strcmp (port_entries_cnf_sort[i].mac_addr,
                    port_entries_cnf_sort[i+3].mac_addr) == 0 &&
            strcmp (port_entries_cnf_sort[i].port_index,
                    port_entries_cnf_sort[i+3].port_index) == 0)
        {
            strcpy (key_not_unique, port_entries_cnf_sort[i].mac_addr);
            strcat (key_not_unique, ".");
            strcat (key_not_unique, port_entries_cnf_sort[i].port_index);
            break;
        }

        if (strcmp (port_entries_cnf_sort[i].mac_addr,
                    port_entries_cnf_sort[i+4].mac_addr) == 0 &&
            strcmp (port_entries_cnf_sort[i].port_index,
                    port_entries_cnf_sort[i+4].port_index) == 0)
        {
            strcpy (key_not_unique, port_entries_cnf_sort[i].mac_addr);
            strcat (key_not_unique, ".");
            strcat (key_not_unique, port_entries_cnf_sort[i].port_index);
            break;
        }
    }

    for (i = 0; i < port_count_cnf; i++)
    {
        int number_of_error = 0;
        libspid_port_check_error_t error_types[10];
        char test_unique_key[LIBSPID_MAC_STR_LEN+2] = {0};
        libspid_eoc_port_entry_t *res;
        int port_index[5] = {0};

        /* for each MAC address, verify that there is */
        /* required number of ports */
        if ((i % 5) == 0)
        {
            if (strcmp (port_entries_cnf_sort[i].mac_addr,
                        port_entries_cnf_sort[i+4].mac_addr) == 0)
            {
                port_index[atoi (port_entries_cnf_sort[i].port_index)] = 1;
                port_index[atoi (port_entries_cnf_sort[i+1].port_index)] = 1;
                port_index[atoi (port_entries_cnf_sort[i+2].port_index)] = 1;
                port_index[atoi (port_entries_cnf_sort[i+3].port_index)] = 1;
                port_index[atoi (port_entries_cnf_sort[i+4].port_index)] = 1;

                if (!(port_index[0] && port_index[1] && port_index[2] &&
                    port_index[3] && port_index[4]))
                {
                    LIBSPID_EOC_LOG_2 ("MAC %s must have required number "
                                       "of ports(%d)",
                                       port_entries_cnf_sort[i].mac_addr,
                                       LIBSPID_PORT_PER_SLAVE_MAX);
                    return LIBSPID_ERROR_CHECK;
                }
            }
            else
            {
                LIBSPID_EOC_LOG_2 ("MAC %s must have required "
                                   "number of ports(%d)",
                                   port_entries_cnf_sort[i].mac_addr,
                                   LIBSPID_PORT_PER_SLAVE_MAX);
                return LIBSPID_ERROR_CHECK;
            }
        }

        port_entry = port_entries_cnf[i];

        /* Check dependency between port.conf and white_list.conf file */
        /* check if MAC defined in port.conf but not in white_list.conf */
        res = bsearch (&port_entry, wl_entries_cnf_sort, wl_count_cnf,
                       sizeof (libspid_eoc_wl_entry_t), cmp_string_mac);
        if (res == NULL)
        {
            LIBSPID_EOC_LOG_1 ("MAC (%s) defined in port.conf but not "
                               "in white_list.conf", port_entry.mac_addr);
            ret = LIBSPID_ERROR_CHECK;
        }

        /* if MAC.index is not unique key */
        /* find error line */
        strcpy (test_unique_key, port_entry.mac_addr);
        strcat (test_unique_key, ".");
        strcat (test_unique_key, port_entry.port_index);

        /** check format of the input parameters */
        number_of_error = port_check_input_parameters (&port_entry,
                                                       port_entries_cnf,
                                                       port_count_cnf,
                                                       error_types);

        if (strcmp (key_not_unique, test_unique_key) == 0)
        {
            number_of_error += 1;
            error_types[number_of_error-1] = LIBSPID_ERROR_PORT_MAC_DUPLICATE;
        }

        if (number_of_error > 0)
        {
            int j = 0;

            ret = LIBSPID_ERROR_CHECK;
            LIBSPID_EOC_LOG_1 ("Error in line:%d\n", i+2);
            for (j = 0; j < number_of_error; j++)
            {
                switch (error_types[j])
                {
                    case LIBSPID_ERROR_PORT_MAC_FORMAT:
                        LIBSPID_EOC_LOG_0 ("Malformed MAC address.");
                        break;
                    case LIBSPID_ERROR_PORT_MAC_DUPLICATE:
                        LIBSPID_EOC_LOG_1 ("MAC_ADDRESS.PORT_NB (%s) is not "
                                           "unique key.", key_not_unique);
                        unique_address = LIBSPID_FALSE;
                        break;
                    case LIBSPID_ERROR_PORT_INDEX_VALUE:
                        LIBSPID_EOC_LOG_1 ("Wrong port index value specified "
                                           "(expected: 0..%d)",
                                           LIBSPID_PORT_PER_SLAVE_MAX-1);
                        break;
                    case LIBSPID_ERROR_PORT_ENABLE_VALUE:
                        LIBSPID_EOC_LOG_0 ("Wrong port enable flag specified "
                                           "(expected: 0..1)");
                        break;
                    case LIBSPID_ERROR_PORT_VLANEN_VALUE:
                        LIBSPID_EOC_LOG_0 ("Wrong VLAN tag enable flag "
                                           "specified (expected: 0..1)");
                        break;
                    case LIBSPID_ERROR_PORT_VLANID_VALUE:
                        LIBSPID_EOC_LOG_1 ("Wrong VLAN mark ID specified "
                                           "(expected: 0..%d)",
                                           LIBSPID_SERVICE_MAX_MATCH_VID);
                        break;
                    case LIBSPID_ERROR_PORT_SERVICE_DEFINED:
                        LIBSPID_EOC_LOG_0 ("Service entry index "
                                           "is not defined");
                        break;
                    case LIBSPID_ERROR_PORT_SERVICE_VALUE:
                        LIBSPID_EOC_LOG_1 ("Wrong service index value "
                                           "specified (expected: 0..%d)",
                                           LIBSPID_SERVICE_AMOUNT_LIMIT);
                        break;
                    default:
                        break;
                }
            }

            if (unique_address == LIBSPID_FALSE)
                break;
        }
        else if (number_of_error == -1)
            return LIBSPID_ERROR_PARAM;
    }

    if (unique_address == LIBSPID_TRUE)
    {
        /* Check dependency between port.conf and white_list.conf file */
        /* check if MAC defined in white_list.conf but not in port.conf*/
        for (i = 0; i < wl_count_cnf; i++)
        {
            libspid_eoc_wl_entry_t *res;
            libspid_eoc_wl_entry_t wl_entry;


            wl_entry = wl_entries_cnf_sort[i];

            res = bsearch (&wl_entry, port_entries_cnf_sort, port_count_cnf,
                           sizeof (libspid_eoc_port_entry_t), cmp_string_mac);
            if (res == NULL)
            {
                LIBSPID_EOC_LOG_1 ("MAC (%s) defined in white_list.conf "
                                   "but not in port.conf", wl_entry.mac_addr);
                ret = LIBSPID_ERROR_CHECK;
            }
        }
    }

    return ret;
}