summaryrefslogtreecommitdiff
path: root/cp/beacon/src/beacons.c
blob: 0abd127089069d5dbf24c0ed1810b369d32fe76d (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/beacon/src/beacons.c
 * \brief   « brief description »
 * \ingroup « module »
 *
 * « long description »
 */
#include "common/std.h"

#include "cp/beacon/beacons.h"
#include "hal/phy/phy.h"

#include "cp/beacon/inc/beacons.h"
#include "cp/beacon/inc/bentry.h"
#include "cp/beacon/inc/beacons_ctx.h"
#include "cp/beacon/inc/trace.h"

#include "mac/ca/ca.h"
#include "mac/common/ntb.h"
#include "mac/common/timings.h"

static cp_beacon_t cp_beacon_global;

/**
 * Init the beacons module.
 *
 * \param  phy  the phy context.
 * \param  sta  the station context.
 * \param  secu  the control plane security layer context.
 * \param  ca  the Channel Access context.
 * \param  mac_store mac_store.
 * \param  pbproc  pbproc.
 * \param  cco  the cco context.
 * \return  the beacon context.
 */
cp_beacon_t *
cp_beacon_init(phy_t *phy, cp_sta_t *sta, cp_secu_t *secu, ca_t *ca,
               pbproc_t *pbproc, mac_store_t *mac_store, cp_cco_t *cco)
{
    uint i;
    cp_beacon_t *ctx;

    dbg_assert(phy);
    dbg_assert(sta);
    dbg_assert(secu);
    dbg_assert(ca);
    dbg_assert(mac_store);
    dbg_assert(pbproc);
    dbg_assert(cco);

    
    ctx = &cp_beacon_global;
    ctx->phy = phy;
    ctx->sta = sta;
    ctx->secu = secu;
    ctx->ca = ca;
    ctx->mac_store = mac_store;
    ctx->pbproc = pbproc;
    ctx->cco = cco;

    // Add trace
    cp_beacon_trace_init(ctx);
    CP_BEACON_TRACE (INIT, mac_ntb());

    // init all the context.
    ctx->last_central_beacon = NULL;
    ctx->mfs_discover_beacon = NULL;
    ctx->mfs_central_beacon = NULL;
    ctx->schedule_index = 0;
    ctx->hm = 0x0;

    // initialize the central beacon data.
    ctx->cbeacon.beacon_nb = 0;
    for (i = 0; i < 4; i++)
    {
        ctx->cbeacon.btse_ntb[i] = 0;
        ctx->cbeacon.bto_bpsto.bto[i] = 0;
        ctx->cbeacon.bp_tau[i] = 0;
    }

    // verify the AC line frequence.
    ctx->pwl.date_ntb[0] = 0;
    ctx->pwl.date_ntb[1] = 0;

    return ctx;
}

/**
 * Uninit the beacon module
 *
 * \param  ctx the beacon module.
 */
void
cp_beacon_uninit(cp_beacon_t *ctx)
{
    dbg_assert(ctx);

    if (ctx->last_central_beacon != NULL)
    {
        blk_release_desc ((blk_t *) ctx->last_central_beacon);
    }

    if (ctx->last_discover_beacon != NULL)
    {
        blk_release_desc ((blk_t *) ctx->last_discover_beacon);
    }

    if (ctx->mfs_central_beacon)
    {
        dbg_assert (ctx->mac_store);
        mac_store_mfs_remove (ctx->mac_store, (mfs_t *)
                              ctx->mfs_central_beacon);

        // Remove the MFS from the CA
        ca_mfs_remove (ctx->ca, ctx->mfs_central_beacon);
        // Release the mfs.
        blk_release (ctx->mfs_central_beacon);
    }

    if (ctx->mfs_discover_beacon)
    {
        dbg_assert (ctx->mac_store);
        mac_store_mfs_remove (ctx->mac_store, (mfs_t *)
                              ctx->mfs_discover_beacon);

        // Remove the MFS from the CA
        ca_mfs_remove (ctx->ca, ctx->mfs_discover_beacon);

        // Release the mfs.
        blk_release (ctx->mfs_discover_beacon);
    }

    CP_BEACON_TRACE (UNINIT, mac_ntb());
    cp_beacon_trace_uninit(ctx);
}

/**
 * Create the default schedule for the CA when the station is set as an
 * Unauthenticated station.
 *
 * \param  ctx  the cp beacon context
 */
void
cp_beacon_usta_create_schedule (cp_beacon_t *ctx)
{
    ca_beacon_period_t bp[4];
    ca_schedule_t *sched;
    uint i;

    dbg_assert (ctx);
    dbg_assert (ctx->ca);
    dbg_assert (ctx->pbproc);

    // Get the schedule 0
    if (ctx->pbproc_activated == false)
    {
        sched = ca_alloc_get_schedule (ctx->ca, 0);
        sched->coexistence_mode = CP_REGION_LOCAL_CSMA;
        sched->allocations_nb = 1;
        sched->allocations[0].end_offset_tck = 10000000;
        sched->allocations[0].glid = 0xFF;

        ctx->schedule_index ++;
    }

    for (i = 0; i < 4; i++)
    {
        bp[i].start_date = mac_ntb() + i*10000000;
        bp[i].schedule_index = 0;
    }

    // add the schedule to the CA.
    ca_alloc_update_beacon_periods (ctx->ca, bp, 4);

    if (!ctx->pbproc_activated)
    {
        ctx->pbproc_activated = true;
        pbproc_activate (ctx->pbproc, true);
    }

    CP_BEACON_TRACE (DEFAULT_SCHED, mac_ntb());
}

/**
 * Compute the AC line frequency from the PRATIC register
 *
 * \param  ctx  beacons module context
 */
void
cp_beacon_acl_frequency_detection (cp_beacon_t *ctx)
{
    uint bp_width_ntb;

    dbg_assert (ctx);
    dbg_assert (ctx->phy);

    ctx->pwl.date_ntb[1] = phy_clock_get_zero_cross_captured_date(ctx->phy);
    cyg_thread_delay(2);
    
    ctx->pwl.date_ntb[0] = phy_clock_get_zero_cross_captured_date(ctx->phy);

    // compute the becon period.
    bp_width_ntb = (ctx->pwl.date_ntb[0] - ctx->pwl.date_ntb[1] + 5000) % 250000;

    if (bp_width_ntb > 6000)
    {
        ctx->pwl.bp_ntb = 833333;
        ctx->pwl.pwzc_ntb = 208333;
    }
    else
    {
        ctx->pwl.bp_ntb = 1000000;
        ctx->pwl.pwzc_ntb = 250000;
    }

    CP_BEACON_TRACE (FREQ, mac_ntb(), ctx->pwl.bp_ntb);
}

/**
 * Compute the next BTS, the four next BTOs and the BP in Time Allocation
 * Unit to provide the data to the generated beacon.
 *
 * \param  ctx  the beacon context.
 * \param  common  the common part of the beacons structure.
 */
void
cp_beacon_estimate_bts_bto_bp (cp_beacon_t *ctx, cp_beacon_common_t *common)
{
    uint i;
    uint btse;
    uint per;

    dbg_assert (ctx);
    dbg_assert (common);

    if (common->beacon_nb == 0)
    {
        for (i = 0; i < 4; i++)
        {
            common->btse_ntb[i] = mac_ntb() + //ctx->pwl.date_ntb[0] +
                i*ctx->pwl.bp_ntb;
            common->bp_tau[i] = MAC_TCK_TO_ATU(ctx->pwl.bp_ntb);
        }

        common->beacon_nb ++;

        common->per_ntb = cp_beacon_pwl_period(ctx);
        return;
    }
    
    per = cp_beacon_pwl_period (ctx);
    per = (uint) ((int)common->per_ntb + (((int)per - (int)common->per_ntb) >>
            CP_BEACON_K));

    btse = (uint) ((int)common->btse_ntb[2] + (int)per +
            (((int)ctx->pwl.date_ntb[0] - ((int)common->btse_ntb[3] 
                                           + (int)per)) >> CP_BEACON_K));

    for (i = 0; i < 3; i++)
    {
        common->btse_ntb[i] = common->btse_ntb[i+1];
        common->bto_bpsto.bto[i] = common->bto_bpsto.bto[i+1];
        common->bp_tau[i] = common->bp_tau[i+1]; 
    }

    common->per_ntb = per;

    common->btse_ntb[3] += per;
    common->bp_tau[3] = MAC_TCK_TO_ATU(per);
    common->bto_bpsto.bto[3] = per - ctx->pwl.bp_ntb;
        
    common->beacon_nb ++;

    CP_BEACON_TRACE (ESTIMATION, mac_ntb(), btse, per, common->bto_bpsto.bto[3]);
}

/**
 * Compute the beacon period using the beacon period thereotic calculated
 * during the pw frequency detection.
 *
 * \param  ctx  the beacon period context.
 */
uint
cp_beacon_pwl_period (cp_beacon_t *ctx)
{
    uint per_ntb;
    uint diff_ntb;

    dbg_assert (ctx);
    dbg_assert (ctx->pwl.bp_ntb);

    ctx->pwl.date_ntb[1] = ctx->pwl.date_ntb[0];
    ctx->pwl.date_ntb[0] = phy_clock_get_zero_cross_captured_date (ctx->phy);

    diff_ntb = (ctx->pwl.date_ntb[0] - ctx->pwl.date_ntb[1]) % ctx->pwl.bp_ntb + 10000;
    per_ntb = (4-(diff_ntb / ctx->pwl.pwzc_ntb)) * ctx->pwl.pwzc_ntb + diff_ntb - 10000;

    CP_BEACON_TRACE (PWL_PER, mac_ntb(), ctx->pwl.date_ntb[1],
                     ctx->pwl.date_ntb[0], per_ntb);

    return per_ntb;
}

/**
 * Process the received beacon.
 *
 * \param  ctx  the beacon context.
 * \param  beacon  the beacon received.
 */
void
cp_beacon_process (cp_beacon_t *ctx, cp_beacon_desc_t *beacon)
{
    dbg_assert (ctx);
    dbg_assert (beacon);

    switch (beacon->payload->bt)
    {
        case CP_BEACON_CENTRAL_BEACON:
            cp_beacon_process_central_beacon (ctx, beacon);
            break;
        case CP_BEACON_DISCOVER_BEACON:
            cp_beacon_process_discover_beacon (ctx, beacon);
            break;
        default:
            cp_beacon_process_proxy_beacon (ctx, beacon);
    }
}

/**
 * Process a the central beacon received.
 *
 * \param  ctx  the cp_beacon context
 * \param  beacon  the beacon received.
 */
void
cp_beacon_process_central_beacon (cp_beacon_t *ctx, cp_beacon_desc_t *beacon)
{
    uint nbe;
    u8 *bentry;
    cp_beacon_desc_alloc_t *sched;
    ca_beacon_period_t beacon_period[4];
    uint bp_estimated;
    uint i;
    uint nb_beacon_period;

    // Keep the mac address of the source STA which emits the beacon to store
    // it in the STA list.
    mac_t mac = 0x0;
    uint stei;

    dbg_assert (ctx);
    dbg_assert (ctx->ca);
    dbg_assert (ctx->sta);
    dbg_assert (beacon);

    // Estimate the beacon period from the AC line.
    bp_estimated = cp_beacon_pwl_period (ctx);

    // Create a schedule.
    sched = blk_alloc ();
    sched->next_alloc = 0;

    // Store the hydbrid mode.
    ctx->hm = beacon->payload->hm;
    cp_cco_set_ncnr (ctx->cco, beacon->payload->ncnr);
    cp_cco_set_npsm(ctx->cco, beacon->payload->npsm);
    cp_cco_set_numslots(ctx->cco, beacon->payload->num_slots);
    cp_cco_set_slotusage(ctx->cco, beacon->payload->slot_usage);
    cp_cco_set_aclss(ctx->cco, beacon->payload->aclss);
    cp_station_set_hoip(ctx->sta, beacon->payload->hoip);
   
    cp_station_set_rtsbf(ctx->sta, beacon->payload->rtsbf);
    cp_cco_set_nm(ctx->cco, beacon->payload->nm);

    // Store the coexistence mode
    sched->ca_sched = ca_alloc_get_schedule (ctx->ca, ctx->schedule_index);
    sched->ca_sched->coexistence_mode = ctx->hm;

    bentry = beacon->payload->bmis;
    for (nbe = beacon->payload->bmi_nbe; nbe; nbe --)
    {
       switch (*bentry)
       {
           case CP_BENTRY_PERSISTENT_SCHEDULE:
               bentry = cp_bentry_persistent_schedule_process (ctx, bentry,
                                                               sched);
               break;
           case CP_BENTRY_NON_PERSISTENT_SCHEDULE:
               bentry = cp_bentry_non_persistent_schedule_process (ctx, bentry, sched);
               break;
           case CP_BENTRY_REGIONS:
               bentry = cp_bentry_regions_process (ctx, bentry);
               break;
           case CP_BENTRY_MAC_ADDRESS:
               bentry = cp_bentry_mac_address_process (ctx, bentry, &mac);
               break;
           case CP_BENTRY_DISCOVER:
               bentry = cp_bentry_discover_process (ctx, bentry, beacon);
               break;
           case CP_BENTRY_DISCOVER_INFO:
               bentry = cp_bentry_discover_info_process (ctx, bentry);
               break;
           case CP_BENTRY_BEACON_PERIOD_START_OFFSET:
               bentry = cp_bentry_bpsto_process (ctx, bentry);
               break;
           case CP_BENTRY_ENCRYPTION_KEY_CHANGE:
               bentry = cp_bentry_encryption_key_change_process (ctx, bentry);
               break;
           case CP_BENTRY_CCO_HANDOVER:
               bentry = cp_bentry_handover_process (ctx, bentry);
               break;
           case CP_BENTRY_BEACON_RELOCATION:
               bentry = cp_bentry_beacon_relocation_process (ctx, bentry);
               break;
           case CP_BENTRY_AC_LINE_SYNC_COUNTDOWN:
               bentry = cp_bentry_ac_line_sync_countdown_process (ctx, bentry);
               break;
           case CP_BENTRY_CHANGE_NUMSLOTS:
               bentry = cp_bentry_change_num_slots_process (ctx, bentry);
               break;
           case CP_BENTRY_CHANGE_HM:
               bentry = cp_bentry_change_hm_process (ctx, bentry);
               break;
           case CP_BENTRY_CHANGE_SNID:
               bentry = cp_bentry_change_snid_process (ctx, bentry);
               break;
           case CP_BENTRY_VENDOR:
               bentry = cp_bentry_vendor_specific_process (ctx, bentry);
               break;
           default:
               dbg_assert (*bentry > CP_BENTRY_CHANGE_SNID && *bentry < CP_BENTRY_VENDOR);
       }
    }

    // Store the beacon in the cp_beacon context.
    if (ctx->last_central_beacon != NULL)
    {
        blk_release_desc ((blk_t *) ctx->last_central_beacon);
        nb_beacon_period = 3;
    }
    else
    {
        nb_beacon_period = 4;
    }
    ctx->last_central_beacon = beacon;

    // ADD the schedule to the CA.
    for (i = 0; i < nb_beacon_period; i++)
    {
        beacon_period[i].start_date = bp_estimated * i + beacon->payload->fc.bts
            + beacon->payload->fc.bto[1];
        beacon_period[i].schedule_index = ctx->schedule_index;
    }
    ca_alloc_update_beacon_periods (ctx->ca, beacon_period, nb_beacon_period);
    ctx->schedule_index ++;

    // Store the mac address and the tei of the CCo in the sta list if still
    // not present.
    if (stei && mac)
    {
        // TODO...
    }

    CP_BEACON_TRACE (CENTRAL_BEACON_PROCESS, mac_ntb());

    blk_release (sched);
}

/**
 * Process a the discover beacon received.
 *
 * \param  ctx  the cp_beacon context
 * \param  beacon  the beacon received.
 */
void
cp_beacon_process_discover_beacon (cp_beacon_t *ctx, cp_beacon_desc_t *beacon)
{
    dbg_assert (false);
}

/**
 * Process a the proxy beacon received.
 *
 * \param  ctx  the cp_beacon context
 * \param  beacon  the beacon received.
 */
void
cp_beacon_process_proxy_beacon (cp_beacon_t *ctx, cp_beacon_desc_t *beacon)
{
    dbg_assert (false);
}

/**
 * Generate the central beacon.
 *
 * \param  ctx  the beacon context.
 * \param  beacon  the central beacon to fill
 */
void
cp_beacon_central_beacon_generate (cp_beacon_t *ctx, cp_beacon_desc_t *beacon)
{
    u64 nid;
    u8 *bentry;
    cp_beacon_desc_alloc_t sched;
    ca_beacon_period_t beacon_period[4];
    uint i;

    dbg_assert (ctx);
    dbg_assert (ctx->sta);
    dbg_assert (ctx->cco);
    dbg_assert (beacon);

    cp_beacon_estimate_bts_bto_bp (ctx, &ctx->cbeacon);

    // Store the coexistence mode
    sched.next_alloc = 0;
    sched.ca_sched = ca_alloc_get_schedule (ctx->ca, ctx->schedule_index);
    sched.ca_sched->coexistence_mode = ctx->hm;

    beacon->payload->fc.bts = ctx->cbeacon.btse_ntb[0];
    *beacon->payload->fc.bto = *ctx->cbeacon.bto_bpsto.bto;

    // Beacon payload
    nid = cp_station_get_nid (ctx->sta);
    beacon->nid_msb = nid >> 22;
    beacon->payload->nid_lsb = nid;

    beacon->payload->hm = ctx->hm;
    beacon->payload->stei = cp_station_get_tei (ctx->sta);
    beacon->payload->bt = CP_BEACON_CENTRAL_BEACON;
    beacon->payload->ncnr = cp_cco_get_ncnr (ctx->cco);
    beacon->payload->npsm = cp_cco_get_npsm (ctx->cco);
    beacon->payload->num_slots = cp_cco_get_numslots (ctx->cco);
    beacon->payload->slot_usage = cp_cco_get_slotusage(ctx->cco);
    beacon->payload->slot_id = cp_cco_get_slot_id (ctx->cco);
    beacon->payload->aclss = cp_cco_get_aclss (ctx->cco);
    beacon->payload->hoip = cp_station_get_hoip (ctx->sta);
    beacon->payload->rtsbf = cp_station_get_rtsbf (ctx->sta);
    beacon->payload->nm = cp_cco_get_nm (ctx->cco);
    beacon->payload->cco_cap = cp_cco_get_cco_cap (ctx->cco);
    beacon->payload->bmi_nbe = 0x4;


    // Bentries.
    bentry = cp_bentry_bpsto (ctx, &ctx->cbeacon, beacon->payload->bmis);
    bentry = cp_bentry_mac_address (ctx, bentry);
    bentry = cp_bentry_persistent_schedule (ctx, bentry, &sched);
    bentry = cp_bentry_regions (ctx, bentry);

    // ADD the schedule to the CA.
    for (i = 0; i < 4; i++)
    {
        beacon_period[i].start_date = ctx->cbeacon.per_ntb * i + beacon->payload->fc.bts
            + beacon->payload->fc.bto[i];
        beacon_period[i].schedule_index = ctx->schedule_index;
    }
    ca_alloc_update_beacon_periods (ctx->ca, beacon_period, 4);
    ctx->schedule_index ++;

    // Activate the pbproc if it is not activated yet
    if (!ctx->pbproc_activated)
    {
        ctx->pbproc_activated = true;
        pbproc_activate (ctx->pbproc, true);
    }

    CP_BEACON_TRACE (CENTRAL_BEACON_GENE, mac_ntb());
}

/**
 * Send the beacon over the PWL.
 *
 * \param  ctx  the cp beacon context
 * \param  beacon the beacon to send
 */
void
cp_beacon_send_beacon (cp_beacon_t *ctx, cp_beacon_desc_t *beacon)
{
    mfs_tx_t *mfs_beacon;
    bool added;

    dbg_assert (ctx);
    dbg_assert (beacon);

    /* Create the MFS
     * see http://pessac/cesar/trac/wiki/SoftMacBeacons#BeaconMFS
     * for more details.
     */
    dbg_assert (ctx->mac_store);
    mfs_beacon = mac_store_mfs_add_tx (ctx->mac_store, true, false, MAC_LID_SPC_CENTRAL, 0xff, &added);

    if (added)
    {
        ctx->mfs_central_beacon = mfs_beacon;
        mfs_beacon->common.ats = false;
        mfs_beacon->cap = 0x2;
        mfs_beacon->beacon = true;

        // Add the mfs to the CA
        ca_mfs_add (ctx->ca, mfs_beacon);
    }

    dbg_assert (ctx->pbproc);
    pbproc_mfs_beacon_prepare (ctx->pbproc, mfs_beacon, ctx->cbeacon.btse_ntb[0],
                               (pb_beacon_t *) beacon, &ctx->cbeacon.bto_bpsto);

    ca_mfs_update (ctx->ca, ctx->mfs_central_beacon);

    CP_BEACON_TRACE (SEND_BEACON, mac_ntb(), ctx->cbeacon.btse_ntb[0]);
}