[136] | 1 | /*
|
---|
| 2 | This file is part of the GSM3 communications library for Arduino
|
---|
| 3 | -- Multi-transport communications platform
|
---|
| 4 | -- Fully asynchronous
|
---|
| 5 | -- Includes code for the Arduino-Telefonica GSM/GPRS Shield V1
|
---|
| 6 | -- Voice calls
|
---|
| 7 | -- SMS
|
---|
| 8 | -- TCP/IP connections
|
---|
| 9 | -- HTTP basic clients
|
---|
| 10 |
|
---|
| 11 | This library has been developed by Telefónica Digital - PDI -
|
---|
| 12 | - Physical Internet Lab, as part as its collaboration with
|
---|
| 13 | Arduino and the Open Hardware Community.
|
---|
| 14 |
|
---|
| 15 | September-December 2012
|
---|
| 16 |
|
---|
| 17 | This library is free software; you can redistribute it and/or
|
---|
| 18 | modify it under the terms of the GNU Lesser General Public
|
---|
| 19 | License as published by the Free Software Foundation; either
|
---|
| 20 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 21 |
|
---|
| 22 | This library is distributed in the hope that it will be useful,
|
---|
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 25 | Lesser General Public License for more details.
|
---|
| 26 |
|
---|
| 27 | You should have received a copy of the GNU Lesser General Public
|
---|
| 28 | License along with this library; if not, write to the Free Software
|
---|
| 29 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 30 |
|
---|
| 31 | The latest version of this library can always be found at
|
---|
| 32 | https://github.com/BlueVia/Official-Arduino
|
---|
| 33 | */
|
---|
| 34 | #ifndef __GSM3_CIRCULARBUFFER__
|
---|
| 35 | #define __GSM3_CIRCULARBUFFER__
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | #include <inttypes.h>
|
---|
| 39 | #include <stddef.h>
|
---|
| 40 |
|
---|
| 41 | #ifndef byte
|
---|
| 42 | #define byte uint8_t
|
---|
| 43 | #endif
|
---|
| 44 |
|
---|
| 45 | // These values have to be interrelated
|
---|
| 46 | // To-Do: may we have just one? (BUFFERMASK)
|
---|
| 47 | #define __BUFFERSIZE__ 128
|
---|
| 48 | #define __BUFFERMASK__ 0x7F
|
---|
| 49 |
|
---|
| 50 | class GSM3CircularBufferManager
|
---|
| 51 | {
|
---|
| 52 | public:
|
---|
| 53 |
|
---|
| 54 | /** If there is spaceAvailable in the buffer, lets send a XON
|
---|
| 55 | */
|
---|
| 56 | virtual void spaceAvailable();
|
---|
| 57 | };
|
---|
| 58 |
|
---|
| 59 | class GSM3CircularBuffer
|
---|
| 60 | {
|
---|
| 61 | private:
|
---|
| 62 | // Buffer pointers.
|
---|
| 63 | // head=tail means buffer empty
|
---|
| 64 | // tail=head-1 means buffer full
|
---|
| 65 | // tail=head+1 means just one char (pointed by head)
|
---|
| 66 | // REMEMBER. head can be moved only by the main program
|
---|
| 67 | // REMEMBER. tail can be moved only by the other thread (interrupts)
|
---|
| 68 | // REMEMBER. head and tail can move only FORWARD
|
---|
| 69 | volatile byte head; // First written one
|
---|
| 70 | volatile byte tail; // Last written one.
|
---|
| 71 |
|
---|
| 72 | GSM3CircularBufferManager* cbm; // Circular buffer manager
|
---|
| 73 |
|
---|
| 74 | // The buffer
|
---|
| 75 | volatile byte theBuffer[__BUFFERSIZE__];
|
---|
| 76 |
|
---|
| 77 | /** Checks if a substring exists in the buffer
|
---|
| 78 | @param reference Substring
|
---|
| 79 | @param thishead Head
|
---|
| 80 | @param thistail Tail
|
---|
| 81 | @param from Initial byte position
|
---|
| 82 | @param to Final byte position
|
---|
| 83 | @return true if exists, in otherwise return false
|
---|
| 84 | */
|
---|
| 85 | bool locate(const char* reference, byte thishead, byte thistail, byte* from=0, byte* to=0);
|
---|
| 86 |
|
---|
| 87 | public:
|
---|
| 88 |
|
---|
| 89 | /** Constructor
|
---|
| 90 | @param mgr Circular buffer manager
|
---|
| 91 | */
|
---|
| 92 | GSM3CircularBuffer(GSM3CircularBufferManager* mgr=0);
|
---|
| 93 |
|
---|
| 94 | // TO-DO.Check if this formule runs too at the buffer limit
|
---|
| 95 |
|
---|
| 96 | /** Get available bytes in circular buffer
|
---|
| 97 | @return available bytes
|
---|
| 98 | */
|
---|
| 99 | inline byte availableBytes(){ return ((head-(tail+1))&__BUFFERMASK__);};
|
---|
| 100 |
|
---|
| 101 | /** Stored bytes in circular buffer
|
---|
| 102 | @return stored bytes
|
---|
| 103 | */
|
---|
| 104 | inline byte storedBytes(){ return ((tail-head)&__BUFFERMASK__);};
|
---|
| 105 |
|
---|
| 106 | /** Write a character in circular buffer
|
---|
| 107 | @param c Character
|
---|
| 108 | @return 1 if successful
|
---|
| 109 | */
|
---|
| 110 | int write(char c);
|
---|
| 111 |
|
---|
| 112 | /** Returns a character and moves the pointer
|
---|
| 113 | @return character
|
---|
| 114 | */
|
---|
| 115 | char read();
|
---|
| 116 |
|
---|
| 117 | /** Returns a character but does not move the pointer.
|
---|
| 118 | @param increment Increment
|
---|
| 119 | @return character
|
---|
| 120 | */
|
---|
| 121 | char peek(int increment);
|
---|
| 122 |
|
---|
| 123 | /** Returns a pointer to the head of the buffer
|
---|
| 124 | @return buffer with pointer in head
|
---|
| 125 | */
|
---|
| 126 | inline char* firstString(){return (char*)theBuffer+head;};
|
---|
| 127 |
|
---|
| 128 | /** Go forward one string
|
---|
| 129 | @return buffer with one string advance
|
---|
| 130 | */
|
---|
| 131 | char* nextString();
|
---|
| 132 |
|
---|
| 133 | /** Flush circular buffer
|
---|
| 134 | */
|
---|
| 135 | void flush();
|
---|
| 136 |
|
---|
| 137 | /** Get tail
|
---|
| 138 | @return tail
|
---|
| 139 | */
|
---|
| 140 | inline byte getTail(){return tail;};
|
---|
| 141 |
|
---|
| 142 | /** Get head
|
---|
| 143 | @return head
|
---|
| 144 | */
|
---|
| 145 | inline byte getHead(){return head;};
|
---|
| 146 |
|
---|
| 147 | // Only can be executed from the interrupt!
|
---|
| 148 | /** Delete circular buffer to the end
|
---|
| 149 | @param from Initial byte position
|
---|
| 150 | */
|
---|
| 151 | inline void deleteToTheEnd(byte from){tail=from;};
|
---|
| 152 |
|
---|
| 153 | /** Checks if a substring exists in the buffer
|
---|
| 154 | move=0, dont move, =1,put head at the beginning of the string, =2, put head at the end
|
---|
| 155 | @param reference
|
---|
| 156 | @return true if exists, in otherwise return false
|
---|
| 157 | */
|
---|
| 158 | bool locate(const char* reference);
|
---|
| 159 |
|
---|
| 160 | /** Locates reference. If found, moves head (or tail) to the beginning (or end)
|
---|
| 161 | @param reference
|
---|
| 162 | @param movetotheend
|
---|
| 163 | @param head
|
---|
| 164 | @return true if successful
|
---|
| 165 | */
|
---|
| 166 | bool chopUntil(const char* reference, bool movetotheend, bool head=true);
|
---|
| 167 |
|
---|
| 168 | /** Reads an integer from the head. Stops with first non blank, non number character
|
---|
| 169 | @return integer from the head
|
---|
| 170 | */
|
---|
| 171 | int readInt();
|
---|
| 172 |
|
---|
| 173 | // Caveat: copies the first bytes until buffer is full
|
---|
| 174 |
|
---|
| 175 | /** Extract a substring from circular buffer
|
---|
| 176 | @param from Initial byte position
|
---|
| 177 | @param to Final byte position
|
---|
| 178 | @param buffer Buffer for copy substring
|
---|
| 179 | @param bufsize Buffer size
|
---|
| 180 | @return true if successful, false if substring does not exists
|
---|
| 181 | */
|
---|
| 182 | bool extractSubstring(const char* from, const char* to, char* buffer, int bufsize);
|
---|
| 183 |
|
---|
| 184 | /** Retrieve all the contents of buffer from head to tail
|
---|
| 185 | @param buffer
|
---|
| 186 | @param bufsize
|
---|
| 187 | @param SizeWritten
|
---|
| 188 | @return true if successful
|
---|
| 189 | */
|
---|
| 190 | bool retrieveBuffer(char* buffer, int bufsize, int& SizeWritten);
|
---|
| 191 |
|
---|
| 192 | /** Debug function to print the buffer after receiving data from the modem.
|
---|
| 193 | */
|
---|
| 194 | void debugBuffer();
|
---|
| 195 |
|
---|
| 196 | /** Utility: dump character if printable, else, put in %x%
|
---|
| 197 | @param c Character
|
---|
| 198 | */
|
---|
| 199 | static void printCharDebug(uint8_t c);
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | };
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | #endif
|
---|