photo_camera_list.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2012, Robert Bosch LLC.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the Robert Bosch nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  *********************************************************************/
36 
37 #include <iostream>
38 #include <sys/types.h>
39 
40 #include "photo/photo_reporter.hpp"
41 
43 
44 
46  camera_list_(NULL),
47  port_info_list_(NULL),
48  abilities_list_(NULL)
49 {
50 }
51 
53 {
54  gp_list_unref( camera_list_ ); //delete camera_list_;
55  gp_port_info_list_free( port_info_list_ ); //delete port_info_list_;
56  gp_abilities_list_free( abilities_list_ ); //delete abilities_list_;
57 }
58 
59 
60 
62 {
63  return camera_list_;
64 }
65 
66 
67 GPPortInfoList* photo_camera_list::getPortInfoList( void )
68 {
69  return port_info_list_;
70 }
71 
72 CameraAbilitiesList* photo_camera_list::getAbilitiesList( void )
73 {
74  return abilities_list_;
75 }
76 
77 
78 bool photo_camera_list::loadPortInfo( ssize_t* port_count )
79 {
80  if( port_info_list_ == NULL )
81  {
82  // Create a new port info list
83  if( gp_port_info_list_new( &port_info_list_ ) != GP_OK )
84  {
85  photo_reporter::error( "gp_port_info_list_new()" );
86  return false;
87  }
88 
89  // Populate the list
90  if( gp_port_info_list_load( port_info_list_ ) != GP_OK )
91  {
92  photo_reporter::error( "gp_port_info_list_load()" );
93  return false;
94  }
95  }
96 
97  // Count the number of ports in the list
98  *port_count = gp_port_info_list_count( port_info_list_ );
99  if( *port_count < GP_OK )
100  {
101  photo_reporter::error( "gp_port_info_list_count()" );
102  return false;
103  }
104 
105  return true;
106 }
107 
108 bool photo_camera_list::loadAbilities( GPContext* context )
109 {
110  // Create a new abilities list
111  if( gp_abilities_list_new( &abilities_list_ ) != GP_OK )
112  {
113  photo_reporter::error( "gp_abilities_list_new()" );
114  return false;
115  }
116 
117  // Populate the abilities list
118  if( gp_abilities_list_load( abilities_list_, context ) != GP_OK )
119  {
120  photo_reporter::error( "gp_abilities_list_load()" );
121  return false;
122  }
123 
124  return true;
125 }
126 
127 
128 
129 bool photo_camera_list::lookupPortInfo( const std::string& port_name, GPPortInfo* port_info )
130 {
131  int list_index = 0;
132 
133  // Find the port in the list of ports and return the index
134  list_index = gp_port_info_list_lookup_path( port_info_list_, port_name.c_str() );
135  if( list_index < GP_OK )
136  {
137  photo_reporter::error( "gp_port_info_list_lookup_path()" );
138  if( list_index == GP_ERROR_UNKNOWN_PORT )
139  {
140  std::cerr << "The specified port (" << port_name << ") cannot be found. Use 'gphoto2 --list-ports' to display available ports. The prefix 'serial:' or 'usb:' is required." << std::endl;
141  }
142  return false;
143  }
144 
145  // Get the port information from from the information list
146  if( gp_port_info_list_get_info( port_info_list_, list_index, port_info ) != GP_OK )
147  {
148  photo_reporter::error( "gp_port_info_list_get_info()" );
149  return false;
150  }
151 
152  return true;
153 }
154 
155 
156 bool photo_camera_list::lookupAbilities( const std::string& model_name, CameraAbilities* abilities )
157 {
158  int list_index = 0;
159 
160  // Find the camera in the list of cameras and return the index
161  list_index = gp_abilities_list_lookup_model( abilities_list_, model_name.c_str() );
162  if( list_index < GP_OK )
163  {
164  photo_reporter::error( "gp_abilities_list_lookup_model()" );
165  return false;
166  }
167 
168  // Find the camera's abilities in the abilities list
169  if( gp_abilities_list_get_abilities( abilities_list_, list_index, abilities ) != GP_OK )
170  {
171  photo_reporter::error( "gp_abilities_list_get_abilities()" );
172  return false;
173  }
174 
175  return true;
176 }
177 
178 
179 
180 
181 
182 
183 /*
184  * This function detects all currently attached photo_cameras and returns them
185  * in a list. It avoids the generic 'usb:' port entry. This function does not
186  * open or initialize the photo_cameras.
187  */
188 
189 bool photo_camera_list::autodetect( GPContext* context )
190 {
191  ssize_t port_count = 0;
192 
193  // Create a new list of cameras
194  if( gp_list_new( &camera_list_ ) != GP_OK )
195  {
196  photo_reporter::error( "gp_list_new()" );
197  return false;
198  }
199 
200  // Load the low-level port drivers
201  if( loadPortInfo( &port_count ) == false )
202  {
203  return false;
204  }
205 
206  // Load the photo_camera drivers
207  if( loadAbilities( context ) == false )
208  {
209  return false;
210  }
211 
212 
213  // Filter the list for USB cameras
214  if( filterCameraList( context, "usb:" ) == false )
215  {
216  return false;
217  }
218  return true;
219 }
220 
221 
222 
223 bool photo_camera_list::filterCameraList( GPContext* context, const std::string& match_string )
224 {
225  CameraList *working_list = NULL;
226  const char *name = NULL;
227  const char *value = NULL;
228  int count = 0;
229 
230  if( gp_list_new( &working_list ) != GP_OK )
231  {
232  photo_reporter::error( "gp_list_new()" );
233  gp_list_free( working_list );
234  return false;
235  }
236 
237  // Autodetect the currently attached photo_cameras.
238  if( gp_abilities_list_detect( abilities_list_, port_info_list_, working_list, context) != GP_OK )
239  {
240  photo_reporter::error( "gp_abilities_list_detect()" );
241  gp_list_free( working_list );
242  return false;
243  }
244 
245 
246  count = gp_list_count( working_list );
247  if( count < GP_OK )
248  {
249  photo_reporter::error( "gp_list_count()" );
250  gp_list_free( working_list );
251  return false;
252  }
253 
254  // Clear camera_list_ for appending
255  if( gp_list_reset( camera_list_ ) != GP_OK )
256  {
257  photo_reporter::error( "gp_list_reset()" );
258  gp_list_free( working_list );
259  return false;
260  }
261 
262  // Filter out the generic 'usb:' entry
263  for( int i = 0; i < count; i++ )
264  {
265  gp_list_get_name( working_list, i, &name );
266  gp_list_get_value( working_list, i, &value );
267 
268  if( name && value && match_string.compare( value ) != 0 )
269  {
270  gp_list_append( camera_list_, name, value );
271  }
272  }
273 
274  gp_list_free( working_list );
275 
276  return true;
277 }
bool loadPortInfo(ssize_t *port_count)
CameraAbilitiesList * getAbilitiesList(void)
bool autodetect(GPContext *context)
void error(const std::string &function_name)
CameraList * getCameraList(void)
GPPortInfoList * getPortInfoList(void)
GPPortInfoList * port_info_list_
bool filterCameraList(GPContext *context, const std::string &match_string)
CameraList * camera_list_
bool lookupAbilities(const std::string &model_name, CameraAbilities *abilities)
CameraAbilitiesList * abilities_list_
bool lookupPortInfo(const std::string &port_name, GPPortInfo *port_info)
bool loadAbilities(GPContext *context)


photo
Author(s): Benjamin Pitzer
autogenerated on Mon Feb 28 2022 23:12:30