コード例 #1
0
int main(int argc, char *argv[])
{
	int opt;
	char* appName = NULL;
	char* resourceID = NULL;
	char* payloadBuffer = NULL;
	char* fileName = NULL;
	unsigned char* writeBuffer = NULL;
	eOperationMode opMode = modeInvalid;
	unsigned int user_no = 0, seat_no = 0;
	unsigned int ldbid = 0xFF;    // default value
	unsigned int doHexdump = 0;

	printf("\n");
   /// debug log and trace (DLT) setup
   DLT_REGISTER_APP("Ptool","persistence client library tools");


	while ((opt = getopt(argc, argv, "hVo:a:u:s:r:-l:p:f:H")) != -1)
	{
		switch (opt)
		{
		   case 'o':      // option
		      if(strcmp(optarg, "readkey")  == 0)
            {
		         opMode = modeReadKey;
            }
		      else if(strcmp(optarg, "writekey")  == 0)
            {
		         opMode = modeWriteKey;
            }
		      else if(strcmp(optarg, "deletekey")  == 0)
            {
		         opMode = modeDeleteKey;
            }
		      else if(strcmp(optarg, "getkeysize")  == 0)
            {
		         opMode = modeGetKeySize;
            }
		      else
		      {
		         printf("Unsupported Unsupported mode: %s\"\n\"", optarg);
		         printSynopsis();
	            exit(EXIT_FAILURE);
		      }
		      break;
		   case 'a':   // application name
		   {
		      size_t len = strlen(optarg);
            appName = malloc(len + 1);
            if(appName != NULL)
            {
               memset(appName, 0, len + 1);
               strncpy(appName, optarg, len);
            }
		   }
		      break;
		   case 'r':   // resource ID
         {
            size_t len = strlen(optarg);
            resourceID = malloc(len + 1);
            if(resourceID != NULL)
            {
               memset(resourceID, 0, len + 1);
               strncpy(resourceID, optarg, len);
            }
         }
            break;
         case 'p':   // payload to write
         {
            size_t len = strlen(optarg);
            payloadBuffer = malloc(len + 1);
            if(payloadBuffer != NULL)
            {
               memset(payloadBuffer, 0, len + 1);
               strncpy(payloadBuffer, optarg, len);
            }
         }
            break;
         case 'f':   // filename to read data from, write data to
         {
            size_t len = strlen(optarg);
            fileName = malloc(len + 1);
            if(fileName != NULL)
            {
               memset(fileName, 0, len + 1);
               strncpy(fileName, optarg, len);
            }
         }
            break;
		   case 'u':   // user number
		      user_no = (unsigned int)atoi(optarg);
		      break;
		   case 's':   // seat number
            seat_no = (unsigned int)atoi(optarg);
            break;
		   case 'l':
		      ldbid = (unsigned int)strtol(optarg, NULL, 16);
		      break;
		   case 'H':   // hexdump of data
		      doHexdump = 1;
		      break;
	   	case 'h':   // help
	   	   printSynopsis();
	         break;
	   	case 'v':   // version
	   		printf("Version: %s\n", PCLT_VERSION);
	         break;
	   	default: /* '?' */
	      	printSynopsis();
	         exit(EXIT_FAILURE);
	         break;
		}
   }


	if(appName != NULL && resourceID != NULL)
	{
	   printf("Application name: %s\n", appName);

	   int shutdownReg = PCL_SHUTDOWN_TYPE_NONE;
	   (void)pclInitLibrary(appName, shutdownReg);

      switch(opMode)
      {
         case modeReadKey:
         {
            unsigned char* buffer = NULL;
            int keysize = pclKeyGetSize(ldbid, resourceID, user_no, seat_no);

            if(keysize > 0)
            {
               buffer = malloc((size_t)keysize + 1);
               if(buffer != NULL)
               {
                  memset(buffer, 0, (size_t)(keysize + 1));
                  readKey(resourceID, user_no, seat_no, ldbid, doHexdump, buffer, keysize);

                  if(fileName != NULL)
                     (void)writeDataToFile(fileName, buffer, keysize);

                  free(buffer);
               }
            }
            else
            {
               printf("readkey: key is empty: %d\n", keysize);
            }
            break;
         }
         case modeWriteKey:
            if(fileName != NULL)    // if filename is available, read data from file
            {
               writeBuffer = readDataFromFile(fileName);
            }
            else
            {
               writeBuffer = (unsigned char*)payloadBuffer;    // use data from payload parameter
            }

            if(writeBuffer != NULL)
            {
               writeKey(resourceID, user_no, seat_no, ldbid, writeBuffer, doHexdump);
            }
            else
            {
               printf("No Data to write to key\n");
            }
            break;
         case modeDeleteKey:
            deletekey(resourceID, user_no, seat_no, ldbid);
            break;
         case modeGetKeySize:
            getkeysize(resourceID, user_no, seat_no, ldbid);
            break;
         default:
            printSynopsis();
            break;
      }

      if(appName != NULL)
         free(appName);

      if(resourceID != NULL)
         free(resourceID);

      if(writeBuffer != NULL)
         free(writeBuffer);

      if(fileName != NULL)
         free(fileName);


      pclLifecycleSet(PCL_SHUTDOWN);

      pclDeinitLibrary();
	}
	else
	{
	   printf("Invalid application name or resourceID\n");
	   exit(EXIT_FAILURE);
	}

   // unregister debug log and trace
   DLT_UNREGISTER_APP();
   dlt_free();

   printf("\n");

   return 0;
}
コード例 #2
0
ファイル: dlt-example-user-func.c プロジェクト: Airtau/genivi
/**
 * Main function of tool.
 */
