API
 
Loading...
Searching...
No Matches
MagAOXApp_test.hpp
Go to the documentation of this file.
1#ifndef app_tests_MagAOXApp_test_hpp
2#define app_tests_MagAOXApp_test_hpp
3
4namespace libXWCTest
5{
6namespace appTest
7{
8namespace MagAOXAppTest
9{
10
11#undef APP_XWCTEST_BASE
12#ifdef XWCTEST_NAMESPACE
13 #define APP_XWCTEST_BASE MagAOX::app::XWCTEST_NAMESPACE::MagAOXApp<true>
14#else
15 #define APP_XWCTEST_BASE MagAOX::app::MagAOXApp<true>
16#endif
17
18#ifdef XWCTEST_NAMESPACE
19namespace XWCTEST_NAMESPACE
20{
21#endif
22
24{
25
26 MagAOXApp_test( bool gitmod = false ) : MagAOXApp( "sha1", gitmod )
27 {
28 }
29
30 ~MagAOXApp_test() noexcept (true)
31 {}
32
33 bool appStartupFail{ false };
34 bool appLogicFail{ false };
35 bool appShutdownFail{ false };
36
38 {
39 config.add( "name2", "", "name2", argType::Required, "", "", true, "string", "" );
40 config.m_sources = true;
41 }
42
43 void setup( int argc, char **argv )
44 {
45 APP_XWCTEST_BASE::setup( argc, argv );
46 }
47
48 virtual int appStartup()
49 {
50 if( appStartupFail )
51 {
52 return -1;
53 }
54
55 return 0;
56 }
57
58 virtual int appLogic()
59 {
60 if( appLogicFail )
61 {
62 return -1;
63 }
64
65 return 0;
66 }
67
68 virtual int appShutdown()
69 {
70 if( appShutdownFail )
71 {
72 return -1;
73 }
74
75 return 0;
76 }
77
78 std::string configPathGlobal()
79 {
80 return APP_XWCTEST_BASE::m_configPathGlobal;
81 }
82
83 std::string configPathUser()
84 {
85 return APP_XWCTEST_BASE::m_configPathUser;
86 }
87
88 std::string configPathLocal()
89 {
90 return APP_XWCTEST_BASE::m_configPathLocal;
91 }
92
93 std::string &invokedName()
94 {
95 return APP_XWCTEST_BASE::invokedName;
96 }
97
98 bool &doHelp()
99 {
100 return APP_XWCTEST_BASE::doHelp;
101 }
102
104 {
105 return APP_XWCTEST_BASE::m_configOnly;
106 }
107
108 void setPowerMgtEnabled( bool pme )
109 {
110 m_powerMgtEnabled = pme;
111 }
112
113 void setConfigName( const std::string &cn )
114 {
115 m_configName = cn;
116
117 m_indiDriver = new MagAOX::app::indiDriver<APP_XWCTEST_BASE>( this, m_configName, "0", "0" );
118 }
119
120 void setConfigBase( const std::string &cb )
121 {
122 m_configBase = cb;
123 }
124
125 int called_back{ 0 };
126
127 void setAlert()
128 {
129 m_stateAlert = true;
130 }
131
133 {
134 pcf::IndiProperty ip( pcf::IndiProperty::Switch );
135 ip.setDevice( configName() );
136 ip.setName( "fsm_clear_alert" );
137 ip.add( pcf::IndiElement( "request" ) );
138 ip["request"].setSwitchState( pcf::IndiElement::On );
139
140 st_newCallBack_clearFSMAlert( this, ip );
141 }
142
143 std::string powerDevice()
144 {
145 return m_powerDevice;
146 }
147
148 std::string powerChannel()
149 {
150 return m_powerChannel;
151 }
152
153 std::string powerElement()
154 {
155 return m_powerElement;
156 }
157
158 std::string powerTargetElement()
159 {
160 return m_powerTargetElement;
161 }
162
164 {
165 return m_powerOnWait;
166 }
167
169 {
170 return APP_XWCTEST_BASE::onPowerOff();
171 }
172
174 {
175 return APP_XWCTEST_BASE::whilePowerOff();
176 }
177
179 {
180 return APP_XWCTEST_BASE::powerOnWaitElapsed();
181 }
182
184 {
185 return APP_XWCTEST_BASE::powerState();
186 }
187
188 void configurePowerManagement( const std::string &device, const std::string &channel )
189 {
190 m_indiP_powerChannel = pcf::IndiProperty( pcf::IndiProperty::Text );
191 m_powerDevice = device;
192 m_indiP_powerChannel.setDevice( device );
193
194 m_powerChannel = channel;
195 m_indiP_powerChannel.setName( channel );
196 }
197
198 void configurePowerOnWait( unsigned long powerOnWait, int powerOnCounter, int loopPause )
199 {
200 m_powerOnWait = powerOnWait;
201 m_powerOnCounter = powerOnCounter;
202 m_loopPause = loopPause;
203 }
204
205 int setPowerState( const std::string &state, const std::string target )
206 {
207 pcf::IndiProperty ip( pcf::IndiProperty::Text );
208 ip.setDevice( m_powerDevice );
209 ip.setName( m_powerChannel );
210 ip.add( pcf::IndiElement( "state" ) );
211 ip["state"].setValue( state );
212
213 ip.add( pcf::IndiElement( "target" ) );
214 ip["target"].setValue( target );
215
216 return setCallBack_m_indiP_powerChannel( ip );
217 }
218
220 {
221 return APP_XWCTEST_BASE::setSigTermHandler();
222 }
223
224 void p_handlerSigTerm( int signum, siginfo_t *siginf, void *ucont )
225 {
226 _handlerSigTerm( signum, siginf, ucont );
227 }
228
230 {
231 return APP_XWCTEST_BASE::setEuidReal();
232 }
233
234 int setEuidReal( int euidr, bool set = true )
235 {
236 m_euidReal = euidr;
237
238 if( set )
239 {
240 return APP_XWCTEST_BASE::setEuidReal();
241 }
242
243 return 0;
244 }
245
247 {
248 elevatedPrivileges elPriv( this );
249 elPriv.elevate();
250 elPriv.restore();
251
252 return 0;
253 }
254
256 {
257 return APP_XWCTEST_BASE::setEuidCalled();
258 }
259
260 int setEuidCalled( int euidc )
261 {
262 m_euidCalled = euidc;
263 return APP_XWCTEST_BASE::setEuidCalled();
264 }
265
267 {
268 return APP_XWCTEST_BASE::lockPID();
269 }
270
272 {
273 return APP_XWCTEST_BASE::unlockPID();
274 }
275
276 // Thread
277 int m_threadPrio{ 0 }; ///< Priority of the framegrabber thread, should normally be > 00.
278
279 std::string m_cpuset; ///< The cpuset to assign the framegrabber thread to. Not used if empty, the default.
280
281 bool m_threadInit{ true }; ///< Synchronizer for thread startup, to allow priority setting to finish.
282
283 pid_t m_threadID{ 0 }; ///< The ID of the thread.
284
285 pcf::IndiProperty m_threadProp; ///< The property to hold the thread details.
286
287 std::thread m_thread; ///< A separate thread
288
289 bool m_threadRunning {false};
290 bool m_threadStopped {false};
291 int m_threadError {0};
292
294 {
295 if( threadStart( m_thread,
296 m_threadInit,
297 m_threadID,
298 m_threadProp,
299 m_threadPrio,
300 m_cpuset,
301 "thread",
302 this,
303 threadStarter ) < 0 )
304 {
305
306 return -1;
307 }
308
309
310 while(m_threadError == 0 && m_threadRunning == false)
311 {
312 sleep(1);
313 }
314
315 if(m_threadError != 0)
316 {
317 return m_threadError;
318 }
319
320 if(m_threadRunning != true)
321 {
322 return -3;
323 }
324
325 m_threadRunning = false;
326
327 while(m_threadError == 0 && m_threadStopped == false)
328 {
329 sleep(1);
330 }
331
332 if(m_threadError != 0)
333 {
334 return m_threadError;
335 }
336
337 XWCAPP_THREAD_STOP(m_thread);
338
339 return 0;
340 }
341 /// Thread starter, called by MagAOXApp::threadStart on thread construction. Calls threadExec.
342 static void threadStarter( MagAOXApp_test *o /**< [in] a pointer to aninstance (normally this) */ )
343 {
344 o->threadExec();
345 }
346
347 /// Execute framegrabbing.
349 {
350 // Get the thread PID immediately so the caller can return.
351 m_threadID = syscall( SYS_gettid );
352
353 // Wait for the thread starter to finish initializing this thread.
354 int n = 0;
355 while( m_threadInit == true && n < 5)
356 {
357 sleep( 1 );
358 ++n;
359 }
360
361 if(n >= 5)
362 {
363 m_threadError = -2;
364 return;
365 }
366
367 std::cerr << "threading\n";
368
369 m_threadRunning = true;
370
371 while(m_threadRunning)
372 {
373 sleep( 1 );
374 }
375
376 m_threadRunning = false;
377 m_threadStopped = true;
378 }
379};
380
381int callback( void *app, const pcf::IndiProperty &ipRecv )
382{
383 static_cast<void>( ipRecv ); // be unused
384
385 MagAOXApp_test *appt = static_cast<MagAOXApp_test *>( app );
386
387 appt->called_back = 1;
388
389 return 0;
390}
391
392#ifdef XWCTEST_NAMESPACE
393} // namespace XWCTEST_NAMESPACE
394#endif
395
396} // namespace MagAOXAppTest
397} // namespace appTest
398} // namespace libXWCTest
399
400#endif // app_tests_MagAOXApp_test_hpp
#define XWCAPP_THREAD_STOP(thrdSt)
Error handlng wrapper for stopping a thread.
#define APP_XWCTEST_BASE
int callback(void *app, const pcf::IndiProperty &ipRecv)
Namespace for all libXWC tests.
Definition MagAOXApp.hpp:49
void configurePowerManagement(const std::string &device, const std::string &channel)
static void threadStarter(MagAOXApp_test *o)
Thread starter, called by MagAOXApp::threadStart on thread construction. Calls threadExec.
void configurePowerOnWait(unsigned long powerOnWait, int powerOnCounter, int loopPause)
int setPowerState(const std::string &state, const std::string target)
std::string m_cpuset
The cpuset to assign the framegrabber thread to. Not used if empty, the default.
pcf::IndiProperty m_threadProp
The property to hold the thread details.
void p_handlerSigTerm(int signum, siginfo_t *siginf, void *ucont)
#define XWCTEST_NAMESPACE