VimbaSystem.cpp
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (C) 2012 Allied Vision Technologies. All Rights Reserved.
3 
4  Redistribution of this file, in original or modified form, without
5  prior written consent of Allied Vision Technologies is prohibited.
6 
7 -------------------------------------------------------------------------------
8 
9  File: VimbaSystem.cpp
10 
11  Description: Implementation of class AVT::VmbAPI::VimbaSystem.
12 
13 -------------------------------------------------------------------------------
14 
15  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
16  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE,
17  NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
23  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 =============================================================================*/
27 
28 #include <cstring>
29 #include <algorithm>
30 
32 
35 #include <VimbaCPP/Source/Clock.h>
37 #include <VimbaCPP/Source/Helper.h>
39 
40 namespace AVT {
41 namespace VmbAPI {
42 
43 typedef std::map<std::string, CameraPtr> CameraPtrMap;
44 typedef std::map<std::string, InterfacePtr> InterfacePtrMap;
45 
47 {
48  // Found cameras and interfaces
53  // Registered observers
58 
59  // GigE specifics
62  // CameraFactory
63  ICameraFactoryPtr m_pCameraFactory;
64 
65  // Logger
67 
70  void AppendCamToMap( VmbCameraInfo_t camInfo );
71  bool IsIPAddress( const char *pStrID );
72 
73  VmbErrorType GetInterfaceList( std::vector<VmbInterfaceInfo_t> &interfaceInfos );
74 
75  static void VMB_CALL CameraDiscoveryCallback( const VmbHandle_t handle, const char *name, void *context );
76  static void VMB_CALL InterfaceDiscoveryCallback( const VmbHandle_t handle, const char *name, void *context );
77 };
78 
80 {
81  return _instance;
82 }
83 
85 {
86  rVersion.major = VIMBACPP_VERSION_MAJOR;
87  rVersion.minor = VIMBACPP_VERSION_MINOR;
88  rVersion.patch = VIMBACPP_VERSION_PATCH;
89 
90  return VmbErrorSuccess;
91 }
92 
94 {
95  VmbError_t res = VmbStartup();
96 
97  VmbFeatureBoolGet( gVimbaHandle, "GeVTLIsPresent", &m_pImpl->m_bGeVTLPresent );
98 
99  return (VmbErrorType)res;
100 }
101 
103 {
105 
106  // Begin exclusive write lock camera observer list
108  {
110 
111  // End write lock camera observer list
113  }
114 
115  // Begin exclusive write lock interface observer list
117  {
119 
120  // End write lock interface observer list
122  }
123 
124  // Begin exclusive write lock camera list
126  {
127  for ( CameraPtrMap::iterator iter = m_pImpl->m_cameras.Map.begin();
128  m_pImpl->m_cameras.Map.end() != iter;
129  ++iter)
130  {
131  SP_ACCESS( iter->second )->Close();
132  }
133  m_pImpl->m_cameras.Map.clear();
134 
135  // End write lock camera list
137  }
138 
139  // Begin exclusive write lock interface list
141  {
142  for ( InterfacePtrMap::iterator iter = m_pImpl->m_interfaces.Map.begin();
143  m_pImpl->m_interfaces.Map.end() != iter;
144  ++iter)
145  {
146  SP_ACCESS( iter->second )->Close();
147  }
148  m_pImpl->m_interfaces.Map.clear();
149 
150  // End write lock interface list
152  }
153 
154  VmbShutdown();
155 
156  return VmbErrorSuccess;
157 }
158 
160 {
162 
163  // Begin write lock interface list
165  {
166  res = m_pImpl->UpdateInterfaceList();
167 
168  if ( VmbErrorSuccess == res)
169  {
170  if ( NULL == pInterfaces )
171  {
172  rnSize = (VmbUint32_t)m_pImpl->m_interfaces.Map.size();
173  res = VmbErrorSuccess;
174  }
175  else if ( m_pImpl->m_interfaces.Map.size() <= rnSize )
176  {
177  VmbUint32_t i = 0;
178  for ( InterfacePtrMap::iterator iter = m_pImpl->m_interfaces.Map.begin();
179  m_pImpl->m_interfaces.Map.end() != iter;
180  ++iter, ++i )
181  {
182  pInterfaces[i] = iter->second;
183  }
184  rnSize = (VmbUint32_t)m_pImpl->m_interfaces.Map.size();
185  res = VmbErrorSuccess;
186  }
187  else
188  {
189  res = VmbErrorMoreData;
190  }
191  }
192 
193  // End write lock interface list
195  }
196 
197  return res;
198 }
199 
200 VmbErrorType VimbaSystem::GetInterfaceByID( const char *pStrID, InterfacePtr &rInterface )
201 {
202  if ( NULL == pStrID )
203  {
204  return VmbErrorBadParameter;
205  }
206 
208 
209  // Begin write lock interface list
211  {
212  InterfacePtrMap::iterator iter = m_pImpl->m_interfaces.Map.find( pStrID );
213  if ( m_pImpl->m_interfaces.Map.end() != iter )
214  {
215  rInterface = iter->second;
216  res = VmbErrorSuccess;
217  }
218  else
219  {
220  std::vector<VmbInterfaceInfo_t> interfaceInfos;
221  res = m_pImpl->GetInterfaceList( interfaceInfos );
222 
223  if ( VmbErrorSuccess == res )
224  {
225  for ( std::vector<VmbInterfaceInfo_t>::iterator iterInfo = interfaceInfos.begin();
226  interfaceInfos.end() != iterInfo;
227  ++iterInfo )
228  {
229  if ( 0 == strcmp( iterInfo->interfaceIdString, pStrID ))
230  {
231  SP_SET( m_pImpl->m_interfaces.Map[pStrID], new Interface( &(*iterInfo) ));
232  break;
233  }
234  }
235 
236  iter = m_pImpl->m_interfaces.Map.find( pStrID );
237  if ( m_pImpl->m_interfaces.Map.end() != iter )
238  {
239  rInterface = iter->second;
240  }
241  else
242  {
243  res = VmbErrorNotFound;
244  }
245  }
246  }
247 
248  // End write lock interface list
250  }
251 
252  return res;
253 }
254 
255 VmbErrorType VimbaSystem::OpenInterfaceByID( const char *pStrID, InterfacePtr &rInterface )
256 {
257  if ( NULL == pStrID )
258  {
259  return VmbErrorBadParameter;
260  }
261 
262  VmbErrorType res = GetInterfaceByID( pStrID, rInterface );
263  if ( VmbErrorSuccess == res )
264  {
265  return SP_ACCESS( rInterface )->Open();
266  }
267 
268  return res;
269 }
270 
272 {
274 
275  // Begin write lock camera list
277  {
278  res = m_pImpl->UpdateCameraList();
279 
280  if ( VmbErrorSuccess == res )
281  {
282  if ( NULL == pCameras )
283  {
284  rnSize = (VmbUint32_t)m_pImpl->m_cameras.Map.size();
285  res = VmbErrorSuccess;
286  }
287  else if ( m_pImpl->m_cameras.Map.size() <= rnSize )
288  {
289  VmbUint32_t i = 0;
290  for ( CameraPtrMap::iterator iter = m_pImpl->m_cameras.Map.begin();
291  m_pImpl->m_cameras.Map.end() != iter;
292  ++iter, ++i )
293  {
294  pCameras[i] = iter->second;
295  }
296  rnSize = (VmbUint32_t)m_pImpl->m_cameras.Map.size();
297  res = VmbErrorSuccess;
298  }
299  else
300  {
301  res = VmbErrorMoreData;
302  }
303  }
304 
305  // End write lock camera list
307  }
308 
309  return res;
310 }
311 
312 VmbErrorType VimbaSystem::GetCameraByID( const char *pStrID, CameraPtr &rCamera )
313 {
314  if ( NULL == pStrID )
315  {
316  return VmbErrorBadParameter;
317  }
318 
320 
321  // Begin write lock camera list
323  {
324  // Try to identify the desired camera by its ID (in the list of known cameras)
325  CameraPtrMap::iterator iter = m_pImpl->m_cameras.Map.find( pStrID );
326  if ( m_pImpl->m_cameras.Map.end() != iter )
327  {
328  rCamera = iter->second;
329  res = VmbErrorSuccess;
330  }
331  else
332  {
333  // Try to identify the desired camera by IP or MAC address (in the list of known cameras)
334  if ( true == m_pImpl->m_bGeVTLPresent
335  && false == m_pImpl->m_bGeVDiscoveryAutoOn
336  && false == m_pImpl->IsIPAddress(pStrID) )
337  {
338  // HINT: We have to send one discovery packet in case we want to open a GigE cam (unless we open it by IP address)
339  res = VmbFeatureCommandRun( gVimbaHandle, "GeVDiscoveryAllOnce" );
340  if ( VmbErrorSuccess != res )
341  {
342  LOG_FREE_TEXT( "Could not ping camera over ethernet" )
343  }
344  }
345 
346  VmbCameraInfo_t camInfo;
347  res = VmbCameraInfoQuery( pStrID, &camInfo, sizeof camInfo );
348  if ( VmbErrorSuccess == res )
349  {
350  iter = m_pImpl->m_cameras.Map.find( camInfo.cameraIdString );
351  if ( m_pImpl->m_cameras.Map.end() != iter )
352  {
353  rCamera = iter->second;
354  }
355  else
356  {
357  // We don't know the camera because it is new or we have to
358  // try to identify it by IP or MAC address directly
359  std::string cameraIdString;
360  if ( std::strcmp( camInfo.cameraIdString, pStrID ))
361  {
362  // TODO: Remove this with interface change
363  cameraIdString.assign( camInfo.cameraIdString ).append( AVT_IP_OR_MAC_ADDRESS ).append( pStrID );
364  camInfo.cameraIdString = cameraIdString.c_str();
365  }
366  m_pImpl->AppendCamToMap( camInfo );
367 
368  iter = m_pImpl->m_cameras.Map.find( camInfo.cameraIdString );
369  if ( m_pImpl->m_cameras.Map.end() != iter )
370  {
371  rCamera = iter->second;
372  }
373  else
374  {
375  res = VmbErrorNotFound;
376  }
377  }
378  }
379  }
380 
381  // End write lock camera list
383  }
384 
385  return (VmbErrorType)res;
386 }
387 
388 VmbErrorType VimbaSystem::OpenCameraByID( const char *pStrID, VmbAccessModeType eAccessMode, CameraPtr &rCamera )
389 {
390  if ( NULL == pStrID )
391  {
392  return VmbErrorBadParameter;
393  }
394 
395  VmbErrorType res = GetCameraByID( pStrID, rCamera );
396  if ( VmbErrorSuccess == res )
397  {
398  return SP_ACCESS( rCamera )->Open( eAccessMode );
399  }
400 
401  return res;
402 }
403 
405 {
406  CameraPtr res;
407 
408  // Begin read lock camera list
410  {
411  for ( CameraPtrMap::const_iterator iter = m_pImpl->m_cameras.Map.begin();
412  m_pImpl->m_cameras.Map.end() != iter;
413  ++iter)
414  {
415  if ( SP_ACCESS( iter->second )->GetHandle() == handle )
416  {
417  res = iter->second;
418  break;
419  }
420  }
421 
422  // End read lock camera list
424  }
425  else
426  {
427  LOG_FREE_TEXT( "Could not lock camera list")
428  }
429 
430  return res;
431 }
432 
433 void VMB_CALL VimbaSystem::Impl::CameraDiscoveryCallback( const VmbHandle_t /*handle*/, const char* /*name*/, void* /*context*/ )
434 {
435  VmbError_t err;
436  std::vector<char> strID;
437  VmbUint32_t nCount = 0;
438 
439  // Get the ID of the camera that has triggered the callback
440  err = VmbFeatureStringMaxlengthQuery( gVimbaHandle, "DiscoveryCameraIdent", &nCount );
441  if ( 0 < nCount
442  && VmbErrorSuccess == err )
443  {
444  strID.resize( nCount );
445  err = VmbFeatureStringGet( gVimbaHandle, "DiscoveryCameraIdent", &strID[0], nCount, &nCount );
446  if ( VmbErrorSuccess == err )
447  {
449  const char* pReason = NULL;
450  VmbInt64_t nReason = 0;
451 
452  // Get the reason that has triggered the callback
453  err = VmbFeatureEnumGet( gVimbaHandle, "DiscoveryCameraEvent", &pReason );
454  if ( VmbErrorSuccess == err )
455  {
456  err = VmbFeatureEnumAsInt( gVimbaHandle, "DiscoveryCameraEvent", pReason, &nReason );
457  if ( VmbErrorSuccess == err )
458  {
459  switch ( nReason )
460  {
461  case 0: reason = UpdateTriggerPluggedOut;
462  break;
463  case 1: reason = UpdateTriggerPluggedIn;
464  break;
465  default: reason = UpdateTriggerOpenStateChanged;
466  }
467 
468  // Begin read lock camera list
470  {
471  CameraPtrMap::iterator iter = _instance.m_pImpl->m_cameras.Map.find( &strID[0] );
472  CameraPtr pCam;
473 
474  bool bFound;
475 
476  // Was the camera known before?
477  if ( _instance.m_pImpl->m_cameras.Map.end() != iter )
478  {
479  bFound = true;
480  pCam = iter->second;
481  }
482  else
483  {
484  bFound = false;
485  }
486 
487  // End read lock camera list
489 
490  // If the camera was not known before we query for it
491  if ( false == bFound )
492  {
493  err = _instance.GetCameraByID( &strID[0], pCam );
494  if ( VmbErrorSuccess != err )
495  {
496  err = VmbErrorInternalFault;
497  LOG_FREE_TEXT( "Could not find a known camera in camera list")
498  }
499  }
500 
501  // Now that we know about the reason for the callback and the camera we can call all registered observers
502  if ( VmbErrorSuccess == err )
503  {
504  // Begin read lock camera observer list
506  {
507  for ( ICameraListObserverPtrVector::iterator iter = _instance.m_pImpl->m_cameraObservers.Vector.begin();
508  _instance.m_pImpl->m_cameraObservers.Vector.end() != iter;
509  ++iter )
510  {
511  SP_ACCESS(( *iter ))->CameraListChanged( pCam, reason );
512  }
513 
514  // End read lock camera observer list
516  }
517  else
518  {
519  LOG_FREE_TEXT( "Could not lock camera observer list")
520  }
521  }
522  }
523  else
524  {
525  LOG_FREE_TEXT( "Could not lock camera list")
526  }
527  }
528  else
529  {
530  LOG_FREE_TEXT( "Could not get integer representation of enum string" )
531  }
532  }
533  else
534  {
535  LOG_FREE_TEXT( "Could not get callback trigger" )
536  }
537  }
538  else
539  {
540  LOG_FREE_TEXT( "Could not get camera ID" )
541  }
542  }
543  else
544  {
545  LOG_FREE_TEXT( "Could not get length of camera ID or length is 0" )
546  }
547 }
548 
549 void VMB_CALL VimbaSystem::Impl::InterfaceDiscoveryCallback( const VmbHandle_t /*handle*/, const char * /*name*/, void * /*context*/ )
550 {
551  VmbError_t err;
552  std::vector<char> strID;
553  VmbUint32_t nCount = 0;
554 
555  // Get the ID of the interface that has triggered the callback
556  err = VmbFeatureStringMaxlengthQuery( gVimbaHandle, "DiscoveryInterfaceIdent", &nCount );
557  if ( 0 < nCount
558  && VmbErrorSuccess == err )
559  {
560  strID.resize( nCount );
561  err = VmbFeatureStringGet( gVimbaHandle, "DiscoveryInterfaceIdent", &strID[0], nCount, &nCount );
562  }
563 
564  if ( VmbErrorSuccess == err )
565  {
566  // Begin read lock interface list
568  {
569  InterfacePtrMap::iterator iter = _instance.m_pImpl->m_interfaces.Map.find( &strID[0] );
570  InterfacePtr pInterface;
572  bool bFound;
573 
574  if ( _instance.m_pImpl->m_interfaces.Map.end() != iter )
575  {
576  bFound = true;
577  pInterface = iter->second;
578  }
579  else
580  {
581  bFound = false;
582  }
583 
584  // End read lock interface list
586 
587  // Begin write lock interface list
589  {
591 
592  // End write lock interface list
594 
595  if ( VmbErrorSuccess == err )
596  {
597  // Begin read lock interface list
599  {
600  iter = _instance.m_pImpl->m_interfaces.Map.find( &strID[0] );
601 
602  // The interface was known before
603  if ( true == bFound )
604  {
605  // The interface now has been removed
606  if ( _instance.m_pImpl->m_interfaces.Map.end() == iter )
607  {
608  reason = UpdateTriggerPluggedOut;
609  }
610  else
611  {
613  }
614  }
615  // The interface is new
616  else
617  {
618  if ( _instance.m_pImpl->m_interfaces.Map.end() != iter )
619  {
620  pInterface = iter->second;
621  reason = UpdateTriggerPluggedIn;
622  }
623  else
624  {
625  err = VmbErrorInternalFault;
626  // Do some logging
627  LOG_FREE_TEXT( "Could not find interface in interface list." )
628  }
629  }
630 
631  // End read lock interface list
633 
634  if ( VmbErrorSuccess == err )
635  {
636  // Begin read lock interface observer list
638  {
639  for ( IInterfaceListObserverPtrVector::iterator iter = _instance.m_pImpl->m_interfaceObservers.Vector.begin();
641  ++iter)
642  {
643  SP_ACCESS(( *iter ))->InterfaceListChanged( pInterface, reason );
644  }
645 
646  // End read lock interface observer list
648  }
649  else
650  {
651  LOG_FREE_TEXT( "Could not lock interface observer list")
652  }
653  }
654  }
655  else
656  {
657  LOG_FREE_TEXT( "Could not lock interface list")
658  }
659  }
660  }
661  }
662  else
663  {
664  LOG_FREE_TEXT( "Could not lock interface list")
665  }
666  }
667 }
668 
669 VmbErrorType VimbaSystem::RegisterCameraListObserver( const ICameraListObserverPtr &rObserver )
670 {
671  if ( SP_ISNULL( rObserver ))
672  {
673  return VmbErrorBadParameter;
674  }
675 
677 
678  // Begin write lock camera observer list
680  {
681  // The very same observer cannot be registered twice
682  for ( size_t i=0; i<m_pImpl->m_cameraObservers.Vector.size(); ++i )
683  {
684  if ( SP_ISEQUAL( rObserver, m_pImpl->m_cameraObservers.Vector[i] ))
685  {
686  res = VmbErrorInvalidCall;
687  break;
688  }
689  }
690 
691  if ( VmbErrorSuccess == res )
692  {
693  m_pImpl->m_cameraObservers.Vector.push_back( rObserver );
694 
695  if ( 1 == m_pImpl->m_cameraObservers.Vector.size() )
696  {
697  res = VmbFeatureInvalidationRegister( gVimbaHandle, "DiscoveryCameraEvent", m_pImpl->CameraDiscoveryCallback, this );
698  if ( VmbErrorSuccess == res
699  && true == m_pImpl->m_bGeVTLPresent )
700  {
701  // HINT: Without enabling GEVDiscovery registering a device observer is pointless
702  res = VmbFeatureCommandRun( gVimbaHandle, "GeVDiscoveryAllAuto" );
703  if ( VmbErrorSuccess == res )
704  {
706  }
707  }
708 
709  if ( VmbErrorSuccess != res )
710  {
711  // Rollback
712  m_pImpl->m_cameraObservers.Vector.pop_back();
713  // Do some logging
714  LOG_FREE_TEXT( "Could not register camera list observer" )
715  }
716  }
717  }
718 
719  // End write lock camera observer list
721  }
722 
723  return (VmbErrorType)res;
724 }
725 
726 VmbErrorType VimbaSystem::UnregisterCameraListObserver( const ICameraListObserverPtr &rObserver )
727 {
728  if ( SP_ISNULL( rObserver ))
729  {
730  return VmbErrorBadParameter;
731  }
732 
734 
735  // Begin exclusive write lock camera observer list
737  {
738  for ( ICameraListObserverPtrVector::iterator iter = m_pImpl->m_cameraObservers.Vector.begin();
739  m_pImpl->m_cameraObservers.Vector.end() != iter;)
740  {
741  if ( SP_ISEQUAL( rObserver, *iter ))
742  {
743  // If we are about to unregister the last observer we cancel all camera discovery notifications
744  if ( 1 == m_pImpl->m_cameraObservers.Vector.size() )
745  {
747  if ( VmbErrorSuccess == res
748  && true == m_pImpl->m_bGeVTLPresent )
749  {
750  // HINT: After unregistering the last device observer we do not need to send discovery pings anymore
751  res = VmbFeatureCommandRun( gVimbaHandle, "GeVDiscoveryAllOff" );
752  if ( VmbErrorSuccess == res )
753  {
755  }
756  else
757  {
758  // Rollback
760  }
761  }
762  }
763 
764  if ( VmbErrorSuccess == res
765  || 1 < m_pImpl->m_cameraObservers.Vector.size() )
766  {
767  iter = m_pImpl->m_cameraObservers.Vector.erase( iter );
768  res = VmbErrorSuccess;
769  }
770  break;
771  }
772  else
773  {
774  ++iter;
775  }
776  }
777 
778  // End write lock camera observer list
780  }
781  else
782  {
783  LOG_FREE_TEXT( "Could not lock camera observer list.")
784  res = VmbErrorInternalFault;
785  }
786 
787  return (VmbErrorType)res;
788 }
789 
790 VmbErrorType VimbaSystem::RegisterInterfaceListObserver( const IInterfaceListObserverPtr &rObserver )
791 {
792  if ( SP_ISNULL( rObserver ))
793  {
794  return VmbErrorBadParameter;
795  }
796 
798 
799  // Begin write lock interface observer list
801  {
802  // The very same observer cannot be registered twice
803  for ( size_t i=0; i<m_pImpl->m_interfaceObservers.Vector.size(); ++i )
804  {
805  if ( SP_ISEQUAL( rObserver, m_pImpl->m_interfaceObservers.Vector[i] ))
806  {
807  res = VmbErrorInvalidCall;
808  break;
809  }
810  }
811 
812  if ( VmbErrorSuccess == res )
813  {
814  m_pImpl->m_interfaceObservers.Vector.push_back( rObserver );
815 
816  if ( 1 == m_pImpl->m_interfaceObservers.Vector.size() )
817  {
818  res = VmbFeatureInvalidationRegister( gVimbaHandle, "DiscoveryInterfaceEvent", m_pImpl->InterfaceDiscoveryCallback, this );
819 
820  if ( VmbErrorSuccess != res )
821  {
822  // Rollback
823  m_pImpl->m_interfaceObservers.Vector.pop_back();
824 
825  // Do some logging
826  LOG_FREE_TEXT( "Could not register interface list observer" )
827  }
828  }
829  }
830 
831  // End write lock interface observer list
833  }
834 
835  return (VmbErrorType)res;
836 }
837 
838 VmbErrorType VimbaSystem::UnregisterInterfaceListObserver( const IInterfaceListObserverPtr &rObserver )
839 {
840  if ( SP_ISNULL( rObserver ))
841  {
842  return VmbErrorBadParameter;
843  }
844 
846 
847  // Begin exclusive write lock interface observer list
849  {
850  for ( IInterfaceListObserverPtrVector::iterator iter = m_pImpl->m_interfaceObservers.Vector.begin();
851  m_pImpl->m_interfaceObservers.Vector.end() != iter;)
852  {
853  if ( SP_ISEQUAL( rObserver, *iter ))
854  {
855  // If we are about to unregister the last observer we cancel all interface discovery notifications
856  if ( 1 == m_pImpl->m_interfaceObservers.Vector.size() )
857  {
859  }
860  if ( VmbErrorSuccess == res
861  || 1 < m_pImpl->m_interfaceObservers.Vector.size() )
862  {
863  iter = m_pImpl->m_interfaceObservers.Vector.erase( iter );
864  res = VmbErrorSuccess;
865  }
866  break;
867  }
868  else
869  {
870  ++iter;
871  }
872  }
873 
874  // End write lock interface observer list
876  }
877  else
878  {
879  LOG_FREE_TEXT( "Could not lock interface observer list.")
880  res = VmbErrorInternalFault;
881  }
882 
883  return (VmbErrorType)res;
884 }
885 
886 VmbErrorType VimbaSystem::RegisterCameraFactory( const ICameraFactoryPtr &cameraFactory )
887 {
888  if ( SP_ISNULL( cameraFactory ))
889  {
890  return VmbErrorBadParameter;
891  }
892 
893  m_pImpl->m_pCameraFactory = cameraFactory;
894 
895  return VmbErrorSuccess;
896 }
897 
899 {
900  m_pImpl->m_pCameraFactory = ICameraFactoryPtr( new DefaultCameraFactory() );
901 
903  {
904  return VmbErrorInternalFault;
905  }
906 
907  return VmbErrorSuccess;
908 }
909 
910 // Singleton
912  : m_pImpl( new Impl() )
913 {
915  m_pImpl->m_bGeVTLPresent = false;
917  m_pImpl->m_pCameraFactory = ICameraFactoryPtr( new DefaultCameraFactory() );
918 }
919 
920 // Singleton
922 {
923  // No generated copy ctor
924 }
925 
927 {
928  // No assignment operator
929  return *this;
930 }
931 
933 {
934  delete m_pImpl->m_pLogger;
935  delete m_pImpl;
936 }
937 
938 // Instance
940 
941 // Gets a list of all connected interfaces and updates the internal interfaces map accordingly.
942 // Reference counting for removed interfaces is decreased,
943 // new interfaces are added.
945 {
946  std::vector<VmbInterfaceInfo_t> interfaceInfos;
947  VmbErrorType res = GetInterfaceList( interfaceInfos );
948  VmbUint32_t nCount = (VmbUint32_t)interfaceInfos.size();
949 
950  if ( VmbErrorSuccess == res )
951  {
952  InterfacePtrMap::iterator iter = m_interfaces.Map.begin();
953  std::vector<VmbInterfaceInfo_t>::iterator iterInfo = interfaceInfos.begin();
954  bool bFound = false;
955 
956  // Delete removed Interfaces from m_interfaces
957  while ( m_interfaces.Map.end() != iter )
958  {
959  for ( VmbUint32_t i=0; i<nCount; ++i, ++iterInfo )
960  {
961  if ( iterInfo->interfaceIdString == iter->first )
962  {
963  bFound = true;
964  break;
965  }
966  }
967 
968  if ( false == bFound )
969  {
970  m_interfaces.Map.erase( iter++ );
971  }
972  else
973  {
974  ++iter;
975  }
976 
977  bFound = false;
978  iterInfo = interfaceInfos.begin();
979  }
980 
981  // Add new Interfaces to m_Interfaces
982  while ( 0 < nCount-- )
983  {
984  iter = m_interfaces.Map.find( iterInfo->interfaceIdString );
985 
986  if ( m_interfaces.Map.end() == iter )
987  {
988  SP_SET( m_interfaces.Map[iterInfo->interfaceIdString], new Interface( &(*iterInfo) ));
989  }
990 
991  ++iterInfo;
992  }
993  }
994 
995  return res;
996 }
997 
998 // Gets a list of all connected cameras and updates the internal cameras map accordingly.
999 // Reference counting for removed cameras is decreased,
1000 // new cameras are added.
1002 {
1004  VmbUint32_t nCount = 0;
1005  std::vector<VmbCameraInfo_t> cameraInfos( 10 );
1006 
1007  // HINT: We explicitly have to enable GeVDiscovery to be able to use UpdateCameraList.
1008  if ( true == m_bGeVTLPresent
1009  && false == m_bGeVDiscoveryAutoOn )
1010  {
1011  res = VmbFeatureCommandRun( gVimbaHandle, "GeVDiscoveryAllOnce" );
1012  }
1013  try
1014  {
1015  if ( VmbErrorSuccess == res )
1016  {
1017  // First get 10 cameras at most
1018  res = VmbCamerasList( &cameraInfos[0], (VmbUint32_t)cameraInfos.size(), &nCount, sizeof(VmbCameraInfo_t) );
1019  // If there are more get them eventually
1020  // If even more new cameras were discovered in between the function calls we increase the allocated memory consecutively
1021  while ( VmbErrorMoreData == res )
1022  {
1023  cameraInfos.resize( nCount );
1024  res = VmbCamerasList( &cameraInfos[0], (VmbUint32_t)cameraInfos.size(), &nCount, sizeof(VmbCameraInfo_t) );
1025  }
1026  }
1027 
1028  if ( VmbErrorSuccess == res )
1029  {
1030  if( 0 != nCount )
1031  {
1032  if( nCount < cameraInfos.size() )
1033  {
1034  cameraInfos.resize( nCount );
1035  }
1036  CameraPtrMap::iterator mapPos = m_cameras.Map.begin();
1037  typedef std::vector<VmbCameraInfo_t>::const_iterator const_info_iterator;
1038 
1039  // Delete removed cameras from m_cameras
1040  while ( m_cameras.Map.end() != mapPos )
1041  {
1042  bool bFound = false;
1043  for( const_info_iterator infoPos = cameraInfos.begin(); cameraInfos.end() != infoPos; ++infoPos )
1044  {
1045  if ( infoPos->cameraIdString == mapPos->first )
1046  {
1047  bFound = true;
1048  break;
1049  }
1050  }
1051 
1052  if ( false == bFound )
1053  {
1054  m_cameras.Map.erase( mapPos++ );
1055  }
1056  else
1057  {
1058  ++mapPos;
1059  }
1060  }
1061 
1062  // Add new cameras to m_cameras
1063  for (const_info_iterator infoPos= cameraInfos.begin(); infoPos != cameraInfos.end(); ++infoPos )
1064  {
1065  CameraPtrMap::const_iterator findPos = m_cameras.Map.find( infoPos->cameraIdString );
1066 
1067  if ( m_cameras.Map.end() == findPos )
1068  {
1069  AppendCamToMap( *infoPos );
1070  }
1071  }
1072  }
1073  else
1074  {
1075  m_cameras.Map.clear();
1076  }
1077  }
1078  }
1079  catch( const std::bad_alloc& /*badAlloc*/ )
1080  {
1081  return VmbErrorResources;
1082  }
1083 
1084 
1085  return (VmbErrorType)res;
1086 }
1087 
1089 {
1090  return m_pImpl->m_pLogger;
1091 }
1092 
1093 bool VimbaSystem::Impl::IsIPAddress( const char *pStrID )
1094 {
1095  if( NULL == pStrID )
1096  {
1097  return false;
1098  }
1099 
1100  size_t nCount = 0;
1101  size_t nSize = 0;
1102  size_t nIndex = 0;
1103  while( pStrID[nIndex] != '\0' )
1104  {
1105  if( isdigit( pStrID[nIndex] ) != 0 )
1106  {
1107  if( nSize >= 3 )
1108  {
1109  return false;
1110  }
1111  nSize++;
1112  }
1113  else if( '.' == pStrID[nIndex] )
1114  {
1115  if( (nSize <= 0)
1116  || (nSize > 3)
1117  || (nCount >= 3) )
1118  {
1119  return false;
1120  }
1121  nCount++;
1122  nSize = 0;
1123  }
1124  else
1125  {
1126  return false;
1127  }
1128 
1129  nIndex++;
1130  }
1131  if( (nSize <= 0)
1132  || (nSize > 3)
1133  || (nCount != 3) )
1134  {
1135  return false;
1136  }
1137 
1138  return true;
1139 }
1140 
1142 {
1143  InterfacePtr pInterface;
1144  std::string strInterfaceName,
1145  strInterfaceSerial;
1146  VmbAccessModeType interfaceAccess;
1147  VmbInterfaceType interfaceType;
1148 
1149  // HINT: Before inserting (and potentially overwriting) a camera, we check whether it is present already
1150  if ( m_cameras.Map.end() == m_cameras.Map.find( camInfo.cameraIdString ))
1151  {
1152  if ( VmbErrorSuccess == _instance.GetInterfaceByID( camInfo.interfaceIdString, pInterface ))
1153  {
1154  if ( VmbErrorSuccess == SP_ACCESS( pInterface )->GetName( strInterfaceName )
1155  && VmbErrorSuccess == SP_ACCESS( pInterface )->GetSerialNumber( strInterfaceSerial )
1156  && VmbErrorSuccess == SP_ACCESS( pInterface )->GetPermittedAccess( interfaceAccess )
1157  && VmbErrorSuccess == SP_ACCESS( pInterface )->GetType( interfaceType ))
1158  {
1159  try
1160  {
1161  // TODO: Remove pCam with Interface change
1162  CameraPtr pCam = SP_ACCESS( m_pCameraFactory )->CreateCamera( camInfo.cameraIdString,
1163  camInfo.cameraName,
1164  camInfo.modelName,
1165  camInfo.serialString,
1166  camInfo.interfaceIdString,
1167  interfaceType,
1168  strInterfaceName.c_str(),
1169  strInterfaceSerial.c_str(),
1170  interfaceAccess );
1171  // TODO: Remove with interface change
1172  char* strTemp = (char*)strstr( camInfo.cameraIdString, AVT_IP_OR_MAC_ADDRESS );
1173  if ( strTemp )
1174  {
1175  *strTemp = '\0';
1176  }
1177  m_cameras.Map[camInfo.cameraIdString] = pCam;
1178  }
1179  catch( ... )
1180  {
1181  // Do some logging
1182  LOG_FREE_TEXT( "Could not create camera" )
1183  }
1184 
1185  CameraPtrMap::iterator iter = m_cameras.Map.find( camInfo.cameraIdString );
1186  if ( m_cameras.Map.end() != iter
1187  && SP_ISNULL( iter->second ))
1188  {
1189  m_cameras.Map.erase( iter );
1190  // Do some logging
1191  LOG_FREE_TEXT( "NULL camera created" )
1192  }
1193  }
1194  else // Could not get interface infos
1195  {
1196  // Do some logging
1197  LOG_FREE_TEXT( "Could not get interface infos" )
1198  }
1199  }
1200  else // Could not get interface
1201  {
1202  // Do some logging
1203  LOG_FREE_TEXT( "Could not get interface" )
1204  }
1205  }
1206 }
1207 
1208 VmbErrorType VimbaSystem::Impl::GetInterfaceList( std::vector<VmbInterfaceInfo_t> &rInterfaceInfos )
1209 {
1210  VmbError_t res;
1211  VmbUint32_t nCount;
1212 
1213  res = VmbInterfacesList( NULL, 0, &nCount, sizeof(VmbInterfaceInfo_t));
1214  if ( VmbErrorSuccess == res )
1215  {
1216  rInterfaceInfos.resize( nCount );
1217  res = VmbInterfacesList( &rInterfaceInfos[0], nCount, &nCount, sizeof(VmbInterfaceInfo_t));
1218  }
1219 
1220  return (VmbErrorType)res;
1221 }
1222 
1223 }} // namespace AVT::VmbAPI
IMEXPORTC VmbError_t VMB_CALL VmbFeatureCommandRun(const VmbHandle_t handle, const char *name)
VmbInt32_t VmbError_t
IMEXPORT VmbErrorType GetCameraByID(const char *pID, CameraPtr &pCamera)
static IMEXPORT VimbaSystem & GetInstance()
Definition: VimbaSystem.cpp:79
std::map< std::string, CameraPtr > CameraPtrMap
Definition: VimbaSystem.cpp:43
bool EnterWriteLock(BasicLockable &rLockable, bool bExclusive=false)
#define SP_ISNULL(sp)
const char * cameraIdString
Definition: VimbaC.h:152
VmbErrorType GetInterfaceList(std::vector< VmbInterfaceInfo_t > &interfaceInfos)
std::vector< T > Vector
Definition: Helper.h:41
IMEXPORTC VmbError_t VMB_CALL VmbFeatureStringMaxlengthQuery(const VmbHandle_t handle, const char *name, VmbUint32_t *pMaxLength)
#define VIMBACPP_VERSION_PATCH
Definition: Version.h:6
long long VmbInt64_t
IMEXPORTC VmbError_t VMB_CALL VmbFeatureStringGet(const VmbHandle_t handle, const char *name, char *buffer, VmbUint32_t bufferSize, VmbUint32_t *pSizeFilled)
#define SP_ACCESS(sp)
IMEXPORT VmbErrorType Startup()
Definition: VimbaSystem.cpp:93
IMEXPORT VmbErrorType RegisterCameraListObserver(const ICameraListObserverPtr &pObserver)
VmbUint32_t minor
VmbErrorType GetInterfaces(InterfacePtrVector &interfaces)
Logger GetLogger() const
char const *const AVT_IP_OR_MAC_ADDRESS
Definition: Helper.h:51
static void VMB_CALL InterfaceDiscoveryCallback(const VmbHandle_t handle, const char *name, void *context)
LockableMap< std::string, InterfacePtr > m_interfaces
Definition: VimbaSystem.cpp:51
const char * cameraName
Definition: VimbaC.h:153
std::map< std::string, InterfacePtr > InterfacePtrMap
Definition: VimbaSystem.cpp:44
CameraPtr GetCameraPtrByHandle(const VmbHandle_t handle) const
void ExitWriteLock(BasicLockable &rLockable)
IMEXPORT VmbErrorType OpenCameraByID(const char *pID, VmbAccessModeType eAccessMode, CameraPtr &pCamera)
LOGGER_DECL * Logger
Definition: LoggerDefines.h:47
VmbErrorType
static const VmbHandle_t gVimbaHandle
Definition: VimbaC.h:102
IMEXPORTC VmbError_t VMB_CALL VmbFeatureInvalidationRegister(const VmbHandle_t handle, const char *name, VmbInvalidationCallback callback, void *pUserContext)
VmbAccessModeType
Definition: VimbaC.h:122
static VimbaSystem _instance
Definition: VimbaSystem.h:363
#define LOG_FREE_TEXT(txt)
Definition: LoggerDefines.h:57
ConditionHelper m_cameraObserversConditionHelper
Definition: VimbaSystem.cpp:55
void * VmbHandle_t
VmbUint32_t major
VmbErrorType GetCameras(CameraPtrVector &cameras)
IMEXPORTC VmbError_t VMB_CALL VmbFeatureInvalidationUnregister(const VmbHandle_t handle, const char *name, VmbInvalidationCallback callback)
IMEXPORTC VmbError_t VMB_CALL VmbCameraInfoQuery(const char *idString, VmbCameraInfo_t *pInfo, VmbUint32_t sizeofCameraInfo)
VimbaSystem & operator=(const VimbaSystem &system)
IMEXPORT VmbErrorType RegisterInterfaceListObserver(const IInterfaceListObserverPtr &pObserver)
IMEXPORT VmbErrorType RegisterCameraFactory(const ICameraFactoryPtr &pCameraFactory)
const char * interfaceIdString
Definition: VimbaC.h:157
ConditionHelper m_interfaceObserversConditionHelper
Definition: VimbaSystem.cpp:57
std::map< T1, T2 > Map
Definition: Helper.h:48
VmbUint32_t patch
LockableMap< std::string, CameraPtr > m_cameras
Definition: VimbaSystem.cpp:49
NetPointer< Camera, AVT::VmbAPINET::Camera > CameraPtr
void AppendCamToMap(VmbCameraInfo_t camInfo)
IMEXPORTC VmbError_t VMB_CALL VmbInterfacesList(VmbInterfaceInfo_t *pInterfaceInfo, VmbUint32_t listLength, VmbUint32_t *pNumFound, VmbUint32_t sizeofInterfaceInfo)
#define LOGGER_DEF
Definition: LoggerDefines.h:42
const char * serialString
Definition: VimbaC.h:155
IMEXPORTC VmbError_t VMB_CALL VmbStartup(void)
const char * modelName
Definition: VimbaC.h:154
IMEXPORT VmbErrorType UnregisterInterfaceListObserver(const IInterfaceListObserverPtr &pObserver)
IMEXPORTC VmbError_t VMB_CALL VmbFeatureEnumAsInt(const VmbHandle_t handle, const char *name, const char *value, VmbInt64_t *pIntVal)
IMEXPORT VmbErrorType QueryVersion(VmbVersionInfo_t &version)
Definition: VimbaSystem.cpp:84
NetPointer< Interface, AVT::VmbAPINET::Interface > InterfacePtr
#define SP_SET(sp, rawPtr)
#define SP_ISEQUAL(sp1, sp2)
VmbInterfaceType
Definition: VimbaC.h:107
void ExitReadLock(BasicLockable &rLockable)
ConditionHelper m_camerasConditionHelper
Definition: VimbaSystem.cpp:50
unsigned int VmbUint32_t
bool EnterReadLock(BasicLockable &rLockable)
IMEXPORTC VmbError_t VMB_CALL VmbFeatureEnumGet(const VmbHandle_t handle, const char *name, const char **pValue)
IMEXPORTC VmbError_t VMB_CALL VmbFeatureBoolGet(const VmbHandle_t handle, const char *name, VmbBool_t *pValue)
LockableVector< ICameraListObserverPtr > m_cameraObservers
Definition: VimbaSystem.cpp:54
IMEXPORTC VmbError_t VMB_CALL VmbCamerasList(VmbCameraInfo_t *pCameraInfo, VmbUint32_t listLength, VmbUint32_t *pNumFound, VmbUint32_t sizeofCameraInfo)
IMEXPORT VmbErrorType UnregisterCameraFactory()
IMEXPORTC void VMB_CALL VmbShutdown(void)
ConditionHelper m_interfacesConditionHelper
Definition: VimbaSystem.cpp:52
#define VIMBACPP_VERSION_MAJOR
Definition: Version.h:4
IMEXPORT VmbErrorType Shutdown()
IMEXPORT VmbErrorType UnregisterCameraListObserver(const ICameraListObserverPtr &pObserver)
static void VMB_CALL CameraDiscoveryCallback(const VmbHandle_t handle, const char *name, void *context)
#define VIMBACPP_VERSION_MINOR
Definition: Version.h:5
bool IsIPAddress(const char *pStrID)
ICameraFactoryPtr m_pCameraFactory
Definition: VimbaSystem.cpp:63
LockableVector< IInterfaceListObserverPtr > m_interfaceObservers
Definition: VimbaSystem.cpp:56
IMEXPORT VmbErrorType GetInterfaceByID(const char *pID, InterfacePtr &pInterface)
IMEXPORT VmbErrorType OpenInterfaceByID(const char *pID, InterfacePtr &pInterface)


avt_vimba_camera
Author(s): Miquel Massot , Allied Vision Technologies
autogenerated on Wed Jun 5 2019 22:22:40