MultiHopBroadcastFrame.cpp
Go to the documentation of this file.
00001 /* 
00002  * File:   MultiHopBroadcastFrame.cpp
00003  * Author: cwioro
00004  * 
00005  * Created on September 17, 2014, 12:00 PM
00006  */
00007 #include <string>
00008 #include "MultiHopBroadcastFrame.h"
00009 #include "EthernetFrame.h"
00010 
00011 MultiHopBroadcastFrame::MultiHopBroadcastFrame(std::string topic_to_publish, std::string data,  string source_host,  uint8_t payload_type_field, uint16_t hop_range)
00012 {
00013     this->topic_ = topic_to_publish;
00014     this->hostname_source_ = source_host;
00015     this->payload_ = data;
00016     this->header_.payload_type = payload_type_field;
00017     this->header_.hop_limit = hop_range;
00018         header_.id = frame_count_stat++;
00019 
00020     memcpy(this->eh_h_.eh_dest, bcast_mac, 6);
00021     
00022     this->header_.current_hop = 1       ;
00023     
00024       this->header_.frame_type = FRAME_TYPE_BROADCAST;
00025     
00026 }
00027 
00028 
00029 MultiHopBroadcastFrame::MultiHopBroadcastFrame(unsigned char* buffer)
00030 {
00031     unsigned char* buffer_start = buffer;
00032     buffer_str_len_ = 0;
00033 
00034     /*ETHERNET HEADER*/
00035     eh_header* eh = (struct eh_header *) buffer;
00036     buffer += sizeof (eh_header);
00037     buffer_str_len_ += sizeof (eh_header);
00038 
00039     /*FRAME HEADER*/
00040     mh_bcast_header* rfh = (struct mh_bcast_header *) buffer;
00041     buffer += sizeof (mh_bcast_header);
00042     buffer_str_len_ += sizeof (mh_bcast_header);
00043 
00044     /*SOURCE HOST*/
00045     hostname_source_ = "";
00046     hostname_source_.append((const char*) buffer, rfh->hostname_source_len);
00047     buffer += rfh->hostname_source_len;
00048     buffer_str_len_ += rfh->hostname_source_len;
00049 
00050     /*TOPIC*/
00051     topic_ = "";
00052     topic_.append((const char*) buffer, rfh->topic_len);
00053     buffer += rfh->topic_len;
00054     buffer_str_len_ += rfh->topic_len;
00055 
00056 
00057     /*PAYLOAD*/
00058     payload_ = "";
00059     payload_.append((const char*) buffer, rfh->payload_len);
00060     buffer += rfh->payload_len;
00061     buffer_str_len_ += rfh->payload_len;
00062 
00063     /*INIT FLAGS*/
00064     if(rfh->current_hop <= rfh->hop_limit)
00065         this->rebroadcast = true;
00066     else
00067         this->rebroadcast = false;
00068 
00069     /*CRC */
00070     uint32_t crc = 0;
00071     memcpy((unsigned char*) &crc, (unsigned char*) buffer, 4); // get CRC
00072 
00073     std::string crc_data_string = "";
00074     crc_data_string.append((const char*) buffer_start, this->HEADER_FIXED_LEN + rfh->hostname_source_len +  rfh->topic_len + rfh->payload_len);
00075     correct_crc_ = (crc == (uint32_t) GetCrc32(crc_data_string));
00076 
00077     
00078     
00079     /* COPY HEADER FIELDS */
00080     memcpy(&this->eh_h_, &(*eh), sizeof (eh_header));
00081     memcpy(&this->header_, &(*rfh), sizeof (mh_bcast_header));
00082 }
00083 
00084 MultiHopBroadcastFrame::~MultiHopBroadcastFrame()
00085 {
00086 }
00087 
00088 std::string MultiHopBroadcastFrame::getFrameAsNetworkString(unsigned char* source)
00089 {
00090 
00091 
00092    
00093     header_.current_hop++;
00094      memcpy(this->eh_h_.eh_source, source, 6);
00095 
00096     /*LEN FIELDS */
00097     this->header_.hostname_source_len = hostname_source_.length();
00098     this->header_.topic_len = topic_.length();
00099     this->header_.payload_len = payload_.length();
00100 
00101 
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 
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 (mh_bcast_header));
00117     buffer += sizeof (mh_bcast_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 
00125     /*TOPIC*/
00126     memcpy(buffer, this->topic_.data(), this->topic_.length());
00127     buffer += topic_.length();
00128 
00129 
00130     /*PAYLOAD */
00131     memcpy(buffer, this->payload_.data(), this->payload_.length());
00132     buffer += payload_.length();
00133 
00134 
00135     /*CRC*/
00136     uint32_t dynamic_field_len = this->hostname_source_.length() + topic_.length() + payload_.length();
00137     std::string crc_string = std::string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len);
00138     uint32_t crc = GetCrc32(crc_string);
00139     memcpy(buffer, &crc, sizeof (uint32_t));
00140     buffer += sizeof (uint32_t);
00141 
00142 
00143     return string((const char*) buffer_start, this->HEADER_FIXED_LEN + dynamic_field_len + sizeof (uint32_t));
00144 
00145 }


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