summaryrefslogtreecommitdiff
path: root/cesar/cl/test/src/test.c
blob: c313dc05b280cb10f31762fe01f1725990105fb1 (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
/* Cesar project {{{
 *
 * Copyright (C) 2010 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cl/test/src/test.c
 * \brief   CL environment test.
 * \ingroup cl
 */
#include "common/std.h"
#include "lib/bitstream.h"
#include "lib/trace.h"
#include "cl/test/test.h"
#include "cl/cl_mactotei.h"
#include "mac/common/ntb.h"

static void
cl_test_buffer_ul_add (void *user_data, u8 *buffer)
{ }

void
cl_test_mme_ul_send_done_cb (void *ul_data, u8 *buffer, uint length)
{ }

static void
cl_test_init_mme_recv_cp (void *user, uint tei, u8 *buffer, uint length,
                          bool from_sar, bool encryption)
{
    cl_test_recv_t *ctx = (cl_test_recv_t*) user;
    dbg_assert (user);
    ctx->stei = tei;
    ctx->buffer = buffer;
    ctx->length = length;
    ctx->from_sar = from_sar;
    ctx->nek_enc = encryption;
}

static void
cl_test_init_data_recv (void *user, u8 *buffer, uint length)
{
    cl_test_recv_t *ctx = (cl_test_recv_t*) user;
    dbg_assert (user);
    ctx->stei = 0;
    ctx->buffer = buffer;
    ctx->length = length;
    ctx->from_sar = true;
    ctx->nek_enc = false;
}

static int
cl_trace_buffer_dbg_dump_callback (void *user, const char *text,
                                   uint text_size)
{
    return 0;
}


void
cl_test_init (cl_test_t *ctx, u32 seed)
{
    static int phy = 0;
    dbg_assert (ctx);
    ctx->mac_store = mac_store_init ();
    mac_config_init (&ctx->mac_config);
    mac_ntb_init ((phy_t *) &phy, &ctx->mac_config);
    ctx->mac_config.authenticated = true;
    ctx->mac_config.tei = 0xfe;
    ctx->mac_config.sta_mac_address = 0xfeffffd71300ull;
    ctx->cl = cl_init (ctx->mac_store, (sar_t*) &ctx->sar, &ctx->mac_config);
    cl_mme_init_buffer_add_cb (ctx->cl, cl_test_buffer_ul_add, INVALID_PTR);
    cl_mme_init_ul_as_data (ctx->cl, cl_test_mme_ul_send_done_cb,
                            INVALID_PTR);
    cl_mme_recv_init (ctx->cl, cl_test_init_mme_recv_cp, &ctx->pwl_recv);
    cl_data_recv_init (ctx->cl, cl_test_init_data_recv, &ctx->pwl_recv);
    lib_rnd_init (&ctx->rnd, seed);
    mac_ntb_init ((phy_t*) &phy, &ctx->mac_config);
}

void
cl_test_uninit (cl_test_t *ctx)
{
    dbg_assert (ctx);
    trace_bundle_dump_all ("dbg", cl_trace_buffer_dbg_dump_callback, NULL);
    cl_uninit (ctx->cl);
    mac_store_uninit (ctx->mac_store);
}

void
cl_test_prepare_mactotei (cl_test_t *ctx, mac_t *dmacs, uint nb)
{
    uint i;
    cl_mactotei_blk_t *table;
    dbg_assert (ctx);
    cl_mactotei_release_table (ctx->cl);
    table = cl_mactotei_new ();
    for (i = 0; i < nb; i++)
    {
        dbg_assert (!mac_is_multicast (dmacs[i]));
        cl_mactotei_addr_add (table, dmacs[i], i + 1, MAC_TEI_UNASSOCIATED);
    }
    cl_mactotei_use_table (ctx->cl, table);
}

void
cl_test_prepare_buffer (cl_test_t *ctx, u8 *buffer, u16 length, mac_t dmac)
{
    u8 data;
    u16 i;
    bitstream_t stream;
    dbg_assert (ctx);
    data = lib_rnd32 (&ctx->rnd) & 0xff;
    bitstream_write_init (&stream, buffer, length);
    bitstream_write_large (&stream, dmac, 48);
    bitstream_write_large (&stream, ctx->mac_config.sta_mac_address, 48);
    for (i = 0; i < length - 12; i++)
        bitstream_write (&stream, data, 8);
    bitstream_finalise (&stream);
}