summaryrefslogtreecommitdiff
path: root/mac/sar/test/maximus_test/src/Maximus_sar.cpp
blob: c5fce74efacb98adb01bc4d4ef946ee88b30e3f8 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    Maximus_sar.c
 * \brief   header of the segmentation test function on maximus.
 * \ingroup mac/sar/test/maximus_test/maximus/src
 * 
 * Maximus selects a packet to segment in a raw of Ethernet packets.
 * This packet is split in  packet of 1024 bytes long corresponding to the
 * maximum size of the maximus parameter.
 * 
 * Maximus send the segmentation request and provide the parameters for the 
 * segmentation module of the SAR.
 * 
 * The Station take the parameters and reconstitute the packet to segment 
 * ( simulate the CL or CP function ). It gives the data necessary for 
 * the SAR to segment the packet.
 * 
 * Once the segmentation is over, the Station answer the request by giving 
 * back the PBs filled with the Ethernet packet.
 * 
 * Maximus take the PBs and verify each byte comparing it with the Ethernet
 * packet data.
 */

#include "maximus/common/interfaces/Maximus.h"
#include "maximus/common/interfaces/Sta.h"
#include "maximus/common/interfaces/Msg.h"
#include "Error.h"
#include <iostream> // for 'cout', 'cerr' and 'clog'
using namespace std;

#include "mac/sar/test/maximus_test/inc/blk_t.h"
#include "mac/sar/test/maximus_test/inc/max_job.h"

typedef void (station_cb_t) (Msg &msg);

/**
 * The station 1 callback
 * 
 * \param  msg the message
 */
void station1_cb (Msg &msg);

/**
 * The station 2 callback
 * 
 * \param  msg the message
 */
void station2_cb (Msg &msg);

/**
 * Launch the segmentation process on the sta with the packet provided given by
 * packet number.
 * 
 * \param  maximus the maximus object simulator
 * \param  station the station on which the segmentation must be done
 * \param  sta_cb the callback to call once the bridge dma had finished to
 * process a segmentation.
 * \param job the job to be processed
 * 
 * \return  a boolean set to true.
 */
bool segmentation (Maximus *maximus, Sta *station, station_cb_t sta_cb,
        sar_max_job_t *job) throw();

/**
 * Launch the segmentation process on the sta with the packet provided given by
 * packet number.
 * 
 * \param  maximus the maximus object simulator
 * \param  station the station on which the segmentation must be done
 * \param  sta_cb the callback to call once the bridge dma had finished to
 * process a segmentation.
 * \param job the job to be processed
 * 
 * 
 * \return  a boolean set to true.
 */
bool reassembly (Maximus *maximus, Sta *station, station_cb_t sta_cb,
        sar_max_job_t *sar_max_job) throw();

/**
 * Launch the procedure with n segmentation and one reassembly
 * 
 * \param  argc the quantity of arguments provided
 * \param  argv the char array containing the arguments
 * \param  sar_max_job an array containing the list of the row's number of the 
 * Ethernet packets.
 * 
 * \return  the end value
 */
int launch_x_segs (int argc, char *argv[], sar_max_job_t *sar_max_job);

bool station1_rsp;
bool station2_rsp;
bool test_fail;

int fifo_blk[2];

int max_jobs_qte;

