Simple test for USSD message.

Dependencies:   CellularModem USBHost

Dependents:   UbloxUSBModem

Fork of CellularUSBModem by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IOSerialStream.cpp Source File

IOSerialStream.cpp

00001 /* IOSerialStream.cpp */
00002 /* Copyright (C) 2012 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 #define __DEBUG__ 0 //Maximum verbosity
00021 #ifndef __MODULE__
00022 #define __MODULE__ "IOSerialStream.cpp"
00023 #endif
00024 
00025 #include "core/fwk.h"
00026 
00027 #include <cstring>
00028 
00029 #include "IOSerialStream.h"
00030 
00031 #define UART_X ((LPC_UART_TypeDef *)(UART_1))
00032 
00033 IOSerialStream::IOSerialStream(mbed::RawSerial& serial) : m_serial(serial), m_serialTxFifoEmpty(true),
00034 m_availableSphre(1), m_spaceSphre(1), m_inBuf(), m_outBuf()
00035 {
00036   m_availableSphre.wait();
00037   m_spaceSphre.wait();
00038   //Attach interrupts
00039   m_serial.attach(this, &IOSerialStream::readable, mbed::SerialBase::RxIrq);
00040   m_serial.attach(this, &IOSerialStream::writeable, mbed::SerialBase::TxIrq);
00041 }
00042 
00043 /*virtual*/ IOSerialStream::~IOSerialStream()
00044 {
00045   m_serial.attach(NULL, mbed::SerialBase::RxIrq);
00046   m_serial.attach(NULL, mbed::SerialBase::TxIrq);
00047 }
00048 
00049 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
00050 /*virtual*/ int IOSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/)
00051 {
00052   DBG("Trying to read at most %d chars", maxLength);
00053   int ret = waitAvailable(timeout);
00054   if(ret)
00055   {
00056     WARN("Error %d while waiting for incoming data", ret);
00057     return ret;
00058   }
00059   int readLen = MIN( available(), maxLength );
00060   *pLength = readLen;
00061   setupReadableISR(false);
00062   while(readLen--)
00063   {
00064     m_inBuf.dequeue(buf);
00065     buf++;
00066   }
00067   setupReadableISR(true);
00068   DBG("Read %d chars successfully", *pLength);
00069   return OK;
00070 }
00071 
00072 /*virtual*/ size_t IOSerialStream::available()
00073 {
00074   setupReadableISR(false); //m_inBuf.available() is not reentrant
00075   size_t len = m_inBuf.available();
00076   setupReadableISR(true);
00077   return len;
00078 }
00079 
00080 /*virtual*/ int IOSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available
00081 {
00082   int ret;
00083   if(available()) //Is data already available?
00084   {
00085     m_availableSphre.wait(0); //Clear the queue as data is available
00086     return OK;
00087   }
00088 
00089   DBG("Waiting for data availability %d ms (-1 is infinite)", timeout);
00090   ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort
00091   if(ret <= 0)
00092   {
00093     DBG("Timeout");
00094     return NET_TIMEOUT;
00095   }
00096   if(!available()) //Even if abort has been called, return that data is available
00097   {
00098     DBG("Aborted");
00099     return NET_INTERRUPTED;
00100   }
00101   DBG("Finished waiting");
00102   m_availableSphre.wait(0); //Clear the queue as data is available
00103   return OK;
00104 }
00105 
00106 /*virtual*/ int IOSerialStream::abortRead() //Abort current reading (or waiting) operation
00107 {
00108   if( !available() ) //If there is data pending, no need to abort
00109   {
00110     m_availableSphre.release(); //Force exiting the waiting state; kludge to pass a RC directly
00111   }
00112   else
00113   {
00114     DBG("Serial is readable"); ;
00115   }
00116   return OK;
00117 }
00118 
00119 void IOSerialStream::setupReadableISR(bool en)
00120 {
00121   if(en)
00122   {
00123     UART_X->IER |= 1 << 0;
00124   }
00125   else
00126   {
00127     UART_X->IER &= ~(1 << 0);
00128   }
00129 }
00130 
00131 void IOSerialStream::readable() //Callback from m_serial when new data is available
00132 {
00133   do
00134   {
00135     m_inBuf.queue(m_serial.getc());
00136   } while(m_serial.readable());
00137   m_availableSphre.release(); //Force exiting the waiting state
00138 }
00139 
00140 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking
00141 /*virtual*/ int IOSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=osWaitForever*/)
00142 {
00143   DBG("Trying to write %d chars", length);
00144   int ret = waitSpace(timeout);
00145   if(ret)
00146   {
00147     WARN("Error %d while waiting for space", ret);
00148     return ret;
00149   }
00150   DBG("Writing %d chars", length);
00151   setupWriteableISR(false);
00152   while(length)
00153   {
00154     m_outBuf.queue(*buf);
00155     buf++;
00156     length--;
00157     if(length && !space())
00158     {
00159       DBG("Waiting to write remaining %d chars", length);
00160       setupWriteableISR(true);
00161       ret = waitSpace(timeout);
00162       if(ret)
00163       {
00164         WARN("Error %d while waiting for space", ret);
00165         return ret;
00166       }
00167       setupWriteableISR(false);
00168     }
00169   }
00170   //If m_serial tx fifo is empty we need to manually tx a byte in order to trigger the interrupt
00171   if( m_outBuf.available() && m_serialTxFifoEmpty )
00172   {
00173     m_serialTxFifoEmpty = false;
00174     uint8_t c;
00175     m_outBuf.dequeue(&c);
00176     m_serial.putc((char)c);
00177   }
00178   setupWriteableISR(true);
00179   DBG("Write successful");
00180   return OK;
00181 }
00182 
00183 /*virtual*/ size_t IOSerialStream::space()
00184 {
00185   setupWriteableISR(false); //m_outBuf.available() is not reentrant
00186   size_t len = CIRCBUF_SIZE - m_outBuf.available();
00187   setupWriteableISR(true);
00188   return len;
00189 }
00190 
00191 /*virtual*/ int IOSerialStream::waitSpace(uint32_t timeout/*=osWaitForever*/) //Wait for space to be available
00192 {
00193   int ret;
00194   if(space()) //Is still space already left?
00195   {
00196     m_spaceSphre.wait(0); //Clear the queue as space is available
00197     return OK;
00198   }
00199 
00200   DBG("Waiting for data space %d ms (-1 is infinite)", timeout);
00201   ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort
00202   if(ret <= 0)
00203   {
00204     DBG("Timeout");
00205     return NET_TIMEOUT;
00206   }
00207   if(!space()) //Even if abort has been called, return that space is available
00208   {
00209     DBG("Aborted");
00210     return NET_INTERRUPTED;
00211   }
00212   m_spaceSphre.wait(0); //Clear the queue as space is available
00213   return OK;
00214 }
00215 
00216 /*virtual*/ int IOSerialStream::abortWrite() //Abort current writing (or waiting) operation
00217 {
00218   if( !space() ) //If there is space left, no need to abort
00219   {
00220     m_spaceSphre.release(); //Force exiting the waiting state
00221   }
00222   return OK;
00223 }
00224 
00225 void IOSerialStream::setupWriteableISR(bool en)
00226 {
00227   if(en)
00228   {
00229     UART_X->IER |= 1 << 1;
00230   }
00231   else
00232   {
00233     UART_X->IER &= ~(1 << 1);
00234   }
00235 }
00236 
00237 void IOSerialStream::writeable() //Callback from m_serial when new space is available
00238 {
00239   if(m_outBuf.isEmpty())
00240   {
00241     m_serialTxFifoEmpty = true;
00242   }
00243   else
00244   {
00245     while(m_serial.writeable() && !m_outBuf.isEmpty())
00246     {
00247       uint8_t c;
00248       m_outBuf.dequeue(&c);
00249       m_serial.putc((char)c);
00250     }
00251   }
00252   m_spaceSphre.release(); //Force exiting the waiting state
00253 }