summaryrefslogtreecommitdiff
path: root/cesar/cp/sta/action/src/misc.c
blob: cd6159e1295ebe64b86f165582dad6454619f5ec (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/sta/action/src/misc.c
 * \brief   STA action, miscellaneous definitions.
 * \ingroup cp_sta_action
 */
#include "common/std.h"
#include "cl/data_rate.h"
#include "mac/common/store.h"

#include "action.h"

#include "cp/inc/context.h"
#include "cp/inc/trace.h"
#include "cp/msg/msg.h"

#include "hal/arch/arch.h"  // for dsr lock and unlock

#include "common/defs/spidcom.h"
#include "mac/common/timings.h"

void
cp_sta_action_process_cc_who_ru_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    /* Only for CCo. */
    cp_nid_t nid;
    if (cp_msg_cc_who_ru_req_receive (ctx, mme, &nid))
    {
        /* If NID matches. */
        if (nid == cp_sta_own_data_get_nid (ctx))
        {
            cp_msg_cc_who_ru_cnf_t data;
            data.nid = nid;
            data.cco_mac = cp_sta_own_data_get_mac_address (ctx);
            cp_sta_own_data_t *own = cp_sta_mgr_get_sta_own_data (ctx);
            strcpy (data.avln_hfid, own->hfid_avln);
            cp_msg_cc_who_ru_cnf_send (ctx, &mme->peer, &data);
        }
    }
}

void
cp_sta_action_process_cc_relay_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    uint length;
    uint mmtype;
    if (cp_msg_cc_relay_req_receive (ctx, mme, &length, &mmtype))
    {
        switch (mmtype)
        {
            /* Special cases. */
        case CM_ENCRYPTED_PAYLOAD_IND:
        case CM_ENCRYPTED_PAYLOAD_RSP:
            mme->relay.ftei = MAC_TEI_BCAST;
            break;
        case CC_ASSOC_REQ:
            if (mme->peer.tei == MAC_TEI_UNASSOCIATED
                && mme->relay.mac_fa == MAC_BROADCAST
                && mme->relay.ftei == MAC_TEI_BCAST)
            {
                /* Send to CCo. */
                cp_mme_peer_t cco_peer;
                cp_net_get_cco_peer (ctx, cp_sta_mgr_get_our_avln (ctx),
                                     &cco_peer);
                mme->relay.mac_fa = cco_peer.mac;
                mme->relay.ftei = cco_peer.tei;
            }
            break;
            /* MME between unassociated STA. */
        case CM_UNASSOCIATED_STA_IND:
        case CC_ASSOC_CNF:
        case CC_WHO_RU_REQ:
        case CC_WHO_RU_CNF:
        case CM_HFID_REQ:
        case CM_HFID_CNF:
        case CM_SC_JOIN_REQ:
        case CM_SC_JOIN_CNF:
        case CM_STA_CAP_REQ:
        case CM_STA_CAP_CNF:
        case CM_MME_ERROR_IND:
        case CC_RELAY_REQ:
        case CC_RELAY_IND:
            /* No special condition. */
            break;
            /* MME between non authenticated STA. */
        case CC_LEAVE_REQ:
        case CC_LEAVE_CNF:
        case CC_LEAVE_IND:
        case CC_LEAVE_RSP:
        case CC_SET_TEI_MAP_REQ:
        case CC_SET_TEI_MAP_IND:
        case PH_PROXY_APPOINT_IND:
        case CM_CHAN_EST_IND:
        case CM_TM_UPDATE_IND:
        case CM_SET_KEY_REQ:
        case CM_SET_KEY_CNF:
        case CM_GET_KEY_REQ:
        case CM_GET_KEY_CNF:
            /* Should be associated. */
            if (!MAC_TEI_IS_STA (mme->peer.tei))
                return;
            break;
            /* Other. */
        default:
            /* Should be authenticated. */
            if (!MAC_TEI_IS_STA (mme->peer.tei) || !mme->encrypt)
                return;
            break;
        }
        cp_msg_cc_relay_ind_send (ctx, mme, mme->peer.mac, mme->peer.tei,
                                  length);
    }
}

