cport.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 "cport.h"
00037 #include "exception.h"
00038 
00039 #include <fstream>
00040 
00041 namespace rcg
00042 {
00043 
00044 CPort::CPort(std::shared_ptr<const GenTLWrapper> _gentl, void **_port) : gentl(_gentl)
00045 {
00046   port=_port;
00047 }
00048 
00049 void CPort::Read(void *buffer, int64_t addr, int64_t length)
00050 {
00051   size_t size=static_cast<size_t>(length);
00052 
00053   if (*port != 0)
00054   {
00055     if (gentl->GCReadPort(*port, addr, buffer, &size) != GenTL::GC_ERR_SUCCESS)
00056     {
00057       throw GenTLException("CPort::Read()", gentl);
00058     }
00059 
00060     if (size != static_cast<size_t>(length))
00061     {
00062       throw GenTLException("CPort::Read(): Returned size not as expected");
00063     }
00064   }
00065   else
00066   {
00067     throw GenTLException("CPort::Read(): Port has been closed");
00068   }
00069 }
00070 
00071 void CPort::Write(const void *buffer, int64_t addr, int64_t length)
00072 {
00073   size_t size=static_cast<size_t>(length);
00074 
00075   if (*port != 0)
00076   {
00077     if (gentl->GCWritePort(*port, addr, buffer, &size) != GenTL::GC_ERR_SUCCESS)
00078     {
00079       throw GenTLException("CPort::Write()", gentl);
00080     }
00081 
00082     if (size != static_cast<size_t>(length))
00083     {
00084       throw GenTLException("CPort::Write(): Returned size not as expected");
00085     }
00086   }
00087   else
00088   {
00089     throw GenTLException("CPort::Write(): Port has been closed");
00090   }
00091 }
00092 
00093 GenApi::EAccessMode CPort::GetAccessMode() const
00094 {
00095   if (*port != 0)
00096   {
00097     return GenApi::RW;
00098   }
00099 
00100   return GenApi::NA;
00101 }
00102 
00103 namespace
00104 {
00105 
00106 inline std::string toLower(const std::string &s, size_t start, size_t size)
00107 {
00108   std::ostringstream out;
00109 
00110   size_t end=std::min(s.size(), start+size);
00111 
00112   while (start < end)
00113   {
00114     out << static_cast<char>(std::tolower(s[start++]));
00115   }
00116 
00117   return out.str();
00118 }
00119 
00120 }
00121 
00122 std::shared_ptr<GenApi::CNodeMapRef> allocNodeMap(std::shared_ptr<const GenTLWrapper> gentl,
00123                                                   void *port, CPort *cport, const char *xml)
00124 {
00125   std::shared_ptr<GenApi::CNodeMapRef> nodemap(new GenApi::CNodeMapRef());
00126 
00127   try
00128   {
00129     // get number of URLS that the given port provides
00130 
00131     uint32_t n=0;
00132     if (gentl->GCGetNumPortURLs(port, &n) != GenTL::GC_ERR_SUCCESS)
00133     {
00134       throw GenTLException("allocNodeMap()", gentl);
00135     }
00136 
00137     if (n == 0)
00138     {
00139       return std::shared_ptr<GenApi::CNodeMapRef>();
00140     }
00141 
00142     // get the first URL
00143 
00144     GenTL::INFO_DATATYPE type;
00145     char tmp[1024]="";
00146     size_t size=sizeof(tmp);
00147 
00148     if (gentl->GCGetPortURLInfo(port, 0, GenTL::URL_INFO_URL, &type, tmp, &size) !=
00149         GenTL::GC_ERR_SUCCESS)
00150     {
00151       throw GenTLException("allocNodeMap()", gentl);
00152     }
00153 
00154     // interpret the URL and load XML File
00155 
00156     std::string url=tmp;
00157     if (toLower(url, 0, 6) == "local:")
00158     {
00159       // interpret local URL
00160 
00161       int i=6;
00162       if (url.compare(i, 3, "///") == 0)
00163       {
00164         i+=3;
00165       }
00166 
00167       std::stringstream in(url.substr(i));
00168       std::string name, saddress, slength;
00169 
00170       std::getline(in, name, ';');
00171       std::getline(in, saddress, ';');
00172       std::getline(in, slength, ';');
00173 
00174       uint64_t address=std::stoull(saddress, 0, 16);
00175       size_t length=static_cast<size_t>(std::stoull(slength, 0, 16));
00176 
00177       // read XML or ZIP from registers
00178 
00179       std::unique_ptr<char[]> buffer(new char[length+1]);
00180 
00181       if (gentl->GCReadPort(port, address, buffer.get(), &length) != GenTL::GC_ERR_SUCCESS)
00182       {
00183         throw GenTLException("allocNodeMap()", gentl);
00184       }
00185 
00186       buffer.get()[length]='\0';
00187 
00188       // store XML file
00189 
00190       if (xml != 0)
00191       {
00192         std::ofstream out(xml, std::ios::binary);
00193 
00194         out.rdbuf()->sputn(buffer.get(), length);
00195       }
00196 
00197       // load XML or ZIP from registers
00198 
00199       if (name.size() > 4 && toLower(name, name.size()-4, 4) == ".zip")
00200       {
00201         nodemap->_LoadXMLFromZIPData(buffer.get(), length);
00202       }
00203       else
00204       {
00205         gcstring xml=buffer.get();
00206         nodemap->_LoadXMLFromString(xml);
00207       }
00208     }
00209     else if (toLower(url, 0, 5) == "file:")
00210     {
00211       // interpret local URL
00212 
00213       int i=6;
00214       if (url.compare(i, 3, "///") == 0)
00215       {
00216         i+=3;
00217       }
00218 
00219       std::string name=url.substr(i);
00220 
00221       // load XML or ZIP from file
00222 
00223       if (name.size() > 4 && toLower(name, name.size()-4, 4) == ".zip")
00224       {
00225         gcstring file=name.c_str();
00226         nodemap->_LoadXMLFromZIPFile(file);
00227       }
00228       else
00229       {
00230         gcstring file=name.c_str();
00231         nodemap->_LoadXMLFromFile(file);
00232       }
00233     }
00234     else
00235     {
00236       throw GenTLException(("allocNodeMap(): Cannot interpret URL: "+url).c_str());
00237     }
00238 
00239     // get port name
00240 
00241     size=sizeof(tmp);
00242 
00243     if (gentl->GCGetPortInfo(port, GenTL::PORT_INFO_PORTNAME, &type, tmp, &size) !=
00244         GenTL::GC_ERR_SUCCESS)
00245     {
00246       throw GenTLException("allocNodeMap()", gentl);
00247     }
00248 
00249     gcstring portname=tmp;
00250     if (!nodemap->_Connect(cport, portname))
00251     {
00252       throw GenTLException((std::string("allocNodeMap(): Cannot connect port: ")+tmp).c_str());
00253     }
00254   }
00255   catch (const GENICAM_NAMESPACE::GenericException &ex)
00256   {
00257     throw GenTLException(ex.what());
00258   }
00259 
00260   return nodemap;
00261 }
00262 
00263 }


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