4 * Scheduler notification tester for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
20 #include <cups/cups.h>
21 #include <cups/debug-private.h>
22 #include <cups/string-private.h>
24 #include <cups/ipp-private.h> /* TODO: Update so we don't need this */
31 static int terminate
= 0;
38 static void print_attributes(ipp_t
*ipp
, int indent
);
39 static void sigterm_handler(int sig
);
40 static void usage(void) __attribute__((noreturn
));
44 * 'main()' - Subscribe to the .
48 main(int argc
, /* I - Number of command-line arguments */
49 char *argv
[]) /* I - Command-line arguments */
51 int i
; /* Looping var */
52 const char *uri
; /* URI to use */
53 int num_events
; /* Number of events */
54 const char *events
[100]; /* Events */
55 int subscription_id
, /* notify-subscription-id */
56 sequence_number
, /* notify-sequence-number */
57 interval
; /* Interval between polls */
58 http_t
*http
; /* HTTP connection */
59 ipp_t
*request
, /* IPP request */
60 *response
; /* IPP response */
61 ipp_attribute_t
*attr
; /* Current attribute */
62 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
63 struct sigaction action
; /* Actions for POSIX signals */
64 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
68 * Parse command-line...
74 for (i
= 1; i
< argc
; i
++)
75 if (!strcmp(argv
[i
], "-E"))
76 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
77 else if (!strcmp(argv
[i
], "-e"))
80 if (i
>= argc
|| num_events
>= 100)
83 events
[num_events
] = argv
[i
];
86 else if (!strcmp(argv
[i
], "-h"))
92 cupsSetServer(argv
[i
]);
94 else if (uri
|| strncmp(argv
[i
], "ipp://", 6))
109 * Connect to the server...
112 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
113 cupsEncryption())) == NULL
)
115 perror(cupsServer());
120 * Catch CTRL-C and SIGTERM...
123 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
124 sigset(SIGINT
, sigterm_handler
);
125 sigset(SIGTERM
, sigterm_handler
);
126 #elif defined(HAVE_SIGACTION)
127 memset(&action
, 0, sizeof(action
));
129 sigemptyset(&action
.sa_mask
);
130 action
.sa_handler
= sigterm_handler
;
131 sigaction(SIGINT
, &action
, NULL
);
132 sigaction(SIGTERM
, &action
, NULL
);
134 signal(SIGINT
, sigterm_handler
);
135 signal(SIGTERM
, sigterm_handler
);
136 #endif /* HAVE_SIGSET */
139 * Create the subscription...
142 if (strstr(uri
, "/jobs/"))
144 request
= ippNewRequest(IPP_CREATE_JOB_SUBSCRIPTION
);
145 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
149 request
= ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION
);
150 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
154 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
157 ippAddStrings(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
, "notify-events",
158 num_events
, NULL
, events
);
159 ippAddString(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
160 "notify-pull-method", NULL
, "ippget");
162 response
= cupsDoRequest(http
, request
, uri
);
163 if (cupsLastError() >= IPP_BAD_REQUEST
)
165 fprintf(stderr
, "Create-%s-Subscription: %s\n",
166 strstr(uri
, "/jobs") ? "Job" : "Printer", cupsLastErrorString());
172 if ((attr
= ippFindAttribute(response
, "notify-subscription-id",
173 IPP_TAG_INTEGER
)) == NULL
)
175 fputs("ERROR: No notify-subscription-id in response!\n", stderr
);
181 subscription_id
= attr
->values
[0].integer
;
183 printf("Create-%s-Subscription: notify-subscription-id=%d\n",
184 strstr(uri
, "/jobs/") ? "Job" : "Printer", subscription_id
);
189 * Monitor for events...
197 * Get the current events...
200 printf("\nGet-Notifications(%d,%d):", subscription_id
, sequence_number
);
203 request
= ippNewRequest(IPP_GET_NOTIFICATIONS
);
205 if (strstr(uri
, "/jobs/"))
206 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
208 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
211 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
212 "requesting-user-name", NULL
, cupsUser());
214 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
215 "notify-subscription-ids", subscription_id
);
217 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
218 "notify-sequence-numbers", sequence_number
+ 1);
220 response
= cupsDoRequest(http
, request
, uri
);
222 printf(" %s\n", ippErrorString(cupsLastError()));
224 if (cupsLastError() >= IPP_BAD_REQUEST
)
225 fprintf(stderr
, "Get-Notifications: %s\n", cupsLastErrorString());
228 print_attributes(response
, 0);
230 for (attr
= ippFindAttribute(response
, "notify-sequence-number",
233 attr
= ippFindNextAttribute(response
, "notify-sequence-number",
235 if (attr
->values
[0].integer
> sequence_number
)
236 sequence_number
= attr
->values
[0].integer
;
239 if ((attr
= ippFindAttribute(response
, "notify-get-interval",
240 IPP_TAG_INTEGER
)) != NULL
&&
241 attr
->values
[0].integer
> 0)
242 interval
= attr
->values
[0].integer
;
247 sleep((unsigned)interval
);
251 * Cancel the subscription...
254 printf("\nCancel-Subscription:");
257 request
= ippNewRequest(IPP_CANCEL_SUBSCRIPTION
);
259 if (strstr(uri
, "/jobs/"))
260 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
262 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
265 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
268 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
269 "notify-subscription-id", subscription_id
);
271 ippDelete(cupsDoRequest(http
, request
, uri
));
273 printf(" %s\n", ippErrorString(cupsLastError()));
275 if (cupsLastError() >= IPP_BAD_REQUEST
)
276 fprintf(stderr
, "Cancel-Subscription: %s\n", cupsLastErrorString());
279 * Close the connection and return...
289 * 'print_attributes()' - Print the attributes in a request...
293 print_attributes(ipp_t
*ipp
, /* I - IPP request */
294 int indent
) /* I - Indentation */
296 int i
; /* Looping var */
297 ipp_tag_t group
; /* Current group */
298 ipp_attribute_t
*attr
; /* Current attribute */
299 _ipp_value_t
*val
; /* Current value */
300 static const char * const tags
[] = /* Value/group tag strings */
303 "operation-attributes-tag",
304 "job-attributes-tag",
305 "end-of-attributes-tag",
306 "printer-attributes-tag",
307 "unsupported-attributes-tag",
308 "subscription-attributes-tag",
309 "event-attributes-tag",
367 "textWithoutLanguage",
368 "nameWithoutLanguage",
380 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
382 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
384 group
= IPP_TAG_ZERO
;
389 if (group
!= attr
->group_tag
)
391 group
= attr
->group_tag
;
394 for (i
= 4; i
< indent
; i
++)
397 printf("%s:\n\n", tags
[group
]);
400 for (i
= 0; i
< indent
; i
++)
403 printf("%s (", attr
->name
);
404 if (attr
->num_values
> 1)
406 printf("%s):", tags
[attr
->value_tag
]);
408 switch (attr
->value_tag
)
411 case IPP_TAG_INTEGER
:
412 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
413 printf(" %d", val
->integer
);
417 case IPP_TAG_BOOLEAN
:
418 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
419 printf(" %s", val
->boolean
? "true" : "false");
424 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
425 printf(" %d-%d", val
->range
.lower
, val
->range
.upper
);
431 char vstring
[256]; /* Formatted time */
433 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
434 printf(" (%s)", _cupsStrDate(vstring
, sizeof(vstring
), ippDateToTime(val
->date
)));
439 case IPP_TAG_RESOLUTION
:
440 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
441 printf(" %dx%d%s", val
->resolution
.xres
, val
->resolution
.yres
,
442 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
446 case IPP_TAG_STRING
:
447 case IPP_TAG_TEXTLANG
:
448 case IPP_TAG_NAMELANG
:
451 case IPP_TAG_KEYWORD
:
453 case IPP_TAG_URISCHEME
:
454 case IPP_TAG_CHARSET
:
455 case IPP_TAG_LANGUAGE
:
456 case IPP_TAG_MIMETYPE
:
457 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
458 printf(" \"%s\"", val
->string
.text
);
462 case IPP_TAG_BEGIN_COLLECTION
:
465 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
469 print_attributes(val
->collection
, indent
+ 4);
474 printf("UNKNOWN (%d values)\n", attr
->num_values
);
482 * 'sigterm_handler()' - Flag when the user hits CTRL-C...
486 sigterm_handler(int sig
) /* I - Signal number (unused) */
495 * 'usage()' - Show program usage...
501 puts("Usage: testsub [-E] [-e event ... -e eventN] [-h hostname] URI");