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 
17 #include "TestHidDescriptor.h"
18 #include "HidLog.h"
19 #include "HidParser.h"
20 #include "StreamIoUtil.h"
21 
22 using namespace HidUtil;
23 
printRawValue(const std::vector<unsigned char> & descriptor)24 void printRawValue(const std::vector<unsigned char> &descriptor) {
25     LOG_D << "Descriptor [" << descriptor.size() << "]: " << std::hex;
26     hexdumpToStream(LOG_D, descriptor.begin(), descriptor.end());
27 }
28 
printToken(const std::vector<HidItem> & hidItemVector)29 void printToken(const std::vector<HidItem> &hidItemVector) {
30     LOG_V << "Total " << hidItemVector.size() << " tokens" << LOG_ENDL;
31     for (auto &i : hidItemVector) {
32         LOG_V << i << LOG_ENDL;
33     }
34 }
35 
main()36 int main() {
37     const TestHidDescriptor *t = findTestDescriptor("accel3");
38     constexpr unsigned int ACCEL_3D_USAGE = 0x200073;
39 
40     assert(t != nullptr);
41     std::vector<unsigned char> descriptor(t->data, t->data + t->len);
42 
43     // parse can be done in one step with HidParser::parse(const unsigned char *begin, size_t size);
44     // here it is done in multiple steps for illustration purpose
45     HidParser hidParser;
46     // print out raw value
47     printRawValue(descriptor);
48 
49     // tokenize it
50     std::vector<HidItem> hidItemVector = HidItem::tokenize(descriptor);
51 
52     // print out tokens
53     printToken(hidItemVector);
54 
55     // parse it
56     if (hidParser.parse(hidItemVector)) {
57         // making a deepcopy of tree (not necessary, but for illustration)
58         std::shared_ptr<HidTreeNode> tree = hidParser.getTree()->deepCopy();
59 
60         LOG_V << "Tree: " << LOG_ENDL;
61         LOG_V << *tree;
62         LOG_V << LOG_ENDL;
63 
64         hidParser.filterTree();
65         LOG_V << "FilteredTree: " << LOG_ENDL;
66         LOG_V << *(hidParser.getTree());
67 
68         LOG_V << "DigestVector: " << LOG_ENDL;
69         std::unordered_set<unsigned int> interested = {ACCEL_3D_USAGE};
70         HidParser::DigestVector digestVector = hidParser.generateDigest(interested);
71         LOG_V << digestVector;
72 
73     } else {
74         LOG_V << "Parsing Error" << LOG_ENDL;
75     }
76 
77     return 0;
78 }
79 
80