DESERT 3.5.1
Loading...
Searching...
No Matches
packer-uwranging-tdoa.cc
Go to the documentation of this file.
1//
2// Copyright (c) 2024 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
39#include "uwranging_tdoa_hdr.h"
40#include <iostream>
41
42static class PackerUwRangingTDOAClass : public TclClass
43{
44public:
46 : TclClass("UW/RANGING_TDOA/Packer")
47 {
48 }
49
50 TclObject *
51 create(int, const char *const *)
52 {
53 return (new packer_uwRangingTDOA());
54 }
56
58 : packer(false)
59 , source_pkt_id_Bits(0)
60 , source_node_id_Bits(0)
61 , times_size_Bits(0)
62{
63 bind("SOURCE_PKT_ID_FIELD", (int *) &source_pkt_id_Bits);
64 bind("SOURCE_NODE_ID_FIELD", (int *) &source_node_id_Bits);
65 bind("TIMES_SIZE_FIELD", (int *) &times_size_Bits);
66
67 this->init();
68}
69
70void
72{
73 if (debug_)
74 std::cout << "Re-initialization of n_bits for the uwRangingTDOA packer."
75 << std::endl;
76
77 n_bits.clear();
78 n_bits.assign(LAST_ELEM, 0);
79
83}
84
85size_t
86packer_uwRangingTDOA::packMyHdr(Packet *p, unsigned char *buffer, size_t offset)
87{
88 hdr_cmn *hcmn = HDR_CMN(p);
89
90 if (hcmn->ptype() == PT_UWRANGING_TDOA) {
92
93 offset += put(buffer,
94 offset,
95 &(tdoah->source_pkt_id),
97 offset += put(buffer,
98 offset,
99 &(tdoah->source_node_id),
101 offset += put(buffer,
102 offset,
103 &(tdoah->times_size_),
105
106 int times_size_bits = tdoah->times_size() * sizeof(tdoa_entry) * 8;
107 offset += put(buffer, offset, &(tdoah->times_), times_size_bits);
108
109 if (debug_) {
110 std::cout << "\033[1;37;45m (TX) UwRangingTDOA::DATA packer hdr "
111 "\033[0m"
112 << std::endl;
114 }
115 }
116 return offset;
117}
118
119size_t
120packer_uwRangingTDOA::unpackMyHdr(unsigned char *buffer, size_t offset, Packet *p)
121{
122 hdr_cmn *hcmn = HDR_CMN(p);
123
124 if (hcmn->ptype() == PT_UWRANGING_TDOA) {
126
127 memset(&(tdoah->source_pkt_id), 0, sizeof(tdoah->source_pkt_id));
128 offset += get(buffer,
129 offset,
130 &(tdoah->source_pkt_id),
132 memset(&(tdoah->source_node_id), 0, sizeof(tdoah->source_node_id));
133 offset += get(buffer,
134 offset,
135 &(tdoah->source_node_id),
137 memset(&(tdoah->times_size_), 0, sizeof(tdoah->times_size_));
138 offset += get(buffer,
139 offset,
140 &(tdoah->times_size_),
142
143 int times_size_bits = tdoah->times_size() * sizeof(tdoa_entry) * 8;
144 offset += get(buffer, offset, &(tdoah->times_), times_size_bits);
145
146 if (debug_) {
147 std::cout << "\033[1;32;40m (RX) UwRangingTDOA::DATA packer hdr "
148 "\033[0m" << std::endl;
150 }
151 }
152 return offset;
153}
154
155void
157{
158 std::cout << "\033[1;37;45m Packer Name \033[0m: UWRANGINGTDOA \n";
159 std::cout << "** DATA fields:\n";
160 std::cout << "\033[1;37;45m Field: SOURCE_PKT_ID_FIELD: \033[0m:"
161 << n_bits[SOURCE_PKT_ID_FIELD] << " bits\n";
162 std::cout << "\033[1;37;45m Field: SOURCE_NODE_ID_FIELD: \033[0m:"
163 << n_bits[SOURCE_NODE_ID_FIELD] << " bits\n";
164 std::cout << "\033[1;37;45m Field: TIMES_SIZE_FIELD: \033[0m:"
165 << n_bits[TIMES_SIZE_FIELD] << " bits\n";
166 std::cout << std::endl;
167}
168
169void
171{
172 hdr_cmn *hcmn = HDR_CMN(p);
173
174 if (hcmn->ptype() == PT_UWRANGING_TDOA) {
175 // Packet to be serialized is a DATA packet
177 std::cout << "\033[1;37;45m 1st field \033[0m, SOURCE_PKT_ID_FIELD: "
178 << (int) tdoah->source_pkt_id << std::endl;
179 std::cout << "\033[1;37;45m 2nd field \033[0m, SOURCE_NODE_ID_FIELD: "
180 << (int) tdoah->source_node_id << std::endl;
181 std::cout << "\033[1;37;45m 3rd field \033[0m, TIMES_SIZE_FIELD: "
182 << (int) tdoah->times_size_ << std::endl;
183 std::cout << "\033[1;37;45m 4th field \033[0m, TIMES: ";
184 for (int i = 0; i < tdoah->times_size(); i++) {
185 std::cout << "(id: " << (int) tdoah->times_[i].id
186 << " node: " << (int) tdoah->times_[i].node
187 << " time: " << tdoah->times_[i].time << ") , ";
188 }
189 std::cout << endl;
190 }
191}
TclObject * create(int, const char *const *)
Class to map a ns2 uwRangingTDOA header into a bit stream, and vice-versa.
packer_uwRangingTDOA()
Constructor packer_uwRangingTDOA class.
virtual size_t packMyHdr(Packet *p, unsigned char *buffer, size_t offset) override
Method to transform the headers of uwRangingTDOA protocol into a stream of bits.
size_t source_pkt_id_Bits
Bit length of the source_pkt_id field to be put in the header stream of bits.
virtual void printMyHdrFields(Packet *) override
Method used for debug purposes.
virtual size_t unpackMyHdr(unsigned char *buffer, size_t offset, Packet *p) override
Method responsible to take the informations from the received buffer and store it into the headers of...
size_t times_size_Bits
Bit length of the times_size_field to be put in the header stream of bits.
virtual void init() override
Init the packer for uwRangingTDOA prototocol.
virtual void printMyHdrMap() override
Method used for debug purposes.
@ SOURCE_PKT_ID_FIELD
ID of the ranging packet.
@ SOURCE_NODE_ID_FIELD
ID of the node.
@ TIMES_SIZE_FIELD
Size of the payload.
size_t source_node_id_Bits
Bit length of the source_node_id 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
PackerUwRangingTDOAClass class_module_packerUwRangingTDOA
Header of the token bus protocol.
uwrange_node_t times_size_
uwrange_pkt_t source_pkt_id
Ranging packet id.
uwrange_node_t source_node_id
Source node id.
tdoa_entry times_[MAX_NUM_NODES]
Number of entries in the packet.
Define the struct of a single tdoa payload entry.
uwrange_pkt_t id
Id of the packet.
uwrange_time_t time
Holdover time.
uwrange_node_t node
Id of the source node.
Common structures and variables in the protocol.
#define HDR_RANGING_TDOA(p)
alias defined to access the hdr_ranging_tdoa HEADER