int main(int argc, char* argv[])
{
    int vflag = 0;
    int gflag = 0;
    int aflag = 0;
    char *dvalue = 0;
    char *fvalue = 0;
    char *nvalue = 0;
    char *message = 0;

    int index;
    int c;
    char *text;
	int num,maxnum;
	int delay;

    opterr = 0;

    while ((c = getopt (argc, argv, "vgad:f:n:")) != -1)
    {
        switch (c)
        {
        case 'v':
        {
            vflag = 1;
            break;
        }
        case 'g':
        {
            gflag = 1;
            break;
        }
        case 'a':
        {
            aflag = 1;
            break;
        }
        case 'd':
        {
            dvalue = optarg;
            break;
        }
        case 'f':
        {
            fvalue = optarg;
            break;
        }
        case 'n':
        {
            nvalue = optarg;
            break;
        }
        case '?':
        {
            if (optopt == 'd' || optopt == 'f' || optopt == 'n')
            {
                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 ();
        }
        }
    }

    for (index = optind; index < argc; index++)
    {
        message = argv[index];
    }

    if (message == 0)
    {
        /* no message, show usage and terminate */
        fprintf(stderr,"ERROR: No message selected\n");
        usage();
        return -1;
    }

    if (fvalue)
    {
        /* DLT is intialised automatically, except another output target will be used */
        if (dlt_init_file(fvalue)<0) /* log to file */
        {
            return -1;
        }
    }

    dlt_register_app("LOG","Test Application for Logging");

    dlt_register_context(&mycontext,"TEST","Test Context for Logging");

    dlt_register_injection_callback(&mycontext, 0xFFF, dlt_user_injection_callback);

    text = message;

    if (gflag)
    {
        dlt_nonverbose_mode();
    }

    if (aflag)
    {
        dlt_enable_local_print();
    }

    if (nvalue)
    {
        maxnum = atoi(nvalue);
    }
    else
    {
		maxnum = 10;
    }

    if (dvalue)
    {
        delay = atoi(dvalue) * 1000;
    }
    else
    {
        delay = 500 * 1000;
    }

	if (gflag)
    {
        /* DLT messages to test Fibex non-verbose description: dlt-example-non-verbose.xml */
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,10)>0)
		{
			dlt_user_log_write_finish(&mycontextdata);
		}
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,11)>0)
		{
			dlt_user_log_write_uint16(&mycontextdata,1011);
			dlt_user_log_write_finish(&mycontextdata);
		}
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,12)>0)
		{
			dlt_user_log_write_uint32(&mycontextdata,1012);
			dlt_user_log_write_uint32(&mycontextdata,1013);
			dlt_user_log_write_finish(&mycontextdata);
		}
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,13)>0)
		{
			dlt_user_log_write_uint8(&mycontextdata,123);
			dlt_user_log_write_float32(&mycontextdata,1.12);
			dlt_user_log_write_finish(&mycontextdata);
		}
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,14)>0)
		{
			dlt_user_log_write_string(&mycontextdata,"DEAD BEEF");
			dlt_user_log_write_finish(&mycontextdata);
		}
    }

    for (num=0;num<maxnum;num++)
    {
        printf("Send %d %s\n",num,text);

        if (gflag)
        {
            /* Non-verbose mode */
            if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_WARN,num)>0)
            {
                dlt_user_log_write_int(&mycontextdata,num);
                dlt_user_log_write_string(&mycontextdata,text);
                dlt_user_log_write_finish(&mycontextdata);
            }
        }
        else
        {
            /* Verbose mode */
            if (dlt_user_log_write_start(&mycontext,&mycontextdata,DLT_LOG_WARN)>0)
            {
                dlt_user_log_write_int(&mycontextdata,num);
                dlt_user_log_write_string(&mycontextdata,text);
                dlt_user_log_write_finish(&mycontextdata);
            }
        }

        if (delay>0)
        {
            usleep(delay);
        }
    }

    dlt_unregister_context(&mycontext);

    dlt_unregister_app();

    dlt_free();

    return 0;
}