diff options
feat(frame): Implemented frame class
Recieves a hex string, containing all frame data and stores it as
something more useful
2 files changed, 113 insertions, 0 deletions
| -rw-r--r-- | src/frame.cpp | 65 | ||||
| -rw-r--r-- | src/frame.h | 48 |
diff --git a/src/frame.cpp b/src/frame.cpp new file mode 100644 index 0000000..ab8a5f3 --- /dev/null +++ b/src/frame.cpp @@ -0,0 +1,65 @@ +#include "frame.h" + +unsigned char hex_byte_to_byte(unsigned char byte) { + if (byte >= 'a' && byte <= 'f') return byte - 'a' + 10; + if (byte >= 'A' && byte <= 'F') return byte - 'A' + 10; + if (byte >= '0' && byte <= '9') return byte - '0'; + throw std::invalid_argument("Character isn't a hex value!"); +} + +unsigned char hex_value_to_byte(const unsigned char* hex) { + return (hex_byte_to_byte(hex[0]) << 4) | hex_byte_to_byte(hex[1]); +} + +unsigned char next_hex_byte(const unsigned char*& iso_tp_frame) { + if (*iso_tp_frame == '\0') + throw "Bad"; + + char byte = hex_value_to_byte(iso_tp_frame); + iso_tp_frame += 2; + return byte; +} + +// 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) { + unsigned char byte0 = next_hex_byte(iso_tp_frame); + + this->type = (FrameType)((byte0 & 0b11110000) >> 4); + this->size = byte0 & 0b00001111; + + switch (this->type) { + // Single frame + case SF: this->data_size = size; + break; + + // First frame + case FF: { + unsigned char byte1 = next_hex_byte(iso_tp_frame); + this->size = (this->size << 8) | byte1; + this->data_size = 6; + } + break; + + // Consecutive frame + case CF: this->data_size = 7; + break; + + // Flow control frame (ignore) + case FC: this->data_size = 0; + this->size = 0; + break; + } + + this->data = (unsigned char*)malloc(sizeof(unsigned char) * this->data_size); + for (int i = 0; i < this->data_size; ++i) { + this->data[i] = next_hex_byte(iso_tp_frame); + } +} + +std::ostream& operator<<(std::ostream& os, const Frame& f) { + for (int i = 0; i < f.data_size; ++i) { + os << (unsigned)f.data[i]; + } + return os; +} diff --git a/src/frame.h b/src/frame.h new file mode 100644 index 0000000..92b9c8f --- /dev/null +++ b/src/frame.h @@ -0,0 +1,48 @@ +#ifndef ISO_TP_FRAME +#define ISO_TP_FRAME + +#include <ostream> + +enum FrameType { + SF = 0, FF = 1, CF = 2, FC = 3 +}; + +class Frame { + FrameType type; + unsigned int size; + unsigned int data_size; + unsigned char* data; + + void parse_frame_str(const unsigned char* iso_tp_frame); + +public: + Frame(const char* iso_tp_frame) { + this->parse_frame_str((const unsigned char*)iso_tp_frame); + } + + ~Frame() { + delete[] this->data; + } + + FrameType get_type() const { + return this->type; + } + + unsigned int get_size() const { + return this->size; + } + + unsigned int get_data_size() const { + return this->data_size; + } + + const unsigned char* get_data() const { + return this->data; + } + + friend std::ostream& operator<<(std::ostream& os, const Frame& f); +}; + +std::ostream& operator<<(std::ostream& os, const Frame& f); + +#endif /* ISO_TP_FRAME */ |
