summaryrefslogtreecommitdiff
path: root/cesar/interface/src/interface.c
blob: dcb6d9eb909d85efded9f089aa5d3c9a852b323c (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    interface/src/interface.c
 * \brief   Interface functions sources.
 * \ingroup interface
 *
 */
#include "common/std.h"
#include "common/defs/ethernet.h"
#include "lib/swap.h"

#include "hle/hle.h"
#include "interface/interface.h"
#include "interface/sniffer/sniffer.h"
#include "interface/fcall/defs.h"

#include "interface/inc/interface.h"
#include "interface/inc/context.h"
#include "interface/inc/mme_process.h"

#include "common/defs/priority.h"

#if CONFIG_INTERFACE_FCALL
# include "interface/fcall/inc/context.h"
# include "interface/fcall/inc/interface_fcall.h"
#endif

static interface_t interface_global;

#ifndef INTERFACE_UNIT_TEST
/**
 * The thread process of the interface.
 * \param  data  the data to be used by the thread.
 */
static void
interface_thread_process (cyg_addrword_t data)
{
    interface_t *ctx;

    dbg_assert (data);
    ctx = (interface_t *) data;

    while (true)
    {
        interface_process (ctx);
    }
}
#endif

#if CONFIG_INTERFACE_FCALL

/**
 * Called by the interface fcall module when the buffer has been used.
 * This allows the HLE to know which buffer is newly available to be used or
 * give it back to the ARM. (the buffer is borrowed by the linux).
 * 
 * \param  ctx  the interface context.
 * \param  buffer the buffer used to send the data.
 */
static void
interface_hle_send_done (interface_t *ctx, u8 *buffer);

#endif


/**
 * Initialise the interface module.
 * \param  hle  the hle context.
 * \param  cl  the cl context.
 * \param  sar  the sar context.
 * \param  mac_config  the mac config context.
 * \return  the interface module context.
 */
interface_t*
interface_init (hle_t *hle, cl_t *cl, sar_t *sar, mac_config_t
                *mac_config)
{
    dbg_assert (hle);
    dbg_assert (cl);
    dbg_assert (sar);
    dbg_assert (mac_config);

    interface_global.hle = hle;
    interface_global.cl = cl;
    interface_global.sar = sar;
    interface_global.mac_config = mac_config;

    interface_global.sniffer = interface_sniffer_init
        ((interface_sniffer_send_message_cb_t) interface_hle_send,
         &interface_global, mac_config);

#if CONFIG_INTERFACE_FCALL
    interface_global.fcall = interface_fcall_init
        ((interface_fcall_send_message_cb_t) interface_hle_send,
         (interface_fcall_send_done_message_cb_t) interface_hle_send_done,
         (interface_fcall_get_buffer_cb_t)  interface_buffer_work_get,
         &interface_global, mac_config);
#endif /* CONFIG_INTERFACE_FCALL */

    mbox_init (&interface_global.buffers_mbox);

    // Integrate the HLE.
    hle_init_interface_cb (hle,
        (hle_interface_buffer_add_cb_t) interface_buffer_work_add,
        (hle_interface_mme_recv_cb_t) interface_hle_recv,
        &interface_global);

    // Integrate the CL.
    cl_mme_init_buffer_add_cb (cl,
                               (cl_mme_buffer_add_cb_t) interface_buffer_add,
                               &interface_global);
    cl_mme_recv_init (cl,(cl_mme_recv_cb_t) interface_mme_recv,
                      &interface_global);

    // Integrate the SAR.
    sar_init_beacon_cb (sar, &interface_global,
                        (sar_beacon_cb_t) interface_beacon_add);

    // Create the mailbox.
    mbox_init (&interface_global.mailbox);

#ifndef INTERFACE_UNIT_TEST
    // Create the interface thread.
    cyg_thread_create(INTERFACE_THREAD_PRIORITY, &interface_thread_process,
                        (cyg_addrword_t) &interface_global,
                        "INTERFACE",
                        interface_global.thread_stack,
                        INTERFACE_THREAD_STACK_SIZE,
                        &interface_global.thread_handle,
                        &interface_global.thread);
    cyg_thread_resume (interface_global.thread_handle);
#endif

    return &interface_global;
}

/**
 * Interface uninit.
 * \param  ctx  the interface context.
 */
void
interface_uninit (interface_t *ctx)
{
    dbg_assert (ctx);
    dbg_assert (ctx->sniffer);

    mbox_uninit (&ctx->mailbox);

#ifndef INTERFACE_UNIT_TEST
    cyg_thread_suspend (ctx->thread_handle);
#endif
    interface_sniffer_uninit (ctx->sniffer);
#if CONFIG_INTERFACE_FCALL
    interface_fcall_uninit (ctx->fcall);
#endif /* CONFIG_INTERFACE_FCALL */
}

/**
 * Initialise the callbacks functions.
 * \param  ctx the interface context.
 * \param  mme_recv_cb the function to call on reception of a MME.
 * \param  buffer_add_cb the function to call on buffer reception.
 * \param  beacon_add_cb the function to call on beacon reception
 * \param  user_data the data to provide on each callback function.
 */
void
interface_callback_init (interface_t *ctx, interface_mme_recv_cb_t mme_recv_cb,
           interface_mme_buffer_add_cb_t buffer_add_cb, void *user_data)
{
    dbg_assert (ctx);
    dbg_assert (mme_recv_cb);
    dbg_assert (buffer_add_cb);

    ctx->mme_recv_cb = mme_recv_cb;
    ctx->buffer_add_cb = buffer_add_cb;
    ctx->interface_mme_user_data = user_data;
}


/**
 * Initialise the callbacks functions.
 * \param  ctx the interface context.
 * \param  beacon_add_cb the function to call on beacon reception
 * \param  user_data the data to provide on each callback function.
 */
void
interface_callback_beacon_init (interface_t *ctx,
           interface_beacon_add_cb_t beacon_add_cb,
           void *user_data)
{
    dbg_assert (ctx);
    dbg_assert (beacon_add_cb);

    ctx->beacon_add_cb = beacon_add_cb;
    ctx->beacon_user_data = user_data;
}


/** Receives an MME from the PWL or the HLE.
 * \param  ctx the interface context
 * \param  tei  the station's source TEI.
 * \param  buffer the buffer containing the MME.
 * \param  length the MME length
 * \param  mme_data data use by the CL.
 * \param  encrypted  inform if the packet comes from the PWL if it has been
 * crypted.
 */
void
interface_mme_recv (interface_t *ctx, uint tei, u8 *buffer, uint length,
                    bool mme_data, bool encrypted)
{
    u8 *copy_buffer;

    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));

    if (interface_sniffer_mme_status_rx(ctx->sniffer))
    {
        copy_buffer = interface_buffer_work_get (ctx);

        if (copy_buffer)
        {
            interface_sniffer_copy_mme_rx (ctx->sniffer, buffer, length,
                                       copy_buffer, encrypted);
        }
    }

    // Call the actor callback.
    dbg_assert (ctx->mme_recv_cb);
    (*ctx->mme_recv_cb) (ctx->interface_mme_user_data, tei, buffer,
                         length, mme_data, encrypted);
}

