summaryrefslogtreecommitdiff
path: root/cleopatre/plcdrv/gidel/src/mailbox.c
blob: db116d34219ca072560434b7af521f2cf9e48fb1 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mailbox.c
 * \brief   Mailbox layer for the PLC driver.
 * \ingroup Cleopatre - Isis
 *
 * this layer provide all mechanisms to manage mailboxes.
 */

#include "common.h"
#include <stdio.h>
#include <string.h>
#ifdef _UTESTS_
#include "hal_stub.h"
#include "processing_stub.h"
#else
#include "hal.h"
#include "processing.h"
#endif
#include "mailbox.h"

/** Data parameters */
#define MBX_MSG_TYPE_DATA_TYPE_DATA 0
#define MBX_MSG_TYPE_DATA_TYPE_MME  1
typedef struct {
    uint32_t type         :8;
    uint32_t length       :4;
    uint32_t param_type   :1;
    uint32_t param_length :11;
    uint32_t              :8;
} __attribute__ ((__packed__)) mbx_data_hdr_t;

/** Buffer add parameters */
#define MBX_MSG_TYPE_BUFF_ADD_TYPE_DATA       0
#define MBX_MSG_TYPE_BUFF_ADD_TYPE_MME        1
#define MBX_MSG_TYPE_BUFF_ADD_TYPE_INTERFACE  2
typedef struct {
    uint32_t type         :8;
    uint32_t length       :4;
    uint32_t param_type   :2;
    uint32_t reserved     :18;
} __attribute__ ((__packed__)) mbx_buffer_add_hdr_t;

/** Send done parameters */
typedef struct {
    uint32_t type         :8;
    uint32_t length       :4;
    uint32_t reserved     :20;
} __attribute__ ((__packed__)) mbx_send_done_hdr_t;

/** Interface parameters */
#define MBX_MSG_TYPE_INTERFACE_TYPE_INTERFACE 0
#define MBX_MSG_TYPE_INTERFACE_TYPE_SNIFFER   1
#define MBX_MSG_TYPE_INTERFACE_TYPE_FCALL     2
typedef struct {
    uint32_t type         :8;
    uint32_t length       :4;
    uint32_t param_type   :8;
    uint32_t param_length :11;
    uint32_t reserved     :1;
} __attribute__ ((__packed__)) mbx_interface_hdr_t;

/** Mailbox type */
#define MBX_MSG_TYPE_DATA       0
#define MBX_MSG_TYPE_BUFFER_ADD 1
#define MBX_MSG_TYPE_SEND_DONE  2
#define MBX_MSG_TYPE_INTERFACE  3

/** Mailbox Header Structure */
typedef union {
    mbx_data_hdr_t data;
    mbx_buffer_add_hdr_t buffer_add;
    mbx_send_done_hdr_t send_done;
    mbx_interface_hdr_t interface;
} mbx_hdr_t;

/** Mailbox interface Structure */
typedef struct {
    uint32_t way        :1;
    uint32_t encryption :1;
    uint32_t data_type  :3;
    uint32_t reserved   :27;
    uint32_t buffer_ptr;
} __attribute__ ((__packed__)) mbx_interface_t;

/** Mailbox Data Structure */
typedef union {
    uint32_t buffer_ptr;
    mbx_interface_t interface;
} __attribute__ ((__packed__)) mbx_data_t;

/** Mailbox Structure */
typedef struct {
    mbx_hdr_t header;
    mbx_data_t data;
} __attribute__ ((__packed__)) mbx_t;


/** Global variables */
void (*txack_handler)(void) = 0;

/** 
 * Send a message to the HLE layer.
 *
 * \param  pointer  the data message pointer.
 * \param  length  the length of the data message.
 * \param  type  type of message.
 * \return  error code.
 */
