summaryrefslogtreecommitdiff
path: root/cp/beacon/test/central_beacon/src/central_beacon.c
blob: fc354ccdfdda51b84862030492a94418590d3602 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/beacon/test/central_beacon/src/central_beacon.c
 * \brief   generation of the central beacon. 
 * \ingroup cp_beacon
 *
 */
#include "common/std.h"

#include <stdio.h>

#include "lib/blk.h"
#include "lib/test.h"
#include "lib/read_word.h"
#include "lib/trace.h"

#include "mac/common/config.h"
#include "mac/common/ntb.h"
#include "mac/common/pb.h"

#include "hal/phy/phy.h"
#include "cp/station/station.h"
#include "cp/secu/secu.h"

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

#include "cp/beacon/inc/beacons_ctx.h"
#include "cp/beacon/test/central_beacon/inc/phy_stub.h"

#include "cp/station/inc/context.h"
#include "cp/secu/inc/context.h"

#define WORD sizeof(u32)

void
test_bentries_generation (void);

void
test_beacon_process (void);

test_t test;
cp_beacon_desc_t *beacon;

int
main (void)
{
    test_init (test, 0, NULL);
    trace_init ();

    test_bentries_generation();
    test_beacon_process ();

    trace_uninit();

    test_begin(test, "memory test")
    {
        test_fail_if(blk_check_memory() == false, "Memory not freed");
    }
    test_end;

    test_result (test);
    HAL_PLATFORM_EXIT (test_nb_failed (test) == 0 ? 0 : 1);
    return test_nb_failed (test) == 0 ? 0 : 1;
}

