summaryrefslogtreecommitdiff
path: root/cesar/cp/cl_interf/src/cl_interf.c
blob: 78f7b6b0b7d3d532794b26092fe539b5d6d77303 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/cl_interf/src/cl_interf.c
 * \brief   Cl Interface source.
 * \ingroup cp_cl_interf
 *
 */
#include "common/std.h"
#include "common/defs/ethernet.h"

#include "lib/blk.h"

#include "cp/cp.h"
#include "cp/defs.h"

#include "interface/interface.h"
#include "cp/cl_interf/cl_interf.h"
#include "cp/msg/msg.h"
#include "cp/sta/core/core.h"
#include "mac/common/ntb.h"

#include "cp/inc/context.h"
#include "cp/inc/trace.h"
#include "cp/cl_interf/inc/context.h"
#include "cp/cl_interf/inc/cl_interf.h"
#include "cp/cl_interf/inc/cl_interf_msg.h"

#include <cyg/kernel/kapi.h>

/** The number of loops before calling the cp sta core checkpoint function. */
#define CP_CL_INTERF_ITERATION_CHECKPOINT_CALL 10
/** The time in eCos ticks to wait before continuing the process. */
#define CP_CL_INTERF_MBOX_GET_WAIT_ECOS_TICKS 10

/**
 * Initialise the module.
 * \param  ctx  the module context.
 *
 */
void
cp_cl_interf_init (cp_t *ctx)
{
    dbg_assert (ctx);

    // Initialise the circular list.
    mbox_init (&ctx->cl_interf.buffers_mbox);

    // Create the mailbox.
    mbox_init(&ctx->cl_interf.mme_rx_mbox);

    // Initialise the call backs.
    interface_callback_init (ctx->interface,
                             cp_cl_interf_rx_mme,
                             (interface_mme_buffer_add_cb_t)
                             cp_cl_interf_add_buffer_tx,
                             ctx);

    // Initialise the cache.
    slab_cache_init (&ctx->cl_interf.msg_slab, "CL_INTERF MSG",
                     sizeof (cp_cl_interf_msg_t),
                     NULL);

}

/**
 * Uninitialise the module.
 * \param  ctx  the module context.
 *
 */
void
cp_cl_interf_uninit (cp_t *ctx)
{
    cp_cl_interf_msg_t *msg = NULL;
    mbox_node_t *node = NULL;
    dbg_assert (ctx);

    while (mbox_peek (&ctx->cl_interf.mme_rx_mbox))
    {
        node = mbox_get (&ctx->cl_interf.mme_rx_mbox);
        if (node)
        {
            msg = PARENT_OF (cp_cl_interf_msg_t, node, node);
            blk_release (msg);
        }
    }

    // Delete the mailbox.
    mbox_uninit (&ctx->cl_interf.mme_rx_mbox);

    while (mbox_peek (&ctx->cl_interf.buffers_mbox))
    {
        node = mbox_get (&ctx->cl_interf.buffers_mbox);
        /*TODO give it back to the linux driver. */
    }

    // Delete the mailbox.
    mbox_uninit (&ctx->cl_interf.buffers_mbox);

    // Uninitialise the slab alloc.
    slab_cache_uninit (&ctx->cl_interf.msg_slab);
}

/**
 * Process the MME received.
 * \param  ctx  the module context.
 *
 * It shall use the MSG function to read the header and get a mme_rx_t
 * context.
 *   - If the MME is complete, it process it and store the data need in
 *     the mme_rx_t context and call the MSG dispatch function.
 *   - if the MMe is fragmented, it shall  verify if the segment has not
 *     already been process.
 *       * if it was process, it shall drop it.
 *       * If not, process it
 */
