/
hybrisalsadaptor.cpp
140 lines (124 loc) · 4.06 KB
/
hybrisalsadaptor.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
/****************************************************************************
**
** 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 <QFile>
#include <QTextStream>
#include "hybrisalsadaptor.h"
#include "logging.h"
#include "datatypes/utils.h"
#include <hardware/sensors.h>
#include "config.h"
#include <fcntl.h>
#include <unistd.h>
HybrisAlsAdaptor::HybrisAlsAdaptor(const QString& id) :
HybrisAdaptor(id,SENSOR_TYPE_LIGHT),
lastLightValue(9999)
{
buffer = new DeviceAdaptorRingBuffer<TimedUnsigned>(1);
setAdaptedSensor("als", "Internal ambient light sensor lux values", buffer);
setDescription("Hybris als");
powerStatePath = Config::configuration()->value("als/powerstate_path").toByteArray();
if (!powerStatePath.isEmpty() && !QFile::exists(powerStatePath))
{
sensordLogW() << "Path does not exists: " << powerStatePath;
powerStatePath.clear();
}
}
HybrisAlsAdaptor::~HybrisAlsAdaptor()
{
delete buffer;
}
bool HybrisAlsAdaptor::startSensor()
{
if (!(HybrisAdaptor::startSensor()))
return false;
if (isRunning() && !powerStatePath.isEmpty())
writeToFile(powerStatePath, "1");
sensordLogD() << "Hybris HybrisAlsAdaptor start\n";
return true;
}
void HybrisAlsAdaptor::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("S: Sysfs=")) {
inputDev = line.split("=").at(1).section("/",-1);
ok = false;
break;
}
if (line.contains("als")) {
ok = true;
}
line = in.readLine();
}
if (inputDev.isEmpty()) {
sensordLogW() << "No sysfs als device found";
return;
}
struct input_absinfo absinfo;
int fd;
inputDev.replace("input","event");
inputDev.prepend("/dev/input/");
if ((fd = open(inputDev.toLatin1(), O_RDONLY)) > -1) {
if (!ioctl(fd, EVIOCGABS(ABS_MISC), &absinfo)) {
if (absinfo.value != (signed)lastLightValue)
lastLightValue = absinfo.value;
TimedUnsigned *d = buffer->nextSlot();
d->timestamp_ = Utils::getTimeStamp();
d->value_ = lastLightValue;
buffer->commit();
buffer->wakeUpReaders();
} else {
qDebug() << "ioctl not opened" ;
}
close(fd);
} else {
qDebug() << "could not open als evdev";
TimedUnsigned *d = buffer->nextSlot();
d->timestamp_ = Utils::getTimeStamp();
d->value_ = lastLightValue;
buffer->commit();
buffer->wakeUpReaders();
}
}
}
void HybrisAlsAdaptor::stopSensor()
{
HybrisAdaptor::stopSensor();
if (!isRunning() && !powerStatePath.isEmpty())
writeToFile(powerStatePath, "0");
sensordLogD() << "Hybris HybrisAlsAdaptor stop\n";
}
void HybrisAlsAdaptor::processSample(const sensors_event_t& data)
{
TimedUnsigned *d = buffer->nextSlot();
d->timestamp_ = quint64(data.timestamp * .001);
d->value_ = data.light;
lastLightValue = d->value_;
buffer->commit();
buffer->wakeUpReaders();
}
void HybrisAlsAdaptor::init()
{
}