]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/net/cellular/CellularUSBModem/serial/usb/USBSerialStream.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / net / cellular / CellularUSBModem / serial / usb / USBSerialStream.h
1 /* USBSerialStream.h */
2 /* Copyright (C) 2012 mbed.org, MIT License
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5  * and associated documentation files (the "Software"), to deal in the Software without restriction,
6  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
7  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in all copies or
11  * substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18  */
19
20 #ifndef USBSERIALSTREAM_H_
21 #define USBSERIALSTREAM_H_
22
23
24 #include "core/fwk.h"
25
26 #include "USBHost3GModule/IUSBHostSerial.h"
27 #include "USBHost3GModule/IUSBHostSerialListener.h"
28
29 #include "rtos.h"
30 #include "core/MtxCircBuffer.h"
31
32 /* Input Serial Stream for USB virtual serial ports interfaces
33 This class is not thread-safe, except for the *Abort() methods that can be called by any thread/ISR
34 */
35
36 class USBSerialStream : public IOStream, IUSBHostSerialListener
37 {
38 public:
39   enum { CIRCBUF_SIZE = 127 };
40   USBSerialStream(IUSBHostSerial& serial);
41   /*virtual*/ ~USBSerialStream();
42
43   //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
44   virtual int read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout=osWaitForever);
45   virtual size_t available();
46   virtual int waitAvailable(uint32_t timeout=osWaitForever); //Wait for data to be available
47   virtual int abortRead(); //Abort current reading (or waiting) operation
48
49
50   //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
51   virtual int write(uint8_t* buf, size_t length, uint32_t timeout=osWaitForever);
52   virtual size_t space();
53   virtual int waitSpace(uint32_t timeout=osWaitForever); //Wait for space to be available
54   virtual int abortWrite(); //Abort current writing (or waiting) operation
55
56 private:
57   IUSBHostSerial& m_serial;
58   volatile bool m_serialTxFifoEmpty;
59
60   void setupReadableISR(bool en);
61   virtual void readable(); //Callback from m_serial when new data is available
62
63   Semaphore m_availableSphre; //Used for signalling
64
65   void setupWriteableISR(bool en);
66   virtual void writeable(); //Callback from m_serial when new space is available
67
68   Semaphore m_spaceSphre; //Used for signalling
69
70   MtxCircBuffer<uint8_t, CIRCBUF_SIZE + 1> m_inBuf;
71   MtxCircBuffer<uint8_t, CIRCBUF_SIZE + 1> m_outBuf;
72 };
73
74 #endif /* USBSERIALSTREAM_H_ */