summaryrefslogtreecommitdiff
path: root/cesar/cp/msg/src/msg_vs.c
blob: c26120afb192fb94b7248871f323ac283b4c25b3 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/msg/src/msg_vs.c
 * \brief   VS family MME.
 * \ingroup cp_msg
 */
#include "common/std.h"
#include "common/defs/homeplugAV.h"
#include "common/defs/ethernet.h"

#include "cp/cp.h"
#include "cp/mme.h"
#include "cp/msg/msg.h"

#include "cp/sta/mgr/net.h"
#include "cp/sta/mgr/sta_mgr.h"

#include "cp/msg/inc/msg_vs.h"
#include "cp/msg/inc/msg.h"
#include "lib/stats.h"

#include "ce/common/mod.h"

/**
 * Receive a VS_GET_TONEMAP.REQ.
 * \param  ctx  the control plane context.
 * \param  mme  the MME handle.
 * \param  mac_addr  the MAC address of remote station.
 * \param  tmi  the tonemap index.
 * \param  int_id  the current tonemap interval list identifier.
 * \param  dir  the direction of tonemap.
 * \return  true on success.
 */
bool
cp_msg_vs_get_tonemap_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                   mac_t *mac_addr,
                                   cp_msg_vs_get_tonemap_tmi_t *tmi,
                                   u8 *int_id,
                                   cp_msg_vs_get_tonemap_req_dir_t *dir)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (mac_addr);
    dbg_assert (tmi);
    dbg_assert (int_id);
    dbg_assert (dir);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        *mac_addr = bitstream_read_large (&mme->bitstream, 48);
        *tmi = bitstream_read (&mme->bitstream, 8);
        *int_id = bitstream_read (&mme->bitstream, 8);
        *dir = bitstream_read (&mme->bitstream, 8);

        if ((!MAC_IS_VALID (*mac_addr))
            ||(!TMI_IS_VALID(*tmi))
            || (*dir >= CP_MSG_VS_GET_TONEMAP_REQ_DIRECTION_NB))
            return false;

        return true;
    }
    return false;
}

/**
 * Start sending of a VS_GET_TONEMAP.CNF.
 * \param  ctx  the control plane context.
 * \param  peer  the peer info.
 * \param  result  the result.
 * \param  beacon_delta  the Delta t btwn beacon priod strt & 50/60Hz 0 cross.
 * \param  int_id  the Current tonemap interval list identifier.
 * \param  tms  the tone maps parameters.
 * \param  tmi  the requested tonemap.
 */
