gc_file.cc
Go to the documentation of this file.
1 /*
2  * This file is part of the rc_genicam_api package.
3  *
4  * Copyright (c) 2020 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 <rc_genicam_api/system.h>
38 #include <rc_genicam_api/device.h>
39 #include <rc_genicam_api/config.h>
40 
41 #include <GenApi/Filestream.h>
42 
43 #include <iostream>
44 #include <fstream>
45 
46 int main(int argc, char *argv[])
47 {
48  int ret=0;
49 
50  try
51  {
52  if (argc >= 2 && std::string(argv[1]) != "-h")
53  {
54  // get device id
55 
56  int k=1;
57  if (k < argc)
58  {
59  std::string devid=argv[k++];
60 
61  // find specific device accross all systems and interfaces and show some
62  // information
63 
64  std::shared_ptr<rcg::Device> dev=rcg::getDevice(devid.c_str());
65 
66  if (dev)
67  {
68  // open device and optionally change some settings
69 
70  if (k < argc)
71  {
72  dev->open(rcg::Device::CONTROL);
73  }
74 
75  std::shared_ptr<GenApi::CNodeMapRef> nodemap=dev->getRemoteNodeMap();
76 
77  // process command line parameters
78 
79  if (k < argc)
80  {
81  std::string devfile=argv[k++];
82 
83  if (devfile == "-f")
84  {
85  std::vector<std::string> list;
86  rcg::getEnum(nodemap, "FileSelector", list, true);
87 
88  for (size_t i=0; i<list.size(); i++)
89  {
90  std::cout << list[i] << std::endl;
91  }
92  }
93  else
94  {
95  std::string op;
96  std::string file;
97 
98  if (k+1 < argc)
99  {
100  op=argv[k++];
101  file=argv[k++];
102  }
103 
104  if (op == "-w")
105  {
106  std::ifstream in(file);
107  std::ostringstream buffer;
108  buffer << in.rdbuf();
109  std::string data=buffer.str();
110 
111  std::cout << "Input file length: " << data.size() << std::endl;
112 
114  rf.attach(nodemap->_Ptr);
115 
116  if (rf.openFile(devfile.c_str(), std::ios::out))
117  {
118  size_t n=rf.write(data.c_str(), 0, data.size(), devfile.c_str());
119 
120  std::cout << "Status: " << rcg::getString(nodemap, "FileOperationStatus") << std::endl;
121 
122  if (n != data.size())
123  {
124  std::cerr << "Error: Can only write " << n << " of " << data.size() << " bytes" << std::endl;
125  }
126 
127  rf.closeFile(devfile.c_str());
128  }
129  else
130  {
131  std::cerr << "ERROR: Failed to open remote file!" << std::endl;
132  }
133  }
134  else if (op == "-r" || op == "")
135  {
136  // load file completely into memory
137 
139  rf.attach(nodemap->_Ptr);
140 
141  if (rf.openFile(devfile.c_str(), std::ios::in))
142  {
143  size_t n=rcg::getInteger(nodemap, "FileSize", 0, 0, true);
144  std::cout << "File size: " << n << std::endl;
145  std::vector<char> buffer(n);
146 
147  n=rf.read(buffer.data(), 0, buffer.size(), devfile.c_str());
148 
149  std::cout << "Status: " << rcg::getString(nodemap, "FileOperationStatus") << std::endl;
150 
151  if (n == buffer.size())
152  {
153  if (op == "-r")
154  {
155  // store in file
156 
157  std::ofstream out(file);
158  out.write(buffer.data(), buffer.size());
159  out.close();
160  }
161  else
162  {
163  // print on stdout
164 
165  std::cout.write(buffer.data(), buffer.size());
166  }
167  }
168  else
169  {
170  std::cerr << "Error: Can only read " << n << " of " << buffer.size() << " bytes" << std::endl;
171  }
172 
173  rf.closeFile(devfile.c_str());
174  }
175  else
176  {
177  std::cerr << "Error: Cannot open remote file: " << devfile << std::endl;
178  }
179  }
180  else
181  {
182  std::cerr << "Error: Expected parameter '-r' or '-w'!" << std::endl;
183  ret=1;
184  }
185  }
186  }
187  else
188  {
189  std::cerr << "Error: More parameters expected!" << std::endl;
190  ret=1;
191  }
192 
193  dev->close();
194  }
195  else
196  {
197  std::cerr << "Error: Device '" << devid << "' not found!" << std::endl;
198  ret=1;
199  }
200  }
201  else
202  {
203  std::cerr << "Error: Device name not given!" << std::endl;
204  ret=1;
205  }
206  }
207  else
208  {
209  std::cout << argv[0] << " -h | [<interface-id>:]<device-id> -f | (<device-file> [-w|-r <file>])" << std::endl;
210  std::cout << std::endl;
211  std::cout << "Downloading or uploading a file via GenICam." << std::endl;
212  std::cout << std::endl;
213  std::cout << "-h Prints help information and exits" << std::endl;
214  std::cout << "-f Lists names of files on the device" << std::endl;
215  std::cout << "-w <file> Writes the given local file into the selected file on the device" << std::endl;
216  std::cout << "-r <file> Reads the selected file on the device and stores it as local file" << std::endl;
217  std::cout << std::endl;
218  std::cout << "The selected file is printed on std out if none of -f, -w and -r are given." << std::endl;
219  ret=1;
220  }
221  }
222  catch (const std::exception &ex)
223  {
224  std::cerr << ex.what() << std::endl;
225  ret=2;
226  }
227  catch (const GENICAM_NAMESPACE::GenericException &ex)
228  {
229  std::cerr << ex.what() << std::endl;
230  ret=2;
231  }
232 
234 
235  return ret;
236 }
std::shared_ptr< Device > getDevice(const char *id)
Searches across all transport layers and interfaces for a device.
Definition: device.cc:469
Definition of ODevFileStream and IDevFileStream.
Adapter between the std::iostreambuf and the SFNC Features representing the device filesystem...
Definition: Filestream.h:106
int64_t getInteger(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, int64_t *vmin, int64_t *vmax, bool exception, bool igncache)
Get the value of an integer feature of the given nodemap.
Definition: config.cc:542
virtual GenICam_streamsize write(const char *buf, int64_t offs, int64_t len, const char *pFileName)
writes data into a file
std::string str()
static void clearSystems()
Clears the internal list of systems.
Definition: system.cc:213
std::string getEnum(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, bool exception)
Get the value of an enumeration of the given nodemap.
Definition: config.cc:655
int main(int argc, char *argv[])
Definition: gc_file.cc:46
virtual bool openFile(const char *pFileName, std::ios_base::openmode mode)
open a file on the device
virtual bool attach(GENAPI_NAMESPACE::INodeMap *pInterface)
attach file protocol adapter to nodemap
virtual GenICam_streamsize read(char *buf, int64_t offs, GenICam_streamsize len, const char *pFileName)
read data from the device into a buffer
GenICam&#39;s exception class.
Definition: GCException.h:63
std::string getString(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, bool exception, bool igncache)
Get the value of a feature of the given nodemap.
Definition: config.cc:773
virtual bool closeFile(const char *pFileName)
close a file on the device
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