]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/api-filter.shtml
Merge changes from CUPS 1.6svn-r10437.
[thirdparty/cups.git] / cups / api-filter.shtml
CommitLineData
ef416fc2 1<!--
75bd9771 2 "$Id: api-filter.shtml 7677 2008-06-19 23:22:19Z mike $"
ef416fc2 3
22c9029b 4 Filter and backend programming introduction for CUPS.
ef416fc2 5
f3c17241 6 Copyright 2007-2012 by Apple Inc.
bc44d920 7 Copyright 1997-2006 by Easy Software Products, all rights reserved.
ef416fc2 8
9 These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 14-->
15
5a738aea 16<h2 class='title'><a name="OVERVIEW">Overview</a></h2>
ef416fc2 17
79e1d494
MS
18<p>Filters (which include printer drivers and port monitors) and backends
19are used to convert job files to a printable format and send that data to the
20printer itself. All of these programs use a common interface for processing
21print jobs and communicating status information to the scheduler. Each is run
22with a standard set of command-line arguments:<p>
ef416fc2 23
5a738aea 24<dl class="code">
ef416fc2 25
5a738aea
MS
26 <dt>argv[1]</dt>
27 <dd>The job ID</dd>
ef416fc2 28
5a738aea
MS
29 <dt>argv[2]</dt>
30 <dd>The user printing the job</dd>
f7deaa1a 31
5a738aea
MS
32 <dt>argv[3]</dt>
33 <dd>The job name/title</dd>
ef416fc2 34
5a738aea
MS
35 <dt>argv[4]</dt>
36 <dd>The number of copies to print</dd>
ef416fc2 37
5a738aea
MS
38 <dt>argv[5]</dt>
39 <dd>The options that were provided when the job was submitted</dd>
f7deaa1a 40
5a738aea 41 <dt>argv[6]</dt>
79e1d494 42 <dd>The file to print (first program only)</dd>
5a738aea 43</dl>
ef416fc2 44
5a738aea
MS
45<p>The scheduler runs one or more of these programs to print any given job. The
46first filter reads from the print file and writes to the standard output, while
47the remaining filters read from the standard input and write to the standard
48output. The backend is the last filter in the chain and writes to the
49device.</p>
f7deaa1a 50
178cb736
MS
51<p>Filters are always run as a non-privileged user, typically "lp", with no
52connection to the user's desktop. Backends are run either as a non-privileged
53user or as root if the file permissions do not allow user or group execution.
54The <a href="#PERMISSIONS">file permissions</a> section talks about this in
55more detail.</p>
56
ac884b6a
MS
57<h3><a name="SECURITY">Security Considerations</a></h3>
58
59<p>It is always important to use security programming practices. Filters and
eac3a0a0 60most backends are run as a non-privileged user, so the major security
ac884b6a
MS
61consideration is resource utilization - filters should not depend on unlimited
62amounts of CPU, memory, or disk space, and should protect against conditions
63that could lead to excess usage of any resource like infinite loops and
64unbounded recursion. In addition, filters must <em>never</em> allow the user to
65specify an arbitrary file path to a separator page, template, or other file
66used by the filter since that can lead to an unauthorized disclosure of
67information. <em>Always</em> treat input as suspect and validate it!</p>
68
4d301e69 69<p>If you are developing a backend that runs as root, make sure to check for
ac884b6a
MS
70potential buffer overflows, integer under/overflow conditions, and file
71accesses since these can lead to privilege escalations. When writing files,
72always validate the file path and <em>never</em> allow a user to determine
73where to store a file.</p>
74
75<blockquote><b>Note:</b>
76
77<p><em>Never</em> write files to a user's home directory. Aside from the
78security implications, CUPS is a network print service and as such the network
79user may not be the same as the local user and/or there may not be a local home
80directory to write to.</p>
81
82<p>In addition, some operating systems provide additional security mechanisms
178cb736 83that further limit file system access, even for backends running as root. On
f3c17241 84OS X, for example, no backend may write to a user's home directory.</p>
ac884b6a
MS
85</blockquote>
86
f228370c 87<h3><a name="SIGNALS">Canceled Jobs and Signal Handling</a></h3>
22c9029b
MS
88
89<p>The scheduler sends <code>SIGTERM</code> when a printing job is canceled or
90held. Filters, backends, and port monitors <em>must</em> catch
91<code>SIGTERM</code> and perform any cleanup necessary to produce a valid output
92file or return the printer to a known good state. The recommended behavior is to
88f9aafc
MS
93end the output on the current page, preferably on the current line or object
94being printed.</p>
22c9029b 95
88f9aafc 96<p>Filters and backends may also receive <code>SIGPIPE</code> when an upstream or downstream filter/backend exits with a non-zero status. Developers should generally ignore <code>SIGPIPE</code> at the beginning of <code>main()</code> with the following function call:</p>
eac3a0a0
MS
97
98<pre class="example">
99#include &lt;signal.h&gt;>
100
101...
102
103int
104main(int argc, char *argv[])
105{
106 signal(SIGPIPE, SIG_IGN);
107
108 ...
109}
110</pre>
111
178cb736
MS
112<h3><a name="PERMISSIONS">File Permissions</a></h3>
113
114<p>For security reasons, CUPS will only run filters and backends that are owned
4d301e69
MS
115by root and do not have world or group write permissions. The recommended
116permissions for filters and backends are 0555 - read and execute but no write.
117Backends that must run as root should use permissions of 0500 - read and execute
118by root, no access for other users. Write permissions can be enabled for the
119root user only.</p>
178cb736
MS
120
121<p>To avoid a warning message, the directory containing your filter(s) must also
4d301e69
MS
122be owned by root and have world and group write disabled - permissions of 0755
123or 0555 are strongly encouraged.</p>
178cb736 124
ac884b6a
MS
125<h3><a name="TEMPFILES">Temporary Files</a></h3>
126
127<p>Temporary files should be created in the directory specified by the
128"TMPDIR" environment variable. The
129<a href="#cupsTempFile2"><code>cupsTempFile2</code></a> function can be
130used to safely create temporary files in this directory.</p>
131
132<h3><a name="COPIES">Copy Generation</a></h3>
133
134<p>The <code>argv[4]</code> argument specifies the number of copies to produce
135of the input file. In general, you should only generate copies if the
136<em>filename</em> argument is supplied. The only exception to this are
137filters that produce device-independent PostScript output, since the PostScript
138filter <var>pstops</var> is responsible for generating copies of PostScript
139files.</p>
140
5a738aea 141<h3><a name="EXITCODES">Exit Codes</a></h3>
f7deaa1a 142
5a738aea
MS
143<p>Filters must exit with status 0 when they successfully generate print data
144or 1 when they encounter an error. Backends can return any of the
145<a href="#cups_backend_t"><code>cups_backend_t</code></a> constants.</p>
f7deaa1a 146
5a738aea 147<h3><a name="ENVIRONMENT">Environment Variables</a></h3>
f7deaa1a 148
79e1d494
MS
149<p>The following environment variables are defined by the printing system
150when running print filters and backends:</p>
f7deaa1a 151
5a738aea 152<dl class="code">
f7deaa1a 153
acb056cb 154 <dt>APPLE_LANGUAGE</dt>
5a738aea 155 <dd>The Apple language identifier associated with the job
f3c17241 156 (OS X only).</dd>
f7deaa1a 157
5a738aea
MS
158 <dt>CHARSET</dt>
159 <dd>The job character set, typically "utf-8".</dd>
f7deaa1a 160
5a738aea
MS
161 <dt>CLASS</dt>
162 <dd>When a job is submitted to a printer class, contains the name of
163 the destination printer class. Otherwise this environment
164 variable will not be set.</dd>
f7deaa1a 165
5a738aea
MS
166 <dt>CONTENT_TYPE</dt>
167 <dd>The MIME type associated with the file (e.g.
168 application/postscript).</dd>
f7deaa1a 169
5a738aea 170 <dt>CUPS_CACHEDIR</dt>
79e1d494
MS
171 <dd>The directory where cache files can be stored. Cache files can be
172 used to retain information between jobs or files in a job.</dd>
f7deaa1a 173
5a738aea 174 <dt>CUPS_DATADIR</dt>
79e1d494 175 <dd>The directory where (read-only) CUPS data files can be found.</dd>
f7deaa1a 176
0af14961
MS
177 <dt>CUPS_FILETYPE</dt>
178 <dd>The type of file being printed: "job-sheet" for a banner page and
179 "document" for a regular print file.</dd>
180
5a738aea
MS
181 <dt>CUPS_SERVERROOT</dt>
182 <dd>The root directory of the server.</dd>
f7deaa1a 183
5a738aea
MS
184 <dt>DEVICE_URI</dt>
185 <dd>The device-uri associated with the printer.</dd>
f7deaa1a 186
5a738aea
MS
187 <dt>FINAL_CONTENT_TYPE</dt>
188 <dd>The MIME type associated with the printer (e.g.
189 application/vnd.cups-postscript).</dd>
f7deaa1a 190
5a738aea
MS
191 <dt>LANG</dt>
192 <dd>The language locale associated with the job.</dd>
f7deaa1a 193
5a738aea
MS
194 <dt>PPD</dt>
195 <dd>The full pathname of the PostScript Printer Description (PPD)
196 file for this printer.</dd>
f7deaa1a 197
5a738aea 198 <dt>PRINTER</dt>
79e1d494 199 <dd>The queue name of the class or printer.</dd>
f7deaa1a 200
5a738aea
MS
201 <dt>RIP_CACHE</dt>
202 <dd>The recommended amount of memory to use for Raster Image
203 Processors (RIPs).</dd>
f7deaa1a 204
79e1d494
MS
205 <dt>TMPDIR</dt>
206 <dd>The directory where temporary files should be created.</dd>
207
5a738aea 208</dl>
f7deaa1a 209
5a738aea 210<h3><a name="MESSAGES">Communicating with the Scheduler</a></h3>
f7deaa1a 211
79e1d494
MS
212<p>Filters and backends communicate with the scheduler by writing messages
213to the standard error file. The scheduler reads messages from all filters in
214a job and processes the message based on its prefix. For example, the following
215code sets the current printer state message to "Printing page 5":</p>
f7deaa1a 216
5a738aea
MS
217<pre class="example">
218int page = 5;
f7deaa1a 219
5a738aea
MS
220fprintf(stderr, "INFO: Printing page %d\n", page);
221</pre>
f7deaa1a 222
5a738aea
MS
223<p>Each message is a single line of text starting with one of the following
224prefix strings:</p>
225
226<dl class="code">
227
228 <dt>ALERT: message</dt>
229 <dd>Sets the printer-state-message attribute and adds the specified
230 message to the current error log file using the "alert" log level.</dd>
231
232 <dt>ATTR: attribute=value [attribute=value]</dt>
233 <dd>Sets the named printer or job attribute(s). Typically this is used
88f9aafc
MS
234 to set the <code>marker-colors</code>, <code>marker-high-levels</code>,
235 <code>marker-levels</code>, <code>marker-low-levels</code>,
75bd9771
MS
236 <code>marker-message</code>, <code>marker-names</code>,
237 <code>marker-types</code>, <code>printer-alert</code>, and
238 <code>printer-alert-description</code> printer attributes. Standard
239 <code>marker-types</code> values are listed in <a href='#TABLE1'>Table
240 1</a>.</dd>
5a738aea
MS
241
242 <dt>CRIT: message</dt>
243 <dd>Sets the printer-state-message attribute and adds the specified
244 message to the current error log file using the "critical" log
245 level.</dd>
246
247 <dt>DEBUG: message</dt>
248 <dd>Sets the printer-state-message attribute and adds the specified
249 message to the current error log file using the "debug" log level.</dd>
250
251 <dt>DEBUG2: message</dt>
252 <dd>Sets the printer-state-message attribute and adds the specified
253 message to the current error log file using the "debug2" log level.</dd>
254
255 <dt>EMERG: message</dt>
256 <dd>Sets the printer-state-message attribute and adds the specified
257 message to the current error log file using the "emergency" log
258 level.</dd>
259
260 <dt>ERROR: message</dt>
261 <dd>Sets the printer-state-message attribute and adds the specified
79e1d494
MS
262 message to the current error log file using the "error" log level.
263 Use "ERROR:" messages for non-persistent processing errors.</dd>
5a738aea
MS
264
265 <dt>INFO: message</dt>
266 <dd>Sets the printer-state-message attribute. If the current log level
267 is set to "debug2", also adds the specified message to the current error
268 log file using the "info" log level.</dd>
269
270 <dt>NOTICE: message</dt>
271 <dd>Sets the printer-state-message attribute and adds the specified
272 message to the current error log file using the "notice" log level.</dd>
273
274 <dt>PAGE: page-number #-copies</dt>
275 <dt>PAGE: total #-pages</dt>
276 <dd>Adds an entry to the current page log file. The first form adds
277 #-copies to the job-media-sheets-completed attribute. The second
278 form sets the job-media-sheets-completed attribute to #-pages.</dd>
279
20fbc903
MS
280 <dt>PPD: keyword=value [keyword=value ...]</dt>
281 <dd>Changes or adds keywords to the printer's PPD file. Typically
282 this is used to update installable options or default media settings
283 based on the printer configuration.</dd>
284
5a738aea
MS
285 <dt>STATE: + printer-state-reason [printer-state-reason ...]</dt>
286 <dt>STATE: - printer-state-reason [printer-state-reason ...]</dt>
88f9aafc
MS
287 <dd>Sets or clears printer-state-reason keywords for the current queue.
288 Typically this is used to indicate persistent media, ink, toner, and
289 configuration conditions or errors on a printer.
79e1d494 290 <a href='#TABLE2'>Table 2</a> lists the standard state keywords -
88f9aafc
MS
291 use vendor-prefixed ("com.example.foo") keywords for custom states. See
292 <a href="#MANAGING_STATE">Managing Printer State in a Filter</a> for more
293 information.
5a738aea
MS
294
295 <dt>WARNING: message</dt>
296 <dd>Sets the printer-state-message attribute and adds the specified
297 message to the current error log file using the "warning" log
298 level.</dd>
299
300</dl>
301
302<p>Messages without one of these prefixes are treated as if they began with
303the "DEBUG:" prefix string.</p>
304
79e1d494
MS
305<div class='table'><table width='80%' summary='Table 1: Standard marker-types Values'>
306<caption>Table 1: <a name='TABLE1'>Standard marker-types Values</a></caption>
307<thead>
308<tr>
309 <th>marker-type</th>
310 <th>Description</th>
311</tr>
312</thead>
313<tbody>
314<tr>
315 <td>developer</td>
316 <td>Developer unit</td>
317</tr>
318<tr>
319 <td>fuser</td>
320 <td>Fuser unit</td>
321</tr>
322<tr>
323 <td>fuserCleaningPad</td>
324 <td>Fuser cleaning pad</td>
325</tr>
326<tr>
327 <td>fuserOil</td>
328 <td>Fuser oil</td>
329</tr>
330<tr>
331 <td>ink</td>
332 <td>Ink supply</td>
333</tr>
334<tr>
335 <td>opc</td>
336 <td>Photo conductor</td>
337</tr>
338<tr>
339 <td>solidWax</td>
340 <td>Wax supply</td>
341</tr>
342<tr>
343 <td>staples</td>
344 <td>Staple supply</td>
345</tr>
346<tr>
347 <td>toner</td>
348 <td>Toner supply</td>
349</tr>
350<tr>
351 <td>transferUnit</td>
352 <td>Transfer unit</td>
353</tr>
354<tr>
355 <td>wasteInk</td>
356 <td>Waste ink tank</td>
357</tr>
358<tr>
359 <td>wasteToner</td>
360 <td>Waste toner tank</td>
361</tr>
362<tr>
363 <td>wasteWax</td>
364 <td>Waste wax tank</td>
365</tr>
366</tbody>
367</table></div>
368
369<br>
370
371<div class='table'><table width='80%' summary='Table 2: Standard State Keywords'>
372<caption>Table 2: <a name='TABLE2'>Standard State Keywords</a></caption>
373<thead>
374<tr>
375 <th>Keyword</th>
376 <th>Description</th>
377</tr>
378</thead>
379<tbody>
380<tr>
381 <td>connecting-to-device</td>
88f9aafc 382 <td>Connecting to printer but not printing yet.</td>
79e1d494
MS
383</tr>
384<tr>
385 <td>cover-open</td>
88f9aafc 386 <td>The printer's cover is open.</td>
79e1d494
MS
387</tr>
388<tr>
389 <td>input-tray-missing</td>
88f9aafc 390 <td>The paper tray is missing.</td>
79e1d494
MS
391</tr>
392<tr>
393 <td>marker-supply-empty</td>
88f9aafc 394 <td>The printer is out of ink.</td>
79e1d494
MS
395</tr>
396<tr>
397 <td>marker-supply-low</td>
88f9aafc 398 <td>The printer is almost out of ink.</td>
79e1d494
MS
399</tr>
400<tr>
401 <td>marker-waste-almost-full</td>
88f9aafc 402 <td>The printer's waste bin is almost full.</td>
79e1d494
MS
403</tr>
404<tr>
405 <td>marker-waste-full</td>
88f9aafc 406 <td>The printer's waste bin is full.</td>
79e1d494
MS
407</tr>
408<tr>
409 <td>media-empty</td>
88f9aafc 410 <td>The paper tray (any paper tray) is empty.</td>
79e1d494
MS
411</tr>
412<tr>
413 <td>media-jam</td>
88f9aafc 414 <td>There is a paper jam.</td>
79e1d494
MS
415</tr>
416<tr>
417 <td>media-low</td>
88f9aafc
MS
418 <td>The paper tray (any paper tray) is almost empty.</td>
419</tr>
420<tr>
421 <td>media-needed</td>
422 <td>The paper tray needs to be filled (for a job that is printing).</td>
79e1d494
MS
423</tr>
424<tr>
425 <td>paused</td>
88f9aafc 426 <td>Stop the printer.</td>
79e1d494
MS
427</tr>
428<tr>
429 <td>timed-out</td>
88f9aafc 430 <td>Unable to connect to printer.</td>
79e1d494
MS
431</tr>
432<tr>
433 <td>toner-empty</td>
88f9aafc 434 <td>The printer is out of toner.</td>
79e1d494
MS
435</tr>
436<tr>
437 <td>toner-low</td>
88f9aafc
MS
438 <td>The printer is low on toner.</td>
439</tr>
440</tbody>
441</table></div>
442
443<h4><a name="MANAGING_STATE">Managing Printer State in a Filter</a></h4>
444
445<p>Filters are responsible for managing the state keywords they set using
446"STATE:" messages. Typically you will update <em>all</em> of the keywords that
447are used by the filter at startup, for example:</p>
448
449<pre class="example">
450if (foo_condition != 0)
451 fputs("STATE: +com.example.foo\n", stderr);
452else
453 fputs("STATE: -com.example.foo\n", stderr);
454
455if (bar_condition != 0)
456 fputs("STATE: +com.example.bar\n", stderr);
457else
458 fputs("STATE: -com.example.bar\n", stderr);
459</pre>
460
461<p>Then as conditions change, your filter sends "STATE: +keyword" or "STATE:
462-keyword" messages as necessary to set or clear the corresponding keyword,
463respectively.</p>
464
465<p>State keywords are often used to notify the user of issues that span across
466jobs, for example "media-empty-warning" that indicates one or more paper trays
467are empty. These keywords should not be cleared unless the corresponding issue
468no longer exists.</p>
469
470<p>Filters should clear job-related keywords on startup and exit so that they
471do not remain set between jobs. For example, "connecting-to-device" is a job
472sub-state and not an issue that applies when a job is not printing.</p>
473
474<blockquote><b>Note:</b>
475
476<p>"STATE:" messages often provide visible alerts to the user. For example,
f3c17241 477on OS X setting a printer-state-reason value with an "-error" or
88f9aafc
MS
478"-warning" suffix will cause the printer's dock item to bounce if the
479corresponding reason is localized with a cupsIPPReason keyword in the
480printer's PPD file.</p>
481
482<p>When providing a vendor-prefixed keyword, <em>always</em> provide the
483corresponding standard keyword (if any) to allow clients to respond to the
484condition correctly. For example, if you provide a vendor-prefixed keyword
485for a low cyan ink condition ("com.example.cyan-ink-low") you must also set the
486"marker-supply-low-warning" keyword. In such cases you should also refrain
487from localizing the vendor-prefixed keyword in the PPD file - otherwise both
488the generic and vendor-specific keyword will be shown in the user
489interface.</p>
490
491</blockquote></dd>
492
493<h4><a name="REPORTING_SUPPLIES">Reporting Supply Levels</a></h4>
494
495<p>CUPS tracks several "marker-*" attributes for ink/toner supply level
496reporting. These attributes allow applications to display the current supply
497levels for a printer without printer-specific software. <a href="#TABLE3">Table 3</a> lists the marker attributes and what they represent.</p>
498
499<p>Filters set marker attributes by sending "ATTR:" messages to stderr. For
500example, a filter supporting an inkjet printer with black and tri-color ink
501cartridges would use the following to initialize the supply attributes:</p>
502
503<pre class="example">
504fputs("ATTR: marker-colors=#000000,#00FFFF#FF00FF#FFFF00\n", stderr);
505fputs("ATTR: marker-low-levels=5,10\n", stderr);
506fputs("ATTR: marker-names=Black,Tri-Color\n", stderr);
507fputs("ATTR: marker-types=ink,ink\n", stderr);
508</pre>
509
510<p>Then periodically the filter queries the printer for its current supply
511levels and updates them with a separate "ATTR:" message:</p>
512
513<pre class="example">
514int black_level, tri_level;
515...
516fprintf(stderr, "ATTR: marker-levels=%d,%d\n", black_level, tri_level);
517</pre>
518
519<div class='table'><table width='80%' summary='Table 3: Supply Level Attributes'>
520<caption>Table 3: <a name='TABLE3'>Supply Level Attributes</a></caption>
521<thead>
522<tr>
523 <th>Attribute</th>
524 <th>Description</th>
525</tr>
526</thead>
527<tbody>
528<tr>
529 <td>marker-colors</td>
530 <td>A list of comma-separated colors; each color is either "none" or one or
531 more hex-encoded sRGB colors of the form "#RRGGBB".</td>
532</tr>
533<tr>
534 <td>marker-high-levels</td>
535 <td>A list of comma-separated "almost full" level values from 0 to 100; a
536 value of 100 should be used for supplies that are consumed/emptied like ink
537 cartridges.</td>
538</tr>
539<tr>
540 <td>marker-levels</td>
541 <td>A list of comma-separated level values for each supply. A value of -1
542 indicates the level is unavailable, -2 indicates unknown, and -3 indicates
543 the level is unknown but has not yet reached capacity. Values from 0 to 100
544 indicate the corresponding percentage.</td>
545</tr>
546<tr>
547 <td>marker-low-levels</td>
548 <td>A list of comma-separated "almost empty" level values from 0 to 100; a
549 value of 0 should be used for supplies that are filled like waste ink
550 tanks.</td>
551</tr>
552<tr>
553 <td>marker-message</td>
554 <td>A human-readable supply status message for the user like "12 pages of
555 ink remaining."</td>
556</tr>
557<tr>
558 <td>marker-names</td>
559 <td>A list of comma-separated supply names like "Cyan Ink", "Fuser",
560 etc.</td>
561</tr>
562<tr>
563 <td>marker-types</td>
564 <td>A list of comma-separated supply types; the types are listed in
565 <a href="#TABLE1">Table 1</a>.</td>
79e1d494
MS
566</tr>
567</tbody>
568</table></div>
569
20fbc903 570<h3><a name="COMMUNICATING_BACKEND">Communicating with the Backend</a></h3>
5a738aea
MS
571
572<p>Filters can communicate with the backend via the
573<a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> and
574<a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
22c9029b 575functions. The
5a738aea
MS
576<a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> function
577reads data that has been sent back from the device and is typically used to
578obtain status and configuration information. For example, the following code
579polls the backend for back-channel data:</p>
580
581<pre class="example">
582#include &lt;cups/cups.h&gt;
f7deaa1a 583
5a738aea
MS
584char buffer[8192];
585ssize_t bytes;
f7deaa1a 586
5a738aea
MS
587/* Use a timeout of 0.0 seconds to poll for back-channel data */
588bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);
f7deaa1a 589</pre>
590
79e1d494
MS
591<p>Filters can also use <code>select()</code> or <code>poll()</code> on the
592back-channel file descriptor (3 or <code>CUPS_BC_FD</code>) to read data only
593when it is available.</p>
594
595<p>The
5a738aea
MS
596<a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
597function allows you to get out-of-band status information and do synchronization
598with the device. For example, the following code gets the current IEEE-1284
599device ID string from the backend:</p>
f7deaa1a 600
5a738aea 601<pre class="example">
f7deaa1a 602#include &lt;cups/sidechannel.h&gt;
603
604char data[2049];
605int datalen;
5a738aea 606<a href="#cups_sc_status_t">cups_sc_status_t</a> status;
f7deaa1a 607
79e1d494
MS
608/* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for
609 nul-termination... */
f7deaa1a 610datalen = sizeof(data) - 1;
611
612/* Get the IEEE-1284 device ID, waiting for up to 1 second */
5a738aea 613status = <a href="#cupsSideChannelDoRequest">cupsSideChannelDoRequest</a>(CUPS_SC_CMD_GET_DEVICE_ID, data, &amp;datalen, 1.0);
f7deaa1a 614
615/* Use the returned value if OK was returned and the length is non-zero */
616if (status == CUPS_SC_STATUS_OK && datalen > 0)
617 data[datalen] = '\0';
618else
619 data[0] = '\0';
620</pre>
621
88f9aafc
MS
622<h4><a name="DRAIN_OUTPUT">Forcing All Output to a Printer</a></h4>
623
624<p>The
625<a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
626function allows you to tell the backend to send all pending data to the printer.
627This is most often needed when sending query commands to the printer. For example:</p>
628
629<pre class="example">
630#include &lt;cups/cups.h&gt;
631#include &lt;cups/sidechannel.h&gt;
632
633char data[1024];
634int datalen = sizeof(data);
635<a href="#cups_sc_status_t">cups_sc_status_t</a> status;
636
637/* Flush pending output to stdout */
638fflush(stdout);
639
640/* Drain output to backend, waiting for up to 30 seconds */
641status = <a href="#cupsSideChannelDoRequest">cupsSideChannelDoRequest</a>(CUPS_SC_CMD_DRAIN_OUTPUT, data, &amp;datalen, 30.0);
642
643/* Read the response if the output was sent */
644if (status == CUPS_SC_STATUS_OK)
645{
646 ssize_t bytes;
647
648 /* Wait up to 10.0 seconds for back-channel data */
649 bytes = cupsBackChannelRead(data, sizeof(data), 10.0);
650 /* do something with the data from the printer */
651}
652</pre>
653
20fbc903
MS
654<h3><a name="COMMUNICATING_FILTER">Communicating with Filters</a></h3>
655
5a738aea
MS
656<p>Backends communicate with filters using the reciprocal functions
657<a href="#cupsBackChannelWrite"><code>cupsBackChannelWrite</code></a>,
658<a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>, and
659<a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a>. We
660recommend writing back-channel data using a timeout of 1.0 seconds:</p>
f7deaa1a 661
5a738aea
MS
662<pre class="example">
663#include &lt;cups/cups.h&gt;
f7deaa1a 664
5a738aea
MS
665char buffer[8192];
666ssize_t bytes;
f7deaa1a 667
79e1d494
MS
668/* Obtain data from printer/device */
669...
670
5a738aea
MS
671/* Use a timeout of 1.0 seconds to give filters a chance to read */
672cupsBackChannelWrite(buffer, bytes, 1.0);
f7deaa1a 673</pre>
674
5a738aea
MS
675<p>The <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>
676function reads a side-channel command from a filter, driver, or port monitor.
677Backends can either poll for commands using a <code>timeout</code> of 0.0, wait
678indefinitely for commands using a <code>timeout</code> of -1.0 (probably in a
679separate thread for that purpose), or use <code>select</code> or
680<code>poll</code> on the <code>CUPS_SC_FD</code> file descriptor (4) to handle
20fbc903 681input and output on several file descriptors at the same time.</p>
5a738aea
MS
682
683<p>Once a command is processed, the backend uses the
684<a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a> function
685to send its response. For example, the following code shows how to poll for a
686side-channel command and respond to it:</p>
687
688<pre class="example">
f7deaa1a 689#include &lt;cups/sidechannel.h&gt;
690
5a738aea
MS
691<a href="#cups_sc_command_t">cups_sc_command_t</a> command;
692<a href="#cups_sc_status_t">cups_sc_status_t</a> status;
20fbc903
MS
693char data[2048];
694int datalen = sizeof(data);
f7deaa1a 695
696/* Poll for a command... */
20fbc903 697if (!<a href="#cupsSideChannelRead">cupsSideChannelRead</a>(&amp;command, &amp;status, data, &amp;datalen, 0.0))
f7deaa1a 698{
f7deaa1a 699 switch (command)
700 {
20fbc903 701 /* handle supported commands, fill data/datalen/status with values as needed */
f7deaa1a 702
703 default :
704 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
705 datalen = 0;
706 break;
707 }
708
709 /* Send a response... */
5a738aea 710 <a href="#cupsSideChannelWrite">cupsSideChannelWrite</a>(command, status, data, datalen, 1.0);
f7deaa1a 711}
712</pre>
ac884b6a
MS
713
714<h3><a name="SNMP">Doing SNMP Queries with Network Printers</a></h3>
715
716<p>The Simple Network Management Protocol (SNMP) allows you to get the current
717status, page counter, and supply levels from most network printers. Every
718piece of information is associated with an Object Identifier (OID), and
719every printer has a <em>community</em> name associated with it. OIDs can be
720queried directly or by "walking" over a range of OIDs with a common prefix.</p>
721
20fbc903
MS
722<p>The two CUPS SNMP functions provide a simple API for querying network
723printers through the side-channel interface. Each accepts a string containing
724an OID like ".1.3.6.1.2.1.43.10.2.1.4.1.1" (the standard page counter OID)
725along with a timeout for the query.</p>
ac884b6a 726
20fbc903
MS
727<p>The <a href="#cupsSideChannelSNMPGet"><code>cupsSideChannelSNMPGet</code></a>
728function queries a single OID and returns the value as a string in a buffer
729you supply:</p>
ac884b6a
MS
730
731<pre class="example">
20fbc903 732#include &lt;cups/sidechannel.h&gt;
ac884b6a 733
20fbc903
MS
734char data[512];
735int datalen = sizeof(data);
ac884b6a 736
20fbc903
MS
737if (<a href="#cupsSideChannelSNMPGet">cupsSideChannelSNMPGet</a>(".1.3.6.1.2.1.43.10.2.1.4.1.1", data, &amp;datalen, 5.0)
738 == CUPS_SC_STATUS_OK)
ac884b6a
MS
739{
740 /* Do something with the value */
20fbc903 741 printf("Page counter is: %s\n", data);
ac884b6a
MS
742}
743</pre>
744
20fbc903
MS
745<p>The
746<a href="#cupsSideChannelSNMPWalk"><code>cupsSideChannelSNMPWalk</code></a>
747function allows you to query a whole group of OIDs, calling a function of your
748choice for each OID that is found:</p>
ac884b6a
MS
749
750<pre class="example">
20fbc903 751#include &lt;cups/sidechannel.h&gt;
ac884b6a
MS
752
753void
20fbc903 754my_callback(const char *oid, const char *data, int datalen, void *context)
ac884b6a
MS
755{
756 /* Do something with the value */
20fbc903 757 printf("%s=%s\n", oid, data);
ac884b6a
MS
758}
759
20fbc903
MS
760...
761
ac884b6a
MS
762void *my_data;
763
20fbc903 764<a href="#cupsSideChannelSNMPWalk">cupsSNMPSideChannelWalk</a>(".1.3.6.1.2.1.43", 5.0, my_callback, my_data);
ac884b6a 765</pre>