| 1 |
/* library.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 |
#define _DARWIN_C_SOURCE |
| 33 |
#include "config.h" |
| 34 |
#include "library.h" |
| 35 |
|
| 36 |
#include <stdlib.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#include <stdio.h> |
| 40 |
#include <fcntl.h> |
| 41 |
#include <string.h> |
| 42 |
#include <errno.h> |
| 43 |
|
| 44 |
#include "libgphoto2/i18n.h" |
| 45 |
|
| 46 |
#include "dc3200.h" |
| 47 |
|
| 48 |
|
| 49 |
/* |
| 50 |
* FIXME: Use properly sized integer types. The dc3200 code used lots |
| 51 |
* of u_char, u_long, u_int, which may be wrong on non-32bit systems. |
| 52 |
*/ |
| 53 |
|
| 54 |
/* #define DEBUG */ |
| 55 |
|
| 56 |
/* |
| 57 |
* dc3200_set_speed |
| 58 |
* |
| 59 |
* handshakes a baud rate with the camera |
| 60 |
* |
| 61 |
*/ |
| 62 |
int dc3200_set_speed(Camera *camera, int baudrate) |
| 63 |
{ |
| 64 |
int baudcode = C9600; |
| 65 |
unsigned char resp[INI_PACKET_LEN], msg[INI_PACKET_LEN]; |
| 66 |
int resp_len = INI_PACKET_LEN, msg_len = INI_PACKET_LEN; |
| 67 |
|
| 68 |
msg[0] = 0xAF; |
| 69 |
msg[1] = 0x00; |
| 70 |
|
| 71 |
switch(baudrate) { |
| 72 |
case 9600: |
| 73 |
baudcode = C9600; |
| 74 |
break; |
| 75 |
case 19200: |
| 76 |
baudcode = C19200; |
| 77 |
break; |
| 78 |
case 38400: |
| 79 |
baudcode = C38400; |
| 80 |
break; |
| 81 |
case 57600: |
| 82 |
baudcode = C57600; |
| 83 |
break; |
| 84 |
case 115200: |
| 85 |
baudcode = C115200; |
| 86 |
break; |
| 87 |
default: |
| 88 |
printf("unsupported baudrate: %d.\n", baudrate); |
| 89 |
return GP_ERROR; |
| 90 |
} |
| 91 |
|
| 92 |
msg[2] = baudcode; |
| 93 |
|
| 94 |
/* send our preferred baud rate */ |
| 95 |
if(dc3200_send_command(camera, msg, msg_len, resp, &resp_len) == GP_ERROR) |
| 96 |
return GP_ERROR; |
| 97 |
/* the camera responds with our preferred baud rate or its |
| 98 |
* maximum baud rate, whichever is lowest |
| 99 |
*/ |
| 100 |
|
| 101 |
/* compile a confirmation packet */ |
| 102 |
msg[0] = 0x9F; |
| 103 |
msg[1] = 0x00; |
| 104 |
if(resp_len == 2) { |
| 105 |
msg_len = 2; |
| 106 |
} else { |
| 107 |
/* calculate reply baud handshake */ |
| 108 |
msg[2] = (resp[2] + 1) / 2; |
| 109 |
msg_len = INI_PACKET_LEN; |
| 110 |
} |
| 111 |
|
| 112 |
if(dc3200_send_command(camera, msg, msg_len, resp, &resp_len) == GP_ERROR) |
| 113 |
return GP_ERROR; |
| 114 |
|
| 115 |
if(resp_len == 2) { |
| 116 |
/* set the handshaked baudrate |
| 117 |
* supports only 9600 |
| 118 |
*/ |
| 119 |
return C9600; |
| 120 |
} else if(resp[1] == 0x01) { |
| 121 |
/* set the handshaked baudrate */ |
| 122 |
return (resp[2]*2) - 1; |
| 123 |
} else { |
| 124 |
/* unknown baud */ |
| 125 |
return GP_ERROR; |
| 126 |
} |
| 127 |
|
| 128 |
return GP_ERROR; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* dc3200_setup |
| 133 |
* |
| 134 |
* sends two commands essential for survival |
| 135 |
* |
| 136 |
*/ |
| 137 |
int dc3200_setup(Camera *camera) |
| 138 |
{ |
| 139 |
/* |
| 140 |
* some strange commands that must be |
| 141 |
* completed after initialization and |
| 142 |
* before any camera access |
| 143 |
* |
| 144 |
* version info? ... |
| 145 |
* |
| 146 |
*/ |
| 147 |
|
| 148 |
unsigned char cmd1[5] = {0x01, 0, 0x01, 0x00, 0x0F}; |
| 149 |
unsigned char cmd2[8] = {0x01, 0, 0x80, 0x00, 0x01, 0x81, 0x00, 0x03}; |
| 150 |
|
| 151 |
unsigned char ack[ACK_PACKET_LEN], resp[DEF_PACKET_LEN]; |
| 152 |
int ack_len = ACK_PACKET_LEN, resp_len = DEF_PACKET_LEN; |
| 153 |
|
| 154 |
cmd1[1] = dc3200_calc_seqnum(camera); |
| 155 |
cmd2[1] = dc3200_calc_seqnum(camera); |
| 156 |
|
| 157 |
if(dc3200_send_command(camera, cmd1, sizeof(cmd1), ack, &ack_len) == GP_ERROR) |
| 158 |
return GP_ERROR; |
| 159 |
if(dc3200_check_ack(camera, ack, ack_len) == GP_ERROR) |
| 160 |
return GP_ERROR; |
| 161 |
if(dc3200_recv_response(camera, resp, &resp_len) == GP_ERROR) |
| 162 |
return GP_ERROR; |
| 163 |
if(dc3200_send_ack(camera, resp[1]) == GP_ERROR) |
| 164 |
return GP_ERROR; |
| 165 |
|
| 166 |
if(dc3200_send_command(camera, cmd2, sizeof(cmd2), ack, &ack_len) == GP_ERROR) |
| 167 |
return GP_ERROR; |
| 168 |
if(dc3200_check_ack(camera, ack, ack_len) == GP_ERROR) |
| 169 |
return GP_ERROR; |
| 170 |
if(dc3200_recv_response(camera, resp, &resp_len) == GP_ERROR) |
| 171 |
return GP_ERROR; |
| 172 |
if(dc3200_send_ack(camera, resp[1]) == GP_ERROR) |
| 173 |
return GP_ERROR; |
| 174 |
|
| 175 |
/* set the cmd sequence number */ |
| 176 |
camera->pl->cmd_seqnum = 0; |
| 177 |
camera->pl->rec_seqnum = 0; |
| 178 |
|
| 179 |
return GP_OK; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
/* |
| 184 |
* dc3200_get_data |
| 185 |
* |
| 186 |
* retrieves data from the camera as either file |
| 187 |
* data or file/folder list data |
| 188 |
* |
| 189 |
*/ |
| 190 |
|
| 191 |
/* command to retrieve directory listing |
| 192 |
unsigned char list_pkt[DEF_PACKET_LEN] = {0x01, dc3200_calc_seqnum(camera), 0x80, 0x00, 0x20, 0x03, 26 + strlen(filename), |
| 193 |
0xc1, 0x50, 0x00, 0x00, 0x00, 0x00, 19 + strlen(filename), |
| 194 |
0x04, 0x01, 0x00, 0x01, 0x00, 0x01, 0x60, 0x01, 0x00, 0x01, 0x00, 0x05, |
| 195 |
0x00, 0x00, 0x00, 2 + strlen(filename), 0x01}; |
| 196 |
*/ |
| 197 |
/* command to get the picture |
| 198 |
unsigned char get_pkt[DEF_PACKET_LEN] = {0x01, calc_seqnum(), 0x80, 0x00, 0x20, 0x03, 26 + strlen(filename), |
| 199 |
0xC1, 0x50, 0x00, 0x00, 0x00, 0x00, 19 + strlen(filename), |
| 200 |
0x04, 0x01, 0x00, 0x01, 0x01, 0x04, 0x60, 0x10, 0x00, 0x01, 0x00, 0x05, |
| 201 |
0x00, 0x00, 0x00, 2 + strlen(filename), 0x01}; |
| 202 |
*/ |
| 203 |
|
| 204 |
int dc3200_get_data(Camera *camera, unsigned char **data, unsigned long *data_len, int command, const char *folder, const char *filename) |
| 205 |
{ |
| 206 |
unsigned char ack[ACK_PACKET_LEN], resp[DEF_PACKET_LEN]; |
| 207 |
int ack_len = ACK_PACKET_LEN, resp_len = DEF_PACKET_LEN; |
| 208 |
unsigned long num_left = 0; |
| 209 |
int data_start_pos = 0; |
| 210 |
unsigned char *ptr_data = NULL; |
| 211 |
unsigned int pid = 0; |
| 212 |
|
| 213 |
/* command bytes */ |
| 214 |
unsigned char cb1=0x00,cb2=0x00,cb3=0x00; |
| 215 |
|
| 216 |
/* packet parts */ |
| 217 |
unsigned char pkt_hdr[7]; |
| 218 |
unsigned char pkt_typ[7]; |
| 219 |
unsigned char pkt_cmd[12]; |
| 220 |
unsigned char pkt_len[5]; |
| 221 |
|
| 222 |
unsigned char *packet; |
| 223 |
int packet_len; |
| 224 |
char *file = NULL; |
| 225 |
char *ptr = NULL; |
| 226 |
|
| 227 |
if(folder) { |
| 228 |
if(filename) { |
| 229 |
/* concatenate the folder + filename */ |
| 230 |
file = aprintf("%s%s%s", folder, folder[strlen(folder)-1] != '\\' ? "\\" : "", filename); |
| 231 |
} else { |
| 232 |
file = strdup(folder); |
| 233 |
} |
| 234 |
if(!file) |
| 235 |
return GP_ERROR; |
| 236 |
} else { |
| 237 |
return GP_ERROR; |
| 238 |
} |
| 239 |
|
| 240 |
/* change all /'s to \'s */ |
| 241 |
while((ptr = strchr(file, '/'))) { |
| 242 |
ptr[0] = '\\'; |
| 243 |
} |
| 244 |
|
| 245 |
/* there should not be a trailing '\', except for root folder */ |
| 246 |
if(strlen(file) > 1 && file[strlen(file)-1] == '\\') |
| 247 |
file[strlen(file)-1] = 0; |
| 248 |
|
| 249 |
switch (command) { |
| 250 |
case CMD_GET_PREVIEW: |
| 251 |
cb1 = 0x02; cb2 = 0x70; cb3 = 0x11; |
| 252 |
break; |
| 253 |
case CMD_GET_FILE: |
| 254 |
cb1 = 0x01; cb2 = 0x60; cb3 = 0x10; |
| 255 |
break; |
| 256 |
case CMD_LIST_FILES: |
| 257 |
cb1 = 0x01; cb2 = 0x60; cb3 = 0x01; |
| 258 |
break; |
| 259 |
} |
| 260 |
|
| 261 |
camera->pl->cmd_seqnum++; |
| 262 |
|
| 263 |
pkt_hdr[0] = 0x01; |
| 264 |
pkt_hdr[1] = dc3200_calc_seqnum(camera); |
| 265 |
pkt_hdr[2] = 0x80; pkt_hdr[3] = 0x00; pkt_hdr[4] = 0x20; pkt_hdr[5] = 0x03; |
| 266 |
pkt_hdr[6] = 26 + strlen(file); |
| 267 |
|
| 268 |
pkt_typ[0] = 0xC1; |
| 269 |
pkt_typ[1] = 0x50; |
| 270 |
pkt_typ[2] = 0x00; pkt_typ[3] = 0x00; pkt_typ[4] = 0x00; pkt_typ[5] = 0x00; |
| 271 |
pkt_typ[6] = 19 + strlen(file); |
| 272 |
|
| 273 |
pkt_cmd[0] = 0x04; pkt_cmd[1] = cb1; /* cmd */ |
| 274 |
pkt_cmd[2] = 0x00; pkt_cmd[3] = 0x01; /* ??? */ |
| 275 |
pkt_cmd[4] = (camera->pl->cmd_seqnum >> 8) & 0xff; pkt_cmd[5] = camera->pl->cmd_seqnum & 0xff; /* SEQ */ |
| 276 |
pkt_cmd[6] = cb2; pkt_cmd[7] = cb3; /* cmd */ |
| 277 |
pkt_cmd[8] = 0x00; pkt_cmd[9] = 0x01; /* ??? */ |
| 278 |
pkt_cmd[10] = 0x00; pkt_cmd[11] = 0x05; /* ??? */ |
| 279 |
|
| 280 |
pkt_len[0] = 0x00; pkt_len[1] = 0x00; pkt_len[2] = 0x00; |
| 281 |
pkt_len[3] = strlen(file) + 2; |
| 282 |
pkt_len[4] = 0x01; |
| 283 |
|
| 284 |
packet_len = sizeof(pkt_hdr) + sizeof(pkt_typ) + sizeof(pkt_cmd) + sizeof(pkt_len) + strlen(file) + 2; |
| 285 |
packet = malloc(packet_len); |
| 286 |
if(!packet) { |
| 287 |
free(file); |
| 288 |
return GP_ERROR; |
| 289 |
} |
| 290 |
|
| 291 |
/* clear packet */ |
| 292 |
memset(packet, 0, packet_len); |
| 293 |
|
| 294 |
/* copy pkt parts into packet */ |
| 295 |
memcpy(packet, pkt_hdr, sizeof(pkt_hdr)); |
| 296 |
memcpy(packet + sizeof(pkt_hdr), pkt_typ, sizeof(pkt_typ)); |
| 297 |
memcpy(packet + sizeof(pkt_hdr) + sizeof(pkt_typ), pkt_cmd, sizeof(pkt_cmd)); |
| 298 |
memcpy(packet + sizeof(pkt_hdr) + sizeof(pkt_typ) + sizeof(pkt_cmd), pkt_len, sizeof(pkt_len)); |
| 299 |
memcpy(packet + sizeof(pkt_hdr) + sizeof(pkt_typ) + sizeof(pkt_cmd) + sizeof(pkt_len), file, strlen(file)); |
| 300 |
|
| 301 |
free(file); |
| 302 |
|
| 303 |
/* send the command to list files */ |
| 304 |
if(dc3200_send_command(camera, packet, packet_len, ack, &ack_len) == GP_ERROR){ |
| 305 |
free(packet); |
| 306 |
return GP_ERROR; |
| 307 |
} |
| 308 |
|
| 309 |
free(packet); |
| 310 |
|
| 311 |
do { |
| 312 |
resp_len = DEF_PACKET_LEN; |
| 313 |
if(dc3200_recv_response(camera, resp, &resp_len) == GP_ERROR) { |
| 314 |
return GP_ERROR; |
| 315 |
} |
| 316 |
|
| 317 |
if(dc3200_send_ack(camera, resp[1]) == GP_ERROR) { |
| 318 |
return GP_ERROR; |
| 319 |
} |
| 320 |
|
| 321 |
if(resp[1] == camera->pl->rec_seqnum) |
| 322 |
continue; |
| 323 |
|
| 324 |
camera->pl->rec_seqnum = resp[1]; |
| 325 |
|
| 326 |
/* |
| 327 |
* we decide what action to take based on |
| 328 |
* the number of packets we are expecting |
| 329 |
* |
| 330 |
* resp[7] holds this info |
| 331 |
* |
| 332 |
*/ |
| 333 |
switch(resp[7]) { |
| 334 |
case 0xC1: |
| 335 |
/* |
| 336 |
* SINGLE PACKET |
| 337 |
*/ |
| 338 |
|
| 339 |
/* get the total list length from the data header */ |
| 340 |
*data_len = bytes_to_l(resp[26], resp[27], resp[28], resp[29]); |
| 341 |
num_left = 0; |
| 342 |
data_start_pos = 31; |
| 343 |
*data_len -= 1; |
| 344 |
|
| 345 |
/* allocate a buffer to store all this data */ |
| 346 |
*data = malloc(*data_len); |
| 347 |
if(*data == NULL) { |
| 348 |
return GP_ERROR; |
| 349 |
} |
| 350 |
|
| 351 |
ptr_data = *data; |
| 352 |
|
| 353 |
/* copy this portion of the data */ |
| 354 |
memcpy(ptr_data, resp + data_start_pos, resp_len - data_start_pos); |
| 355 |
/* incr the buff ptr */ |
| 356 |
ptr_data += resp_len - data_start_pos; |
| 357 |
break; |
| 358 |
|
| 359 |
case 0x41: |
| 360 |
/* |
| 361 |
* MULTIPACKET, START |
| 362 |
*/ |
| 363 |
|
| 364 |
/* get the total list length from the data header */ |
| 365 |
*data_len = bytes_to_l(resp[34], resp[35], resp[36], resp[37]); |
| 366 |
num_left = bytes_to_l(resp[12], resp[13], resp[14], resp[15]); |
| 367 |
data_start_pos = 39; |
| 368 |
*data_len -= 1; |
| 369 |
|
| 370 |
/* allocate a buffer to store all this data */ |
| 371 |
*data = malloc(*data_len); |
| 372 |
if(*data == NULL) { |
| 373 |
return GP_ERROR; |
| 374 |
} |
| 375 |
|
| 376 |
ptr_data = *data; |
| 377 |
|
| 378 |
/* copy this portion of the data */ |
| 379 |
memcpy(ptr_data, resp + data_start_pos, resp_len - data_start_pos); |
| 380 |
/* incr the data ptr */ |
| 381 |
ptr_data += resp_len - data_start_pos; |
| 382 |
|
| 383 |
/* update gphoto2 frontend */ |
| 384 |
if(command == CMD_GET_FILE || command == CMD_GET_PREVIEW) |
| 385 |
{ |
| 386 |
pid = gp_context_progress_start(camera->pl->context,(int)*data_len,"%s", filename); |
| 387 |
gp_context_progress_update(camera->pl->context, pid, ptr_data - *data); |
| 388 |
if(gp_context_cancel(camera->pl->context) == GP_CONTEXT_FEEDBACK_CANCEL) { |
| 389 |
free(*data); |
| 390 |
dc3200_cancel_get_data(camera); |
| 391 |
return GP_ERROR_CANCEL; |
| 392 |
} |
| 393 |
} |
| 394 |
break; |
| 395 |
case 0x01: |
| 396 |
/* |
| 397 |
* MULTIPACKET, MIDDLE |
| 398 |
*/ |
| 399 |
/* calculate the number of packets remaining */ |
| 400 |
num_left = bytes_to_l(resp[12], resp[13], resp[14], resp[15]); |
| 401 |
data_start_pos = 16; |
| 402 |
|
| 403 |
if(*data == NULL) { |
| 404 |
return GP_ERROR; |
| 405 |
} |
| 406 |
|
| 407 |
/* copy this portion of the data */ |
| 408 |
memcpy(ptr_data, resp + data_start_pos, resp_len - data_start_pos); |
| 409 |
/* incr the buff ptr */ |
| 410 |
ptr_data += resp_len - data_start_pos; |
| 411 |
|
| 412 |
/* update gphoto2 frontend */ |
| 413 |
if(command == CMD_GET_FILE || command == CMD_GET_PREVIEW) |
| 414 |
{ |
| 415 |
gp_context_progress_update(camera->pl->context, pid, ptr_data - *data); |
| 416 |
if(gp_context_cancel(camera->pl->context) == GP_CONTEXT_FEEDBACK_CANCEL) { |
| 417 |
free(*data); |
| 418 |
dc3200_cancel_get_data(camera); |
| 419 |
return GP_ERROR_CANCEL; |
| 420 |
} |
| 421 |
} |
| 422 |
break; |
| 423 |
case 0x81: |
| 424 |
/* |
| 425 |
* MULTIPACKET, END |
| 426 |
*/ |
| 427 |
/* calculate the number of packets remaining */ |
| 428 |
num_left = bytes_to_l(resp[12], resp[13], resp[14], resp[15]); |
| 429 |
data_start_pos = 16; |
| 430 |
|
| 431 |
if(*data == NULL) { |
| 432 |
return GP_ERROR; |
| 433 |
} |
| 434 |
|
| 435 |
/* copy this portion of the data */ |
| 436 |
memcpy(ptr_data, resp + data_start_pos, resp_len - data_start_pos); |
| 437 |
/* incr the buff ptr */ |
| 438 |
ptr_data += resp_len - data_start_pos; |
| 439 |
|
| 440 |
/* update gphoto2 frontend */ |
| 441 |
if(command == CMD_GET_FILE || command == CMD_GET_PREVIEW) |
| 442 |
{ |
| 443 |
gp_context_progress_update(camera->pl->context, pid, ptr_data - *data); |
| 444 |
if(gp_context_cancel(camera->pl->context) == GP_CONTEXT_FEEDBACK_CANCEL) { |
| 445 |
free(*data); |
| 446 |
dc3200_cancel_get_data(camera); |
| 447 |
return GP_ERROR_CANCEL; |
| 448 |
} |
| 449 |
} |
| 450 |
break; |
| 451 |
default: |
| 452 |
return GP_ERROR; |
| 453 |
} |
| 454 |
} while(num_left > 1); |
| 455 |
|
| 456 |
if(pid != 0 && (command == CMD_GET_FILE || command == CMD_GET_PREVIEW)) |
| 457 |
gp_context_progress_stop(camera->pl->context, pid); |
| 458 |
|
| 459 |
return GP_OK; |
| 460 |
} |
| 461 |
|
| 462 |
/* |
| 463 |
* dc3200_cancel_get_data |
| 464 |
* |
| 465 |
* stops a get data command |
| 466 |
* |
| 467 |
*/ |
| 468 |
int dc3200_cancel_get_data(Camera *camera) |
| 469 |
{ |
| 470 |
unsigned char pkt[20] = {0x01, 0, 0x80, 0x00, 0x20, 0x03, 0x0d, 0xc1, 0x50, 0xc0, |
| 471 |
0x00, 0x00, 0x00, 0x06, 0x04, 0x01, 0x00, 0x01, 0, 0}; |
| 472 |
unsigned char ack[ACK_PACKET_LEN], resp[DEF_PACKET_LEN]; |
| 473 |
int ack_len = ACK_PACKET_LEN, resp_len = DEF_PACKET_LEN; |
| 474 |
|
| 475 |
pkt[1] = dc3200_calc_seqnum(camera); |
| 476 |
pkt[18] = (camera->pl->cmd_seqnum >> 8) & 0xff; |
| 477 |
pkt[19] = camera->pl->cmd_seqnum & 0xff; |
| 478 |
|
| 479 |
/* wait a bit ... */ |
| 480 |
sleep(1); |
| 481 |
|
| 482 |
/* clear the buffer */ |
| 483 |
dc3200_clear_read_buffer(camera); |
| 484 |
|
| 485 |
if(dc3200_send_command(camera, (unsigned char *)pkt, 20, ack, &ack_len) == GP_ERROR) |
| 486 |
return GP_ERROR; |
| 487 |
|
| 488 |
if(dc3200_recv_response(camera, resp, &resp_len) == GP_ERROR) |
| 489 |
return GP_ERROR; |
| 490 |
else |
| 491 |
dc3200_send_ack(camera, resp[1]); |
| 492 |
|
| 493 |
resp_len = DEF_PACKET_LEN; |
| 494 |
if(dc3200_recv_response(camera, resp, &resp_len) == GP_ERROR) |
| 495 |
return GP_ERROR; |
| 496 |
else |
| 497 |
dc3200_send_ack(camera, resp[1]); |
| 498 |
|
| 499 |
return GP_OK; |
| 500 |
} |
| 501 |
|
| 502 |
/* |
| 503 |
* dc3200_send_command |
| 504 |
* |
| 505 |
* if cmd == NULL || cmd_len == 0, send_command waits for a response |
| 506 |
* otherwise, it sends a command and waits for an acknowledgement |
| 507 |
* |
| 508 |
*/ |
| 509 |
int dc3200_send_command(Camera *camera, unsigned char *cmd, int cmd_len, unsigned char *ack, int *ack_len) |
| 510 |
{ |
| 511 |
int sends, reads; |
| 512 |
int buff_len = *ack_len; |
| 513 |
int received = 0; |
| 514 |
unsigned char *buff = NULL; |
| 515 |
|
| 516 |
buff = malloc(sizeof(unsigned char) * *ack_len); |
| 517 |
if(buff == NULL) { |
| 518 |
return GP_ERROR; |
| 519 |
} |
| 520 |
|
| 521 |
sends = SEND_RETRIES; |
| 522 |
|
| 523 |
while(sends > 0) { |
| 524 |
reads = READ_RETRIES; |
| 525 |
/* check that we are actually sending a command |
| 526 |
* and not just waiting for data |
| 527 |
*/ |
| 528 |
if(cmd != NULL && cmd_len > 0) { |
| 529 |
/* clear the read buffer */ |
| 530 |
dc3200_clear_read_buffer(camera); |
| 531 |
|
| 532 |
/* send the command */ |
| 533 |
dc3200_send_packet(camera, cmd, cmd_len); |
| 534 |
} |
| 535 |
|
| 536 |
/* wait for response */ |
| 537 |
while(reads > 0) { |
| 538 |
if(dc3200_recv_packet(camera, buff, &buff_len) != GP_ERROR) { |
| 539 |
received = 1; |
| 540 |
break; |
| 541 |
} |
| 542 |
reads--; |
| 543 |
} |
| 544 |
|
| 545 |
if(received) { |
| 546 |
if(buff_len > *ack_len) { |
| 547 |
/* buffer size too small */ |
| 548 |
free(buff); |
| 549 |
return GP_ERROR; |
| 550 |
} |
| 551 |
|
| 552 |
/* sukksess */ |
| 553 |
*ack_len = buff_len; |
| 554 |
memcpy(ack, buff, buff_len); |
| 555 |
free(buff); |
| 556 |
return GP_OK; |
| 557 |
} else { |
| 558 |
sends--; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
/* send failed */ |
| 563 |
free(buff); |
| 564 |
return GP_ERROR; |
| 565 |
} |
| 566 |
|
| 567 |
/* |
| 568 |
* dc3200_recv_response |
| 569 |
* |
| 570 |
* receives a response from a previous command |
| 571 |
* |
| 572 |
*/ |
| 573 |
int dc3200_recv_response(Camera *camera, unsigned char *resp, int *resp_len) |
| 574 |
{ |
| 575 |
return dc3200_send_command(camera, NULL, 0, resp, resp_len); |
| 576 |
} |
| 577 |
|
| 578 |
/* |
| 579 |
* dc3200_send_ack |
| 580 |
* |
| 581 |
* sends an ack packet |
| 582 |
* |
| 583 |
*/ |
| 584 |
int dc3200_send_ack(Camera *camera, int seqnum) |
| 585 |
{ |
| 586 |
unsigned char ack[ACK_PACKET_LEN]; |
| 587 |
ack[0] = 0x01; |
| 588 |
ack[1] = seqnum + 16; |
| 589 |
|
| 590 |
return dc3200_send_packet(camera, ack, sizeof(ack)); |
| 591 |
} |
| 592 |
|
| 593 |
/* |
| 594 |
* dc3200_check_ack |
| 595 |
* |
| 596 |
* checks an ack packet (well ok not really) |
| 597 |
* |
| 598 |
*/ |
| 599 |
int dc3200_check_ack(Camera *camera, unsigned char *ack, int ack_len) |
| 600 |
{ |
| 601 |
return GP_OK; |
| 602 |
} |
| 603 |
|
| 604 |
/* |
| 605 |
* dc3200_send_packet |
| 606 |
* |
| 607 |
* compiles a packet from data and sends it |
| 608 |
* |
| 609 |
*/ |
| 610 |
int dc3200_send_packet(Camera *camera, unsigned char *data, int data_len) |
| 611 |
{ |
| 612 |
int res; |
| 613 |
|
| 614 |
int buff_len = data_len; |
| 615 |
unsigned char *buff = NULL; |
| 616 |
|
| 617 |
buff = malloc(sizeof(unsigned char) * buff_len); |
| 618 |
if(!buff) |
| 619 |
return GP_ERROR; |
| 620 |
|
| 621 |
memcpy(buff, data, buff_len); |
| 622 |
|
| 623 |
/* compile this into a packet */ |
| 624 |
res = dc3200_compile_packet(camera, &buff, &buff_len); |
| 625 |
if(res == GP_ERROR) |
| 626 |
return res; |
| 627 |
|
| 628 |
#ifdef DEBUG |
| 629 |
dump_buffer(buff, buff_len, "s", 16); |
| 630 |
#endif |
| 631 |
res = gp_port_write(camera->port, (char *)buff, data_len + 3); |
| 632 |
free(buff); |
| 633 |
return res; |
| 634 |
} |
| 635 |
|
| 636 |
/* |
| 637 |
* dc3200_recv_packet |
| 638 |
* |
| 639 |
* waits for a packet to arrive, checks it, and returns it |
| 640 |
* |
| 641 |
*/ |
| 642 |
int dc3200_recv_packet(Camera *camera, unsigned char *data, int *data_len) |
| 643 |
{ |
| 644 |
int complete = 0; |
| 645 |
int num_read = 0, res = 0, fails = 0; |
| 646 |
|
| 647 |
/* allocate storage for size, checksum, and EOP */ |
| 648 |
unsigned char *buff = NULL; |
| 649 |
|
| 650 |
buff = malloc(sizeof(unsigned char) * (*data_len + 3)); |
| 651 |
if(buff == NULL) |
| 652 |
return GP_ERROR; |
| 653 |
|
| 654 |
memset(buff, 0, *data_len + 3); |
| 655 |
|
| 656 |
/* |
| 657 |
* - read data until we get an 0xFF |
| 658 |
* - process packet |
| 659 |
* |
| 660 |
*/ |
| 661 |
|
| 662 |
res = gp_port_read(camera->port, (char *)&buff[num_read], 1); |
| 663 |
|
| 664 |
while(res >= 0 && fails < READ_RETRIES) { |
| 665 |
if(res == 0) { |
| 666 |
/* read nothing */ |
| 667 |
fails++; |
| 668 |
} else { |
| 669 |
/* reset # of fails */ |
| 670 |
fails = 0; |
| 671 |
num_read++; |
| 672 |
if(buff[num_read-1] == 0xFF) { |
| 673 |
complete = 1; |
| 674 |
break; |
| 675 |
} |
| 676 |
if(num_read == *data_len + 3) { |
| 677 |
/* we've reached the buffer limit */ |
| 678 |
complete = 0; |
| 679 |
break; |
| 680 |
} |
| 681 |
} |
| 682 |
res = gp_port_read(camera->port, (char *)&buff[num_read], 1); |
| 683 |
} |
| 684 |
|
| 685 |
if(!complete) { |
| 686 |
/* packet incomplete */ |
| 687 |
free(buff); |
| 688 |
return GP_ERROR; |
| 689 |
} |
| 690 |
|
| 691 |
#ifdef DEBUG |
| 692 |
dump_buffer(buff, num_read, "r", 16); |
| 693 |
#endif |
| 694 |
|
| 695 |
if(dc3200_process_packet(camera, buff, &num_read) == GP_ERROR) { |
| 696 |
free(buff); |
| 697 |
return GP_ERROR; |
| 698 |
} |
| 699 |
|
| 700 |
/* save the last successful packet received time */ |
| 701 |
time(&(camera->pl->last)); |
| 702 |
|
| 703 |
memcpy(data, buff, *data_len); |
| 704 |
|
| 705 |
/* return with new packet size */ |
| 706 |
*data_len = num_read; |
| 707 |
|
| 708 |
free(buff); |
| 709 |
return GP_OK; |
| 710 |
} |
| 711 |
|
| 712 |
/* |
| 713 |
* dc3200_compile_packet |
| 714 |
* |
| 715 |
* changes 0xFF -> 0xFE 0x01 |
| 716 |
* 0xFE -> 0xFE 0x00 |
| 717 |
* adds packet length, checksum, and EOP (0xFF) |
| 718 |
* |
| 719 |
*/ |
| 720 |
int dc3200_compile_packet(Camera *camera, unsigned char **data, int *data_len) |
| 721 |
{ |
| 722 |
int count, i, j; |
| 723 |
unsigned char *new_data = NULL; |
| 724 |
unsigned char *tmp_ptr = NULL; |
| 725 |
|
| 726 |
/* realloc + 2 for len and checksum */ |
| 727 |
*data_len += 2; |
| 728 |
|
| 729 |
tmp_ptr = (unsigned char*)realloc(*data, *data_len); |
| 730 |
if(tmp_ptr == NULL) |
| 731 |
return GP_ERROR; |
| 732 |
|
| 733 |
*data = tmp_ptr; |
| 734 |
|
| 735 |
/* add length, checksum */ |
| 736 |
(*data)[*data_len - 2] = *data_len - 2; |
| 737 |
(*data)[*data_len - 1] = dc3200_calc_checksum(camera, *data, *data_len - 1); |
| 738 |
|
| 739 |
/* this is a hack to fix the 0xFF/0xFE problem. |
| 740 |
* we adjust a "don't care" byte so the checksum |
| 741 |
* is not 0xFF or 0xFE |
| 742 |
*/ |
| 743 |
if((*data)[*data_len - 1] >= 0xFE) { |
| 744 |
if(*data_len > 19) { |
| 745 |
(*data)[19] += 2; |
| 746 |
(*data)[*data_len - 1] = dc3200_calc_checksum(camera, *data, *data_len - 1); |
| 747 |
printf("adjusting checksum to %02x\n", (*data)[*data_len - 1]); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
/* count number of 0xfe's and 0xff's */ |
| 752 |
count = 0; |
| 753 |
for(i=0;i<*data_len; i++) { |
| 754 |
if((*data)[i] == 0xFE || (*data)[i] == 0xFF) |
| 755 |
count++; |
| 756 |
} |
| 757 |
|
| 758 |
new_data = (unsigned char*)malloc(*data_len + count + 3); /* 3 for length, checksum, EOP */ |
| 759 |
if(!new_data) |
| 760 |
return GP_ERROR; |
| 761 |
|
| 762 |
j = 0; |
| 763 |
for(i=0;i<*data_len; i++) { |
| 764 |
if((*data)[i] == 0xFE || (*data)[i] == 0xFF) { |
| 765 |
printf("(*data)[i] == %02x\n", (*data)[i]); |
| 766 |
printf("(*data)[i] - 0xFE == %02x\n", (*data)[i] - 0xFE); |
| 767 |
new_data[j++] = 0xFE; |
| 768 |
new_data[j++] = (*data)[i] - 0xFE; |
| 769 |
} else { |
| 770 |
new_data[j++] = (*data)[i]; |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
/* adjust the new packet count */ |
| 775 |
*data_len += count + 1; |
| 776 |
|
| 777 |
new_data[*data_len - 1] = 0xFF; |
| 778 |
|
| 779 |
/* free the old packet data */ |
| 780 |
free(*data); |
| 781 |
|
| 782 |
/* set the ptr */ |
| 783 |
*data = new_data; |
| 784 |
|
| 785 |
return GP_OK; |
| 786 |
} |
| 787 |
|
| 788 |
/* |
| 789 |
* dc3200_process_packet |
| 790 |
* |
| 791 |
* changes 0xFE 0x01 -> 0xFF |
| 792 |
* 0xFE 0x00 -> 0xFE |
| 793 |
* checks & removes packet length, checksum, EOP (0xFF) |
| 794 |
* |
| 795 |
*/ |
| 796 |
int dc3200_process_packet(Camera *camera, unsigned char *data, int *data_len) |
| 797 |
{ |
| 798 |
int count = 0, i; |
| 799 |
int length, checksum; |
| 800 |
unsigned char *buff; |
| 801 |
|
| 802 |
if(data == NULL || *data_len < 1) { |
| 803 |
return GP_ERROR; |
| 804 |
} |
| 805 |
|
| 806 |
/* allocate a new buffer */ |
| 807 |
buff = (unsigned char*)malloc(sizeof(unsigned char) * *data_len); |
| 808 |
if(!buff) |
| 809 |
return GP_ERROR; |
| 810 |
|
| 811 |
/* change 0xFE 0x00 | 0x01 -> 0xFF | 0xFE */ |
| 812 |
for(i=0; i<*data_len; i++) { |
| 813 |
if(data[i] == 0xFE) { |
| 814 |
if(i<*data_len-1) { /* so we don't run out of array bounds */ |
| 815 |
if(data[i+1] == 0x00) { |
| 816 |
buff[count] = 0xFE; |
| 817 |
/* advance i so we don't pick up the trailing byte */ |
| 818 |
i++; |
| 819 |
/* incr count */ |
| 820 |
count++; |
| 821 |
} else if(data[i+1] == 0x01) { |
| 822 |
buff[count] = 0xFF; |
| 823 |
i++; |
| 824 |
count++; |
| 825 |
} |
| 826 |
} else { |
| 827 |
/* we've found an 0xFE but there is no trailing byte */ |
| 828 |
free(buff); |
| 829 |
return GP_ERROR; |
| 830 |
} |
| 831 |
} else { |
| 832 |
buff[count] = data[i]; |
| 833 |
count++; |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
/* copy over the old buffer */ |
| 838 |
memcpy(data, buff, count); |
| 839 |
|
| 840 |
length = data[count - 3]; |
| 841 |
checksum = data[count - 2]; |
| 842 |
|
| 843 |
/* make sure its a valid packet */ |
| 844 |
if(length != count - 3 || checksum != dc3200_calc_checksum(camera, data, count - 2)) { |
| 845 |
printf("%02x=%02x %02x=%02x\n", length, count - 3, checksum, dc3200_calc_checksum(camera, data, count - 2)); |
| 846 |
free(buff); |
| 847 |
return GP_ERROR; |
| 848 |
} |
| 849 |
|
| 850 |
/* adjust packet size, strip length, checksum, EOP */ |
| 851 |
*data_len = count - 3; |
| 852 |
|
| 853 |
free(buff); |
| 854 |
return GP_OK; |
| 855 |
} |
| 856 |
|
| 857 |
/* |
| 858 |
* dc3200_calc_checksum |
| 859 |
* |
| 860 |
* calculates the checksum for a byte array |
| 861 |
* |
| 862 |
*/ |
| 863 |
int dc3200_calc_checksum(Camera *camera, unsigned char * buffer, int len) |
| 864 |
{ |
| 865 |
int sum = 0, i; |
| 866 |
|
| 867 |
for(i=0; i<len; i++) { |
| 868 |
sum += buffer[i]; |
| 869 |
} |
| 870 |
|
| 871 |
sum = -sum + 0xFF; |
| 872 |
if(sum < 0) sum = sum & 0xFF; |
| 873 |
|
| 874 |
return sum; |
| 875 |
} |
| 876 |
|
| 877 |
/* |
| 878 |
* dc3200_calc_seqnum |
| 879 |
* |
| 880 |
* calculates the seqnum for a packet |
| 881 |
* |
| 882 |
*/ |
| 883 |
int dc3200_calc_seqnum(Camera *camera) |
| 884 |
{ |
| 885 |
if(camera->pl->pkt_seqnum >= 0x1F || camera->pl->pkt_seqnum < 0x10) { |
| 886 |
camera->pl->pkt_seqnum = 0x10; |
| 887 |
return camera->pl->pkt_seqnum; |
| 888 |
} else { |
| 889 |
camera->pl->pkt_seqnum++; |
| 890 |
return camera->pl->pkt_seqnum; |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
/* |
| 895 |
* dc3200_keep_alive |
| 896 |
* |
| 897 |
* sends and receives a ping packet to keep the |
| 898 |
* camera awake |
| 899 |
* |
| 900 |
*/ |
| 901 |
int dc3200_keep_alive(Camera *camera) |
| 902 |
{ |
| 903 |
unsigned char ka[ACK_PACKET_LEN]; /* keepalive */ |
| 904 |
unsigned char ak[ACK_PACKET_LEN]; /* keepalive ack */ |
| 905 |
int ak_len = ACK_PACKET_LEN; |
| 906 |
|
| 907 |
ka[0] = 0xCF; |
| 908 |
ka[1] = 0x01; |
| 909 |
|
| 910 |
/* send keep alive packet */ |
| 911 |
if(dc3200_send_command(camera, ka, sizeof(ka), ak, &ak_len) == GP_ERROR) { |
| 912 |
return GP_ERROR; |
| 913 |
} |
| 914 |
|
| 915 |
/* check the ack (should be same packet) */ |
| 916 |
if(memcmp(ak,ka,ak_len) == 0) { |
| 917 |
return GP_OK; |
| 918 |
} |
| 919 |
|
| 920 |
return GP_ERROR; |
| 921 |
} |
| 922 |
|
| 923 |
/* |
| 924 |
* dc3200_clear_read_buffer |
| 925 |
* |
| 926 |
* clears data in the read buffer by reading it out |
| 927 |
* |
| 928 |
* there is definitely a better way to accomplish this... |
| 929 |
* |
| 930 |
*/ |
| 931 |
int dc3200_clear_read_buffer(Camera *camera) |
| 932 |
{ |
| 933 |
unsigned char byte; |
| 934 |
int count = 0; |
| 935 |
|
| 936 |
gp_port_set_timeout(camera->port, 0); |
| 937 |
|
| 938 |
while(gp_port_read(camera->port, (char *)&byte, 1) > 0) |
| 939 |
count++; |
| 940 |
|
| 941 |
if(count > 0) |
| 942 |
printf("cleared %d bytes from read buffer\n", count); |
| 943 |
|
| 944 |
gp_port_set_timeout(camera->port, TIMEOUT); |
| 945 |
|
| 946 |
return GP_OK; |
| 947 |
} |
| 948 |
|
| 949 |
/* |
| 950 |
* dump_buffer |
| 951 |
* |
| 952 |
* displays a byte array nicely formatted in hex format |
| 953 |
* |
| 954 |
*/ |
| 955 |
int dump_buffer(unsigned char * buffer, int len, char * title, int bytesperline) |
| 956 |
{ |
| 957 |
char spacer[80]; |
| 958 |
int i; |
| 959 |
|
| 960 |
memset(spacer, 0, sizeof(spacer)); |
| 961 |
memset(spacer, ' ', strlen(title)+2); |
| 962 |
|
| 963 |
printf("%s: ", title); |
| 964 |
|
| 965 |
for(i=0; i<len; i++) { |
| 966 |
if(i%bytesperline==0 && i > 0) { |
| 967 |
printf("\n%s", spacer); |
| 968 |
} |
| 969 |
printf("%02x ", buffer[i]); |
| 970 |
} |
| 971 |
|
| 972 |
printf("\n"); |
| 973 |
|
| 974 |
printf("%s: ", title); |
| 975 |
|
| 976 |
for(i=0; i<len; i++) { |
| 977 |
if(i%bytesperline==0 && i > 0) { |
| 978 |
printf("\n%s", spacer); |
| 979 |
} |
| 980 |
if(buffer[i] >= 0x20 && buffer[i] < 0x7F) { |
| 981 |
printf("%c", buffer[i]); |
| 982 |
} else { |
| 983 |
printf("."); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
printf("\n"); |
| 988 |
|
| 989 |
return GP_OK; |
| 990 |
} |
| 991 |
|
| 992 |
unsigned long bytes_to_l(int a, int b, int c, int d) |
| 993 |
{ |
| 994 |
unsigned long res = a; |
| 995 |
res = res << 24 | b << 16 | c << 8 | d; |
| 996 |
return res; |
| 997 |
} |