/
abstractsensor_i.h
524 lines (452 loc) · 14.2 KB
/
abstractsensor_i.h
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
/**
@file abstractsensor_i.h
@brief Base class for sensor interface
<p>
Copyright (C) 2009-2010 Nokia Corporation
@author Joep van Gassel <joep.van.gassel@nokia.com>
@author Semi Malinen <semi.malinen@nokia.com
@author Timo Rongas <ext-timo.2.rongas@nokia.com>
@author Antti Virtanen <antti.i.virtanen@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>
*/
#ifndef ABSTRACTSENSOR_I_H
#define ABSTRACTSENSOR_I_H
#include <QtDBus/QtDBus>
#include <QList>
#include <QVector>
#include <QString>
#include "sfwerror.h"
#include "serviceinfo.h"
#include "socketreader.h"
#include "datatypes/datarange.h"
/**
* Base-class for client facades of different sensor types.
*/
class AbstractSensorChannelInterface : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(AbstractSensorChannelInterface)
Q_PROPERTY(int sessionId READ sessionId)
Q_PROPERTY(SensorError errorCode READ errorCode)
Q_PROPERTY(QString errorString READ errorString)
Q_PROPERTY(QString description READ description)
Q_PROPERTY(QString id READ id)
Q_PROPERTY(int interval READ interval WRITE setInterval)
Q_PROPERTY(bool standbyOverride READ standbyOverride WRITE setStandbyOverride)
Q_PROPERTY(QString type READ type)
Q_PROPERTY(unsigned int bufferInterval READ bufferInterval WRITE setBufferInterval)
Q_PROPERTY(unsigned int bufferSize READ bufferSize WRITE setBufferSize)
Q_PROPERTY(bool hwBuffering READ hwBuffering)
Q_PROPERTY(bool downsampling READ downsampling WRITE setDownsampling)
public:
/**
* Destructor
*/
virtual ~AbstractSensorChannelInterface();
/**
* Release the sensor instance.
*
* @return true always.
* @deprecated Session lifecycle ends by destructing this object.
*/
bool release();
/**
* Get ID of the current session.
*
* @return current session ID.
*/
int sessionId() const;
/**
* Get error code of occured local or remote error.
*
* @return error code.
*/
SensorError errorCode();
/**
* Get error description of occured local or remote error.
*
* @return error description.
*/
QString errorString();
/**
* Get description of the sensor.
*
* @return description of the sensor.
*/
QString description();
/**
* Get ID of the sensor.
*
* @return ID of the sensor.
*/
QString id();
/**
* Get used sensor sampling interval (in millisecs).
* If sensor has been started (by #start()) effective interval is
* returned.
*
* @return used sampling interval (in millisecs)
*/
int interval();
/**
* Set sensor sampling interval (in millisecs).
* Value "0" will clear previously set interval.
* Supported intervals are listed by #getAvailableIntervals().
*
* @param value sampling interval (in millisecs).
*/
void setInterval(int value);
/**
* Is standby-override enabled or not.
* Standby-override is used to keep sensor enabled when display
* is blanked.
*
* @return enabled or not.
*/
bool standbyOverride();
/**
* Enable or disable standby-override.
* Standby-override is used to keep sensor enabled when display
* is blanked.
*
* @param override standby-override state
* @return true if succesfully set.
*/
bool setStandbyOverride(bool override);
/**
* Used buffer interval. Buffer interval defines the timeout for
* buffered data to be flushed unless the buffer is filled before it.
*
* @return interval in millisecs.
*/
unsigned int bufferInterval();
/**
* Set buffer interval. Buffer interval defines the timeout for
* buffered data to be flushed unless the buffer is filled before it.
* Supported intervals are listed by #getAvailableBufferIntervals().
*
* @param value interval in millisecs.
*/
void setBufferInterval(unsigned int value);
/**
* Is downsampling enabled or not.
* Downsampling is used to fit requested measurement interval
* #setInterval(int) to effective interval.
*
* @return downsampling state.
*/
bool downsampling();
/**
* Enable or disable downsampling.
* Downsampling is used to fit requested measurement interval
* #setInterval(int) to effective interval. Not all sensors support
* downsampling so the call will fail for those.
*
* @param value enable or disable downsampling.
* @return was downsampling state succesfully changed.
*/
bool setDownsampling(bool value);
/**
* Returns list of available buffer interval ranges.
*
* @return The list of supported buffer interval ranges.
*/
IntegerRangeList getAvailableBufferIntervals();
/**
* Get used buffer size. Buffer size is used to control how many
* samples are collected before signaling application about them.
*
* @return used buffer size.
*/
unsigned int bufferSize();
/**
* Set buffer size. Buffer size is used to control how many
* samples are collected before signaling application about them.
*
* @param value buffer size.
*/
void setBufferSize(unsigned int value);
/**
* Returns list of available buffer sizes. The list is ordered by
* efficiency of the size.
*
* @return The list of supported buffer sizes ordered by efficiency.
*/
IntegerRangeList getAvailableBufferSizes();
/**
* Textual description about sensor type.
*
* @return sensor type description.
*/
QString type();
/**
* Start sensor. This will cause necessary resources to be
* acquired so the sensor readings can be received.
*
* @return object from which the success of call can be seen.
*/
virtual QDBusReply<void> start();
/**
* Stop sensor. This will cause acquired resourced to be released.
*
* @return object from which the success of call can be seen.
*/
virtual QDBusReply<void> stop();
/**
* Get the list of available intervals ranges for the sensor.
*
* @return List of available intervals ranges.
*/
DataRangeList getAvailableIntervals();
/**
* Get the list of available data ranges for the sensor.
*
* @return List of available data ranges.
*/
DataRangeList getAvailableDataRanges();
/**
* Get current used data range.
*
* @return currently used data range.
*/
DataRange getCurrentDataRange();
/**
* Request data range. Usable ranges are listed by
* #getAvailableDataRanges().
*
* @param range requested range.
*/
void requestDataRange(DataRange range);
/**
* Remove set data range request.
*/
void removeDataRangeRequest();
/**
* Request data range by using index which points to the list
* returned by #getAvailableDataRanges().
*
* @param dataRangeIndex requeted data range index.
* @return false if given index is invalid.
*/
bool setDataRangeIndex(int dataRangeIndex);
/**
* Does the sensor driver support buffering or not.
*
* @return Does the sensor driver support buffering or not.
*/
bool hwBuffering();
/**
* Does the current instance have valid connection established
* to sensor daemon.
*
* @return is the established connection valid.
*/
bool isValid() const;
private:
/**
* Set error information.
*
* @param errorCode error code.
* @param errorString error description.
*/
void setError(SensorError errorCode, const QString& errorString);
/**
* Clear set error information.
*/
void clearError();
/**
* Get socket reader instance.
*/
SocketReader& getSocketReader() const;
private Q_SLOTS: // METHODS
void displayStateChanged(bool displayState);
/**
* Set interval to session.
*
* @param sessionId session ID.
* @param value interval.
* @return DBus reply.
*/
QDBusReply<void> setInterval(int sessionId, int value);
/**
* Set standby-override to session.
*
* @param sessionId session ID.
* @param value standby-override.
* @return DBus reply.
*/
QDBusReply<bool> setStandbyOverride(int sessionId, bool value);
/**
* Set buffer interval to session.
*
* @param sessionId session ID.
* @param value buffer interval.
* @return DBus reply.
*/
QDBusReply<void> setBufferInterval(int sessionId, unsigned int value);
/**
* Set buffer size to session.
*
* @param sessionId session ID.
* @param value buffer size.
* @return DBus reply.
*/
QDBusReply<void> setBufferSize(int sessionId, unsigned int value);
/**
* Set downsampling to session.
*
* @param sessionId session ID.
* @param value downsampling.
* @return DBus reply.
*/
QDBusReply<void> setDownsampling(int sessionId, bool value);
/**
* Start sensor for session.
*
* @param sessionId session ID.
* @return DBus reply.
*/
QDBusReply<void> start(int sessionId);
/**
* Stop sensor for session.
*
* @param sessionId session ID.
* @return DBus reply.
*/
QDBusReply<void> stop(int sessionId);
/**
* Callback for pending data in data socket.
*/
void dataReceived();
protected:
/**
* Constructor.
*
* @param path DBus object path.
* @param interfaceName DBus object name.
* @param sessionId Session ID.
*/
AbstractSensorChannelInterface(const QString& path, const char* interfaceName, int sessionId);
/**
* Read data from socket into buffer.
*
* @param buffer Pointer to buffer where to write.
* @param size Number of bytes to read.
*/
bool read(void* buffer, int size);
/**
* Read data from socket into passed container.
*
* @tparam Type to which to convert raw data.
* @param values Container for data.
* @return was read successful.
*/
template<typename T>
bool read(QVector<T>& values);
/**
* Callback for subclasses in which they must read their expected data
* from socket.
*
* @return was read successful.
*/
virtual bool dataReceivedImpl() = 0;
/**
* Utility for calling DBus methods from current connection which
* return value and take no args.
*
* @tparam return type.
* @param name method name.
* @return called method return value.
*/
template<typename T>
T getAccessor(const char* name);
/**
* Utility for calling DBus methods from current connection which
* return nothing and take one arg.
*
* @tparam argument type.
* @param name method name.
* @param value method argument.
*/
template<typename T>
void setAccessor(const char* name, const T& value);
/**
* Wrapper for function of QDBusAbstractInterface.
* For more details see QDBusAbstractInterface docs.
*/
QDBusMessage call(QDBus::CallMode mode,
const QString& method,
const QVariant& arg1 = QVariant(),
const QVariant& arg2 = QVariant(),
const QVariant& arg3 = QVariant(),
const QVariant& arg4 = QVariant(),
const QVariant& arg5 = QVariant(),
const QVariant& arg6 = QVariant(),
const QVariant& arg7 = QVariant(),
const QVariant& arg8 = QVariant());
/**
* Wrapper for function of QDBusAbstractInterface.
* For more details see QDBusAbstractInterface docs.
*/
QDBusMessage callWithArgumentList(QDBus::CallMode mode, const QString& method, const QList<QVariant>& args);
/**
* Wrapper for function of QDBusAbstractInterface.
* For more details see QDBusAbstractInterface docs.
*/
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
void dbusConnectNotify(const char* signal);
#else
void dbusConnectNotify(const QMetaMethod& signal);
#endif
protected slots:
void startFinished(QDBusPendingCallWatcher *watch);
void stopFinished(QDBusPendingCallWatcher *watch);
void setIntervalFinished(QDBusPendingCallWatcher *watch);
void setBufferIntervalFinished(QDBusPendingCallWatcher *watch);
void setBufferSizeFinished(QDBusPendingCallWatcher *watch);
void setStandbyOverrideFinished(QDBusPendingCallWatcher *watch);
void setDownsamplingFinished(QDBusPendingCallWatcher *watch);
void setDataRangeIndexFinished(QDBusPendingCallWatcher *watch);
private:
struct AbstractSensorChannelInterfaceImpl;
AbstractSensorChannelInterfaceImpl* pimpl_; /**< private impl pointer */
};
template<typename T>
bool AbstractSensorChannelInterface::read(QVector<T>& values)
{
return getSocketReader().read(values);
}
template<typename T>
T AbstractSensorChannelInterface::getAccessor(const char* name)
{
QDBusReply<T> reply(call(QDBus::Block, QLatin1String(name)));
if(!reply.isValid())
{
qDebug() << "Failed to get '" << name << "' from sensord: " << reply.error().message();
return T();
}
return reply.value();
}
template<typename T>
void AbstractSensorChannelInterface::setAccessor(const char* name, const T& value)
{
QDBusReply<void> reply(call(QDBus::NoBlock, QLatin1String(name), qVariantFromValue(value)));
if(!reply.isValid())
{
qDebug() << "Failed to set '" << name << " = " << value << "' to sensord: " << reply.error().message();
}
}
namespace local {
typedef ::AbstractSensorChannelInterface AbstractSensor;
}
#endif