summaryrefslogtreecommitdiff
path: root/cp/station/src/station_actions.c
blob: ba0ec9a5934c65981df94fe828a2435af0bb63a4 (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
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/station/src/station_events.c
 * \brief   the events generated in station module
 * \ingroup cp_station
 */

#include "common/std.h"
#include "cp/beacon/beacons.h"
#include "cp/station/inc/station_actions.h"

extern cp_sta_t cp_sta_global;

extern int mme_buffer_count;
extern int mme_buffer_first;
extern int mme_buffer_last;
extern u8 * mme_tx_buffer[MME_BUFFER_MAXCOUNT];

/*
void 
station_ustt_expires(void)
{    
    printf("%u %s()\n", cyg_hal_sys_getpid(), __FUNCTION__);   
    sta_add_event(USTT_EXPIRES, NULL);    
}
*/
void
transmit_unassoc_sta_mme(void)
{
    //printf("%u %s()\n", cyg_hal_sys_getpid(), __FUNCTION__);
    msg_cm_unassociated_sta_ind_send(BROADCAST_MAC_AD);    
}

void 
init_system (void)
{
//    printf("%u %s()\n", cyg_hal_sys_getpid(), __FUNCTION__);
}

void 
launch_bbt_timer (void)
{
    bool sta_was_cco = false;
    uint min_bbt, max_bbt, bbt;
    
    //printf("%u %s()\n", cyg_hal_sys_getpid(), __FUNCTION__);
    // TODO check if before the previous reboot the sta was CCO
    // if yes, set sta_was_cco to true
    // calculate the BBT
    if (sta_was_cco)
    {
        min_bbt = MinCCoScanTime_ms;
        max_bbt = MaxCCoScanTime_ms;
    }
    else
    {
        min_bbt = MinScanTime_ms;
        max_bbt = MaxScanTime_ms;        
    }
    bbt = rand_in(min_bbt, max_bbt);
    dbg_assert( (bbt > MinCCoScanTime_ms) && (bbt < MaxScanTime_ms) );
    // set the BBT timer  
    //printf("%u bbt = %i ms\n", cyg_hal_sys_getpid(), bbt);
    cp_station_gen_timed_event(&station_flag, STATION_FLAG_FSM, BBT_EXPIRES, bbt);

}

uint rand_in(uint min, uint max)
{
    double tmp;
    
    if(max)
    {
        dbg_assert(min < max);
        tmp = rand_r( & station_seed);
        tmp /= RAND_MAX;
        tmp *= (max - min);
        return (uint) (tmp + min);
    }
    else return rand_r( & station_seed);
}

void 
launch_beacon_timer (void)
{
    //printf("%u %s()\n", cyg_hal_sys_getpid(), __FUNCTION__);
    
}
void 
launch_ustt_timer (void)
{  
    uint ustt;

    //printf("%u %s()\n", cyg_hal_sys_getpid(), __FUNCTION__);

    ustt = rand_in(0, USAI_ms);
    // TODO si la station est USTA, ustt se choisit sur un autre intervalle
    if(ustt < 10) ustt = 10; // tick is 10 ms
    //printf("%u ustt = %i \n", cyg_hal_sys_getpid(), ustt);
    dbg_assert(ustt < USAI_ms);
    dbg_assert(ustt);
    cp_station_gen_timed_event(&station_flag, STATION_FLAG_FSM, USTT_EXPIRES, ustt);
 
}

void 
station_bbt_expires(void) // see figure 7-76
{
    process_usta_result_t process_usta_result;

    //printf("%u %s()\n", cyg_hal_sys_getpid(), __FUNCTION__);
    // check the condition "NID match & Should become CCO"
    if( (process_usta_result = cp_station_process_recorded_usta()) == BECOME_CCO)
    {
        cp_station_add_event(TO_CCO, NULL);
        return;
    }
    if(process_usta_result == CHECK_EXISTING_OTHER_AVLN)
    {
        // no other AVLN has been detected, and no matching nid so become uCCO
        // fire the transition anyway, it is protected with a positive state condition
        cp_station_add_event(POND_TO_UCCO, NULL);
        return;
    }
    // there are some matching nid or some other avln, so become usta
    // fire the condition anyway but we know that it some case it will not fire
    cp_station_add_event(TO_USTA, NULL);
}

VS_VOID 
record_usta_mme (VS_VOIDPTR mme_address)
{
    cm_unassociated_sta_t *cm_unassociated_sta;
    msg_mme_t *msg_mme;
    
    //printf("%u %s() mme_address = 0x%08lX\n", cyg_hal_sys_getpid(), __FUNCTION__, mme_address);
    dbg_assert ( !msg_check_wrong_mme_const_values (mme_address));
    
    msg_mme = mme_address;
    cm_unassociated_sta = (cm_unassociated_sta_t *) &msg_mme->mm_entry;
    cp_station_add_usta(msg_mme->osa, cm_unassociated_sta->cco_capability, cm_unassociated_sta->nid);    
}

VS_VOID 
process_usta_mme (VS_VOIDPTR mme_address)
{
    cm_unassociated_sta_t *cm_unassociated_sta;
    msg_mme_t *msg_mme;
    process_usta_result_t process_usta_result;
    
    //printf("%u %s()\n", cyg_hal_sys_getpid(), __FUNCTION__);
    dbg_assert ( !msg_check_wrong_mme_const_values (mme_address));

    msg_mme = mme_address;
    cm_unassociated_sta = (cm_unassociated_sta_t *) &msg_mme->mm_entry;
    process_usta_result = cp_station_process_usta(msg_mme->osa, cm_unassociated_sta->cco_capability, cm_unassociated_sta->nid);
    if(process_usta_result == BECOME_CCO) cp_station_add_event(TO_CCO, NULL); 
}

#define TRACE_FSM_STATE 1

VS_VOID trace_cco (VS_VOID)
{
    cp_station_set_cco_status(true);
#if TRACE_FSM_STATE
    printf("%u station 0x%016llx is now CCO\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.mac_address);
#endif
}
VS_VOID trace_pond (VS_VOID)
{
#if TRACE_FSM_STATE
    printf("%u station 0x%016llx is now POND\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.mac_address);
#endif
}
VS_VOID trace_sta (VS_VOID)
{
#if TRACE_FSM_STATE
    printf("%u station 0x%016llx is now associated STA\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.mac_address);
#endif
}
VS_VOID trace_ucco (VS_VOID)
{
#if TRACE_FSM_STATE
    printf("%u station 0x%016llx is now UCCO\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.mac_address);
#endif
}
VS_VOID trace_usta (VS_VOID)
{
#if TRACE_FSM_STATE
    printf("%u station 0x%016llx is now USTA\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.mac_address);
#endif
}

VS_VOID try_associate (VS_VOIDPTR beacon_address)
{
    beacon_t *beacon;
    
    dbg_assert(beacon_address);
    beacon = (beacon_t *) beacon_address;
    // check if NID match
    if(beacon->nid != cp_station_get_nid(NULL)) return; //TODO : pass CP ctx as first argument instead of NULL pointer...
    // if yes, then we start the association procedure (see 7.3.2)
    // first, send a cm_assoc.req to the cco
    msg_cc_assoc_req_send(ZEROS_MAC_AD);    
}

VS_VOID process_cc_assoc_req (VS_VOIDPTR mme_address)
{ // 11.2.28
    cc_assoc_req_t *cc_assoc_req;
    msg_mme_t *msg_mme;
    station_t sta;
    tei_t tei, next_tei;
    u16 lease_time_m = 0xF;   // default lease time in minutes
    u8 number_of_associated_sta;
    unsigned int i;

    dbg_assert ( !msg_check_wrong_mme_const_values (mme_address));
    msg_mme = (msg_mme_t *) mme_address;
    cc_assoc_req = (cc_assoc_req_t *) msg_mme->mm_entry;
    // check if nid match
    if(cc_assoc_req->nid != cp_station_get_nid(NULL)) return; //TODO : pass CP ctx as first argument instead of NULL pointer...
    // check req_type
    if(cc_assoc_req->req_type == 0)
    {
        // try to associate the new sta
        sta.cco_capa = cc_assoc_req->cco_capability;
        sta.last_discover_beacon = cyg_current_time();
        sta.tei_lease_time = sta.last_discover_beacon + lease_time_m * MINUTES_TO_TICK_FACTOR; 
        memcpy(sta.mac_address, msg_mme->osa, sizeof(mac_address_t));
        sta.proxy_net_capa = cc_assoc_req->proxy_networking_capability;
        sta.station_status = STATION_ASSOCIATED;
        tei = cp_station_associate_new(sta);
        if( ! tei)
        {
            // we failed to associate the sta
            msg_cc_assoc_cnf_send(msg_mme->osa, 1, 0, 0);
        }
        else
        {
            // we succed to associate the sta
            msg_cc_assoc_cnf_send(msg_mme->osa, 0, tei, lease_time_m);
            // send the message cc_set_tei_map.ind to the newly associated station
            number_of_associated_sta = cp_station_get_number_of_assoc();
            for(i=0 ; i<number_of_associated_sta ; i+= SET_TEI_MAP_SUB_NB_ELEM)
                msg_cc_set_tei_map_ind_send(msg_mme->osa, 0, 0, i);
            // and send cc_set_tei_map.ind in add mode to all other stations
            // BEWARE, msg_cc_set_tei_map_ind_send() also use station_get_associate_info function
            // and this may corrupt info
            next_tei = cp_station_get_associate_info(&sta, true, 0);
            for(i=1 ; i<number_of_associated_sta ; i++)
            {
                if(memcmp(sta.mac_address, msg_mme->osa, sizeof(mac_address_t)))
                    msg_cc_set_tei_map_ind_send(sta.mac_address, 1, tei, 0);
                next_tei = cp_station_get_associate_info(&sta, false, next_tei);            
            }        
        }
        return;
    }
    if(cc_assoc_req->req_type == 1)
    {
        // this is a TEI renewal
        // calculate the new lease_time
        if(sta.station_status == STATION_AUTHENTICATED)
            lease_time_m = LEASE_TIME_AUTH_STA;
        sta.tei_lease_time = cyg_current_time() + lease_time_m * MINUTES_TO_TICK_FACTOR;
        // process the renew
        tei = cp_station_renew(sta);
        if( ! tei)
        {
            // we failed to renew the tei
            msg_cc_assoc_cnf_send(msg_mme->osa, 1, 0, 0);
        }
        else
        {
            // we succed to associate the sta
            msg_cc_assoc_cnf_send(msg_mme->osa, 0, tei, lease_time_m);
            return;
        }
    }
    // how can we be there ???
    dbg_assert(0);
}

VS_VOID process_cc_assoc_cnf (VS_VOIDPTR mme_address)
{
    cc_assoc_cnf_t *cc_assoc_cnf;
    msg_mme_t *msg_mme;

    dbg_assert ( !msg_check_wrong_mme_const_values (mme_address));
    msg_mme = (msg_mme_t *) mme_address;
    cc_assoc_cnf = (cc_assoc_cnf_t *) msg_mme->mm_entry;
    // check if nid match
    if(cc_assoc_cnf->nid != cp_station_get_nid(NULL)) return; //TODO : pass CP ctx as first argument instead of NULL pointer...
    // if yes, check the assoc result
    if(cc_assoc_cnf->result == 0)
    {
        // association success
        cp_station_set_tei(cc_assoc_cnf->sta_tei, cc_assoc_cnf->lease_time);
        cp_station_add_event(TO_STA, NULL);
    }
    else
    {
        // et sinon quoi ?
        // TODO
    }
}

VS_VOID process_cc_set_tei_map_ind (VS_VOIDPTR mme_address)
{
    msg_mme_t *msg_mme;

    dbg_assert ( !msg_check_wrong_mme_const_values (mme_address));
    msg_mme = (msg_mme_t *) mme_address;
    msg_cc_set_tei_map_ind_send(msg_mme->osa, 0, 0, 0);
    
}

VS_VOID 
process_cc_set_tei_map_req (VS_VOIDPTR mme_address)
{//11.2.34
    u8 i;
    cc_set_tei_map_ind_t *cc_set_tei_map_ind;
    msg_mme_t *msg_mme;
    station_t sta;
    
    dbg_assert ( !msg_check_wrong_mme_const_values (mme_address));
    msg_mme = (msg_mme_t *) mme_address;
    cc_set_tei_map_ind = (cc_set_tei_map_ind_t *) msg_mme->mm_entry;
    switch(cc_set_tei_map_ind->mode)
    {
        case 0: // update entire sta list
            cp_station_clear_assoc_sta_list();
            // intentionaly no break here !!!
        case 1: // add some new sta
            for(i=0 ; i < cc_set_tei_map_ind->num ; i++)
            {
                sta.cco_capa = 0;
                sta.last_discover_beacon = 0;
                memcpy(sta.mac_address, cc_set_tei_map_ind->sub[i].mac_address, sizeof(mac_address_t));
                sta.proxy_net_capa = 0;
                if(cc_set_tei_map_ind->sub[i].status == 0x0)
                    sta.station_status = STATION_ASSOCIATED;
                else sta.station_status = STATION_AUTHENTICATED;
                sta.tei_lease_time = 0;
                cp_station_associate_old(cc_set_tei_map_ind->sub[i].tei, sta);
            }
            break;
        case 2: // remove sta from the list
            for(i=0 ; i < cc_set_tei_map_ind->num ; i++)
            {
                cp_station_remove_associated(cc_set_tei_map_ind->sub[i].tei);
            }
            break;              
    }    
}


VS_VOID 
send_discover_beacon_as_ucco (VS_VOID)
{
    // first, create and send a discover beacon
#if 0
    beacon_send_discover();
#else
    // TODO : call the right function dedicated to sending discover beacon,
    //        with right arguments (like the beacon context...)
    cp_beacon_cco_send_discover_beacon (cp_sta_global.cp_beacon); // TODO : the pointer to beacon context should be
                                                                  // an argument of the local send_discover_beacon_as_ucco() function...
#endif
    // then, launch the timer for the next beacon
    cp_station_gen_timed_event(&station_flag, STATION_FLAG_FSM, UCCO_MAX_DISCOVER_EXPIRES, MaxDiscoverPeriod_s * 1000);
}

/**
 * \brief Read and verify MME message header info.
 *
 * \param  msg  the MME message buffer   
 * \param  recv_mme_header  the pointer to structure where to copy header info extracted sequentially from MME message buffer
 *
 * \return size of header if valid header, else (-1) 
 */
int read_mme_header(VS_VOIDPTR msg, mme_header_type *recv_mme_header)
{
    u8 *buf = (u8*)msg;
    bitstream_t bstr_ctx;
    int header_size = (-1); // by default, -1 indicates invalid header (not finding IEEE ETHERTYPE 0x88E1 pattern at expected offset or other invalid field)

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);

    dbg_assert(msg);
    dbg_assert(recv_mme_header);

    //*(buf+12)=0x81; *(buf+13)=0x00; *(buf+14)=0x03; *(buf+15)=0x04; 

    /* decode whole header info, depending on "VLAN Tag" field is present or not */
    bitstream_init(&bstr_ctx,(u8*)msg,MME_HEADER_MAXSIZE,BITSTREAM_READ);
    memset(recv_mme_header,0x00,sizeof(*recv_mme_header));
    bitstream_access(&bstr_ctx,&(recv_mme_header->un_osa.u64),48);
    bitstream_access(&bstr_ctx,&(recv_mme_header->un_oda.u64),48);
    /* see if "VLAN Tag" field is present or not... */
    bitstream_access(&bstr_ctx,&(recv_mme_header->un_mtype.u32),16);
    if ((big_to_cpuhost_endian_u32(recv_mme_header->un_mtype.u32) >> 16) == (u32)MSG_MTYPE_IEEE_ETHERTYPE)
    {
        // Header without "AVLN Tag' field (IEEE_ETHERTYPE value 0x88E1 found at "VLAN tag" field offset => no "VLAN Tag" field in this header)
        printf("%u %s() : Header without AVLN Tag field...\n",cyg_hal_sys_getpid(),__FUNCTION__);
        recv_mme_header->vlan_tag_flag = false;
        header_size = MME_HEADER_MINSIZE;
    }
    else
    {
        // May be, Header with "AVLN Tag" field
        printf("%u %s() : May be, Header with AVLN Tag field...\n",cyg_hal_sys_getpid(),__FUNCTION__);
        recv_mme_header->vlan_tag_flag = true;
        header_size = MME_HEADER_MAXSIZE;
        // read last 16 bits of VLAN Tag field...
        bitstream_access(&bstr_ctx,&(recv_mme_header->un_vlan_tag.u32),16);
        // ... and recombine them with first 16 bits already read, ...
        recv_mme_header->un_vlan_tag.u32 = (recv_mme_header->un_vlan_tag.u32 << 16) | recv_mme_header->un_mtype.u32;
#if 0
        printf("%u %s() : vlan_tag = 0x%04x\n",cyg_hal_sys_getpid(),__FUNCTION__,(u16)(big_to_cpuhost_endian_u32(recv_mme_header->un_vlan_tag.u32)>>16));
#endif
        // ... and then, verify the IEEE 802.1Q VLAN Tag pattern (0x8100) at the expected offset
        if ((big_to_cpuhost_endian_u32(recv_mme_header->un_vlan_tag.u32)>>16) != (u32)MSG_VLAN_TAG_IEEE_802_1Q)
            header_size = (-1);
        // read the MTYPE field (16 bits)...
        bitstream_access(&bstr_ctx,&(recv_mme_header->un_mtype.u32),16);
        // ... and verify the IEEE ETHERTYPE pattern (0x88E1) at this offset...
        if (big_to_cpuhost_endian_u32(recv_mme_header->un_mtype.u32) == (u32)MSG_MTYPE_IEEE_ETHERTYPE)
            header_size = (-1);
    }

    bitstream_access(&bstr_ctx,&(recv_mme_header->un_mmv.u32),8);
    // HP_AV section 11.1.5 : Implementations based on HomePlug AV specification Version 1.1
    //                        shall discard all MMEs with MMV greater than 0x01
    if (recv_mme_header->un_mmv.u32 > 1)
        header_size = (-1);

    bitstream_access(&bstr_ctx,&(recv_mme_header->un_mmtype.u32),16);
    bitstream_access(&bstr_ctx,&(recv_mme_header->un_fmi.u32),16);
    bitstream_finalise(&bstr_ctx);

    big_to_cpuhost_endian(&(recv_mme_header->un_osa.b[0]),MAC_ADDR_SIZE);
    big_to_cpuhost_endian(&(recv_mme_header->un_oda.b[0]),MAC_ADDR_SIZE);
    big_to_cpuhost_endian(&(recv_mme_header->un_vlan_tag.b[0]),4);
    big_to_cpuhost_endian(&(recv_mme_header->un_mtype.b[0]),2);
    little_to_cpuhost_endian(&(recv_mme_header->un_mmtype.b[0]),4);

    if (header_size != (-1))
        printf
           ("%u /* VALID MME HEADER (len = %u bytes) :\n",cyg_hal_sys_getpid(),header_size);
    else
        printf
           ("%u /* INVALID MME HEADER :\n",cyg_hal_sys_getpid());

    display_mme_header(recv_mme_header);

    if (header_size == (-1))
    {
        printf ("%u /*\n",cyg_hal_sys_getpid());
        printf ("%u /* => IGNORING THIS INVALID MESSAGE !\n",cyg_hal_sys_getpid());
        printf ("%u /*\n",cyg_hal_sys_getpid());
    }
    return header_size;
}

/***
 * \brief Dump MME specific payload data.
 *
 * \param buffer  the MME message buffer
 * \param mme_header_len  the length of MME header
 * \param mme_data_len  the length of MME data  
 */
void
mme_payload_hexdump(u8 *buffer,int mme_header_len,int mme_data_len)
{
    int i, j;

    j = 0;
    printf("%u /* ",cyg_hal_sys_getpid());
    for (i = 0; i < (mme_data_len / 16); i++)
    {
        for (j = 0; j < 16; j++) printf ("%02x.",buffer[mme_header_len+(i*16)+j]);
        printf("\n");
        printf("%u /* ",cyg_hal_sys_getpid());
    }
    for (i = 0; i < (mme_data_len % 16); i++)
    {
        printf ("%02x.",buffer[mme_header_len+(j*16)+i]);
    }
    printf("\n");    
}

/***
 * \brief Build and send a ".CNF" MME message.
 *
 * \param msg_ctx  the incoming MME message context
 * \param recv_mme_header  the incoming MME header working data structure
 * \param cnf_result  the CNF result field value
 * \param cnf_errcode  the CNF error code field value
 * \param send_mme_len  pointer to result length of the MME to be sent
 * \mme_tx_buffer  buffer where to build the MME
 */
void
build_and_send_cnf(msg_ctx_t *msg_ctx,
                   mme_header_type *recv_mme_header,
                   mme_drv_cnf_type *send_mme,
                   e_drv_mme_cnf_result cnf_result,
                   e_drv_mme_cnf_errcode cnf_errcode,
                   uint *send_mme_len,
                   u8 *mme_tx_buffer)
{
    mfs_tx_t *txmfs;
    bool txmfs_added;

    /* preparing answer to incoming MME */
    cp_mme_build_cnf (recv_mme_header,
                      send_mme,
                      cnf_result,
                      cnf_errcode,
                      send_mme_len,
                      mme_tx_buffer,
                      &mme_buffer_first);

    /* as we've finished to work with the buffer associated to received MME, free this buffer... */
    printf("%u AVANT interface_mme_recv_done()...\n",cyg_hal_sys_getpid());
    interface_mme_recv_done (cp_sta_global.interface, msg_ctx->cl_mme_data);
    printf("%u APRES interface_mme_recv_done()...\n",cyg_hal_sys_getpid());

    /* answer to initial OSA (sending the answer to incoming MME) */
    if (msg_ctx->mfs == (mfs_rx_t *)NULL)
    {
        /* (answer = DRV MME ".CNF" for incoming DRV MME ".REQ") */
        printf("%u AVANT interface_mme_send()...\n",cyg_hal_sys_getpid());
        interface_mme_send (cp_sta_global.interface, mme_tx_buffer, *send_mme_len, NULL);
        printf("%u APRES interface_mme_send()...\n",cyg_hal_sys_getpid());
    }
    else
    {
        /* (answer to other incoming MME) */
        txmfs = mac_store_mfs_add_tx (cp_sta_global.mac_store, msg_ctx->mfs->common.bcast, true, msg_ctx->mfs->common.tei, MAC_LID_NONE, &txmfs_added);

        /* Vérif MFS : assert si NULL (aucune MFS disponible !?) */
        dbg_assert (txmfs);

        interface_mme_send (cp_sta_global.interface, mme_tx_buffer, *send_mme_len, txmfs);
        blk_release (txmfs);
        blk_release (msg_ctx->mfs);
    }
    /* update buffer management data */
    printf("%u ### AVANT liberation mme_buffer (first = %d ; last = %d ; count = %d) ###\n",
           cyg_hal_sys_getpid(), mme_buffer_first, mme_buffer_last, mme_buffer_count);
    mme_buffer_first = (mme_buffer_first + 1) % MME_BUFFER_MAXCOUNT;
    mme_buffer_count--;
    printf("%u ### APRES liberation mme_buffer (first = %d ; last = %d ; count = %d) ###\n",
           cyg_hal_sys_getpid(), mme_buffer_first, mme_buffer_last, mme_buffer_count);
}

VS_VOID 
process_drv_set_mac_address (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = MAC_ADDR_SIZE;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        bitstream_init(&bstr_ctx,((u8*)msg_ctx->buffer)+mme_header_len,pld_maxlen,BITSTREAM_READ);
        cp_sta_global.cp_sta_flash_params.mac_address = 0;
        bitstream_access(&bstr_ctx,&cp_sta_global.cp_sta_flash_params.mac_address,48);
        bitstream_finalise(&bstr_ctx);
#if 0
        big_to_cpuhost_endian((u8 *)&cp_sta_global.cp_sta_flash_params.mac_address,MAC_ADDR_SIZE);
        printf("%u /* @MAC ............ %02x:%02x:%02x:%02x:%02x:%02x (0x%016llx)\n",
               cyg_hal_sys_getpid(),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >> 40),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >> 32),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >> 24),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >> 16),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >>  8),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address      ),
               cp_sta_global.cp_sta_flash_params.mac_address
              );
