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