summaryrefslogtreecommitdiff
path: root/modules/sensors/dynamic_sensor/test/HidRawDeviceTest.cpp
blob: 2a68e39a3b4171a2159848f0efbc7e5840673ee5 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * 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.
 */
#define LOG_TAG "HidRawDeviceTest"

#include "HidRawDevice.h"
#include "HidRawSensor.h"
#include "HidSensorDef.h"
#include "SensorEventCallback.h"
#include "Utils.h"
#include "HidLog.h"
#include "StreamIoUtil.h"

namespace android {
namespace SensorHalExt {

/*
 * Host test that verifies HidRawDevice and HidRawSensor works correctly.
 */
class HidRawDeviceTest {
public:
    static void test(const char *devicePath) {
        using namespace Hid::Sensor::SensorTypeUsage;
        using HidUtil::hexdumpToStream;

        std::unordered_set<unsigned int> interestedUsage{
                ACCELEROMETER_3D, GYROMETER_3D, COMPASS_3D, CUSTOM};

        SP(HidRawDevice) device =
                std::make_shared<HidRawDevice>(std::string(devicePath), interestedUsage);
        const HidDevice::HidDeviceInfo &info = device->getDeviceInfo();

        LOG_V << "Sizeof descriptor: " << info.descriptor.size() << LOG_ENDL;
        LOG_V << "Descriptor: " << LOG_ENDL;
        hexdumpToStream(LOG_V, info.descriptor.begin(), info.descriptor.end());

        if (!device->isValid()) {
            LOG_E << "invalid device" << LOG_ENDL;
            return;
        }

        LOG_V << "Digest: " << LOG_ENDL;
        LOG_V << device->mDigestVector;

        std::vector<uint8_t> buffer;
        // Dump first few feature ID to help debugging.
        // If device does not implement all these features, it will show error messages.
        for (int featureId = 0; featureId <= 5; ++featureId) {
            if (!device->getFeature(featureId, &buffer)) {
                LOG_E << "cannot get feature " << featureId << LOG_ENDL;
            } else {
                LOG_V << "Dump of feature " << featureId << LOG_ENDL;
                hexdumpToStream(LOG_V, buffer.begin(), buffer.end());
            }
        }
        //
        // use HidRawSensor to operate the device, pick first digest
        //
        auto &reportDigest = device->mDigestVector[0];
        SP(HidRawSensor) sensor = std::make_shared<HidRawSensor>(
                device, reportDigest.fullUsage, reportDigest.packets);

        if (!sensor->isValid()) {
            LOG_E << "Sensor is not valid " << LOG_ENDL;
            return;
        }

        const sensor_t *s = sensor->getSensor();
        LOG_V << "Sensor name: " << s->name << ", vendor: " << s->vendor << LOG_ENDL;
        LOG_V << sensor->dump() << LOG_ENDL;

        class Callback : public SensorEventCallback {
            virtual int submitEvent(SP(BaseSensorObject) /*sensor*/, const sensors_event_t &e) {
                LOG_V << "sensor: " << e.sensor << ", type: " << e.type << ", ts: " << e.timestamp
                      << ", values (" << e.data[0] << ", " << e.data[1] << ", " << e.data[2] << ")"
                      << LOG_ENDL;
                return 1;
            }
        };
        Callback callback;
        sensor->setEventCallback(&callback);

        // Request sensor samples at to 10Hz (100ms)
        sensor->batch(100LL*1000*1000 /*ns*/, 0);
        sensor->enable(true);

        // get a couple of events
        for (size_t i = 0; i < 100; ++i) {
            uint8_t id;
            if (!device->receiveReport(&id, &buffer)) {
                LOG_E << "Receive report error" << LOG_ENDL;
                continue;
            }
            sensor->handleInput(id, buffer);
        }

        // clean up
        sensor->enable(false);

        LOG_V << "Done!" << LOG_ENDL;
    }
};
} //namespace SensorHalExt
} //namespace android

int main(int argc, char* argv[]) {
    if (argc != 2) {
        LOG_E << "Usage: " << argv[0] << " hidraw-dev-path" << LOG_ENDL;
        return -1;
    }
    android::SensorHalExt::HidRawDeviceTest::test(argv[1]);
    return 0;
}