/
hybrisproximityadaptor.cpp
154 lines (136 loc) · 4.33 KB
/
hybrisproximityadaptor.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/****************************************************************************
**
** Copyright (C) 2013 Jolla Ltd
** Contact: lorn.potter@jollamobile.com
**
**
** $QT_BEGIN_LICENSE:LGPL$
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include "hybrisproximityadaptor.h"
#include "logging.h"
#include "datatypes/utils.h"
#include <hardware/sensors.h>
#include "config.h"
#include <fcntl.h>
#include <unistd.h>
HybrisProximityAdaptor::HybrisProximityAdaptor(const QString& id) :
HybrisAdaptor(id,SENSOR_TYPE_PROXIMITY),
lastNearValue(-1)
{
if (isValid()) {
buffer = new DeviceAdaptorRingBuffer<ProximityData>(1);
setAdaptedSensor("proximity", "Internal proximity coordinates", buffer);
setDescription("Hybris proximity");
powerStatePath = Config::configuration()->value("proximity/powerstate_path").toByteArray();
if (!powerStatePath.isEmpty() && !QFile::exists(powerStatePath))
{
sensordLogW() << "Path does not exists: " << powerStatePath;
powerStatePath.clear();
}
}
}
HybrisProximityAdaptor::~HybrisProximityAdaptor()
{
if (isValid()) {
delete buffer;
}
}
bool HybrisProximityAdaptor::startSensor()
{
if (!(HybrisAdaptor::startSensor()))
return false;
if (isRunning() && !powerStatePath.isEmpty())
writeToFile(powerStatePath, "1");
sensordLogD() << "HybrisProximityAdaptor start\n";
return true;
}
void HybrisProximityAdaptor::sendInitialData()
{
QFile file("/proc/bus/input/devices");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
bool ok = false;
QString inputDev;
QTextStream in(&file);
QString line = in.readLine();
while (!line.isNull()) {
if (ok && line.startsWith("H: Handlers")) {
inputDev = line.split("=").at(1).section("/",-1).simplified();
ok = false;
break;
}
if (line.contains("proximity")) {
ok = true;
}
line = in.readLine();
}
if (inputDev.isEmpty()) {
sensordLogW() << "No sysfs proximity device found";
return;
}
struct input_absinfo absinfo;
int fd;
inputDev.prepend("/dev/input/");
if ((fd = open(inputDev.toLatin1(), O_RDONLY)) > -1) {
if (!ioctl(fd, EVIOCGABS(ABS_DISTANCE), &absinfo)) {
bool near = false;
if (absinfo.value == 0)
near = true;
ProximityData *d = buffer->nextSlot();
d->timestamp_ = Utils::getTimeStamp();
d->withinProximity_ = near;
d->value_ = absinfo.value;
buffer->commit();
buffer->wakeUpReaders();
} else {
qDebug() << "ioctl not opened" ;
}
close(fd);
} else {
qDebug() << "could not open proximity evdev";
ProximityData *d = buffer->nextSlot();
d->timestamp_ = Utils::getTimeStamp();
d->withinProximity_ = false;
d->value_ = 10;
buffer->commit();
buffer->wakeUpReaders();
}
}
}
void HybrisProximityAdaptor::stopSensor()
{
HybrisAdaptor::stopSensor();
if (!isRunning() && !powerStatePath.isEmpty())
writeToFile(powerStatePath, "0");
sensordLogD() << "HybrisProximityAdaptor stop\n";
}
void HybrisProximityAdaptor::processSample(const sensors_event_t& data)
{
ProximityData *d = buffer->nextSlot();
d->timestamp_ = quint64(data.timestamp * .001);
bool near = false;
if (data.distance < maxRange()) {
near = true;
}
d->withinProximity_ = near;
d->value_ = data.distance;
lastNearValue = near;
buffer->commit();
buffer->wakeUpReaders();
}
void HybrisProximityAdaptor::init()
{
}