]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/net/cellular/CellularUSBModem/serial/io/IOSerialStream.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / net / cellular / CellularUSBModem / serial / io / IOSerialStream.h
1 /* IOSerialStream.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 #ifndef IOSERIALSTREAM_H_
20 #define IOSERIALSTREAM_H_
21
22 #include "core/fwk.h"
23
24 #include "RawSerial.h"
25
26 #include "rtos.h"
27 #include "core/MtxCircBuffer.h"
28
29 /** Input Serial Stream for physical serial interfaces (UART...)
30 This class is not thread-safe, except for the *Abort() methods that can be called by any thread/ISR
31 */
32 class IOSerialStream : public IOStream
33 {
34 public:
35   enum { CIRCBUF_SIZE = 255 };
36   IOSerialStream(mbed::RawSerial& serial);
37   /*virtual*/ ~IOSerialStream();
38
39   //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
40   virtual int read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout=osWaitForever);
41   virtual size_t available();
42   virtual int waitAvailable(uint32_t timeout=osWaitForever); //Wait for data to be available
43   virtual int abortRead(); //Abort current reading (or waiting) operation
44
45
46   //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
47   virtual int write(uint8_t* buf, size_t length, uint32_t timeout=osWaitForever);
48   virtual size_t space();
49   virtual int waitSpace(uint32_t timeout=osWaitForever); //Wait for space to be available
50   virtual int abortWrite(); //Abort current writing (or waiting) operation
51
52 private:
53
54   mbed::RawSerial& m_serial;
55   volatile bool m_serialTxFifoEmpty;
56
57   void setupReadableISR(bool en);
58   void readable(); //Callback from m_serial when new data is available
59
60   Semaphore m_availableSphre; //Used for signalling
61
62   void setupWriteableISR(bool en);
63   void writeable(); //Callback from m_serial when new space is available
64
65   Semaphore m_spaceSphre; //Used for signalling
66
67   MtxCircBuffer<uint8_t, CIRCBUF_SIZE + 1> m_inBuf;
68   MtxCircBuffer<uint8_t, CIRCBUF_SIZE + 1> m_outBuf;
69
70 };
71
72 #endif /* IOSERIALSTREAM_H_ */