summaryrefslogtreecommitdiff
path: root/cesar/test_general/maximus/integration/interface-dp/src/station.c
blob: 3d99fe03581ffd03dda96a8eb4cdd434dcbf6a5e (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    test_general/integration/interface-dp/src/station.c
 * \brief   Station interface - DP.
 * \ingroup test_general
 *
 * Test the integration of the interface with the data plane.
 * The sniffer will be added to this test later.
 */
#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_arch.h>

#include "common/std.h"
#include "host/station/station.h"
#include <stdio.h>

/** Library include. */
#include "lib/trace.h"
#include "lib/stats.h"
#include "lib/circular_buffer.h"
#include "cl/cl_mactotei.h"
#include "lib/read_word.h"

/** Data include. */
#include "mac/common/config.h"
#include "mac/common/ntb.h"
#include "mac/common/store.h"

/** Layers include. */
#include "interface/interface.h"
#include "hle/hle.h"
#include "cl/cl.h"
#include "mac/sar/sar.h"
#include "mac/pbproc/pbproc.h"
#include "mac/ca/ca.h"

/** station context. */
struct station_test_t
{
    /** The mac config. */
    mac_config_t mac_config;
    /** Short network identifier. */
    u8 snid;
    /** The mac store. */
    mac_store_t *mac_store;

    // Layers
    /** Pbproc context. */
    pbproc_t *pbproc;
    /** Sar context. */
    sar_t *sar;
    /** CL context. */
    cl_t *cl;
    /** HLE context. */
    hle_t *hle;

    /** Interface context. */
    interface_t *interface;


    /** Circular buffer to simulate the CP. */
    circular_buffer_t cp_buffers;
    u8 *cp_buffer_list [20];

    /** Cl mac to tei table. */
    cl_mactotei_blk_t *cl_table;

    // Create the Thread for the SAR.
    cyg_thread sar_thread;
    cyg_handle_t sar_thread_handle;
    u8 sar_stack [CYGNUM_HAL_STACK_SIZE_TYPICAL];

    /** Cp thread. */
    cyg_thread cp_thread;
    cyg_handle_t cp_thread_handle;

    u8 cp_thread_stack [CYGNUM_HAL_STACK_SIZE_TYPICAL];
};
typedef struct station_test_t station_test_t;

struct ce_rx_params_node_t
{
    list_node_t node;
    pbproc_rx_params_t rx_params;
};
typedef struct ce_rx_params_node_t ce_rx_params_node_t;

struct cp_send_data_t
{
    uint beacon;
    uint tei;
    uint length;
    uint bcast;
    u8 *buffer;
    uint msg_id;
};
typedef struct cp_send_data_t cp_send_data_t;

static station_test_t station_test;
/* Use to keep a list of rx params in the CE */
list_t ce_rx_params_list;

/* Fill this to send a MME. */
cp_send_data_t cp_data;


// ---------------------------- Function calls -----------------------------

/** Creates a link in the STA.
 *
 * - dtei  the tei of the station.
 * - dmac_addr  the destination mac address.
 * - fw  forward link
 * - rw  reverse link
 * - bcast  broadcast.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_add_mme_link (fcall_ctx_t *fcall, fcall_param_t **param,
    sci_msg_t **msg, void *data)
{
    uint dtei = 0;
    mac_t mac_address = 0;
    uint fw = 0;
    uint rw = 0;
    uint bcast = 0;

    mfs_tx_t *mfs_tx;
    mfs_rx_t *mfs_rx;
    bool added;

    fcall_param_bind_short (*param, *msg, "dtei", &dtei);
    fcall_param_bind (*param, *msg, "dmac_addr", sizeof(mac_t), &mac_address);
    fcall_param_bind_short (*param, *msg, "fw", &fw);
    fcall_param_bind_short (*param, *msg, "rw", &rw);
    fcall_param_bind_short (*param, *msg, "bcast", &bcast);

    fcall_param_reset (*param);

    if (mac_address == 0)
        return false;

    if (fw)
    {
        mfs_tx = mac_store_mfs_add_tx (station_test.mac_store, bcast, true, MAC_LID_NONE, dtei, &added);

        if (mfs_tx == NULL)
            return false;

        sar_mfs_add (station_test.sar, (mfs_t *) mfs_tx);
        blk_release (mfs_tx);
    }

    if (rw)
    {
        mfs_rx = mac_store_mfs_add_rx (station_test.mac_store, bcast, true, MAC_LID_NONE, dtei, &added);

        if (mfs_rx == NULL)
            return false;

        sar_mfs_add (station_test.sar, (mfs_t *) mfs_rx);
        blk_release (mfs_rx);
    }

    if (!station_test.cl_table)
        station_test.cl_table = cl_mactotei_new ();

    cl_mactotei_addr_add (station_test.cl_table, mac_address, dtei, dtei);


    return true;
}

/** Initialise the station parameters data as the mac address the tei.
 *
 * - stei.
 * - mac_addr.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_config (fcall_ctx_t *fcall, fcall_param_t **param,
    sci_msg_t **msg, void *data)
{
    uint stei = 0;
    mac_t mac_address = 0;

    fcall_param_bind_short (*param, *msg, "stei", &stei);
    fcall_param_bind (*param, *msg, "mac_addr", sizeof(mac_t), &mac_address);

    station_test.mac_config.sta_mac_address = mac_address;
    station_test.mac_config.tei = stei;

    fcall_param_reset (*param);

    return true;
}


/** Prepare the station to work.
 * It will create default schedules for the Channel Access in CSMA-only mode
 * and activate the pbproc to start the communication with the others
 * stations.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_start (fcall_ctx_t *fcall, fcall_param_t **param,
    sci_msg_t **msg, void *data)
{
    ca_beacon_period_t beacons_periods[4];
    uint i;

    /* Get and fill the schedule */
    ca_schedule_t *sched = ca_alloc_get_schedule (pbproc_get_ca (station_test.pbproc),0);

    sched->coexistence_mode = MAC_COEXISTENCE_SHARED_CSMA_HYBRID_MODE;
    sched->snid = station_test.snid;
    sched->nek_switch = 0; //TODO
    sched->allocations_nb = 1;
    sched->allocations[0].end_offset_tck = BITS_ONES (24);
    sched->allocations[0].glid = 0xff;

    /* Create a schedule for 14 beacon period */
    for (i = 0; i < 4; i++)
    {
        beacons_periods[i].start_date = BITS_ONES(24) * i
                + my_station.current_tick_tck;
        beacons_periods[i].schedule_index = 0;
    }

    /* Use the new schedule */
    ca_alloc_update_beacon_periods (pbproc_get_ca (station_test.pbproc), beacons_periods,
            4);

    /* Only run this one !!! */
    pbproc_activate (station_test.pbproc, true);


    /* request the CL to use the cl_mactotei table. */
    cl_mactotei_use_table (station_test.cl, station_test.cl_table);

    return true;
}