void
cp_sta_action_process_cm_hfid_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    enum cp_msg_cm_hfid_req_reqtype_t type;
    cp_nid_t nid;
    char hfid[CP_HFID_SIZE + 1];
    const char *hfid_p = "";
    if (cp_msg_cm_hfid_req_receive (ctx, mme, &type, &nid, hfid))
    {
        cp_sta_own_data_t *own_data = cp_sta_mgr_get_sta_own_data (ctx);
        enum cp_msg_cm_hfid_cnf_restype_t res = type;
        switch (type)
        {
        case CM_HFID_REQ_REQTYPE_PROVIDE_MANUFACTURER_SET_HFID:
            hfid_p = own_data->hfid_manufacturer;
            break;
        case CM_HFID_REQ_REQTYPE_PROVIDE_USER_SET_HFID:
            hfid_p = own_data->hfid_user;
            break;
        case CM_HFID_REQ_REQTYPE_PROVIDE_NETWORK_HFID:
            if (nid == cp_sta_own_data_get_nid (ctx))
                hfid_p = own_data->hfid_avln;
            else
                res = CM_HFID_CNF_RESTYPE_FAILURE;
            break;
        case CM_HFID_REQ_REQTYPE_SET_USER_SET_HFID:
            if (mme->peer.tei == MAC_TEI_FOREIGN)
                cp_sta_own_data_set_hfid_user (ctx, hfid);
            else
                res = CM_HFID_CNF_RESTYPE_FAILURE;
            hfid_p = own_data->hfid_user;
            break;
        case CM_HFID_REQ_REQTYPE_SET_NETWORK_HFID:
            if (nid == cp_sta_own_data_get_nid (ctx))
            {
                if (mme->peer.tei == MAC_TEI_FOREIGN)
                    cp_sta_own_data_set_hfid_avln (ctx, hfid);
                else
                    res = CM_HFID_CNF_RESTYPE_FAILURE;
                hfid_p = own_data->hfid_avln;
            }
            else
                res = CM_HFID_CNF_RESTYPE_FAILURE;
            break;
        default:
            dbg_assert_default ();
        }
        cp_msg_cm_hfid_cnf_send (ctx, &mme->peer, res, hfid_p);
    }
}

void
cp_sta_action_process_cm_mme_error_ind (cp_t *ctx, cp_mme_rx_t *mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    if (CONFIG_TRACE)
    {
        cp_msg_cm_mme_error_ind_t data;
        if (cp_msg_cm_mme_error_ind_receive (ctx, mme, &data))
        {
            CP_TRACE (MME_ERROR_IND, mme->peer.tei, data.reason, data.rx_mmv,
                      data.rx_mmtype, data.offset);
        }
    }
}

void
cp_sta_action_process_cm_nw_info_req (cp_t *ctx, cp_mme_rx_t *rx_mme)
{
    dbg_assert (ctx);
    dbg_assert (rx_mme);

    cp_mme_tx_t *tx_mme;
    cp_msg_cm_nw_info_cnf_t data;
    cp_net_t* our_avln = NULL;
    cp_sta_t* cco_sta = NULL;
    uint NumNWs;

    if (cp_msg_cm_nw_info_req_receive (ctx, rx_mme))
    {
        /* Determine the number of AVLN     */
        /* Most likely will be:             */
        /* 0 if sta is not authenticated    */
        /* 1 if sta is authenticated        */
        if (cp_sta_own_data_get_authenticated_status (ctx) == true)
            NumNWs = 1;
        else
            NumNWs = 0;

        tx_mme = cp_msg_cm_nw_info_cnf_send_begin (ctx, &rx_mme->peer, NumNWs);

        if (NumNWs)
        {
            data.nid = cp_sta_own_data_get_nid (ctx);
            data.snid = cp_sta_own_data_get_snid (ctx);
            data.tei = cp_sta_own_data_get_tei (ctx);

            if (cp_sta_own_data_get_pco_status (ctx))
            {
                data.sta_role = CM_NW_INFO_CNF_STA_ROLE_PCO;
            }
            else if (cp_sta_own_data_get_cco_status (ctx))
            {
                data.sta_role = CM_NW_INFO_CNF_STA_ROLE_CCO;

                /* In this case the CCo's mac is own data mac */
                data.cco_mac = cp_sta_own_data_get_mac_address (ctx);
            }
            else
            {
                data.sta_role = CM_NW_INFO_CNF_STA_ROLE_STA;
            }

            our_avln = cp_sta_mgr_get_our_avln (ctx);
            dbg_assert (our_avln);

            if (our_avln)
                cco_sta = cp_net_get_cco (ctx, our_avln);

            if (cco_sta)
            {
                data.cco_mac = cp_sta_get_mac_address (cco_sta);
                slab_release (cco_sta);
            }

            data.access = cp_net_get_access (ctx, our_avln);
            data.num_coord_nets = 0;    /* Functionnality limitation */

            cp_msg_cm_nw_info_cnf_send (ctx, tx_mme, &data);
        }

        cp_msg_cm_nw_info_cnf_send_end (ctx, tx_mme);
    }
}


