Exemplo n.º 1
0
int dlt_testclient_message_callback(DltMessage *message, void *data)
{
    static char text[DLT_TESTCLIENT_TEXTBUFSIZE];
    DltTestclientData *dltdata;

    uint32_t type_info, type_info_tmp;
    int16_t length,length_tmp; /* the macro can set this variable to -1 */
    uint8_t *ptr;
    int32_t datalength;
    int32_t value;
    uint32_t value_tmp = 0;

    struct iovec iov[2];
    int bytes_written;

    if ((message==0) || (data==0))
    {
        return -1;
    }

    dltdata = (DltTestclientData*)data;

    /* prepare storage header */
    if (DLT_IS_HTYP_WEID(message->standardheader->htyp))
    {
        dlt_set_storageheader(message->storageheader,message->headerextra.ecu);
    }
    else
    {
        dlt_set_storageheader(message->storageheader,dltdata->ecuid);
    }

    if ((dltdata->fvalue==0) || (dltdata->fvalue && dlt_message_filter_check(message,&(dltdata->filter),dltdata->vflag)==1))
    {

        //dlt_message_header(message,text,sizeof(text),dltdata->vflag);
        if (dltdata->aflag)
        {
            //printf("%s ",text);
        }
        //dlt_message_payload(message,text,sizeof(text),DLT_OUTPUT_ASCII,dltdata->vflag);
        if (dltdata->aflag)
        {
            //printf("[%s]\n",text);
        }

        /* do something here */

        // Count number of received bytes
        dltdata->bytes_received += message->datasize+message->headersize-sizeof(DltStorageHeader);

        // print number of received bytes
        if((dlt_uptime() - dltdata->time_elapsed) > 10000)
        {
            printf("Received %lu Bytes/s\n",dltdata->bytes_received/**10000/(dlt_uptime()-dltdata->time_elapsed)*/);
            //printf("Received %lu Bytes received\n",dltdata->bytes_received);
            dltdata->time_elapsed = dlt_uptime();
            dltdata->bytes_received = 0;
        }

        /* Extended header */
        if (DLT_IS_HTYP_UEH(message->standardheader->htyp))
        {
            /* Log message */
            if ((DLT_GET_MSIN_MSTP(message->extendedheader->msin))==DLT_TYPE_LOG)
            {
                /* Verbose */
                if (DLT_IS_MSIN_VERB(message->extendedheader->msin))
                {
                    /* 2 arguments */
                    if (message->extendedheader->noar==2)
                    {
                        /* verbose mode */
                        type_info=0;
                        type_info_tmp=0;
                        length=0;
                        length_tmp=0; /* the macro can set this variable to -1 */

                        ptr = message->databuffer;
                        datalength = message->datasize;

                        /* first read the type info of the first argument: must be string */
                        DLT_MSG_READ_VALUE(type_info_tmp,ptr,datalength,uint32_t);
                        type_info=DLT_ENDIAN_GET_32(message->standardheader->htyp, type_info_tmp);

                        if (type_info & DLT_TYPE_INFO_SINT)
                        {
                            /* read value */
                            DLT_MSG_READ_VALUE(value_tmp,ptr,datalength,int32_t);
                            value=DLT_ENDIAN_GET_32(message->standardheader->htyp, value_tmp);
                            //printf("%d\n",value);

                            if(value < dltdata->last_value)
                            {
                                if(dltdata->nvalue == dltdata->count_received_messages)
                                    printf("PASSED: %d Msg received, %d not received\n",dltdata->count_received_messages,dltdata->count_not_received_messages);
                                else
                                    printf("FAILED: %d Msg received, %d not received\n",dltdata->count_received_messages,dltdata->count_not_received_messages);

                                dltdata->last_value = 0;
                                dltdata->count_received_messages = 0;
                                dltdata->count_not_received_messages = value -1;
                            }
                            else
                            {
                                dltdata->count_not_received_messages += value - dltdata->last_value -1;
                            }
                            dltdata->last_value = value;
                            dltdata->count_received_messages++;

                            if (length>=0)
                            {
                                ptr+=length;
                                datalength-=length;

                                /* read type of second argument: must be raw */
                                DLT_MSG_READ_VALUE(type_info_tmp,ptr,datalength,uint32_t);
                                type_info=DLT_ENDIAN_GET_32(message->standardheader->htyp, type_info_tmp);

                                if (type_info & DLT_TYPE_INFO_RAWD)
                                {
                                    /* get length of raw data block */
                                    DLT_MSG_READ_VALUE(length_tmp,ptr,datalength,uint16_t);
                                    length=DLT_ENDIAN_GET_16(message->standardheader->htyp, length_tmp);

                                    if ((length>=0) && (length==datalength))
                                    {
                                        //printf("Raw data found in payload, length=");
                                        //printf("%d, datalength=%d \n", length, datalength);
                                        dltdata->test_counter_macro[3]++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        /* if no filter set or filter is matching display message */
        if (dltdata->xflag)
        {
            dlt_message_print_hex(message,text,DLT_TESTCLIENT_TEXTBUFSIZE,dltdata->vflag);
        }
        else if (dltdata->mflag)
        {
            dlt_message_print_mixed_plain(message,text,DLT_TESTCLIENT_TEXTBUFSIZE,dltdata->vflag);
        }
        else if (dltdata->sflag)
        {
            dlt_message_print_header(message,text,sizeof(text),dltdata->vflag);
        }

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

            bytes_written = writev(dltdata->ohandle, iov, 2);
        }
    }

    return 0;
}
Exemplo n.º 2
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;
}