Fork of the official USBDevice library

Fork of USBDevice by mbed official

Committer:
screamer
Date:
Fri Apr 28 11:26:51 2017 +0100
Branch:
github-merge
Revision:
73:8d28a0cb7b43
Parent:
25:7c72828865f3
Merge from mbed-os @ github

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 73:8d28a0cb7b43 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
screamer 73:8d28a0cb7b43 2 *
screamer 73:8d28a0cb7b43 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
screamer 73:8d28a0cb7b43 4 * and associated documentation files (the "Software"), to deal in the Software without
screamer 73:8d28a0cb7b43 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
screamer 73:8d28a0cb7b43 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
screamer 73:8d28a0cb7b43 7 * Software is furnished to do so, subject to the following conditions:
screamer 73:8d28a0cb7b43 8 *
screamer 73:8d28a0cb7b43 9 * The above copyright notice and this permission notice shall be included in all copies or
screamer 73:8d28a0cb7b43 10 * substantial portions of the Software.
screamer 73:8d28a0cb7b43 11 *
screamer 73:8d28a0cb7b43 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
screamer 73:8d28a0cb7b43 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
screamer 73:8d28a0cb7b43 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
screamer 73:8d28a0cb7b43 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
screamer 73:8d28a0cb7b43 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
screamer 73:8d28a0cb7b43 17 */
screamer 73:8d28a0cb7b43 18
screamer 73:8d28a0cb7b43 19 #ifndef USBDEVICE_H
screamer 73:8d28a0cb7b43 20 #define USBDEVICE_H
screamer 73:8d28a0cb7b43 21
screamer 73:8d28a0cb7b43 22 #include "mbed.h"
screamer 73:8d28a0cb7b43 23 #include "USBDevice_Types.h"
screamer 73:8d28a0cb7b43 24 #include "USBHAL.h"
screamer 73:8d28a0cb7b43 25
screamer 73:8d28a0cb7b43 26 class USBDevice: public USBHAL
screamer 73:8d28a0cb7b43 27 {
screamer 73:8d28a0cb7b43 28 public:
screamer 73:8d28a0cb7b43 29 USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
screamer 73:8d28a0cb7b43 30
screamer 73:8d28a0cb7b43 31 /*
screamer 73:8d28a0cb7b43 32 * Check if the device is configured
screamer 73:8d28a0cb7b43 33 *
screamer 73:8d28a0cb7b43 34 * @returns true if configured, false otherwise
screamer 73:8d28a0cb7b43 35 */
screamer 73:8d28a0cb7b43 36 bool configured(void);
screamer 73:8d28a0cb7b43 37
screamer 73:8d28a0cb7b43 38 /*
screamer 73:8d28a0cb7b43 39 * Connect a device
screamer 73:8d28a0cb7b43 40 *
screamer 73:8d28a0cb7b43 41 * @param blocking: block if not configured
screamer 73:8d28a0cb7b43 42 */
screamer 73:8d28a0cb7b43 43 void connect(bool blocking = true);
screamer 73:8d28a0cb7b43 44
screamer 73:8d28a0cb7b43 45 /*
screamer 73:8d28a0cb7b43 46 * Disconnect a device
screamer 73:8d28a0cb7b43 47 */
screamer 73:8d28a0cb7b43 48 void disconnect(void);
screamer 73:8d28a0cb7b43 49
screamer 73:8d28a0cb7b43 50 /*
screamer 73:8d28a0cb7b43 51 * Add an endpoint
screamer 73:8d28a0cb7b43 52 *
screamer 73:8d28a0cb7b43 53 * @param endpoint endpoint which will be added
screamer 73:8d28a0cb7b43 54 * @param maxPacket Maximum size of a packet which can be sent for this endpoint
screamer 73:8d28a0cb7b43 55 * @returns true if successful, false otherwise
screamer 73:8d28a0cb7b43 56 */
screamer 73:8d28a0cb7b43 57 bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
screamer 73:8d28a0cb7b43 58
screamer 73:8d28a0cb7b43 59 /*
screamer 73:8d28a0cb7b43 60 * Start a reading on a certain endpoint.
screamer 73:8d28a0cb7b43 61 * You can access the result of the reading by USBDevice_read
screamer 73:8d28a0cb7b43 62 *
screamer 73:8d28a0cb7b43 63 * @param endpoint endpoint which will be read
screamer 73:8d28a0cb7b43 64 * @param maxSize the maximum length that can be read
screamer 73:8d28a0cb7b43 65 * @return true if successful
screamer 73:8d28a0cb7b43 66 */
screamer 73:8d28a0cb7b43 67 bool readStart(uint8_t endpoint, uint32_t maxSize);
screamer 73:8d28a0cb7b43 68
screamer 73:8d28a0cb7b43 69 /*
screamer 73:8d28a0cb7b43 70 * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
screamer 73:8d28a0cb7b43 71 * must be called.
screamer 73:8d28a0cb7b43 72 *
screamer 73:8d28a0cb7b43 73 * Warning: blocking
screamer 73:8d28a0cb7b43 74 *
screamer 73:8d28a0cb7b43 75 * @param endpoint endpoint which will be read
screamer 73:8d28a0cb7b43 76 * @param buffer buffer will be filled with the data received
screamer 73:8d28a0cb7b43 77 * @param size the number of bytes read will be stored in *size
screamer 73:8d28a0cb7b43 78 * @param maxSize the maximum length that can be read
screamer 73:8d28a0cb7b43 79 * @returns true if successful
screamer 73:8d28a0cb7b43 80 */
screamer 73:8d28a0cb7b43 81 bool readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
screamer 73:8d28a0cb7b43 82
screamer 73:8d28a0cb7b43 83 /*
screamer 73:8d28a0cb7b43 84 * Read a certain endpoint.
screamer 73:8d28a0cb7b43 85 *
screamer 73:8d28a0cb7b43 86 * Warning: non blocking
screamer 73:8d28a0cb7b43 87 *
screamer 73:8d28a0cb7b43 88 * @param endpoint endpoint which will be read
screamer 73:8d28a0cb7b43 89 * @param buffer buffer will be filled with the data received (if data are available)
screamer 73:8d28a0cb7b43 90 * @param size the number of bytes read will be stored in *size
screamer 73:8d28a0cb7b43 91 * @param maxSize the maximum length that can be read
screamer 73:8d28a0cb7b43 92 * @returns true if successful
screamer 73:8d28a0cb7b43 93 */
screamer 73:8d28a0cb7b43 94 bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
screamer 73:8d28a0cb7b43 95
screamer 73:8d28a0cb7b43 96 /*
screamer 73:8d28a0cb7b43 97 * Write a certain endpoint.
screamer 73:8d28a0cb7b43 98 *
screamer 73:8d28a0cb7b43 99 * Warning: blocking
screamer 73:8d28a0cb7b43 100 *
screamer 73:8d28a0cb7b43 101 * @param endpoint endpoint to write
screamer 73:8d28a0cb7b43 102 * @param buffer data contained in buffer will be write
screamer 73:8d28a0cb7b43 103 * @param size the number of bytes to write
screamer 73:8d28a0cb7b43 104 * @param maxSize the maximum length that can be written on this endpoint
screamer 73:8d28a0cb7b43 105 */
screamer 73:8d28a0cb7b43 106 bool write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
screamer 73:8d28a0cb7b43 107
screamer 73:8d28a0cb7b43 108
screamer 73:8d28a0cb7b43 109 /*
screamer 73:8d28a0cb7b43 110 * Write a certain endpoint.
screamer 73:8d28a0cb7b43 111 *
screamer 73:8d28a0cb7b43 112 * Warning: non blocking
screamer 73:8d28a0cb7b43 113 *
screamer 73:8d28a0cb7b43 114 * @param endpoint endpoint to write
screamer 73:8d28a0cb7b43 115 * @param buffer data contained in buffer will be write
screamer 73:8d28a0cb7b43 116 * @param size the number of bytes to write
screamer 73:8d28a0cb7b43 117 * @param maxSize the maximum length that can be written on this endpoint
screamer 73:8d28a0cb7b43 118 */
screamer 73:8d28a0cb7b43 119 bool writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
screamer 73:8d28a0cb7b43 120
screamer 73:8d28a0cb7b43 121
screamer 73:8d28a0cb7b43 122 /*
screamer 73:8d28a0cb7b43 123 * Called by USBDevice layer on bus reset. Warning: Called in ISR context
screamer 73:8d28a0cb7b43 124 *
screamer 73:8d28a0cb7b43 125 * May be used to reset state
screamer 73:8d28a0cb7b43 126 */
screamer 73:8d28a0cb7b43 127 virtual void USBCallback_busReset(void) {};
screamer 73:8d28a0cb7b43 128
screamer 73:8d28a0cb7b43 129 /*
screamer 73:8d28a0cb7b43 130 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
screamer 73:8d28a0cb7b43 131 * This is used to handle extensions to standard requests
screamer 73:8d28a0cb7b43 132 * and class specific requests
screamer 73:8d28a0cb7b43 133 *
screamer 73:8d28a0cb7b43 134 * @returns true if class handles this request
screamer 73:8d28a0cb7b43 135 */
screamer 73:8d28a0cb7b43 136 virtual bool USBCallback_request() { return false; };
screamer 73:8d28a0cb7b43 137
screamer 73:8d28a0cb7b43 138 /*
screamer 73:8d28a0cb7b43 139 * Called by USBDevice on Endpoint0 request completion
screamer 73:8d28a0cb7b43 140 * if the 'notify' flag has been set to true. Warning: Called in ISR context
screamer 73:8d28a0cb7b43 141 *
screamer 73:8d28a0cb7b43 142 * In this case it is used to indicate that a HID report has
screamer 73:8d28a0cb7b43 143 * been received from the host on endpoint 0
screamer 73:8d28a0cb7b43 144 *
screamer 73:8d28a0cb7b43 145 * @param buf buffer received on endpoint 0
screamer 73:8d28a0cb7b43 146 * @param length length of this buffer
screamer 73:8d28a0cb7b43 147 */
screamer 73:8d28a0cb7b43 148 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {};
screamer 73:8d28a0cb7b43 149
screamer 73:8d28a0cb7b43 150 /*
screamer 73:8d28a0cb7b43 151 * Called by USBDevice layer. Set configuration of the device.
screamer 73:8d28a0cb7b43 152 * For instance, you can add all endpoints that you need on this function.
screamer 73:8d28a0cb7b43 153 *
screamer 73:8d28a0cb7b43 154 * @param configuration Number of the configuration
screamer 73:8d28a0cb7b43 155 */
screamer 73:8d28a0cb7b43 156 virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; };
screamer 73:8d28a0cb7b43 157
screamer 73:8d28a0cb7b43 158 /*
screamer 73:8d28a0cb7b43 159 * Called by USBDevice layer. Set interface/alternate of the device.
screamer 73:8d28a0cb7b43 160 *
screamer 73:8d28a0cb7b43 161 * @param interface Number of the interface to be configured
screamer 73:8d28a0cb7b43 162 * @param alternate Number of the alternate to be configured
screamer 73:8d28a0cb7b43 163 * @returns true if class handles this request
screamer 73:8d28a0cb7b43 164 */
screamer 73:8d28a0cb7b43 165 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
screamer 73:8d28a0cb7b43 166
screamer 73:8d28a0cb7b43 167 /*
screamer 73:8d28a0cb7b43 168 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
screamer 73:8d28a0cb7b43 169 *
screamer 73:8d28a0cb7b43 170 * @returns pointer to the device descriptor
screamer 73:8d28a0cb7b43 171 */
screamer 73:8d28a0cb7b43 172 virtual uint8_t * deviceDesc();
screamer 73:8d28a0cb7b43 173
screamer 73:8d28a0cb7b43 174 /*
screamer 73:8d28a0cb7b43 175 * Get configuration descriptor
screamer 73:8d28a0cb7b43 176 *
screamer 73:8d28a0cb7b43 177 * @returns pointer to the configuration descriptor
screamer 73:8d28a0cb7b43 178 */
screamer 73:8d28a0cb7b43 179 virtual uint8_t * configurationDesc(){return NULL;};
screamer 73:8d28a0cb7b43 180
screamer 73:8d28a0cb7b43 181 /*
screamer 73:8d28a0cb7b43 182 * Get string lang id descriptor
screamer 73:8d28a0cb7b43 183 *
screamer 73:8d28a0cb7b43 184 * @return pointer to the string lang id descriptor
screamer 73:8d28a0cb7b43 185 */
screamer 73:8d28a0cb7b43 186 virtual uint8_t * stringLangidDesc();
screamer 73:8d28a0cb7b43 187
screamer 73:8d28a0cb7b43 188 /*
screamer 73:8d28a0cb7b43 189 * Get string manufacturer descriptor
screamer 73:8d28a0cb7b43 190 *
screamer 73:8d28a0cb7b43 191 * @returns pointer to the string manufacturer descriptor
screamer 73:8d28a0cb7b43 192 */
screamer 73:8d28a0cb7b43 193 virtual uint8_t * stringImanufacturerDesc();
screamer 73:8d28a0cb7b43 194
screamer 73:8d28a0cb7b43 195 /*
screamer 73:8d28a0cb7b43 196 * Get string product descriptor
screamer 73:8d28a0cb7b43 197 *
screamer 73:8d28a0cb7b43 198 * @returns pointer to the string product descriptor
screamer 73:8d28a0cb7b43 199 */
screamer 73:8d28a0cb7b43 200 virtual uint8_t * stringIproductDesc();
screamer 73:8d28a0cb7b43 201
screamer 73:8d28a0cb7b43 202 /*
screamer 73:8d28a0cb7b43 203 * Get string serial descriptor
screamer 73:8d28a0cb7b43 204 *
screamer 73:8d28a0cb7b43 205 * @returns pointer to the string serial descriptor
screamer 73:8d28a0cb7b43 206 */
screamer 73:8d28a0cb7b43 207 virtual uint8_t * stringIserialDesc();
screamer 73:8d28a0cb7b43 208
screamer 73:8d28a0cb7b43 209 /*
screamer 73:8d28a0cb7b43 210 * Get string configuration descriptor
screamer 73:8d28a0cb7b43 211 *
screamer 73:8d28a0cb7b43 212 * @returns pointer to the string configuration descriptor
screamer 73:8d28a0cb7b43 213 */
screamer 73:8d28a0cb7b43 214 virtual uint8_t * stringIConfigurationDesc();
screamer 73:8d28a0cb7b43 215
screamer 73:8d28a0cb7b43 216 /*
screamer 73:8d28a0cb7b43 217 * Get string interface descriptor
screamer 73:8d28a0cb7b43 218 *
screamer 73:8d28a0cb7b43 219 * @returns pointer to the string interface descriptor
screamer 73:8d28a0cb7b43 220 */
screamer 73:8d28a0cb7b43 221 virtual uint8_t * stringIinterfaceDesc();
screamer 73:8d28a0cb7b43 222
screamer 73:8d28a0cb7b43 223 /*
screamer 73:8d28a0cb7b43 224 * Get the length of the report descriptor
screamer 73:8d28a0cb7b43 225 *
screamer 73:8d28a0cb7b43 226 * @returns length of the report descriptor
screamer 73:8d28a0cb7b43 227 */
screamer 73:8d28a0cb7b43 228 virtual uint16_t reportDescLength() { return 0; };
screamer 73:8d28a0cb7b43 229
screamer 73:8d28a0cb7b43 230
screamer 73:8d28a0cb7b43 231
screamer 73:8d28a0cb7b43 232 protected:
screamer 73:8d28a0cb7b43 233 virtual void busReset(void);
screamer 73:8d28a0cb7b43 234 virtual void EP0setupCallback(void);
screamer 73:8d28a0cb7b43 235 virtual void EP0out(void);
screamer 73:8d28a0cb7b43 236 virtual void EP0in(void);
screamer 73:8d28a0cb7b43 237 virtual void connectStateChanged(unsigned int connected);
screamer 73:8d28a0cb7b43 238 virtual void suspendStateChanged(unsigned int suspended);
screamer 73:8d28a0cb7b43 239 uint8_t * findDescriptor(uint8_t descriptorType);
screamer 73:8d28a0cb7b43 240 CONTROL_TRANSFER * getTransferPtr(void);
screamer 73:8d28a0cb7b43 241
screamer 73:8d28a0cb7b43 242 uint16_t VENDOR_ID;
screamer 73:8d28a0cb7b43 243 uint16_t PRODUCT_ID;
screamer 73:8d28a0cb7b43 244 uint16_t PRODUCT_RELEASE;
screamer 73:8d28a0cb7b43 245
screamer 73:8d28a0cb7b43 246 private:
screamer 73:8d28a0cb7b43 247 bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
screamer 73:8d28a0cb7b43 248 bool requestGetDescriptor(void);
screamer 73:8d28a0cb7b43 249 bool controlOut(void);
screamer 73:8d28a0cb7b43 250 bool controlIn(void);
screamer 73:8d28a0cb7b43 251 bool requestSetAddress(void);
screamer 73:8d28a0cb7b43 252 bool requestSetConfiguration(void);
screamer 73:8d28a0cb7b43 253 bool requestSetFeature(void);
screamer 73:8d28a0cb7b43 254 bool requestClearFeature(void);
screamer 73:8d28a0cb7b43 255 bool requestGetStatus(void);
screamer 73:8d28a0cb7b43 256 bool requestSetup(void);
screamer 73:8d28a0cb7b43 257 bool controlSetup(void);
screamer 73:8d28a0cb7b43 258 void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);
screamer 73:8d28a0cb7b43 259 bool requestGetConfiguration(void);
screamer 73:8d28a0cb7b43 260 bool requestGetInterface(void);
screamer 73:8d28a0cb7b43 261 bool requestSetInterface(void);
screamer 73:8d28a0cb7b43 262
screamer 73:8d28a0cb7b43 263 CONTROL_TRANSFER transfer;
screamer 73:8d28a0cb7b43 264 USB_DEVICE device;
screamer 73:8d28a0cb7b43 265
screamer 73:8d28a0cb7b43 266 uint16_t currentInterface;
screamer 73:8d28a0cb7b43 267 uint8_t currentAlternate;
screamer 73:8d28a0cb7b43 268 };
screamer 73:8d28a0cb7b43 269
screamer 73:8d28a0cb7b43 270
screamer 73:8d28a0cb7b43 271 #endif