void
cp_sta_action_process_cm_nw_stats_req (cp_t *ctx, cp_mme_rx_t *rx_mme)
{
    dbg_assert (ctx);
    dbg_assert (rx_mme);

    cp_mme_tx_t *tx_mme;
    cp_net_t* our_avln = NULL;
    cp_sta_t* sta_list = NULL;
    sta_t* sta;
    uint NumSTAs = 0;

    if (cp_msg_cm_nw_stats_req_receive (ctx, rx_mme))
    {
        /* Make sure we have an avln */
        if (MAC_TEI_IS_STA (cp_sta_own_data_get_tei (ctx)))
        {
            our_avln = cp_sta_mgr_get_our_avln (ctx);

            /* Get authenticated stas */
            sta_list = cp_net_sta_get_first (ctx, our_avln, CP_NET_STA_ASSOC);
            while (sta_list)
            {
                if (cp_sta_get_authenticated (ctx, sta_list))
                {
                    NumSTAs++;
                }

                sta_list = cp_net_sta_get_next (ctx, our_avln, sta_list);
            }
        }
        else
        {
            NumSTAs = 0;
        }

        tx_mme = cp_msg_cm_nw_stats_cnf_send_begin (ctx, &rx_mme->peer,
                NumSTAs);

        if (NumSTAs)
        {
            sta_list = cp_net_sta_get_first (ctx, our_avln, CP_NET_STA_ASSOC);

            while (sta_list)
            {
                if (cp_sta_get_authenticated (ctx, sta_list))
                {
                    mac_t mac;
                    uint phy_dr_tx;
                    uint phy_dr_rx;
                    cl_data_rate_t tx_data_rate;
                    cl_data_rate_t rx_data_rate;

                    mac = cp_sta_get_mac_address(sta_list);

                    /* Get TX : local STA -> avln STA */
                    /* Get RX : avln STA -> local STA */
                    sta = mac_store_sta_get (ctx->mac_store,
                            cp_sta_get_tei(sta_list));

                    arch_dsr_lock();
                    tx_data_rate = sta->tx_data_rate;
                    rx_data_rate = sta->rx_data_rate;
                    arch_dsr_unlock();

                    /* Update DR with delay between last DR update and now */
                    data_rate_update_info(&tx_data_rate, 0);
                    data_rate_update_info(&rx_data_rate, 0);

                    /* Convert DR from octets/s to Mbit/s */
                    phy_dr_tx = (uint)((tx_data_rate.data_rate * 8 + 500000)
                            /1000000);
                    phy_dr_rx = (uint)((rx_data_rate.data_rate * 8 + 500000)
                            /1000000);

                    cp_msg_cm_nw_stats_cnf_send (ctx, tx_mme, mac,
                            phy_dr_tx, phy_dr_rx);

                    /* Clean. */
                    blk_release (sta);
                }

                sta_list = cp_net_sta_get_next (ctx, our_avln, sta_list);
            }
        }

        cp_msg_cm_nw_stats_cnf_send_end (ctx, tx_mme);
    }
}

