Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <string>
00011
00012 #include "AckLinkFrame.h"
00013 #include "EthernetFrame.h"
00014 #include "defines.h"
00015
00016 AckLinkFrame::AckLinkFrame(unsigned char source[],unsigned char confirmer_mac[],unsigned char dest[],uint32_t frame_id,std::string hostname,uint8_t type)
00017 {
00018
00019
00020
00021 memcpy(eh_h_.eh_source, source, 6);
00022 memcpy(eh_h_.eh_dest, bcast_mac, 6);
00023
00024
00025 memcpy(header_.mac_confirmer, confirmer_mac, 6);
00026 memcpy(header_.mac_destination_, dest, 6);
00027 this->header_.ack_frame_type = type;
00028 this->header_.frame_id = frame_id;
00029 this->hostname_source_ = hostname;
00030 this->header_.frame_type = FRAME_TYPE_ACK;
00031 this->pos_ack_flag_ = true;
00032 this->cr_flag_ = false;
00033 }
00034
00035
00036
00037 AckLinkFrame::AckLinkFrame(unsigned char* buffer)
00038 {
00039
00040 unsigned char* buffer_start = buffer;
00041 buffer_str_len_ = 0;
00042
00043
00044 eh_header* eh = (struct eh_header *) buffer;
00045 buffer += sizeof (eh_header);
00046 buffer_str_len_ += sizeof (eh_header);
00047
00048
00049 ack_lf_header* rfh = (struct ack_lf_header *) buffer;
00050 buffer += sizeof (ack_lf_header);
00051 buffer_str_len_ += sizeof (ack_lf_header);
00052
00053
00054 hostname_source_ = "";
00055 hostname_source_.append((const char*) buffer, rfh->hostname_source_len);
00056 buffer += rfh->hostname_source_len;
00057 buffer_str_len_ += hostname_source_.length();
00058
00059
00060
00061 pos_ack_flag_ = (rfh->flag_field / 128) % 2 == 1;
00062 cr_flag_ = (rfh->flag_field / 64) % 2 == 1;
00063
00064
00065
00066 uint32_t crc = 0;
00067 memcpy((unsigned char*) &crc, (unsigned char*) buffer, 4);
00068
00069 std::string crc_data_string = "";
00070 crc_data_string.append((const char*) buffer_start, this->HEADER_FIXED_LEN + rfh->hostname_source_len );
00071 correct_crc_ = (crc == (uint32_t) GetCrc32(crc_data_string));
00072 buffer_str_len_++;
00073
00074
00075
00076
00077 memcpy(&this->eh_h_, &(*eh), sizeof (eh_header));
00078 memcpy(&this->header_, &(*rfh), sizeof (ack_lf_header));
00079
00080
00081
00082 }
00083
00084 AckLinkFrame::~AckLinkFrame()
00085 {
00086
00087 }
00088
00089 std::string AckLinkFrame::getFrameAsNetworkString()
00090 {
00091
00092
00093
00094 this->header_.flag_field = 0;
00095
00096 if (pos_ack_flag_)
00097 this->header_.flag_field += 128;
00098 if(cr_flag_)
00099 this->header_.flag_field += 64;
00100
00101
00102 this->header_.hostname_source_len = hostname_source_.length();
00103
00104
00105
00106 unsigned char f_buffer[ETHER_MAX_LEN];
00107 unsigned char* buffer = f_buffer;
00108 unsigned char* buffer_start = f_buffer;
00109
00110
00111 memcpy(buffer, &this->eh_h_, sizeof (eh_header));
00112 buffer += sizeof (eh_header);
00113
00114
00115 memcpy(buffer, &this->header_, sizeof (ack_lf_header));
00116 buffer += sizeof (ack_lf_header);
00117
00118
00119 memcpy(buffer, this->hostname_source_.data(),this->hostname_source_.length());
00120 buffer += this->hostname_source_.length();
00121
00122
00123
00124 int dynamic_field_len = this->hostname_source_.length() ;
00125 std::string crc_string = std::string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len);
00126 uint32_t crc = GetCrc32(crc_string);
00127 memcpy(buffer, &crc, sizeof (uint32_t));
00128 buffer += sizeof (uint32_t);
00129
00130
00131
00132 return string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len + sizeof (crc));
00133
00134
00135 }
00136
00137
00138
00139 void AckLinkFrame::print_frame()
00140 {
00141 ROS_ERROR("FRAME ID [%u] SRC[%s] TYPE[%u]", header_.frame_id,hostname_source_.c_str(), header_.ack_frame_type);
00142 ROS_ERROR("MAC_CONF[%s] SRC MAC[%s] DST MAC[%s]", getMacAsStr(header_.mac_confirmer).c_str(),getMacAsStr(eh_h_.eh_source).c_str(),getMacAsStr(eh_h_.eh_dest).c_str() );
00143 }