/
abstractsensor_i.cpp
551 lines (460 loc) · 17.3 KB
/
abstractsensor_i.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
/**
@file abstractsensor_i.cpp
@brief Base class for sensor interface
<p>
Copyright (C) 2009-2010 Nokia Corporation
@author Joep van Gassel <joep.van.gassel@nokia.com>
@author Timo Rongas <ext-timo.2.rongas@nokia.com>
@author Antti Virtanen <antti.i.virtanen@nokia.com>
@author Lihan Guo <ext-lihan.4.guo@nokia.com>
This file is part of Sensord.
Sensord is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Sensord is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Sensord. If not, see <http://www.gnu.org/licenses/>.
</p>
*/
#include "sensormanagerinterface.h"
#include "abstractsensor_i.h"
#ifdef SENSORFW_MCE_WATCHER
#include "mcewatcher.h"
#endif
#ifdef SENSORFW_LUNA_SERVICE_CLIENT
#include "lsclient.h"
#endif
struct AbstractSensorChannelInterface::AbstractSensorChannelInterfaceImpl : public QDBusAbstractInterface
{
AbstractSensorChannelInterfaceImpl(QObject* parent, int sessionId, const QString& path, const char* interfaceName);
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
void dbusConnectNotify(const char* signal) { QDBusAbstractInterface::connectNotify(signal); }
#else
void dbusConnectNotify(const QMetaMethod& signal) { QDBusAbstractInterface::connectNotify(signal); }
#endif
SensorError errorCode_;
QString errorString_;
int sessionId_;
int interval_;
unsigned int bufferInterval_;
unsigned int bufferSize_;
SocketReader socketReader_;
bool running_;
bool standbyOverride_;
bool downsampling_;
};
AbstractSensorChannelInterface::AbstractSensorChannelInterfaceImpl::AbstractSensorChannelInterfaceImpl(QObject* parent, int sessionId, const QString& path, const char* interfaceName) :
QDBusAbstractInterface(SERVICE_NAME, path, interfaceName, QDBusConnection::systemBus(), 0),
errorCode_(SNoError),
errorString_(""),
sessionId_(sessionId),
interval_(0),
bufferInterval_(0),
bufferSize_(1),
socketReader_(parent),
running_(false),
standbyOverride_(false),
downsampling_(true)
{
}
AbstractSensorChannelInterface::AbstractSensorChannelInterface(const QString& path, const char* interfaceName, int sessionId) :
pimpl_(new AbstractSensorChannelInterfaceImpl(this, sessionId, path, interfaceName))
{
if (!pimpl_->socketReader_.initiateConnection(sessionId)) {
setError(SClientSocketError, "Socket connection failed.");
}
#ifdef SENSORFW_MCE_WATCHER
MceWatcher *mcewatcher;
mcewatcher = new MceWatcher(this);
QObject::connect(mcewatcher,SIGNAL(displayStateChanged(bool)),
this,SLOT(displayStateChanged(bool)),Qt::UniqueConnection);
#endif
#ifdef SENSORFW_LUNA_SERVICE
LSClient *lsclient;
lsclient = new LSClient(this);
QObject::connect(lsclient,SIGNAL(displayStateChanged(bool)),
this,SLOT(displayStateChanged(bool)),Qt::UniqueConnection);
#endif
}
AbstractSensorChannelInterface::~AbstractSensorChannelInterface()
{
if ( pimpl_->isValid() )
SensorManagerInterface::instance().releaseInterface(id(), pimpl_->sessionId_);
if (!pimpl_->socketReader_.dropConnection())
setError(SClientSocketError, "Socket disconnect failed.");
delete pimpl_;
}
SocketReader& AbstractSensorChannelInterface::getSocketReader() const
{
return pimpl_->socketReader_;
}
bool AbstractSensorChannelInterface::release()
{
return true;
}
void AbstractSensorChannelInterface::setError(SensorError errorCode, const QString& errorString)
{
pimpl_->errorCode_ = errorCode;
pimpl_->errorString_ = errorString;
}
QDBusReply<void> AbstractSensorChannelInterface::start()
{
return start(pimpl_->sessionId_);
}
QDBusReply<void> AbstractSensorChannelInterface::stop()
{
return stop(pimpl_->sessionId_);
}
QDBusReply<void> AbstractSensorChannelInterface::start(int sessionId)
{
clearError();
if (pimpl_->running_) {
return QDBusReply<void>();
}
pimpl_->running_ = true;
connect(pimpl_->socketReader_.socket(), SIGNAL(readyRead()), this, SLOT(dataReceived()));
QList<QVariant> argumentList;
argumentList << qVariantFromValue(sessionId);
QDBusPendingReply <void> returnValue = pimpl_->asyncCallWithArgumentList(QLatin1String("start"), argumentList);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(returnValue, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(startFinished(QDBusPendingCallWatcher*)));
setStandbyOverride(sessionId, pimpl_->standbyOverride_);
setInterval(sessionId, pimpl_->interval_);
setBufferInterval(sessionId, pimpl_->bufferInterval_);
setBufferSize(sessionId, pimpl_->bufferSize_);
setDownsampling(pimpl_->sessionId_, pimpl_->downsampling_);
return returnValue;
}
void AbstractSensorChannelInterface::startFinished(QDBusPendingCallWatcher *watch)
{
watch->deleteLater();
QDBusPendingReply<void> reply = *watch;
if(reply.isError()) {
qDebug() << reply.error().message();
setError(SHwSensorStartFailed, reply.error().message());
}
}
QDBusReply<void> AbstractSensorChannelInterface::stop(int sessionId)
{
clearError();
if (!pimpl_->running_) {
return QDBusReply<void>();
}
pimpl_->running_ = false ;
disconnect(pimpl_->socketReader_.socket(), SIGNAL(readyRead()), this, SLOT(dataReceived()));
QList<QVariant> argumentList;
argumentList << qVariantFromValue(sessionId);
QDBusPendingReply <void> returnValue = pimpl_->asyncCallWithArgumentList(QLatin1String("stop"), argumentList);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(returnValue, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(stopFinished(QDBusPendingCallWatcher*)));
return returnValue;
}
void AbstractSensorChannelInterface::stopFinished(QDBusPendingCallWatcher *watch)
{
watch->deleteLater();
QDBusPendingReply<void> reply = *watch;
if(reply.isError()) {
qDebug() << reply.error().message();
setError(SaCannotAccessSensor, reply.error().message());
}
}
QDBusReply<void> AbstractSensorChannelInterface::setInterval(int sessionId, int value)
{
clearError();
QList<QVariant> argumentList;
argumentList << qVariantFromValue(sessionId) << qVariantFromValue(value);
QDBusPendingReply <void> returnValue = pimpl_->asyncCallWithArgumentList(QLatin1String("setInterval"), argumentList);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(returnValue, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(setIntervalFinished(QDBusPendingCallWatcher*)));
return returnValue;
}
void AbstractSensorChannelInterface::setIntervalFinished(QDBusPendingCallWatcher *watch)
{
watch->deleteLater();
QDBusPendingReply<void> reply = *watch;
if(reply.isError()) {
qDebug() << reply.error().message();
setError(SaCannotAccessSensor, reply.error().message());
}
}
QDBusReply<void> AbstractSensorChannelInterface::setBufferInterval(int sessionId, unsigned int value)
{
clearError();
QList<QVariant> argumentList;
argumentList << qVariantFromValue(sessionId) << qVariantFromValue(value);
QDBusPendingReply <void> returnValue = pimpl_->asyncCallWithArgumentList(QLatin1String("setBufferInterval"), argumentList);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(returnValue, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(setBufferIntervalFinished(QDBusPendingCallWatcher*)));
return returnValue;
}
void AbstractSensorChannelInterface::setBufferIntervalFinished(QDBusPendingCallWatcher *watch)
{
watch->deleteLater();
QDBusPendingReply<void> reply = *watch;
if(reply.isError()) {
qDebug() << reply.error().message();
setError(SaCannotAccessSensor, reply.error().message());
}
}
QDBusReply<void> AbstractSensorChannelInterface::setBufferSize(int sessionId, unsigned int value)
{
clearError();
QList<QVariant> argumentList;
argumentList << qVariantFromValue(sessionId) << qVariantFromValue(value);
QDBusPendingReply <void> returnValue = pimpl_->asyncCallWithArgumentList(QLatin1String("setBufferSize"), argumentList);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(returnValue, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(setBufferSizeFinished(QDBusPendingCallWatcher*)));
return returnValue;
}
void AbstractSensorChannelInterface::setBufferSizeFinished(QDBusPendingCallWatcher *watch)
{
watch->deleteLater();
QDBusPendingReply<void> reply = *watch;
if(reply.isError()) {
qDebug() << reply.error().message();
setError(SaCannotAccessSensor, reply.error().message());
}
}
QDBusReply<bool> AbstractSensorChannelInterface::setStandbyOverride(int sessionId, bool value)
{
clearError();
QList<QVariant> argumentList;
argumentList << qVariantFromValue(sessionId) << qVariantFromValue(value);
QDBusPendingReply <void> returnValue = pimpl_->asyncCallWithArgumentList(QLatin1String("setStandbyOverride"), argumentList);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(returnValue, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(setStandbyOverrideFinished(QDBusPendingCallWatcher*)));
return returnValue;
}
void AbstractSensorChannelInterface::setStandbyOverrideFinished(QDBusPendingCallWatcher *watch)
{
watch->deleteLater();
QDBusPendingReply<void> reply = *watch;
if(reply.isError()) {
qDebug() << reply.error().message();
setError(SaCannotAccessSensor, reply.error().message());
}
}
DataRangeList AbstractSensorChannelInterface::getAvailableDataRanges()
{
return getAccessor<DataRangeList>("getAvailableDataRanges");
}
DataRange AbstractSensorChannelInterface::getCurrentDataRange()
{
return getAccessor<DataRange>("getCurrentDataRange");
}
void AbstractSensorChannelInterface::requestDataRange(DataRange range)
{
clearError();
call(QDBus::NoBlock, QLatin1String("requestDataRange"), qVariantFromValue(pimpl_->sessionId_), qVariantFromValue(range));
}
void AbstractSensorChannelInterface::removeDataRangeRequest()
{
clearError();
call(QDBus::NoBlock, QLatin1String("removeDataRangeRequest"), qVariantFromValue(pimpl_->sessionId_));
}
DataRangeList AbstractSensorChannelInterface::getAvailableIntervals()
{
return getAccessor<DataRangeList>("getAvailableIntervals");
}
IntegerRangeList AbstractSensorChannelInterface::getAvailableBufferIntervals()
{
return getAccessor<IntegerRangeList>("getAvailableBufferIntervals");
}
IntegerRangeList AbstractSensorChannelInterface::getAvailableBufferSizes()
{
return getAccessor<IntegerRangeList>("getAvailableBufferSizes");
}
bool AbstractSensorChannelInterface::hwBuffering()
{
return getAccessor<bool>("hwBuffering");
}
int AbstractSensorChannelInterface::sessionId() const
{
return pimpl_->sessionId_;
}
SensorError AbstractSensorChannelInterface::errorCode()
{
if (pimpl_->errorCode_ != SNoError) {
return pimpl_->errorCode_;
}
return static_cast<SensorError>(getAccessor<int>("errorCodeInt"));
}
QString AbstractSensorChannelInterface::errorString()
{
if (pimpl_->errorCode_ != SNoError)
return pimpl_->errorString_;
return getAccessor<QString>("errorString");
}
QString AbstractSensorChannelInterface::description()
{
return getAccessor<QString>("description");
}
QString AbstractSensorChannelInterface::id()
{
return getAccessor<QString>("id");
}
int AbstractSensorChannelInterface::interval()
{
if (pimpl_->running_)
return static_cast<int>(getAccessor<unsigned int>("interval"));
return pimpl_->interval_;
}
void AbstractSensorChannelInterface::setInterval(int value)
{
pimpl_->interval_ = value;
if (pimpl_->running_)
setInterval(pimpl_->sessionId_, value);
}
unsigned int AbstractSensorChannelInterface::bufferInterval()
{
if (pimpl_->running_)
return getAccessor<unsigned int>("bufferInterval");
return pimpl_->bufferInterval_;
}
void AbstractSensorChannelInterface::setBufferInterval(unsigned int value)
{
pimpl_->bufferInterval_ = value;
if (pimpl_->running_)
setBufferInterval(pimpl_->sessionId_, value);
}
unsigned int AbstractSensorChannelInterface::bufferSize()
{
if (pimpl_->running_)
return getAccessor<unsigned int>("bufferSize");
return pimpl_->bufferSize_;
}
void AbstractSensorChannelInterface::setBufferSize(unsigned int value)
{
pimpl_->bufferSize_ = value;
if (pimpl_->running_)
setBufferSize(pimpl_->sessionId_, value);
}
bool AbstractSensorChannelInterface::standbyOverride()
{
if (pimpl_->running_)
return getAccessor<unsigned int>("standbyOverride");
return pimpl_->standbyOverride_;
}
bool AbstractSensorChannelInterface::setStandbyOverride(bool override)
{
pimpl_->standbyOverride_ = override;
return setStandbyOverride(pimpl_->sessionId_, override);
}
QString AbstractSensorChannelInterface::type()
{
return getAccessor<QString>("type");
}
void AbstractSensorChannelInterface::clearError()
{
pimpl_->errorCode_ = SNoError;
pimpl_->errorString_.clear();
}
void AbstractSensorChannelInterface::dataReceived()
{
do
{
if(!dataReceivedImpl())
return;
} while(pimpl_->socketReader_.socket()->bytesAvailable());
}
bool AbstractSensorChannelInterface::read(void* buffer, int size)
{
return pimpl_->socketReader_.read(buffer, size);
}
bool AbstractSensorChannelInterface::setDataRangeIndex(int dataRangeIndex)
{
clearError();
QList<QVariant> argumentList;
argumentList << qVariantFromValue(pimpl_->sessionId_) << qVariantFromValue(dataRangeIndex);
QDBusPendingReply <bool> returnValue = pimpl_->asyncCallWithArgumentList(QLatin1String("setDataRangeIndex"), argumentList);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(returnValue, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(setDataRangeIndexFinished(QDBusPendingCallWatcher*)));
return returnValue;
}
void AbstractSensorChannelInterface::setDataRangeIndexFinished(QDBusPendingCallWatcher *watch)
{
watch->deleteLater();
QDBusPendingReply<void> reply = *watch;
if(reply.isError()) {
qDebug() << reply.error().message();
setError(SaCannotAccessSensor, reply.error().message());
}
}
QDBusMessage AbstractSensorChannelInterface::call(QDBus::CallMode mode,
const QString& method,
const QVariant& arg1,
const QVariant& arg2,
const QVariant& arg3,
const QVariant& arg4,
const QVariant& arg5,
const QVariant& arg6,
const QVariant& arg7,
const QVariant& arg8)
{
return pimpl_->call(mode, method, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
QDBusMessage AbstractSensorChannelInterface::callWithArgumentList(QDBus::CallMode mode, const QString& method, const QList<QVariant>& args)
{
return pimpl_->call(mode, method, args);
}
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
void AbstractSensorChannelInterface::dbusConnectNotify(const char* signal)
#else
void AbstractSensorChannelInterface::dbusConnectNotify(const QMetaMethod& signal)
#endif
{
pimpl_->dbusConnectNotify(signal);
}
bool AbstractSensorChannelInterface::isValid() const
{
return pimpl_->isValid();
}
bool AbstractSensorChannelInterface::downsampling()
{
return pimpl_->downsampling_;
}
bool AbstractSensorChannelInterface::setDownsampling(bool value)
{
pimpl_->downsampling_ = value;
return setDownsampling(pimpl_->sessionId_, value).isValid();
}
QDBusReply<void> AbstractSensorChannelInterface::setDownsampling(int sessionId, bool value)
{
clearError();
QList<QVariant> argumentList;
argumentList << qVariantFromValue(sessionId) << qVariantFromValue(value);
QDBusPendingReply <void> returnValue = pimpl_->asyncCallWithArgumentList(QLatin1String("setDownsampling"), argumentList);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(returnValue, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(setDownsamplingFinished(QDBusPendingCallWatcher*)));
return returnValue;
}
void AbstractSensorChannelInterface::setDownsamplingFinished(QDBusPendingCallWatcher *watch)
{
watch->deleteLater();
QDBusPendingReply<void> reply = *watch;
if(reply.isError()) {
qDebug() << reply.error().message();
setError(SaCannotAccessSensor, reply.error().message());
}
}
void AbstractSensorChannelInterface::displayStateChanged(bool displayState)
{
if (!pimpl_->standbyOverride_) {
if (!displayState) {
stop();
} else {
start();
}
}
}