mirror of
https://github.com/spice2x/spice2x.github.io.git
synced 2026-08-02 23:00:42 -07:00
Initial commit for GitHub migration based on spice2x-25-03-03
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
#include "message.h"
|
||||
#include "util/logging.h"
|
||||
|
||||
Message::Message(std::vector<uint8_t> data) {
|
||||
this->data = std::move(data);
|
||||
}
|
||||
|
||||
uint8_t Message::chk_sum() {
|
||||
|
||||
// initial checksum of 0
|
||||
uint64_t chk = 0;
|
||||
|
||||
// add all bytes to checksum
|
||||
for (uint8_t c : this->data)
|
||||
chk += c;
|
||||
|
||||
// return checksum
|
||||
return (uint8_t) (chk & 0xFF);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Message::get_data_encoded() {
|
||||
|
||||
// check if already encoded
|
||||
if (data.size() <= encoded.size())
|
||||
return encoded;
|
||||
|
||||
// encode data
|
||||
for (uint8_t c : this->data) {
|
||||
if (c == 0xAA || c == 0xFF) {
|
||||
encoded.push_back(0xFF);
|
||||
encoded.push_back(~c);
|
||||
} else {
|
||||
encoded.push_back(c);
|
||||
}
|
||||
}
|
||||
|
||||
// return data
|
||||
return this->encoded;
|
||||
}
|
||||
|
||||
void Message::print_debug() {
|
||||
std::ostringstream ss;
|
||||
for (uint8_t b : this->data)
|
||||
ss << " " << (void *) ((long long) b);
|
||||
ss << " / ";
|
||||
for (uint8_t b : this->get_data_encoded())
|
||||
ss << " " << (void *) ((long long) b);
|
||||
|
||||
log_info("reader", "{}", ss.str());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user