int main (int argc, char *argv[])
{
    sar_max_job_t *sar_max_job = NULL;

    try
    {
        station1_rsp = 0;
        station2_rsp = 0;

        /* create the fifo */
        if (pipe (fifo_blk) < 0)
        {
            printf ("Can not create the pipe for the PBs communication\n");
            exit (-1);
        }

        sar_max_job = (sar_max_job_t *) malloc (sizeof (sar_max_job_t));
        sar_max_job->next = (sar_max_job_t *) malloc (sizeof (sar_max_job_t));
        sar_max_job->next->next = (sar_max_job_t *) malloc (sizeof (sar_max_job_t));
        sar_max_job->next->next->next = NULL;

        /* Init job 1 */
        sar_max_job->lid = 1;
        sar_max_job->tei_src = 1;
        sar_max_job->tei_dest = 2;
        sar_max_job->bcast = false;
        sar_max_job->mf_type = 2;
        sar_max_job->packet_id = 0;

        /* Init job 2 */
        sar_max_job->next->lid = 1;
        sar_max_job->next->tei_src = 1;
        sar_max_job->next->tei_dest = 2;
        sar_max_job->next->bcast = false;
        sar_max_job->next->mf_type = 2;
        sar_max_job->next->packet_id = 1;
        
        /* Init job 3 */
        sar_max_job->next->next->lid = 1;
        sar_max_job->next->next->tei_src = 1;
        sar_max_job->next->next->tei_dest = 2;
        sar_max_job->next->next->bcast = false;
        sar_max_job->next->next->mf_type = 2;
        sar_max_job->next->next->packet_id = 2;

        max_jobs_qte = 3;

        return launch_x_segs (argc, argv, sar_max_job);
    }
    catch (Error &e)
    {
        sar_max_job_t *sar_max_job_curr;
        while (sar_max_job)
        {
            sar_max_job_curr = sar_max_job;
            sar_max_job = sar_max_job->next;
            free (sar_max_job_curr);
        }
    }
}

/**
 * Launch the procedure with n segmentation and one reassembly
 * 
 * \param  argc the quantity of arguments provided
 * \param  argv the char array containing the arguments
 * \param  sar_max_job an array containing the list of the row's number of the 
 * Ethernet packets.
 * 
 * \return  the end value
 */
int launch_x_segs (int argc, char *argv[], sar_max_job_t *sar_max_job)
{
    bool logs;
    int i;
    test_fail = false;

    Maximus maximus;
    maximus.init (argc, argv);

    Sta station1 = maximus.create_sta ();
    station1.debug ();
    Sta station2 = maximus.create_sta ();
    station2.debug ();

    // activate the sta 1 logs
    Msg fc = maximus.create_fc ("activate_logs");
    logs = true;
    fc.add_param ("rea", sizeof(unsigned char), (unsigned char *) &logs);
    //fc.send (station1);
    fc.send (station2);

    segmentation (&maximus, &station1, station1_cb, sar_max_job);

    i = 0;
    while (!station1_rsp && !test_fail)
    {
        if (i > 50)
        {
            test_fail = true;
        }
        i++;

        maximus.process ();
    }

    if (test_fail)
        return -1;

    reassembly (&maximus, &station2, &station2_cb, sar_max_job);

    i = 0;
    while (!station2_rsp && !test_fail)
    {
        if (i > 50)
        {
            test_fail = true;
        }
        i++;

        maximus.process ();
    }
    
    maximus.wait (100);

    if (test_fail)
        return -1;
    else
        return 0;
}

/**
 * The station 1 callback
 * 
 * \param  msg the message
 */
void station1_cb (Msg &msg)
{
    char value[2];
    char id [5];
    unsigned int val;
    void *msg_return;
    blk_t *blk;

    //char * buffer = new char [FUNCTION_CALL_PARAM_MAX_SIZE];
    char * buffer = new char [512];
    unsigned long length = 512*sizeof(char);

    // Try to get the error message if the segmentation fail.
    msg_return = msg.bind_param ("type", length, (unsigned char * &) buffer);

    if (strcmp (buffer, "SEG") != 0)
    {
        test_fail = true;
    }
    else
    {
        blk = (blk_t *) malloc (sizeof(blk_t));
        blk->qte = msg.bind_param<unsigned int> ("qte");

        for (val = 0; val < blk->qte; val++)
        {
            sprintf (value, "%d", val);

            strcpy (id, "pb");
            strcat (id, value);

            length = 512*sizeof(char);
            msg_return = msg.bind_param (id, length,
                    (unsigned char * &) buffer);

            if (msg_return != NULL)
            {
                memcpy (blk->pbs[val], buffer, 512);
            }

            strcpy (id, "pbh");
            strcat (id, value);
            blk->header[val] = msg.bind_param<unsigned int> (id);
        }

        write (fifo_blk[1], blk, sizeof(blk_t));
    }

    delete [] buffer;
    buffer = NULL;

    station1_rsp = true;
}

