diff options
chore: Add documentation comments where needed
4 files changed, 16 insertions, 5 deletions
| -rw-r--r-- | src/frame.cpp | 16 | ||||
| -rw-r--r-- | src/frame.h | 1 | ||||
| -rw-r--r-- | src/main.cpp | 1 | ||||
| -rw-r--r-- | src/message.cpp | 3 |
diff --git a/src/frame.cpp b/src/frame.cpp index babfef7..bfa9daf 100644 --- a/src/frame.cpp +++ b/src/frame.cpp @@ -9,6 +9,7 @@ unsigned char hex_byte_to_byte(unsigned char byte) { } unsigned char hex_value_to_byte(const unsigned char* hex) { + // Could've been done with sprintf, however this is faster return (hex_byte_to_byte(hex[0]) << 4) | hex_byte_to_byte(hex[1]); } @@ -23,13 +24,16 @@ unsigned char next_hex_byte(const unsigned char*& iso_tp_frame) { /* Private */ -// b0 b1 b2 b3 b4 b5 b6 b7 -// 02 10 C0 00 00 00 00 00 void Frame::parse_frame_str(const unsigned char* iso_tp_frame) { + // Every two characters in a frame represent the hex value of a single byte + // Example (spaces added for visual clarity): + // b0 b1 b2 b3 b4 b5 b6 b7 + // 02 10 C0 00 00 00 00 00 + unsigned char byte0 = next_hex_byte(iso_tp_frame); this->type =(FrameType)((byte0 & 0b11110000) >> 4); - this->size = byte0 & 0b00001111; + this->size = byte0 & 0b00001111; switch (this->type) { // Single frame @@ -45,11 +49,13 @@ void Frame::parse_frame_str(const unsigned char* iso_tp_frame) { break; // Consecutive frame - case CF: this->data_size = 7; + case CF: this->data_size = 7; // Message objects track total amount of data + // from multipart messages break; // Flow control frame - case FC: this->data_size = 2; + case FC: this->data_size = 2; // The FC flag takes less than a byte, so I'm + // not considering it data. Maybe I should? this->size = 2; break; } diff --git a/src/frame.h b/src/frame.h index 989ee38..c928edd 100644 --- a/src/frame.h +++ b/src/frame.h @@ -4,6 +4,7 @@ #include <ostream> enum FrameType { + // Explicit values, so it is apparent they correspond to the appropriate type values SF = 0, FF = 1, CF = 2, FC = 3 }; diff --git a/src/main.cpp b/src/main.cpp index 1e7d7d0..6a1bf2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,7 @@ int main() { sscanf(data, "%3x", &can_id); Frame frame(data + 3); + // When the message is split into multiple frames (an FF and then multiple CFs) if (unfinished_messages.count(can_id)) { Message& m = unfinished_messages[can_id]; diff --git a/src/message.cpp b/src/message.cpp index ee1d8fe..834fd87 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -41,7 +41,10 @@ Message& Message::operator=(const Message& other) { } void Message::receive_frame(const Frame& f) { + // 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()); + std::memcpy(this->msg + this->received, f.get_data(), expects); this->received += expects; } |
