Simple test for USSD message.

Dependencies:   CellularModem USBHost

Dependents:   UbloxUSBModem

Fork of CellularUSBModem by mbed official

Committer:
nherriot
Date:
Mon Feb 24 15:22:14 2014 +0000
Revision:
6:ca9f8bd80a5f
Parent:
1:6547cd17fdb6
Simple test for USSD message.

Who changed what in which revision?

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