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

Committer:
kranjan
Date:
Sat Jan 04 05:28:45 2014 +0000
Revision:
141:571e0ef6c8dc
Parent:
110:8f3149c99112
Added licensing header to all files in the library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kranjan 141:571e0ef6c8dc 1 /* Universal Socket Modem Interface Library
kranjan 141:571e0ef6c8dc 2 * Copyright (c) 2013 Multi-Tech Systems
kranjan 141:571e0ef6c8dc 3 *
kranjan 141:571e0ef6c8dc 4 * Licensed under the Apache License, Version 2.0 (the "License");
kranjan 141:571e0ef6c8dc 5 * you may not use this file except in compliance with the License.
kranjan 141:571e0ef6c8dc 6 * You may obtain a copy of the License at
kranjan 141:571e0ef6c8dc 7 *
kranjan 141:571e0ef6c8dc 8 * http://www.apache.org/licenses/LICENSE-2.0
kranjan 141:571e0ef6c8dc 9 *
kranjan 141:571e0ef6c8dc 10 * Unless required by applicable law or agreed to in writing, software
kranjan 141:571e0ef6c8dc 11 * distributed under the License is distributed on an "AS IS" BASIS,
kranjan 141:571e0ef6c8dc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kranjan 141:571e0ef6c8dc 13 * See the License for the specific language governing permissions and
kranjan 141:571e0ef6c8dc 14 * limitations under the License.
kranjan 141:571e0ef6c8dc 15 */
kranjan 141:571e0ef6c8dc 16
jengbrecht 0:563b70517320 17 #include "MTSBufferedIO.h"
jengbrecht 0:563b70517320 18
mfiore 39:6e94520a3217 19 using namespace mts;
mfiore 39:6e94520a3217 20
jengbrecht 0:563b70517320 21 MTSBufferedIO::MTSBufferedIO(int txBufferSize, int rxBufferSize)
sgodinez 86:186bbf974c7c 22 : txBuffer(txBufferSize)
sgodinez 86:186bbf974c7c 23 , rxBuffer(rxBufferSize)
jengbrecht 0:563b70517320 24 {
sgodinez 86:186bbf974c7c 25
jengbrecht 0:563b70517320 26 }
jengbrecht 0:563b70517320 27
jengbrecht 0:563b70517320 28 MTSBufferedIO::~MTSBufferedIO()
jengbrecht 0:563b70517320 29 {
sgodinez 86:186bbf974c7c 30
jengbrecht 0:563b70517320 31 }
jengbrecht 0:563b70517320 32
sgodinez 68:c490e4a51778 33 int MTSBufferedIO::write(const char* data, int length, unsigned int timeoutMillis)
sgodinez 68:c490e4a51778 34 {
sgodinez 91:9439ad14d7f0 35 //Writes until empty or timeout is reached (different implementation planned once tx isr is working)
sgodinez 68:c490e4a51778 36 int bytesWritten = 0;
sgodinez 68:c490e4a51778 37 Timer tmr;
sgodinez 68:c490e4a51778 38 tmr.start();
sgodinez 110:8f3149c99112 39 length = MAX(0,length);
sgodinez 68:c490e4a51778 40 do {
sgodinez 91:9439ad14d7f0 41 int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten);
sgodinez 91:9439ad14d7f0 42 if(bytesWrittenSwBuffer > 0) {
sgodinez 91:9439ad14d7f0 43 handleWrite();
sgodinez 91:9439ad14d7f0 44 int bytesRemainingSwBuffer = txBuffer.size();
sgodinez 91:9439ad14d7f0 45 txBuffer.clear();
sgodinez 91:9439ad14d7f0 46 bytesWritten += (bytesWrittenSwBuffer - bytesRemainingSwBuffer);
sgodinez 91:9439ad14d7f0 47 }
sgodinez 68:c490e4a51778 48 } while(tmr.read_ms() <= timeoutMillis && bytesWritten < length);
sgodinez 68:c490e4a51778 49 return bytesWritten;
sgodinez 68:c490e4a51778 50 }
sgodinez 68:c490e4a51778 51
sgodinez 41:81d035fb0b6a 52 int MTSBufferedIO::write(const char* data, int length)
sgodinez 91:9439ad14d7f0 53 {
sgodinez 91:9439ad14d7f0 54 //Blocks until all bytes are written (different implementation planned once tx isr is working)
sgodinez 91:9439ad14d7f0 55 int bytesWritten = 0;
sgodinez 110:8f3149c99112 56 length = MAX(0,length);
sgodinez 91:9439ad14d7f0 57 do {
sgodinez 91:9439ad14d7f0 58 int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten);
sgodinez 91:9439ad14d7f0 59 handleWrite();
sgodinez 91:9439ad14d7f0 60 int bytesRemainingSwBuffer = txBuffer.size();
sgodinez 91:9439ad14d7f0 61 txBuffer.clear();
sgodinez 91:9439ad14d7f0 62 bytesWritten += bytesWrittenSwBuffer - bytesRemainingSwBuffer;
sgodinez 91:9439ad14d7f0 63 } while(bytesWritten < length);
sgodinez 91:9439ad14d7f0 64 return length;
jengbrecht 0:563b70517320 65 }
jengbrecht 0:563b70517320 66
sgodinez 68:c490e4a51778 67 int MTSBufferedIO::write(char data, unsigned int timeoutMillis)
sgodinez 68:c490e4a51778 68 {
sgodinez 68:c490e4a51778 69 return write(&data, 1, timeoutMillis);
sgodinez 68:c490e4a51778 70 }
sgodinez 68:c490e4a51778 71
jengbrecht 0:563b70517320 72 int MTSBufferedIO::write(char data)
jengbrecht 0:563b70517320 73 {
sgodinez 91:9439ad14d7f0 74 return write(&data, 1);
jengbrecht 0:563b70517320 75 }
jengbrecht 0:563b70517320 76
sgodinez 17:2d7c4ea7491b 77 int MTSBufferedIO::writeable() {
sgodinez 86:186bbf974c7c 78 return txBuffer.remaining();
sgodinez 17:2d7c4ea7491b 79 }
sgodinez 17:2d7c4ea7491b 80
sgodinez 68:c490e4a51778 81 int MTSBufferedIO::read(char* data, int length, unsigned int timeoutMillis)
sgodinez 68:c490e4a51778 82 {
sgodinez 68:c490e4a51778 83 int bytesRead = 0;
sgodinez 68:c490e4a51778 84 Timer tmr;
sgodinez 68:c490e4a51778 85 tmr.start();
sgodinez 110:8f3149c99112 86 length = MAX(0,length);
sgodinez 68:c490e4a51778 87 do {
sgodinez 91:9439ad14d7f0 88 bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead);
sgodinez 68:c490e4a51778 89 } while(tmr.read_ms() <= timeoutMillis && bytesRead < length);
sgodinez 68:c490e4a51778 90 return bytesRead;
sgodinez 68:c490e4a51778 91 }
sgodinez 68:c490e4a51778 92
jengbrecht 0:563b70517320 93 int MTSBufferedIO::read(char* data, int length)
jengbrecht 0:563b70517320 94 {
sgodinez 91:9439ad14d7f0 95 int bytesRead = 0;
sgodinez 110:8f3149c99112 96 length = MAX(0,length);
sgodinez 91:9439ad14d7f0 97 while(bytesRead < length) {
sgodinez 91:9439ad14d7f0 98 bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead);
sgodinez 91:9439ad14d7f0 99 }
sgodinez 91:9439ad14d7f0 100 return length;
jengbrecht 0:563b70517320 101 }
jengbrecht 0:563b70517320 102
sgodinez 68:c490e4a51778 103 int MTSBufferedIO::read(char& data, unsigned int timeoutMillis)
sgodinez 68:c490e4a51778 104 {
sgodinez 68:c490e4a51778 105 return read(&data, 1, timeoutMillis);
sgodinez 68:c490e4a51778 106 }
sgodinez 68:c490e4a51778 107
sgodinez 17:2d7c4ea7491b 108 int MTSBufferedIO::read(char& data)
jengbrecht 0:563b70517320 109 {
sgodinez 91:9439ad14d7f0 110 return rxBuffer.read(&data, 1);
sgodinez 17:2d7c4ea7491b 111 }
sgodinez 17:2d7c4ea7491b 112
sgodinez 17:2d7c4ea7491b 113 int MTSBufferedIO::readable() {
sgodinez 86:186bbf974c7c 114 return rxBuffer.size();
jengbrecht 0:563b70517320 115 }
jengbrecht 0:563b70517320 116
jengbrecht 0:563b70517320 117 bool MTSBufferedIO::txEmpty()
jengbrecht 0:563b70517320 118 {
sgodinez 86:186bbf974c7c 119 return txBuffer.isEmpty();
jengbrecht 0:563b70517320 120 }
jengbrecht 0:563b70517320 121
jengbrecht 0:563b70517320 122 bool MTSBufferedIO::rxEmpty()
jengbrecht 0:563b70517320 123 {
sgodinez 86:186bbf974c7c 124 return rxBuffer.isEmpty();
jengbrecht 0:563b70517320 125 }
jengbrecht 0:563b70517320 126
jengbrecht 0:563b70517320 127 bool MTSBufferedIO::txFull()
jengbrecht 0:563b70517320 128 {
sgodinez 86:186bbf974c7c 129 return txBuffer.isFull();
jengbrecht 0:563b70517320 130 }
jengbrecht 0:563b70517320 131
jengbrecht 0:563b70517320 132 bool MTSBufferedIO::rxFull()
jengbrecht 0:563b70517320 133 {
sgodinez 86:186bbf974c7c 134 return rxBuffer.isFull();
jengbrecht 0:563b70517320 135 }
jengbrecht 0:563b70517320 136
jengbrecht 0:563b70517320 137 void MTSBufferedIO::txClear()
jengbrecht 0:563b70517320 138 {
sgodinez 86:186bbf974c7c 139 txBuffer.clear();
jengbrecht 0:563b70517320 140 }
jengbrecht 0:563b70517320 141
jengbrecht 0:563b70517320 142 void MTSBufferedIO::rxClear()
jengbrecht 0:563b70517320 143 {
sgodinez 86:186bbf974c7c 144 rxBuffer.clear();
kranjan 141:571e0ef6c8dc 145 }