summaryrefslogtreecommitdiff
path: root/camlibs/canon/serial.h
blob: efea177b036d00ff2c6279e62ca47a9e007d0b7e (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
/****************************************************************************
 *
 * File: serial.h
 *
 * $Id$
 *
 ****************************************************************************/

#ifndef _SERIAL_H
#define _SERIAL_H

/****************************************************************************
 *
 * prototypes
 *
 ****************************************************************************/

int canon_serial_change_speed(GPPort *gdev, int speed);
int canon_serial_init(Camera *camera);
int canon_serial_send(Camera *camera, const unsigned char *buf, int len, int sleep);
int canon_serial_get_byte(GPPort *gdev);
int canon_serial_get_cts(GPPort *gdev);

void serial_flush_input(GPPort *gdev);
void serial_flush_output(GPPort *gdev);
void serial_set_timeout(GPPort *gdev, int to);

int canon_serial_send_frame (Camera *camera, const unsigned char *pkt, int len);
unsigned char *canon_serial_recv_frame (Camera *camera, int *len);
void canon_serial_error_type(Camera *camera);

unsigned char * canon_serial_dialogue (Camera *camera, GPContext *context, unsigned char mtype, unsigned char dir, unsigned int *len, ...);
int canon_serial_send_packet (Camera *camera, unsigned char type, unsigned char seq, unsigned char *pkt, int len);
unsigned char *canon_serial_recv_packet (Camera *camera, unsigned char *type, unsigned char *seq, int *len);
int canon_serial_wait_for_ack (Camera *camera);
unsigned char *canon_serial_recv_msg (Camera *camera, unsigned char mtype, unsigned char dir, unsigned int *total, GPContext *context);

unsigned char *canon_serial_get_file (Camera *camera, const char *name, unsigned int *length, GPContext *context);
int canon_serial_put_file (Camera *camera, CameraFile *file, char *destname, char *destpath, GPContext *context);
int canon_serial_get_dirents (Camera *camera, unsigned char **dirent_data, unsigned int *dirents_length, const char *path, GPContext *context);


int canon_serial_ready (Camera *camera, GPContext *context);
int canon_serial_get_thumbnail (Camera *camera, const char *name, unsigned char **data, unsigned int *length, GPContext *context);

/**
 * MAX_TRIES
 *
 * Maximum number of retries for a serial send operation.
 *
 */
#define MAX_TRIES 10

/**
 * USLEEP1
 *
 * Number of microseconds to wait between characters when trying to
 * contact the camera by sending "U" characters. Currently zero
 * (i.e. no pause between characters).
 *
 */
#define USLEEP1 0

/**
 * USLEEP2
 *
 * Number of microseconds to wait between characters under all other
 * circumstances. Currently 1.
 *
 */
#define USLEEP2 1

/**
 * HDR_FIXED_LEN
 *
 * Length of fixed part of header for uploading a file.
 *
 */
#define HDR_FIXED_LEN 30

/**
 * DATA_BLOCK
 *
 * Maximum length of a data block to upload through the serial port.
 *
 */
#define DATA_BLOCK 1536

/* Defines for error handling */
/* #define NOERROR		0 */
/* #define ERROR_RECEIVED	1 */
/* #define ERROR_ADDRESSED	2 */
/* #define FATAL_ERROR	3 */
/* #define ERROR_LOWBATT	4 */

/**
 * canonSerialErrorCode:
 * @NOERROR: No error
 * @ERROR_RECEIVED: Packet length doesn't match received packet
 * @ERROR_ADDRESSED: Problem receiving EOT
 * @FATAL_ERROR: Fatal error
 * @ERROR_LOWBATT: Battery is low
 *
 * Used within serial code to signal various error conditions.
 *
 */
typedef enum {
	NOERROR		= 0,
	ERROR_RECEIVED	= 1,
	ERROR_ADDRESSED	= 2,
	FATAL_ERROR	= 3,
	ERROR_LOWBATT   = 4
} canonSerialErrorCode;

/* ------------------------- Frame-level processing ------------------------- */

/* #define CANON_FBEG      0xc0 */
/* #define CANON_FEND      0xc1 */
/* #define CANON_ESC       0x7e */
/* #define CANON_XOR       0x20 */
/**
 * canonSerialFramingByte:
 * @CANON_FBEG: Beginning of frame
 * @CANON_FEND: End of frame
 * @CANON_ESC: XOR next byte with 0x20
 * @CANON_XOR: value to use with %CANON_ESC
 *
 * Enumeration of all "special" byte codes on the frame level.
 *
 */
typedef enum {
	CANON_FBEG    = 0xc0,		     /* Beginning of frame */
	CANON_FEND    = 0xc1,		     /* End of frame */
	CANON_ESC     = 0x7e,		     /* XOR next byte with 0x20 */
	CANON_XOR     = 0x20
} canonSerialFramingByte;
/* ------------------------ Packet-level processing ------------------------- */

/**
 * MAX_PKT_PAYLOAD
 *
 * Maximum size of a packet payload; used to allocate buffers.
 *
 */
#define MAX_PKT_PAYLOAD 65535

/* #define MAX_PKT_SIZE    (MAX_PKT_PAYLOAD+4) */

/* #define PKT_HDR_LEN     4 */

/* #define PKT_SEQ         0 */
/* #define PKT_TYPE        1 */
/* #define PKT_LEN_LSB     2 */
/* #define PKT_LEN_MSB     3 */
/**
 * canonPacketOffset:
 * @PKT_SEQ: Offset in packet to message sequence number
 * @PKT_TYPE: Offset in packet to type code
 * @PKT_LEN_LSB: Offset in packet to least-significant byte of packet length.
 * @PKT_LEN_MSB: Offset in packet to most-significant byte of packet length.
 * @PKT_HDR_LEN: Length of complete header.
 *
 * Offsets to bytes in a serial packet header. 
 *
 */
typedef enum {
	PKT_SEQ       = 0,
	PKT_TYPE      = 1,
	PKT_LEN_LSB   = 2,
	PKT_LEN_MSB   = 3,
	PKT_HDR_LEN   = 4
} canonPacketOffset;

/* #define PKT_MSG         0 */
/* #define PKT_EOT         4 */
/* #define PKT_ACK         5 */

/**
 * canonPacketType:
 * @PKT_MSG: Message fragment
 * @PKT_SPD: Speed message from computer sent once, early in the
 *           initialization for the computer to ask the camera to
 *           switch to a higher speed.
 * @PKT_EOT: EOT
 * @PKT_ACK: ACK (or NAK)
 *
 * Packet type for byte 2 of packet header.
 * Unfortunately, these are mixed with %canonPacketThirdByte
 * codes to tell %canon_serial_send_packet what to do.
 *
 */
typedef enum {
	PKT_MSG       = 0,
	PKT_SPD       = 3,
	PKT_EOT       = 4,
	PKT_ACK       = 5
} canonPacketType;


 
/* #define PKT_NACK        255 */
/* #define PKTACK_NACK     0x01 */
/* #define PKT_UPLOAD_EOT  3 */

/**
 * canonPacketThirdByte:
 * @PKTACK_NACK: This ACK is a NACK (not acknowledged) message
 * @PKT_UPLOAD_EOT: This EOT is to end an upload
 * @PKT_NACK: this ACK is a request to retransmit
 *
 * Codes to go in the third byte of an ACK or EOT message.
 * Unfortunately, these are mixed with %canonPacketType
 * codes to tell %canon_serial_send_packet what to do.
 *
 */
typedef enum {
	PKTACK_NACK    = 0x01,
	PKT_UPLOAD_EOT = 3,
	PKT_NACK       = 255
} canonPacketThirdByte;

/* ----------------------- Message-level processing ------------------------ */


/**
 * MAX_MSG_SIZE
 *
 * Maximum size of a message to fit within a packet.
 *
 */
#define MAX_MSG_SIZE    (MAX_PKT_PAYLOAD-12)

/* #define FRAG_NUM                0 */
/* #define FRAG_LEN_LSB    2 */
/* #define FRAG_LEN_MSB    3 */

/* #define MSG_HDR_LEN     16 */
/* #define MSG_02          0 */
/* #define MSG_MTYPE       4 */
/* #define MSG_DIR         7 */
/* #define MSG_LEN_LSB     8 */
/* #define MSG_LEN_MSB     9 */

/* #define MSG_FFFB     12 */

/**
 * canonSerialMsgHeader:
 * @MSG_02: offset to bytes "00 02" in header
 * @MSG_MTYPE: offset to message type byte in header
 * @MSG_DIR : offset to message direction byte in header: 0x11 or 0x12
 *            is output to camera, 0x21 or 0x22 is response from camera
 * @MSG_LEN_LSB: offset to least-significant byte of 16-but message length
 * @MSG_LEN_MSB: offset to most-significant byte of 16-but message length
 * @MSG_HDR_LEN: length of entire message header
 *
 *
 */
typedef enum {
	MSG_02        = 0,
	MSG_MTYPE     = 4,
	MSG_DIR       = 7,
	MSG_LEN_LSB   = 8,
	MSG_LEN_MSB   = 9,
/*	MSG_FFFB      = 12,*/
	MSG_HDR_LEN   = 16
} canonSerialMsgHeader;


/**
 * DIR_REVERSE
 *
 * Value to XOR with direction byte to reverse direction.
 * Converts 0x21 -> 0x11, 0x11 -> 0x21.
 *
 */
#define DIR_REVERSE     0x30

/**
 * UPLOAD_DATA_BLOCK
 *
 * Size of blocks to upload a file.
 */
#define UPLOAD_DATA_BLOCK 900

/* ----------------------- Command-level processing ------------------------ */


/**
 * SPEED_9600
 *
 * String to send to set camera speed to 9600 bits per second.
 *
 */
#define SPEED_9600   (unsigned char *)"\x00\x03\x02\x02\x01\x10\x00\x00\x00\x00\xc0\x39"

/**
 * SPEED_19200
 *
 * String to send to set camera speed to 19200 bits per second.
 *
 */
#define SPEED_19200  (unsigned char *)"\x00\x03\x08\x02\x01\x10\x00\x00\x00\x00\x13\x1f"

/**
 * SPEED_38400
 *
 * String to send to set camera speed to 38400 bits per second.
 *
 */
#define SPEED_38400  (unsigned char *)"\x00\x03\x20\x02\x01\x10\x00\x00\x00\x00\x5f\x84"

/**
 * SPEED_57600
 *
 * String to send to set camera speed to 57600 bits per second.
 *
 */
#define SPEED_57600  (unsigned char *)"\x00\x03\x40\x02\x01\x10\x00\x00\x00\x00\x5e\x57"

/**
 * SPEED_115200
 *
 * String to send to set camera speed to 115200 bits per second.
 *
 */
#define SPEED_115200 (unsigned char *)"\x00\x03\x80\x02\x01\x10\x00\x00\x00\x00\x4d\xf9"



#endif /* _SERIAL_H */

/****************************************************************************
 *
 * End of file: serial.h
 *
 ****************************************************************************/

/*
 * Local Variables:
 * c-file-style:"linux"
 * indent-tabs-mode:t
 * End:
 */