A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
MTSBufferedIO.cpp
00001 /* Universal Socket Modem Interface Library 00002 * Copyright (c) 2013 Multi-Tech Systems 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "MTSBufferedIO.h" 00018 00019 using namespace mts; 00020 00021 MTSBufferedIO::MTSBufferedIO(int txBufferSize, int rxBufferSize) 00022 : txBuffer(txBufferSize) 00023 , rxBuffer(rxBufferSize) 00024 { 00025 00026 } 00027 00028 MTSBufferedIO::~MTSBufferedIO() 00029 { 00030 00031 } 00032 00033 int MTSBufferedIO::write(const char* data, int length, unsigned int timeoutMillis) 00034 { 00035 //Writes until empty or timeout is reached (different implementation planned once tx isr is working) 00036 int bytesWritten = 0; 00037 Timer tmr; 00038 tmr.start(); 00039 length = MAX(0,length); 00040 do { 00041 int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten); 00042 if(bytesWrittenSwBuffer > 0) { 00043 handleWrite(); 00044 int bytesRemainingSwBuffer = txBuffer.size(); 00045 txBuffer.clear(); 00046 bytesWritten += (bytesWrittenSwBuffer - bytesRemainingSwBuffer); 00047 } 00048 } while(tmr.read_ms() <= timeoutMillis && bytesWritten < length); 00049 return bytesWritten; 00050 } 00051 00052 int MTSBufferedIO::write(const char* data, int length) 00053 { 00054 //Blocks until all bytes are written (different implementation planned once tx isr is working) 00055 int bytesWritten = 0; 00056 length = MAX(0,length); 00057 do { 00058 int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten); 00059 handleWrite(); 00060 int bytesRemainingSwBuffer = txBuffer.size(); 00061 txBuffer.clear(); 00062 bytesWritten += bytesWrittenSwBuffer - bytesRemainingSwBuffer; 00063 } while(bytesWritten < length); 00064 return length; 00065 } 00066 00067 int MTSBufferedIO::write(char data, unsigned int timeoutMillis) 00068 { 00069 return write(&data, 1, timeoutMillis); 00070 } 00071 00072 int MTSBufferedIO::write(char data) 00073 { 00074 return write(&data, 1); 00075 } 00076 00077 int MTSBufferedIO::writeable() { 00078 return txBuffer.remaining(); 00079 } 00080 00081 int MTSBufferedIO::read(char* data, int length, unsigned int timeoutMillis) 00082 { 00083 int bytesRead = 0; 00084 Timer tmr; 00085 tmr.start(); 00086 length = MAX(0,length); 00087 do { 00088 bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead); 00089 } while(tmr.read_ms() <= timeoutMillis && bytesRead < length); 00090 return bytesRead; 00091 } 00092 00093 int MTSBufferedIO::read(char* data, int length) 00094 { 00095 int bytesRead = 0; 00096 length = MAX(0,length); 00097 while(bytesRead < length) { 00098 bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead); 00099 } 00100 return length; 00101 } 00102 00103 int MTSBufferedIO::read(char& data, unsigned int timeoutMillis) 00104 { 00105 return read(&data, 1, timeoutMillis); 00106 } 00107 00108 int MTSBufferedIO::read(char& data) 00109 { 00110 return rxBuffer.read(&data, 1); 00111 } 00112 00113 int MTSBufferedIO::readable() { 00114 return rxBuffer.size(); 00115 } 00116 00117 bool MTSBufferedIO::txEmpty() 00118 { 00119 return txBuffer.isEmpty(); 00120 } 00121 00122 bool MTSBufferedIO::rxEmpty() 00123 { 00124 return rxBuffer.isEmpty(); 00125 } 00126 00127 bool MTSBufferedIO::txFull() 00128 { 00129 return txBuffer.isFull(); 00130 } 00131 00132 bool MTSBufferedIO::rxFull() 00133 { 00134 return rxBuffer.isFull(); 00135 } 00136 00137 void MTSBufferedIO::txClear() 00138 { 00139 txBuffer.clear(); 00140 } 00141 00142 void MTSBufferedIO::rxClear() 00143 { 00144 rxBuffer.clear(); 00145 }
Generated on Tue Jul 12 2022 21:46:23 by 1.7.2