PHS module SMA-01 library. see: https://developer.mbed.org/users/phsfan/notebook/abitusbmodem/
Dependencies: Socket lwip-sys lwip
Dependents: AbitUSBModem_HTTPTest AbitUSBModem_MQTTTest AbitUSBModem_WebsocketTest AbitUSBModem_SMSTest
Fork of VodafoneUSBModem by
serial/usb/USBSerialStream.cpp@99:514e67a69ad6, 2015-02-25 (annotated)
- Committer:
- phsfan
- Date:
- Wed Feb 25 14:34:13 2015 +0000
- Revision:
- 99:514e67a69ad6
- Parent:
- 95:84f01d280c9b
supported SMS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:3b2f052c333b | 1 | /* USBSerialStream.cpp */ |
donatien | 22:06fb2a93a1f6 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
donatien | 22:06fb2a93a1f6 | 3 | * |
donatien | 22:06fb2a93a1f6 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 22:06fb2a93a1f6 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
donatien | 22:06fb2a93a1f6 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
donatien | 22:06fb2a93a1f6 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
donatien | 22:06fb2a93a1f6 | 8 | * furnished to do so, subject to the following conditions: |
donatien | 22:06fb2a93a1f6 | 9 | * |
donatien | 22:06fb2a93a1f6 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 22:06fb2a93a1f6 | 11 | * substantial portions of the Software. |
donatien | 22:06fb2a93a1f6 | 12 | * |
donatien | 22:06fb2a93a1f6 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 22:06fb2a93a1f6 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 22:06fb2a93a1f6 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 22:06fb2a93a1f6 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 22:06fb2a93a1f6 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 22:06fb2a93a1f6 | 18 | */ |
donatien | 0:3b2f052c333b | 19 | |
ashleymills | 83:897a0de9d668 | 20 | |
ashleymills | 83:897a0de9d668 | 21 | |
donatien | 61:0bcb8c5216d4 | 22 | #define __DEBUG__ 0 |
donatien | 0:3b2f052c333b | 23 | #ifndef __MODULE__ |
donatien | 0:3b2f052c333b | 24 | #define __MODULE__ "USBSerialStream.cpp" |
donatien | 0:3b2f052c333b | 25 | #endif |
donatien | 0:3b2f052c333b | 26 | |
donatien | 0:3b2f052c333b | 27 | #include "core/fwk.h" |
donatien | 0:3b2f052c333b | 28 | |
donatien | 62:6f42a974eea6 | 29 | #include <cstring> |
donatien | 0:3b2f052c333b | 30 | |
donatien | 0:3b2f052c333b | 31 | #include "USBSerialStream.h" |
donatien | 0:3b2f052c333b | 32 | |
donatien | 0:3b2f052c333b | 33 | USBSerialStream::USBSerialStream(IUSBHostSerial& serial) : m_serial(serial), m_serialTxFifoEmpty(true), |
donatien | 0:3b2f052c333b | 34 | m_availableSphre(1), m_spaceSphre(1), m_inBuf() |
donatien | 0:3b2f052c333b | 35 | { |
donatien | 0:3b2f052c333b | 36 | m_availableSphre.wait(); |
donatien | 0:3b2f052c333b | 37 | m_spaceSphre.wait(); |
donatien | 0:3b2f052c333b | 38 | //Attach interrupts |
donatien | 0:3b2f052c333b | 39 | m_serial.attach(this); |
donatien | 0:3b2f052c333b | 40 | } |
donatien | 0:3b2f052c333b | 41 | |
donatien | 0:3b2f052c333b | 42 | /*virtual*/ USBSerialStream::~USBSerialStream() |
donatien | 0:3b2f052c333b | 43 | { |
donatien | 0:3b2f052c333b | 44 | m_serial.attach(NULL); |
donatien | 0:3b2f052c333b | 45 | } |
donatien | 0:3b2f052c333b | 46 | |
donatien | 0:3b2f052c333b | 47 | //0 for non-blocking (returns immediately), -1 for infinite blocking |
donatien | 0:3b2f052c333b | 48 | /*virtual*/ int USBSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/) |
donatien | 0:3b2f052c333b | 49 | { |
donatien | 0:3b2f052c333b | 50 | DBG("Trying to read at most %d chars", maxLength); |
donatien | 0:3b2f052c333b | 51 | int ret = waitAvailable(timeout); |
donatien | 0:3b2f052c333b | 52 | if(ret) |
donatien | 0:3b2f052c333b | 53 | { |
donatien | 0:3b2f052c333b | 54 | WARN("Error %d while waiting for incoming data", ret); |
donatien | 0:3b2f052c333b | 55 | return ret; |
donatien | 0:3b2f052c333b | 56 | } |
donatien | 10:21a6f09d5631 | 57 | int a = available(); //Prevent macro issues |
donatien | 10:21a6f09d5631 | 58 | int readLen = MIN( a, maxLength ); |
donatien | 0:3b2f052c333b | 59 | *pLength = readLen; |
ashleymills | 83:897a0de9d668 | 60 | |
ashleymills | 83:897a0de9d668 | 61 | |
donatien | 0:3b2f052c333b | 62 | setupReadableISR(false); |
donatien | 0:3b2f052c333b | 63 | while(readLen--) |
donatien | 0:3b2f052c333b | 64 | { |
donatien | 0:3b2f052c333b | 65 | m_inBuf.dequeue(buf); |
donatien | 0:3b2f052c333b | 66 | buf++; |
donatien | 0:3b2f052c333b | 67 | } |
donatien | 0:3b2f052c333b | 68 | setupReadableISR(true); |
ashleymills | 83:897a0de9d668 | 69 | |
donatien | 0:3b2f052c333b | 70 | return OK; |
donatien | 0:3b2f052c333b | 71 | } |
donatien | 0:3b2f052c333b | 72 | |
donatien | 0:3b2f052c333b | 73 | /*virtual*/ size_t USBSerialStream::available() |
donatien | 0:3b2f052c333b | 74 | { |
donatien | 0:3b2f052c333b | 75 | setupReadableISR(false); //m_inBuf.available() is not reentrant |
donatien | 0:3b2f052c333b | 76 | size_t len = m_inBuf.available(); |
donatien | 0:3b2f052c333b | 77 | setupReadableISR(true); |
donatien | 0:3b2f052c333b | 78 | return len; |
donatien | 0:3b2f052c333b | 79 | } |
donatien | 0:3b2f052c333b | 80 | |
donatien | 0:3b2f052c333b | 81 | /*virtual*/ int USBSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available |
donatien | 0:3b2f052c333b | 82 | { |
donatien | 0:3b2f052c333b | 83 | int ret; |
donatien | 0:3b2f052c333b | 84 | if(available()) //Is data already available? |
donatien | 0:3b2f052c333b | 85 | { |
donatien | 10:21a6f09d5631 | 86 | while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available |
donatien | 0:3b2f052c333b | 87 | return OK; |
donatien | 0:3b2f052c333b | 88 | } |
donatien | 0:3b2f052c333b | 89 | |
donatien | 0:3b2f052c333b | 90 | DBG("Waiting for data availability %d ms (-1 is infinite)", timeout); |
donatien | 0:3b2f052c333b | 91 | ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort |
donatien | 0:3b2f052c333b | 92 | if(ret <= 0) |
donatien | 0:3b2f052c333b | 93 | { |
donatien | 0:3b2f052c333b | 94 | DBG("Timeout"); |
donatien | 0:3b2f052c333b | 95 | return NET_TIMEOUT; |
donatien | 0:3b2f052c333b | 96 | } |
donatien | 0:3b2f052c333b | 97 | if(!m_inBuf.available()) //Even if abort has been called, return that data is available |
donatien | 0:3b2f052c333b | 98 | { |
donatien | 0:3b2f052c333b | 99 | DBG("Aborted"); |
donatien | 0:3b2f052c333b | 100 | return NET_INTERRUPTED; |
donatien | 0:3b2f052c333b | 101 | } |
donatien | 0:3b2f052c333b | 102 | DBG("Finished waiting"); |
donatien | 10:21a6f09d5631 | 103 | while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available |
donatien | 0:3b2f052c333b | 104 | return OK; |
donatien | 0:3b2f052c333b | 105 | } |
donatien | 0:3b2f052c333b | 106 | |
donatien | 0:3b2f052c333b | 107 | /*virtual*/ int USBSerialStream::abortRead() //Abort current reading (or waiting) operation |
donatien | 0:3b2f052c333b | 108 | { |
donatien | 0:3b2f052c333b | 109 | if( /*!available()*/true ) //If there is data pending, no need to abort |
donatien | 0:3b2f052c333b | 110 | { |
donatien | 0:3b2f052c333b | 111 | m_availableSphre.release(); //Force exiting the waiting state |
donatien | 0:3b2f052c333b | 112 | } |
donatien | 0:3b2f052c333b | 113 | else |
donatien | 0:3b2f052c333b | 114 | { |
donatien | 0:3b2f052c333b | 115 | DBG("Serial is readable"); ; |
donatien | 0:3b2f052c333b | 116 | } |
donatien | 0:3b2f052c333b | 117 | return OK; |
donatien | 0:3b2f052c333b | 118 | } |
donatien | 0:3b2f052c333b | 119 | |
donatien | 0:3b2f052c333b | 120 | void USBSerialStream::setupReadableISR(bool en) |
donatien | 0:3b2f052c333b | 121 | { |
donatien | 0:3b2f052c333b | 122 | m_serial.setupIrq(en, IUSBHostSerial::RxIrq); |
donatien | 0:3b2f052c333b | 123 | } |
donatien | 0:3b2f052c333b | 124 | |
donatien | 0:3b2f052c333b | 125 | void USBSerialStream::readable() //Callback from m_serial when new data is available |
donatien | 0:3b2f052c333b | 126 | { |
donatien | 0:3b2f052c333b | 127 | while(m_serial.readable()) |
donatien | 0:3b2f052c333b | 128 | { |
donatien | 0:3b2f052c333b | 129 | m_inBuf.queue(m_serial.getc()); |
donatien | 0:3b2f052c333b | 130 | } |
donatien | 0:3b2f052c333b | 131 | m_serial.readPacket(); //Start read of next packet |
donatien | 0:3b2f052c333b | 132 | m_availableSphre.release(); //Force exiting the waiting state |
donatien | 0:3b2f052c333b | 133 | } |
donatien | 0:3b2f052c333b | 134 | |
ashleymills | 83:897a0de9d668 | 135 | |
ashleymills | 83:897a0de9d668 | 136 | |
donatien | 0:3b2f052c333b | 137 | //0 for non-blocking (returns immediately), -1 for infinite blocking |
donatien | 0:3b2f052c333b | 138 | /*virtual*/ int USBSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=-1*/) |
donatien | 0:3b2f052c333b | 139 | { |
donatien | 0:3b2f052c333b | 140 | DBG("Trying to write %d chars", length); |
donatien | 0:3b2f052c333b | 141 | do |
donatien | 0:3b2f052c333b | 142 | { |
donatien | 0:3b2f052c333b | 143 | int ret = waitSpace(timeout); |
donatien | 0:3b2f052c333b | 144 | if(ret) |
donatien | 0:3b2f052c333b | 145 | { |
donatien | 10:21a6f09d5631 | 146 | WARN("Error %d while waiting for space", ret); |
donatien | 0:3b2f052c333b | 147 | return ret; |
donatien | 0:3b2f052c333b | 148 | } |
donatien | 10:21a6f09d5631 | 149 | int s = space(); //Prevent macro issues |
donatien | 10:21a6f09d5631 | 150 | int writeLen = MIN( s, length ); |
donatien | 10:21a6f09d5631 | 151 | DBG("Writing %d chars", writeLen); |
donatien | 0:3b2f052c333b | 152 | setupWriteableISR(false); |
donatien | 0:3b2f052c333b | 153 | while(writeLen) |
donatien | 0:3b2f052c333b | 154 | { |
donatien | 0:3b2f052c333b | 155 | m_outBuf.queue(*buf); |
donatien | 0:3b2f052c333b | 156 | buf++; |
donatien | 0:3b2f052c333b | 157 | length--; |
donatien | 0:3b2f052c333b | 158 | writeLen--; |
donatien | 0:3b2f052c333b | 159 | } |
donatien | 10:21a6f09d5631 | 160 | //If m_serial tx fifo is empty we need to start the packet write |
donatien | 10:21a6f09d5631 | 161 | if( m_outBuf.available() && m_serialTxFifoEmpty ) |
donatien | 10:21a6f09d5631 | 162 | { |
donatien | 10:21a6f09d5631 | 163 | writeable(); |
donatien | 10:21a6f09d5631 | 164 | } |
donatien | 0:3b2f052c333b | 165 | setupWriteableISR(true); |
donatien | 0:3b2f052c333b | 166 | } while(length); |
donatien | 0:3b2f052c333b | 167 | DBG("Write successful"); |
donatien | 0:3b2f052c333b | 168 | return OK; |
donatien | 0:3b2f052c333b | 169 | } |
donatien | 0:3b2f052c333b | 170 | |
donatien | 0:3b2f052c333b | 171 | /*virtual*/ size_t USBSerialStream::space() |
donatien | 0:3b2f052c333b | 172 | { |
donatien | 0:3b2f052c333b | 173 | setupWriteableISR(false); //m_outBuf.available() is not reentrant |
donatien | 0:3b2f052c333b | 174 | size_t len = CIRCBUF_SIZE - m_outBuf.available(); |
donatien | 0:3b2f052c333b | 175 | setupWriteableISR(true); |
donatien | 0:3b2f052c333b | 176 | return len; |
donatien | 0:3b2f052c333b | 177 | } |
donatien | 0:3b2f052c333b | 178 | |
donatien | 0:3b2f052c333b | 179 | /*virtual*/ int USBSerialStream::waitSpace(uint32_t timeout/*=-1*/) //Wait for space to be available |
donatien | 0:3b2f052c333b | 180 | { |
donatien | 0:3b2f052c333b | 181 | int ret; |
donatien | 0:3b2f052c333b | 182 | if(space()) //Is still space already left? |
donatien | 0:3b2f052c333b | 183 | { |
donatien | 10:21a6f09d5631 | 184 | while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available |
donatien | 0:3b2f052c333b | 185 | return OK; |
donatien | 0:3b2f052c333b | 186 | } |
donatien | 0:3b2f052c333b | 187 | |
donatien | 0:3b2f052c333b | 188 | DBG("Waiting for data space %d ms (-1 is infinite)", timeout); |
donatien | 0:3b2f052c333b | 189 | ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort |
donatien | 0:3b2f052c333b | 190 | if(ret <= 0) |
donatien | 0:3b2f052c333b | 191 | { |
donatien | 0:3b2f052c333b | 192 | DBG("Timeout"); |
donatien | 0:3b2f052c333b | 193 | return NET_TIMEOUT; |
donatien | 0:3b2f052c333b | 194 | } |
donatien | 0:3b2f052c333b | 195 | if(!space()) //Even if abort has been called, return that space is available |
donatien | 0:3b2f052c333b | 196 | { |
donatien | 0:3b2f052c333b | 197 | DBG("Aborted"); |
donatien | 0:3b2f052c333b | 198 | return NET_INTERRUPTED; |
donatien | 0:3b2f052c333b | 199 | } |
donatien | 10:21a6f09d5631 | 200 | while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available |
donatien | 0:3b2f052c333b | 201 | return OK; |
donatien | 0:3b2f052c333b | 202 | } |
donatien | 0:3b2f052c333b | 203 | |
donatien | 0:3b2f052c333b | 204 | /*virtual*/ int USBSerialStream::abortWrite() //Abort current writing (or waiting) operation |
donatien | 0:3b2f052c333b | 205 | { |
donatien | 0:3b2f052c333b | 206 | if( !space() ) //If there is space left, no need to abort |
donatien | 0:3b2f052c333b | 207 | { |
donatien | 0:3b2f052c333b | 208 | m_spaceSphre.release(); //Force exiting the waiting state |
donatien | 0:3b2f052c333b | 209 | } |
donatien | 0:3b2f052c333b | 210 | return OK; |
donatien | 0:3b2f052c333b | 211 | } |
donatien | 0:3b2f052c333b | 212 | |
donatien | 0:3b2f052c333b | 213 | void USBSerialStream::setupWriteableISR(bool en) |
donatien | 0:3b2f052c333b | 214 | { |
donatien | 0:3b2f052c333b | 215 | m_serial.setupIrq(en, IUSBHostSerial::TxIrq); |
donatien | 0:3b2f052c333b | 216 | } |
donatien | 0:3b2f052c333b | 217 | |
donatien | 0:3b2f052c333b | 218 | void USBSerialStream::writeable() //Callback from m_serial when new space is available |
donatien | 0:3b2f052c333b | 219 | { |
donatien | 0:3b2f052c333b | 220 | if(m_outBuf.isEmpty()) |
donatien | 0:3b2f052c333b | 221 | { |
donatien | 0:3b2f052c333b | 222 | m_serialTxFifoEmpty = true; |
donatien | 0:3b2f052c333b | 223 | } |
donatien | 0:3b2f052c333b | 224 | else |
donatien | 0:3b2f052c333b | 225 | { |
donatien | 0:3b2f052c333b | 226 | m_serialTxFifoEmpty = false; |
donatien | 0:3b2f052c333b | 227 | while(m_serial.writeable() && !m_outBuf.isEmpty()) |
donatien | 0:3b2f052c333b | 228 | { |
donatien | 0:3b2f052c333b | 229 | uint8_t c; |
donatien | 0:3b2f052c333b | 230 | m_outBuf.dequeue(&c); |
donatien | 0:3b2f052c333b | 231 | m_serial.putc((char)c); |
donatien | 0:3b2f052c333b | 232 | } |
donatien | 0:3b2f052c333b | 233 | m_serial.writePacket(); //Start packet write |
donatien | 0:3b2f052c333b | 234 | } |
donatien | 10:21a6f09d5631 | 235 | if(!m_outBuf.isFull()) |
donatien | 10:21a6f09d5631 | 236 | { |
donatien | 10:21a6f09d5631 | 237 | m_spaceSphre.release(); //Force exiting the waiting state |
donatien | 10:21a6f09d5631 | 238 | } |
donatien | 0:3b2f052c333b | 239 | } |