2 * Scheduler notification tester for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 2006-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include <cups/cups.h>
19 #include <cups/debug-private.h>
20 #include <cups/string-private.h>
22 #include <cups/ipp-private.h> /* TODO: Update so we don't need this */
29 static int terminate
= 0;
36 static void print_attributes(ipp_t
*ipp
, int indent
);
37 static void sigterm_handler(int sig
);
38 static void usage(void) __attribute__((noreturn
));
42 * 'main()' - Subscribe to the .
46 main(int argc
, /* I - Number of command-line arguments */
47 char *argv
[]) /* I - Command-line arguments */
49 int i
; /* Looping var */
50 const char *uri
; /* URI to use */
51 int num_events
; /* Number of events */
52 const char *events
[100]; /* Events */
53 int subscription_id
, /* notify-subscription-id */
54 sequence_number
, /* notify-sequence-number */
55 interval
; /* Interval between polls */
56 http_t
*http
; /* HTTP connection */
57 ipp_t
*request
, /* IPP request */
58 *response
; /* IPP response */
59 ipp_attribute_t
*attr
; /* Current attribute */
60 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
61 struct sigaction action
; /* Actions for POSIX signals */
62 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
66 * Parse command-line...
72 for (i
= 1; i
< argc
; i
++)
73 if (!strcmp(argv
[i
], "-E"))
74 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
75 else if (!strcmp(argv
[i
], "-e"))
78 if (i
>= argc
|| num_events
>= 100)
81 events
[num_events
] = argv
[i
];
84 else if (!strcmp(argv
[i
], "-h"))
90 cupsSetServer(argv
[i
]);
92 else if (uri
|| strncmp(argv
[i
], "ipp://", 6))
107 * Connect to the server...
110 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
111 cupsEncryption())) == NULL
)
113 perror(cupsServer());
118 * Catch CTRL-C and SIGTERM...
121 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
122 sigset(SIGINT
, sigterm_handler
);
123 sigset(SIGTERM
, sigterm_handler
);
124 #elif defined(HAVE_SIGACTION)
125 memset(&action
, 0, sizeof(action
));
127 sigemptyset(&action
.sa_mask
);
128 action
.sa_handler
= sigterm_handler
;
129 sigaction(SIGINT
, &action
, NULL
);
130 sigaction(SIGTERM
, &action
, NULL
);
132 signal(SIGINT
, sigterm_handler
);
133 signal(SIGTERM
, sigterm_handler
);
134 #endif /* HAVE_SIGSET */
137 * Create the subscription...
140 if (strstr(uri
, "/jobs/"))
142 request
= ippNewRequest(IPP_CREATE_JOB_SUBSCRIPTION
);
143 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
147 request
= ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION
);
148 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
152 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
155 ippAddStrings(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
, "notify-events",
156 num_events
, NULL
, events
);
157 ippAddString(request
, IPP_TAG_SUBSCRIPTION
, IPP_TAG_KEYWORD
,
158 "notify-pull-method", NULL
, "ippget");
160 response
= cupsDoRequest(http
, request
, uri
);
161 if (cupsLastError() >= IPP_BAD_REQUEST
)
163 fprintf(stderr
, "Create-%s-Subscription: %s\n",
164 strstr(uri
, "/jobs") ? "Job" : "Printer", cupsLastErrorString());
170 if ((attr
= ippFindAttribute(response
, "notify-subscription-id",
171 IPP_TAG_INTEGER
)) == NULL
)
173 fputs("ERROR: No notify-subscription-id in response!\n", stderr
);
179 subscription_id
= attr
->values
[0].integer
;
181 printf("Create-%s-Subscription: notify-subscription-id=%d\n",
182 strstr(uri
, "/jobs/") ? "Job" : "Printer", subscription_id
);
187 * Monitor for events...
195 * Get the current events...
198 printf("\nGet-Notifications(%d,%d):", subscription_id
, sequence_number
);
201 request
= ippNewRequest(IPP_GET_NOTIFICATIONS
);
203 if (strstr(uri
, "/jobs/"))
204 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
206 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
209 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
210 "requesting-user-name", NULL
, cupsUser());
212 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
213 "notify-subscription-ids", subscription_id
);
215 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
216 "notify-sequence-numbers", sequence_number
+ 1);
218 response
= cupsDoRequest(http
, request
, uri
);
220 printf(" %s\n", ippErrorString(cupsLastError()));
222 if (cupsLastError() >= IPP_BAD_REQUEST
)
223 fprintf(stderr
, "Get-Notifications: %s\n", cupsLastErrorString());
226 print_attributes(response
, 0);
228 for (attr
= ippFindAttribute(response
, "notify-sequence-number",
231 attr
= ippFindNextAttribute(response
, "notify-sequence-number",
233 if (attr
->values
[0].integer
> sequence_number
)
234 sequence_number
= attr
->values
[0].integer
;
237 if ((attr
= ippFindAttribute(response
, "notify-get-interval",
238 IPP_TAG_INTEGER
)) != NULL
&&
239 attr
->values
[0].integer
> 0)
240 interval
= attr
->values
[0].integer
;
245 sleep((unsigned)interval
);
249 * Cancel the subscription...
252 printf("\nCancel-Subscription:");
255 request
= ippNewRequest(IPP_CANCEL_SUBSCRIPTION
);
257 if (strstr(uri
, "/jobs/"))
258 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
260 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
263 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
266 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
,
267 "notify-subscription-id", subscription_id
);
269 ippDelete(cupsDoRequest(http
, request
, uri
));
271 printf(" %s\n", ippErrorString(cupsLastError()));
273 if (cupsLastError() >= IPP_BAD_REQUEST
)
274 fprintf(stderr
, "Cancel-Subscription: %s\n", cupsLastErrorString());
277 * Close the connection and return...
287 * 'print_attributes()' - Print the attributes in a request...
291 print_attributes(ipp_t
*ipp
, /* I - IPP request */
292 int indent
) /* I - Indentation */
294 int i
; /* Looping var */
295 ipp_tag_t group
; /* Current group */
296 ipp_attribute_t
*attr
; /* Current attribute */
297 _ipp_value_t
*val
; /* Current value */
298 static const char * const tags
[] = /* Value/group tag strings */
301 "operation-attributes-tag",
302 "job-attributes-tag",
303 "end-of-attributes-tag",
304 "printer-attributes-tag",
305 "unsupported-attributes-tag",
306 "subscription-attributes-tag",
307 "event-attributes-tag",
365 "textWithoutLanguage",
366 "nameWithoutLanguage",
378 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
380 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
382 group
= IPP_TAG_ZERO
;
387 if (group
!= attr
->group_tag
)
389 group
= attr
->group_tag
;
392 for (i
= 4; i
< indent
; i
++)
395 printf("%s:\n\n", tags
[group
]);
398 for (i
= 0; i
< indent
; i
++)
401 printf("%s (", attr
->name
);
402 if (attr
->num_values
> 1)
404 printf("%s):", tags
[attr
->value_tag
]);
406 switch (attr
->value_tag
)
409 case IPP_TAG_INTEGER
:
410 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
411 printf(" %d", val
->integer
);
415 case IPP_TAG_BOOLEAN
:
416 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
417 printf(" %s", val
->boolean
? "true" : "false");
422 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
423 printf(" %d-%d", val
->range
.lower
, val
->range
.upper
);
429 char vstring
[256]; /* Formatted time */
431 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
432 printf(" (%s)", _cupsStrDate(vstring
, sizeof(vstring
), ippDateToTime(val
->date
)));
437 case IPP_TAG_RESOLUTION
:
438 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
439 printf(" %dx%d%s", val
->resolution
.xres
, val
->resolution
.yres
,
440 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpcm");
444 case IPP_TAG_STRING
:
445 case IPP_TAG_TEXTLANG
:
446 case IPP_TAG_NAMELANG
:
449 case IPP_TAG_KEYWORD
:
451 case IPP_TAG_URISCHEME
:
452 case IPP_TAG_CHARSET
:
453 case IPP_TAG_LANGUAGE
:
454 case IPP_TAG_MIMETYPE
:
455 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
456 printf(" \"%s\"", val
->string
.text
);
460 case IPP_TAG_BEGIN_COLLECTION
:
463 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
467 print_attributes(val
->collection
, indent
+ 4);
472 printf("UNKNOWN (%d values)\n", attr
->num_values
);
480 * 'sigterm_handler()' - Flag when the user hits CTRL-C...
484 sigterm_handler(int sig
) /* I - Signal number (unused) */
493 * 'usage()' - Show program usage...
499 puts("Usage: testsub [-E] [-e event ... -e eventN] [-h hostname] URI");