void
cp_msg_vs_get_tonemap_cnf_send (cp_t *ctx, cp_mme_peer_t *peer,
        cp_msg_vs_get_tonemap_cnf_result_t result,
        uint beacon_delta, u8 int_id,tonemaps_t *tms,
        cp_msg_vs_get_tonemap_tmi_t tmi)
{
    cp_mme_tx_t * mme;

    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (peer);
    dbg_assert (result < CP_MSG_VS_GET_TONEMAP_CNF_RESULT_NB);

    mme = cp_msg_mme_init (ctx, peer, VS_GET_TONEMAP_CNF);
    dbg_assert (mme);

    bitstream_write (&mme->bitstream, result, 8);
    bitstream_write (&mme->bitstream, beacon_delta, 32);

    uint i = 0;

    /* If tms is available */
    if (result != CP_MSG_VS_GET_TONEMAP_CNF_RESULT_FAILURE)
    {
        /* Tonemap interval list identifier (version) */
        bitstream_write (&mme->bitstream, int_id, 8);

        /* TMI default : index of default tonemap */
        bitstream_write (&mme->bitstream, tms->default_tmi, 8);

        /* TMI AV valid list. */
        u8 tm_count = 0;
        u32 tmi_list = tonemaps_get_tonemap_enabled_list (tms);

        for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB ; i++)
        {
            if (tmi_list & (1 << i))
                tm_count++;
        }

        bitstream_write (&mme->bitstream, tm_count, 8);

        for (i = TONEMAP_INDEX_NEGOTIATED_FIRST; i < TONEMAP_INDEX_NB; i++)
            if (tmi_list & (1 << i))
            {
                bitstream_write (&mme->bitstream, i, 8);
                tm_count--;
            }
        dbg_assert (tm_count == 0);

        /* Intervals. */
        tonemap_intervals_t *int_list = tms->intervals;
        dbg_assert (int_list);

        bitstream_write (&mme->bitstream, int_list->intervals_nb, 8);

        for (i = 0; i < int_list->intervals_nb; i++)
        {
            bitstream_write (&mme->bitstream,
                             int_list->interval[i].end_offset_atu, 16);
            bitstream_write (&mme->bitstream, int_list->interval[i].tmi, 8);
        }

        /* Requested TMI? */
        bitstream_write (&mme->bitstream, tmi, 8);
    }
    else
    {
        /* Tonemap interval list identifier (version) */
        bitstream_write (&mme->bitstream, 0, 8);
        /* TMI default */
        bitstream_write (&mme->bitstream, 0, 8);
        /* TMI length AV valid list. */
        bitstream_write (&mme->bitstream, 0, 8);
        /* Intervals length. */
        bitstream_write (&mme->bitstream, 0, 8);
        /* Invalid informations ; tmi ... */
        bitstream_write (&mme->bitstream, 0, 8);
    }

    if ((tmi == CP_MSG_VS_GET_TONEMAP_TMI_AND_INT_ONLY) ||
            (result != CP_MSG_VS_GET_TONEMAP_CNF_RESULT_SUCCESS))
    {
        /* tm fec ... */
        bitstream_write (&mme->bitstream, 0, 8);
        /* tm gi ... */
        bitstream_write (&mme->bitstream, 0, 8);
        for (i = 0; i < PHY_ALL_CARRIER_NB; i += 2)
            bitstream_write (&mme->bitstream, 0, 8);
    }
    else
    {
        /* Get tone map. */
        dbg_assert (PHY_CARRIER_NB < 2*2*BLK_SIZE);

        tonemap_t *tm = tonemap_get (tms, tmi);

        dbg_assert (tm);
        dbg_assert (tm->fecrate < CP_MSG_VS_GET_TONEMAP_CNF_FEC_NB);
        dbg_assert (tm->gil < CP_MSG_VS_GET_TONEMAP_CNF_GI_NB);

        bitstream_write (&mme->bitstream, tm->fecrate, 8);
        bitstream_write (&mme->bitstream, tm->gil, 8);

        u32* mods = (u32*)tm->tmdma_desc_head->data;

        for (i = 0; i < PHY_CARRIER_OFFSET; i ++)
        {
            bitstream_write (&mme->bitstream, 0, 4);
        }
        for (i = 0; i < CEIL_DIV(PHY_CARRIER_NB, 8); i ++)
        {
            bitstream_write (&mme->bitstream, *mods, 32);

            /* Check if the first block is full */
            if ((8*(i+1)) == (2*BLK_SIZE))
                mods = (u32*)tm->tmdma_desc_head->next->data;
            else
                mods++;
        }

        for (i = PHY_CARRIER_OFFSET + CEIL_DIV(PHY_CARRIER_NB, 8) * 8;
                i < PHY_ALL_CARRIER_NB; i ++)
        {
            bitstream_write (&mme->bitstream, 0, 4);
        }
    }
    cp_msg_mme_send (ctx, mme);
}

bool
cp_msg_vs_get_snr_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                               mac_t *mac_addr,
                               cp_msg_vs_get_snr_req_int_t *tm_int_i,
                               u8 *int_id,
                               u8 *carrier_gr)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (mac_addr);
    dbg_assert (tm_int_i);
    dbg_assert (int_id);
    dbg_assert (carrier_gr);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        *mac_addr = bitstream_read_large (&mme->bitstream, 48);
        *tm_int_i = bitstream_read (&mme->bitstream, 8);
        *int_id = bitstream_read (&mme->bitstream, 8);
        *carrier_gr = bitstream_read (&mme->bitstream, 8);

        if ((!MAC_IS_VALID (*mac_addr))
            ||(!TM_INT_I_IS_VALID(*tm_int_i))
            || (*carrier_gr >= CP_MSG_VS_GET_SNR_REQ_CARRIER_GR_NB))
            return false;

        return true;
    }
    return false;
}

