/
sensormanager.cpp
893 lines (753 loc) · 25.2 KB
/
sensormanager.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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/**
@file sensormanager.cpp
@brief SensorManager
<p>
Copyright (C) 2009-2010 Nokia Corporation
@author Semi Malinen <semi.malinen@nokia.com
@author Joep van Gassel <joep.van.gassel@nokia.com>
@author Timo Rongas <ext-timo.2.rongas@nokia.com>
@author Ustun Ergenoglu <ext-ustun.ergenoglu@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 "sensormanager_a.h"
#include "serviceinfo.h"
#include "sensormanager.h"
#include "loader.h"
#include "idutils.h"
#include "logging.h"
#ifdef SENSORFW_MCE_WATCHER
#include "mcewatcher.h"
#endif // SENSORFW_MCE_WATCHER
#ifdef SENSORFW_LUNA_SERVICE_CLIENT
#include "lsclient.h"
#endif // SENSORFW_LUNA_SERVICE_CLIENT
#include <QSocketNotifier>
#include <errno.h>
#include "sockethandler.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <QSettings>
typedef struct {
int id;
int size;
void* buffer;
} PipeData;
SensorManager* SensorManager::instance_ = NULL;
int SensorManager::sessionIdCount_ = 0;
SensorInstanceEntry::SensorInstanceEntry(const QString& type) :
sensor_(0),
type_(type)
{
}
SensorInstanceEntry::~SensorInstanceEntry()
{
}
ChainInstanceEntry::ChainInstanceEntry(const QString& type) :
cnt_(0),
chain_(0),
type_(type)
{
}
ChainInstanceEntry::~ChainInstanceEntry()
{
}
DeviceAdaptorInstanceEntry::DeviceAdaptorInstanceEntry(const QString& type, const QString& id) :
adaptor_(0),
cnt_(0),
type_(type)
{
propertyMap_ = ParameterParser::getPropertyMap(id);
}
DeviceAdaptorInstanceEntry::~DeviceAdaptorInstanceEntry()
{
}
inline QDBusConnection bus()
{
return QDBusConnection::systemBus();
}
SensorManager& SensorManager::instance()
{
if ( !instance_ )
{
instance_ = new SensorManager;
}
return *instance_;
}
SensorManager::SensorManager()
: errorCode_(SmNoError),
pipeNotifier_(0),
deviation(0)
{
QString pluginPath;
const char* SOCKET_NAME = "/var/run/sensord.sock";
QByteArray env = qgetenv("SENSORFW_SOCKET_PATH");
if (!env.isEmpty()) {
env += SOCKET_NAME;
SOCKET_NAME = env;
}
new SensorManagerAdaptor(this);
socketHandler_ = new SocketHandler(this);
connect(socketHandler_, SIGNAL(lostSession(int)), this, SLOT(lostClient(int)));
Q_ASSERT(socketHandler_->listen(SOCKET_NAME));
if (pipe(pipefds_) == -1) {
sensordLogC() << "Failed to create pipe: " << strerror(errno);
pipefds_[0] = pipefds_[1] = 0;
} else {
pipeNotifier_ = new QSocketNotifier(pipefds_[0], QSocketNotifier::Read);
connect(pipeNotifier_, SIGNAL(activated(int)), this, SLOT(sensorDataHandler(int)));
}
if (chmod(SOCKET_NAME, S_IRWXU|S_IRWXG|S_IRWXO) != 0) {
sensordLogW() << "Error setting socket permissions! " << SOCKET_NAME;
}
#ifdef SENSORFW_MCE_WATCHER
mceWatcher_ = new MceWatcher(this);
connect(mceWatcher_, SIGNAL(displayStateChanged(const bool)),
this, SLOT(displayStateChanged(const bool)));
connect(mceWatcher_, SIGNAL(devicePSMStateChanged(const bool)),
this, SLOT(devicePSMStateChanged(const bool)));
#endif //SENSORFW_MCE_WATCHER
#ifdef SENSORFW_LUNA_SERVICE_CLIENT
lsClient_ = new LSClient(this);
connect(lsClient_, SIGNAL(displayStateChanged(const bool)),
this, SLOT(displayStateChanged(const bool)));
connect(lsClient_, SIGNAL(devicePSMStateChanged(const bool)),
this, SLOT(devicePSMStateChanged(const bool)));
#endif //SENSORFW_LUNA_SERVICE_CLIENT
}
SensorManager::~SensorManager()
{
// stop adaptor threads and acquired resources
for(QMap<QString, DeviceAdaptorInstanceEntry>::const_iterator it = deviceAdaptorInstanceMap_.begin(); it != deviceAdaptorInstanceMap_.end(); ++it)
{
releaseDeviceAdaptor(it.key());
}
sleep(1); // sleep for seconds so adaptor threads have time to die
// close open sessions
for(QMap<QString, SensorInstanceEntry>::const_iterator it = sensorInstanceMap_.begin(); it != sensorInstanceMap_.end(); ++it)
{
for(QSet<int>::const_iterator it2 = it.value().sessions_.begin(); it2 != it.value().sessions_.end(); ++it2)
{
lostClient(*it2);
}
}
// delete sensors
for(QMap<QString, SensorInstanceEntry>::iterator it = sensorInstanceMap_.begin(); it != sensorInstanceMap_.end(); ++it)
{
if(it.value().sensor_)
{
delete it.value().sensor_;
it.value().sensor_ = 0;
}
}
// delete chains
for(QMap<QString, ChainInstanceEntry>::iterator it = chainInstanceMap_.begin(); it != chainInstanceMap_.end(); ++it)
{
if(it.value().chain_)
{
delete it.value().chain_;
it.value().chain_ = 0;
}
}
// delete adaptors
for(QMap<QString, DeviceAdaptorInstanceEntry>::iterator it = deviceAdaptorInstanceMap_.begin(); it != deviceAdaptorInstanceMap_.end(); ++it)
{
if(it.value().adaptor_)
{
delete it.value().adaptor_;
it.value().adaptor_ = 0;
}
}
delete socketHandler_;
delete pipeNotifier_;
if (pipefds_[0]) close(pipefds_[0]);
if (pipefds_[1]) close(pipefds_[1]);
#ifdef SENSORFW_MCE_WATCHER
delete mceWatcher_;
#endif //SENSORFW_MCE_WATCHER
#ifdef SENSORFW_LUNA_SERVICE_CLIENT
delete lsClient_;
#endif //SENSORFW_LUNA_SERVICE_CLIENT
}
void SensorManager::setError(SensorManagerError errorCode, const QString& errorString)
{
sensordLogW() << "SensorManagerError: " << errorString;
errorCode_ = errorCode;
errorString_ = errorString;
emit errorSignal(errorCode);
}
bool SensorManager::registerService()
{
clearError();
bool ok = bus().isConnected();
if ( !ok )
{
QDBusError error = bus().lastError();
setError(SmNotConnected, error.message());
return false;
}
ok = bus().registerObject( OBJECT_PATH, this );
if ( !ok )
{
QDBusError error = bus().lastError();
setError(SmCanNotRegisterObject, error.message());
return false;
}
ok = bus().registerService ( SERVICE_NAME );
if ( !ok )
{
QDBusError error = bus().lastError();
setError(SmCanNotRegisterService, error.message());
return false;
}
return true;
}
AbstractSensorChannel* SensorManager::addSensor(const QString& id)
{
sensordLogD() << "Adding sensor: " << id;
clearError();
QString cleanId = getCleanId(id);
QMap<QString, SensorInstanceEntry>::const_iterator entryIt = sensorInstanceMap_.find(cleanId);
if (entryIt == sensorInstanceMap_.end()) {
sensordLogC() << QString("%1 not present").arg(cleanId);
setError( SmIdNotRegistered, QString(tr("instance for sensor type '%1' not registered").arg(cleanId)) );
return NULL;
}
const QString& typeName = entryIt.value().type_;
if ( !sensorFactoryMap_.contains(typeName) )
{
setError( SmFactoryNotRegistered, QString(tr("factory for sensor type '%1' not registered").arg(typeName)) );
return NULL;
}
AbstractSensorChannel* sensorChannel = sensorFactoryMap_[typeName](id);
if ( !sensorChannel->isValid() )
{
sensordLogC() << QString("%1 instantiation failed").arg(cleanId);
delete sensorChannel;
removeSensor(getCleanId(id));
sensorFactoryMap_.remove(id);
return NULL;
}
bool ok = bus().registerObject(OBJECT_PATH + "/" + sensorChannel->id(), sensorChannel);
if ( !ok )
{
QDBusError error = bus().lastError();
setError(SmCanNotRegisterObject, error.message());
sensordLogC() << "Failed to register sensor '" << OBJECT_PATH + "/" + sensorChannel->id() << "'";
delete sensorChannel;
return NULL;
}
return sensorChannel;
}
void SensorManager::removeSensor(const QString& id)
{
sensordLogD() << "SensorManager removing sensor:" << id;
QMap<QString, SensorInstanceEntry>::iterator entryIt = sensorInstanceMap_.find(id);
bus().unregisterObject(OBJECT_PATH + "/" + id);
delete entryIt.value().sensor_;
entryIt.value().sensor_ = 0;
sensorInstanceMap_.remove(id);
}
bool SensorManager::loadPlugin(const QString& name)
{
sensordLogD() << "SensorManager loading plugin:" << name;
QString errorMessage;
bool result;
Loader& l = Loader::instance();
if (! (result = l.loadPlugin(name, &errorMessage))) {
setError (SmCanNotRegisterObject, errorMessage);
}
return result;
}
QStringList SensorManager::availablePlugins() const
{
Loader& l = Loader::instance();
return l.availablePlugins();
}
bool SensorManager::pluginAvailable(const QString &name) const
{
Loader& l = Loader::instance();
return l.pluginAvailable(name);
}
QStringList SensorManager::availableSensorPlugins() const
{
Loader& l = Loader::instance();
return l.availableSensorPlugins();
}
int SensorManager::requestSensor(const QString& id)
{
sensordLogD() << "Requesting sensor:" << id;
clearError();
QString cleanId = getCleanId(id);
qDebug() << sensorInstanceMap_.keys();
QMap<QString, SensorInstanceEntry>::iterator entryIt = sensorInstanceMap_.find(cleanId);
if ( entryIt == sensorInstanceMap_.end() )
{
setError(SmIdNotRegistered, QString(tr("requested sensor id '%1' not registered")).arg(cleanId));
return INVALID_SESSION;
}
int sessionId = createNewSessionId();
if(!entryIt.value().sensor_)
{
AbstractSensorChannel* sensor = addSensor(id);
if ( sensor == NULL )
{
setError(SmNotInstantiated, tr("sensor has not been instantiated"));
return INVALID_SESSION;
}
entryIt.value().sensor_ = sensor;
}
entryIt.value().sessions_.insert(sessionId);
return sessionId;
}
bool SensorManager::releaseSensor(const QString& id, int sessionId)
{
sensordLogD() << "Releasing sensor '" << id << "' for session: " << sessionId;
clearError();
if( id.contains(';') ) // no parameter passing in release
{
sensordLogW() << "Invalid parameter passed to releaseSensor(): " << id;
return false;
}
QMap<QString, SensorInstanceEntry>::iterator entryIt = sensorInstanceMap_.find(id);
if ( entryIt == sensorInstanceMap_.end() )
{
setError( SmIdNotRegistered, QString(tr("requested sensor id '%1' not registered").arg(id)) );
return false;
}
/// Remove any property requests by this session
entryIt.value().sensor_->removeSession(sessionId);
if (entryIt.value().sessions_.empty())
{
setError(SmNotInstantiated, tr("sensor has not been instantiated, no session to release"));
return false;
}
bool returnValue = false;
if(entryIt.value().sessions_.remove( sessionId ))
{
/** Fix for NB#242237
if ( entryIt.value().sessions_.empty() )
{
removeSensor(id);
}
*/
returnValue = true;
}
else
{
// sessionId does not correspond to a request
setError( SmNotInstantiated, tr("invalid sessionId, no session to release") );
}
socketHandler_->removeSession(sessionId);
return returnValue;
}
AbstractChain* SensorManager::requestChain(const QString& id)
{
sensordLogD() << "Requesting chain: " << id;
clearError();
AbstractChain* chain = NULL;
QMap<QString, ChainInstanceEntry>::iterator entryIt = chainInstanceMap_.find(id);
if (entryIt != chainInstanceMap_.end())
{
if (entryIt.value().chain_ )
{
chain = entryIt.value().chain_;
entryIt.value().cnt_++;
sensordLogD() << "Found chain '" << id << "'. Ref count: " << entryIt.value().cnt_;
}
else
{
QString type = entryIt.value().type_;
if (chainFactoryMap_.contains(type))
{
chain = chainFactoryMap_[type](id);
Q_ASSERT(chain);
sensordLogD() << "Instantiated chain '" << id << "'. Valid =" << chain->isValid();
entryIt.value().cnt_++;
entryIt.value().chain_ = chain;
}
else
{
setError( SmFactoryNotRegistered, QString(tr("unknown chain type '%1'").arg(type)) );
}
}
}
else
{
setError( SmIdNotRegistered, QString(tr("unknown chain id '%1'").arg(id)) );
}
return chain;
}
void SensorManager::releaseChain(const QString& id)
{
sensordLogD() << "Releasing chain: " << id;
clearError();
QMap<QString, ChainInstanceEntry>::iterator entryIt = chainInstanceMap_.find(id);
if (entryIt != chainInstanceMap_.end())
{
if (entryIt.value().chain_)
{
entryIt.value().cnt_--;
/** Fix for NB#242237
if (entryIt.value().cnt_ == 0)
*/
if (false)
{
sensordLogD() << "Chain '" << id << "' has no more references. Deleting it.";
delete entryIt.value().chain_;
entryIt.value().chain_ = 0;
}
else
{
sensordLogD() << "Chain '" << id << "' ref count: " << entryIt.value().cnt_;
}
}
else
{
setError( SmNotInstantiated, QString(tr("chain '%1' not instantiated, cannot release").arg(id)) );
}
}
else
{
setError( SmIdNotRegistered, QString(tr("unknown chain id '%1'").arg(id)) );
}
}
DeviceAdaptor* SensorManager::requestDeviceAdaptor(const QString& id)
{
sensordLogD() << "Requesting adaptor:" << id;
clearError();
if( id.contains(';') ) // no parameter passing in release
{
setError( SmIdNotRegistered, QString(tr("unknown adaptor id '%1'").arg(id)) );
return Q_NULLPTR;
}
DeviceAdaptor* da = NULL;
QMap<QString, DeviceAdaptorInstanceEntry>::iterator entryIt = deviceAdaptorInstanceMap_.find(id);
if ( entryIt != deviceAdaptorInstanceMap_.end() )
{
if ( entryIt.value().adaptor_ )
{
Q_ASSERT( entryIt.value().adaptor_ );
da = entryIt.value().adaptor_;
entryIt.value().cnt_++;
sensordLogD() << "Found adaptor '" << id << "'. Ref count:" << entryIt.value().cnt_;
}
else
{
QString type = entryIt.value().type_;
if ( deviceAdaptorFactoryMap_.contains(type) )
{
da = deviceAdaptorFactoryMap_[type](id);
Q_ASSERT( da );
bool ok = da->isValid();
if (ok) {
da->init();
ParameterParser::applyPropertyMap(da, entryIt.value().propertyMap_);
ok = da->startAdaptor();
}
if (ok)
{
entryIt.value().adaptor_ = da;
entryIt.value().cnt_++;
sensordLogD() << "Instantiated adaptor '" << id << "'. Valid =" << da->isValid();
}
else
{
setError(SmAdaptorNotStarted, QString(tr("adaptor '%1' can not be started").arg(id)) );
delete da;
da = NULL;
}
}
else
{
setError( SmFactoryNotRegistered, QString(tr("unknown adaptor type '%1'").arg(type)) );
}
}
}
else
{
setError( SmIdNotRegistered, QString(tr("unknown adaptor id '%1'").arg(id)) );
}
return da;
}
void SensorManager::releaseDeviceAdaptor(const QString& id)
{
sensordLogD() << "Releasing adaptor:" << id;
clearError();
if( id.contains(';') ) // no parameter passing in release
{
setError( SmIdNotRegistered, QString(tr("unknown adaptor id '%1'").arg(id)) );
return;
}
QMap<QString, DeviceAdaptorInstanceEntry>::iterator entryIt = deviceAdaptorInstanceMap_.find(id);
if ( entryIt != deviceAdaptorInstanceMap_.end() )
{
if ( entryIt.value().adaptor_ )
{
Q_ASSERT( entryIt.value().adaptor_ );
entryIt.value().cnt_--;
if ( entryIt.value().cnt_ == 0 )
{
sensordLogD() << "Adaptor '" << id << "' has no more references.";
Q_ASSERT( entryIt.value().adaptor_ );
entryIt.value().adaptor_->stopAdaptor();
/** Fix for NB#242237
delete entryIt.value().adaptor_;
entryIt.value().adaptor_ = 0;
*/
}
else
{
sensordLogD() << "Adaptor '" << id << "' has ref count:" << entryIt.value().cnt_;
}
}
else
{
setError( SmNotInstantiated, QString(tr("adaptor '%1' not instantiated, cannot release").arg(id)) );
}
}
else
{
setError( SmIdNotRegistered, QString(tr("unknown adaptor id '%1'").arg(id)) );
}
}
FilterBase* SensorManager::instantiateFilter(const QString& id)
{
sensordLogD() << "Instantiating filter:" << id;
QMap<QString, FilterFactoryMethod>::iterator it = filterFactoryMap_.find(id);
if(it == filterFactoryMap_.end())
{
sensordLogW() << "Filter " << id << " not found.";
return NULL;
}
return it.value()();
}
bool SensorManager::write(int id, const void* source, int size)
{
void* buffer = malloc(size);
if(!buffer) {
sensordLogC() << "Malloc failed!";
return false;
}
PipeData pipeData;
pipeData.id = id;
pipeData.size = size;
pipeData.buffer = buffer;
memcpy(buffer, source, size);
if (::write(pipefds_[1], &pipeData, sizeof(pipeData)) < (int)sizeof(pipeData)) {
sensordLogW() << "Failed to write all data to pipe.";
return false;
}
return true;
}
void SensorManager::sensorDataHandler(int)
{
PipeData pipeData;
ssize_t bytesRead = read(pipefds_[0], &pipeData, sizeof(pipeData));
if (!bytesRead || !socketHandler_->write(pipeData.id, pipeData.buffer, pipeData.size)) {
sensordLogW() << "Failed to write data to socket.";
}
free(pipeData.buffer);
}
void SensorManager::lostClient(int sessionId)
{
for(QMap<QString, SensorInstanceEntry>::iterator it = sensorInstanceMap_.begin(); it != sensorInstanceMap_.end(); ++it) {
if (it.value().sessions_.contains(sessionId)) {
sensordLogD() << "[SensorManager]: Lost session " << sessionId << " detected as " << it.key();
sensordLogD() << "[SensorManager]: Stopping sessionId " << sessionId;
it.value().sensor_->stop(sessionId);
sensordLogD() << "[SensorManager]: Releasing sessionId " << sessionId;
releaseSensor(it.key(), sessionId);
return;
}
}
sensordLogW() << "[SensorManager]: Lost session " << sessionId << " detected, but not found from session list";
}
void SensorManager::displayStateChanged(bool displayState)
{
sensordLogD() << "Signal detected, display state changed to:" << displayState;
if (displayState) {
/// Emit signal to make background calibration resume from sleep
emit displayOn();
#ifdef SENSORFW_MCE_WATCHER
if (!mceWatcher_->PSMEnabled())
#else
#ifdef SENSORFW_LUNA_SERVICE_CLIENT
if (!lsClient_->PSMEnabled())
#endif // SENSORFW_LUNA_SERVICE_CLIENT
#endif // SENSORFW_MCE_WATCHER
{
emit resumeCalibration();
}
}
foreach (const DeviceAdaptorInstanceEntry& adaptor, deviceAdaptorInstanceMap_) {
if (adaptor.adaptor_) {
if (displayState) {
adaptor.adaptor_->setScreenBlanked(false);
adaptor.adaptor_->resume();
} else {
adaptor.adaptor_->setScreenBlanked(true);
adaptor.adaptor_->standby();
}
}
}
}
void SensorManager::devicePSMStateChanged(bool psmState)
{
if (psmState)
{
emit stopCalibration();
}
}
void SensorManager::printStatus(QStringList& output) const
{
output.append(" Adaptors:");
for (QMap<QString, DeviceAdaptorInstanceEntry>::const_iterator it = deviceAdaptorInstanceMap_.constBegin(); it != deviceAdaptorInstanceMap_.constEnd(); ++it) {
output.append(QString(" %1 [%2 listener(s)] %3").arg(it.value().type_).arg(it.value().cnt_).arg(it.value().adaptor_->deviceStandbyOverride() ? "Standby Overriden" : "No standby override"));
}
output.append(" Chains:\n");
for (QMap<QString, ChainInstanceEntry>::const_iterator it = chainInstanceMap_.constBegin(); it != chainInstanceMap_.constEnd(); ++it) {
output.append(QString(" %1 [%2 listener(s)]. %3").arg(it.value().type_).arg(it.value().cnt_).arg((it.value().chain_ && it.value().chain_->running()) ? "Running" : "Stopped"));
}
output.append(" Logical sensors:");
for (QMap<QString, SensorInstanceEntry>::const_iterator it = sensorInstanceMap_.constBegin(); it != sensorInstanceMap_.constEnd(); ++it) {
QString str;
str.append(QString(" %1 [").arg(it.value().type_));
if(it.value().sessions_.size())
str.append(QString("%1 session(s), PID(s): %2]").arg(it.value().sessions_.size()).arg(socketToPid(it.value().sessions_)));
else
str.append("No sessions]");
str.append(QString(". %1").arg((it.value().sensor_ && it.value().sensor_->running()) ? "Running" : "Stopped"));
output.append(str);
}
}
QString SensorManager::socketToPid(int id) const
{
struct ucred cr;
socklen_t len = sizeof(cr);
int fd = socketHandler_->getSocketFd(id);
if (fd)
{
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len) == 0)
return QString("%1").arg(cr.pid);
else
return strerror(errno);
}
return "n/a";
}
QString SensorManager::socketToPid(const QSet<int>& ids) const
{
QString str;
bool first = true;
foreach (int id, ids)
{
if(!first)
str.append(", ");
first = false;
str.append(socketToPid(id));
}
return str;
}
int SensorManager::createNewSessionId()
{
return ++sessionIdCount_;
}
const SensorInstanceEntry* SensorManager::getSensorInstance(const QString& id) const
{
QMap<QString, SensorInstanceEntry>::const_iterator it(sensorInstanceMap_.find(id));
if(it == sensorInstanceMap_.end())
{
sensordLogW() << "Failed to locate sensor instance: " << id;
return NULL;
}
return &it.value();
}
SensorManagerError SensorManager::errorCode() const
{
return errorCode_;
}
int SensorManager::errorCodeInt() const
{
return static_cast<int>(errorCode_);
}
const QString& SensorManager::errorString() const
{
return errorString_;
}
void SensorManager::clearError()
{
errorCode_ = SmNoError;
errorString_.clear();
}
SocketHandler& SensorManager::socketHandler() const
{
return *socketHandler_;
}
QList<QString> SensorManager::getAdaptorTypes() const
{
return deviceAdaptorInstanceMap_.keys();
}
int SensorManager::getAdaptorCount(const QString& type) const
{
QMap<QString, DeviceAdaptorInstanceEntry>::const_iterator it = deviceAdaptorInstanceMap_.find(type);
if(it == deviceAdaptorInstanceMap_.end())
return 0;
return it.value().cnt_;
}
#ifdef SENSORFW_MCE_WATCHER
MceWatcher* SensorManager::MCEWatcher() const
{
return mceWatcher_;
}
#endif // SENSORFW_MCE_WATCHER
#ifdef SENSORFW_LUNA_SERVICE_CLIENT
LSClient* SensorManager::LSClient_instance() const
{
return lsClient_;
}
#endif // SENSORFW_LUNA_SERVICE_CLIENT
#ifdef SM_PRINT
void SensorManager::print() const
{
sensordLogD() << "Registry Dump:";
foreach(const QString& id, sensorInstanceMap_.keys())
{
sensordLogD() << "Registry entry id =" << id;
sensordLogD() << "sessions =" << sensorInstanceMap_[id].sessions_;
sensordLogD() << "sensor =" << sensorInstanceMap_[id].sensor_;
sensordLogD() << "type =" << sensorInstanceMap_[id].type_ << endl;
}
sensordLogD() << "sensorInstanceMap(" << sensorInstanceMap_.size() << "):" << sensorInstanceMap_.keys();
sensordLogD() << "sensorFactoryMap(" << sensorFactoryMap_.size() << "):" << sensorFactoryMap_.keys();
}
#endif
double SensorManager::magneticDeviation()
{
if (deviation == 0) {
QSettings confFile("/etc/xdg/sensorfw/location.conf", QSettings::IniFormat);
confFile.beginGroup("location");
deviation = confFile.value("declination",0).toDouble();
}
return deviation;
}
void SensorManager::setMagneticDeviation(double level)
{
if (level != deviation) {
QSettings confFile("/etc/xdg/sensorfw/location.conf", QSettings::IniFormat);
confFile.beginGroup("location");
confFile.setValue("declination",level);
deviation = level;
}
}