void
reset_link_stats (mfs_t *mfs, cp_msg_cm_link_stats_tlflag_t transmit)
{
    switch (transmit)
    {
    case CM_LINK_STATS_TLFLAG_TRANSMIT:
        link_stats_tx_reset (&mfs->tx.stats);
        break;

    case CM_LINK_STATS_TLFLAG_RECEIVE:
        link_stats_rx_reset (&mfs->rx.stats);
        break;

    default:
        break;
    }
}

void
cp_sta_action_process_cm_link_stats_req (cp_t *ctx, cp_mme_rx_t *rx_mme)
{
    dbg_assert (ctx);
    dbg_assert (rx_mme);

    cp_msg_cm_link_stats_req_t req_data;
    cp_msg_cm_link_stats_restype_t res_type;
    cp_tei_t tei;
    mfs_t *mfs = NULL;

    res_type = CM_LINK_STATS_RESTYPE_SUCCESS;

    if (cp_msg_cm_link_stats_req_receive (ctx, rx_mme, &req_data))
    {
        cp_mme_tx_t *tx_mme;

        /* check req_type */
        if (req_data.req_type >= CM_LINK_STATS_REQTYPE_NB)
            res_type = CM_LINK_STATS_RESTYPE_FAILURE;

        /* Check nid requested is our avln */
        if (res_type == CM_LINK_STATS_RESTYPE_SUCCESS)
        {
            cp_net_t* our_avln;

            our_avln = cp_sta_mgr_get_our_avln (ctx);

            if (req_data.nid != our_avln->nid)
                res_type = CM_LINK_STATS_RESTYPE_FAILURE;
        }

        /* Get tei from mac */
        if (res_type == CM_LINK_STATS_RESTYPE_SUCCESS)
        {
            cp_sta_t *sta;

            sta = cp_sta_mgr_sta_get_from_mac (ctx, req_data.mac);
            if (sta)
            {
                // The station exists get the TEI.
                tei = cp_sta_get_tei (sta);
                slab_release (sta);
            }
            else
                res_type = CM_LINK_STATS_RESTYPE_FAILURE;
        }

        if (res_type == CM_LINK_STATS_RESTYPE_SUCCESS)
        {
            switch (req_data.transmit)
            {
            case CM_LINK_STATS_TLFLAG_TRANSMIT:
                mfs = mac_store_mfs_get(ctx->mac_store, true, false,
                        req_data.mgmt_flag, req_data.lid, tei);
                if (!mfs)
                    res_type = CM_LINK_STATS_RESTYPE_FAILURE;
                break;

            case CM_LINK_STATS_TLFLAG_RECEIVE:
                mfs = mac_store_mfs_get(ctx->mac_store, false, false,
                        req_data.mgmt_flag, req_data.lid, tei);
                if (!mfs)
                    res_type = CM_LINK_STATS_RESTYPE_FAILURE;
                break;

            default:
                break;
            }
        }

        tx_mme = cp_msg_cm_link_stats_cnf_send_begin (ctx, &rx_mme->peer,
                req_data.req_id, res_type);

        if (res_type == CM_LINK_STATS_RESTYPE_SUCCESS)
        {
            switch (req_data.req_type)
            {
                case CM_LINK_STATS_REQTYPE_GET_AND_RESET:
                    cp_msg_cm_link_stats_cnf_send (ctx, tx_mme, mfs,
                            req_data.transmit);
                    reset_link_stats(mfs, req_data.transmit);
                    break;

                case CM_LINK_STATS_REQTYPE_GET:
                    cp_msg_cm_link_stats_cnf_send (ctx, tx_mme, mfs,
                            req_data.transmit);
                    break;

                case CM_LINK_STATS_REQTYPE_RESET:
                    reset_link_stats(mfs, req_data.transmit);
                    break;

                default:
                    break;
            }
        }

        /* release the mfs access */
        if (mfs)
            blk_release (mfs);

        cp_msg_cm_link_stats_cnf_send_end (ctx, tx_mme);
    }
}