void
cp_msg_vs_get_snr_cnf_send (cp_t *ctx, cp_mme_peer_t *peer,
                            const cp_msg_vs_get_snr_cnf_t *data)
{
    uint i = 0;
    cp_mme_tx_t * mme;

    dbg_assert (ctx);
    dbg_assert (peer);
    dbg_assert (data);
    dbg_assert (data->result < CP_MSG_VS_GET_SNR_CNF_RESULT_NB);
    dbg_assert (data->intervals_nb <= TONEMAP_INTERVAL_NB);
    dbg_assert (data->carrier_gr < CP_MSG_VS_GET_SNR_REQ_CARRIER_GR_NB);

    if (data->intervals)
    {
        dbg_assert (data->intervals->version == data->int_id);
        dbg_assert (data->intervals->intervals_nb == data->intervals_nb);
    }
    else
    {
        dbg_assert (data->intervals_nb == 0);
    }

    mme = cp_msg_mme_init (ctx, peer, VS_GET_SNR_CNF);
    dbg_assert (mme);

    bitstream_write (&mme->bitstream, data->result, 8);
    bitstream_write (&mme->bitstream, data->int_id, 8);
    bitstream_write (&mme->bitstream, data->intervals_nb, 8);
    for (i = 0; i < data->intervals_nb; i++)
    {
        bitstream_write (&mme->bitstream,
                         data->intervals->interval[i].end_offset_atu, 16);
    }
    bitstream_write (&mme->bitstream, data->tm_ber, 16);
    bitstream_write (&mme->bitstream, data->carrier_gr, 8);
    for (i = 0; i < CP_MSG_SNR_IN_MSG_NB; i++)
        bitstream_write (&mme->bitstream, data->snr[i], 32);

    bitstream_write_finalise (&mme->bitstream);

    cp_msg_mme_send (ctx, mme);
}

/**
 * Receive a VS_GET_SPECTRUM.REQ.
 * \param  ctx  the control plane context.
 * \param  mme  the MME handle.
 * \param  mac_addr  the MAC address of remote station.
 * \param  carrier_gr  the carrier group modulo 8.
 * \return  true on success.
 */
bool
cp_msg_vs_get_spectrum_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                    mac_t *mac_addr,
                                    cp_msg_vs_get_spectrum_req_carrier_gr_t
                                    *carrier_gr)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (mac_addr);
    dbg_assert (carrier_gr);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        *mac_addr = bitstream_read_large (&mme->bitstream, 48);
        *carrier_gr = bitstream_read (&mme->bitstream, 8);

        if ((!MAC_IS_VALID (*mac_addr))
            ||(!CARRIER_GR_IS_VALID(*carrier_gr)))
            return false;

        return true;
    }
    return false;
}

/**
 * Start sending of a VS_GET_SPECTRUM.CNF.
 * \param  ctx  the control plane context.
 * \param  peer  the peer info.
 * \param  result  the result.
 * \param  AvgAGC  the Average AGC.
 * \return  the message context.
 */
cp_mme_tx_t *
cp_msg_vs_get_spectrum_cnf_send_begin (cp_t *ctx, cp_mme_peer_t *peer,
                                       cp_msg_vs_get_spectrum_cnf_result_t
                                       result, u8 AvgAGC)
{
    cp_mme_tx_t * mme;

    dbg_assert (ctx);
    dbg_assert (peer);
    dbg_assert (result < CP_MSG_VS_GET_SPECTRUM_CNF_RESULT_NB);


    mme = cp_msg_mme_init (ctx, peer, VS_GET_SPECTRUM_CNF);
    dbg_assert (mme);

    bitstream_write (&mme->bitstream, result, 8);
    bitstream_write (&mme->bitstream, AvgAGC, 8);

    return mme;
}

/**
 * Fill the MME with lst of spectrum values for requested carrier group modulo
 * \param  ctx  the module context.
 * \param  mme  the MME message.
 * \param  spectrum  the struct that contains the spectrum value.
 */