#else
        printf("%u /* @MAC ............ %02x:%02x:%02x:%02x:%02x:%02x (0x%016llx)\n",
               cyg_hal_sys_getpid(),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address      ),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >>  8),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >> 16),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >> 24),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >> 32),
               (u8)(cp_sta_global.cp_sta_flash_params.mac_address >> 40),
               cp_sta_global.cp_sta_flash_params.mac_address
              );
#endif
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID 
process_drv_set_avln_hfid (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = MAX_HFID_SIZE;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        strncpy(cp_sta_global.cp_sta_flash_params.avln_hfid,&msg_ctx->buffer[mme_header_len],MAX_HFID_SIZE);
        cp_sta_global.cp_sta_flash_params.avln_hfid[MAX_HFID_SIZE] = '\0';
        if (!check_password_or_hfid_is_valid(cp_sta_global.cp_sta_flash_params.avln_hfid,0,MAX_HFID_SIZE))
        {
            cnf_result = E_DRVMME_RESULT_FAILURE;
            cnf_errcode = E_DRVMME_ERRCODE_INVALID_VALUE;
        }

        printf ("%u /* AVLN_HFID ....... %s\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.avln_hfid);
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID 
process_drv_set_cco_preference (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = 1;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;
    u8 cco_pref;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        bitstream_init(&bstr_ctx,((u8*)msg_ctx->buffer)+mme_header_len,pld_maxlen,BITSTREAM_READ);
        bitstream_access(&bstr_ctx,&cco_pref,8);
        bitstream_finalise(&bstr_ctx);

        if      (cco_pref == 0)
            cp_sta_global.cp_sta_flash_params.cco_preference = false;
        else if (cco_pref == 1)
            cp_sta_global.cp_sta_flash_params.cco_preference = true;
        else
        {
            cp_sta_global.cp_sta_flash_params.cco_preference = false;
            cnf_result = E_DRVMME_RESULT_FAILURE;
            cnf_errcode = E_DRVMME_ERRCODE_INVALID_VALUE;
        }

        printf ("%u /* preferred CCo ... %s\n",cyg_hal_sys_getpid(),get_str_yes_no(cp_sta_global.cp_sta_flash_params.cco_preference));
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID 
process_drv_set_dpw_req (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = MAX_PWD_SIZE;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        strncpy(cp_sta_global.cp_sta_flash_params.dpw,&msg_ctx->buffer[mme_header_len],MAX_PWD_SIZE);
        cp_sta_global.cp_sta_flash_params.dpw[MAX_PWD_SIZE] = '\0';
        if (!check_password_or_hfid_is_valid(cp_sta_global.cp_sta_flash_params.dpw,0,MAX_PWD_SIZE))
        {
            cnf_result = E_DRVMME_RESULT_FAILURE;
            cnf_errcode = E_DRVMME_ERRCODE_INVALID_VALUE;
        }

        printf ("%u /* DPW ............. %s\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.dpw);
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID 
process_drv_set_m_sta_hfid (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = MAX_HFID_SIZE;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        strncpy(cp_sta_global.cp_sta_flash_params.m_sta_hfid,&msg_ctx->buffer[mme_header_len],MAX_HFID_SIZE);
        cp_sta_global.cp_sta_flash_params.m_sta_hfid[MAX_HFID_SIZE] = '\0';
        if (!check_password_or_hfid_is_valid(cp_sta_global.cp_sta_flash_params.m_sta_hfid,0,MAX_HFID_SIZE))
        {
            cnf_result = E_DRVMME_RESULT_FAILURE;
            cnf_errcode = E_DRVMME_ERRCODE_INVALID_VALUE;
        }

        printf ("%u /* M_STA_HFID ...... %s\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.m_sta_hfid);
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID process_drv_set_nid (VS_VOIDPTR msg)
{
    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    
}

VS_VOID process_drv_set_npw (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = MAX_PWD_SIZE;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        strncpy(cp_sta_global.cp_sta_flash_params.npw,&msg_ctx->buffer[mme_header_len],MAX_PWD_SIZE);
        cp_sta_global.cp_sta_flash_params.npw[MAX_PWD_SIZE] = '\0';
        if (!check_password_or_hfid_is_valid(cp_sta_global.cp_sta_flash_params.npw,MIN_NPW_SIZE,MAX_PWD_SIZE))
        {
            cnf_result = E_DRVMME_RESULT_FAILURE;
            cnf_errcode = E_DRVMME_ERRCODE_INVALID_VALUE;
        }

        printf ("%u /* NPW ............. %s\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.npw);
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID process_drv_set_sl (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = 1;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        if (msg_ctx->buffer[mme_header_len] <= 2)
            cp_sta_global.cp_sta_flash_params.sl = msg_ctx->buffer[mme_header_len];
        else
        {
            cp_sta_global.cp_sta_flash_params.sl = 0;
            cnf_result = E_DRVMME_RESULT_FAILURE;
            cnf_errcode = E_DRVMME_ERRCODE_INVALID_VALUE;
        }
        printf ("%u /* SL .............. %u\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.sl);
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID process_drv_set_snid (VS_VOIDPTR msg)
{
    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    
}

VS_VOID process_drv_set_tonemask (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;
    u16 tonemask_offset;
    u16 tonemask_length_in_bits;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        pld_maxlen = 4 + (((msg_ctx->buffer[26] + (256 * msg_ctx->buffer[27])) + 7) / 8);

        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        tonemask_offset         = (u16)msg_ctx->buffer[mme_header_len]+256*(u16)msg_ctx->buffer[mme_header_len+1];
        tonemask_length_in_bits = (u16)msg_ctx->buffer[mme_header_len+2]+256*(u16)msg_ctx->buffer[mme_header_len+3];
        printf("%u /* tonemask cfg .... offset = %u ; length (in bits) = %u\n",
               cyg_hal_sys_getpid(),
               tonemask_offset,
               tonemask_length_in_bits);
        if ( ((tonemask_length_in_bits+7)/8) > TONEMASK_MAXSIZE)
        {
            cnf_result = E_DRVMME_RESULT_FAILURE;
            cnf_errcode = E_DRVMME_ERRCODE_INVALID_VALUE;
        }
        printf ("%u /* tonemask ........ (see payload dump)\n",cyg_hal_sys_getpid());
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID process_drv_set_u_sta_hfid (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = MAX_HFID_SIZE;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        strncpy(cp_sta_global.cp_sta_flash_params.u_sta_hfid,&msg_ctx->buffer[mme_header_len],MAX_HFID_SIZE);
        cp_sta_global.cp_sta_flash_params.u_sta_hfid[MAX_HFID_SIZE] = '\0';
        if (!check_password_or_hfid_is_valid(cp_sta_global.cp_sta_flash_params.u_sta_hfid,0,MAX_HFID_SIZE))
        {
            cnf_result = E_DRVMME_RESULT_FAILURE;
            cnf_errcode = E_DRVMME_ERRCODE_INVALID_VALUE;
        }

        printf ("%u /* U_STA_HFID ...... %s\n",cyg_hal_sys_getpid(),cp_sta_global.cp_sta_flash_params.u_sta_hfid);
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID 
process_drv_set_was_cco (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    bitstream_t bstr_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = 1;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;
    u8 was_cco;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;

        bitstream_init(&bstr_ctx,((u8*)msg_ctx->buffer)+mme_header_len,pld_maxlen,BITSTREAM_READ);
        bitstream_access(&bstr_ctx,&was_cco,8);
        bitstream_finalise(&bstr_ctx);

        if      (was_cco == 0)
            cp_sta_global.cp_sta_flash_params.was_cco = false;
        else if (was_cco == 1)
            cp_sta_global.cp_sta_flash_params.was_cco = true;
        else
        {
            cp_sta_global.cp_sta_flash_params.was_cco = false;
            cnf_result = E_DRVMME_RESULT_FAILURE;
            cnf_errcode = E_DRVMME_ERRCODE_INVALID_VALUE;
        }

        printf ("%u /* was CCo ......... %s\n",cyg_hal_sys_getpid(),get_str_yes_no(cp_sta_global.cp_sta_flash_params.was_cco));
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* answer to incoming MME (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}

VS_VOID process_drv_start_mac_req (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = 0;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;
    E_ErrCode ret;
    int i;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;
        /* (no MME specific payload data to decode)*/

        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* acknowledge MME request (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);

        /*
         * TODO : init some cp_sta_secu_params parameters from flash parameters
         *        and then, we shall/can start...
         */
        ret = secu_npw2nmk(cp_sta_global.cp_sta_flash_params.npw, cp_sta_global.cp_sta_secu_params.nmk);
        ret = secu_nmk2nid(cp_sta_global.cp_sta_secu_params.nmk, cp_sta_global.cp_sta_flash_params.sl, cp_sta_global.cp_sta_secu_params.nid);
        memcpy(&cp_sta_global.nid,cp_sta_global.cp_sta_secu_params.nid,7);
        //cp_sta_global.nid = cp_sta_global.nid >> 8;
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* STA params defined from NPW :\n",cyg_hal_sys_getpid());
        printf ("%u /* NMK_HS .......... ",cyg_hal_sys_getpid());
        for (i = 0; i < 16; i++) printf("%02x.",cp_sta_global.cp_sta_secu_params.nmk[i]);
        printf ("\n");
        printf ("%u /* NID ............. ",cyg_hal_sys_getpid());
        for (i = 0; i < 7; i++) printf("%02x.",cp_sta_global.cp_sta_secu_params.nid[i]);
        printf (" (0x%016llx)",cp_sta_global.nid);
        printf ("\n");
    }

}

VS_VOID process_drv_stop_mac_req (VS_VOIDPTR msg)
{
    msg_ctx_t *msg_ctx;
    mme_header_type recv_mme_header;
    int mme_header_len;
    uint pld_maxlen = 0;
    mme_drv_cnf_type send_mme;
    uint send_mme_len;
    e_drv_mme_cnf_result cnf_result;
    e_drv_mme_cnf_errcode cnf_errcode;

    diag_printf("%u %s()...\n",cyg_hal_sys_getpid(),__FUNCTION__);
    dbg_assert(msg);
    msg_ctx = (msg_ctx_t *)msg;

    /* decode MME header */
    mme_header_len = read_mme_header(msg_ctx->buffer,&recv_mme_header);

    if (mme_header_len != (-1))
    {
        /* dump MME specific payload data */
        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());
        printf ("%u /* MME PAYLOAD :\n",cyg_hal_sys_getpid());
        mme_payload_hexdump(msg_ctx->buffer,mme_header_len,pld_maxlen);

        /* decode MME specific payload data and prepare answer to incoming MME */
        cnf_result = E_DRVMME_RESULT_SUCCESS;
        cnf_errcode = 0 /*E_DRVMME_ERRCODE_BAD_PARAMETER*/;
        /* (no MME specific payload data to decode)*/

        printf ("%u /***************************************************/\n",cyg_hal_sys_getpid());

        /* acknowledge MME request (acknowledge MME request) */
        build_and_send_cnf(msg_ctx,
                           &recv_mme_header,
                           &send_mme,
                           cnf_result,
                           cnf_errcode,
                           &send_mme_len,
                           mme_tx_buffer[mme_buffer_first]);
    }

}