Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
GcStreamParser.cxx
1
35#include <string>
36
38#include "utils/gc_format.h"
39
41{
42 if (c == ':')
43 {
44 // Frame is starting here.
45 offset_ = 0;
46 return false;
47 }
48 if (c == ';')
49 {
50 if (offset_ < 0)
51 {
52 return false;
53 }
54 // Frame ends here.
55 cbuf_[offset_] = 0;
56 offset_ = -1;
57 return true;
58 }
59 if (offset_ >= static_cast<int>(sizeof(cbuf_) - 1))
60 {
61 // We overran the buffer, so this can't be a valid frame.
62 // Reset and look for sync byte again.
63 offset_ = -1;
64 return false;
65 }
66 if (offset_ >= 0)
67 {
68 cbuf_[offset_++] = c;
69 }
70 else
71 {
72 // Drop byte to the floor -- we're not in the middle of a
73 // packet.
74 }
75 return false;
76}
77
78void GcStreamParser::frame_buffer(std::string* payload) {
79 if (offset_ >= 0) {
80 payload->assign(cbuf_, offset_);
81 } else {
82 payload->assign(cbuf_);
83 }
84}
85
86bool GcStreamParser::parse_frame_to_output(struct can_frame* output_frame) {
87 int ret = gc_format_parse(cbuf_, output_frame);
88 return (ret == 0);
89}
bool consume_byte(char c)
Adds the next character from the source stream.
int offset_
offset of next byte in cbuf to write.
void frame_buffer(std::string *payload)
bool parse_frame_to_output(struct can_frame *output_frame)
Parses the current contents of the frame buffer to a can_frame struct.
char cbuf_[32]
Collects data from a partial GC packet.
int gc_format_parse(const char *buf, struct can_frame *can_frame)
Parses a GridConnect packet.
Definition gc_format.cxx:82