Fork of https://developer.mbed.org/users/bscott/code/STM32_USBDevice/
Fork of STM32_USBDevice by
USBWebUSBSerial.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #ifndef USBWEBUSBSERIAL_H 00020 #define USBWEBUSBSERIAL_H 00021 00022 #include <string> 00023 #include "USBDevice.h" 00024 #include "CircBuffer.h" 00025 #include "USBEndpoints.h" 00026 #include "USBDescriptor.h" 00027 #include "USBDevice_Types.h" 00028 00029 #ifndef USBWEBUSBSERIAL_INPUT_BUFFER_SIZE 00030 #define USBWEBUSBSERIAL_INPUT_BUFFER_SIZE 128 00031 #endif 00032 00033 #ifndef USBWEBUSBSERIAL_OUTPUT_BUFFER_SIZE 00034 #define USBWEBUSBSERIAL_OUTPUT_BUFFER_SIZE 512 00035 #endif 00036 00037 /** 00038 * WebUSB/USBSerial composite device class 00039 * Handles outputting to standard CDC or WebUSB endpoint depending on mode 00040 * 00041 */ 00042 class USBWebUSBSerial: public USBDevice 00043 { 00044 public: 00045 00046 /** 00047 * Constructor 00048 * 00049 * @param vendor_id Your vendor_id 00050 * @param product_id Your product_id 00051 * @param product_release Your product_release (default: 0x0001) 00052 * @param connect_blocking define if the connection must be blocked if USB not plugged in 00053 * 00054 */ 00055 USBWebUSBSerial(uint16_t vendor_id, uint16_t product_id, uint16_t product_release = 0x0001, bool connect_blocking = true); 00056 virtual ~USBWebUSBSerial(); 00057 00058 const std::string& manufacturerName() const { return m_manufacturerName; } 00059 void setManufacturerName(const std::string &manufacturerName); 00060 00061 const std::string& productName() const { return m_productName; } 00062 void setProductName(const std::string &productName); 00063 00064 const std::string& serialNumber() const { return m_serialNumber; } 00065 void setSerialNumber(const std::string &serialNumber); 00066 00067 /** 00068 * Read up to size bytes from input buffer 00069 * 00070 * @returns the number of bytes read 00071 */ 00072 00073 int read(uint8_t *buf, uint16_t size); 00074 00075 /** 00076 * Check the number of bytes available. 00077 * 00078 * @returns the number of bytes available 00079 */ 00080 uint8_t available(); 00081 00082 /** Determine if there is a character available to read 00083 * 00084 * @returns 00085 * 1 if there is a character available to read, 00086 * 0 otherwise 00087 */ 00088 int readable() { return available() ? 1 : 0; } 00089 00090 /** Determine if there is space available to write a character 00091 * 00092 * @returns 00093 * 1 if there is space to write a character, 00094 * 0 otherwise 00095 */ 00096 int writeable() { return 1; } // Always return 1 since we use a circular buffer 00097 00098 /** 00099 * Write data asynchronously using internal output buffer 00100 * 00101 * @param buf pointer to data 00102 * @param size size of the buffer. The maximum size of data is limited by the size of the internal buffer 00103 * 00104 * @returns true if successfull 00105 */ 00106 bool writeBuffered(const uint8_t * buf, uint16_t size); 00107 00108 /** 00109 * Attach a member function to call when a packet is received. 00110 * 00111 * @param tptr pointer to the object to call the member function on 00112 * @param mptr pointer to the member function to be called 00113 */ 00114 template<typename T> 00115 void attach(T* tptr, void (T::*mptr)(void)) 00116 { 00117 if (mptr && tptr) 00118 { 00119 m_rx.attach(tptr, mptr); 00120 } 00121 } 00122 00123 /** 00124 * Attach a callback called when a packet is received 00125 * 00126 * @param fptr function pointer 00127 */ 00128 void attach(void (*fptr)(void)) 00129 { 00130 if (fptr) 00131 { 00132 m_rx.attach(fptr); 00133 } 00134 } 00135 00136 /** 00137 * Attach a callback to call when serial's settings are changed. 00138 * 00139 * @param fptr function pointer 00140 */ 00141 void attach(void (*fptr)(int baud, int bits, int parity, int stop)) 00142 { 00143 m_settingsChangedCallback = fptr; 00144 } 00145 00146 protected: 00147 00148 /* 00149 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength. 00150 * 00151 * @returns pointer to the device descriptor 00152 */ 00153 virtual uint8_t * deviceDesc(); 00154 00155 /* 00156 * Get string manufacturer descriptor 00157 * 00158 * @returns pointer to the string manufacturer descriptor 00159 */ 00160 virtual uint8_t * stringImanufacturerDesc(); 00161 00162 /* 00163 * Get string product descriptor 00164 * 00165 * @returns pointer to the string product descriptor 00166 */ 00167 virtual uint8_t * stringIproductDesc(); 00168 00169 /* 00170 * Get string serial descriptor 00171 * 00172 * @returns pointer to the string serial descriptor 00173 */ 00174 virtual uint8_t * stringIserialDesc(); 00175 00176 /* 00177 * Get configuration descriptor 00178 * 00179 * @returns pointer to the configuration descriptor 00180 */ 00181 virtual uint8_t * configurationDesc(); 00182 00183 /* 00184 * Get BOS descriptor 00185 * 00186 * @returns pointer to the BOS descriptor 00187 */ 00188 virtual uint8_t * bosDesc(); 00189 00190 /* 00191 * Write from internal buffer to active endpoint 00192 */ 00193 void writeToActiveEndpoint(); 00194 00195 /* 00196 * Read from the active endpoint into the internal buffer (non-blocking) 00197 * 00198 * @returns true if successful 00199 */ 00200 bool readActiveEP(); 00201 00202 /* 00203 * Called by USBCallback_requestCompleted when CDC line coding is changed 00204 * Warning: Called in ISR 00205 * 00206 * @param baud The baud rate 00207 * @param bits The number of bits in a word (5-8) 00208 * @param parity The parity 00209 * @param stop The number of stop bits (1 or 2) 00210 */ 00211 virtual void lineCodingChanged(int baud, int bits, int parity, int stop) 00212 { 00213 if (m_settingsChangedCallback) 00214 { 00215 m_settingsChangedCallback(baud, bits, parity, stop); 00216 } 00217 } 00218 00219 virtual bool USBCallback_request(); 00220 virtual void USBCallback_requestCompleted(uint8_t *buf, uint32_t length); 00221 virtual bool USBCallback_setConfiguration(uint8_t configuration); 00222 virtual void USBCallback_busReset(void); 00223 00224 virtual bool EPCDC_OUT_callback(); 00225 virtual bool EPWEBUSB_OUT_callback(); 00226 virtual bool EPOUTCallbackHandler(uint8_t endpoint); 00227 00228 virtual bool EPCDC_IN_callback(); 00229 virtual bool EPWEBUSB_IN_callback(); 00230 00231 private: 00232 uint8_t activeOutEndpoint() const { return m_webUSBMode ? EPWEBUSB_OUT : EPCDC_OUT; } 00233 uint8_t activeInEndpoint() const { return m_webUSBMode ? EPWEBUSB_IN : EPCDC_IN; } 00234 void createDynamicDescriptors(); 00235 uint8_t *createStringDescriptor(const std::string &string) const; 00236 void setWebUSBMode(bool webUSBMode); 00237 uint32_t timeSinceWrite() const; 00238 00239 static uint8_t s_MSOS2Descriptor[]; 00240 volatile bool m_terminalConnected; 00241 FunctionPointer m_rx; 00242 CircBuffer<uint8_t,USBWEBUSBSERIAL_INPUT_BUFFER_SIZE> m_inputBuffer; 00243 CircBuffer<uint8_t,USBWEBUSBSERIAL_OUTPUT_BUFFER_SIZE> m_outputBuffer; 00244 void (*m_settingsChangedCallback)(int baud, int bits, int parity, int stop) = nullptr; 00245 00246 bool m_pendingWrite = false; 00247 uint32_t m_lastWriteTime = 0; 00248 std::string m_manufacturerName = "mbed.org"; 00249 uint8_t *m_manufacturerStringDesc = nullptr; 00250 std::string m_productName = "CDC/WebUSB Device"; 00251 uint8_t *m_productStringDesc = nullptr; 00252 std::string m_serialNumber = "0123456789"; 00253 uint8_t *m_serialStringDesc = nullptr; 00254 00255 bool m_webUSBMode = false; 00256 }; 00257 00258 #endif
Generated on Wed Jul 13 2022 00:18:45 by 1.7.2