From 293241b39f58f2bdb6921a5c18be3580e8da41dd Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Thu, 27 Jun 2013 23:02:34 -0400 Subject: [PATCH] all: Start moving common code out into a separate library. The goal is to avoid code duplication. There's a lot of similar functionality remaining. --- Makefile | 7 ++- backend_common.c | 140 +++++++++++++++++++++++++++++++++++++++++++++ kodak1400_print.c | 116 +++---------------------------------- kodak6800_print.c | 113 ++---------------------------------- selphy_print.c | 142 +++++----------------------------------------- 5 files changed, 171 insertions(+), 347 deletions(-) create mode 100644 backend_common.c diff --git a/Makefile b/Makefile index b81dfc8..5a1551e 100644 --- a/Makefile +++ b/Makefile @@ -2,16 +2,17 @@ CFLAGS = -Wall LDFLAGS = -lusb-1.0 CUPS_BACKEND_DIR = /usr/lib/cups/backend +DEPS = backend_common.c all: selphy_print kodak1400_print kodak6800_print -selphy_print: selphy_print.c +selphy_print: selphy_print.c $(DEPS) gcc -o $@ $< $(LDFLAGS) $(CFLAGS) -kodak6800_print: $< kodak6800_print.c +kodak6800_print: kodak6800_print.c $(DEPS) gcc -o $@ $< $(LDFLAGS) $(CFLAGS) -kodak1400_print: kodak1400_print.c +kodak1400_print: kodak1400_print.c $(DEPS) gcc -o $@ $< $(LDFLAGS) $(CFLAGS) install: diff --git a/backend_common.c b/backend_common.c new file mode 100644 index 0000000..989f12f --- /dev/null +++ b/backend_common.c @@ -0,0 +1,140 @@ +/* + * CUPS Backend common code + * + * (c) 2013 Solomon Peachy + * + * The latest version of this program can be found at: + * + * http://git.shaftnet.org/git/gitweb.cgi?p=selphy_print.git + * + * 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 + +#define STR_LEN_MAX 64 +#define DEBUG( ... ) fprintf(stderr, "DEBUG: " __VA_ARGS__ ) +#define INFO( ... ) fprintf(stderr, "INFO: " __VA_ARGS__ ) +#define ERROR( ... ) do { fprintf(stderr, "ERROR: " __VA_ARGS__ ); sleep(1); } while (0) + +#define DEBUG( ... ) fprintf(stderr, "DEBUG: " __VA_ARGS__ ) +#define INFO( ... ) fprintf(stderr, "INFO: " __VA_ARGS__ ) +#define ERROR( ... ) do { fprintf(stderr, "ERROR: " __VA_ARGS__ ); sleep(1); } while (0) + +#if (__BYTE_ORDER == __LITTLE_ENDIAN) +#define le32_to_cpu(__x) __x +#define le16_to_cpu(__x) __x +#define be16_to_cpu(__x) ntohs(__x) +#define be32_to_cpu(__x) ntohl(__x) +#else +#define le32_to_cpu(x) \ + ({ \ + uint32_t __x = (x); \ + ((uint32_t)( \ + (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ + (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ + (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ + (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ + }) +#define le16_to_cpu(x) \ + ({ \ + uint16_t __x = (x); \ + ((uint16_t)( \ + (((uint16_t)(__x) & (uint16_t)0x00ff) << 8) | \ + (((uint16_t)(__x) & (uint16_t)0xff00) >> 8) | \ + }) +#define be32_to_cpu(__x) __x +#define be16_to_cpu(__x) __x +#endif + +#define ID_BUF_SIZE 2048 +static char *get_device_id(struct libusb_device_handle *dev) +{ + int length; + int claimed = 0; + int iface = 0; + char *buf = malloc(ID_BUF_SIZE + 1); + + claimed = libusb_kernel_driver_active(dev, iface); + if (claimed) + libusb_detach_kernel_driver(dev, iface); + + libusb_claim_interface(dev, iface); + + if (libusb_control_transfer(dev, + LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN | + LIBUSB_RECIPIENT_INTERFACE, + 0, 0, + (iface << 8), + (unsigned char *)buf, ID_BUF_SIZE, 5000) < 0) + { + *buf = '\0'; + goto done; + } + + /* length is the first two bytes, MSB first */ + length = (((unsigned)buf[0] & 255) << 8) | + ((unsigned)buf[1] & 255); + + /* Sanity checks */ + if (length > ID_BUF_SIZE || length < 14) + length = (((unsigned)buf[1] & 255) << 8) | + ((unsigned)buf[0] & 255); + + if (length > ID_BUF_SIZE) + length = ID_BUF_SIZE; + + if (length < 14) { + *buf = '\0'; + goto done; + } + + /* Move, and terminate */ + memmove(buf, buf + 2, length); + buf[length] = '\0'; + +done: + libusb_release_interface(dev, iface); + if (claimed) + libusb_attach_kernel_driver(dev, iface); + + return buf; +} + +static int send_data(struct libusb_device_handle *dev, uint8_t endp, + uint8_t *buf, uint16_t len) +{ + int num; + + int ret = libusb_bulk_transfer(dev, endp, + buf, len, + &num, 10000); + + if (ret < 0) { + ERROR("Failure to send data to printer (libusb error %d: (%d/%d to 0x%02x))\n", ret, num, len, endp); + return ret; + } + return 0; +} + +static int terminate = 0; + +void sigterm_handler(int signum) { + terminate = 1; + INFO("Job Cancelled"); +} diff --git a/kodak1400_print.c b/kodak1400_print.c index 97d805c..88d5641 100644 --- a/kodak1400_print.c +++ b/kodak1400_print.c @@ -37,38 +37,10 @@ #include #include -#include - -#define VERSION "0.09" -#define STR_LEN_MAX 64 -#define CMDBUF_LEN 96 -#define READBACK_LEN 8 +#define VERSION "0.10" #define URI_PREFIX "kodak1400://" -#define DEBUG( ... ) fprintf(stderr, "DEBUG: " __VA_ARGS__ ) -#define INFO( ... ) fprintf(stderr, "INFO: " __VA_ARGS__ ) -#define ERROR( ... ) do { fprintf(stderr, "ERROR: " __VA_ARGS__ ); sleep(1); } while (0) -#if (__BYTE_ORDER == __LITTLE_ENDIAN) -#define le32_to_cpu(__x) __x -#define le16_to_cpu(__x) __x -#else -#define le32_to_cpu(x) \ - ({ \ - uint32_t __x = (x); \ - ((uint32_t)( \ - (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ - (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ - (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ - (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ - }) -#define le16_to_cpu(x) \ - ({ \ - uint16_t __x = (x); \ - ((uint16_t)( \ - (((uint16_t)(__x) & (uint16_t)0x00ff) << 8) | \ - (((uint16_t)(__x) & (uint16_t)0xff00) >> 8) | \ - }) -#endif +#include "backend_common.c" /* USB Identifiers */ #define USB_VID_KODAK 0x040A @@ -105,63 +77,12 @@ struct kodak1400_hdr { uint8_t null4[12]; }; +#define CMDBUF_LEN 96 +#define READBACK_LEN 8 + static uint8_t idle_data[READBACK_LEN] = { 0xe4, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -#define ID_BUF_SIZE 2048 -static char *get_device_id(struct libusb_device_handle *dev) -{ - int length; - int claimed = 0; - int iface = 0; - char *buf = malloc(ID_BUF_SIZE + 1); - - claimed = libusb_kernel_driver_active(dev, iface); - if (claimed) - libusb_detach_kernel_driver(dev, iface); - - libusb_claim_interface(dev, iface); - - if (libusb_control_transfer(dev, - LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN | - LIBUSB_RECIPIENT_INTERFACE, - 0, 0, - (iface << 8), - (unsigned char *)buf, ID_BUF_SIZE, 5000) < 0) - { - *buf = '\0'; - goto done; - } - - /* length is the first two bytes, MSB first */ - length = (((unsigned)buf[0] & 255) << 8) | - ((unsigned)buf[1] & 255); - - /* Sanity checks */ - if (length > ID_BUF_SIZE || length < 14) - length = (((unsigned)buf[1] & 255) << 8) | - ((unsigned)buf[0] & 255); - - if (length > ID_BUF_SIZE) - length = ID_BUF_SIZE; - - if (length < 14) { - *buf = '\0'; - goto done; - } - - /* Move, and terminate */ - memmove(buf, buf + 2, length); - buf[length] = '\0'; - -done: - libusb_release_interface(dev, iface); - if (claimed) - libusb_attach_kernel_driver(dev, iface); - - return buf; -} - static int find_and_enumerate(struct libusb_context *ctx, struct libusb_device ***list, char *match_serno, @@ -257,22 +178,6 @@ static int find_and_enumerate(struct libusb_context *ctx, return found; } -static int send_data(struct libusb_device_handle *dev, uint8_t endp, - uint8_t *buf, uint16_t len) -{ - int num; - - int ret = libusb_bulk_transfer(dev, endp, - buf, len, - &num, 5000); - - if (ret < 0) { - ERROR("Failure to send data to printer (libusb error %d: (%d/%d to 0x%02x))\n", ret, num, len, endp); - return ret; - } - return 0; -} - static int send_plane(struct libusb_device_handle *dev, uint8_t endp, uint8_t planeno, uint8_t *planedata, struct kodak1400_hdr *hdr, uint8_t *cmdbuf) @@ -332,13 +237,6 @@ static int send_plane(struct libusb_device_handle *dev, uint8_t endp, return 0; } -static int terminate = 0; - -void sigterm_handler(int signum) { - terminate = 1; - INFO("Job Cancelled"); -} - int main (int argc, char **argv) { struct libusb_context *ctx; @@ -588,9 +486,9 @@ top: cmdbuf[0] = 0x1b; cmdbuf[1] = 0x5a; cmdbuf[2] = 0x53; - temp16 = ntohs(hdr.columns); + temp16 = be16_to_cpu(hdr.columns); memcpy(cmdbuf+3, &temp16, 2); - temp16 = ntohs(hdr.rows); + temp16 = be16_to_cpu(hdr.rows); memcpy(cmdbuf+5, &temp16, 2); if ((ret = send_data(dev, endp_down, diff --git a/kodak6800_print.c b/kodak6800_print.c index ed62bf4..7060fd0 100644 --- a/kodak6800_print.c +++ b/kodak6800_print.c @@ -37,38 +37,11 @@ #include #include -#include - -#define VERSION "0.01" -#define STR_LEN_MAX 64 -#define CMDBUF_LEN 17 -#define READBACK_LEN 58 +#define VERSION "0.02" #define URI_PREFIX "kodak6800://" -#define DEBUG( ... ) fprintf(stderr, "DEBUG: " __VA_ARGS__ ) -#define INFO( ... ) fprintf(stderr, "INFO: " __VA_ARGS__ ) -#define ERROR( ... ) do { fprintf(stderr, "ERROR: " __VA_ARGS__ ); sleep(1); } while (0) +#define STR_LEN_MAX 64 -#if (__BYTE_ORDER == __LITTLE_ENDIAN) -#define le32_to_cpu(__x) __x -#define le16_to_cpu(__x) __x -#else -#define le32_to_cpu(x) \ - ({ \ - uint32_t __x = (x); \ - ((uint32_t)( \ - (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ - (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ - (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ - (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ - }) -#define le16_to_cpu(x) \ - ({ \ - uint16_t __x = (x); \ - ((uint16_t)( \ - (((uint16_t)(__x) & (uint16_t)0x00ff) << 8) | \ - (((uint16_t)(__x) & (uint16_t)0xff00) >> 8) | \ - }) -#endif +#include "backend_common.c" /* USB Identifiers */ #define USB_VID_KODAK 0x040A @@ -94,59 +67,8 @@ struct kodak6800_hdr { uint8_t null; }; -#define ID_BUF_SIZE 2048 -static char *get_device_id(struct libusb_device_handle *dev) -{ - int length; - int claimed = 0; - int iface = 0; - char *buf = malloc(ID_BUF_SIZE + 1); - - claimed = libusb_kernel_driver_active(dev, iface); - if (claimed) - libusb_detach_kernel_driver(dev, iface); - - libusb_claim_interface(dev, iface); - - if (libusb_control_transfer(dev, - LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN | - LIBUSB_RECIPIENT_INTERFACE, - 0, 0, - (iface << 8), - (unsigned char *)buf, ID_BUF_SIZE, 5000) < 0) - { - *buf = '\0'; - goto done; - } - - /* length is the first two bytes, MSB first */ - length = (((unsigned)buf[0] & 255) << 8) | - ((unsigned)buf[1] & 255); - - /* Sanity checks */ - if (length > ID_BUF_SIZE || length < 14) - length = (((unsigned)buf[1] & 255) << 8) | - ((unsigned)buf[0] & 255); - - if (length > ID_BUF_SIZE) - length = ID_BUF_SIZE; - - if (length < 14) { - *buf = '\0'; - goto done; - } - - /* Move, and terminate */ - memmove(buf, buf + 2, length); - buf[length] = '\0'; - -done: - libusb_release_interface(dev, iface); - if (claimed) - libusb_attach_kernel_driver(dev, iface); - - return buf; -} +#define CMDBUF_LEN 17 +#define READBACK_LEN 58 static int find_and_enumerate(struct libusb_context *ctx, struct libusb_device ***list, @@ -238,29 +160,6 @@ static int find_and_enumerate(struct libusb_context *ctx, return found; } -static int send_data(struct libusb_device_handle *dev, uint8_t endp, - uint8_t *buf, uint16_t len) -{ - int num; - - int ret = libusb_bulk_transfer(dev, endp, - buf, len, - &num, 5000); - - if (ret < 0) { - ERROR("Failure to send data to printer (libusb error %d: (%d/%d to 0x%02x))\n", ret, num, len, endp); - return ret; - } - return 0; -} - -static int terminate = 0; - -void sigterm_handler(int signum) { - terminate = 1; - INFO("Job Cancelled"); -} - int main (int argc, char **argv) { struct libusb_context *ctx; @@ -365,7 +264,7 @@ int main (int argc, char **argv) /* Read in image data */ cmdbuf = malloc(CMDBUF_LEN); - datasize = ntohs(hdr.rows) * ntohs(hdr.columns) * 3; + datasize = be16_to_cpu(hdr.rows) * be16_to_cpu(hdr.columns) * 3; planedata = malloc(datasize + CMDBUF_LEN); if (!cmdbuf || !planedata) { ERROR("Memory allocation failure!\n"); diff --git a/selphy_print.c b/selphy_print.c index e1289fc..e97c1f0 100644 --- a/selphy_print.c +++ b/selphy_print.c @@ -35,11 +35,11 @@ #include #include -#include - -#define STR_LEN_MAX 64 +#define VERSION "0.47" #define URI_PREFIX "selphy://" +#include "backend_common.c" + /* USB Identifiers */ #define USB_VID_CANON 0x04a9 #define USB_PID_CANON_ES1 0x3141 @@ -73,26 +73,6 @@ #define USB_PID_CANON_CP810 0x3256 #define USB_PID_CANON_CP900 0x3255 -#define VERSION "0.46" - -#define DEBUG( ... ) fprintf(stderr, "DEBUG: " __VA_ARGS__ ) -#define INFO( ... ) fprintf(stderr, "INFO: " __VA_ARGS__ ) -#define ERROR( ... ) do { fprintf(stderr, "ERROR: " __VA_ARGS__ ); sleep(1); } while (0) - -#if (__BYTE_ORDER == __LITTLE_ENDIAN) -#define le32_to_cpu(__x) __x -#else -#define le32_to_cpu(x) \ - ({ \ - uint32_t __x = (x); \ - ((uint32_t)( \ - (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ - (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ - (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ - (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ - }) -#endif - #define READBACK_LEN 12 struct printer_data { @@ -373,60 +353,6 @@ static int read_data(int remaining, int present, int data_fd, uint8_t *target, return wrote; } -#define ID_BUF_SIZE 2048 -static char *get_device_id(struct libusb_device_handle *dev) -{ - int length; - int claimed = 0; - int iface = 0; - char *buf = malloc(ID_BUF_SIZE + 1); - - claimed = libusb_kernel_driver_active(dev, iface); - if (claimed) - libusb_detach_kernel_driver(dev, iface); - - libusb_claim_interface(dev, iface); - - if (libusb_control_transfer(dev, - LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN | - LIBUSB_RECIPIENT_INTERFACE, - 0, 0, - (iface << 8), - (unsigned char *)buf, ID_BUF_SIZE, 5000) < 0) - { - *buf = '\0'; - goto done; - } - - /* length is the first two bytes, MSB first */ - length = (((unsigned)buf[0] & 255) << 8) | - ((unsigned)buf[1] & 255); - - /* Sanity checks */ - if (length > ID_BUF_SIZE || length < 14) - length = (((unsigned)buf[1] & 255) << 8) | - ((unsigned)buf[0] & 255); - - if (length > ID_BUF_SIZE) - length = ID_BUF_SIZE; - - if (length < 14) { - *buf = '\0'; - goto done; - } - - /* Move, and terminate */ - memmove(buf, buf + 2, length); - buf[length] = '\0'; - -done: - libusb_release_interface(dev, iface); - if (claimed) - libusb_attach_kernel_driver(dev, iface); - - return buf; -} - static int find_and_enumerate(struct libusb_context *ctx, struct libusb_device ***list, char *match_serno, @@ -585,13 +511,6 @@ static int find_and_enumerate(struct libusb_context *ctx, return found; } -static int terminate = 0; - -void sigterm_handler(int signum) { - terminate = 1; - INFO("Job Cancelled"); -} - int main (int argc, char **argv) { struct libusb_context *ctx; @@ -842,16 +761,8 @@ top: INFO("Printing started\n"); /* Send printer init */ - ret = libusb_bulk_transfer(dev, endp_down, - header, - header_len, - &num, - 2000); - if (ret < 0) { - ERROR("Failure to send data to printer (libusb error %d: (%d/%d to 0x%02x))\n", ret, num, header_len, endp_down); - ret = 4; + if ((ret = send_data(dev, endp_down, header, header_len))) goto done_claimed; - } state = S_PRINTER_INIT_SENT; break; @@ -865,15 +776,10 @@ top: DEBUG("Sending BLACK plane\n"); else DEBUG("Sending YELLOW plane\n"); - ret = libusb_bulk_transfer(dev, endp_down, - plane_y, - plane_len, - &num, - 10000); - if (ret < 0) { - ret = 4; + + if ((ret = send_data(dev, endp_down, plane_y, plane_len))) goto done_claimed; - } + state = S_PRINTER_Y_SENT; break; case S_PRINTER_Y_SENT: @@ -886,16 +792,10 @@ top: break; case S_PRINTER_READY_M: DEBUG("Sending MAGENTA plane\n"); - ret = libusb_bulk_transfer(dev, endp_down, - plane_m, - plane_len, - &num, - 10000); - if (ret < 0) { - ERROR("Failure to send data to printer (libusb error %d: (%d/%d to 0x%02x))\n", ret, num, footer_len, endp_down); - ret = 4; + + if ((ret = send_data(dev, endp_down, plane_m, plane_len))) goto done_claimed; - } + state = S_PRINTER_M_SENT; break; case S_PRINTER_M_SENT: @@ -905,16 +805,10 @@ top: break; case S_PRINTER_READY_C: DEBUG("Sending CYAN plane\n"); - ret = libusb_bulk_transfer(dev, endp_down, - plane_c, - plane_len, - &num, - 10000); - if (ret < 0) { - ERROR("Failure to send data to printer (libusb error %d: (%d/%d to 0x%02x))\n", ret, num, footer_len, endp_down); - ret = 4; + + if ((ret = send_data(dev, endp_down, plane_c, plane_len))) goto done_claimed; - } + state = S_PRINTER_C_SENT; break; case S_PRINTER_C_SENT: @@ -926,16 +820,8 @@ top: if (footer_len) { DEBUG("Sending cleanup sequence\n"); - ret = libusb_bulk_transfer(dev, endp_down, - footer, - footer_len, - &num, - 2000); - if (ret < 0) { - ERROR("Failure to send data to printer (libusb error %d: (%d/%d to 0x%02x))\n", ret, num, footer_len, endp_down); - ret = 4; + if ((ret = send_data(dev, endp_down, footer, footer_len))) goto done_claimed; - } } state = S_FINISHED; /* Intentional Fallthrough */