22 for (uint32_t j = 0; j < 8; j++) {
23 uint32_t mask = (uint32_t) -(
checksum & 1U);
68 uint8_t header_seen{0}, frame_length{0};
69 uint32_t frame_crc{0};
83 void byte(uint8_t b) {
89 if (header_seen == 2) {
91 if (b == HEADER_BYTE) {
92 state = RECEIVING_ID_CONTROL;
95 if (b == STUFF_BYTE) {
100 state = SEARCHING_FOR_SOF;
105 if (b == HEADER_BYTE) {
112 case SEARCHING_FOR_SOF:
115 case RECEIVING_ID_CONTROL:
116 frame.
id = b & (uint8_t) 0x3fU;
120 state = RECEIVING_LENGTH;
122 case RECEIVING_LENGTH:
125 if (frame_length > 0) {
126 state = RECEIVING_PAYLOAD;
128 state = RECEIVING_CHECKSUM_3;
131 case RECEIVING_PAYLOAD:
134 if (--frame_length == 0) {
135 state = RECEIVING_CHECKSUM_3;
138 case RECEIVING_CHECKSUM_3:
139 frame_crc = ((uint32_t) b) << 24;
140 state = RECEIVING_CHECKSUM_2;
142 case RECEIVING_CHECKSUM_2:
143 frame_crc |= ((uint32_t) b) << 16;
144 state = RECEIVING_CHECKSUM_1;
146 case RECEIVING_CHECKSUM_1:
147 frame_crc |= ((uint32_t) b) << 8;
148 state = RECEIVING_CHECKSUM_0;
150 case RECEIVING_CHECKSUM_0:
154 queue.
trypush(std::move(frame));
160 state = SEARCHING_FOR_SOF;
165 state = SEARCHING_FOR_SOF;
174 while (!queue.
full() && !source.
empty()) {
175 for (
auto b: source.
pop()) {
179 return queue.
empty();
189 void *
operator new(
size_t sz,
In *where) {
212 uint8_t header_countdown = 2;
213 void stuff(uint8_t b) {
218 if (b == HEADER_BYTE) {
219 if (--header_countdown == 0) {
221 header_countdown = 2U;
224 header_countdown = 2U;
227 void nostuff(uint8_t b) {
241 nostuff(HEADER_BYTE);
242 nostuff(HEADER_BYTE);
243 nostuff(HEADER_BYTE);
246 for (
size_t i = 0; i < f.b.len; ++i) {
250 stuff((uint8_t) ((sum >> 24) & 0xff));
251 stuff((uint8_t) ((sum >> 16) & 0xff));
252 stuff((uint8_t) ((sum >> 8) & 0xff));
253 stuff((uint8_t) ((sum >> 0) & 0xff));
257 void *
operator new(
size_t sz,
Out *where) {
268 void poll(uint32_t, uint32_t) {
incoming Min stream
Definition min.h:63
Frame pop() override
get available Frame
Definition min.h:186
In(Source< Buffer< uint8_t > > &from)
unwrap given Buffer stream into Frame
Definition min.h:171
bool empty() override
check if Frame available
Definition min.h:173
outgoing Min stream
Definition min.h:208
bool full() override
check if sink is full
Definition min.h:234
Out(Sink< Buffer< uint8_t > > &to)
create Buffer stream wrapper
Definition min.h:232
void push(Frame &&f) override
push Frame through to underlying Buffer stream
Definition min.h:238
simple Buffer backed queue implementation
Definition queue.h:12
T pop() override
remove front of queue and return it
Definition queue.h:65
bool empty() override
check if queue is empty
Definition queue.h:83
bool full() override
return true if queue is full
Definition queue.h:87
dynamically allocated, but fixed-size buffer template
Definition buffer.h:18
Buffer & append(T b)
simple append single item
Definition buffer.h:44
size_t len
number of items stored in buffer
Definition buffer.h:21
simple CRC32 implementation
Definition min.h:12
void init()
reinitialize state
Definition min.h:16
uint32_t finalize()
get final CRC32 value
Definition min.h:28
void step(uint8_t byte)
calculate step with given byte
Definition min.h:20
uint32_t checksum
state
Definition min.h:14
Registry for dispatching frames to their registered destinations.
Definition frameregistry.h:61
void handle(Frame &&f)
handle given Frame and consume it
Definition frameregistry.h:108
pyWisp communication frame
Definition frameregistry.h:10
Buffer< uint8_t > b
buffer containing the Frame payload
Definition frameregistry.h:12
uint8_t id
buffer id: [0..63]
Definition frameregistry.h:14
full min-based connection wrapper
Definition min.h:34
void poll(uint32_t, uint32_t)
dispatch incoming Frames through registry
Definition min.h:268
Out out
outgoing Frame stream
Definition min.h:264
In in
incoming Frame stream
Definition min.h:262
FrameRegistry reg
Frame registry for this connection.
Definition min.h:266
generic object sink, i.e.
Definition streams.h:15
virtual bool full()=0
check if sink is full
void trypush(T &&t)
use this if you don't care if it's going to be successful. data gets discarded if sink is full.
Definition streams.h:28
generic object source, i.e.
Definition streams.h:40
virtual T pop()=0
pull object from source does not check for data. guard by using 'if (!empty) { ......
virtual bool empty()=0
check if source is empty