system.cc
Go to the documentation of this file.
00001 /*
00002  * This file is part of the rc_genicam_api package.
00003  *
00004  * Copyright (c) 2017 Roboception GmbH
00005  * All rights reserved
00006  *
00007  * Author: Heiko Hirschmueller
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright notice,
00013  * this list of conditions and the following disclaimer.
00014  *
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  * this list of conditions and the following disclaimer in the documentation
00017  * and/or other materials provided with the distribution.
00018  *
00019  * 3. Neither the name of the copyright holder nor the names of its contributors
00020  * may be used to endorse or promote products derived from this software without
00021  * specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  * POSSIBILITY OF SUCH DAMAGE.
00034  */
00035 
00036 #include "system.h"
00037 
00038 #include "gentl_wrapper.h"
00039 #include "exception.h"
00040 #include "interface.h"
00041 #include "cport.h"
00042 
00043 #include <iostream>
00044 
00045 namespace rcg
00046 {
00047 
00048 System::~System()
00049 {
00050   if (n_open > 0 && tl != 0)
00051   {
00052     gentl->TLClose(tl);
00053   }
00054 
00055   gentl->GCCloseLib();
00056 }
00057 
00058 namespace
00059 {
00060 
00061 std::vector<std::shared_ptr<System> > slist;
00062 
00063 int find(const std::vector<std::shared_ptr<System> > &list, const std::string &filename)
00064 {
00065   for (size_t i=0; i<list.size(); i++)
00066   {
00067     if (list[i]->getFilename() == filename)
00068     {
00069       return i;
00070     }
00071   }
00072 
00073   return -1;
00074 }
00075 
00076 }
00077 
00078 std::vector<std::shared_ptr<System> > System::getSystems()
00079 {
00080   std::vector<std::shared_ptr<System> > ret;
00081 
00082   // get list of all available transport layer libraries
00083 
00084   const char *env=0;
00085   if (sizeof(size_t) == 8)
00086   {
00087     env="GENICAM_GENTL64_PATH";
00088   }
00089   else
00090   {
00091     env="GENICAM_GENTL32_PATH";
00092   }
00093 
00094   const char *path=std::getenv(env);
00095 
00096   if (path == 0 || *path == '\0')
00097   {
00098     path=GENTL_INSTALL_PATH;
00099   }
00100 
00101   std::vector<std::string> name=getAvailableGenTLs(path);
00102 
00103   // create list of systems according to the list, using either existing
00104   // systems or instantiating new ones
00105 
00106   for (size_t i=0; i<name.size(); i++)
00107   {
00108     int k=find(slist, name[i]);
00109 
00110     if (k >= 0)
00111     {
00112       ret.push_back(slist[k]);
00113     }
00114     else
00115     {
00116       try
00117       {
00118         System *p=new System(name[i]);
00119         ret.push_back(std::shared_ptr<System>(p));
00120       }
00121       catch (const std::exception &ex)
00122       {
00123         // ingnore transport layers that cannot be used
00124       }
00125     }
00126   }
00127 
00128   // remember returned list for reusing existing systems on the next call
00129 
00130   slist=ret;
00131 
00132   // throw exception if no transport layers are available
00133 
00134   if (ret.size() == 0)
00135   {
00136     throw GenTLException(std::string("No transport layers found in path ")+env);
00137   }
00138 
00139   return ret;
00140 }
00141 
00142 void System::clearSystems()
00143 {
00144   slist.clear();
00145 }
00146 
00147 const std::string &System::getFilename() const
00148 {
00149   return filename;
00150 }
00151 
00152 void System::open()
00153 {
00154   if (n_open == 0)
00155   {
00156     if (gentl->TLOpen(&tl) != GenTL::GC_ERR_SUCCESS)
00157     {
00158       throw GenTLException("System::open()", gentl);
00159     }
00160   }
00161 
00162   n_open++;
00163 }
00164 
00165 void System::close()
00166 {
00167   if (n_open > 0)
00168   {
00169     n_open--;
00170   }
00171 
00172   if (n_open == 0)
00173   {
00174     gentl->TLClose(tl);
00175     tl=0;
00176 
00177     nodemap=0;
00178     cport=0;
00179   }
00180 }
00181 
00182 namespace
00183 {
00184 
00185 int find(const std::vector<std::shared_ptr<Interface> > &list, const std::string &id)
00186 {
00187   for (size_t i=0; i<list.size(); i++)
00188   {
00189     if (list[i]->getID() == id)
00190     {
00191       return i;
00192     }
00193   }
00194 
00195   return -1;
00196 }
00197 
00198 }
00199 
00200 std::vector<std::shared_ptr<Interface> > System::getInterfaces()
00201 {
00202   std::vector<std::shared_ptr<Interface> > ret;
00203 
00204   if (tl != 0)
00205   {
00206     // get list of previously requested interfaces that are still in use
00207 
00208     std::vector<std::shared_ptr<Interface> > current;
00209 
00210     for (size_t i=0; i<ilist.size(); i++)
00211     {
00212       std::shared_ptr<Interface> p=ilist[i].lock();
00213       if (p)
00214       {
00215         current.push_back(p);
00216       }
00217     }
00218 
00219     // update available interfaces
00220 
00221     if (gentl->TLUpdateInterfaceList(tl, 0, 10) != GenTL::GC_ERR_SUCCESS)
00222     {
00223       throw GenTLException("System::getInterfaces()", gentl);
00224     }
00225 
00226     // create list of interfaces, using either existing interfaces or
00227     // instantiating new ones
00228 
00229     uint32_t n=0;
00230     if (gentl->TLGetNumInterfaces(tl, &n) != GenTL::GC_ERR_SUCCESS)
00231     {
00232       throw GenTLException("System::getInterfaces()", gentl);
00233     }
00234 
00235     for (uint32_t i=0; i<n; i++)
00236     {
00237       char tmp[256]="";
00238       size_t size=sizeof(tmp);
00239 
00240       if (gentl->TLGetInterfaceID(tl, i, tmp, &size) != GenTL::GC_ERR_SUCCESS)
00241       {
00242         throw GenTLException("System::getInterfaces()", gentl);
00243       }
00244 
00245       int k=find(current, tmp);
00246 
00247       if (k >= 0)
00248       {
00249         ret.push_back(current[k]);
00250       }
00251       else
00252       {
00253         ret.push_back(std::shared_ptr<Interface>(new Interface(shared_from_this(), gentl, tmp)));
00254       }
00255     }
00256 
00257     // update internal list of interfaces for reusage on next call
00258 
00259     ilist.clear();
00260     for (size_t i=0; i<ret.size(); i++)
00261     {
00262       ilist.push_back(ret[i]);
00263     }
00264   }
00265 
00266   return ret;
00267 }
00268 
00269 namespace
00270 {
00271 
00272 std::string cTLGetInfo(GenTL::TL_HANDLE tl, const std::shared_ptr<const GenTLWrapper> &gentl,
00273                        GenTL::TL_INFO_CMD info)
00274 {
00275   std::string ret;
00276 
00277   GenTL::INFO_DATATYPE type;
00278   char tmp[1024]="";
00279   size_t tmp_size=sizeof(tmp);
00280   GenTL::GC_ERROR err=GenTL::GC_ERR_SUCCESS;
00281 
00282   if (tl != 0)
00283   {
00284     err=gentl->TLGetInfo(tl, info, &type, tmp, &tmp_size);
00285   }
00286   else
00287   {
00288     err=gentl->GCGetInfo(info, &type, tmp, &tmp_size);
00289   }
00290 
00291   if (err == GenTL::GC_ERR_SUCCESS && type == GenTL::INFO_DATATYPE_STRING)
00292   {
00293     for (size_t i=0; i<tmp_size && tmp[i] != '\0'; i++)
00294     {
00295       ret.push_back(tmp[i]);
00296     }
00297   }
00298 
00299   return ret;
00300 }
00301 
00302 }
00303 
00304 std::string System::getID() const
00305 {
00306   return cTLGetInfo(tl, gentl, GenTL::TL_INFO_ID);
00307 }
00308 
00309 std::string System::getVendor() const
00310 {
00311   return cTLGetInfo(tl, gentl, GenTL::TL_INFO_VENDOR);
00312 }
00313 
00314 std::string System::getModel() const
00315 {
00316   return cTLGetInfo(tl, gentl, GenTL::TL_INFO_MODEL);
00317 }
00318 
00319 std::string System::getVersion() const
00320 {
00321   return cTLGetInfo(tl, gentl, GenTL::TL_INFO_VERSION);
00322 }
00323 
00324 std::string System::getTLType() const
00325 {
00326   return cTLGetInfo(tl, gentl, GenTL::TL_INFO_TLTYPE);
00327 }
00328 
00329 std::string System::getPathname() const
00330 {
00331   return cTLGetInfo(tl, gentl, GenTL::TL_INFO_PATHNAME);
00332 }
00333 
00334 bool System::isCharEncodingASCII() const
00335 {
00336   bool ret=true;
00337 
00338   GenTL::INFO_DATATYPE type;
00339   int32_t v;
00340   size_t size=sizeof(v);
00341   GenTL::GC_ERROR err=GenTL::GC_ERR_SUCCESS;
00342 
00343   if (tl != 0)
00344   {
00345     err=gentl->TLGetInfo(tl, GenTL::TL_INFO_CHAR_ENCODING, &type, &v, &size);
00346   }
00347   else
00348   {
00349     err=gentl->GCGetInfo(GenTL::TL_INFO_CHAR_ENCODING, &type, &v, &size);
00350   }
00351 
00352   if (err == GenTL::GC_ERR_SUCCESS && type == GenTL::INFO_DATATYPE_INT32 &&
00353       v != GenTL::TL_CHAR_ENCODING_ASCII)
00354   {
00355     ret=false;
00356   }
00357 
00358   return ret;
00359 }
00360 
00361 int System::getMajorVersion() const
00362 {
00363   uint32_t ret=0;
00364 
00365   GenTL::INFO_DATATYPE type;
00366   size_t size=sizeof(ret);
00367 
00368   if (tl != 0)
00369   {
00370     gentl->TLGetInfo(tl, GenTL::TL_INFO_GENTL_VER_MAJOR, &type, &ret, &size);
00371   }
00372   else
00373   {
00374     gentl->GCGetInfo(GenTL::TL_INFO_GENTL_VER_MAJOR, &type, &ret, &size);
00375   }
00376 
00377   return ret;
00378 }
00379 
00380 int System::getMinorVersion() const
00381 {
00382   uint32_t ret=0;
00383 
00384   GenTL::INFO_DATATYPE type;
00385   size_t size=sizeof(ret);
00386 
00387   if (tl != 0)
00388   {
00389     gentl->TLGetInfo(tl, GenTL::TL_INFO_GENTL_VER_MINOR, &type, &ret, &size);
00390   }
00391   else
00392   {
00393     gentl->GCGetInfo(GenTL::TL_INFO_GENTL_VER_MINOR, &type, &ret, &size);
00394   }
00395 
00396   return ret;
00397 }
00398 
00399 std::shared_ptr<GenApi::CNodeMapRef> System::getNodeMap()
00400 {
00401   if (tl != 0 && !nodemap)
00402   {
00403     cport=std::shared_ptr<CPort>(new CPort(gentl, &tl));
00404     nodemap=allocNodeMap(gentl, tl, cport.get());
00405   }
00406 
00407   return nodemap;
00408 }
00409 
00410 void *System::getHandle() const
00411 {
00412   return tl;
00413 }
00414 
00415 System::System(const std::string &_filename)
00416 {
00417   filename=_filename;
00418 
00419   gentl=std::shared_ptr<const GenTLWrapper>(new GenTLWrapper(filename));
00420 
00421   if (gentl->GCInitLib() != GenTL::GC_ERR_SUCCESS)
00422   {
00423     throw GenTLException("System::System()", gentl);
00424   }
00425 
00426   n_open=0;
00427   tl=0;
00428 }
00429 
00430 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06