void
interface_mme_recv_done (interface_t *ctx, u8 *buffer, bool mme_recv)
{
    dbg_assert (ctx);
    dbg_assert (ctx->cl);

    cl_mme_recv_done (ctx->cl, buffer, mme_recv);
}

/** Provides a MME to send to the CL. This MME can be send as a MME or a data.
 * \param  ctx the interface context.
 * \param  buffer the buffer containing the MME.
 * \param  length the length of the MME.
 * \param  tei  the destination TEI.
 */
void
interface_mme_send (interface_t *ctx, u8* buffer, uint length, uint tei)
{
    u8 *copy_buffer;

    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));

    if (interface_sniffer_mme_status_tx (ctx->sniffer))
    {
        copy_buffer = interface_buffer_work_get (ctx);

        if (copy_buffer)
        {
            interface_sniffer_copy_mme_tx (ctx->sniffer, buffer, length,
                                       copy_buffer, false);
        }
    }

    dbg_assert (ctx->cl);
    cl_mme_send (ctx->cl, buffer, length, tei);
}


void
interface_beacon_prepare (interface_t *ctx, pb_beacon_t *beacon,
                          mfs_tx_t *beacon_mfs, void *bto_bpsto)
{
    u8 *buffer;

    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (ctx->sar);

    if (interface_sniffer_beacon_status_tx (ctx->sniffer))
    {
        buffer = interface_buffer_work_get (ctx);

        if (buffer)
        {
            interface_sniffer_copy_beacon_tx (ctx->sniffer, beacon, buffer);
        }
    }

    sar_beacon_send (ctx->sar, beacon, beacon_mfs, bto_bpsto);
}

void interface_beacon_add (interface_t *ctx, pb_beacon_t *pb,
                           bsu_params_t *bparams)
{
    u8 *buffer;

    dbg_assert (ctx);
    dbg_assert (pb);
    dbg_assert (ctx->mac_config);

    /* Get the buffer to copy the data. */
    if (interface_sniffer_beacon_status_rx (ctx->sniffer))
    {
        buffer = interface_buffer_work_get (ctx);

        if (buffer)
        {
            interface_sniffer_copy_beacon_rx (ctx->sniffer, pb, buffer);
        }
    }

    if (bparams->direction == BSU_BEACON_DIRECTION_FROM_PLC)
    {
        dbg_assert (ctx->beacon_add_cb);
        (*ctx->beacon_add_cb) (ctx->beacon_user_data, pb);
    }
}


/**
 * Sends a message to the IPMbox
 * \param  ctx the interface context
 * \param  data the message to post in the ipmbox.
 * \param  length  the length of the data in bytes.
 */
