SingingCat 0
application
packetbuffer.c
Go to the documentation of this file.
1#include "main-header.h"
2
15struct cnw_packetbuf packetbuffer1; //CC1101
16
17
23static struct cnw_packetbuf *get_buf(const int num) {
24 if (num == PACKETBUFFER_CC1101) {
25 return &packetbuffer1;
26 } else {
27 fatal_error("Invalid packet buffer %i", num);
28 return NULL;
29 }
30}
31
37void packet_buf_init(const int num) {
38 void *buf = get_buf(num);
39
40 if (buf == NULL) {
41 return;
42 }
43 memset(buf, 0, sizeof(packetbuffer1));
44}
45
46
53 packet_buf_init(PACKETBUFFER_CC1101);
54}
55
56
64static void packet_buf_overflow(struct cnw_packetbuf *pkt, int num) {
65 printf("Overflow in buf %i, BytesInBuf: %i\r\n", num, pkt->bytesinbuf);
66 packet_buf_init(num);
67}
68
78int packet_put_byte(const int num, const byte b) {
79 struct cnw_packetbuf *pkt;
80
81 pkt = get_buf(num);
82 if (pkt == NULL) {
83 return -2;
84 }
85 if ((pkt->recvstarted != 0)
86 && ((mculib_get_seconds_since_boot() - pkt->recvstarted) > 5)
87 ) {
88 packet_buf_init(num);
89 }
90 if (pkt->bytesinbuf == PACKETBUF_SIZE) {
91 packet_buf_overflow(pkt, num);
92 return -1;
93 }
94 if (pkt->recvstarted == 0) {
95 pkt->recvstarted = mculib_get_seconds_since_boot();
96 }
97 pkt->buf[pkt->bytesinbuf] = b;
98 pkt->bytesinbuf++;
99 return 1;
100}
101
102
108void packet_mark_valid(const int num) {
109 struct cnw_packetbuf *pkt;
110
111 pkt = get_buf(num);
112 if (pkt == NULL) {
113 return;
114 }
115 pkt->isvalid = 1;
116}
117
125int packet_isvalid(const int num) {
126 struct cnw_packetbuf *pkt;
127
128 pkt = get_buf(num);
129 if (pkt == NULL) {
130 return -2;
131 }
132 if (pkt->isvalid) {
133 return 1;
134 }
135 return 0;
136}
137
143byte *packet_getbuf(const int num) {
144 struct cnw_packetbuf *pkt;
145
146 pkt = get_buf(num);
147 if (pkt == NULL) {
148 return NULL;
149 }
150 return (byte *)&pkt->buf;
151}
152
158int packet_getbytesinbuf(const int num) {
159 struct cnw_packetbuf *pkt;
160
161 pkt = get_buf(num);
162 if (pkt == NULL) {
163 return -2;
164 }
165 return pkt->bytesinbuf;
166}
void packet_init()
initialize (reset, clear) all buffers
Definition: packetbuffer.c:52
int packet_put_byte(const int num, const byte b)
add a byte to a buffer
Definition: packetbuffer.c:78
void packet_mark_valid(const int num)
mark this buffer as containing a valid packet
Definition: packetbuffer.c:108
int packet_getbytesinbuf(const int num)
count bytes in the buffer
Definition: packetbuffer.c:158
byte * packet_getbuf(const int num)
get pointer to the contents of the buffer
Definition: packetbuffer.c:143
int packet_isvalid(const int num)
initialize (reset, clear) a given buffer
Definition: packetbuffer.c:125
void packet_buf_init(const int num)
initialize (reset, clear) a given buffer
Definition: packetbuffer.c:37