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=static_cast<size_t>(length);
61 
62  if (*port != 0)
63  {
64  if (gentl->GCReadPort(*port, static_cast<uint64_t>(addr), buffer, &size) !=
66  {
67  throw GenTLException("CPort::Read()", gentl);
68  }
69 
70  if (size != static_cast<size_t>(length))
71  {
72  throw GenTLException("CPort::Read(): Returned size not as expected");
73  }
74  }
75  else
76  {
77  throw GenTLException("CPort::Read(): Port has been closed");
78  }
79 }
80 
81 void CPort::Write(const void *buffer, int64_t addr, int64_t length)
82 {
83  size_t size=static_cast<size_t>(length);
84 
85  if (*port != 0)
86  {
87  if (gentl->GCWritePort(*port, static_cast<uint64_t>(addr), buffer, &size) !=
89  {
90  throw GenTLException("CPort::Write()", gentl);
91  }
92 
93  if (size != static_cast<size_t>(length))
94  {
95  throw GenTLException("CPort::Write(): Returned size not as expected");
96  }
97  }
98  else
99  {
100  throw GenTLException("CPort::Write(): Port has been closed");
101  }
102 }
103 
105 {
106  if (*port != 0)
107  {
108  return GenApi::RW;
109  }
110 
111  return GenApi::NA;
112 }
113 
114 namespace
115 {
116 
117 inline std::string toLower(const std::string &s, size_t start, size_t size)
118 {
119  std::ostringstream out;
120 
121  size_t end=std::min(s.size(), start+size);
122 
123  while (start < end)
124  {
125  out << static_cast<char>(std::tolower(s[start++]));
126  }
127 
128  return out.str();
129 }
130 
131 }
132 
133 std::shared_ptr<GenApi::CNodeMapRef> allocNodeMap(std::shared_ptr<const GenTLWrapper> gentl,
134  void *port, CPort *cport, const char *xml)
135 {
136  std::shared_ptr<GenApi::CNodeMapRef> nodemap(new GenApi::CNodeMapRef());
137 
138  try
139  {
140  // get number of URLS that the given port provides
141 
142  uint32_t n=0;
143  if (gentl->GCGetNumPortURLs(port, &n) != GenTL::GC_ERR_SUCCESS)
144  {
145  throw GenTLException("allocNodeMap()", gentl);
146  }
147 
148  if (n == 0)
149  {
150  return std::shared_ptr<GenApi::CNodeMapRef>();
151  }
152 
153  // get the first URL
154 
156  char tmp[1024]="";
157  size_t size=sizeof(tmp);
158 
159  if (gentl->GCGetPortURLInfo(port, 0, GenTL::URL_INFO_URL, &type, tmp, &size) !=
161  {
162  throw GenTLException("allocNodeMap()", gentl);
163  }
164 
165  // interpret the URL and load XML File
166 
167  std::string url=tmp;
168  if (toLower(url, 0, 6) == "local:")
169  {
170  // interpret local URL
171 
172  size_t i=6;
173  if (url.compare(i, 3, "///") == 0)
174  {
175  i+=3;
176  }
177 
178  std::stringstream in(url.substr(i));
179  std::string name, saddress, slength;
180 
181  std::getline(in, name, ';');
182  std::getline(in, saddress, ';');
183  std::getline(in, slength, ';');
184 
185  uint64_t address=std::stoull(saddress, 0, 16);
186  size_t length=static_cast<size_t>(std::stoull(slength, 0, 16));
187 
188  // read XML or ZIP from registers
189 
190  std::unique_ptr<char[]> buffer(new char[length+1]);
191 
192  if (gentl->GCReadPort(port, address, buffer.get(), &length) != GenTL::GC_ERR_SUCCESS)
193  {
194  throw GenTLException("allocNodeMap()", gentl);
195  }
196 
197  buffer.get()[length]='\0';
198 
199  // store XML file
200 
201  if (xml != 0)
202  {
203  std::ofstream out(xml, std::ios::binary);
204 
205  out.rdbuf()->sputn(buffer.get(), static_cast<std::streamsize>(length));
206  }
207 
208  // load XML or ZIP from registers
209 
210  if (name.size() > 4 && toLower(name, name.size()-4, 4) == ".zip")
211  {
212  nodemap->_LoadXMLFromZIPData(buffer.get(), length);
213  }
214  else
215  {
216  GENICAM_NAMESPACE::gcstring sxml=buffer.get();
217  nodemap->_LoadXMLFromString(sxml);
218  }
219  }
220  else if (toLower(url, 0, 5) == "file:")
221  {
222  // interpret local URL
223 
224  size_t i=6;
225  if (url.compare(i, 3, "///") == 0)
226  {
227  i+=3;
228  }
229 
230  std::string name=url.substr(i);
231 
232  // load XML or ZIP from file
233 
234  if (name.size() > 4 && toLower(name, name.size()-4, 4) == ".zip")
235  {
237  nodemap->_LoadXMLFromZIPFile(file);
238  }
239  else
240  {
242  nodemap->_LoadXMLFromFile(file);
243  }
244  }
245  else
246  {
247  throw GenTLException(("allocNodeMap(): Cannot interpret URL: "+url).c_str());
248  }
249 
250  // get port name
251 
252  size=sizeof(tmp);
253 
254  if (gentl->GCGetPortInfo(port, GenTL::PORT_INFO_PORTNAME, &type, tmp, &size) !=
256  {
257  throw GenTLException("allocNodeMap()", gentl);
258  }
259 
260  GENICAM_NAMESPACE::gcstring portname=tmp;
261  if (!nodemap->_Connect(cport, portname))
262  {
263  throw GenTLException((std::string("allocNodeMap(): Cannot connect port: ")+tmp).c_str());
264  }
265  }
266  catch (const GENICAM_NAMESPACE::GenericException &ex)
267  {
268  throw GenTLException(ex.what());
269  }
270 
271  return nodemap;
272 }
273 
274 }
void Read(void *buffer, int64_t addr, int64_t length)
Definition: cport.cc:58
This is the port definition that connects GenAPI to GenTL.
Definition: cport.h:52
std::istream & getline(std::istream &is, GENICAM_NAMESPACE::gcstring &str)
STL getline.
Definition: GCString.h:188
__int64 int64_t
Definition: config-win32.h:21
std::string str()
virtual gcstring substr(size_t offset=0, size_t count=GCSTRING_NPOS) const
void ** port
Definition: cport.h:65
void Write(const void *buffer, int64_t addr, int64_t length)
Definition: cport.cc:81
CPort(std::shared_ptr< const GenTLWrapper > gentl, void **port)
Definition: cport.cc:53
std::shared_ptr< const GenTLWrapper > gentl
Definition: cport.h:64
GenApi::EAccessMode GetAccessMode() const
Definition: cport.cc:104
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:133
Not available.
Definition: Types.h:57
A string class which is a clone of std::string.
Definition: GCString.h:52
enum GENAPI_NAMESPACE::_EAccessMode EAccessMode
access mode of a node
Definition: buffer.cc:47
GenICam&#39;s exception class.
Definition: GCException.h:63
Read and Write.
Definition: Types.h:60
int32_t INFO_DATATYPE
Definition: GenTL_v1_5.h:258
virtual const char * c_str(void) const
Smartpointer for NodeMaps with create function.
Definition: NodeMapRef.h:516
virtual const char * what() const
Get error description (overwrite from std:exception)


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Wed Mar 17 2021 02:48:40