All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cport.cc
Go to the documentation of this file.
1 /*
2  * This file is part of the rc_genicam_api package.
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Heiko Hirschmueller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "cport.h"
37 #include "exception.h"
38 
39 #include <fstream>
40 #include <sstream>
41 #include <cctype>
42 #include <string>
43 #include <algorithm>
44 
45 #ifdef _WIN32
46 #undef min
47 #undef max
48 #endif
49 
50 namespace rcg
51 {
52 
53 CPort::CPort(std::shared_ptr<const GenTLWrapper> _gentl, void **_port) : gentl(_gentl)
54 {
55  port=_port;
56 }
57 
58 void CPort::Read(void *buffer, int64_t addr, int64_t length)
59 {
60  size_t size=0;
61 
62  if (*port != 0)
63  {
64  int retry=1;
66 
67  while (err != GenTL::GC_ERR_SUCCESS && retry > 0)
68  {
69  retry--;
70 
71  size=static_cast<size_t>(length);
72  err=gentl->GCReadPort(*port, static_cast<uint64_t>(addr), buffer, &size);
73  }
74 
75  if (err != GenTL::GC_ERR_SUCCESS)
76  {
78  out << "CPort::Read(address=0x" << std::hex << addr << ", length=" <<
79  std::dec << static_cast<size_t>(length) << ")";
80 
81  throw GenTLException(out.str(), gentl);
82  }
83 
84  if (size == 0)
85  {
86  throw GenTLException("CPort::Read(): Returned size is 0");
87  }
88 
89  while (size < static_cast<size_t>(length))
90  {
91  reinterpret_cast<uint8_t *>(buffer)[size++]=0;
92  }
93  }
94  else
95  {
96  throw GenTLException("CPort::Read(): Port has been closed");
97  }
98 }
99 
100 void CPort::Write(const void *buffer, int64_t addr, int64_t length)
101 {
102  size_t size=static_cast<size_t>(length);
103 
104  if (*port != 0)
105  {
106  if (gentl->GCWritePort(*port, static_cast<uint64_t>(addr), buffer, &size) !=
108  {
109  std::ostringstream out;
110  out << "CPort::Write(address=0x" << std::hex << addr << ", length=" <<
111  std::dec << length << ")";
112 
113  throw GenTLException(out.str(), gentl);
114  }
115 
116  if (size != static_cast<size_t>(length))
117  {
118  throw GenTLException("CPort::Write(): Returned size not as expected");
119  }
120  }
121  else
122  {
123  throw GenTLException("CPort::Write(): Port has been closed");
124  }
125 }
126 
128 {
129  if (*port != 0)
130  {
131  return GenApi::RW;
132  }
133 
134  return GenApi::NA;
135 }
136 
137 namespace
138 {
139 
140 inline std::string toLower(const std::string &s, size_t start, size_t size)
141 {
142  std::ostringstream out;
143 
144  size_t end=std::min(s.size(), start+size);
145 
146  while (start < end)
147  {
148  out << static_cast<char>(std::tolower(s[start++]));
149  }
150 
151  return out.str();
152 }
153 
154 }
155 
156 std::shared_ptr<GenApi::CNodeMapRef> allocNodeMap(std::shared_ptr<const GenTLWrapper> gentl,
157  void *port, CPort *cport, const char *xml)
158 {
159  std::shared_ptr<GenApi::CNodeMapRef> nodemap(new GenApi::CNodeMapRef());
160 
161  try
162  {
163  // get number of URLS that the given port provides
164 
165  uint32_t n=0;
166  if (gentl->GCGetNumPortURLs(port, &n) != GenTL::GC_ERR_SUCCESS)
167  {
168  throw GenTLException("allocNodeMap()", gentl);
169  }
170 
171  if (n == 0)
172  {
173  return std::shared_ptr<GenApi::CNodeMapRef>();
174  }
175 
176  // get the first URL
177 
179  char tmp[1024]="";
180  size_t size=sizeof(tmp);
181 
182  if (gentl->GCGetPortURLInfo(port, 0, GenTL::URL_INFO_URL, &type, tmp, &size) !=
184  {
185  throw GenTLException("allocNodeMap()", gentl);
186  }
187 
188  // interpret the URL and load XML File
189 
190  std::string url=tmp;
191  if (toLower(url, 0, 6) == "local:")
192  {
193  // interpret local URL
194 
195  size_t i=6;
196  if (url.compare(i, 3, "///") == 0)
197  {
198  i+=3;
199  }
200 
201  std::stringstream in(url.substr(i));
202  std::string name, saddress, slength;
203 
204  std::getline(in, name, ';');
205  std::getline(in, saddress, ';');
206  std::getline(in, slength, ';');
207 
208  uint64_t address=std::stoull(saddress, 0, 16);
209  size_t length=static_cast<size_t>(std::stoull(slength, 0, 16));
210 
211  // read XML or ZIP from registers
212 
213  std::unique_ptr<char[]> buffer(new char[length+1]);
214 
215  if (gentl->GCReadPort(port, address, buffer.get(), &length) != GenTL::GC_ERR_SUCCESS)
216  {
217  throw GenTLException("allocNodeMap()", gentl);
218  }
219 
220  buffer.get()[length]='\0';
221 
222  // store XML file
223 
224  if (xml != 0)
225  {
226  if (xml[0] == '\0')
227  {
228  xml=name.c_str();
229  }
230 
231  std::ofstream out(xml, std::ios::binary);
232 
233  out.rdbuf()->sputn(buffer.get(), static_cast<std::streamsize>(length));
234  }
235 
236  // load XML or ZIP from registers
237 
238  if (name.size() > 4 && toLower(name, name.size()-4, 4) == ".zip")
239  {
240  nodemap->_LoadXMLFromZIPData(buffer.get(), length);
241  }
242  else
243  {
244  GENICAM_NAMESPACE::gcstring sxml=buffer.get();
245  nodemap->_LoadXMLFromString(sxml);
246  }
247  }
248  else if (toLower(url, 0, 5) == "file:")
249  {
250  // interpret local URL
251 
252  size_t i=5;
253  if (url.compare(i, 3, "///") == 0)
254  {
255  i+=3;
256  }
257 
258  std::string name=url.substr(i);
259 
260  // load XML or ZIP from file
261 
262  if (name.size() > 4 && toLower(name, name.size()-4, 4) == ".zip")
263  {
265  nodemap->_LoadXMLFromZIPFile(file);
266  }
267  else
268  {
270  nodemap->_LoadXMLFromFile(file);
271  }
272  }
273  else
274  {
275  throw GenTLException(("allocNodeMap(): Cannot interpret URL: "+url).c_str());
276  }
277 
278  // get port name
279 
280  size=sizeof(tmp);
281 
282  if (gentl->GCGetPortInfo(port, GenTL::PORT_INFO_PORTNAME, &type, tmp, &size) !=
284  {
285  throw GenTLException("allocNodeMap()", gentl);
286  }
287 
288  GENICAM_NAMESPACE::gcstring portname=tmp;
289  if (!nodemap->_Connect(cport, portname))
290  {
291  throw GenTLException((std::string("allocNodeMap(): Cannot connect port: ")+tmp).c_str());
292  }
293  }
294  catch (const GENICAM_NAMESPACE::GenericException &ex)
295  {
296  throw GenTLException(ex.what());
297  }
298 
299  return nodemap;
300 }
301 
302 }
GENAPI_NAMESPACE::EAccessMode
enum GENAPI_NAMESPACE::_EAccessMode EAccessMode
access mode of a node
INFO_DATATYPE
int32_t INFO_DATATYPE
Definition: GenTL_v1_6.h:261
URL_INFO_URL
@ URL_INFO_URL
Definition: GenTL_v1_6.h:562
GENICAM_NAMESPACE::gcstring::substr
virtual gcstring substr(size_t offset=0, size_t count=GCSTRING_NPOS) const
GENICAM_NAMESPACE::gcstring
A string class which is a clone of std::string.
Definition: GCString.h:52
GC_ERROR
int32_t GC_ERROR
Definition: GenTL_v1_6.h:185
rcg
Definition: buffer.cc:47
GC_ERR_ERROR
@ GC_ERR_ERROR
Definition: GenTL_v1_6.h:159
rcg::CPort
This is the port definition that connects GenAPI to GenTL.
Definition: cport.h:52
GC_ERR_SUCCESS
@ GC_ERR_SUCCESS
Definition: GenTL_v1_6.h:158
rcg::allocNodeMap
std::shared_ptr< GenApi::CNodeMapRef > allocNodeMap(std::shared_ptr< const GenTLWrapper > gentl, void *port, CPort *cport, const char *xml)
Convenience function that returns a GenICam node map from the given port.
Definition: cport.cc:156
rcg::CPort::port
void ** port
Definition: cport.h:65
GENICAM_NAMESPACE::getline
std::istream & getline(std::istream &is, GENICAM_NAMESPACE::gcstring &str)
STL getline.
Definition: GCString.h:194
GENICAM_NAMESPACE::GenericException::what
virtual const char * what() const
Get error description (overwrite from std:exception)
rcg::CPort::GetAccessMode
GenApi::EAccessMode GetAccessMode() const
Definition: cport.cc:127
rcg::CPort::gentl
std::shared_ptr< const GenTLWrapper > gentl
Definition: cport.h:64
GENAPI_NAMESPACE::RW
@ RW
Read and Write.
Definition: Types.h:60
GENICAM_NAMESPACE::GenericException
GenICam's exception class.
Definition: GCException.h:63
std::ostringstream::str
std::string str()
GENAPI_NAMESPACE::CNodeMapRef
Smartpointer for NodeMaps with create function.
Definition: NodeMapRef.h:551
int64_t
__int64 int64_t
Definition: config-win32.h:21
std::ostringstream
Definition: Portability.hh:42
GENICAM_NAMESPACE::gcstring::c_str
virtual const char * c_str(void) const
cport.h
exception.h
rcg::CPort::Read
void Read(void *buffer, int64_t addr, int64_t length)
Definition: cport.cc:58
PORT_INFO_PORTNAME
@ PORT_INFO_PORTNAME
Definition: GenTL_v1_6.h:540
rcg::CPort::Write
void Write(const void *buffer, int64_t addr, int64_t length)
Definition: cport.cc:100
rcg::CPort::CPort
CPort(std::shared_ptr< const GenTLWrapper > gentl, void **port)
Definition: cport.cc:53
rcg::GenTLException
Definition: exception.h:47
GENAPI_NAMESPACE::NA
@ NA
Not available.
Definition: Types.h:57


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Wed Dec 4 2024 03:10:11