1#include "decode_esp_packet.h"
36static byte escaped = 0;
42static struct espbuf *cur_out_buf;
43static struct espbuf *cur_in_buf;
44static byte decoder_state = 0;
45static byte dodgy_packet_counter = 0;
51 cur_in_buf = &bufs[0];
53 memset(&bufs, 0,
sizeof(bufs));
61static void find_best_buf() {
66 for (i = 0; i < (
sizeof(bufs) /
sizeof(bufs[0])); i++) {
67 if ((ob == -1) || ((bufs[i].order < oo))) {
71 if (bufs[i].order == 0) {
72 cur_in_buf = &bufs[i];
77 cur_in_buf = &bufs[ob];
80static void find_empty_buf() {
83 for (i = 0; i < (
sizeof(bufs) /
sizeof(bufs[0])); i++) {
84 if (bufs[i].order == 0) {
85 cur_in_buf = &bufs[i];
98 if (cur_in_buf == NULL) {
101 if (cur_in_buf == NULL) {
102 printf(
"No esp packet buf (all bufs in use)\r\n");
107 if (decoder_state == 0) {
108 if (b == PKT_START_BYTE) {
116 if (decoder_state == 5) {
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;
122 return cur_in_buf->size;
126 if (cur_in_buf->size >=
sizeof(cur_in_buf->buf)) {
127 dodgy_packet_counter++;
130 printf(
"Warning - esp8266 buf overflow (%i,%i)\r\n", cur_in_buf->size, dodgy_packet_counter);
132 for (i = 0; i < cur_in_buf->size; i++) {
133 write_serial_char(USART_CONSOLE, cur_in_buf->buf[i]);
135 cur_in_buf->size = 0;
143 cur_in_buf->buf[cur_in_buf->size++] = b;
147 escaped = (b == PKT_ESC_BYTE);
156 if (b == PKT_END_BYTE) {
157 cur_in_buf->order = curorder++;
158 if (cur_out_buf == NULL) {
164 return cur_out_buf->size;
168 cur_in_buf->size = 0;
172 cur_in_buf->buf[cur_in_buf->size++] = b;
198 CNWDEBUG(
"Resetting buf\r\n");
199 if (cur_out_buf != NULL) {
200 cur_out_buf->size = 0;
201 cur_out_buf->order = 0;
205 struct espbuf *res = NULL;
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)) {
223 if (cur_out_buf == NULL) {
226 return (
byte *)&cur_out_buf->buf;
235 return decoder_state > 0;
242 if (cur_out_buf == NULL) {
245 if (cur_out_buf->order == 0) {
248 return cur_out_buf->size;
250void esp8266_decoder_print_info() {
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);
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
void esp8266_reset_buf()
reset buf, e.g. after packet was processed