]> git.ipfire.org Git - thirdparty/cups.git/blame - doc/help/api-filter.html
Merge CUPS 1.4svn-r7588 (dependency updates, compiler warnings, and one web UI buglet)
[thirdparty/cups.git] / doc / help / api-filter.html
CommitLineData
ef416fc2 1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2<html>
3<!-- SECTION: Programming -->
4<head>
5a738aea
MS
5<title>Filter and Backend Programming</title>
6<meta name="keywords" content="Programming">
7<meta name="creator" content="Mini-XML v2.5">
8<style type="text/css"><!--
9BODY {
10 font-family: lucida grande, geneva, helvetica, arial, sans-serif;
11}
12
13H1, H2, H3, H4, H5, H6, P, TD, TH {
14 font-family: lucida grande, geneva, helvetica, arial, sans-serif;
15}
16
17KBD {
18 font-family: monaco, courier, monospace;
19 font-weight: bold;
20}
21
22PRE {
23 font-family: monaco, courier, monospace;
24}
25
26PRE.command {
27 margin-left: 36pt;
28}
29
30PRE.example {
31 background: #eeeeee;
32 border: dotted thin #999999;
33 margin-left: 36pt;
34 padding: 10px;
35}
36
37PRE.command EM, PRE.example EM {
38 font-family: lucida grande, geneva, helvetica, arial, sans-serif;
39}
40
41P.command {
42 font-family: monaco, courier, monospace;
43 margin-left: 36pt;
44}
45
46P.formula {
47 font-style: italic;
48 margin-left: 36pt;
49}
50
51BLOCKQUOTE {
52 background: #cccccc;
53 border: solid thin #999999;
54 padding: 10pt;
55}
56
57A:link, A:visited {
568fa3fa 58 font-weight: normal;
5a738aea 59 text-decoration: none;
5a738aea
MS
60}
61
62A:link:hover, A:visited:hover, A:active {
63 text-decoration: underline;
5a738aea
MS
64}
65
66SUB, SUP {
67 font-size: 50%;
68}
69
70DIV.table TABLE {
71 border: solid thin #999999;
72 border-collapse: collapse;
73 border-spacing: 0;
74 margin-left: auto;
75 margin-right: auto;
76}
77
78DIV.table CAPTION {
79 caption-side: top;
80 font-size: 120%;
81 font-style: italic;
82 font-weight: bold;
83 margin-left: auto;
84 margin-right: auto;
85}
86
87DIV.table TABLE TD {
88 border: solid thin #cccccc;
89 padding-top: 5pt;
90}
91
92DIV.table TABLE TH {
93 background: #cccccc;
94 border: none;
95 border-bottom: solid thin #999999;
96}
97
98DIV.figure TABLE {
99 margin-left: auto;
100 margin-right: auto;
101}
102
103DIV.figure CAPTION {
104 caption-side: bottom;
105 font-size: 120%;
106 font-style: italic;
107 font-weight: bold;
108 margin-left: auto;
109 margin-right: auto;
110}
111
112TH.label {
113 padding-top: 5pt;
114 text-align: right;
115 vertical-align: top;
116}
117
118HR {
119 border: solid thin;
120}
121
122SPAN.info {
123 background: #000000;
124 border: thin solid #000000;
125 color: #ffffff;
126 font-size: 80%;
127 font-style: italic;
128 font-weight: bold;
129 white-space: nowrap;
130}
131
132H2 SPAN.info, H3 SPAN.info, H4 SPAN.info {
133 float: right;
134 font-size: 100%;
135}
136
137H2.title, H3.title {
138 border-bottom: solid 2pt #000000;
139}
140
141DT {
142 margin-left: 36pt;
143 margin-top: 12pt;
144}
145
146DD {
147 margin-left: 54pt;
148}
149
150DL.category DT {
151 font-weight: bold;
152}
153
154P.summary {
155 margin-left: 36pt;
156 font-family: monaco, courier, monospace;
157}
158
159SPAN.message {
160 font-style: italic;
161 font-size: smaller;
162}
163
164DIV.summary TABLE {
165 border: solid thin #999999;
166 border-collapse: collapse;
167 border-spacing: 0;
168 margin: 10px;
169}
170
171DIV.summary TABLE TD, DIV.summary TABLE TH {
172 border: solid thin #999999;
173 padding: 5px;
174 text-align: left;
175 vertical-align: top;
176}
177
178DIV.summary TABLE THEAD TH {
179 background: #eeeeee;
180}
181
182/* API documentation styles... */
183div.body h1 {
184 margin: 0;
185}
186div.body h2 {
187 margin-top: 1.5em;
188}
189div.body h3, div.body h4, div.body h5 {
190 margin-bottom: 0.5em;
191 margin-top: 1.5em;
192}
193.class, .enumeration, .function, .struct, .typedef, .union {
194 border-bottom: solid thin #999999;
195 margin-bottom: 0;
196 margin-top: 2em;
197}
198.description {
199 margin-top: 0.5em;
200}
201code, p.code, pre, ul.code li {
202 font-family: monaco, courier, monospace;
203 font-size: 90%;
204}
205ul.code, ul.contents, ul.subcontents {
206 list-style-type: none;
207 margin: 0;
208 padding-left: 0;
209}
210ul.code li {
211 margin: 0;
212}
213ul.contents > li {
214 margin-top: 1em;
215}
216ul.contents li ul.code, ul.contents li ul.subcontents {
217 padding-left: 2em;
218}
219div.body dl {
220 margin-left: 0;
221 margin-top: 0;
222}
223div.body dt {
224 font-style: italic;
225 margin-left: 0;
226 margin-top: 0;
227}
228div.body dd {
229 margin-bottom: 0.5em;
230}
231
232/* This is just for the HTML files generated with the framedhelp target */
233div.contents {
234 background: #e8e8e8;
235 border: solid thin black;
236 padding: 10px;
237}
238div.contents h1 {
239 font-size: 110%;
240}
241div.contents h2 {
242 font-size: 100%;
243}
244div.contents ul.contents {
245 font-size: 80%;
246}
ac884b6a
MS
247div.contents ul.subcontents li {
248 margin-left: 1em;
249 text-indent: -1em;
250}
5a738aea 251--></style>
ef416fc2 252</head>
253<body>
5a738aea 254<div class='body'>
ef416fc2 255<!--
ac884b6a 256 "$Id: api-filter.header 7285 2008-02-01 23:57:39Z mike $"
ef416fc2 257
5a738aea
MS
258 Filter and backend programming header for the Common UNIX Printing System
259 (CUPS).
ef416fc2 260
5a738aea 261 Copyright 2008 by Apple Inc.
ef416fc2 262
263 These coded instructions, statements, and computer programs are the
bc44d920 264 property of Apple Inc. and are protected by Federal copyright
265 law. Distribution and use rights are outlined in the file "LICENSE.txt"
266 which should have been included with this file. If this file is
267 file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 268-->
269
5a738aea 270<h1 class="title">Filter and Backend Programming</h1>
ef416fc2 271
5a738aea
MS
272<div class='summary'><table summary='General Information'>
273<thead>
274<tr>
ac884b6a 275 <th>Headers</th>
5a738aea 276 <th>cups/backend.h<br>
ac884b6a
MS
277 cups/sidechannel.h<br>
278 cups/snmp.h</th>
5a738aea
MS
279</tr>
280</thead>
281<tbody>
282<tr>
283 <th>Library</th>
284 <td>-lcups</td>
285</tr>
286<tr>
287 <th>See Also</th>
288 <td>Programming: <a href='api-overview.html' target='_top'>Introduction to CUPS Programming</a><br>
289 Programming: <a href='api-cups.html' target='_top'>CUPS API</a><br>
290 Programming: <a href='api-ppd.html' target='_top'>PPD API</a><br>
291 Programming: <a href='api-raster.html' target='_top'>Raster API</a></td>
292</tr>
293</tbody>
294</table></div>
295<h2 class="title">Contents</h2>
296<ul class="contents">
297</li>
298<li><a href="#OVERVIEW">Overview</a><ul class="subcontents">
ac884b6a
MS
299<li><a href="#SECURITY">Security Considerations</a></li>
300<li><a href="#TEMPFILES">Temporary Files</a></li>
301<li><a href="#COPIES">Copy Generation</a></li>
5a738aea
MS
302<li><a href="#EXITCODES">Exit Codes</a></li>
303<li><a href="#ENVIRONMENT">Environment Variables</a></li>
304<li><a href="#MESSAGES">Communicating with the Scheduler</a></li>
305<li><a href="#COMMUNICATING">Communicating with the Backend</a></li>
ac884b6a 306<li><a href="#SNMP">Doing SNMP Queries with Network Printers</a></li>
5a738aea
MS
307</ul></li>
308<li><a href="#FUNCTIONS">Functions</a><ul class="code">
309<li><a href="#cupsBackChannelRead" title="Read data from the backchannel.">cupsBackChannelRead</a></li>
310<li><a href="#cupsBackChannelWrite" title="Write data to the backchannel.">cupsBackChannelWrite</a></li>
ac884b6a 311<li><a href="#cupsBackendDeviceURI" title="Get the device URI for a backend.">cupsBackendDeviceURI</a></li>
5a738aea
MS
312<li><a href="#cupsSideChannelDoRequest" title="Send a side-channel command to a backend and wait for a response.">cupsSideChannelDoRequest</a></li>
313<li><a href="#cupsSideChannelRead" title="Read a side-channel message.">cupsSideChannelRead</a></li>
314<li><a href="#cupsSideChannelWrite" title="Write a side-channel message.">cupsSideChannelWrite</a></li>
315</ul>
316<li><a href="#TYPES">Data Types</a><ul class="code">
317 <li><a href="#cups_backend_t" title="Backend exit codes">cups_backend_t</a></li>
318 <li><a href="#cups_sc_bidi_t" title="Bidirectional capabilities">cups_sc_bidi_t</a></li>
319 <li><a href="#cups_sc_command_t" title="Request command codes">cups_sc_command_t</a></li>
320 <li><a href="#cups_sc_state_t" title="Printer state bits">cups_sc_state_t</a></li>
321 <li><a href="#cups_sc_status_t" title="Response status codes">cups_sc_status_t</a></li>
322</ul></li>
323<li><a href="#ENUMERATIONS">Constants</a><ul class="code">
324 <li><a href="#cups_backend_e" title="Backend exit codes">cups_backend_e</a></li>
325 <li><a href="#cups_sc_bidi_e" title="Bidirectional capabilities">cups_sc_bidi_e</a></li>
326 <li><a href="#cups_sc_command_e" title="Request command codes">cups_sc_command_e</a></li>
327 <li><a href="#cups_sc_state_e" title="Printer state bits">cups_sc_state_e</a></li>
328 <li><a href="#cups_sc_status_e" title="Response status codes">cups_sc_status_e</a></li>
329</ul></li>
330</ul>
331<!--
ac884b6a 332 "$Id: api-filter.shtml 7288 2008-02-06 01:39:05Z mike $"
ef416fc2 333
5a738aea
MS
334 Filter and backend programming introduction for the Common UNIX Printing
335 System (CUPS).
ef416fc2 336
5a738aea
MS
337 Copyright 2007-2008 by Apple Inc.
338 Copyright 1997-2006 by Easy Software Products, all rights reserved.
ef416fc2 339
5a738aea
MS
340 These coded instructions, statements, and computer programs are the
341 property of Apple Inc. and are protected by Federal copyright
342 law. Distribution and use rights are outlined in the file "LICENSE.txt"
343 which should have been included with this file. If this file is
344 file is missing or damaged, see the license at "http://www.cups.org/".
345-->
f7deaa1a 346
5a738aea 347<h2 class='title'><a name="OVERVIEW">Overview</a></h2>
ef416fc2 348
5a738aea
MS
349<p>Filters, printer drivers, port monitors, and backends use a common interface
350for processing print jobs and communicating status information to the scheduler.
351Each filter is run with a standard set of command-line arguments:<p>
ef416fc2 352
5a738aea 353<dl class="code">
f7deaa1a 354
5a738aea
MS
355 <dt>argv[1]</dt>
356 <dd>The job ID</dd>
ef416fc2 357
5a738aea
MS
358 <dt>argv[2]</dt>
359 <dd>The user printing the job</dd>
f7deaa1a 360
5a738aea
MS
361 <dt>argv[3]</dt>
362 <dd>The job name/title</dd>
f7deaa1a 363
5a738aea
MS
364 <dt>argv[4]</dt>
365 <dd>The number of copies to print</dd>
f7deaa1a 366
5a738aea
MS
367 <dt>argv[5]</dt>
368 <dd>The options that were provided when the job was submitted</dd>
f7deaa1a 369
5a738aea
MS
370 <dt>argv[6]</dt>
371 <dd>The file to print (first filter only)</dd>
372</dl>
f7deaa1a 373
5a738aea
MS
374<p>The scheduler runs one or more of these programs to print any given job. The
375first filter reads from the print file and writes to the standard output, while
376the remaining filters read from the standard input and write to the standard
377output. The backend is the last filter in the chain and writes to the
378device.</p>
f7deaa1a 379
ac884b6a
MS
380<h3><a name="SECURITY">Security Considerations</a></h3>
381
382<p>It is always important to use security programming practices. Filters and
383most backends are run as a non-priviledged user, so the major security
384consideration is resource utilization - filters should not depend on unlimited
385amounts of CPU, memory, or disk space, and should protect against conditions
386that could lead to excess usage of any resource like infinite loops and
387unbounded recursion. In addition, filters must <em>never</em> allow the user to
388specify an arbitrary file path to a separator page, template, or other file
389used by the filter since that can lead to an unauthorized disclosure of
390information. <em>Always</em> treat input as suspect and validate it!</p>
391
392<p>If you are developing a backend that runs as root, make sure to check for
393potential buffer overflows, integer under/overflow conditions, and file
394accesses since these can lead to privilege escalations. When writing files,
395always validate the file path and <em>never</em> allow a user to determine
396where to store a file.</p>
397
398<blockquote><b>Note:</b>
399
400<p><em>Never</em> write files to a user's home directory. Aside from the
401security implications, CUPS is a network print service and as such the network
402user may not be the same as the local user and/or there may not be a local home
403directory to write to.</p>
404
405<p>In addition, some operating systems provide additional security mechanisms
406that further limit file system access, even for backends running as root. On
407Mac OS X, for example, no backend may write to a user's home directory.</p>
408</blockquote>
409
410<h3><a name="TEMPFILES">Temporary Files</a></h3>
411
412<p>Temporary files should be created in the directory specified by the
413"TMPDIR" environment variable. The
414<a href="#cupsTempFile2"><code>cupsTempFile2</code></a> function can be
415used to safely create temporary files in this directory.</p>
416
417<h3><a name="COPIES">Copy Generation</a></h3>
418
419<p>The <code>argv[4]</code> argument specifies the number of copies to produce
420of the input file. In general, you should only generate copies if the
421<em>filename</em> argument is supplied. The only exception to this are
422filters that produce device-independent PostScript output, since the PostScript
423filter <var>pstops</var> is responsible for generating copies of PostScript
424files.</p>
425
5a738aea 426<h3><a name="EXITCODES">Exit Codes</a></h3>
f7deaa1a 427
5a738aea
MS
428<p>Filters must exit with status 0 when they successfully generate print data
429or 1 when they encounter an error. Backends can return any of the
430<a href="#cups_backend_t"><code>cups_backend_t</code></a> constants.</p>
f7deaa1a 431
5a738aea 432<h3><a name="ENVIRONMENT">Environment Variables</a></h3>
f7deaa1a 433
5a738aea 434<p>The following environment variables are defined by the printing system:</p>
f7deaa1a 435
5a738aea 436<dl class="code">
f7deaa1a 437
5a738aea
MS
438 <dt>APPLE_LANGUAGES</dt>
439 <dd>The Apple language identifier associated with the job
440 (Mac OS X only).</dd>
f7deaa1a 441
5a738aea
MS
442 <dt>CHARSET</dt>
443 <dd>The job character set, typically "utf-8".</dd>
f7deaa1a 444
5a738aea
MS
445 <dt>CLASS</dt>
446 <dd>When a job is submitted to a printer class, contains the name of
447 the destination printer class. Otherwise this environment
448 variable will not be set.</dd>
f7deaa1a 449
5a738aea
MS
450 <dt>CONTENT_TYPE</dt>
451 <dd>The MIME type associated with the file (e.g.
452 application/postscript).</dd>
f7deaa1a 453
5a738aea
MS
454 <dt>CUPS_CACHEDIR</dt>
455 <dd>The directory where cache files can be stored.</dd>
f7deaa1a 456
5a738aea
MS
457 <dt>CUPS_DATADIR</dt>
458 <dd>The directory where data files can be found.</dd>
f7deaa1a 459
5a738aea
MS
460 <dt>CUPS_SERVERROOT</dt>
461 <dd>The root directory of the server.</dd>
f7deaa1a 462
5a738aea
MS
463 <dt>DEVICE_URI</dt>
464 <dd>The device-uri associated with the printer.</dd>
f7deaa1a 465
5a738aea
MS
466 <dt>FINAL_CONTENT_TYPE</dt>
467 <dd>The MIME type associated with the printer (e.g.
468 application/vnd.cups-postscript).</dd>
f7deaa1a 469
5a738aea
MS
470 <dt>LANG</dt>
471 <dd>The language locale associated with the job.</dd>
f7deaa1a 472
5a738aea
MS
473 <dt>PPD</dt>
474 <dd>The full pathname of the PostScript Printer Description (PPD)
475 file for this printer.</dd>
f7deaa1a 476
5a738aea
MS
477 <dt>PRINTER</dt>
478 <dd>The name of the printer.</dd>
f7deaa1a 479
5a738aea
MS
480 <dt>RIP_CACHE</dt>
481 <dd>The recommended amount of memory to use for Raster Image
482 Processors (RIPs).</dd>
f7deaa1a 483
5a738aea 484</dl>
f7deaa1a 485
5a738aea 486<h3><a name="MESSAGES">Communicating with the Scheduler</a></h3>
f7deaa1a 487
5a738aea
MS
488<p>Filters and backends communicate wih the scheduler by writing messages
489to the standard error file. For example, the following code sets the current
490printer state message to "Printing page 5":</p>
f7deaa1a 491
5a738aea
MS
492<pre class="example">
493int page = 5;
f7deaa1a 494
5a738aea 495fprintf(stderr, "INFO: Printing page %d\n", page);
f7deaa1a 496</pre>
497
5a738aea
MS
498<p>Each message is a single line of text starting with one of the following
499prefix strings:</p>
500
501<dl class="code">
502
503 <dt>ALERT: message</dt>
504 <dd>Sets the printer-state-message attribute and adds the specified
505 message to the current error log file using the "alert" log level.</dd>
506
507 <dt>ATTR: attribute=value [attribute=value]</dt>
508 <dd>Sets the named printer or job attribute(s). Typically this is used
509 to set the <code>marker-colors</code>, <code>marker-levels</code>,
510 <code>marker-names</code>, <code>marker-types</code>,
511 <code>printer-alert</code>, and <code>printer-alert-description</code>
512 printer attributes.</dd>
513
514 <dt>CRIT: message</dt>
515 <dd>Sets the printer-state-message attribute and adds the specified
516 message to the current error log file using the "critical" log
517 level.</dd>
518
519 <dt>DEBUG: message</dt>
520 <dd>Sets the printer-state-message attribute and adds the specified
521 message to the current error log file using the "debug" log level.</dd>
522
523 <dt>DEBUG2: message</dt>
524 <dd>Sets the printer-state-message attribute and adds the specified
525 message to the current error log file using the "debug2" log level.</dd>
526
527 <dt>EMERG: message</dt>
528 <dd>Sets the printer-state-message attribute and adds the specified
529 message to the current error log file using the "emergency" log
530 level.</dd>
531
532 <dt>ERROR: message</dt>
533 <dd>Sets the printer-state-message attribute and adds the specified
534 message to the current error log file using the "error" log level.</dd>
535
536 <dt>INFO: message</dt>
537 <dd>Sets the printer-state-message attribute. If the current log level
538 is set to "debug2", also adds the specified message to the current error
539 log file using the "info" log level.</dd>
540
541 <dt>NOTICE: message</dt>
542 <dd>Sets the printer-state-message attribute and adds the specified
543 message to the current error log file using the "notice" log level.</dd>
544
545 <dt>PAGE: page-number #-copies</dt>
546 <dt>PAGE: total #-pages</dt>
547 <dd>Adds an entry to the current page log file. The first form adds
548 #-copies to the job-media-sheets-completed attribute. The second
549 form sets the job-media-sheets-completed attribute to #-pages.</dd>
550
551 <dt>STATE: printer-state-reason [printer-state-reason ...]</dt>
552 <dt>STATE: + printer-state-reason [printer-state-reason ...]</dt>
553 <dt>STATE: - printer-state-reason [printer-state-reason ...]</dt>
554 <dd>Sets, adds, or removes printer-state-reason keywords to the
555 current queue. Typically this is used to indicate media, ink, and
556 toner conditions on a printer.</dd>
557
558 <dt>WARNING: message</dt>
559 <dd>Sets the printer-state-message attribute and adds the specified
560 message to the current error log file using the "warning" log
561 level.</dd>
562
563</dl>
564
565<p>Messages without one of these prefixes are treated as if they began with
566the "DEBUG:" prefix string.</p>
567
568<h3><a name="COMMUNICATING">Communicating with the Backend</a></h3>
569
570<p>Filters can communicate with the backend via the
571<a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> and
572<a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
573functions. The
574<a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> function
575reads data that has been sent back from the device and is typically used to
576obtain status and configuration information. For example, the following code
577polls the backend for back-channel data:</p>
578
579<pre class="example">
580#include &lt;cups/cups.h&gt;
581
582char buffer[8192];
583ssize_t bytes;
584
585/* Use a timeout of 0.0 seconds to poll for back-channel data */
586bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);
587</pre>
f7deaa1a 588
5a738aea
MS
589The
590<a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
591function allows you to get out-of-band status information and do synchronization
592with the device. For example, the following code gets the current IEEE-1284
593device ID string from the backend:</p>
594
595<pre class="example">
f7deaa1a 596#include &lt;cups/sidechannel.h&gt;
597
598char data[2049];
599int datalen;
5a738aea 600<a href="#cups_sc_status_t">cups_sc_status_t</a> status;
f7deaa1a 601
602/* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for nul-termination... */
603datalen = sizeof(data) - 1;
604
605/* Get the IEEE-1284 device ID, waiting for up to 1 second */
5a738aea 606status = <a href="#cupsSideChannelDoRequest">cupsSideChannelDoRequest</a>(CUPS_SC_CMD_GET_DEVICE_ID, data, &amp;datalen, 1.0);
f7deaa1a 607
608/* Use the returned value if OK was returned and the length is non-zero */
609if (status == CUPS_SC_STATUS_OK && datalen > 0)
610 data[datalen] = '\0';
611else
612 data[0] = '\0';
613</pre>
614
5a738aea
MS
615<p>Backends communicate with filters using the reciprocal functions
616<a href="#cupsBackChannelWrite"><code>cupsBackChannelWrite</code></a>,
617<a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>, and
618<a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a>. We
619recommend writing back-channel data using a timeout of 1.0 seconds:</p>
f7deaa1a 620
5a738aea
MS
621<pre class="example">
622#include &lt;cups/cups.h&gt;
f7deaa1a 623
5a738aea
MS
624char buffer[8192];
625ssize_t bytes;
f7deaa1a 626
5a738aea
MS
627/* Use a timeout of 1.0 seconds to give filters a chance to read */
628cupsBackChannelWrite(buffer, bytes, 1.0);
f7deaa1a 629</pre>
630
5a738aea
MS
631<p>The <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>
632function reads a side-channel command from a filter, driver, or port monitor.
633Backends can either poll for commands using a <code>timeout</code> of 0.0, wait
634indefinitely for commands using a <code>timeout</code> of -1.0 (probably in a
635separate thread for that purpose), or use <code>select</code> or
636<code>poll</code> on the <code>CUPS_SC_FD</code> file descriptor (4) to handle
637input and output on several file descriptors at the same time. Backends can pass
638<code>NULL</code> for the <code>data</code> and <code>datalen</code> parameters
639since none of the commands sent by upstream filters contain any data at this
640time.</p>
641
642<p>Once a command is processed, the backend uses the
643<a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a> function
644to send its response. For example, the following code shows how to poll for a
645side-channel command and respond to it:</p>
646
647<pre class="example">
f7deaa1a 648#include &lt;cups/sidechannel.h&gt;
649
5a738aea
MS
650<a href="#cups_sc_command_t">cups_sc_command_t</a> command;
651<a href="#cups_sc_status_t">cups_sc_status_t</a> status;
f7deaa1a 652
653/* Poll for a command... */
5a738aea 654if (!<a href="#cupsSideChannelRead">cupsSideChannelRead</a>(&amp;command, &amp;status, NULL, NULL, 0.0))
f7deaa1a 655{
656 char data[2048];
657 int datalen;
658
659 switch (command)
660 {
5a738aea 661 /* handle supported commands, file data/datalen/status with values as needed */
f7deaa1a 662
663 default :
664 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
665 datalen = 0;
666 break;
667 }
668
669 /* Send a response... */
5a738aea 670 <a href="#cupsSideChannelWrite">cupsSideChannelWrite</a>(command, status, data, datalen, 1.0);
f7deaa1a 671}
672</pre>
ac884b6a
MS
673
674<h3><a name="SNMP">Doing SNMP Queries with Network Printers</a></h3>
675
7a14d768
MS
676<p>Re-write for side-channel-based SNMP queries.</p>
677
678<!--
ac884b6a
MS
679<p>The Simple Network Management Protocol (SNMP) allows you to get the current
680status, page counter, and supply levels from most network printers. Every
681piece of information is associated with an Object Identifier (OID), and
682every printer has a <em>community</em> name associated with it. OIDs can be
683queried directly or by "walking" over a range of OIDs with a common prefix.</p>
684
685<p>The CUPS SNMP functions provide a simple API for querying network printers.
686Queries are made using a datagram socket that is created using
687<a href="#cupsSNMPOpen"><code>cupsSNMPOpen</code></a> and destroyed using
688<a href="#cupsSNMPClose"><code>cupsSNMPClose</code></a>:</p>
689
690<pre class="example">
691#include &lt;cups/snmp.h&gt;
692
693int snmp = <a href="#cupsSNMPOpen">cupsSNMPOpen</a>(AF_INET);
694
695/* do some queries */
696
697<a href="#cupsSNMPClose">cupsSNMPClose</a>(snmp);
698</pre>
699
700<p>OIDs are simple C arrays of integers, terminated by a value of -1. For
701example, the page counter OID .1.3.6.1.2.1.43.10.2.1.4.1.1 would be:</p>
702
703<pre class="example">
704int page_counter_oid[] = { 1, 3, 6, 1, 2, 1, 43, 10, 2, 1, 4, 1, 1, -1 };
705</pre>
706
707<p>You send a query using
708<a href="#cupsSNMPWrite"><code>cupsSNMPWrite</code></a> and read the value back
709using <a href="#cupsSNMPRead"><code>cupsSNMPRead</code></a>. The value is read
710into a structure called <a href="#cups_snmp_t"><code>cups_snmp_t</code></a>:</p>
711
712<pre class="example">
713#include &lt;cups/snmp.h&gt;
714
715int page_counter_oid[] = { 1, 3, 6, 1, 2, 1, 43, 10, 2, 1, 4, 1, 1, -1 };
716http_addrlist_t *host = httpAddrGetList("myprinter", AF_UNSPEC, "161");
717int snmp = <a href="#cupsSNMPOpen">cupsSNMPOpen</a>(host->addr.addr.sa_family);
718<a href="#cups_snmp_t">cups_snmp_t</a> packet;
719
720<a href="#cupsSNMPWrite">cupsSNMPWrite</a>(snmp, &amp;(host->addr), CUPS_SNMP_VERSION_1,
721 <a href="#cupsSNMPDefaultCommunity">cupsSNMPDefaultCommunity</a>(), CUPS_ASN1_GET_REQUEST, 1,
722 page_counter_oid);
723if (<a href="#cupsSNMPRead">cupsSNMPRead</a>(snmp, &amp;packet, 5000))
724{
725 /* Do something with the value */
726 printf("Page counter is: %d\n", packet.object_value.integer);
727}
728</pre>
729
730<p>The <a href="#cupsSNMPWalk"><code>cupsSNMPWalk</code></a> function allows you
731to query a whole group of OIDs, calling a function of your choice for each OID
732that is found:</p>
733
734<pre class="example">
735#include &lt;cups/snmp.h&gt;
736
737void
738my_callback(<a href="#cups_snmp_t">cups_snmp_t</a> *packet, void *data)
739{
740 /* Do something with the value */
741}
742
743int printer_mib_oid[] = { 1, 3, 6, 1, 2, 1, 43, -1 };
744http_addrlist_t *host = httpAddrGetList("myprinter", AF_UNSPEC, "161");
745int snmp = <a href="#cupsSNMPOpen">cupsSNMPOpen</a>(host->addr.addr.sa_family);
746void *my_data;
747
748<a href="#cupsSNMPWalk">cupsSNMPWalk</a>(snmp, &amp;(host->addr), CUPS_SNMP_VERSION_1,
749 <a href="#cupsSNMPDefaultCommunity">cupsSNMPDefaultCommunity</a>(), printer_mib_oid, my_callback, my_data);
750</pre>
7a14d768 751--><h2 class="title"><a name="FUNCTIONS">Functions</a></h2>
5a738aea
MS
752<h3 class="function"><span class="info">&nbsp;CUPS 1.2&nbsp;</span><a name="cupsBackChannelRead">cupsBackChannelRead</a></h3>
753<p class="description">Read data from the backchannel.</p>
754<p class="code">
755ssize_t cupsBackChannelRead (<br>
756&nbsp;&nbsp;&nbsp;&nbsp;char *buffer,<br>
757&nbsp;&nbsp;&nbsp;&nbsp;size_t bytes,<br>
758&nbsp;&nbsp;&nbsp;&nbsp;double timeout<br>
759);</p>
760<h4 class="parameters">Parameters</h4>
761<dl>
762<dt>buffer</dt>
763<dd class="description">Buffer to read</dd>
764<dt>bytes</dt>
765<dd class="description">Bytes to read</dd>
766<dt>timeout</dt>
767<dd class="description">Timeout in seconds</dd>
768</dl>
769<h4 class="returnvalue">Return Value</h4>
770<p class="description">Bytes read or -1 on error</p>
771<h4 class="discussion">Discussion</h4>
772<p class="discussion">Reads up to &quot;bytes&quot; bytes from the backchannel. The &quot;timeout&quot;
ef416fc2 773parameter controls how many seconds to wait for the data - use
7740.0 to return immediately if there is no data, -1.0 to wait
775for data indefinitely.
776
5a738aea
MS
777</p>
778<h3 class="function"><span class="info">&nbsp;CUPS 1.2&nbsp;</span><a name="cupsBackChannelWrite">cupsBackChannelWrite</a></h3>
779<p class="description">Write data to the backchannel.</p>
780<p class="code">
781ssize_t cupsBackChannelWrite (<br>
782&nbsp;&nbsp;&nbsp;&nbsp;const char *buffer,<br>
783&nbsp;&nbsp;&nbsp;&nbsp;size_t bytes,<br>
784&nbsp;&nbsp;&nbsp;&nbsp;double timeout<br>
785);</p>
786<h4 class="parameters">Parameters</h4>
787<dl>
788<dt>buffer</dt>
789<dd class="description">Buffer to write</dd>
790<dt>bytes</dt>
791<dd class="description">Bytes to write</dd>
792<dt>timeout</dt>
793<dd class="description">Timeout in seconds</dd>
794</dl>
795<h4 class="returnvalue">Return Value</h4>
796<p class="description">Bytes written or -1 on error</p>
797<h4 class="discussion">Discussion</h4>
798<p class="discussion">Writes &quot;bytes&quot; bytes to the backchannel. The &quot;timeout&quot; parameter
ef416fc2 799controls how many seconds to wait for the data to be written - use
8000.0 to return immediately if the data cannot be written, -1.0 to wait
801indefinitely.
802
ac884b6a
MS
803</p>
804<h3 class="function"><a name="cupsBackendDeviceURI">cupsBackendDeviceURI</a></h3>
805<p class="description">Get the device URI for a backend.</p>
806<p class="code">
807const char *cupsBackendDeviceURI (<br>
808&nbsp;&nbsp;&nbsp;&nbsp;char **argv<br>
809);</p>
810<h4 class="parameters">Parameters</h4>
811<dl>
812<dt>argv</dt>
813<dd class="description">Command-line arguments</dd>
814</dl>
815<h4 class="returnvalue">Return Value</h4>
816<p class="description">Device URI or <code>NULL</code></p>
817<h4 class="discussion">Discussion</h4>
818<p class="discussion">The &quot;argv&quot; argument is the argv argument passed to main(). This
819function returns the device URI passed in the DEVICE_URI environment
820variable or the device URI passed in argv[0], whichever is found
821first.</p>
5a738aea
MS
822<h3 class="function"><span class="info">&nbsp;CUPS 1.3&nbsp;</span><a name="cupsSideChannelDoRequest">cupsSideChannelDoRequest</a></h3>
823<p class="description">Send a side-channel command to a backend and wait for a response.</p>
824<p class="code">
825<a href="#cups_sc_status_t">cups_sc_status_t</a> cupsSideChannelDoRequest (<br>
826&nbsp;&nbsp;&nbsp;&nbsp;<a href="#cups_sc_command_t">cups_sc_command_t</a> command,<br>
827&nbsp;&nbsp;&nbsp;&nbsp;char *data,<br>
828&nbsp;&nbsp;&nbsp;&nbsp;int *datalen,<br>
829&nbsp;&nbsp;&nbsp;&nbsp;double timeout<br>
830);</p>
831<h4 class="parameters">Parameters</h4>
832<dl>
833<dt>command</dt>
834<dd class="description">Command to send</dd>
835<dt>data</dt>
836<dd class="description">Response data buffer pointer</dd>
837<dt>datalen</dt>
838<dd class="description">Size of data buffer on entry, number of bytes in buffer on return</dd>
839<dt>timeout</dt>
840<dd class="description">Timeout in seconds</dd>
841</dl>
842<h4 class="returnvalue">Return Value</h4>
843<p class="description">Status of command</p>
844<h4 class="discussion">Discussion</h4>
845<p class="discussion">This function is normally only called by filters, drivers, or port
f7deaa1a 846monitors in order to communicate with the backend used by the current
847printer. Programs must be prepared to handle timeout or &quot;not
848implemented&quot; status codes, which indicate that the backend or device
5a738aea
MS
849do not support the specified side-channel command.<br>
850<br>
851The &quot;datalen&quot; parameter must be initialized to the size of the buffer
f7deaa1a 852pointed to by the &quot;data&quot; parameter. cupsSideChannelDoRequest() will
853update the value to contain the number of data bytes in the buffer.
854
5a738aea
MS
855</p>
856<h3 class="function"><span class="info">&nbsp;CUPS 1.3&nbsp;</span><a name="cupsSideChannelRead">cupsSideChannelRead</a></h3>
857<p class="description">Read a side-channel message.</p>
858<p class="code">
859int cupsSideChannelRead (<br>
860&nbsp;&nbsp;&nbsp;&nbsp;<a href="#cups_sc_command_t">cups_sc_command_t</a> *command,<br>
861&nbsp;&nbsp;&nbsp;&nbsp;<a href="#cups_sc_status_t">cups_sc_status_t</a> *status,<br>
862&nbsp;&nbsp;&nbsp;&nbsp;char *data,<br>
863&nbsp;&nbsp;&nbsp;&nbsp;int *datalen,<br>
864&nbsp;&nbsp;&nbsp;&nbsp;double timeout<br>
865);</p>
866<h4 class="parameters">Parameters</h4>
867<dl>
868<dt>command</dt>
869<dd class="description">Command code</dd>
870<dt>status</dt>
871<dd class="description">Status code</dd>
872<dt>data</dt>
873<dd class="description">Data buffer pointer</dd>
874<dt>datalen</dt>
875<dd class="description">Size of data buffer on entry, number of bytes in buffer on return</dd>
876<dt>timeout</dt>
877<dd class="description">Timeout in seconds</dd>
878</dl>
879<h4 class="returnvalue">Return Value</h4>
880<p class="description">0 on success, -1 on error</p>
881<h4 class="discussion">Discussion</h4>
882<p class="discussion">This function is normally only called by backend programs to read
f7deaa1a 883commands from a filter, driver, or port monitor program. The
884caller must be prepared to handle incomplete or invalid messages
5a738aea
MS
885and return the corresponding status codes.<br>
886<br>
887The &quot;datalen&quot; parameter must be initialized to the size of the buffer
f7deaa1a 888pointed to by the &quot;data&quot; parameter. cupsSideChannelDoRequest() will
889update the value to contain the number of data bytes in the buffer.
890
5a738aea
MS
891</p>
892<h3 class="function"><span class="info">&nbsp;CUPS 1.3&nbsp;</span><a name="cupsSideChannelWrite">cupsSideChannelWrite</a></h3>
893<p class="description">Write a side-channel message.</p>
894<p class="code">
895int cupsSideChannelWrite (<br>
896&nbsp;&nbsp;&nbsp;&nbsp;<a href="#cups_sc_command_t">cups_sc_command_t</a> command,<br>
897&nbsp;&nbsp;&nbsp;&nbsp;<a href="#cups_sc_status_t">cups_sc_status_t</a> status,<br>
898&nbsp;&nbsp;&nbsp;&nbsp;const char *data,<br>
899&nbsp;&nbsp;&nbsp;&nbsp;int datalen,<br>
900&nbsp;&nbsp;&nbsp;&nbsp;double timeout<br>
901);</p>
902<h4 class="parameters">Parameters</h4>
903<dl>
904<dt>command</dt>
905<dd class="description">Command code</dd>
906<dt>status</dt>
907<dd class="description">Status code</dd>
908<dt>data</dt>
909<dd class="description">Data buffer pointer</dd>
910<dt>datalen</dt>
911<dd class="description">Number of bytes of data</dd>
912<dt>timeout</dt>
913<dd class="description">Timeout in seconds</dd>
914</dl>
915<h4 class="returnvalue">Return Value</h4>
916<p class="description">0 on success, -1 on error</p>
917<h4 class="discussion">Discussion</h4>
918<p class="discussion">This function is normally only called by backend programs to send
f7deaa1a 919responses to a filter, driver, or port monitor program.
920
5a738aea
MS
921</p>
922<h2 class="title"><a name="TYPES">Data Types</a></h2>
923<h3 class="typedef"><a name="cups_backend_t">cups_backend_t</a></h3>
924<p class="description">Backend exit codes</p>
925<p class="code">
926typedef enum <a href="#cups_backend_e">cups_backend_e</a> cups_backend_t;
927</p>
928<h3 class="typedef"><a name="cups_sc_bidi_t">cups_sc_bidi_t</a></h3>
929<p class="description">Bidirectional capabilities</p>
930<p class="code">
931typedef enum <a href="#cups_sc_bidi_e">cups_sc_bidi_e</a> cups_sc_bidi_t;
932</p>
933<h3 class="typedef"><a name="cups_sc_command_t">cups_sc_command_t</a></h3>
934<p class="description">Request command codes</p>
935<p class="code">
936typedef enum <a href="#cups_sc_command_e">cups_sc_command_e</a> cups_sc_command_t;
937</p>
938<h3 class="typedef"><a name="cups_sc_state_t">cups_sc_state_t</a></h3>
939<p class="description">Printer state bits</p>
940<p class="code">
941typedef enum <a href="#cups_sc_state_e">cups_sc_state_e</a> cups_sc_state_t;
942</p>
943<h3 class="typedef"><a name="cups_sc_status_t">cups_sc_status_t</a></h3>
944<p class="description">Response status codes</p>
945<p class="code">
946typedef enum <a href="#cups_sc_status_e">cups_sc_status_e</a> cups_sc_status_t;
947</p>
948<h2 class="title"><a name="ENUMERATIONS">Constants</a></h2>
949<h3 class="enumeration"><a name="cups_backend_e">cups_backend_e</a></h3>
950<p class="description">Backend exit codes</p>
951<h4 class="constants">Constants</h4>
952<dl>
953<dt>CUPS_BACKEND_AUTH_REQUIRED </dt>
954<dd class="description">Job failed, authentication required</dd>
955<dt>CUPS_BACKEND_CANCEL </dt>
956<dd class="description">Job failed, cancel job</dd>
957<dt>CUPS_BACKEND_FAILED </dt>
958<dd class="description">Job failed, use error-policy</dd>
959<dt>CUPS_BACKEND_HOLD </dt>
960<dd class="description">Job failed, hold job</dd>
961<dt>CUPS_BACKEND_OK </dt>
962<dd class="description">Job completed successfully</dd>
963<dt>CUPS_BACKEND_STOP </dt>
964<dd class="description">Job failed, stop queue</dd>
965</dl>
966<h3 class="enumeration"><a name="cups_sc_bidi_e">cups_sc_bidi_e</a></h3>
967<p class="description">Bidirectional capabilities</p>
968<h4 class="constants">Constants</h4>
969<dl>
970<dt>CUPS_SC_BIDI_NOT_SUPPORTED </dt>
971<dd class="description">Bidirectional I/O is not supported</dd>
972<dt>CUPS_SC_BIDI_SUPPORTED </dt>
973<dd class="description">Bidirectional I/O is supported</dd>
974</dl>
975<h3 class="enumeration"><a name="cups_sc_command_e">cups_sc_command_e</a></h3>
976<p class="description">Request command codes</p>
977<h4 class="constants">Constants</h4>
978<dl>
979<dt>CUPS_SC_CMD_DRAIN_OUTPUT </dt>
980<dd class="description">Drain all pending output</dd>
981<dt>CUPS_SC_CMD_GET_BIDI </dt>
982<dd class="description">Return bidirectional capabilities</dd>
983<dt>CUPS_SC_CMD_GET_DEVICE_ID </dt>
984<dd class="description">Return the IEEE-1284 device ID</dd>
985<dt>CUPS_SC_CMD_GET_STATE </dt>
986<dd class="description">Return the device state</dd>
987<dt>CUPS_SC_CMD_SOFT_RESET </dt>
988<dd class="description">Do a soft reset</dd>
989</dl>
990<h3 class="enumeration"><a name="cups_sc_state_e">cups_sc_state_e</a></h3>
991<p class="description">Printer state bits</p>
992<h4 class="constants">Constants</h4>
993<dl>
994<dt>CUPS_SC_STATE_BUSY </dt>
995<dd class="description">Device is busy</dd>
996<dt>CUPS_SC_STATE_ERROR </dt>
997<dd class="description">Other error condition</dd>
998<dt>CUPS_SC_STATE_MARKER_EMPTY </dt>
999<dd class="description">Toner/ink out condition</dd>
1000<dt>CUPS_SC_STATE_MARKER_LOW </dt>
1001<dd class="description">Toner/ink low condition</dd>
1002<dt>CUPS_SC_STATE_MEDIA_EMPTY </dt>
1003<dd class="description">Paper out condition</dd>
1004<dt>CUPS_SC_STATE_MEDIA_LOW </dt>
1005<dd class="description">Paper low condition</dd>
1006<dt>CUPS_SC_STATE_OFFLINE </dt>
1007<dd class="description">Device is off-line</dd>
1008<dt>CUPS_SC_STATE_ONLINE </dt>
1009<dd class="description">Device is on-line</dd>
1010</dl>
1011<h3 class="enumeration"><a name="cups_sc_status_e">cups_sc_status_e</a></h3>
1012<p class="description">Response status codes</p>
1013<h4 class="constants">Constants</h4>
1014<dl>
1015<dt>CUPS_SC_STATUS_BAD_MESSAGE </dt>
1016<dd class="description">The command/response message was invalid</dd>
1017<dt>CUPS_SC_STATUS_IO_ERROR </dt>
1018<dd class="description">An I/O error occurred</dd>
1019<dt>CUPS_SC_STATUS_NONE </dt>
1020<dd class="description">No status</dd>
1021<dt>CUPS_SC_STATUS_NOT_IMPLEMENTED </dt>
1022<dd class="description">Command not implemented</dd>
1023<dt>CUPS_SC_STATUS_NO_RESPONSE </dt>
1024<dd class="description">The device did not respond</dd>
1025<dt>CUPS_SC_STATUS_OK </dt>
1026<dd class="description">Operation succeeded</dd>
1027<dt>CUPS_SC_STATUS_TIMEOUT </dt>
1028<dd class="description">The backend did not respond</dd>
1029<dt>CUPS_SC_STATUS_TOO_BIG </dt>
1030<dd class="description">Response too big</dd>
1031</dl>
1032</div>
ef416fc2 1033</body>
1034</html>