int mailbox_send(void *pointer, uint32_t length, enum buffer_type type)
{
    mbx_t msg;
    uint32_t gidel_pointer;
    int status;

    //Check pointer ans length
    if(pointer == NULL || length == 0)
        return -1;

    //Check room
    status = halmbx_A2Lmail_status_queue();
    if(status == NEARLY_FULL || status == FULL)
    {
        //the mailbox is nearly full stop upper layers
        //and activate tx_ack interrupt
        A2La_it_enable();
        if(status == FULL)
            return status;
    }
    else
    {
        A2La_it_disable();
    }

    TRACE("In the mailbox send\n");

    memset(&msg, 0, sizeof(msg));

    switch(type)
    {
    case DATA: 
               //header construction
               msg.header.data.type  = MBX_MSG_TYPE_DATA;
               msg.header.data.length = 1;
               TRACE("SEND data data\n");
               msg.header.data.param_type = MBX_MSG_TYPE_DATA_TYPE_DATA;
               msg.header.data.param_length = length;
               break;
    case MME:
               //header construction
               msg.header.data.type  = MBX_MSG_TYPE_DATA;
               msg.header.data.length = 1;
               TRACE("SEND data mme\n");
               msg.header.data.param_type = MBX_MSG_TYPE_DATA_TYPE_MME;
               msg.header.data.param_length = length;
               break;
    case INTERFACE:
               //header construction
               msg.header.interface.type  = MBX_MSG_TYPE_INTERFACE;
               msg.header.interface.length = 1;
               TRACE("SEND interface\n");
               msg.header.interface.param_type = MBX_MSG_TYPE_INTERFACE_TYPE_FCALL;
               msg.header.interface.param_length = length;
               break;
    default:
               return -1;
    }

    //Copy the frame to send to the HLE into the Gidel Memory
    //because the leon code can only access to Gidel Memory
    gidel_pointer = halmbx_save_in_gidel_memory((uint32_t*)pointer, length);
    if(gidel_pointer == 0) //Do not append
        return FULL;

    //data construction
    msg.data.buffer_ptr = gidel_pointer;

    //Copy to ring buffer and send to HLE layer
    if(halmbx_copy_to_ring((uint32_t*)&msg, sizeof(mbx_hdr_t) + (msg.header.data.length * sizeof(uint32_t))))
        return -1;

    //Update the ring management
    if(halmbx_A2Lmail_update(sizeof(mbx_hdr_t) + (msg.header.data.length * sizeof(uint32_t))))
        return -1;

#ifdef DEBUG
    uint32_t *ptr = (uint32_t*)&msg;
    TRACE("msg(size=%d)=%x, %x, %x\n", msg.header.data.length * sizeof(uint32_t), *ptr, *(ptr+1), *(ptr+2));
#endif

    return 0;
}

/** 
 * Send a new buffer to the HLE layer.
 *
 * \param  pointer  the buffer pointer.
 * \param  type  type of message.
 * \return  error code.
 */
#ifdef _UTESTS_
int mailbox_buffer_add(enum buffer_type type)
#else
static int mailbox_buffer_add(enum buffer_type type)
#endif
{
    mbx_t msg;
    uint32_t *gidel_pointer;
    int status;

    //Check room
    status = halmbx_A2Lmail_status_queue();
    if(status == NEARLY_FULL || status == FULL)
    {
        //The mailbox is nearly full stop upper layers
        //and activate tx_ack interrupt
        A2La_it_enable();
        if(status == FULL)
            return status;
    }
    else
    {
        A2La_it_disable();
    }

    //Find a free buffer in Gidel Memory
    gidel_pointer = (uint32_t*)halmbx_alloc_in_gidel_memory();
    if(gidel_pointer == 0) //Do not append
        return FULL;

    memset(&msg, 0, sizeof(msg));

    //Header construction
    msg.header.buffer_add.type  = MBX_MSG_TYPE_BUFFER_ADD;
    msg.header.buffer_add.length = 1;
    switch(type)
    {
    case DATA: 
               TRACE("SEND BUFF ADD data data\n");
               msg.header.buffer_add.param_type = MBX_MSG_TYPE_BUFF_ADD_TYPE_DATA;
               break;
    case MME:
               TRACE("SEND BUFF ADD data mme\n");
               msg.header.buffer_add.param_type = MBX_MSG_TYPE_BUFF_ADD_TYPE_MME;
               break;
    case INTERFACE:
               TRACE("SEND BUFF ADD interface\n");
               msg.header.buffer_add.param_type = MBX_MSG_TYPE_BUFF_ADD_TYPE_INTERFACE;
               break;
    default:
               return -1;
    }

    //Data construction
    msg.data.buffer_ptr = (uint32_t)gidel_pointer;

    //Copy to ring buffer
    if(halmbx_copy_to_ring((uint32_t*)&msg, sizeof(mbx_hdr_t) + (msg.header.buffer_add.length * sizeof(uint32_t))))
        return -1;

    //Update the ring management
    if(halmbx_A2Lmail_update(sizeof(mbx_hdr_t) + (msg.header.buffer_add.length * sizeof(uint32_t))))
        return -1;

#ifdef DEBUG
    uint32_t *ptr = (uint32_t*)&msg;
    TRACE("msg(size=%d)=%x, %x, %x\n", msg.header.buffer_add.length * sizeof(uint32_t), *ptr, *(ptr+1), *(ptr+2));
#endif

    return 0;
}

/** 
 * Receive everything from the HLE layer.
 *
 * \return  error code.
 */
