1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
3 <!-- SECTION: Programming -->
5 <title>Filter and Backend APIs
</title>
6 <meta name='keywords' content='Programming'
>
7 <meta name='creator' content='Mini-XML v2.3'
>
8 <style type='text/css'
><!--
9 h1, h2, h3, p { font-family: sans-serif; text-align: justify; }
10 tt, pre a:link, pre a:visited, tt a:link, tt a:visited { font-weight: bold; color: #7f0000; }
11 pre { font-weight: bold; color: #7f0000; margin-left: 2em; }
12 span.info { background: #000000; border: solid thin #000000; color: #ffffff; font-size: 80%; font-style: italic; font-weight: bold; white-space: nowrap; }
13 h3 span.info { float: right; font-size: 100%; }
14 h1.title, h2.title, h3.title { border-bottom: solid 2px #000000; }
19 "$Id: api-filter.shtml 6649 2007-07-11 21:46:42Z mike $"
21 Filter and backend API introduction for the Common UNIX Printing System (CUPS).
23 Copyright 2007 by Apple Inc.
24 Copyright 1997-2006 by Easy Software Products, all rights reserved.
26 These coded instructions, statements, and computer programs are the
27 property of Apple Inc. and are protected by Federal copyright
28 law. Distribution and use rights are outlined in the file "LICENSE.txt"
29 which should have been included with this file. If this file is
30 file is missing or damaged, see the license at "http://www.cups.org/".
33 <h2 class='title'
>Introduction
</h2>
35 <p>The CUPS filter and backend APIs define standard exit codes
36 and provide access to the backchannel data stream. They are only
37 used when writing backends, filters, and port monitors.
</p>
39 <h2 class='title'
>General Usage
</h2>
41 <p>The
<var><cups/backend.h
></var> and
42 <var><cups/cups.h
></var> header files must be included to
43 use the
<tt>CUPS_BACKEND_
</tt> constants and
44 <tt>cupsBackChannel
</tt> functions, respectively.
</p>
46 <p>The
<var><cups/sidechannel.h
></var> header file must be
47 included to use the
<tt>CUPS_SC_
</tt> constants and
<tt>cupsSideChannel
</tt> functions.
</p>
49 <p>Programs using these functions must be linked to the CUPS
50 library:
<var>libcups.a
</var>,
<var>libcups.so
.2</var>,
51 <var>libcups
.2.dylib
</var>,
<var>libcups_s.a
</var>, or
52 <var>libcups2.lib
</var> depending on the platform. The following
53 command compiles
<var>myprogram.c
</var> using GCC and the CUPS
57 <kbd>gcc -o myprogram myprogram.c -lcups
</kbd>
61 <h2 class='title'
>Compatibility
</h2>
63 <p>The
<tt>cupsBackChannel
</tt> functions require CUPS
1.2 or higher. The
<tt>cupsSideChannel
</tt> functions require CUPS
1.3 or higher.
</p>
66 <h2 class='title'
>Using the cupsBackChannel APIs
</h2>
68 <p>The
<tt>cupsBackChannel
</tt> APIs allow your filters, drivers, and port monitors to read data back from a printer and your backends to send data from a printer to the filters, drivers, and port monitors associated with the current job. Back-channel data is normally sent by the printer in response to a command sent from your program to the printer via
<tt>stdout
</tt>.
</p>
70 <p>The
<tt>cupsBackChannelRead()
</tt> function reads data from the printer via the backend. You provide a timeout in seconds along with a buffer pointer and the size of that buffer. It returns the number of bytes or -
1 if there was an error. The following code example shows how to poll for back-channel data in your program:
</p>
73 #include
<cups/cups.h
>
78 /* Use a timeout of
0.0 seconds to poll for back-channel data */
79 bytes = cupsBackChannelRead(buffer, sizeof(buffer),
0.0);
82 <p>If you are writing a backend, the
<tt>cupsBackChannelWrite()
</tt> function sends any back-channel data you have received from the printer to upstream filters in the print filter chain. We recommend using a timeout of
1.0 seconds:
</p>
85 #include
<cups/cups.h
>
90 /* Use a timeout of
1.0 seconds to give filters a chance to read */
91 cupsBackChannelWrite(buffer, bytes,
1.0);
95 <h2 class='title'
>Using the cupsSideChannel APIs
</h2>
97 <p>The
<tt>cupsSideChannel
</tt> APIs allow your filters, drivers, port monitors, and backend to send and receive the following out-of-band commands:
</p>
101 <li><tt>CUPS_SC_CMD_SOFT_RESET
</tt> - Do a soft reset
</li>
102 <li><tt>CUPS_SC_CMD_DRAIN_OUTPUT
</tt> - Drain all pending output
</li>
103 <li><tt>CUPS_SC_CMD_GET_BIDI
</tt> - Return bidirectional capabilities
</li>
104 <li><tt>CUPS_SC_CMD_GET_DEVICE_ID
</tt> - Return the IEEE-
1284 device ID
</li>
105 <li><tt>CUPS_SC_CMD_GET_STATE
</tt> - Return the device state
</li>
110 <h3>Sending Commands from a Filter, Driver, or Port Monitor
</h3>
112 <p>The
<tt>cupsSideChannelDoRequest()
</tt> function is used by filters, drivers, and port monitors to send a command to the backend and read back a response:
</p>
114 <pre class='command'
>
115 cups_sc_status_t cupsSideChannelDoRequest(cups_sc_command_t command,
116 char *data, int *datalen,
120 <p>The
<tt>CUPS_SC_CMD_SOFT_RESET
</tt> and
<tt>CUPS_SC_CMD_DRAIN_OUTPUT
</tt> commands do not return any data values, while the others return one or more bytes. The
<tt>timeout
</tt> parameter allows your program to poll or wait for the command to complete - use a timeout of
30 seconds for
<tt>CUPS_SC_CMD_SOFT_RESET
</tt> and
<tt>CUPS_SC_CMD_DRAIN_OUTPUT
</tt> and a timeout of
1 second for all other commands.
</p>
122 <p><tt>CUPS_SC_CMD_GET_BIDI
</tt> returns a single
<tt>char
</tt> value that tells you whether the backend supports bidirectional communications:
</p>
124 <pre class='command'
>
125 #include
<cups/sidechannel.h
>
130 cups_sc_status_t status;
132 /* Tell cupsSideChannelDoRequest() how big our buffer is... */
135 /* Get the bidirectional capabilities, waiting for up to
1 second */
136 status = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI,
&data,
&datalen,
1.0);
138 /* Use the returned value if OK was returned and the length is still
1 */
139 if (status == CUPS_SC_STATUS_OK && datalen ==
1)
140 bidi = (cups_sc_bidi_t)data;
142 bidi = CUPS_SC_BIDI_NOT_SUPPORTED;
145 <p><tt>CUPS_SC_CMD_GET_DEVICE_ID
</tt> returns a string of characters containing the IEEE-
1284 device ID for the connected printer:
</p>
147 <pre class='command'
>
148 #include
<cups/sidechannel.h
>
152 cups_sc_status_t status;
154 /* Tell cupsSideChannelDoRequest() how big our buffer is, less
1 byte for nul-termination... */
155 datalen = sizeof(data) -
1;
157 /* Get the IEEE-
1284 device ID, waiting for up to
1 second */
158 status = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_DEVICE_ID, data,
&datalen,
1.0);
160 /* Use the returned value if OK was returned and the length is non-zero */
161 if (status == CUPS_SC_STATUS_OK && datalen
> 0)
162 data[datalen] = '\
0';
167 <p><tt>CUPS_SC_CMD_GET_STATE
</tt> returns a single
<tt>char
</tt> value that tells you the current device state:
</p>
169 <pre class='command'
>
170 #include
<cups/sidechannel.h
>
174 cups_sc_state_t state;
175 cups_sc_status_t status;
177 /* Tell cupsSideChannelDoRequest() how big our buffer is... */
180 /* Get the bidirectional capabilities, waiting for up to
1 second */
181 status = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_STATE,
&data,
&datalen,
1.0);
183 /* Use the returned value if OK was returned and the length is still
1 */
184 if (status == CUPS_SC_STATUS_OK && datalen ==
1)
185 state = (cups_sc_state_t)data;
187 state = CUPS_SC_STATE_OFFLINE;
191 <h3>Handling Commands in your Backend
</h3>
193 <p>The
<tt>cupsSideChannelRead()
</tt> function is used by backends to read a command from a filter, driver, or port monitor:
</p>
195 <pre class='command'
>
196 int cupsSideChannelRead(cups_sc_command_t
&command,
197 cups_sc_status_t
&status,
198 char *data, int *datalen, double timeout);
201 <p>Backends can either poll for commands using a
<tt>timeout
</tt> of
0.0, wait indefinitely for commands using a
<tt>timeout
</tt> of -
1.0 (probably in a separate thread for that purpose), or use
<tt>select()
</tt> or
<tt>poll()
</tt> on the
<tt>CUPS_SC_FD
</tt> file descriptor (
4) to handle input and output on several file descriptors at the same time. Backends can pass
<tt>NULL
</tt> for the
<tt>data
</tt> and
<tt>datalen
</tt> parameters, since none of the commands sent by upstream filters contain any data at this time.
</p>
203 <p>Once a command is processed, the backend uses the
<tt>cupsSideChannelWrite()
</tt> function to send its response:
</p>
205 <pre class='command'
>
206 #include
<cups/sidechannel.h
>
208 cups_sc_command_t command;
209 cups_sc_status_t status;
211 /* Poll for a command... */
212 if (!cupsSideChannelRead(
&command,
&status, NULL, NULL,
0.0))
219 ... handle supported commands, file data/datalen/status with values as needed ...
222 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
227 /* Send a response... */
228 cupsSideChannelWrite(command, status, data, datalen,
1.0);
231 <h2 class='title'
>Contents
</h2>
233 <li><a href='#FUNCTIONS'
>Functions
</a></li>
236 <h2 class='title'
><a name='FUNCTIONS'
>Functions
</a></h2>
238 <li><a href='#cupsBackChannelRead'
><tt>cupsBackChannelRead()
</tt></a> <span class='info'
> CUPS
1.2 </span></li>
239 <li><a href='#cupsBackChannelWrite'
><tt>cupsBackChannelWrite()
</tt></a> <span class='info'
> CUPS
1.2 </span></li>
240 <li><a href='#cupsSideChannelDoRequest'
><tt>cupsSideChannelDoRequest()
</tt></a> <span class='info'
> CUPS
1.3 </span></li>
241 <li><a href='#cupsSideChannelRead'
><tt>cupsSideChannelRead()
</tt></a> <span class='info'
> CUPS
1.3 </span></li>
242 <li><a href='#cupsSideChannelWrite'
><tt>cupsSideChannelWrite()
</tt></a> <span class='info'
> CUPS
1.3 </span></li>
245 <h3 class='title'
><span class='info'
> CUPS
1.2 </span><a name='cupsBackChannelRead'
>cupsBackChannelRead()
</a></h3>
247 <p>Read data from the backchannel.
248 <p>Reads up to
"bytes
" bytes from the backchannel. The
"timeout
"
249 parameter controls how many seconds to wait for the data - use
250 0.0 to return immediately if there is no data, -
1.0 to wait
251 for data indefinitely.
263 <div class='table'
><table align='center' border='
1' width='
80%' cellpadding='
5' cellspacing='
0' summary='Arguments'
>
264 <thead><tr><th>Name
</th><th>Description
</th></tr></thead>
266 <tr><td><tt>buffer
</tt></td><td>Buffer to read
</td></tr>
267 <tr><td><tt>bytes
</tt></td><td>Bytes to read
</td></tr>
268 <tr><td><tt>timeout
</tt></td><td>Timeout in seconds
</td></tr>
269 </tbody></table></div>
271 <p>Bytes read or -
1 on error
</p>
273 <h3 class='title'
><span class='info'
> CUPS
1.2 </span><a name='cupsBackChannelWrite'
>cupsBackChannelWrite()
</a></h3>
275 <p>Write data to the backchannel.
276 <p>Writes
"bytes
" bytes to the backchannel. The
"timeout
" parameter
277 controls how many seconds to wait for the data to be written - use
278 0.0 to return immediately if the data cannot be written, -
1.0 to wait
285 cupsBackChannelWrite(
291 <div class='table'
><table align='center' border='
1' width='
80%' cellpadding='
5' cellspacing='
0' summary='Arguments'
>
292 <thead><tr><th>Name
</th><th>Description
</th></tr></thead>
294 <tr><td><tt>buffer
</tt></td><td>Buffer to write
</td></tr>
295 <tr><td><tt>bytes
</tt></td><td>Bytes to write
</td></tr>
296 <tr><td><tt>timeout
</tt></td><td>Timeout in seconds
</td></tr>
297 </tbody></table></div>
299 <p>Bytes written or -
1 on error
</p>
301 <h3 class='title'
><span class='info'
> CUPS
1.3 </span><a name='cupsSideChannelDoRequest'
>cupsSideChannelDoRequest()
</a></h3>
303 <p>Send a side-channel command to a backend and wait for a response.
304 <p>This function is normally only called by filters, drivers, or port
305 monitors in order to communicate with the backend used by the current
306 printer. Programs must be prepared to handle timeout or
"not
307 implemented
" status codes, which indicate that the backend or device
308 do not support the specified side-channel command.
309 <p>The
"datalen
" parameter must be initialized to the size of the buffer
310 pointed to by the
"data
" parameter. cupsSideChannelDoRequest() will
311 update the value to contain the number of data bytes in the buffer.
316 <a href='#cups_sc_status_t'
>cups_sc_status_t
</a><br>
317 cupsSideChannelDoRequest(
318 cups_sc_command_t command,
324 <div class='table'
><table align='center' border='
1' width='
80%' cellpadding='
5' cellspacing='
0' summary='Arguments'
>
325 <thead><tr><th>Name
</th><th>Description
</th></tr></thead>
327 <tr><td><tt>command
</tt></td><td>Command to send
</td></tr>
328 <tr><td><tt>data
</tt></td><td>Response data buffer pointer
</td></tr>
329 <tr><td><tt>datalen
</tt></td><td>Size of data buffer on entry, number of bytes in buffer on return
</td></tr>
330 <tr><td><tt>timeout
</tt></td><td>Timeout in seconds
</td></tr>
331 </tbody></table></div>
333 <p>Status of command
</p>
335 <h3 class='title'
><span class='info'
> CUPS
1.3 </span><a name='cupsSideChannelRead'
>cupsSideChannelRead()
</a></h3>
337 <p>Read a side-channel message.
338 <p>This function is normally only called by backend programs to read
339 commands from a filter, driver, or port monitor program. The
340 caller must be prepared to handle incomplete or invalid messages
341 and return the corresponding status codes.
342 <p>The
"datalen
" parameter must be initialized to the size of the buffer
343 pointed to by the
"data
" parameter. cupsSideChannelDoRequest() will
344 update the value to contain the number of data bytes in the buffer.
351 cups_sc_command_t * command,
352 <a href='#cups_sc_status_t'
>cups_sc_status_t
</a> * status,
358 <div class='table'
><table align='center' border='
1' width='
80%' cellpadding='
5' cellspacing='
0' summary='Arguments'
>
359 <thead><tr><th>Name
</th><th>Description
</th></tr></thead>
361 <tr><td><tt>command
</tt></td><td>Command code
</td></tr>
362 <tr><td><tt>status
</tt></td><td>Status code
</td></tr>
363 <tr><td><tt>data
</tt></td><td>Data buffer pointer
</td></tr>
364 <tr><td><tt>datalen
</tt></td><td>Size of data buffer on entry, number of bytes in buffer on return
</td></tr>
365 <tr><td><tt>timeout
</tt></td><td>Timeout in seconds
</td></tr>
366 </tbody></table></div>
368 <p>0 on success, -
1 on error
</p>
370 <h3 class='title'
><span class='info'
> CUPS
1.3 </span><a name='cupsSideChannelWrite'
>cupsSideChannelWrite()
</a></h3>
372 <p>Write a side-channel message.
373 <p>This function is normally only called by backend programs to send
374 responses to a filter, driver, or port monitor program.
380 cupsSideChannelWrite(
381 cups_sc_command_t command,
382 <a href='#cups_sc_status_t'
>cups_sc_status_t
</a> status,
388 <div class='table'
><table align='center' border='
1' width='
80%' cellpadding='
5' cellspacing='
0' summary='Arguments'
>
389 <thead><tr><th>Name
</th><th>Description
</th></tr></thead>
391 <tr><td><tt>command
</tt></td><td>Command code
</td></tr>
392 <tr><td><tt>status
</tt></td><td>Status code
</td></tr>
393 <tr><td><tt>data
</tt></td><td>Data buffer pointer
</td></tr>
394 <tr><td><tt>datalen
</tt></td><td>Number of bytes of data
</td></tr>
395 <tr><td><tt>timeout
</tt></td><td>Timeout in seconds
</td></tr>
396 </tbody></table></div>
398 <p>0 on success, -
1 on error
</p>