MeterLogger
Data Structures | Typedefs | Functions
proto.h File Reference
#include <stdlib.h>
#include "typedef.h"
#include "ringbuf.h"
Include dependency graph for proto.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PROTO_PARSER
 

Typedefs

typedef void() PROTO_PARSE_CALLBACK()
 

Functions

I8 ICACHE_FLASH_ATTR PROTO_Init (PROTO_PARSER *parser, PROTO_PARSE_CALLBACK *completeCallback, U8 *buf, U16 bufSize)
 
I8 ICACHE_FLASH_ATTR PROTO_Parse (PROTO_PARSER *parser, U8 *buf, U16 len)
 
I16 ICACHE_FLASH_ATTR PROTO_Add (U8 *buf, const U8 *packet, I16 bufSize)
 
I16 ICACHE_FLASH_ATTR PROTO_AddRb (RINGBUF *rb, const U8 *packet, I16 len)
 
I8 ICACHE_FLASH_ATTR PROTO_ParseByte (PROTO_PARSER *parser, U8 value)
 
I16 ICACHE_FLASH_ATTR PROTO_ParseRb (RINGBUF *rb, U8 *bufOut, U16 *len, U16 maxBufLen)
 

Typedef Documentation

◆ PROTO_PARSE_CALLBACK

typedef void() PROTO_PARSE_CALLBACK()

Definition at line 14 of file proto.h.

Function Documentation

◆ PROTO_Add()

I16 ICACHE_FLASH_ATTR PROTO_Add ( U8 buf,
const U8 packet,
I16  bufSize 
)

Definition at line 70 of file proto.c.

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 }
unsigned short U16
Definition: typedef.h:12

◆ PROTO_AddRb()

I16 ICACHE_FLASH_ATTR PROTO_AddRb ( RINGBUF rb,
const U8 packet,
I16  len 
)

Definition at line 106 of file proto.c.

References RINGBUF_Put().

Referenced by QUEUE_Puts().

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 }
I16 ICACHE_FLASH_ATTR RINGBUF_Put(RINGBUF *r, U8 c)
put a character into ring buffer
Definition: ringbuf.c:32
unsigned short U16
Definition: typedef.h:12
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PROTO_Init()

I8 ICACHE_FLASH_ATTR PROTO_Init ( PROTO_PARSER parser,
PROTO_PARSE_CALLBACK completeCallback,
U8 buf,
U16  bufSize 
)

Definition at line 3 of file proto.c.

References PROTO_PARSER::buf, PROTO_PARSER::bufSize, PROTO_PARSER::callback, PROTO_PARSER::dataLen, and PROTO_PARSER::isEsc.

Referenced by PROTO_ParseRb().

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 }
PROTO_PARSE_CALLBACK * callback
Definition: proto.h:22
U16 dataLen
Definition: proto.h:19
U8 isEsc
Definition: proto.h:20
U8 * buf
Definition: proto.h:17
U16 bufSize
Definition: proto.h:18
Here is the caller graph for this function:

◆ PROTO_Parse()

I8 ICACHE_FLASH_ATTR PROTO_Parse ( PROTO_PARSER parser,
U8 buf,
U16  len 
)

Definition at line 49 of file proto.c.

References PROTO_ParseByte().

50 {
51  while(len--)
52  PROTO_ParseByte(parser, *buf++);
53 
54  return 0;
55 }
I8 ICACHE_FLASH_ATTR PROTO_ParseByte(PROTO_PARSER *parser, U8 value)
Definition: proto.c:13
Here is the call graph for this function:

◆ PROTO_ParseByte()

I8 ICACHE_FLASH_ATTR PROTO_ParseByte ( PROTO_PARSER parser,
U8  value 
)

Definition at line 13 of file proto.c.

References PROTO_PARSER::buf, PROTO_PARSER::bufSize, PROTO_PARSER::callback, PROTO_PARSER::dataLen, PROTO_PARSER::isBegin, PROTO_PARSER::isEsc, and NULL.

Referenced by PROTO_Parse(), and PROTO_ParseRb().

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 }
PROTO_PARSE_CALLBACK * callback
Definition: proto.h:22
U8 isBegin
Definition: proto.h:21
#define NULL
Definition: def.h:47
U16 dataLen
Definition: proto.h:19
U8 isEsc
Definition: proto.h:20
U8 * buf
Definition: proto.h:17
U16 bufSize
Definition: proto.h:18
Here is the caller graph for this function:

◆ PROTO_ParseRb()

I16 ICACHE_FLASH_ATTR PROTO_ParseRb ( RINGBUF rb,
U8 bufOut,
U16 len,
U16  maxBufLen 
)

Definition at line 56 of file proto.c.

References PROTO_PARSER::dataLen, NULL, PROTO_Init(), PROTO_ParseByte(), and RINGBUF_Get().

Referenced by QUEUE_Gets().

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 }
I16 ICACHE_FLASH_ATTR RINGBUF_Get(RINGBUF *r, U8 *c)
get a character from ring buffer
Definition: ringbuf.c:53
#define NULL
Definition: def.h:47
I8 ICACHE_FLASH_ATTR PROTO_Init(PROTO_PARSER *parser, PROTO_PARSE_CALLBACK *completeCallback, U8 *buf, U16 bufSize)
Definition: proto.c:3
U16 dataLen
Definition: proto.h:19
unsigned char U8
Definition: typedef.h:10
I8 ICACHE_FLASH_ATTR PROTO_ParseByte(PROTO_PARSER *parser, U8 value)
Definition: proto.c:13
Here is the call graph for this function:
Here is the caller graph for this function: