Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2017 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 #include "HidLog.h"
     17 #include "HidParser.h"
     18 #include "TestHidDescriptor.h"
     19 #include <errno.h>
     20 
     21 using HidUtil::HidParser;
     22 
     23 bool doParse() {
     24     HidParser hidParser;
     25     bool ret = true;
     26 
     27     for (const TestHidDescriptor *p = gDescriptorArray; ; ++p) {
     28         if (p->data == nullptr || p->len == 0) {
     29             break;
     30         }
     31         const char *name = p->name != nullptr ? p->name : "unnamed";
     32         bool parseResult = hidParser.parse(p->data, p->len);
     33 
     34         if (parseResult) {
     35             LOG_V << name << "  filtered tree: " << LOG_ENDL;
     36             LOG_V << *(hidParser.getTree());
     37         } else {
     38             ret = false;
     39             LOG_E << name << " parsing error!" << LOG_ENDL;
     40         }
     41     }
     42     return ret;
     43 }
     44 
     45 bool doParseAndFilter() {
     46     HidParser hidParser;
     47     bool ret = true;
     48 
     49     for (const TestHidDescriptor *p = gDescriptorArray; ; ++p) {
     50         if (p->data == nullptr || p->len == 0) {
     51             break;
     52         }
     53         const char *name = p->name != nullptr ? p->name : "unnamed";
     54         bool parseResult = hidParser.parse(p->data, p->len);
     55 
     56         if (parseResult) {
     57             hidParser.filterTree();
     58             LOG_V << name << "  filtered tree: " << LOG_ENDL;
     59             LOG_V << *(hidParser.getTree());
     60         } else {
     61             ret = false;
     62             LOG_E << name << " parsing error!" << LOG_ENDL;
     63         }
     64     }
     65     return ret;
     66 }
     67 
     68 bool doDigest() {
     69     HidParser hidParser;
     70     bool ret = true;
     71 
     72     // value from HID sensor usage page spec
     73     std::unordered_set<unsigned int> interestedUsage = {
     74         0x200073, // accelerometer 3d
     75         0x200076, // gyro 3d
     76         0x200083, // mag 3d
     77         0x20008a, // device orientation (rotation vector)
     78     };
     79 
     80     for (const TestHidDescriptor *p = gDescriptorArray; ; ++p) {
     81         if (p->data == nullptr || p->len == 0) {
     82             break;
     83         }
     84         const char *name = p->name != nullptr ? p->name : "unnamed";
     85         bool parseResult = hidParser.parse(p->data, p->len);
     86 
     87         if (!parseResult) {
     88             LOG_E << name << " parsing error!" << LOG_ENDL;
     89             ret = false;
     90             continue;
     91         }
     92 
     93         hidParser.filterTree();
     94         LOG_V << name << "  digest: " << LOG_ENDL;
     95         HidParser::DigestVector digestVector = hidParser.generateDigest(interestedUsage);
     96         LOG_V << digestVector;
     97     }
     98     return ret;
     99 }
    100 
    101 void printUsage(char *argv0) {
    102     LOG_V << "Usage: " << argv0 << " test_name" << LOG_ENDL;
    103     LOG_V << "  test_name can be parse, parse_filter, digest." << LOG_ENDL;
    104 }
    105 
    106 int main(int argc, char* argv[]) {
    107     int ret;
    108 
    109     if (argc != 2) {
    110         LOG_E << "Error: need param" << LOG_ENDL;
    111         printUsage(argv[0]);
    112         return -EINVAL;
    113     }
    114 
    115     if (strcmp(argv[1], "parse") == 0) {
    116         ret = doParse() ? 0 : 1;
    117     } else if (strcmp(argv[1], "parse_filter") == 0) {
    118         ret = doParseAndFilter() ? 0 : 1;
    119     } else if (strcmp(argv[1], "digest") == 0) {
    120         ret = doDigest() ? 0 : 1;
    121     } else {
    122         LOG_E << "Error: unknown test name" << LOG_ENDL;
    123         printUsage(argv[0]);
    124         ret = -ENOENT;
    125     }
    126 
    127     return ret;
    128 }
    129