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:
sgodinez
Date:
Thu Dec 19 21:16:30 2013 +0000
Revision:
41:81d035fb0b6a
Parent:
17:2d7c4ea7491b
Child:
43:3cacf019ed7d
Added echo client test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jengbrecht 0:563b70517320 1 #ifndef MTSCIRCULARBUFFER_H
jengbrecht 0:563b70517320 2 #define MTSCIRCULARBUFFER_H
jengbrecht 0:563b70517320 3
jengbrecht 0:563b70517320 4 #include "mbed.h"
jengbrecht 0:563b70517320 5 #include "Vars.h"
jengbrecht 0:563b70517320 6
jengbrecht 0:563b70517320 7 /** This class provides a circular byte buffer meant for temporary storage
jengbrecht 0:563b70517320 8 * during IO transactions. It contains many of the common methods you
jengbrecht 0:563b70517320 9 * would expect from a circular buffer like read, write, and various
jengbrecht 0:563b70517320 10 * methods for checking the size or status. It should be noted that
jengbrecht 0:563b70517320 11 * this class does not include any special code for thread safety like
jengbrecht 0:563b70517320 12 * a lock. In most cases this is not problematic, but is something
jengbrecht 0:563b70517320 13 * to be aware of.
jengbrecht 0:563b70517320 14 */
jengbrecht 0:563b70517320 15 class MTSCircularBuffer
jengbrecht 0:563b70517320 16 {
jengbrecht 0:563b70517320 17 public:
jengbrecht 0:563b70517320 18 /** Creates an MTSCircularBuffer object with the specified static size.
jengbrecht 0:563b70517320 19 *
jengbrecht 0:563b70517320 20 * @prarm bufferSize size of the buffer in bytes.
jengbrecht 0:563b70517320 21 */
jengbrecht 0:563b70517320 22 MTSCircularBuffer(int bufferSize);
jengbrecht 0:563b70517320 23
jengbrecht 0:563b70517320 24 /** Destructs an MTSCircularBuffer object and frees all related resources.
jengbrecht 0:563b70517320 25 */
jengbrecht 0:563b70517320 26 ~MTSCircularBuffer();
jengbrecht 0:563b70517320 27
jengbrecht 0:563b70517320 28 /** This method enables bulk reads from the buffer. If more data is
jengbrecht 0:563b70517320 29 * requested then available it simply returns all remaining data within the
jengbrecht 0:563b70517320 30 * buffer.
jengbrecht 0:563b70517320 31 *
jengbrecht 0:563b70517320 32 * @param data the buffer where data read will be added to.
jengbrecht 0:563b70517320 33 * @param length the amount of data in bytes to be read into the buffer.
jengbrecht 0:563b70517320 34 * @returns the total number of bytes that were read.
jengbrecht 0:563b70517320 35 */
jengbrecht 0:563b70517320 36 int read(char* data, int length);
jengbrecht 0:563b70517320 37
jengbrecht 0:563b70517320 38 /** This method reads a single byte from the buffer.
jengbrecht 0:563b70517320 39 *
sgodinez 17:2d7c4ea7491b 40 * @returns length read or -1 if error.
jengbrecht 0:563b70517320 41 */
sgodinez 17:2d7c4ea7491b 42 int read(char& data);
jengbrecht 0:563b70517320 43
jengbrecht 0:563b70517320 44 /** This method enables bulk writes to the buffer. If more data
jengbrecht 0:563b70517320 45 * is requested to be written then space available no data is written
jengbrecht 0:563b70517320 46 * and an error value returned.
jengbrecht 0:563b70517320 47 *
jengbrecht 0:563b70517320 48 * @param data the byte array to be written.
jengbrecht 0:563b70517320 49 * @param length the length of data to be written from the data paramter.
mfiore 2:8d3ea0dfce39 50 * @returns the number of bytes written in the buffer or -1 if the write failed
jengbrecht 0:563b70517320 51 * because the buffer was full.
jengbrecht 0:563b70517320 52 */
sgodinez 41:81d035fb0b6a 53 int write(const char* data, int length);
jengbrecht 0:563b70517320 54
jengbrecht 0:563b70517320 55 /** This method writes a signle byte as a char to the buffer.
jengbrecht 0:563b70517320 56 *
jengbrecht 0:563b70517320 57 * @param data the byte to be written as a char.
mfiore 2:8d3ea0dfce39 58 * @returns the number of bytes written or -1 if the write failed
jengbrecht 0:563b70517320 59 * because the buffer was full.
jengbrecht 0:563b70517320 60 */
jengbrecht 0:563b70517320 61 int write(char data);
jengbrecht 0:563b70517320 62
jengbrecht 0:563b70517320 63 /**
jengbrecht 0:563b70517320 64 *
jengbrecht 0:563b70517320 65 */
jengbrecht 0:563b70517320 66 template<typename T>
mfiore 2:8d3ea0dfce39 67 void attach(T *tptr, void( T::*mptr)(void), int threshold, Vars::RelationalOperator op)
mfiore 2:8d3ea0dfce39 68 {
mfiore 2:8d3ea0dfce39 69 _threshold = threshold;
mfiore 2:8d3ea0dfce39 70 _op = op;
mfiore 2:8d3ea0dfce39 71 notify.attach(tptr, mptr);
mfiore 2:8d3ea0dfce39 72 }
jengbrecht 0:563b70517320 73
mfiore 2:8d3ea0dfce39 74 void attach(void(*fptr)(void), int threshold, Vars::RelationalOperator op)
mfiore 2:8d3ea0dfce39 75 {
mfiore 2:8d3ea0dfce39 76 _threshold = threshold;
mfiore 2:8d3ea0dfce39 77 _op = op;
mfiore 2:8d3ea0dfce39 78 notify.attach(fptr);
mfiore 2:8d3ea0dfce39 79 }
jengbrecht 0:563b70517320 80
jengbrecht 0:563b70517320 81 /** This method returns the static size of the buffer. This value
jengbrecht 0:563b70517320 82 * is equivalent to the one passed into the constructor.
jengbrecht 0:563b70517320 83 *
jengbrecht 0:563b70517320 84 * @returns the static size of the buffer in bytes.
jengbrecht 0:563b70517320 85 */
jengbrecht 0:563b70517320 86 int getSize();
jengbrecht 0:563b70517320 87
jengbrecht 0:563b70517320 88 /** This method returns the amount of space left for writing.
jengbrecht 0:563b70517320 89 *
jengbrecht 0:563b70517320 90 * @returns numbers of unused bytes in buffer.
jengbrecht 0:563b70517320 91 */
jengbrecht 0:563b70517320 92 int capacity();
jengbrecht 0:563b70517320 93
jengbrecht 0:563b70517320 94 /** This method returns the number of bytes available for reading.
jengbrecht 0:563b70517320 95 *
jengbrecht 0:563b70517320 96 * @returns number of bytes currently in buffer.
jengbrecht 0:563b70517320 97 */
jengbrecht 0:563b70517320 98 int available();
jengbrecht 0:563b70517320 99
jengbrecht 0:563b70517320 100 /** This method returns whether the buffer is empty.
jengbrecht 0:563b70517320 101 *
jengbrecht 0:563b70517320 102 * @returns true if empty, otherwise false.
jengbrecht 0:563b70517320 103 */
jengbrecht 0:563b70517320 104 bool isEmpty();
jengbrecht 0:563b70517320 105
jengbrecht 0:563b70517320 106 /** This method returns whether the buffer is full.
jengbrecht 0:563b70517320 107 *
jengbrecht 0:563b70517320 108 * @returns true if full, otherwise false.
jengbrecht 0:563b70517320 109 */
jengbrecht 0:563b70517320 110 bool isFull();
jengbrecht 0:563b70517320 111
jengbrecht 0:563b70517320 112 /** This method clears the buffer. This is done through
jengbrecht 0:563b70517320 113 * setting the internal read and write indexes to the same
jengbrecht 0:563b70517320 114 * value and is therefore not an expensive operation.
jengbrecht 0:563b70517320 115 */
jengbrecht 0:563b70517320 116 void clear();
jengbrecht 0:563b70517320 117
jengbrecht 0:563b70517320 118
jengbrecht 0:563b70517320 119 private:
jengbrecht 0:563b70517320 120 int bufferSize; // total size of the buffer
jengbrecht 0:563b70517320 121 char* buffer; // internal byte buffer as a character buffer
jengbrecht 0:563b70517320 122 int readIndex; // read index for circular buffer
jengbrecht 0:563b70517320 123 int writeIndex; // write index for circular buffer
jengbrecht 0:563b70517320 124 FunctionPointer notify; // function pointer used for the internal callback notification
mfiore 2:8d3ea0dfce39 125 int _threshold; // threshold for the notification
mfiore 2:8d3ea0dfce39 126 Vars::RelationalOperator _op; // operator that determines the direction of the threshold
jengbrecht 0:563b70517320 127 void checkThreshold(); // private function that checks thresholds and processes notifications
jengbrecht 0:563b70517320 128 };
jengbrecht 0:563b70517320 129
jengbrecht 0:563b70517320 130 #endif /* MTSCIRCULARBUFFER_H */