void
interface_hle_send (interface_t *ctx, uint *data, uint length)
{
    dbg_assert (ctx);
    dbg_assert (data);
    dbg_assert (length);

    hle_ipmbox_send (ctx->hle, data, length);
}

#if CONFIG_INTERFACE_FCALL

/**
 * Called by the interface fcall module when the buffer has been used.
 * This allows the HLE to know which buffer is newly available to be used or
 * give it back to the ARM. (the buffer is borrowed by the linux).
 *
 * \param  ctx  the interface context.
 * \param  buffer the buffer used to send the data.
 */
static void
interface_hle_send_done (interface_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ctx->hle);

    hle_send_done (ctx->hle, buffer);
}

#endif /* CONFIG_INTERFACE_FCALL */

/**
 * Add a buffer to the registered actor.
 * \param ctx the interface context.
 * \param buffer the buffer to add.
 */
void
interface_buffer_add (interface_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (ctx->buffer_add_cb);

    (*ctx->buffer_add_cb) (ctx->interface_mme_user_data, buffer);
}

/**
 * Add a buffer to its own list.
 * \param ctx the interface context.
 * \param buffer the buffer to add.
 */
void
interface_buffer_work_add (interface_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);

    dbg_assert (ctx->buffer_add_cb);

    mbox_put (&ctx->buffers_mbox, (mbox_node_t *) buffer);
}

/**
 * Get a buffer from the list.
 * \param  ctx the interface context.
 * \return  the buffer to use, NULL if no buffer is available.
 */
u8*
interface_buffer_work_get (interface_t *ctx)
{
    u8 *buffer;
    dbg_assert (ctx);

    buffer = (u8*) mbox_try_get (&ctx->buffers_mbox);

    return buffer;
}

/**
 * Receive a data from the HLE.
 * \param  ctx  the interface context.
 * \param  buffer  the Message received.
 *
 * This will be use by the Fcall module in the interface. It can be use by
 * anything in the future.
 */
void
interface_hle_recv (interface_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);

    mbox_put (&ctx->mailbox, (mbox_node_t *) buffer);
}

static inline bool
interface_read_mme (interface_t *ctx, u8 *mme, uint *mmtype)
{
    bitstream_t stream;
    mac_t oda = 0;
    mac_t osa = 0;
    uint vlantag = 0;
    uint mtype = 0;
    uint mmv = 0;
    uint fmi = 0;
    uint offset = 0;

    bitstream_read_init (&stream, mme, ETH_PACKET_MAX_SIZE);
    oda = bitstream_read_large (&stream, 48);
    osa = bitstream_read_large (&stream, 48);

    if (bitstream_direct_read (mme, 96, 16) != swap16(HPAV_MTYPE_MME))
        vlantag = bitstream_read (&stream, 32);

    mtype = swap16(bitstream_read (&stream, 16));
    mmv = bitstream_read (&stream, 8);
    *mmtype = bitstream_read (&stream, 16);
    fmi = bitstream_read (&stream, 16);
    offset = bitstream_finalise (&stream);

    if ((mtype == HPAV_MTYPE_MME)
        && (mmv == HPAV_MMV)
        && ((*mmtype == VS_SNIFFER_REQ)
            || (*mmtype == INTERFACE_FCALL_MMTYPE)))
        return true;
    return false;
}

void
interface_process (interface_t *ctx)
{
    u8 *buffer;
    u8 *ans;
    uint mmtype = 0;
    uint word[2];
    dbg_assert (ctx);

    buffer = (u8 *) mbox_get(&ctx->mailbox);

    if (interface_read_mme (ctx, buffer, &mmtype))
    {
        switch (mmtype)
        {
            // Configure the interface sniffer sub module.
        case VS_SNIFFER_REQ:
            ans = interface_buffer_work_get (ctx);
            interface_sniffer_configure_and_respond (ctx->sniffer, buffer,
                                                     ans);
            /* Give back the current buffer. */
            word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE,
                                            HLE_MSG_TYPE_SEND_DONE),
                               (MSG_LENGTH, 1));
            word[1] = (uint) buffer;
            interface_hle_send (ctx, word, 2);
            /* Send the CNF MME. */
            word[0] = BF_FILL (IPMBOX_REG, (MSG_TYPE,
                                            HLE_MSG_TYPE_INTERFACE),
                               (MSG_LENGTH, 1),
                               (PARAM_INTERFACE_TYPE,
                                INTERFACE_MODULE_SNIFFER),
                               (PARAM_INTERFACE_LENGTH, 60));
            word[1] = (uint) ans;
            interface_hle_send (ctx, word, 2);
            break;
#if CONFIG_INTERFACE_FCALL
        case INTERFACE_FCALL_MMTYPE:
            interface_fcall_mme_recv (ctx->fcall, buffer);
            break;
#endif /* CONFIG_INTERFACE_FCALL */
        }
    }
}