Home | History | Annotate | Download | only in target
      1 /*
      2  * Copyright 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "poc_test.h"
     18 
     19 #include <getopt.h>
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include <unistd.h>
     23 
     24 #include <iostream>
     25 #include <sstream>
     26 
     27 using std::cout;
     28 using std::cerr;
     29 using std::endl;
     30 using std::map;
     31 using std::string;
     32 
     33 static struct option long_options[] = {
     34   {"device_model", required_argument, 0, 'd'},
     35   {"params", required_argument, 0, 'p'}
     36 };
     37 
     38 static DeviceModel TranslateDeviceModel(const char *model_name) {
     39   DeviceModel device_model;
     40   if (!strcmp("Nexus 5", model_name)) {
     41       device_model = NEXUS_5;
     42   } else if (!strcmp("Nexus 5X", model_name)) {
     43       device_model = NEXUS_5X;
     44   } else if (!strcmp("Nexus 6", model_name)) {
     45       device_model = NEXUS_6;
     46   } else if (!strcmp("Nexus 6P", model_name)) {
     47       device_model = NEXUS_6P;
     48   } else if (!strcmp("Pixel", model_name)) {
     49       device_model = PIXEL;
     50   } else if (!strcmp("Pixel XL", model_name)) {
     51       device_model = PIXEL_XL;
     52   } else {
     53       device_model = OTHER;
     54   }
     55   return device_model;
     56 }
     57 
     58 static map<string, string> ExtractParams(const char *test_params) {
     59   map<string, string> params;
     60   string input(test_params);
     61   std::istringstream iss(input);
     62 
     63   string key_value;
     64   while(std::getline(iss, key_value, ',')) {
     65     size_t delim = key_value.find('=');
     66     if (delim == string::npos) {
     67       cerr << "Missing '=' delimiter.\n";
     68       exit(POC_TEST_SKIP);
     69     }
     70 
     71     string key = key_value.substr(0, delim);
     72     string value = key_value.substr(delim + 1);
     73 
     74     params[key] = value;
     75   }
     76 
     77   return params;
     78 }
     79 
     80 VtsHostInput ParseVtsHostFlags(int argc, char *argv[]) {
     81   VtsHostInput host_input;
     82   int opt = 0;
     83   int index = 0;
     84   while ((opt = getopt_long_only(argc, argv, "", long_options, &index)) != -1) {
     85     switch(opt) {
     86       case 'd':
     87         host_input.device_model = TranslateDeviceModel(optarg);
     88         break;
     89       case 'p':
     90         host_input.params = ExtractParams(optarg);
     91         break;
     92       default:
     93         cerr << "Wrong parameters.\n";
     94         exit(POC_TEST_SKIP);
     95     }
     96   }
     97   return host_input;
     98 }
     99