2012-10-26 22:06:47 -04:00
|
|
|
/*
|
2012-11-03 09:37:12 -04:00
|
|
|
* Canon SELPHY ES/CP series print assister -- Native Linux version
|
2012-10-26 22:06:47 -04:00
|
|
|
*
|
2012-10-27 10:27:31 -04:00
|
|
|
* (c) 2007-2012 Solomon Peachy <pizza@shaftnet.org>
|
2012-10-26 22:06:47 -04:00
|
|
|
*
|
2012-10-27 10:27:31 -04:00
|
|
|
* The latest version of this program can be found at
|
2012-10-26 22:06:47 -04:00
|
|
|
*
|
2012-10-27 10:27:31 -04:00
|
|
|
* http://git.shaftnet.org/git/gitweb.cgi?p=selphy_print.git
|
2012-10-26 22:06:47 -04:00
|
|
|
*
|
|
|
|
* 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 3 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* [http://www.gnu.org/licenses/gpl-3.0.html]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2012-10-26 22:50:04 -04:00
|
|
|
#include "es_print_common.h"
|
|
|
|
|
2012-10-27 10:38:36 -04:00
|
|
|
#define dump_data dump_data_linux
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-10-27 10:38:36 -04:00
|
|
|
int dump_data_linux(int remaining, int present, int data_fd,
|
|
|
|
int dev_fd, uint8_t *buf, uint16_t buflen)
|
2012-10-26 22:06:47 -04:00
|
|
|
{
|
2012-10-27 10:35:26 -04:00
|
|
|
int cnt;
|
|
|
|
int i;
|
|
|
|
int wrote;
|
|
|
|
|
|
|
|
while (remaining > 0) {
|
|
|
|
cnt = read(data_fd, buf, (remaining < buflen) ? remaining : buflen);
|
|
|
|
|
|
|
|
if (cnt < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (present) {
|
|
|
|
cnt += present;
|
|
|
|
present = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = write(dev_fd, buf, cnt);
|
|
|
|
if (i < 0)
|
|
|
|
return wrote;
|
|
|
|
if (i != cnt) {
|
|
|
|
/* Realign buffer.. */
|
|
|
|
present = cnt - i;
|
|
|
|
memmove(buf, buf + i, present);
|
|
|
|
}
|
|
|
|
wrote += i;
|
|
|
|
remaining -= cnt;
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Wrote %d bytes (%d)\n", wrote, buflen);
|
2012-10-27 10:35:26 -04:00
|
|
|
|
|
|
|
return wrote;
|
2012-10-26 22:06:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-10-27 10:35:26 -04:00
|
|
|
int dev_fd, data_fd;
|
|
|
|
|
|
|
|
uint8_t buffer[BUF_LEN];
|
2012-10-28 12:19:17 -04:00
|
|
|
uint8_t rdbuf[READBACK_LEN];
|
|
|
|
uint8_t rdbuf2[READBACK_LEN];
|
2012-10-27 10:35:26 -04:00
|
|
|
|
|
|
|
int last_state, state = S_IDLE;
|
|
|
|
int printer_type = P_END;
|
|
|
|
int printer_type2 = P_END;
|
|
|
|
int plane_len = 0;
|
|
|
|
int bw_mode = 0;
|
|
|
|
int16_t paper_code_offset = -1;
|
|
|
|
int16_t paper_code = -1;
|
|
|
|
|
|
|
|
/* Static initialization */
|
|
|
|
setup_paper_codes();
|
|
|
|
|
|
|
|
/* Cmdline help */
|
|
|
|
if (argc < 2) {
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("SELPHY ES/CP Print Assist version %s\n\nUsage:\n\t%s [ infile | - ] [ outdev ]\n\n",
|
|
|
|
VERSION,
|
|
|
|
argv[0]);
|
2012-10-27 10:35:26 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open input file */
|
|
|
|
data_fd = fileno(stdin);
|
|
|
|
if (strcmp("-", argv[1])) {
|
|
|
|
data_fd = open(argv[1], O_RDONLY);
|
|
|
|
if (data_fd < 0) {
|
|
|
|
perror("Can't open input file");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open output device */
|
|
|
|
dev_fd = open(argv[2], O_RDWR);
|
|
|
|
if (dev_fd < 0) {
|
|
|
|
perror("Can't open output device");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Figure out the printer type based on the readback */
|
2012-10-28 12:19:17 -04:00
|
|
|
read(dev_fd, rdbuf, READBACK_LEN);
|
2012-10-27 10:35:26 -04:00
|
|
|
for (printer_type2 = 0; printer_type2 < P_END ; printer_type2++) {
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type2].init_readback, READBACK_LEN, -1, -1))
|
2012-10-27 10:35:26 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (printer_type2 == P_END) {
|
2012-11-13 07:14:32 -05:00
|
|
|
ERROR("Unrecognized printer!\n");
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("readback: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
|
2012-10-27 10:35:26 -04:00
|
|
|
rdbuf[0], rdbuf[1], rdbuf[2], rdbuf[3],
|
|
|
|
rdbuf[4], rdbuf[5], rdbuf[6], rdbuf[7],
|
|
|
|
rdbuf[8], rdbuf[9], rdbuf[10], rdbuf[11]);
|
|
|
|
}
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-10-27 10:35:26 -04:00
|
|
|
/* Figure out printer this file is intended for */
|
|
|
|
read(data_fd, buffer, MAX_HEADER);
|
|
|
|
|
2012-10-27 10:46:56 -04:00
|
|
|
printer_type = parse_printjob(buffer, &bw_mode, &plane_len);
|
|
|
|
if (printer_type < 0) {
|
2012-11-13 07:14:32 -05:00
|
|
|
ERROR("Unrecognized file format!\n");
|
2012-11-06 22:34:23 -05:00
|
|
|
exit(-1);
|
2012-10-27 10:35:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (printer_type != printer_type2) {
|
2012-11-13 07:14:32 -05:00
|
|
|
ERROR("File intended for a %s printer, aborting!\n", printers[printer_type].model);
|
2012-11-06 22:34:23 -05:00
|
|
|
exit(-1);
|
2012-10-27 10:35:26 -04:00
|
|
|
} else {
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Printing a %s file\n", printers[printer_type].model);
|
2012-10-27 10:35:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
plane_len += 12; /* Add in plane header */
|
2012-10-28 12:19:17 -04:00
|
|
|
paper_code_offset = printers[printer_type].paper_code_offset;
|
2012-10-27 10:35:26 -04:00
|
|
|
if (paper_code_offset != -1)
|
2012-11-07 19:00:29 -05:00
|
|
|
paper_code = printers[printer_type].paper_codes[buffer[printers[printer_type].pgcode_offset]];
|
2012-10-27 10:35:26 -04:00
|
|
|
|
|
|
|
top:
|
|
|
|
|
2012-10-28 12:19:17 -04:00
|
|
|
read(dev_fd, rdbuf, READBACK_LEN); /* Read the status from printer */
|
|
|
|
if (memcmp(rdbuf, rdbuf2, READBACK_LEN)) {
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("readback: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
|
2012-10-27 10:35:26 -04:00
|
|
|
rdbuf[0], rdbuf[1], rdbuf[2], rdbuf[3],
|
|
|
|
rdbuf[4], rdbuf[5], rdbuf[6], rdbuf[7],
|
|
|
|
rdbuf[8], rdbuf[9], rdbuf[10], rdbuf[11]);
|
2012-10-28 12:19:17 -04:00
|
|
|
memcpy(rdbuf2, rdbuf, READBACK_LEN);
|
2012-10-27 10:35:26 -04:00
|
|
|
} else {
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
if (state != last_state) {
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("last_state %d new %d\n", last_state, state);
|
2012-10-27 10:35:26 -04:00
|
|
|
last_state = state;
|
|
|
|
}
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
switch(state) {
|
|
|
|
case S_IDLE:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].init_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-27 10:35:26 -04:00
|
|
|
state = S_PRINTER_READY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_READY:
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending init sequence (%d bytes)\n", printers[printer_type].init_length);
|
2012-10-28 12:19:17 -04:00
|
|
|
write(dev_fd, buffer, printers[printer_type].init_length); /* Send printer_init */
|
2012-10-27 10:35:26 -04:00
|
|
|
/* Realign plane data to start of buffer.. */
|
2012-10-28 12:19:17 -04:00
|
|
|
memmove(buffer, buffer+printers[printer_type].init_length,
|
|
|
|
MAX_HEADER-printers[printer_type].init_length);
|
2012-10-27 10:35:26 -04:00
|
|
|
|
|
|
|
state = S_PRINTER_INIT_SENT;
|
|
|
|
break;
|
|
|
|
case S_PRINTER_INIT_SENT:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].ready_y_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-27 10:35:26 -04:00
|
|
|
state = S_PRINTER_READY_Y;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_READY_Y:
|
|
|
|
if (bw_mode)
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending BLACK plane\n");
|
2012-10-27 10:35:26 -04:00
|
|
|
else
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending YELLOW plane\n");
|
2012-10-28 12:19:17 -04:00
|
|
|
dump_data(plane_len, MAX_HEADER-printers[printer_type].init_length, data_fd, dev_fd, buffer, BUF_LEN);
|
2012-10-27 10:35:26 -04:00
|
|
|
state = S_PRINTER_Y_SENT;
|
|
|
|
break;
|
|
|
|
case S_PRINTER_Y_SENT:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].ready_m_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-27 10:35:26 -04:00
|
|
|
if (bw_mode)
|
|
|
|
state = S_PRINTER_DONE;
|
|
|
|
else
|
|
|
|
state = S_PRINTER_READY_M;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_READY_M:
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending MAGENTA plane\n");
|
2012-10-27 10:38:36 -04:00
|
|
|
dump_data(plane_len, 0, data_fd, dev_fd, buffer, BUF_LEN);
|
2012-10-27 10:35:26 -04:00
|
|
|
state = S_PRINTER_M_SENT;
|
|
|
|
break;
|
|
|
|
case S_PRINTER_M_SENT:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].ready_c_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-27 10:35:26 -04:00
|
|
|
state = S_PRINTER_READY_C;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_READY_C:
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending CYAN plane\n");
|
2012-10-27 10:38:36 -04:00
|
|
|
dump_data(plane_len, 0, data_fd, dev_fd, buffer, BUF_LEN);
|
2012-10-27 10:35:26 -04:00
|
|
|
state = S_PRINTER_C_SENT;
|
|
|
|
break;
|
|
|
|
case S_PRINTER_C_SENT:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].done_c_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-27 10:35:26 -04:00
|
|
|
state = S_PRINTER_DONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_DONE:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (printers[printer_type].foot_length) {
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending cleanup sequence\n");
|
2012-10-28 12:19:17 -04:00
|
|
|
dump_data(printers[printer_type].foot_length, 0, data_fd, dev_fd, buffer, BUF_LEN);
|
2012-10-27 10:35:26 -04:00
|
|
|
}
|
|
|
|
state = S_FINISHED;
|
|
|
|
break;
|
|
|
|
case S_FINISHED:
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("All data sent to printer!\n");
|
2012-10-27 10:35:26 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (state != S_FINISHED)
|
|
|
|
goto top;
|
|
|
|
|
|
|
|
return 0;
|
2012-10-26 22:06:47 -04:00
|
|
|
}
|