DESERT 3.5.1
Loading...
Searching...
No Matches
packer_uwApplication.cc
Go to the documentation of this file.
1//
2// Copyright (c) 2013 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
38
39static class PackerUwApplicationClass : public TclClass {
40 public:
41
42 PackerUwApplicationClass() : TclClass("UW/APP/uwApplication/Packer") {
43 }
44
45 TclObject* create(int, const char*const*) {
46 return (new packer_uwApplication());
47 }
49
51:
52packer(false),
53sn_field_Bits(0),
54rfft_field_Bits(0),
55rfftvalid_field_Bits(0),
56priority_filed_Bits(0),
57payload_size_field_Bits(0)
58{
59 bind("SN_FIELD_", (int*) &sn_field_Bits);
60 bind("RFFT_FIELD_", (int*) &rfft_field_Bits);
61 bind("RFFTVALID_FIELD_", (int*) &rfftvalid_field_Bits);
62 bind("PRIORITY_FIELD_", (int*) &priority_filed_Bits);
63 bind("PAYLOADMSG_FIELD_SIZE_", (int*) &payload_size_field_Bits);
64
65 this->init();
66} //end packer_uwApplication() constructor
67
69
70} //end packer_uwApplication() destructor
71
73 if (debug_)
74 std::cout << "Re-initialization of n_bits for the uwApplication packer." << std::endl;
75
76 n_bits.clear();
77 n_bits.assign(5, 0);
78
84
85} //end init()
86
87size_t packer_uwApplication::packMyHdr(Packet* p, unsigned char* buffer, size_t offset) {
88 hdr_cmn* hcmn = HDR_CMN(p);
89
90 if (hcmn->ptype() == PT_DATA_APPLICATION) {
91 //Packet to be serialized is DATA packet
93
94 offset += put(buffer, offset, &(applh->sn_), n_bits[SN_FIELD]);
95 offset += put(buffer, offset, &(applh->rftt_), n_bits[RFFT_FIELD]);
96 offset += put(buffer, offset, &(applh->rftt_valid_), n_bits[RFFTVALID_FIELD]);
97 offset += put(buffer, offset, &(applh->priority_), n_bits[PRIORITY_FIELD]);
98 offset += put(buffer, offset, &(applh->payload_size_), n_bits[PAYLOAD_SIZE_FIELD]);
99 int payload_size_bits = applh->payload_size()*8;
100 offset += put(buffer, offset, &(applh->payload_msg), payload_size_bits);
101
102 if (debug_) {
103 std::cout << "\033[1;37;45m (TX) UWAPPLICATION::DATA packer hdr \033[0m" << std::endl;
105 }
106 }
107 return offset;
108} //end packMyHdr() method
109
110size_t packer_uwApplication::unpackMyHdr(unsigned char* buffer, size_t offset, Packet* p) {
111
112 hdr_cmn* hcmn = HDR_CMN(p);
113
114 if (hcmn->ptype() == PT_DATA_APPLICATION) {
115 //Packet to be serialized is a DATA packet
117
118 memset(&(applh->sn_), 0, sizeof (applh->sn_));
119 offset += get(buffer, offset, &(applh->sn_), n_bits[SN_FIELD]);
120 memset(&(applh->rftt_), 0, sizeof (applh->rftt_));
121 offset += get(buffer, offset, &(applh->rftt_), n_bits[RFFT_FIELD]);
122 memset(&(applh->rftt_valid_), 0, sizeof (applh->rftt_valid_));
123 offset += get(buffer, offset, &(applh->rftt_valid_), n_bits[RFFTVALID_FIELD]);
124 memset(&(applh->priority_), 0, sizeof (applh->priority_));
125 offset += get(buffer, offset, &(applh->priority_), n_bits[PRIORITY_FIELD]);
126 memset(&(applh->payload_msg), 0, sizeof (applh->payload_size_));
127 offset += get(buffer, offset, &(applh->payload_size_), n_bits[PAYLOAD_SIZE_FIELD]);
128 memset(&(applh->payload_msg), 0, sizeof (applh->payload_msg));
129 int payload_size_bit = applh->payload_size()*8;
130 //offset += get(buffer, offset, &(applh->payload_msg), n_bits[PAYLOADMSG_FIELD]);
131 offset += get(buffer, offset, &(applh->payload_msg), payload_size_bit);
132
133 if (debug_) {
134 std::cout << "\033[1;32;40m (RX) UWAPPLICATION::DATA packer hdr \033[0m" << std::endl;
136 }
137 }
138 return offset;
139} //end unpackMyHdr() method
140
142 std::cout << "\033[1;37;45m Packer Name \033[0m: UWAPPLICATION \n";
143 std::cout << "** DATA fields:\n";
144 std::cout << "\033[1;37;45m Field: SN_FIELD: \033[0m:" << n_bits[SN_FIELD] << " bits\n";
145 std::cout << "\033[1;37;45m Field: RFFT_FIELD: \033[0m:" << n_bits[RFFT_FIELD] << " bits\n";
146 std::cout << "\033[1;37;45m Field: RFFTVALID_FIELD: \033[0m:" << n_bits[RFFTVALID_FIELD] << " bits\n";
147 std::cout << "\033[1;37;45m Field: PRIORITY_FIELD: \033[0m:" << n_bits[PRIORITY_FIELD] << " bits\n";
148 std::cout << "\033[1;37;45m Field: PAYLOADMSG_SIZE_FIELD: \033[0m:" << n_bits[PAYLOAD_SIZE_FIELD] << " bits\n";
149
150 std::cout << std::endl;
151} //end printMyHdrMap() method
152
154 //Print information packet serialized by the method packMyHdr
155 hdr_cmn* hcmn = HDR_CMN(p);
156
157 if (hcmn->ptype() == PT_DATA_APPLICATION) {
158 //Packet to be serialized is a DATA packet
160 std::cout << "\033[1;37;45m 1st field \033[0m, SN_FIELD: " << applh->sn_ << std::endl;
161 std::cout << "\033[1;37;45m 2nd field \033[0m, RFFT_FIELD: " << applh->rftt_ << std::endl;
162 std::cout << "\033[1;37;45m 3rd field \033[0m, RFFTVALID_FIELD: " << applh->rftt_valid_ << std::endl;
163 std::cout << "\033[1;37;45m 4th field \033[0m, PRIORITY_FIELD: " << (int)applh->priority_ << std::endl;
164 std::cout << "\033[1;37;45m 5th field \033[0m, PAYLOADMSG_SIZE_FIELD: " << applh->payload_size_ << std::endl;
165 std::cout << "\033[1;37;45m 5th field \033[0m, PAYLOADMSG_FIELD: ";
166 for(int i=0;i<applh->payload_size();i++)
167 {
168 cout << applh->payload_msg[i];
169 }
170 std::cout << endl;
171 }
172} //end printMyHdrFields() method
packet_t PT_DATA_APPLICATION
DATA packet type.
TclObject * create(int, const char *const *)
Class to map a ns2 mac header into a bit stream, and vice-versa.
void printMyHdrMap()
Method used for debug purposes.
size_t unpackMyHdr(unsigned char *buffer, size_t offset, Packet *p)
Method responsible to take the informations from the received buffer and store it into the headers of...
void init()
Init the packer for uwApplication prototocol.
size_t sn_field_Bits
Bit length of the sn_ field to be put in the header stream of bits.
size_t rfft_field_Bits
Bit length of the rfft_field to be put in the header stream of bits.
void printMyHdrFields(Packet *)
Method used for debug purposes.
size_t rfftvalid_field_Bits
Bit length of the rfftvalid_field to be put in the header stream of bits.
@ PRIORITY_FIELD
Priority flag: 1 means high priority, 0 normal priority.
@ SN_FIELD
Serial number of the packet.
@ RFFT_FIELD
Forward Trip Time of the packet.
@ RFFTVALID_FIELD
Flag used to set the validity of the fft field.
@ PAYLOAD_SIZE_FIELD
Message payload.
packer_uwApplication()
Constructor packer_uwApplication class.
size_t packMyHdr(Packet *p, unsigned char *buffer, size_t offset)
Method to transform the headers of uwApplication protocol into a stream of bits.
~packer_uwApplication()
Destructor packer_uwApplication class.
size_t payload_size_field_Bits
Bit length of the payloadmsg_field to be put in the header stream of bits.
size_t priority_filed_Bits
Bit length of the priority_field to be put in the header stream of bits.
Class exploited by the Uwal module to map an NS-Miracle packet into a bit stream, and vice-versa.
Definition packer.h:57
std::vector< size_t > n_bits
Vector of elements containing the indication of the number of bits to consider for each header field.
Definition packer.h:224
size_t put(unsigned char *buffer, size_t offset, void *val, size_t h)
Method used to map in a certain number of bits, contained in a buffer of chars, a given variable.
Definition packer.cpp:685
size_t get(unsigned char *buffer, size_t offset, void *val, size_t h)
Method used to retrieve a given variable from a certain number of bits contained in a buffer of chars...
Definition packer.cpp:673
int debug_
Flag to enable debug messages.
Definition packer.h:227
PackerUwApplicationClass class_module_packerUwApplication
Content header of TRIGGER packet.
int rftt_
Forward Trip Time of the packet.
char payload_msg[MAX_LENGTH_PAYLOAD]
Message payload.
uint16_t sn_
Serial number of the packet.
bool rftt_valid_
Flag used to set the validity of the fft field.
uint16_t payload_size_
Size (bytes) of the payload.
uint8_t priority_
Priority flag: 1 means high priority, 0 normal priority.
#define HDR_DATA_APPLICATION(p)
alias defined to access the TRIGGER HEADER