summaryrefslogtreecommitdiff
path: root/cp/interf/src/interf.c
blob: 0cbdf42ba6742d9b4c87300ecbbc877cf2055acc (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/interf/interf.c
 * \brief   Interfaces pour l'envoie/réception de messages MME
 * \ingroup cp_interf
 */

#include "common/std.h"
#include "cp/interf/interf.h"
#include "cp/interf/inc/interf_private.h"

/*
 * locale Variables
 */

static interf_buffer_t pool[INTERF_NB_BUFFER];
static interf_frag_msg_t frag_msg[INTERF_MAX_FRAG_MSG_SIM];
const interf_desc_t interf_desc[]= 
    { // nb_buf, pf_add_buf, pf_send_buf, pf_rel_buf, pf_cb_rc
        { 8, interf_local_buf_release, interf_receive, NULL, NULL }, // CP
        { 88, NULL, NULL, NULL, NULL }, // SAR
        { 2, NULL, NULL, NULL, NULL }, // CE
        { 3, NULL, NULL, NULL, NULL } // HLE
    };
// FIFOs (STA and CCO)
static interf_msg_file_t fifo[2];
static cyg_mutex_t cco_fifo_lock;
// local buf ressources (for STA and CCO)
static uint nb_local_buf_available;
static cyg_mutex_t local_ressource_lock;
static cyg_cond_t local_ressource_wait;
// mbox for incomming msg
static cyg_handle_t incoming_mbox_handle;
static cyg_mbox incoming_mbox;
// mbox for buf to release
static cyg_handle_t buf_rel_mbox_handle;
static cyg_mbox buf_rel_mbox;
// delay in ticks for the frag MMI timeout
static cyg_tick_count_t frag_MMI_time_out_delay;


/***************************************************************/
/***************************************************************/
/***************************************************************/
/* 					"Private" functions                        */
/***************************************************************/
/***************************************************************/
/***************************************************************/

msg_mme_t *
interf_received_frag (msg_mme_t *msg)
{
    int i, j, checksum;
    msg_mme_t *first_frag;
    interf_buffer_t *buffer = NULL;

    dbg_assert ( !msg_check_wrong_mme_const_values (msg));

    // check if some old messages must be removed
    // this can be done anywhere, but i think here is a good place.
    interf_msg_purge ();
    // now, process the received fragment
    // check if previous fragment has been received
    for (i=0; i < INTERF_MAX_FRAG_MSG_SIM ; i++)
    {
        if ( (frag_msg[i].fmsn == msg->fmi.fmsn) 
            && (memcmp (frag_msg[i].osa,msg->osa, sizeof(msg->osa))==0) 
           )
        {
            // we find previous fragment, so stop searching
            break;
        }
    }
    if(i == INTERF_MAX_FRAG_MSG_SIM)
    {
        // we did not find previous fragment
        // so look for first empty slot
        for(i=0 ; i < INTERF_MAX_FRAG_MSG_SIM ; i++)
        {
            if( ! frag_msg[i].frag_counter)
            {
                // we found an empty slot
                frag_msg[i].fmsn = msg->fmi.fmsn;
                memcpy(frag_msg[i].osa, msg->osa, sizeof(msg->osa));
                break;
            }
        }
    }
    if(i == INTERF_MAX_FRAG_MSG_SIM)
    {
        // obviously, there is no more space...
        dbg_assert_print(0, "interf.c : frag_msg array is full, loose fragment");
        // and so, we loose the message
        interf_release_buf(msg);
        return NULL;
    }
    frag_msg[i].date_cyg_tick = cyg_current_time();
    // message are ordered in our structure
    frag_msg[i].msg[msg->fmi.fn_mi] = msg;
    frag_msg[i].frag_counter ++;
    // check if message is complete and return
    if( frag_msg[i].frag_counter == (msg->fmi.nf_mi + 1) )
    {
        // check if all fragment are coherent
        checksum = 0;
        for(j=0 ; j<frag_msg[i].frag_counter ; j++) 
            checksum += frag_msg[i].msg[j]->fmi.fn_mi;
        if(checksum != (frag_msg[i].frag_counter * msg->fmi.nf_mi / 2))
        {
            // the message fragment are not coherent (and they will be purged later) 
            dbg_assert_print(0, "interf.c : MME fragments incoherents\n");
            return NULL;
        }
        // update the buffer structure
        first_frag = frag_msg[i].msg[0];
        for(j=0; j<frag_msg[i].frag_counter; j++)
        {
            buffer = PARENT_OF(interf_buffer_t, msg, frag_msg[i].msg[j]);
            dbg_assert(buffer);
            buffer->next_msg = frag_msg[i].msg[j+1];
        }
        // the last fragment has no NextPart
        dbg_assert(buffer);
        buffer->next_msg = NULL;
        // reset the current slot
        bzero(&frag_msg[i], sizeof(interf_frag_msg_t));
        // and return ptr to first part of MME
        return first_frag;
    }
    return NULL;
}

void 
interf_msg_add (msg_mme_dest_t dest, msg_mme_t *msg)
{
    dbg_assert ( !msg_check_wrong_mme_const_values (msg));
    dbg_assert (dest <= MSG_CCO);

    // if requested, take the CCO mutex
    if (dest == MSG_CCO) 
cyg_mutex_lock ( &cco_fifo_lock);
    
    if (fifo[dest].start >= INTERF_NB_BUFFER)
    {
        // this should never occur, but who knowes...
        dbg_assert_print (0, "interf.c : fifo %i is full\n", MSG_CCO);
        // and we loose the message
        interf_release_buf (msg);
    }
    else
    {
        fifo[dest].msg[fifo[dest].start] = msg;
        fifo[dest].start ++;
    }

    // if necessary, release the CCO mutex
    if (dest == MSG_CCO)
cyg_mutex_unlock ( &cco_fifo_lock);
    
}

bool 
interf_msg_remove (msg_mme_dest_t dest, msg_mme_t *msg)
{
    uint i;
    bool return_value = false;

    dbg_assert ( !msg_check_wrong_mme_const_values (msg));
    dbg_assert (dest <= MSG_CCO);

    // if requested, take the CCO mutex
    if (dest == MSG_CCO)
cyg_mutex_lock ( &cco_fifo_lock);

    // search the message index in the fifo
    for (i=fifo[dest].start; i>0; i--)
        if (fifo[dest].msg[i-1] == msg)
            break;
    // if we found the message
    if (i)
    {
        // first we release the buffer(s) of the removed message
        interf_release_buf (msg);
        // then we remove the message from the fifo
        memcpy ( &fifo[dest].msg[i-1], &fifo[dest].msg[i], sizeof(fifo[dest].msg[0]) * (fifo[dest].start - i) );
        fifo[dest].start --;
        dbg_assert(fifo[dest].start < INTERF_NB_BUFFER);
        if(fifo[dest].current >= i) fifo[dest].current --;
        return_value = true;
    }
    // if necessary, release the CCO mutex
    if(dest == MSG_CCO) 
cyg_mutex_unlock( & cco_fifo_lock);

    return return_value;
}

void 
interf_local_buf_release (msg_mme_t *msg)
{
    dbg_assert ( !msg_check_wrong_mme_const_values (msg));

cyg_mutex_lock ( &local_ressource_lock);

    nb_local_buf_available ++;
    // wake up any waiting allocators
    cyg_cond_signal ( &local_ressource_wait);

cyg_mutex_unlock ( &local_ressource_lock);

}

void 
interf_buf_to_release (void)
{
    interf_buffer_t *buf;
    msg_mme_t *msg, *next_msg;
    #if DEBUG
    int i;
    #endif

    // as we have buffer to release in the mailbox
    while ( (msg = cyg_mbox_tryget (buf_rel_mbox_handle)) != NULL )
    {
        #if DEBUG
        dbg_assert( ! msg_check_wrong_mme_const_values(msg));
        // check that the message to release is not the last part
        // of a fragmented message (which would cause a severe  
        // memory leak)
        for(i=0; i<INTERF_NB_BUFFER; i++)
        {
            dbg_assert(pool[i].next_msg != msg);
        }
        #endif
        do
        {
            buf = PARENT_OF (interf_buffer_t, msg, msg);
            buf->size = 0;
            next_msg = buf->next_msg;
            buf->next_msg = NULL;
            interf_desc[buf->id].pf_add_buf (msg);
        } while (next_msg);
    }
}

/***************************************************************/
/***************************************************************/
/***************************************************************/
/* 					"Public" functions                         */
/***************************************************************/
/***************************************************************/
/***************************************************************/

E_ErrCode 
interf_init (void)
{
    unsigned int i, j, k;
    #if DEBUG
    unsigned int nb_buf=0;
    // check parameters
    for(i=0; i<COUNT(interf_desc); i++) nb_buf += interf_desc[i].nb_buf;
    if(nb_buf != INTERF_NB_BUFFER) return NbBufIncoherent;
    if(interf_desc[INTERF_SAR].nb_buf < (INTERF_MAX_FRAG_PER_MSG * INTERF_MAX_FRAG_MSG_SIM) ) return NbBufIncoherent;
    if(interf_desc[INTERF_SAR].nb_buf < CYGNUM_KERNEL_SYNCH_MBOX_QUEUE_SIZE)
    {
        printf("interf.h : Warning : InterfDesc[SAR].NbBuf should be greater than CYGNUM_KERNEL_SYNCH_MBOX_QUEUE_SIZE\n");
    }
    if (sizeof(pool[0].msg) != SAR_MSDU_PAYLOAD_MAX_SIZE )
    {
        printf("wrong mmentry size : %lu octets instead of %i\n", sizeof(pool[0].msg), SAR_MSDU_PAYLOAD_MAX_SIZE);
        printf("	osa  		   : %lu\n", sizeof(pool[0].msg.osa));
        printf("	oda  		   : %lu\n", sizeof(pool[0].msg.oda));
        printf("	v_lan_tag      : %lu\n", sizeof(pool[0].msg.v_lan_tag));
        printf("	m_type		   : %lu\n", sizeof(pool[0].msg.m_type));
        printf("	mmv;  		   : %lu\n", sizeof(pool[0].msg.mmv));
        printf("	mm_type;       : %lu\n", sizeof(pool[0].msg.mm_type));
        printf("	fmi;           : %lu\n", sizeof(pool[0].msg.fmi));
        printf("	mm_entry	   : %lu\n", sizeof(pool[0].msg.mm_entry));
    }
    #endif

    // calculate the frag MMI timeout delay
    frag_MMI_time_out_delay = FragMMI_ReassemblyTimeOut_sec;
    // convert it from s to ms
    frag_MMI_time_out_delay *= 1000;
    // and then convert it to system ticks
    frag_MMI_time_out_delay = interf_ms_to_cyg_tick (frag_MMI_time_out_delay);
    // attribute buffer to their owner and use the loop to init msg const values
    bzero (pool, sizeof(pool));
    for(i=0, j=0, k=0 ; i<COUNT(interf_desc) ; i++)
    {
        j += interf_desc[i].nb_buf;
        for(  ; k<j ; k++)
        {
            pool[k].id = i;
            msg_set_mme_const_values(&pool[k].msg);
        }
    }
    dbg_assert(k == INTERF_NB_BUFFER);
    // Init FragMsg
    bzero(frag_msg, sizeof(frag_msg));
    // Init FIFOs
    bzero(fifo, sizeof(fifo));
    cyg_mutex_init( & cco_fifo_lock);
    // init of local ressources
    nb_local_buf_available = 0;
    cyg_mutex_init( & local_ressource_lock);
    cyg_cond_init( & local_ressource_wait, & local_ressource_lock);
    // Init callback and transmit buffer's pool
    for(i=0, j=0 , k=0 ; i<COUNT(interf_desc) ; i++)
    {
        if(interf_desc[i].pf_rel_buf != NULL) interf_desc[i].pf_rel_buf();
        if(interf_desc[i].pf_cb_rc != NULL) interf_desc[i].pf_cb_rc();
        k = j;
        j += interf_desc[i].nb_buf;
        if(interf_desc[i].pf_add_buf != NULL)
        {
            for(; k<j; k++)
            {
                interf_desc[i].pf_add_buf( & pool[k].msg);
            }
        }
    }
    // init of mailboxes
    cyg_mbox_create( & incoming_mbox_handle, & incoming_mbox);
    cyg_mbox_create( & buf_rel_mbox_handle, & buf_rel_mbox);
    // init SAR layer
    interf_sar_layer_init();
    return Success;
}

void 
interf_receive (msg_mme_t *msg)
{
    dbg_assert ( !msg_check_wrong_mme_const_values (msg));

    #if DEBUG
    dbg_assert(cyg_mbox_tryput(incoming_mbox_handle, msg));
    #else
    cyg_mbox_put (incoming_mbox_handle, msg);
    #endif
    // set the flag for the station
    cyg_flag_setbits (&station_flag, STATION_FLAG_MSG_RCV);
}

void 
interf_received (void)
{
    msg_mme_t * msg;
    bool message_complete, msg_RSP_or_CNF;
    int msg_nb;

    // as we have msg in the mailbox
    while ( (msg = cyg_mbox_tryget (incoming_mbox_handle)) != NULL )
    {
        dbg_assert ( !msg_check_wrong_mme_const_values (msg));
        // TODO : vérifier que l'ODA est bien celle de la station
        // check if message is complete
        if (msg->fmi.nf_mi == 0)
            message_complete = true;
        else
        {
            msg = interf_received_frag (msg);
            if (msg != NULL)
                message_complete = true;
            else
                message_complete = false;
        }
        // if MME is not complete, then look for next message
        if ( !message_complete)
            continue;
        // check if it is a .CNF or .RSP type message
        msg_RSP_or_CNF = msg->mm_type & 0x1;
        // check if message is for CCO or STA and add it in corresponding fifo
        msg_nb = msg_get_number (msg);
        interf_msg_add (msg_list[msg_nb].msg_dest, msg);
        // TODO : set corresponding flags
    }
}

void 
interf_send (msg_mme_t *msg, uint msg_size, interf_id_t interf_id)
{
    dbg_assert ( !msg_check_wrong_mme_const_values (msg));
    dbg_assert (msg_size <= SAR_MSDU_PAYLOAD_MAX_SIZE);
    dbg_assert (interf_id < COUNT (interf_desc));

    // check if padding is requested (and if yes, do so)
    if (msg_size < SAR_MSDU_PAYLOAD_MIN_SIZE)
        msg_size = SAR_MSDU_PAYLOAD_MIN_SIZE;
    // and then, send the message
    dbg_assert_print (interf_desc[interf_id].pf_send_buf, "interface not implemented\n");
    interf_desc[interf_id].pf_send_buf (msg);
}

msg_mme_t *
interf_msg_get_last (msg_mme_dest_t dest)
{
    msg_mme_t *return_value = NULL;

    dbg_assert (dest < COUNT (fifo));

    fifo[dest].current = fifo[dest].start;
    if (fifo[dest].current != 0)
        return_value = fifo[dest].msg[fifo[dest].current - 1];
    return return_value;
}

msg_mme_t *
interf_msg_get_last_of_type (msg_mme_dest_t dest, mm_type_t mm_type)
{
    msg_mme_t *return_value = NULL;

    dbg_assert (dest < COUNT (fifo));

    fifo[dest].current = fifo[dest].start;
    while (fifo[dest].current != 0)
    {
        if (fifo[dest].msg[fifo[dest].current - 1]->mm_type == mm_type)
        {
            return_value = fifo[dest].msg[fifo[dest].current - 1];
            break;
        }
        fifo[dest].current --;
    }
    return return_value;
}

msg_mme_t *
interf_msg_get_previous (msg_mme_dest_t dest)
{
    msg_mme_t *return_value = NULL;

    dbg_assert (dest < COUNT (fifo));

    if (fifo[dest].current > 1)
    {
        fifo[dest].current --;
        return_value = fifo[dest].msg[fifo[dest].current - 1];
    }
    return return_value;
}

msg_mme_t *
interf_msg_get_previous_of_type (msg_mme_dest_t dest, mm_type_t mm_type)
{
    msg_mme_t *return_value = NULL;

    dbg_assert (dest < COUNT (fifo));

    while (fifo[dest].current > 1)
    {
        fifo[dest].current --;
        if (fifo[dest].msg[fifo[dest].current - 1]->mm_type == mm_type)
        {
            return_value = fifo[dest].msg[fifo[dest].current - 1];
            break;
        }
    }
    return return_value;
}

msg_mme_t *
interf_msg_get_next_part (msg_mme_t *msg)
{
    dbg_assert ( !msg_check_wrong_mme_const_values (msg));

    return PARENT_OF(interf_buffer_t, msg, msg)->next_msg;
}

void 
interf_release_buf (msg_mme_t *msg)
{
    dbg_assert ( !msg_check_wrong_mme_const_values (msg));

    #if DEBUG
    dbg_assert(cyg_mbox_tryput(buf_rel_mbox_handle, msg));
    #else
    cyg_mbox_put (buf_rel_mbox_handle, msg);
    #endif
    // set the flag
    cyg_flag_setbits (&station_flag, STATION_FLAG_BUF_REL);
}

msg_mme_t *
interf_give_buf (void)
{
    msg_mme_t *msg;
    int i;

cyg_mutex_lock ( &local_ressource_lock);
    // if there is no buffer available, wait for one
    while (nb_local_buf_available == 0)
        cyg_cond_wait ( &local_ressource_wait);
    // search for an available buffer
    dbg_assert (INTERF_CP == 0);
    for (i=0; i<interf_desc[INTERF_CP].nb_buf; i++)
    {
        if (pool[i].size == 0)
            break;
    }
    dbg_assert (i != interf_desc[INTERF_CP].nb_buf);
    // lock the availble buffer and return it
    pool[i].size = 1;
    msg = &pool[i].msg;
    nb_local_buf_available --;
cyg_mutex_unlock ( &local_ressource_lock);
    // and return the buffer
    dbg_assert ( !msg_check_wrong_mme_const_values (msg));
    return msg;
}

void 
interf_msg_purge (void)
{
    int i, j;
    cyg_tick_count_t time_out_date;
    dbg_assert (frag_MMI_time_out_delay >= 1);
    // calculate the date before which messages has expired
    time_out_date = cyg_current_time ();
    if (time_out_date < frag_MMI_time_out_delay)
        return;
    time_out_date -= frag_MMI_time_out_delay;
    // check if some messages has expired
    for (i=0; i<INTERF_MAX_FRAG_MSG_SIM ; i++)
    {
        if ( (frag_msg[i].date_cyg_tick != 0)&& (frag_msg[i].date_cyg_tick
                < time_out_date))
        {
            // the message has expired, so remove it parts
            for (j=0; j<INTERF_MAX_FRAG_PER_MSG ; j++)
            {
                if (frag_msg[i].msg[j])
                    interf_release_buf (frag_msg[i].msg[j]);
            }
            // and reset it slot in the waiting file 
            bzero ( &frag_msg[i], sizeof(frag_msg[i]));
        }
    }
}

cyg_tick_count_t 
interf_ms_to_cyg_tick (uint ms)
{
    cyg_resolution_t resolution;
    cyg_tick_count_t return_value;

    return_value = ms;
    // convert it from ms to ns
    return_value *= 1000000;
    // and then convert it to system ticks
    resolution = cyg_clock_get_resolution (cyg_real_time_clock ());
    return_value *= resolution.divisor;
    return_value /= resolution.dividend;
    return return_value;
}