ParserStatus parse_frame(in_cmd_t *cmd, plchar_t *payload, const frame_t frame) { int in_pos = 0; int in_len = strlen(frame); if (frame[in_pos++] != START_CHAR) { return PARSER_INVALID_START; } mk_address_t addr = addr_decode(frame[in_pos++]); if (addr < 1 || addr > 3) { return PARSER_INVALID_ADDRESS; } in_cmd_t _cmd = MERGE_ADDR_CMD(addr, frame[in_pos++]); if (!in_command_exists(_cmd)) { return PARSER_INVALID_COMMAND; } const char *crc_chars = &frame[in_len - 3]; crc_t crc = calc_crc(frame, in_len - 3); if (!crc_ok(crc, crc_chars)) { return PARSER_INVALID_CRC; /* invalid crc */ } (void)mb64_decode(payload, in_len - 6, &frame[in_pos]); *cmd = _cmd; return PARSER_NO_ERROR; }
bool ID12LA::update() { bool updated = false; while (serial_->available()) { char c = serial_->read(); /* start of tag */ if (c == 0x02) { index_ = 0; continue; } /* ignore whitespace, stop */ if (c == '\r' || c == '\n' || c == 0x03) continue; if (index_ >= sizeof(buf_)) { Serial.println("ID12LA fail"); while (1); } buf_[index_++] = c; /* hit the end of the tag? check crc and copy tag */ if (index_ == sizeof(buf_) && crc_ok()) { memcpy(tag_, buf_, sizeof(tag_)); updated = true; } } return updated; }
static void hexdumpedid(FILE *f, struct v4l2_edid *e) { for (unsigned b = 0; b < e->blocks; b++) { unsigned char *buf = e->edid + 128 * b; for (unsigned i = 0; i < 128; i += 0x10) { fprintf(f, "%02x", buf[i]); for (unsigned j = 1; j < 0x10; j++) { fprintf(f, " %02x", buf[i + j]); } fprintf(f, "\n"); } if (!crc_ok(buf)) fprintf(f, "Block has a checksum error\n"); } }
static void carraydumpedid(FILE *f, struct v4l2_edid *e) { fprintf(f, "unsigned char edid[] = {\n"); for (unsigned b = 0; b < e->blocks; b++) { unsigned char *buf = e->edid + 128 * b; if (b) fprintf(f, "\n"); for (unsigned i = 0; i < 128; i += 8) { fprintf(f, "\t0x%02x,", buf[i]); for (unsigned j = 1; j < 8; j++) { fprintf(f, " 0x%02x,", buf[i + j]); } fprintf(f, "\n"); } if (!crc_ok(buf)) fprintf(f, "\t/* Block has a checksum error */\n"); } fprintf(f, "};\n"); }
bool Packet::is_valid() const { return length_valid() && crc_ok(); }