1 /* Copyright (c) 2010-2011 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "USBDevice_Types.h"
26 class USBDevice: public USBHAL
29 USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
32 * Check if the device is configured
34 * @returns true if configured, false otherwise
36 bool configured(void);
41 * @param blocking: block if not configured
43 void connect(bool blocking = true);
48 void disconnect(void);
53 * @param endpoint endpoint which will be added
54 * @param maxPacket Maximum size of a packet which can be sent for this endpoint
55 * @returns true if successful, false otherwise
57 bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
60 * Start a reading on a certain endpoint.
61 * You can access the result of the reading by USBDevice_read
63 * @param endpoint endpoint which will be read
64 * @param maxSize the maximum length that can be read
65 * @return true if successful
67 bool readStart(uint8_t endpoint, uint32_t maxSize);
70 * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
75 * @param endpoint endpoint which will be read
76 * @param buffer buffer will be filled with the data received
77 * @param size the number of bytes read will be stored in *size
78 * @param maxSize the maximum length that can be read
79 * @returns true if successful
81 bool readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
84 * Read a certain endpoint.
86 * Warning: non blocking
88 * @param endpoint endpoint which will be read
89 * @param buffer buffer will be filled with the data received (if data are available)
90 * @param size the number of bytes read will be stored in *size
91 * @param maxSize the maximum length that can be read
92 * @returns true if successful
94 bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
97 * Write a certain endpoint.
101 * @param endpoint endpoint to write
102 * @param buffer data contained in buffer will be write
103 * @param size the number of bytes to write
104 * @param maxSize the maximum length that can be written on this endpoint
106 bool write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
110 * Write a certain endpoint.
112 * Warning: non blocking
114 * @param endpoint endpoint to write
115 * @param buffer data contained in buffer will be write
116 * @param size the number of bytes to write
117 * @param maxSize the maximum length that can be written on this endpoint
119 bool writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
123 * Called by USBDevice layer on bus reset. Warning: Called in ISR context
125 * May be used to reset state
127 virtual void USBCallback_busReset(void) {};
130 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
131 * This is used to handle extensions to standard requests
132 * and class specific requests
134 * @returns true if class handles this request
136 virtual bool USBCallback_request() { return false; };
139 * Called by USBDevice on Endpoint0 request completion
140 * if the 'notify' flag has been set to true. Warning: Called in ISR context
142 * In this case it is used to indicate that a HID report has
143 * been received from the host on endpoint 0
145 * @param buf buffer received on endpoint 0
146 * @param length length of this buffer
148 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {};
151 * Called by USBDevice layer. Set configuration of the device.
152 * For instance, you can add all endpoints that you need on this function.
154 * @param configuration Number of the configuration
156 virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; };
159 * Called by USBDevice layer. Set interface/alternate of the device.
161 * @param interface Number of the interface to be configured
162 * @param alternate Number of the alternate to be configured
163 * @returns true if class handles this request
165 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
168 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
170 * @returns pointer to the device descriptor
172 virtual uint8_t * deviceDesc();
175 * Get configuration descriptor
177 * @returns pointer to the configuration descriptor
179 virtual uint8_t * configurationDesc(){return NULL;};
182 * Get string lang id descriptor
184 * @return pointer to the string lang id descriptor
186 virtual uint8_t * stringLangidDesc();
189 * Get string manufacturer descriptor
191 * @returns pointer to the string manufacturer descriptor
193 virtual uint8_t * stringImanufacturerDesc();
196 * Get string product descriptor
198 * @returns pointer to the string product descriptor
200 virtual uint8_t * stringIproductDesc();
203 * Get string serial descriptor
205 * @returns pointer to the string serial descriptor
207 virtual uint8_t * stringIserialDesc();
210 * Get string configuration descriptor
212 * @returns pointer to the string configuration descriptor
214 virtual uint8_t * stringIConfigurationDesc();
217 * Get string interface descriptor
219 * @returns pointer to the string interface descriptor
221 virtual uint8_t * stringIinterfaceDesc();
224 * Get the length of the report descriptor
226 * @returns length of the report descriptor
228 virtual uint16_t reportDescLength() { return 0; };
233 virtual void busReset(void);
234 virtual void EP0setupCallback(void);
235 virtual void EP0out(void);
236 virtual void EP0in(void);
237 virtual void connectStateChanged(unsigned int connected);
238 virtual void suspendStateChanged(unsigned int suspended);
239 uint8_t * findDescriptor(uint8_t descriptorType);
240 CONTROL_TRANSFER * getTransferPtr(void);
244 uint16_t PRODUCT_RELEASE;
247 bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
248 bool requestGetDescriptor(void);
249 bool controlOut(void);
250 bool controlIn(void);
251 bool requestSetAddress(void);
252 bool requestSetConfiguration(void);
253 bool requestSetFeature(void);
254 bool requestClearFeature(void);
255 bool requestGetStatus(void);
256 bool requestSetup(void);
257 bool controlSetup(void);
258 void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);
259 bool requestGetConfiguration(void);
260 bool requestGetInterface(void);
261 bool requestSetInterface(void);
263 CONTROL_TRANSFER transfer;
266 uint16_t currentInterface;
267 uint8_t currentAlternate;