void
cp_cl_interf_process_mme (cp_t *ctx)
{
    cp_mme_rx_t *mme = NULL;
    cp_cl_interf_msg_t *msg = NULL;
    mbox_node_t *node = NULL;
    uint fmi;

    dbg_assert (ctx);

    node = mbox_get (&ctx->cl_interf.mme_rx_mbox);
    dbg_check (node);
    msg = PARENT_OF (cp_cl_interf_msg_t, node, node);

    mme = cp_msg_mme_read_header (ctx, msg->buffer, msg->length,
                                  msg->tei,
                                  &fmi);
    CP_TRACE (CL_INTERF_PROCESS_MME,
                        mac_ntb(),
                        msg->buffer,
                        msg->length,
                        msg->tei,
                        fmi);

    // MME is not formatted correctly.
    // Release the buffer.
    if (!mme)
    {
        interface_mme_recv_done (ctx->interface, msg->buffer, msg->cl_data);
        CP_TRACE (CL_INTERF_MME_NOT_CORRECT, msg->buffer);
    }
    else
    {
        mme->cl_data = msg->cl_data;
        // Verify if the MME is a segmented MME.
        if (fmi)
        {
            bool last_mme;
            uint ssn;
            cp_sta_t *sta;
            cp_net_t *net;

            net = cp_sta_mgr_get_our_avln (ctx);
            sta = cp_sta_mgr_sta_get_assoc (ctx, net, msg->tei);
            ssn = sta->reassembly_ctx.last_seg_ssn;

            last_mme = cp_cl_interf_rx_mme_frag (ctx, msg->tei,
                                      msg->buffer, msg->length,
                                      fmi);

            interface_mme_recv_done (ctx->interface, msg->buffer, msg->cl_data);
            mme->p_mme = NULL;

            if (last_mme)
            {
                mme->p_frag = (blk_t *)sta->reassembly_ctx.head;
                mme->p_frag_current = mme->p_frag_current;
                mme->encrypt = sta->reassembly_ctx.encrypt;

                memset (&sta->reassembly_ctx, 0,
                        sizeof (cp_sta_reassembly_ctx_t));
                cp_msg_dispatch (ctx, mme);
            }
            else
            {
                /* Store the encrypt information. If SSN is 0 then it takes
                 * the hardware encryption of the MSG, otherwise it shall be
                 * false if the status is false. */
                if (ssn == 0)
                    sta->reassembly_ctx.encrypt = msg->hard_encrypt;
                else
                    sta->reassembly_ctx.encrypt &= msg->hard_encrypt;

                blk_release (mme);
            }

            slab_release (sta);
        }
        // Normal MME call the MSG dispatcher.
        else
        {
            mme->encrypt = msg->hard_encrypt;
            cp_msg_dispatch (ctx, mme);
        }
    }

    slab_release (msg);
}

/**
 * Get the available buffer.
 * \param  ctx  the module context.
 * \return MME TX buffer.
 *
 *  Return null if no buffers are available
 */
u8 *
cp_cl_interf_get_buffer_tx (cp_t *ctx)
{
    u8 *buffer = NULL;
    uint loop;
    dbg_assert (ctx);

    for (buffer = NULL, loop = 0; buffer == NULL; loop ++)
    {
        buffer = (u8 *) mbox_timed_get (&ctx->cl_interf.buffers_mbox,
                                    CP_CL_INTERF_MBOX_GET_WAIT_ECOS_TICKS);
        cp_sta_core_checkpoint (ctx);
    }
    CP_TRACE (CL_INTERF_GET_BUFFER_TX, mac_ntb(), buffer);

    return buffer;
}

/**
 * Send a MME over the PWL or the HLE.
 * \param  ctx  the module context.
 * \param  mme  The MME to send.
 */
void
cp_cl_interf_mme_send (cp_t *ctx, cp_mme_tx_t * mme)
{
    dbg_assert (ctx);
    dbg_assert (mme);

    interface_mme_send (ctx->interface, mme->p_mme,
                        mme->length, mme->peer.tei);

    CP_TRACE (CL_INTERF_MME_SEND, mme->p_mme, mme->length, mme->peer.tei);

    /* Remove the MME from the MME TX object. */
    mme->p_mme = NULL;
}

/**
 * Add a buffer to the circular list.
 * \param  ctx  the module context.
 * \param  buffer  The buffer received.
 *
 */
void
cp_cl_interf_add_buffer_tx (cp_t *ctx, u8 * buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);

    mbox_put (&ctx->cl_interf.buffers_mbox, (mbox_node_t *) buffer);
    CP_TRACE (CL_INTERF_BUFFER_ADD, buffer);
}

/**
 * Remove all oldest MME received.
 * \param  ctx  the module context.
 *
 */
void
cp_cl_interf_garbage_collector (cp_t *ctx){}


/**
 * Receive a MME from the PWL or the HLE.
 * \param  cp  the module context.
 * \param  tei  station's source TEI.
 * \param  buffer  The MME buffer containing the MME.
 * \param  length  The MME length.
 * \param  cl-data  The CL data.
 * \param  hardware_encrypt  Indicate if the MME was Phy encrypted or not.
 *
 */
