MeterLogger
proto.c
Go to the documentation of this file.
1 #include "proto.h"
2 #include "ringbuf.h"
3 I8 ICACHE_FLASH_ATTR PROTO_Init(PROTO_PARSER *parser, PROTO_PARSE_CALLBACK *completeCallback, U8 *buf, U16 bufSize)
4 {
5  parser->buf = buf;
6  parser->bufSize = bufSize;
7  parser->dataLen = 0;
8  parser->callback = completeCallback;
9  parser->isEsc = 0;
10  return 0;
11 }
12 
14 {
15  switch(value){
16  case 0x7D:
17  parser->isEsc = 1;
18  break;
19 
20  case 0x7E:
21  parser->dataLen = 0;
22  parser->isEsc = 0;
23  parser->isBegin = 1;
24  break;
25 
26  case 0x7F:
27  if (parser->callback != NULL)
28  parser->callback();
29  parser->isBegin = 0;
30  return 0;
31  break;
32 
33  default:
34  if(parser->isBegin == 0) break;
35 
36  if(parser->isEsc){
37  value ^= 0x20;
38  parser->isEsc = 0;
39  }
40 
41  if(parser->dataLen < parser->bufSize)
42  parser->buf[parser->dataLen++] = value;
43 
44  break;
45  }
46  return -1;
47 }
48 
50 {
51  while(len--)
52  PROTO_ParseByte(parser, *buf++);
53 
54  return 0;
55 }
56 I16 ICACHE_FLASH_ATTR PROTO_ParseRb(RINGBUF* rb, U8 *bufOut, U16* len, U16 maxBufLen)
57 {
58  U8 c;
59 
60  PROTO_PARSER proto;
61  PROTO_Init(&proto, NULL, bufOut, maxBufLen);
62  while(RINGBUF_Get(rb, &c) == 0){
63  if(PROTO_ParseByte(&proto, c) == 0){
64  *len = proto.dataLen;
65  return 0;
66  }
67  }
68  return -1;
69 }
70 I16 ICACHE_FLASH_ATTR PROTO_Add(U8 *buf, const U8 *packet, I16 bufSize)
71 {
72  U16 i = 2;
73  U16 len = *(U16*) packet;
74 
75  if (bufSize < 1) return -1;
76 
77  *buf++ = 0x7E;
78  bufSize--;
79 
80  while (len--) {
81  switch (*packet) {
82  case 0x7D:
83  case 0x7E:
84  case 0x7F:
85  if (bufSize < 2) return -1;
86  *buf++ = 0x7D;
87  *buf++ = *packet++ ^ 0x20;
88  i += 2;
89  bufSize -= 2;
90  break;
91  default:
92  if (bufSize < 1) return -1;
93  *buf++ = *packet++;
94  i++;
95  bufSize--;
96  break;
97  }
98  }
99 
100  if (bufSize < 1) return -1;
101  *buf++ = 0x7F;
102 
103  return i;
104 }
105 
106 I16 ICACHE_FLASH_ATTR PROTO_AddRb(RINGBUF *rb, const U8 *packet, I16 len)
107 {
108  U16 i = 2;
109  if(RINGBUF_Put(rb, 0x7E) == -1) return -1;
110  while (len--) {
111  switch (*packet) {
112  case 0x7D:
113  case 0x7E:
114  case 0x7F:
115  if(RINGBUF_Put(rb, 0x7D) == -1) return -1;
116  if(RINGBUF_Put(rb, *packet++ ^ 0x20) == -1) return -1;
117  i += 2;
118  break;
119  default:
120  if(RINGBUF_Put(rb, *packet++) == -1) return -1;
121  i++;
122  break;
123  }
124  }
125  if(RINGBUF_Put(rb, 0x7F) == -1) return -1;
126 
127  return i;
128 }
129 
I16 ICACHE_FLASH_ATTR RINGBUF_Get(RINGBUF *r, U8 *c)
get a character from ring buffer
Definition: ringbuf.c:53
PROTO_PARSE_CALLBACK * callback
Definition: proto.h:22
I16 ICACHE_FLASH_ATTR RINGBUF_Put(RINGBUF *r, U8 c)
put a character into ring buffer
Definition: ringbuf.c:32
Definition: ringbuf.h:7
U8 isBegin
Definition: proto.h:21
#define NULL
Definition: def.h:47
short I16
Definition: typedef.h:11
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
I16 ICACHE_FLASH_ATTR PROTO_Add(U8 *buf, const U8 *packet, I16 bufSize)
Definition: proto.c:70
unsigned short U16
Definition: typedef.h:12
I8 ICACHE_FLASH_ATTR PROTO_Init(PROTO_PARSER *parser, PROTO_PARSE_CALLBACK *completeCallback, U8 *buf, U16 bufSize)
Definition: proto.c:3
char I8
Definition: typedef.h:9
I8 ICACHE_FLASH_ATTR PROTO_Parse(PROTO_PARSER *parser, U8 *buf, U16 len)
Definition: proto.c:49
I16 ICACHE_FLASH_ATTR PROTO_AddRb(RINGBUF *rb, const U8 *packet, I16 len)
Definition: proto.c:106
U16 dataLen
Definition: proto.h:19
unsigned char U8
Definition: typedef.h:10
void() PROTO_PARSE_CALLBACK()
Definition: proto.h:14
U8 isEsc
Definition: proto.h:20
U8 * buf
Definition: proto.h:17
U16 bufSize
Definition: proto.h:18
I8 ICACHE_FLASH_ATTR PROTO_ParseByte(PROTO_PARSER *parser, U8 value)
Definition: proto.c:13
I16 ICACHE_FLASH_ATTR PROTO_ParseRb(RINGBUF *rb, U8 *bufOut, U16 *len, U16 maxBufLen)
Definition: proto.c:56