Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 141 lines (108 sloc) 2.695 kb
764d073 Import source from alpha 1 release (http://sparkfun.com/Code/wifly/WiFly...
follower authored
1
2 #include "ParsedStream.h"
3
4
5 void ParsedStream::storeByte(unsigned char c) {
6 int i = (_rx_buffer.head + 1) % RX_BUFFER_SIZE;
7
8 // if we should be storing the received character into the location
9 // just before the tail (meaning that the head would advance to the
10 // current location of the tail), we're about to overflow the buffer
11 // and so we don't write the character or advance the head.
12 if (i != _rx_buffer.tail) {
13 _rx_buffer.buffer[_rx_buffer.head] = c;
14 _rx_buffer.head = i;
15 }
16 }
17
c52eb6a @jcrouchley Updated to allow the specification of a different uart to SPISerial
authored
18 ParsedStream::ParsedStream()
19 {
764d073 Import source from alpha 1 release (http://sparkfun.com/Code/wifly/WiFly...
follower authored
20 /*
21 */
22 reset();
23 }
24
c52eb6a @jcrouchley Updated to allow the specification of a different uart to SPISerial
authored
25 void ParsedStream::begin(Stream* theUart)
26 {
27 _uart = theUart;
28 }
764d073 Import source from alpha 1 release (http://sparkfun.com/Code/wifly/WiFly...
follower authored
29
30 void ParsedStream::reset() {
31 /*
32 */
33 ring_buffer _rx_buffer = { { 0 }, 0, 0};
34 _closed = false;
35 bytes_matched = 0;
36 }
37
38
39 uint8_t ParsedStream::available(bool raw) {
40 uint8_t available_bytes;
41
42 available_bytes = (RX_BUFFER_SIZE + _rx_buffer.head - _rx_buffer.tail) % RX_BUFFER_SIZE;
43
44 if (!raw) {
45 if (available_bytes > bytes_matched) {
46 available_bytes -= bytes_matched;
47 } else {
48 available_bytes = 0;
49 }
50 }
51
52 return available_bytes;
53 }
54
55 uint8_t ParsedStream::available() {
56
57 // NOTE: This causes a read/buffer fill which isn't entirely
58 // consistent with how `available()` is normally
59 // handled.
60 // TODO: Put this buffer fill in the read section instead?
61
62 // TODO: Don't refill if we're almost full and don't have a partial
63 // match?
c52eb6a @jcrouchley Updated to allow the specification of a different uart to SPISerial
authored
64
65 while (!_closed && freeSpace() && _uart->available()) {
764d073 Import source from alpha 1 release (http://sparkfun.com/Code/wifly/WiFly...
follower authored
66 getByte();
67 }
68 return available(false);
69 }
70
71 bool ParsedStream::closed() {
72 return _closed && !available();
73 }
74
75 int ParsedStream::read(void) {
76
77 if (!available()) {
78 getByte();
79 }
80
81 if (!available()) {
82 return -1;
83 } else {
84 unsigned char c = _rx_buffer.buffer[_rx_buffer.tail];
85 _rx_buffer.tail = (_rx_buffer.tail + 1) % RX_BUFFER_SIZE;
86 return c;
87 }
88 }
89
cbca8af @jcrouchley Inherited Client from Stream instead of Print
authored
90 int ParsedStream::peek(void) {
91
92 if (!available()) {
93 getByte();
94 }
95
96 if (!available()) {
97 return -1;
98 } else {
99 unsigned char c = _rx_buffer.buffer[_rx_buffer.tail];
100 return c;
101 }
102 }
103
764d073 Import source from alpha 1 release (http://sparkfun.com/Code/wifly/WiFly...
follower authored
104
105
106 int ParsedStream::freeSpace() {
107 return RX_BUFFER_SIZE - available(true) - 1 /* The -1 fudge due to storeByte calculation*/;
108 }
109
110 void ParsedStream::getByte() {
111 int c;
112 if (_closed) {
113 return;
114 }
115
116 if (freeSpace() == 0) {
117 return;
118 }
119
120 // TODO: Tidy this...
c52eb6a @jcrouchley Updated to allow the specification of a different uart to SPISerial
authored
121 c = _uart->read();
764d073 Import source from alpha 1 release (http://sparkfun.com/Code/wifly/WiFly...
follower authored
122 if (c == -1) {
123 return;
124 }
125
126 if (c == MATCH_TOKEN[bytes_matched]) {
127 bytes_matched++;
128 if (bytes_matched == strlen(MATCH_TOKEN)) {
129 _closed = true;
130 }
131 } else if (c == MATCH_TOKEN[0]) {
132 // Handle e.g. case "**CLOS*"
133 bytes_matched = 1;
134 } else {
135 bytes_matched = 0;
136 }
137
138 storeByte(c);
139
140 }
Something went wrong with that request. Please try again.