void
cp_msg_vs_get_spectrum_cnf_send_spectrum (cp_t *ctx, cp_mme_tx_t *mme,
                                          cp_msg_vs_get_spectrum_cnf_spec_t
                                          *spectrum)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    bitstream_write (&mme->bitstream, spectrum->Imaginary, 16);
    bitstream_write (&mme->bitstream, spectrum->Real, 16);
}

/**
 * End sending of a VS_GET_SPECTRUM.CNF.
 * \param  ctx  the control plane context.
 * \param  mme  the MME to send.
 */
void
cp_msg_vs_get_spectrum_cnf_send_end (cp_t *ctx, cp_mme_tx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    bitstream_write_finalise (&mme->bitstream);

    cp_msg_mme_send (ctx, mme);
}

/**
 * Receive a VS_GET_LINK_STATS.REQ.
 * \param  ctx  the control plane context.
 * \param  mme  the MME handle.
 * \param  data  the received MME data.
 * \return  true on success.
 */
bool
cp_msg_vs_get_link_stats_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                      cp_msg_vs_get_link_stats_req_t *data)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (data);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        data->ReqType = bitstream_read (&mme->bitstream, 8);
        data->ReqID = bitstream_read (&mme->bitstream, 8);
        data->nid = bitstream_read_large (&mme->bitstream, 56);
        data->lid = bitstream_read (&mme->bitstream, 8);
        data->TLFlag = bitstream_read (&mme->bitstream, 8);
        data->Mgmt_Flag = bitstream_read (&mme->bitstream, 8);
        data->dasa = bitstream_read_large (&mme->bitstream, 48);

        if ((!MAC_IS_VALID (data->dasa))
            || (data->ReqType >= CP_MSG_VS_GET_LINK_STATS_REQ_REQTYPE_NB)
            || (data->TLFlag >= CP_MSG_VS_GET_LINK_STATS_REQ_TLFLAG_NB)
            || (data->Mgmt_Flag >= CP_MSG_VS_GET_LINK_STATS_REQ_MGMTFLAG_NB))
            return false;

        return true;
    }
    return false;
}

/**
 * Send a VS_GET_LINK_STATS.CNF.
 * \param  ctx  the control plane context.
 * \param  peer  the peer info.
 * \param  ReqID  the Request Identifier from the corresponding request.
 * \param  result  the result.
 * \param  Bad_CRC  the Bad CRC number from received PBs.
 * \return  the message context.
 */
void
cp_msg_vs_get_link_stats_cnf_send (cp_t *ctx, cp_mme_peer_t *peer,
                                   u8 ReqID,
                                   cp_msg_vs_get_link_stats_cnf_result_t
                                   result,
                                   u32 Bad_CRC)
{
    cp_mme_tx_t * mme;

    dbg_assert (ctx);
    dbg_assert (peer);
    dbg_assert (result < CP_MSG_VS_GET_LINK_STATS_CNF_RESULT_NB);


    mme = cp_msg_mme_init (ctx, peer, VS_GET_LINK_STATS_CNF);
    dbg_assert (mme);

    bitstream_write (&mme->bitstream, ReqID, 8);
    bitstream_write (&mme->bitstream, result, 8);
    bitstream_write (&mme->bitstream, Bad_CRC, 32);

    bitstream_write_finalise (&mme->bitstream);

    cp_msg_mme_send (ctx, mme);
}

/**
 * Start sending of a VS_GET_AMP_MAP.CNF.
 * \param  ctx  the control plane context.
 * \param  peer  the peer info.
 * \param  result  the result.
 * \param  amlen  the Number of amplitude map data entries.
 * \return  the message context.
 */
