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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
/******************************************************************************/
/* See lowlevel.h for licence and details. */
/******************************************************************************/
/******************************************************************************/
/* Included header files */
/******************************************************************************/
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <gpio/gpio.h>
#include <gphoto2.h>
#include "lowlevel.h"
/******************************************************************************/
/* Definitions */
/******************************************************************************/
#define DEFAULT_TIMEOUT 300
#define STX 0x02 /****************/
#define ETX 0x03 /* */
#define ENQ 0x05 /* */
#define ACK 0x06 /* */
#define XOFF 0x11 /* ESC quotes. */
#define XON 0x13 /* */
#define NACK 0x15 /* */
#define ETB 0x17 /* */
#define ESC 0x1b /****************/
#define EOT 0x04
/******************************************************************************/
/* Static Variables */
/******************************************************************************/
static gpio_device *device;
static gboolean l_debug_flag = FALSE;
/******************************************************************************/
/* Prototypes */
/******************************************************************************/
l_return_status_t l_esc_read (guchar *c);
l_return_status_t l_send (
unsigned char *send_buffer,
unsigned int send_buffer_size);
l_return_status_t l_receive (
unsigned char **rb,
unsigned int *rbs,
guint timeout);
/******************************************************************************/
/* Functions */
/******************************************************************************/
l_return_status_t l_init (CameraPortInfo port_settings, gboolean debug_flag)
{
guchar c;
gint i;
gpio_device_settings settings;
l_debug_flag = debug_flag;
device = gpio_new (GPIO_DEVICE_SERIAL);
gpio_set_timeout (device, DEFAULT_TIMEOUT);
strcpy (settings.serial.port, port_settings.path);
settings.serial.speed = port_settings.speed;
settings.serial.bits = 8;
settings.serial.parity = 0;
settings.serial.stopbits = 1;
gpio_set_settings(device, settings);
if (gpio_open(device) == GPIO_ERROR) return L_IO_ERROR;
for (i = 0; ; i++) {
/****************/
/* Write ENQ. */
/****************/
if (gpio_write (device, "\5", 1) == GPIO_ERROR)
return L_IO_ERROR;
if (l_debug_flag) printf ("Sent:\n 5\n");
if (gpio_read (device, &c, 1) < 1) {
/******************************************************/
/* We didn't receive anything. We'll try up to five */
/* time. */
/******************************************************/
if (i == 4) return (L_IO_ERROR);
continue;
}
if (l_debug_flag) printf ("Received:\n%4i\n", c);
switch (c) {
case ACK:
/******************************************************/
/* ACK received. We can proceed. */
/******************************************************/
return (L_SUCCESS);
default:
/******************************************************/
/* The camera seems to be sending something. We'll */
/* dump all bytes we get and try again. */
/******************************************************/
if (l_debug_flag) printf ("Received:\n");
while (gpio_read (device, &c, 1) > 0)
if (l_debug_flag) printf ("%4i", c);
if (l_debug_flag) printf ("\n");
i--;
break;
}
}
}
l_return_status_t l_exit ()
{
if (gpio_close (device) == GPIO_ERROR) return (L_IO_ERROR);
if (gpio_free (device) == GPIO_ERROR) return (L_IO_ERROR);
return L_SUCCESS;
}
l_return_status_t l_esc_read (guchar *c)
{
if (gpio_read (device, c, 1) < 1) return L_IO_ERROR;
if (l_debug_flag) printf ("%4i", *c);
/**********************************************************************/
/* STX, ETX, ENQ, ACK, XOFF, XON, NACK, and ETB have to be masked by */
/* ESC. If we receive one of those (except ETX and ETB) without mask, */
/* we will not report an error, as it will be recovered automatically */
/* later. If we receive ETX or ETB, we reached the end of the packet */
/* and report a transmission error, so that the error can be */
/* recovered. */
/* If the camera sends us ESC (the mask), we will not count this byte */
/* and read a second one. This will be reverted and processed. It */
/* then must be one of STX, ETX, ENQ, ACK, XOFF, XON, NACK, ETB, or */
/* ESC. As before, if it is not one of those, we'll not report an */
/* error, as it will be recovered automatically later. */
/**********************************************************************/
if ((*c == STX ) || (*c == ETX) || (*c == ENQ ) || (*c == ACK) ||
(*c == XOFF) || (*c == XON) || (*c == NACK) || (*c == ETB)) {
if (l_debug_flag) printf ("****");
if ((*c == ETX) || (*c == ETB)) return (L_TRANSMISSION_ERROR);
} else if (*c == ESC) {
if (gpio_read (device, c, 1) < 1) return L_IO_ERROR;
if (l_debug_flag) printf ("%4i", *c);
*c = (~*c & 0xff);
if ((*c != STX ) && (*c != ETX ) && (*c != ENQ) &&
(*c != ACK ) && (*c != XOFF) && (*c != XON) &&
(*c != NACK) && (*c != ETB ) && (*c != ESC))
if (l_debug_flag) printf ("****");
}
return (L_SUCCESS);
}
l_return_status_t l_send (guchar *send_buffer, guint send_buffer_size)
{
guchar c;
guint i, j;
guchar checksum;
/**********************************************************************/
/* sb: A pointer to the buffer that we will send. */
/* sbs: Its size. */
/**********************************************************************/
guchar *sb;
guint sbs;
for (;;) {
/****************/
/* Write ENQ. */
/****************/
if (gpio_write (device, "\5", 1) == GPIO_ERROR)
return (L_IO_ERROR);
if (l_debug_flag) printf ("Sent:\n 5\n");
/****************/
/* Read. */
/****************/
if (gpio_read (device, &c, 1) < 1) return (L_IO_ERROR);
if (l_debug_flag) printf ("Received:\n%4i\n", c);
switch (c) {
case ACK:
/****************************************/
/* ACK received. We can proceed. */
/****************************************/
break;
case NACK:
/****************************************/
/* NACK received. We'll start from the */
/* beginning. */
/****************************************/
i--;
break;
case ENQ:
/******************************************************/
/* ENQ received. It seems that the camera would like */
/* to send us data, but we do not want it and */
/* therefore simply reject it. The camera will try */
/* two more times with ENQ to get us to receive data */
/* before finally giving up and sending us ACK. */
/******************************************************/
/****************/
/* Write NACK. */
/****************/
c = NACK;
if (gpio_write (device, &c, 1) == GPIO_ERROR)
return (L_IO_ERROR);
for (;;) {
if (gpio_read (device, &c, 1) < 1)
return (L_IO_ERROR);
switch (c) {
case ENQ:
/**************************************/
/* The camera has not yet given up. */
/**************************************/
break;
case ACK:
/**************************************/
/* ACK received. We can proceed. */
/**************************************/
break;
default:
/**************************************/
/* This should not happen. */
/**************************************/
return (L_TRANSMISSION_ERROR);
}
if (c == ACK) break;
}
break;
default:
/******************************************************/
/* The camera seems to send us data. We'll */
/* simply dump it. */
/******************************************************/
break;
}
if (c == ACK) break;
}
/********************************************************/
/* We will write: */
/* - STX */
/* - low order byte of (send_buffer_size + 5) */
/* - high order byte of (send_buffer_size + 5) */
/* - 'send_buffer_size' bytes data plus ESC quotes */
/* - ETX */
/* - 1 byte for checksum plus ESC quotes */
/* */
/* The checksum covers all bytes after STX and before */
/* the checksum byte(s). */
/********************************************************/
sbs = send_buffer_size + 5;
sb = g_new (guchar, sbs);
sb[0] = STX;
sb[1] = send_buffer_size;
sb[2] = send_buffer_size >> 8;
checksum = sb[1];
checksum += sb[2];
for (i = 3; i < (sbs - 2); i++) {
checksum += *send_buffer;
if ( (*send_buffer == STX ) ||
(*send_buffer == ETX ) ||
(*send_buffer == ENQ ) ||
(*send_buffer == ACK ) ||
(*send_buffer == XOFF) ||
(*send_buffer == XON ) ||
(*send_buffer == NACK) ||
(*send_buffer == ETB ) ||
(*send_buffer == ESC )) {
sb = g_renew (guchar, sb, ++sbs);
sb[ i] = ESC;
sb[++i] = ~*send_buffer;
} else sb[ i] = *send_buffer;
send_buffer++;
}
sb[sbs - 2] = ETX;
checksum += ETX;
if ( (checksum == STX ) ||
(checksum == ETX ) ||
(checksum == ENQ ) ||
(checksum == ACK ) ||
(checksum == XOFF) ||
(checksum == XON ) ||
(checksum == NACK) ||
(checksum == ETB ) ||
(checksum == ESC )) {
sb = g_renew (guchar, sb, ++sbs);
sb[sbs - 2] = ESC;
sb[sbs - 1] = ~checksum;
} else sb[sbs - 1] = checksum;
for (i = 0; ; i++) {
/************************/
/* Write data as above. */
/************************/
if (gpio_write (device, sb, sbs) == GPIO_ERROR) {
g_free (sb);
return (L_IO_ERROR);
}
if (l_debug_flag) {
printf ("Sent:\n");
for (j = 0; j < sbs; j++) printf ("%4i", sb[j]);
printf ("\n");
}
if (gpio_read (device, &c, 1) < 1) {
g_free (sb);
return L_IO_ERROR;
}
if (l_debug_flag) printf ("Received:\n%4i\n", c);
switch (c) {
case ACK:
/******************************************************/
/* ACK received. We can proceed. */
/******************************************************/
g_free (sb);
/****************/
/* Write EOT. */
/****************/
c = EOT;
if (gpio_write (device, &c, 1) == GPIO_ERROR)
return (L_IO_ERROR);
if (l_debug_flag) printf ("Sent:\n 4\n");
return (L_SUCCESS);
case NACK:
/******************************************************/
/* NACK received. We'll try up to three times. */
/******************************************************/
if (i == 2) {
g_free (sb);
return (L_TRANSMISSION_ERROR);
} else break;
default:
/******************************************************/
/* Should not happen. */
/******************************************************/
return (L_TRANSMISSION_ERROR);
}
}
}
l_return_status_t l_receive (guchar **rb, guint *rbs, guint timeout)
{
guchar c, d;
gboolean error_flag;
guint i, j, rbs_internal;
unsigned char checksum;
l_return_status_t return_status;
for (i = 0; ; ) {
gpio_set_timeout (device, timeout);
if (gpio_read (device, &c, 1) < 1) return (L_IO_ERROR);
if (l_debug_flag) printf ("Received:\n%4i\n", c);
gpio_set_timeout (device, DEFAULT_TIMEOUT);
switch (c) {
case ENQ:
/******************************************************/
/* ENQ received. We can proceed. */
/******************************************************/
break;
case ACK:
/******************************************************/
/* ACK received. We'll try again at most ten times. */
/******************************************************/
if (i == 9) {
/**********************************************/
/* The camera hangs! Although it could be */
/* that the camera accepts one of the */
/* commands */
/* - KONICA_CANCEL, */
/* - KONICA_GET_IO_CAPABILITY, and */
/* - KONICA_SET_IO_CAPABILITY, */
/* we can not get the camera back to working */
/* correctly. */
/**********************************************/
return (L_TRANSMISSION_ERROR);
}
i++;
break;
default:
/******************************************************/
/* We'll dump this data until we get ENQ (then, we'll */
/* proceed) or nothing any more (then, we'll report */
/* error). */
/******************************************************/
for (;;) {
if (gpio_read (device, &c, 1) < 0)
return (L_TRANSMISSION_ERROR);
if (l_debug_flag)
printf ("Received:\n%4i\n", c);
if (c == ENQ) break;
}
break;
}
if (c == ENQ) break;
}
/**************/
/* Write ACK. */
/**************/
if (gpio_write (device, "\6", 1) == GPIO_ERROR)
return (L_IO_ERROR);
if (l_debug_flag) printf ("Sent:\n 6\n");
for (*rbs = 0; ; ) {
for (j = 0; ; j++) {
if (gpio_read (device, &c, 1) < 1) return (L_IO_ERROR);
if (l_debug_flag) printf ("Received:\n%4i", c);
switch (c) {
case STX:
/**********************************************/
/* STX received. We can proceed. */
/**********************************************/
break;
default:
/**********************************************/
/* We'll dump this data and try again. */
/**********************************************/
continue;
}
/******************************************************/
/* Read 2 bytes for size (low order byte, high order */
/* byte) plus ESC quotes. */
/******************************************************/
return_status = l_esc_read (&c);
if (return_status != L_SUCCESS) return (return_status);
checksum = c;
return_status = l_esc_read (&d);
if (return_status != L_SUCCESS) return (return_status);
checksum += d;
rbs_internal = (d << 8) | c;
if (*rbs == 0) *rb = g_new (guchar, rbs_internal);
else *rb = g_renew (guchar, *rb, *rbs + rbs_internal);
/******************************************************/
/* Read 'rbs_internal' bytes data plus ESC quotes. */
/******************************************************/
error_flag = FALSE;
for (i = 0; i < rbs_internal; i++) {
return_status = l_esc_read (&((*rb)[*rbs + i]));
switch (return_status) {
case L_IO_ERROR:
return (L_IO_ERROR);
case L_TRANSMISSION_ERROR:
/**************************************/
/* We already received ETX or ETB. */
/* Later, we'll read the checksum */
/* plus ESC quotes and reject the */
/* packet. */
/**************************************/
error_flag = TRUE;
break;
case L_SUCCESS:
/**************************************/
/* We can proceed. */
/**************************************/
checksum += (*rb)[*rbs + i];
break;
}
if (error_flag) break;
}
if (!error_flag) {
if (gpio_read (device, &d, 1) < 1)
return (L_IO_ERROR);
if (l_debug_flag) printf ("%4i", c);
switch (d) {
case ETX:
/**************************************/
/* ETX received. This is the last */
/* packet. */
/**************************************/
break;
case ETB:
/**************************************/
/* ETB received. There are more */
/* packets to come. */
/**************************************/
break;
default:
/**************************************/
/* We get more bytes than expected. */
/* Nothing serious, as we will simply */
/* dump all bytes until we receive */
/* ETX or ETB. Later, we'll read the */
/* checksum with ESC quotes and */
/* reject the packet. */
/**************************************/
while ((d != ETX) && (d != ETB)) {
if (gpio_read (
device, &d, 1) < 1)
return (L_IO_ERROR);
if (l_debug_flag)
printf ("%4i", c);
}
error_flag = TRUE;
break;
}
}
checksum += d;
/******************************************************/
/* Read 1 byte for checksum plus ESC quotes. */
/******************************************************/
return_status = l_esc_read (&c);
if (l_debug_flag) printf ("\n");
if (return_status != L_SUCCESS) return (return_status);
if ((c == checksum) && (!error_flag)) {
*rbs += rbs_internal;
/****************/
/* Write ACK. */
/****************/
if (gpio_write (device, "\6", 1) == GPIO_ERROR)
return (L_IO_ERROR);
if (l_debug_flag) printf ("Sent:\n 6\n");
break;
} else {
/**********************************************/
/* Checksum wrong or transmission error. The */
/* camera will send us the data at the most */
/* three times. */
/**********************************************/
if (j == 2) return (L_TRANSMISSION_ERROR);
/****************/
/* Write NACK. */
/****************/
c = NACK;
if (gpio_write (device, &c, 1) == GPIO_ERROR)
return (L_IO_ERROR);
if (l_debug_flag) printf ("Sent:\n 15\n");
continue;
}
}
if (gpio_read (device, &c, 1) < 1) return (L_IO_ERROR);
if (l_debug_flag) printf ("Received:\n%4i\n", c);
switch (c) {
case EOT:
/**********************************************/
/* EOT received. We can proceed. */
/**********************************************/
break;
default:
/**********************************************/
/* Should not happen. */
/**********************************************/
return (L_TRANSMISSION_ERROR);
}
/**************************************************************/
/* Depending on d, we will either continue to receive data or */
/* stop. */
/* - ETX: We are all done. */
/* - ETB: We expect more data. */
/* - else: Should not happen. */
/**************************************************************/
switch (d) {
case ETX:
/******************************************************/
/* We are all done. */
/******************************************************/
return (L_SUCCESS);
case ETB:
/******************************************************/
/* We expect more data. */
/******************************************************/
/****************/
/* Read ENQ. */
/****************/
if (gpio_read (device, &c, 1) < 1) return (L_IO_ERROR);
if (l_debug_flag) printf ("Received:\n%4i\n", c);
switch (c) {
case ENQ:
/**********************************************/
/* ENQ received. We can proceed. */
/**********************************************/
break;
default:
/**********************************************/
/* Should not happen. */
/**********************************************/
return (L_TRANSMISSION_ERROR);
}
/****************/
/* Write ACK. */
/****************/
if (gpio_write (device, "\6", 1) == GPIO_ERROR)
return (L_IO_ERROR);
if (l_debug_flag) printf ("Sent:\n 6\n");
break;
default:
/******************************************************/
/* Should not happen. */
/******************************************************/
return (L_TRANSMISSION_ERROR);
}
}
}
l_return_status_t l_send_receive (
guchar *send_buffer,
guint send_buffer_size,
guchar **receive_buffer,
guint *receive_buffer_size)
{
/****************/
/* Send data. */
/****************/
l_return_status_t return_status = l_send (
send_buffer,
send_buffer_size);
if (return_status != L_SUCCESS) return (return_status);
/********************************/
/* Receive control data. */
/********************************/
return_status = l_receive (receive_buffer, receive_buffer_size, 2000);
if (return_status != L_SUCCESS) return (return_status);
if (((*receive_buffer)[0] != send_buffer[0]) ||
((*receive_buffer)[1] != send_buffer[1])) {
printf ("Error: Commands differ! %i %i != %i %i.\n",
(*receive_buffer)[0], (*receive_buffer)[1],
send_buffer[0], send_buffer[1]);
return (L_TRANSMISSION_ERROR);
}
else return (return_status);
}
l_return_status_t l_send_receive_receive (
guchar *send_buffer,
guint send_buffer_size,
guchar **image_buffer,
guint *image_buffer_size,
guchar **receive_buffer,
guint *receive_buffer_size,
guint timeout)
{
/****************/
/* Send data. */
/****************/
l_return_status_t return_status = l_send (
send_buffer,
send_buffer_size);
if (return_status != L_SUCCESS) return (return_status);
/************************/
/* Receive data. */
/************************/
return_status = l_receive (image_buffer, image_buffer_size, timeout);
if (return_status != L_SUCCESS) return (return_status);
/********************************/
/* Receive control data. */
/********************************/
return_status = l_receive (
receive_buffer,
receive_buffer_size,
DEFAULT_TIMEOUT);
if (return_status != L_SUCCESS) return (return_status);
if (((*receive_buffer)[0] != send_buffer[0]) ||
((*receive_buffer)[1] != send_buffer[1])) {
printf ("Error: Commands differ! %i %i != %i %i.\n",
(*receive_buffer)[0], (*receive_buffer)[1],
send_buffer[0], send_buffer[1]);
return (L_TRANSMISSION_ERROR);
}
return (return_status);
}
|