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
utils/MTSCircularBuffer.h@152:9a2c7ed27744, 2014-09-02 (annotated)
- Committer:
- mfiore
- Date:
- Tue Sep 02 18:38:55 2014 +0000
- Revision:
- 152:9a2c7ed27744
- Parent:
- 141:571e0ef6c8dc
Wifi: fix compatibility break with old shields by checking for both old and new style responses to "show connection" command
Who changed what in which revision?
User | Revision | Line number | New 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 | #ifndef MTSCIRCULARBUFFER_H |
jengbrecht | 0:563b70517320 | 18 | #define MTSCIRCULARBUFFER_H |
jengbrecht | 0:563b70517320 | 19 | |
jengbrecht | 0:563b70517320 | 20 | #include "mbed.h" |
jengbrecht | 0:563b70517320 | 21 | #include "Vars.h" |
jengbrecht | 0:563b70517320 | 22 | |
mfiore | 39:6e94520a3217 | 23 | namespace mts { |
mfiore | 39:6e94520a3217 | 24 | |
jengbrecht | 0:563b70517320 | 25 | /** This class provides a circular byte buffer meant for temporary storage |
jengbrecht | 0:563b70517320 | 26 | * during IO transactions. It contains many of the common methods you |
jengbrecht | 0:563b70517320 | 27 | * would expect from a circular buffer like read, write, and various |
jengbrecht | 0:563b70517320 | 28 | * methods for checking the size or status. It should be noted that |
jengbrecht | 0:563b70517320 | 29 | * this class does not include any special code for thread safety like |
jengbrecht | 0:563b70517320 | 30 | * a lock. In most cases this is not problematic, but is something |
jengbrecht | 0:563b70517320 | 31 | * to be aware of. |
jengbrecht | 0:563b70517320 | 32 | */ |
jengbrecht | 0:563b70517320 | 33 | class MTSCircularBuffer |
jengbrecht | 0:563b70517320 | 34 | { |
jengbrecht | 0:563b70517320 | 35 | public: |
jengbrecht | 0:563b70517320 | 36 | /** Creates an MTSCircularBuffer object with the specified static size. |
jengbrecht | 0:563b70517320 | 37 | * |
jengbrecht | 0:563b70517320 | 38 | * @prarm bufferSize size of the buffer in bytes. |
jengbrecht | 0:563b70517320 | 39 | */ |
jengbrecht | 0:563b70517320 | 40 | MTSCircularBuffer(int bufferSize); |
jengbrecht | 0:563b70517320 | 41 | |
jengbrecht | 0:563b70517320 | 42 | /** Destructs an MTSCircularBuffer object and frees all related resources. |
jengbrecht | 0:563b70517320 | 43 | */ |
jengbrecht | 0:563b70517320 | 44 | ~MTSCircularBuffer(); |
jengbrecht | 0:563b70517320 | 45 | |
jengbrecht | 0:563b70517320 | 46 | /** This method enables bulk reads from the buffer. If more data is |
jengbrecht | 0:563b70517320 | 47 | * requested then available it simply returns all remaining data within the |
jengbrecht | 0:563b70517320 | 48 | * buffer. |
jengbrecht | 0:563b70517320 | 49 | * |
jengbrecht | 0:563b70517320 | 50 | * @param data the buffer where data read will be added to. |
jengbrecht | 0:563b70517320 | 51 | * @param length the amount of data in bytes to be read into the buffer. |
jengbrecht | 0:563b70517320 | 52 | * @returns the total number of bytes that were read. |
jengbrecht | 0:563b70517320 | 53 | */ |
jengbrecht | 0:563b70517320 | 54 | int read(char* data, int length); |
jengbrecht | 0:563b70517320 | 55 | |
jengbrecht | 0:563b70517320 | 56 | /** This method reads a single byte from the buffer. |
jengbrecht | 0:563b70517320 | 57 | * |
jengbrecht | 45:40745c2036cf | 58 | * @param data char where the read byte will be stored. |
jengbrecht | 45:40745c2036cf | 59 | * @returns 1 if byte is read or 0 if no bytes available. |
jengbrecht | 0:563b70517320 | 60 | */ |
sgodinez | 17:2d7c4ea7491b | 61 | int read(char& data); |
jengbrecht | 0:563b70517320 | 62 | |
jengbrecht | 0:563b70517320 | 63 | /** This method enables bulk writes to the buffer. If more data |
jengbrecht | 45:40745c2036cf | 64 | * is requested to be written then space available the method writes |
jengbrecht | 45:40745c2036cf | 65 | * as much data as possible and returns the actual amount written. |
jengbrecht | 0:563b70517320 | 66 | * |
jengbrecht | 0:563b70517320 | 67 | * @param data the byte array to be written. |
jengbrecht | 0:563b70517320 | 68 | * @param length the length of data to be written from the data paramter. |
jengbrecht | 45:40745c2036cf | 69 | * @returns the number of bytes written to the buffer, which is 0 if |
jengbrecht | 45:40745c2036cf | 70 | * the buffer is full. |
jengbrecht | 0:563b70517320 | 71 | */ |
sgodinez | 41:81d035fb0b6a | 72 | int write(const char* data, int length); |
jengbrecht | 0:563b70517320 | 73 | |
jengbrecht | 0:563b70517320 | 74 | /** This method writes a signle byte as a char to the buffer. |
jengbrecht | 0:563b70517320 | 75 | * |
jengbrecht | 0:563b70517320 | 76 | * @param data the byte to be written as a char. |
jengbrecht | 45:40745c2036cf | 77 | * @returns 1 if the byte was written or 0 if the buffer was full. |
jengbrecht | 0:563b70517320 | 78 | */ |
jengbrecht | 0:563b70517320 | 79 | int write(char data); |
jengbrecht | 0:563b70517320 | 80 | |
jengbrecht | 45:40745c2036cf | 81 | /** This method is used to setup a callback funtion when the buffer reaches |
jengbrecht | 45:40745c2036cf | 82 | * a certain threshold. The threshold condition is checked after every read |
jengbrecht | 45:40745c2036cf | 83 | * and write call is completed. The condition is made up of both a threshold |
jengbrecht | 45:40745c2036cf | 84 | * value and operator. An example that would trigger a callback is if the |
jengbrecht | 45:40745c2036cf | 85 | * threshold was 10, the operator GREATER, and there were 12 bytes added to an |
jengbrecht | 45:40745c2036cf | 86 | * empty buffer. |
jengbrecht | 45:40745c2036cf | 87 | * |
jengbrecht | 45:40745c2036cf | 88 | * @param tptr a pointer to the object to be called when the condition is met. |
jengbrecht | 45:40745c2036cf | 89 | * @param mptr a pointer to the function within the object to be called when |
jengbrecht | 45:40745c2036cf | 90 | * the condition is met. |
jengbrecht | 45:40745c2036cf | 91 | * @param threshold the value in bytes to be used as part of the condition. |
jengbrecht | 45:40745c2036cf | 92 | * @param op the operator to be used in conjunction with the threshold |
jengbrecht | 45:40745c2036cf | 93 | * as part of the condition. |
jengbrecht | 0:563b70517320 | 94 | */ |
jengbrecht | 0:563b70517320 | 95 | template<typename T> |
mfiore | 2:8d3ea0dfce39 | 96 | void attach(T *tptr, void( T::*mptr)(void), int threshold, Vars::RelationalOperator op) |
mfiore | 2:8d3ea0dfce39 | 97 | { |
mfiore | 2:8d3ea0dfce39 | 98 | _threshold = threshold; |
mfiore | 2:8d3ea0dfce39 | 99 | _op = op; |
mfiore | 2:8d3ea0dfce39 | 100 | notify.attach(tptr, mptr); |
mfiore | 2:8d3ea0dfce39 | 101 | } |
jengbrecht | 0:563b70517320 | 102 | |
jengbrecht | 45:40745c2036cf | 103 | /** This method is used to setup a callback funtion when the buffer reaches |
jengbrecht | 45:40745c2036cf | 104 | * a certain threshold. The threshold condition is checked after every read |
jengbrecht | 45:40745c2036cf | 105 | * and write call is completed. The condition is made up of both a threshold |
jengbrecht | 45:40745c2036cf | 106 | * value and operator. An example that would trigger a callback is if the |
jengbrecht | 45:40745c2036cf | 107 | * threshold was 10, the operator GREATER, and there were 12 bytes added to an |
jengbrecht | 45:40745c2036cf | 108 | * empty buffer. |
jengbrecht | 45:40745c2036cf | 109 | * |
jengbrecht | 45:40745c2036cf | 110 | * @param fptr a pointer to the static function to be called when the condition |
jengbrecht | 45:40745c2036cf | 111 | * is met. |
jengbrecht | 45:40745c2036cf | 112 | * @param threshold the value in bytes to be used as part of the condition. |
jengbrecht | 45:40745c2036cf | 113 | * @param op the operator to be used in conjunction with the threshold |
jengbrecht | 45:40745c2036cf | 114 | * as part of the condition. |
jengbrecht | 45:40745c2036cf | 115 | */ |
mfiore | 2:8d3ea0dfce39 | 116 | void attach(void(*fptr)(void), int threshold, Vars::RelationalOperator op) |
mfiore | 2:8d3ea0dfce39 | 117 | { |
mfiore | 2:8d3ea0dfce39 | 118 | _threshold = threshold; |
mfiore | 2:8d3ea0dfce39 | 119 | _op = op; |
mfiore | 2:8d3ea0dfce39 | 120 | notify.attach(fptr); |
mfiore | 2:8d3ea0dfce39 | 121 | } |
jengbrecht | 0:563b70517320 | 122 | |
sgodinez | 67:1003b410f781 | 123 | /** This method returns the size of the storage space currently allocated for |
sgodinez | 67:1003b410f781 | 124 | * the buffer. This value is equivalent to the one passed into the constructor. |
sgodinez | 67:1003b410f781 | 125 | * This value is equal or greater than the size() of the buffer. |
jengbrecht | 0:563b70517320 | 126 | * |
sgodinez | 67:1003b410f781 | 127 | * @returns the allocated size of the buffer in bytes. |
jengbrecht | 0:563b70517320 | 128 | */ |
sgodinez | 67:1003b410f781 | 129 | int capacity(); |
jengbrecht | 0:563b70517320 | 130 | |
jengbrecht | 0:563b70517320 | 131 | /** This method returns the amount of space left for writing. |
jengbrecht | 0:563b70517320 | 132 | * |
jengbrecht | 0:563b70517320 | 133 | * @returns numbers of unused bytes in buffer. |
jengbrecht | 0:563b70517320 | 134 | */ |
sgodinez | 67:1003b410f781 | 135 | int remaining(); |
jengbrecht | 0:563b70517320 | 136 | |
jengbrecht | 0:563b70517320 | 137 | /** This method returns the number of bytes available for reading. |
jengbrecht | 0:563b70517320 | 138 | * |
jengbrecht | 0:563b70517320 | 139 | * @returns number of bytes currently in buffer. |
jengbrecht | 0:563b70517320 | 140 | */ |
sgodinez | 67:1003b410f781 | 141 | int size(); |
jengbrecht | 0:563b70517320 | 142 | |
jengbrecht | 0:563b70517320 | 143 | /** This method returns whether the buffer is empty. |
jengbrecht | 0:563b70517320 | 144 | * |
jengbrecht | 0:563b70517320 | 145 | * @returns true if empty, otherwise false. |
jengbrecht | 0:563b70517320 | 146 | */ |
jengbrecht | 0:563b70517320 | 147 | bool isEmpty(); |
jengbrecht | 0:563b70517320 | 148 | |
jengbrecht | 0:563b70517320 | 149 | /** This method returns whether the buffer is full. |
jengbrecht | 0:563b70517320 | 150 | * |
jengbrecht | 0:563b70517320 | 151 | * @returns true if full, otherwise false. |
jengbrecht | 0:563b70517320 | 152 | */ |
jengbrecht | 0:563b70517320 | 153 | bool isFull(); |
jengbrecht | 0:563b70517320 | 154 | |
jengbrecht | 0:563b70517320 | 155 | /** This method clears the buffer. This is done through |
jengbrecht | 0:563b70517320 | 156 | * setting the internal read and write indexes to the same |
jengbrecht | 0:563b70517320 | 157 | * value and is therefore not an expensive operation. |
jengbrecht | 0:563b70517320 | 158 | */ |
jengbrecht | 0:563b70517320 | 159 | void clear(); |
jengbrecht | 0:563b70517320 | 160 | |
jengbrecht | 0:563b70517320 | 161 | |
jengbrecht | 0:563b70517320 | 162 | private: |
jengbrecht | 0:563b70517320 | 163 | int bufferSize; // total size of the buffer |
jengbrecht | 0:563b70517320 | 164 | char* buffer; // internal byte buffer as a character buffer |
jengbrecht | 0:563b70517320 | 165 | int readIndex; // read index for circular buffer |
jengbrecht | 0:563b70517320 | 166 | int writeIndex; // write index for circular buffer |
sgodinez | 64:0ca9c7123ffc | 167 | int bytes; // available data |
jengbrecht | 0:563b70517320 | 168 | FunctionPointer notify; // function pointer used for the internal callback notification |
mfiore | 2:8d3ea0dfce39 | 169 | int _threshold; // threshold for the notification |
mfiore | 2:8d3ea0dfce39 | 170 | Vars::RelationalOperator _op; // operator that determines the direction of the threshold |
jengbrecht | 0:563b70517320 | 171 | void checkThreshold(); // private function that checks thresholds and processes notifications |
jengbrecht | 0:563b70517320 | 172 | }; |
jengbrecht | 0:563b70517320 | 173 | |
mfiore | 39:6e94520a3217 | 174 | } |
mfiore | 39:6e94520a3217 | 175 | |
kranjan | 141:571e0ef6c8dc | 176 | #endif /* MTSCIRCULARBUFFER_H */ |