summaryrefslogtreecommitdiff
path: root/camlibs/kodak/dc210/dc210.c
blob: 374abfc527edf35c108ac16e9bcc766d758f4edc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
  DC210 driver
  Copyright (c) 2001 Hubert Figuiere
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <gphoto2.h>
#include <gphoto2-port.h>

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define _(String) (String)
#    define N_(String) (String)
#  endif
#else
#  define _(String) (String)
#  define N_(String) (String)
#endif

#include "dc210.h"
#include "library.h"

int camera_id (CameraText *id) 
{
	strcpy(id->text, "REPLACE WITH UNIQUE LIBRARY ID");

	return (GP_OK);
}

int camera_abilities (CameraAbilitiesList *list) 
{
    CameraAbilities a;
    
    memset(&a, 0, sizeof(a));
    strcpy(a.model, "Kodak DC210");
    a.port     = GP_PORT_SERIAL;
    a.speed[0] = 9600;
    a.speed[1] = 19200;
    a.speed[2] = 38400;
    a.speed[3] = 57600;
    a.speed[4] = 115200;
    a.speed[5] = 0;
    a.operations        = 	GP_OPERATION_CAPTURE_IMAGE;
    a.file_operations   = 	GP_FILE_OPERATION_DELETE | 
                                GP_FILE_OPERATION_PREVIEW;
    a.folder_operations = 	GP_FOLDER_OPERATION_NONE;
    
    gp_abilities_list_append(list, a);
    
    return (GP_OK);
}

int camera_init (Camera *camera) 
{
    gp_port_settings settings;

    DC210Data *dd;
    int ret;
    
    if (!camera) {
        return (GP_ERROR);
    }
    
    dd = (DC210Data *)malloc (sizeof (DC210Data));
    if (dd == NULL) {
        return GP_ERROR;
    }

    /* First, set up all the function pointers */
    camera->functions->id 			= camera_id;
    camera->functions->abilities 		= camera_abilities;
    camera->functions->init 		= camera_init;
    camera->functions->exit 		= camera_exit;
    camera->functions->folder_list_folders 	= camera_folder_list_folders;
    camera->functions->folder_list_files	= camera_folder_list_files;
    camera->functions->file_get 		= camera_file_get;
    camera->functions->file_delete 		= camera_file_delete;
#if 0
    camera->functions->config_get   	= camera_config_get;
    camera->functions->config_set   	= camera_config_set;
    camera->functions->folder_config_get 	= camera_folder_config_get;
    camera->functions->folder_config_set 	= camera_folder_config_set;
    camera->functions->file_config_get 	= camera_file_config_get;
    camera->functions->file_config_set 	= camera_file_config_set;
#endif
    camera->functions->capture 		= camera_capture;
    camera->functions->summary		= camera_summary;
    camera->functions->manual 		= camera_manual;
    camera->functions->about 		= camera_about;
    camera->functions->result_as_string 	= camera_result_as_string;

    switch (camera->port->type) {
    case GP_PORT_SERIAL:
        if ((ret = gp_port_new(&(dd->dev), GP_PORT_SERIAL)) < 0) {
            free(dd);
            return (GP_ERROR);
        }
        strcpy(settings.serial.port, camera->port->path);
        settings.serial.speed    = 9600;
        settings.serial.bits     = 8;
        settings.serial.parity   = 0;
        settings.serial.stopbits = 1;
        break;
    default:
        return GP_ERROR;
        break;
    }

    if (gp_port_settings_set(dd->dev, settings) == GP_ERROR) {
        gp_port_free(dd->dev);
        free(dd);
        return (GP_ERROR);
    }

    if (gp_port_open(dd->dev) == GP_ERROR) {
        gp_port_free(dd->dev);
        free(dd);
        return (GP_ERROR);
    }

    /* timeout is set to 1500 */
    gp_port_timeout_set (dd->dev, 1500);

    /* Reset the camera to 9600 by sending a break. */
    gp_port_send_break(dd->dev, 1);
    
    /* Wait for it to reset */
    GP_SYSTEM_SLEEP(1500);

    if (kodak_dc210_open_camera(dd) == GP_ERROR) {
        gp_port_close(dd->dev);
        gp_port_free(dd->dev);
        free(dd);
        return (GP_ERROR);
    }

    camera->camlib_data = dd;

    

    return (GP_OK);
}

