Debian Codegraph @

camlibs/kodak/dc3200/dc3200.c

raw
1 /* dc3200.c
2 *
3 * Copyright (C) 2000,2001,2002 donn morrison - [email protected]
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 */
20
21 /****************************************************
22 * kodak dc3200 digital camera driver library *
23 * for gphoto2 *
24 * *
25 * author: donn morrison - [email protected] *
26 * date: dec 2000 - jan 2002 *
27 * license: gpl *
28 * version: 1.6 *
29 * *
30 ****************************************************/
31
32 #include "config.h"
33 #include "dc3200.h"
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <time.h>
39
40 #include <gphoto2/gphoto2-library.h>
41
42 #include "libgphoto2/i18n.h"
43
44 #include "library.h"
45
46
47 /*
48 * FIXME: Use properly sized integer types. The dc3200 code used lots
49 * of u_char, u_long, u_int, which may be wrong on non-32bit systems.
50 */
51
52 #define CONTEXT_EXISTS _("There is currently an operation in progress. \
53 This camera only supports one operation \
54 at a time. Please wait until the current \
55 operation has finished.")
56
57 int camera_id (CameraText *id)
58 {
59 strcpy(id->text, "kodak-dc3200");
60
61 return (GP_OK);
62 }
63
64 int camera_abilities (CameraAbilitiesList *list)
65 {
66 CameraAbilities a;
67
68 memset (&a, 0, sizeof(a));
69 strcpy(a.model, "Kodak:DC3200");
70 a.port = GP_PORT_SERIAL;
71 a.speed[0] = 9600;
72 a.speed[1] = 19200;
73 a.speed[2] = 38400;
74 a.speed[3] = 57600;
75 a.speed[4] = 115200;
76 a.speed[5] = 0;
77 a.operations = GP_OPERATION_NONE;
78 a.file_operations = GP_FILE_OPERATION_PREVIEW;
79 a.folder_operations = GP_FOLDER_OPERATION_NONE;
80
81 gp_abilities_list_append(list, a);
82
83 return (GP_OK);
84 }
85
86 static
87 int init(Camera *camera)
88 {
89 GPPortSettings settings;
90 int ret, selected_speed;
91
92 ret = gp_port_get_settings (camera->port, &settings);
93 if (ret < 0)
94 return (ret);
95
96 /* Remember the selected speed 0 == fastest */
97 selected_speed = settings.serial.speed == 0 ? 115200 : settings.serial.speed;
98
99 settings.serial.speed = 9600;
100 settings.serial.bits = 8;
101 settings.serial.parity = 0;
102 settings.serial.stopbits = 1;
103
104 ret = gp_port_set_settings (camera->port, settings);
105 if (ret < 0)
106 return (ret);
107
108 gp_port_set_timeout (camera->port, TIMEOUT);
109
110 if (dc3200_set_speed (camera, selected_speed) == GP_ERROR)
111 return GP_ERROR;
112
113 /* Set the new speed */
114 settings.serial.speed = selected_speed;
115 ret = gp_port_set_settings (camera->port, settings);
116 if (ret < 0)
117 return (ret);
118
119 /* Wait for it to update */
120 sleep(1);
121
122 /* Try to talk after speed change */
123 if (dc3200_keep_alive(camera) == GP_ERROR)
124 return GP_ERROR;
125
126 /* setup the camera */
127 if (dc3200_setup(camera) == GP_ERROR)
128 return GP_ERROR;
129
130 return GP_OK;
131 }
132
133 static int camera_exit (Camera *camera, GPContext *context)
134 {
135 if (camera->pl) {
136 if(camera->pl->context) {
137 gp_context_error(context, CONTEXT_EXISTS);
138 return GP_ERROR;
139 }
140 free (camera->pl);
141 camera->pl = NULL;
142 }
143
144 return GP_OK;
145 }
146
147 int check_last_use(Camera *camera)
148 {
149 time_t t;
150
151 time(&t);
152
153 if(t - camera->pl->last > 9) {
154 /* we have to re-init the camera */
155 printf(_("camera inactive for > 9 seconds, re-initing.\n"));
156 return init(camera);
157 }
158
159 return GP_OK;
160 }
161
162 static int folder_list_func (CameraFilesystem *fs, const char *folder,
163 CameraList *list, void *user_data,
164 GPContext *context)
165 {
166 Camera *camera = user_data;
167 unsigned char *data = NULL;
168 long unsigned data_len = 0;
169 unsigned char *ptr_data_buff;
170 char filename[13], *ptr;
171 int res;
172 unsigned int i;
173
174 if(camera->pl->context)
175 {
176 gp_context_error(context, CONTEXT_EXISTS);
177 return GP_ERROR;
178 }
179
180 if(check_last_use(camera) == GP_ERROR)
181 {
182 return GP_ERROR;
183 }
184
185 /* get file list data */
186 res = dc3200_get_data (camera, &data, &data_len, CMD_LIST_FILES, folder,
187 NULL);
188 if (res == GP_ERROR)
189 {
190 return GP_ERROR;
191 }
192
193 /* check the data length, each record is 20 bytes */
194 if(data_len%20 != 0 || data_len < 1)
195 {
196 return GP_ERROR;
197 }
198
199 if (data == NULL)
200 {
201 return GP_ERROR;
202 }
203
204 /* add directories to the list */
205 ptr_data_buff = data;
206 i = 0;
207
208 while(i < data_len) {
209 /* directories have 0x10 in their attribute */
210 if(!(ptr_data_buff[11] & 0x10)) {
211 ptr_data_buff += 20;
212 i += 20;
213 continue;
214 }
215
216 /* skip directories starting with . */
217 if(ptr_data_buff[0] == '.') {
218 ptr_data_buff += 20;
219 i += 20;
220 continue;
221 }
222
223 /* copy the filename */
224 strncpy(filename, (char *)ptr_data_buff, sizeof(filename));
225
226 /* chop off the trailing spaces and null terminate */
227 ptr = strchr(filename, 0x20);
228 if(ptr) ptr[0] = 0;
229
230 /* in case of long directory */
231 filename[12] = 0;
232
233 /* append dir to the list */
234 gp_list_append(list, filename, NULL);
235
236 ptr_data_buff += 20;
237 i += 20;
238 }
239
240 free (data);
241 return (dc3200_keep_alive (camera));
242 }
243
244 static int file_list_func (CameraFilesystem *fs, const char *folder,
245 CameraList *list, void *user_data,
246 GPContext *context)
247 {
248 Camera *camera = user_data;
249 unsigned char *data = NULL;
250 long unsigned data_len = 0;
251 unsigned char *ptr_data_buff;
252 char filename[13];
253 int res;
254 unsigned int i;
255
256 if(camera->pl->context)
257 {
258 gp_context_error(context, CONTEXT_EXISTS);
259 return GP_ERROR;
260 }
261
262 if(check_last_use(camera) == GP_ERROR)
263 {
264 return GP_ERROR;
265 }
266
267 /* get file list data */
268 res = dc3200_get_data (camera, &data, &data_len, CMD_LIST_FILES, folder,
269 NULL);
270 if (res == GP_ERROR)
271 {
272 return GP_ERROR;
273 }
274
275 /* check the data length */
276 if(data_len%20 != 0 || data_len < 1)
277 {
278 return GP_ERROR;
279 }
280
281 if(data == NULL)
282 {
283 return GP_ERROR;
284 }
285
286 /* add files to the list */
287 ptr_data_buff = data;
288 i = 0;
289
290 while(i < data_len) {
291 /* files don't have 0x10 in their attribute */
292 if(ptr_data_buff[11] & 0x10) {
293 ptr_data_buff += 20;
294 i += 20;
295 continue;
296 }
297
298 /* copy the first 8 bytes of filename */
299 strncpy(filename, (char *)ptr_data_buff, 8);
300 filename[8] = 0;
301 /* add dot */
302 strcat(filename, ".");
303 /* copy extension, last 3 bytes*/
304 strncat(filename, (char *)ptr_data_buff+8, 3);
305
306 if(!strstr(filename, ".JPG") && !strstr(filename, ".jpg")) {
307 ptr_data_buff += 20;
308 i += 20;
309 continue;
310 }
311
312 /* append file to the list */
313 gp_list_append(list, filename, NULL);
314
315 ptr_data_buff += 20;
316 i += 20;
317 }
318
319 free(data);
320 return (dc3200_keep_alive(camera));
321 }
322
323 static int get_file_func (CameraFilesystem *fs, const char *folder,
324 const char *filename, CameraFileType type,
325 CameraFile *file, void *user_data,
326 GPContext *context)
327 {
328 Camera *camera = user_data;
329 unsigned char *data = NULL;
330 long unsigned data_len = 0;
331 int res;
332
333 if(camera->pl->context)
334 {
335 gp_context_error(context, CONTEXT_EXISTS);
336 return GP_ERROR;
337 }
338
339 camera->pl->context = context;
340
341 if(check_last_use(camera) == GP_ERROR)
342 {
343 camera->pl->context = NULL;
344 return GP_ERROR;
345 }
346
347 switch (type) {
348 case GP_FILE_TYPE_PREVIEW:
349 res = dc3200_get_data (camera, &data, &data_len,
350 CMD_GET_PREVIEW, folder, filename);
351 break;
352 case GP_FILE_TYPE_NORMAL:
353 res = dc3200_get_data (camera, &data, &data_len,
354 CMD_GET_FILE, folder, filename);
355 break;
356 default:
357 camera->pl->context = NULL;
358 return (GP_ERROR_NOT_SUPPORTED);
359 }
360 if (res < 0)
361 {
362 camera->pl->context = NULL;
363 return (res);
364 }
365
366 if (data == NULL || data_len < 1)
367 {
368 camera->pl->context = NULL;
369 return GP_ERROR;
370 }
371
372 gp_file_append (file, (char *)data, data_len);
373
374 free(data);
375 camera->pl->context = NULL;
376 return (dc3200_keep_alive(camera));
377 }
378
379 static int
380 get_info_func (CameraFilesystem *fs, const char *folder,
381 const char *filename, CameraFileInfo *info, void *user_data,
382 GPContext *context)
383 {
384 Camera *camera = user_data;
385 unsigned char *data = NULL;
386 long unsigned data_len = 0;
387 int res;
388 char file[1024];
389
390 if(camera->pl->context)
391 {
392 gp_context_error(context, CONTEXT_EXISTS);
393 return GP_ERROR;
394 }
395
396 if(check_last_use(camera) == GP_ERROR)
397 {
398 return GP_ERROR;
399 }
400
401 if(!folder)
402 {
403 return GP_ERROR;
404 }
405
406 strcpy(file, folder);
407 if(folder[strlen(folder)-1] != '\\' || folder[strlen(folder)-1] != '/')
408 strcat(file, "\\");
409 strcat(file, filename);
410
411 /* get file list data */
412 res = dc3200_get_data (camera, &data, &data_len, CMD_LIST_FILES, file,
413 NULL);
414 if (res == GP_ERROR)
415 {
416 return GP_ERROR;
417 }
418
419 /* check the data length */
420 if(data_len%20 != 0 || data_len < 1)
421 {
422 /* there is a problem */
423 return GP_ERROR;
424 }
425
426 if(data == NULL)
427 {
428 return GP_ERROR;
429 }
430
431 /* get the file length && type and stuff */
432 info->file.fields = GP_FILE_INFO_SIZE | GP_FILE_INFO_TYPE;
433 info->file.size = bytes_to_l(data[19], data[18], data[17], data[16]);
434 strcpy (info->file.type, GP_MIME_JPEG);
435
436 info->preview.fields = GP_FILE_INFO_TYPE;
437 strcpy (info->preview.type, GP_MIME_JPEG);
438
439 free(data);
440 return (dc3200_keep_alive(camera));
441 }
442
443 static int camera_manual (Camera *camera, CameraText *manual, GPContext *context)
444 {
445 strcpy (manual->text,
446 _("Known problems:\n"
447 "\n"
448 "1. If the Kodak DC3200 does not receive a command at least "
449 "every 10 seconds, it will time out, and will have to be "
450 "re-initialized. If you notice the camera does not respond, "
451 "simply re-select the camera. This will cause it to "
452 "reinitialize."));
453 return (GP_OK);
454 }
455
456 static int camera_about (Camera *camera, CameraText *about, GPContext *context)
457 {
458 strcpy (about->text,
459 _("Kodak DC3200 Driver\n"
460 "Donn Morrison <[email protected]>\n"
461 "\n"
462 "Questions and comments appreciated."));
463 return (GP_OK);
464 }
465
466 static CameraFilesystemFuncs fsfuncs = {
467 .file_list_func = file_list_func,
468 .get_file_func = get_file_func,
469 .get_info_func = get_info_func,
470 .folder_list_func = folder_list_func
471 };
472
473 int camera_init (Camera *camera, GPContext *context)
474 {
475 int ret;
476
477 camera->pl = malloc (sizeof (CameraPrivateLibrary));
478 if (!camera->pl)
479 return (GP_ERROR_NO_MEMORY);
480
481 /* First, set up all the function pointers */
482 camera->functions->exit = camera_exit;
483 camera->functions->manual = camera_manual;
484 camera->functions->about = camera_about;
485
486 /* Set up the CameraFilesystem */
487 gp_filesystem_set_funcs (camera->fs, &fsfuncs, camera);
488
489 /* initialize the camera */
490 ret = init (camera);
491 if (ret < 0) {
492 free (camera->pl);
493 camera->pl = NULL;
494 return (ret);
495 }
496
497 ret = dc3200_keep_alive (camera);
498 if (ret < 0) {
499 free (camera->pl);
500 camera->pl = NULL;
501 return (ret);
502 }
503 camera->pl->context = NULL;
504 return (GP_OK);
505 }
506