Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * @file sys.System.cpp | ||
3 | * @author Sergey Baigudin, sergey@baigudin.software | ||
4 | * @copyright 2014-2022, Sergey Baigudin, Baigudin Software | ||
5 | */ | ||
6 | #include "sys.System.hpp" | ||
7 | #include "sys.Mutex.hpp" | ||
8 | #include "sys.Semaphore.hpp" | ||
9 | #include "Program.hpp" | ||
10 | #include "lib.LinkedList.hpp" | ||
11 | #include "lib.UniquePointer.hpp" | ||
12 | |||
13 | namespace eoos | ||
14 | { | ||
15 | namespace sys | ||
16 | { | ||
17 | |||
18 | api::System* System::eoos_( NULLPTR ); | ||
19 | |||
20 | 184 | System::System() | |
21 | : NonCopyable() | ||
22 | , api::System() | ||
23 | 184 | , configuration_() | |
24 |
1/2✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
|
184 | , scheduler_() |
25 |
1/2✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
|
184 | , heap_() |
26 |
1/2✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
|
184 | , cout_(OutStreamChar::TYPE_COUT) |
27 |
1/2✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
|
368 | , cerr_(OutStreamChar::TYPE_CERR) { |
28 |
1/2✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
|
184 | bool_t const isConstructed( construct() ); |
29 | 184 | setConstructed( isConstructed ); | |
30 | 184 | } | |
31 | |||
32 | 368 | System::~System() ///< SCA MISRA-C++:2008 Defected Rule 10-3-2 | |
33 | { | ||
34 |
1/2✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
|
368 | static_cast<void>(cout_.flush()); |
35 |
1/2✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
|
368 | static_cast<void>(cerr_.flush()); |
36 | 368 | eoos_ = NULLPTR; | |
37 | } | ||
38 | |||
39 | 1492 | bool_t System::isConstructed() const ///< SCA MISRA-C++:2008 Justified Rule 10-3-1 and Defected Rule 10-3-2 | |
40 | { | ||
41 | 1492 | return Parent::isConstructed(); | |
42 | } | ||
43 | |||
44 | 38 | api::Scheduler& System::getScheduler() ///< SCA MISRA-C++:2008 Defected Rule 10-3-2 | |
45 | { | ||
46 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
|
38 | if( !isConstructed() ) |
47 | { ///< UT Justified Branch: HW dependency | ||
48 | ✗ | exit(ERROR_SYSCALL_CALLED); | |
49 | } | ||
50 | 38 | return scheduler_; ///< SCA MISRA-C++:2008 Justified Rule 9-3-2 | |
51 | } | ||
52 | |||
53 | 1181 | api::Heap& System::getHeap() ///< SCA MISRA-C++:2008 Defected Rule 10-3-2 | |
54 | { | ||
55 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1181 times.
|
1181 | if( !isConstructed() ) |
56 | { ///< UT Justified Branch: HW dependency | ||
57 | ✗ | exit(ERROR_SYSCALL_CALLED); | |
58 | } | ||
59 | 1181 | return heap_; ///< SCA MISRA-C++:2008 Justified Rule 9-3-2 | |
60 | } | ||
61 | |||
62 | 6 | api::OutStream<char_t>& System::getOutStreamChar() ///< SCA MISRA-C++:2008 Defected Rule 10-3-2 | |
63 | { | ||
64 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if( !isConstructed() ) |
65 | { ///< UT Justified Branch: HW dependency | ||
66 | ✗ | exit(ERROR_SYSCALL_CALLED); | |
67 | } | ||
68 | 6 | return cout_; ///< SCA MISRA-C++:2008 Justified Rule 9-3-2 | |
69 | } | ||
70 | |||
71 | 2 | api::OutStream<char_t>& System::getErrorStreamChar() ///< SCA MISRA-C++:2008 Defected Rule 10-3-2 | |
72 | { | ||
73 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if( !isConstructed() ) |
74 | { ///< UT Justified Branch: HW dependency | ||
75 | ✗ | exit(ERROR_SYSCALL_CALLED); | |
76 | } | ||
77 | 2 | return cerr_; ///< SCA MISRA-C++:2008 Justified Rule 9-3-2 | |
78 | } | ||
79 | |||
80 | 63 | api::Mutex* System::createMutex() ///< SCA MISRA-C++:2008 Defected Rule 10-3-2 | |
81 | { | ||
82 | 63 | api::Mutex* ptr( NULLPTR ); | |
83 |
1/2✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
|
63 | if( isConstructed() ) |
84 | { | ||
85 |
4/8✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 63 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 63 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 63 times.
✗ Branch 10 not taken.
|
63 | lib::UniquePointer<api::Mutex> res( new Mutex() ); |
86 |
2/4✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 63 times.
✗ Branch 4 not taken.
|
63 | if( !res.isNull() ) |
87 | { | ||
88 |
3/6✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 63 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 63 times.
|
63 | if( !res->isConstructed() ) |
89 | { ///< UT Justified Branch: HW dependency | ||
90 | ✗ | res.reset(); | |
91 | } | ||
92 | } | ||
93 |
1/2✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
|
63 | ptr = res.release(); |
94 |
1/2✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
|
63 | } |
95 | 63 | return ptr; | |
96 | } | ||
97 | |||
98 | 8 | api::Semaphore* System::createSemaphore(int32_t permits) ///< SCA MISRA-C++:2008 Defected Rule 10-3-2 | |
99 | { | ||
100 | 8 | api::Semaphore* ptr( NULLPTR ); | |
101 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | if( isConstructed() ) |
102 | { | ||
103 |
4/8✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
|
8 | lib::UniquePointer<api::Semaphore> res( new Semaphore(permits) ); |
104 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
|
8 | if( !res.isNull() ) |
105 | { | ||
106 |
4/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 6 times.
|
8 | if( !res->isConstructed() ) |
107 | { | ||
108 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | res.reset(); |
109 | } | ||
110 | } | ||
111 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | ptr = res.release(); |
112 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | } |
113 | 8 | return ptr; | |
114 | } | ||
115 | |||
116 | 1 | int32_t System::execute() const | |
117 | { | ||
118 | 1 | char_t* args[] = {NULLPTR}; | |
119 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return execute(0, args); ///< SCA MISRA-C++:2008 Justified Rule 5-2-12 |
120 | } | ||
121 | |||
122 | 9 | int32_t System::execute(int32_t argc, char_t* argv[]) const | |
123 | { | ||
124 | 9 | int32_t error( ERROR_OK ); | |
125 |
7/8✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 2 times.
|
9 | if( isConstructed() && (argc >= 0) && (argv != NULLPTR) ) |
126 | { | ||
127 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | lib::LinkedList<char_t*> args; |
128 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
|
15 | for(int32_t i(0); i<argc; i++) |
129 | { | ||
130 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
|
10 | if( argv[i] != NULLPTR ) |
131 | { | ||
132 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
|
8 | if( args.add(argv[i]) == true ) |
133 | { | ||
134 | 8 | continue; | |
135 | } | ||
136 | } | ||
137 | 2 | error = ERROR_ARGUMENT; | |
138 | 2 | break; | |
139 | } | ||
140 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
|
7 | if( error != ERROR_ARGUMENT ) |
141 | { | ||
142 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | if( argv[argc] == NULLPTR ) |
143 | { | ||
144 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | error = Program::start(args); |
145 | } | ||
146 | else | ||
147 | { | ||
148 | 1 | error = ERROR_ARGUMENT; | |
149 | } | ||
150 | |||
151 | } | ||
152 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | } |
153 | else | ||
154 | { | ||
155 | 2 | error = ERROR_ARGUMENT; | |
156 | } | ||
157 | 9 | return error; | |
158 | } | ||
159 | |||
160 | 1298 | api::System& System::getSystem() | |
161 | { | ||
162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1298 times.
|
1298 | if(eoos_ == NULLPTR) |
163 | { ///< UT Justified Branch: Startup dependency | ||
164 | ✗ | exit(ERROR_SYSCALL_CALLED); | |
165 | } | ||
166 | 1298 | return *eoos_; | |
167 | } | ||
168 | |||
169 | ✗ | void System::exit(Error const error) ///< UT Justified Branch: HW dependency | |
170 | { | ||
171 | ✗ | ::exit( static_cast<int_t>(error) ); ///< SCA MISRA-C++:2008 Justified Rule 18-0-3 | |
172 | // This code must NOT be executed | ||
173 | // @todo throw an exection here is better. | ||
174 | while( true ) {} | ||
175 | } | ||
176 | |||
177 | 184 | bool_t System::construct() | |
178 | { | ||
179 | 184 | bool_t res( false ); | |
180 | do | ||
181 | { | ||
182 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
|
184 | if( !isConstructed() ) |
183 | { ///< UT Justified Branch: HW dependency | ||
184 | ✗ | break; | |
185 | } | ||
186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if( eoos_ != NULLPTR ) |
187 | { ///< UT Justified Branch: Startup dependency | ||
188 | ✗ | break; | |
189 | } | ||
190 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
|
184 | if( !scheduler_.isConstructed() ) |
191 | { ///< UT Justified Branch: HW dependency | ||
192 | ✗ | break; | |
193 | } | ||
194 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
|
184 | if( !heap_.isConstructed() ) |
195 | { ///< UT Justified Branch: HW dependency | ||
196 | ✗ | break; | |
197 | } | ||
198 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
|
184 | if( !cout_.isConstructed() ) |
199 | { ///< UT Justified Branch: HW dependency | ||
200 | ✗ | break; | |
201 | } | ||
202 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
|
184 | if( !cerr_.isConstructed() ) |
203 | { ///< UT Justified Branch: HW dependency | ||
204 | ✗ | break; | |
205 | } | ||
206 | 184 | eoos_ = this; | |
207 | 184 | res = true; | |
208 | } while(false); | ||
209 | 184 | return res; | |
210 | } | ||
211 | |||
212 | } // namespace sys | ||
213 | } // namespace eoos | ||
214 |