int camera_exit (Camera *camera) 
{
    int ret;
    DC210Data * dd = (DC210Data *)camera->camlib_data;

    ret = kodak_dc210_close_camera (dd);

    if (dd->dev) {
	gp_port_close(dd->dev);
        if (ret == GP_ERROR) { 
	    gp_debug_printf (GP_DEBUG_LOW, "dc210", 
			     "%s,%d: gp_port_close = %d", 
			     __FILE__, __LINE__, ret);
	    
	}
        gp_port_free(dd->dev);
    }
    free(dd);

    return ret;
}

int camera_folder_list_folders (Camera *camera, const char *folder, 
				CameraList *list) 
{
    /* there is only one folder */
    /* don't list */
    gp_debug_printf (GP_DEBUG_LOW, "dc210", 
		     "%s,%d: camera_folder_list_folders: unimplemented", 
		     __FILE__, __LINE__);
    
    return (GP_ERROR);
}

int camera_folder_list_files (Camera *camera, const char *folder, 
			      CameraList *list)
{
    int ret;
    DC210Data * dd = (DC210Data *)camera->camlib_data;

    ret = kodak_dc210_get_directory_listing (dd, list);
/*    gp_debug_printf (GP_DEBUG_LOW, "dc210", 
			     "kodak_dc210_get_picture_info failed %d", ret);*/
    return ret;
}


/*
  Possible ops are:
  -getPic 0;
  -getThumbnail 1;
  -delete 2;
 */
static int _camera_file_op (Camera *camera, const char *folder, 
                            const char *filename, CameraFile *file, int op)
{
    int ret = GP_OK;
    int count = 0;
    int i;
    CameraList * list;
    const char * current;
    DC210Data * dd = (DC210Data *)camera->camlib_data;

    if (!dd) {
	gp_debug_printf (GP_DEBUG_LOW, "dc210", "%s,%d: unable to get dd", 
			 __FILE__, __LINE__);
   	return GP_ERROR;
    }

    gp_list_new (&list);
    ret = kodak_dc210_get_directory_listing (dd, list);
    count = gp_list_count (list);

    for (i = 0; i < count; i++) {
        gp_list_get_name (list, i, &current);
        if (current == NULL) {
            /* ERROR */
            ret = GP_ERROR;
            break;
        }

        if (strcmp (current, filename) == 0) {
            /* picture found */
            switch (op) {
            case 0:
                ret = kodak_dc210_get_picture (dd, i, file);
                break;
            case 1:
                ret = kodak_dc210_get_thumbnail (dd, i, file);                
                break;
            case 2:
                ret = kodak_dc210_delete_picture (dd, i);
                break;
            default:
		gp_debug_printf (GP_DEBUG_LOW, "dc210", 
				 "%s,%d: unknown file op = %d", 
				 __FILE__, __LINE__, op);
                /* DOH ! */
                break;
            }
	    /* we found our file */
            break;
        }
    }
    gp_list_free (list);
    gp_debug_printf (GP_DEBUG_LOW, "dc210", "%s,%d: file op completed = %d", 
		     __FILE__, __LINE__, ret);
    return ret;
}


/* Get the file */
int camera_file_get (Camera *camera, const char *folder, const char *filename, 
		     CameraFileType type, CameraFile *file)
{ 
    switch (type) 
    {
    case GP_FILE_TYPE_NORMAL:
	return _camera_file_op (camera, folder, filename, file, 0);
	break;
    case GP_FILE_TYPE_PREVIEW:
	return _camera_file_op (camera, folder, filename, file, 1);
	break;
    default:
	return GP_ERROR_NOT_SUPPORTED;
    }
}


