1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include <gphoto2/gphoto2-camera.h>
6
7 #include "samples.h"
8
9 /* Sample for AFL usage.
10 *
11 */
12
errordumper(GPLogLevel level,const char * domain,const char * str,void * data)13 static void errordumper(GPLogLevel level, const char *domain, const char *str,
14 void *data) {
15 /* Do not log ... but let it appear here so we discover debug paths */
16 fprintf(stderr, "%s:%s\n", domain, str);
17 }
18
19 static int
recursive_directory(Camera * camera,const char * folder,GPContext * context,int * foundfile)20 recursive_directory(Camera *camera, const char *folder, GPContext *context, int *foundfile) {
21 int i, ret;
22 CameraList *list;
23 const char *newfile;
24 CameraFileInfo fileinfo;
25 CameraFile *file;
26
27 ret = gp_list_new (&list);
28 if (ret < GP_OK) {
29 printf ("Could not allocate list.\n");
30 return ret;
31 }
32
33 ret = gp_camera_folder_list_folders (camera, folder, list, context);
34 if (ret < GP_OK) {
35 printf ("Could not list folders.\n");
36 gp_list_free (list);
37 return ret;
38 }
39 gp_list_sort (list);
40
41 for (i = 0; i < gp_list_count (list); i++) {
42 const char *newfolder;
43 char *buf;
44 int havefile = 0;
45
46 gp_list_get_name (list, i, &newfolder);
47
48 if (!strlen(newfolder)) continue;
49
50 buf = malloc (strlen(folder) + 1 + strlen(newfolder) + 1);
51 strcpy(buf, folder);
52 if (strcmp(folder,"/")) /* avoid double / */
53 strcat(buf, "/");
54 strcat(buf, newfolder);
55
56 fprintf(stderr,"newfolder=%s\n", newfolder);
57
58 ret = recursive_directory (camera, buf, context, &havefile);
59 free (buf);
60 if (ret != GP_OK) {
61 gp_list_free (list);
62 printf ("Failed to recursively list folders.\n");
63 return ret;
64 }
65 if (havefile) /* only look for the first directory with a file */
66 break;
67 }
68 gp_list_reset (list);
69
70 ret = gp_camera_folder_list_files (camera, folder, list, context);
71 if (ret < GP_OK) {
72 gp_list_free (list);
73 printf ("Could not list files.\n");
74 return ret;
75 }
76 gp_list_sort (list);
77 if (gp_list_count (list) <= 0) {
78 gp_list_free (list);
79 return GP_OK;
80 }
81
82 gp_list_get_name (list, 0, &newfile); /* only entry 0 needed */
83 ret = gp_camera_file_get_info (camera, folder, newfile, &fileinfo, context);
84 if (ret != GP_OK) {
85 gp_list_free (list);
86 printf ("Could not get file info.\n");
87 return ret;
88 }
89
90 /* get file */
91 gp_file_new (&file);
92 ret = gp_camera_file_get (camera, folder, newfile, GP_FILE_TYPE_NORMAL, file, context);
93 if ((ret != GP_OK) && (ret != GP_ERROR_NOT_SUPPORTED)) {
94 gp_list_free (list);
95 printf ("Could not get file.\n");
96 return ret;
97 }
98 gp_file_unref (file);
99 /* get preview */
100 gp_file_new (&file);
101 ret = gp_camera_file_get (camera, folder, newfile, GP_FILE_TYPE_PREVIEW, file, context);
102 if ((ret != GP_OK) && (ret != GP_ERROR_NOT_SUPPORTED)) {
103 gp_list_free (list);
104 printf ("Could not get file preview.\n");
105 return ret;
106 }
107 gp_file_unref (file);
108 /* get exif */
109 gp_file_new (&file);
110 ret = gp_camera_file_get (camera, folder, newfile, GP_FILE_TYPE_EXIF, file, context);
111 if ((ret != GP_OK) && (ret != GP_ERROR_NOT_SUPPORTED)) {
112 gp_list_free (list);
113 printf ("Could not get file preview.\n");
114 return ret;
115 }
116 gp_file_unref (file);
117 /* Trigger the ptp things */
118 gp_file_new (&file);
119 ret = gp_camera_file_get (camera, folder, newfile, GP_FILE_TYPE_METADATA, file, context);
120 if ((ret != GP_OK) && (ret != GP_ERROR_NOT_SUPPORTED)) {
121 gp_list_free (list);
122 printf ("Could not get file metadata.\n");
123 return ret;
124 }
125 gp_file_unref (file);
126 if (foundfile) *foundfile = 1;
127 gp_list_free (list);
128 return GP_OK;
129 }
130
131
main(int argc,char ** argv)132 int main(int argc, char **argv) {
133 Camera *camera = NULL;
134 int ret, storagecnt;
135 CameraStorageInformation *storageinfo;
136 GPPortInfoList *gpinfolist;
137 GPContext *context;
138 CameraWidget *rootwidget;
139 char buf[200];
140 const char *name;
141 CameraText summary;
142 CameraFile *file;
143 /*CameraFilePath path;*/
144 CameraList *list;
145 CameraAbilitiesList *abilities = NULL;
146
147
148 gp_log_add_func(GP_LOG_DEBUG, errordumper, NULL);
149
150 context = sample_create_context (); /* see context.c */
151
152 strcpy(buf,"usb:");
153 if (argc > 1)
154 strcat (buf, argv[1]);
155
156 fprintf(stderr,"setting path %s.\n", buf);
157
158 gp_port_info_list_new (&gpinfolist);
159 ret = gp_port_info_list_load (gpinfolist);
160 if (ret < GP_OK) return ret;
161
162 ret = gp_port_info_list_lookup_path (gpinfolist, buf);
163 if (ret < GP_OK) return ret;
164
165 /* Detect all the cameras that can be autodetected... */
166 ret = gp_list_new (&list);
167 if (ret < GP_OK) return 1;
168
169 /* Load all the camera drivers we have... */
170 ret = gp_abilities_list_new (&abilities);
171 if (ret < GP_OK) return ret;
172 ret = gp_abilities_list_load (abilities, context);
173 if (ret < GP_OK) return ret;
174 ret = gp_abilities_list_detect (abilities, gpinfolist, list, context);
175 if (ret < GP_OK) return ret;
176
177 fprintf(stderr, "detect list has count %d\n", gp_list_count(list));
178
179 ret = gp_list_get_name(list, 0, &name);
180 if (ret < GP_OK) goto out;
181
182 ret = sample_open_camera (&camera, name, buf, context);
183 if (ret < GP_OK) {
184 fprintf(stderr,"camera %s at %s not found.\n", name, buf);
185 goto out;
186 }
187
188 ret = gp_camera_init (camera, context);
189 if (ret < GP_OK) {
190 fprintf(stderr,"No camera auto detected.\n");
191 goto out;
192 }
193
194 /* AFL PART STARTS HERE */
195
196 ret = recursive_directory(camera, "/", context, NULL);
197 if (ret < GP_OK) {
198 printf ("Could not recursive list files.\n");
199 goto out;
200 }
201
202 ret = gp_camera_get_summary (camera, &summary, context);
203 if ((ret != GP_OK) && (ret != GP_ERROR_NOT_SUPPORTED)) {
204 printf ("Could not get summary.\n");
205 goto out;
206 }
207
208 #if 1
209 ret = gp_camera_get_config (camera, &rootwidget, context);
210 if (ret < GP_OK) {
211 fprintf (stderr,"Could not get config.\n");
212 goto out;
213 }
214 #endif
215 printf ("OK, %s\n", summary.text);
216
217 ret = gp_camera_get_storageinfo (camera, &storageinfo, &storagecnt, context);
218 if ((ret != GP_OK) && (ret != GP_ERROR_NOT_SUPPORTED)) {
219 printf ("Could not get storage info.\n");
220 goto out;
221 }
222
223
224 ret = gp_camera_trigger_capture (camera, context);
225 if ((ret != GP_OK) && (ret != GP_ERROR_NOT_SUPPORTED)) {
226 printf ("Could not trigger capture.\n");
227 goto out;
228 }
229
230 while (1) {
231 CameraEventType evttype;
232 void *data = NULL;
233
234 ret = gp_camera_wait_for_event(camera, 1, &evttype, &data, context);
235 if (ret < GP_OK) break;
236 if (data) free (data);
237 if (evttype == GP_EVENT_TIMEOUT) break;
238 }
239
240 gp_file_new (&file);
241 ret = gp_camera_capture_preview (camera, file, context);
242 if ((ret != GP_OK) && (ret != GP_ERROR_NOT_SUPPORTED)) {
243 gp_file_free (file);
244 printf ("Could not capture preview.\n");
245 goto out;
246 }
247 gp_file_free (file);
248
249 #if 0
250 /* this gives endless event check loops occasionaly ... need review how to do this best */
251 ret = gp_camera_capture (camera, GP_CAPTURE_IMAGE, &path, context);
252 if ((ret != GP_OK) && (ret != GP_ERROR_NOT_SUPPORTED)) {
253 printf ("Could not capture preview.\n");
254 goto out;
255 }
256 #endif
257
258 /* AFL PART ENDS HERE */
259 out:
260 gp_camera_exit (camera, context);
261 gp_context_unref (context);
262 gp_camera_free (camera);
263 return 0;
264 }
265