/
nodebase.cpp
751 lines (655 loc) · 19.2 KB
/
nodebase.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
/**
@file nodebase.cpp
@brief Base class for all nodes in filtering chain
<p>
Copyright (C) 2009-2010 Nokia Corporation
@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>
*/
#include "nodebase.h"
#include "logging.h"
#include "ringbuffer.h"
#include "config.h"
NodeBase::NodeBase(const QString& id, QObject* parent) :
QObject(parent),
m_bufferSize(0),
m_bufferInterval(0),
m_dataRangeSource(NULL),
m_intervalSource(NULL),
m_hasDefault(false),
m_defaultInterval(0),
DEFAULT_DATA_RANGE_REQUEST(-1),
id_(id),
isValid_(false)
{
}
NodeBase::~NodeBase()
{
}
const QString& NodeBase::id() const
{
return id_;
}
bool NodeBase::isValid() const
{
return isValid_;
}
bool NodeBase::isMetadataValid() const
{
if (!hasLocalRange())
{
return m_dataRangeSource->isMetadataValid();
}
return true;
}
const QString& NodeBase::description() const
{
return m_description;
}
void NodeBase::setDescription(const QString& str)
{
m_description = str;
}
void NodeBase::introduceAvailableDataRange(const DataRange& range)
{
if (!m_dataRangeList.contains(range))
{
sensordLogD() << "Introduced new data range for '" << id_ << "':" << range.min << "-" << range.max << "," << range.resolution;
m_dataRangeList.append(range);
}
}
void NodeBase::introduceAvailableDataRanges(const QString& typeName)
{
QVariant ranges = SensorFrameworkConfig::configuration()->value(typeName + "/dataranges");
if(ranges.isValid())
{
DataRangeList list(parseDataRangeList(ranges.toString(), 1));
foreach(const DataRange& range, list)
{
introduceAvailableDataRange(range);
}
}
}
const QList<DataRange>& NodeBase::getAvailableDataRanges() const
{
if (hasLocalRange())
{
return m_dataRangeList;
}
else
{
return m_dataRangeSource->getAvailableDataRanges();
}
}
DataRangeRequest NodeBase::getCurrentDataRange() const
{
if (hasLocalRange()) {
if (m_dataRangeQueue.empty()) {
return DataRangeRequest(-1, m_dataRangeList.at(0));
}
return m_dataRangeQueue.at(0);
} else {
return m_dataRangeSource->getCurrentDataRange();
}
}
void NodeBase::requestDataRange(int sessionId, const DataRange& range)
{
if (hasLocalRange())
{
// Do not process invalid ranges
if (!(m_dataRangeList.contains(range))) {
return;
}
// Check if the range is going to change (no requests or we have the
// active request)
bool rangeChanged = false;
if (m_dataRangeQueue.empty())
{
if (!(range == m_dataRangeList.at(0)))
{
rangeChanged = true;
}
} else {
if (m_dataRangeQueue.at(0).id == sessionId && !(m_dataRangeQueue.at(0).range == range)) {
rangeChanged = true;
}
}
// If an earlier request exists by same id, replace.
bool hadPreviousRequest = false;
for (int i = 0; i < m_dataRangeQueue.size() && hadPreviousRequest == false; ++i) {
if (m_dataRangeQueue.at(i).id == sessionId) {
m_dataRangeQueue[i].range = range;
hadPreviousRequest = true;
}
}
if (!hadPreviousRequest) {
DataRangeRequest request(sessionId, range);
m_dataRangeQueue.append(request);
}
if (rangeChanged)
{
DataRangeRequest currentRequest = getCurrentDataRange();
if (!setDataRange(currentRequest.range, currentRequest.id))
{
sensordLogW() << "Failed to set DataRange.";
}
emit propertyChanged("datarange");
}
} else {
m_dataRangeSource->requestDataRange(sessionId, range);
}
}
void NodeBase::removeDataRangeRequest(int sessionId)
{
if (hasLocalRange())
{
int index = -1;
for (int i = 0; i < m_dataRangeQueue.size() && index == -1; ++i) {
if (m_dataRangeQueue.at(i).id == sessionId) {
index = i;
}
}
if (index < 0) {
sensordLogD() << "No data range request for id " << sessionId;
return;
}
DataRangeRequest request = m_dataRangeQueue.takeAt(index);
bool rangeChanged = false;
if (index == 0)
{
if (((m_dataRangeQueue.size() > 0) && !(m_dataRangeQueue.at(0).range == request.range)) ||
!(m_dataRangeList.at(0) == request.range))
{
rangeChanged = true;
}
}
if (rangeChanged)
{
DataRangeRequest currentRequest = getCurrentDataRange();
if (!setDataRange(currentRequest.range, currentRequest.id))
{
sensordLogW() << "Failed to set DataRange.";
}
emit propertyChanged("datarange");
}
} else {
m_dataRangeSource->removeDataRangeRequest(sessionId);
}
}
void NodeBase::setRangeSource(NodeBase* node)
{
m_dataRangeSource = node;
connect(m_dataRangeSource, SIGNAL(propertyChanged(const QString&)), this, SIGNAL(propertyChanged(const QString&)));
}
bool NodeBase::hasLocalRange() const
{
return (m_dataRangeSource == NULL);
}
DataRangeList NodeBase::parseDataRangeList(const QString& input, int defaultResolution) const
{
DataRangeList list;
foreach(const QString& fragment, input.split(",", QString::SkipEmptyParts))
{
QStringList pair(fragment.split("=>"));
QStringList pair2(fragment.split(":"));
DataRange range;
range.resolution = defaultResolution;
if(pair.size() == 1)
{
QVariant value(fragment);
range.min = value.toDouble();
range.max = range.min;
}
else if(pair.size() == 2)
{
QVariant value(fragment);
range.min = QVariant(pair.at(0)).toDouble();
range.max = QVariant(pair.at(1)).toDouble();
}
else
{
continue;
}
if(pair2.size() == 2)
range.resolution = QVariant(pair2.at(1)).toDouble();
list.push_back(range);
}
return list;
}
const QList<DataRange>& NodeBase::getAvailableIntervals() const
{
if (m_intervalSource && !hasLocalInterval())
{
return m_intervalSource->getAvailableIntervals();
}
return m_intervalList;
}
void NodeBase::introduceAvailableInterval(const DataRange& interval)
{
if (!m_intervalList.contains(interval))
{
sensordLogD() << "Introduced new interval for '" << id_ << "':" << interval.min << "-" << interval.max;
m_intervalList.append(interval);
}
}
void NodeBase::introduceAvailableIntervals(const QString& typeName)
{
QVariant ranges = SensorFrameworkConfig::configuration()->value(typeName + "/intervals");
if(ranges.isValid())
{
DataRangeList list(parseDataRangeList(ranges.toString(), 0));
foreach(const DataRange& range, list)
{
introduceAvailableInterval(range);
}
}
}
unsigned int NodeBase::getInterval() const
{
if (!hasLocalInterval())
{
return m_intervalSource->getInterval();
}
return interval();
}
unsigned int NodeBase::getInterval(int sessionId) const
{
if (!hasLocalInterval())
{
return m_intervalSource->getInterval(sessionId);
}
QMap<int, unsigned int>::const_iterator it(m_intervalMap.find(sessionId));
if(it == m_intervalMap.end())
{
return 0;
}
return it.value();
}
bool NodeBase::setIntervalRequest(const int sessionId, const unsigned int value)
{
// Has single defined source, pass the request that way
if (!hasLocalInterval())
{
return m_intervalSource->setIntervalRequest(sessionId, value);
}
// Validate interval request
if (!isValidIntervalRequest(value))
{
sensordLogW() << "Invalid interval requested for node '" << id() << "' by session '" << sessionId << "': " << value;
return false;
}
// Store the request for the session
m_intervalMap[sessionId] = value;
// Store the current interval
unsigned int previousInterval = interval();
// Re-evaluate
int winningSessionId;
unsigned int winningRequest = evaluateIntervalRequests(winningSessionId);
if (winningSessionId >= 0) {
sensordLogD() << "Setting new interval for node: " << id() << ". Evaluation won by session '" << winningSessionId << "' with request: " << winningRequest;
setInterval(winningRequest, winningSessionId);
}
// Signal listeners about change
if (previousInterval != interval())
{
emit propertyChanged("interval");
}
return true;
}
void NodeBase::addStandbyOverrideSource(NodeBase* node)
{
if (node)
m_standbySourceList.append(node);
}
bool NodeBase::standbyOverride() const
{
if (m_standbySourceList.size() == 0) {
return false;
}
bool returnValue = true;
foreach (NodeBase* node, m_standbySourceList)
{
returnValue = returnValue && node->standbyOverride();
}
return returnValue;
}
bool NodeBase::setStandbyOverrideRequest(const int sessionId, const bool override)
{
sensordLogD() << sessionId << "requested standbyoverride for '" << id() << "' :" << override;
// Only store true requests, id is enough, no need for value
if (override == false)
{
m_standbyRequestList.removeAll(sessionId);
}
else
{
if (!m_standbyRequestList.contains(sessionId))
{
m_standbyRequestList.append(sessionId);
}
}
// Re-evaluate state for nodes that implement handling locally.
if (m_standbySourceList.size() == 0)
{
return setStandbyOverride(m_standbyRequestList.size() > 0);
}
// Pass request to sources
bool returnValue = true;
foreach (NodeBase* node, m_standbySourceList)
{
returnValue = node->setStandbyOverrideRequest(sessionId, override) && returnValue;
}
// Revert changes if any source failed while trying to set true.
if (override == true && returnValue == false)
{
foreach (NodeBase* node, m_standbySourceList)
{
node->setStandbyOverrideRequest(sessionId, false);
}
}
return returnValue;
}
bool NodeBase::hasLocalInterval() const
{
return (m_intervalSource == NULL);
}
void NodeBase::setIntervalSource(NodeBase* node)
{
m_intervalSource = node;
connect(m_intervalSource, SIGNAL(propertyChanged(const QString&)), this, SIGNAL(propertyChanged(const QString&)));
}
unsigned int NodeBase::evaluateIntervalRequests(int& sessionId) const
{
if (m_intervalMap.size() == 0)
{
sessionId = -1;
return defaultInterval();
}
// Get the winning request
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) {
highestValue = it.value();
winningSessionId = it.key();
}
}
sessionId = winningSessionId;
return highestValue;
}
unsigned int NodeBase::defaultInterval() const
{
return m_defaultInterval;
}
bool NodeBase::setDefaultInterval(const unsigned int value)
{
if (!isValidIntervalRequest(value))
{
sensordLogW() << "Attempting to define invalid default data rate:" << value;
return false;
}
m_defaultInterval = value;
m_hasDefault = true;
return true;
}
bool NodeBase::requestDefaultInterval(const int sessionId)
{
foreach (NodeBase *source, m_sourceList)
{
source->requestDefaultInterval(sessionId);
}
if (m_hasDefault)
{
return setIntervalRequest(sessionId, defaultInterval());
}
return true;
}
void NodeBase::removeIntervalRequest(const int sessionId)
{
unsigned int previousInterval = interval();
foreach (NodeBase *source, m_sourceList)
{
source->removeIntervalRequest(sessionId);
}
if (hasLocalInterval())
{
// Remove from local list
if (m_intervalMap.keys().contains(sessionId))
{
m_intervalMap.remove(sessionId);
}
// Re-evaluate local setting
int winningSessionId;
unsigned int winningRequest = evaluateIntervalRequests(winningSessionId);
if (winningSessionId >= 0) {
setInterval(winningRequest, winningSessionId);
}
// Signal listeners if changed.
if (previousInterval != interval())
{
emit propertyChanged("interval");
}
}
}
bool NodeBase::connectToSource(NodeBase* source, const QString& bufferName, RingBufferReaderBase* reader)
{
if (!source)
return false;
RingBufferBase* rb = source->findBuffer(bufferName);
if (rb == NULL)
{
// This is critical as long as connections are statically defined.
sensordLogC() << "Buffer '" << bufferName << "' not found while building connections for node: " << id();
return false;
}
bool success = rb->join(reader);
if (success)
{
// Store a reference to the source
m_sourceList.append(source);
}
return success;
}
bool NodeBase::disconnectFromSource(NodeBase* source, const QString& bufferName, RingBufferReaderBase* reader)
{
if (!source)
return false;
RingBufferBase* rb = source->findBuffer(bufferName);
if (rb == NULL)
{
sensordLogW() << "Buffer '" << bufferName << "' not found while erasing connections for node: " << id();
return false;
}
bool success = rb->unjoin(reader);
if (success)
{
// Remove the source reference from storage
if (!m_sourceList.removeOne(source))
{
sensordLogW() << "Buffer '" << bufferName << "' not disconnected properly for node: " << id();
}
}
return success;
}
bool NodeBase::isValidIntervalRequest(const unsigned int value) const
{
for(QList<DataRange>::const_iterator it = m_intervalList.constBegin(); it != m_intervalList.constEnd(); ++it)
{
if (it->min <= value && it->max >= value)
{
return true;
}
}
return false;
}
IntegerRangeList NodeBase::getAvailableBufferSizes(bool& hwSupported) const
{
IntegerRangeList list;
foreach (NodeBase* source, m_sourceList)
{
list = source->getAvailableBufferSizes(hwSupported);
if(hwSupported)
return list;
}
if(list.isEmpty())
list.push_back(IntegerRange(1, 256));
hwSupported = false;
return list;
}
IntegerRangeList NodeBase::getAvailableBufferIntervals(bool& hwSupported) const
{
IntegerRangeList list;
foreach (NodeBase* source, m_sourceList)
{
list = source->getAvailableBufferIntervals(hwSupported);
if(hwSupported)
return list;
}
if(list.isEmpty())
list.push_back(IntegerRange(0, 60000));
hwSupported = false;
return list;
}
bool NodeBase::setBufferSize(int sessionId, unsigned int value)
{
bool hwbuffering = false;
if(!isInRange(value, getAvailableBufferSizes(hwbuffering)))
return false;
m_bufferSizeMap.insert(sessionId, value);
return updateBufferSize();
}
bool NodeBase::clearBufferSize(int sessionId)
{
int index = m_bufferSizeMap.remove(sessionId);
updateBufferSize();
return index != 0;
}
bool NodeBase::updateBufferSize()
{
int key = 0;
int value = 0;
for(QMap<int, unsigned int>::const_iterator it = m_bufferSizeMap.constBegin(); it != m_bufferSizeMap.constEnd(); ++it)
{
if(it.key() >= key)
{
key = it.key();
value = it.value();
}
}
if(setBufferSize(value))
{
emit propertyChanged("buffersize");
return true;
}
return false;
}
bool NodeBase::setBufferInterval(int sessionId, unsigned int value)
{
bool hwbuffering = false;
if(!isInRange(value, getAvailableBufferIntervals(hwbuffering)))
return false;
m_bufferIntervalMap.insert(sessionId, value);
return updateBufferInterval();
}
bool NodeBase::clearBufferInterval(int sessionId)
{
int index = m_bufferIntervalMap.remove(sessionId);
updateBufferInterval();
return index != 0;
}
bool NodeBase::updateBufferInterval()
{
int key = 0;
int value = 0;
for(QMap<int, unsigned int>::const_iterator it = m_bufferIntervalMap.constBegin(); it != m_bufferIntervalMap.constEnd(); ++it)
{
if(it.key() >= key)
{
key = it.key();
value = it.value();
}
}
if(setBufferInterval(value))
{
emit propertyChanged("bufferinterval");
return true;
}
return false;
}
bool NodeBase::setDataRangeIndex(int sessionId, int rangeIndex)
{
if (rangeIndex < 0)
return false;
if (rangeIndex > (m_dataRangeList.size() - 1))
return false;
requestDataRange(sessionId, m_dataRangeList.at(rangeIndex));
DataRangeList ranges = getAvailableDataRanges();
DataRange range = getCurrentDataRange().range;
return ranges.at(rangeIndex) == range;
}
void NodeBase::removeSession(int sessionId)
{
setStandbyOverrideRequest(sessionId, false);
removeIntervalRequest(sessionId);
removeDataRangeRequest(sessionId);
clearBufferSize(sessionId);
clearBufferInterval(sessionId);
}
void NodeBase::setValid(bool valid)
{
isValid_ = valid;
if(!isValid_)
{
sensordLogW() << "Node '" << id() << "' state changed to invalid";
}
}
bool NodeBase::setDataRange(const DataRange& range, int sessionId)
{
Q_UNUSED(range);
Q_UNUSED(sessionId);
sensordLogD() << __func__ << "not implemented in some node using it.";
return false;
}
bool NodeBase::setStandbyOverride(bool override)
{
Q_UNUSED(override);
sensordLogD() << __func__ << "not implemented in some node using it.";
return false;
}
unsigned int NodeBase::interval() const
{
sensordLogD() << __func__ << "not implemented in some node using it.";
return 0;
}
bool NodeBase::setInterval(unsigned int value, int sessionId)
{
Q_UNUSED(value);
Q_UNUSED(sessionId);
sensordLogD() << __func__ << "not implemented in some node using it.";
return false;
}
bool NodeBase::setBufferSize(unsigned int value)
{
Q_UNUSED(value);
sensordLogD() << __func__ << "not implemented in some node using it.";
return false;
}
bool NodeBase::setBufferInterval(unsigned int value)
{
Q_UNUSED(value);
sensordLogD() << __func__ << "not implemented in some node using it.";
return false;
}