int camera_folder_put_file (Camera *camera, const char *folder, 
			    CameraFile *file)
{
    /* not supported */
    return GP_ERROR;
}

int camera_file_delete (Camera *camera, const char *folder, 
			const char *filename) 
{
    return _camera_file_op (camera, folder, filename, NULL, 2);
}

#if 0
int camera_config_get (Camera *camera, CameraWidget **window) 
{

	*window = gp_widget_new (GP_WIDGET_WINDOW, "Camera Configuration");

	// Append your sections and widgets here.

	return (GP_OK);
}

int camera_config_set (Camera *camera, CameraWidget *window) 
{
	// Check, if the widget's value have changed.

	return (GP_OK);
}
#endif

int camera_capture (Camera *camera, int capture_type, CameraFilePath *path) 
{
    int ret = GP_OK;
    int numPicBefore;
    int numPicAfter;
    struct kodak_dc210_picture_info picInfo;    
    DC210Data * dd = (DC210Data *)camera->camlib_data;
    
    /* find out how many pictures are in the camera so we can
       make sure a picture was taken later */
    numPicBefore = kodak_dc210_number_of_pictures(dd);
    
    /* take a picture -- it returns the picture number taken */
    numPicAfter = kodak_dc210_capture (dd);
    
    /* if a picture was taken then get the picture from the camera and
       then delete it */
    if (numPicBefore + 1 == numPicAfter)
    {
	strcpy (path->folder, "/");
	ret = kodak_dc210_get_picture_info (dd, numPicAfter, &picInfo);
	strncpy (path->name, picInfo.fileName, 12);
	path->name[12] = 0;
    }
    
    return ret;
}

int camera_summary (Camera *camera, CameraText *summary) 
{
    static char summary_string[2048] = "";
    char buff[1024];
    struct kodak_dc210_status status;

    DC210Data * dd = (DC210Data *)camera->camlib_data;
    
    if (kodak_dc210_get_camera_status (dd, &status))
    {
        strcpy(summary_string,"Kodak DC210\n");
        
        snprintf(buff,1024,"Camera Identification: %s\n",status.camera_ident);
        strcat(summary_string,buff);
        
        snprintf(buff,1024,"Camera Type: %d\n",status.camera_type_id);
        strcat(summary_string,buff);
        
        snprintf(buff,1024,"Firmware: %d.%d\n",status.firmware_major,status.firmware_minor);
        strcat(summary_string,buff);
        
        snprintf(buff,1024,"Battery Status: %d\n",status.batteryStatusId);
        strcat(summary_string,buff);
        
        snprintf(buff,1024,"AC Status: %d\n",status.acStatusId);
        strcat(summary_string,buff);
        
        strftime(buff,1024,"Time: %a, %d %b %y %T\n",localtime((time_t *)&status.time));
        strcat(summary_string,buff);
        
        fprintf(stderr,"step 4\n");
        snprintf(buff,1024,"Total Pictures Taken: %d\n",status.totalPicturesTaken);
        strcat(summary_string,buff);
        
        snprintf(buff,1024,"Total Flashes Fired: %d\n",status.totalFlashesFired);
        strcat(summary_string,buff);
        
        snprintf(buff,1024,"Pictures in Camera: %d\n",status.num_pictures);
        strcat(summary_string,buff);
        
    }
    
    strcpy(summary->text, buff);
    
    return (GP_OK);
}

int camera_manual (Camera *camera, CameraText *manual) 
{
    strcpy(manual->text, _("No Manual Available"));
    
    return (GP_OK);
}

int camera_about (Camera *camera, CameraText *about) 
{
	strcpy(about->text, 
               "Kodak DC210 Camera Library\n
by Brian Hirt <bhirt@mobygames.com> http://www.mobygames.com\n
gphoto2 port by Hubert Figuiere <hfiguiere@teaser.fr>");
        
	return (GP_OK);
}

char* camera_result_as_string (Camera *camera, int result) 
{
	if (result >= 0) return ("This is not an error...");
	if (-result < 100) return gp_result_as_string (result);
	return ("This is a template specific error.");
}