3 Copyright (C) Dean Camera, 2017.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaims all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
32 * \brief Device mode driver for the library USB Printer Class driver.
34 * Device mode driver for the library USB Printer Class driver.
36 * \note This file should not be included directly. It is automatically included as needed by the USB module driver
37 * dispatch header located in LUFA/Drivers/USB.h.
40 /** \ingroup Group_USBClassPrinter
41 * \defgroup Group_USBClassPrinterDevice Printer Class Device Mode Driver
43 * \section Sec_USBClassPrinterDevice_Dependencies Module Source Dependencies
44 * The following files must be built with any user project that uses this module:
45 * - LUFA/Drivers/USB/Class/Device/PrinterClassDevice.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
47 * \section Sec_USBClassPrinterDevice_ModDescription Module Description
48 * Device Mode USB Class driver framework interface, for the Printer USB Class driver.
53 #ifndef _PRINTER_CLASS_DEVICE_H_
54 #define _PRINTER_CLASS_DEVICE_H_
57 #include "../../USB.h"
58 #include "../Common/PrinterClassCommon.h"
62 /* Enable C linkage for C++ Compilers: */
63 #if defined(__cplusplus)
67 /* Preprocessor Checks: */
68 #if !defined(__INCLUDE_FROM_PRINTER_DRIVER)
69 #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
72 /* Public Interface - May be used in end-application: */
74 /** \brief Printer Class Device Mode Configuration and State Structure.
76 * Class state structure. An instance of this structure should be made for each Printer interface
77 * within the user application, and passed to each of the Printer class driver functions as the
78 * PRNTInterfaceInfo parameter. This stores each Printer interface's configuration and state information.
84 uint8_t InterfaceNumber; /**< Interface number of the Printer interface within the device. */
86 USB_Endpoint_Table_t DataINEndpoint; /**< Data IN endpoint configuration table. */
87 USB_Endpoint_Table_t DataOUTEndpoint; /**< Data OUT endpoint configuration table. */
89 char* IEEE1284String; /**< IEEE 1284 identification string, sent to the host during enumeration
90 * to identify the printer model, manufacturer and other characteristics.
92 } Config; /**< Config data for the USB class interface within the device. All elements in this section
93 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
97 uint8_t PortStatus; /**< Current status of the Printer virtual port, a collection of \c PRNT_PORTSTATUS_*
101 volatile bool IsPrinterReset; /**< Flag indicating that the host has requested that the Printer interface be reset
102 * and that all current Mass Storage operations should immediately abort.
104 } State; /**< State data for the USB class interface within the device. All elements in this section
105 * are reset to their defaults when the interface is enumerated.
107 } USB_ClassInfo_PRNT_Device_t;
109 /* Function Prototypes: */
110 /** Configures the endpoints of a given Printer interface, ready for use. This should be linked to the library
111 * \ref EVENT_USB_Device_ConfigurationChanged() event so that the endpoints are configured when the configuration containing
112 * the given Printer interface is selected.
114 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
116 * \return Boolean \c true if the endpoints were successfully configured, \c false otherwise.
118 bool PRNT_Device_ConfigureEndpoints(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
120 /** Processes incoming control requests from the host, that are directed to the given Printer class interface. This should be
121 * linked to the library \ref EVENT_USB_Device_ControlRequest() event.
123 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
125 void PRNT_Device_ProcessControlRequest(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
127 /** General management task for a given Printer class interface, required for the correct operation of the interface. This should
128 * be called frequently in the main program loop, before the master USB management task \ref USB_USBTask().
130 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
132 void PRNT_Device_USBTask(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
134 /** Printer class driver event for a soft reset request on a Printer interface. This event fires each time the host
135 * requests a reset of the printer interface's internal state, and may be hooked in the user program by declaring a
136 * handler function with the same name and parameters listed here.
138 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
140 void EVENT_PRNT_Device_SoftReset(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
142 /** Sends a given data buffer to the attached USB host, if connected. If a host is not connected when the function is
143 * called, the string is discarded. Bytes will be queued for transmission to the host until either the endpoint bank
144 * becomes full, or the \ref PRNT_Device_Flush() function is called to flush the pending data to the host. This allows
145 * for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
147 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
148 * the call will fail.
150 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
151 * \param[in] Buffer Pointer to a buffer containing the data to send to the device.
152 * \param[in] Length Length of the data to send to the host.
154 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
156 uint8_t PRNT_Device_SendData(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
157 const void* const Buffer,
158 const uint16_t Length) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
160 /** Sends a given null terminated string to the attached USB host, if connected. If a host is not connected when
161 * the function is called, the string is discarded. Bytes will be queued for transmission to the host until either
162 * the endpoint bank becomes full, or the \ref PRNT_Device_Flush() function is called to flush the pending data to
163 * the host. This allows for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
165 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
166 * the call will fail.
168 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
169 * \param[in] String Pointer to the null terminated string to send to the host.
171 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
173 uint8_t PRNT_Device_SendString(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
174 const char* const String) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
176 /** Sends a given byte to the attached USB host, if connected. If a host is not connected when the function is called, the
177 * byte is discarded. Bytes will be queued for transmission to the host until either the endpoint bank becomes full, or the
178 * \ref PRNT_Device_Flush() function is called to flush the pending data to the host. This allows for multiple bytes to be
179 * packed into a single endpoint packet, increasing data throughput.
181 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
182 * the call will fail.
184 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
185 * \param[in] Data Byte of data to send to the host.
187 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
189 uint8_t PRNT_Device_SendByte(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
190 const uint8_t Data) ATTR_NON_NULL_PTR_ARG(1);
192 /** Determines the number of bytes received by the Printer interface from the host, waiting to be read. This indicates the number
193 * of bytes in the OUT endpoint bank only, and thus the number of calls to \ref PRNT_Device_ReceiveByte() which are guaranteed to
194 * succeed immediately. If multiple bytes are to be received, they should be buffered by the user application, as the endpoint
195 * bank will not be released back to the USB controller until all bytes are read.
197 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
198 * the call will fail.
200 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
202 * \return Total number of buffered bytes received from the host.
204 uint16_t PRNT_Device_BytesReceived(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
206 /** Reads a byte of data from the host. If no data is waiting to be read of if a USB host is not connected, the function
207 * returns a negative value. The \ref PRNT_Device_BytesReceived() function may be queried in advance to determine how many
208 * bytes are currently buffered in the Printer interface's data receive endpoint bank, and thus how many repeated calls to this
209 * function which are guaranteed to succeed.
211 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
212 * the call will fail.
214 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
216 * \return Next received byte from the host, or a negative value if no data received.
218 int16_t PRNT_Device_ReceiveByte(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
220 /** Flushes any data waiting to be sent, ensuring that the send buffer is cleared.
222 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
223 * the call will fail.
225 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
227 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
229 uint8_t PRNT_Device_Flush(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
231 #if defined(FDEV_SETUP_STREAM) || defined(__DOXYGEN__)
232 /** Creates a standard character stream for the given Printer Device instance so that it can be used with all the regular
233 * functions in the standard <stdio.h> library that accept a \c FILE stream as a destination (e.g. \c fprintf()). The created
234 * stream is bidirectional and can be used for both input and output functions.
236 * Reading data from this stream is non-blocking, i.e. in most instances, complete strings cannot be read in by a single
237 * fetch, as the endpoint will not be ready at some point in the transmission, aborting the transfer. However, this may
238 * be used when the read data is processed byte-per-bye (via \c getc()) or when the user application will implement its own
241 * \note The created stream can be given as \c stdout if desired to direct the standard output from all \c <stdio.h> functions
242 * to the given Printer interface.
245 * \note This function is not available on all microcontroller architectures.
247 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
248 * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed.
250 void PRNT_Device_CreateStream(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
251 FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
253 /** Identical to \ref PRNT_Device_CreateStream(), except that reads are blocking until the calling stream function terminates
254 * the transfer. While blocking, the USB and Printer service tasks are called repeatedly to maintain USB communications.
256 * \note This function is not available on all microcontroller architectures.
258 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
259 * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed.
261 void PRNT_Device_CreateBlockingStream(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
262 FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
265 /* Private Interface - For use in library only: */
266 #if !defined(__DOXYGEN__)
267 /* Function Prototypes: */
268 #if defined(__INCLUDE_FROM_PRINTER_DEVICE_C)
269 #if defined(FDEV_SETUP_STREAM)
270 static int PRNT_Device_putchar(char c,
271 FILE* Stream) ATTR_NON_NULL_PTR_ARG(2);
272 static int PRNT_Device_getchar(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
273 static int PRNT_Device_getchar_Blocking(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
276 void PRNT_Device_Event_Stub(void) ATTR_CONST;
278 void EVENT_PRNT_Device_SoftReset(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo)
279 ATTR_WEAK ATTR_NON_NULL_PTR_ARG(1) ATTR_ALIAS(PRNT_Device_Event_Stub);
285 /* Disable C linkage for C++ Compilers: */
286 #if defined(__cplusplus)