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