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


adhoc_communication
Author(s): Guenter Cwioro , Torsten Andre
autogenerated on Thu Aug 27 2015 11:56:40