void
test_bentries_generation (void)
{
    cp_beacon_t *cp_beacon;
    u8 cbeacon[512];
    u8 *beacon;
    mac_t mac_addr;
    mac_config_t mac_config;

    pbproc_t *pbproc;
    ca_t *ca;
    mac_store_t *mac_store;

    phy_t *phy;
    cp_sta_t *sta;
    cp_secu_t *secu;

    phy = blk_alloc();
    sta = blk_alloc();
    secu = blk_alloc();
    
    pbproc = blk_alloc();
    ca = blk_alloc();
    mac_store = blk_alloc();

    mac_ntb_init ((phy_t *)phy, &mac_config);

    cp_beacon = cp_beacon_init ((phy_t *)phy, sta, secu, ca, pbproc, mac_store);
    cp_beacon_central_beacon_estimate_bts_bto_bp (cp_beacon,
                                                  &cp_beacon->cbeacon);

    cp_beacon->bentries_data.cscd = 0x7;
    beacon = cbeacon + 1;
    beacon = cp_bentry_persitent_schedule (cp_beacon, beacon);
    test_begin (test, "persistent bentry")
    {
        cp_bentry_persistent_schedule_struct_t *pss;
        cp_bentry_sai_stpf_t *sai;

        pss = (cp_bentry_persistent_schedule_struct_t *) (cbeacon + 1);
        sai = (cp_bentry_sai_stpf_t *) &pss->sai;

        test_fail_if (pss->behdr != 0x1, "Wrong bentry header");
        test_fail_if (pss->belen != 0x6, "Wrong bentry length");
        test_fail_if (pss->pscd != 0x0, "Wrong pscd");
        test_fail_if (pss->cscd != 0x7, "Wrong cscd");

        test_fail_if (sai->stpf != true, "Wrong stpf value");
        test_fail_if (sai->glid != 0x7f, "Wrong glid value");
        test_fail_if (sai->st != 0x0, "Wrong start time");
        test_fail_if (sai->et != 3906, "Wrong end time");
    }
    test_end;


    sta->mac_addr = 0x123456789ABC;
    beacon = cp_bentry_mac_address (cp_beacon, cbeacon + 1);

    test_begin (test, "Mac address bentry")
    {
        memcpy (&mac_addr, cbeacon + 3, 6 * sizeof(u8));

        test_fail_if (*(u8*) (cbeacon + 1) != 0x3, "Wrong bentry header value");
        test_fail_if (*(u8*) (cbeacon + 2) != 0x6, "Wrong bentry size");
        test_fail_if (mac_addr != sta->mac_addr, "Wrong Mac address");
    }
    test_end;


    beacon = cp_bentry_discover (cp_beacon, cbeacon + 1, 0x10);

    test_begin (test, "Discover bentry")
    {
        test_fail_if (*(u8*) (cbeacon + 1) != 0x4, "Wrong bentry header");
        test_fail_if (*(u8*) (cbeacon + 2) != 0x1, "Wrong size");
        test_fail_if (*(u8*) (cbeacon + 3) != 0x10, "Wrong tei");
    }
    test_end;

    // Configuring the station
    sta->dis.cco_cap = 0x2;
    sta->dis.proxy_net_cap = false;
    sta->dis.backup_cco_cap = true;
    sta->dis.cco_status = true;
    sta->dis.pco_status = false;
    sta->dis.backup_cco_status = false;
    sta->dis.num_dis_sta = 20;
    sta->dis.num_dis_net = 10;
    sta->dis.authentication = true;
    sta->dis.status_user_ap_cco = true;

    // copy it to the bentries data modifying the num_dis_net value
    cp_beacon->bentries_data.discover_info = *(uint *) &sta->dis;
    ((cp_bentry_discover_info_struct_t *) &cp_beacon->bentries_data.discover_info)->num_dis_sta++;

    beacon = cp_bentry_discover_info (cp_beacon, cbeacon + 1);

    test_begin (test, "Discover info bentry")
    {
        cp_bentry_discover_info_struct_t *data;

        test_fail_if (*(u8*) (cbeacon+1) != 0x5, "Wrong bentry type");
        test_fail_if (*(u8*) (cbeacon+2) != 4, "Wrong bentry length");

        data = (cp_bentry_discover_info_struct_t *) (cbeacon + 3);
        test_fail_if (data->updated != true, "Wrong updated boolean value");
        test_fail_if (data->cco_cap != sta->dis.cco_cap, "Wrong cco_cap");
        test_fail_if (data->proxy_net_cap != sta->dis.proxy_net_cap, "Wrong proxy net cap");
        test_fail_if (data->backup_cco_cap != sta->dis.backup_cco_cap, "Wrong backup cco cap");
        test_fail_if (data->cco_status != sta->dis.cco_status, "Wrong CCo status"); 
        test_fail_if (data->pco_status != sta->dis.pco_status, "Wrong PCo status");
        test_fail_if (data->backup_cco_status != sta->dis.backup_cco_status, "Wrong backup CCo Status");  
        test_fail_if (data->num_dis_sta != sta->dis.num_dis_sta, "Wrong num discover station"); 
        test_fail_if (data->num_dis_net != sta->dis.num_dis_net, "Wrong num discover network");
        test_fail_if (data->authentication != sta->dis.authentication, "Wrong authentification status");
        test_fail_if (data->status_user_ap_cco != sta->dis.status_user_ap_cco, "Wrong user ap cco status");
    }
    test_end;

    // Create the bpsto
    // The test will only verify the bentry header because the bpsto is
    // stamped by the pbproc on the send.
    beacon = cp_bentry_bpsto (cp_beacon, &cp_beacon->cbeacon, cbeacon + 1);
    test_begin (test, "BPSTO")
    {
        test_fail_if (*(u8*) (cbeacon + 1) != 0x6, "Wrong bentry type");
        test_fail_if (*(u8*) (cbeacon + 2) != 0x3, "Wrong bentry length");
    }
    test_end;


    // security key change.
    secu->eks = 0xC;
    secu->kbc = true;

    beacon = cp_bentry_encryption_key_change (cp_beacon, cbeacon + 1);
    test_begin (test, "Key change")
    {
        cp_bentry_key_change_struct_t *data;

        test_fail_if (*(u8*) (cbeacon + 1) != 0x7, "Wrong bentry type");
        test_fail_if (*(u8*) (cbeacon + 2) != 0x2, "Wrong bentry size");

        data = (cp_bentry_key_change_struct_t *) (cbeacon + 3);
        test_fail_if (data->kccd != cp_beacon->bentries_data.kccd, "Wrong kccd countdown");
        test_fail_if (data->kbc != secu->kbc, "Wrong kbc");
        test_fail_if (data->new_eks != secu->eks, "Wrong eks");
    }
    test_end;


    // test handover in progress.
    cp_beacon->bentries_data.hcd = 0x4;
    sta->new_cco_tei = 0xFD;

    beacon = cp_bentry_handover (cp_beacon, cbeacon + 1);
    test_begin (test, "handover")
    {
        cp_bentry_handover_struct_t *data;
        test_fail_if (*(u8*) (cbeacon + 1) != 0x8, "Wrong bentry type");
        test_fail_if (*(u8*) (cbeacon + 2) != 0x2, "Wrong bentry length");

        data = (cp_bentry_handover_struct_t *) (cbeacon + 3);
        test_fail_if (data->hcd != cp_beacon->bentries_data.hcd, "Wrong  handover countdown value");
        test_fail_if (data->nctei != sta->new_cco_tei, "Wrong new CCo tei");
    }
    test_end;


    // Changing hybrid mode.
    cp_beacon->bentries_data.hmccd = 0x3;
    sta->hm = 1;

    beacon = cp_bentry_change_hm (cp_beacon, cbeacon + 1);
    test_begin (test, "Changing hybrid mode")
    {
        cp_bentry_hm_struct_t *data;
        test_fail_if (*(u8*) (cbeacon + 1) != 0xC, "Wrong bentry type");
        test_fail_if (*(u8*) (cbeacon + 2) != 0x1, "Wrong bentry length");

        data = (cp_bentry_hm_struct_t *) (cbeacon + 3);
        test_fail_if (data->hmccd != cp_beacon->bentries_data.hmccd, "Wrong  hybrid mode countdown value");
        test_fail_if (data->new_hm != sta->hm, "Wrong Hybrid mode");

    }
    test_end;


    blk_release (phy);
    blk_release (sta);
    blk_release (secu);

    blk_release (pbproc);
    blk_release (ca);
    blk_release (mac_store);

    cp_beacon_uninit(cp_beacon);
    mac_ntb_uninit();
}

