![](/media/cache/profiles/aa7fd480bd75d86948e7f7aac11d2704.jpg.50x50_q85.jpg)
support library for C027 helper functions for Buffer Pipes, Buffered Serial Port (rtos capable) and GPS parsing. It includes modem APIs for USSD, SMS and Sockets.
Fork of C027_Support by
Pipe.h@13:e2446fcdc246, 2013-11-19 (annotated)
- Committer:
- mazgch
- Date:
- Tue Nov 19 08:34:51 2013 +0000
- Revision:
- 13:e2446fcdc246
- Parent:
- 9:e7a5959ffae1
- Child:
- 21:c4d64830bf02
stable version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 0:cb2d45baaca3 | 1 | #pragma once |
mazgch | 0:cb2d45baaca3 | 2 | |
mazgch | 0:cb2d45baaca3 | 3 | template <class T> |
mazgch | 0:cb2d45baaca3 | 4 | class Pipe |
mazgch | 0:cb2d45baaca3 | 5 | { |
mazgch | 0:cb2d45baaca3 | 6 | private: |
mazgch | 0:cb2d45baaca3 | 7 | inline int _inc(int i, int n = 1) |
mazgch | 0:cb2d45baaca3 | 8 | { |
mazgch | 0:cb2d45baaca3 | 9 | i += n; |
mazgch | 9:e7a5959ffae1 | 10 | if (i >= _s) |
mazgch | 9:e7a5959ffae1 | 11 | i -= _s; |
mazgch | 0:cb2d45baaca3 | 12 | return i; |
mazgch | 0:cb2d45baaca3 | 13 | } |
mazgch | 0:cb2d45baaca3 | 14 | public: |
mazgch | 9:e7a5959ffae1 | 15 | Pipe(int n, T* b = NULL) |
mazgch | 0:cb2d45baaca3 | 16 | { |
mazgch | 9:e7a5959ffae1 | 17 | _a = b ? NULL : new T[n]; |
mazgch | 9:e7a5959ffae1 | 18 | _r = 0; |
mazgch | 9:e7a5959ffae1 | 19 | _w = 0; |
mazgch | 9:e7a5959ffae1 | 20 | _b = b ? b : _a; |
mazgch | 9:e7a5959ffae1 | 21 | _s = n; |
mazgch | 0:cb2d45baaca3 | 22 | } |
mazgch | 13:e2446fcdc246 | 23 | virtual ~Pipe(void) |
mazgch | 0:cb2d45baaca3 | 24 | { |
mazgch | 9:e7a5959ffae1 | 25 | if (_a) |
mazgch | 9:e7a5959ffae1 | 26 | delete [] _a; |
mazgch | 0:cb2d45baaca3 | 27 | } |
mazgch | 0:cb2d45baaca3 | 28 | // writing thread |
mazgch | 0:cb2d45baaca3 | 29 | bool writeable(void) // = not full |
mazgch | 0:cb2d45baaca3 | 30 | { |
mazgch | 9:e7a5959ffae1 | 31 | return free() > 0; |
mazgch | 0:cb2d45baaca3 | 32 | } |
mazgch | 0:cb2d45baaca3 | 33 | int free(void) // number of elements that can be added |
mazgch | 0:cb2d45baaca3 | 34 | { |
mazgch | 9:e7a5959ffae1 | 35 | int s = _r - _w; |
mazgch | 9:e7a5959ffae1 | 36 | if (s <= 0) |
mazgch | 9:e7a5959ffae1 | 37 | s += _s; |
mazgch | 9:e7a5959ffae1 | 38 | return s - 1; |
mazgch | 0:cb2d45baaca3 | 39 | } |
mazgch | 13:e2446fcdc246 | 40 | T putc(T c) |
mazgch | 0:cb2d45baaca3 | 41 | { |
mazgch | 9:e7a5959ffae1 | 42 | int i = _w; |
mazgch | 0:cb2d45baaca3 | 43 | int j = i; |
mazgch | 0:cb2d45baaca3 | 44 | i = _inc(i); |
mazgch | 9:e7a5959ffae1 | 45 | while (i == _r) // = !writeable() |
mazgch | 9:e7a5959ffae1 | 46 | /*wait for space*/; |
mazgch | 9:e7a5959ffae1 | 47 | _b[j] = c; |
mazgch | 13:e2446fcdc246 | 48 | _w = i; |
mazgch | 13:e2446fcdc246 | 49 | return c; |
mazgch | 0:cb2d45baaca3 | 50 | } |
mazgch | 9:e7a5959ffae1 | 51 | int put(const T* p, int n, bool t = false) |
mazgch | 0:cb2d45baaca3 | 52 | { |
mazgch | 9:e7a5959ffae1 | 53 | int c = n; |
mazgch | 9:e7a5959ffae1 | 54 | while (c) |
mazgch | 0:cb2d45baaca3 | 55 | { |
mazgch | 9:e7a5959ffae1 | 56 | int f; |
mazgch | 9:e7a5959ffae1 | 57 | for (;;) // wait for space |
mazgch | 0:cb2d45baaca3 | 58 | { |
mazgch | 9:e7a5959ffae1 | 59 | f = free(); |
mazgch | 9:e7a5959ffae1 | 60 | if (f) break; // data avail |
mazgch | 9:e7a5959ffae1 | 61 | if (!t) return n - c; // no more space and not blocking |
mazgch | 0:cb2d45baaca3 | 62 | } |
mazgch | 9:e7a5959ffae1 | 63 | // check free space |
mazgch | 9:e7a5959ffae1 | 64 | if (c < f) f = c; |
mazgch | 9:e7a5959ffae1 | 65 | int w = _w; |
mazgch | 9:e7a5959ffae1 | 66 | int m = _s - w; |
mazgch | 9:e7a5959ffae1 | 67 | // check wrap |
mazgch | 9:e7a5959ffae1 | 68 | if (f > m) f = m; |
mazgch | 9:e7a5959ffae1 | 69 | memcpy(&_b[w], p, f); |
mazgch | 9:e7a5959ffae1 | 70 | _w = _inc(w, f); |
mazgch | 9:e7a5959ffae1 | 71 | c -= f; |
mazgch | 9:e7a5959ffae1 | 72 | p += f; |
mazgch | 8:2435cdff8015 | 73 | } |
mazgch | 9:e7a5959ffae1 | 74 | return n - c; |
mazgch | 0:cb2d45baaca3 | 75 | } |
mazgch | 0:cb2d45baaca3 | 76 | // reading thread |
mazgch | 0:cb2d45baaca3 | 77 | // -------------------------------------------------------- |
mazgch | 0:cb2d45baaca3 | 78 | //! check if there are any values available |
mazgch | 0:cb2d45baaca3 | 79 | bool readable(void) // = not empty |
mazgch | 0:cb2d45baaca3 | 80 | { |
mazgch | 9:e7a5959ffae1 | 81 | return (_r != _w); |
mazgch | 0:cb2d45baaca3 | 82 | } |
mazgch | 0:cb2d45baaca3 | 83 | //! get the number of values avialable in the buffer |
mazgch | 2:b6012cd91657 | 84 | virtual int size(void) |
mazgch | 0:cb2d45baaca3 | 85 | { |
mazgch | 9:e7a5959ffae1 | 86 | int s = _w - _r; |
mazgch | 9:e7a5959ffae1 | 87 | if (s < 0) |
mazgch | 9:e7a5959ffae1 | 88 | s += _s; |
mazgch | 9:e7a5959ffae1 | 89 | return s; |
mazgch | 0:cb2d45baaca3 | 90 | } |
mazgch | 0:cb2d45baaca3 | 91 | //! get a value from buffer (this function will block if no values available) |
mazgch | 0:cb2d45baaca3 | 92 | T getc(void) |
mazgch | 0:cb2d45baaca3 | 93 | { |
mazgch | 9:e7a5959ffae1 | 94 | int r = _r; |
mazgch | 9:e7a5959ffae1 | 95 | while (r == _w) // = !readable() |
mazgch | 9:e7a5959ffae1 | 96 | /*wait for data*/; |
mazgch | 9:e7a5959ffae1 | 97 | T t = _b[r]; |
mazgch | 9:e7a5959ffae1 | 98 | _r = _inc(r); |
mazgch | 0:cb2d45baaca3 | 99 | return t; |
mazgch | 0:cb2d45baaca3 | 100 | } |
mazgch | 9:e7a5959ffae1 | 101 | //! get values from buffer |
mazgch | 9:e7a5959ffae1 | 102 | virtual int get(T* p, int n, bool t = false) |
mazgch | 0:cb2d45baaca3 | 103 | { |
mazgch | 9:e7a5959ffae1 | 104 | int c = n; |
mazgch | 9:e7a5959ffae1 | 105 | while (c) |
mazgch | 0:cb2d45baaca3 | 106 | { |
mazgch | 9:e7a5959ffae1 | 107 | int f; |
mazgch | 9:e7a5959ffae1 | 108 | for (;;) // wait for data |
mazgch | 0:cb2d45baaca3 | 109 | { |
mazgch | 9:e7a5959ffae1 | 110 | f = size(); |
mazgch | 9:e7a5959ffae1 | 111 | if (f) break; // free space |
mazgch | 9:e7a5959ffae1 | 112 | if (!t) return n - c; // no space and not blocking |
mazgch | 0:cb2d45baaca3 | 113 | } |
mazgch | 9:e7a5959ffae1 | 114 | // check available data |
mazgch | 9:e7a5959ffae1 | 115 | if (c < f) f = c; |
mazgch | 9:e7a5959ffae1 | 116 | int r = _r; |
mazgch | 9:e7a5959ffae1 | 117 | int m = _s - r; |
mazgch | 9:e7a5959ffae1 | 118 | // check wrap |
mazgch | 9:e7a5959ffae1 | 119 | if (f > m) f = m; |
mazgch | 9:e7a5959ffae1 | 120 | memcpy(p, &_b[r], f); |
mazgch | 9:e7a5959ffae1 | 121 | _r = _inc(r, f); |
mazgch | 9:e7a5959ffae1 | 122 | c -= f; |
mazgch | 9:e7a5959ffae1 | 123 | p += f; |
mazgch | 0:cb2d45baaca3 | 124 | } |
mazgch | 9:e7a5959ffae1 | 125 | return n - c; |
mazgch | 0:cb2d45baaca3 | 126 | } |
mazgch | 9:e7a5959ffae1 | 127 | |
mazgch | 0:cb2d45baaca3 | 128 | // the following functions are useful if you like to inspect or parse the buffer |
mazgch | 9:e7a5959ffae1 | 129 | |
mazgch | 9:e7a5959ffae1 | 130 | //! reset the parsing index and return the number of available elments |
mazgch | 9:e7a5959ffae1 | 131 | virtual int start(void) |
mazgch | 9:e7a5959ffae1 | 132 | { |
mazgch | 9:e7a5959ffae1 | 133 | _o = _r; |
mazgch | 9:e7a5959ffae1 | 134 | return size(); |
mazgch | 9:e7a5959ffae1 | 135 | } |
mazgch | 9:e7a5959ffae1 | 136 | //! get the next element and increment |
mazgch | 9:e7a5959ffae1 | 137 | virtual T next(void) |
mazgch | 9:e7a5959ffae1 | 138 | { |
mazgch | 9:e7a5959ffae1 | 139 | int o = _o; |
mazgch | 9:e7a5959ffae1 | 140 | T t = _b[o]; |
mazgch | 9:e7a5959ffae1 | 141 | _o = _inc(o); |
mazgch | 9:e7a5959ffae1 | 142 | return t; |
mazgch | 9:e7a5959ffae1 | 143 | } |
mazgch | 9:e7a5959ffae1 | 144 | //! commit the index |
mazgch | 9:e7a5959ffae1 | 145 | virtual void done(void) |
mazgch | 9:e7a5959ffae1 | 146 | { |
mazgch | 9:e7a5959ffae1 | 147 | _r = _o; |
mazgch | 9:e7a5959ffae1 | 148 | } |
mazgch | 0:cb2d45baaca3 | 149 | |
mazgch | 0:cb2d45baaca3 | 150 | private: |
mazgch | 9:e7a5959ffae1 | 151 | T* _b; //!< buffer |
mazgch | 9:e7a5959ffae1 | 152 | T* _a; //!< allocated buffer |
mazgch | 9:e7a5959ffae1 | 153 | int _s; //!< size of buffer (s - 1) elements can be stored |
mazgch | 9:e7a5959ffae1 | 154 | volatile int _w; //!< write index |
mazgch | 9:e7a5959ffae1 | 155 | volatile int _r; //!< read index |
mazgch | 9:e7a5959ffae1 | 156 | int _o; //!< offest index used by parsing functions |
mazgch | 0:cb2d45baaca3 | 157 | }; |