API
streamCircBuff.hpp
Go to the documentation of this file.
1 /** \file streamCircBuff.hpp
2  * \brief The MagAO-X streamCircBuff app header file
3  *
4  * \ingroup streamCircBuff_files
5  */
6 
7 #ifndef streamCircBuff_hpp
8 #define streamCircBuff_hpp
9 
10 
11 #include "../../libMagAOX/libMagAOX.hpp" //Note this is included on command line to trigger pch
12 #include "../../magaox_git_version.h"
13 
14 /** \defgroup streamCircBuff
15  * \brief An application to keep a circular buffer of a stream
16  *
17  * <a href="../handbook/operating/software/apps/streamCircBuff.html">Application Documentation</a>
18  *
19  * \ingroup apps
20  *
21  */
22 
23 /** \defgroup streamCircBuff_files
24  * \ingroup streamCircBuff
25  */
26 
27 namespace MagAOX
28 {
29 namespace app
30 {
31 
32 /// Class for application to keep a circular buffer of a stream and publish it to another stream
33 /**
34  * \ingroup streamCircBuff
35  */
36 class streamCircBuff : public MagAOXApp<true>,
37  public dev::shmimMonitor<streamCircBuff>,
38  public dev::frameGrabber<streamCircBuff>,
39  public dev::telemeter<streamCircBuff>
40 {
41  friend class dev::shmimMonitor<streamCircBuff>;
42  friend class dev::frameGrabber<streamCircBuff>;
43  friend class dev::telemeter<streamCircBuff>;
44 
45 public:
46 
47  /** \name app::dev Configurations
48  *@{
49  */
50 
51  static constexpr bool c_frameGrabber_flippable = false; ///< app:dev config to tell framegrabber these images can not be flipped
52 
53  ///@}
54 
55  typedef float realT;
56 
57  /// The base shmimMonitor type
59 
60  /// The base frameGrabber type
62 
63  /// The telemeter type
65 
66 
67 protected:
68 
69  /** \name Configurable Parameters
70  *@{
71  */
72  ///@}
73 
74  char * m_currSrc {nullptr};
75 
76  sem_t m_smSemaphore {0}; ///< Semaphore used to synchronize the fg thread and the sm thread.
77 
78 public:
79  /// Default c'tor.
81 
82  /// D'tor, declared and defined for noexcept.
83  ~streamCircBuff() noexcept
84  {}
85 
86  virtual void setupConfig();
87 
88  /// Implementation of loadConfig logic, separated for testing.
89  /** This is called by loadConfig().
90  */
91  int loadConfigImpl( mx::app::appConfigurator & _config /**< [in] an application configuration from which to load values*/);
92 
93  virtual void loadConfig();
94 
95  /// Startup function
96  /**
97  *
98  */
99  virtual int appStartup();
100 
101  /// Implementation of the FSM for streamCircBuff.
102  /**
103  * \returns 0 on no critical error
104  * \returns -1 on an error requiring shutdown
105  */
106  virtual int appLogic();
107 
108  /// Shutdown the app.
109  /**
110  *
111  */
112  virtual int appShutdown();
113 
114 
115 protected:
116 
117  //shmimMonitor Interface
118  int allocate( const dev::shmimT & dummy /**< [in] tag to differentiate shmimMonitor parents.*/);
119 
121 
122  int processImage( void * curr_src, ///< [in] pointer to start of current frame.
123  const dev::shmimT & dummy ///< [in] tag to differentiate shmimMonitor parents.
124  );
125 
126  /** \name dev::frameGrabber interface
127  *
128  * @{
129  */
130 
131  /// Implementation of the framegrabber configureAcquisition interface
132  /**
133  * \returns 0 on success
134  * \returns -1 on error
135  */
136  int configureAcquisition();
137 
138  /// Implementation of the framegrabber fps interface
139  /**
140  * \todo this needs to infer the stream fps and return it
141  */
142  float fps()
143  {
144  return 1.0;
145  }
146 
147  /// Implementation of the framegrabber startAcquisition interface
148  /**
149  * \returns 0 on success
150  * \returns -1 on error
151  */
152  int startAcquisition();
153 
154  /// Implementation of the framegrabber acquireAndCheckValid interface
155  /**
156  * \returns 0 on success
157  * \returns -1 on error
158  */
159  int acquireAndCheckValid();
160 
161  /// Implementation of the framegrabber loadImageIntoStream interface
162  /**
163  * \returns 0 on success
164  * \returns -1 on error
165  */
166  int loadImageIntoStream( void * dest /**< [in] */);
167 
168  /// Implementation of the framegrabber reconfig interface
169  /**
170  * \returns 0 on success
171  * \returns -1 on error
172  */
173  int reconfig();
174 
175  /** \name Telemeter Interface
176  *
177  * @{
178  */
179  int checkRecordTimes();
180 
181  int recordTelem( const telem_fgtimings * );
182 
183  ///@}
184 
185 };
186 
187 streamCircBuff::streamCircBuff() : MagAOXApp(MAGAOX_CURRENT_SHA1, MAGAOX_REPO_MODIFIED)
188 {
189 
190  return;
191 }
192 
194 {
196 
198 
199  TELEMETER_SETUP_CONFIG(config);
200 
201 }
202 
203 int streamCircBuff::loadConfigImpl( mx::app::appConfigurator & _config )
204 {
205  SHMIMMONITOR_LOAD_CONFIG(_config);
206 
207  FRAMEGRABBER_LOAD_CONFIG(_config);
208 
209  TELEMETER_LOAD_CONFIG(config);
210 
211  return 0;
212 }
213 
215 {
216  loadConfigImpl(config);
217 }
218 
220 {
221  if(sem_init(&m_smSemaphore, 0,0) < 0)
222  {
223  log<software_critical>({__FILE__, __LINE__, errno,0, "Initializing S.M. semaphore"});
224  return -1;
225  }
226 
228 
230 
232 
234 
235  return 0;
236 }
237 
239 {
241 
243 
245 
246  std::unique_lock<std::mutex> lock(m_indiMutex);
247 
249 
251 
252  return 0;
253 }
254 
256 {
260 
261  return 0;
262 }
263 
265 {
266  static_cast<void>(dummy);
267 
268  //we don't actually do anything here -- just a pass through to f.g.
269 
270  m_reconfig = true;
271 
272  return 0;
273 }
274 
275 int streamCircBuff::processImage( void * curr_src,
276  const dev::shmimT & dummy
277  )
278 {
279  static_cast<void>(dummy);
280 
281  m_currSrc = static_cast<char *>(curr_src);
282 
283  //Now tell the f.g. to get going
284  if(sem_post(&m_smSemaphore) < 0)
285  {
286  log<software_critical>({__FILE__, __LINE__, errno, 0, "Error posting to semaphore"});
287  return -1;
288  }
289 
290  return 0;
291 }
292 
294 {
295  std::unique_lock<std::mutex> lock(m_indiMutex);
296 
297  ///\todo potential but verrrrry unlikely bug: shmimMonitorT could change these before allocate sets the lock above. Should use a local set of w/h instead.
299  {
300  //This means we haven't connected to the stream to accumulate. so wait.
301  lock.unlock(); //don't hold the lock for a whole second.
302  sleep(1);
303  return -1;
304  }
305 
309 
310  return 0;
311 }
312 
314 {
315  return 0;
316 }
317 
319 {
320  timespec ts;
321 
322  if(clock_gettime(CLOCK_REALTIME, &ts) < 0)
323  {
324  log<software_critical>({__FILE__,__LINE__,errno,0,"clock_gettime"});
325  return -1;
326  }
327 
328  ts.tv_sec += 1;
329 
330  if(sem_timedwait(&m_smSemaphore, &ts) == 0)
331  {
332  clock_gettime(CLOCK_REALTIME, &m_currImageTimestamp);
333  return 0;
334  }
335  else
336  {
337  return 1;
338  }
339 
340  if(m_currSrc == nullptr)
341  {
342  return 1;
343  }
344 }
345 
347 {
348  if(m_currSrc == nullptr)
349  {
350  return -1;
351  }
352 
354  return 0;
355 }
356 
358 {
359  return 0;
360 }
361 
363 {
365 }
366 
368 {
369  return recordFGTimings(true);
370 }
371 
372 
373 } //namespace app
374 } //namespace MagAOX
375 
376 #endif //streamCircBuff_hpp
The base-class for MagAO-X applications.
Definition: MagAOXApp.hpp:73
stateCodes::stateCodeT state()
Get the current state code.
Definition: MagAOXApp.hpp:2297
std::mutex m_indiMutex
Mutex for locking INDI communications.
Definition: MagAOXApp.hpp:545
timespec m_currImageTimestamp
The timestamp of the current image.
uint32_t m_width
The width of the image, once deinterlaced etc.
size_t m_typeSize
The size of the type, in bytes. Result of sizeof.
uint8_t m_dataType
The ImageStreamIO type code.
bool m_reconfig
Flag to set if a camera reconfiguration requires a framegrabber reset.
uint32_t m_height
The height of the image, once deinterlaced etc.
uint32_t m_width
The width of the images in the stream.
uint32_t m_height
The height of the images in the stream.
uint8_t m_dataType
The ImageStreamIO type code.
Class for application to keep a circular buffer of a stream and publish it to another stream.
~streamCircBuff() noexcept
D'tor, declared and defined for noexcept.
dev::frameGrabber< streamCircBuff > frameGrabberT
The base frameGrabber type.
dev::shmimMonitor< streamCircBuff > shmimMonitorT
The base shmimMonitor type.
static constexpr bool c_frameGrabber_flippable
app:dev config to tell framegrabber these images can not be flipped
int acquireAndCheckValid()
Implementation of the framegrabber acquireAndCheckValid interface.
int loadConfigImpl(mx::app::appConfigurator &_config)
Implementation of loadConfig logic, separated for testing.
virtual int appLogic()
Implementation of the FSM for streamCircBuff.
int allocate(const dev::shmimT &dummy)
int processImage(void *curr_src, const dev::shmimT &dummy)
int loadImageIntoStream(void *dest)
Implementation of the framegrabber loadImageIntoStream interface.
dev::telemeter< streamCircBuff > telemeterT
The telemeter type.
int recordTelem(const telem_fgtimings *)
virtual int appShutdown()
Shutdown the app.
int configureAcquisition()
Implementation of the framegrabber configureAcquisition interface.
sem_t m_smSemaphore
Semaphore used to synchronize the fg thread and the sm thread.
int reconfig()
Implementation of the framegrabber reconfig interface.
int startAcquisition()
Implementation of the framegrabber startAcquisition interface.
virtual int appStartup()
Startup function.
float fps()
Implementation of the framegrabber fps interface.
#define FRAMEGRABBER_SETUP_CONFIG(cfig)
Call frameGrabberT::setupConfig with error checking for frameGrabber.
#define FRAMEGRABBER_APP_LOGIC
Call frameGrabberT::appLogic with error checking for frameGrabber.
#define FRAMEGRABBER_APP_SHUTDOWN
Call frameGrabberT::appShutdown with error checking for frameGrabber.
#define FRAMEGRABBER_UPDATE_INDI
Call frameGrabberT::updateINDI with error checking for frameGrabber.
#define FRAMEGRABBER_LOAD_CONFIG(cfig)
Call frameGrabberT::loadConfig with error checking for frameGrabber.
#define FRAMEGRABBER_APP_STARTUP
Call frameGrabberT::appStartup with error checking for frameGrabber.
@ OPERATING
The device is operating, other than homing.
Definition: stateCodes.hpp:55
std::unique_lock< std::mutex > lock(m_indiMutex)
Definition: dm.hpp:24
#define SHMIMMONITOR_APP_SHUTDOWN
Call shmimMonitorT::appShutdown with error checking for shmimMonitor.
#define SHMIMMONITOR_APP_LOGIC
Call shmimMonitorT::appLogic with error checking for shmimMonitor.
#define SHMIMMONITOR_APP_STARTUP
Call shmimMonitorT::appStartup with error checking for shmimMonitor.
#define SHMIMMONITOR_LOAD_CONFIG(cfig)
Call shmimMonitorT::loadConfig with error checking for shmimMonitor.
#define SHMIMMONITOR_UPDATE_INDI
Call shmimMonitorT::updateINDI with error checking for shmimMonitor.
#define SHMIMMONITOR_SETUP_CONFIG(cfig)
Call shmimMonitorT::setupConfig with error checking for shmimMonitor.
A device base class which saves telemetry.
Definition: telemeter.hpp:69
int checkRecordTimes(const telT &tel, telTs... tels)
Check the time of the last record for each telemetry type and make an entry if needed.
Definition: telemeter.hpp:281
Log entry recording framegrabber timings.
#define TELEMETER_APP_LOGIC
Call telemeter::appLogic with error checking.
Definition: telemeter.hpp:339
#define TELEMETER_LOAD_CONFIG(cfig)
Call telemeter::loadConfig with error checking.
Definition: telemeter.hpp:325
#define TELEMETER_APP_STARTUP
Call telemeter::appStartup with error checking.
Definition: telemeter.hpp:332
#define TELEMETER_SETUP_CONFIG(cfig)
Call telemeter::setupConfig with error checking.
Definition: telemeter.hpp:314
#define TELEMETER_APP_SHUTDOWN
Call telemeter::appShutdown with error checking.
Definition: telemeter.hpp:346