summaryrefslogtreecommitdiff
path: root/camlibs/minolta/dimagev/data.c
blob: 0474a9fc4dc72ed0fbc8ba870f248e30ec49d971 (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
/**********************************************************************
*       Minolta Dimage V digital camera communication library         *
*               Copyright 2000,2001 Gus Hartmann                      *
*                                                                     *
*    This program is free software; you can redistribute it and/or    *
*    modify it under the terms of the GNU General Public License as   *
*    published by the Free Software Foundation; either version 2 of   *
*    the License, or (at your option) any later version.              *
*                                                                     *
*    This program is distributed in the hope that it will be useful,  *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of   *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
*    GNU General Public License for more details.                     *
*                                                                     *
*    You should have received a copy of the GNU General Public        *
*    License along with this program; if not, write to the *
*    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
*    Boston, MA  02110-1301  USA
*                                                                     *
**********************************************************************/

/* $Id$ */

#include "config.h"

#include "dimagev.h"

#define GP_MODULE "dimagev"

/* This is the parent function, who calls most of the functions below.
   It returns GP_ERROR if it cannot get the camera data, and GP_OK otherwise.
   The subroutines will print out more detained information should they fail.
*/
int dimagev_get_camera_data(dimagev_t *dimagev) {
	dimagev_packet *p, *raw;
	unsigned char char_buffer;

	/* Check the device. */
	if ( dimagev->dev == NULL ) {
		GP_DEBUG( "dimagev_get_camera_data::device not valid");
		return GP_ERROR_BAD_PARAMETERS;
	}

	/* Let's say hello and get the current status. */
	if ( ( p = dimagev_make_packet(DIMAGEV_GET_DATA, 1, 0)) == NULL ) {
		GP_DEBUG( "dimagev_get_camera_data::unable to allocate packet");
		return GP_ERROR_NO_MEMORY;
	}

	if ( gp_port_write(dimagev->dev, (char*)p->buffer, p->length) < GP_OK ) {
		GP_DEBUG( "dimagev_get_camera_data::unable to write packet");
		free(p);
		return GP_ERROR_IO;
	} else if ( gp_port_read(dimagev->dev, (char*)&char_buffer, 1) < GP_OK ) {
		GP_DEBUG( "dimagev_get_camera_data::no response from camera");
		free(p);
		return GP_ERROR_IO;
	}

	free(p);

	switch ( char_buffer ) {
		case DIMAGEV_ACK:
			break;
		case DIMAGEV_NAK:
			GP_DEBUG( "dimagev_get_camera_data::camera did not acknowledge transmission");
			return GP_ERROR_IO;
		case DIMAGEV_CAN:
			GP_DEBUG( "dimagev_get_camera_data::camera cancels transmission");
			return GP_ERROR_IO;
		default:
			GP_DEBUG( "dimagev_get_camera_data::camera responded with unknown value %x", char_buffer);
			return GP_ERROR_IO;
	}

	if ( ( p = dimagev_read_packet(dimagev) ) == NULL ) {
		GP_DEBUG( "dimagev_get_camera_data::unable to read packet");
		return GP_ERROR_IO;
	}

	char_buffer = DIMAGEV_EOT;
	if ( gp_port_write(dimagev->dev, (char*)&char_buffer, 1) < GP_OK ) {
		GP_DEBUG( "dimagev_get_camera_data::unable to send EOT");
		free(p);
		return GP_ERROR_IO;
	}
		
	if ( gp_port_read(dimagev->dev, (char*)&char_buffer, 1) < GP_OK ) {
		GP_DEBUG( "dimagev_get_camera_data::no response from camera");
		free(p);
		return GP_ERROR_IO;
	}

	switch ( char_buffer ) {
		case DIMAGEV_ACK:
			break;
		case DIMAGEV_NAK:
			GP_DEBUG( "dimagev_get_camera_data::camera did not acknowledge transmission");
			free(p);
			return GP_ERROR_IO;
		case DIMAGEV_CAN:
			GP_DEBUG( "dimagev_get_camera_data::camera cancels transmission");
			free(p);
			return GP_ERROR_IO;
		default:
			GP_DEBUG( "dimagev_get_camera_data::camera responded with unknown value %x", char_buffer);
			free(p);
			return GP_ERROR_IO;
	}

	if ( ( raw = dimagev_strip_packet(p) ) == NULL ) {
		GP_DEBUG( "dimagev_get_camera_data::unable to strip data packet");
		free(p);
		return GP_ERROR;
	}

	if ( ( dimagev->data = dimagev_import_camera_data(raw->buffer)) == NULL ) {
		GP_DEBUG( "dimagev_get_camera_data::unable to read camera data");
		free(raw);
		free(p);
		return GP_ERROR;
	}

	/* Sure it *should* get freed automagically, but why take the risk? */
	free(p);
	free(raw);

	return GP_OK;
}

/* This function sends the contents of a dimagev_data_t to the current camera.
   This allows many changes to be made (e.g. entering host mode and record
   mode) while only sending a single set_data command.
*/
int dimagev_send_data(dimagev_t *dimagev) {
	dimagev_packet *p;
	unsigned char *export_data, char_buffer;


	if ( dimagev == NULL ) {
		GP_DEBUG( "dimagev_send_data::unable to use NULL dimagev_t");
		return GP_ERROR_BAD_PARAMETERS;
	}

	if ( ( export_data = dimagev_export_camera_data(dimagev->data) ) == NULL ) {
		GP_DEBUG( "dimagev_send_data::unable to export camera data");
		return GP_ERROR;
	}

	dimagev_dump_camera_data(dimagev->data);

	if ( ( p = dimagev_make_packet(DIMAGEV_SET_DATA, 1, 0) ) == NULL ) {
		GP_DEBUG( "dimagev_send_data::unable to create set_data packet");
		free(export_data);
		return GP_ERROR_NO_MEMORY;
	}

	if ( gp_port_write(dimagev->dev, (char*)p->buffer, p->length) < GP_OK ) {
		GP_DEBUG( "dimagev_send_data::unable to send set_data packet");
		free(p);
		free(export_data);
		return GP_ERROR_IO;
	} else if ( gp_port_read(dimagev->dev, (char*)&char_buffer, 1) < GP_OK ) {
		GP_DEBUG( "dimagev_send_data::no response from camera - trying to send NAK");
		free(p);
		free(export_data);
		return GP_ERROR_IO;
	}
		
	free(p);

	switch ( char_buffer ) {
		case DIMAGEV_ACK:
			break;
		case DIMAGEV_NAK:
			GP_DEBUG( "dimagev_send_data::camera did not acknowledge transmission");
			free(export_data);
			return GP_ERROR_IO;
		case DIMAGEV_CAN:
			GP_DEBUG( "dimagev_send_data::camera cancels transmission");
			free(export_data);
			return GP_ERROR_IO;
		default:
			GP_DEBUG( "dimagev_send_data::camera responded with unknown value %x", char_buffer);
			free(export_data);
			return GP_ERROR_IO;
	}

	if ( ( p = dimagev_make_packet(export_data, 9, 1) ) == NULL ) {
		GP_DEBUG( "dimagev_send_data::unable to create set_data packet");
		free(export_data);
		return GP_ERROR_NO_MEMORY;
	}

	free(export_data);

	if ( gp_port_write(dimagev->dev, (char*)p->buffer, p->length) < GP_OK ) {
		GP_DEBUG( "dimagev_send_data::unable to send data packet");
		free(p);
		return GP_ERROR_IO;
	}

	free(p);
		
	if ( gp_port_read(dimagev->dev, (char*)&char_buffer, 1) < GP_OK ) {
		GP_DEBUG( "dimagev_send_data::no response from camera");
		return GP_ERROR_IO;
	}

	switch ( char_buffer ) {
		case DIMAGEV_ACK:
			break;
		case DIMAGEV_NAK:
			GP_DEBUG( "dimagev_send_data::camera did not acknowledge transmission");
			return GP_ERROR_IO;
		case DIMAGEV_CAN:
			GP_DEBUG( "dimagev_send_data::camera cancels transmission");
			return GP_ERROR_IO;
		default:
			GP_DEBUG( "dimagev_send_data::camera responded with unknown value %x", char_buffer);
			return GP_ERROR_IO;
	}


	char_buffer = DIMAGEV_EOT;
	if ( gp_port_write(dimagev->dev, (char*)&char_buffer, 1) < GP_OK ) {
		GP_DEBUG( "dimagev_send_data::unable to send EOT");
		return GP_ERROR_IO;
	}
		
	if ( gp_port_read(dimagev->dev, (char*)&char_buffer, 1) < GP_OK ) {
		GP_DEBUG( "dimagev_send_data::no response from camera");
		return GP_ERROR_IO;
	}

	switch ( char_buffer ) {
		case DIMAGEV_ACK:
			break;
		case DIMAGEV_NAK:
			GP_DEBUG( "dimagev_send_data::camera did not acknowledge transmission");
			return GP_ERROR_IO;
		case DIMAGEV_CAN:
			GP_DEBUG( "dimagev_send_data::camera cancels transmission");
			return GP_ERROR_IO;
		default:
			GP_DEBUG( "dimagev_send_data::camera responded with unknown value %x", char_buffer);
			return GP_ERROR_IO;
	}

	if ( sleep(3) != 0 ) {
		GP_DEBUG( "dimagev_send_data::sleep() returned non-zero value");
	}

	return GP_OK;
}

/* This function populates and retuens a dimagev_data_t with the values
   contained inside an array of bytes, as returned by the Dimage V. See
   the description of these fields in the appropriate header file for
   more information on the bit-packing done here.
*/
dimagev_data_t *dimagev_import_camera_data(unsigned char *raw_data) {
	dimagev_data_t *processed_data;

	if ( raw_data == NULL ) {
		return NULL;
	}

	if ( ( processed_data = malloc(sizeof(dimagev_data_t)) ) == NULL ) {
		return NULL;
	}

	/* They really pack those bit fields. */
	processed_data->host_mode = ( raw_data[0] & 0x80) >> 7;
	processed_data->exposure_valid = (raw_data[0] & 0x40 ) >> 6;
	processed_data->date_valid = (raw_data[0] & 0x20 ) >> 5;
	processed_data->self_timer_mode = (raw_data[0] & 0x10 ) >> 4;
	processed_data->flash_mode = ( raw_data[0] & 0x0C ) >> 2;
	processed_data->quality_setting = ( raw_data[0] & 0x02 ) >> 1;
	processed_data->play_rec_mode = (raw_data[0] & 0x01 );
	processed_data->year = dimagev_bcd_to_decimal(raw_data[1]);
	processed_data->month = dimagev_bcd_to_decimal(raw_data[2]);
	processed_data->day = dimagev_bcd_to_decimal(raw_data[3]);
	processed_data->hour = dimagev_bcd_to_decimal(raw_data[4]);
	processed_data->minute = dimagev_bcd_to_decimal(raw_data[5]);
	processed_data->second = dimagev_bcd_to_decimal(raw_data[6]);
	processed_data->exposure_correction = raw_data[7];
	processed_data->valid = ( raw_data[8] & 0x80) >> 7;
	processed_data->id_number = ( raw_data[8] & 0x7F);

	return processed_data;
}

unsigned char *dimagev_export_camera_data(dimagev_data_t *good_data) {
	unsigned char *export_data;

	if ( ( export_data = malloc(9) ) == NULL ) {
		perror("dimagev_export_camera_data::unable to allocate buffer");
		return NULL;
	}

	export_data[0] = (unsigned char) 0;
	export_data[0] = export_data[0] | ( ( ( good_data->host_mode ) << 7 ) & 0x80 );
	export_data[0] = export_data[0] | ( ( ( good_data->exposure_valid ) << 6 ) & 0x40 );
	export_data[0] = export_data[0] | ( ( ( good_data->date_valid ) << 5 ) & 0x20 );
	export_data[0] = export_data[0] | ( ( ( good_data->self_timer_mode ) << 4 ) & 0x10 );
	export_data[0] = export_data[0] | ( ( ( good_data->flash_mode ) << 2 ) & 0x0C );
	export_data[0] = export_data[0] | ( ( ( good_data->quality_setting ) << 1 ) & 0x02 );
	export_data[0] = export_data[0] | ( ( ( good_data->play_rec_mode )) & 0x01 );
	export_data[1] = (unsigned char) 0;
	export_data[1] = dimagev_decimal_to_bcd(good_data->year);
	export_data[2] = (unsigned char) 0;
	export_data[2] = dimagev_decimal_to_bcd(good_data->month);
	export_data[3] = (unsigned char) 0;
	export_data[3] = dimagev_decimal_to_bcd(good_data->day);
	export_data[4] = (unsigned char) 0;
	export_data[4] = dimagev_decimal_to_bcd(good_data->hour);
	export_data[5] = (unsigned char) 0;
	export_data[5] = dimagev_decimal_to_bcd(good_data->minute);
	export_data[6] = (unsigned char) 0;
	export_data[6] = dimagev_decimal_to_bcd(good_data->second);
	export_data[7] = (unsigned char) 0;
	export_data[7] = good_data->exposure_correction;
	export_data[8] = (unsigned char) 0;
	export_data[8] = export_data[8] & ( ( ( good_data->valid ) << 7 ) & 0x80 );
	export_data[8] = export_data[8] & ( ( ( good_data->id_number ) ) & 0x7F );

	return export_data;
}

void dimagev_dump_camera_data(dimagev_data_t *data) {
	GP_DEBUG( "========= Begin Camera Data =========");
	GP_DEBUG( "Host mode: %s ( %d )", data->host_mode != (unsigned char) 0 ? "Host mode" : "Camera mode", data->host_mode);
	GP_DEBUG( "Exposure valid: %s ( %d )", data->exposure_valid != (unsigned char) 0 ? "Valid" : "Not Valid", data->exposure_valid);
	GP_DEBUG( "Exposure correction: %d", (signed char)data->exposure_correction);
	GP_DEBUG( "Date valid: %s ( %d )", data->date_valid != (unsigned char) 0 ? "Valid" : "Not Valid", data->exposure_valid);
	GP_DEBUG( "Self timer mode: %s ( %d )", data->self_timer_mode != (unsigned char) 0 ? "Yes" : "No", data->self_timer_mode);
	GP_DEBUG( "Flash mode: ");

	switch ( data->flash_mode ) {
		case 0:
			GP_DEBUG( "\tAuto ( 0 )");
			break;
		case 1:
			GP_DEBUG( "\tForce Flash ( 1 )");
			break;
		case 2:
			GP_DEBUG( "\tProhibit Flash ( 2 )");
			break;
		default:
			GP_DEBUG( "\tInvalid mode for flash ( %d )", data->flash_mode);
			break;
	}

	GP_DEBUG( "Quality mode: %s ( %d )", data->quality_setting ? "High" : "Low", data->quality_setting);
	GP_DEBUG( "Play or Record mode: %s ( %d )", data->play_rec_mode != (unsigned char) 0 ? "Record" : "Play", data->play_rec_mode);
	GP_DEBUG( "Date: %02d/%02d/%02d %02d:%02d:%02d", data->year, data->month, data->day, data->hour, data->minute, data->second);
	GP_DEBUG( "Card ID Valid: %s ( %d )", data->valid != (unsigned char) 0 ? "Valid" : "Invalid", data->valid);
	GP_DEBUG( "Card ID Data: %02x", data->id_number);
	GP_DEBUG( "========== End Camera Data ==========");

	return;
}

/* This function gets the current system time, sets the contents of the current
   dimagev->data struct appropriately, and then sends the data.
*/
int dimagev_set_date(dimagev_t *dimagev) {
	struct tm *this_time;
	time_t now;

	if ( dimagev == NULL ) {
		return GP_ERROR_BAD_PARAMETERS;
	}

	if ( ( now = time(NULL) ) < 0 ) {
		GP_DEBUG( "dimagev_set_date::unable to get system time");
		return GP_ERROR;
	}

	if ( ( this_time = localtime(&now) ) == NULL ) {
		GP_DEBUG( "dimagev_set_date::unable to convert system time to local time");
		return GP_ERROR;
	}

	GP_DEBUG( "Setting clock to %02d/%02d/%02d %02d:%02d:%02d", this_time->tm_year % 100, ( this_time->tm_mon + 1 ), this_time->tm_mday, this_time->tm_hour, this_time->tm_min, this_time->tm_sec);

	dimagev->data->date_valid = (unsigned char) 1;
	dimagev->data->year = (unsigned char) ( this_time->tm_year % 100 );
	dimagev->data->month = (unsigned char) ( this_time->tm_mon + 1 );
	dimagev->data->day = (unsigned char) this_time->tm_mday;
	dimagev->data->hour = (unsigned char) this_time->tm_hour;
	dimagev->data->minute = (unsigned char) this_time->tm_min;
	dimagev->data->second = (unsigned char) this_time->tm_sec;

	if ( dimagev_send_data(dimagev) < GP_OK ) {
		GP_DEBUG( "dimagev_set_date::unable to set time");
		return GP_ERROR_IO;
	}

	/* So we don't set this date again later by mistake. */
	dimagev->data->date_valid = (unsigned char) 0;

	if ( dimagev_send_data(dimagev) < GP_OK ) {
		GP_DEBUG( "dimagev_set_date::unable to set time");
		return GP_ERROR_IO;
	}

	return GP_OK;
}