void
test_beacon_process (void)
{
    cp_beacon_t *cp_beacon;
    cp_sta_t *sta;
    cp_secu_t *secu;
    mac_config_t *mac_config;
    phy_t *phy;
    ca_t *ca;
    pbproc_t *pbproc;
    mac_store_t *mac_store;

    u8 *bentries;
    u8 *bentry_tmp;
    uint bpsto;

    // Allocate the data
    secu = blk_alloc();
    mac_config = blk_alloc();
    phy = blk_alloc();
    ca = blk_alloc();
    mac_store = blk_alloc ();
    pbproc = blk_alloc ();

    sta = cp_station_init (mac_store, (cl_t *) mac_config, pbproc, mac_config);

    mac_ntb_init (phy, mac_config);
    cp_beacon = cp_beacon_init ((phy_t *)phy, sta, secu, ca, pbproc, mac_store);
    cp_beacon_central_beacon_estimate_bts_bto_bp (cp_beacon,
                                                  &cp_beacon->cbeacon);
    cp_beacon_central_beacon_estimate_bts_bto_bp (cp_beacon,
                                                  &cp_beacon->dbeacon);


    sta->mac_addr = 0x123456789ABC;

    beacon = (cp_beacon_desc_t *) blk_alloc_desc ();

    beacon->nid_msb = 0x12345678;
    beacon->payload->nid_lsb = 0x9ABC;
    beacon->payload->hm = 0;
    beacon->payload->stei = 0xA;
    beacon->payload->bt = 0x0; // central beacon.
    beacon->payload->ncnr = 0x0;
    beacon->payload->npsm = 0x0;
    beacon->payload->num_slots = 0x1;
    beacon->payload->slot_usage = 0x1;
    beacon->payload->slot_id = 0x0;
    beacon->payload->aclss = 0x1;
    beacon->payload->hoip = 0x0;
    beacon->payload->rtsbf = 0x0;
    beacon->payload->nm = 0x2;
    beacon->payload->cco_cap = 0x0;
    beacon->payload->bmi_nbe = 0x4;

    bentries = &beacon->payload->bmis;

    test_case_begin (test, "Bentries generation test");

    bentry_tmp = bentries;
    bentries = cp_bentry_bpsto (cp_beacon, &cp_beacon->cbeacon, bentries);

    test_begin(test, "BPSTO")
    {
        test_fail_if (*bentry_tmp != 0x6, "Wrong bentry type, shall be 0x6 \
                      instead of %x\n", *bentry_tmp);
        test_fail_if (*(bentry_tmp + 1) != 0x3, "Wrong bentry len, shall be \
                      0x3 instead of %x\n", *(bentry_tmp + 1));
    }
    test_end;

    // Stamping the bpsto as it will be done by the CA.
    bpsto = 0x123456;
    memcpy (bentry_tmp + 2, &bpsto, 3);

    bentry_tmp = bentries;
    bentries = cp_bentry_persitent_schedule(cp_beacon, bentries);

    test_begin (test, "Mac Persistent schedule");
    {
        cp_bentry_sai_stpf_t *sai;
        test_fail_if (*bentry_tmp != 0x1, "Wrong bentry type, shall be 0x6 \
                      instead of %x\n", *bentry_tmp);

        sai = (cp_bentry_sai_stpf_t *) (bentry_tmp + 4);
        test_fail_if (sai->stpf != true, "Error on sai generation");
        test_fail_if (sai->glid != 0x7F, "Error on sai glid");
        test_fail_if (sai->st != 0, "Error on sai st");
        test_fail_if (sai->et != 3906, "Error on sai et");
    }
    test_end;

    sta->mac_addr = 0x123456789ABCll;
    bentry_tmp = bentries;
    bentries = cp_bentry_mac_address (cp_beacon, bentries);

    test_begin (test, "Mac address")
    {
         test_fail_if (*bentry_tmp != 0x3, "Wrong bentry type, shall be 0x3 \
                      instead of %x\n", *bentry_tmp);
        test_fail_if (*(bentry_tmp + 1) != 0x6, "Wrong bentry len, shall be \
                      0x6 instead of %x\n", *(bentry_tmp + 1));       
    }
    test_end;


    bentry_tmp = bentries;
    bentries = cp_bentry_discover (cp_beacon, bentries, 0xA);

    test_begin (test, "Discover bentry")
    {
        test_fail_if (*bentry_tmp != 0x4, "Wrong bentry type, shall be 0x4 \
                      instead of %x\n", *bentry_tmp);
        test_fail_if (*(bentry_tmp + 1) != 1, "Wrong bentry length, shall be \
                      0x1 instead of %x\n", *(bentry_tmp + 1));
        test_fail_if (*(bentry_tmp + 2) != 0xA, "Wrong tei");
    }
    test_end;

    // Read the beacon.
    cp_beacon_process (cp_beacon, beacon);    

    // Verify the result.
    test_case_begin (test, "Verification");

    test_begin (test, "verify")
    {
        test_fail_if (sta->bpsto != bpsto, "Wrong BPSTO");
        test_fail_if (sta->mac_addr != 0x123456789ABCll, "Wrong mac address");
    }
    test_end;

    // Launch the same beacon but simulating that the sta which receives it
    // has been designated by the CCo to send a discover beacon.
    cp_beacon->last_central_beacon = NULL;
    sta->mac_config->tei = 0xA;
    cp_beacon_process (cp_beacon, beacon);

    /*
     * Warn : Do not release the beacon because the cp_beacon_uninit already
     * do it if the beacon as been processed.
     */

    cp_beacon_uninit (cp_beacon);
    mac_ntb_uninit ();
    cp_sta_uninit (sta);
    // Uninit the data
    blk_release (phy);
    blk_release (secu);
    blk_release (mac_config);
    blk_release (ca);
    blk_release (mac_store);
    blk_release (pbproc);
}