void
cp_cl_interf_rx_mme (void *cp, cp_tei_t tei,
                     u8 * buffer, uint length, bool cl_data,
                     bool hardware_encrypt)
{
    cp_cl_interf_msg_t *msg;
    cp_t *ctx;
    mac_t mac;
    dbg_assert (cp);
    dbg_assert (buffer);
    dbg_assert (length <= ETH_PACKET_MAX_SIZE);

    ctx = cp;

    mac = bitstream_direct_read_large (buffer, 0, BYTES_SIZE_TO_BITS
                                       (ETH_MAC_ADDRESS_SIZE));
    /* Verify the Destination mac address only if the MFS == NULL. */
    if ((mac == cp_sta_own_data_get_mac_address (ctx))
        || (mac == MAC_BROADCAST)
        || (MAC_TEI_FOREIGN == tei))
    {
        msg = slab_alloc (&ctx->cl_interf.msg_slab);

        msg->tei = tei;
        msg->buffer = buffer;
        msg->length = length;
        msg->cl_data = cl_data;
        msg->hard_encrypt = hardware_encrypt;

        CP_TRACE (CL_INTERF_MME_RX, mac_ntb(), buffer, length);

        // Store the message in the mailbox.
        mbox_put (&ctx->cl_interf.mme_rx_mbox, &msg->node);

        // Raise an event in station core.
        cp_sta_core_signal_recv_mme_event (ctx);
    }
    /* The MME is not for this station. */
    else
    {
        interface_mme_recv_done (ctx->interface, buffer, cl_data);
    }
}


/**
 * Copy the Fragmented MME into blocks.
 * \param  ctx  the cp module context.
 * \param  tei  the station source TEI.
 * \param  mme  the MME to fragment.
 * \param  length  the MME length.
 * \param  fmi  the FMI MME data.
 * \return  true if it was the last message, false otherwise.
 */
bool
cp_cl_interf_rx_mme_frag (cp_t *ctx, cp_tei_t tei, u8 *mme, uint length,
                          uint fmi)
{
    uint fmi_frag_nbs;
    uint fmi_frag_nb;
    uint fmi_frag_ssn;
    cp_net_t *net;
    cp_sta_t *sta;
    cp_sta_reassembly_ctx_blk_t *head;
    cp_sta_reassembly_ctx_blk_t *tail;
    bitstream_t bitstream;
    uint nb_blk;
    cp_sta_reassembly_ctx_blk_t *current;
    uint offset;
    uint rest;


    bitstream_init (&bitstream, &fmi, 16, BITSTREAM_READ);
    bitstream_access (&bitstream, &fmi_frag_nbs, 4);
    bitstream_access (&bitstream, &fmi_frag_nb, 4);
    bitstream_access (&bitstream, &fmi_frag_ssn, 4);
    bitstream_finalise (&bitstream);

    net = cp_sta_mgr_get_our_avln (ctx);
    sta = cp_sta_mgr_sta_get_assoc (ctx, net, tei);

    dbg_assert (sta);

    sta->reassembly_ctx.last_seg_ssn = fmi_frag_ssn;
    nb_blk = (length / BLK_SIZE) + 1;

    // Compute the number of blocks.
    head = (cp_sta_reassembly_ctx_blk_t *)
        blk_alloc_desc_range (nb_blk, (blk_t **) &tail);
    tail->next = NULL;

    for (current = head, offset = 0;
         offset < length && current != NULL;
         offset += BLK_SIZE, current = current->next)
    {
        current->length = ((rest = (length - offset)) > BLK_SIZE)
            ? BLK_SIZE : rest;
        current->ssn = fmi_frag_ssn;
        bitstream_memcpy (current->data, mme + offset,
                          (rest > BLK_SIZE) ? BLK_SIZE : rest);
    }

    // Chain the blocks.
    if (sta->reassembly_ctx.tail)
    {
        sta->reassembly_ctx.tail->next = head;
        sta->reassembly_ctx.tail = tail;
    }
    else
    {
        sta->reassembly_ctx.head = head;
        sta->reassembly_ctx.tail = tail;
    }
    slab_release (sta);

    if (fmi_frag_nb == fmi_frag_nbs)
        return true;
    else
        return false;
}