DESERT 3.5.1
Loading...
Searching...
No Matches
DESERT_Addons/uwmphy_modem/uwmphy_modem.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 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
37#include "uwmphy_modem.h"
38
39UWMPhy_modem::UWMPhy_modem(std::string pToDevice_)
40{
41
42 pToDevice = pToDevice_;
43
44 bind("ID_", &ID);
45 bind("period_", &period);
46 bind("debug_", &debug_);
47 bind("loglevel_", &loglevel_);
48 bind("SetModemID_", &SetModemID);
49 bind("ModemQueueLength_", &queue_length);
50 t = -1;
51 PktRx = NULL;
52 pcheckTmr = NULL;
53 pDropTimer = NULL;
54 pmDriver = NULL;
55
56 for (int i = 0; i < _MTBL; i++) {
57 modemTxBuff[i] = NULL;
58 }
59
62
63 if (queue_length < 0)
67}
68
70{
71}
72
73int
74UWMPhy_modem::command(int argc, const char *const *argv)
75{
76 Tcl &tcl = Tcl::instance();
77
78 if (argc == 2) {
79 if (!strcmp(argv[1], "start")) {
80 start();
81 return TCL_OK;
82 }
83 if (!strcmp(argv[1], "stop")) {
84 stop();
85 return TCL_OK;
86 }
87 } else if (argc == 3) {
88 if (!strcmp(argv[1], "setLogSuffix")) {
89 string tmp_ = (char *) argv[2];
90 // tmp_.push_back('\n');
91 log_suffix = new char[tmp_.size()];
92 strcpy(log_suffix, tmp_.c_str());
93 return TCL_OK;
94 }
95 }
96 return Module::command(argc, argv);
97}
98
99void
101{
102 PktRx = p;
103}
104
105void
106UWMPhy_modem::recv(Packet *p)
107{
108
110 LOG_LEVEL_INFO, "UWMPHY_MODEM", "RECV::RECV_PKT_FROM_UPPER_LAYER");
111
112 int modemStatus = pmDriver->getStatus();
113 if (t >= -1 && t < _MTBL) {
114 if (t == _MTBL - 1) {
115 free(p);
117 "UWMPHY_MODEM",
118 "NO_SPACE_LEFT_ON_MODEM_QUEUE");
119 } else {
120 modemTxBuff[++t] = p;
121 }
122 } else {
124 "UWMPHY_MODEM",
125 "MODEM_QUEUE_INDEX_OUT_OF_BOUNDS");
126 }
127
128 while (modemStatus == MODEM_CFG) {
129 modemStatus = check_modem();
130 }
131
132 while (modemStatus == MODEM_RESET) {
133 modemStatus = check_modem();
134 }
135 if (modemStatus == MODEM_RX || modemStatus == MODEM_IDLE_RX) {
137 free(PktRx);
138 PktRx = NULL;
139 modemStatus = MODEM_IDLE;
141 "UWMPHY_MODEM",
142 "RX_PACKET_NOT_DELIVERED_CONCURRENT_PACKET");
143 }
144 if (modemStatus == MODEM_IDLE) {
145 hdr_mac *mach = HDR_MAC(modemTxBuff[0]);
146 hdr_uwal *uwalh = HDR_UWAL(modemTxBuff[0]);
147 std::string payload_string;
148 payload_string.assign(uwalh->binPkt(), uwalh->binPktLength());
149 pmDriver->updateTx(mach->macDA(), payload_string);
151 if (pmDriver->getStatus() != MODEM_TX) {
152 endTx(popTxBuff());
153 }
154 }
155}
156
157void
159 CheckTimer *pcheckTmr_, UWMdriver *pmDriver_, DropTimer *pDropTimer_)
160{
161 pcheckTmr = pcheckTmr_;
162 pmDriver = pmDriver_;
163 pDropTimer = pDropTimer_;
164}
165
166void
168{
169 std::stringstream str("");
170 if (log_suffix) {
171 str << "MODEM_log_" << ID << "_" << log_suffix << "_" << getEpoch();
172 } else {
173 str << "MODEM_log_" << ID << getEpoch();
174 }
175 str >> logFile;
176
177 outLog.open(logFile.c_str());
178 if (!outLog) {
179 std::cout << "WARNING: Opening error ( " << strerror(errno)
180 << " ). It was not possible to create " << logFile.c_str()
181 << "\n";
182 }
183 outLog.close();
184 pmDriver->setID(ID);
185 if (SetModemID == 1) {
186 pmDriver->setModemID(true);
187 } else {
188 pmDriver->setModemID(false);
189 }
190 pmDriver->start();
191 pcheckTmr->resched(period);
192}
193
194void
196{
197 pmDriver->stop();
198 pcheckTmr->force_cancel();
199}
200
201void
202UWMPhy_modem::startTx(Packet *p)
203{
205 LOG_LEVEL_DEBUG, "UWMPHY_MODEM", "CHECK_MODEM::START_TX");
206
207 pmDriver->modemTx();
208}
209
210void
211UWMPhy_modem::endTx(Packet *p)
212{
213 Phy2MacEndTx(p);
215 LOG_LEVEL_DEBUG, "UWMPHY_MODEM", "CHECK_MODEM::END_TX");
216 free(p);
217}
218
219void
220UWMPhy_modem::startRx(Packet *p)
221{
222
224 LOG_LEVEL_DEBUG, "UWMPHY_MODEM", "CHECK_MODEM::START_RX");
225 Phy2MacStartRx(p);
226}
227
228void
229UWMPhy_modem::endRx(Packet *p)
230{
231
232 std::string str = pmDriver->getRxPayload();
233 unsigned char *buf = (unsigned char *) str.c_str();
234 Packet *p_rx = Packet::alloc();
235 hdr_uwal *uwalh = HDR_UWAL(p_rx);
236 uwalh->binPktLength() = str.length();
237 memset(uwalh->binPkt(), 0, sizeof(uwalh->binPkt()));
238 memcpy(uwalh->binPkt(), buf, uwalh->binPktLength());
239 this->updatePktRx(p_rx);
241 LOG_LEVEL_DEBUG, "UWMPHY_MODEM", "CHECK_MODEM::END_RX");
242 sendUp(PktRx);
243 PktRx = NULL;
245}
246
247Packet *
249{
250
251 Packet *temp = modemTxBuff[0];
252 for (int i = 0; i < t; i++) {
253 modemTxBuff[i] = modemTxBuff[i + 1];
254 }
255
256 t--;
257 return temp;
258}
259
260void
261CheckTimer::expire(Event *e)
262{
263 pmModem->check_modem();
264
265 resched(pmModem->getPeriod());
266}
267
268void
269DropTimer::expire(Event *e)
270{
272}
#define _MTBL
Defintion of the maximum length of the transmission buffer to store \ packets at the interface level.
Header of the main class that implements the general interface between ns2/NS-Miracle and real acoust...
The class used by UwModem to handle simulator's event expirations; it is exploited to schedule the re...
Definition uwmodem.h:326
UwModem * pmModem
Pointer to an UwModem object.
Definition uwmodem.h:353
virtual void expire(Event *e)
Method to handle the expiration of a given event.
Definition uwmodem.cpp:181
Packet * popTxBuff()
Method to pop the oldest packet in the TX buffer or to delete after a tx or to drop it.
unsigned long int getEpoch()
Calculate the epoch of the event.
virtual void startRx(Packet *)
Method to start a packet reception.
std::ofstream outLog
output strem to print into a disk-file log messages.
int debug_
Flag to enable debug mode (i.e., printing of debug messages) if set to 1.
char * log_suffix
Possibility to insert a log suffix.
CheckTimer * pcheckTmr
Pointer to an object to schedule the "check-modem" events.
UWMdriver * pmDriver
Pointer to an object to drive the modem operations.
virtual modem_state_t check_modem()=0
Modem checker.
virtual void recv(Packet *)
Method to handle the reception of packets arriving from the upper layers of the network simulator.
double period
Checking period of the modem's buffer.
virtual int command(int, const char *const *)
Method to map tcl commands into c++ methods.
int SetModemID
Flag to indicate if the interface has to force the modem to have the ID indicated in the tcl script.
Packet * modemTxBuff[_MTBL]
Transmission buffer to store packets that cannot be sent immediately because the real acoustic modem ...
Packet * PktRx
Transmission buffer's index; it must be in {-1, 0, 1, ..., _MTBL-1}.
virtual void endRx(Packet *)
Method to end a packet reception.
std::string logFile
Name of the disk-file where to write the interface's log messages.
int loglevel_
Log level on file, from ERROR (0) to DEBUG (2) in UWMPhy_modem::logFile.
std::string pToDevice
A string containing the path to the device to be connected with the network simulator.
virtual void startTx(Packet *)
Method to send to an UWMdriver object the packet to be transmitted, see UWMdriver::modemTx().
virtual void endTx(Packet *)
Method to end a packet transmission.
void setConnections(CheckTimer *, UWMdriver *, DropTimer *)
Link connector.
void updatePktRx(Packet *)
Method to update the value of the pointer to the last received packet.
The class needed by UWMPhy_modem to handle the different transmissions cases and corresponding protoc...
void updateTx(int, std::string)
Method to update the values of both UWMdriver::payload_tx and UWMdriver::dest.
modem_state_t getStatus()
Method to return the modem's status.
virtual void stop()=0
Driver stopper.
void printOnLog(log_level_t log_level, std::string module, std::string message)
virtual void modemTx()=0
Method to notify to the driver that there is a packet to be sent via modem.
virtual void start()=0
Driver starter.
void resetModemStatus()
Method to reset the modem status.
std::string getRxPayload()
Method to access to the payload of the last packet acoustically received.
void setID(int ID_)
Method to change the modem ID.
#define HDR_UWAL(p)
Definition hdr-uwal.h:43
hdr_uwal describes the packet header used by Uwal objects.
Definition hdr-uwal.h:52
char * binPkt()
Return to the binPkt_ array pointer.
Definition hdr-uwal.h:141
uint32_t & binPktLength()
Reference to the binPktLength_ variable.
Definition hdr-uwal.h:150