4 * Scheduler notification tester for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Subscribe to the .
18 * print_attributes() - Print the attributes in a request...
19 * sigterm_handler() - Flag when the user hits CTRL-C...
20 * usage() - Show program usage...
24 * Include necessary headers...
27 #include <cups/cups.h>
28 #include <cups/debug-private.h>
29 #include <cups/string-private.h>
31 #include <cups/ipp-private.h> /* TODO: Update so we don't need this */
38 static int terminate
= 0;
45 static void print_attributes(ipp_t
*ipp
, int indent
);
46 static void sigterm_handler(int sig
);
47 static void usage(void) __attribute__((noreturn
));
51 * 'main()' - Subscribe to the .
55 main(int argc
, /* I - Number of command-line arguments */
56 char *argv
[]) /* I - Command-line arguments */
58 int i
; /* Looping var */
59 const char *uri
; /* URI to use */
60 int num_events
; /* Number of events */
61 const char *events
[100]; /* Events */
62 int subscription_id
, /* notify-subscription-id */
63 sequence_number
, /* notify-sequence-number */
64 interval
; /* Interval between polls */
65 http_t
*http
; /* HTTP connection */
66 ipp_t
*request
, /* IPP request */
67 *response
; /* IPP response */
68 ipp_attribute_t
*attr
; /* Current attribute */
69 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
70 struct sigaction action
; /* Actions for POSIX signals */
71 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
75 * Parse command-line...
81 for (i
= 1; i
< argc
; i
++)
82 if (!strcmp(argv
[i
], "-E"))
83 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
84 else if (!strcmp(argv
[i
], "-e"))
87 if (i
>= argc
|| num_events
>= 100)
90 events
[num_events
] = argv
[i
];
93 else if (!strcmp(argv
[i
], "-h"))
99 cupsSetServer(argv
[i
]);
101 else if (uri
|| strncmp(argv
[i
], "ipp://", 6))
116 * Connect to the server...
119 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
120 cupsEncryption())) == NULL
)
122 perror(cupsServer());
127 * Catch CTRL-C and SIGTERM...
130 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
131 sigset(SIGINT
, sigterm_handler
);
132 sigset(SIGTERM
, sigterm_handler
);
133 #elif defined(HAVE_SIGACTION)
134 memset(&action
, 0, sizeof(action
));
136 sigemptyset(&action
.sa_mask
);
137 action
.sa_handler
= sigterm_handler
;
138 sigaction(SIGINT
, &action
, NULL
);
139 sigaction(SIGTERM
, &action
, NULL
);
141 signal(SIGINT
, sigterm_handler
);
142 signal(SIGTERM
, sigterm_handler
);
143 #endif /* HAVE_SIGSET */
146 * Create the subscription...
149 if (strstr(uri
, "/jobs/"))
151 request
= ippNewRequest(IPP_CREATE_JOB_SUBSCRIPTION
);
152 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
156 request
= ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION
);
157 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
161 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
164 ippAddStrings(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
, "notify-events",
165 num_events
, NULL
, events
);
166 ippAddString(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
167 "notify-pull-method", NULL
, "ippget");
169 response
= cupsDoRequest(http
, request
, uri
);
170 if (cupsLastError() >= IPP_BAD_REQUEST
)
172 fprintf(stderr
, "Create-%s-Subscription: %s\n",
173 strstr(uri
, "/jobs") ? "Job" : "Printer", cupsLastErrorString());
179 if ((attr
= ippFindAttribute(response
, "notify-subscription-id",
180 IPP_TAG_INTEGER
)) == NULL
)
182 fputs("ERROR: No notify-subscription-id in response!\n", stderr
);
188 subscription_id
= attr
->values
[0].integer
;
190 printf("Create-%s-Subscription: notify-subscription-id=%d\n",
191 strstr(uri
, "/jobs/") ? "Job" : "Printer", subscription_id
);
196 * Monitor for events...
204 * Get the current events...
207 printf("\nGet-Notifications(%d,%d):", subscription_id
, sequence_number
);
210 request
= ippNewRequest(IPP_GET_NOTIFICATIONS
);
212 if (strstr(uri
, "/jobs/"))
213 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
215 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
218 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
219 "requesting-user-name", NULL
, cupsUser());
221 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
222 "notify-subscription-ids", subscription_id
);
224 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
225 "notify-sequence-numbers", sequence_number
+ 1);
227 response
= cupsDoRequest(http
, request
, uri
);
229 printf(" %s\n", ippErrorString(cupsLastError()));
231 if (cupsLastError() >= IPP_BAD_REQUEST
)
232 fprintf(stderr
, "Get-Notifications: %s\n", cupsLastErrorString());
235 print_attributes(response
, 0);
237 for (attr
= ippFindAttribute(response
, "notify-sequence-number",
240 attr
= ippFindNextAttribute(response
, "notify-sequence-number",
242 if (attr
->values
[0].integer
> sequence_number
)
243 sequence_number
= attr
->values
[0].integer
;
246 if ((attr
= ippFindAttribute(response
, "notify-get-interval",
247 IPP_TAG_INTEGER
)) != NULL
&&
248 attr
->values
[0].integer
> 0)
249 interval
= attr
->values
[0].integer
;
258 * Cancel the subscription...
261 printf("\nCancel-Subscription:");
264 request
= ippNewRequest(IPP_CANCEL_SUBSCRIPTION
);
266 if (strstr(uri
, "/jobs/"))
267 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
269 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
272 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
275 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
276 "notify-subscription-id", subscription_id
);
278 ippDelete(cupsDoRequest(http
, request
, uri
));
280 printf(" %s\n", ippErrorString(cupsLastError()));
282 if (cupsLastError() >= IPP_BAD_REQUEST
)
283 fprintf(stderr
, "Cancel-Subscription: %s\n", cupsLastErrorString());
286 * Close the connection and return...
296 * 'print_attributes()' - Print the attributes in a request...
300 print_attributes(ipp_t
*ipp
, /* I - IPP request */
301 int indent
) /* I - Indentation */
303 int i
; /* Looping var */
304 ipp_tag_t group
; /* Current group */
305 ipp_attribute_t
*attr
; /* Current attribute */
306 _ipp_value_t
*val
; /* Current value */
307 static const char * const tags
[] = /* Value/group tag strings */
310 "operation-attributes-tag",
311 "job-attributes-tag",
312 "end-of-attributes-tag",
313 "printer-attributes-tag",
314 "unsupported-attributes-tag",
315 "subscription-attributes-tag",
316 "event-attributes-tag",
374 "textWithoutLanguage",
375 "nameWithoutLanguage",
387 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
389 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
391 group
= IPP_TAG_ZERO
;
396 if (group
!= attr
->group_tag
)
398 group
= attr
->group_tag
;
401 for (i
= 4; i
< indent
; i
++)
404 printf("%s:\n\n", tags
[group
]);
407 for (i
= 0; i
< indent
; i
++)
410 printf("%s (", attr
->name
);
411 if (attr
->num_values
> 1)
413 printf("%s):", tags
[attr
->value_tag
]);
415 switch (attr
->value_tag
)
418 case IPP_TAG_INTEGER
:
419 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
420 printf(" %d", val
->integer
);
424 case IPP_TAG_BOOLEAN
:
425 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
426 printf(" %s", val
->boolean
? "true" : "false");
431 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
432 printf(" %d-%d", val
->range
.lower
, val
->range
.upper
);
438 time_t vtime
; /* Date/Time value */
439 struct tm
*vdate
; /* Date info */
440 char vstring
[256]; /* Formatted time */
442 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
444 vtime
= ippDateToTime(val
->date
);
445 vdate
= localtime(&vtime
);
446 strftime(vstring
, sizeof(vstring
), "%c", vdate
);
447 printf(" (%s)", vstring
);
453 case IPP_TAG_RESOLUTION
:
454 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
455 printf(" %dx%d%s", val
->resolution
.xres
, val
->resolution
.yres
,
456 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
460 case IPP_TAG_STRING
:
461 case IPP_TAG_TEXTLANG
:
462 case IPP_TAG_NAMELANG
:
465 case IPP_TAG_KEYWORD
:
467 case IPP_TAG_URISCHEME
:
468 case IPP_TAG_CHARSET
:
469 case IPP_TAG_LANGUAGE
:
470 case IPP_TAG_MIMETYPE
:
471 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
472 printf(" \"%s\"", val
->string
.text
);
476 case IPP_TAG_BEGIN_COLLECTION
:
479 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
483 print_attributes(val
->collection
, indent
+ 4);
488 printf("UNKNOWN (%d values)\n", attr
->num_values
);
496 * 'sigterm_handler()' - Flag when the user hits CTRL-C...
500 sigterm_handler(int sig
) /* I - Signal number (unused) */
509 * 'usage()' - Show program usage...
515 puts("Usage: testsub [-E] [-e event ... -e eventN] [-h hostname] URI");