summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/plcdrv/gidel/src/plc_drv.c
blob: 22fee484de40a40c506d00082e61c1d7e1b16cd4 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    plc_drv.c
 * \brief   OS layer for the PLC driver.
 * \ingroup Cleopatre - Isis
 *
 * For the Gidel prototype this layer only precise how to receive a packet
 * and what to do when we receive an acknowledge of a previous sending frame.
 */

#define DEBUG

#define DRV_NAME	"SPC300_Gidel"
#define DRV_VERSION	"1.1"
#define DRV_RELDATE	"Mar 19, 2009"

#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <signal.h>

#include "common.h"
#include "plc_drv.h"
#ifndef __UTESTS__
#include "processing.h"
#include "hal.h"
#else
#include "processing_stub.h"

#endif

#ifdef __UTESTS__
int help_function = 0;
#endif

#define DEFAULT_NB_DATA_BUFF      13
#define DEFAULT_NB_INTERFACE_BUFF 2
#define DEFAULT_NB_MME_BUFF       1

//These identify the driver base version and may not be removed
static char version[] = DRV_NAME " PLC driver v" DRV_VERSION " (" DRV_RELDATE ")\n";

/** Global variable definition */
static struct init_info init;
static uint8_t frame_tx[ETH_MAX_SIZE+1];
static int tuntap_fd;
int thread_ending = 0;
pthread_mutex_t mutex_listener = PTHREAD_MUTEX_INITIALIZER;

/**
 * Help message given to the user when arguments are false.
 */
static void help(char *file)
{
#ifndef __UTESTS__
    printf("Usage: %s [OPTIONS]\nOPTIONS:\n",file);
    printf("--bin    [bin file] download the leon bin.\n");
    printf("--rbf    <rbf file> path to the fpga file.\n");
    printf("--tun    <TUN/TAP name> name of the tun/tap interface.\n");
    printf("--data   <number> number of data buffer to allocate for LEON.\n");
    printf("--mme    <number> number of mme data buffer to allocate for LEON.\n");
    printf("--grmon  Cesar binary doesn't run, it wait debug connexion.\n");
    printf("--extclk Change Gidel clock to external clock.\n");
#else
    help_function = 1;
#endif
}

/**
 * Fill all parameters in our structure.
 *
 * \param  argc  number of arguments.
 * \param  argv  pointer to each argument.
 * \return error code.
 */
#ifndef __UTESTS__
static
#endif
int parse_args(struct init_info *init, int argc, char **argv)
{
    int i;
    int result = 0;

    i = 1;
    while(i<argc)
    {
        if (!strcmp(argv[i],"--help"))
        {
            help(basename(argv[0]));
            result = -1;
            break;
        }
        else if (!strcmp(argv[i],"--bin"))
        {
            if(i+1<argc && strncmp(argv[i+1],"--",2))
            {
                i++;
                strncpy(init->bin, argv[i], sizeof(init->bin));
            }
            else
            {
                strncpy(init->bin, "OK", 3);
            }
        }
        else if (!strcmp(argv[i],"--rbf") && i+1<argc)
        {
            i++;
            strncpy(init->rbf, argv[i], sizeof(init->rbf));
        }
        else if (!strcmp(argv[i],"--tun") && i+1<argc)
        {
            i++;
            strncpy(init->tuntap_name, argv[i], sizeof(init->tuntap_name));
        }
        else if (!strcmp(argv[i],"--data") && i+1<argc)
        {
            i++;
            init->nb_data_buf_alloc = (int)strtoul(argv[i],NULL,0);
        }
        else if (!strcmp(argv[i],"--mme") && i+1<argc)
        {
            i++;
            init->nb_mme_buf_alloc = (int)strtoul(argv[i],NULL,0);
        }
        else if (!strcmp(argv[i],"--grmon"))
        {
            init->wait_debugger = 1;
        }
        else if (!strcmp(argv[i],"--extclk"))
        {
            init->use_ext_clock = 1;
        }
        else
        {
            help(basename(argv[0]));
            result = -1;
            break;
        }
        i++;
    }
    return result;
}

/**
 * Create TUN/TAP mechanism
 *
 * \param  init  intitialization structure.
 * \return error code.
 */
