.TH pappl-device 3 "pappl device functions" "2020-12-08" "pappl device functions" .SH NAME pappl-device \- pappl device functions .SH LIBRARY Printer Application Framework (libpappl, "pkg-config --cflags --libs pappl") .SH SYNOPSIS .B #include .PP .I typedef struct _pappl_device_s .B pappl_device_t; .PP .I void .br .BI papplDeviceAddScheme "(const char *scheme, pappl_dtype_t dtype, pappl_devlist_cb_t list_cb, pappl_devopen_cb_t open_cb, pappl_devclose_cb_t close_cb, pappl_devread_cb_t read_cb, pappl_devwrite_cb_t write_cb, pappl_devstatus_cb_t status_cb);" .PP .I void .br .BI papplDeviceClose "(pappl_device_t *device);" .PP .I void .br .BI papplDeviceError "(pappl_device_t *device, const char *message, ...);" .PP .I void .br .BI papplDeviceFlush "(pappl_device_t *device);" .PP .I void * .br .BI papplDeviceGetData "(pappl_device_t *device);" .PP .I pappl_dmetrics_t * .br .BI papplDeviceGetMetrics "(pappl_device_t *device, pappl_dmetrics_t *metrics);" .PP .I pappl_preason_t .br .BI papplDeviceGetStatus "(pappl_device_t *device);" .PP .I bool .br .BI papplDeviceList "(pappl_dtype_t types, pappl_device_cb_t cb, void *data, pappl_deverror_cb_t err_cb, void *err_data);" .PP .I pappl_device_t * .br .BI papplDeviceOpen "(const char *device_uri, const char *name, pappl_deverror_cb_t err_cb, void *err_data);" .PP .I int .br .BI papplDeviceParse1284ID "(const char *device_id, cups_option_t **pairs);" .PP .I ssize_t .br .BI papplDevicePrintf "(pappl_device_t *device, const char *format, ...);" .PP .I ssize_t .br .BI papplDevicePuts "(pappl_device_t *device, const char *s);" .PP .I ssize_t .br .BI papplDeviceRead "(pappl_device_t *device, void *buffer, size_t bytes);" .PP .I void .br .BI papplDeviceSetData "(pappl_device_t *device, void *data);" .PP .I ssize_t .br .BI papplDeviceWrite "(pappl_device_t *device, const void *buffer, size_t bytes);" .SH DESCRIPTION The .B PAPPL device functions provide access to output device connections and to list available output devices. Output devices are accessed using Uniform Resource Identifier (URI) strings such as "file:///path/to/file-or-directory", "socket://11.22.33.44", and "usb://make/model?serial=number". .PP The .B papplDeviceList function lists available output devices, providing each available output device to the supplied callback function. The list only contains devices whose URI scheme supports discovery, at present USB printers and network printers that advertise themselves using DNS-SD/mDNS and/or SNMPv1. .PP The .B papplDeviceOpen function opens a connection to an output device using its URI. The .B papplDeviceClose function closes the connection. .PP The .B papplDevicePrintf, papplDevicePuts, and .B papplDeviceWrite functions send data to the device, while the .B papplDeviceRead function reads data from the device. .PP The .B papplDeviceGetMetrics function gets statistical information about all communications with the device while it has been open, while the .B papplDeviceGetStatus function gets the hardware status of a device and maps it to the .B pappl_preason_t bitfield. .SH ENUMERATIONS .SS pappl_devtype_e Device type bit values .TP 5 PAPPL_DEVTYPE_ALL .br All printers .TP 5 PAPPL_DEVTYPE_CUSTOM_LOCAL .br Local printer using a custom interface or protocol .TP 5 PAPPL_DEVTYPE_CUSTOM_NETWORK .br Network printer using a custom interface or protocol .TP 5 PAPPL_DEVTYPE_DNS_SD .br Network printers discovered via DNS-SD/mDNS .TP 5 PAPPL_DEVTYPE_FILE .br Local file/directory .TP 5 PAPPL_DEVTYPE_LOCAL .br All local printers .TP 5 PAPPL_DEVTYPE_NETWORK .br All network printers .TP 5 PAPPL_DEVTYPE_SNMP .br Network printers discovered via SNMP .TP 5 PAPPL_DEVTYPE_SOCKET .br Network printers using raw socket .TP 5 PAPPL_DEVTYPE_USB .br USB printers .SH FUNCTIONS .SS papplDeviceAddScheme Add a device URI scheme. .PP .nf void papplDeviceAddScheme ( const char *scheme, pappl_devtype_t dtype, pappl_devlist_cb_t list_cb, pappl_devopen_cb_t open_cb, pappl_devclose_cb_t close_cb, pappl_devread_cb_t read_cb, pappl_devwrite_cb_t write_cb, pappl_devstatus_cb_t status_cb, pappl_devid_cb_t id_cb ); .fi .PP This function registers a device URI scheme with PAPPL, so that devices using the named scheme can receive print data, report status information, and so forth. PAPPL includes support for the following URI schemes: .PP .IP \(bu 5 \fBdnssd\fR: Network printers discovered using DNS-SD. .IP \(bu 5 \fBfile\fR: Character device files, plain files, and directories. .IP \(bu 5 \fBsnmp\fR: Network printers discovered using SNMPv1. .IP \(bu 5 \fBsocket\fR: Network printers using a hostname or numeric IP address. .IP \(bu 5 \fBusb\fR: Class 1 (unidirectional) or 2 (bidirectional) USB printers. .PP The "scheme" parameter specifies the URI scheme and must consist of lowercase letters, digits, "-", "_", and/or ".", for example "x-foo" or "com.example.bar". .PP The "dtype" parameter specifies the device type and should be \fBPAPPL_DTYPE_CUSTOM_LOCAL\fR for locally connected printers and \fBPAPPL_DTYPE_CUSTOM_NETWORK\fR for network printers. .PP Each of the callbacks corresponds to one of the \fBpapplDevice\fR functions: .PP .IP \(bu 5 "list_cb": Implements discovery of devices (optional) .IP \(bu 5 "open_cb": Opens communication with a device and allocates any device- specific data as needed .IP \(bu 5 "close_cb": Closes communication with a device and frees any device- specific data as needed .IP \(bu 5 "read_cb": Reads data from a device .IP \(bu 5 "write_cb": Write data to a device .IP \(bu 5 "status_cb": Gets basic printer state information from a device (optional) .IP \(bu 5 "id_cb": Gets the current IEEE-1284 device ID from a device (optional) .PP The "open_cb" callback typically calls \fIpapplDeviceSetData\fR to store a pointer to contextual information for the connection while the "close_cb", "id_cb", "read_cb", "write_cb", and "status_cb" callbacks typically call \fIpapplDeviceGetData\fR to retrieve it. .SS papplDeviceClose Close a device connection. .PP .nf void papplDeviceClose ( pappl_device_t *device ); .fi .PP This function flushes any pending write data and closes the connection to a device. .SS papplDeviceError Report an error on a device. .PP .nf void papplDeviceError ( pappl_device_t *device, const char *message, ... ); .fi .PP This function reports an error on a device using the client-supplied callback function. It is normally called from any custom device URI scheme callbacks you implement. .SS papplDeviceFlush Flush any buffered data to the device. .PP .nf void papplDeviceFlush ( pappl_device_t *device ); .fi .PP This function flushes any pending write data sent using the \fIpapplDevicePrintf\fR, \fIpapplDevicePuts\fR, or \fIpapplDeviceWrite\fR functions to the device. .SS papplDeviceGetData Get device-specific data. .PP .nf void * papplDeviceGetData ( pappl_device_t *device ); .fi .PP This function returns any device-specific data that has been set by the device open callback. It is normally only called from any custom device URI scheme callbacks you implement. .SS papplDeviceGetID Get the IEEE-1284 device ID. .PP .nf char * papplDeviceGetID ( pappl_device_t *device, char *buffer, size_t bufsize ); .fi .PP This function queries the IEEE-1284 device ID from the device and copies it to the provided buffer. The buffer must be at least 64 bytes and should be at least 1024 bytes in length. .PP .IN 5 \fINote:\fR This function can block for up to several seconds depending on .IN 5 the type of connection. .SS papplDeviceGetMetrics Get the device metrics. .PP .nf pappl_devmetrics_t * papplDeviceGetMetrics ( pappl_device_t *device, pappl_devmetrics_t *metrics ); .fi .PP This function returns a copy of the device metrics data, which includes the number, length (in bytes), and duration (in milliseconds) of read, status, and write requests for the current session. This information is normally used for performance measurement and optimization during development of a printer application. It can also be useful diagnostic information. .SS papplDeviceGetStatus Get the printer status bits. .PP .nf pappl_preason_t papplDeviceGetStatus ( pappl_device_t *device ); .fi .PP This function returns the current printer status bits, as applicable to the current device. .PP The status bits for USB devices come from the original Centronics parallel printer "standard" which was later formally standardized in IEEE 1284-1984 and the USB Device Class Definition for Printing Devices. Some vendor extensions are also supported. .PP The status bits for network devices come from the hrPrinterDetectedErrorState property that is defined in the SNMP Printer MIB v2 (RFC 3805). .PP This function returns a \fIpappl_preason_t\fR bitfield which can be passed to the \fIpapplPrinterSetReasons\fR function. Use the \fIPAPPL_PREASON_DEVICE_STATUS\fR value as the value of the "remove" argument. .PP .IN 5 Note: This function can block for several seconds while getting the status .IN 5 information. .SS papplDeviceIsSupported Determine whether a given URI is supported. .PP .nf bool papplDeviceIsSupported ( const char *uri ); .fi .PP This function determines whether a given URI or URI scheme is supported as a device. .SS papplDeviceList List available devices. .PP .nf bool papplDeviceList ( pappl_devtype_t types, pappl_device_cb_t cb, void *data, pappl_deverror_cb_t err_cb, void *err_data ); .fi .PP This function lists the available devices, calling the "cb" function once per device that is discovered/listed. The callback function receives the device URI, IEEE-1284 device ID (if any), and "data" pointer, and returns \fBtrue\fR to stop listing devices and \fBfalse\fR to continue. .PP The "types" argument determines which devices are listed, for example \fBPAPPL_DEVTYPE_ALL\fR will list all types of devices while \fBPAPPL_DEVTYPE_USB\fR only lists USB printers. .PP Any errors are reported using the supplied "err_cb" function. If you specify \fBNULL\fR for this argument, errors are sent to \fBstderr\fR. .PP .IN 5 Note: This function will block (not return) until each of the device URI .IN 5 schemes has reported all of the devices \fIor\fR the supplied callback function .IN 5 returns \fBtrue\fR. .SS papplDeviceOpen Open a connection to a device. .PP .nf pappl_device_t * papplDeviceOpen ( const char *device_uri, const char *name, pappl_deverror_cb_t err_cb, void *err_data ); .fi .PP This function opens a connection to the specified device URI. The "name" argument provides textual context for the connection and is usually the name (title) of the print job. .PP Any errors are reported using the supplied "err_cb" function. If you specify \fBNULL\fR for this argument, errors are sent to \fBstderr\fR. .SS papplDeviceParseID Parse an IEEE-1284 device ID string. .PP .nf int papplDeviceParseID ( const char *device_id, cups_option_t **pairs ); .fi .PP This function parses an IEEE-1284 device ID string and returns an array of key/value pairs as a \fBcups_option_t\fR array. The returned array must be freed using the \fBcupsFreeOptions\fR function. .SS papplDevicePrintf Write a formatted string. .PP .nf ssize_t papplDevicePrintf ( pappl_device_t *device, const char *format, ... ); .fi .PP This function buffers a formatted string that will be sent to the device. The "format" argument accepts all \fBprintf\fR format specifiers and behaves identically to that function. .PP Call the \fIpapplDeviceFlush\fR function to ensure that the formatted string is immediately sent to the device. .SS papplDevicePuts Write a literal string. .PP .nf ssize_t papplDevicePuts ( pappl_device_t *device, const char *s ); .fi .PP This function buffers a literal string that will be sent to the device. Call the \fIpapplDeviceFlush\fR function to ensure that the literal string is immediately sent to the device. .SS papplDeviceRead Read from a device. .PP .nf ssize_t papplDeviceRead ( pappl_device_t *device, void *buffer, size_t bytes ); .fi .PP This function reads data from the device. Depending on the device, this function may block indefinitely. .SS papplDeviceSetData Set device-specific data. .PP .nf void papplDeviceSetData ( pappl_device_t *device, void *data ); .fi .PP This function sets any device-specific data needed to communicate with the device. It is normally only called from the open callback that was registered for the device URI scheme. .SS papplDeviceWrite Write to a device. .PP .nf ssize_t papplDeviceWrite ( pappl_device_t *device, const void *buffer, size_t bytes ); .fi .PP This function buffers data that will be sent to the device. Call the \fIpapplDeviceFlush\fR function to ensure that the data is immediately sent to the device. .SH STRUCTURES .SS pappl_devmetrics_s Device metrics .PP .nf struct pappl_devmetrics_s { size_t read_bytes; size_t read_msecs; size_t read_requests; size_t status_msecs; size_t status_requests; size_t write_bytes; size_t write_msecs; size_t write_requests; }; .fi .SH TYPES .SS pappl_devclose_cb_t Device close callback .PP .nf typedef void (*pappl_devclose_cb_t)(pappl_device_t *device); .fi .SS pappl_deverror_cb_t Device error callback .PP .nf typedef void (*pappl_deverror_cb_t)(const char *message, void *err_data); .fi .SS pappl_device_cb_t Device callback - return \fBtrue\fR to stop, \fBfalse\fR to continue .PP .nf typedef bool (*pappl_device_cb_t)(const char *device_info, const char *device_uri, const char *device_id, void *data); .fi .SS pappl_devid_cb_t Device ID callback .PP .nf typedef char * (*pappl_devid_cb_t)(pappl_device_t *device, char *buffer, size_t bufsize); .fi .SS pappl_devlist_cb_t Device list callback .PP .nf typedef bool (*pappl_devlist_cb_t)(pappl_device_cb_t cb, void *data, pappl_deverror_cb_t err_cb, void *err_data); .fi .SS pappl_devmetrics_t Device metrics .PP .nf typedef struct pappl_devmetrics_s pappl_devmetrics_t; .fi .SS pappl_devopen_cb_t Device open callback .PP .nf typedef bool (*pappl_devopen_cb_t)(pappl_device_t *device, const char *device_uri, const char *name); .fi .SS pappl_devread_cb_t Device read callback .PP .nf typedef ssize_t (*pappl_devread_cb_t)(pappl_device_t *device, void *buffer, size_t bytes); .fi .SS pappl_devstatus_cb_t Device status callback .PP .nf typedef pappl_preason_t (*pappl_devstatus_cb_t)(pappl_device_t *device); .fi .SS pappl_devtype_t Device type bitfield .PP .nf typedef unsigned pappl_devtype_t; .fi .SS pappl_devwrite_cb_t Device write callback .PP .nf typedef ssize_t (*pappl_devwrite_cb_t)(pappl_device_t *device, const void *buffer, size_t bytes); .fi .SH SEE ALSO .BR pappl (1), .BR pappl-client (3), .BR pappl-device (3), .BR pappl-job (3), .BR pappl-log (3), .BR pappl-mainline (3), .BR pappl-makeresheader (1), .BR pappl-printer (3), .BR pappl-resource (3), .BR pappl-system (3), https://www.msweet.org/pappl .SH COPYRIGHT Copyright \[co] 2019-2020 by Michael R Sweet. .PP .B PAPPL is licensed under the Apache License Version 2.0 with an (optional) exception to allow linking against GPL2/LGPL2 software (like older versions of CUPS), so it can be used .I freely in any project you'd like. See the files "LICENSE" and "NOTICE" in the source distribution for more information.