PrinterClassHost.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2017.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. * \brief Host mode driver for the library USB Printer Class driver.
  28. *
  29. * Host mode driver for the library USB Printer Class driver.
  30. *
  31. * \note This file should not be included directly. It is automatically included as needed by the USB module driver
  32. * dispatch header located in LUFA/Drivers/USB.h.
  33. */
  34. /** \ingroup Group_USBClassPrinter
  35. * \defgroup Group_USBClassPrinterHost Printer Class Host Mode Driver
  36. *
  37. * \section Sec_USBClassPrinterHost_Dependencies Module Source Dependencies
  38. * The following files must be built with any user project that uses this module:
  39. * - LUFA/Drivers/USB/Class/Host/PrinterClassHost.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
  40. *
  41. * \section Sec_USBClassPrinterHost_ModDescription Module Description
  42. * Host Mode USB Class driver framework interface, for the Printer USB Class driver.
  43. *
  44. * @{
  45. */
  46. #ifndef __PRINTER_CLASS_HOST_H__
  47. #define __PRINTER_CLASS_HOST_H__
  48. /* Includes: */
  49. #include "../../USB.h"
  50. #include "../Common/PrinterClassCommon.h"
  51. /* Enable C linkage for C++ Compilers: */
  52. #if defined(__cplusplus)
  53. extern "C" {
  54. #endif
  55. /* Preprocessor Checks: */
  56. #if !defined(__INCLUDE_FROM_PRINTER_DRIVER)
  57. #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
  58. #endif
  59. /* Public Interface - May be used in end-application: */
  60. /* Type Defines: */
  61. /** \brief Printer Class Host Mode Configuration and State Structure.
  62. *
  63. * Class state structure. An instance of this structure should be made within the user application,
  64. * and passed to each of the Printer class driver functions as the \c PRNTInterfaceInfo parameter. This
  65. * stores each Printer interface's configuration and state information.
  66. */
  67. typedef struct
  68. {
  69. struct
  70. {
  71. USB_Pipe_Table_t DataINPipe; /**< Data IN Pipe configuration table. */
  72. USB_Pipe_Table_t DataOUTPipe; /**< Data OUT Pipe configuration table. */
  73. } Config; /**< Config data for the USB class interface within the device. All elements in this section
  74. * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
  75. */
  76. struct
  77. {
  78. bool IsActive; /**< Indicates if the current interface instance is connected to an attached device, valid
  79. * after \ref PRNT_Host_ConfigurePipes() is called and the Host state machine is in the
  80. * Configured state.
  81. */
  82. uint8_t InterfaceNumber; /**< Interface index of the Printer interface within the attached device. */
  83. uint8_t AlternateSetting; /**< Alternate setting within the Printer Interface in the attached device. */
  84. } State; /**< State data for the USB class interface within the device. All elements in this section
  85. * <b>may</b> be set to initial values, but may also be ignored to default to sane values when
  86. * the interface is enumerated.
  87. */
  88. } USB_ClassInfo_PRNT_Host_t;
  89. /* Enums: */
  90. /** Enum for the possible error codes returned by the \ref PRNT_Host_ConfigurePipes() function. */
  91. enum PRNT_Host_EnumerationFailure_ErrorCodes_t
  92. {
  93. PRNT_ENUMERROR_NoError = 0, /**< Configuration Descriptor was processed successfully. */
  94. PRNT_ENUMERROR_InvalidConfigDescriptor = 1, /**< The device returned an invalid Configuration Descriptor. */
  95. PRNT_ENUMERROR_NoCompatibleInterfaceFound = 2, /**< A compatible Printer interface was not found in the device's Configuration Descriptor. */
  96. PRNT_ENUMERROR_PipeConfigurationFailed = 3, /**< One or more pipes for the specified interface could not be configured correctly. */
  97. };
  98. /* Function Prototypes: */
  99. /** Host interface configuration routine, to configure a given Printer host interface instance using the
  100. * Configuration Descriptor read from an attached USB device. This function automatically updates the given Printer
  101. * instance's state values and configures the pipes required to communicate with the interface if it is found within
  102. * the device. This should be called once after the stack has enumerated the attached device, while the host state
  103. * machine is in the Addressed state.
  104. *
  105. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  106. * \param[in] ConfigDescriptorSize Length of the attached device's Configuration Descriptor.
  107. * \param[in] ConfigDescriptorData Pointer to a buffer containing the attached device's Configuration Descriptor.
  108. *
  109. * \return A value from the \ref PRNT_Host_EnumerationFailure_ErrorCodes_t enum.
  110. */
  111. uint8_t PRNT_Host_ConfigurePipes(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
  112. uint16_t ConfigDescriptorSize,
  113. void* ConfigDescriptorData) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);
  114. /** General management task for a given Printer host class interface, required for the correct operation of
  115. * the interface. This should be called frequently in the main program loop, before the master USB management task
  116. * \ref USB_USBTask().
  117. *
  118. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  119. */
  120. void PRNT_Host_USBTask(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  121. /** Configures the printer to enable Bidirectional mode, if it is not already in this mode. This should be called
  122. * once the connected device's configuration has been set, to ensure the printer is ready to accept commands.
  123. *
  124. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  125. *
  126. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
  127. */
  128. uint8_t PRNT_Host_SetBidirectionalMode(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  129. /** Retrieves the status of the virtual Printer port's inbound status lines. The result can then be masked against the
  130. * \c PRNT_PORTSTATUS_* macros to determine the printer port's status.
  131. *
  132. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  133. * \param[out] PortStatus Location where the retrieved port status should be stored.
  134. *
  135. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
  136. */
  137. uint8_t PRNT_Host_GetPortStatus(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
  138. uint8_t* const PortStatus)
  139. ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  140. /** Soft-resets the attached printer, readying it for new commands.
  141. *
  142. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  143. *
  144. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
  145. */
  146. uint8_t PRNT_Host_SoftReset(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  147. /** Flushes any data waiting to be sent, ensuring that the send buffer is cleared.
  148. *
  149. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  150. * call will fail.
  151. *
  152. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  153. *
  154. * \return A value from the \ref Pipe_WaitUntilReady_ErrorCodes_t enum.
  155. */
  156. uint8_t PRNT_Host_Flush(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  157. /** Sends the given null terminated string to the attached printer's input endpoint.
  158. *
  159. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  160. * call will fail.
  161. *
  162. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  163. * \param[in] String Pointer to a null terminated string to send.
  164. *
  165. * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
  166. */
  167. uint8_t PRNT_Host_SendString(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
  168. const char* const String) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  169. /** Sends the given raw data stream to the attached printer's input endpoint. This should contain commands that the
  170. * printer is able to understand - for example, PCL data. Not all printers accept all printer languages; see
  171. * \ref PRNT_Host_GetDeviceID() for details on determining acceptable languages for an attached printer.
  172. *
  173. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  174. * call will fail.
  175. *
  176. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  177. * \param[in] Buffer Pointer to a buffer containing the raw command stream to send to the printer.
  178. * \param[in] Length Size in bytes of the command stream to be sent.
  179. *
  180. * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
  181. */
  182. uint8_t PRNT_Host_SendData(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
  183. const void* Buffer,
  184. const uint16_t Length) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  185. /** Sends a given byte to the attached USB device, if connected. If a device is not connected when the function is called, the
  186. * byte is discarded. Bytes will be queued for transmission to the device until either the pipe bank becomes full, or the
  187. * \ref PRNT_Host_Flush() function is called to flush the pending data to the host. This allows for multiple bytes to be
  188. * packed into a single pipe packet, increasing data throughput.
  189. *
  190. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  191. * call will fail.
  192. *
  193. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  194. * \param[in] Data Byte of data to send to the device.
  195. *
  196. * \return A value from the \ref Pipe_WaitUntilReady_ErrorCodes_t enum.
  197. */
  198. uint8_t PRNT_Host_SendByte(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
  199. const uint8_t Data) ATTR_NON_NULL_PTR_ARG(1);
  200. /** Determines the number of bytes received by the printer interface from the device, waiting to be read. This indicates the number
  201. * of bytes in the IN pipe bank only, and thus the number of calls to \ref PRNT_Host_ReceiveByte() which are guaranteed to succeed
  202. * immediately. If multiple bytes are to be received, they should be buffered by the user application, as the pipe bank will not be
  203. * released back to the USB controller until all bytes are read.
  204. *
  205. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  206. * call will fail.
  207. *
  208. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  209. *
  210. * \return Total number of buffered bytes received from the device.
  211. */
  212. uint16_t PRNT_Host_BytesReceived(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  213. /** Reads a byte of data from the device. If no data is waiting to be read of if a USB device is not connected, the function
  214. * returns a negative value. The \ref PRNT_Host_BytesReceived() function may be queried in advance to determine how many bytes
  215. * are currently buffered in the Printer interface's data receive pipe.
  216. *
  217. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  218. * call will fail.
  219. *
  220. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  221. *
  222. * \return Next received byte from the device, or a negative value if no data received.
  223. */
  224. int16_t PRNT_Host_ReceiveByte(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  225. /** Retrieves the attached printer device's ID string, formatted according to IEEE 1284. This string is sent as a
  226. * Unicode string from the device and is automatically converted to an ASCII encoded C string by this function, thus
  227. * the maximum reportable string length is two less than the size given (to accommodate the Unicode string length
  228. * bytes which are removed).
  229. *
  230. * This string, when supported, contains the model, manufacturer and acceptable printer languages for the attached device.
  231. *
  232. * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
  233. * \param[out] DeviceIDString Pointer to a buffer where the Device ID string should be stored, in ASCII format.
  234. * \param[in] BufferSize Size in bytes of the buffer allocated for the Device ID string.
  235. *
  236. * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
  237. */
  238. uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
  239. char* const DeviceIDString,
  240. const uint16_t BufferSize) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  241. /* Private Interface - For use in library only: */
  242. #if !defined(__DOXYGEN__)
  243. /* Function Prototypes: */
  244. #if defined(__INCLUDE_FROM_PRINTER_HOST_C)
  245. static uint8_t DCOMP_PRNT_Host_NextPRNTInterface(void* const CurrentDescriptor)
  246. ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
  247. static uint8_t DCOMP_PRNT_Host_NextPRNTInterfaceEndpoint(void* const CurrentDescriptor)
  248. ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
  249. #endif
  250. #endif
  251. /* Disable C linkage for C++ Compilers: */
  252. #if defined(__cplusplus)
  253. }
  254. #endif
  255. #endif
  256. /** @} */