cp_mme_tx_t *
cp_msg_vs_get_amp_map_cnf_send_begin (cp_t *ctx, cp_mme_peer_t *peer,
                                      cp_msg_vs_get_amp_map_cnf_result_t
                                      result, u16 amlen)
{
    cp_mme_tx_t * mme;

    dbg_assert (ctx);
    dbg_assert (peer);
    dbg_assert (result < CP_MSG_VS_GET_AMP_MAP_CNF_RESULT_NB);


    mme = cp_msg_mme_init (ctx, peer, VS_GET_AMP_MAP_CNF);
    dbg_assert (mme);

    bitstream_write (&mme->bitstream, result, 8);
    bitstream_write (&mme->bitstream, amlen, 16);

    return mme;
}

/**
 * Fill the MME with lst of amp map data used for the 1536 max carriers
 * \param  ctx  the module context.
 * \param  mme  the MME message.
 * \param  ampmap  the amplitude map value.
 */
void
cp_msg_vs_get_amp_map_cnf_send_ampmap (cp_t *ctx, cp_mme_tx_t *mme,
                                       u8 ampmap)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    bitstream_write (&mme->bitstream, ampmap, 4);
}


/**
 * End sending of a VS_GET_AMP_MAP.CNF.
 * \param  ctx  the control plane context.
 * \param  mme  the MME to send.
 */
void
cp_msg_vs_get_amp_map_cnf_send_end (cp_t *ctx, cp_mme_tx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    bitstream_write_finalise (&mme->bitstream);

    cp_msg_mme_send (ctx, mme);
}

/**
 * Receive a VS_GET_STATS.REQ.
 * \param  ctx  the control plane context.
 * \param  mme  the MME handle.
 * \param  page_number  the page requested.
 * \return  true on success.
 */
bool
cp_msg_vs_get_stats_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                 u8 *page_number, u8* level)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (page_number);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        *page_number = bitstream_read (&mme->bitstream, 8);
        *level = bitstream_read (&mme->bitstream, 8);
        return true;
    }
    return false;
}

/**
 * Start sending of a VS_GET_STATS.CNF.
 * \param  ctx  the control plane context.
 * \param  peer  the peer info.
 * \return  the message context.
 */
cp_mme_tx_t *
cp_msg_vs_get_stats_cnf_send_begin (cp_t *ctx, cp_mme_peer_t *peer)
{
    cp_mme_tx_t * mme;

    dbg_assert (ctx);
    dbg_assert (peer);

    mme = cp_msg_mme_init (ctx, peer, VS_GET_STATS_CNF);
    dbg_assert (mme);

    return mme;
}

/**
 * Sending the VS_GET_STATS.CNF
 * \param  ctx  the ctx module.
 * \param  mme  the MME handle.
 * \param  page  the page to send.
 */
void
cp_msg_vs_get_stats_cnf_send (cp_t *ctx, cp_mme_tx_t *mme, u8 page,
                              u8 nb_pages, lib_stats_level_t level,
                              cp_msg_vs_get_stats_cnf_result_t result)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (result < CP_MSG_VS_GET_STATS_CNF_RESULT_NB);

    bitstream_write (&mme->bitstream, result, 8);
    bitstream_write (&mme->bitstream, nb_pages, 8);
    if (result == CP_MSG_VS_GET_STATS_CNF_RESULT_SUCCESS)
        lib_stats_get_page (&mme->bitstream, page, level);
}

/**
 * Ends sending the VS_GET_STATS.CNF
 * \param  ctx  the ctx module.
 * \param  mme  the MME handle.
 */
void
cp_msg_vs_get_stats_cnf_send_end (cp_t *ctx, cp_mme_tx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    cp_msg_mme_send (ctx, mme);
}

/**
 * Receive a IMAC_GET_DISCOVER_LIST.REQ.
 * \param  ctx  the control plane context.
 * \param  mme  the MME handle.
 * \return  true on success.
 */
bool
cp_msg_imac_get_discover_list_req_receive (cp_t *ctx, cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    return cp_msg_mme_read_error (ctx, mme);
}

/**
 * Start sending of a IMAC_GET_DISCOVER_LIST.CNF.
 * \param  ctx  the control plane context.
 * \param  peer  the peer info.
 * \param  result  the result of the request
 * \param  version  the format version of station list
 * \param  num_stations  the number of discovered stations
 * \return  the message context.
 */
