/
hybrisadaptor.cpp
616 lines (528 loc) · 16 KB
/
hybrisadaptor.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/****************************************************************************
**
** 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 "hybrisadaptor.h"
#include "deviceadaptor.h"
#include <QDebug>
#include <QCoreApplication>
#include <QTimer>
#include <android/hardware/hardware.h>
#include <android/hardware/sensors.h>
#ifndef SENSOR_TYPE_ACCELEROMETER
#define SENSOR_TYPE_ACCELEROMETER (1)
#endif
#ifndef SENSOR_TYPE_MAGNETIC_FIELD
#define SENSOR_TYPE_MAGNETIC_FIELD (2)
#endif
#ifndef SENSOR_TYPE_ORIENTATION
#define SENSOR_TYPE_ORIENTATION (3)
#endif
#ifndef SENSOR_TYPE_GYROSCOPE
#define SENSOR_TYPE_GYROSCOPE (4)
#endif
#ifndef SENSOR_TYPE_LIGHT
#define SENSOR_TYPE_LIGHT (5)
#endif
#ifndef SENSOR_TYPE_PRESSURE
#define SENSOR_TYPE_PRESSURE (6)
#endif
#ifndef SENSOR_TYPE_TEMPERATURE
#define SENSOR_TYPE_TEMPERATURE (7)
#endif
#ifndef SENSOR_TYPE_PROXIMITY
#define SENSOR_TYPE_PROXIMITY (8)
#endif
#ifndef SENSOR_TYPE_GRAVITY
#define SENSOR_TYPE_GRAVITY (9)
#endif
#ifndef SENSOR_TYPE_LINEAR_ACCELERATION
#define SENSOR_TYPE_LINEAR_ACCELERATION (10)
#endif
#ifndef SENSOR_TYPE_ROTATION_VECTOR
#define SENSOR_TYPE_ROTATION_VECTOR (11)
#endif
#ifndef SENSOR_TYPE_RELATIVE_HUMIDITY
#define SENSOR_TYPE_RELATIVE_HUMIDITY (12)
#endif
#ifndef SENSOR_TYPE_AMBIENT_TEMPERATURE
#define SENSOR_TYPE_AMBIENT_TEMPERATURE (13)
#endif
//#define SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED (14)
//#define SENSOR_TYPE_GAME_ROTATION_VECTOR (15)
//#define SENSOR_TYPE_GYROSCOPE_UNCALIBRATED (16)
//#define SENSOR_TYPE_SIGNIFICANT_MOTION (17)
//#define SENSOR_TYPE_STEP_DETECTOR (18)
//#define SENSOR_TYPE_STEP_COUNTER (19)
//#define SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR (20)
static QHash<QString,int> HybrisAdaptor_sensorTypes()
{
QHash<QString,int> types;
types["accelerometer"] = SENSOR_TYPE_ACCELEROMETER;
types["magnetomoter"] = SENSOR_TYPE_MAGNETIC_FIELD;
types["orientation"] = SENSOR_TYPE_ORIENTATION;
types["gyroscope"] = SENSOR_TYPE_GYROSCOPE;
types["light"] = SENSOR_TYPE_LIGHT;
types["pressure"] = SENSOR_TYPE_PRESSURE;
types["temperature"] = SENSOR_TYPE_TEMPERATURE;
types["proximity"] = SENSOR_TYPE_PROXIMITY;
types["gravity"] = SENSOR_TYPE_GRAVITY;
types["lacceration"] = SENSOR_TYPE_LINEAR_ACCELERATION;
return types;
}
Q_GLOBAL_STATIC(HybrisManager, hybrisManager)
HybrisManager::HybrisManager(QObject *parent) :
QObject(parent),
adaptorReader(parent),
sensorsCount(0),
sensorsOpened(0)
{
init();
}
HybrisManager::~HybrisManager()
{
closeAllSensors();
}
HybrisManager *HybrisManager::instance()
{
HybrisManager *priv = hybrisManager();
return priv;
}
void HybrisManager::init()
{
int errorCode;
errorCode = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
if (errorCode != 0) {
qDebug() << "hw_get_module() failed" << strerror(-errorCode);
return ;
}
errorCode = sensors_open(&module->common, &device);
if (errorCode != 0) {
qDebug() << "sensors_open() failed" << strerror(-errorCode);
return ;
}
sensorsCount = module->get_sensors_list(module, &sensorList);
for (int i = 0 ; i < sensorsCount ; i++) {
sensorMap.insert(sensorList[i].type, i);
}
}
int HybrisManager::handleForType(int sensorType)
{
if (sensorMap.contains(sensorType))
return sensorList[sensorMap[sensorType]].handle;
return 0;
}
int HybrisManager::maxRange(int sensorType)
{
if (sensorMap.contains(sensorType))
return sensorList[sensorMap[sensorType]].maxRange;
return 0;
}
int HybrisManager::minDelay(int sensorType)
{
if (sensorMap.contains(sensorType))
return sensorList[sensorMap[sensorType]].minDelay;
return 0;
}
int HybrisManager::resolution(int sensorType)
{
if (sensorMap.contains(sensorType))
return sensorList[sensorMap[sensorType]].resolution;
return 0;
}
bool HybrisManager::setDelay(int sensorHandle, int interval)
{
int result = device->setDelay(device, sensorHandle, interval);
if (result < 0) {
sensordLogW() << "setDelay() failed" << strerror(-result);
return false;
}
return true;
}
void HybrisManager::startReader(HybrisAdaptor *adaptor)
{
if (registeredAdaptors.values().contains(adaptor)) {
sensordLogD() << "activating " << adaptor->name();
int error = device->activate(device, adaptor->sensorHandle, 1);
if (error != 0) {
sensordLogW() <<Q_FUNC_INFO<< "failed for"<< strerror(-error);
}
if (!adaptorReader.isRunning())
adaptorReader.startReader();
}
}
void HybrisManager::stopReader(HybrisAdaptor *adaptor)
{
QList <HybrisAdaptor *> list;
list = registeredAdaptors.values();
bool okToStop = true;
for (int i = 0; i < list.count(); i++) {
if (list.at(i) != adaptor && list.at(i)->isRunning()) {
okToStop = false;
}
}
if (okToStop) {
adaptorReader.stopReader();
adaptorReader.wait();
sensordLogD() << "deactivating " << adaptor->name();
int error = device->activate(device, adaptor->sensorHandle, 0);
if (error != 0) {
sensordLogW() <<Q_FUNC_INFO<< "failed for"<< strerror(-error);
}
}
}
bool HybrisManager::resumeReader(HybrisAdaptor *adaptor)
{
sensordLogD() << Q_FUNC_INFO << adaptor->id() << adaptor->deviceStandbyOverride(); //alwaysOn
QList <HybrisAdaptor *> list;
list = registeredAdaptors.values();
bool okToResume = false;
for (int i = 0; i < list.count(); i++) {
if (list.at(i) != adaptor && list.at(i)->isRunning()) {
qDebug() << Q_FUNC_INFO << "running";
okToResume = true;
}
}
if (okToResume) {
sensordLogD() << "activating for resume" << adaptor->name();
int error = device->activate(device, adaptor->sensorHandle, 1);
if (error != 0) {
sensordLogW() <<Q_FUNC_INFO<< "failed for"<< strerror(-error);
}
}
return true;
}
void HybrisManager::standbyReader(HybrisAdaptor *adaptor)
{
sensordLogD() << Q_FUNC_INFO << adaptor->id() << adaptor->deviceStandbyOverride(); //alwaysOn
QList <HybrisAdaptor *> list;
list = registeredAdaptors.values();
bool okToStandby = true;
for (int i = 0; i < list.count(); i++) {
if (adaptor->sensorType == list.at(i)->sensorType && list.at(i)->isRunning()) {
qDebug() << Q_FUNC_INFO << list.at(i) << "running";
okToStandby = false;
}
}
if (okToStandby) {
sensordLogD() << "deactivating for standby" << adaptor->name();
int error = device->activate(device, adaptor->sensorHandle, 0);
if (error != 0) {
sensordLogW() <<Q_FUNC_INFO<< "failed for"<< strerror(-error);
}
}
}
bool HybrisManager::openSensors()
{
if (!sensorsOpened) {
int errorCode = sensors_open(&module->common, &device);
if (errorCode != 0) {
qDebug() << "sensors_open() failed" << strerror(-errorCode);
return false;
}
}
sensorsOpened = true;
return true;
}
bool HybrisManager::closeSensors()
{
QList <HybrisAdaptor *> list;
list = registeredAdaptors.values();
bool okToStop = true;
for (int i = 0; i < list.count(); i++) {
if (list.at(i)->isRunning())
okToStop = false;
qDebug() << Q_FUNC_INFO << "still running"<< list.at(i);
}
if (okToStop) {
if (sensorsOpened) { //TODO
int errorCode = sensors_close(device);
if (errorCode != 0) {
qDebug() << "sensors_close() failed" << strerror(-errorCode);
return false;
}
}
}
return true;
}
void HybrisManager::closeAllSensors()
{
QList <HybrisAdaptor *> list;
list = registeredAdaptors.values();
for (int i = 0; i < list.count(); i++) {
if (list.at(i)->isRunning())
list.at(i)->stopSensor();
}
int errorCode = sensors_close(device);
if (errorCode != 0) {
qDebug() << "sensors_close() failed" << strerror(-errorCode);
}
}
void HybrisManager::processSample(const sensors_event_t& data)
{
if (registeredAdaptors.contains(data.type)) {
QList <HybrisAdaptor *> list;
list = registeredAdaptors.values(data.type);
for (int i = 0; i < list.count(); i++){
if (list.at(i)->isRunning())
list.at(i)->processSample(data);
}
}
}
void HybrisManager::registerAdaptor(HybrisAdaptor *adaptor)
{
if (!registeredAdaptors.values().contains(adaptor)) {
registeredAdaptors.insertMulti(adaptor->sensorType, adaptor);
}
}
//////////////////////////////////
HybrisAdaptor::HybrisAdaptor(const QString& id, int type)
: DeviceAdaptor(id),
sensorType(type),
cachedInterval(50),
inStandbyMode_(false),
running_(false)
{
if (!HybrisAdaptor_sensorTypes().values().contains(sensorType)) {
qDebug() << Q_FUNC_INFO <<"no such sensor" << id;
return;
}
hybrisManager()->registerAdaptor(this);
init();
introduceAvailableInterval(DataRange(minDelay, 1000, 0));
introduceAvailableDataRange(DataRange(-(maxRange*.5), (maxRange*.5), 1));
}
HybrisAdaptor::~HybrisAdaptor()
{
}
void HybrisAdaptor::init()
{
sensorHandle = hybrisManager()->handleForType(sensorType);
maxRange = hybrisManager()->maxRange(sensorType);
minDelay = hybrisManager()->minDelay(sensorType);
resolution = hybrisManager()->resolution(sensorType);
}
bool HybrisAdaptor::addSensorType(int type)
{
sensorIds.append(type);
return true;
}
bool HybrisAdaptor::startAdaptor()
{
return hybrisManager()->openSensors();
}
bool HybrisAdaptor::isRunning() const
{
return running_;
}
void HybrisAdaptor::stopAdaptor()
{
if (getAdaptedSensor()->isRunning())
stopSensor();
hybrisManager()->closeSensors();
}
bool HybrisAdaptor::startSensor()
{
AdaptedSensorEntry *entry = getAdaptedSensor();
if (entry == NULL) {
qDebug() << "Sensor not found: " << name();
return false;
}
// Increase listener count
entry->addReference();
/// Check from entry
if (isRunning()) {
qDebug() << Q_FUNC_INFO << "already running";
return false;
}
// Do not open if in standby mode.
if (inStandbyMode_ && !deviceStandbyOverride()) {
qDebug() << Q_FUNC_INFO << "inStandbyMode_ true";
return false;
}
shouldBeRunning_ = true;
/// We are waking up from standby or starting fresh, no matter
inStandbyMode_ = false;
if (!startReaderThread()) {
entry->removeReference();
entry->setIsRunning(false);
running_ = false;
shouldBeRunning_ = false;
return false;
}
entry->setIsRunning(true);
running_ = true;
return true;
}
void HybrisAdaptor::stopSensor()
{
AdaptedSensorEntry *entry = getAdaptedSensor();
if (entry == NULL) {
sensordLogW() << "Sensor not found " << name();
return;
}
if (!shouldBeRunning_) {
return;
}
entry->removeReference();
if (entry->referenceCount() <= 0) {
entry->setIsRunning(false);
running_ = false;
shouldBeRunning_ = false;
if (!inStandbyMode_) {
stopReaderThread();
}
}
}
bool HybrisAdaptor::standby()
{
sensordLogD() << "Adaptor '" << id() << "' requested to go to standby";
if (inStandbyMode_) {
sensordLogD() << "Adaptor '" << id() << "' not going to standby: already in standby";
return false;
}
if (!isRunning()) {
sensordLogD() << "Adaptor '" << id() << "' not going to standby: not running";
return false;
}
inStandbyMode_ = true;
sensordLogD() << "Adaptor '" << id() << "' going to standby";
running_ = deviceStandbyOverride();
hybrisManager()->standbyReader(this);
return true;
}
bool HybrisAdaptor::resume()
{
sensordLogD() << "Adaptor '" << id() << "' requested to resume from standby";
// Don't resume if not in standby
if (!inStandbyMode_) {
sensordLogD() << "Adaptor '" << id() << "' not resuming: not in standby";
return false;
}
inStandbyMode_ = false;
if (!shouldBeRunning_) {
sensordLogD() << "Adaptor '" << id() << "' not resuming from standby: not running";
return false;
}
sensordLogD() << "Adaptor '" << id() << "' resuming from standby";
if (!hybrisManager()->resumeReader(this)) {
sensordLogW() << "Adaptor '" << id() << "' failed to resume from standby!";
return false;
}
running_ = true;
return true;
}
unsigned int HybrisAdaptor::interval() const
{
return cachedInterval;
}
bool HybrisAdaptor::setInterval(const unsigned int value, const int /*sessionId*/)
{ // 1000000
cachedInterval = value;
bool ok;
qreal ns = value * 1000000; // ms to ns
ok = hybrisManager()->setDelay(sensorHandle, ns);
if (!ok) {
qDebug() << Q_FUNC_INFO << "setInterval not ok";
}
return ok;
}
void HybrisAdaptor::stopReaderThread()
{
hybrisManager()->stopReader(this);
running_ = false;
}
bool HybrisAdaptor::startReaderThread()
{
running_ = true;
hybrisManager()->startReader(this);
return true;
}
unsigned int HybrisAdaptor::evaluateIntervalRequests(int& sessionId) const
{
if (m_intervalMap.size() == 0)
{
sessionId = -1;
return defaultInterval();
}
// Get the smallest positive request, 0 is reserved for HW wakeup
QMap<int, unsigned int>::const_iterator it = m_intervalMap.constBegin();
unsigned int highestValue = it.value();
int winningSessionId = it.key();
for (++it; it != m_intervalMap.constEnd(); ++it)
{
if (((it.value() < highestValue) && (it.value() > 0)) || highestValue == 0) {
highestValue = it.value();
winningSessionId = it.key();
}
}
sessionId = winningSessionId;
return highestValue > 0 ? highestValue : defaultInterval();
}
/*/////////////////////////////////////////////////////////////////////
/// \brief HybrisAdaptorReader::HybrisAdaptorReader
/// \param parent
///
*/
HybrisAdaptorReader::HybrisAdaptorReader(QObject *parent)
: QThread(parent),
running_(false)
{
}
HybrisAdaptorReader::~HybrisAdaptorReader()
{
}
////
/// \brief HybrisAdaptorReader::stopReader
///
void HybrisAdaptorReader::stopReader()
{
running_ = false;
}
void HybrisAdaptorReader::startReader()
{
running_ = true;
start();
}
void HybrisAdaptorReader::run()
{
int err = 0;
static const size_t numEvents = 16;
sensors_event_t buffer[numEvents];
while (running_) {
int numberOfEvents = hybrisManager()->device->poll(hybrisManager()->device, buffer, numEvents);
if (numberOfEvents < 0) {
sensordLogW() << "poll() failed" << strerror(-err);
QThread::msleep(1000);
} else {
bool errorInInput = false;
for (int i = 0; i < numberOfEvents; i++) {
const sensors_event_t& data = buffer[i];
if (data.version != sizeof(sensors_event_t)) {
sensordLogW()<< QString("incorrect event version (version=%1, expected=%2").arg(data.version).arg(sizeof(sensors_event_t));
errorInInput = true;
}
hybrisManager()->processSample(data);
}
if (errorInInput)
QThread::msleep(50);
}
}
sensordLogT() << Q_FUNC_INFO << "runner thread end";
}
////////////////////////////////////////////////////////