diff options
feat: Track consecutive frame index
4 files changed, 20 insertions, 3 deletions
| -rw-r--r-- | src/frame.cpp | 11 | ||||
| -rw-r--r-- | src/frame.h | 2 | ||||
| -rw-r--r-- | src/message.cpp | 9 | ||||
| -rw-r--r-- | src/message.h | 1 |
diff --git a/src/frame.cpp b/src/frame.cpp index bfa9daf..fe96308 100644 --- a/src/frame.cpp +++ b/src/frame.cpp @@ -49,8 +49,10 @@ void Frame::parse_frame_str(const unsigned char* iso_tp_frame) { break; // Consecutive frame - case CF: this->data_size = 7; // Message objects track total amount of data + case CF: this->index = this->size; + this->data_size = 7; // Message objects track total amount of data // from multipart messages + this->size = data_size; break; // Flow control frame @@ -72,6 +74,7 @@ void Frame::free() { void Frame::copyFrom(const Frame& other) { this->type = other.type; + this->index = other.index; this->size = other.size; this->data_size = other.data_size; @@ -81,7 +84,7 @@ void Frame::copyFrom(const Frame& other) { /* Public */ -Frame::Frame(const char* iso_tp_frame) { +Frame::Frame(const char* iso_tp_frame) : index(0) { this->parse_frame_str((const unsigned char*)iso_tp_frame); } @@ -105,6 +108,10 @@ FrameType Frame::get_type() const { return this->type; } +unsigned char Frame::get_index() const { + return this->index; +} + unsigned int Frame::get_size() const { return this->size; } diff --git a/src/frame.h b/src/frame.h index c928edd..07ddab2 100644 --- a/src/frame.h +++ b/src/frame.h @@ -10,6 +10,7 @@ enum FrameType { class Frame { FrameType type; + unsigned char index; unsigned int size; unsigned int data_size; unsigned char* data; @@ -30,6 +31,7 @@ public: /* Getters */ FrameType get_type() const; + unsigned char get_index() const; unsigned int get_size() const; unsigned int get_data_size() const; const unsigned char* get_data() const; diff --git a/src/message.cpp b/src/message.cpp index 834fd87..0769533 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -11,6 +11,7 @@ void Message::copyFrom(const Message& other) { this->can_id = other.can_id; this->size = other.size; this->received = other.received; + this->latest_index = other.latest_index; this->msg = new unsigned char[this->size]; std::memcpy(this->msg, other.msg, this->size); @@ -18,7 +19,7 @@ void Message::copyFrom(const Message& other) { /* Public */ -Message::Message(unsigned int can_id, const Frame& first_frame) : can_id(can_id), received(0) { +Message::Message(unsigned int can_id, const Frame& first_frame) : can_id(can_id), received(0), latest_index(0) { this->size = first_frame.get_size(); this->msg = new unsigned char[this->size]; this->receive_frame(first_frame); @@ -41,6 +42,12 @@ Message& Message::operator=(const Message& other) { } void Message::receive_frame(const Frame& f) { + // This logic isn't really necessary, but I like to be thorough + if ((this->latest_index + 1) % 16 < f.get_index()) { + throw std::invalid_argument("A consecutive frame was lost!"); + } + this->latest_index = f.get_index(); + // For CFs we set the data size to 7 (the maximum), however, we may be expecting // to receive less data (in the last CF). size_t expects = std::min(this->size - this->received, f.get_data_size()); diff --git a/src/message.h b/src/message.h index fcc8258..764cf69 100644 --- a/src/message.h +++ b/src/message.h @@ -8,6 +8,7 @@ class Message { unsigned int can_id; unsigned int size; unsigned int received; + unsigned char latest_index; unsigned char* msg; void free(); |
