45f49d4e3482dbda916bada8983fe4253d7967b2
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / PrinterClassDevice.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2013.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 * \brief Device mode driver for the library USB Printer Class driver.
33 *
34 * Device mode driver for the library USB Printer Class driver.
35 *
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.
38 */
39
40 /** \ingroup Group_USBClassPrinter
41 * \defgroup Group_USBClassPrinterDevice Printer Class Device Mode Driver
42 *
43 * \section Sec_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>
46 *
47 * \section Sec_ModDescription Module Description
48 * Device Mode USB Class driver framework interface, for the Printer USB Class driver.
49 *
50 * @{
51 */
52
53 #ifndef _PRINTER_CLASS_DEVICE_H_
54 #define _PRINTER_CLASS_DEVICE_H_
55
56 /* Includes: */
57 #include "../../USB.h"
58 #include "../Common/PrinterClassCommon.h"
59
60 #include <stdio.h>
61
62 /* Enable C linkage for C++ Compilers: */
63 #if defined(__cplusplus)
64 extern "C" {
65 #endif
66
67 /* Preprocessor Checks: */
68 #if !defined(__INCLUDE_FROM_PRINTER_DRIVER)
69 #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
70 #endif
71
72 /* Public Interface - May be used in end-application: */
73 /* Type Defines: */
74 /** \brief Printer Class Device Mode Configuration and State Structure.
75 *
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.
79 */
80 typedef struct
81 {
82 struct
83 {
84 uint8_t InterfaceNumber; /**< Interface number of the Printer interface within the device. */
85
86 USB_Endpoint_Table_t DataINEndpoint; /**< Data IN endpoint configuration table. */
87 USB_Endpoint_Table_t DataOUTEndpoint; /**< Data OUT endpoint configuration table. */
88
89 char* IEEE1284String; /**< IEEE 1284 identification string, sent to the host during enumeration
90 * to identify the printer model, manufacturer and other characteristics. */
91 } Config; /**< Config data for the USB class interface within the device. All elements in this section
92 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
93 */
94 struct
95 {
96 uint8_t PortStatus; /**< Current status of the Printer virtual port, a collection of \c PRNT_PORTSTATUS_*
97 * bitmask values. */
98 } State; /**< State data for the USB class interface within the device. All elements in this section
99 * are reset to their defaults when the interface is enumerated.
100 */
101 } USB_ClassInfo_PRNT_Device_t;
102
103 /* Function Prototypes: */
104 /** Configures the endpoints of a given Printer interface, ready for use. This should be linked to the library
105 * \ref EVENT_USB_Device_ConfigurationChanged() event so that the endpoints are configured when the configuration containing
106 * the given Printer interface is selected.
107 *
108 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
109 *
110 * \return Boolean \c true if the endpoints were successfully configured, \c false otherwise.
111 */
112 bool PRNT_Device_ConfigureEndpoints(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
113
114 /** Processes incoming control requests from the host, that are directed to the given Printer class interface. This should be
115 * linked to the library \ref EVENT_USB_Device_ControlRequest() event.
116 *
117 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
118 */
119 void PRNT_Device_ProcessControlRequest(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
120
121 /** General management task for a given Printer class interface, required for the correct operation of the interface. This should
122 * be called frequently in the main program loop, before the master USB management task \ref USB_USBTask().
123 *
124 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
125 */
126 void PRNT_Device_USBTask(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
127
128 /** Sends a given data buffer to the attached USB host, if connected. If a host is not connected when the function is
129 * called, the string is discarded. Bytes will be queued for transmission to the host until either the endpoint bank
130 * becomes full, or the \ref PRNT_Device_Flush() function is called to flush the pending data to the host. This allows
131 * for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
132 *
133 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
134 * the call will fail.
135 *
136 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
137 * \param[in] Buffer Pointer to a buffer containing the data to send to the device.
138 * \param[in] Length Length of the data to send to the host.
139 *
140 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
141 */
142 uint8_t PRNT_Device_SendData(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
143 const void* const Buffer,
144 const uint16_t Length) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
145
146 /** Sends a given null terminated string to the attached USB host, if connected. If a host is not connected when
147 * the function is called, the string is discarded. Bytes will be queued for transmission to the host until either
148 * the endpoint bank becomes full, or the \ref PRNT_Device_Flush() function is called to flush the pending data to
149 * the host. This allows for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
150 *
151 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
152 * the call will fail.
153 *
154 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
155 * \param[in] String Pointer to the null terminated string to send to the host.
156 *
157 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
158 */
159 uint8_t PRNT_Device_SendString(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
160 const char* const String) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
161
162 /** Sends a given byte to the attached USB host, if connected. If a host is not connected when the function is called, the
163 * byte is discarded. Bytes will be queued for transmission to the host until either the endpoint bank becomes full, or the
164 * \ref PRNT_Device_Flush() function is called to flush the pending data to the host. This allows for multiple bytes to be
165 * packed into a single endpoint packet, increasing data throughput.
166 *
167 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
168 * the call will fail.
169 *
170 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
171 * \param[in] Data Byte of data to send to the host.
172 *
173 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
174 */
175 uint8_t PRNT_Device_SendByte(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
176 const uint8_t Data) ATTR_NON_NULL_PTR_ARG(1);
177
178 /** Determines the number of bytes received by the Printer interface from the host, waiting to be read. This indicates the number
179 * of bytes in the OUT endpoint bank only, and thus the number of calls to \ref PRNT_Device_ReceiveByte() which are guaranteed to
180 * succeed immediately. If multiple bytes are to be received, they should be buffered by the user application, as the endpoint
181 * bank will not be released back to the USB controller until all bytes are read.
182 *
183 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
184 * the call will fail.
185 *
186 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
187 *
188 * \return Total number of buffered bytes received from the host.
189 */
190 uint16_t PRNT_Device_BytesReceived(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
191
192 /** 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
193 * returns a negative value. The \ref PRNT_Device_BytesReceived() function may be queried in advance to determine how many
194 * bytes are currently buffered in the Printer interface's data receive endpoint bank, and thus how many repeated calls to this
195 * function which are guaranteed to succeed.
196 *
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.
199 *
200 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
201 *
202 * \return Next received byte from the host, or a negative value if no data received.
203 */
204 int16_t PRNT_Device_ReceiveByte(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
205
206 /** Flushes any data waiting to be sent, ensuring that the send buffer is cleared.
207 *
208 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
209 * the call will fail.
210 *
211 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
212 *
213 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
214 */
215 uint8_t PRNT_Device_Flush(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
216
217 #if defined(FDEV_SETUP_STREAM) || defined(__DOXYGEN__)
218 /** Creates a standard character stream for the given Printer Device instance so that it can be used with all the regular
219 * functions in the standard <stdio.h> library that accept a \c FILE stream as a destination (e.g. \c fprintf()). The created
220 * stream is bidirectional and can be used for both input and output functions.
221 *
222 * Reading data from this stream is non-blocking, i.e. in most instances, complete strings cannot be read in by a single
223 * fetch, as the endpoint will not be ready at some point in the transmission, aborting the transfer. However, this may
224 * be used when the read data is processed byte-per-bye (via \c getc()) or when the user application will implement its own
225 * line buffering.
226 *
227 * \note The created stream can be given as \c stdout if desired to direct the standard output from all \c <stdio.h> functions
228 * to the given Printer interface.
229 * \n\n
230 *
231 * \note This function is not available on all microcontroller architectures.
232 *
233 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
234 * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed.
235 */
236 void PRNT_Device_CreateStream(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
237 FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
238
239 /** Identical to \ref PRNT_Device_CreateStream(), except that reads are blocking until the calling stream function terminates
240 * the transfer. While blocking, the USB and Printer service tasks are called repeatedly to maintain USB communications.
241 *
242 * \note This function is not available on all microcontroller architectures.
243 *
244 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
245 * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed.
246 */
247 void PRNT_Device_CreateBlockingStream(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
248 FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
249 #endif
250
251 /* Private Interface - For use in library only: */
252 #if !defined(__DOXYGEN__)
253 /* Function Prototypes: */
254 #if defined(__INCLUDE_FROM_PRINTER_DEVICE_C)
255 #if defined(FDEV_SETUP_STREAM)
256 static int PRNT_Device_putchar(char c,
257 FILE* Stream) ATTR_NON_NULL_PTR_ARG(2);
258 static int PRNT_Device_getchar(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
259 static int PRNT_Device_getchar_Blocking(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
260 #endif
261
262 void PRNT_Device_Event_Stub(void) ATTR_CONST;
263
264 void EVENT_PRNT_Device_SoftReset(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo)
265 ATTR_WEAK ATTR_NON_NULL_PTR_ARG(1) ATTR_ALIAS(PRNT_Device_Event_Stub);
266
267 #endif
268
269 #endif
270
271 /* Disable C linkage for C++ Compilers: */
272 #if defined(__cplusplus)
273 }
274 #endif
275
276 #endif
277
278 /** @} */
279