/** Send a MME over the PLC.
 *
 * - tei  the destination tei
 * - mme  the mme buffer to send
 * - length  the MME length
 * - bcast  the bradcast flag
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_mme_send (fcall_ctx_t *fcall, fcall_param_t **param,
    sci_msg_t **msg, void *data)
{
    uint dtei = 0;
    uint length = 0;
    u8 *buffer;
    uint bcast = 0;

    buffer = circular_buffer_get (&station_test.cp_buffers);

    if (buffer == NULL)
    {
        printf ("Warning : No MME buffer available");
        fcall_param_reset (*param);
        return false;
    }

    fcall_param_bind_short (*param, *msg, "tei", &dtei);
    fcall_param_bind_short (*param, *msg, "length", &length);
    fcall_param_bind_short (*param, *msg, "bcast", &bcast);
    fcall_param_bind (*param, *msg, "mme", length, buffer);

    cp_data.beacon = false;
    cp_data.tei = dtei;
    cp_data.length = length;
    cp_data.bcast = bcast;
    cp_data.buffer = buffer;
    cp_data.msg_id = (*param)->msg_id;

    cyg_thread_resume (station_test.cp_thread_handle);

    fcall_param_reset (*param);
    return true;
}

/** Send a beacon over the PLC.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_beacon_send(fcall_ctx_t *fcall, fcall_param_t **param,
    sci_msg_t **msg, void *data)
{
    cp_data.beacon = true;
    cp_data.msg_id = (*param)->msg_id;

    cyg_thread_resume (station_test.cp_thread_handle);

    fcall_param_reset (*param);
    return true;
}

// ---------------------------- Stub functions ----------------------------

/**
 * Cp run function..
 * Use to send MMEs... on maximus request.
 */
