summaryrefslogtreecommitdiff
path: root/modules/sensors/dynamic_sensor/HidUtils/test/HidParserExample2.cpp
blob: b151dffd0dc21e522a15d5ca243b753536070ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "TestHidDescriptor.h"
#include "HidLog.h"
#include "HidParser.h"
#include "StreamIoUtil.h"

using namespace HidUtil;

void printRawValue(const std::vector<unsigned char> &descriptor) {
    LOG_D << "Descriptor [" << descriptor.size() << "]: " << std::hex;
    hexdumpToStream(LOG_D, descriptor.begin(), descriptor.end());
}

void printToken(const std::vector<HidItem> &hidItemVector) {
    LOG_V << "Total " << hidItemVector.size() << " tokens" << LOG_ENDL;
    for (auto &i : hidItemVector) {
        LOG_V << i << LOG_ENDL;
    }
}

int main() {
    const TestHidDescriptor *t = findTestDescriptor("accel3");
    constexpr unsigned int ACCEL_3D_USAGE = 0x200073;

    assert(t != nullptr);
    std::vector<unsigned char> descriptor(t->data, t->data + t->len);

    // parse can be done in one step with HidParser::parse(const unsigned char *begin, size_t size);
    // here it is done in multiple steps for illustration purpose
    HidParser hidParser;
    // print out raw value
    printRawValue(descriptor);

    // tokenize it
    std::vector<HidItem> hidItemVector = HidItem::tokenize(descriptor);

    // print out tokens
    printToken(hidItemVector);

    // parse it
    if (hidParser.parse(hidItemVector)) {
        // making a deepcopy of tree (not necessary, but for illustration)
        std::shared_ptr<HidTreeNode> tree = hidParser.getTree()->deepCopy();

        LOG_V << "Tree: " << LOG_ENDL;
        LOG_V << *tree;
        LOG_V << LOG_ENDL;

        hidParser.filterTree();
        LOG_V << "FilteredTree: " << LOG_ENDL;
        LOG_V << *(hidParser.getTree());

        LOG_V << "DigestVector: " << LOG_ENDL;
        std::unordered_set<unsigned int> interested = {ACCEL_3D_USAGE};
        HidParser::DigestVector digestVector = hidParser.generateDigest(interested);
        LOG_V << digestVector;

    } else {
        LOG_V << "Parsing Error" << LOG_ENDL;
    }

    return 0;
}