/**
 * Get date of last zero-cross.
 * \param  ctx  phy context
 * \return  last zero-cross date
 * set errno to:
 * - EINVAL if ctx is null
 */
u32
phy_clock_get_zero_cross_captured_date (phy_t *ctx)
{
    phy_test_t *phy;
    dbg_assert(ctx);

    phy = (phy_test_t *) ctx;

    if(phy->freq_test == 50)
    {
        // Add a jitter on the 20 beacon.
        if (phy->ntb >= 20e6 && phy->ntb <= 21e6)
        {
            phy->ntb += 1e6 + 200;
        }
        // Add a jitter on the 40 beacon.
        if (phy->ntb >= 40e6 && phy->ntb <= 41e6)
        {
            phy->ntb += 1e6 - 200;
        }
        else
        {
            phy->ntb += 1e6;
        }
    }
    else if (phy->freq_test == 60)
    {
        phy->ntb += 833333;
    }

    return phy->ntb;
}

void
pbproc_mfs_beacon_prepare (pbproc_t *ctx, mfs_tx_t *mfs,
                           u32 beacon_period_start_date, pb_beacon_t *pb,
                           const pbproc_tx_beacon_params_t *params)
{
    cp_beacon_desc_t *dbeacon;
    uint c_payload;
    uint d_payload;
    uint i;

    dbg_assert (pb);
    dbg_assert (beacon);

    test_case_begin (test, "Discover beacon verifycation");
    dbeacon = (cp_beacon_desc_t *) pb;

    test_begin (test, "Verify NID")
    {
        test_fail_if (beacon->nid_msb != dbeacon->nid_msb, "Wrong nid MSB");
        test_fail_if (beacon->payload->nid_lsb != dbeacon->payload->nid_lsb,
                      "Wrong nid lsb");
    }
    test_end;

    test_begin (test, "Paylaod")
    {
        memcpy (&c_payload, beacon->payload + 8, 4);
        memcpy (&d_payload, dbeacon->payload + 8, 4);

        test_fail_if ((c_payload & 0x1FFFFFF0) != (d_payload & 0x1FFFFFF0), "Wrong \
                      payload copy from the central beacon");
        test_fail_if (dbeacon->payload->bmi_nbe != beacon->payload->bmi_nbe + 1,
                      "Wrong quantity of bentries");
        test_fail_if (*(&dbeacon->payload->bmis) != CP_BENTRY_DISCOVER_INFO,
                      "Wrong bentruy header for the discover info");

        for (i = 0; i < 122; i++)
        {
            test_fail_if (*(&dbeacon->payload->bmis + 6 + i) != *(&beacon->payload->bmis + i),
                      "First Bentry copied from central beacon is false");
        }
    }
    test_end;

    blk_release_desc ((blk_t *) pb);
}