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


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Thu Jun 6 2019 18:42:46