void
cp_run (cyg_addrword_t data)
{
    while (true)
    {
        cyg_thread_delay (10);

        if (cp_data.beacon)
        {
            pb_beacon_t *beacon;
            mfs_tx_t *mfs;
            uint i;
            bool added;

            mfs = mac_store_mfs_add_tx (station_test.mac_store, true, false,
                                               MAC_LID_SPC_CENTRAL, 0xff, &added);

            if (mfs)
            {
                if (added)
                {
                    mfs->cap = 0x3;
                    mfs->beacon = true;
                    mfs->common.ats = false;
                    ca_mfs_add (pbproc_get_ca (station_test.pbproc), mfs);
                }

                beacon = (pb_beacon_t *) blk_alloc_desc ();
                beacon->first_data_word = 0x12345678;

                for (i = 0; i < 128; i++)
                    beacon->data[i] = i;

                // send the beacon.
                interface_beacon_prepare (station_test.interface, beacon,
                                          mfs, &beacon->data[128]);

                interface_beacon_add (station_test.interface, beacon,
                                      (pbproc_rx_beacon_params_t *)&beacon->data[128]);

                blk_release_desc ((blk_t *) beacon);
                blk_release (mfs);
            }
        }
        else
        {
            interface_mme_send (station_test.interface, cp_data.buffer,
                                cp_data.length, cp_data.tei);
        }

        cyg_thread_suspend(station_test.cp_thread_handle);
    }
}

/** Stubs the CP init function.
 * It will just initialise the buffer list within the station_test structure.
 */
void
cp_init (void)
{
    circular_buffer_init (&station_test.cp_buffers,
                          station_test.cp_buffer_list, 20);

    cyg_thread_create(9, cp_run, 0, "Cp_run", station_test.cp_thread_stack,
                      CYGNUM_HAL_STACK_SIZE_TYPICAL, &station_test.cp_thread_handle,
                      &station_test.cp_thread);
}

void
ce_measurements (void *data, pbproc_rx_params_t *rx_params,
                 uint total_pb_count, pb_t *chan_data,
                 uint chan_data_count, u8 false_pb_count, u32 ber_sum)
{
    if (chan_data)
        blk_release_desc_range_nb ((blk_t*) chan_data, chan_data_count);
}


/**
* CP receives a new MME.
* \param  user_data  the data registered by the actor in the init function.
* \param  mfs  the mfs
* \param  buffer  the buffer containing the MME.
* \param  length  the MME length
* \param  mme_recv  data use by the data plane.
* \param  encryption  inform if the packet comes from the PWL if it had been
* crypted or not.
*/
void
cp_mme_recv (void *user_data, uint tei, u8 *buffer, uint length,
    bool mme_recv, bool encryption)
{
    printf ("/*************************************************/\n");
    printf ("/ STATION : %d   Receive a MME          **********/\n",
            station_test.mac_config.tei);
    printf ("/ MME length : %d                       **********/\n", length);
    printf ("/ MME encrypted : %d                    **********/\n",
            encryption);
    printf ("/*************************************************/\n");
}