#ifndef __UTESTS__
static
#endif
int create_tuntap(struct init_info *init)
{
#ifndef __UTESTS__
    int fd;
    struct ifreq ifr;
    char default_name[] = "tap0";
    int error;

    //Open TUN device
    if((fd = open("/dev/net/tun", O_RDWR)) < 0)
    {
        printf("open /dev/net/tun failed");
        return -1;
    }

    //Prepare TUN/TAP name
    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
    if(strncmp(init->tuntap_name, default_name, sizeof(default_name)) < 0)
        strncpy(init->tuntap_name, default_name, IFNAMSIZ);

    strncpy(ifr.ifr_name, init->tuntap_name, IFNAMSIZ);

    //Create a TUN/TAP interface
    if((error = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0)
    {
        close(fd);
        if (error == EPERM)
            printf("TUN/TAP creation error.\nYou need to be root to create an network interface.\n");
        else
            printf("TUN/TAP creation error(%d).\n",error);
        return -1;
    }

    tuntap_fd = fd;

    return 0;

#else
    struct ifreq ifr;
    char default_name[] = "tap0";

    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TAP;
    if(strncmp(init->tuntap_name, default_name, sizeof(default_name)) < 0)
        return -1;
    else
        return 0;

#endif
}

/**
 * Close TUN/TAP mechanism
 *
 * \param  fd  TUN/TAP file descriptor.
 */
static inline void close_tuntap(int fd)
{
    close(fd);
}

/**
 * Uninit procedure, called when the user type Ctrl+C.
 *
 * \param  sig  signal catched
 */
static void catch_sigint(int sig)
{
    printf("Stopping PLC Driver...\n");

    //Stop Mutex and thread
    thread_ending = 1;
    pthread_mutex_unlock(&mutex_listener);
    pthread_mutex_destroy(&mutex_listener);

    //Uninit lower layers
    processing_uninit();

    //CLose TUN/TAP interface
    close_tuntap(tuntap_fd);

    exit(0);
}

/** 
 * Communication with User to read/write in debug memories
 */
static void* thread_user(void* data)
{
    char reading[21], cmd[2];
    uint32_t offset, value, res, i;

    TRACE("User Communication starting...\n");
    printf("Only 4 commands allowed  with this format : cmd %%x %%x\n");
    printf("  rd <offset> <length> --> to read under ctrl_dsp_ss\n");
    printf("  rw <offset> <value>  --> to write under ctrl_dsp_ss\n");
    printf("  md <offset> <length> --> to read under Afredi Memory\n");
    printf("  mw <offset> <value>  --> to write under Afredi Memory\n");

    while(!thread_ending)
    {
        fflush(stdin);
        fgets(reading, sizeof(reading), stdin);
        sscanf(reading,"%c%c %x %x", &cmd[0], &cmd[1], &offset, &value);

        if(!strncmp(cmd, "rd", 2))
        {
            if(value > 32)
                value = 32;
            for(i=0;i<value;i++)
            {
                res = hal_ctrl_dsp_ss_read(offset+(i*4));
                printf("%08x %08x\n",offset+(i*4), res);
            }
        }
        else if(!strncmp(cmd, "rw", 2))
        {
            res = hal_ctrl_dsp_ss_write(offset, &value);
            if(res)
                printf("Error during writing, %08x = %08x\n", offset, value);
        }
        else if(!strncmp(cmd, "md", 2))
        {
            if(value > 131072)
                value = 131072;
            for(i=0;i<value;i++)
            {
                res = hal_afredi_memory_read(offset+(i*4));
                printf("%08x %08x\n",offset+(i*4), res);
            }
        }
        else if(!strncmp(cmd, "mw", 2))
        {
            res = hal_afredi_memory_write(offset, &value);
            if(res)
                printf("Error during writing, %08x = %08x\n", offset, value);
        }
        else
        {
            printf("Unknown command\n");
        }
        printf("\n");
    }
    TRACE("User Thread stop\n");
    pthread_exit(NULL);
}

/** 
 * Send a packet, after received it from TUN/TAP.
 */
static void* thread_listener(void* data)
{
    int recv_len;
    int status;
#ifdef DEBUG
    uint32_t *check_frame;
    int i;
#endif

    TRACE("Thread starting...\n");
    while(!thread_ending)
    {
        //Recover the frame from the TUN/TAP interface
        TRACE("Before read\n");
        recv_len = read(tuntap_fd, (void*)frame_tx, ETH_MAX_SIZE);
#ifdef DEBUG
        check_frame = (uint32_t*)frame_tx;
        TRACE("\n\nSEND a Frame (%d bytes) : \n",recv_len);
        for(i=0;i<((recv_len/4)+1);i++)
        {
            if((i%4 == 0) && i)
                printf("\n");
            printf("%08X ",*(check_frame+i));
        }
        printf("\n");
#endif        
        TRACE("After read\n");

        TRACE("Lock Normal\n");
        pthread_mutex_lock(&mutex_listener);

        //Send the frame to lower layers
        status = processing_send((void *)frame_tx, recv_len);
        if(status == NEARLY_FULL)
        {
            //There is nearly room in the mailbox ring
            //so stop the tx part (packets from TUN/TAP)
            TRACE("Lock NEARLY_FULL\n");
            pthread_mutex_lock(&mutex_listener);
        }
        else if(status == FULL)
        {
            //There is no room in the mailbox ring
            //so stop the tx part (packets from TUN/TAP)
            //and drop the frame
            TRACE("Lock FULL\n");
            pthread_mutex_lock(&mutex_listener);
        }

        TRACE("UnLock Normal\n");
        pthread_mutex_unlock(&mutex_listener);
    }
    TRACE("Thread stop\n");
    pthread_exit(NULL);
}

/** 
 * Receive a packet.
 *
 * \param  pointer  the packet pointer.
 * \param  length  the length of the packet.
 * \return  error code.
 */
int plcdrv_rx(void *pointer, int length)
{
#ifdef DEBUG
    uint32_t *check_frame;
    int i;
    check_frame = (uint32_t*)pointer;
    printf("RECEIVE a Frame (%d bytes) : \n",length);
    for(i=0;i<((length/4)+1);i++)
    {
        if((i%4 == 0) && i)
            printf("\n");
        printf("%08X ",*(check_frame+i));
    }
    printf("\n");
#endif

    //Send the frame received from lower layers to TUN/TAP interface
    if(write(tuntap_fd, pointer, length) != length)
        return -1;
    else
        return 0;
}

/** 
 * Precedure executed when the frame is really sending to CESAR
 * and only when there are few buffer in the mailbox ring.
 */
void plcdrv_tx_ack(void)
{
    //there is room in the mailbox ring now
    //so re-enable emission part
    //(frames coming from the TUN/TAP interface)
    TRACE("UnLock FULL\n");
    pthread_mutex_unlock(&mutex_listener);
}

/**
 * Main Procedure.
 *
 * \param  argc  number of arguments.
 * \param  argv  pointer to each argument.
 * \return error code.
 */
#if defined __UTESTS__ || defined __HTESTS__
int plc_drv_main(int argc, const char **argv)
#else
int main(int argc, const char **argv)
#endif
{
    pthread_t listener;
    pthread_t user;
    pthread_attr_t attribut;

    printf("%s",version);

    //Set default value in the init structure
    memset(&init, 0, sizeof(init));
    init.nb_data_buf_alloc = DEFAULT_NB_DATA_BUFF;
    init.nb_interface_buf_alloc = DEFAULT_NB_INTERFACE_BUFF;
    init.nb_mme_buf_alloc = DEFAULT_NB_MME_BUFF;

    //Recover user parameters and fill the init structure
    if(parse_args(&init, argc, (char **)argv))
        return -1;

    //Finish construction of init structure
    init.txack_handler = &plcdrv_tx_ack;

    //Remap the Ctrl+C to close the plcdrv
    signal(SIGINT, catch_sigint);

    //Create a TUN/TAP interface
    if(create_tuntap(&init))
        return -1;

    //Open device tap
/*    if((tuntap_fd = open("/dev/tap0",O_RDWR)) <= 0)
    {
        printf("Error open /dev/tap0 (%d)\n",tuntap_fd);
        return -1;
    }
*/
    //Initialize the lower layers
    processing_init(&init);

    //Create a pthread for eth frames receive from TUN/TAP
    pthread_mutex_init(&mutex_listener, NULL);
    if(pthread_attr_init(&attribut))
    {
        perror("ERROR pthread_attr_init");
        return -1;
    }
    if(pthread_attr_setschedpolicy(&attribut, SCHED_RR))
    {
        perror("ERROR pthread_setschedpolicy");
        return -1;
    }
    if(pthread_create(&listener, &attribut, &thread_listener, NULL))
    {
        perror("ERROR pthread_create Listener");
        return -1;
    }
    if(pthread_create(&user, &attribut, &thread_user, NULL))
    {
        perror("ERROR pthread_create User");
        return -1;
    }
    pthread_attr_destroy(&attribut);

    //Wait thread ending and CTRL+C
    pthread_join(listener, 0);
    pthread_join(user, 0);
    while(1);

    return 0;
}