McAckFrame.cpp
Go to the documentation of this file.
00001 /* 
00002  * File:   McAckFrame.cpp
00003  * Author: cwioro
00004  * 
00005  * Created on August 27, 2014, 10:26 AM
00006  */
00007 
00008 
00009 
00010 #include "McAckFrame.h"
00011 
00012 McAckFrame::McAckFrame(unsigned char* source, unsigned char* dest, std::string hostname_src, std::string group_n, uint32_t p_id, uint32_t seq_n)
00013 {
00014 
00015     /*ETH HEADER*/
00016     memcpy(eh_h_.eh_source, source, 6);
00017     memcpy(eh_h_.eh_dest, bcast_mac, 6);
00018 
00019     /*FRAME HEADER*/
00020 
00021     memcpy(header_.mac_destination_, dest, 6);
00022     this->hostname_source_ = hostname_src;
00023     this->header_.packet_id = p_id;
00024     this->header_.frame_seq_num = seq_n;
00025     this->header_.frame_type = FRAME_TYPE_MC_ACK;
00026     this->mc_group_ = group_n;
00027    
00028  
00029     this->cr_flag_ = false;
00030 }
00031 
00032 McAckFrame::McAckFrame(unsigned char* buffer)
00033 {
00034 
00035     unsigned char* buffer_start = buffer;
00036     buffer_str_len_ = 0;
00037 
00038     /*ETHERNET HEADER*/
00039     eh_header* eh = (struct eh_header *) buffer;
00040     buffer += sizeof (eh_header);
00041     buffer_str_len_ += sizeof (eh_header);
00042 
00043     /*FRAME HEADER*/
00044     mc_ack_header* rfh = (struct mc_ack_header *) buffer;
00045     buffer += sizeof (mc_ack_header);
00046     buffer_str_len_ += sizeof (mc_ack_header);
00047 
00048     /*SOURCE HOST*/
00049     hostname_source_ = "";
00050     hostname_source_.append((const char*) buffer, rfh->hostname_source_len);
00051     buffer += rfh->hostname_source_len;
00052     buffer_str_len_ += hostname_source_.length();
00053 
00054     /*MC GROUP */
00055     mc_group_ = "";
00056     if (rfh->mc_group_len > 0)
00057     {
00058         mc_group_.append((const char*) buffer, rfh->mc_group_len);
00059         buffer += rfh->mc_group_len;
00060     }
00061     buffer_str_len_ += mc_group_.length();
00062 
00063 
00064     /*INIT FLAGS*/
00065     cr_flag_ = (rfh->flag_field / 64) % 2 == 1;
00066 
00067 
00068     /*CRC */
00069     uint32_t crc = 0;
00070     memcpy((unsigned char*) &crc, (unsigned char*) buffer, 4); // get CRC
00071 
00072     std::string crc_data_string = "";
00073     crc_data_string.append((const char*) buffer_start,
00074                            this->HEADER_FIXED_LEN + rfh->hostname_source_len + rfh->mc_group_len);
00075     correct_crc_ = (crc == (uint32_t) GetCrc32(crc_data_string));
00076     buffer_str_len_++;
00077 
00078 
00079 
00080     /* COPY HEADER FIELDS */
00081     memcpy(&this->eh_h_, &(*eh), sizeof (eh_header));
00082     memcpy(&this->header_, &(*rfh), sizeof (mc_ack_header));
00083 
00084 
00085 
00086 }
00087 
00088 McAckFrame::~McAckFrame()
00089 {
00090     // TODO Auto-generated destructor stub
00091 }
00092 
00093 std::string McAckFrame::getFrameAsNetworkString()
00094 {
00095 
00096 
00097     
00098 
00099     /*LEN FIELDS */
00100     this->header_.hostname_source_len = hostname_source_.length();
00101     this->header_.mc_group_len = mc_group_.length();
00102 
00103 
00104     unsigned char f_buffer[ETHER_MAX_LEN]; // = unsigned char[ETHER_MAX_LEN];
00105     unsigned char* buffer = f_buffer;
00106     unsigned char* buffer_start = f_buffer;
00107 
00108     /*ETHERNET FIELDS*/
00109     //eh_header* eh = (struct eh_header *) buffer;
00110     memcpy(buffer, &this->eh_h_, sizeof (eh_header));
00111     buffer += sizeof (eh_header);
00112 
00113     /*FIXED RF HEADER FIELDS*/
00114     memcpy(buffer, &this->header_, sizeof (mc_ack_header));
00115     buffer += sizeof (mc_ack_header);
00116 
00117     /*SOURCE HOST */
00118     memcpy(buffer, this->hostname_source_.data(),
00119            this->hostname_source_.length());
00120     buffer += this->hostname_source_.length();
00121 
00122     /*MC GROUP */
00123     memcpy(buffer, this->mc_group_.data(), this->mc_group_.length());
00124     buffer += mc_group_.length();
00125 
00126 
00127 
00128     /*CRC*/
00129     int dynamic_field_len = this->hostname_source_.length() + this->mc_group_.length();
00130     std::string crc_string = std::string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len);
00131     uint32_t crc = GetCrc32(crc_string);
00132     memcpy(buffer, &crc, sizeof (uint32_t));
00133     buffer += sizeof (uint32_t);
00134 
00135 
00136 
00137     return string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len + sizeof (crc));
00138 
00139 
00140 }
00141 
00142 void McAckFrame::print_frame()
00143 {
00144     ROS_ERROR("FRAME SEQ NUM [%u] PACKET ID[%u] SRC[%s]  MC GROUP[%s]", header_.frame_seq_num,header_.packet_id, hostname_source_.c_str(),mc_group_.c_str());
00145     ROS_ERROR("SRC MAC[%s] DST MAC[%s]", getMacAsStr(eh_h_.eh_source).c_str(), getMacAsStr(eh_h_.eh_dest).c_str());
00146 }


adhoc_communication
Author(s): Guenter Cwioro , Torsten Andre
autogenerated on Thu Jun 6 2019 20:59:42