/**
* CP receives a empty buffer.
* \param  user_data  the data registered by the actor in the init function.
* \param  buffer  the buffer to add.
*/
void
cp_mme_buffer_add (void *user_data, u8 *buffer)
{
    printf ("/*************************************************/\n");
    printf ("/ STATION : %d   Receive a new MME Buffer ********/\n",
            station_test.mac_config.tei);
    printf ("/*************************************************/\n");


    // Keep the buffer as the control plane will do.
    circular_buffer_add (&station_test.cp_buffers, buffer);
}


/**
* CP receives a beacon.
* \param  user_data the data registered by the actor in the init function.
* \param  beacon  the beacon freshly received.
*/
void
cp_beacon_add (void *user_data, pb_beacon_t *beacon)
{
    printf ("/*************************************************/\n");
    printf ("/ STATION : %d   Receive a beacon         ********/\n",
            station_test.mac_config.tei);
    printf ("/*************************************************/\n");


    // release the beacon.
    blk_release_desc ((blk_t *) beacon);
}

// -------------------------------------------------------------------------


/** Cesar init function as described in the wiki
 * http://pessac/cesar/trac/wiki/20071128Cesar#LEONcore
 */
void
cesar_init (void)
{
    // Initialise the trace system.
    trace_init ();

    // Initialise the mac_store.
    station_test.mac_store = mac_store_init ();

    // Initialise the mac config.
    mac_config_init (&station_test.mac_config);

    // Intialise the pbproc.
    station_test.pbproc = pbproc_init (&station_test.mac_config,
                                       station_test.mac_store);

    // Initialise the mac ntb.
    mac_ntb_init (pbproc_get_phy(station_test.pbproc), &station_test.mac_config);

    // Initialise the SAR.
    station_test.sar = sar_init (station_test.mac_store, station_test.pbproc,
                                 pbproc_get_ca (station_test.pbproc),
                                 station_test.mac_config.seed);

    // Initialise the CL.
    station_test.cl = cl_init (station_test.mac_store, station_test.sar,
                               &station_test.mac_config);

    // Initialise the HLE
    station_test.hle = hle_init (station_test.cl);

    // Interface init.
    station_test.interface = interface_init (station_test.hle,
                                             station_test.cl, station_test.sar,
                                             &station_test.mac_config);

    // Init the control plane.
    cp_init ();

    // activate the reception of driver messages
    hle_activate (station_test.hle, true);
}


/* ---------------------- Main function -------------------*/
int
cyg_user_start (void)
{
    lib_stats_init ();
    cesar_init ();

    station_log_set_level(&my_station, STATION_LOG_DEBUG);
    my_station.pipe_log_fd = 1;

    // Stub the CE functionalities.
    sar_init_measure_context (station_test.sar, station_test.sar);
    sar_init_measurement_cb (station_test.sar, ce_measurements);

    // Stub the interface communication with the CP.
    interface_callback_init (station_test.interface, cp_mme_recv,
                             cp_mme_buffer_add, NULL);
    interface_callback_beacon_init (station_test.interface,
                                    cp_beacon_add, NULL);


    station_test.cl_table = NULL;

    list_init (&ce_rx_params_list);


    // Register the function calls.
    fcall_register (my_station.fcall, "fc_add_mme_link",
        &fc_add_mme_link , NULL);

    fcall_register (my_station.fcall, "fc_station_config",
        &fc_station_config, NULL);

    fcall_register (my_station.fcall, "fc_station_start",
        &fc_station_start, NULL);

    fcall_register (my_station.fcall, "fc_station_mme_send",
        &fc_station_mme_send , NULL);

    fcall_register (my_station.fcall, "fc_station_beacon_send",
        &fc_station_beacon_send, NULL);

    return 0;
}