cp_mme_tx_t *
cp_msg_imac_get_discover_list_cnf_send_begin (cp_t *ctx, cp_mme_peer_t *peer,
        cp_msg_imac_get_discover_list_cnf_result_t result, u8 version,
        u8 num_stations)
{
    cp_mme_tx_t * mme;

    dbg_assert (ctx);
    dbg_assert (peer);

    mme = cp_msg_mme_init (ctx, peer, IMAC_GET_DISCOVER_LIST_CNF);
    dbg_assert (mme);
    bitstream_write (&mme->bitstream, result, 8);
    bitstream_write (&mme->bitstream, version, 8);
    bitstream_write (&mme->bitstream, num_stations, 8);

    return mme;
}

/**
 * Send one station info field of IMAC_GET_DISCOVER_LIST.CNF
 * \param  ctx  the ctx module.
 * \param  mme  the MME handle.
 * \param  data  data to be sent.
 */
void
cp_msg_imac_get_discover_list_cnf_send (cp_t *ctx, cp_mme_tx_t *mme,
        const cp_msg_imac_discover_list_sta_info_t *data)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (data);

    bitstream_write_large (&mme->bitstream, data->mac, 48);
    bitstream_write (&mme->bitstream, data->ble_tx, 8);
    bitstream_write (&mme->bitstream, data->ble_rx, 8);
}

/**
 * Ends sending the IMAC_GET_DISCOVER_LIST.CNF
 * \param  ctx  the ctx module.
 * \param  mme  the MME handle.
 */
void
cp_msg_imac_get_discover_list_cnf_send_end (cp_t *ctx, cp_mme_tx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    cp_msg_mme_send (ctx, mme);
}

bool
cp_msg_vs_get_ce_stats_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                                    mac_t *mac_addr)
{
    dbg_assert (mme);
    dbg_assert (mac_addr);

    if (cp_msg_mme_read_error (ctx, mme))
    {
        *mac_addr = bitstream_read_large (&mme->bitstream, 48);
        if (MAC_IS_VALID (*mac_addr))
            return true;
    }
    return false;
}

