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 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
|
/*****************************************************************************\
hp.c - hp cups backend
(c) 2004-2008 Copyright HP Development Company, LP
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\*****************************************************************************/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <syslog.h>
#include <ctype.h>
#include <pthread.h>
#ifdef HAVE_DBUS
#include <dbus/dbus.h>
#endif
#include "hpmud.h"
#include <signal.h>
#include<errno.h>
#include<utils.h>
//#define HP_DEBUG
enum BACKEND_RESULT
{
BACKEND_OK = 0,
BACKEND_FAILED = 1, /* use error-policy */
BACKEND_HOLD = 3, /* hold job */
BACKEND_STOP = 4, /* stop queue */
BACKEND_CANCEL = 5 /* cancel job */
};
struct pjl_attributes
{
int pjl_device; /* 0=disabled, 1=enabled */
int current_status;
int eoj_pages; /* end-of-job pages */
int abort; /* 0=no, 1=yes */
int done; /* 0=no, 1=yes */
HPMUD_DEVICE dd;
HPMUD_CHANNEL cd;
pthread_t tid;
pthread_mutex_t mutex;
pthread_cond_t done_cond;
};
#define _STRINGIZE(x) #x
#define STRINGIZE(x) _STRINGIZE(x)
#define BUG(args...) bug(__FILE__ " " STRINGIZE(__LINE__) ": " args)
#ifdef HP_DEBUG
#define DBG(args...) syslog(LOG_INFO, __FILE__ " " STRINGIZE(__LINE__) ": " args)
#define DBG_DUMP(data, size) sysdump((data), (size))
#define DBG_SZ(args...) syslog(LOG_INFO, args)
#else
#define DBG(args...)
#define DBG_DUMP(data, size)
#define DBG_SZ(args...)
#endif
#define RETRY_TIMEOUT 30 /* seconds */
#define EXCEPTION_TIMEOUT 45 /* seconds */
#define NFAULT_BIT 0x08
#define PERROR_BIT 0x20
#define OOP (NFAULT_BIT | PERROR_BIT)
#define JAMMED (PERROR_BIT)
#define ERROR_TRAP (0)
#define STATUS_MASK (NFAULT_BIT | PERROR_BIT)
#define DEVICE_IS_OOP(reg) ((reg & STATUS_MASK) == OOP)
#define DEVICE_PAPER_JAMMED(reg) ((reg & STATUS_MASK) == JAMMED)
#define DEVICE_IO_TRAP(reg) ((reg & STATUS_MASK) == ERROR_TRAP)
#define HEX2INT(x, i) if (x >= '0' && x <= '9') i |= x - '0'; \
else if (x >= 'A' && x <= 'F') i |= 0xA + x - 'A'; \
else if (x >= 'a' && x <= 'f') i |= 0xA + x - 'a'
/* Definitions for hpLogLevel in cupsd.conf. */
#define BASIC_LOG 1
#define SAVE_OUT_FILE 2
#define SAVE_INPUT_RASTERS 4
#define SAVE_OUT_FILE_IN_BACKEND 8
#define DONT_SEND_TO_BACKEND 16
#define DONT_SEND_TO_PRINTER 32
/* Actual vstatus codes are mapped to 1000+vstatus for DeviceError messages. */
typedef enum
{
VSTATUS_IDLE = 1000,
VSTATUS_BUSY,
VSTATUS_PRNT, /* io printing */
VSTATUS_OFFF, /* turning off */
VSTATUS_RPRT, /* report printing */
VSTATUS_CNCL, /* canceling */
VSTATUS_IOST, /* io stall */
VSTATUS_DRYW, /* dry time wait */
VSTATUS_PENC, /* pen change */
VSTATUS_OOPA, /* out of paper */
VSTATUS_BNEJ, /* banner eject needed */
VSTATUS_BNMZ, /* banner mismatch */
VSTATUS_PHMZ, /* photo mismatch */
VSTATUS_DPMZ, /* duplex mismatch */
VSTATUS_PAJM, /* media jam */
VSTATUS_CARS, /* carriage stall */
VSTATUS_PAPS, /* paper stall */
VSTATUS_PENF, /* pen failure */
VSTATUS_ERRO, /* hard error */
VSTATUS_PWDN, /* power down */
VSTATUS_FPTS, /* front panel test */
VSTATUS_CLNO /* clean out tray missing */
} VSTATUS;
#define EVENT_START_JOB 500
#define EVENT_END_JOB 501
//const char pjl_status_cmd[] = "\e%-12345X@PJL INFO STATUS \r\n\e%-12345X";
static const char pjl_ustatus_cmd[] = "\e%-12345X@PJL USTATUS DEVICE = ON \r\n@PJL USTATUS JOB = ON \r\n@PJL JOB \r\n\e%-12345X";
static const char pjl_job_end_cmd[] = "\e%-12345X@PJL EOJ \r\n\e%-12345X";
static const char pjl_ustatus_off_cmd[] = "\e%-12345X@PJL USTATUSOFF \r\n\e%-12345X";
#ifdef HAVE_DBUS
#define DBUS_INTERFACE "com.hplip.StatusService"
#define DBUS_PATH "/"
static DBusError dbus_err;
static DBusConnection *dbus_conn;
#endif
static int bug(const char *fmt, ...)
{
char buf[256];
va_list args;
int n;
va_start(args, fmt);
if ((n = vsnprintf(buf, 256, fmt, args)) == -1)
buf[255] = 0; /* output was truncated */
fprintf(stderr, "%s", buf);
syslog(LOG_ERR, "%s", buf);
fflush(stderr);
va_end(args);
return n;
}
#ifdef HP_DEBUG
static void sysdump(const void *data, int size)
{
/* Dump size bytes of *data. Output looks like:
* [0000] 75 6E 6B 6E 6F 77 6E 20 30 FF 00 00 00 00 39 00 unknown 0.....9.
*/
unsigned char *p = (unsigned char *)data;
unsigned char c;
int n;
char bytestr[4] = {0};
char addrstr[10] = {0};
char hexstr[16*3 + 5] = {0};
char charstr[16*1 + 5] = {0};
for(n=1;n<=size;n++) {
if (n%16 == 1) {
/* store address for this line */
snprintf(addrstr, sizeof(addrstr), "%.4d", (int)((p-(unsigned char *)data) & 0xffff));
}
c = *p;
if (isprint(c) == 0) {
c = '.';
}
/* store hex str (for left side) */
snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
/* store char str (for right side) */
snprintf(bytestr, sizeof(bytestr), "%c", c);
strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
if(n%16 == 0) {
/* line completed */
DBG_SZ("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
hexstr[0] = 0;
charstr[0] = 0;
}
p++; /* next byte */
}
if (strlen(hexstr) > 0) {
/* print rest of buffer if not empty */
DBG_SZ("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
}
}
#endif
/* Map printer status to IPP printer-state-reasons (see RFC-2911). */
static int map_ipp_printer_state_reason(int status, const char **state_msg)
{
if (status >= 1000 && status <= 1999)
{
/* inkjet vstatus */
switch (status)
{
case VSTATUS_IDLE:
case VSTATUS_PRNT:
*state_msg = "none";
break;
case VSTATUS_OOPA:
*state_msg = "media-empty-error";
break;
case(VSTATUS_PAJM):
*state_msg = "media-jam-error";
break;
default:
*state_msg = "other";
break;
}
}
else if (status >= 10000 && status <= 55999)
{
/* laserjet pjl status */
if (status >= 10000 && status <= 10999)
*state_msg = "none";
else if (status >= 41000 && status <= 41999)
*state_msg = "media-empty-error";
else if ((status >= 42000 && status <= 42999) || (status >= 44000 && status <= 44999) || (status == 40022))
*state_msg = "media-jam-error";
else if (status == 40021)
*state_msg = "cover-open-error";
else if (status == 40600)
*state_msg = "toner-empty-error";
else
*state_msg = "other"; /* 40017 - cartridge E-LABEL is unreadable (ie: ljp1005) */
}
else
{
/* Assume hpmud error */
*state_msg = "other";
}
return 0;
}
static enum HPMUD_RESULT get_pjl_input(HPMUD_DEVICE dd, HPMUD_CHANNEL cd, char *buf, int buf_size, int sec_timeout, int *bytes_read)
{
enum HPMUD_RESULT stat;
int len;
*bytes_read = 0;
/* Read unsolicited status from device. */
stat = hpmud_read_channel(dd, cd, buf, buf_size, sec_timeout, &len);
if (stat != HPMUD_R_OK)
goto bugout;
buf[len]=0;
DBG("pjl result len=%d\n", len);
DBG_DUMP(buf, len);
*bytes_read = len;
stat = HPMUD_R_OK;
bugout:
return stat;
}
static int parse_pjl_job_end(char *buf, int *pages)
{
char *p, *tail;
int stat=0;
if (buf[0] == 0)
goto bugout;
if ((p = strcasestr(buf, "ustatus job")) != NULL)
{
if (strncasecmp(p+13, "end", 3) == 0)
{
stat = 1;
if ((p = strcasestr(p+13+5, "pages=")) != NULL)
*pages = strtol(p+6, &tail, 10);
}
}
bugout:
return stat;
}
static int parse_pjl_device_status(char *buf, int *status)
{
char *p, *tail;
int stat=0;
if (buf[0] == 0)
goto bugout;
if ((p = strcasestr(buf, "code=")) != NULL)
{
*status = strtol(p+5, &tail, 10);
stat = 1;
}
bugout:
return stat;
}
static void pjl_read_thread(struct pjl_attributes *pa)
{
enum HPMUD_RESULT stat;
int len, new_status, new_eoj;
char buf[1024];
pthread_detach(pthread_self());
DBG("starting thread %d\n", (int)pa->tid);
pa->current_status = 10001; /* default is ready */
pa->eoj_pages = pa->abort = pa->done = 0;
while (!pa->abort)
{
stat = get_pjl_input(pa->dd, pa->cd, buf, sizeof(buf), 0, &len);
if (!(stat == HPMUD_R_OK || stat == HPMUD_R_IO_TIMEOUT))
{
BUG("exiting thread %d error=%d\n", (int)pa->tid, stat);
pthread_mutex_lock(&pa->mutex);
pa->current_status = 5000+stat; /* io error */
pthread_mutex_unlock(&pa->mutex);
break;
}
if (stat == HPMUD_R_OK)
{
pthread_mutex_lock(&pa->mutex);
new_status = parse_pjl_device_status(buf, &pa->current_status);
new_eoj = parse_pjl_job_end(buf, &pa->eoj_pages);
pthread_mutex_unlock(&pa->mutex);
if (new_status)
BUG("read new pjl status: %d\n", pa->current_status);
if (new_eoj)
BUG("read pjl job_end: %d\n", pa->eoj_pages);
}
else
sleep(1);
}
DBG("exiting thread %d abort=%d stat=%d\n", (int)pa->tid, pa->abort, stat);
pa->done=1;
pthread_cond_signal(&pa->done_cond);
return;
}
/*
* get_printer_status
*
* inputs:
* dd - device descriptor
* pa - see pjl_attributes definition
*
* outputs:
* return - printer status, 1000 to 1999 = inkjet vstatus, 5000 to 5999 = hpmud error, 10000 to 55999 = pjl status code
*
*/
static int get_printer_status(HPMUD_DEVICE dd, HPMUD_CHANNEL cd, struct pjl_attributes *pa)
{
char id[1024];
char *pSf;
int status, ver, len;
enum HPMUD_RESULT r;
if (pa->pjl_device)
{
pthread_mutex_lock(&pa->mutex);
status = pa->current_status;
pthread_mutex_unlock(&pa->mutex);
}
else
{
status = VSTATUS_IDLE; /* set default */
r = hpmud_get_device_id(dd, id, sizeof(id), &len);
// if (!(r == HPMUD_R_OK || r == HPMUD_R_DEVICE_BUSY))
if (r != HPMUD_R_OK)
{
status = 5000+r; /* no deviceid, return some error */
goto bugout;
}
/* Check for valid S-field in device id string. */
if ((pSf = strstr(id, ";S:")) == NULL)
{
/* No S-field, use status register instead of device id. */
unsigned int bit_status;
r = hpmud_get_device_status(dd, &bit_status);
// if (!(r == HPMUD_R_OK || r == HPMUD_R_DEVICE_BUSY))
if (r != HPMUD_R_OK)
{
status = 5000+r; /* no 8-bit status, return some error */
goto bugout;
}
if (DEVICE_IS_OOP(bit_status))
status = VSTATUS_OOPA;
else if (DEVICE_PAPER_JAMMED(bit_status))
status = VSTATUS_PAJM;
else if (DEVICE_IO_TRAP(bit_status))
status = VSTATUS_CARS;
}
else
{
/* Valid S-field, get version number. */
pSf+=3;
ver = 0;
HEX2INT(*pSf, ver);
pSf++;
ver = ver << 4;
HEX2INT(*pSf, ver);
pSf++;
/* Position pointer to printer state subfield. */
switch (ver)
{
case 0:
case 1:
case 2:
pSf+=12;
break;
case 3:
pSf+=14;
break;
case 4:
pSf+=18;
break;
default:
BUG("WARNING: unknown S-field version=%d\n", ver);
pSf+=12;
break;
}
/* Extract VStatus.*/
status = 0;
HEX2INT(*pSf, status);
pSf++;
status = status << 4;
HEX2INT(*pSf, status);
status += 1000;
}
}
bugout:
return status;
}
static int device_discovery()
{
char buf[HPMUD_LINE_SIZE*64];
int cnt=0, bytes_read, r=1;
enum HPMUD_RESULT stat;
stat = hpmud_probe_printers(HPMUD_BUS_ALL, buf, sizeof(buf), &cnt, &bytes_read);
if (stat != HPMUD_R_OK)
goto bugout;
if (cnt == 0)
#ifdef HAVE_CUPS11
fprintf(stdout, "direct hp:/no_device_found \"Unknown\" \"hp no_device_found\"\n");
#else
fprintf(stdout, "direct hp \"Unknown\" \"HP Printer (HPLIP)\"\n");
#endif
else
fprintf(stdout, "%s", buf);
r = 0;
bugout:
return r;
}
#ifdef HAVE_DBUS
static int device_event(const char *dev, const char *printer, int code,
const char *username, const char *jobid, const char *title)
{
DBusMessage * msg = NULL;
int id = atoi(jobid);
if (dbus_conn == NULL)
return 0;
msg = dbus_message_new_signal(DBUS_PATH, DBUS_INTERFACE, "Event");
if (NULL == msg)
{
BUG("dbus message is NULL!\n");
return 0;
}
dbus_message_append_args(msg,
DBUS_TYPE_STRING, &dev,
DBUS_TYPE_STRING, &printer,
DBUS_TYPE_UINT32, &code,
DBUS_TYPE_STRING, &username,
DBUS_TYPE_UINT32, &id,
DBUS_TYPE_STRING, &title,
DBUS_TYPE_INVALID);
if (!dbus_connection_send(dbus_conn, msg, NULL))
{
BUG("dbus message send failed!\n");
return 0;
}
dbus_connection_flush(dbus_conn);
dbus_message_unref(msg);
return 1;
}
int init_dbus(void)
{
dbus_error_init(&dbus_err);
dbus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_err);
if (dbus_error_is_set(&dbus_err))
{
BUG("dBus Connection Error (%s)!\n", dbus_err.message);
dbus_error_free(&dbus_err);
}
if (dbus_conn == NULL)
{
return 0;
}
return 1;
}
#else
static int device_event(const char *dev, const char *printer, int code,
const char *username, const char *jobid, const char *title)
{
return 1;
}
int init_dbus(void)
{
return 1;
}
#endif /* HAVE_DBUS */
/* Check printer status, if a valid error state, loop until error condition is cleared. */
static int loop_test(HPMUD_DEVICE dd, HPMUD_CHANNEL cd, struct pjl_attributes *pa,
const char *dev, const char *printer, const char *username, const char *jobid, const char *title)
{
int status, stat;
const char *pstate, *old_state=NULL;
while (1)
{
status = get_printer_status(dd, cd, pa);
map_ipp_printer_state_reason(status, &pstate);
/* Check for user intervention errors. */
if (strstr(pstate, "error"))
{
if (pstate != old_state)
{
if (old_state)
{
/* Clear old error. */
// device_event(dev, printer, status, username, jobid, title);
fprintf(stderr, "STATE: -%s\n", old_state);
}
/* Display error. */
device_event(dev, printer, status, username, jobid, title);
fprintf(stderr, "STATE: +%s\n", pstate);
old_state = pstate;
}
BUG("ERROR: %d %s; will retry in %d seconds...\n", status, pstate, RETRY_TIMEOUT);
sleep(RETRY_TIMEOUT);
continue;
}
/* Clear any old state. */
if (old_state)
fprintf(stderr, "STATE: -%s\n", old_state);
/* Check for system errors. */
if (status >= 5000 && status <= 5999)
{
/* Display error. */
device_event(dev, printer, status, username, jobid, title);
BUG("ERROR: %d device communication error!\n", status);
stat = 1;
}
else
stat = 0;
break; /* done */
}
return stat;
}
static FILE* create_out_file(char* job_id, char* user_name)
{
char fname[256] = {0,};
FILE *temp_fp = NULL;
snprintf(fname, sizeof(fname), "%s/hp_%s_out_%s_XXXXXX",CUPS_TMP_DIR, user_name, job_id);
createTempFile(fname, &temp_fp);
if (temp_fp)
{
chmod(fname, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
}
else
{
BUG("ERROR: unable to create Temporary file %s: %m\n", fname);
}
return temp_fp;
}
static void save_out_file(int fd, int copies, FILE * temp_fp)
{
int len = 0;
char buf[HPMUD_BUFFER_SIZE];
if (NULL == temp_fp)
{
BUG("ERROR: save_out_file function received NULL temp_fp pointer\n");
return;
}
while (copies > 0)
{
copies--;
if (fd != 0)
{
lseek(fd, 0, SEEK_SET);
}
while ((len = read(fd, buf, sizeof(buf))) > 0)
{
fwrite (buf, 1, len, temp_fp);
}
}
}
int main(int argc, char *argv[])
{
int fd;
int copies;
int len, status, cnt, exit_stat=BACKEND_FAILED;
char buf[HPMUD_BUFFER_SIZE];
struct hpmud_model_attributes ma;
struct pjl_attributes pa;
HPMUD_DEVICE hd=-1;
HPMUD_CHANNEL cd=-1;
int n, total=0, retry=0, size, pages;
enum HPMUD_RESULT stat;
char *printer = getenv("PRINTER");
int iLogLevel = 0;
FILE *temp_fp = NULL;
int saveoutfile = 0;
// 0 1 2 3 4 5
// device_uri job-id user title copies options
openlog("hp", LOG_PID, LOG_DAEMON);
pa.tid = 0;
if (argc > 1)
{
const char *arg = argv[1];
if ((arg[0] == '-') && (arg[1] == 'h'))
{
fprintf(stdout, "HP Linux Imaging and Printing System\nCUPS Backend %s\n", VERSION);
fprintf(stdout, "(c) 2003-2008 Copyright HP Development Company, LP\n");
exit(0);
}
}
if (argc == 1)
exit (device_discovery());
if (argc < 6 || argc > 7)
{
BUG("ERROR: invalid usage: device_uri job-id user title copies options [file]\n");
exit (1);
}
if (argc == 6)
{
fd = 0; /* use stdin. */
copies = 1;
}
else
{
if ((fd = open(argv[6], O_RDONLY)) < 0) /* use specified file */
{
BUG("ERROR: unable to open print file %s: %m\n", argv[6]);
exit (1);
}
copies = atoi(argv[4]);
}
iLogLevel = getHPLogLevel();
if(SAVE_OUT_FILE_IN_BACKEND & iLogLevel)
{
temp_fp = create_out_file(argv[1], argv[2]);
if(temp_fp)
saveoutfile = 1;
}
if( DONT_SEND_TO_PRINTER & iLogLevel )
{
if(temp_fp)
{
saveoutfile = 1;
save_out_file(fd, copies, temp_fp);
}
exit (BACKEND_OK);
}
signal(SIGTERM, SIG_IGN);
init_dbus();
/* Get any parameters needed for DeviceOpen. */
hpmud_query_model(argv[0], &ma);
DBG("job start %s prt_mode=%d statustype=%d\n", argv[0], ma.prt_mode, ma.statustype);
pa.pjl_device = 0;
if (strcasestr(argv[0], ":/net") == NULL && (ma.statustype==HPMUD_STATUSTYPE_PJL || ma.statustype==HPMUD_STATUSTYPE_PJLPML))
pa.pjl_device = 1;
device_event(argv[0], printer, EVENT_START_JOB, argv[2], argv[1], argv[3]);
/* Write print file. */
while (copies > 0)
{
copies--;
if (fd != 0)
{
fputs("PAGE: 1 1\n", stderr);
lseek(fd, 0, SEEK_SET);
}
while ((len = read(fd, buf, sizeof(buf))) > 0)
{
size=len;
total=0;
while (size > 0)
{
if (saveoutfile)
fwrite (buf, 1, len, temp_fp);
/* Got some data now open the hp device. This will handle any HPIJS device contention. */
if (hd <= 0)
{
fputs("STATE: +connecting-to-device\n", stderr);
/* Open hp device. */
while ((stat = hpmud_open_device(argv[0], ma.prt_mode, &hd)) != HPMUD_R_OK)
{
if (getenv("CLASS") != NULL)
{
/* The job was submitted to a class and not a specific queue. Abort to
* give another class member a chance to print the job.
*/
BUG("INFO: open device failed stat=%d: %s; trying next printer in class...\n", stat, argv[0]);
sleep (5); /* Prevent job requeuing too quickly. */
goto bugout;
}
/* Display user error. */
device_event(argv[0], printer, 5000+stat, argv[2], argv[1], argv[3]);
BUG("INFO: open device failed stat=%d: %s; will retry in %d seconds...\n", stat, argv[0], RETRY_TIMEOUT);
sleep(RETRY_TIMEOUT);
retry = 1;
}
if (retry)
{
/* Clear user error. */
device_event(argv[0], printer, VSTATUS_PRNT, argv[2], argv[1], argv[3]);
retry=0;
}
while ((stat = hpmud_open_channel(hd, HPMUD_S_PRINT_CHANNEL, &cd)) != HPMUD_R_OK)
{
if (stat != HPMUD_R_DEVICE_BUSY)
{
BUG("ERROR: cannot open channel %s\n", HPMUD_S_PRINT_CHANNEL);
goto bugout;
}
device_event(argv[0], printer, 5000+stat, argv[2], argv[1], argv[3]);
BUG("INFO: open print channel failed stat=%d; will retry in %d seconds...\n", stat, RETRY_TIMEOUT);
sleep(RETRY_TIMEOUT);
retry = 1;
}
if (retry)
{
/* Clear user error. */
device_event(argv[0], printer, VSTATUS_PRNT, argv[2], argv[1], argv[3]);
retry=0;
}
fputs("STATE: -connecting-to-device\n", stderr);
if (pa.pjl_device)
{
/* Enable unsolicited status. */
hpmud_write_channel(hd, cd, pjl_ustatus_cmd, sizeof(pjl_ustatus_cmd)-1, 5, &len);
pa.dd = hd;
pa.cd = cd;
pthread_mutex_init(&pa.mutex, NULL);
pthread_cond_init(&pa.done_cond, NULL);
pthread_create(&pa.tid, NULL, (void *(*)(void*))pjl_read_thread, (void *)&pa);
}
/* Clear any errors left over from a previous job. */
fprintf(stderr, "STATE: -%s\n", "media-empty-error,media-jam-error,hplip.plugin-error,"
"cover-open-error,toner-empty-error,other");
} /* if (hd <= 0) */
stat = hpmud_write_channel(hd, cd, buf+total, size, EXCEPTION_TIMEOUT, &n);
if (n != size)
{
/* IO error, get printer status. */
if (loop_test(hd, cd, &pa, argv[0], printer, argv[2], argv[1], argv[3]))
{
exit_stat = BACKEND_STOP; /* stop queue */
goto bugout;
}
}
else
{
/* Data was sent to device successfully. */
if (pa.pjl_device)
{
/* Laserjets have a large data buffer, so manually check for operator intervention condition. */
if (loop_test(hd, cd, &pa, argv[0], printer, argv[2], argv[1], argv[3]))
{
exit_stat = BACKEND_STOP; /* stop queue */
goto bugout;
}
}
}
total+=n;
size-=n;
} /* while (size > 0) */
} /* while ((len = read(fd, buf, HPLIP_BUFFER_SIZE)) > 0) */
} /* while (copies > 0) */
DBG("job end %s prt_mode=%d statustype=%d total=%d\n", argv[0], ma.prt_mode, ma.statustype, total);
/* Note read() could return zero bytes. Check for bogus null print job. */
if (total==0)
{
exit_stat = BACKEND_OK; /* leave queue up */
BUG("ERROR: null print job total=%d\n", total);
goto bugout;
}
if (pa.pjl_device && pa.tid)
{
pthread_mutex_lock(&pa.mutex);
pa.eoj_pages=0;
pthread_mutex_unlock(&pa.mutex);
hpmud_write_channel(hd, cd, pjl_job_end_cmd, sizeof(pjl_job_end_cmd)-1, 5, &len);
/* Look for job end status. */
for (cnt=0; cnt<10; cnt++)
{
if (loop_test(hd, cd, &pa, argv[0], printer, argv[2], argv[1], argv[3]))
{
exit_stat = BACKEND_OK; /* leave queue up */
goto bugout;
}
pthread_mutex_lock(&pa.mutex);
pages = pa.eoj_pages;
pthread_mutex_unlock(&pa.mutex);
if (pages > 0)
{
DBG("job end pages=%d\n", pages);
break;
}
DBG("waiting for job end status...\n");
sleep(2);
}
hpmud_write_channel(hd, cd, pjl_ustatus_off_cmd, sizeof(pjl_ustatus_off_cmd)-1, 5, &len);
}
else if ((ma.prt_mode != HPMUD_UNI_MODE) && (ma.statustype == HPMUD_STATUSTYPE_SFIELD))
{
/* Wait for printer to receive all data before closing print channel. Otherwise data can be truncated. */
status = get_printer_status(hd, cd, &pa);
if (status < 5000)
{
/* Got valid status, wait for idle. */
cnt=0;
while ((status != VSTATUS_IDLE) && (status < 5000) && (cnt < 5))
{
sleep(2);
status = get_printer_status(hd, cd, &pa);
cnt++;
}
}
}
else
{
/* Just use fixed delay for uni-di, VSTATUS devices and laserjets without pjl. */
sleep(8);
}
exit_stat = BACKEND_OK;
fputs("INFO: ready to print\n", stderr);
bugout:
device_event(argv[0], printer, EVENT_END_JOB, argv[2], argv[1], argv[3]);
if (pa.pjl_device && pa.tid)
{
/* Gracefully kill the pjl_read_thread. */
pthread_mutex_lock(&pa.mutex);
pa.abort=1;
while (!pa.done)
pthread_cond_wait(&pa.done_cond, &pa.mutex);
pthread_mutex_unlock(&pa.mutex);
pthread_cancel(pa.tid);
pthread_mutex_destroy(&pa.mutex);
pthread_cond_destroy(&pa.done_cond);
}
if (cd >= 0)
hpmud_close_channel(hd, cd);
if (hd >= 0)
hpmud_close_device(hd);
if (fd != 0)
close(fd);
if (temp_fp)
fclose(temp_fp);
exit (exit_stat);
}
|