forked from felis/PTP_2.0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qp.cpp
2205 lines (1919 loc) · 90.8 KB
/
qp.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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////
// Product: QP/C++, cooperative "Vanilla" kernel
// Last Updated for QP ver: 4.2.04 (modified to fit in one file)
// Date of the Last Update: Sep 25, 2011
//
// Q u a n t u m L e a P s
// ---------------------------
// innovating embedded systems
//
// Copyright (C) 2002-2011 Quantum Leaps, LLC. All rights reserved.
//
// This software may be distributed and modified under the terms of the GNU
// General Public License version 2 (GPL) as published by the Free Software
// Foundation and appearing in the file GPL.TXT included in the packaging of
// this file. Please note that GPL Section 2[b] requires that all works based
// on this software must also be made publicly available under the terms of
// the GPL ("Copyleft").
//
// Alternatively, this software may be distributed and modified under the
// terms of Quantum Leaps commercial licenses, which expressly supersede
// the GPL and are specifically designed for licensees interested in
// retaining the proprietary status of their code.
//
// Contact information:
// Quantum Leaps Web site: http://www.quantum-leaps.com
// e-mail: [email protected]
//////////////////////////////////////////////////////////////////////////////
#include "qp_port.h" // QP port
#ifdef Q_USE_NAMESPACE
namespace QP {
#endif
Q_DEFINE_THIS_MODULE(qp)
// "qep_pkg.h" ===============================================================
/// internal QEP constants
enum QEPConst {
QEP_EMPTY_SIG_ = 0, ///< empty signal for internal use only
/// maximum depth of state nesting (including the top level), must be >= 3
QEP_MAX_NEST_DEPTH_ = 6
};
/// helper macro to trigger internal event in an HSM
#define QEP_TRIG_(state_, sig_) \
((*(state_))(this, &QEP_reservedEvt_[sig_]))
/// helper macro to trigger exit action in an HSM
#define QEP_EXIT_(state_) \
if (QEP_TRIG_(state_, Q_EXIT_SIG) == Q_RET_HANDLED) { \
QS_BEGIN_(QS_QEP_STATE_EXIT, QS::smObj_, this) \
QS_OBJ_(this); \
QS_FUN_(state_); \
QS_END_() \
}
/// helper macro to trigger entry action in an HSM
#define QEP_ENTER_(state_) \
if (QEP_TRIG_(state_, Q_ENTRY_SIG) == Q_RET_HANDLED) { \
QS_BEGIN_(QS_QEP_STATE_ENTRY, QS::smObj_, this) \
QS_OBJ_(this); \
QS_FUN_(state_); \
QS_END_() \
}
// "qep.cpp" =================================================================
// Package-scope objects -----------------------------------------------------
QEvent const QEP_reservedEvt_[] = {
#ifdef Q_EVT_CTOR
(QSignal)QEP_EMPTY_SIG_,
(QSignal)Q_ENTRY_SIG,
(QSignal)Q_EXIT_SIG,
(QSignal)Q_INIT_SIG
#else
{(QSignal)QEP_EMPTY_SIG_, (uint8_t)0, (uint8_t)0},
{(QSignal)Q_ENTRY_SIG, (uint8_t)0, (uint8_t)0},
{(QSignal)Q_EXIT_SIG, (uint8_t)0, (uint8_t)0},
{(QSignal)Q_INIT_SIG, (uint8_t)0, (uint8_t)0}
#endif
};
//............................................................................
//lint -e970 -e971 -e778 ignore MISRA rules 13 and 14 in this function
char const Q_ROM * Q_ROM_VAR QEP::getVersion(void) {
static char const Q_ROM Q_ROM_VAR version[] = {
(char)(((QP_VERSION >> 12U) & 0xFU) + (uint8_t)'0'),
'.',
(char)(((QP_VERSION >> 8U) & 0xFU) + (uint8_t)'0'),
'.',
(char)(((QP_VERSION >> 4U) & 0xFU) + (uint8_t)'0'),
(char)((QP_VERSION & 0xFU) + (uint8_t)'0'),
'\0'
};
return version;
}
// "qhsm_top.cpp" ============================================================
QState QHsm::top(QHsm *, QEvent const *) {
return Q_IGNORED(); // the top state ignores all events
}
// "qhsm_ini.cpp" ============================================================
QHsm::~QHsm() {
}
//............................................................................
void QHsm::init(QEvent const *e) {
QStateHandler t;
QS_INT_LOCK_KEY_
// the top-most initial transition must be taken
Q_ALLEGE((*m_state)(this, e) == Q_RET_TRAN);
t = (QStateHandler)&QHsm::top; // HSM starts in the top state
do { // drill into the target...
QStateHandler path[QEP_MAX_NEST_DEPTH_];
int8_t ip = (int8_t)0; // transition entry path index
QS_BEGIN_(QS_QEP_STATE_INIT, QS::smObj_, this)
QS_OBJ_(this); // this state machine object
QS_FUN_(t); // the source state
QS_FUN_(m_state); // the target of the initial transition
QS_END_()
path[0] = m_state;
(void)QEP_TRIG_(m_state, QEP_EMPTY_SIG_);
while (m_state != t) {
++ip;
path[ip] = m_state;
(void)QEP_TRIG_(m_state, QEP_EMPTY_SIG_);
}
m_state = path[0];
// entry path must not overflow
Q_ASSERT(ip < (int8_t)QEP_MAX_NEST_DEPTH_);
do { // retrace the entry path in reverse (desired) order...
QEP_ENTER_(path[ip]); // enter path[ip]
--ip;
} while (ip >= (int8_t)0);
t = path[0]; // current state becomes the new source
} while (QEP_TRIG_(t, Q_INIT_SIG) == Q_RET_TRAN);
m_state = t;
QS_BEGIN_(QS_QEP_INIT_TRAN, QS::smObj_, this)
QS_TIME_(); // time stamp
QS_OBJ_(this); // this state machine object
QS_FUN_(m_state); // the new active state
QS_END_()
}
// "qhsm_dis.cpp" ============================================================
void QHsm::dispatch(QEvent const *e) {
QStateHandler path[QEP_MAX_NEST_DEPTH_];
QStateHandler s;
QStateHandler t;
QState r;
QS_INT_LOCK_KEY_
t = m_state; // save the current state
QS_BEGIN_(QS_QEP_DISPATCH, QS::smObj_, this)
QS_TIME_(); // time stamp
QS_SIG_(e->sig); // the signal of the event
QS_OBJ_(this); // this state machine object
QS_FUN_(t); // the current state
QS_END_()
do { // process the event hierarchically...
s = m_state;
r = (*s)(this, e); // invoke state handler s
} while (r == Q_RET_SUPER);
if (r == Q_RET_TRAN) { // transition taken?
#ifdef Q_SPY
QStateHandler src = s; // save the transition source for tracing
#endif
int8_t ip = (int8_t)(-1); // transition entry path index
int8_t iq; // helper transition entry path index
path[0] = m_state; // save the target of the transition
path[1] = t;
while (t != s) { // exit current state to transition source s...
if (QEP_TRIG_(t, Q_EXIT_SIG) == Q_RET_HANDLED) { //exit handled?
QS_BEGIN_(QS_QEP_STATE_EXIT, QS::smObj_, this)
QS_OBJ_(this); // this state machine object
QS_FUN_(t); // the exited state
QS_END_()
(void)QEP_TRIG_(t, QEP_EMPTY_SIG_); // find superstate of t
}
t = m_state; // m_state holds the superstate
}
t = path[0]; // target of the transition
if (s == t) { // (a) check source==target (transition to self)
QEP_EXIT_(s) // exit the source
ip = (int8_t)0; // enter the target
}
else {
(void)QEP_TRIG_(t, QEP_EMPTY_SIG_); // superstate of target
t = m_state;
if (s == t) { // (b) check source==target->super
ip = (int8_t)0; // enter the target
}
else {
(void)QEP_TRIG_(s, QEP_EMPTY_SIG_); // superstate of src
// (c) check source->super==target->super
if (m_state == t) {
QEP_EXIT_(s) // exit the source
ip = (int8_t)0; // enter the target
}
else {
// (d) check source->super==target
if (m_state == path[0]) {
QEP_EXIT_(s) // exit the source
}
else { // (e) check rest of source==target->super->super..
// and store the entry path along the way
//
iq = (int8_t)0; // indicate that LCA not found
ip = (int8_t)1; // enter target and its superstate
path[1] = t; // save the superstate of target
t = m_state; // save source->super
// find target->super->super
r = QEP_TRIG_(path[1], QEP_EMPTY_SIG_);
while (r == Q_RET_SUPER) {
++ip;
path[ip] = m_state; // store the entry path
if (m_state == s) { // is it the source?
iq = (int8_t)1; // indicate that LCA found
// entry path must not overflow
Q_ASSERT(ip < (int8_t)QEP_MAX_NEST_DEPTH_);
--ip; // do not enter the source
r = Q_RET_HANDLED; // terminate the loop
}
else { // it is not the source, keep going up
r = QEP_TRIG_(m_state, QEP_EMPTY_SIG_);
}
}
if (iq == (int8_t)0) { // the LCA not found yet?
// entry path must not overflow
Q_ASSERT(ip < (int8_t)QEP_MAX_NEST_DEPTH_);
QEP_EXIT_(s) // exit the source
// (f) check the rest of source->super
// == target->super->super...
//
iq = ip;
r = Q_RET_IGNORED; // indicate LCA NOT found
do {
if (t == path[iq]) { // is this the LCA?
r = Q_RET_HANDLED; // indicate LCA found
ip = (int8_t)(iq - 1); // do not enter LCA
iq = (int8_t)(-1); // terminate the loop
}
else {
--iq; // try lower superstate of target
}
} while (iq >= (int8_t)0);
if (r != Q_RET_HANDLED) { // LCA not found yet?
// (g) check each source->super->...
// for each target->super...
//
r = Q_RET_IGNORED; // keep looping
do {
// exit t unhandled?
if (QEP_TRIG_(t, Q_EXIT_SIG)
== Q_RET_HANDLED)
{
QS_BEGIN_(QS_QEP_STATE_EXIT,
QS::smObj_, this)
QS_OBJ_(this);
QS_FUN_(t);
QS_END_()
(void)QEP_TRIG_(t, QEP_EMPTY_SIG_);
}
t = m_state; // set to super of t
iq = ip;
do {
if (t == path[iq]) { // is this LCA?
// do not enter LCA
ip = (int8_t)(iq - 1);
iq = (int8_t)(-1); //break inner
r = Q_RET_HANDLED; //break outer
}
else {
--iq;
}
} while (iq >= (int8_t)0);
} while (r != Q_RET_HANDLED);
}
}
}
}
}
}
// retrace the entry path in reverse (desired) order...
for (; ip >= (int8_t)0; --ip) {
QEP_ENTER_(path[ip]) // enter path[ip]
}
t = path[0]; // stick the target into register
m_state = t; // update the current state
// drill into the target hierarchy...
while (QEP_TRIG_(t, Q_INIT_SIG) == Q_RET_TRAN) {
QS_BEGIN_(QS_QEP_STATE_INIT, QS::smObj_, this)
QS_OBJ_(this); // this state machine object
QS_FUN_(t); // the source (pseudo)state
QS_FUN_(m_state); // the target of the transition
QS_END_()
ip = (int8_t)0;
path[0] = m_state;
(void)QEP_TRIG_(m_state, QEP_EMPTY_SIG_); // find superstate
while (m_state != t) {
++ip;
path[ip] = m_state;
(void)QEP_TRIG_(m_state, QEP_EMPTY_SIG_); // find superstate
}
m_state = path[0];
// entry path must not overflow
Q_ASSERT(ip < (int8_t)QEP_MAX_NEST_DEPTH_);
do { // retrace the entry path in reverse (correct) order...
QEP_ENTER_(path[ip]) // enter path[ip]
--ip;
} while (ip >= (int8_t)0);
t = path[0];
}
QS_BEGIN_(QS_QEP_TRAN, QS::smObj_, this)
QS_TIME_(); // time stamp
QS_SIG_(e->sig); // the signal of the event
QS_OBJ_(this); // this state machine object
QS_FUN_(src); // the source of the transition
QS_FUN_(t); // the new active state
QS_END_()
}
else { // transition not taken
#ifdef Q_SPY
if (r == Q_RET_IGNORED) { // event ignored?
QS_BEGIN_(QS_QEP_IGNORED, QS::smObj_, this)
QS_TIME_(); // time stamp
QS_SIG_(e->sig); // the signal of the event
QS_OBJ_(this); // this state machine object
QS_FUN_(t); // the current state
QS_END_()
}
else { // event handled
QS_BEGIN_(QS_QEP_INTERN_TRAN, QS::smObj_, this)
QS_TIME_(); // time stamp
QS_SIG_(e->sig); // the signal of the event
QS_OBJ_(this); // this state machine object
QS_FUN_(s); // the state that handled the event
QS_END_()
}
#endif
}
m_state = t; // set new state or restore the current state
}
// "qf_pkg.h" ================================================================
// QF-specific interrupt locking/unlocking
#ifndef QF_INT_KEY_TYPE
/// \brief This is an internal macro for defining the interrupt lock key.
///
/// The purpose of this macro is to enable writing the same code for the
/// case when interrupt key is defined and when it is not. If the macro
/// #QF_INT_KEY_TYPE is defined, this internal macro provides the
/// definition of the lock key variable. Otherwise this macro is empty.
/// \sa #QF_INT_KEY_TYPE
#define QF_INT_LOCK_KEY_
/// \brief This is an internal macro for locking interrupts.
///
/// The purpose of this macro is to enable writing the same code for the
/// case when interrupt key is defined and when it is not. If the macro
/// #QF_INT_KEY_TYPE is defined, this internal macro invokes #QF_INT_LOCK
/// passing the key variable as the parameter. Otherwise #QF_INT_LOCK
/// is invoked with a dummy parameter.
/// \sa #QF_INT_LOCK, #QK_INT_LOCK
#define QF_INT_LOCK_() QF_INT_LOCK(dummy)
/// \brief This is an internal macro for unlocking interrupts.
///
/// The purpose of this macro is to enable writing the same code for the
/// case when interrupt key is defined and when it is not. If the macro
/// #QF_INT_KEY_TYPE is defined, this internal macro invokes
/// #QF_INT_UNLOCK passing the key variable as the parameter.
/// Otherwise #QF_INT_UNLOCK is invoked with a dummy parameter.
/// \sa #QF_INT_UNLOCK, #QK_INT_UNLOCK
#define QF_INT_UNLOCK_() QF_INT_UNLOCK(dummy)
#else
#define QF_INT_LOCK_KEY_ QF_INT_KEY_TYPE intLockKey_;
#define QF_INT_LOCK_() QF_INT_LOCK(intLockKey_)
#define QF_INT_UNLOCK_() QF_INT_UNLOCK(intLockKey_)
#endif
// package-scope objects -----------------------------------------------------
extern QTimeEvt *QF_timeEvtListHead_; ///< head of linked list of time events
extern QF_EPOOL_TYPE_ QF_pool_[QF_MAX_EPOOL]; ///< allocate event pools
extern uint8_t QF_maxPool_; ///< # of initialized event pools
extern QSubscrList *QF_subscrList_; ///< the subscriber list array
extern QSignal QF_maxSignal_; ///< the maximum published signal
//............................................................................
/// \brief Structure representing a free block in the Native QF Memory Pool
/// \sa ::QMPool
struct QFreeBlock {
QFreeBlock *m_next;
};
/// \brief access to the poolId of an event \a e_
#define EVT_POOL_ID(e_) ((e_)->poolId_)
/// \brief access to the refCtr of an event \a e_
#define EVT_REF_CTR(e_) ((e_)->refCtr_)
/// \brief increment the refCtr of an event \a e_
#define EVT_INC_REF_CTR(e_) (++((QEvent *)(e_))->refCtr_)
/// \brief decrement the refCtr of an event \a e_
#define EVT_DEC_REF_CTR(e_) (--((QEvent *)(e_))->refCtr_)
// "qa_defer.cpp" ============================================================
//............................................................................
void QActive::defer(QEQueue *eq, QEvent const *e) {
eq->postFIFO(e);
}
//............................................................................
uint8_t QActive::recall(QEQueue *eq) {
QEvent const *e = eq->get(); // try to get an event from deferred queue
if (e != (QEvent *)0) { // event available?
postLIFO(e); // post it to the front of the Active Object's queue
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
if (EVT_POOL_ID(e) != (uint8_t)0) { // is it a dynamic event?
// after posting to the AO's queue the event must be referenced
// at least twice: once in the deferred event queue (eq->get()
// did NOT decrement the reference counter) and once in the
// AO's event queue.
Q_ASSERT(EVT_REF_CTR(e) > (uint8_t)1);
// we need to decrement the reference counter once, to account
// for removing the event from the deferred event queue.
//
EVT_DEC_REF_CTR(e); // decrement the reference counter
}
QF_INT_UNLOCK_();
return (uint8_t)1; // event recalled
}
else {
return (uint8_t)0; // event not recalled
}
}
// "qa_fifo.cpp" =============================================================
//............................................................................
#ifndef Q_SPY
void QActive::postFIFO(QEvent const *e) {
#else
void QActive::postFIFO(QEvent const *e, void const *sender) {
#endif
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
QS_BEGIN_NOLOCK_(QS_QF_ACTIVE_POST_FIFO, QS::aoObj_, this)
QS_TIME_(); // timestamp
QS_OBJ_(sender); // the sender object
QS_SIG_(e->sig); // the signal of the event
QS_OBJ_(this); // this active object
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_EQC_(m_eQueue.m_nFree); // number of free entries
QS_EQC_(m_eQueue.m_nMin); // min number of free entries
QS_END_NOLOCK_()
if (EVT_POOL_ID(e) != (uint8_t)0) { // is it a dynamic event?
EVT_INC_REF_CTR(e); // increment the reference counter
}
if (m_eQueue.m_frontEvt == (QEvent *)0) { // is the queue empty?
m_eQueue.m_frontEvt = e; // deliver event directly
QACTIVE_EQUEUE_SIGNAL_(this); // signal the event queue
}
else { // queue is not empty, leave event in the ring-buffer
// queue must accept all posted events
Q_ASSERT(m_eQueue.m_nFree != (QEQueueCtr)0);
m_eQueue.m_ring[m_eQueue.m_head] = e;//insert e into the buffer (FIFO)
if (m_eQueue.m_head == (QEQueueCtr)0) { // need to wrap the head?
m_eQueue.m_head = m_eQueue.m_end; // wrap around
}
--m_eQueue.m_head;
--m_eQueue.m_nFree; // update number of free events
if (m_eQueue.m_nMin > m_eQueue.m_nFree) {
m_eQueue.m_nMin = m_eQueue.m_nFree; // update minimum so far
}
}
QF_INT_UNLOCK_();
}
// "qa_get_.cpp" =============================================================
QEvent const *QActive::get_(void) {
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
QACTIVE_EQUEUE_WAIT_(this); // wait for event to arrive directly
QEvent const *e = m_eQueue.m_frontEvt;
if (m_eQueue.m_nFree != m_eQueue.m_end) { //any events in the ring buffer?
// remove event from the tail
m_eQueue.m_frontEvt = m_eQueue.m_ring[m_eQueue.m_tail];
if (m_eQueue.m_tail == (QEQueueCtr)0) { // need to wrap the tail?
m_eQueue.m_tail = m_eQueue.m_end; // wrap around
}
--m_eQueue.m_tail;
++m_eQueue.m_nFree; // one more free event in the ring buffer
QS_BEGIN_NOLOCK_(QS_QF_ACTIVE_GET, QS::aoObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(e->sig); // the signal of this event
QS_OBJ_(this); // this active object
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_EQC_(m_eQueue.m_nFree); // number of free entries
QS_END_NOLOCK_()
}
else {
m_eQueue.m_frontEvt = (QEvent *)0; // the queue becomes empty
QACTIVE_EQUEUE_ONEMPTY_(this);
QS_BEGIN_NOLOCK_(QS_QF_ACTIVE_GET_LAST, QS::aoObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(e->sig); // the signal of this event
QS_OBJ_(this); // this active object
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_END_NOLOCK_()
}
QF_INT_UNLOCK_();
return e;
}
//............................................................................
uint32_t QF::getQueueMargin(uint8_t prio) {
Q_REQUIRE((prio <= (uint8_t)QF_MAX_ACTIVE)
&& (active_[prio] != (QActive *)0));
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
uint32_t margin = (uint32_t)(active_[prio]->m_eQueue.m_nMin);
QF_INT_UNLOCK_();
return margin;
}
// "qa_lifo.cpp" =============================================================
void QActive::postLIFO(QEvent const *e) {
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
QS_BEGIN_NOLOCK_(QS_QF_ACTIVE_POST_LIFO, QS::aoObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(e->sig); // the signal of this event
QS_OBJ_(this); // this active object
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_EQC_(m_eQueue.m_nFree); // number of free entries
QS_EQC_(m_eQueue.m_nMin); // min number of free entries
QS_END_NOLOCK_()
if (EVT_POOL_ID(e) != (uint8_t)0) { // is it a dynamic event?
EVT_INC_REF_CTR(e); // increment the reference counter
}
if (m_eQueue.m_frontEvt == (QEvent *)0) { // is the queue empty?
m_eQueue.m_frontEvt = e; // deliver event directly
QACTIVE_EQUEUE_SIGNAL_(this); // signal the event queue
}
else { // queue is not empty, leave event in the ring-buffer
// queue must accept all posted events
Q_ASSERT(m_eQueue.m_nFree != (QEQueueCtr)0);
++m_eQueue.m_tail;
if (m_eQueue.m_tail == m_eQueue.m_end) { // need to wrap the tail?
m_eQueue.m_tail = (QEQueueCtr)0; // wrap around
}
m_eQueue.m_ring[m_eQueue.m_tail] = m_eQueue.m_frontEvt;
m_eQueue.m_frontEvt = e; // put event to front
--m_eQueue.m_nFree; // update number of free events
if (m_eQueue.m_nMin > m_eQueue.m_nFree) {
m_eQueue.m_nMin = m_eQueue.m_nFree; // update minimum so far
}
}
QF_INT_UNLOCK_();
}
// "qa_sub.cpp" ==============================================================
void QActive::subscribe(QSignal sig) const {
uint8_t p = m_prio;
Q_REQUIRE(((QSignal)Q_USER_SIG <= sig)
&& (sig < QF_maxSignal_)
&& ((uint8_t)0 < p) && (p <= (uint8_t)QF_MAX_ACTIVE)
&& (QF::active_[p] == this));
uint8_t i = Q_ROM_BYTE(QF_div8Lkup[p]);
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
QS_BEGIN_NOLOCK_(QS_QF_ACTIVE_SUBSCRIBE, QS::aoObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(sig); // the signal of this event
QS_OBJ_(this); // this active object
QS_END_NOLOCK_()
// set the priority bit
QF_subscrList_[sig].m_bits[i] |= Q_ROM_BYTE(QF_pwr2Lkup[p]);
QF_INT_UNLOCK_();
}
// "qa_usub.cpp" =============================================================
void QActive::unsubscribe(QSignal sig) const {
uint8_t p = m_prio;
Q_REQUIRE(((QSignal)Q_USER_SIG <= sig)
&& (sig < QF_maxSignal_)
&& ((uint8_t)0 < p) && (p <= (uint8_t)QF_MAX_ACTIVE)
&& (QF::active_[p] == this));
uint8_t i = Q_ROM_BYTE(QF_div8Lkup[p]);
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
QS_BEGIN_NOLOCK_(QS_QF_ACTIVE_UNSUBSCRIBE, QS::aoObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(sig); // the signal of this event
QS_OBJ_(this); // this active object
QS_END_NOLOCK_()
// clear the priority bit
QF_subscrList_[sig].m_bits[i] &= Q_ROM_BYTE(QF_invPwr2Lkup[p]);
QF_INT_UNLOCK_();
}
// "qa_usuba.cpp" ============================================================
void QActive::unsubscribeAll(void) const {
uint8_t p = m_prio;
Q_REQUIRE(((uint8_t)0 < p) && (p <= (uint8_t)QF_MAX_ACTIVE)
&& (QF::active_[p] == this));
uint8_t i = Q_ROM_BYTE(QF_div8Lkup[p]);
QSignal sig;
for (sig = (QSignal)Q_USER_SIG; sig < QF_maxSignal_; ++sig) {
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
if ((QF_subscrList_[sig].m_bits[i] & Q_ROM_BYTE(QF_pwr2Lkup[p]))
!= 0)
{
QS_BEGIN_NOLOCK_(QS_QF_ACTIVE_UNSUBSCRIBE, QS::aoObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(sig); // the signal of this event
QS_OBJ_(this); // this active object
QS_END_NOLOCK_()
// clear the priority bit
QF_subscrList_[sig].m_bits[i] &= Q_ROM_BYTE(QF_invPwr2Lkup[p]);
}
QF_INT_UNLOCK_();
}
}
// "qeq_fifo.cpp" ============================================================
void QEQueue::postFIFO(QEvent const *e) {
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
QS_BEGIN_NOLOCK_(QS_QF_EQUEUE_POST_FIFO, QS::eqObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(e->sig); // the signal of this event
QS_OBJ_(this); // this queue object
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_EQC_(m_nFree); // number of free entries
QS_EQC_(m_nMin); // min number of free entries
QS_END_NOLOCK_()
if (EVT_POOL_ID(e) != (uint8_t)0) { // is it a dynamic event?
EVT_INC_REF_CTR(e); // increment the reference counter
}
if (m_frontEvt == (QEvent *)0) { // is the queue empty?
m_frontEvt = e; // deliver event directly
}
else { // queue is not empty, leave event in the ring-buffer
// the queue must be able to accept the event (cannot overflow)
Q_ASSERT(m_nFree != (QEQueueCtr)0);
m_ring[m_head] = e; // insert event into the buffer (FIFO)
if (m_head == (QEQueueCtr)0) { // need to wrap the head?
m_head = m_end; // wrap around
}
--m_head;
--m_nFree; // update number of free events
if (m_nMin > m_nFree) {
m_nMin = m_nFree; // update minimum so far
}
}
QF_INT_UNLOCK_();
}
// "qeq_get.cpp" =============================================================
QEvent const *QEQueue::get(void) {
QEvent const *e;
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
if (m_frontEvt == (QEvent *)0) { // is the queue empty?
e = (QEvent *)0; // no event available at this time
}
else {
e = m_frontEvt;
if (m_nFree != m_end) { // any events in the the ring buffer?
m_frontEvt = m_ring[m_tail]; // remove event from the tail
if (m_tail == (QEQueueCtr)0) { // need to wrap the tail?
m_tail = m_end; // wrap around
}
--m_tail;
++m_nFree; // one more free event in the ring buffer
QS_BEGIN_NOLOCK_(QS_QF_EQUEUE_GET, QS::eqObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(e->sig); // the signal of this event
QS_OBJ_(this); // this queue object
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_EQC_(m_nFree); // number of free entries
QS_END_NOLOCK_()
}
else {
m_frontEvt = (QEvent *)0; // the queue becomes empty
QS_BEGIN_NOLOCK_(QS_QF_EQUEUE_GET_LAST, QS::eqObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(e->sig); // the signal of this event
QS_OBJ_(this); // this queue object
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_END_NOLOCK_()
}
}
QF_INT_UNLOCK_();
return e;
}
// "qeq_init.cpp" ============================================================
void QEQueue::init(QEvent const *qSto[], QEQueueCtr qLen) {
m_frontEvt = (QEvent *)0; // no events in the queue
m_ring = &qSto[0];
m_end = qLen;
m_head = (QEQueueCtr)0;
m_tail = (QEQueueCtr)0;
m_nFree = qLen;
m_nMin = qLen;
QS_INT_LOCK_KEY_
QS_BEGIN_(QS_QF_EQUEUE_INIT, QS::eqObj_, this)
QS_OBJ_(qSto); // this QEQueue object
QS_EQC_(qLen); // the length of the queue
QS_END_()
}
// "qeq_lifo.cpp" ============================================================
void QEQueue::postLIFO(QEvent const *e) {
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
QS_BEGIN_NOLOCK_(QS_QF_EQUEUE_POST_LIFO, QS::eqObj_, this)
QS_TIME_(); // timestamp
QS_SIG_(e->sig); // the signal of this event
QS_OBJ_(this); // this queue object
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_EQC_(m_nFree); // number of free entries
QS_EQC_(m_nMin); // min number of free entries
QS_END_NOLOCK_()
if (EVT_POOL_ID(e) != (uint8_t)0) { // is it a dynamic event?
EVT_INC_REF_CTR(e); // increment the reference counter
}
if (m_frontEvt != (QEvent *)0) { // is the queue not empty?
// the queue must be able to accept the event (cannot overflow)
Q_ASSERT(m_nFree != (QEQueueCtr)0);
++m_tail;
if (m_tail == m_end) { // need to wrap the tail?
m_tail = (QEQueueCtr)0; // wrap around
}
m_ring[m_tail] = m_frontEvt; // buffer the old front evt
--m_nFree; // update number of free events
if (m_nMin > m_nFree) {
m_nMin = m_nFree; // update minimum so far
}
}
m_frontEvt = e; // stick the new event to the front
QF_INT_UNLOCK_();
}
// "qf_act.cpp" ==============================================================
// public objects ------------------------------------------------------------
QActive *QF::active_[QF_MAX_ACTIVE + 1]; // to be used by QF ports only
uint8_t QF_intLockNest_; // interrupt-lock nesting level
//............................................................................
//lint -e970 -e971 ignore MISRA rules 13 and 14 in this function
const char Q_ROM * Q_ROM_VAR QF::getVersion(void) {
static char const Q_ROM Q_ROM_VAR version[] = {
(char)(((QP_VERSION >> 12U) & 0xFU) + (uint8_t)'0'),
'.',
(char)(((QP_VERSION >> 8U) & 0xFU) + (uint8_t)'0'),
'.',
(char)(((QP_VERSION >> 4U) & 0xFU) + (uint8_t)'0'),
(char)((QP_VERSION & 0xFU) + (uint8_t)'0'),
'\0'
};
return version;
}
//............................................................................
void QF::add_(QActive *a) {
uint8_t p = a->m_prio;
Q_REQUIRE(((uint8_t)0 < p) && (p <= (uint8_t)QF_MAX_ACTIVE)
&& (active_[p] == (QActive *)0));
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
active_[p] = a; // registger the active object at this priority
QS_BEGIN_NOLOCK_(QS_QF_ACTIVE_ADD, QS::aoObj_, a)
QS_TIME_(); // timestamp
QS_OBJ_(a); // the active object
QS_U8_(p); // the priority of the active object
QS_END_NOLOCK_()
QF_INT_UNLOCK_();
}
//............................................................................
void QF::remove_(QActive const *a) {
uint8_t p = a->m_prio;
Q_REQUIRE(((uint8_t)0 < p) && (p <= (uint8_t)QF_MAX_ACTIVE)
&& (active_[p] == a));
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
active_[p] = (QActive *)0; // free-up the priority level
QS_BEGIN_NOLOCK_(QS_QF_ACTIVE_REMOVE, QS::aoObj_, a)
QS_TIME_(); // timestamp
QS_OBJ_(a); // the active object
QS_U8_(p); // the priority of the active object
QS_END_NOLOCK_()
QF_INT_UNLOCK_();
}
// "qf_gc.cpp" ===============================================================
void QF::gc(QEvent const *e) {
if (EVT_POOL_ID(e) != (uint8_t)0) { // is it a dynamic event?
QF_INT_LOCK_KEY_
QF_INT_LOCK_();
if (EVT_REF_CTR(e) > (uint8_t)1) { // isn't this the last reference?
EVT_DEC_REF_CTR(e); // decrement the ref counter
QS_BEGIN_NOLOCK_(QS_QF_GC_ATTEMPT, (void *)0, (void *)0)
QS_TIME_(); // timestamp
QS_SIG_(e->sig); // the signal of the event
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_END_NOLOCK_()
QF_INT_UNLOCK_();
}
else { // this is the last reference to this event, recycle it
uint8_t idx = (uint8_t)(EVT_POOL_ID(e) - 1);
QS_BEGIN_NOLOCK_(QS_QF_GC, (void *)0, (void *)0)
QS_TIME_(); // timestamp
QS_SIG_(e->sig); // the signal of the event
QS_U8_(EVT_POOL_ID(e)); // the pool Id of the event
QS_U8_(EVT_REF_CTR(e)); // the ref count of the event
QS_END_NOLOCK_()
QF_INT_UNLOCK_();
Q_ASSERT(idx < QF_maxPool_);
#ifdef Q_EVT_CTOR
//lint -e1773 Attempt to cast away const
((QEvent *)e)->~QEvent(); // call the xtor, cast 'const' away,
// which is legitimate, because it's a pool event
#endif
//lint -e1773 Attempt to cast away const
QF_EPOOL_PUT_(QF_pool_[idx], (QEvent *)e); // cast 'const' away,
// which is legitimate, because it's a pool event
}
}
}
// "qf_log2.cpp" =============================================================
uint8_t const Q_ROM Q_ROM_VAR QF_log2Lkup[256] = {
0U, 1U, 2U, 2U, 3U, 3U, 3U, 3U, 4U, 4U, 4U, 4U, 4U, 4U, 4U, 4U,
5U, 5U, 5U, 5U, 5U, 5U, 5U, 5U, 5U, 5U, 5U, 5U, 5U, 5U, 5U, 5U,
6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U,
6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U, 6U,
7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U,
7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U,
7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U,
7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U, 7U,
8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U
};
// "qf_new.cpp" ==============================================================
QEvent *QF::new_(uint16_t evtSize, QSignal sig) {
// find the pool id that fits the requested event size ...
uint8_t idx = (uint8_t)0;
while (evtSize > QF_EPOOL_EVENT_SIZE_(QF_pool_[idx])) {
++idx;
Q_ASSERT(idx < QF_maxPool_); // cannot run out of registered pools
}
QS_INT_LOCK_KEY_
QS_BEGIN_(QS_QF_NEW, (void *)0, (void *)0)
QS_TIME_(); // timestamp
QS_EVS_(evtSize); // the size of the event
QS_SIG_(sig); // the signal of the event
QS_END_()
QEvent *e;
QF_EPOOL_GET_(QF_pool_[idx], e);
Q_ASSERT(e != (QEvent *)0); // pool must not run out of events
e->sig = sig; // set signal for this event
EVT_POOL_ID(e) = (uint8_t)(idx + 1); // store the pool ID in the event
EVT_REF_CTR(e) = (uint8_t)0; // set the reference counter to 0
return e;
}
// "qf_pool.cpp" =============================================================
// Package-scope objects -----------------------------------------------------
QF_EPOOL_TYPE_ QF_pool_[QF_MAX_EPOOL]; // allocate the event pools
uint8_t QF_maxPool_; // number of initialized event pools
//............................................................................
void QF::poolInit(void *poolSto, uint32_t poolSize, QEventSize evtSize) {
// cannot exceed the number of available memory pools