void
cp_sta_action_process_cm_sta_cap_req (cp_t *ctx, cp_mme_rx_t *rx_mme)
{
    dbg_assert (ctx);
    dbg_assert (rx_mme);
    cp_msg_cm_sta_cap_cnf_t data;

    if (cp_msg_cm_sta_cap_req_receive (ctx, rx_mme))
    {
        data.av_version = CP_AV_VERSION;
        data.mac = cp_sta_own_data_get_mac_address (ctx);
        data.oui = SPC_OUI;
        data.auto_connect = CP_ACS_CAP;
        data.smoothing = CP_SMOOTHING_CAP;
        data.cco_cap = CP_CCO_LEVEL;
        data.proxy_cap = CP_PCO_CAP;
        data.backup_cco_cap = CP_BACKUP_CCO_CAP;
        data.soft_handover = CP_SOFT_HANDOVER_CAP;
        data.two_sym_fc = CP_TWO_SYM_FC_CAP;
        data.max_fl_av = MAC_MAX_FL_MAX_FL;
        data.homeplug_11_cap = CP_HOMEPLUG_AV11;
        data.homeplug_101_int = CP_HOMEPLUG_AV101;
        data.regulatory_cap = CP_REGULATORY_CAP;
        data.bidir_burst = CP_BIDIRECTIONAL_BURSTING_CAP;
        data.implementation_version = CP_IMPLEMENTATION_VERSION;

        cp_msg_cm_sta_cap_cnf_send (ctx, &rx_mme->peer, &data);
    }
}

/**
 * Get ble value from tone map in TX.
 * \param  ctx  control plane context
 * \param  sta  distant station
 * \param  tmi  tonemap index
 * \return  the ble for this station in this tonemap
 */
static u8
cp_sta_action_get_ble (cp_t *ctx, sta_t *sta, int tmi)
{
    dbg_assert (ctx);

    if (TONEMAP_INDEX_IS_NEGOTIATED(tmi))
    {
        dbg_assert (sta);
        dbg_assert (sta->tx_tonemaps);
        dbg_assert (sta->tx_tonemaps->tm[tmi]);
        return sta->tx_tonemaps->tm[tmi]->ble;
    }
    else
    {
        dbg_assert (tmi < PHY_MOD_ROBO_NB);
        dbg_assert (ctx->mac_config);
        return ctx->mac_config->tonemask_info.tonemap_robo[tmi].ble;
    }
}

/**
 * Get the average ble value for one station.
 * \param  ctx  control plane context
 * \param  tei  the tei of the distant station
 * \return  the average ble for this station
 */
static u8
cp_sta_action_get_average_ble (cp_t *ctx, cp_tei_t tei)
{
    sta_t *sta = mac_store_sta_get (ctx->mac_store, tei);
    u8 average_ble;

    if (sta)
    {
        u8 int_nb = sta->tx_tonemaps->intervals->intervals_nb;

        if (int_nb)
        {
            int i;
            uint ble_sum = 0;
            u16 offset_atu;
            u16 last_offset_atu = 0;

            for (i = 0; i < int_nb; i++)
            {
                int tmi = sta->tx_tonemaps->intervals->interval[i].tmi;
                offset_atu =
                    sta->tx_tonemaps->intervals->interval[i].end_offset_atu;

                ble_sum +=
                    tonemap_ble_mant_2_uf5 (cp_sta_action_get_ble (ctx, sta,
                                                                   tmi))
                    * (offset_atu - last_offset_atu);

                last_offset_atu = offset_atu;
            }

            dbg_assert (last_offset_atu);

            average_ble =
                tonemap_ble_uf5_2_mant (ble_sum / last_offset_atu);
        }
        else
        {
            average_ble =
                cp_sta_action_get_ble (ctx, sta,
                                       sta->tx_tonemaps->default_tmi);
        }

        blk_release (sta);
    }
    else
    {
        average_ble = 0;
    }

    return average_ble;
}