/**
 * The station 2 callback
 * 
 * \param  msg the message
 */
void station2_cb (Msg &msg)
{
    station2_rsp = true;
}

/**
 * Launch the segmentation process on the sta with the packet provided given by
 * packet number.
 * 
 * \param  maximus the maximus object simulator
 * \param  station the station on which the segmentation must be done
 * \param  sta_cb the callback to call once the bridge dma had finished to
 * process a segmentation.
 * \param job the job to be processed
 * 
 * \return  a boolean set to true.
 */
bool segmentation (Maximus *maximus, Sta *station, station_cb_t sta_cb,
        sar_max_job_t *job) throw()
{
    sar_max_job_t *curr;
    unsigned char job_nb;
    char id[5];
    char val[2];

    if (!maximus || !station || !job)
    {
        throw Error (__PRETTY_FUNCTION__, "!maximus || !station || !job");
    }

    curr = job;

    //create the function call
    Msg fc_sta = maximus->create_fc ("segmentation");

    /* add the buffer address */
    job_nb = 0;
    for (curr = job; curr; curr = curr->next, job_nb++)
    {
        sprintf (val, "%d", job_nb);
        strcpy (id, "job");
        strcat (id, val);
        fc_sta.add_param (id, sizeof(sar_max_job_t), (unsigned char*) curr);
        printf ("[DEBUG - MAX] job id : %d\n", curr->packet_id);
    }

    fc_sta.add_param ("qte", sizeof(unsigned char), (unsigned char*) &job_nb);

    fc_sta.set_cb (sta_cb);
    fc_sta.send (*station);

    return true;
}

/**
 * Launch the segmentation process on the sta with the packet provided given by
 * packet number.
 * 
 * \param  maximus the maximus object simulator
 * \param  station the station on which the segmentation must be done
 * \param  sta_cb the callback to call once the bridge dma had finished to
 * process a segmentation.
 * \param job the job to be processed
 * 
 * 
 * \return  a boolean set to true.
 */
bool reassembly (Maximus *maximus, Sta *station, station_cb_t sta_cb,
        sar_max_job_t *sar_max_job) throw()
{
    char value[3];
    char id[5];
    unsigned int i;
    sar_max_job_t *curr;
    blk_t blk;
    unsigned char job_nb;

    if (!maximus || !station || !sar_max_job)
    {
        throw Error (__PRETTY_FUNCTION__, "!maximus || !station || !job");
    }

    // take the first element of the fifo
    if (read (fifo_blk[0], &blk, sizeof(blk_t)) != sizeof(blk_t))
    {
        return false;
    }

    //create the function call
    Msg fc_sta = maximus->create_fc ("reassembly");

    job_nb = 0;
    for (curr = sar_max_job; curr; curr = curr->next, job_nb++)
    {
        sprintf (value, "%d", job_nb);
        strcpy (id, "job");
        strcat (id, value);
        fc_sta.add_param (id, sizeof(sar_max_job_t), (unsigned char*) curr);
        printf ("[DEBUG - MAX] job id : %d\n", curr->packet_id);
    }

    fc_sta.add_param ("job_qte", sizeof(unsigned char),
            (unsigned char*) &job_nb);

    /* add the buffer address */
    fc_sta.add_param ("pb_qte", sizeof(unsigned int),
            (unsigned char*) &blk.qte);

    for (i = 0; i < blk.qte; i++)
    {
        sprintf (value, "%d", i);
        strcpy (id, "pb");
        strcat (id, value);

        fc_sta.add_param (id, 1024 * sizeof(unsigned char),
                (unsigned char*) blk.pbs[i]);

        strcpy (id, "pbh");
        strcat (id, value);
        fc_sta.add_param (id, sizeof(unsigned int),
                (unsigned char*) &blk.header[i]);
    }

    fc_sta.set_cb (sta_cb);
    fc_sta.send (*station);

    return true;
}