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.
*/
#include "HidRawSensorDevice.h"
#include "HidRawSensor.h"
#include "HidSensorDef.h"
#include <utils/Log.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/hidraw.h>
#include <sys/ioctl.h>
#include <set>
namespace android {
namespace SensorHalExt {
using namespace Hid::Sensor::SensorTypeUsage;
using namespace Hid::Sensor::PropertyUsage;
const std::unordered_set<unsigned int> HidRawSensorDevice::sInterested{
ACCELEROMETER_3D, GYROMETER_3D, COMPASS_3D, CUSTOM};
sp<HidRawSensorDevice> HidRawSensorDevice::create(const std::string &devName) {
sp<HidRawSensorDevice> device(new HidRawSensorDevice(devName));
// offset +1 strong count added by constructor
device->decStrong(device.get());
if (device->mValid) {
return device;
} else {
return nullptr;
}
}
HidRawSensorDevice::HidRawSensorDevice(const std::string &devName)
: RefBase(), HidRawDevice(devName, sInterested),
Thread(false /*canCallJava*/), mValid(false) {
// create HidRawSensor objects from digest
// HidRawSensor object will take sp<HidRawSensorDevice> as parameter, so increment strong count
// to prevent "this" being destructed.
this->incStrong(this);
if (!HidRawDevice::isValid()) {
return;
}
for (const auto &digest : mDigestVector) { // for each usage - vec<ReportPacket> pair
uint32_t usage = static_cast<uint32_t>(digest.fullUsage);
sp<HidRawSensor> s(new HidRawSensor(this, usage, digest.packets));
if (s->isValid()) {
for (const auto &packet : digest.packets) {
if (packet.type == HidParser::REPORT_TYPE_INPUT) { // only used for input mapping
mSensors.emplace(packet.id/* report id*/, s);
}
}
}
}
if (mSensors.size() == 0) {
return;
}
run("HidRawSensor");
mValid = true;
}
HidRawSensorDevice::~HidRawSensorDevice() {
ALOGV("~HidRawSensorDevice %p", this);
requestExitAndWait();
ALOGV("~HidRawSensorDevice %p, thread exited", this);
}
bool HidRawSensorDevice::threadLoop() {
ALOGV("Hid Raw Device thread started %p", this);
std::vector<uint8_t> buffer;
bool ret;
uint8_t usageId;
while(!Thread::exitPending()) {
ret = receiveReport(&usageId, &buffer);
if (!ret) {
break;
}
auto i = mSensors.find(usageId);
if (i == mSensors.end()) {
ALOGW("Input of unknow usage id %u received", usageId);
continue;
}
i->second->handleInput(usageId, buffer);
}
ALOGI("Hid Raw Device thread ended for %p", this);
return false;
}
BaseSensorVector HidRawSensorDevice::getSensors() const {
BaseSensorVector ret;
std::set<sp<BaseSensorObject>> set;
for (const auto &s : mSensors) {
if (set.find(s.second) == set.end()) {
ret.push_back(s.second);
set.insert(s.second);
}
}
return ret;
}
} // namespace SensorHalExt
} // namespace android
|