SingingCat 0
application
decode_esp_packet.c
Go to the documentation of this file.
1#include "decode_esp_packet.h"
2
30typedef struct espbuf {
31 int order; /* >1: is ready for consumption */
32 int size; /* size of packet (if valid packet in buf) */
33 byte buf[260];
35struct espbuf bufs[7];
36static byte escaped = 0;
41static int curorder;
42static struct espbuf *cur_out_buf; // buffer to be consumed...
43static struct espbuf *cur_in_buf; // buffer bytes from IRQ for main app
44static byte decoder_state = 0;
45static byte dodgy_packet_counter = 0;
46static byte debug = 0;
51 cur_in_buf = &bufs[0];
52 cur_out_buf = NULL;
53 memset(&bufs, 0, sizeof(bufs));
54 escaped = 0;
55 decoder_state = 0;
56 curorder = 1;
57}
58
59
60// use an empty buf or the oldest one
61static void find_best_buf() {
62 int i;
63 int ob = -1;
64 int oo = 0;
65
66 for (i = 0; i < (sizeof(bufs) / sizeof(bufs[0])); i++) {
67 if ((ob == -1) || ((bufs[i].order < oo))) {
68 ob = i;
69 oo = bufs[i].order;
70 }
71 if (bufs[i].order == 0) {
72 cur_in_buf = &bufs[i];
73 return;
74 }
75 }
76 if (ob != -1) {
77 cur_in_buf = &bufs[ob];
78 }
79}
80static void find_empty_buf() {
81 int i;
82
83 for (i = 0; i < (sizeof(bufs) / sizeof(bufs[0])); i++) {
84 if (bufs[i].order == 0) {
85 cur_in_buf = &bufs[i];
86 return;
87 }
88 }
89}
90
97int esp8266_add_byte(byte b) {
98 if (cur_in_buf == NULL) {
99 find_best_buf();
100 }
101 if (cur_in_buf == NULL) {
102 printf("No esp packet buf (all bufs in use)\r\n");
104 return 0;
105 }
106 // if no bytes in buf yet, we only accept PKT_START_BYTE
107 if (decoder_state == 0) {
108 if (b == PKT_START_BYTE) {
109 decoder_state = 1;
110 return 0;
111 }
112 return 0;
113 }
114
115 // got complete packet in buf. we return this until resetbuf() is called
116 if (decoder_state == 5) { // never called afaict
117 if ((!escaped) && (b == PKT_START_BYTE)) {
118 printf("Warning - PKT_START_BYTE received while buf contains a packet!\r\n");
119 printf("buf [bytesinbuf=%i]: \"%s\"\r\n", cur_in_buf->size, &cur_in_buf->buf);
120 cur_in_buf->size = 0;
121 }
122 return cur_in_buf->size;
123 }
124
125 // check if our buf overflows - if so reset all bufs
126 if (cur_in_buf->size >= sizeof(cur_in_buf->buf)) {
127 dodgy_packet_counter++;
128 if (debug) {
129 // bad: printf from IRQ. eeks.
130 printf("Warning - esp8266 buf overflow (%i,%i)\r\n", cur_in_buf->size, dodgy_packet_counter);
131 int i;
132 for (i = 0; i < cur_in_buf->size; i++) {
133 write_serial_char(USART_CONSOLE, cur_in_buf->buf[i]);
134 }
135 cur_in_buf->size = 0;
136 }
138 return 0;
139 }
140
141
142 if (escaped) {
143 cur_in_buf->buf[cur_in_buf->size++] = b;
144 escaped = 0;
145 return 0;
146 }
147 escaped = (b == PKT_ESC_BYTE);
148
149 // packets never end with PKT_ESC
150 if (escaped) {
151 decoder_state = 2;
152 return 0;
153 }
154
155 // not escaped.
156 if (b == PKT_END_BYTE) {
157 cur_in_buf->order = curorder++;
158 if (cur_out_buf == NULL) {
160 }
161 cur_in_buf = NULL;
162 find_empty_buf();
163 decoder_state = 0;
164 return cur_out_buf->size;
165 } else {
166 if (b == '{') {
167 // if we receive a '{' (which is not escaped), then it _MUST_ be start of a packet, so reset buf
168 cur_in_buf->size = 0;
169 return 0;
170 }
171 decoder_state = 2;
172 cur_in_buf->buf[cur_in_buf->size++] = b;
173 }
174 return 0;
175}
176
198 CNWDEBUG("Resetting buf\r\n");
199 if (cur_out_buf != NULL) {
200 cur_out_buf->size = 0;
201 cur_out_buf->order = 0;
202 }
203 // any other bufs ready for consumption?
204 int i;
205 struct espbuf *res = NULL;
206
207 for (i = 0; i < (sizeof(bufs) / sizeof(bufs[0])); i++) {
208 if (bufs[i].order != 0) {
209 if ((res == NULL) || (res->order > bufs[i].order)) {
210 res = &bufs[i];
211 }
212 }
213 }
214 cur_out_buf = res;
215}
216
223 if (cur_out_buf == NULL) {
224 return NULL;
225 }
226 return (byte *)&cur_out_buf->buf;
227}
228
235 return decoder_state > 0;
236}
242 if (cur_out_buf == NULL) {
243 return 0;
244 }
245 if (cur_out_buf->order == 0) {
246 return 0;
247 }
248 return cur_out_buf->size;
249}
250void esp8266_decoder_print_info() {
251 int i;
252
253 printf(" decoder.escaped : %i\r\n", escaped);
254 printf(" decoder.curorder : %i\r\n", curorder);
255 printf(" decoder.decoder_state: %i\r\n", decoder_state);
256 for (i = 0; i < (sizeof(bufs) / sizeof(bufs[0])); i++) {
257 printf(" #%i order=%i, size=%i\r\n", i, bufs[i].order, bufs[i].size);
258 }
259}
byte esp8266_is_in_packet()
returns 1 if currently parsing a packet
void decode_esp_packet_init()
int esp8266_packet_size()
returns 0 if no packet is in buf, otherwise returns size of packet in bytes
byte * esp8266_get_buf()
get the buffer
int esp8266_add_byte(byte b)
feed this with one byte at a time
struct espbuf _espbuf
void esp8266_reset_buf()
reset buf, e.g. after packet was processed