2 * Simulated client test program for CUPS.
4 * Copyright 2017 by Apple Inc.
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
10 * missing or damaged, see the license at "http://www.cups.org/".
12 * This file is subject to the Apple OS-Developed Software exception.
16 * Include necessary headers...
21 #include <cups/cups.h>
22 #include <cups/raster.h>
23 #include <cups/string-private.h>
24 #include <cups/thread-private.h>
31 typedef struct _client_monitor_s
33 const char *uri
, /* Printer URI */
34 *hostname
, /* Hostname */
36 *resource
; /* Resource path */
37 int port
; /* Port number */
38 http_encryption_t encryption
; /* Use encryption? */
39 ipp_pstate_t printer_state
; /* Current printer state */
40 char printer_state_reasons
[1024];
41 /* Current printer-state-reasons */
42 int job_id
; /* Job ID for submitted job */
43 ipp_jstate_t job_state
; /* Current job state */
44 char job_state_reasons
[1024];
45 /* Current job-state-reasons */
53 static const char *make_raster_file(ipp_t
*response
, char *tempname
, size_t tempsize
, const char **format
);
54 static void *monitor_printer(_client_monitor_t
*monitor
);
55 static void show_capabilities(ipp_t
*response
);
56 static void usage(void);
60 * 'main()' - Main entry.
63 int /* O - Exit status */
64 main(int argc
, /* I - Number of command-line arguments */
65 char *argv
[]) /* I - Command-line arguments */
67 int i
; /* Looping var */
68 const char *opt
, /* Current option */
69 *uri
= NULL
, /* Printer URI */
74 char tempfile
[1024] = "",
75 /* Temporary file (if any) */
76 scheme
[32], /* URI scheme */
77 userpass
[256], /* Username:password */
78 hostname
[256], /* Hostname */
79 resource
[256]; /* Resource path */
80 int port
; /* Port number */
81 http_encryption_t encryption
; /* Encryption mode */
82 _client_monitor_t monitor
; /* Monitoring data */
83 http_t
*http
; /* HTTP connection */
84 ipp_t
*request
, /* IPP request */
85 *response
; /* IPP response */
86 ipp_attribute_t
*attr
; /* IPP attribute */
87 static const char * const pattrs
[] = /* Printer attributes we are interested in */
91 "printer-description",
98 * Parse command-line options...
101 for (i
= 1; i
< argc
; i
++)
103 if (argv
[i
][0] == '-')
105 for (opt
= argv
[i
] + 1; *opt
; opt
++)
109 case 'f' : /* -f print-file */
112 puts("Print file can only be specified once.");
120 puts("Expected print file after '-f'.");
129 printf("Unknown option '-%c'.\n", *opt
);
135 else if (uri
|| (strncmp(argv
[i
], "ipp://", 6) && strncmp(argv
[i
], "ipps://", 7)))
137 printf("Unknown command-line argument '%s'.\n", argv
[i
]);
146 * Make sure we have everything we need.
151 puts("Expected printer URI.");
157 * Connect to the printer...
160 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
162 printf("Bad printer URI '%s'.\n", uri
);
169 if (!strcmp(scheme
, "https") || !strcmp(scheme
, "ipps"))
170 encryption
= HTTP_ENCRYPTION_ALWAYS
;
172 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
174 if ((http
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 0, NULL
)) == NULL
)
176 printf("Unable to connect to '%s' on port %d: %s\n", hostname
, port
, cupsLastErrorString());
181 * Query printer status and capabilities...
184 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
185 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
186 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
187 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
189 response
= cupsDoRequest(http
, request
, resource
);
191 show_capabilities(response
);
194 * Now figure out what we will be printing...
200 * User specified a print file, figure out the format...
203 if ((opt
= strrchr(printfile
, '.')) != NULL
)
206 * Guess the format from the extension...
209 if (!strcmp(opt
, ".jpg"))
210 printformat
= "image/jpeg";
211 else if (!strcmp(opt
, ".pdf"))
212 printformat
= "application/pdf";
213 else if (!strcmp(opt
, ".ps"))
214 printformat
= "application/postscript";
215 else if (!strcmp(opt
, ".pwg"))
216 printformat
= "image/pwg-raster";
217 else if (!strcmp(opt
, ".urf"))
218 printformat
= "image/urf";
220 printformat
= "application/octet-stream";
225 * Tell the printer to auto-detect...
228 printformat
= "application/octet-stream";
234 * No file specified, make something to test with...
237 if ((printfile
= make_raster_file(response
, tempfile
, sizeof(tempfile
), &printformat
)) == NULL
)
244 * Start monitoring the printer in the background...
247 memset(&monitor
, 0, sizeof(monitor
));
250 monitor
.hostname
= hostname
;
251 monitor
.resource
= resource
;
253 monitor
.encryption
= encryption
;
255 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
258 * Create the job and wait for completion...
261 request
= ippNewRequest(IPP_OP_CREATE_JOB
);
262 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
263 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
265 if ((opt
= strrchr(printfile
, '/')) != NULL
)
270 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
, opt
);
272 response
= cupsDoRequest(http
, request
, resource
);
274 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
276 printf("Unable to create print job: %s\n", cupsLastErrorString());
278 monitor
.job_state
= IPP_JSTATE_ABORTED
;
283 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
285 puts("No job-id returned in Create-Job request.");
287 monitor
.job_state
= IPP_JSTATE_ABORTED
;
292 monitor
.job_id
= ippGetInteger(attr
, 0);
296 request
= ippNewRequest(IPP_OP_SEND_DOCUMENT
);
297 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
298 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", monitor
.job_id
);
299 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
300 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format", NULL
, printformat
);
301 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
303 response
= cupsDoFileRequest(http
, request
, resource
, printfile
);
305 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
307 printf("Unable to print file: %s\n", cupsLastErrorString());
309 monitor
.job_state
= IPP_JSTATE_ABORTED
;
314 while (monitor
.job_state
< IPP_JSTATE_CANCELED
)
318 * Cleanup after ourselves...
328 return (monitor
.job_state
== IPP_JSTATE_COMPLETED
);
333 * 'make_raster_file()' - Create a temporary raster file.
336 static const char * /* O - Print filename */
337 make_raster_file(ipp_t
*response
, /* I - Printer attributes */
338 char *tempname
, /* I - Temporary filename buffer */
339 size_t tempsize
, /* I - Size of temp file buffer */
340 const char **format
) /* O - Print format */
342 int i
, /* Looping var */
343 count
; /* Number of values */
344 ipp_attribute_t
*attr
; /* Printer attribute */
345 const char *type
= NULL
; /* Raster type (colorspace + bits) */
346 pwg_media_t
*media
= NULL
; /* Media size */
347 int xdpi
= 0, /* Horizontal resolution */
348 ydpi
= 0; /* Vertical resolution */
349 int fd
; /* Temporary file */
350 cups_mode_t mode
; /* Raster mode */
351 cups_raster_t
*ras
; /* Raster stream */
352 cups_page_header2_t header
; /* Page header */
353 unsigned char *line
, /* Line of raster data */
354 *lineptr
; /* Pointer into line */
355 unsigned y
, /* Current position on page */
356 xcount
, ycount
, /* Current count for X and Y */
357 xrep
, yrep
, /* Repeat count for X and Y */
358 xoff
, yoff
, /* Offsets for X and Y */
359 yend
; /* End Y value */
360 int temprow
, /* Row in template */
361 tempcolor
; /* Template color */
362 const char *template; /* Pointer into template */
363 const unsigned char *color
; /* Current color */
364 static const unsigned char colors
[][3] =
365 { /* Colors for test */
382 static const char * const templates
[] =
383 { /* Raster template */
384 " CCC U U PPPP SSS TTTTT EEEEE SSS TTTTT 000 1 222 333 4 55555 66 77777 888 999 ",
385 "C C U U P P S S T E S S T 0 0 11 2 2 3 3 4 4 5 6 7 8 8 9 9 ",
386 "C U U P P S T E S T 0 0 1 2 3 4 4 5 6 7 8 8 9 9 ",
387 "C U U PPPP SSS ----- T EEEE SSS T 0 0 0 1 22 333 44444 555 6666 7 888 9999 ",
388 "C U U P S T E S T 0 0 1 2 3 4 5 6 6 7 8 8 9 ",
389 "C C U U P S S T E S S T 0 0 1 2 3 3 4 5 5 6 6 7 8 8 9 ",
390 " CCC UUU P SSS T EEEEE SSS T 000 111 22222 333 4 555 666 7 888 99 ",
396 * Figure out the output format...
399 if ((attr
= ippFindAttribute(response
, "document-format-supported", IPP_TAG_MIMETYPE
)) == NULL
)
401 puts("No supported document formats, aborting.");
405 if (ippContainsString(attr
, "image/urf"))
408 * Apple Raster format...
411 *format
= "image/urf";
412 mode
= CUPS_RASTER_WRITE_APPLE
;
414 else if (ippContainsString(attr
, "image/pwg-raster"))
417 * PWG Raster format...
420 *format
= "image/pwg-raster";
421 mode
= CUPS_RASTER_WRITE_PWG
;
426 * No supported raster format...
429 puts("Printer does not support Apple or PWG raster files, aborting.");
434 * Figure out the the media, resolution, and color mode...
437 if ((attr
= ippFindAttribute(response
, "media-default", IPP_TAG_KEYWORD
)) != NULL
)
440 * Use default media...
443 media
= pwgMediaForPWG(ippGetString(attr
, 0, NULL
));
445 else if ((attr
= ippFindAttribute(response
, "media-ready", IPP_TAG_KEYWORD
)) != NULL
)
451 if (ippContainsString(attr
, "na_letter_8.5x11in"))
452 media
= pwgMediaForPWG("na_letter_8.5x11in");
453 else if (ippContainsString(attr
, "iso_a4_210x297mm"))
454 media
= pwgMediaForPWG("iso_a4_210x297mm");
456 media
= pwgMediaForPWG(ippGetString(attr
, 0, NULL
));
460 puts("No default or ready media reported by printer, aborting.");
464 if ((attr
= ippFindAttribute(response
, "urf-supported", IPP_TAG_KEYWORD
)) != NULL
)
466 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
468 const char *val
= ippGetString(attr
, i
, NULL
);
471 xdpi
= ydpi
= atoi(val
+ 1);
472 else if (!strcmp(val
, "W8") && !type
)
474 else if (!strcmp(val
, "SRGB24"))
478 else if ((attr
= ippFindAttribute(response
, "pwg-raster-document-resolution-supported", IPP_TAG_RESOLUTION
)) != NULL
)
480 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
482 int tempxdpi
, tempydpi
;
485 tempxdpi
= ippGetResolution(attr
, 0, &tempydpi
, &tempunits
);
487 if (i
== 0 || tempxdpi
< xdpi
|| tempydpi
< ydpi
)
494 if ((attr
= ippFindAttribute(response
, "pwg-raster-document-type-supported", IPP_TAG_KEYWORD
)) != NULL
)
496 if (ippContainsString(attr
, "srgb_8"))
498 else if (ippContainsString(attr
, "sgray_8"))
503 if (xdpi
< 72 || ydpi
< 72)
505 puts("No supported raster resolutions, aborting.");
511 puts("No supported color spaces or bit depths, aborting.");
516 * Make the raster context and details...
519 if (!cupsRasterInitPWGHeader(&header
, media
, type
, xdpi
, ydpi
, "one-sided", NULL
))
521 printf("Unable to initialize raster context: %s\n", cupsRasterErrorString());
525 header
.cupsInteger
[CUPS_RASTER_PWG_TotalPageCount
] = 1;
527 if (header
.cupsWidth
> (4 * header
.HWResolution
[0]))
529 xoff
= header
.HWResolution
[0] / 2;
530 yoff
= header
.HWResolution
[1] / 2;
538 xrep
= (header
.cupsWidth
- 2 * xoff
) / 140;
539 yrep
= xrep
* header
.HWResolution
[1] / header
.HWResolution
[0];
540 yend
= header
.cupsHeight
- yoff
;
543 * Prepare the raster file...
546 if ((line
= malloc(header
.cupsBytesPerLine
)) == NULL
)
548 printf("Unable to allocate %u bytes for raster output: %s\n", header
.cupsBytesPerLine
, strerror(errno
));
552 if ((fd
= cupsTempFd(tempname
, (int)tempsize
)) < 0)
554 printf("Unable to create temporary print file: %s\n", strerror(errno
));
558 if ((ras
= cupsRasterOpen(fd
, mode
)) == NULL
)
560 printf("Unable to open raster stream: %s\n", cupsRasterErrorString());
566 * Write a single page consisting of the template dots repeated over the page.
569 cupsRasterWriteHeader2(ras
, &header
);
571 memset(line
, 0xff, header
.cupsBytesPerLine
);
573 for (y
= 0; y
< yoff
; y
++)
574 cupsRasterWritePixels(ras
, line
, header
.cupsBytesPerLine
);
576 for (temprow
= 0, tempcolor
= 0; y
< yend
; y
++)
578 template = templates
[temprow
];
579 color
= colors
[tempcolor
];
582 if (temprow
>= (int)(sizeof(templates
) / sizeof(templates
[0])))
586 if (tempcolor
>= (int)(sizeof(colors
) / sizeof(colors
[0])))
590 memset(line
, 0xff, header
.cupsBytesPerLine
);
592 if (header
.cupsColorSpace
== CUPS_CSPACE_SW
)
595 * Do grayscale output...
598 for (lineptr
= line
+ xoff
; *template; template ++)
600 if (*template != ' ')
602 for (xcount
= xrep
; xcount
> 0; xcount
--)
617 for (lineptr
= line
+ 3 * xoff
; *template; template ++)
619 if (*template != ' ')
621 for (xcount
= xrep
; xcount
> 0; xcount
--, lineptr
+= 3)
622 memcpy(lineptr
, color
, 3);
631 for (ycount
= yrep
; ycount
> 0 && y
< yend
; ycount
--, y
++)
632 cupsRasterWritePixels(ras
, line
, header
.cupsBytesPerLine
);
635 memset(line
, 0xff, header
.cupsBytesPerLine
);
637 for (y
= 0; y
< header
.cupsHeight
; y
++)
638 cupsRasterWritePixels(ras
, line
, header
.cupsBytesPerLine
);
640 cupsRasterClose(ras
);
644 printf("PRINT FILE: %s\n", tempname
);
651 * 'monitor_printer()' - Monitor the job and printer states.
654 static void * /* O - Thread exit code */
656 _client_monitor_t
*monitor
) /* I - Monitoring data */
658 http_t
*http
; /* Connection to printer */
659 ipp_t
*request
, /* IPP request */
660 *response
; /* IPP response */
661 ipp_attribute_t
*attr
; /* Attribute in response */
662 ipp_pstate_t printer_state
; /* Printer state */
663 char printer_state_reasons
[1024];
664 /* Printer state reasons */
665 ipp_jstate_t job_state
; /* Job state */
666 char job_state_reasons
[1024];/* Printer state reasons */
667 static const char * const jattrs
[] = /* Job attributes we want */
672 static const char * const pattrs
[] = /* Printer attributes we want */
675 "printer-state-reasons"
680 * Open a connection to the printer...
683 http
= httpConnect2(monitor
->hostname
, monitor
->port
, NULL
, AF_UNSPEC
, monitor
->encryption
, 1, 0, NULL
);
686 * Loop until the job is canceled, aborted, or completed.
689 printer_state
= (ipp_pstate_t
)0;
690 printer_state_reasons
[0] = '\0';
692 job_state
= (ipp_jstate_t
)0;
693 job_state_reasons
[0] = '\0';
695 while (monitor
->job_state
< IPP_JSTATE_CANCELED
)
698 * Reconnect to the printer as needed...
701 if (httpGetFd(http
) < 0)
702 httpReconnect2(http
, 30000, NULL
);
704 if (httpGetFd(http
) >= 0)
707 * Connected, so check on the printer state...
710 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
711 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, monitor
->uri
);
712 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
713 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
715 response
= cupsDoRequest(http
, request
, monitor
->resource
);
717 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
718 printer_state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
720 if ((attr
= ippFindAttribute(response
, "printer-state-reasons", IPP_TAG_KEYWORD
)) != NULL
)
721 ippAttributeString(attr
, printer_state_reasons
, sizeof(printer_state_reasons
));
723 if (printer_state
!= monitor
->printer_state
|| strcmp(printer_state_reasons
, monitor
->printer_state_reasons
))
725 printf("PRINTER: %s (%s)\n", ippEnumString("printer-state", printer_state
), printer_state_reasons
);
727 monitor
->printer_state
= printer_state
;
728 strlcpy(monitor
->printer_state_reasons
, printer_state_reasons
, sizeof(monitor
->printer_state_reasons
));
733 if (monitor
->job_id
> 0)
736 * Check the status of the job itself...
739 request
= ippNewRequest(IPP_OP_GET_JOB_ATTRIBUTES
);
740 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, monitor
->uri
);
741 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", monitor
->job_id
);
742 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
743 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
745 response
= cupsDoRequest(http
, request
, monitor
->resource
);
747 if ((attr
= ippFindAttribute(response
, "job-state", IPP_TAG_ENUM
)) != NULL
)
748 job_state
= (ipp_jstate_t
)ippGetInteger(attr
, 0);
750 if ((attr
= ippFindAttribute(response
, "job-state-reasons", IPP_TAG_KEYWORD
)) != NULL
)
751 ippAttributeString(attr
, job_state_reasons
, sizeof(job_state_reasons
));
753 if (job_state
!= monitor
->job_state
|| strcmp(job_state_reasons
, monitor
->job_state_reasons
))
755 printf("JOB %d: %s (%s)\n", monitor
->job_id
, ippEnumString("job-state", job_state
), job_state_reasons
);
757 monitor
->job_state
= job_state
;
758 strlcpy(monitor
->job_state_reasons
, job_state_reasons
, sizeof(monitor
->job_state_reasons
));
765 if (monitor
->job_state
< IPP_JSTATE_CANCELED
)
768 * Sleep for 5 seconds...
776 * Cleanup and return...
786 * 'show_capabilities()' - Show printer capabilities.
790 show_capabilities(ipp_t
*response
) /* I - Printer attributes */
792 int i
; /* Looping var */
793 ipp_attribute_t
*attr
; /* Attribute */
794 char buffer
[1024]; /* Attribute value buffer */
795 static const char * const pattrs
[] = /* Attributes we want to show */
799 "finishings-default",
801 "finishings-supported",
805 "output-bin-default",
806 "output-bin-supported",
807 "print-color-mode-default",
808 "print-color-mode-supported",
811 "document-format-default",
812 "document-format-supported",
813 "pwg-raster-document-resolution-supported",
814 "pwg-raster-document-type-supported",
819 puts("CAPABILITIES:");
820 for (i
= 0; i
< (int)(sizeof(pattrs
) / sizeof(pattrs
[0])); i
++)
822 if ((attr
= ippFindAttribute(response
, pattrs
[i
], IPP_TAG_ZERO
)) != NULL
)
824 ippAttributeString(attr
, buffer
, sizeof(buffer
));
825 printf(" %s=%s\n", pattrs
[i
], buffer
);
832 * 'usage()' - Show program usage...
838 puts("Usage: ./testclient printer-uri [-f print-file]");