void
cp_sta_action_process_cc_discover_list_req (cp_t *ctx, cp_mme_rx_t *rx_mme)
{
    dbg_assert (ctx);
    dbg_assert (rx_mme);

    if (cp_msg_cc_discover_list_req_receive (ctx, rx_mme))
    {
        cp_mme_tx_t *tx_mme;
        cp_msg_cc_discover_list_ctx_t disc_ctx;
        cp_net_t *our_net = NULL;

        /* Check if our station is associated in a network. */
        if (cp_sta_own_data_get_tei (ctx) != MAC_TEI_UNASSOCIATED)
        {
            our_net = cp_sta_mgr_get_our_avln(ctx);
        }

        /* Prepare CC_DISCOVER_LIST.CNF */
        tx_mme = cp_msg_cc_discover_list_cnf_send_begin (ctx, &rx_mme->peer,
                cp_sta_mgr_get_num_discovered_stas (ctx),
                cp_sta_mgr_get_num_discovered_net (ctx),
                &disc_ctx);

        /* Send NumStation */
        cp_msg_cc_discover_list_cnf_send_stations_begin (ctx, tx_mme,
                &disc_ctx);

        /* Get stations informations for each existing network. */
        cp_net_t *net;
        for (net = cp_sta_mgr_get_first_avln (ctx); net;
             net = cp_sta_mgr_get_next_avln (ctx, net))
        {
            /* Collect info valid for all stations of this net. */
            cp_msg_cc_discover_list_sta_t list;

            list.snid = cp_net_get_snid (ctx, net);
            list.access = cp_net_get_access (ctx, net);

            /* For all kind of association states we seek stations. */
            cp_net_sta_status_t assoc;

            for (assoc = 0; assoc < CP_NET_STA_NB; assoc++)
            {
                /* Check association. */
                if ((assoc == CP_NET_STA_ASSOC) && (net == our_net))
                    list.same_network = CC_DISCOVER_LIST_NET_SAME_NETWORK;
                else
                    list.same_network = CC_DISCOVER_LIST_NET_DIFFERENT_NETWORK;

                cp_sta_t *cp_sta;

                for (cp_sta = cp_net_sta_get_first (ctx, net, assoc);
                     cp_sta;
                     cp_sta = cp_net_sta_get_next (ctx, net, cp_sta))
                {
                    list.mac_addr = cp_sta_get_mac_address (cp_sta);
                    list.tei = cp_sta_get_tei (cp_sta);
                    list.cco_cap = cp_sta->cco_cap;
                    list.proxy_cap = cp_sta->pco_cap;
                    list.backup_cco_cap = cp_sta->backup_cco_cap;
                    list.cco_status = cp_sta_get_cco_status (cp_sta);
                    list.pco_status = cp_sta_get_pco_status (cp_sta);
                    list.backup_cco_status = cp_sta->is_backup_cco;
                    list.signal_level = 0x00;

                    /** Compute average ble of the sta. */
                    list.average_ble =
                        cp_sta_action_get_average_ble (ctx, list.tei);

                    cp_msg_cc_discover_list_cnf_send_station (ctx, tx_mme,
                                                              &list);
                }
            }
        }

        /* Send NumNetwork */
        cp_msg_cc_discover_list_cnf_send_net_begin (ctx, tx_mme, &disc_ctx);

        /* For each present network but our network if we have one. */
        for (net = cp_sta_mgr_get_first_avln (ctx); net;
             net = cp_sta_mgr_get_next_avln (ctx, net))
        {
            if (our_net != net)
            {
                /* Collect info for all valid net */
                cp_msg_cc_discover_list_net_t data;

                data.nid = cp_net_get_nid (ctx, net);
                data.snid = cp_net_get_snid (ctx, net);
                data.access = cp_net_get_access (ctx, net);
                data.hm = net->hm;
                data.numslots = net->avln_num_slots;

                if (net->network_mode == CP_NET_NM_COORDINATED)
                    /* Unsupported. */
                    data.coordinated_status =
                        CC_DISCOVER_LIST_COORD_COORDINATED_GROUP_UNKNOWN;
                else
                    data.coordinated_status =
                        CC_DISCOVER_LIST_COORD_NON_COORDINATED;
                data.offset = 0;

                cp_msg_cc_discover_list_cnf_send_net (ctx, tx_mme, &data);
            }
        }

        cp_msg_cc_discover_list_cnf_send_end (ctx, tx_mme);
    }
}