/**
 * Main function of tool.
 */
int main(int argc, char* argv[])
{
    DltClient      dltclient;
    DltReceiveData dltdata;
    int c;
    int index;

    /* Initialize dltdata */
    dltdata.vflag = 0;
    dltdata.yflag = 0;
    dltdata.evalue = 0;
    dltdata.bvalue = 0;

    dltdata.avalue = 0;
    dltdata.cvalue = 0;
    dltdata.svalue = 0;
    dltdata.mvalue = 0;
    dltdata.xvalue = 0;
    dltdata.tvalue = 1000;
    dltdata.lvalue = -1;
    dltdata.rvalue = -1;
    dltdata.dvalue = -1;
    dltdata.fvalue = -1;
    dltdata.ivalue = -1;
    dltdata.oflag = -1;
    dltdata.gflag = -1;

    /* Fetch command line arguments */
    opterr = 0;

    while ((c = getopt (argc, argv, "vhye:b:a:c:s:m:x:t:l:r:d:f:i:og")) != -1)
        switch (c)
        {
        case 'v':
			{
            	dltdata.vflag = 1;
            	break;
			}
        case 'h':
			{
            	usage();
            	return -1;
			}
        case 'y':
			{
            	dltdata.yflag = 1;
            	break;
			}
        case 'e':
			{
            	dltdata.evalue = optarg;
            	break;
			}
        case 'b':
			{
            	dltdata.bvalue = atoi(optarg);
            	break;
			}

        case 'a':
			{
            	dltdata.avalue = optarg;
            	break;
			}
        case 'c':
			{
            	dltdata.cvalue = optarg;
            	break;
			}
        case 's':
			{
            	dltdata.svalue = atoi(optarg);
            	break;
			}
        case 'm':
			{
            	dltdata.mvalue = optarg;
            	break;
			}
        case 'x':
			{
            	dltdata.xvalue = optarg;
            	break;
			}
        case 't':
			{
            	dltdata.tvalue = atoi(optarg);;
            	break;
			}
        case 'l':
			{
            	dltdata.lvalue = atoi(optarg);;
            	break;
			}
        case 'r':
			{
            	dltdata.rvalue = atoi(optarg);;
            	break;
			}
        case 'd':
			{
            	dltdata.dvalue = atoi(optarg);;
            	break;
			}
        case 'f':
			{
            	dltdata.fvalue = atoi(optarg);;
            	break;
			}
        case 'i':
			{
            	dltdata.ivalue = atoi(optarg);;
            	break;
			}
        case 'o':
			{
            	dltdata.oflag = 1;
            	break;
			}
        case 'g':
			{
            	dltdata.gflag = 1;
            	break;
			}

        case '?':
			{
		        if (optopt == 'o' || optopt == 'f')
				{
		            fprintf (stderr, "Option -%c requires an argument.\n", optopt);
		        }
				else if (isprint (optopt))
				{
		            fprintf (stderr, "Unknown option `-%c'.\n", optopt);
		        }
				else
				{
		            fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
				}
		        /* unknown or wrong option used, show usage information and terminate */
		        usage();
		        return -1;
			}
        default:
			{
            	abort ();
                return -1;//for parasoft
			}
        }

    /* Initialize DLT Client */
    dlt_client_init(&dltclient, dltdata.vflag);

    /* Register callback to be called when message was received */
    dlt_client_register_message_callback(dlt_receive_message_callback);

    /* Setup DLT Client structure */
    dltclient.serial_mode = dltdata.yflag;

    if (dltclient.serial_mode==0)
    {
        for (index = optind; index < argc; index++)
        {
            dltclient.servIP = argv[index];
        }

        if (dltclient.servIP == 0)
        {
            /* no hostname selected, show usage and terminate */
            fprintf(stderr,"ERROR: No hostname selected\n");
            usage();
            dlt_client_cleanup(&dltclient,dltdata.vflag);
            return -1;
        }
    }
    else
    {
        for (index = optind; index < argc; index++)
        {
            dltclient.serialDevice = argv[index];
        }

        if (dltclient.serialDevice == 0)
        {
            /* no serial device name selected, show usage and terminate */
            fprintf(stderr,"ERROR: No serial device name specified\n");
            usage();
            return -1;
        }

		dlt_client_setbaudrate(&dltclient,dltdata.bvalue);
    }

    /* initialise structure to use DLT file */
    dlt_file_init(&(dltdata.file),dltdata.vflag);

    /* first parse filter file if filter parameter is used */
    dlt_filter_init(&(dltdata.filter),dltdata.vflag);

    if (dltdata.evalue)
	{
        dlt_set_id(dltdata.ecuid,dltdata.evalue);
    }
	else
	{
        dlt_set_id(dltdata.ecuid,DLT_RECEIVE_ECU_ID);
	}

    /* Connect to TCP socket or open serial device */
    if (dlt_client_connect(&dltclient, dltdata.vflag)!=-1)
    {
    	/* send injection message */
    	if(dltdata.mvalue && dltdata.avalue && dltdata.cvalue)
    	{
    		/* ASCII */
    		printf("Send injection message:\n");
    		printf("AppId: %s\n",dltdata.avalue);
    		printf("ConId: %s\n",dltdata.cvalue);
    		printf("ServiceId: %d\n",dltdata.svalue);
    		printf("Message: %s\n",dltdata.mvalue);
    		/* send control message in ascii */
    		dlt_client_send_inject_msg(&dltclient,dltdata.avalue,dltdata.cvalue,dltdata.svalue,(uint8_t*)dltdata.mvalue,strlen(dltdata.mvalue));
    	}
    	else if(dltdata.xvalue && dltdata.avalue && dltdata.cvalue)
    	{
    		/* Hex */
    		uint8_t buffer[1024];
    		int size = 1024;
    		printf("Send injection message:\n");
    		printf("AppId: %s\n",dltdata.avalue);
    		printf("ConId: %s\n",dltdata.cvalue);
    		printf("ServiceId: %d\n",dltdata.svalue);
    		printf("Message: %s\n",dltdata.xvalue);
    		hexAsciiToBinary(dltdata.xvalue,buffer,&size);
    		printf("Size: %d\n",size);
    		/* send control message in hex */
    		dlt_client_send_inject_msg(&dltclient,dltdata.avalue,dltdata.cvalue,dltdata.svalue,buffer,size);
    	}
    	else if(dltdata.lvalue!=-1 && dltdata.avalue && dltdata.cvalue)
    	{
    		/* log level */
    		printf("Set log level:\n");
    		printf("AppId: %s\n",dltdata.avalue);
    		printf("ConId: %s\n",dltdata.cvalue);
    		printf("Loglevel: %d\n",dltdata.lvalue);
    		/* send control message*/
    		dlt_client_send_log_level(&dltclient,dltdata.avalue,dltdata.cvalue,dltdata.lvalue);
    	}
    	else if(dltdata.rvalue!=-1 && dltdata.avalue && dltdata.cvalue)
    	{
    		/* trace status */
    		printf("Set trace status:\n");
    		printf("AppId: %s\n",dltdata.avalue);
    		printf("ConId: %s\n",dltdata.cvalue);
    		printf("TraceStatus: %d\n",dltdata.rvalue);
    		/* send control message in*/
    		dlt_client_send_trace_status(&dltclient,dltdata.avalue,dltdata.cvalue,dltdata.rvalue);
    	}
    	else if(dltdata.dvalue!=-1)
    	{
    		/* default log level */
    		printf("Set default log level:\n");
    		printf("Loglevel: %d\n",dltdata.dvalue);
    		/* send control message in*/
    		dlt_client_send_default_log_level(&dltclient,dltdata.dvalue);
    	}
    	else if(dltdata.rvalue!=-1)
    	{
    		/* default trace status */
    		printf("Set default trace status:\n");
    		printf("TraceStatus: %d\n",dltdata.rvalue);
    		/* send control message in*/
    		dlt_client_send_default_trace_status(&dltclient,dltdata.rvalue);
    	}
    	else if(dltdata.ivalue!=-1)
    	{
    		/* timing pakets */
    		printf("Set timing pakets:\n");
    		printf("Timing packets: %d\n",dltdata.ivalue);
    		/* send control message in*/
    		dlt_client_send_timing_pakets(&dltclient,dltdata.ivalue);
    	}
    	else if(dltdata.oflag!=-1)
    	{
    		/* default trace status */
    		printf("Store config\n");
    		/* send control message in*/
    		dlt_client_send_store_config(&dltclient);
    	}
    	else if(dltdata.gflag!=-1)
    	{
    		/* reset to factory default */
    		printf("Reset to factory default\n");
    		/* send control message in*/
    		dlt_client_send_reset_to_factory_default(&dltclient);
    	}

        /* Dlt Client Main Loop */
        //dlt_client_main_loop(&dltclient, &dltdata, dltdata.vflag);

    	/* Wait timeout */
    	usleep(dltdata.tvalue*1000);

        /* Dlt Client Cleanup */
        dlt_client_cleanup(&dltclient,dltdata.vflag);
    }

    dlt_file_free(&(dltdata.file),dltdata.vflag);

    dlt_filter_free(&(dltdata.filter),dltdata.vflag);

    return 0;
}
Esempio n. 2
0
/**
 * Main function of tool.
 */