void
cp_msg_vs_get_ce_stats_cnf_send (cp_t *ctx, cp_mme_peer_t *peer,
                                 u8 version,
                                 u8 result,
                                 sta_t *sta,
                                 tonemask_info_t *ti)
{
    dbg_assert (ctx);
    dbg_assert (result < CP_MSG_VS_GET_CE_STATS_CNF_RESULT_NB);

    cp_mme_tx_t *mme = cp_msg_mme_init (ctx, peer, VS_GET_CE_STATS_CNF);
    dbg_assert (mme);

    bitstream_write (&mme->bitstream, version, 8);
    bitstream_write (&mme->bitstream, result, 8);
    /* Result success, peer exists. */
    if (result == CP_MSG_VS_GET_CE_STATS_CNF_RESULT_SUCCESS)
    {
        dbg_assert (sta);
        dbg_assert (sta->rx_tonemaps);
        dbg_assert (sta->rx_tonemaps->intervals);
        dbg_assert (ti);
        bitstream_write (&mme->bitstream, sta->rx_tonemaps->default_tmi, 8);
        u8 intervals_nb = sta->rx_tonemaps->intervals->intervals_nb;
        bitstream_write (&mme->bitstream, intervals_nb, 8);
        /* For each intervals. */
        int i, m;
        for (i = 0; i < intervals_nb; i++)
        {
            bitstream_write (
                &mme->bitstream,
                sta->rx_tonemaps->intervals->interval[i].end_offset_atu, 16);
            if (sta->intervals)
                bitstream_write (&mme->bitstream,
                                 (i % mac_interval_repetition_count_) + 1,
                                 8);
            else
                bitstream_write (&mme->bitstream, 0, 8);
        }
        /* For each FSM. */
        tonemap_t *tm;
        int fsm_count = (sta->intervals ? mac_interval_fsm_count_ + 1 : 1);
        for (i = 0; i < fsm_count; i++)
        {
            ce_rx_bitloading_t *bl;
            u8 tmi;
            /* Main FSM corresponds to 0. */
            if (i == 0)
            {
                bl = &sta->ce_rx_bt;
                tmi = sta->rx_tonemaps->default_tmi;
            }
            else
            {
                dbg_assert (sta->intervals);
                bl = sta->intervals->intervals[i - 1];
                tmi = sta->intervals->tmi[i - 1];
            }
            dbg_assert (bl);
            bitstream_write (&mme->bitstream, i, 8);
            bitstream_write (&mme->bitstream, bl->fsm, 8);
            bitstream_write (&mme->bitstream, tmi, 8);
            bool is_negociated = TONEMAP_INDEX_IS_NEGOTIATED (tmi);
            if (is_negociated)
                tm = tonemap_get (sta->rx_tonemaps, tmi);
            else if (tmi < PHY_MOD_ROBO_NB)
                tm = &ti->tonemap_robo[tmi];
            else
                tm = NULL;

            if (tm)
            {
                bitstream_write (&mme->bitstream, tm->fecrate, 8);
                bitstream_write (&mme->bitstream, tm->ble, 8);

                /* Simplified representation of tone map. */
                u16 mod_count[CE_MOD_COUNT];
                for (m = 0; m < CE_MOD_COUNT; m++)
                    mod_count[m] = 0;
                if (is_negociated)
                {
                    TONEMAP_READ_BEGIN (tm, ti->tonemask, m)
                    {
                        mod_count[m]++;
                    }
                    TONEMAP_READ_END;
                }
                for (m = 0; m < CE_MOD_COUNT; m++)
                    bitstream_write (&mme->bitstream, mod_count[m], 2 * 8);
                bitstream_write_large (&mme->bitstream,
                                       tm->ber_target,
                                       8 * 8);
                bitstream_write_large (&mme->bitstream,
                                       tm->ber_target_reached,
                                       8 * 8);
                bitstream_write (&mme->bitstream, tm->nb_frame, 4 * 8);
                bitstream_write (&mme->bitstream, tm->nb_pb, 4 * 8);
            }
            else
            {
                int tm_part_size = (1 + 1 + 16 + 8 + 8 + 4 + 4) * 8;
                for (m = 0; m < tm_part_size / 64; m++)
                    bitstream_write_large (&mme->bitstream, 0, 64);
                uint tmp = tm_part_size % 64;
                if (tmp <= 32)
                    bitstream_write (&mme->bitstream, 0, tmp);
                else
                    bitstream_write_large (&mme->bitstream, 0, tmp);
            }

            bitstream_write (&mme->bitstream, bl->stats.curr_nb_pb, 8);
            bitstream_write (&mme->bitstream,
                             bl->stats.curr_nb_pb_crc_false,
                             8);
            bitstream_write (&mme->bitstream, bl->stats.curr_ber_sum, 4 * 8);
            for (m = 0; m < CE_RX_BL_BER_SLIDING_MEAN_NB; m++)
                bitstream_write_large (&mme->bitstream,
                                       bl->stats.curr_ber_mean[m],
                                       8 * 8);
            bitstream_write (&mme->bitstream,
                             bl->stats.curr_pberr_rate_mean,
                             4 * 8);
            for (m = 0; m < CE_RX_BL_RESTART_REASON_NB; m++)
                bitstream_write (&mme->bitstream, bl->stats.restart_reason[m],
                                 4 * 8);
            bitstream_write_large (&mme->bitstream,
                                   bl->stats.prev_ber_target,
                                   8 * 8);
            bitstream_write_large (&mme->bitstream,
                                   bl->stats.prev_ber_target_reached,
                                   8 * 8);
            for (m = 0; m < CE_RX_BL_BER_SLIDING_MEAN_NB; m++)
                bitstream_write_large (&mme->bitstream,
                                       bl->stats.prev_ber_mean[m],
                                       8 * 8);
            bitstream_write (&mme->bitstream,
                             bl->stats.prev_pberr_rate_mean,
                             4 * 8);
            bitstream_write (&mme->bitstream, bl->stats.last_restart_reason, 8);
        }
    }

    bitstream_write_finalise (&mme->bitstream);
    cp_msg_mme_send (ctx, mme);
}