Summary Files Commits Branches @commit @diff @stats
path: root / src / message.cpp
blob: dfeb582f818b03dbc6ec5e8cc23b21107ad0ebeb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstring>
#include "message.h"

Message::Message(unsigned int can_id, const Frame& first_frame) : can_id(can_id), received(0) {
	this->size = first_frame.get_size();
	this->msg = (unsigned char*)malloc(sizeof(unsigned char) * this->size);
	this->receive_frame(first_frame);
}

Message::Message(const Message& rhs) : can_id(rhs.can_id), size(rhs.size), received(rhs.received) {
	this->msg = (unsigned char*)malloc(sizeof(unsigned char) * this->size);
	std::memcpy(this->msg, rhs.msg, this->size);
}

void Message::receive_frame(const Frame& f) {
	size_t expects = std::min(this->size - this->received, f.get_data_size());
	std::memcpy(this->msg + this->received, f.get_data(), expects);
	this->received += expects;
}

bool Message::expects_more_frames() const {
	return this->received < this->size;
}

std::ostream& operator<<(std::ostream& os, const Message& m) {
	os << m.can_id << ":";
	for (int i = 0; i < m.received; ++i) {
		os << " " << (unsigned)m.msg[i];
	}
	if (m.expects_more_frames())
		os << "...";
	return os;
}