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 mbed official

/media/uploads/phsfan/sma01_003.png

Committer:
donatien
Date:
Tue Nov 13 12:00:34 2012 +0000
Revision:
62:6f42a974eea6
Parent:
61:0bcb8c5216d4
Child:
83:897a0de9d668
GCC-compliant

Who changed what in which revision?

UserRevisionLine numberNew 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
donatien 61:0bcb8c5216d4 20 #define __DEBUG__ 0
donatien 0:3b2f052c333b 21 #ifndef __MODULE__
donatien 0:3b2f052c333b 22 #define __MODULE__ "USBSerialStream.cpp"
donatien 0:3b2f052c333b 23 #endif
donatien 0:3b2f052c333b 24
donatien 0:3b2f052c333b 25 #include "core/fwk.h"
donatien 0:3b2f052c333b 26
donatien 62:6f42a974eea6 27 #include <cstring>
donatien 0:3b2f052c333b 28
donatien 0:3b2f052c333b 29 #include "USBSerialStream.h"
donatien 0:3b2f052c333b 30
donatien 0:3b2f052c333b 31
donatien 0:3b2f052c333b 32 USBSerialStream::USBSerialStream(IUSBHostSerial& serial) : m_serial(serial), m_serialTxFifoEmpty(true),
donatien 0:3b2f052c333b 33 m_availableSphre(1), m_spaceSphre(1), m_inBuf()
donatien 0:3b2f052c333b 34 {
donatien 0:3b2f052c333b 35 m_availableSphre.wait();
donatien 0:3b2f052c333b 36 m_spaceSphre.wait();
donatien 0:3b2f052c333b 37 //Attach interrupts
donatien 0:3b2f052c333b 38 m_serial.attach(this);
donatien 0:3b2f052c333b 39 }
donatien 0:3b2f052c333b 40
donatien 0:3b2f052c333b 41 /*virtual*/ USBSerialStream::~USBSerialStream()
donatien 0:3b2f052c333b 42 {
donatien 0:3b2f052c333b 43 m_serial.attach(NULL);
donatien 0:3b2f052c333b 44 }
donatien 0:3b2f052c333b 45
donatien 0:3b2f052c333b 46 //0 for non-blocking (returns immediately), -1 for infinite blocking
donatien 0:3b2f052c333b 47 /*virtual*/ int USBSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/)
donatien 0:3b2f052c333b 48 {
donatien 0:3b2f052c333b 49 DBG("Trying to read at most %d chars", maxLength);
donatien 0:3b2f052c333b 50 int ret = waitAvailable(timeout);
donatien 0:3b2f052c333b 51 if(ret)
donatien 0:3b2f052c333b 52 {
donatien 0:3b2f052c333b 53 WARN("Error %d while waiting for incoming data", ret);
donatien 0:3b2f052c333b 54 return ret;
donatien 0:3b2f052c333b 55 }
donatien 10:21a6f09d5631 56 int a = available(); //Prevent macro issues
donatien 10:21a6f09d5631 57 int readLen = MIN( a, maxLength );
donatien 0:3b2f052c333b 58 *pLength = readLen;
donatien 0:3b2f052c333b 59
donatien 0:3b2f052c333b 60 setupReadableISR(false);
donatien 0:3b2f052c333b 61 while(readLen--)
donatien 0:3b2f052c333b 62 {
donatien 0:3b2f052c333b 63 m_inBuf.dequeue(buf);
donatien 0:3b2f052c333b 64 buf++;
donatien 0:3b2f052c333b 65 }
donatien 0:3b2f052c333b 66 setupReadableISR(true);
donatien 0:3b2f052c333b 67 DBG("Read %d chars successfully", *pLength);
donatien 0:3b2f052c333b 68 return OK;
donatien 0:3b2f052c333b 69 }
donatien 0:3b2f052c333b 70
donatien 0:3b2f052c333b 71 /*virtual*/ size_t USBSerialStream::available()
donatien 0:3b2f052c333b 72 {
donatien 0:3b2f052c333b 73 setupReadableISR(false); //m_inBuf.available() is not reentrant
donatien 0:3b2f052c333b 74 size_t len = m_inBuf.available();
donatien 0:3b2f052c333b 75 setupReadableISR(true);
donatien 0:3b2f052c333b 76 return len;
donatien 0:3b2f052c333b 77 }
donatien 0:3b2f052c333b 78
donatien 0:3b2f052c333b 79 /*virtual*/ int USBSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available
donatien 0:3b2f052c333b 80 {
donatien 0:3b2f052c333b 81 int ret;
donatien 0:3b2f052c333b 82 if(available()) //Is data already available?
donatien 0:3b2f052c333b 83 {
donatien 10:21a6f09d5631 84 while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available
donatien 0:3b2f052c333b 85 return OK;
donatien 0:3b2f052c333b 86 }
donatien 0:3b2f052c333b 87
donatien 0:3b2f052c333b 88 DBG("Waiting for data availability %d ms (-1 is infinite)", timeout);
donatien 0:3b2f052c333b 89 ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort
donatien 0:3b2f052c333b 90 if(ret <= 0)
donatien 0:3b2f052c333b 91 {
donatien 0:3b2f052c333b 92 DBG("Timeout");
donatien 0:3b2f052c333b 93 return NET_TIMEOUT;
donatien 0:3b2f052c333b 94 }
donatien 0:3b2f052c333b 95 if(!m_inBuf.available()) //Even if abort has been called, return that data is available
donatien 0:3b2f052c333b 96 {
donatien 0:3b2f052c333b 97 DBG("Aborted");
donatien 0:3b2f052c333b 98 return NET_INTERRUPTED;
donatien 0:3b2f052c333b 99 }
donatien 0:3b2f052c333b 100 DBG("Finished waiting");
donatien 10:21a6f09d5631 101 while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available
donatien 0:3b2f052c333b 102 return OK;
donatien 0:3b2f052c333b 103 }
donatien 0:3b2f052c333b 104
donatien 0:3b2f052c333b 105 /*virtual*/ int USBSerialStream::abortRead() //Abort current reading (or waiting) operation
donatien 0:3b2f052c333b 106 {
donatien 0:3b2f052c333b 107 if( /*!available()*/true ) //If there is data pending, no need to abort
donatien 0:3b2f052c333b 108 {
donatien 0:3b2f052c333b 109 m_availableSphre.release(); //Force exiting the waiting state
donatien 0:3b2f052c333b 110 }
donatien 0:3b2f052c333b 111 else
donatien 0:3b2f052c333b 112 {
donatien 0:3b2f052c333b 113 DBG("Serial is readable"); ;
donatien 0:3b2f052c333b 114 }
donatien 0:3b2f052c333b 115 return OK;
donatien 0:3b2f052c333b 116 }
donatien 0:3b2f052c333b 117
donatien 0:3b2f052c333b 118 void USBSerialStream::setupReadableISR(bool en)
donatien 0:3b2f052c333b 119 {
donatien 0:3b2f052c333b 120 m_serial.setupIrq(en, IUSBHostSerial::RxIrq);
donatien 0:3b2f052c333b 121 }
donatien 0:3b2f052c333b 122
donatien 0:3b2f052c333b 123 void USBSerialStream::readable() //Callback from m_serial when new data is available
donatien 0:3b2f052c333b 124 {
donatien 0:3b2f052c333b 125 while(m_serial.readable())
donatien 0:3b2f052c333b 126 {
donatien 0:3b2f052c333b 127 m_inBuf.queue(m_serial.getc());
donatien 0:3b2f052c333b 128 }
donatien 0:3b2f052c333b 129 m_serial.readPacket(); //Start read of next packet
donatien 0:3b2f052c333b 130 m_availableSphre.release(); //Force exiting the waiting state
donatien 0:3b2f052c333b 131 }
donatien 0:3b2f052c333b 132
donatien 0:3b2f052c333b 133 //0 for non-blocking (returns immediately), -1 for infinite blocking
donatien 0:3b2f052c333b 134 /*virtual*/ int USBSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=-1*/)
donatien 0:3b2f052c333b 135 {
donatien 0:3b2f052c333b 136 DBG("Trying to write %d chars", length);
donatien 0:3b2f052c333b 137 do
donatien 0:3b2f052c333b 138 {
donatien 0:3b2f052c333b 139 int ret = waitSpace(timeout);
donatien 0:3b2f052c333b 140 if(ret)
donatien 0:3b2f052c333b 141 {
donatien 10:21a6f09d5631 142 WARN("Error %d while waiting for space", ret);
donatien 0:3b2f052c333b 143 return ret;
donatien 0:3b2f052c333b 144 }
donatien 10:21a6f09d5631 145 int s = space(); //Prevent macro issues
donatien 10:21a6f09d5631 146 int writeLen = MIN( s, length );
donatien 10:21a6f09d5631 147 DBG("Writing %d chars", writeLen);
donatien 0:3b2f052c333b 148 setupWriteableISR(false);
donatien 0:3b2f052c333b 149 while(writeLen)
donatien 0:3b2f052c333b 150 {
donatien 0:3b2f052c333b 151 m_outBuf.queue(*buf);
donatien 0:3b2f052c333b 152 buf++;
donatien 0:3b2f052c333b 153 length--;
donatien 0:3b2f052c333b 154 writeLen--;
donatien 0:3b2f052c333b 155 }
donatien 10:21a6f09d5631 156 //If m_serial tx fifo is empty we need to start the packet write
donatien 10:21a6f09d5631 157 if( m_outBuf.available() && m_serialTxFifoEmpty )
donatien 10:21a6f09d5631 158 {
donatien 10:21a6f09d5631 159 writeable();
donatien 10:21a6f09d5631 160 }
donatien 0:3b2f052c333b 161 setupWriteableISR(true);
donatien 0:3b2f052c333b 162 } while(length);
donatien 10:21a6f09d5631 163
donatien 0:3b2f052c333b 164 DBG("Write successful");
donatien 0:3b2f052c333b 165 return OK;
donatien 0:3b2f052c333b 166 }
donatien 0:3b2f052c333b 167
donatien 0:3b2f052c333b 168 /*virtual*/ size_t USBSerialStream::space()
donatien 0:3b2f052c333b 169 {
donatien 0:3b2f052c333b 170 setupWriteableISR(false); //m_outBuf.available() is not reentrant
donatien 0:3b2f052c333b 171 size_t len = CIRCBUF_SIZE - m_outBuf.available();
donatien 0:3b2f052c333b 172 setupWriteableISR(true);
donatien 0:3b2f052c333b 173 return len;
donatien 0:3b2f052c333b 174 }
donatien 0:3b2f052c333b 175
donatien 0:3b2f052c333b 176 /*virtual*/ int USBSerialStream::waitSpace(uint32_t timeout/*=-1*/) //Wait for space to be available
donatien 0:3b2f052c333b 177 {
donatien 0:3b2f052c333b 178 int ret;
donatien 0:3b2f052c333b 179 if(space()) //Is still space already left?
donatien 0:3b2f052c333b 180 {
donatien 10:21a6f09d5631 181 while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available
donatien 0:3b2f052c333b 182 return OK;
donatien 0:3b2f052c333b 183 }
donatien 0:3b2f052c333b 184
donatien 0:3b2f052c333b 185 DBG("Waiting for data space %d ms (-1 is infinite)", timeout);
donatien 0:3b2f052c333b 186 ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort
donatien 0:3b2f052c333b 187 if(ret <= 0)
donatien 0:3b2f052c333b 188 {
donatien 0:3b2f052c333b 189 DBG("Timeout");
donatien 0:3b2f052c333b 190 return NET_TIMEOUT;
donatien 0:3b2f052c333b 191 }
donatien 0:3b2f052c333b 192 if(!space()) //Even if abort has been called, return that space is available
donatien 0:3b2f052c333b 193 {
donatien 0:3b2f052c333b 194 DBG("Aborted");
donatien 0:3b2f052c333b 195 return NET_INTERRUPTED;
donatien 0:3b2f052c333b 196 }
donatien 10:21a6f09d5631 197 while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available
donatien 0:3b2f052c333b 198 return OK;
donatien 0:3b2f052c333b 199 }
donatien 0:3b2f052c333b 200
donatien 0:3b2f052c333b 201 /*virtual*/ int USBSerialStream::abortWrite() //Abort current writing (or waiting) operation
donatien 0:3b2f052c333b 202 {
donatien 0:3b2f052c333b 203 if( !space() ) //If there is space left, no need to abort
donatien 0:3b2f052c333b 204 {
donatien 0:3b2f052c333b 205 m_spaceSphre.release(); //Force exiting the waiting state
donatien 0:3b2f052c333b 206 }
donatien 0:3b2f052c333b 207 return OK;
donatien 0:3b2f052c333b 208 }
donatien 0:3b2f052c333b 209
donatien 0:3b2f052c333b 210 void USBSerialStream::setupWriteableISR(bool en)
donatien 0:3b2f052c333b 211 {
donatien 0:3b2f052c333b 212 m_serial.setupIrq(en, IUSBHostSerial::TxIrq);
donatien 0:3b2f052c333b 213 }
donatien 0:3b2f052c333b 214
donatien 0:3b2f052c333b 215 void USBSerialStream::writeable() //Callback from m_serial when new space is available
donatien 0:3b2f052c333b 216 {
donatien 0:3b2f052c333b 217 if(m_outBuf.isEmpty())
donatien 0:3b2f052c333b 218 {
donatien 0:3b2f052c333b 219 m_serialTxFifoEmpty = true;
donatien 0:3b2f052c333b 220 }
donatien 0:3b2f052c333b 221 else
donatien 0:3b2f052c333b 222 {
donatien 0:3b2f052c333b 223 m_serialTxFifoEmpty = false;
donatien 0:3b2f052c333b 224 while(m_serial.writeable() && !m_outBuf.isEmpty())
donatien 0:3b2f052c333b 225 {
donatien 0:3b2f052c333b 226 uint8_t c;
donatien 0:3b2f052c333b 227 m_outBuf.dequeue(&c);
donatien 0:3b2f052c333b 228 m_serial.putc((char)c);
donatien 0:3b2f052c333b 229 }
donatien 0:3b2f052c333b 230 m_serial.writePacket(); //Start packet write
donatien 0:3b2f052c333b 231 }
donatien 10:21a6f09d5631 232 if(!m_outBuf.isFull())
donatien 10:21a6f09d5631 233 {
donatien 10:21a6f09d5631 234 m_spaceSphre.release(); //Force exiting the waiting state
donatien 10:21a6f09d5631 235 }
donatien 0:3b2f052c333b 236 }