interface.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 "interface.h"
00037 #include "device.h"
00038 
00039 #include "gentl_wrapper.h"
00040 #include "exception.h"
00041 #include "cport.h"
00042 
00043 #include <iostream>
00044 
00045 namespace rcg
00046 {
00047 
00048 Interface::Interface(const std::shared_ptr<System> &_parent,
00049                      const std::shared_ptr<const GenTLWrapper> &_gentl, const char *_id)
00050 {
00051   parent=_parent;
00052   gentl=_gentl;
00053   id=_id;
00054 
00055   n_open=0;
00056   ifh=0;
00057 }
00058 
00059 Interface::~Interface()
00060 {
00061   if (n_open > 0)
00062   {
00063     gentl->IFClose(ifh);
00064     parent->close();
00065   }
00066 }
00067 
00068 std::shared_ptr<System> Interface::getParent() const
00069 {
00070   return parent;
00071 }
00072 
00073 const std::string &Interface::getID() const
00074 {
00075   return id;
00076 }
00077 
00078 void Interface::open()
00079 {
00080   if (n_open == 0)
00081   {
00082     parent->open();
00083 
00084     // updating before opening is apparently necessary for some TLs
00085 
00086     gentl->TLUpdateInterfaceList(parent->getHandle(), 0, 10);
00087 
00088     if (gentl->TLOpenInterface(parent->getHandle(), id.c_str(), &ifh) != GenTL::GC_ERR_SUCCESS)
00089     {
00090       parent->close();
00091       throw GenTLException("Interface::open()", gentl);
00092     }
00093   }
00094 
00095   n_open++;
00096 }
00097 
00098 void Interface::close()
00099 {
00100   if (n_open > 0)
00101   {
00102     n_open--;
00103   }
00104 
00105   if (n_open == 0)
00106   {
00107     gentl->IFClose(ifh);
00108     ifh=0;
00109 
00110     nodemap=0;
00111     cport=0;
00112 
00113     parent->close();
00114   }
00115 }
00116 
00117 namespace
00118 {
00119 
00120 int find(const std::vector<std::shared_ptr<Device> > &list, const std::string &id)
00121 {
00122   for (size_t i=0; i<list.size(); i++)
00123   {
00124     if (list[i]->getID() == id)
00125     {
00126       return i;
00127     }
00128   }
00129 
00130   return -1;
00131 }
00132 
00133 }
00134 
00135 std::vector<std::shared_ptr<Device> > Interface::getDevices()
00136 {
00137   std::vector<std::shared_ptr<Device> > ret;
00138 
00139   if (ifh != 0)
00140   {
00141     // get list of previously requested devices that are still in use
00142 
00143     std::vector<std::shared_ptr<Device> > current;
00144 
00145     for (size_t i=0; i<dlist.size(); i++)
00146     {
00147       std::shared_ptr<Device> p=dlist[i].lock();
00148       if (p)
00149       {
00150         current.push_back(p);
00151       }
00152     }
00153 
00154     // update available interfaces
00155 
00156     if (gentl->IFUpdateDeviceList(ifh, 0, 10) != GenTL::GC_ERR_SUCCESS)
00157     {
00158       throw GenTLException("Interface::getDevices()", gentl);
00159     }
00160 
00161     // create list of interfaces, using either existing interfaces or
00162     // instantiating new ones
00163 
00164     uint32_t n=0;
00165     if (gentl->IFGetNumDevices(ifh, &n) != GenTL::GC_ERR_SUCCESS)
00166     {
00167       throw GenTLException("Interface::getDevices()", gentl);
00168     }
00169 
00170     for (uint32_t i=0; i<n; i++)
00171     {
00172       char tmp[256]="";
00173       size_t size=sizeof(tmp);
00174 
00175       if (gentl->IFGetDeviceID(ifh, i, tmp, &size) != GenTL::GC_ERR_SUCCESS)
00176       {
00177         throw GenTLException("Interface::getDevices()", gentl);
00178       }
00179 
00180       int k=find(current, tmp);
00181 
00182       if (k >= 0)
00183       {
00184         ret.push_back(current[k]);
00185       }
00186       else
00187       {
00188         ret.push_back(std::shared_ptr<Device>(new Device(shared_from_this(), gentl, tmp)));
00189       }
00190     }
00191 
00192     // update internal list of devices for reusage on next call
00193 
00194     dlist.clear();
00195     for (size_t i=0; i<ret.size(); i++)
00196     {
00197       dlist.push_back(ret[i]);
00198     }
00199   }
00200 
00201   return ret;
00202 }
00203 
00204 std::shared_ptr<Device> Interface::getDevice(const char *devid)
00205 {
00206   // get list of all devices
00207 
00208   std::vector<std::shared_ptr<Device> > list=getDevices();
00209 
00210   // find requested device by ID or user defined name
00211 
00212   std::shared_ptr<Device> ret;
00213 
00214   for (size_t i=0; i<list.size(); i++)
00215   {
00216     std::shared_ptr<Device> p=list[i];
00217 
00218     if (p && (p->getID() == devid || p->getDisplayName() == devid))
00219     {
00220       if (ret)
00221       {
00222         std::cerr << "There is more than one device with ID or user defined name: " << devid
00223                   << std::endl;
00224         ret=0;
00225         break;
00226       }
00227 
00228       ret=p;
00229     }
00230   }
00231 
00232   return ret;
00233 }
00234 
00235 namespace
00236 {
00237 
00238 std::string cIFGetInfo(const Interface *obj,
00239                        const std::shared_ptr<const GenTLWrapper> &gentl,
00240                        GenTL::INTERFACE_INFO_CMD info)
00241 {
00242   std::string ret;
00243 
00244   GenTL::INFO_DATATYPE type;
00245   char tmp[1024]="";
00246   size_t tmp_size=sizeof(tmp);
00247   GenTL::GC_ERROR err=GenTL::GC_ERR_ERROR;
00248 
00249   if (obj->getHandle() != 0)
00250   {
00251     err=gentl->IFGetInfo(obj->getHandle(), info, &type, tmp, &tmp_size);
00252   }
00253   else if (obj->getParent()->getHandle() != 0)
00254   {
00255     err=gentl->TLGetInterfaceInfo(obj->getParent()->getHandle(), obj->getID().c_str(), info,
00256                                   &type, tmp, &tmp_size);
00257   }
00258 
00259   if (err == GenTL::GC_ERR_SUCCESS && type == GenTL::INFO_DATATYPE_STRING)
00260   {
00261     for (size_t i=0; i<tmp_size && tmp[i] != '\0'; i++)
00262     {
00263       ret.push_back(tmp[i]);
00264     }
00265   }
00266 
00267   return ret;
00268 }
00269 
00270 }
00271 
00272 std::string Interface::getDisplayName() const
00273 {
00274   return cIFGetInfo(this, gentl, GenTL::INTERFACE_INFO_DISPLAYNAME);
00275 }
00276 
00277 std::string Interface::getTLType() const
00278 {
00279   return cIFGetInfo(this, gentl, GenTL::INTERFACE_INFO_TLTYPE);
00280 }
00281 
00282 std::shared_ptr<GenApi::CNodeMapRef> Interface::getNodeMap()
00283 {
00284   if (ifh != 0 && !nodemap)
00285   {
00286     cport=std::shared_ptr<CPort>(new CPort(gentl, &ifh));
00287     nodemap=allocNodeMap(gentl, ifh, cport.get());
00288   }
00289 
00290   return nodemap;
00291 }
00292 
00293 void *Interface::getHandle() const
00294 {
00295   return ifh;
00296 }
00297 
00298 }


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