int mailbox_receive(void)
{
    mbx_t msg;
    uint32_t *pc_pointer;
    int result = 0;

    while(halmbx_L2Amail_not_empty_queue())
    {
        //Copy the new message from the ring buffer
        if(halmbx_copy_from_ring((uint32_t*)&msg, sizeof(msg)))
            return -2;

        //Update the ring management
        if(halmbx_L2Amail_update(sizeof(mbx_hdr_t) + (msg.header.send_done.length * sizeof(uint32_t))))
            return -3;

        //Proceed the message
#ifdef DEBUG
        uint32_t *ptr = (uint32_t*)&msg;
        TRACE("msg(size=%d)=%x, %x, %x\n", msg.header.send_done.length * sizeof(uint32_t), *ptr, *(ptr+1), *(ptr+2));
#endif
        switch(msg.header.send_done.type)
        {
        case MBX_MSG_TYPE_SEND_DONE: //Free the Gidel memory allocated with the buffer_add
            TRACE("RECEIVE a send_done\n");
            if(halmbx_free_in_gidel_memory((uint32_t*)msg.data.buffer_ptr))
                result = -4;
            else
                result = 0;
            break;

        case MBX_MSG_TYPE_INTERFACE: //Copy the frame receive from the HLE into the PC Memory
            //because the PC driver code can only access to PC Memory
            TRACE("RECEIVE an interface\n");
            pc_pointer = (uint32_t*)halmbx_save_in_pc_memory((uint32_t*)msg.data.buffer_ptr, (int)msg.header.interface.param_length);
            if(pc_pointer == NULL)
            {
                result = -5;
                break;
            }

            //Transmit to the upper layers
            if(processing_receive(pc_pointer, msg.header.interface.param_length, INTERFACE))
            {
                result = -6;
                break;
            }
            mailbox_buffer_add(INTERFACE);

            //Free the previous buffer allocated in the PC Memory
            halmbx_free_in_pc_memory(pc_pointer);
            result = 0;
            break;

        case MBX_MSG_TYPE_DATA: //Copy the frame receive from the HLE into the PC Memory
            //because the PC driver code can only access to PC Memory
            TRACE("RECEIVE a data\n");
            pc_pointer = (uint32_t*)halmbx_save_in_pc_memory((uint32_t*)msg.data.buffer_ptr, (int)msg.header.data.param_length);
            if(pc_pointer == NULL)
            {
                result = -7;
                break;
            }

            //Transmit to the upper layers
            if(msg.header.data.param_type == MBX_MSG_TYPE_DATA_TYPE_MME)
            {
                if(processing_receive(pc_pointer, msg.header.data.param_length, MME))
                {
                    result = -8;
                    break;
                }
                mailbox_buffer_add(MME);
            }
            else
            {
                if(processing_receive(pc_pointer, msg.header.data.param_length, DATA))
                {
                    result = -9;
                    break;
                }
                mailbox_buffer_add(DATA);
            }

            //Free the previous buffer allocated in the PC Memory
            halmbx_free_in_pc_memory(pc_pointer);
            result = 0;
            break;
        default:
            result = -1;
            break;
        }
        if(result)
            break;
    }
    return result;
}

/** 
 * Receive everything from the HLE layer.
 *
 * \param  data  argument (not used).
 */
void mailbox_irq (void*data)
{
    int result;
    switch(check_its())
    {
    case A2LA :
        clr_A2La_interrupt();
        if(txack_handler)
            (*txack_handler)();
        break;

    case L2AT : 
        clr_L2At_interrupt();
        result = mailbox_receive();
        TRACE("mailbox_receive=%d\n\n",result);
        break;

    case ALL : 
        clr_A2La_interrupt();
        if(txack_handler)
            (*txack_handler)();
        clr_L2At_interrupt();
        result = mailbox_receive();
        TRACE("mailbox_receive=%d\n\n",result);
        break;
    case NONE :
    default:
        break;
    }
}

/** 
 * Initialize the mailbox layer.
 *
 * \param  init  user information.
 */
void mailbox_init(struct init_info *init)
{
    int i;
    //Register TX acknowledge Interrupt handler
    txack_handler = init->txack_handler;

    //Init lower layers
    halmbx_init(init, &mailbox_irq);

    //Stop all interrupt
    A2La_it_disable();
    L2At_it_disable();
    
    //Unmask sending interrupt
    L2At_it_enable();

    //Add to Leon the RX buffers
//    for(i=0 ; i < init->nb_data_buf_alloc ; i++)
//        mailbox_buffer_add(DATA);
//    for(i=0 ; i < init->nb_mme_buf_alloc ; i++)
//        mailbox_buffer_add(MME);
//    for(i=0 ; i < init->nb_interface_buf_alloc ; i++)
//        mailbox_buffer_add(INTERFACE);

      mailbox_buffer_add(INTERFACE);
      mailbox_buffer_add(INTERFACE);
}

/** 
 * UnInitialize the mailbox layer.
 */
void mailbox_uninit(void)
{
    //Mask all interrupts
    A2La_it_disable();
    L2At_it_disable();

    //Uninit lower layers
    halmbx_uninit();
}