int main(int argc, char* argv[])
{
    DltClient       dltclient;
    DltTestclientData dltdata;
    int c,i;
    int index;

    /* Initialize dltdata */
    dltdata.aflag = 0;
    dltdata.sflag = 0;
    dltdata.xflag = 0;
    dltdata.mflag = 0;
    dltdata.vflag = 0;
    dltdata.yflag = 0;
    dltdata.ovalue = 0;
    dltdata.fvalue = 0;
    dltdata.evalue = 0;
    dltdata.bvalue = 0;
    dltdata.nvalue = 10000;
    dltdata.ohandle= -1;

    dltdata.running_test = 0;

    for (i=0; i<DLT_TESTCLIENT_NUM_TESTS; i++)
    {
        dltdata.test_counter_macro[i]=0;
        dltdata.test_counter_function[i]=0;
    }

    dltdata.tests_passed = 0;
    dltdata.tests_failed = 0;

    dltdata.bytes_received = 0;
    dltdata.time_elapsed = dlt_uptime();

    dltdata.last_value = 0;
    dltdata.count_received_messages = 0;
    dltdata.count_not_received_messages = 0;


    dltdata.sock = -1;

    /* Fetch command line arguments */
    opterr = 0;

    while ((c = getopt (argc, argv, "vashyxmf:o:e:b:n:")) != -1)
    {
        switch (c)
        {
        case 'v':
        {
            dltdata.vflag = 1;
            break;
        }
        case 'a':
        {
            dltdata.aflag = 1;
            break;
        }
        case 's':
        {
            dltdata.sflag = 1;
            break;
        }
        case 'x':
        {
            dltdata.xflag = 1;
            break;
        }
        case 'm':
        {
            dltdata.mflag = 1;
            break;
        }
        case 'h':
        {
            usage();
            return -1;
        }
        case 'y':
        {
            dltdata.yflag = 1;
            break;
        }
        case 'f':
        {
            dltdata.fvalue = optarg;
            break;
        }
        case 'o':
        {
            dltdata.ovalue = optarg;
            break;
        }
        case 'e':
        {
            dltdata.evalue = optarg;
            break;
        }
        case 'b':
        {
            dltdata.bvalue = atoi(optarg);
            break;
        }
        case 'n':
        {
            dltdata.nvalue = atoi(optarg);
            break;
        }
        case '?':
        {
            if (optopt == 'o' || optopt == 'f' || optopt == 't')
            {
                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
            }
            else if (isprint (optopt))
            {
                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
            }
            else
            {
                fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
            }
            /* unknown or wrong option used, show usage information and terminate */
            usage();
            return -1;
        }
        default:
        {
            abort ();
        }
        }
    }

    /* Initialize DLT Client */
    dlt_client_init(&dltclient, dltdata.vflag);

    /* Register callback to be called when message was received */
    dlt_client_register_message_callback(dlt_testclient_message_callback);

    /* Setup DLT Client structure */
    dltclient.serial_mode = dltdata.yflag;

    if (dltclient.serial_mode==0)
    {
        for (index = optind; index < argc; index++)
        {
            dltclient.servIP = argv[index];
        }

        if (dltclient.servIP == 0)
        {
            /* no hostname selected, show usage and terminate */
            fprintf(stderr,"ERROR: No hostname selected\n");
            usage();
            dlt_client_cleanup(&dltclient,dltdata.vflag);
            return -1;
        }
    }
    else
    {
        for (index = optind; index < argc; index++)
        {
            dltclient.serialDevice = argv[index];
        }

        if (dltclient.serialDevice == 0)
        {
            /* no serial device name selected, show usage and terminate */
            fprintf(stderr,"ERROR: No serial device name specified\n");
            usage();
            return -1;
        }

        dlt_client_setbaudrate(&dltclient,dltdata.bvalue);
    }

    /* initialise structure to use DLT file */
    dlt_file_init(&(dltdata.file),dltdata.vflag);

    /* first parse filter file if filter parameter is used */
    dlt_filter_init(&(dltdata.filter),dltdata.vflag);

    if (dltdata.fvalue)
    {
        if (dlt_filter_load(&(dltdata.filter),dltdata.fvalue,dltdata.vflag)<0)
        {
            dlt_file_free(&(dltdata.file),dltdata.vflag);
            return -1;
        }

        dlt_file_set_filter(&(dltdata.file),&(dltdata.filter),dltdata.vflag);
    }

    /* open DLT output file */
    if (dltdata.ovalue)
    {
        dltdata.ohandle = open(dltdata.ovalue,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */

        if (dltdata.ohandle == -1)
        {
            dlt_file_free(&(dltdata.file),dltdata.vflag);
            fprintf(stderr,"ERROR: Output file %s cannot be opened!\n",dltdata.ovalue);
            return -1;
        }
    }

    if (dltdata.evalue)
    {
        dlt_set_id(dltdata.ecuid,dltdata.evalue);
    }
    else
    {
        dlt_set_id(dltdata.ecuid,DLT_TESTCLIENT_ECU_ID);
    }

    /* Connect to TCP socket or open serial device */
    if (dlt_client_connect(&dltclient, dltdata.vflag)!=-1)
    {
        dltdata.sock = dltclient.sock;

        /* Dlt Client Main Loop */
        dlt_client_main_loop(&dltclient, &dltdata, dltdata.vflag);

        /* Dlt Client Cleanup */
        dlt_client_cleanup(&dltclient,dltdata.vflag);
    }

    /* dlt-receive cleanup */
    if (dltdata.ovalue)
    {
        close(dltdata.ohandle);
    }

    dlt_file_free(&(dltdata.file),dltdata.vflag);

    dlt_filter_free(&(dltdata.filter),dltdata.vflag);

    return 0;
}
/**
 * Main function of tool.
 */
int main(int argc, char* argv[])
{
    int vflag = 0;
    int cflag = 0;
    int aflag = 0;
    int sflag = 0;
    int xflag = 0;
    int mflag = 0;
    int wflag = 0;
    char *fvalue = 0;
    char *bvalue = 0;
    char *evalue = 0;
    char *ovalue = 0;

    int index;
    int c;

	DltFile file;
	DltFilter filter;

	int ohandle=-1;

	int num, begin, end;

	char text[DLT_CONVERT_TEXTBUFSIZE];

	struct iovec iov[2];
    int bytes_written;

    opterr = 0;

    while ((c = getopt (argc, argv, "vcashxmwf:b:e:o:")) != -1)
        switch (c)
        {
        case 'v':
			{
            	vflag = 1;
            	break;
			}
        case 'c':
			{
            	cflag = 1;
            	break;
			}
        case 'a':
			{
            	aflag = 1;
            	break;
			}
        case 's':
			{
            	sflag = 1;
            	break;
			}
        case 'x':
			{
            	xflag = 1;
            	break;
			}
        case 'm':
			{
            	mflag = 1;
            	break;
			}
        case 'w':
			{
            	wflag = 1;
            	break;
			}
        case 'h':
			{
            	usage();
            	return -1;
			}
        case 'f':
			{
            	fvalue = optarg;
            	break;
			}
        case 'b':
			{
            	bvalue = optarg;
            	break;
			}
        case 'e':
			{
            	evalue = optarg;
            	break;
			}
        case 'o':
			{
            	ovalue = optarg;
            	break;
			}
        case '?':
			{
		        if (optopt == 'f' || optopt == 'b' || optopt == 'e' || optopt == 'o')
				{
		            fprintf (stderr, "Option -%c requires an argument.\n", optopt);
				}
				else if (isprint (optopt))
				{
		            fprintf (stderr, "Unknown option `-%c'.\n", optopt);
				}
				else
				{
		            fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
				}
		        /* unknown or wrong option used, show usage information and terminate */
		        usage();
		        return -1;
			}
        default:
			{
            	abort();
                return -1;//for parasoft
			}
        }

    /* initialise structure to use DLT file */
    dlt_file_init(&file,vflag);

    /* first parse filter file if filter parameter is used */
    if (fvalue)
    {
        if (dlt_filter_load(&filter,fvalue,vflag)<0)
        {
            dlt_file_free(&file,vflag);
            return -1;
        }

        dlt_file_set_filter(&file,&filter,vflag);
    }

    if (ovalue)
    {
        ohandle = open(ovalue,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
        if (ohandle == -1)
        {
            dlt_file_free(&file,vflag);
            fprintf(stderr,"ERROR: Output file %s cannot be opened!\n",ovalue);
            return -1;
        }

    }

    for (index = optind; index < argc; index++)
    {
        /* load, analyse data file and create index list */
        if (dlt_file_open(&file,argv[index],vflag)>=0)
        {
            while (dlt_file_read(&file,vflag)>=0)
            {
            }
        }

        if (aflag || sflag || xflag || mflag || ovalue)
        {
            if (bvalue)
			{
                begin = atoi(bvalue);
            }
			else
			{
                begin = 0;
			}

            if (evalue && (wflag==0))
			{
                end = atoi(evalue);
            }
			else
			{
                end = file.counter-1;
			}

            if (begin<0 || begin>=file.counter)
            {
                fprintf(stderr,"ERROR: Selected first message %d is out of range!\n",begin);
                return -1;
            }
            if (end<0 || end>=file.counter || end<begin)
            {
                fprintf(stderr,"ERROR: Selected end message %d is out of range!\n",end);
                return -1;
            }
            for (num = begin; num <= end ;num++)
            {
                dlt_file_message(&file,num,vflag);

                if (xflag)
                {
                    printf("%d ",num);
                    dlt_message_print_hex(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
                }
                else if (aflag)
                {
                    printf("%d ",num);

                    dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);

                    printf("%s ",text);

                    dlt_message_payload(&file.msg,text,DLT_CONVERT_TEXTBUFSIZE,DLT_OUTPUT_ASCII,vflag);

                    printf("[%s]\n",text);
                }
                else if (mflag)
                {
                    printf("%d ",num);
                    dlt_message_print_mixed_plain(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
                }
                else if (sflag)
                {
                    printf("%d ",num);

                    dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);

                    printf("%s \n",text);
                }

                /* if file output enabled write message */
                if (ovalue)
                {
                    iov[0].iov_base = file.msg.headerbuffer;
                    iov[0].iov_len = file.msg.headersize;
                    iov[1].iov_base = file.msg.databuffer;
                    iov[1].iov_len = file.msg.datasize;

                    bytes_written = writev(ohandle, iov, 2);
                    if (0 > bytes_written){
                            printf("in main: writev(ohandle, iov, 2); returned an error!" );
                            dlt_file_free(&file,vflag);
                            return -1;
                    }
                }

                /* check for new messages if follow flag set */
                if (wflag && num==end)
                {
                    while (1)
                    {
                        while (dlt_file_read(&file,0)>=0)
                        {
                        }
                        if (end == (file.counter-1))
						{
                            /* Sleep if no new message was received */
                            sleep(1);
                        }
						else
                        {
                            /* set new end of log file and continue reading */
                            end = file.counter-1;
                            break;
                        }
                    }
                }
            }
        }
        if (cflag)
        {
            printf("Total number of messages: %d\n",file.counter_total);
            if (file.filter)
			{
                printf("Filtered number of messages: %d\n",file.counter);
			}
        }
    }
    if (ovalue)
    {
        close(ohandle);
    }
    if (index == optind)
    {
        /* no file selected, show usage and terminate */
        fprintf(stderr,"ERROR: No file selected\n");
        usage();
        return -1;
    }

    dlt_file_free(&file,vflag);

    return 0;
}